Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 2 | /* |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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_PUBLIC_KEY_HPP |
| 23 | #define NDN_CXX_SECURITY_TRANSFORM_PUBLIC_KEY_HPP |
| 24 | |
| 25 | #include "../security-common.hpp" |
| 26 | #include "../../encoding/buffer.hpp" |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace security { |
| 30 | namespace transform { |
| 31 | |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 32 | /** |
| 33 | * @brief Abstraction of public key in crypto transformation |
| 34 | */ |
| 35 | class PublicKey : noncopyable |
| 36 | { |
| 37 | public: |
| 38 | class Error : public std::runtime_error |
| 39 | { |
| 40 | public: |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 41 | using std::runtime_error::runtime_error; |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 44 | public: |
| 45 | /** |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 46 | * @brief Create an empty public key instance |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 47 | * |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 48 | * One must call loadXXXX(...) to load a public key. |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 49 | */ |
| 50 | PublicKey(); |
| 51 | |
| 52 | ~PublicKey(); |
| 53 | |
| 54 | /** |
| 55 | * @brief Get the type of the public key |
| 56 | */ |
| 57 | KeyType |
| 58 | getKeyType() const; |
| 59 | |
| 60 | /** |
| 61 | * @brief Load the public key in PKCS#8 format from a buffer @p buf |
| 62 | */ |
| 63 | void |
| 64 | loadPkcs8(const uint8_t* buf, size_t size); |
| 65 | |
| 66 | /** |
| 67 | * @brief Load the public key in PKCS#8 format from a stream @p is |
| 68 | */ |
| 69 | void |
| 70 | loadPkcs8(std::istream& is); |
| 71 | |
| 72 | /** |
| 73 | * @brief Load the public key in base64-encoded PKCS#8 format from a buffer @p buf |
| 74 | */ |
| 75 | void |
| 76 | loadPkcs8Base64(const uint8_t* buf, size_t size); |
| 77 | |
| 78 | /** |
| 79 | * @brief Load the public key in base64-encoded PKCS#8 format from a stream @p is |
| 80 | */ |
| 81 | void |
| 82 | loadPkcs8Base64(std::istream& is); |
| 83 | |
| 84 | /** |
| 85 | * @brief Save the public key in PKCS#8 format into a stream @p os |
| 86 | */ |
| 87 | void |
| 88 | savePkcs8(std::ostream& os) const; |
| 89 | |
| 90 | /** |
| 91 | * @brief Save the public key in base64-encoded PKCS#8 format into a stream @p os |
| 92 | */ |
| 93 | void |
| 94 | savePkcs8Base64(std::ostream& os) const; |
| 95 | |
| 96 | /** |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 97 | * @return Cipher text of @p plainText encrypted using this public key. |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 98 | * |
| 99 | * Only RSA encryption is supported for now. |
| 100 | */ |
| 101 | ConstBufferPtr |
| 102 | encrypt(const uint8_t* plainText, size_t plainLen) const; |
| 103 | |
| 104 | private: |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 105 | friend class VerifierFilter; |
| 106 | |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 107 | /** |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 108 | * @return A pointer to an OpenSSL EVP_PKEY instance. |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 109 | * |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 110 | * The caller needs to explicitly cast the return value to `EVP_PKEY*`. |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 111 | */ |
| 112 | void* |
| 113 | getEvpPkey() const; |
| 114 | |
| 115 | private: |
| 116 | ConstBufferPtr |
| 117 | toPkcs8() const; |
| 118 | |
| 119 | ConstBufferPtr |
| 120 | rsaEncrypt(const uint8_t* plainText, size_t plainLen) const; |
| 121 | |
| 122 | private: |
| 123 | class Impl; |
Davide Pesavento | 794f687 | 2017-05-15 23:33:38 -0400 | [diff] [blame] | 124 | const unique_ptr<Impl> m_impl; |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | } // namespace transform |
| 128 | } // namespace security |
| 129 | } // namespace ndn |
| 130 | |
| 131 | #endif // NDN_CXX_SECURITY_TRANSFORM_PUBLIC_KEY_HPP |