Replace RawData with RawBuffer.
[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(IStream &stream) {
61 //    int type;
62 //    Deserialization::Deserialize(stream, type);
63 //    Deserialization::Deserialize(stream, m_key);
64 //    m_type = static_cast<KeyType>(type);
65 //}
66
67 KeyImpl::KeyImpl(const RawBuffer &data, KeyType type, const RawBuffer &password)
68   : m_type(KeyType::KEY_NONE)
69 {
70     int size = 0;
71     RSA *rsa = NULL;
72     char *pass = NULL;
73     RawBuffer passtmp(password);
74
75     if (!passtmp.empty()) {
76         passtmp.push_back(0);
77         pass = reinterpret_cast<char*>(passtmp.data());
78     }
79
80     if (data[0] == PEM_FIRST_CHAR && type == KeyType::KEY_RSA_PUBLIC) {
81         BIO *bio = BIO_new(BIO_s_mem());
82         BIO_write(bio, data.data(), data.size());
83         rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, pass);
84         BIO_free_all(bio);
85     } else if (data[0] == PEM_FIRST_CHAR && type == KeyType::KEY_RSA_PRIVATE) {
86         BIO *bio = BIO_new(BIO_s_mem());
87         BIO_write(bio, data.data(), data.size());
88         rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, pass);
89         BIO_free_all(bio);
90     } else if (type == KeyType::KEY_RSA_PUBLIC) {
91         const unsigned char *p = (const unsigned char*)data.data();
92         rsa = d2i_RSA_PUBKEY(NULL, &p, data.size());
93     } else if (type == KeyType::KEY_RSA_PRIVATE) {
94         BIO *bio = BIO_new(BIO_s_mem());
95         BIO_write(bio, data.data(), data.size());
96         rsa = d2i_RSAPrivateKey_bio(bio, NULL);
97         BIO_free_all(bio);
98     } else {
99         return;
100     }
101
102     if (!rsa)
103         return;
104
105     BIO *bio = BIO_new(BIO_s_mem());
106
107     if (type == KeyType::KEY_RSA_PUBLIC) {
108         size = i2d_RSAPublicKey_bio(bio, rsa);
109     } else {
110         size = i2d_RSAPrivateKey_bio(bio, rsa);
111     }
112
113     if (size > 0) {
114         m_key.resize(size);
115         BIO_read(bio, m_key.data(), m_key.size());
116         m_type = type;
117     }
118     BIO_free_all(bio);
119 }
120
121 //void KeyImpl::Serialize(IStream &stream) const {
122 //    Serialization::Serialize(stream, static_cast<int>(m_type));
123 //    Serialization::Serialize(stream, m_key);
124 //}
125
126 KeyImpl::~KeyImpl(){}
127
128 } // namespace CKM
129