xref: /openbsd-src/lib/libcrypto/rsa/rsa_lib.c (revision 68dd5bb1859285b71cb62a10bf107b8ad54064d9)
1 /* $OpenBSD: rsa_lib.c,v 1.49 2023/11/19 15:46:10 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include <stdio.h>
60 
61 #include <openssl/opensslconf.h>
62 
63 #include <openssl/bn.h>
64 #include <openssl/crypto.h>
65 #include <openssl/err.h>
66 #include <openssl/evp.h>
67 #include <openssl/lhash.h>
68 #include <openssl/rsa.h>
69 
70 #include "bn_local.h"
71 #include "evp_local.h"
72 #include "rsa_local.h"
73 
74 static const RSA_METHOD *default_RSA_meth = NULL;
75 
76 RSA *
77 RSA_new(void)
78 {
79 	RSA *r = RSA_new_method(NULL);
80 
81 	return r;
82 }
83 LCRYPTO_ALIAS(RSA_new);
84 
85 void
86 RSA_set_default_method(const RSA_METHOD *meth)
87 {
88 	default_RSA_meth = meth;
89 }
90 LCRYPTO_ALIAS(RSA_set_default_method);
91 
92 const RSA_METHOD *
93 RSA_get_default_method(void)
94 {
95 	if (default_RSA_meth == NULL)
96 		default_RSA_meth = RSA_PKCS1_SSLeay();
97 
98 	return default_RSA_meth;
99 }
100 LCRYPTO_ALIAS(RSA_get_default_method);
101 
102 const RSA_METHOD *
103 RSA_get_method(const RSA *rsa)
104 {
105 	return rsa->meth;
106 }
107 LCRYPTO_ALIAS(RSA_get_method);
108 
109 int
110 RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
111 {
112 	/*
113 	 * NB: The caller is specifically setting a method, so it's not up to us
114 	 * to deal with which ENGINE it comes from.
115 	 */
116 	const RSA_METHOD *mtmp;
117 
118 	mtmp = rsa->meth;
119 	if (mtmp->finish)
120 		mtmp->finish(rsa);
121 	rsa->meth = meth;
122 	if (meth->init)
123 		meth->init(rsa);
124 	return 1;
125 }
126 LCRYPTO_ALIAS(RSA_set_method);
127 
128 RSA *
129 RSA_new_method(ENGINE *engine)
130 {
131 	RSA *ret;
132 
133 	if ((ret = calloc(1, sizeof(RSA))) == NULL) {
134 		RSAerror(ERR_R_MALLOC_FAILURE);
135 		return NULL;
136 	}
137 
138 	ret->meth = RSA_get_default_method();
139 
140 	ret->references = 1;
141 	ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
142 
143 	if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data))
144 		goto err;
145 
146 	if (ret->meth->init != NULL && !ret->meth->init(ret)) {
147 		CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
148 		goto err;
149 	}
150 
151 	return ret;
152 
153  err:
154 	free(ret);
155 
156 	return NULL;
157 }
158 LCRYPTO_ALIAS(RSA_new_method);
159 
160 void
161 RSA_free(RSA *r)
162 {
163 	int i;
164 
165 	if (r == NULL)
166 		return;
167 
168 	i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_RSA);
169 	if (i > 0)
170 		return;
171 
172 	if (r->meth->finish)
173 		r->meth->finish(r);
174 
175 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
176 
177 	BN_free(r->n);
178 	BN_free(r->e);
179 	BN_free(r->d);
180 	BN_free(r->p);
181 	BN_free(r->q);
182 	BN_free(r->dmp1);
183 	BN_free(r->dmq1);
184 	BN_free(r->iqmp);
185 	BN_BLINDING_free(r->blinding);
186 	BN_BLINDING_free(r->mt_blinding);
187 	RSA_PSS_PARAMS_free(r->pss);
188 	free(r);
189 }
190 LCRYPTO_ALIAS(RSA_free);
191 
192 int
193 RSA_up_ref(RSA *r)
194 {
195 	int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA);
196 	return i > 1 ? 1 : 0;
197 }
198 LCRYPTO_ALIAS(RSA_up_ref);
199 
200 int
201 RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
202     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
203 {
204 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp,
205 	    new_func, dup_func, free_func);
206 }
207 LCRYPTO_ALIAS(RSA_get_ex_new_index);
208 
209 int
210 RSA_set_ex_data(RSA *r, int idx, void *arg)
211 {
212 	return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
213 }
214 LCRYPTO_ALIAS(RSA_set_ex_data);
215 
216 void *
217 RSA_get_ex_data(const RSA *r, int idx)
218 {
219 	return CRYPTO_get_ex_data(&r->ex_data, idx);
220 }
221 LCRYPTO_ALIAS(RSA_get_ex_data);
222 
223 int
224 RSA_security_bits(const RSA *rsa)
225 {
226 	return BN_security_bits(RSA_bits(rsa), -1);
227 }
228 LCRYPTO_ALIAS(RSA_security_bits);
229 
230 void
231 RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
232 {
233 	if (n != NULL)
234 		*n = r->n;
235 	if (e != NULL)
236 		*e = r->e;
237 	if (d != NULL)
238 		*d = r->d;
239 }
240 LCRYPTO_ALIAS(RSA_get0_key);
241 
242 int
243 RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
244 {
245 	if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL))
246 		return 0;
247 
248 	if (n != NULL) {
249 		BN_free(r->n);
250 		r->n = n;
251 	}
252 	if (e != NULL) {
253 		BN_free(r->e);
254 		r->e = e;
255 	}
256 	if (d != NULL) {
257 		BN_free(r->d);
258 		r->d = d;
259 	}
260 
261 	return 1;
262 }
263 LCRYPTO_ALIAS(RSA_set0_key);
264 
265 void
266 RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1,
267     const BIGNUM **iqmp)
268 {
269 	if (dmp1 != NULL)
270 		*dmp1 = r->dmp1;
271 	if (dmq1 != NULL)
272 		*dmq1 = r->dmq1;
273 	if (iqmp != NULL)
274 		*iqmp = r->iqmp;
275 }
276 LCRYPTO_ALIAS(RSA_get0_crt_params);
277 
278 int
279 RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
280 {
281 	if ((r->dmp1 == NULL && dmp1 == NULL) ||
282 	    (r->dmq1 == NULL && dmq1 == NULL) ||
283 	    (r->iqmp == NULL && iqmp == NULL))
284 		return 0;
285 
286 	if (dmp1 != NULL) {
287 		BN_free(r->dmp1);
288 		r->dmp1 = dmp1;
289 	}
290 	if (dmq1 != NULL) {
291 		BN_free(r->dmq1);
292 		r->dmq1 = dmq1;
293 	}
294 	if (iqmp != NULL) {
295 		BN_free(r->iqmp);
296 		r->iqmp = iqmp;
297 	}
298 
299 	return 1;
300 }
301 LCRYPTO_ALIAS(RSA_set0_crt_params);
302 
303 void
304 RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
305 {
306 	if (p != NULL)
307 		*p = r->p;
308 	if (q != NULL)
309 		*q = r->q;
310 }
311 LCRYPTO_ALIAS(RSA_get0_factors);
312 
313 int
314 RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
315 {
316 	if ((r->p == NULL && p == NULL) || (r->q == NULL && q == NULL))
317 		return 0;
318 
319 	if (p != NULL) {
320 		BN_free(r->p);
321 		r->p = p;
322 	}
323 	if (q != NULL) {
324 		BN_free(r->q);
325 		r->q = q;
326 	}
327 
328 	return 1;
329 }
330 LCRYPTO_ALIAS(RSA_set0_factors);
331 
332 const BIGNUM *
333 RSA_get0_n(const RSA *r)
334 {
335 	return r->n;
336 }
337 LCRYPTO_ALIAS(RSA_get0_n);
338 
339 const BIGNUM *
340 RSA_get0_e(const RSA *r)
341 {
342 	return r->e;
343 }
344 LCRYPTO_ALIAS(RSA_get0_e);
345 
346 const BIGNUM *
347 RSA_get0_d(const RSA *r)
348 {
349 	return r->d;
350 }
351 LCRYPTO_ALIAS(RSA_get0_d);
352 
353 const BIGNUM *
354 RSA_get0_p(const RSA *r)
355 {
356 	return r->p;
357 }
358 LCRYPTO_ALIAS(RSA_get0_p);
359 
360 const BIGNUM *
361 RSA_get0_q(const RSA *r)
362 {
363 	return r->q;
364 }
365 LCRYPTO_ALIAS(RSA_get0_q);
366 
367 const BIGNUM *
368 RSA_get0_dmp1(const RSA *r)
369 {
370 	return r->dmp1;
371 }
372 LCRYPTO_ALIAS(RSA_get0_dmp1);
373 
374 const BIGNUM *
375 RSA_get0_dmq1(const RSA *r)
376 {
377 	return r->dmq1;
378 }
379 LCRYPTO_ALIAS(RSA_get0_dmq1);
380 
381 const BIGNUM *
382 RSA_get0_iqmp(const RSA *r)
383 {
384 	return r->iqmp;
385 }
386 LCRYPTO_ALIAS(RSA_get0_iqmp);
387 
388 const RSA_PSS_PARAMS *
389 RSA_get0_pss_params(const RSA *r)
390 {
391 	return r->pss;
392 }
393 LCRYPTO_ALIAS(RSA_get0_pss_params);
394 
395 void
396 RSA_clear_flags(RSA *r, int flags)
397 {
398 	r->flags &= ~flags;
399 }
400 LCRYPTO_ALIAS(RSA_clear_flags);
401 
402 int
403 RSA_test_flags(const RSA *r, int flags)
404 {
405 	return r->flags & flags;
406 }
407 LCRYPTO_ALIAS(RSA_test_flags);
408 
409 void
410 RSA_set_flags(RSA *r, int flags)
411 {
412 	r->flags |= flags;
413 }
414 LCRYPTO_ALIAS(RSA_set_flags);
415 
416 int
417 RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
418 {
419 	/* Return an error if the key type is not RSA or RSA-PSS. */
420 	if (ctx != NULL && ctx->pmeth != NULL &&
421 	    ctx->pmeth->pkey_id != EVP_PKEY_RSA &&
422 	    ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
423 		return -1;
424 
425 	return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
426 }
427 LCRYPTO_ALIAS(RSA_pkey_ctx_ctrl);
428