blob: 16dd3d1161c2c62cb17f9a4e5b379587490f7535 [file] [log] [blame]
Yingdi Yu202a2e92015-07-12 16:49:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventof45fa212017-09-14 17:23:56 -04002/*
Muktadir R Chowdhury80fba6c2017-05-05 15:30:15 -05003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yu202a2e92015-07-12 16:49:25 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "public-key.hpp"
Yingdi Yu202a2e92015-07-12 16:49:25 -070023#include "base64-decode.hpp"
Davide Pesavento1c31a712017-09-15 00:52:03 -040024#include "base64-encode.hpp"
25#include "buffer-source.hpp"
Yingdi Yu202a2e92015-07-12 16:49:25 -070026#include "stream-sink.hpp"
Davide Pesavento1c31a712017-09-15 00:52:03 -040027#include "stream-source.hpp"
Yingdi Yu202a2e92015-07-12 16:49:25 -070028#include "../detail/openssl-helper.hpp"
Davide Pesavento1c31a712017-09-15 00:52:03 -040029#include "../../encoding/buffer-stream.hpp"
Yingdi Yu202a2e92015-07-12 16:49:25 -070030
31#define ENSURE_PUBLIC_KEY_LOADED(key) \
32 do { \
Davide Pesavento1c31a712017-09-15 00:52:03 -040033 if ((key) == nullptr) \
Yingdi Yu202a2e92015-07-12 16:49:25 -070034 BOOST_THROW_EXCEPTION(Error("Public key has not been loaded yet")); \
35 } while (false)
36
Davide Pesavento1c31a712017-09-15 00:52:03 -040037#define ENSURE_PUBLIC_KEY_NOT_LOADED(key) \
38 do { \
39 if ((key) != nullptr) \
40 BOOST_THROW_EXCEPTION(Error("Public key has already been loaded")); \
41 } while (false)
42
Yingdi Yu202a2e92015-07-12 16:49:25 -070043namespace ndn {
44namespace security {
45namespace transform {
46
47class PublicKey::Impl
48{
49public:
Davide Pesavento1c31a712017-09-15 00:52:03 -040050 Impl() noexcept
Yingdi Yu202a2e92015-07-12 16:49:25 -070051 : key(nullptr)
52 {
53 }
54
55 ~Impl()
56 {
57 EVP_PKEY_free(key);
58 }
59
60public:
61 EVP_PKEY* key;
62};
63
64PublicKey::PublicKey()
Davide Pesavento1c31a712017-09-15 00:52:03 -040065 : m_impl(make_unique<Impl>())
Yingdi Yu202a2e92015-07-12 16:49:25 -070066{
67}
68
69PublicKey::~PublicKey() = default;
70
71KeyType
72PublicKey::getKeyType() const
73{
Davide Pesavento06f1bdf2017-09-16 18:59:15 -040074 if (!m_impl->key)
75 return KeyType::NONE;
Yingdi Yu202a2e92015-07-12 16:49:25 -070076
Davide Pesaventoc21979d2017-09-16 14:52:17 -040077 switch (detail::getEvpPkeyType(m_impl->key)) {
Yingdi Yu202a2e92015-07-12 16:49:25 -070078 case EVP_PKEY_RSA:
79 return KeyType::RSA;
80 case EVP_PKEY_EC:
81 return KeyType::EC;
82 default:
Davide Pesaventoc21979d2017-09-16 14:52:17 -040083 return KeyType::NONE;
Yingdi Yu202a2e92015-07-12 16:49:25 -070084 }
85}
86
87void
88PublicKey::loadPkcs8(const uint8_t* buf, size_t size)
89{
Davide Pesavento1c31a712017-09-15 00:52:03 -040090 ENSURE_PUBLIC_KEY_NOT_LOADED(m_impl->key);
Yingdi Yu202a2e92015-07-12 16:49:25 -070091
Davide Pesavento1c31a712017-09-15 00:52:03 -040092 if (d2i_PUBKEY(&m_impl->key, &buf, static_cast<long>(size)) == nullptr)
93 BOOST_THROW_EXCEPTION(Error("Failed to load public key"));
Yingdi Yu202a2e92015-07-12 16:49:25 -070094}
95
96void
97PublicKey::loadPkcs8(std::istream& is)
98{
99 OBufferStream os;
Davide Pesavento1c31a712017-09-15 00:52:03 -0400100 streamSource(is) >> streamSink(os);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700101 this->loadPkcs8(os.buf()->buf(), os.buf()->size());
102}
103
104void
105PublicKey::loadPkcs8Base64(const uint8_t* buf, size_t size)
106{
107 OBufferStream os;
Davide Pesavento1c31a712017-09-15 00:52:03 -0400108 bufferSource(buf, size) >> base64Decode() >> streamSink(os);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700109 this->loadPkcs8(os.buf()->buf(), os.buf()->size());
110}
111
112void
113PublicKey::loadPkcs8Base64(std::istream& is)
114{
115 OBufferStream os;
Davide Pesavento1c31a712017-09-15 00:52:03 -0400116 streamSource(is) >> base64Decode() >> streamSink(os);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700117 this->loadPkcs8(os.buf()->buf(), os.buf()->size());
118}
119
120void
121PublicKey::savePkcs8(std::ostream& os) const
122{
Yingdi Yu202a2e92015-07-12 16:49:25 -0700123 bufferSource(*this->toPkcs8()) >> streamSink(os);
124}
125
126void
127PublicKey::savePkcs8Base64(std::ostream& os) const
128{
Yingdi Yu202a2e92015-07-12 16:49:25 -0700129 bufferSource(*this->toPkcs8()) >> base64Encode() >> streamSink(os);
130}
131
132ConstBufferPtr
133PublicKey::encrypt(const uint8_t* plainText, size_t plainLen) const
134{
135 ENSURE_PUBLIC_KEY_LOADED(m_impl->key);
136
Davide Pesaventoc21979d2017-09-16 14:52:17 -0400137 int keyType = detail::getEvpPkeyType(m_impl->key);
Davide Pesaventof45fa212017-09-14 17:23:56 -0400138 switch (keyType) {
139 case EVP_PKEY_NONE:
140 BOOST_THROW_EXCEPTION(Error("Failed to determine key type"));
141 case EVP_PKEY_RSA:
142 return rsaEncrypt(plainText, plainLen);
143 default:
144 BOOST_THROW_EXCEPTION(Error("Encryption is not supported for key type " + to_string(keyType)));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700145 }
146}
147
148void*
149PublicKey::getEvpPkey() const
150{
151 return m_impl->key;
152}
153
154ConstBufferPtr
155PublicKey::toPkcs8() const
156{
157 ENSURE_PUBLIC_KEY_LOADED(m_impl->key);
158
159 uint8_t* pkcs8 = nullptr;
160 int len = i2d_PUBKEY(m_impl->key, &pkcs8);
Davide Pesavento1c31a712017-09-15 00:52:03 -0400161 if (len < 0)
162 BOOST_THROW_EXCEPTION(Error("Cannot convert key to PKCS #8 format"));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700163
164 auto buffer = make_shared<Buffer>(pkcs8, len);
165 OPENSSL_free(pkcs8);
166
167 return buffer;
168}
169
170ConstBufferPtr
171PublicKey::rsaEncrypt(const uint8_t* plainText, size_t plainLen) const
172{
173 detail::EvpPkeyCtx ctx(m_impl->key);
174
Davide Pesaventof45fa212017-09-14 17:23:56 -0400175 if (EVP_PKEY_encrypt_init(ctx) <= 0)
Yingdi Yu202a2e92015-07-12 16:49:25 -0700176 BOOST_THROW_EXCEPTION(Error("Failed to initialize encryption context"));
177
Davide Pesaventof45fa212017-09-14 17:23:56 -0400178 if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING) <= 0)
Yingdi Yu202a2e92015-07-12 16:49:25 -0700179 BOOST_THROW_EXCEPTION(Error("Failed to set padding"));
180
181 size_t outlen = 0;
182 // Determine buffer length
Davide Pesaventof45fa212017-09-14 17:23:56 -0400183 if (EVP_PKEY_encrypt(ctx, nullptr, &outlen, plainText, plainLen) <= 0)
Yingdi Yu202a2e92015-07-12 16:49:25 -0700184 BOOST_THROW_EXCEPTION(Error("Failed to estimate output length"));
185
186 auto out = make_shared<Buffer>(outlen);
Davide Pesaventof45fa212017-09-14 17:23:56 -0400187 if (EVP_PKEY_encrypt(ctx, out->buf(), &outlen, plainText, plainLen) <= 0)
Muktadir R Chowdhury80fba6c2017-05-05 15:30:15 -0500188 BOOST_THROW_EXCEPTION(Error("Failed to encrypt plaintext"));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700189
190 out->resize(outlen);
191 return out;
192}
193
194} // namespace transform
195} // namespace security
196} // namespace ndn