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 | 794f687 | 2017-05-15 23:33:38 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #ifndef NDN_CXX_SECURITY_TRANSFORM_PRIVATE_KEY_HPP |
| 23 | #define NDN_CXX_SECURITY_TRANSFORM_PRIVATE_KEY_HPP |
| 24 | |
Davide Pesavento | 06f1bdf | 2017-09-16 18:59:15 -0400 | [diff] [blame] | 25 | #include "../security-common.hpp" |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 26 | #include "../../encoding/buffer.hpp" |
| 27 | |
| 28 | namespace ndn { |
| 29 | |
| 30 | class KeyParams; |
| 31 | |
| 32 | namespace security { |
| 33 | namespace transform { |
| 34 | |
| 35 | /** |
| 36 | * @brief Abstraction of private key in crypto transformation |
| 37 | */ |
| 38 | class PrivateKey : noncopyable |
| 39 | { |
| 40 | public: |
| 41 | class Error : public std::runtime_error |
| 42 | { |
| 43 | public: |
| 44 | explicit |
| 45 | Error(const std::string& what) |
| 46 | : std::runtime_error(what) |
| 47 | { |
| 48 | } |
| 49 | }; |
| 50 | |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 51 | /** |
| 52 | * @brief Callback for application to handle password input |
| 53 | * |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame] | 54 | * The password must be written to @p buf and must not be longer than @p bufSize chars. |
| 55 | * It is recommended to ask the user to verify the password if @p shouldConfirm is true, |
| 56 | * e.g., by prompting for it twice. The callback must return the number of characters |
| 57 | * in the password or 0 if an error occurred. |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 58 | */ |
| 59 | typedef function<int(char* buf, size_t bufSize, bool shouldConfirm)> PasswordCallback; |
| 60 | |
| 61 | public: |
| 62 | /** |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 63 | * @brief Create an empty private key instance |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 64 | * |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 65 | * One must call loadXXXX(...) to load a private key. |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 66 | */ |
| 67 | PrivateKey(); |
| 68 | |
| 69 | ~PrivateKey(); |
| 70 | |
| 71 | /** |
Davide Pesavento | 06f1bdf | 2017-09-16 18:59:15 -0400 | [diff] [blame] | 72 | * @brief Get the type of the private key |
| 73 | */ |
| 74 | KeyType |
| 75 | getKeyType() const; |
| 76 | |
| 77 | /** |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 78 | * @brief Load the private key in PKCS#1 format from a buffer @p buf |
| 79 | */ |
| 80 | void |
| 81 | loadPkcs1(const uint8_t* buf, size_t size); |
| 82 | |
| 83 | /** |
| 84 | * @brief Load the private key in PKCS#1 format from a stream @p is |
| 85 | */ |
| 86 | void |
| 87 | loadPkcs1(std::istream& is); |
| 88 | |
| 89 | /** |
| 90 | * @brief Load the private key in base64-encoded PKCS#1 format from a buffer @p buf |
| 91 | */ |
| 92 | void |
| 93 | loadPkcs1Base64(const uint8_t* buf, size_t size); |
| 94 | |
| 95 | /** |
| 96 | * @brief Load the private key in base64-encoded PKCS#1 format from a stream @p is |
| 97 | */ |
| 98 | void |
| 99 | loadPkcs1Base64(std::istream& is); |
| 100 | |
| 101 | /** |
| 102 | * @brief Load the private key in encrypted PKCS#8 format from a buffer @p buf with passphrase @p pw |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 103 | * @pre strlen(pw) == pwLen |
| 104 | */ |
| 105 | void |
| 106 | loadPkcs8(const uint8_t* buf, size_t size, const char* pw, size_t pwLen); |
| 107 | |
| 108 | /** |
| 109 | * @brief Load the private key in encrypted PKCS#8 format from a buffer @p buf with |
| 110 | * passphrase obtained from @p pwCallback |
| 111 | * |
| 112 | * The default password callback is provided by OpenSSL |
| 113 | */ |
| 114 | void |
| 115 | loadPkcs8(const uint8_t* buf, size_t size, PasswordCallback pwCallback = nullptr); |
| 116 | |
| 117 | /** |
| 118 | * @brief Load the private key in encrypted PKCS#8 format from a stream @p is with passphrase @p pw |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 119 | * @pre strlen(pw) == pwLen |
| 120 | */ |
| 121 | void |
| 122 | loadPkcs8(std::istream& is, const char* pw, size_t pwLen); |
| 123 | |
| 124 | /** |
| 125 | * @brief Load the private key in encrypted PKCS#8 format from a stream @p is with passphrase |
| 126 | * obtained from @p pwCallback |
| 127 | * |
| 128 | * The default password callback is provided by OpenSSL |
| 129 | */ |
| 130 | void |
| 131 | loadPkcs8(std::istream& is, PasswordCallback pwCallback = nullptr); |
| 132 | |
| 133 | /** |
| 134 | * @brief Load the private key in base64-encoded encrypted PKCS#8 format from a buffer @p buf |
| 135 | * with passphrase @p pw |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 136 | * @pre strlen(pw) == pwLen |
| 137 | */ |
| 138 | void |
| 139 | loadPkcs8Base64(const uint8_t* buf, size_t size, const char* pw, size_t pwLen); |
| 140 | |
| 141 | /** |
| 142 | * @brief Load the private key in encrypted PKCS#8 format from a buffer @p buf with |
| 143 | * passphrase obtained from @p pwCallback |
| 144 | * |
| 145 | * The default password callback is provided by OpenSSL |
| 146 | */ |
| 147 | void |
| 148 | loadPkcs8Base64(const uint8_t* buf, size_t size, PasswordCallback pwCallback = nullptr); |
| 149 | |
| 150 | /** |
| 151 | * @brief Load the private key in base64-encoded encrypted PKCS#8 format from a stream @p is |
| 152 | * with passphrase @p pw |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 153 | * @pre strlen(pw) == pwLen |
| 154 | */ |
| 155 | void |
| 156 | loadPkcs8Base64(std::istream& is, const char* pw, size_t pwLen); |
| 157 | |
| 158 | /** |
| 159 | * @brief Load the private key in base64-encoded encrypted PKCS#8 format from a stream @p is |
| 160 | * with passphrase obtained from @p pwCallback |
| 161 | * |
| 162 | * The default password callback is provided by OpenSSL |
| 163 | */ |
| 164 | void |
| 165 | loadPkcs8Base64(std::istream& is, PasswordCallback pwCallback = nullptr); |
| 166 | |
| 167 | /** |
| 168 | * @brief Save the private key in PKCS#1 format into a stream @p os |
| 169 | */ |
| 170 | void |
| 171 | savePkcs1(std::ostream& os) const; |
| 172 | |
| 173 | /** |
| 174 | * @brief Save the private key in base64-encoded PKCS#1 format into a stream @p os |
| 175 | */ |
| 176 | void |
| 177 | savePkcs1Base64(std::ostream& os) const; |
| 178 | |
| 179 | /** |
| 180 | * @brief Save the private key in encrypted PKCS#8 format into a stream @p os |
| 181 | */ |
| 182 | void |
| 183 | savePkcs8(std::ostream& os, const char* pw, size_t pwLen) const; |
| 184 | |
| 185 | /** |
| 186 | * @brief Save the private key in encrypted PKCS#8 format into a stream @p os with passphrase |
| 187 | * obtained from @p pwCallback |
| 188 | * |
| 189 | * The default password callback is provided by OpenSSL |
| 190 | */ |
| 191 | void |
| 192 | savePkcs8(std::ostream& os, PasswordCallback pwCallback = nullptr) const; |
| 193 | |
| 194 | /** |
| 195 | * @brief Save the private key in base64-encoded encrypted PKCS#8 format into a stream @p os |
| 196 | */ |
| 197 | void |
| 198 | savePkcs8Base64(std::ostream& os, const char* pw, size_t pwLen) const; |
| 199 | |
| 200 | /** |
| 201 | * @brief Save the private key in base64-encoded encrypted PKCS#8 format into a stream @p os |
| 202 | * with passphrase obtained from @p pwCallback |
| 203 | * |
| 204 | * The default password callback is provided by OpenSSL |
| 205 | */ |
| 206 | void |
| 207 | savePkcs8Base64(std::ostream& os, PasswordCallback pwCallback = nullptr) const; |
| 208 | |
| 209 | /** |
| 210 | * @return Public key bits in PKCS#8 format |
| 211 | */ |
| 212 | ConstBufferPtr |
| 213 | derivePublicKey() const; |
| 214 | |
| 215 | /** |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 216 | * @return Plain text of @p cipherText decrypted using this private key. |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 217 | * |
| 218 | * Only RSA encryption is supported for now. |
| 219 | */ |
| 220 | ConstBufferPtr |
| 221 | decrypt(const uint8_t* cipherText, size_t cipherLen) const; |
| 222 | |
| 223 | private: |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 224 | friend class SignerFilter; |
| 225 | |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 226 | /** |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 227 | * @return A pointer to an OpenSSL EVP_PKEY instance. |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 228 | * |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 229 | * The caller needs to explicitly cast the return value to `EVP_PKEY*`. |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 230 | */ |
| 231 | void* |
| 232 | getEvpPkey() const; |
| 233 | |
| 234 | private: |
| 235 | ConstBufferPtr |
| 236 | toPkcs1() const; |
| 237 | |
| 238 | ConstBufferPtr |
| 239 | toPkcs8(const char* pw, size_t pwLen) const; |
| 240 | |
| 241 | ConstBufferPtr |
| 242 | toPkcs8(PasswordCallback pwCallback = nullptr) const; |
| 243 | |
| 244 | ConstBufferPtr |
| 245 | rsaDecrypt(const uint8_t* cipherText, size_t cipherLen) const; |
| 246 | |
| 247 | private: |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 248 | friend unique_ptr<PrivateKey> generatePrivateKey(const KeyParams&); |
| 249 | |
| 250 | static unique_ptr<PrivateKey> |
| 251 | generateRsaKey(uint32_t keySize); |
| 252 | |
| 253 | static unique_ptr<PrivateKey> |
| 254 | generateEcKey(uint32_t keySize); |
| 255 | |
| 256 | private: |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 257 | class Impl; |
Davide Pesavento | 794f687 | 2017-05-15 23:33:38 -0400 | [diff] [blame] | 258 | const unique_ptr<Impl> m_impl; |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 259 | }; |
| 260 | |
| 261 | /** |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 262 | * @brief Generate a private key according to @p keyParams |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 263 | * |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 264 | * @note The public key can be derived from the private key. |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 265 | * |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 266 | * @throw std::invalid_argument the specified key type is not supported |
| 267 | * @throw std::runtime_error key generation fails |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 268 | */ |
| 269 | unique_ptr<PrivateKey> |
| 270 | generatePrivateKey(const KeyParams& keyParams); |
| 271 | |
| 272 | } // namespace transform |
| 273 | } // namespace security |
| 274 | } // namespace ndn |
| 275 | |
| 276 | #endif // NDN_CXX_SECURITY_TRANSFORM_PRIVATE_KEY_HPP |