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 | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 2 | /* |
Muktadir R Chowdhury | 80fba6c | 2017-05-05 15:30:15 -0500 | [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 | #include "public-key.hpp" |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 23 | #include "base64-decode.hpp" |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 24 | #include "base64-encode.hpp" |
| 25 | #include "buffer-source.hpp" |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 26 | #include "stream-sink.hpp" |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 27 | #include "stream-source.hpp" |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 28 | #include "../detail/openssl-helper.hpp" |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 29 | #include "../../encoding/buffer-stream.hpp" |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 30 | |
| 31 | #define ENSURE_PUBLIC_KEY_LOADED(key) \ |
| 32 | do { \ |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 33 | if ((key) == nullptr) \ |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 34 | BOOST_THROW_EXCEPTION(Error("Public key has not been loaded yet")); \ |
| 35 | } while (false) |
| 36 | |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 37 | #define ENSURE_PUBLIC_KEY_NOT_LOADED(key) \ |
| 38 | do { \ |
| 39 | if ((key) != nullptr) \ |
| 40 | BOOST_THROW_EXCEPTION(Error("Public key has already been loaded")); \ |
| 41 | } while (false) |
| 42 | |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 43 | namespace ndn { |
| 44 | namespace security { |
| 45 | namespace transform { |
| 46 | |
| 47 | class PublicKey::Impl |
| 48 | { |
| 49 | public: |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 50 | Impl() noexcept |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 51 | : key(nullptr) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | ~Impl() |
| 56 | { |
| 57 | EVP_PKEY_free(key); |
| 58 | } |
| 59 | |
| 60 | public: |
| 61 | EVP_PKEY* key; |
| 62 | }; |
| 63 | |
| 64 | PublicKey::PublicKey() |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 65 | : m_impl(make_unique<Impl>()) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 66 | { |
| 67 | } |
| 68 | |
| 69 | PublicKey::~PublicKey() = default; |
| 70 | |
| 71 | KeyType |
| 72 | PublicKey::getKeyType() const |
| 73 | { |
| 74 | ENSURE_PUBLIC_KEY_LOADED(m_impl->key); |
| 75 | |
Davide Pesavento | c21979d | 2017-09-16 14:52:17 -0400 | [diff] [blame^] | 76 | switch (detail::getEvpPkeyType(m_impl->key)) { |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 77 | case EVP_PKEY_RSA: |
| 78 | return KeyType::RSA; |
| 79 | case EVP_PKEY_EC: |
| 80 | return KeyType::EC; |
| 81 | default: |
Davide Pesavento | c21979d | 2017-09-16 14:52:17 -0400 | [diff] [blame^] | 82 | return KeyType::NONE; |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | PublicKey::loadPkcs8(const uint8_t* buf, size_t size) |
| 88 | { |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 89 | ENSURE_PUBLIC_KEY_NOT_LOADED(m_impl->key); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 90 | |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 91 | if (d2i_PUBKEY(&m_impl->key, &buf, static_cast<long>(size)) == nullptr) |
| 92 | BOOST_THROW_EXCEPTION(Error("Failed to load public key")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void |
| 96 | PublicKey::loadPkcs8(std::istream& is) |
| 97 | { |
| 98 | OBufferStream os; |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 99 | streamSource(is) >> streamSink(os); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 100 | this->loadPkcs8(os.buf()->buf(), os.buf()->size()); |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | PublicKey::loadPkcs8Base64(const uint8_t* buf, size_t size) |
| 105 | { |
| 106 | OBufferStream os; |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 107 | bufferSource(buf, size) >> base64Decode() >> streamSink(os); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 108 | this->loadPkcs8(os.buf()->buf(), os.buf()->size()); |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | PublicKey::loadPkcs8Base64(std::istream& is) |
| 113 | { |
| 114 | OBufferStream os; |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 115 | streamSource(is) >> base64Decode() >> streamSink(os); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 116 | this->loadPkcs8(os.buf()->buf(), os.buf()->size()); |
| 117 | } |
| 118 | |
| 119 | void |
| 120 | PublicKey::savePkcs8(std::ostream& os) const |
| 121 | { |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 122 | bufferSource(*this->toPkcs8()) >> streamSink(os); |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | PublicKey::savePkcs8Base64(std::ostream& os) const |
| 127 | { |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 128 | bufferSource(*this->toPkcs8()) >> base64Encode() >> streamSink(os); |
| 129 | } |
| 130 | |
| 131 | ConstBufferPtr |
| 132 | PublicKey::encrypt(const uint8_t* plainText, size_t plainLen) const |
| 133 | { |
| 134 | ENSURE_PUBLIC_KEY_LOADED(m_impl->key); |
| 135 | |
Davide Pesavento | c21979d | 2017-09-16 14:52:17 -0400 | [diff] [blame^] | 136 | int keyType = detail::getEvpPkeyType(m_impl->key); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 137 | switch (keyType) { |
| 138 | case EVP_PKEY_NONE: |
| 139 | BOOST_THROW_EXCEPTION(Error("Failed to determine key type")); |
| 140 | case EVP_PKEY_RSA: |
| 141 | return rsaEncrypt(plainText, plainLen); |
| 142 | default: |
| 143 | BOOST_THROW_EXCEPTION(Error("Encryption is not supported for key type " + to_string(keyType))); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | void* |
| 148 | PublicKey::getEvpPkey() const |
| 149 | { |
| 150 | return m_impl->key; |
| 151 | } |
| 152 | |
| 153 | ConstBufferPtr |
| 154 | PublicKey::toPkcs8() const |
| 155 | { |
| 156 | ENSURE_PUBLIC_KEY_LOADED(m_impl->key); |
| 157 | |
| 158 | uint8_t* pkcs8 = nullptr; |
| 159 | int len = i2d_PUBKEY(m_impl->key, &pkcs8); |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 160 | if (len < 0) |
| 161 | BOOST_THROW_EXCEPTION(Error("Cannot convert key to PKCS #8 format")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 162 | |
| 163 | auto buffer = make_shared<Buffer>(pkcs8, len); |
| 164 | OPENSSL_free(pkcs8); |
| 165 | |
| 166 | return buffer; |
| 167 | } |
| 168 | |
| 169 | ConstBufferPtr |
| 170 | PublicKey::rsaEncrypt(const uint8_t* plainText, size_t plainLen) const |
| 171 | { |
| 172 | detail::EvpPkeyCtx ctx(m_impl->key); |
| 173 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 174 | if (EVP_PKEY_encrypt_init(ctx) <= 0) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 175 | BOOST_THROW_EXCEPTION(Error("Failed to initialize encryption context")); |
| 176 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 177 | if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING) <= 0) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 178 | BOOST_THROW_EXCEPTION(Error("Failed to set padding")); |
| 179 | |
| 180 | size_t outlen = 0; |
| 181 | // Determine buffer length |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 182 | if (EVP_PKEY_encrypt(ctx, nullptr, &outlen, plainText, plainLen) <= 0) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 183 | BOOST_THROW_EXCEPTION(Error("Failed to estimate output length")); |
| 184 | |
| 185 | auto out = make_shared<Buffer>(outlen); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 186 | if (EVP_PKEY_encrypt(ctx, out->buf(), &outlen, plainText, plainLen) <= 0) |
Muktadir R Chowdhury | 80fba6c | 2017-05-05 15:30:15 -0500 | [diff] [blame] | 187 | BOOST_THROW_EXCEPTION(Error("Failed to encrypt plaintext")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 188 | |
| 189 | out->resize(outlen); |
| 190 | return out; |
| 191 | } |
| 192 | |
| 193 | } // namespace transform |
| 194 | } // namespace security |
| 195 | } // namespace ndn |