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(const RawBuffer &data, KeyType type, const std::string &password)
61 : m_type(KeyType::KEY_NONE)
66 std::string passtmp(password);
68 if (!passtmp.empty()) {
69 pass = const_cast<char *>(passtmp.c_str());
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);
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);
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);
97 BIO *bio = BIO_new(BIO_s_mem());
99 if (type == KeyType::KEY_RSA_PUBLIC) {
100 size = i2d_RSAPublicKey_bio(bio, rsa);
102 size = i2d_RSAPrivateKey_bio(bio, rsa);
107 BIO_read(bio, m_key.data(), m_key.size());
113 //void KeyImpl::Serialize(IStream &stream) const {
114 // Serialization::Serialize(stream, static_cast<int>(m_type));
115 // Serialization::Serialize(stream, m_key);
118 KeyImpl::~KeyImpl(){}