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