xref: /openbsd-src/lib/libcrypto/rsa/rsa_lib.c (revision 323e3475f1da3db6e3b7e4f8288ec466cec2ddcc)
1 /* $OpenBSD: rsa_lib.c,v 1.26 2014/07/10 07:43:11 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 #include <openssl/crypto.h>
61 #include "cryptlib.h"
62 #include <openssl/lhash.h>
63 #include <openssl/bn.h>
64 #include <openssl/rsa.h>
65 #include <openssl/rand.h>
66 #ifndef OPENSSL_NO_ENGINE
67 #include <openssl/engine.h>
68 #endif
69 
70 static const RSA_METHOD *default_RSA_meth = NULL;
71 
72 RSA *
73 RSA_new(void)
74 {
75 	RSA *r = RSA_new_method(NULL);
76 
77 	return r;
78 }
79 
80 void
81 RSA_set_default_method(const RSA_METHOD *meth)
82 {
83 	default_RSA_meth = meth;
84 }
85 
86 const RSA_METHOD *
87 RSA_get_default_method(void)
88 {
89 	if (default_RSA_meth == NULL)
90 		default_RSA_meth = RSA_PKCS1_SSLeay();
91 
92 	return default_RSA_meth;
93 }
94 
95 const RSA_METHOD *
96 RSA_get_method(const RSA *rsa)
97 {
98 	return rsa->meth;
99 }
100 
101 int
102 RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
103 {
104 	/*
105 	 * NB: The caller is specifically setting a method, so it's not up to us
106 	 * to deal with which ENGINE it comes from.
107 	 */
108 	const RSA_METHOD *mtmp;
109 
110 	mtmp = rsa->meth;
111 	if (mtmp->finish)
112 		mtmp->finish(rsa);
113 #ifndef OPENSSL_NO_ENGINE
114 	if (rsa->engine) {
115 		ENGINE_finish(rsa->engine);
116 		rsa->engine = NULL;
117 	}
118 #endif
119 	rsa->meth = meth;
120 	if (meth->init)
121 		meth->init(rsa);
122 	return 1;
123 }
124 
125 RSA *
126 RSA_new_method(ENGINE *engine)
127 {
128 	RSA *ret;
129 
130 	ret = malloc(sizeof(RSA));
131 	if (ret == NULL) {
132 		RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
133 		return NULL;
134 	}
135 
136 	ret->meth = RSA_get_default_method();
137 #ifndef OPENSSL_NO_ENGINE
138 	if (engine) {
139 		if (!ENGINE_init(engine)) {
140 			RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
141 			free(ret);
142 			return NULL;
143 		}
144 		ret->engine = engine;
145 	} else
146 		ret->engine = ENGINE_get_default_RSA();
147 	if (ret->engine) {
148 		ret->meth = ENGINE_get_RSA(ret->engine);
149 		if (!ret->meth) {
150 			RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
151 			ENGINE_finish(ret->engine);
152 			free(ret);
153 			return NULL;
154 		}
155 	}
156 #endif
157 
158 	ret->pad = 0;
159 	ret->version = 0;
160 	ret->n = NULL;
161 	ret->e = NULL;
162 	ret->d = NULL;
163 	ret->p = NULL;
164 	ret->q = NULL;
165 	ret->dmp1 = NULL;
166 	ret->dmq1 = NULL;
167 	ret->iqmp = NULL;
168 	ret->references = 1;
169 	ret->_method_mod_n = NULL;
170 	ret->_method_mod_p = NULL;
171 	ret->_method_mod_q = NULL;
172 	ret->blinding = NULL;
173 	ret->mt_blinding = NULL;
174 	ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
175 	if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
176 #ifndef OPENSSL_NO_ENGINE
177 		if (ret->engine)
178 			ENGINE_finish(ret->engine);
179 #endif
180 		free(ret);
181 		return NULL;
182 	}
183 
184 	if (ret->meth->init != NULL && !ret->meth->init(ret)) {
185 #ifndef OPENSSL_NO_ENGINE
186 		if (ret->engine)
187 			ENGINE_finish(ret->engine);
188 #endif
189 		CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
190 		free(ret);
191 		ret = NULL;
192 	}
193 	return ret;
194 }
195 
196 void
197 RSA_free(RSA *r)
198 {
199 	int i;
200 
201 	if (r == NULL)
202 		return;
203 
204 	i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_RSA);
205 	if (i > 0)
206 		return;
207 
208 	if (r->meth->finish)
209 		r->meth->finish(r);
210 #ifndef OPENSSL_NO_ENGINE
211 	if (r->engine)
212 		ENGINE_finish(r->engine);
213 #endif
214 
215 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
216 
217 	BN_clear_free(r->n);
218 	BN_clear_free(r->e);
219 	BN_clear_free(r->d);
220 	BN_clear_free(r->p);
221 	BN_clear_free(r->q);
222 	BN_clear_free(r->dmp1);
223 	BN_clear_free(r->dmq1);
224 	BN_clear_free(r->iqmp);
225 	BN_BLINDING_free(r->blinding);
226 	BN_BLINDING_free(r->mt_blinding);
227 	free(r);
228 }
229 
230 int
231 RSA_up_ref(RSA *r)
232 {
233 	int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA);
234 	return i > 1 ? 1 : 0;
235 }
236 
237 int
238 RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
239     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
240 {
241 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp,
242 	    new_func, dup_func, free_func);
243 }
244 
245 int
246 RSA_set_ex_data(RSA *r, int idx, void *arg)
247 {
248 	return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
249 }
250 
251 void *
252 RSA_get_ex_data(const RSA *r, int idx)
253 {
254 	return CRYPTO_get_ex_data(&r->ex_data, idx);
255 }
256