Coverage Report

Created: 2026-10-17 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/bind9/lib/isc/ossl_wrap/ossl_common.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 <openssl/bn.h>
15
#include <openssl/err.h>
16
17
#include <isc/ossl_wrap.h>
18
#include <isc/util.h>
19
20
#include "../openssl_shim.h"
21
22
void
23
214
isc_ossl_wrap_rsa_components_cleanup(isc_ossl_wrap_rsa_components_t *c) {
24
214
  REQUIRE(c != NULL);
25
26
214
  if (!c->needs_cleanup) {
27
0
    return;
28
0
  }
29
30
  /*
31
   * NOTE: BN_free() frees the components of the BIGNUM, and if it was
32
   * created by BN_new(), also the structure itself. BN_clear_free()
33
   * additionally overwrites the data before the memory is returned to the
34
   * system. If a is NULL, nothing is done.
35
   */
36
214
  BN_free(c->e);
37
214
  BN_free(c->n);
38
214
  BN_clear_free(c->d);
39
214
  BN_clear_free(c->p);
40
214
  BN_clear_free(c->q);
41
214
  BN_clear_free(c->dmp1);
42
214
  BN_clear_free(c->dmq1);
43
214
  BN_clear_free(c->iqmp);
44
45
214
  c->needs_cleanup = false;
46
214
}
47
48
isc_result_t
49
11
isc_ossl_wrap_toresult(isc_result_t fallback) {
50
11
  isc_result_t result = fallback;
51
11
  unsigned long err = ERR_peek_error();
52
#ifdef ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED
53
  int lib = ERR_GET_LIB(err);
54
#endif /* ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED */
55
11
  int reason = ERR_GET_REASON(err);
56
57
11
  switch (reason) {
58
  /*
59
   * ERR_* errors are globally unique; others
60
   * are unique per sublibrary
61
   */
62
0
  case ERR_R_MALLOC_FAILURE:
63
0
    result = ISC_R_NOMEMORY;
64
0
    break;
65
11
  default:
66
#ifdef ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED
67
    if (lib == ERR_R_ECDSA_LIB &&
68
        reason == ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED)
69
    {
70
      result = ISC_R_NOENTROPY;
71
      break;
72
    }
73
#endif /* ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED */
74
11
    break;
75
11
  }
76
77
11
  return result;
78
11
}
79
80
isc_result_t
81
isc__ossl_wrap_logged_toresult(isc_logcategory_t category,
82
             isc_logmodule_t module, const char *funcname,
83
             isc_result_t fallback, const char *file,
84
11
             int line) {
85
11
  isc_result_t result = isc_ossl_wrap_toresult(fallback);
86
87
  /*
88
   * This is an exception - normally, we don't allow this, but the
89
   * compatibility shims in dst_openssl.h needs a call that just
90
   * translates the error code and don't do any logging.
91
   */
92
11
  if (category == ISC_LOGCATEGORY_INVALID) {
93
11
    goto done;
94
11
  }
95
96
0
  isc_log_write(category, module, ISC_LOG_WARNING,
97
0
          "%s (%s:%d) failed (%s)", funcname, file, line,
98
0
          isc_result_totext(result));
99
100
0
  if (result == ISC_R_NOMEMORY) {
101
0
    goto done;
102
0
  }
103
104
0
  for (;;) {
105
0
    const char *func, *data;
106
0
    int flags;
107
0
    unsigned long err = ERR_get_error_all(&file, &line, &func,
108
0
                  &data, &flags);
109
0
    if (err == 0U) {
110
0
      break;
111
0
    }
112
113
0
    char buf[256];
114
0
    ERR_error_string_n(err, buf, sizeof(buf));
115
116
0
    isc_log_write(category, module, ISC_LOG_INFO, "%s:%s:%d:%s",
117
0
            buf, file, line,
118
0
            ((flags & ERR_TXT_STRING) != 0) ? data : "");
119
0
  }
120
121
11
done:
122
11
  ERR_clear_error();
123
11
  return result;
124
0
}