Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 2 | /** |
| 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndncert, a certificate management system based on NDN. |
| 6 | * |
| 7 | * ndncert is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndncert 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 General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ndncert authors and contributors. |
| 19 | */ |
| 20 | |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 21 | #include "crypto-helpers.hpp" |
| 22 | |
| 23 | #include <boost/endian/conversion.hpp> |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 24 | #include <cmath> |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 25 | #include <ndn-cxx/encoding/buffer-stream.hpp> |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 26 | #include <ndn-cxx/security/transform/base64-decode.hpp> |
| 27 | #include <ndn-cxx/security/transform/base64-encode.hpp> |
| 28 | #include <ndn-cxx/security/transform/buffer-source.hpp> |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 29 | #include <ndn-cxx/security/transform/stream-sink.hpp> |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 30 | #include <ndn-cxx/util/random.hpp> |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 31 | #include <openssl/ec.h> |
| 32 | #include <openssl/err.h> |
| 33 | #include <openssl/evp.h> |
| 34 | #include <openssl/hmac.h> |
| 35 | #include <openssl/kdf.h> |
| 36 | #include <openssl/pem.h> |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 37 | |
| 38 | namespace ndn { |
| 39 | namespace ndncert { |
| 40 | |
Zhiyi Zhang | 6312317 | 2020-10-12 14:24:44 -0700 | [diff] [blame] | 41 | struct ECDHState::ECDH_CTX |
| 42 | { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 43 | ~ECDH_CTX() |
| 44 | { |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 45 | // contexts |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 46 | if (ctx_params != nullptr) { |
| 47 | EVP_PKEY_CTX_free(ctx_params); |
| 48 | } |
| 49 | if (ctx_keygen != nullptr) { |
| 50 | EVP_PKEY_CTX_free(ctx_keygen); |
| 51 | } |
| 52 | // Keys |
| 53 | if (privkey != nullptr) { |
| 54 | EVP_PKEY_free(privkey); |
| 55 | } |
| 56 | if (peerkey != nullptr) { |
| 57 | EVP_PKEY_free(peerkey); |
| 58 | } |
| 59 | if (params != nullptr) { |
| 60 | EVP_PKEY_free(params); |
| 61 | } |
| 62 | } |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 63 | EVP_PKEY_CTX* ctx_params = nullptr; |
| 64 | EVP_PKEY_CTX* ctx_keygen = nullptr; |
| 65 | EVP_PKEY* privkey = nullptr; |
| 66 | EVP_PKEY* peerkey = nullptr; |
| 67 | EVP_PKEY* params = nullptr; |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 68 | }; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 69 | |
| 70 | ECDHState::ECDHState() |
| 71 | { |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 72 | m_context = std::make_unique<ECDH_CTX>(); |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 73 | auto EC_NID = NID_X9_62_prime256v1; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 74 | |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 75 | if (nullptr == (m_context->ctx_params = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr))) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 76 | NDN_THROW(std::runtime_error("Could not create context.")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 77 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 78 | if (EVP_PKEY_paramgen_init(m_context->ctx_params) != 1) { |
| 79 | m_context.reset(); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 80 | NDN_THROW(std::runtime_error("Could not initialize parameter generation.")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 81 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 82 | if (1 != EVP_PKEY_CTX_set_ec_paramgen_curve_nid(m_context->ctx_params, EC_NID)) { |
| 83 | m_context.reset(); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 84 | NDN_THROW(std::runtime_error("Likely unknown elliptical curve ID specified.")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 85 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 86 | if (!EVP_PKEY_paramgen(m_context->ctx_params, &m_context->params)) { |
| 87 | m_context.reset(); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 88 | NDN_THROW(std::runtime_error("Could not create parameter object parameters.")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 89 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 90 | if (nullptr == (m_context->ctx_keygen = EVP_PKEY_CTX_new(m_context->params, nullptr))) { |
| 91 | m_context.reset(); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 92 | NDN_THROW(std::runtime_error("Could not create the context for the key generation")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 93 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 94 | if (1 != EVP_PKEY_keygen_init(m_context->ctx_keygen)) { |
| 95 | m_context.reset(); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 96 | NDN_THROW(std::runtime_error("Could not init context for key generation.")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 97 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 98 | if (1 != EVP_PKEY_keygen(m_context->ctx_keygen, &m_context->privkey)) { |
| 99 | m_context.reset(); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 100 | NDN_THROW(std::runtime_error("Could not generate DHE keys in final step")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | ECDHState::~ECDHState() |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 105 | {} |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 106 | |
| 107 | uint8_t* |
| 108 | ECDHState::getRawSelfPubKey() |
| 109 | { |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 110 | auto privECKey = EVP_PKEY_get1_EC_KEY(m_context->privkey); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 111 | if (privECKey == nullptr) { |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 112 | m_context.reset(); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 113 | NDN_THROW(std::runtime_error("Could not get key when calling EVP_PKEY_get1_EC_KEY().")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 114 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 115 | auto ecPoint = EC_KEY_get0_public_key(privECKey); |
| 116 | const EC_GROUP* group = EC_KEY_get0_group(privECKey); |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 117 | m_publicKeyLen = EC_POINT_point2oct(group, ecPoint, POINT_CONVERSION_COMPRESSED, |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 118 | m_publicKey, sizeof(m_publicKey), nullptr); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 119 | EC_KEY_free(privECKey); |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 120 | if (m_publicKeyLen == 0) { |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 121 | m_context.reset(); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 122 | NDN_THROW(std::runtime_error("Could not convert EC_POINTS to octet string when calling EC_POINT_point2oct.")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 123 | } |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 124 | return m_publicKey; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | std::string |
| 128 | ECDHState::getBase64PubKey() |
| 129 | { |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 130 | if (m_publicKeyLen == 0) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 131 | this->getRawSelfPubKey(); |
| 132 | } |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 133 | std::ostringstream os; |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 134 | namespace t = ndn::security::transform; |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 135 | t::bufferSource(m_publicKey, m_publicKeyLen) >> t::base64Encode(false) >> t::streamSink(os); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 136 | return os.str(); |
| 137 | } |
| 138 | |
| 139 | uint8_t* |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 140 | ECDHState::deriveSecret(const uint8_t* peerkey, size_t peerKeySize) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 141 | { |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 142 | auto privECKey = EVP_PKEY_get1_EC_KEY(m_context->privkey); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 143 | if (privECKey == nullptr) { |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 144 | m_context.reset(); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 145 | NDN_THROW(std::runtime_error("Could not get key when calling EVP_PKEY_get1_EC_KEY()")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 146 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 147 | auto group = EC_KEY_get0_group(privECKey); |
| 148 | auto peerPoint = EC_POINT_new(group); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 149 | int result = EC_POINT_oct2point(group, peerPoint, peerkey, peerKeySize, nullptr); |
| 150 | if (result == 0) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 151 | EC_POINT_free(peerPoint); |
| 152 | EC_KEY_free(privECKey); |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 153 | m_context.reset(); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 154 | NDN_THROW(std::runtime_error("Cannot convert peer's key into a EC point when calling EC_POINT_oct2point()")); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 155 | } |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 156 | result = ECDH_compute_key(m_sharedSecret, sizeof(m_sharedSecret), peerPoint, privECKey, nullptr); |
Zhiyi Zhang | 938d703 | 2020-10-12 17:23:45 -0700 | [diff] [blame] | 157 | if (result == -1) { |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 158 | EC_POINT_free(peerPoint); |
| 159 | EC_KEY_free(privECKey); |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 160 | m_context.reset(); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 161 | NDN_THROW(std::runtime_error("Cannot generate ECDH secret when calling ECDH_compute_key()")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 162 | } |
Zhiyi Zhang | 938d703 | 2020-10-12 17:23:45 -0700 | [diff] [blame] | 163 | m_sharedSecretLen = static_cast<size_t>(result); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 164 | EC_POINT_free(peerPoint); |
| 165 | EC_KEY_free(privECKey); |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 166 | return m_sharedSecret; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | uint8_t* |
| 170 | ECDHState::deriveSecret(const std::string& peerKeyStr) |
| 171 | { |
| 172 | namespace t = ndn::security::transform; |
| 173 | OBufferStream os; |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 174 | t::bufferSource(peerKeyStr) >> t::base64Decode(false) >> t::streamSink(os); |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 175 | auto result = os.buf(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 176 | return this->deriveSecret(result->data(), result->size()); |
| 177 | } |
| 178 | |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 179 | void |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 180 | hmacSha256(const uint8_t* data, size_t dataLen, |
| 181 | const uint8_t* key, size_t keyLen, |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 182 | uint8_t* result) |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 183 | { |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 184 | auto ret = HMAC(EVP_sha256(), key, keyLen, |
| 185 | static_cast<const unsigned char*>(data), dataLen, |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 186 | static_cast<unsigned char*>(result), nullptr); |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 187 | if (ret == nullptr) { |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 188 | NDN_THROW(std::runtime_error("Error computing HMAC when calling HMAC()")); |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 189 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 192 | size_t |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 193 | hkdf(const uint8_t* secret, size_t secretLen, const uint8_t* salt, |
| 194 | size_t saltLen, uint8_t* output, size_t outputLen, |
| 195 | const uint8_t* info, size_t infoLen) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 196 | { |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 197 | EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, nullptr); |
| 198 | if (EVP_PKEY_derive_init(pctx) <= 0) { |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 199 | EVP_PKEY_CTX_free(pctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 200 | NDN_THROW(std::runtime_error("HKDF: Cannot init ctx when calling EVP_PKEY_derive_init().")); |
Zhiyi Zhang | a2ce599 | 2019-08-14 17:35:00 -0700 | [diff] [blame] | 201 | } |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 202 | if (EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()) <= 0) { |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 203 | EVP_PKEY_CTX_free(pctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 204 | NDN_THROW(std::runtime_error("HKDF: Cannot set md when calling EVP_PKEY_CTX_set_hkdf_md().")); |
Zhiyi Zhang | a2ce599 | 2019-08-14 17:35:00 -0700 | [diff] [blame] | 205 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 206 | if (EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt, saltLen) <= 0) { |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 207 | EVP_PKEY_CTX_free(pctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 208 | NDN_THROW(std::runtime_error("HKDF: Cannot set salt when calling EVP_PKEY_CTX_set1_hkdf_salt().")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 209 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 210 | if (EVP_PKEY_CTX_set1_hkdf_key(pctx, secret, secretLen) <= 0) { |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 211 | EVP_PKEY_CTX_free(pctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 212 | NDN_THROW(std::runtime_error("HKDF: Cannot set secret when calling EVP_PKEY_CTX_set1_hkdf_key().")); |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 213 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 214 | if (EVP_PKEY_CTX_add1_hkdf_info(pctx, info, infoLen) <= 0) { |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 215 | EVP_PKEY_CTX_free(pctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 216 | NDN_THROW(std::runtime_error("HKDF: Cannot set info when calling EVP_PKEY_CTX_add1_hkdf_info().")); |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 217 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 218 | size_t outLen = outputLen; |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 219 | if (EVP_PKEY_derive(pctx, output, &outLen) <= 0) { |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 220 | EVP_PKEY_CTX_free(pctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 221 | NDN_THROW(std::runtime_error("HKDF: Cannot derive result when calling EVP_PKEY_derive().")); |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 222 | } |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 223 | EVP_PKEY_CTX_free(pctx); |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 224 | return outLen; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 227 | int |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 228 | aesGcm128Encrypt(const uint8_t* plaintext, size_t plaintextLen, const uint8_t* associated, size_t associatedLen, |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 229 | const uint8_t* key, const uint8_t* iv, uint8_t* ciphertext, uint8_t* tag) |
| 230 | { |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 231 | EVP_CIPHER_CTX* ctx; |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 232 | int len; |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 233 | int ciphertextLen; |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 234 | if (!(ctx = EVP_CIPHER_CTX_new())) { |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 235 | NDN_THROW(std::runtime_error("Cannot create and initialise the context when calling EVP_CIPHER_CTX_new()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 236 | } |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 237 | if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), nullptr, nullptr, nullptr)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 238 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 239 | NDN_THROW(std::runtime_error("Cannot initialise the encryption operation when calling EVP_EncryptInit_ex()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 240 | } |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 241 | if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 12, nullptr)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 242 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 243 | NDN_THROW(std::runtime_error("Cannot set IV length when calling EVP_CIPHER_CTX_ctrl()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 244 | } |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 245 | if (1 != EVP_EncryptInit_ex(ctx, nullptr, nullptr, key, iv)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 246 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 247 | NDN_THROW(std::runtime_error("Cannot initialize key and IV when calling EVP_EncryptInit_ex()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 248 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 249 | if (1 != EVP_EncryptUpdate(ctx, nullptr, &len, associated, associatedLen)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 250 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 251 | NDN_THROW(std::runtime_error("Cannot set associated authentication data when calling EVP_EncryptUpdate()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 252 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 253 | if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintextLen)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 254 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 255 | NDN_THROW(std::runtime_error("Cannot encrypt when calling EVP_EncryptUpdate()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 256 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 257 | ciphertextLen = len; |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 258 | if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 259 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 260 | NDN_THROW(std::runtime_error("Cannot finalise the encryption when calling EVP_EncryptFinal_ex()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 261 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 262 | ciphertextLen += len; |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 263 | if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 264 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 265 | NDN_THROW(std::runtime_error("Cannot get tag when calling EVP_CIPHER_CTX_ctrl()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 266 | } |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 267 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 268 | return ciphertextLen; |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | int |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 272 | aesGcm128Decrypt(const uint8_t* ciphertext, size_t ciphertextLen, const uint8_t* associated, size_t associatedLen, |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 273 | const uint8_t* tag, const uint8_t* key, const uint8_t* iv, uint8_t* plaintext) |
| 274 | { |
| 275 | EVP_CIPHER_CTX* ctx; |
| 276 | int len; |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 277 | int plaintextLen; |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 278 | int ret; |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 279 | if (!(ctx = EVP_CIPHER_CTX_new())) { |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 280 | NDN_THROW(std::runtime_error("Cannot create and initialise the context when calling EVP_CIPHER_CTX_new()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 281 | } |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 282 | if (!EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), nullptr, nullptr, nullptr)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 283 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 284 | NDN_THROW(std::runtime_error("Cannot initialise the decryption operation when calling EVP_DecryptInit_ex()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 285 | } |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 286 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 12, nullptr)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 287 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 288 | NDN_THROW(std::runtime_error("Cannot set IV length when calling EVP_CIPHER_CTX_ctrl")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 289 | } |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 290 | if (!EVP_DecryptInit_ex(ctx, nullptr, nullptr, key, iv)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 291 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 292 | NDN_THROW(std::runtime_error("Cannot initialise key and IV when calling EVP_DecryptInit_ex()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 293 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 294 | if (!EVP_DecryptUpdate(ctx, nullptr, &len, associated, associatedLen)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 295 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 296 | NDN_THROW(std::runtime_error("Cannot set associated authentication data when calling EVP_EncryptUpdate()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 297 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 298 | if (!EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertextLen)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 299 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 300 | NDN_THROW(std::runtime_error("Cannot decrypt when calling EVP_DecryptUpdate()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 301 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 302 | plaintextLen = len; |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 303 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, (void*)tag)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 304 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 305 | NDN_THROW(std::runtime_error("Cannot set tag value when calling EVP_CIPHER_CTX_ctrl")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 306 | } |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 307 | ret = EVP_DecryptFinal_ex(ctx, plaintext + len, &len); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 308 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 309 | if (ret > 0) { |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 310 | plaintextLen += len; |
| 311 | return plaintextLen; |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 312 | } |
| 313 | else { |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 314 | return -1; |
| 315 | } |
| 316 | } |
| 317 | |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 318 | Block |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 319 | encodeBlockWithAesGcm128(uint32_t tlvType, const uint8_t* key, const uint8_t* payload, size_t payloadSize, |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 320 | const uint8_t* associatedData, size_t associatedDataSize, uint32_t& counter) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 321 | { |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 322 | Buffer iv(12); |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 323 | random::generateSecureBytes(iv.data(), iv.size()); |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 324 | if (tlvType == ndn::tlv::ApplicationParameters) { |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 325 | // requester |
| 326 | iv[0] &= ~(1UL << 7); |
| 327 | } |
| 328 | else { |
| 329 | // CA |
| 330 | iv[0] |= 1UL << 7; |
| 331 | } |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 332 | uint32_t temp = boost::endian::native_to_big(counter); |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 333 | std::memcpy(&iv[8], reinterpret_cast<const uint8_t*>(&temp), 4); |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 334 | uint32_t increment = std::ceil((payloadSize + 16 - 1)/16); |
| 335 | if (std::numeric_limits<uint32_t>::max() - counter < increment) { |
| 336 | // simply set counter to be 0. Will not hurt the property of being unique. |
| 337 | counter = 0; |
| 338 | } |
| 339 | else { |
| 340 | counter += increment; |
| 341 | } |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 342 | |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 343 | Buffer encryptedPayload(payloadSize); |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 344 | uint8_t tag[16]; |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 345 | size_t encryptedPayloadLen = aesGcm128Encrypt(payload, payloadSize, associatedData, associatedDataSize, |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 346 | key, iv.data(), encryptedPayload.data(), tag); |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 347 | auto content = makeEmptyBlock(tlvType); |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 348 | content.push_back(makeBinaryBlock(tlv::InitializationVector, iv.data(), iv.size())); |
| 349 | content.push_back(makeBinaryBlock(tlv::AuthenticationTag, tag, 16)); |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 350 | content.push_back(makeBinaryBlock(tlv::EncryptedPayload, encryptedPayload.data(), encryptedPayloadLen)); |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 351 | content.encode(); |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 352 | return content; |
| 353 | } |
| 354 | |
| 355 | Buffer |
| 356 | decodeBlockWithAesGcm128(const Block& block, const uint8_t* key, const uint8_t* associatedData, size_t associatedDataSize) |
| 357 | { |
| 358 | block.parse(); |
Zhiyi Zhang | cace6ab | 2020-10-16 21:51:44 -0700 | [diff] [blame] | 359 | Buffer result(block.get(tlv::EncryptedPayload).value_size()); |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 360 | int resultLen = aesGcm128Decrypt(block.get(tlv::EncryptedPayload).value(), |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 361 | block.get(tlv::EncryptedPayload).value_size(), |
| 362 | associatedData, associatedDataSize, block.get(tlv::AuthenticationTag).value(), |
| 363 | key, block.get(tlv::InitializationVector).value(), result.data()); |
| 364 | if (resultLen == -1 || resultLen != (int)block.get(tlv::EncryptedPayload).value_size()) { |
| 365 | return Buffer(); |
| 366 | } |
| 367 | return result; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 370 | } // namespace ndncert |
| 371 | } // namespace ndn |