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 | /* |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2019 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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/security/transform/private-key.hpp" |
| 23 | #include "ndn-cxx/security/transform/base64-decode.hpp" |
| 24 | #include "ndn-cxx/security/transform/base64-encode.hpp" |
| 25 | #include "ndn-cxx/security/transform/buffer-source.hpp" |
| 26 | #include "ndn-cxx/security/transform/stream-sink.hpp" |
| 27 | #include "ndn-cxx/security/transform/stream-source.hpp" |
Junxiao Shi | 24c5a00 | 2018-12-12 04:47:15 +0000 | [diff] [blame] | 28 | #include "ndn-cxx/security/impl/openssl-helper.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 29 | #include "ndn-cxx/security/key-params.hpp" |
| 30 | #include "ndn-cxx/encoding/buffer-stream.hpp" |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 31 | #include "ndn-cxx/util/random.hpp" |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 32 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 33 | #include <boost/lexical_cast.hpp> |
Davide Pesavento | e178989 | 2017-02-26 15:50:52 -0500 | [diff] [blame] | 34 | #include <cstring> |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 35 | |
| 36 | #define ENSURE_PRIVATE_KEY_LOADED(key) \ |
| 37 | do { \ |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 38 | if ((key) == nullptr) \ |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 39 | NDN_THROW(Error("Private key has not been loaded yet")); \ |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 40 | } while (false) |
| 41 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 42 | #define ENSURE_PRIVATE_KEY_NOT_LOADED(key) \ |
| 43 | do { \ |
| 44 | if ((key) != nullptr) \ |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 45 | NDN_THROW(Error("Private key has already been loaded")); \ |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 46 | } while (false) |
| 47 | |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 48 | namespace ndn { |
| 49 | namespace security { |
| 50 | namespace transform { |
| 51 | |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 52 | static void |
| 53 | opensslInitAlgorithms() |
| 54 | { |
| 55 | #if OPENSSL_VERSION_NUMBER < 0x1010000fL |
| 56 | static bool isInitialized = false; |
| 57 | if (!isInitialized) { |
| 58 | OpenSSL_add_all_algorithms(); |
| 59 | isInitialized = true; |
| 60 | } |
| 61 | #endif // OPENSSL_VERSION_NUMBER < 0x1010000fL |
| 62 | } |
| 63 | |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 64 | class PrivateKey::Impl : noncopyable |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 65 | { |
| 66 | public: |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 67 | ~Impl() |
| 68 | { |
| 69 | EVP_PKEY_free(key); |
| 70 | } |
| 71 | |
| 72 | public: |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 73 | EVP_PKEY* key = nullptr; |
Davide Pesavento | 1bac111 | 2019-06-13 21:48:46 -0400 | [diff] [blame] | 74 | |
| 75 | #if OPENSSL_VERSION_NUMBER < 0x1010100fL |
| 76 | size_t keySize = 0; // in bits, used only for HMAC |
| 77 | #endif |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 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 | |
Davide Pesavento | 06f1bdf | 2017-09-16 18:59:15 -0400 | [diff] [blame] | 87 | KeyType |
| 88 | PrivateKey::getKeyType() const |
| 89 | { |
| 90 | if (!m_impl->key) |
| 91 | return KeyType::NONE; |
| 92 | |
| 93 | switch (detail::getEvpPkeyType(m_impl->key)) { |
| 94 | case EVP_PKEY_RSA: |
| 95 | return KeyType::RSA; |
| 96 | case EVP_PKEY_EC: |
| 97 | return KeyType::EC; |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 98 | case EVP_PKEY_HMAC: |
| 99 | return KeyType::HMAC; |
Davide Pesavento | 06f1bdf | 2017-09-16 18:59:15 -0400 | [diff] [blame] | 100 | default: |
| 101 | return KeyType::NONE; |
| 102 | } |
| 103 | } |
| 104 | |
Davide Pesavento | 1bac111 | 2019-06-13 21:48:46 -0400 | [diff] [blame] | 105 | size_t |
| 106 | PrivateKey::getKeySize() const |
| 107 | { |
| 108 | switch (getKeyType()) { |
| 109 | case KeyType::RSA: |
| 110 | case KeyType::EC: |
| 111 | return static_cast<size_t>(EVP_PKEY_bits(m_impl->key)); |
| 112 | case KeyType::HMAC: { |
| 113 | #if OPENSSL_VERSION_NUMBER >= 0x1010100fL |
| 114 | size_t nBytes = 0; |
| 115 | EVP_PKEY_get_raw_private_key(m_impl->key, nullptr, &nBytes); |
| 116 | return nBytes * 8; |
| 117 | #else |
| 118 | return m_impl->keySize; |
| 119 | #endif |
| 120 | } |
| 121 | default: |
| 122 | return 0; |
| 123 | } |
| 124 | } |
| 125 | |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 126 | void |
laqinfan | 48f9724 | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 127 | PrivateKey::loadRaw(KeyType type, const uint8_t* buf, size_t size) |
| 128 | { |
| 129 | ENSURE_PRIVATE_KEY_NOT_LOADED(m_impl->key); |
| 130 | |
| 131 | int pkeyType; |
| 132 | switch (type) { |
| 133 | case KeyType::HMAC: |
| 134 | pkeyType = EVP_PKEY_HMAC; |
| 135 | break; |
| 136 | default: |
| 137 | NDN_THROW(std::invalid_argument("Unsupported key type " + boost::lexical_cast<std::string>(type))); |
| 138 | } |
| 139 | |
| 140 | m_impl->key = |
| 141 | #if OPENSSL_VERSION_NUMBER >= 0x1010100fL |
| 142 | EVP_PKEY_new_raw_private_key(pkeyType, nullptr, buf, size); |
| 143 | #else |
| 144 | EVP_PKEY_new_mac_key(pkeyType, nullptr, buf, static_cast<int>(size)); |
| 145 | #endif |
| 146 | if (m_impl->key == nullptr) |
| 147 | NDN_THROW(Error("Failed to load private key")); |
| 148 | |
Davide Pesavento | 1bac111 | 2019-06-13 21:48:46 -0400 | [diff] [blame] | 149 | #if OPENSSL_VERSION_NUMBER < 0x1010100fL |
| 150 | m_impl->keySize = size * 8; |
| 151 | #endif |
laqinfan | 48f9724 | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 155 | PrivateKey::loadPkcs1(const uint8_t* buf, size_t size) |
| 156 | { |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 157 | ENSURE_PRIVATE_KEY_NOT_LOADED(m_impl->key); |
| 158 | opensslInitAlgorithms(); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 159 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 160 | if (d2i_AutoPrivateKey(&m_impl->key, &buf, static_cast<long>(size)) == nullptr) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 161 | NDN_THROW(Error("Failed to load private key")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void |
| 165 | PrivateKey::loadPkcs1(std::istream& is) |
| 166 | { |
| 167 | OBufferStream os; |
| 168 | streamSource(is) >> streamSink(os); |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 169 | this->loadPkcs1(os.buf()->data(), os.buf()->size()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void |
| 173 | PrivateKey::loadPkcs1Base64(const uint8_t* buf, size_t size) |
| 174 | { |
| 175 | OBufferStream os; |
| 176 | bufferSource(buf, size) >> base64Decode() >> streamSink(os); |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 177 | this->loadPkcs1(os.buf()->data(), os.buf()->size()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void |
| 181 | PrivateKey::loadPkcs1Base64(std::istream& is) |
| 182 | { |
| 183 | OBufferStream os; |
| 184 | streamSource(is) >> base64Decode() >> streamSink(os); |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 185 | this->loadPkcs1(os.buf()->data(), os.buf()->size()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void |
| 189 | PrivateKey::loadPkcs8(const uint8_t* buf, size_t size, const char* pw, size_t pwLen) |
| 190 | { |
| 191 | BOOST_ASSERT(std::strlen(pw) == pwLen); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 192 | ENSURE_PRIVATE_KEY_NOT_LOADED(m_impl->key); |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 193 | opensslInitAlgorithms(); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 194 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 195 | detail::Bio membio(BIO_s_mem()); |
| 196 | if (!membio.write(buf, size)) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 197 | NDN_THROW(Error("Failed to copy buffer")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 198 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 199 | if (d2i_PKCS8PrivateKey_bio(membio, &m_impl->key, nullptr, const_cast<char*>(pw)) == nullptr) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 200 | NDN_THROW(Error("Failed to load private key")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | static inline int |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 204 | passwordCallbackWrapper(char* buf, int size, int rwflag, void* u) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 205 | { |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 206 | BOOST_ASSERT(size >= 0); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 207 | auto cb = reinterpret_cast<PrivateKey::PasswordCallback*>(u); |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 208 | return (*cb)(buf, static_cast<size_t>(size), rwflag); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void |
| 212 | PrivateKey::loadPkcs8(const uint8_t* buf, size_t size, PasswordCallback pwCallback) |
| 213 | { |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 214 | ENSURE_PRIVATE_KEY_NOT_LOADED(m_impl->key); |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 215 | opensslInitAlgorithms(); |
| 216 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 217 | detail::Bio membio(BIO_s_mem()); |
| 218 | if (!membio.write(buf, size)) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 219 | NDN_THROW(Error("Failed to copy buffer")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 220 | |
| 221 | if (pwCallback) |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 222 | m_impl->key = d2i_PKCS8PrivateKey_bio(membio, nullptr, &passwordCallbackWrapper, &pwCallback); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 223 | else |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 224 | m_impl->key = d2i_PKCS8PrivateKey_bio(membio, nullptr, nullptr, nullptr); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 225 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 226 | if (m_impl->key == nullptr) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 227 | NDN_THROW(Error("Failed to load private key")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void |
| 231 | PrivateKey::loadPkcs8(std::istream& is, const char* pw, size_t pwLen) |
| 232 | { |
| 233 | OBufferStream os; |
| 234 | streamSource(is) >> streamSink(os); |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 235 | this->loadPkcs8(os.buf()->data(), os.buf()->size(), pw, pwLen); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void |
| 239 | PrivateKey::loadPkcs8(std::istream& is, PasswordCallback pwCallback) |
| 240 | { |
| 241 | OBufferStream os; |
| 242 | streamSource(is) >> streamSink(os); |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 243 | this->loadPkcs8(os.buf()->data(), os.buf()->size(), pwCallback); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void |
| 247 | PrivateKey::loadPkcs8Base64(const uint8_t* buf, size_t size, const char* pw, size_t pwLen) |
| 248 | { |
| 249 | OBufferStream os; |
| 250 | bufferSource(buf, size) >> base64Decode() >> streamSink(os); |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 251 | this->loadPkcs8(os.buf()->data(), os.buf()->size(), pw, pwLen); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void |
| 255 | PrivateKey::loadPkcs8Base64(const uint8_t* buf, size_t size, PasswordCallback pwCallback) |
| 256 | { |
| 257 | OBufferStream os; |
| 258 | bufferSource(buf, size) >> base64Decode() >> streamSink(os); |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 259 | this->loadPkcs8(os.buf()->data(), os.buf()->size(), pwCallback); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void |
| 263 | PrivateKey::loadPkcs8Base64(std::istream& is, const char* pw, size_t pwLen) |
| 264 | { |
| 265 | OBufferStream os; |
| 266 | streamSource(is) >> base64Decode() >> streamSink(os); |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 267 | this->loadPkcs8(os.buf()->data(), os.buf()->size(), pw, pwLen); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | void |
| 271 | PrivateKey::loadPkcs8Base64(std::istream& is, PasswordCallback pwCallback) |
| 272 | { |
| 273 | OBufferStream os; |
| 274 | streamSource(is) >> base64Decode() >> streamSink(os); |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 275 | this->loadPkcs8(os.buf()->data(), os.buf()->size(), pwCallback); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | void |
| 279 | PrivateKey::savePkcs1(std::ostream& os) const |
| 280 | { |
| 281 | bufferSource(*this->toPkcs1()) >> streamSink(os); |
| 282 | } |
| 283 | |
| 284 | void |
| 285 | PrivateKey::savePkcs1Base64(std::ostream& os) const |
| 286 | { |
| 287 | bufferSource(*this->toPkcs1()) >> base64Encode() >> streamSink(os); |
| 288 | } |
| 289 | |
| 290 | void |
| 291 | PrivateKey::savePkcs8(std::ostream& os, const char* pw, size_t pwLen) const |
| 292 | { |
| 293 | bufferSource(*this->toPkcs8(pw, pwLen)) >> streamSink(os); |
| 294 | } |
| 295 | |
| 296 | void |
| 297 | PrivateKey::savePkcs8(std::ostream& os, PasswordCallback pwCallback) const |
| 298 | { |
| 299 | bufferSource(*this->toPkcs8(pwCallback)) >> streamSink(os); |
| 300 | } |
| 301 | |
| 302 | void |
| 303 | PrivateKey::savePkcs8Base64(std::ostream& os, const char* pw, size_t pwLen) const |
| 304 | { |
| 305 | bufferSource(*this->toPkcs8(pw, pwLen)) >> base64Encode() >> streamSink(os); |
| 306 | } |
| 307 | |
| 308 | void |
| 309 | PrivateKey::savePkcs8Base64(std::ostream& os, PasswordCallback pwCallback) const |
| 310 | { |
| 311 | bufferSource(*this->toPkcs8(pwCallback)) >> base64Encode() >> streamSink(os); |
| 312 | } |
| 313 | |
| 314 | ConstBufferPtr |
| 315 | PrivateKey::derivePublicKey() const |
| 316 | { |
| 317 | ENSURE_PRIVATE_KEY_LOADED(m_impl->key); |
| 318 | |
| 319 | uint8_t* pkcs8 = nullptr; |
| 320 | int len = i2d_PUBKEY(m_impl->key, &pkcs8); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 321 | if (len < 0) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 322 | NDN_THROW(Error("Failed to derive public key")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 323 | |
| 324 | auto result = make_shared<Buffer>(pkcs8, len); |
| 325 | OPENSSL_free(pkcs8); |
| 326 | |
| 327 | return result; |
| 328 | } |
| 329 | |
| 330 | ConstBufferPtr |
| 331 | PrivateKey::decrypt(const uint8_t* cipherText, size_t cipherLen) const |
| 332 | { |
| 333 | ENSURE_PRIVATE_KEY_LOADED(m_impl->key); |
| 334 | |
Davide Pesavento | c21979d | 2017-09-16 14:52:17 -0400 | [diff] [blame] | 335 | int keyType = detail::getEvpPkeyType(m_impl->key); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 336 | switch (keyType) { |
| 337 | case EVP_PKEY_NONE: |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 338 | NDN_THROW(Error("Failed to determine key type")); |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 339 | case EVP_PKEY_RSA: |
| 340 | return rsaDecrypt(cipherText, cipherLen); |
| 341 | default: |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 342 | NDN_THROW(Error("Decryption is not supported for key type " + to_string(keyType))); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
| 346 | void* |
| 347 | PrivateKey::getEvpPkey() const |
| 348 | { |
| 349 | return m_impl->key; |
| 350 | } |
| 351 | |
| 352 | ConstBufferPtr |
| 353 | PrivateKey::toPkcs1() const |
| 354 | { |
| 355 | ENSURE_PRIVATE_KEY_LOADED(m_impl->key); |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 356 | opensslInitAlgorithms(); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 357 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 358 | detail::Bio membio(BIO_s_mem()); |
| 359 | if (!i2d_PrivateKey_bio(membio, m_impl->key)) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 360 | NDN_THROW(Error("Cannot convert key to PKCS #1 format")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 361 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 362 | auto buffer = make_shared<Buffer>(BIO_pending(membio)); |
Davide Pesavento | 140e9cb | 2019-08-19 00:02:05 -0400 | [diff] [blame^] | 363 | if (!membio.read(buffer->data(), buffer->size())) |
| 364 | NDN_THROW(Error("Read error during PKCS #1 conversion")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 365 | |
| 366 | return buffer; |
| 367 | } |
| 368 | |
| 369 | ConstBufferPtr |
| 370 | PrivateKey::toPkcs8(const char* pw, size_t pwLen) const |
| 371 | { |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 372 | BOOST_ASSERT(std::strlen(pw) == pwLen); |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 373 | ENSURE_PRIVATE_KEY_LOADED(m_impl->key); |
| 374 | opensslInitAlgorithms(); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 375 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 376 | detail::Bio membio(BIO_s_mem()); |
Davide Pesavento | ee77ca0 | 2018-10-10 01:45:24 -0400 | [diff] [blame] | 377 | if (!i2d_PKCS8PrivateKey_bio(membio, m_impl->key, EVP_aes_256_cbc(), nullptr, 0, |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 378 | nullptr, const_cast<char*>(pw))) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 379 | NDN_THROW(Error("Cannot convert key to PKCS #8 format")); |
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 | auto buffer = make_shared<Buffer>(BIO_pending(membio)); |
Davide Pesavento | 140e9cb | 2019-08-19 00:02:05 -0400 | [diff] [blame^] | 382 | if (!membio.read(buffer->data(), buffer->size())) |
| 383 | NDN_THROW(Error("Read error during PKCS #8 conversion")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 384 | |
| 385 | return buffer; |
| 386 | } |
| 387 | |
| 388 | ConstBufferPtr |
| 389 | PrivateKey::toPkcs8(PasswordCallback pwCallback) const |
| 390 | { |
| 391 | ENSURE_PRIVATE_KEY_LOADED(m_impl->key); |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 392 | opensslInitAlgorithms(); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 393 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 394 | detail::Bio membio(BIO_s_mem()); |
Davide Pesavento | ee77ca0 | 2018-10-10 01:45:24 -0400 | [diff] [blame] | 395 | if (!i2d_PKCS8PrivateKey_bio(membio, m_impl->key, EVP_aes_256_cbc(), nullptr, 0, |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 396 | &passwordCallbackWrapper, &pwCallback)) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 397 | NDN_THROW(Error("Cannot convert key to PKCS #8 format")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 398 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 399 | auto buffer = make_shared<Buffer>(BIO_pending(membio)); |
Davide Pesavento | 140e9cb | 2019-08-19 00:02:05 -0400 | [diff] [blame^] | 400 | if (!membio.read(buffer->data(), buffer->size())) |
| 401 | NDN_THROW(Error("Read error during PKCS #8 conversion")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 402 | |
| 403 | return buffer; |
| 404 | } |
| 405 | |
| 406 | ConstBufferPtr |
| 407 | PrivateKey::rsaDecrypt(const uint8_t* cipherText, size_t cipherLen) const |
| 408 | { |
| 409 | detail::EvpPkeyCtx ctx(m_impl->key); |
| 410 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 411 | if (EVP_PKEY_decrypt_init(ctx) <= 0) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 412 | NDN_THROW(Error("Failed to initialize decryption context")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 413 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 414 | if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING) <= 0) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 415 | NDN_THROW(Error("Failed to set padding")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 416 | |
| 417 | size_t outlen = 0; |
| 418 | // Determine buffer length |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 419 | if (EVP_PKEY_decrypt(ctx, nullptr, &outlen, cipherText, cipherLen) <= 0) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 420 | NDN_THROW(Error("Failed to estimate output length")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 421 | |
| 422 | auto out = make_shared<Buffer>(outlen); |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 423 | if (EVP_PKEY_decrypt(ctx, out->data(), &outlen, cipherText, cipherLen) <= 0) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 424 | NDN_THROW(Error("Failed to decrypt ciphertext")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 425 | |
| 426 | out->resize(outlen); |
| 427 | return out; |
| 428 | } |
| 429 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 430 | unique_ptr<PrivateKey> |
| 431 | PrivateKey::generateRsaKey(uint32_t keySize) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 432 | { |
| 433 | detail::EvpPkeyCtx kctx(EVP_PKEY_RSA); |
| 434 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 435 | if (EVP_PKEY_keygen_init(kctx) <= 0) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 436 | NDN_THROW(PrivateKey::Error("Failed to initialize RSA keygen context")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 437 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 438 | if (EVP_PKEY_CTX_set_rsa_keygen_bits(kctx, static_cast<int>(keySize)) <= 0) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 439 | NDN_THROW(PrivateKey::Error("Failed to set RSA key length")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 440 | |
| 441 | auto privateKey = make_unique<PrivateKey>(); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 442 | if (EVP_PKEY_keygen(kctx, &privateKey->m_impl->key) <= 0) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 443 | NDN_THROW(PrivateKey::Error("Failed to generate RSA key")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 444 | |
| 445 | return privateKey; |
| 446 | } |
| 447 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 448 | unique_ptr<PrivateKey> |
| 449 | PrivateKey::generateEcKey(uint32_t keySize) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 450 | { |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 451 | detail::EvpPkeyCtx pctx(EVP_PKEY_EC); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 452 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 453 | if (EVP_PKEY_paramgen_init(pctx) <= 0) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 454 | NDN_THROW(PrivateKey::Error("Failed to initialize EC paramgen context")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 455 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 456 | int ret; |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 457 | switch (keySize) { |
Davide Pesavento | 3c7969f | 2018-09-08 15:31:35 -0400 | [diff] [blame] | 458 | case 224: |
| 459 | ret = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_secp224r1); |
| 460 | break; |
| 461 | case 256: |
| 462 | ret = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_X9_62_prime256v1); // same as secp256r1 |
| 463 | break; |
| 464 | case 384: |
| 465 | ret = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_secp384r1); |
| 466 | break; |
| 467 | case 521: |
| 468 | ret = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_secp521r1); |
| 469 | break; |
| 470 | default: |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 471 | NDN_THROW(std::invalid_argument("Unsupported EC key length " + to_string(keySize))); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 472 | } |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 473 | if (ret <= 0) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 474 | NDN_THROW(PrivateKey::Error("Failed to set EC curve")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 475 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 476 | Impl params; |
| 477 | if (EVP_PKEY_paramgen(pctx, ¶ms.key) <= 0) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 478 | NDN_THROW(PrivateKey::Error("Failed to generate EC parameters")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 479 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 480 | detail::EvpPkeyCtx kctx(params.key); |
| 481 | if (EVP_PKEY_keygen_init(kctx) <= 0) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 482 | NDN_THROW(PrivateKey::Error("Failed to initialize EC keygen context")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 483 | |
| 484 | auto privateKey = make_unique<PrivateKey>(); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 485 | if (EVP_PKEY_keygen(kctx, &privateKey->m_impl->key) <= 0) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 486 | NDN_THROW(PrivateKey::Error("Failed to generate EC key")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 487 | |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 488 | return privateKey; |
| 489 | } |
| 490 | |
| 491 | unique_ptr<PrivateKey> |
| 492 | PrivateKey::generateHmacKey(uint32_t keySize) |
| 493 | { |
| 494 | std::vector<uint8_t> rawKey(keySize / 8); |
| 495 | random::generateSecureBytes(rawKey.data(), rawKey.size()); |
| 496 | |
| 497 | auto privateKey = make_unique<PrivateKey>(); |
laqinfan | 48f9724 | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 498 | try { |
| 499 | privateKey->loadRaw(KeyType::HMAC, rawKey.data(), rawKey.size()); |
| 500 | } |
| 501 | catch (const PrivateKey::Error&) { |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 502 | NDN_THROW(PrivateKey::Error("Failed to generate HMAC key")); |
laqinfan | 48f9724 | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 503 | } |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 504 | |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 505 | return privateKey; |
| 506 | } |
| 507 | |
| 508 | unique_ptr<PrivateKey> |
| 509 | generatePrivateKey(const KeyParams& keyParams) |
| 510 | { |
| 511 | switch (keyParams.getKeyType()) { |
| 512 | case KeyType::RSA: { |
| 513 | const RsaKeyParams& rsaParams = static_cast<const RsaKeyParams&>(keyParams); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 514 | return PrivateKey::generateRsaKey(rsaParams.getKeySize()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 515 | } |
| 516 | case KeyType::EC: { |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 517 | const EcKeyParams& ecParams = static_cast<const EcKeyParams&>(keyParams); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 518 | return PrivateKey::generateEcKey(ecParams.getKeySize()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 519 | } |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 520 | case KeyType::HMAC: { |
| 521 | const HmacKeyParams& hmacParams = static_cast<const HmacKeyParams&>(keyParams); |
| 522 | return PrivateKey::generateHmacKey(hmacParams.getKeySize()); |
| 523 | } |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 524 | default: |
laqinfan | 48f9724 | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 525 | NDN_THROW(std::invalid_argument("Unsupported key type " + |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 526 | boost::lexical_cast<std::string>(keyParams.getKeyType()))); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
| 530 | } // namespace transform |
| 531 | } // namespace security |
| 532 | } // namespace ndn |