1 /* Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * @file client-echo.cpp
17 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19 * @brief Key implementation.
21 #include <openssl/bio.h>
22 #include <openssl/pem.h>
24 #include <client-key-impl.h>
28 const char PEM_FIRST_CHAR = '-';
30 } // namespace anonymous
35 : m_type(KeyType::KEY_NONE)
38 KeyImpl::KeyImpl(const KeyImpl &second)
39 : m_type(second.m_type)
43 KeyImpl::KeyImpl(KeyImpl &&second)
44 : m_type(second.m_type)
45 , m_key(std::move(second.m_key))
48 KeyImpl& KeyImpl::operator=(const KeyImpl &second) {
49 m_type = second.m_type;
54 KeyImpl& KeyImpl::operator=(KeyImpl &&second) {
55 m_type = std::move(second.m_type);
56 m_key = std::move(second.m_key);
60 //KeyImpl::KeyImpl(IStream &stream) {
62 // Deserialization::Deserialize(stream, type);
63 // Deserialization::Deserialize(stream, m_key);
64 // m_type = static_cast<KeyType>(type);
67 KeyImpl::KeyImpl(const RawBuffer &data, KeyType type, const RawBuffer &password)
68 : m_type(KeyType::KEY_NONE)
73 RawBuffer passtmp(password);
75 if (!passtmp.empty()) {
77 pass = reinterpret_cast<char*>(passtmp.data());
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);
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);
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);
105 BIO *bio = BIO_new(BIO_s_mem());
107 if (type == KeyType::KEY_RSA_PUBLIC) {
108 size = i2d_RSAPublicKey_bio(bio, rsa);
110 size = i2d_RSAPrivateKey_bio(bio, rsa);
115 BIO_read(bio, m_key.data(), m_key.size());
121 //void KeyImpl::Serialize(IStream &stream) const {
122 // Serialization::Serialize(stream, static_cast<int>(m_type));
123 // Serialization::Serialize(stream, m_key);
126 KeyImpl::~KeyImpl(){}