xref: /openbsd-src/lib/libcrypto/rsa/rsa_lib.c (revision aa88ce08dc8b367b254af7566f45c4db85f08d74)
1 /* $OpenBSD: rsa_lib.c,v 1.48 2023/07/28 10:05:16 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 #ifndef OPENSSL_NO_ENGINE
75 #include <openssl/engine.h>
76 #endif
77 
78 static const RSA_METHOD *default_RSA_meth = NULL;
79 
80 RSA *
81 RSA_new(void)
82 {
83 	RSA *r = RSA_new_method(NULL);
84 
85 	return r;
86 }
87 LCRYPTO_ALIAS(RSA_new);
88 
89 void
90 RSA_set_default_method(const RSA_METHOD *meth)
91 {
92 	default_RSA_meth = meth;
93 }
94 LCRYPTO_ALIAS(RSA_set_default_method);
95 
96 const RSA_METHOD *
97 RSA_get_default_method(void)
98 {
99 	if (default_RSA_meth == NULL)
100 		default_RSA_meth = RSA_PKCS1_SSLeay();
101 
102 	return default_RSA_meth;
103 }
104 LCRYPTO_ALIAS(RSA_get_default_method);
105 
106 const RSA_METHOD *
107 RSA_get_method(const RSA *rsa)
108 {
109 	return rsa->meth;
110 }
111 LCRYPTO_ALIAS(RSA_get_method);
112 
113 int
114 RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
115 {
116 	/*
117 	 * NB: The caller is specifically setting a method, so it's not up to us
118 	 * to deal with which ENGINE it comes from.
119 	 */
120 	const RSA_METHOD *mtmp;
121 
122 	mtmp = rsa->meth;
123 	if (mtmp->finish)
124 		mtmp->finish(rsa);
125 #ifndef OPENSSL_NO_ENGINE
126 	ENGINE_finish(rsa->engine);
127 	rsa->engine = NULL;
128 #endif
129 	rsa->meth = meth;
130 	if (meth->init)
131 		meth->init(rsa);
132 	return 1;
133 }
134 LCRYPTO_ALIAS(RSA_set_method);
135 
136 RSA *
137 RSA_new_method(ENGINE *engine)
138 {
139 	RSA *ret;
140 
141 	if ((ret = calloc(1, sizeof(RSA))) == NULL) {
142 		RSAerror(ERR_R_MALLOC_FAILURE);
143 		return NULL;
144 	}
145 
146 	ret->meth = RSA_get_default_method();
147 
148 #ifndef OPENSSL_NO_ENGINE
149 	if (engine != NULL) {
150 		if (!ENGINE_init(engine)) {
151 			RSAerror(ERR_R_ENGINE_LIB);
152 			goto err;
153 		}
154 		ret->engine = engine;
155 	} else {
156 		ret->engine = ENGINE_get_default_RSA();
157 	}
158 
159 	if (ret->engine != NULL) {
160 		if ((ret->meth = ENGINE_get_RSA(ret->engine)) == NULL) {
161 			RSAerror(ERR_R_ENGINE_LIB);
162 			goto err;
163 		}
164 	}
165 #endif
166 
167 	ret->references = 1;
168 	ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
169 
170 	if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data))
171 		goto err;
172 
173 	if (ret->meth->init != NULL && !ret->meth->init(ret)) {
174 		CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
175 		goto err;
176 	}
177 
178 	return ret;
179 
180  err:
181 #ifndef OPENSSL_NO_ENGINE
182 	ENGINE_finish(ret->engine);
183 #endif
184 	free(ret);
185 
186 	return NULL;
187 }
188 LCRYPTO_ALIAS(RSA_new_method);
189 
190 void
191 RSA_free(RSA *r)
192 {
193 	int i;
194 
195 	if (r == NULL)
196 		return;
197 
198 	i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_RSA);
199 	if (i > 0)
200 		return;
201 
202 	if (r->meth->finish)
203 		r->meth->finish(r);
204 #ifndef OPENSSL_NO_ENGINE
205 	ENGINE_finish(r->engine);
206 #endif
207 
208 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
209 
210 	BN_free(r->n);
211 	BN_free(r->e);
212 	BN_free(r->d);
213 	BN_free(r->p);
214 	BN_free(r->q);
215 	BN_free(r->dmp1);
216 	BN_free(r->dmq1);
217 	BN_free(r->iqmp);
218 	BN_BLINDING_free(r->blinding);
219 	BN_BLINDING_free(r->mt_blinding);
220 	RSA_PSS_PARAMS_free(r->pss);
221 	free(r);
222 }
223 LCRYPTO_ALIAS(RSA_free);
224 
225 int
226 RSA_up_ref(RSA *r)
227 {
228 	int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA);
229 	return i > 1 ? 1 : 0;
230 }
231 LCRYPTO_ALIAS(RSA_up_ref);
232 
233 int
234 RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
235     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
236 {
237 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp,
238 	    new_func, dup_func, free_func);
239 }
240 LCRYPTO_ALIAS(RSA_get_ex_new_index);
241 
242 int
243 RSA_set_ex_data(RSA *r, int idx, void *arg)
244 {
245 	return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
246 }
247 LCRYPTO_ALIAS(RSA_set_ex_data);
248 
249 void *
250 RSA_get_ex_data(const RSA *r, int idx)
251 {
252 	return CRYPTO_get_ex_data(&r->ex_data, idx);
253 }
254 LCRYPTO_ALIAS(RSA_get_ex_data);
255 
256 int
257 RSA_security_bits(const RSA *rsa)
258 {
259 	return BN_security_bits(RSA_bits(rsa), -1);
260 }
261 LCRYPTO_ALIAS(RSA_security_bits);
262 
263 void
264 RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
265 {
266 	if (n != NULL)
267 		*n = r->n;
268 	if (e != NULL)
269 		*e = r->e;
270 	if (d != NULL)
271 		*d = r->d;
272 }
273 LCRYPTO_ALIAS(RSA_get0_key);
274 
275 int
276 RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
277 {
278 	if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL))
279 		return 0;
280 
281 	if (n != NULL) {
282 		BN_free(r->n);
283 		r->n = n;
284 	}
285 	if (e != NULL) {
286 		BN_free(r->e);
287 		r->e = e;
288 	}
289 	if (d != NULL) {
290 		BN_free(r->d);
291 		r->d = d;
292 	}
293 
294 	return 1;
295 }
296 LCRYPTO_ALIAS(RSA_set0_key);
297 
298 void
299 RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1,
300     const BIGNUM **iqmp)
301 {
302 	if (dmp1 != NULL)
303 		*dmp1 = r->dmp1;
304 	if (dmq1 != NULL)
305 		*dmq1 = r->dmq1;
306 	if (iqmp != NULL)
307 		*iqmp = r->iqmp;
308 }
309 LCRYPTO_ALIAS(RSA_get0_crt_params);
310 
311 int
312 RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
313 {
314 	if ((r->dmp1 == NULL && dmp1 == NULL) ||
315 	    (r->dmq1 == NULL && dmq1 == NULL) ||
316 	    (r->iqmp == NULL && iqmp == NULL))
317 		return 0;
318 
319 	if (dmp1 != NULL) {
320 		BN_free(r->dmp1);
321 		r->dmp1 = dmp1;
322 	}
323 	if (dmq1 != NULL) {
324 		BN_free(r->dmq1);
325 		r->dmq1 = dmq1;
326 	}
327 	if (iqmp != NULL) {
328 		BN_free(r->iqmp);
329 		r->iqmp = iqmp;
330 	}
331 
332 	return 1;
333 }
334 LCRYPTO_ALIAS(RSA_set0_crt_params);
335 
336 void
337 RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
338 {
339 	if (p != NULL)
340 		*p = r->p;
341 	if (q != NULL)
342 		*q = r->q;
343 }
344 LCRYPTO_ALIAS(RSA_get0_factors);
345 
346 int
347 RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
348 {
349 	if ((r->p == NULL && p == NULL) || (r->q == NULL && q == NULL))
350 		return 0;
351 
352 	if (p != NULL) {
353 		BN_free(r->p);
354 		r->p = p;
355 	}
356 	if (q != NULL) {
357 		BN_free(r->q);
358 		r->q = q;
359 	}
360 
361 	return 1;
362 }
363 LCRYPTO_ALIAS(RSA_set0_factors);
364 
365 const BIGNUM *
366 RSA_get0_n(const RSA *r)
367 {
368 	return r->n;
369 }
370 LCRYPTO_ALIAS(RSA_get0_n);
371 
372 const BIGNUM *
373 RSA_get0_e(const RSA *r)
374 {
375 	return r->e;
376 }
377 LCRYPTO_ALIAS(RSA_get0_e);
378 
379 const BIGNUM *
380 RSA_get0_d(const RSA *r)
381 {
382 	return r->d;
383 }
384 LCRYPTO_ALIAS(RSA_get0_d);
385 
386 const BIGNUM *
387 RSA_get0_p(const RSA *r)
388 {
389 	return r->p;
390 }
391 LCRYPTO_ALIAS(RSA_get0_p);
392 
393 const BIGNUM *
394 RSA_get0_q(const RSA *r)
395 {
396 	return r->q;
397 }
398 LCRYPTO_ALIAS(RSA_get0_q);
399 
400 const BIGNUM *
401 RSA_get0_dmp1(const RSA *r)
402 {
403 	return r->dmp1;
404 }
405 LCRYPTO_ALIAS(RSA_get0_dmp1);
406 
407 const BIGNUM *
408 RSA_get0_dmq1(const RSA *r)
409 {
410 	return r->dmq1;
411 }
412 LCRYPTO_ALIAS(RSA_get0_dmq1);
413 
414 const BIGNUM *
415 RSA_get0_iqmp(const RSA *r)
416 {
417 	return r->iqmp;
418 }
419 LCRYPTO_ALIAS(RSA_get0_iqmp);
420 
421 const RSA_PSS_PARAMS *
422 RSA_get0_pss_params(const RSA *r)
423 {
424 	return r->pss;
425 }
426 LCRYPTO_ALIAS(RSA_get0_pss_params);
427 
428 void
429 RSA_clear_flags(RSA *r, int flags)
430 {
431 	r->flags &= ~flags;
432 }
433 LCRYPTO_ALIAS(RSA_clear_flags);
434 
435 int
436 RSA_test_flags(const RSA *r, int flags)
437 {
438 	return r->flags & flags;
439 }
440 LCRYPTO_ALIAS(RSA_test_flags);
441 
442 void
443 RSA_set_flags(RSA *r, int flags)
444 {
445 	r->flags |= flags;
446 }
447 LCRYPTO_ALIAS(RSA_set_flags);
448 
449 int
450 RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
451 {
452 	/* Return an error if the key type is not RSA or RSA-PSS. */
453 	if (ctx != NULL && ctx->pmeth != NULL &&
454 	    ctx->pmeth->pkey_id != EVP_PKEY_RSA &&
455 	    ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
456 		return -1;
457 
458 	return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
459 }
460 LCRYPTO_ALIAS(RSA_pkey_ctx_ctrl);
461