Add initial values support - values to feed the shared database on first startup.
[platform/core/security/key-manager.git] / src / manager / initial-values / KeyHandler.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        KeyHandler.cpp
18  * @author      Maciej Karpiuk (m.karpiuk2@samsung.com)
19  * @version     1.0
20  * @brief       KeyHandler class implementation.
21  */
22
23 #include <string>
24 #include <algorithm>
25 #include <parser.h>
26 #include <KeyHandler.h>
27 #include <InitialValueHandler.h>
28 #include <ckm/ckm-type.h>
29
30 namespace
31 {
32 const char * const XML_ATTR_TYPE    = "type";
33 const char * const XML_ATTR_TYPE_VAL_RSA_PRV    =   "RSA_PRV";
34 const char * const XML_ATTR_TYPE_VAL_RSA_PUB    =   "RSA_PUB";
35 const char * const XML_ATTR_TYPE_VAL_DSA_PRV    =   "DSA_PRV";
36 const char * const XML_ATTR_TYPE_VAL_DSA_PUB    =   "DSA_PUB";
37 const char * const XML_ATTR_TYPE_VAL_ECDSA_PRV  =   "ECDSA_PRV";
38 const char * const XML_ATTR_TYPE_VAL_ECDSA_PUB  =   "ECDSA_PUB";
39 const char * const XML_ATTR_TYPE_VAL_AES        =   "AES";
40 }
41
42 namespace CKM {
43 namespace InitialValues {
44
45 KeyHandler::~KeyHandler() {}
46
47 void KeyHandler::Start(const XML::Parser::Attributes &attr)
48 {
49     InitialValueHandler::Start(attr);
50
51     // get key type
52     if(attr.find(XML_ATTR_TYPE) != attr.end())
53         m_keyType = KeyHandler::parseType(attr.at(XML_ATTR_TYPE));
54 }
55
56 KeyType KeyHandler::parseType(const std::string & typeStr)
57 {
58     if     (typeStr == XML_ATTR_TYPE_VAL_RSA_PRV)      return KeyType::KEY_RSA_PRIVATE;
59     else if(typeStr == XML_ATTR_TYPE_VAL_RSA_PUB)      return KeyType::KEY_RSA_PUBLIC;
60     else if(typeStr == XML_ATTR_TYPE_VAL_DSA_PRV)      return KeyType::KEY_DSA_PRIVATE;
61     else if(typeStr == XML_ATTR_TYPE_VAL_DSA_PUB)      return KeyType::KEY_DSA_PUBLIC;
62     else if(typeStr == XML_ATTR_TYPE_VAL_ECDSA_PRV)    return KeyType::KEY_ECDSA_PRIVATE;
63     else if(typeStr == XML_ATTR_TYPE_VAL_ECDSA_PUB)    return KeyType::KEY_ECDSA_PUBLIC;
64     else if(typeStr == XML_ATTR_TYPE_VAL_AES)          return KeyType::KEY_AES;
65     else // should not happen
66         throw std::runtime_error("error: invalid value discovered as key type");
67 }
68
69 DataType KeyHandler::getDataType() const
70 {
71     return DataType(m_keyType);
72 }
73
74 }
75 }