xref: /netbsd-src/external/mpl/bind/dist/lib/dns/openssl_shim.c (revision bcda20f65a8566e103791ec395f7f499ef322704)
1 /*	$NetBSD: openssl_shim.c,v 1.3 2025/01/26 16:25:23 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * SPDX-License-Identifier: MPL-2.0
7  *
8  * This Source Code Form is subject to the terms of the Mozilla Public
9  * License, v. 2.0. If a copy of the MPL was not distributed with this
10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11  *
12  * See the COPYRIGHT file distributed with this work for additional
13  * information regarding copyright ownership.
14  */
15 
16 #include "openssl_shim.h"
17 
18 #include <isc/util.h>
19 
20 #if !HAVE_RSA_SET0_KEY && OPENSSL_VERSION_NUMBER < 0x30000000L
21 /* From OpenSSL 1.1.0 */
22 int
23 RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) {
24 	/*
25 	 * If the fields n and e in r are NULL, the corresponding input
26 	 * parameters MUST be non-NULL for n and e.  d may be
27 	 * left NULL (in case only the public key is used).
28 	 */
29 	if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL)) {
30 		return 0;
31 	}
32 
33 	if (n != NULL) {
34 		BN_free(r->n);
35 		r->n = n;
36 	}
37 	if (e != NULL) {
38 		BN_free(r->e);
39 		r->e = e;
40 	}
41 	if (d != NULL) {
42 		BN_clear_free(r->d);
43 		r->d = d;
44 	}
45 
46 	return 1;
47 }
48 
49 int
50 RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q) {
51 	/*
52 	 * If the fields p and q in r are NULL, the corresponding input
53 	 * parameters MUST be non-NULL.
54 	 */
55 	if ((r->p == NULL && p == NULL) || (r->q == NULL && q == NULL)) {
56 		return 0;
57 	}
58 
59 	if (p != NULL) {
60 		BN_clear_free(r->p);
61 		r->p = p;
62 	}
63 	if (q != NULL) {
64 		BN_clear_free(r->q);
65 		r->q = q;
66 	}
67 
68 	return 1;
69 }
70 
71 int
72 RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp) {
73 	/*
74 	 * If the fields dmp1, dmq1 and iqmp in r are NULL, the
75 	 * corresponding input parameters MUST be non-NULL.
76 	 */
77 	if ((r->dmp1 == NULL && dmp1 == NULL) ||
78 	    (r->dmq1 == NULL && dmq1 == NULL) ||
79 	    (r->iqmp == NULL && iqmp == NULL))
80 	{
81 		return 0;
82 	}
83 
84 	if (dmp1 != NULL) {
85 		BN_clear_free(r->dmp1);
86 		r->dmp1 = dmp1;
87 	}
88 	if (dmq1 != NULL) {
89 		BN_clear_free(r->dmq1);
90 		r->dmq1 = dmq1;
91 	}
92 	if (iqmp != NULL) {
93 		BN_clear_free(r->iqmp);
94 		r->iqmp = iqmp;
95 	}
96 
97 	return 1;
98 }
99 
100 void
101 RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e,
102 	     const BIGNUM **d) {
103 	SET_IF_NOT_NULL(n, r->n);
104 	SET_IF_NOT_NULL(e, r->e);
105 	SET_IF_NOT_NULL(d, r->d);
106 }
107 
108 void
109 RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q) {
110 	SET_IF_NOT_NULL(p, r->p);
111 	SET_IF_NOT_NULL(q, r->q);
112 }
113 
114 void
115 RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1,
116 		    const BIGNUM **iqmp) {
117 	SET_IF_NOT_NULL(dmp1, r->dmp1);
118 	SET_IF_NOT_NULL(dmq1, r->dmq1);
119 	SET_IF_NOT_NULL(iqmp, r->iqmp);
120 }
121 
122 int
123 RSA_test_flags(const RSA *r, int flags) {
124 	return r->flags & flags;
125 }
126 #endif /* !HAVE_RSA_SET0_KEY && OPENSSL_VERSION_NUMBER < 0x30000000L */
127 
128 #if !HAVE_ECDSA_SIG_GET0
129 /* From OpenSSL 1.1 */
130 void
131 ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps) {
132 	SET_IF_NOT_NULL(pr, sig->r);
133 	SET_IF_NOT_NULL(ps, sig->s);
134 }
135 
136 int
137 ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) {
138 	if (r == NULL || s == NULL) {
139 		return 0;
140 	}
141 
142 	BN_clear_free(sig->r);
143 	BN_clear_free(sig->s);
144 	sig->r = r;
145 	sig->s = s;
146 
147 	return 1;
148 }
149 #endif /* !HAVE_ECDSA_SIG_GET0 */
150 
151 #if !HAVE_ERR_GET_ERROR_ALL
152 static const char err_empty_string = '\0';
153 
154 unsigned long
155 ERR_get_error_all(const char **file, int *line, const char **func,
156 		  const char **data, int *flags) {
157 	SET_IF_NOT_NULL(func, &err_empty_string);
158 	return ERR_get_error_line_data(file, line, data, flags);
159 }
160 #endif /* if !HAVE_ERR_GET_ERROR_ALL */
161