Add initial values support - values to feed the shared database on first startup.
[platform/core/security/key-manager.git] / src / manager / initial-values / InitialValueHandler.cpp
1 /*
2  *  Copyright (c) 2000 - 2015 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        InitialValueHandler.cpp
18  * @author      Maciej Karpiuk (m.karpiuk2@samsung.com)
19  * @version     1.0
20  * @brief       InitialValueHandler class implementation.
21  */
22
23 #include <sstream>
24 #include <algorithm>
25 #include <memory>
26 #include <exception>
27 #include <InitialValueHandler.h>
28 #include <EncodingType.h>
29 #include <ckm/ckm-type.h>
30
31 namespace
32 {
33 const char * const XML_ATTR_NAME        = "name";
34 const char * const XML_ATTR_PASSWORD    = "password";
35 const char * const XML_ATTR_EXPORTABLE  = "exportable";
36 }
37
38 namespace CKM {
39 namespace InitialValues {
40
41 void InitialValueHandler::Start(const XML::Parser::Attributes &attr)
42 {
43     // get name
44     if(attr.find(XML_ATTR_NAME) != attr.end())
45         m_name = Alias(attr.at(XML_ATTR_NAME));
46
47     // get password
48     if(attr.find(XML_ATTR_PASSWORD) != attr.end())
49         m_password = Password(attr.at(XML_ATTR_PASSWORD).c_str());
50
51     // get exportable
52     if(attr.find(XML_ATTR_EXPORTABLE) != attr.end())
53     {
54         std::string flagVal = attr.at(XML_ATTR_EXPORTABLE);
55         std::transform(flagVal.begin(), flagVal.end(), flagVal.begin(), ::tolower);
56         std::istringstream is(flagVal);
57         is >> std::boolalpha >> m_exportable;
58     }
59 }
60
61 void InitialValueHandler::Characters(const std::string &)
62 {
63     throw std::runtime_error("error: value handler detected raw data outside data-specific tag");
64 }
65
66 void InitialValueHandler::End()
67 {
68     if(m_bufferHandler)
69     {
70         // save data
71         Policy policy(m_password, m_exportable);
72         int ec = m_db_logic.verifyAndSaveDataHelper(
73                 Credentials(CKMLogic::SYSTEM_DB_UID, LABEL_SYSTEM_DB),
74                 m_name,
75                 LABEL_SYSTEM_DB,
76                 m_bufferHandler->getData(),
77                 getDataType(),
78                 PolicySerializable(policy));
79         if(CKM_API_SUCCESS == ec)
80         {
81             // save permissions
82             for(const auto & permission : m_permissions)
83             {
84                 ec = m_db_logic.setPermissionHelper(
85                         Credentials(CKMLogic::SYSTEM_DB_UID, LABEL_SYSTEM_DB),
86                         m_name,
87                         LABEL_SYSTEM_DB,
88                         permission->getAccessor(),
89                         Permission::READ);
90                 if(CKM_API_SUCCESS != ec)
91                     LogError("Saving permission to: " << m_name << " with params: accessor("<<permission->getAccessor()<<") failed, code: " << ec);
92             }
93         }
94         else
95             LogError("Saving type: " << getDataType() << " with params: name("<<m_name<<"), exportable("<<m_exportable<<") failed, code: " << ec);
96     }
97     else
98         LogError("Invalid data with name: " << m_name << ", reason: no key data!");
99 }
100
101 BufferHandler::BufferHandlerPtr InitialValueHandler::CreateBufferHandler(EncodingType type)
102 {
103     m_bufferHandler = std::make_shared<BufferHandler>(type);
104     return m_bufferHandler;
105 }
106
107 PermissionHandler::PermissionHandlerPtr InitialValueHandler::CreatePermissionHandler()
108 {
109     PermissionHandler::PermissionHandlerPtr newPermission = std::make_shared<PermissionHandler>();
110     m_permissions.push_back(newPermission);
111     return newPermission;
112 }
113
114 }
115 }