NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
public-key.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 Regents of the University of California.
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 
30 
31 #define ENSURE_PUBLIC_KEY_LOADED(key) \
32  do { \
33  if ((key) == nullptr) \
34  NDN_THROW(Error("Public key has not been loaded yet")); \
35  } while (false)
36 
37 #define ENSURE_PUBLIC_KEY_NOT_LOADED(key) \
38  do { \
39  if ((key) != nullptr) \
40  NDN_THROW(Error("Public key has already been loaded")); \
41  } while (false)
42 
43 namespace ndn {
44 namespace security {
45 namespace transform {
46 
48 {
49 public:
50  Impl() noexcept
51  : key(nullptr)
52  {
53  }
54 
56  {
57  EVP_PKEY_free(key);
58  }
59 
60 public:
61  EVP_PKEY* key;
62 };
63 
65  : m_impl(make_unique<Impl>())
66 {
67 }
68 
69 PublicKey::~PublicKey() = default;
70 
71 KeyType
73 {
74  if (!m_impl->key)
75  return KeyType::NONE;
76 
77  switch (detail::getEvpPkeyType(m_impl->key)) {
78  case EVP_PKEY_RSA:
79  return KeyType::RSA;
80  case EVP_PKEY_EC:
81  return KeyType::EC;
82  default:
83  return KeyType::NONE;
84  }
85 }
86 
87 void
88 PublicKey::loadPkcs8(const uint8_t* buf, size_t size)
89 {
90  ENSURE_PUBLIC_KEY_NOT_LOADED(m_impl->key);
91 
92  if (d2i_PUBKEY(&m_impl->key, &buf, static_cast<long>(size)) == nullptr)
93  NDN_THROW(Error("Failed to load public key"));
94 }
95 
96 void
97 PublicKey::loadPkcs8(std::istream& is)
98 {
99  OBufferStream os;
100  streamSource(is) >> streamSink(os);
101  this->loadPkcs8(os.buf()->data(), os.buf()->size());
102 }
103 
104 void
105 PublicKey::loadPkcs8Base64(const uint8_t* buf, size_t size)
106 {
107  OBufferStream os;
108  bufferSource(buf, size) >> base64Decode() >> streamSink(os);
109  this->loadPkcs8(os.buf()->data(), os.buf()->size());
110 }
111 
112 void
113 PublicKey::loadPkcs8Base64(std::istream& is)
114 {
115  OBufferStream os;
116  streamSource(is) >> base64Decode() >> streamSink(os);
117  this->loadPkcs8(os.buf()->data(), os.buf()->size());
118 }
119 
120 void
121 PublicKey::savePkcs8(std::ostream& os) const
122 {
123  bufferSource(*this->toPkcs8()) >> streamSink(os);
124 }
125 
126 void
127 PublicKey::savePkcs8Base64(std::ostream& os) const
128 {
129  bufferSource(*this->toPkcs8()) >> base64Encode() >> streamSink(os);
130 }
131 
133 PublicKey::encrypt(const uint8_t* plainText, size_t plainLen) const
134 {
135  ENSURE_PUBLIC_KEY_LOADED(m_impl->key);
136 
137  int keyType = detail::getEvpPkeyType(m_impl->key);
138  switch (keyType) {
139  case EVP_PKEY_NONE:
140  NDN_THROW(Error("Failed to determine key type"));
141  case EVP_PKEY_RSA:
142  return rsaEncrypt(plainText, plainLen);
143  default:
144  NDN_THROW(Error("Encryption is not supported for key type " + to_string(keyType)));
145  }
146 }
147 
148 void*
149 PublicKey::getEvpPkey() const
150 {
151  return m_impl->key;
152 }
153 
155 PublicKey::toPkcs8() const
156 {
157  ENSURE_PUBLIC_KEY_LOADED(m_impl->key);
158 
159  uint8_t* pkcs8 = nullptr;
160  int len = i2d_PUBKEY(m_impl->key, &pkcs8);
161  if (len < 0)
162  NDN_THROW(Error("Cannot convert key to PKCS #8 format"));
163 
164  auto buffer = make_shared<Buffer>(pkcs8, len);
165  OPENSSL_free(pkcs8);
166 
167  return buffer;
168 }
169 
171 PublicKey::rsaEncrypt(const uint8_t* plainText, size_t plainLen) const
172 {
173  detail::EvpPkeyCtx ctx(m_impl->key);
174 
175  if (EVP_PKEY_encrypt_init(ctx) <= 0)
176  NDN_THROW(Error("Failed to initialize encryption context"));
177 
178  if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING) <= 0)
179  NDN_THROW(Error("Failed to set padding"));
180 
181  size_t outlen = 0;
182  // Determine buffer length
183  if (EVP_PKEY_encrypt(ctx, nullptr, &outlen, plainText, plainLen) <= 0)
184  NDN_THROW(Error("Failed to estimate output length"));
185 
186  auto out = make_shared<Buffer>(outlen);
187  if (EVP_PKEY_encrypt(ctx, out->data(), &outlen, plainText, plainLen) <= 0)
188  NDN_THROW(Error("Failed to encrypt plaintext"));
189 
190  out->resize(outlen);
191  return out;
192 }
193 
194 } // namespace transform
195 } // namespace security
196 } // namespace ndn
buf
const uint8_t * buf
Definition: verification-helpers.cpp:47
ndn::KeyType
KeyType
The type of a cryptographic key.
Definition: security-common.hpp:85
ndn::security::transform::PublicKey::~PublicKey
~PublicKey()
ndn::security::transform::PublicKey::encrypt
ConstBufferPtr encrypt(const uint8_t *plainText, size_t plainLen) const
Definition: public-key.cpp:133
buffer-source.hpp
transform
ndn::security::transform::PublicKey::loadPkcs8
void loadPkcs8(const uint8_t *buf, size_t size)
Load the public key in PKCS#8 format from a buffer buf.
Definition: public-key.cpp:88
ndn::security::transform::PublicKey::savePkcs8
void savePkcs8(std::ostream &os) const
Save the public key in PKCS#8 format into a stream os.
Definition: public-key.cpp:121
ndn::security::transform::PublicKey::loadPkcs8Base64
void loadPkcs8Base64(const uint8_t *buf, size_t size)
Load the public key in base64-encoded PKCS#8 format from a buffer buf.
Definition: public-key.cpp:105
ndn::security::transform::base64Decode
unique_ptr< Transform > base64Decode(bool expectNewlineEvery64Bytes)
Definition: base64-decode.cpp:129
ndn::security::transform::PublicKey::Impl::key
EVP_PKEY * key
Definition: public-key.cpp:61
public-key.hpp
ndn::security::transform::bufferSource
BufferSource bufferSource
Definition: buffer-source.hpp:73
ndn::security::transform::PublicKey::Impl::~Impl
~Impl()
Definition: public-key.cpp:55
base64-decode.hpp
ndn::security::transform::PublicKey::Impl
Definition: public-key.cpp:48
ndn::KeyType::EC
@ EC
Elliptic Curve key (e.g. for ECDSA), supports sign/verify operations.
ndn::security::transform::PublicKey::PublicKey
PublicKey()
Create an empty public key instance.
Definition: public-key.cpp:64
ndn::security::transform::PublicKey::getKeyType
KeyType getKeyType() const
Get the type of the public key.
Definition: public-key.cpp:72
ENSURE_PUBLIC_KEY_LOADED
#define ENSURE_PUBLIC_KEY_LOADED(key)
Definition: public-key.cpp:31
ENSURE_PUBLIC_KEY_NOT_LOADED
#define ENSURE_PUBLIC_KEY_NOT_LOADED(key)
Definition: public-key.cpp:37
ndn::security::transform::PublicKey::savePkcs8Base64
void savePkcs8Base64(std::ostream &os) const
Save the public key in base64-encoded PKCS#8 format into a stream os.
Definition: public-key.cpp:127
ndn::security::detail::getEvpPkeyType
int getEvpPkeyType(EVP_PKEY *key)
Definition: openssl-helper.cpp:62
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
openssl-helper.hpp
ndn::security::transform::streamSource
StreamSource streamSource
Definition: stream-source.hpp:62
stream-sink.hpp
ndn::security::transform::base64Encode
unique_ptr< Transform > base64Encode(bool needBreak)
Definition: base64-encode.cpp:129
stream-source.hpp
ndn::OBufferStream
implements an output stream that constructs ndn::Buffer
Definition: buffer-stream.hpp:71
ndn::security::transform::PublicKey::Impl::Impl
Impl() noexcept
Definition: public-key.cpp:50
ndn::security::transform::PublicKey::Error
Definition: public-key.hpp:39
ndn::security::transform::streamSink
unique_ptr< Sink > streamSink(std::ostream &os)
Definition: stream-sink.cpp:53
ndn::to_string
std::string to_string(const T &val)
Definition: backports.hpp:102
ndn::KeyType::NONE
@ NONE
Unknown or unsupported key type.
buffer-stream.hpp
ndn::OBufferStream::buf
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
Definition: buffer-stream.cpp:54
base64-encode.hpp
ndn::KeyType::RSA
@ RSA
RSA key, supports sign/verify and encrypt/decrypt operations.
ndn::ConstBufferPtr
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34