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