684049a481b11792094f3993778a87c76e641506
[platform/core/security/key-manager.git] / src / manager / client / client-key-impl.cpp
1 /* Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
2  *
3  *  Licensed under the Apache License, Version 2.0 (the "License");
4  *  you may not use this file except in compliance with the License.
5  *  You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  *  Unless required by applicable law or agreed to in writing, software
10  *  distributed under the License is distributed on an "AS IS" BASIS,
11  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  *  See the License for the specific language governing permissions and
13  *  limitations under the License
14  *
15  *
16  * @file        client-echo.cpp
17  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
18  * @version     1.0
19  * @brief       Key implementation.
20  */
21 #include <openssl/bio.h>
22 #include <openssl/pem.h>
23
24 #include <client-key-impl.h>
25
26 namespace {
27
28 const char PEM_FIRST_CHAR = '-';
29
30 } // namespace anonymous
31
32 namespace CKM {
33
34 KeyImpl::KeyImpl()
35   : m_type(KeyType::KEY_NONE)
36 {}
37
38 KeyImpl::KeyImpl(const KeyImpl &second)
39   : m_type(second.m_type)
40   , m_key(second.m_key)
41 {}
42
43 KeyImpl::KeyImpl(KeyImpl &&second)
44   : m_type(second.m_type)
45   , m_key(std::move(second.m_key))
46 {}
47
48 KeyImpl& KeyImpl::operator=(const KeyImpl &second) {
49     m_type = second.m_type;
50     m_key = second.m_key;
51     return *this;
52 }
53
54 KeyImpl& KeyImpl::operator=(KeyImpl &&second) {
55     m_type = std::move(second.m_type);
56     m_key = std::move(second.m_key);
57     return *this;
58 }
59
60 KeyImpl::KeyImpl(const RawBuffer &data, KeyType type, const std::string &password)
61   : m_type(KeyType::KEY_NONE)
62 {
63     int size = 0;
64     RSA *rsa = NULL;
65     char *pass = NULL;
66     std::string passtmp(password);
67
68     if (!passtmp.empty()) {
69         pass = const_cast<char *>(passtmp.c_str());
70     }
71
72     if (data[0] == PEM_FIRST_CHAR && type == KeyType::KEY_RSA_PUBLIC) {
73         BIO *bio = BIO_new(BIO_s_mem());
74         BIO_write(bio, data.data(), data.size());
75         rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, pass);
76         BIO_free_all(bio);
77     } else if (data[0] == PEM_FIRST_CHAR && type == KeyType::KEY_RSA_PRIVATE) {
78         BIO *bio = BIO_new(BIO_s_mem());
79         BIO_write(bio, data.data(), data.size());
80         rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, pass);
81         BIO_free_all(bio);
82     } else if (type == KeyType::KEY_RSA_PUBLIC) {
83         const unsigned char *p = (const unsigned char*)data.data();
84         rsa = d2i_RSA_PUBKEY(NULL, &p, data.size());
85     } else if (type == KeyType::KEY_RSA_PRIVATE) {
86         BIO *bio = BIO_new(BIO_s_mem());
87         BIO_write(bio, data.data(), data.size());
88         rsa = d2i_RSAPrivateKey_bio(bio, NULL);
89         BIO_free_all(bio);
90     } else {
91         return;
92     }
93
94     if (!rsa)
95         return;
96
97     BIO *bio = BIO_new(BIO_s_mem());
98
99     if (type == KeyType::KEY_RSA_PUBLIC) {
100         size = i2d_RSAPublicKey_bio(bio, rsa);
101     } else {
102         size = i2d_RSAPrivateKey_bio(bio, rsa);
103     }
104
105     if (size > 0) {
106         m_key.resize(size);
107         BIO_read(bio, m_key.data(), m_key.size());
108         m_type = type;
109     }
110     BIO_free_all(bio);
111 }
112
113 //void KeyImpl::Serialize(IStream &stream) const {
114 //    Serialization::Serialize(stream, static_cast<int>(m_type));
115 //    Serialization::Serialize(stream, m_key);
116 //}
117
118 KeyImpl::~KeyImpl(){}
119
120 } // namespace CKM
121