tizen beta release
[framework/web/wrt-commons.git] / modules / vcore / src / vcore / CertificateConfigReader.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief
21  */
22 #include "CertificateConfigReader.h"
23
24 #include <cstdlib>
25
26 #include <dpl/assert.h>
27
28 namespace {
29 const std::string XML_EMPTY_NAMESPACE = "";
30
31 const std::string TOKEN_CERTIFICATE_SET = "CertificateSet";
32 const std::string TOKEN_CERTIFICATE_DOMAIN = "CertificateDomain";
33 const std::string TOKEN_FINGERPRINT_SHA1 = "FingerprintSHA1";
34
35 const std::string TOKEN_ATTR_NAME = "name";
36 const std::string TOKEN_VALUE_WAC_ROOT = "wacroot";
37 const std::string TOKEN_VALUE_WAC_PUBLISHER = "wacpublisher";
38 const std::string TOKEN_VALUE_WAC_MEMBER = "wacmember";
39 const std::string TOKEN_VALUE_DEVELOPER = "developer";
40
41 int hexCharToInt(char c)
42 {
43     if (c >= 'a' && c <= 'f') {
44         return 10 + static_cast<int>(c) - 'a';
45     }
46     if (c >= 'A' && c <= 'F') {
47         return 10 + static_cast<int>(c) - 'A';
48     }
49     if (c >= '0' && c <= '9') {
50         return static_cast<int>(c) - '0';
51     }
52     return c;
53 }
54 } // anonymous namespace
55
56 namespace ValidationCore {
57 CertificateConfigReader::CertificateConfigReader() :
58     m_certificateDomain(0),
59     m_parserSchema(this)
60 {
61     m_parserSchema.addBeginTagCallback(
62         TOKEN_CERTIFICATE_SET,
63         XML_EMPTY_NAMESPACE,
64         &CertificateConfigReader::blankFunction);
65
66     m_parserSchema.addBeginTagCallback(
67         TOKEN_CERTIFICATE_DOMAIN,
68         XML_EMPTY_NAMESPACE,
69         &CertificateConfigReader::tokenCertificateDomain);
70
71     m_parserSchema.addBeginTagCallback(
72         TOKEN_FINGERPRINT_SHA1,
73         XML_EMPTY_NAMESPACE,
74         &CertificateConfigReader::blankFunction);
75
76     m_parserSchema.addEndTagCallback(
77         TOKEN_CERTIFICATE_SET,
78         XML_EMPTY_NAMESPACE,
79         &CertificateConfigReader::blankFunction);
80
81     m_parserSchema.addEndTagCallback(
82         TOKEN_CERTIFICATE_DOMAIN,
83         XML_EMPTY_NAMESPACE,
84         &CertificateConfigReader::blankFunction);
85
86     m_parserSchema.addEndTagCallback(
87         TOKEN_FINGERPRINT_SHA1,
88         XML_EMPTY_NAMESPACE,
89         &CertificateConfigReader::tokenEndFingerprintSHA1);
90 }
91
92 void CertificateConfigReader::tokenCertificateDomain(CertificateIdentifier &)
93 {
94     std::string name = m_parserSchema.getReader().
95             attribute(TOKEN_ATTR_NAME, SaxReader::THROW_DISABLE);
96
97     if (name.empty()) {
98         LogWarning("Invalid fingerprint file. Domain name is mandatory");
99         ThrowMsg(Exception::InvalidFile,
100                  "Invalid fingerprint file. Domain name is mandatory");
101     } else if (name == TOKEN_VALUE_DEVELOPER) {
102         m_certificateDomain = CertStoreId::DEVELOPER;
103     } else if (name == TOKEN_VALUE_WAC_ROOT) {
104         m_certificateDomain = CertStoreId::WAC_ROOT;
105     } else if (name == TOKEN_VALUE_WAC_PUBLISHER) {
106         m_certificateDomain = CertStoreId::WAC_PUBLISHER;
107     } else if (name == TOKEN_VALUE_WAC_MEMBER) {
108         m_certificateDomain = CertStoreId::WAC_MEMBER;
109     }
110 }
111
112 void CertificateConfigReader::tokenEndFingerprintSHA1(
113         CertificateIdentifier &identificator)
114 {
115     std::string text = m_parserSchema.getText();
116     text += ":"; // add guard at the end of fingerprint
117     Certificate::Fingerprint fingerprint;
118     int s = 0;
119     int byteDescLen = 0;
120     for (size_t i = 0; i < text.size(); ++i) {
121         if (isxdigit(text[i])) {
122             s <<= 4;
123             s += hexCharToInt(text[i]);
124             byteDescLen++;
125             if (byteDescLen > 2) {
126                 Assert(0 && "Unsupported fingerprint format in xml file.");
127             }
128         } else if (text[i] == ':') {
129             fingerprint.push_back(static_cast<unsigned char>(s));
130             s = 0;
131             byteDescLen = 0;
132         } else {
133             Assert(0 && "Unussported fingerprint format in xml file.");
134         }
135     }
136     identificator.add(fingerprint, m_certificateDomain);
137 }
138 } // namespace ValidationCore