Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 2 | /* |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 4 | * |
| 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 "private-key.hpp" |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 23 | #include "base64-decode.hpp" |
Davide Pesavento | e178989 | 2017-02-26 15:50:52 -0500 | [diff] [blame] | 24 | #include "base64-encode.hpp" |
| 25 | #include "buffer-source.hpp" |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 26 | #include "stream-sink.hpp" |
Davide Pesavento | e178989 | 2017-02-26 15:50:52 -0500 | [diff] [blame] | 27 | #include "stream-source.hpp" |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 28 | #include "../detail/openssl-helper.hpp" |
| 29 | #include "../key-params.hpp" |
Davide Pesavento | e178989 | 2017-02-26 15:50:52 -0500 | [diff] [blame] | 30 | #include "../../encoding/buffer-stream.hpp" |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 32 | #include <boost/lexical_cast.hpp> |
Davide Pesavento | e178989 | 2017-02-26 15:50:52 -0500 | [diff] [blame] | 33 | #include <cstring> |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 34 | |
| 35 | #define ENSURE_PRIVATE_KEY_LOADED(key) \ |
| 36 | do { \ |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 37 | if ((key) == nullptr) \ |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 38 | BOOST_THROW_EXCEPTION(Error("Private key has not been loaded yet")); \ |
| 39 | } while (false) |
| 40 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 41 | #define ENSURE_PRIVATE_KEY_NOT_LOADED(key) \ |
| 42 | do { \ |
| 43 | if ((key) != nullptr) \ |
| 44 | BOOST_THROW_EXCEPTION(Error("Private key has already been loaded")); \ |
| 45 | } while (false) |
| 46 | |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 47 | namespace ndn { |
| 48 | namespace security { |
| 49 | namespace transform { |
| 50 | |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 51 | static void |
| 52 | opensslInitAlgorithms() |
| 53 | { |
| 54 | #if OPENSSL_VERSION_NUMBER < 0x1010000fL |
| 55 | static bool isInitialized = false; |
| 56 | if (!isInitialized) { |
| 57 | OpenSSL_add_all_algorithms(); |
| 58 | isInitialized = true; |
| 59 | } |
| 60 | #endif // OPENSSL_VERSION_NUMBER < 0x1010000fL |
| 61 | } |
| 62 | |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 63 | class PrivateKey::Impl |
| 64 | { |
| 65 | public: |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 66 | Impl() noexcept |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 67 | : key(nullptr) |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | ~Impl() |
| 72 | { |
| 73 | EVP_PKEY_free(key); |
| 74 | } |
| 75 | |
| 76 | public: |
| 77 | EVP_PKEY* key; |
| 78 | }; |
| 79 | |
| 80 | PrivateKey::PrivateKey() |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 81 | : m_impl(make_unique<Impl>()) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 82 | { |
| 83 | } |
| 84 | |
| 85 | PrivateKey::~PrivateKey() = default; |
| 86 | |
| 87 | void |
| 88 | PrivateKey::loadPkcs1(const uint8_t* buf, size_t size) |
| 89 | { |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 90 | ENSURE_PRIVATE_KEY_NOT_LOADED(m_impl->key); |
| 91 | opensslInitAlgorithms(); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 92 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 93 | if (d2i_AutoPrivateKey(&m_impl->key, &buf, static_cast<long>(size)) == nullptr) |
| 94 | BOOST_THROW_EXCEPTION(Error("Failed to load private key")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void |
| 98 | PrivateKey::loadPkcs1(std::istream& is) |
| 99 | { |
| 100 | OBufferStream os; |
| 101 | streamSource(is) >> streamSink(os); |
| 102 | this->loadPkcs1(os.buf()->buf(), os.buf()->size()); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | PrivateKey::loadPkcs1Base64(const uint8_t* buf, size_t size) |
| 107 | { |
| 108 | OBufferStream os; |
| 109 | bufferSource(buf, size) >> base64Decode() >> streamSink(os); |
| 110 | this->loadPkcs1(os.buf()->buf(), os.buf()->size()); |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | PrivateKey::loadPkcs1Base64(std::istream& is) |
| 115 | { |
| 116 | OBufferStream os; |
| 117 | streamSource(is) >> base64Decode() >> streamSink(os); |
| 118 | this->loadPkcs1(os.buf()->buf(), os.buf()->size()); |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | PrivateKey::loadPkcs8(const uint8_t* buf, size_t size, const char* pw, size_t pwLen) |
| 123 | { |
| 124 | BOOST_ASSERT(std::strlen(pw) == pwLen); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 125 | ENSURE_PRIVATE_KEY_NOT_LOADED(m_impl->key); |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 126 | opensslInitAlgorithms(); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 127 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 128 | detail::Bio membio(BIO_s_mem()); |
| 129 | if (!membio.write(buf, size)) |
| 130 | BOOST_THROW_EXCEPTION(Error("Failed to copy buffer")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 131 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 132 | if (d2i_PKCS8PrivateKey_bio(membio, &m_impl->key, nullptr, const_cast<char*>(pw)) == nullptr) |
| 133 | BOOST_THROW_EXCEPTION(Error("Failed to load private key")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static inline int |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 137 | passwordCallbackWrapper(char* buf, int size, int rwflag, void* u) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 138 | { |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 139 | BOOST_ASSERT(size >= 0); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 140 | auto cb = reinterpret_cast<PrivateKey::PasswordCallback*>(u); |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 141 | return (*cb)(buf, static_cast<size_t>(size), rwflag); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void |
| 145 | PrivateKey::loadPkcs8(const uint8_t* buf, size_t size, PasswordCallback pwCallback) |
| 146 | { |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 147 | ENSURE_PRIVATE_KEY_NOT_LOADED(m_impl->key); |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 148 | opensslInitAlgorithms(); |
| 149 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 150 | detail::Bio membio(BIO_s_mem()); |
| 151 | if (!membio.write(buf, size)) |
| 152 | BOOST_THROW_EXCEPTION(Error("Failed to copy buffer")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 153 | |
| 154 | if (pwCallback) |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 155 | m_impl->key = d2i_PKCS8PrivateKey_bio(membio, nullptr, &passwordCallbackWrapper, &pwCallback); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 156 | else |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 157 | m_impl->key = d2i_PKCS8PrivateKey_bio(membio, nullptr, nullptr, nullptr); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 158 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 159 | if (m_impl->key == nullptr) |
| 160 | BOOST_THROW_EXCEPTION(Error("Failed to load private key")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void |
| 164 | PrivateKey::loadPkcs8(std::istream& is, const char* pw, size_t pwLen) |
| 165 | { |
| 166 | OBufferStream os; |
| 167 | streamSource(is) >> streamSink(os); |
| 168 | this->loadPkcs8(os.buf()->buf(), os.buf()->size(), pw, pwLen); |
| 169 | } |
| 170 | |
| 171 | void |
| 172 | PrivateKey::loadPkcs8(std::istream& is, PasswordCallback pwCallback) |
| 173 | { |
| 174 | OBufferStream os; |
| 175 | streamSource(is) >> streamSink(os); |
| 176 | this->loadPkcs8(os.buf()->buf(), os.buf()->size(), pwCallback); |
| 177 | } |
| 178 | |
| 179 | void |
| 180 | PrivateKey::loadPkcs8Base64(const uint8_t* buf, size_t size, const char* pw, size_t pwLen) |
| 181 | { |
| 182 | OBufferStream os; |
| 183 | bufferSource(buf, size) >> base64Decode() >> streamSink(os); |
| 184 | this->loadPkcs8(os.buf()->buf(), os.buf()->size(), pw, pwLen); |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | PrivateKey::loadPkcs8Base64(const uint8_t* buf, size_t size, PasswordCallback pwCallback) |
| 189 | { |
| 190 | OBufferStream os; |
| 191 | bufferSource(buf, size) >> base64Decode() >> streamSink(os); |
| 192 | this->loadPkcs8(os.buf()->buf(), os.buf()->size(), pwCallback); |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | PrivateKey::loadPkcs8Base64(std::istream& is, const char* pw, size_t pwLen) |
| 197 | { |
| 198 | OBufferStream os; |
| 199 | streamSource(is) >> base64Decode() >> streamSink(os); |
| 200 | this->loadPkcs8(os.buf()->buf(), os.buf()->size(), pw, pwLen); |
| 201 | } |
| 202 | |
| 203 | void |
| 204 | PrivateKey::loadPkcs8Base64(std::istream& is, PasswordCallback pwCallback) |
| 205 | { |
| 206 | OBufferStream os; |
| 207 | streamSource(is) >> base64Decode() >> streamSink(os); |
| 208 | this->loadPkcs8(os.buf()->buf(), os.buf()->size(), pwCallback); |
| 209 | } |
| 210 | |
| 211 | void |
| 212 | PrivateKey::savePkcs1(std::ostream& os) const |
| 213 | { |
| 214 | bufferSource(*this->toPkcs1()) >> streamSink(os); |
| 215 | } |
| 216 | |
| 217 | void |
| 218 | PrivateKey::savePkcs1Base64(std::ostream& os) const |
| 219 | { |
| 220 | bufferSource(*this->toPkcs1()) >> base64Encode() >> streamSink(os); |
| 221 | } |
| 222 | |
| 223 | void |
| 224 | PrivateKey::savePkcs8(std::ostream& os, const char* pw, size_t pwLen) const |
| 225 | { |
| 226 | bufferSource(*this->toPkcs8(pw, pwLen)) >> streamSink(os); |
| 227 | } |
| 228 | |
| 229 | void |
| 230 | PrivateKey::savePkcs8(std::ostream& os, PasswordCallback pwCallback) const |
| 231 | { |
| 232 | bufferSource(*this->toPkcs8(pwCallback)) >> streamSink(os); |
| 233 | } |
| 234 | |
| 235 | void |
| 236 | PrivateKey::savePkcs8Base64(std::ostream& os, const char* pw, size_t pwLen) const |
| 237 | { |
| 238 | bufferSource(*this->toPkcs8(pw, pwLen)) >> base64Encode() >> streamSink(os); |
| 239 | } |
| 240 | |
| 241 | void |
| 242 | PrivateKey::savePkcs8Base64(std::ostream& os, PasswordCallback pwCallback) const |
| 243 | { |
| 244 | bufferSource(*this->toPkcs8(pwCallback)) >> base64Encode() >> streamSink(os); |
| 245 | } |
| 246 | |
| 247 | ConstBufferPtr |
| 248 | PrivateKey::derivePublicKey() const |
| 249 | { |
| 250 | ENSURE_PRIVATE_KEY_LOADED(m_impl->key); |
| 251 | |
| 252 | uint8_t* pkcs8 = nullptr; |
| 253 | int len = i2d_PUBKEY(m_impl->key, &pkcs8); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 254 | if (len < 0) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 255 | BOOST_THROW_EXCEPTION(Error("Failed to derive public key")); |
| 256 | |
| 257 | auto result = make_shared<Buffer>(pkcs8, len); |
| 258 | OPENSSL_free(pkcs8); |
| 259 | |
| 260 | return result; |
| 261 | } |
| 262 | |
| 263 | ConstBufferPtr |
| 264 | PrivateKey::decrypt(const uint8_t* cipherText, size_t cipherLen) const |
| 265 | { |
| 266 | ENSURE_PRIVATE_KEY_LOADED(m_impl->key); |
| 267 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 268 | int keyType = |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 269 | #if OPENSSL_VERSION_NUMBER < 0x1010000fL |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 270 | EVP_PKEY_type(m_impl->key->type); |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 271 | #else |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 272 | EVP_PKEY_base_id(m_impl->key); |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 273 | #endif // OPENSSL_VERSION_NUMBER < 0x1010000fL |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 274 | |
| 275 | switch (keyType) { |
| 276 | case EVP_PKEY_NONE: |
| 277 | BOOST_THROW_EXCEPTION(Error("Failed to determine key type")); |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 278 | case EVP_PKEY_RSA: |
| 279 | return rsaDecrypt(cipherText, cipherLen); |
| 280 | default: |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 281 | BOOST_THROW_EXCEPTION(Error("Decryption is not supported for key type " + to_string(keyType))); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | void* |
| 286 | PrivateKey::getEvpPkey() const |
| 287 | { |
| 288 | return m_impl->key; |
| 289 | } |
| 290 | |
| 291 | ConstBufferPtr |
| 292 | PrivateKey::toPkcs1() const |
| 293 | { |
| 294 | ENSURE_PRIVATE_KEY_LOADED(m_impl->key); |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 295 | opensslInitAlgorithms(); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 296 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 297 | detail::Bio membio(BIO_s_mem()); |
| 298 | if (!i2d_PrivateKey_bio(membio, m_impl->key)) |
| 299 | BOOST_THROW_EXCEPTION(Error("Cannot convert key to PKCS #1 format")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 300 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 301 | auto buffer = make_shared<Buffer>(BIO_pending(membio)); |
| 302 | membio.read(buffer->buf(), buffer->size()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 303 | |
| 304 | return buffer; |
| 305 | } |
| 306 | |
| 307 | ConstBufferPtr |
| 308 | PrivateKey::toPkcs8(const char* pw, size_t pwLen) const |
| 309 | { |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 310 | BOOST_ASSERT(std::strlen(pw) == pwLen); |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 311 | ENSURE_PRIVATE_KEY_LOADED(m_impl->key); |
| 312 | opensslInitAlgorithms(); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 313 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 314 | detail::Bio membio(BIO_s_mem()); |
| 315 | if (!i2d_PKCS8PrivateKey_bio(membio, m_impl->key, EVP_des_cbc(), nullptr, 0, |
| 316 | nullptr, const_cast<char*>(pw))) |
| 317 | BOOST_THROW_EXCEPTION(Error("Cannot convert key to PKCS #8 format")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 318 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 319 | auto buffer = make_shared<Buffer>(BIO_pending(membio)); |
| 320 | membio.read(buffer->buf(), buffer->size()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 321 | |
| 322 | return buffer; |
| 323 | } |
| 324 | |
| 325 | ConstBufferPtr |
| 326 | PrivateKey::toPkcs8(PasswordCallback pwCallback) const |
| 327 | { |
| 328 | ENSURE_PRIVATE_KEY_LOADED(m_impl->key); |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 329 | opensslInitAlgorithms(); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 330 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 331 | detail::Bio membio(BIO_s_mem()); |
| 332 | if (!i2d_PKCS8PrivateKey_bio(membio, m_impl->key, EVP_des_cbc(), nullptr, 0, |
| 333 | &passwordCallbackWrapper, &pwCallback)) |
| 334 | BOOST_THROW_EXCEPTION(Error("Cannot convert key to PKCS #8 format")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 335 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 336 | auto buffer = make_shared<Buffer>(BIO_pending(membio)); |
| 337 | membio.read(buffer->buf(), buffer->size()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 338 | |
| 339 | return buffer; |
| 340 | } |
| 341 | |
| 342 | ConstBufferPtr |
| 343 | PrivateKey::rsaDecrypt(const uint8_t* cipherText, size_t cipherLen) const |
| 344 | { |
| 345 | detail::EvpPkeyCtx ctx(m_impl->key); |
| 346 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 347 | if (EVP_PKEY_decrypt_init(ctx) <= 0) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 348 | BOOST_THROW_EXCEPTION(Error("Failed to initialize decryption context")); |
| 349 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 350 | if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING) <= 0) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 351 | BOOST_THROW_EXCEPTION(Error("Failed to set padding")); |
| 352 | |
| 353 | size_t outlen = 0; |
| 354 | // Determine buffer length |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 355 | if (EVP_PKEY_decrypt(ctx, nullptr, &outlen, cipherText, cipherLen) <= 0) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 356 | BOOST_THROW_EXCEPTION(Error("Failed to estimate output length")); |
| 357 | |
| 358 | auto out = make_shared<Buffer>(outlen); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 359 | if (EVP_PKEY_decrypt(ctx, out->buf(), &outlen, cipherText, cipherLen) <= 0) |
| 360 | BOOST_THROW_EXCEPTION(Error("Failed to decrypt ciphertext")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 361 | |
| 362 | out->resize(outlen); |
| 363 | return out; |
| 364 | } |
| 365 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 366 | unique_ptr<PrivateKey> |
| 367 | PrivateKey::generateRsaKey(uint32_t keySize) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 368 | { |
| 369 | detail::EvpPkeyCtx kctx(EVP_PKEY_RSA); |
| 370 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 371 | if (EVP_PKEY_keygen_init(kctx) <= 0) |
| 372 | BOOST_THROW_EXCEPTION(PrivateKey::Error("Failed to initialize RSA keygen context")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 373 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 374 | if (EVP_PKEY_CTX_set_rsa_keygen_bits(kctx, static_cast<int>(keySize)) <= 0) |
| 375 | BOOST_THROW_EXCEPTION(PrivateKey::Error("Failed to set RSA key length")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 376 | |
| 377 | auto privateKey = make_unique<PrivateKey>(); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 378 | if (EVP_PKEY_keygen(kctx, &privateKey->m_impl->key) <= 0) |
| 379 | BOOST_THROW_EXCEPTION(PrivateKey::Error("Failed to generate RSA key")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 380 | |
| 381 | return privateKey; |
| 382 | } |
| 383 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 384 | unique_ptr<PrivateKey> |
| 385 | PrivateKey::generateEcKey(uint32_t keySize) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 386 | { |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 387 | detail::EvpPkeyCtx pctx(EVP_PKEY_EC); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 388 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 389 | if (EVP_PKEY_paramgen_init(pctx) <= 0) |
| 390 | BOOST_THROW_EXCEPTION(PrivateKey::Error("Failed to initialize EC paramgen context")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 391 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 392 | int ret; |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 393 | switch (keySize) { |
| 394 | case 256: |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 395 | ret = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_X9_62_prime256v1); // same as secp256r1 |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 396 | break; |
| 397 | case 384: |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 398 | ret = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_secp384r1); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 399 | break; |
| 400 | default: |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 401 | BOOST_THROW_EXCEPTION(PrivateKey::Error("Unsupported EC key length")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 402 | } |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 403 | if (ret <= 0) |
| 404 | BOOST_THROW_EXCEPTION(PrivateKey::Error("Failed to set EC curve")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 405 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 406 | Impl params; |
| 407 | if (EVP_PKEY_paramgen(pctx, ¶ms.key) <= 0) |
| 408 | BOOST_THROW_EXCEPTION(PrivateKey::Error("Failed to generate EC parameters")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 409 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 410 | detail::EvpPkeyCtx kctx(params.key); |
| 411 | if (EVP_PKEY_keygen_init(kctx) <= 0) |
| 412 | BOOST_THROW_EXCEPTION(PrivateKey::Error("Failed to initialize EC keygen context")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 413 | |
| 414 | auto privateKey = make_unique<PrivateKey>(); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 415 | if (EVP_PKEY_keygen(kctx, &privateKey->m_impl->key) <= 0) |
| 416 | BOOST_THROW_EXCEPTION(PrivateKey::Error("Failed to generate EC key")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 417 | |
| 418 | return privateKey; |
| 419 | } |
| 420 | |
| 421 | unique_ptr<PrivateKey> |
| 422 | generatePrivateKey(const KeyParams& keyParams) |
| 423 | { |
| 424 | switch (keyParams.getKeyType()) { |
| 425 | case KeyType::RSA: { |
| 426 | const RsaKeyParams& rsaParams = static_cast<const RsaKeyParams&>(keyParams); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 427 | return PrivateKey::generateRsaKey(rsaParams.getKeySize()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 428 | } |
| 429 | case KeyType::EC: { |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 430 | const EcKeyParams& ecParams = static_cast<const EcKeyParams&>(keyParams); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 431 | return PrivateKey::generateEcKey(ecParams.getKeySize()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 432 | } |
| 433 | default: |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 434 | BOOST_THROW_EXCEPTION(std::invalid_argument("Unsupported asymmetric key type " + |
| 435 | boost::lexical_cast<std::string>(keyParams.getKeyType()))); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
| 439 | } // namespace transform |
| 440 | } // namespace security |
| 441 | } // namespace ndn |