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