Coverage Report

Created: 2026-12-01 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/bind9/lib/isc/crypto/ossl3.c
Line
Count
Source
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
 * SPDX-License-Identifier: MPL-2.0
5
 *
6
 * This Source Code Form is subject to the terms of the Mozilla Public
7
 * License, v. 2.0. If a copy of the MPL was not distributed with this
8
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9
 *
10
 * See the COPYRIGHT file distributed with this work for additional
11
 * information regarding copyright ownership.
12
 */
13
14
#include <stdbool.h>
15
#include <stdint.h>
16
17
#include <openssl/core_names.h>
18
#include <openssl/crypto.h>
19
#include <openssl/err.h>
20
#include <openssl/evp.h>
21
#include <openssl/kdf.h>
22
#include <openssl/provider.h>
23
#include <openssl/rand.h>
24
#include <openssl/ssl.h>
25
26
#include <isc/attributes.h>
27
#include <isc/buffer.h>
28
#include <isc/crypto.h>
29
#include <isc/hmac.h>
30
#include <isc/log.h>
31
#include <isc/magic.h>
32
#include <isc/md.h>
33
#include <isc/mem.h>
34
#include <isc/ossl_wrap.h>
35
#include <isc/overflow.h>
36
#include <isc/region.h>
37
#include <isc/safe.h>
38
#include <isc/util.h>
39
40
#define CRYPTO_ERROR(fn)                                           \
41
0
  isc__ossl_wrap_logged_toresult(                            \
42
0
    ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_CRYPTO, fn, \
43
0
    ISC_R_CRYPTOFAILURE, __FILE__, __LINE__)
44
45
struct isc_hmac_key {
46
  uint32_t magic;
47
  uint32_t len;
48
  isc_mem_t *mctx;
49
  const OSSL_PARAM *params;
50
  uint8_t secret[] ISC_ATTR_COUNTED_BY(len);
51
};
52
53
struct isc_crypto_quic_hp_protect {
54
  uint32_t magic;
55
  isc_mem_t *mctx;
56
  EVP_CIPHER_CTX *ctx;
57
};
58
59
STATIC_ASSERT(ISC_TYPES_COMPATIBLE(isc_crypto_aead_t, EVP_CIPHER_CTX),
60
        "isc_crypto_aead_t is not compatible with EVP_CIPHER_CTX");
61
62
constexpr uint32_t quic_hp_protect_magic = ISC_MAGIC('C', 'Q', 'h', 'p');
63
constexpr uint32_t hmac_key_magic = ISC_MAGIC('H', 'M', 'A', 'C');
64
65
static OSSL_PROVIDER *base = NULL, *fips = NULL;
66
67
/*
68
 * Because HKDF-Expand-Label is defined in the RFC of TLS 1.3, OpenSSL
69
 * has named the algorithm as TLS1.3 KDF internally.
70
 */
71
static EVP_KDF *evp_tls_1_3_kdf = NULL;
72
static EVP_KDF *evp_hkdf = NULL;
73
74
static EVP_MAC *evp_hmac = NULL;
75
76
/* AEAD */
77
static EVP_CIPHER *evp_aes_128_gcm = NULL;
78
static EVP_CIPHER *evp_aes_256_gcm = NULL;
79
static EVP_CIPHER *evp_chacha20poly1305 = NULL;
80
81
/* QUIC Header Protection */
82
static EVP_CIPHER *evp_aes_128_ctr = NULL;
83
static EVP_CIPHER *evp_aes_256_ctr = NULL;
84
static EVP_CIPHER *evp_chacha20 = NULL;
85
86
static isc_constregion_t md_to_name[ISC_MD_MAX] = {
87
  [ISC_MD_UNKNOWN] = { 0 },
88
  [ISC_MD_MD5] = { .base = "MD5", .length = sizeof("MD5") - 1 },
89
  [ISC_MD_SHA1] = { .base = "SHA1", .length = sizeof("SHA1") - 1 },
90
  [ISC_MD_SHA224] = { .base = "SHA2-224",
91
          .length = sizeof("SHA2-224") - 1 },
92
  [ISC_MD_SHA256] = { .base = "SHA2-256",
93
          .length = sizeof("SHA2-256") - 1 },
94
  [ISC_MD_SHA384] = { .base = "SHA2-384",
95
          .length = sizeof("SHA2-384") - 1 },
96
  [ISC_MD_SHA512] = { .base = "SHA2-512",
97
          .length = sizeof("SHA2-512") - 1 },
98
  [ISC_MD_SHAKE256] = { .base = "SHAKE-256",
99
            .length = sizeof("SHAKE-256") - 1 },
100
};
101
102
static OSSL_PARAM md_to_hmac_params[ISC_MD_MAX][2] = {
103
  [ISC_MD_UNKNOWN] = { OSSL_PARAM_END },
104
  [ISC_MD_MD5] = {
105
    OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, UNCONST("MD5"), sizeof("MD5") - 1),
106
    OSSL_PARAM_END,
107
  },
108
  [ISC_MD_SHA1] = {
109
    OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, UNCONST("SHA1"), sizeof("SHA1") - 1),
110
    OSSL_PARAM_END,
111
  },
112
  [ISC_MD_SHA224] = {
113
    OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, UNCONST("SHA2-224"), sizeof("SHA2-224") - 1),
114
    OSSL_PARAM_END,
115
  },
116
  [ISC_MD_SHA256] = {
117
    OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, UNCONST("SHA2-256"), sizeof("SHA2-256") - 1),
118
    OSSL_PARAM_END,
119
  },
120
  [ISC_MD_SHA384] = {
121
    OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, UNCONST("SHA2-384"), sizeof("SHA2-384") - 1),
122
    OSSL_PARAM_END,
123
  },
124
  [ISC_MD_SHA512] = {
125
    OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, UNCONST("SHA2-512"), sizeof("SHA2-512") - 1),
126
    OSSL_PARAM_END,
127
  },
128
  [ISC_MD_SHAKE256] = {
129
    OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, UNCONST("SHAKE-256"), sizeof("SHAKE-256") - 1), OSSL_PARAM_END, },
130
};
131
132
#define md_register_algorithm(alg)                                             \
133
132
  {                                                                      \
134
132
    REQUIRE(isc__crypto_md[ISC_MD_##alg] == NULL);                 \
135
132
    isc__crypto_md[ISC_MD_##alg] = EVP_MD_fetch(NULL, #alg, NULL); \
136
132
    if (isc__crypto_md[ISC_MD_##alg] == NULL) {                    \
137
0
      ERR_clear_error();                                     \
138
0
    }                                                              \
139
132
  }
140
141
static isc_result_t
142
22
register_algorithms(void) {
143
22
  if (!isc_crypto_fips_mode()) {
144
22
    md_register_algorithm(MD5);
145
146
22
    INSIST(evp_chacha20poly1305 == NULL);
147
22
    evp_chacha20poly1305 =
148
22
      EVP_CIPHER_fetch(NULL, "ChaCha20-Poly1305", NULL);
149
150
22
    INSIST(evp_chacha20 == NULL);
151
22
    evp_chacha20 = EVP_CIPHER_fetch(NULL, "ChaCha20", NULL);
152
22
  }
153
154
22
  md_register_algorithm(SHA1);
155
22
  md_register_algorithm(SHA224);
156
22
  md_register_algorithm(SHA256);
157
22
  md_register_algorithm(SHA384);
158
22
  md_register_algorithm(SHA512);
159
#if HAVE_OPENSSL_MLDSA44
160
  md_register_algorithm(SHAKE256);
161
#endif
162
163
22
  INSIST(evp_aes_128_gcm == NULL);
164
22
  evp_aes_128_gcm = EVP_CIPHER_fetch(NULL, "AES-128-GCM", NULL);
165
166
22
  INSIST(evp_aes_256_gcm == NULL);
167
22
  evp_aes_256_gcm = EVP_CIPHER_fetch(NULL, "AES-256-GCM", NULL);
168
169
22
  INSIST(evp_aes_128_ctr == NULL);
170
22
  evp_aes_128_ctr = EVP_CIPHER_fetch(NULL, "AES-128-CTR", NULL);
171
172
22
  INSIST(evp_aes_256_ctr == NULL);
173
22
  evp_aes_256_ctr = EVP_CIPHER_fetch(NULL, "AES-256-CTR", NULL);
174
175
  /* We _must_ have HMAC */
176
22
  evp_hmac = EVP_MAC_fetch(NULL, "HMAC", NULL);
177
22
  if (evp_hmac == NULL) {
178
0
    FATAL_ERROR("OpenSSL failed to find an HMAC implementation. "
179
0
          "Please make sure the default provider has an "
180
0
          "EVP_MAC-HMAC implementation");
181
0
  }
182
183
22
  evp_tls_1_3_kdf = EVP_KDF_fetch(NULL, OSSL_KDF_NAME_TLS1_3_KDF, NULL);
184
22
  if (evp_tls_1_3_kdf == NULL) {
185
0
    FATAL_ERROR(
186
0
      "OpenSSL failed to find an TLS 1.3 KDF implementation."
187
0
      "Please make sure the default provider has an "
188
0
      "EVP_KDF-TLS13_KDF implementation");
189
0
  }
190
191
22
  evp_hkdf = EVP_KDF_fetch(NULL, OSSL_KDF_NAME_HKDF, NULL);
192
22
  if (evp_hkdf == NULL) {
193
0
    FATAL_ERROR("OpenSSL failed to find an HKDF implementation. "
194
0
          "Please make sure the default provider has an "
195
0
          "EVP_KDF-HKDF implementation");
196
0
  }
197
198
22
  ERR_clear_error();
199
200
22
  return ISC_R_SUCCESS;
201
22
}
202
203
static void
204
0
unregister_algorithms(void) {
205
0
  size_t i;
206
207
0
  INSIST(evp_hkdf != NULL);
208
0
  EVP_KDF_free(evp_hkdf);
209
0
  evp_hkdf = NULL;
210
211
0
  INSIST(evp_tls_1_3_kdf != NULL);
212
0
  EVP_KDF_free(evp_tls_1_3_kdf);
213
0
  evp_tls_1_3_kdf = NULL;
214
215
0
  INSIST(evp_hmac != NULL);
216
0
  EVP_MAC_free(evp_hmac);
217
0
  evp_hmac = NULL;
218
219
0
  EVP_CIPHER_free(evp_chacha20);
220
0
  evp_chacha20 = NULL;
221
222
0
  EVP_CIPHER_free(evp_aes_256_ctr);
223
0
  evp_aes_256_ctr = NULL;
224
225
0
  EVP_CIPHER_free(evp_aes_128_ctr);
226
0
  evp_aes_128_ctr = NULL;
227
228
0
  EVP_CIPHER_free(evp_chacha20poly1305);
229
0
  evp_chacha20poly1305 = NULL;
230
231
0
  EVP_CIPHER_free(evp_aes_256_gcm);
232
0
  evp_aes_256_gcm = NULL;
233
234
0
  EVP_CIPHER_free(evp_aes_128_gcm);
235
0
  evp_aes_128_gcm = NULL;
236
237
0
  for (i = 0; i < ISC_MD_MAX; i++) {
238
0
    if (isc__crypto_md[i] != NULL) {
239
0
      EVP_MD_free(isc__crypto_md[i]);
240
0
      isc__crypto_md[i] = NULL;
241
0
    }
242
0
  }
243
0
}
244
245
#undef md_register_algorithm
246
247
/*
248
 * HMAC
249
 */
250
251
/*
252
 * Do not call EVP_Q_mac or HMAC (since it calls EVP_Q_mac internally)
253
 *
254
 * Each invocation of the EVP_Q_mac function causes an explicit fetch.
255
 */
256
isc_result_t
257
isc_hmac(isc_md_type_t type, const void *key, const size_t keylen,
258
   const unsigned char *buf, const size_t len, unsigned char *digest,
259
132
   unsigned int *digestlen) {
260
132
  EVP_MAC_CTX *ctx;
261
132
  size_t maclen;
262
263
132
  REQUIRE(type < ISC_MD_MAX);
264
265
132
  if (isc__crypto_md[type] == NULL) {
266
0
    return ISC_R_NOTIMPLEMENTED;
267
0
  }
268
269
132
  if (*digestlen < (unsigned int)EVP_MD_size(isc__crypto_md[type])) {
270
0
    return ISC_R_NOSPACE;
271
0
  }
272
273
132
  ctx = EVP_MAC_CTX_new(evp_hmac);
274
132
  RUNTIME_CHECK(ctx != NULL);
275
276
132
  if (EVP_MAC_init(ctx, key, keylen, md_to_hmac_params[type]) != 1) {
277
0
    goto fail;
278
0
  }
279
280
132
  if (EVP_MAC_update(ctx, buf, len) != 1) {
281
0
    goto fail;
282
0
  }
283
284
132
  maclen = *digestlen;
285
132
  if (EVP_MAC_final(ctx, digest, &maclen, maclen) != 1) {
286
0
    goto fail;
287
0
  }
288
289
132
  *digestlen = maclen;
290
291
132
  EVP_MAC_CTX_free(ctx);
292
132
  return ISC_R_SUCCESS;
293
294
0
fail:
295
0
  ERR_clear_error();
296
0
  EVP_MAC_CTX_free(ctx);
297
0
  return ISC_R_CRYPTOFAILURE;
298
132
}
299
300
/*
301
 * You do not need to process the key to fit the block size.
302
 *
303
 * https://github.com/openssl/openssl/blob/925e4fba1098036e8f8d22652cff6f64c5c7d571/crypto/hmac/hmac.c#L61-L80
304
 */
305
isc_result_t
306
isc_hmac_key_create(isc_md_type_t type, const void *secret, const size_t len,
307
2
        isc_mem_t *mctx, isc_hmac_key_t **keyp) {
308
2
  isc_hmac_key_t *key;
309
2
  uint8_t digest[ISC_MAX_MD_SIZE];
310
2
  unsigned int digest_len = sizeof(digest);
311
2
  size_t key_len;
312
313
2
  REQUIRE(keyp != NULL && *keyp == NULL);
314
2
  REQUIRE(type < ISC_MD_MAX);
315
316
2
  if (isc__crypto_md[type] == NULL) {
317
0
    return ISC_R_NOTIMPLEMENTED;
318
0
  }
319
320
2
  if (len > (size_t)EVP_MD_block_size(isc__crypto_md[type])) {
321
0
    RETERR(isc_md(type, secret, len, digest, &digest_len));
322
0
    secret = digest;
323
0
    key_len = digest_len;
324
2
  } else {
325
2
    key_len = len;
326
2
  }
327
328
2
  key = isc_mem_get(mctx, STRUCT_FLEX_SIZE(key, secret, key_len));
329
2
  *key = (isc_hmac_key_t){
330
2
    .magic = hmac_key_magic,
331
2
    .len = key_len,
332
2
    .params = md_to_hmac_params[type],
333
2
  };
334
2
  memmove(key->secret, secret, key_len);
335
2
  isc_mem_attach(mctx, &key->mctx);
336
337
  /* The digest of a long secret is as good as the secret itself. */
338
2
  isc_safe_memwipe(digest, sizeof(digest));
339
340
2
  *keyp = key;
341
342
2
  return ISC_R_SUCCESS;
343
2
}
344
345
void
346
0
isc_hmac_key_destroy(isc_hmac_key_t **keyp) {
347
0
  isc_hmac_key_t *key;
348
349
0
  REQUIRE(keyp != NULL && *keyp != NULL);
350
0
  REQUIRE((*keyp)->magic == hmac_key_magic);
351
352
0
  key = *keyp;
353
0
  *keyp = NULL;
354
355
0
  key->magic = 0x00;
356
357
0
  isc_safe_memwipe(key->secret, key->len);
358
0
  isc_mem_putanddetach(&key->mctx, key,
359
0
           STRUCT_FLEX_SIZE(key, secret, key->len));
360
0
}
361
362
isc_region_t
363
4
isc_hmac_key_expose(isc_hmac_key_t *key) {
364
4
  REQUIRE(key != NULL && key->magic == hmac_key_magic);
365
366
4
  return (isc_region_t){ .base = key->secret, .length = key->len };
367
4
}
368
369
bool
370
0
isc_hmac_key_equal(isc_hmac_key_t *a, isc_hmac_key_t *b) {
371
0
  REQUIRE(a != NULL && a->magic == hmac_key_magic);
372
0
  REQUIRE(b != NULL && b->magic == hmac_key_magic);
373
374
0
  if (a->params != b->params) {
375
0
    return false;
376
0
  }
377
378
0
  if (a->len != b->len) {
379
0
    return false;
380
0
  }
381
382
0
  return isc_safe_memequal(a->secret, b->secret, a->len);
383
0
}
384
385
isc_hmac_t *
386
36
isc_hmac_new(void) {
387
36
  EVP_MAC_CTX *ctx = EVP_MAC_CTX_new(evp_hmac);
388
36
  RUNTIME_CHECK(ctx != NULL);
389
36
  return ctx;
390
36
}
391
392
void
393
36
isc_hmac_free(isc_hmac_t *hmac) {
394
36
  EVP_MAC_CTX_free(hmac);
395
36
}
396
397
isc_result_t
398
36
isc_hmac_init(isc_hmac_t *hmac, isc_hmac_key_t *key) {
399
36
  REQUIRE(key != NULL && key->magic == hmac_key_magic);
400
36
  REQUIRE(hmac != NULL);
401
402
36
  if (EVP_MAC_init(hmac, key->secret, key->len, key->params) != 1) {
403
0
    ERR_clear_error();
404
0
    return ISC_R_CRYPTOFAILURE;
405
0
  }
406
407
36
  return ISC_R_SUCCESS;
408
36
}
409
410
isc_result_t
411
251
isc_hmac_update(isc_hmac_t *hmac, const unsigned char *buf, const size_t len) {
412
251
  REQUIRE(hmac != NULL);
413
414
251
  if (buf == NULL || len == 0) {
415
0
    return ISC_R_SUCCESS;
416
0
  }
417
418
251
  if (EVP_MAC_update(hmac, buf, len) != 1) {
419
0
    ERR_clear_error();
420
0
    return ISC_R_CRYPTOFAILURE;
421
0
  }
422
423
251
  return ISC_R_SUCCESS;
424
251
}
425
426
isc_result_t
427
36
isc_hmac_final(isc_hmac_t *hmac, isc_buffer_t *out) {
428
36
  size_t len;
429
430
36
  REQUIRE(hmac != NULL);
431
432
36
  len = isc_buffer_availablelength(out);
433
36
  if (len < EVP_MAC_CTX_get_mac_size(hmac)) {
434
0
    return ISC_R_NOSPACE;
435
0
  }
436
437
36
  if (EVP_MAC_final(hmac, isc_buffer_used(out), &len, len) != 1) {
438
0
    ERR_clear_error();
439
0
    return ISC_R_CRYPTOFAILURE;
440
0
  }
441
442
36
  isc_buffer_add(out, len);
443
444
36
  return ISC_R_SUCCESS;
445
36
}
446
447
void
448
0
isc_crypto_aead_destroy(isc_crypto_aead_t **aeadp) {
449
0
  EVP_CIPHER_CTX *ctx;
450
451
0
  REQUIRE(aeadp != NULL && *aeadp != NULL);
452
453
0
  ctx = MOVE_OWNERSHIP(*aeadp);
454
455
0
  EVP_CIPHER_CTX_free(ctx);
456
0
}
457
458
isc_result_t
459
isc_crypto_aead_create(isc_crypto_aead_algorithm_t algorithm,
460
           isc_constregion_t key,
461
           isc_crypto_aead_direction_t direction,
462
0
           isc_crypto_aead_t **aeadp) {
463
0
  EVP_CIPHER_CTX *ctx;
464
0
  isc_result_t result;
465
0
  EVP_CIPHER *evp;
466
0
  size_t nonce;
467
0
  int dir;
468
469
0
  const OSSL_PARAM params[] = {
470
0
    OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, &nonce),
471
0
    OSSL_PARAM_END,
472
0
  };
473
474
0
  REQUIRE(key.base != NULL);
475
0
  REQUIRE(aeadp != NULL && *aeadp == NULL);
476
477
0
  switch (direction) {
478
0
  case ISC_CRYPTO_AEAD_DIRECTION_SEAL:
479
0
    dir = 1;
480
0
    break;
481
0
  case ISC_CRYPTO_AEAD_DIRECTION_OPEN:
482
0
    dir = 0;
483
0
    break;
484
0
  default:
485
0
    UNREACHABLE();
486
0
  }
487
488
0
  switch (algorithm) {
489
0
  case ISC_CRYPTO_AEAD_ALGORITHM_AES128GCM:
490
0
    nonce = isc_crypto_aes128gcm_nonce_length;
491
0
    evp = evp_aes_128_gcm;
492
0
    break;
493
0
  case ISC_CRYPTO_AEAD_ALGORITHM_AES256GCM:
494
0
    nonce = isc_crypto_aes256gcm_nonce_length;
495
0
    evp = evp_aes_256_gcm;
496
0
    break;
497
0
  case ISC_CRYPTO_AEAD_ALGORITHM_CHACHA20POLY1305:
498
0
    nonce = isc_crypto_chacha20poly1305_nonce_length;
499
0
    evp = evp_chacha20poly1305;
500
0
    break;
501
0
  default:
502
0
    UNREACHABLE();
503
0
  };
504
505
0
  if (evp == NULL) {
506
0
    return ISC_R_NOTIMPLEMENTED;
507
0
  }
508
509
0
  ctx = EVP_CIPHER_CTX_new();
510
0
  if (ctx == NULL) {
511
0
    CLEANUP(CRYPTO_ERROR("EVP_CIPHER_CTX_new"));
512
0
  }
513
514
0
  if (EVP_CipherInit_ex2(ctx, evp, key.base, NULL, dir, params) != 1) {
515
0
    CLEANUP(CRYPTO_ERROR("EVP_CipherInit_ex2"));
516
0
  }
517
518
0
  *aeadp = MOVE_OWNERSHIP(ctx);
519
520
0
  result = ISC_R_SUCCESS;
521
0
cleanup:
522
0
  EVP_CIPHER_CTX_free(ctx);
523
0
  return result;
524
0
}
525
526
isc_result_t
527
isc_crypto_aead_seal(isc_crypto_aead_t *aead, isc_constregion_t nonce,
528
         isc_constregion_t plaintext, isc_region_t out,
529
         size_t *out_sealed_len,
530
0
         isc_constregion_t additional_data) {
531
0
  isc_result_t result;
532
0
  size_t sealed;
533
0
  int len;
534
535
0
  REQUIRE(aead != NULL);
536
0
  REQUIRE(nonce.base != NULL);
537
0
  REQUIRE(out.base != NULL && plaintext.base != NULL);
538
0
  REQUIRE(out.length ==
539
0
    ISC_CHECKED_ADD(plaintext.length, isc_crypto_aead_tag_length));
540
0
  REQUIRE(out_sealed_len != NULL);
541
542
0
  OSSL_PARAM params[] = {
543
0
    OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
544
0
          out.base + plaintext.length,
545
0
          isc_crypto_aead_tag_length),
546
0
    OSSL_PARAM_END,
547
0
  };
548
549
0
  ERR_set_mark();
550
551
0
  if (EVP_EncryptInit_ex(aead, NULL, NULL, NULL, nonce.base) != 1) {
552
0
    CLEANUP(ISC_R_CRYPTOFAILURE);
553
0
  }
554
555
0
  if (additional_data.base != NULL) {
556
0
    INSIST(additional_data.length != 0);
557
0
    if (EVP_EncryptUpdate(aead, NULL, &len, additional_data.base,
558
0
              additional_data.length) != 1)
559
0
    {
560
0
      CLEANUP(ISC_R_CRYPTOFAILURE);
561
0
    }
562
0
  }
563
564
0
  len = out.length;
565
0
  if (EVP_EncryptUpdate(aead, out.base, &len, plaintext.base,
566
0
            plaintext.length) != 1)
567
0
  {
568
0
    CLEANUP(ISC_R_CRYPTOFAILURE);
569
0
  }
570
571
0
  sealed = len + isc_crypto_aead_tag_length;
572
573
0
  out.base += len;
574
0
  len = out.length - len;
575
0
  if (EVP_EncryptFinal_ex(aead, out.base, &len) != 1) {
576
0
    CLEANUP(ISC_R_CRYPTOFAILURE);
577
0
  }
578
579
0
  if (EVP_CIPHER_CTX_get_params(aead, params) != 1) {
580
0
    CLEANUP(ISC_R_CRYPTOFAILURE);
581
0
  }
582
583
0
  *out_sealed_len = sealed + len;
584
585
0
  result = ISC_R_SUCCESS;
586
0
cleanup:
587
0
  ERR_pop_to_mark();
588
0
  return result;
589
0
}
590
591
isc_result_t
592
isc_crypto_aead_open(isc_crypto_aead_t *aead, isc_constregion_t nonce,
593
         isc_constregion_t ciphertext, isc_region_t out,
594
         size_t *out_opened_len,
595
0
         isc_constregion_t additional_data) {
596
0
  isc_result_t result;
597
0
  const uint8_t *ct;
598
0
  size_t opened;
599
0
  int len = 0;
600
601
0
  uint8_t *k = NULL;
602
603
0
  REQUIRE(aead != NULL);
604
0
  REQUIRE(nonce.base != NULL);
605
0
  REQUIRE(out.base != NULL && ciphertext.base != NULL);
606
0
  REQUIRE(out.length ==
607
0
    ISC_CHECKED_SUB(ciphertext.length, isc_crypto_aead_tag_length));
608
0
  REQUIRE(out_opened_len != NULL);
609
610
0
  ct = ciphertext.base;
611
0
  const OSSL_PARAM params[] = {
612
0
    OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
613
0
          UNCONST(ct + out.length),
614
0
          isc_crypto_aead_tag_length),
615
0
    OSSL_PARAM_END,
616
0
  };
617
618
0
  ERR_set_mark();
619
620
0
  if (EVP_DecryptInit_ex2(aead, NULL, k, nonce.base, params) != 1) {
621
0
    CLEANUP(ISC_R_CRYPTOFAILURE);
622
0
  }
623
624
0
  if (additional_data.base != NULL) {
625
0
    INSIST(additional_data.length != 0);
626
0
    if (EVP_DecryptUpdate(aead, NULL, &len, additional_data.base,
627
0
              additional_data.length) != 1)
628
0
    {
629
0
      CLEANUP(ISC_R_CRYPTOFAILURE);
630
0
    }
631
0
  }
632
633
0
  len = out.length;
634
0
  if (EVP_DecryptUpdate(aead, out.base, &len, ciphertext.base, len) != 1)
635
0
  {
636
0
    CLEANUP(ISC_R_CRYPTOFAILURE);
637
0
  }
638
639
0
  opened = len;
640
641
0
  out.base += len;
642
0
  len = out.length - len;
643
0
  if (EVP_DecryptFinal_ex(aead, out.base, &len) != 1) {
644
0
    CLEANUP(ISC_R_CRYPTOFAILURE);
645
0
  }
646
647
0
  *out_opened_len = opened;
648
0
  result = ISC_R_SUCCESS;
649
0
cleanup:
650
0
  ERR_pop_to_mark();
651
0
  return result;
652
0
}
653
654
isc_result_t
655
isc_crypto_hkdf_extract(isc_region_t out, isc_md_type_t md,
656
0
      isc_constregion_t secret, isc_constregion_t salt) {
657
0
  isc_result_t result;
658
0
  EVP_KDF_CTX *ctx;
659
0
  int mode = EVP_KDF_HKDF_MODE_EXTRACT_ONLY;
660
661
0
  REQUIRE(md != ISC_MD_UNKNOWN && md < ISC_MD_MAX);
662
0
  REQUIRE(out.base != NULL && out.length != 0);
663
0
  REQUIRE(secret.base != NULL && secret.length != 0);
664
0
  REQUIRE(salt.base != NULL && salt.length != 0);
665
666
0
  if (isc__crypto_md[md] == NULL) {
667
0
    return ISC_R_NOTIMPLEMENTED;
668
0
  }
669
670
0
  const OSSL_PARAM params[] = {
671
0
    OSSL_PARAM_int(OSSL_KDF_PARAM_MODE, &mode),
672
0
    OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST,
673
0
               UNCONST(md_to_name[md].base),
674
0
               md_to_name[md].length),
675
0
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY,
676
0
          UNCONST(secret.base), secret.length),
677
0
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, UNCONST(salt.base),
678
0
          salt.length),
679
0
    OSSL_PARAM_END,
680
0
  };
681
682
0
  ctx = EVP_KDF_CTX_new(evp_hkdf);
683
0
  if (ctx == NULL) {
684
0
    CLEANUP(CRYPTO_ERROR("EVP_KDF_CTX_new"));
685
0
  }
686
687
0
  if (EVP_KDF_derive(ctx, out.base, out.length, params) != 1) {
688
0
    CLEANUP(CRYPTO_ERROR("EVP_KDF_derive"));
689
0
  }
690
691
0
  result = ISC_R_SUCCESS;
692
0
cleanup:
693
0
  EVP_KDF_CTX_free(ctx);
694
0
  return result;
695
0
}
696
697
isc_result_t
698
isc_crypto_hkdf_expand(isc_region_t out, isc_md_type_t md,
699
0
           isc_constregion_t prk, isc_constregion_t info) {
700
0
  isc_result_t result;
701
0
  EVP_KDF_CTX *ctx;
702
0
  int mode = EVP_KDF_HKDF_MODE_EXPAND_ONLY;
703
704
0
  REQUIRE(md != ISC_MD_UNKNOWN && md < ISC_MD_MAX);
705
0
  REQUIRE(out.base != NULL && out.length != 0);
706
0
  REQUIRE(prk.base != NULL && prk.length != 0);
707
0
  REQUIRE(info.base != NULL && info.length != 0);
708
709
0
  if (isc__crypto_md[md] == NULL) {
710
0
    return ISC_R_NOTIMPLEMENTED;
711
0
  }
712
713
0
  const OSSL_PARAM params[] = {
714
0
    OSSL_PARAM_int(OSSL_KDF_PARAM_MODE, &mode),
715
0
    OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST,
716
0
               UNCONST(md_to_name[md].base),
717
0
               md_to_name[md].length),
718
0
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, UNCONST(prk.base),
719
0
          prk.length),
720
0
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, UNCONST(info.base),
721
0
          info.length),
722
0
    OSSL_PARAM_END,
723
0
  };
724
725
0
  ctx = EVP_KDF_CTX_new(evp_hkdf);
726
0
  if (ctx == NULL) {
727
0
    CLEANUP(CRYPTO_ERROR("EVP_KDF_CTX_new"));
728
0
  }
729
730
0
  if (EVP_KDF_derive(ctx, out.base, out.length, params) != 1) {
731
0
    CLEANUP(CRYPTO_ERROR("EVP_KDF_derive"));
732
0
  }
733
734
0
  result = ISC_R_SUCCESS;
735
0
cleanup:
736
0
  EVP_KDF_CTX_free(ctx);
737
0
  return result;
738
0
}
739
740
isc_result_t
741
isc_crypto_hkdf_expand_label(isc_region_t out, isc_md_type_t md,
742
           isc_constregion_t secret,
743
0
           isc_constregion_t label) {
744
0
  isc_result_t result;
745
0
  EVP_KDF_CTX *ctx;
746
0
  int mode = EVP_PKEY_HKDEF_MODE_EXPAND_ONLY;
747
748
0
  REQUIRE(out.base != NULL && out.length != 0);
749
0
  REQUIRE(md != ISC_MD_UNKNOWN && md < ISC_MD_MAX);
750
0
  REQUIRE(secret.base != NULL && secret.length != 0);
751
0
  REQUIRE(label.base != NULL && label.length != 0);
752
753
0
  if (isc__crypto_md[md] == NULL) {
754
0
    return ISC_R_NOTIMPLEMENTED;
755
0
  }
756
757
0
  const OSSL_PARAM params[] = {
758
0
    OSSL_PARAM_int(OSSL_KDF_PARAM_MODE, &mode),
759
0
    OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST,
760
0
               UNCONST(md_to_name[md].base),
761
0
               md_to_name[md].length),
762
0
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PREFIX,
763
0
          UNCONST("tls13 "),
764
0
          sizeof("tls13 ") - 1),
765
0
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY,
766
0
          UNCONST(secret.base), secret.length),
767
0
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_LABEL,
768
0
          UNCONST(label.base), label.length),
769
0
    OSSL_PARAM_END,
770
0
  };
771
772
  /* Please see the comment in `evp_tls_1_3_kdf` */
773
0
  ctx = EVP_KDF_CTX_new(evp_tls_1_3_kdf);
774
0
  if (ctx == NULL) {
775
0
    CLEANUP(CRYPTO_ERROR("EVP_KDF_CTX_new"));
776
0
  }
777
778
0
  if (EVP_KDF_derive(ctx, out.base, out.length, params) != 1) {
779
0
    CLEANUP(CRYPTO_ERROR("EVP_KDF_derive"));
780
0
  }
781
782
0
  result = ISC_R_SUCCESS;
783
0
cleanup:
784
0
  EVP_KDF_CTX_free(ctx);
785
0
  return result;
786
0
}
787
788
isc_result_t
789
isc_crypto_hkdf(isc_region_t out, isc_md_type_t md, isc_constregion_t ikm,
790
0
    isc_constregion_t salt, isc_constregion_t info) {
791
0
  isc_result_t result;
792
0
  EVP_KDF_CTX *ctx;
793
0
  int mode = EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND;
794
795
0
  REQUIRE(md != ISC_MD_UNKNOWN && md < ISC_MD_MAX);
796
0
  REQUIRE(out.base != NULL && out.length != 0);
797
0
  REQUIRE(ikm.base != NULL && ikm.length != 0);
798
0
  REQUIRE(salt.base != NULL && salt.length != 0);
799
0
  REQUIRE(info.base != NULL && info.length != 0);
800
801
0
  if (isc__crypto_md[md] == NULL) {
802
0
    return ISC_R_NOTIMPLEMENTED;
803
0
  }
804
805
0
  const OSSL_PARAM params[] = {
806
0
    OSSL_PARAM_int(OSSL_KDF_PARAM_MODE, &mode),
807
0
    OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST,
808
0
               UNCONST(md_to_name[md].base),
809
0
               md_to_name[md].length),
810
0
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, UNCONST(ikm.base),
811
0
          ikm.length),
812
0
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, UNCONST(info.base),
813
0
          info.length),
814
0
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, UNCONST(salt.base),
815
0
          salt.length),
816
0
    OSSL_PARAM_END,
817
0
  };
818
819
0
  ctx = EVP_KDF_CTX_new(evp_hkdf);
820
0
  if (ctx == NULL) {
821
0
    CLEANUP(CRYPTO_ERROR("EVP_KDF_CTX_new"));
822
0
  }
823
824
0
  if (EVP_KDF_derive(ctx, out.base, out.length, params) != 1) {
825
0
    CLEANUP(CRYPTO_ERROR("EVP_KDF_derive"));
826
0
  }
827
828
0
  result = ISC_R_SUCCESS;
829
0
cleanup:
830
0
  EVP_KDF_CTX_free(ctx);
831
0
  return result;
832
0
}
833
834
void
835
0
isc_crypto_quic_hp_protect_destroy(isc_crypto_quic_hp_protect_t **protp) {
836
0
  isc_crypto_quic_hp_protect_t *prot;
837
838
0
  REQUIRE(protp != NULL && *protp != NULL &&
839
0
    (*protp)->magic == quic_hp_protect_magic);
840
841
0
  prot = MOVE_OWNERSHIP(*protp);
842
0
  prot->magic = 0x00;
843
0
  EVP_CIPHER_CTX_free(prot->ctx);
844
0
  isc_mem_putanddetach(&prot->mctx, prot, sizeof(*prot));
845
0
}
846
847
isc_result_t
848
isc_crypto_quic_hp_protect_create(
849
  isc_mem_t *mctx, isc_constregion_t key,
850
  isc_crypto_quic_hp_protect_algorithm_t algorithm,
851
0
  isc_crypto_quic_hp_protect_t **protp) {
852
0
  isc_crypto_quic_hp_protect_t *prot;
853
0
  const EVP_CIPHER *evp;
854
0
  EVP_CIPHER_CTX *ctx;
855
0
  size_t len;
856
857
0
  REQUIRE(protp != NULL && *protp == NULL);
858
0
  REQUIRE(key.base != NULL);
859
860
0
  switch (algorithm) {
861
0
  case ISC_CRYPTO_QUIC_HP_PROTECT_ALGORITHM_AES128:
862
0
    len = isc_crypto_aes128gcm_key_length;
863
0
    evp = evp_aes_128_ctr;
864
0
    break;
865
0
  case ISC_CRYPTO_QUIC_HP_PROTECT_ALGORITHM_AES256:
866
0
    len = isc_crypto_aes256gcm_key_length;
867
0
    evp = evp_aes_256_ctr;
868
0
    break;
869
0
  case ISC_CRYPTO_QUIC_HP_PROTECT_ALGORITHM_CHACHA20:
870
0
    len = isc_crypto_chacha20poly1305_key_length;
871
0
    evp = evp_chacha20;
872
0
    break;
873
0
  default:
874
0
    UNREACHABLE();
875
0
  }
876
877
0
  INSIST(key.length == len);
878
879
0
  if (evp == NULL) {
880
0
    return ISC_R_NOTIMPLEMENTED;
881
0
  }
882
883
0
  ctx = EVP_CIPHER_CTX_new();
884
0
  if (ctx == NULL) {
885
0
    return CRYPTO_ERROR("EVP_CIPHER_CTX_new");
886
0
  }
887
888
0
  if (EVP_CipherInit_ex2(ctx, evp, key.base, NULL, 1, NULL) != 1) {
889
0
    EVP_CIPHER_CTX_free(ctx);
890
0
    return CRYPTO_ERROR("EVP_CipherInit_ex2");
891
0
  }
892
893
0
  prot = isc_mem_get(mctx, sizeof(*prot));
894
0
  *prot = (isc_crypto_quic_hp_protect_t){
895
0
    .magic = quic_hp_protect_magic,
896
0
    .ctx = ctx,
897
0
  };
898
0
  isc_mem_attach(mctx, &prot->mctx);
899
900
0
  *protp = prot;
901
902
0
  return ISC_R_SUCCESS;
903
0
}
904
905
isc_result_t
906
isc_crypto_quic_hp_protect_mask(isc_crypto_quic_hp_protect_t *prot,
907
0
        uint8_t *out, const uint8_t *sample) {
908
0
  static const uint8_t zeros[5] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
909
0
  isc_result_t result;
910
0
  int len;
911
912
0
  REQUIRE(prot != NULL && prot->magic == quic_hp_protect_magic);
913
0
  REQUIRE(out != NULL && sample != NULL);
914
915
0
  if (EVP_EncryptInit_ex2(prot->ctx, NULL, NULL, sample, NULL) != 1) {
916
0
    CLEANUP(CRYPTO_ERROR("EVP_EncryptInit_ex2"));
917
0
  }
918
919
0
  if (EVP_EncryptUpdate(prot->ctx, out, &len, zeros, sizeof(zeros)) != 1)
920
0
  {
921
0
    CLEANUP(CRYPTO_ERROR("EVP_EncryptUpdate"));
922
0
  }
923
924
0
  if (EVP_EncryptFinal_ex(prot->ctx, out + sizeof(zeros), &len) != 1) {
925
0
    CLEANUP(CRYPTO_ERROR("EVP_EncryptFinal_ex"));
926
0
  }
927
928
0
  result = ISC_R_SUCCESS;
929
930
0
cleanup:
931
0
  return result;
932
0
}
933
934
bool
935
22
isc_crypto_fips_mode(void) {
936
22
  return EVP_default_properties_is_fips_enabled(NULL) != 0;
937
22
}
938
939
isc_result_t
940
0
isc_crypto_fips_enable(void) {
941
0
  if (isc_crypto_fips_mode()) {
942
0
    return ISC_R_SUCCESS;
943
0
  }
944
945
0
  INSIST(fips == NULL);
946
0
  fips = OSSL_PROVIDER_load(NULL, "fips");
947
0
  if (fips == NULL) {
948
0
    return isc_ossl_wrap_logged_toresult(
949
0
      ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_CRYPTO,
950
0
      "OSSL_PROVIDER_load", ISC_R_CRYPTOFAILURE);
951
0
  }
952
953
0
  INSIST(base == NULL);
954
0
  base = OSSL_PROVIDER_load(NULL, "base");
955
0
  if (base == NULL) {
956
0
    OSSL_PROVIDER_unload(fips);
957
0
    return isc_ossl_wrap_logged_toresult(
958
0
      ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_CRYPTO,
959
0
      "OSS_PROVIDER_load", ISC_R_CRYPTOFAILURE);
960
0
  }
961
962
0
  if (EVP_default_properties_enable_fips(NULL, 1) == 0) {
963
0
    return isc_ossl_wrap_logged_toresult(
964
0
      ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_CRYPTO,
965
0
      "EVP_default_properties_enable_fips",
966
0
      ISC_R_CRYPTOFAILURE);
967
0
  }
968
969
0
  unregister_algorithms();
970
0
  register_algorithms();
971
972
0
  return ISC_R_SUCCESS;
973
0
}
974
975
/*
976
 * OPENSSL_cleanup() in OpenSSL 4 doesn't free the memory, which is not
977
 * compatible with BIND 9's memory leak detection code, that is why the memory
978
 * tracking has been disabled in this module, and this function is a no-op.
979
 * This can be cleaned up once OpenSSL 1.1.x support is removed.
980
 *
981
 * See https://github.com/openssl/openssl/pull/29721
982
 */
983
void
984
0
isc__crypto_setdestroycheck(bool check) {
985
0
  UNUSED(check);
986
0
}
987
988
void
989
22
isc__crypto_initialize(void) {
990
  /*
991
   * We call OPENSSL_cleanup() manually, in a correct order, thus disable
992
   * the automatic atexit() handler.
993
   */
994
22
  uint64_t opts = OPENSSL_INIT_LOAD_CONFIG | OPENSSL_INIT_NO_ATEXIT;
995
996
22
  RUNTIME_CHECK(OPENSSL_init_ssl(opts, NULL) == 1);
997
998
22
  register_algorithms();
999
1000
#if defined(ENABLE_FIPS_MODE)
1001
  if (isc_crypto_fips_enable() != ISC_R_SUCCESS) {
1002
    ERR_clear_error();
1003
    FATAL_ERROR("Failed to toggle FIPS mode but is "
1004
          "required for this build");
1005
  }
1006
#endif
1007
1008
  /* Protect ourselves against unseeded PRNG */
1009
22
  if (RAND_status() != 1) {
1010
0
    isc_ossl_wrap_logged_toresult(
1011
0
      ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_CRYPTO,
1012
0
      "RAND_status", ISC_R_CRYPTOFAILURE);
1013
0
    FATAL_ERROR("OpenSSL pseudorandom number generator "
1014
0
          "cannot be initialized (see the `PRNG not "
1015
0
          "seeded' message in the OpenSSL FAQ)");
1016
0
  }
1017
22
}
1018
1019
void
1020
0
isc__crypto_shutdown(void) {
1021
0
  unregister_algorithms();
1022
1023
0
  if (base != NULL) {
1024
0
    OSSL_PROVIDER_unload(base);
1025
0
  }
1026
1027
0
  if (fips != NULL) {
1028
0
    OSSL_PROVIDER_unload(fips);
1029
0
  }
1030
1031
0
  OPENSSL_cleanup();
1032
0
}