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 | c21979d | 2017-09-16 14:52:17 -0400 | [diff] [blame] | 268 | int keyType = detail::getEvpPkeyType(m_impl->key); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 269 | switch (keyType) { |
| 270 | case EVP_PKEY_NONE: |
| 271 | BOOST_THROW_EXCEPTION(Error("Failed to determine key type")); |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 272 | case EVP_PKEY_RSA: |
| 273 | return rsaDecrypt(cipherText, cipherLen); |
| 274 | default: |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 275 | 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] | 276 | } |
| 277 | } |
| 278 | |
| 279 | void* |
| 280 | PrivateKey::getEvpPkey() const |
| 281 | { |
| 282 | return m_impl->key; |
| 283 | } |
| 284 | |
| 285 | ConstBufferPtr |
| 286 | PrivateKey::toPkcs1() const |
| 287 | { |
| 288 | ENSURE_PRIVATE_KEY_LOADED(m_impl->key); |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 289 | opensslInitAlgorithms(); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 290 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 291 | detail::Bio membio(BIO_s_mem()); |
| 292 | if (!i2d_PrivateKey_bio(membio, m_impl->key)) |
| 293 | BOOST_THROW_EXCEPTION(Error("Cannot convert key to PKCS #1 format")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 294 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 295 | auto buffer = make_shared<Buffer>(BIO_pending(membio)); |
| 296 | membio.read(buffer->buf(), buffer->size()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 297 | |
| 298 | return buffer; |
| 299 | } |
| 300 | |
| 301 | ConstBufferPtr |
| 302 | PrivateKey::toPkcs8(const char* pw, size_t pwLen) const |
| 303 | { |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 304 | BOOST_ASSERT(std::strlen(pw) == pwLen); |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 305 | ENSURE_PRIVATE_KEY_LOADED(m_impl->key); |
| 306 | opensslInitAlgorithms(); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 307 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 308 | detail::Bio membio(BIO_s_mem()); |
Davide Pesavento | cafa402 | 2017-09-15 23:20:20 -0400 | [diff] [blame] | 309 | if (!i2d_PKCS8PrivateKey_bio(membio, m_impl->key, EVP_des_ede3_cbc(), nullptr, 0, |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 310 | nullptr, const_cast<char*>(pw))) |
| 311 | BOOST_THROW_EXCEPTION(Error("Cannot convert key to PKCS #8 format")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 312 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 313 | auto buffer = make_shared<Buffer>(BIO_pending(membio)); |
| 314 | membio.read(buffer->buf(), buffer->size()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 315 | |
| 316 | return buffer; |
| 317 | } |
| 318 | |
| 319 | ConstBufferPtr |
| 320 | PrivateKey::toPkcs8(PasswordCallback pwCallback) const |
| 321 | { |
| 322 | ENSURE_PRIVATE_KEY_LOADED(m_impl->key); |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 323 | opensslInitAlgorithms(); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 324 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 325 | detail::Bio membio(BIO_s_mem()); |
Davide Pesavento | cafa402 | 2017-09-15 23:20:20 -0400 | [diff] [blame] | 326 | if (!i2d_PKCS8PrivateKey_bio(membio, m_impl->key, EVP_des_ede3_cbc(), nullptr, 0, |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 327 | &passwordCallbackWrapper, &pwCallback)) |
| 328 | BOOST_THROW_EXCEPTION(Error("Cannot convert key to PKCS #8 format")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 329 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 330 | auto buffer = make_shared<Buffer>(BIO_pending(membio)); |
| 331 | membio.read(buffer->buf(), buffer->size()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 332 | |
| 333 | return buffer; |
| 334 | } |
| 335 | |
| 336 | ConstBufferPtr |
| 337 | PrivateKey::rsaDecrypt(const uint8_t* cipherText, size_t cipherLen) const |
| 338 | { |
| 339 | detail::EvpPkeyCtx ctx(m_impl->key); |
| 340 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 341 | if (EVP_PKEY_decrypt_init(ctx) <= 0) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 342 | BOOST_THROW_EXCEPTION(Error("Failed to initialize decryption context")); |
| 343 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 344 | 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] | 345 | BOOST_THROW_EXCEPTION(Error("Failed to set padding")); |
| 346 | |
| 347 | size_t outlen = 0; |
| 348 | // Determine buffer length |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 349 | if (EVP_PKEY_decrypt(ctx, nullptr, &outlen, cipherText, cipherLen) <= 0) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 350 | BOOST_THROW_EXCEPTION(Error("Failed to estimate output length")); |
| 351 | |
| 352 | auto out = make_shared<Buffer>(outlen); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 353 | if (EVP_PKEY_decrypt(ctx, out->buf(), &outlen, cipherText, cipherLen) <= 0) |
| 354 | BOOST_THROW_EXCEPTION(Error("Failed to decrypt ciphertext")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 355 | |
| 356 | out->resize(outlen); |
| 357 | return out; |
| 358 | } |
| 359 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 360 | unique_ptr<PrivateKey> |
| 361 | PrivateKey::generateRsaKey(uint32_t keySize) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 362 | { |
| 363 | detail::EvpPkeyCtx kctx(EVP_PKEY_RSA); |
| 364 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 365 | if (EVP_PKEY_keygen_init(kctx) <= 0) |
| 366 | BOOST_THROW_EXCEPTION(PrivateKey::Error("Failed to initialize RSA keygen context")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 367 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 368 | if (EVP_PKEY_CTX_set_rsa_keygen_bits(kctx, static_cast<int>(keySize)) <= 0) |
| 369 | BOOST_THROW_EXCEPTION(PrivateKey::Error("Failed to set RSA key length")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 370 | |
| 371 | auto privateKey = make_unique<PrivateKey>(); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 372 | if (EVP_PKEY_keygen(kctx, &privateKey->m_impl->key) <= 0) |
| 373 | BOOST_THROW_EXCEPTION(PrivateKey::Error("Failed to generate RSA key")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 374 | |
| 375 | return privateKey; |
| 376 | } |
| 377 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 378 | unique_ptr<PrivateKey> |
| 379 | PrivateKey::generateEcKey(uint32_t keySize) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 380 | { |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 381 | detail::EvpPkeyCtx pctx(EVP_PKEY_EC); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 382 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 383 | if (EVP_PKEY_paramgen_init(pctx) <= 0) |
| 384 | BOOST_THROW_EXCEPTION(PrivateKey::Error("Failed to initialize EC paramgen context")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 385 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 386 | int ret; |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 387 | switch (keySize) { |
| 388 | case 256: |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 389 | 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] | 390 | break; |
| 391 | case 384: |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 392 | ret = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_secp384r1); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 393 | break; |
| 394 | default: |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 395 | BOOST_THROW_EXCEPTION(PrivateKey::Error("Unsupported EC key length")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 396 | } |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 397 | if (ret <= 0) |
| 398 | BOOST_THROW_EXCEPTION(PrivateKey::Error("Failed to set EC curve")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 399 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 400 | Impl params; |
| 401 | if (EVP_PKEY_paramgen(pctx, ¶ms.key) <= 0) |
| 402 | BOOST_THROW_EXCEPTION(PrivateKey::Error("Failed to generate EC parameters")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 403 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 404 | detail::EvpPkeyCtx kctx(params.key); |
| 405 | if (EVP_PKEY_keygen_init(kctx) <= 0) |
| 406 | BOOST_THROW_EXCEPTION(PrivateKey::Error("Failed to initialize EC keygen context")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 407 | |
| 408 | auto privateKey = make_unique<PrivateKey>(); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 409 | if (EVP_PKEY_keygen(kctx, &privateKey->m_impl->key) <= 0) |
| 410 | BOOST_THROW_EXCEPTION(PrivateKey::Error("Failed to generate EC key")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 411 | |
| 412 | return privateKey; |
| 413 | } |
| 414 | |
| 415 | unique_ptr<PrivateKey> |
| 416 | generatePrivateKey(const KeyParams& keyParams) |
| 417 | { |
| 418 | switch (keyParams.getKeyType()) { |
| 419 | case KeyType::RSA: { |
| 420 | const RsaKeyParams& rsaParams = static_cast<const RsaKeyParams&>(keyParams); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 421 | return PrivateKey::generateRsaKey(rsaParams.getKeySize()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 422 | } |
| 423 | case KeyType::EC: { |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 424 | const EcKeyParams& ecParams = static_cast<const EcKeyParams&>(keyParams); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 425 | return PrivateKey::generateEcKey(ecParams.getKeySize()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 426 | } |
| 427 | default: |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 428 | BOOST_THROW_EXCEPTION(std::invalid_argument("Unsupported asymmetric key type " + |
| 429 | boost::lexical_cast<std::string>(keyParams.getKeyType()))); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
| 433 | } // namespace transform |
| 434 | } // namespace security |
| 435 | } // namespace ndn |