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