467e2892789bfbf25b0690d703c4394e28b1890c
[platform/core/security/cert-svc.git] / 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
23 #include <vcore/CertificateConfigReader.h>
24
25 #include <dpl/assert.h>
26
27 #include <cstdlib>
28
29 namespace {
30 const std::string XML_EMPTY_NAMESPACE = "";
31
32 const std::string TOKEN_CERTIFICATE_SET = "CertificateSet";
33 const std::string TOKEN_CERTIFICATE_DOMAIN = "CertificateDomain";
34 const std::string TOKEN_FINGERPRINT_SHA1 = "FingerprintSHA1";
35
36 const std::string TOKEN_ATTR_NAME = "name";
37 const std::string TOKEN_VALUE_TIZEN_DEVELOPER = "tizen-developer";
38 const std::string TOKEN_VALUE_TIZEN_TEST = "tizen-test";
39 const std::string TOKEN_VALUE_TIZEN_VERIFY = "tizen-verify";
40 const std::string TOKEN_VALUE_TIZEN_STORE = "tizen-store";
41 const std::string TOKEN_VALUE_VISIBILITY_PUBLIC = "tizen-public";
42 const std::string TOKEN_VALUE_VISIBILITY_PARTNER = "tizen-partner";
43 const std::string TOKEN_VALUE_VISIBILITY_PARTNER_OPERATOR = "tizen-partner-operator";
44 const std::string TOKEN_VALUE_VISIBILITY_PARTNER_MANUFACTURER = "tizen-partner-manufacturer";
45 const std::string TOKEN_VALUE_VISIBILITY_PLATFORM = "tizen-platform";
46
47 int hexCharToInt(char c)
48 {
49     if (c >= 'a' && c <= 'f') {
50         return 10 + static_cast<int>(c) - 'a';
51     }
52     if (c >= 'A' && c <= 'F') {
53         return 10 + static_cast<int>(c) - 'A';
54     }
55     if (c >= '0' && c <= '9') {
56         return static_cast<int>(c) - '0';
57     }
58     return c;
59 }
60 } // anonymous namespace
61
62 namespace ValidationCore {
63 CertificateConfigReader::CertificateConfigReader()
64   : m_certificateDomain(0)
65   , m_parserSchema(this)
66 {
67     m_parserSchema.addBeginTagCallback(
68         TOKEN_CERTIFICATE_SET,
69         XML_EMPTY_NAMESPACE,
70         &CertificateConfigReader::blankFunction);
71
72     m_parserSchema.addBeginTagCallback(
73         TOKEN_CERTIFICATE_DOMAIN,
74         XML_EMPTY_NAMESPACE,
75         &CertificateConfigReader::tokenCertificateDomain);
76
77     m_parserSchema.addBeginTagCallback(
78         TOKEN_FINGERPRINT_SHA1,
79         XML_EMPTY_NAMESPACE,
80         &CertificateConfigReader::blankFunction);
81
82     m_parserSchema.addEndTagCallback(
83         TOKEN_CERTIFICATE_SET,
84         XML_EMPTY_NAMESPACE,
85         &CertificateConfigReader::blankFunction);
86
87     m_parserSchema.addEndTagCallback(
88         TOKEN_CERTIFICATE_DOMAIN,
89         XML_EMPTY_NAMESPACE,
90         &CertificateConfigReader::blankFunction);
91
92     m_parserSchema.addEndTagCallback(
93         TOKEN_FINGERPRINT_SHA1,
94         XML_EMPTY_NAMESPACE,
95         &CertificateConfigReader::tokenEndFingerprintSHA1);
96 }
97
98 void CertificateConfigReader::initialize(
99     const std::string &file,
100     const std::string &scheme)
101 {
102     m_parserSchema.initialize(file, true, SaxReader::VALIDATION_XMLSCHEME, scheme);
103 }
104
105 void CertificateConfigReader::read(CertificateIdentifier &identificator)
106 {
107     m_parserSchema.read(identificator);
108 }
109
110 void CertificateConfigReader::blankFunction(CertificateIdentifier &)
111 {
112 }
113
114 void CertificateConfigReader::tokenCertificateDomain(CertificateIdentifier &)
115 {
116     std::string name = m_parserSchema.getReader().attribute(TOKEN_ATTR_NAME);
117
118     if (name.empty()) {
119         VcoreThrowMsg(CertificateConfigReader::Exception::InvalidFile,
120                       "Invalid fingerprint file. Domain name is mandatory");
121     } else if (name == TOKEN_VALUE_TIZEN_DEVELOPER) {
122         m_certificateDomain = CertStoreId::TIZEN_DEVELOPER;
123     } else if (name == TOKEN_VALUE_TIZEN_TEST) {
124         m_certificateDomain = CertStoreId::TIZEN_TEST;
125     } else if (name == TOKEN_VALUE_TIZEN_VERIFY) {
126         m_certificateDomain = CertStoreId::TIZEN_VERIFY;
127     } else if (name == TOKEN_VALUE_TIZEN_STORE) {
128         m_certificateDomain = CertStoreId::TIZEN_STORE;
129     } else if (name == TOKEN_VALUE_VISIBILITY_PUBLIC) {
130         m_certificateDomain = CertStoreId::VIS_PUBLIC;
131     } else if (name == TOKEN_VALUE_VISIBILITY_PARTNER) {
132         m_certificateDomain = CertStoreId::VIS_PARTNER;
133     } else if (name == TOKEN_VALUE_VISIBILITY_PARTNER_OPERATOR) {
134         m_certificateDomain = CertStoreId::VIS_PARTNER_OPERATOR;
135     } else if (name == TOKEN_VALUE_VISIBILITY_PARTNER_MANUFACTURER) {
136         m_certificateDomain = CertStoreId::VIS_PARTNER_MANUFACTURER;
137     } else if (name == TOKEN_VALUE_VISIBILITY_PLATFORM) {
138         m_certificateDomain = CertStoreId::VIS_PLATFORM;
139     } else {
140         m_certificateDomain = 0;
141     }
142 }
143
144 void CertificateConfigReader::tokenEndFingerprintSHA1(
145         CertificateIdentifier &identificator)
146 {
147     std::string text = m_parserSchema.getText();
148     text += ":"; // add guard at the end of fingerprint
149     Certificate::Fingerprint fingerprint;
150     int s = 0;
151     int byteDescLen = 0;
152     for (size_t i = 0; i < text.size(); ++i) {
153         if (isxdigit(text[i])) {
154             s <<= 4;
155             s += hexCharToInt(text[i]);
156             byteDescLen++;
157             if (byteDescLen > 2) {
158                 Assert(0 && "Unsupported fingerprint format in xml file.");
159             }
160         } else if (text[i] == ':') {
161             fingerprint.push_back(static_cast<unsigned char>(s));
162             s = 0;
163             byteDescLen = 0;
164         } else {
165             Assert(0 && "Unussported fingerprint format in xml file.");
166         }
167     }
168
169     identificator.add(fingerprint, m_certificateDomain);
170 }
171 } // namespace ValidationCore