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 | { |
Davide Pesavento | 06f1bdf | 2017-09-16 18:59:15 -0400 | [diff] [blame^] | 74 | if (!m_impl->key) |
| 75 | return KeyType::NONE; |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 76 | |
Davide Pesavento | c21979d | 2017-09-16 14:52:17 -0400 | [diff] [blame] | 77 | switch (detail::getEvpPkeyType(m_impl->key)) { |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 78 | case EVP_PKEY_RSA: |
| 79 | return KeyType::RSA; |
| 80 | case EVP_PKEY_EC: |
| 81 | return KeyType::EC; |
| 82 | default: |
Davide Pesavento | c21979d | 2017-09-16 14:52:17 -0400 | [diff] [blame] | 83 | return KeyType::NONE; |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | PublicKey::loadPkcs8(const uint8_t* buf, size_t size) |
| 89 | { |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 90 | ENSURE_PUBLIC_KEY_NOT_LOADED(m_impl->key); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 91 | |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 92 | if (d2i_PUBKEY(&m_impl->key, &buf, static_cast<long>(size)) == nullptr) |
| 93 | BOOST_THROW_EXCEPTION(Error("Failed to load public key")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void |
| 97 | PublicKey::loadPkcs8(std::istream& is) |
| 98 | { |
| 99 | OBufferStream os; |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 100 | streamSource(is) >> streamSink(os); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 101 | this->loadPkcs8(os.buf()->buf(), os.buf()->size()); |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | PublicKey::loadPkcs8Base64(const uint8_t* buf, size_t size) |
| 106 | { |
| 107 | OBufferStream os; |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 108 | bufferSource(buf, size) >> base64Decode() >> streamSink(os); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 109 | this->loadPkcs8(os.buf()->buf(), os.buf()->size()); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | PublicKey::loadPkcs8Base64(std::istream& is) |
| 114 | { |
| 115 | OBufferStream os; |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 116 | streamSource(is) >> base64Decode() >> streamSink(os); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 117 | this->loadPkcs8(os.buf()->buf(), os.buf()->size()); |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | PublicKey::savePkcs8(std::ostream& os) const |
| 122 | { |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 123 | bufferSource(*this->toPkcs8()) >> streamSink(os); |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | PublicKey::savePkcs8Base64(std::ostream& os) const |
| 128 | { |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 129 | bufferSource(*this->toPkcs8()) >> base64Encode() >> streamSink(os); |
| 130 | } |
| 131 | |
| 132 | ConstBufferPtr |
| 133 | PublicKey::encrypt(const uint8_t* plainText, size_t plainLen) const |
| 134 | { |
| 135 | ENSURE_PUBLIC_KEY_LOADED(m_impl->key); |
| 136 | |
Davide Pesavento | c21979d | 2017-09-16 14:52:17 -0400 | [diff] [blame] | 137 | int keyType = detail::getEvpPkeyType(m_impl->key); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 138 | switch (keyType) { |
| 139 | case EVP_PKEY_NONE: |
| 140 | BOOST_THROW_EXCEPTION(Error("Failed to determine key type")); |
| 141 | case EVP_PKEY_RSA: |
| 142 | return rsaEncrypt(plainText, plainLen); |
| 143 | default: |
| 144 | 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] | 145 | } |
| 146 | } |
| 147 | |
| 148 | void* |
| 149 | PublicKey::getEvpPkey() const |
| 150 | { |
| 151 | return m_impl->key; |
| 152 | } |
| 153 | |
| 154 | ConstBufferPtr |
| 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); |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 161 | if (len < 0) |
| 162 | BOOST_THROW_EXCEPTION(Error("Cannot convert key to PKCS #8 format")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 163 | |
| 164 | auto buffer = make_shared<Buffer>(pkcs8, len); |
| 165 | OPENSSL_free(pkcs8); |
| 166 | |
| 167 | return buffer; |
| 168 | } |
| 169 | |
| 170 | ConstBufferPtr |
| 171 | PublicKey::rsaEncrypt(const uint8_t* plainText, size_t plainLen) const |
| 172 | { |
| 173 | detail::EvpPkeyCtx ctx(m_impl->key); |
| 174 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 175 | if (EVP_PKEY_encrypt_init(ctx) <= 0) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 176 | BOOST_THROW_EXCEPTION(Error("Failed to initialize encryption context")); |
| 177 | |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 178 | 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] | 179 | BOOST_THROW_EXCEPTION(Error("Failed to set padding")); |
| 180 | |
| 181 | size_t outlen = 0; |
| 182 | // Determine buffer length |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 183 | if (EVP_PKEY_encrypt(ctx, nullptr, &outlen, plainText, plainLen) <= 0) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 184 | BOOST_THROW_EXCEPTION(Error("Failed to estimate output length")); |
| 185 | |
| 186 | auto out = make_shared<Buffer>(outlen); |
Davide Pesavento | f45fa21 | 2017-09-14 17:23:56 -0400 | [diff] [blame] | 187 | if (EVP_PKEY_encrypt(ctx, out->buf(), &outlen, plainText, plainLen) <= 0) |
Muktadir R Chowdhury | 80fba6c | 2017-05-05 15:30:15 -0500 | [diff] [blame] | 188 | BOOST_THROW_EXCEPTION(Error("Failed to encrypt plaintext")); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 189 | |
| 190 | out->resize(outlen); |
| 191 | return out; |
| 192 | } |
| 193 | |
| 194 | } // namespace transform |
| 195 | } // namespace security |
| 196 | } // namespace ndn |