xref: /openbsd-src/lib/libcrypto/rsa/rsa_lib.c (revision 8e0c768258d4632c51876b4397034bc3152bf8db)
1 /* $OpenBSD: rsa_lib.c,v 1.39 2019/11/01 03:41:40 jsing 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_locl.h"
71 
72 #ifndef OPENSSL_NO_ENGINE
73 #include <openssl/engine.h>
74 #endif
75 
76 static const RSA_METHOD *default_RSA_meth = NULL;
77 
78 RSA *
79 RSA_new(void)
80 {
81 	RSA *r = RSA_new_method(NULL);
82 
83 	return r;
84 }
85 
86 void
87 RSA_set_default_method(const RSA_METHOD *meth)
88 {
89 	default_RSA_meth = meth;
90 }
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 
101 const RSA_METHOD *
102 RSA_get_method(const RSA *rsa)
103 {
104 	return rsa->meth;
105 }
106 
107 int
108 RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
109 {
110 	/*
111 	 * NB: The caller is specifically setting a method, so it's not up to us
112 	 * to deal with which ENGINE it comes from.
113 	 */
114 	const RSA_METHOD *mtmp;
115 
116 	mtmp = rsa->meth;
117 	if (mtmp->finish)
118 		mtmp->finish(rsa);
119 #ifndef OPENSSL_NO_ENGINE
120 	ENGINE_finish(rsa->engine);
121 	rsa->engine = NULL;
122 #endif
123 	rsa->meth = meth;
124 	if (meth->init)
125 		meth->init(rsa);
126 	return 1;
127 }
128 
129 RSA *
130 RSA_new_method(ENGINE *engine)
131 {
132 	RSA *ret;
133 
134 	if ((ret = calloc(1, sizeof(RSA))) == NULL) {
135 		RSAerror(ERR_R_MALLOC_FAILURE);
136 		return NULL;
137 	}
138 
139 	ret->meth = RSA_get_default_method();
140 
141 #ifndef OPENSSL_NO_ENGINE
142 	if (engine != NULL) {
143 		if (!ENGINE_init(engine)) {
144 			RSAerror(ERR_R_ENGINE_LIB);
145 			goto err;
146 		}
147 		ret->engine = engine;
148 	} else {
149 		ret->engine = ENGINE_get_default_RSA();
150 	}
151 
152 	if (ret->engine != NULL) {
153 		if ((ret->meth = ENGINE_get_RSA(ret->engine)) == NULL) {
154 			RSAerror(ERR_R_ENGINE_LIB);
155 			goto err;
156 		}
157 	}
158 #endif
159 
160 	ret->references = 1;
161 	ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
162 
163 	if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data))
164 		goto err;
165 
166 	if (ret->meth->init != NULL && !ret->meth->init(ret)) {
167 		CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
168 		goto err;
169 	}
170 
171 	return ret;
172 
173  err:
174 #ifndef OPENSSL_NO_ENGINE
175 	ENGINE_finish(ret->engine);
176 #endif
177 	free(ret);
178 
179 	return NULL;
180 }
181 
182 void
183 RSA_free(RSA *r)
184 {
185 	int i;
186 
187 	if (r == NULL)
188 		return;
189 
190 	i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_RSA);
191 	if (i > 0)
192 		return;
193 
194 	if (r->meth->finish)
195 		r->meth->finish(r);
196 #ifndef OPENSSL_NO_ENGINE
197 	ENGINE_finish(r->engine);
198 #endif
199 
200 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
201 
202 	BN_clear_free(r->n);
203 	BN_clear_free(r->e);
204 	BN_clear_free(r->d);
205 	BN_clear_free(r->p);
206 	BN_clear_free(r->q);
207 	BN_clear_free(r->dmp1);
208 	BN_clear_free(r->dmq1);
209 	BN_clear_free(r->iqmp);
210 	BN_BLINDING_free(r->blinding);
211 	BN_BLINDING_free(r->mt_blinding);
212 	free(r);
213 }
214 
215 int
216 RSA_up_ref(RSA *r)
217 {
218 	int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA);
219 	return i > 1 ? 1 : 0;
220 }
221 
222 int
223 RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
224     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
225 {
226 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp,
227 	    new_func, dup_func, free_func);
228 }
229 
230 int
231 RSA_set_ex_data(RSA *r, int idx, void *arg)
232 {
233 	return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
234 }
235 
236 void *
237 RSA_get_ex_data(const RSA *r, int idx)
238 {
239 	return CRYPTO_get_ex_data(&r->ex_data, idx);
240 }
241 
242 void
243 RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
244 {
245 	if (n != NULL)
246 		*n = r->n;
247 	if (e != NULL)
248 		*e = r->e;
249 	if (d != NULL)
250 		*d = r->d;
251 }
252 
253 int
254 RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
255 {
256 	if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL))
257 		return 0;
258 
259 	if (n != NULL) {
260 		BN_free(r->n);
261 		r->n = n;
262 	}
263 	if (e != NULL) {
264 		BN_free(r->e);
265 		r->e = e;
266 	}
267 	if (d != NULL) {
268 		BN_free(r->d);
269 		r->d = d;
270 	}
271 
272 	return 1;
273 }
274 
275 void
276 RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1,
277     const BIGNUM **iqmp)
278 {
279 	if (dmp1 != NULL)
280 		*dmp1 = r->dmp1;
281 	if (dmq1 != NULL)
282 		*dmq1 = r->dmq1;
283 	if (iqmp != NULL)
284 		*iqmp = r->iqmp;
285 }
286 
287 int
288 RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
289 {
290 	if ((r->dmp1 == NULL && dmp1 == NULL) ||
291 	    (r->dmq1 == NULL && dmq1 == NULL) ||
292 	    (r->iqmp == NULL && iqmp == NULL))
293 	       	return 0;
294 
295 	if (dmp1 != NULL) {
296 		BN_free(r->dmp1);
297 		r->dmp1 = dmp1;
298 	}
299 	if (dmq1 != NULL) {
300 		BN_free(r->dmq1);
301 		r->dmq1 = dmq1;
302 	}
303 	if (iqmp != NULL) {
304 		BN_free(r->iqmp);
305 		r->iqmp = iqmp;
306 	}
307 
308 	return 1;
309 }
310 
311 void
312 RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
313 {
314 	if (p != NULL)
315 		*p = r->p;
316 	if (q != NULL)
317 		*q = r->q;
318 }
319 
320 int
321 RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
322 {
323 	if ((r->p == NULL && p == NULL) || (r->q == NULL && q == NULL))
324 		return 0;
325 
326 	if (p != NULL) {
327 		BN_free(r->p);
328 		r->p = p;
329 	}
330 	if (q != NULL) {
331 		BN_free(r->q);
332 		r->q = q;
333 	}
334 
335 	return 1;
336 }
337 
338 void
339 RSA_clear_flags(RSA *r, int flags)
340 {
341 	r->flags &= ~flags;
342 }
343 
344 int
345 RSA_test_flags(const RSA *r, int flags)
346 {
347 	return r->flags & flags;
348 }
349 
350 void
351 RSA_set_flags(RSA *r, int flags)
352 {
353 	r->flags |= flags;
354 }
355 
356 int
357 RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
358 {
359 	/* Return an error if the key type is not RSA or RSA-PSS. */
360 	if (ctx != NULL && ctx->pmeth != NULL &&
361 	    ctx->pmeth->pkey_id != EVP_PKEY_RSA &&
362 	    ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
363 		return -1;
364 
365 	return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
366 }
367