1 | /*
|
---|
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Licensed under the OpenSSL license (the "License"). You may not use
|
---|
5 | * this file except in compliance with the License. You can obtain a copy
|
---|
6 | * in the file LICENSE in the source distribution or at
|
---|
7 | * https://www.openssl.org/source/license.html
|
---|
8 | */
|
---|
9 |
|
---|
10 | #include <stdio.h>
|
---|
11 | #include <openssl/crypto.h>
|
---|
12 | #include "internal/cryptlib.h"
|
---|
13 | #include <openssl/lhash.h>
|
---|
14 | #include "internal/bn_int.h"
|
---|
15 | #include <openssl/engine.h>
|
---|
16 | #include "rsa_locl.h"
|
---|
17 |
|
---|
18 | RSA *RSA_new(void)
|
---|
19 | {
|
---|
20 | return RSA_new_method(NULL);
|
---|
21 | }
|
---|
22 |
|
---|
23 | const RSA_METHOD *RSA_get_method(const RSA *rsa)
|
---|
24 | {
|
---|
25 | return rsa->meth;
|
---|
26 | }
|
---|
27 |
|
---|
28 | int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
|
---|
29 | {
|
---|
30 | /*
|
---|
31 | * NB: The caller is specifically setting a method, so it's not up to us
|
---|
32 | * to deal with which ENGINE it comes from.
|
---|
33 | */
|
---|
34 | const RSA_METHOD *mtmp;
|
---|
35 | mtmp = rsa->meth;
|
---|
36 | if (mtmp->finish)
|
---|
37 | mtmp->finish(rsa);
|
---|
38 | #ifndef OPENSSL_NO_ENGINE
|
---|
39 | ENGINE_finish(rsa->engine);
|
---|
40 | rsa->engine = NULL;
|
---|
41 | #endif
|
---|
42 | rsa->meth = meth;
|
---|
43 | if (meth->init)
|
---|
44 | meth->init(rsa);
|
---|
45 | return 1;
|
---|
46 | }
|
---|
47 |
|
---|
48 | RSA *RSA_new_method(ENGINE *engine)
|
---|
49 | {
|
---|
50 | RSA *ret = OPENSSL_zalloc(sizeof(*ret));
|
---|
51 |
|
---|
52 | if (ret == NULL) {
|
---|
53 | RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
|
---|
54 | return NULL;
|
---|
55 | }
|
---|
56 |
|
---|
57 | ret->references = 1;
|
---|
58 | ret->lock = CRYPTO_THREAD_lock_new();
|
---|
59 | if (ret->lock == NULL) {
|
---|
60 | RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
|
---|
61 | OPENSSL_free(ret);
|
---|
62 | return NULL;
|
---|
63 | }
|
---|
64 |
|
---|
65 | ret->meth = RSA_get_default_method();
|
---|
66 | #ifndef OPENSSL_NO_ENGINE
|
---|
67 | ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
|
---|
68 | if (engine) {
|
---|
69 | if (!ENGINE_init(engine)) {
|
---|
70 | RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
|
---|
71 | goto err;
|
---|
72 | }
|
---|
73 | ret->engine = engine;
|
---|
74 | } else
|
---|
75 | ret->engine = ENGINE_get_default_RSA();
|
---|
76 | if (ret->engine) {
|
---|
77 | ret->meth = ENGINE_get_RSA(ret->engine);
|
---|
78 | if (ret->meth == NULL) {
|
---|
79 | RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
|
---|
80 | goto err;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | #endif
|
---|
84 |
|
---|
85 | ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
|
---|
86 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
|
---|
87 | goto err;
|
---|
88 | }
|
---|
89 |
|
---|
90 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
|
---|
91 | RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
|
---|
92 | goto err;
|
---|
93 | }
|
---|
94 |
|
---|
95 | return ret;
|
---|
96 |
|
---|
97 | err:
|
---|
98 | RSA_free(ret);
|
---|
99 | return NULL;
|
---|
100 | }
|
---|
101 |
|
---|
102 | void RSA_free(RSA *r)
|
---|
103 | {
|
---|
104 | int i;
|
---|
105 |
|
---|
106 | if (r == NULL)
|
---|
107 | return;
|
---|
108 |
|
---|
109 | CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
|
---|
110 | REF_PRINT_COUNT("RSA", r);
|
---|
111 | if (i > 0)
|
---|
112 | return;
|
---|
113 | REF_ASSERT_ISNT(i < 0);
|
---|
114 |
|
---|
115 | if (r->meth->finish)
|
---|
116 | r->meth->finish(r);
|
---|
117 | #ifndef OPENSSL_NO_ENGINE
|
---|
118 | ENGINE_finish(r->engine);
|
---|
119 | #endif
|
---|
120 |
|
---|
121 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
|
---|
122 |
|
---|
123 | CRYPTO_THREAD_lock_free(r->lock);
|
---|
124 |
|
---|
125 | BN_clear_free(r->n);
|
---|
126 | BN_clear_free(r->e);
|
---|
127 | BN_clear_free(r->d);
|
---|
128 | BN_clear_free(r->p);
|
---|
129 | BN_clear_free(r->q);
|
---|
130 | BN_clear_free(r->dmp1);
|
---|
131 | BN_clear_free(r->dmq1);
|
---|
132 | BN_clear_free(r->iqmp);
|
---|
133 | BN_BLINDING_free(r->blinding);
|
---|
134 | BN_BLINDING_free(r->mt_blinding);
|
---|
135 | OPENSSL_free(r->bignum_data);
|
---|
136 | OPENSSL_free(r);
|
---|
137 | }
|
---|
138 |
|
---|
139 | int RSA_up_ref(RSA *r)
|
---|
140 | {
|
---|
141 | int i;
|
---|
142 |
|
---|
143 | if (CRYPTO_atomic_add(&r->references, 1, &i, r->lock) <= 0)
|
---|
144 | return 0;
|
---|
145 |
|
---|
146 | REF_PRINT_COUNT("RSA", r);
|
---|
147 | REF_ASSERT_ISNT(i < 2);
|
---|
148 | return ((i > 1) ? 1 : 0);
|
---|
149 | }
|
---|
150 |
|
---|
151 | int RSA_set_ex_data(RSA *r, int idx, void *arg)
|
---|
152 | {
|
---|
153 | return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
|
---|
154 | }
|
---|
155 |
|
---|
156 | void *RSA_get_ex_data(const RSA *r, int idx)
|
---|
157 | {
|
---|
158 | return (CRYPTO_get_ex_data(&r->ex_data, idx));
|
---|
159 | }
|
---|
160 |
|
---|
161 | int RSA_security_bits(const RSA *rsa)
|
---|
162 | {
|
---|
163 | return BN_security_bits(BN_num_bits(rsa->n), -1);
|
---|
164 | }
|
---|
165 |
|
---|
166 | int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
|
---|
167 | {
|
---|
168 | /* If the fields n and e in r are NULL, the corresponding input
|
---|
169 | * parameters MUST be non-NULL for n and e. d may be
|
---|
170 | * left NULL (in case only the public key is used).
|
---|
171 | */
|
---|
172 | if ((r->n == NULL && n == NULL)
|
---|
173 | || (r->e == NULL && e == NULL))
|
---|
174 | return 0;
|
---|
175 |
|
---|
176 | if (n != NULL) {
|
---|
177 | BN_free(r->n);
|
---|
178 | r->n = n;
|
---|
179 | }
|
---|
180 | if (e != NULL) {
|
---|
181 | BN_free(r->e);
|
---|
182 | r->e = e;
|
---|
183 | }
|
---|
184 | if (d != NULL) {
|
---|
185 | BN_free(r->d);
|
---|
186 | r->d = d;
|
---|
187 | }
|
---|
188 |
|
---|
189 | return 1;
|
---|
190 | }
|
---|
191 |
|
---|
192 | int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
|
---|
193 | {
|
---|
194 | /* If the fields p and q in r are NULL, the corresponding input
|
---|
195 | * parameters MUST be non-NULL.
|
---|
196 | */
|
---|
197 | if ((r->p == NULL && p == NULL)
|
---|
198 | || (r->q == NULL && q == NULL))
|
---|
199 | return 0;
|
---|
200 |
|
---|
201 | if (p != NULL) {
|
---|
202 | BN_free(r->p);
|
---|
203 | r->p = p;
|
---|
204 | }
|
---|
205 | if (q != NULL) {
|
---|
206 | BN_free(r->q);
|
---|
207 | r->q = q;
|
---|
208 | }
|
---|
209 |
|
---|
210 | return 1;
|
---|
211 | }
|
---|
212 |
|
---|
213 | int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
|
---|
214 | {
|
---|
215 | /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
|
---|
216 | * parameters MUST be non-NULL.
|
---|
217 | */
|
---|
218 | if ((r->dmp1 == NULL && dmp1 == NULL)
|
---|
219 | || (r->dmq1 == NULL && dmq1 == NULL)
|
---|
220 | || (r->iqmp == NULL && iqmp == NULL))
|
---|
221 | return 0;
|
---|
222 |
|
---|
223 | if (dmp1 != NULL) {
|
---|
224 | BN_free(r->dmp1);
|
---|
225 | r->dmp1 = dmp1;
|
---|
226 | }
|
---|
227 | if (dmq1 != NULL) {
|
---|
228 | BN_free(r->dmq1);
|
---|
229 | r->dmq1 = dmq1;
|
---|
230 | }
|
---|
231 | if (iqmp != NULL) {
|
---|
232 | BN_free(r->iqmp);
|
---|
233 | r->iqmp = iqmp;
|
---|
234 | }
|
---|
235 |
|
---|
236 | return 1;
|
---|
237 | }
|
---|
238 |
|
---|
239 | void RSA_get0_key(const RSA *r,
|
---|
240 | const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
|
---|
241 | {
|
---|
242 | if (n != NULL)
|
---|
243 | *n = r->n;
|
---|
244 | if (e != NULL)
|
---|
245 | *e = r->e;
|
---|
246 | if (d != NULL)
|
---|
247 | *d = r->d;
|
---|
248 | }
|
---|
249 |
|
---|
250 | void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
|
---|
251 | {
|
---|
252 | if (p != NULL)
|
---|
253 | *p = r->p;
|
---|
254 | if (q != NULL)
|
---|
255 | *q = r->q;
|
---|
256 | }
|
---|
257 |
|
---|
258 | void RSA_get0_crt_params(const RSA *r,
|
---|
259 | const BIGNUM **dmp1, const BIGNUM **dmq1,
|
---|
260 | const BIGNUM **iqmp)
|
---|
261 | {
|
---|
262 | if (dmp1 != NULL)
|
---|
263 | *dmp1 = r->dmp1;
|
---|
264 | if (dmq1 != NULL)
|
---|
265 | *dmq1 = r->dmq1;
|
---|
266 | if (iqmp != NULL)
|
---|
267 | *iqmp = r->iqmp;
|
---|
268 | }
|
---|
269 |
|
---|
270 | void RSA_clear_flags(RSA *r, int flags)
|
---|
271 | {
|
---|
272 | r->flags &= ~flags;
|
---|
273 | }
|
---|
274 |
|
---|
275 | int RSA_test_flags(const RSA *r, int flags)
|
---|
276 | {
|
---|
277 | return r->flags & flags;
|
---|
278 | }
|
---|
279 |
|
---|
280 | void RSA_set_flags(RSA *r, int flags)
|
---|
281 | {
|
---|
282 | r->flags |= flags;
|
---|
283 | }
|
---|
284 |
|
---|
285 | ENGINE *RSA_get0_engine(const RSA *r)
|
---|
286 | {
|
---|
287 | return r->engine;
|
---|
288 | }
|
---|