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 | |
| 21 | #include "crypto-helper.hpp" |
| 22 | #include "../logging.hpp" |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 23 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 24 | #include <openssl/err.h> |
Zhiyi Zhang | a2ce599 | 2019-08-14 17:35:00 -0700 | [diff] [blame] | 25 | #include <openssl/hmac.h> |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame^] | 26 | #include <openssl/pem.h> |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 27 | |
| 28 | #include <ndn-cxx/encoding/buffer-stream.hpp> |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 29 | #include <ndn-cxx/security/transform/base64-decode.hpp> |
| 30 | #include <ndn-cxx/security/transform/base64-encode.hpp> |
| 31 | #include <ndn-cxx/security/transform/buffer-source.hpp> |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 32 | #include <ndn-cxx/security/transform/private-key.hpp> |
| 33 | #include <ndn-cxx/security/transform/signer-filter.hpp> |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 34 | #include <ndn-cxx/security/transform/step-source.hpp> |
| 35 | #include <ndn-cxx/security/transform/stream-sink.hpp> |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 36 | |
| 37 | namespace ndn { |
| 38 | namespace ndncert { |
| 39 | |
| 40 | const size_t HASH_SIZE = 32; |
| 41 | |
| 42 | _LOG_INIT(crypto-support); |
| 43 | |
| 44 | ECDHState::ECDHState() |
| 45 | { |
| 46 | OpenSSL_add_all_algorithms(); |
| 47 | context = std::make_unique<ECDH_CTX>(); |
| 48 | context->EC_NID = NID_X9_62_prime256v1; |
| 49 | |
| 50 | // Create the context for parameter generation |
| 51 | if (nullptr == (context->ctx_params = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr))) { |
| 52 | handleErrors("Could not create context contexts."); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | // Initialise the parameter generation |
| 57 | if (EVP_PKEY_paramgen_init(context->ctx_params) != 1) { |
| 58 | handleErrors("Could not initialize parameter generation."); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | // We're going to use the ANSI X9.62 Prime 256v1 curve |
| 63 | if (1 != EVP_PKEY_CTX_set_ec_paramgen_curve_nid(context->ctx_params, context->EC_NID)) { |
| 64 | handleErrors("Likely unknown elliptical curve ID specified."); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | // Create the parameter object params |
| 69 | if (!EVP_PKEY_paramgen(context->ctx_params, &context->params)) { |
| 70 | // the generated key is written to context->params |
| 71 | handleErrors("Could not create parameter object parameters."); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | // Create the context for the key generation |
| 76 | if (nullptr == (context->ctx_keygen = EVP_PKEY_CTX_new(context->params, nullptr))) { |
| 77 | //The EVP_PKEY_CTX_new() function allocates public key algorithm context using |
| 78 | //the algorithm specified in pkey and ENGINE e (in this case nullptr). |
| 79 | handleErrors("Could not create the context for the key generation"); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | // initializes a public key algorithm context |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame^] | 84 | if (1 != EVP_PKEY_keygen_init(context->ctx_keygen)) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 85 | handleErrors("Could not init context for key generation."); |
| 86 | return; |
| 87 | } |
| 88 | if (1 != EVP_PKEY_keygen(context->ctx_keygen, &context->privkey)) { |
| 89 | //performs a key generation operation, the generated key is written to context->privkey. |
| 90 | handleErrors("Could not generate DHE keys in final step"); |
| 91 | return; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | ECDHState::~ECDHState() |
| 96 | { |
| 97 | // Contexts |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame^] | 98 | if (context->ctx_params != nullptr) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 99 | EVP_PKEY_CTX_free(context->ctx_params); |
| 100 | } |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame^] | 101 | if (context->ctx_keygen != nullptr) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 102 | EVP_PKEY_CTX_free(context->ctx_keygen); |
| 103 | } |
| 104 | |
| 105 | // Keys |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame^] | 106 | if (context->privkey != nullptr) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 107 | EVP_PKEY_free(context->privkey); |
| 108 | } |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame^] | 109 | if (context->peerkey != nullptr) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 110 | EVP_PKEY_free(context->peerkey); |
| 111 | } |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame^] | 112 | if (context->params != nullptr) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 113 | EVP_PKEY_free(context->params); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | uint8_t* |
| 118 | ECDHState::getRawSelfPubKey() |
| 119 | { |
| 120 | auto privECKey = EVP_PKEY_get1_EC_KEY(context->privkey); |
| 121 | |
| 122 | if (privECKey == nullptr) { |
| 123 | handleErrors("Could not get referenced key when calling EVP_PKEY_get1_EC_KEY()."); |
| 124 | return nullptr; |
| 125 | } |
| 126 | |
| 127 | auto ecPoint = EC_KEY_get0_public_key(privECKey); |
| 128 | const EC_GROUP* group = EC_KEY_get0_group(privECKey); |
| 129 | context->publicKeyLen = EC_POINT_point2oct(group, ecPoint, POINT_CONVERSION_COMPRESSED, |
| 130 | context->publicKey, 256, nullptr); |
| 131 | EC_KEY_free(privECKey); |
| 132 | if (context->publicKeyLen == 0) { |
| 133 | handleErrors("Could not convert EC_POINTS to octet string when calling EC_POINT_point2oct."); |
| 134 | return nullptr; |
| 135 | } |
| 136 | |
| 137 | return context->publicKey; |
| 138 | } |
| 139 | |
| 140 | std::string |
| 141 | ECDHState::getBase64PubKey() |
| 142 | { |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 143 | namespace t = ndn::security::transform; |
| 144 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 145 | if (context->publicKeyLen == 0) { |
| 146 | this->getRawSelfPubKey(); |
| 147 | } |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 148 | |
| 149 | std::ostringstream os; |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame^] | 150 | t::bufferSource(context->publicKey, context->publicKeyLen) >> t::base64Encode(false) >> t::streamSink(os); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 151 | return os.str(); |
| 152 | } |
| 153 | |
| 154 | uint8_t* |
| 155 | ECDHState::deriveSecret(const uint8_t* peerkey, int peerKeySize) |
| 156 | { |
| 157 | auto privECKey = EVP_PKEY_get1_EC_KEY(context->privkey); |
| 158 | |
| 159 | if (privECKey == nullptr) { |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 160 | handleErrors("Could not get referenced key when calling EVP_PKEY_get1_EC_KEY()"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 161 | return nullptr; |
| 162 | } |
| 163 | |
| 164 | auto group = EC_KEY_get0_group(privECKey); |
| 165 | auto peerPoint = EC_POINT_new(group); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 166 | int result = EC_POINT_oct2point(group, peerPoint, peerkey, peerKeySize, nullptr); |
| 167 | if (result == 0) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 168 | EC_POINT_free(peerPoint); |
| 169 | EC_KEY_free(privECKey); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 170 | handleErrors("Cannot convert peer's key into a EC point when calling EC_POINT_oct2point()"); |
| 171 | } |
| 172 | |
| 173 | if (-1 == (context->sharedSecretLen = ECDH_compute_key(context->sharedSecret, 256, |
| 174 | peerPoint, privECKey, nullptr))) { |
| 175 | EC_POINT_free(peerPoint); |
| 176 | EC_KEY_free(privECKey); |
| 177 | handleErrors("Cannot generate ECDH secret when calling ECDH_compute_key()"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 178 | } |
| 179 | EC_POINT_free(peerPoint); |
| 180 | EC_KEY_free(privECKey); |
| 181 | return context->sharedSecret; |
| 182 | } |
| 183 | |
| 184 | uint8_t* |
| 185 | ECDHState::deriveSecret(const std::string& peerKeyStr) |
| 186 | { |
| 187 | namespace t = ndn::security::transform; |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 188 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 189 | OBufferStream os; |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 190 | t::bufferSource(peerKeyStr) >> t::base64Decode(false) >> t::streamSink(os); |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 191 | auto result = os.buf(); |
| 192 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 193 | return this->deriveSecret(result->data(), result->size()); |
| 194 | } |
| 195 | |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 196 | int |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame^] | 197 | ndn_compute_hmac_sha256(const uint8_t* data, const unsigned data_length, |
| 198 | const uint8_t* key, const unsigned key_length, |
| 199 | uint8_t* prk) |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 200 | { |
Zhiyi Zhang | a2ce599 | 2019-08-14 17:35:00 -0700 | [diff] [blame] | 201 | HMAC(EVP_sha256(), key, key_length, |
| 202 | (unsigned char*)data, data_length, |
| 203 | (unsigned char*)prk, nullptr); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 207 | // avoid dependency on OpenSSL >= 1.1 |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 208 | int |
| 209 | hkdf(const uint8_t* secret, int secretLen, const uint8_t* salt, |
| 210 | int saltLen, uint8_t* okm, int okm_len, |
| 211 | const uint8_t* info, int info_len) |
| 212 | { |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 213 | namespace t = ndn::security::transform; |
| 214 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 215 | // hkdf generate prk |
| 216 | uint8_t prk[HASH_SIZE]; |
Zhiyi Zhang | a2ce599 | 2019-08-14 17:35:00 -0700 | [diff] [blame] | 217 | if (saltLen == 0) { |
| 218 | uint8_t realSalt[HASH_SIZE] = {0}; |
| 219 | ndn_compute_hmac_sha256(secret, secretLen, realSalt, HASH_SIZE, prk); |
| 220 | } |
| 221 | else { |
| 222 | ndn_compute_hmac_sha256(secret, secretLen, salt, saltLen, prk); |
| 223 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 224 | |
| 225 | // hkdf expand |
| 226 | uint8_t prev[HASH_SIZE] = {0}; |
| 227 | int done_len = 0, dig_len = HASH_SIZE, n = okm_len / dig_len; |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 228 | if (okm_len % dig_len) |
| 229 | n++; |
| 230 | if (n > 255 || okm == nullptr) |
| 231 | return 0; |
| 232 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 233 | for (int i = 1; i <= n; i++) { |
| 234 | size_t copy_len; |
| 235 | const uint8_t ctr = i; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 236 | |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 237 | t::StepSource source; |
| 238 | t::PrivateKey privKey; |
| 239 | privKey.loadRaw(KeyType::HMAC, prk, dig_len); |
| 240 | OBufferStream os; |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame^] | 241 | source >> t::signerFilter(DigestAlgorithm::SHA256, privKey) >> t::streamSink(os); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 242 | |
| 243 | if (i > 1) { |
| 244 | source.write(prev, dig_len); |
| 245 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 246 | source.write(info, info_len); |
| 247 | source.write(&ctr, 1); |
| 248 | source.end(); |
| 249 | |
| 250 | auto result = os.buf(); |
| 251 | memcpy(prev, result->data(), dig_len); |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 252 | copy_len = (done_len + dig_len > okm_len) ? okm_len - done_len : dig_len; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 253 | memcpy(okm + done_len, prev, copy_len); |
| 254 | done_len += copy_len; |
| 255 | } |
| 256 | return done_len; |
| 257 | } |
| 258 | |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 259 | int |
| 260 | aes_gcm_128_encrypt(const uint8_t* plaintext, size_t plaintext_len, const uint8_t* associated, size_t associated_len, |
| 261 | const uint8_t* key, const uint8_t* iv, uint8_t* ciphertext, uint8_t* tag) |
| 262 | { |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame^] | 263 | EVP_CIPHER_CTX* ctx; |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 264 | int len; |
| 265 | int ciphertext_len; |
| 266 | |
| 267 | // Create and initialise the context |
| 268 | if (!(ctx = EVP_CIPHER_CTX_new())) { |
| 269 | handleErrors("Cannot create and initialise the context when calling EVP_CIPHER_CTX_new()"); |
| 270 | } |
| 271 | |
| 272 | // Initialise the encryption operation. |
| 273 | if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), nullptr, nullptr, nullptr)) { |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame^] | 274 | handleErrors("Cannot initialise the encryption operation when calling EVP_EncryptInit_ex()"); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | // Set IV length if default 12 bytes (96 bits) is not appropriate |
| 278 | if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 12, nullptr)) { |
| 279 | handleErrors("Cannot set IV length when calling EVP_CIPHER_CTX_ctrl()"); |
| 280 | } |
| 281 | |
| 282 | // Initialise key and IV |
| 283 | if (1 != EVP_EncryptInit_ex(ctx, nullptr, nullptr, key, iv)) { |
| 284 | handleErrors("Cannot initialize key and IV when calling EVP_EncryptInit_ex()"); |
| 285 | } |
| 286 | |
| 287 | // Provide any AAD data. This can be called zero or more times as required |
| 288 | if (1 != EVP_EncryptUpdate(ctx, nullptr, &len, associated, associated_len)) { |
| 289 | handleErrors("Cannot set associated authentication data when calling EVP_EncryptUpdate()"); |
| 290 | } |
| 291 | |
| 292 | // Provide the message to be encrypted, and obtain the encrypted output. |
| 293 | // EVP_EncryptUpdate can be called multiple times if necessary |
| 294 | if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)) { |
| 295 | handleErrors("Cannot encrypt when calling EVP_EncryptUpdate()"); |
| 296 | } |
| 297 | ciphertext_len = len; |
| 298 | |
| 299 | // Finalise the encryption. Normally ciphertext bytes may be written at |
| 300 | // this stage, but this does not occur in GCM mode |
| 301 | if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) { |
| 302 | handleErrors("Cannot finalise the encryption when calling EVP_EncryptFinal_ex()"); |
| 303 | } |
| 304 | ciphertext_len += len; |
| 305 | |
| 306 | // Get the tag |
| 307 | if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag)) { |
| 308 | handleErrors("Cannot get tag when calling EVP_CIPHER_CTX_ctrl()"); |
| 309 | } |
| 310 | |
| 311 | // Clean up |
| 312 | EVP_CIPHER_CTX_free(ctx); |
| 313 | return ciphertext_len; |
| 314 | } |
| 315 | |
| 316 | int |
| 317 | aes_gcm_128_decrypt(const uint8_t* ciphertext, size_t ciphertext_len, const uint8_t* associated, size_t associated_len, |
| 318 | const uint8_t* tag, const uint8_t* key, const uint8_t* iv, uint8_t* plaintext) |
| 319 | { |
| 320 | EVP_CIPHER_CTX* ctx; |
| 321 | int len; |
| 322 | int plaintext_len; |
| 323 | int ret; |
| 324 | |
| 325 | // Create and initialise the context |
| 326 | if (!(ctx = EVP_CIPHER_CTX_new())) { |
| 327 | handleErrors("Cannot create and initialise the context when calling EVP_CIPHER_CTX_new()"); |
| 328 | } |
| 329 | |
| 330 | // Initialise the decryption operation. |
| 331 | if (!EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), nullptr, nullptr, nullptr)) { |
| 332 | handleErrors("Cannot initialise the decryption operation when calling EVP_DecryptInit_ex()"); |
| 333 | } |
| 334 | |
| 335 | // Set IV length. Not necessary if this is 12 bytes (96 bits) |
| 336 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 12, nullptr)) { |
| 337 | handleErrors("Cannot set IV length when calling EVP_CIPHER_CTX_ctrl"); |
| 338 | } |
| 339 | |
| 340 | // Initialise key and IV |
| 341 | if (!EVP_DecryptInit_ex(ctx, nullptr, nullptr, key, iv)) { |
| 342 | handleErrors("Cannot initialise key and IV when calling EVP_DecryptInit_ex()"); |
| 343 | } |
| 344 | |
| 345 | // Provide any AAD data. This can be called zero or more times as required |
| 346 | if (!EVP_DecryptUpdate(ctx, nullptr, &len, associated, associated_len)) { |
| 347 | handleErrors("Cannot set associated authentication data when calling EVP_EncryptUpdate()"); |
| 348 | } |
| 349 | |
| 350 | // Provide the message to be decrypted, and obtain the plaintext output. |
| 351 | // EVP_DecryptUpdate can be called multiple times if necessary |
| 352 | if (!EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len)) { |
| 353 | handleErrors("Cannot decrypt when calling EVP_DecryptUpdate()"); |
| 354 | } |
| 355 | plaintext_len = len; |
| 356 | |
| 357 | // Set expected tag value. Works in OpenSSL 1.0.1d and later |
| 358 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, (void*)tag)) { |
| 359 | handleErrors("Cannot set tag value when calling EVP_CIPHER_CTX_ctrl"); |
| 360 | } |
| 361 | |
| 362 | // Finalise the decryption. A positive return value indicates success, |
| 363 | // anything else is a failure - the plaintext is not trustworthy. |
| 364 | ret = EVP_DecryptFinal_ex(ctx, plaintext + len, &len); |
| 365 | |
| 366 | // Clean up |
| 367 | EVP_CIPHER_CTX_free(ctx); |
| 368 | |
| 369 | if (ret > 0) { |
| 370 | // Success |
| 371 | plaintext_len += len; |
| 372 | return plaintext_len; |
| 373 | } |
| 374 | else { |
| 375 | // Verify failed |
| 376 | return -1; |
| 377 | } |
| 378 | } |
| 379 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 380 | void |
| 381 | handleErrors(const std::string& errorInfo) |
| 382 | { |
| 383 | _LOG_DEBUG("Error in CRYPTO SUPPORT " << errorInfo); |
| 384 | BOOST_THROW_EXCEPTION(CryptoError("Error in CRYPTO SUPPORT: " + errorInfo)); |
| 385 | } |
| 386 | |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame^] | 387 | } // namespace ndncert |
| 388 | } // namespace ndn |