2 * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 * @file InitialValuesFile.cpp
18 * @author Maciej Karpiuk (m.karpiuk2@samsung.com)
20 * @brief InitialValuesFile class implementation.
24 #include <InitialValuesFile.h>
25 #include <InitialValueHandler.h>
26 #include <BufferHandler.h>
27 #include <EncodingType.h>
28 #include <KeyHandler.h>
29 #include <CertHandler.h>
30 #include <DataHandler.h>
31 #include <EncodingType.h>
32 #include <dpl/log/log.h>
35 const int XML_CURRENT_VERSION = 1;
36 const char * const XML_TAG_INITIAL_VALUES = "InitialValues";
37 const char * const XML_TAG_KEY = "Key";
38 const char * const XML_TAG_DATA = "Data";
39 const char * const XML_TAG_CERT = "Cert";
40 const char * const XML_TAG_PEM = "PEM";
41 const char * const XML_TAG_DER = "DER";
42 const char * const XML_TAG_ASCII = "ASCII";
43 const char * const XML_TAG_BASE64 = "Base64";
44 const char * const XML_TAG_PERMISSION = "Permission";
45 const char * const XML_ATTR_VERSION = "version";
49 namespace InitialValues {
51 InitialValuesFile::InitialValuesFile(const std::string &XML_filename, CKMLogic & db_logic)
52 : m_parser(XML_filename), m_db_logic(db_logic),
53 m_header(std::make_shared<HeaderHandler>(*this))
55 m_parser.RegisterErrorCb(InitialValuesFile::Error);
56 m_parser.RegisterElementCb(XML_TAG_INITIAL_VALUES,
57 [this]() -> XML::Parser::ElementHandlerPtr
61 [this](const XML::Parser::ElementHandlerPtr &) {});
64 void InitialValuesFile::registerElementListeners()
66 m_parser.RegisterElementCb(XML_TAG_KEY,
67 [this]() -> XML::Parser::ElementHandlerPtr
69 return GetObjectHandler(ObjectType::KEY);
71 [this](const XML::Parser::ElementHandlerPtr &)
73 ReleaseObjectHandler(ObjectType::KEY);
75 m_parser.RegisterElementCb(XML_TAG_CERT,
76 [this]() -> XML::Parser::ElementHandlerPtr
78 return GetObjectHandler(ObjectType::CERT);
80 [this](const XML::Parser::ElementHandlerPtr &)
82 ReleaseObjectHandler(ObjectType::CERT);
84 m_parser.RegisterElementCb(XML_TAG_DATA,
85 [this]() -> XML::Parser::ElementHandlerPtr
87 return GetObjectHandler(ObjectType::DATA);
89 [this](const XML::Parser::ElementHandlerPtr &)
91 ReleaseObjectHandler(ObjectType::DATA);
94 m_parser.RegisterElementCb(XML_TAG_PEM,
95 [this]() -> XML::Parser::ElementHandlerPtr
97 return GetBufferHandler(EncodingType::PEM);
99 [this](const XML::Parser::ElementHandlerPtr &)
101 ReleaseBufferHandler(EncodingType::PEM);
103 m_parser.RegisterElementCb(XML_TAG_DER,
104 [this]() -> XML::Parser::ElementHandlerPtr
106 return GetBufferHandler(EncodingType::DER);
108 [this](const XML::Parser::ElementHandlerPtr &)
110 ReleaseBufferHandler(EncodingType::DER);
112 m_parser.RegisterElementCb(XML_TAG_ASCII,
113 [this]() -> XML::Parser::ElementHandlerPtr
115 return GetBufferHandler(EncodingType::ASCII);
117 [this](const XML::Parser::ElementHandlerPtr &)
119 ReleaseBufferHandler(EncodingType::ASCII);
121 m_parser.RegisterElementCb(XML_TAG_BASE64,
122 [this]() -> XML::Parser::ElementHandlerPtr
124 return GetBufferHandler(EncodingType::BASE64);
126 [this](const XML::Parser::ElementHandlerPtr &)
128 ReleaseBufferHandler(EncodingType::BASE64);
130 m_parser.RegisterElementCb(XML_TAG_PERMISSION,
131 [this]() -> XML::Parser::ElementHandlerPtr
133 return GetPermissionHandler();
135 [this](const XML::Parser::ElementHandlerPtr &)
137 ReleasePermissionHandler();
141 void InitialValuesFile::Error(const XML::Parser::ErrorType errorType,
142 const std::string & log_msg)
146 case XML::Parser::VALIDATION_ERROR:
147 LogWarning("validating error: " << log_msg);
149 case XML::Parser::PARSE_WARNING:
150 LogWarning("parsing warning: " << log_msg);
152 case XML::Parser::PARSE_ERROR:
153 LogWarning("parsing error: " << log_msg);
158 int InitialValuesFile::Validate(const std::string &XSD_file)
160 return m_parser.Validate(XSD_file);
163 int InitialValuesFile::Parse()
165 int ec = m_parser.Parse();
166 if(!m_header || !m_header->isCorrectVersion()) {
167 LogError("bypassing XML file: " << m_filename << " - wrong file version!");
168 ec = XML::Parser::ERROR_INVALID_VERSION;
173 XML::Parser::ElementHandlerPtr InitialValuesFile::GetObjectHandler(ObjectType type)
178 m_currentHandler = std::make_shared<KeyHandler>(m_db_logic);
182 m_currentHandler = std::make_shared<CertHandler>(m_db_logic);
186 m_currentHandler = std::make_shared<DataHandler>(m_db_logic);
190 m_currentHandler.reset();
194 return m_currentHandler;
197 void InitialValuesFile::ReleaseObjectHandler(ObjectType /*type*/)
199 m_currentHandler.reset();
205 XML::Parser::ElementHandlerPtr InitialValuesFile::GetBufferHandler(EncodingType type)
207 if( !m_currentHandler )
208 return XML::Parser::ElementHandlerPtr();
210 return m_currentHandler->CreateBufferHandler(type);
212 void InitialValuesFile::ReleaseBufferHandler(EncodingType /*type*/)
217 XML::Parser::ElementHandlerPtr InitialValuesFile::GetPermissionHandler()
219 if( !m_currentHandler )
220 return XML::Parser::ElementHandlerPtr();
222 return m_currentHandler->CreatePermissionHandler();
224 void InitialValuesFile::ReleasePermissionHandler()
229 InitialValuesFile::HeaderHandler::HeaderHandler(InitialValuesFile & parent)
230 : m_version(-1), m_parent(parent) {}
231 void InitialValuesFile::HeaderHandler::Start(const XML::Parser::Attributes & attr)
234 if(attr.find(XML_ATTR_VERSION) != attr.end())
236 m_version = atoi(attr.at(XML_ATTR_VERSION).c_str());
238 if(isCorrectVersion())
239 m_parent.registerElementListeners();
242 bool InitialValuesFile::HeaderHandler::isCorrectVersion() const {
243 return m_version == XML_CURRENT_VERSION;