blob: 41aeacb22c56cbfdd151c4535f6bdb639c492c4a [file] [log] [blame]
Yingdi Yu202a2e92015-07-12 16:49:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento1c31a712017-09-15 00:52:03 -04002/*
Junxiao Shi68b53852018-07-25 13:56:38 -06003 * Copyright (c) 2013-2018 Regents of the University of California.
Yingdi Yu202a2e92015-07-12 16:49:25 -07004 *
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
28namespace ndn {
29namespace security {
30namespace transform {
31
Yingdi Yu202a2e92015-07-12 16:49:25 -070032/**
33 * @brief Abstraction of public key in crypto transformation
34 */
35class PublicKey : noncopyable
36{
37public:
38 class Error : public std::runtime_error
39 {
40 public:
Junxiao Shi68b53852018-07-25 13:56:38 -060041 using std::runtime_error::runtime_error;
Yingdi Yu202a2e92015-07-12 16:49:25 -070042 };
43
Yingdi Yu202a2e92015-07-12 16:49:25 -070044public:
45 /**
Davide Pesavento1c31a712017-09-15 00:52:03 -040046 * @brief Create an empty public key instance
Yingdi Yu202a2e92015-07-12 16:49:25 -070047 *
Davide Pesavento1c31a712017-09-15 00:52:03 -040048 * One must call loadXXXX(...) to load a public key.
Yingdi Yu202a2e92015-07-12 16:49:25 -070049 */
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 Pesavento1c31a712017-09-15 00:52:03 -040097 * @return Cipher text of @p plainText encrypted using this public key.
Yingdi Yu202a2e92015-07-12 16:49:25 -070098 *
99 * Only RSA encryption is supported for now.
100 */
101 ConstBufferPtr
102 encrypt(const uint8_t* plainText, size_t plainLen) const;
103
104private:
Davide Pesavento1c31a712017-09-15 00:52:03 -0400105 friend class VerifierFilter;
106
Yingdi Yu202a2e92015-07-12 16:49:25 -0700107 /**
Davide Pesavento1c31a712017-09-15 00:52:03 -0400108 * @return A pointer to an OpenSSL EVP_PKEY instance.
Yingdi Yu202a2e92015-07-12 16:49:25 -0700109 *
Davide Pesavento1c31a712017-09-15 00:52:03 -0400110 * The caller needs to explicitly cast the return value to `EVP_PKEY*`.
Yingdi Yu202a2e92015-07-12 16:49:25 -0700111 */
112 void*
113 getEvpPkey() const;
114
115private:
116 ConstBufferPtr
117 toPkcs8() const;
118
119 ConstBufferPtr
120 rsaEncrypt(const uint8_t* plainText, size_t plainLen) const;
121
122private:
123 class Impl;
Davide Pesavento794f6872017-05-15 23:33:38 -0400124 const unique_ptr<Impl> m_impl;
Yingdi Yu202a2e92015-07-12 16:49:25 -0700125};
126
127} // namespace transform
128} // namespace security
129} // namespace ndn
130
131#endif // NDN_CXX_SECURITY_TRANSFORM_PUBLIC_KEY_HPP