blob: a94814dd67d4d21af67d15654e11a6a091d3bf8b [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/*
Davide Pesavento794f6872017-05-15 23:33:38 -04003 * Copyright (c) 2013-2017 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:
41 explicit
42 Error(const std::string& what)
43 : std::runtime_error(what)
44 {
45 }
46 };
47
Yingdi Yu202a2e92015-07-12 16:49:25 -070048public:
49 /**
Davide Pesavento1c31a712017-09-15 00:52:03 -040050 * @brief Create an empty public key instance
Yingdi Yu202a2e92015-07-12 16:49:25 -070051 *
Davide Pesavento1c31a712017-09-15 00:52:03 -040052 * One must call loadXXXX(...) to load a public key.
Yingdi Yu202a2e92015-07-12 16:49:25 -070053 */
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 Pesavento1c31a712017-09-15 00:52:03 -0400101 * @return Cipher text of @p plainText encrypted using this public key.
Yingdi Yu202a2e92015-07-12 16:49:25 -0700102 *
103 * Only RSA encryption is supported for now.
104 */
105 ConstBufferPtr
106 encrypt(const uint8_t* plainText, size_t plainLen) const;
107
108private:
Davide Pesavento1c31a712017-09-15 00:52:03 -0400109 friend class VerifierFilter;
110
Yingdi Yu202a2e92015-07-12 16:49:25 -0700111 /**
Davide Pesavento1c31a712017-09-15 00:52:03 -0400112 * @return A pointer to an OpenSSL EVP_PKEY instance.
Yingdi Yu202a2e92015-07-12 16:49:25 -0700113 *
Davide Pesavento1c31a712017-09-15 00:52:03 -0400114 * The caller needs to explicitly cast the return value to `EVP_PKEY*`.
Yingdi Yu202a2e92015-07-12 16:49:25 -0700115 */
116 void*
117 getEvpPkey() const;
118
119private:
120 ConstBufferPtr
121 toPkcs8() const;
122
123 ConstBufferPtr
124 rsaEncrypt(const uint8_t* plainText, size_t plainLen) const;
125
126private:
127 class Impl;
Davide Pesavento794f6872017-05-15 23:33:38 -0400128 const unique_ptr<Impl> m_impl;
Yingdi Yu202a2e92015-07-12 16:49:25 -0700129};
130
131} // namespace transform
132} // namespace security
133} // namespace ndn
134
135#endif // NDN_CXX_SECURITY_TRANSFORM_PUBLIC_KEY_HPP