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