/src/openssl/crypto/rsa/rsa_pmeth.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* crypto/rsa/rsa_pmeth.c */ |
2 | | /* |
3 | | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project |
4 | | * 2006. |
5 | | */ |
6 | | /* ==================================================================== |
7 | | * Copyright (c) 2006 The OpenSSL Project. All rights reserved. |
8 | | * |
9 | | * Redistribution and use in source and binary forms, with or without |
10 | | * modification, are permitted provided that the following conditions |
11 | | * are met: |
12 | | * |
13 | | * 1. Redistributions of source code must retain the above copyright |
14 | | * notice, this list of conditions and the following disclaimer. |
15 | | * |
16 | | * 2. Redistributions in binary form must reproduce the above copyright |
17 | | * notice, this list of conditions and the following disclaimer in |
18 | | * the documentation and/or other materials provided with the |
19 | | * distribution. |
20 | | * |
21 | | * 3. All advertising materials mentioning features or use of this |
22 | | * software must display the following acknowledgment: |
23 | | * "This product includes software developed by the OpenSSL Project |
24 | | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
25 | | * |
26 | | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
27 | | * endorse or promote products derived from this software without |
28 | | * prior written permission. For written permission, please contact |
29 | | * licensing@OpenSSL.org. |
30 | | * |
31 | | * 5. Products derived from this software may not be called "OpenSSL" |
32 | | * nor may "OpenSSL" appear in their names without prior written |
33 | | * permission of the OpenSSL Project. |
34 | | * |
35 | | * 6. Redistributions of any form whatsoever must retain the following |
36 | | * acknowledgment: |
37 | | * "This product includes software developed by the OpenSSL Project |
38 | | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
39 | | * |
40 | | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
41 | | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
42 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
43 | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
44 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
45 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
46 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
47 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
48 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
49 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
50 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
51 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
52 | | * ==================================================================== |
53 | | * |
54 | | * This product includes cryptographic software written by Eric Young |
55 | | * (eay@cryptsoft.com). This product includes software written by Tim |
56 | | * Hudson (tjh@cryptsoft.com). |
57 | | * |
58 | | */ |
59 | | |
60 | | #include <stdio.h> |
61 | | #include "cryptlib.h" |
62 | | #include <openssl/asn1t.h> |
63 | | #include <openssl/x509.h> |
64 | | #include <openssl/rsa.h> |
65 | | #include <openssl/bn.h> |
66 | | #include <openssl/evp.h> |
67 | | #include <openssl/x509v3.h> |
68 | | #ifndef OPENSSL_NO_CMS |
69 | | # include <openssl/cms.h> |
70 | | #endif |
71 | | #ifdef OPENSSL_FIPS |
72 | | # include <openssl/fips.h> |
73 | | #endif |
74 | | #include "evp_locl.h" |
75 | | #include "rsa_locl.h" |
76 | | |
77 | | /* RSA pkey context structure */ |
78 | | |
79 | | typedef struct { |
80 | | /* Key gen parameters */ |
81 | | int nbits; |
82 | | BIGNUM *pub_exp; |
83 | | /* Keygen callback info */ |
84 | | int gentmp[2]; |
85 | | /* RSA padding mode */ |
86 | | int pad_mode; |
87 | | /* message digest */ |
88 | | const EVP_MD *md; |
89 | | /* message digest for MGF1 */ |
90 | | const EVP_MD *mgf1md; |
91 | | /* PSS salt length */ |
92 | | int saltlen; |
93 | | /* Temp buffer */ |
94 | | unsigned char *tbuf; |
95 | | /* OAEP label */ |
96 | | unsigned char *oaep_label; |
97 | | size_t oaep_labellen; |
98 | | } RSA_PKEY_CTX; |
99 | | |
100 | | static int pkey_rsa_init(EVP_PKEY_CTX *ctx) |
101 | 0 | { |
102 | 0 | RSA_PKEY_CTX *rctx; |
103 | 0 | rctx = OPENSSL_malloc(sizeof(RSA_PKEY_CTX)); |
104 | 0 | if (!rctx) |
105 | 0 | return 0; |
106 | 0 | rctx->nbits = 1024; |
107 | 0 | rctx->pub_exp = NULL; |
108 | 0 | rctx->pad_mode = RSA_PKCS1_PADDING; |
109 | 0 | rctx->md = NULL; |
110 | 0 | rctx->mgf1md = NULL; |
111 | 0 | rctx->tbuf = NULL; |
112 | |
|
113 | 0 | rctx->saltlen = -2; |
114 | |
|
115 | 0 | rctx->oaep_label = NULL; |
116 | 0 | rctx->oaep_labellen = 0; |
117 | |
|
118 | 0 | ctx->data = rctx; |
119 | 0 | ctx->keygen_info = rctx->gentmp; |
120 | 0 | ctx->keygen_info_count = 2; |
121 | |
|
122 | 0 | return 1; |
123 | 0 | } |
124 | | |
125 | | static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) |
126 | 0 | { |
127 | 0 | RSA_PKEY_CTX *dctx, *sctx; |
128 | 0 | if (!pkey_rsa_init(dst)) |
129 | 0 | return 0; |
130 | 0 | sctx = src->data; |
131 | 0 | dctx = dst->data; |
132 | 0 | dctx->nbits = sctx->nbits; |
133 | 0 | if (sctx->pub_exp) { |
134 | 0 | dctx->pub_exp = BN_dup(sctx->pub_exp); |
135 | 0 | if (!dctx->pub_exp) |
136 | 0 | return 0; |
137 | 0 | } |
138 | 0 | dctx->pad_mode = sctx->pad_mode; |
139 | 0 | dctx->md = sctx->md; |
140 | 0 | dctx->mgf1md = sctx->mgf1md; |
141 | 0 | if (sctx->oaep_label) { |
142 | 0 | if (dctx->oaep_label) |
143 | 0 | OPENSSL_free(dctx->oaep_label); |
144 | 0 | dctx->oaep_label = BUF_memdup(sctx->oaep_label, sctx->oaep_labellen); |
145 | 0 | if (!dctx->oaep_label) |
146 | 0 | return 0; |
147 | 0 | dctx->oaep_labellen = sctx->oaep_labellen; |
148 | 0 | } |
149 | 0 | return 1; |
150 | 0 | } |
151 | | |
152 | | static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk) |
153 | 0 | { |
154 | 0 | if (ctx->tbuf) |
155 | 0 | return 1; |
156 | 0 | ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey)); |
157 | 0 | if (!ctx->tbuf) |
158 | 0 | return 0; |
159 | 0 | return 1; |
160 | 0 | } |
161 | | |
162 | | static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx) |
163 | 0 | { |
164 | 0 | RSA_PKEY_CTX *rctx = ctx->data; |
165 | 0 | if (rctx) { |
166 | 0 | if (rctx->pub_exp) |
167 | 0 | BN_free(rctx->pub_exp); |
168 | 0 | if (rctx->tbuf) |
169 | 0 | OPENSSL_free(rctx->tbuf); |
170 | 0 | if (rctx->oaep_label) |
171 | 0 | OPENSSL_free(rctx->oaep_label); |
172 | 0 | OPENSSL_free(rctx); |
173 | 0 | } |
174 | 0 | } |
175 | | |
176 | | #ifdef OPENSSL_FIPS |
177 | | /* |
178 | | * FIP checker. Return value indicates status of context parameters: 1 : |
179 | | * redirect to FIPS. 0 : don't redirect to FIPS. -1 : illegal operation in |
180 | | * FIPS mode. |
181 | | */ |
182 | | |
183 | | static int pkey_fips_check_rsa(const RSA *rsa, const EVP_MD **pmd, |
184 | | const EVP_MD **pmgf1md) |
185 | | { |
186 | | int rv = -1; |
187 | | |
188 | | if (!FIPS_mode()) |
189 | | return 0; |
190 | | if (rsa->flags & RSA_FLAG_NON_FIPS_ALLOW) |
191 | | rv = 0; |
192 | | if (!(rsa->meth->flags & RSA_FLAG_FIPS_METHOD) && rv) |
193 | | return -1; |
194 | | if (*pmd != NULL) { |
195 | | *pmd = FIPS_get_digestbynid(EVP_MD_type(*pmd)); |
196 | | if (*pmd == NULL || !((*pmd)->flags & EVP_MD_FLAG_FIPS)) |
197 | | return rv; |
198 | | } |
199 | | if (*pmgf1md != NULL) { |
200 | | *pmgf1md = FIPS_get_digestbynid(EVP_MD_type(*pmgf1md)); |
201 | | if (*pmgf1md == NULL || !((*pmgf1md)->flags & EVP_MD_FLAG_FIPS)) |
202 | | return rv; |
203 | | } |
204 | | return 1; |
205 | | } |
206 | | #endif |
207 | | |
208 | | static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, |
209 | | size_t *siglen, const unsigned char *tbs, |
210 | | size_t tbslen) |
211 | 0 | { |
212 | 0 | int ret; |
213 | 0 | RSA_PKEY_CTX *rctx = ctx->data; |
214 | 0 | RSA *rsa = ctx->pkey->pkey.rsa; |
215 | 0 | const EVP_MD *md = rctx->md; |
216 | 0 | const EVP_MD *mgf1md = rctx->mgf1md; |
217 | |
|
218 | | #ifdef OPENSSL_FIPS |
219 | | ret = pkey_fips_check_rsa(rsa, &md, &mgf1md); |
220 | | if (ret < 0) { |
221 | | RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE); |
222 | | return -1; |
223 | | } |
224 | | #endif |
225 | |
|
226 | 0 | if (md != NULL) { |
227 | 0 | if (tbslen != (size_t)EVP_MD_size(md)) { |
228 | 0 | RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_INVALID_DIGEST_LENGTH); |
229 | 0 | return -1; |
230 | 0 | } |
231 | | #ifdef OPENSSL_FIPS |
232 | | if (ret > 0) { |
233 | | unsigned int slen; |
234 | | ret = FIPS_rsa_sign_digest(rsa, tbs, tbslen, md, rctx->pad_mode, |
235 | | rctx->saltlen, mgf1md, sig, &slen); |
236 | | if (ret > 0) |
237 | | *siglen = slen; |
238 | | else |
239 | | *siglen = 0; |
240 | | return ret; |
241 | | } |
242 | | #endif |
243 | | |
244 | 0 | if (EVP_MD_type(md) == NID_mdc2) { |
245 | 0 | unsigned int sltmp; |
246 | 0 | if (rctx->pad_mode != RSA_PKCS1_PADDING) |
247 | 0 | return -1; |
248 | 0 | ret = RSA_sign_ASN1_OCTET_STRING(NID_mdc2, tbs, tbslen, sig, &sltmp, |
249 | 0 | rsa); |
250 | |
|
251 | 0 | if (ret <= 0) |
252 | 0 | return ret; |
253 | 0 | ret = sltmp; |
254 | 0 | } else if (rctx->pad_mode == RSA_X931_PADDING) { |
255 | 0 | if ((size_t)EVP_PKEY_size(ctx->pkey) < tbslen + 1) { |
256 | 0 | RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_KEY_SIZE_TOO_SMALL); |
257 | 0 | return -1; |
258 | 0 | } |
259 | 0 | if (!setup_tbuf(rctx, ctx)) { |
260 | 0 | RSAerr(RSA_F_PKEY_RSA_SIGN, ERR_R_MALLOC_FAILURE); |
261 | 0 | return -1; |
262 | 0 | } |
263 | 0 | memcpy(rctx->tbuf, tbs, tbslen); |
264 | 0 | rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(md)); |
265 | 0 | ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf, |
266 | 0 | sig, rsa, RSA_X931_PADDING); |
267 | 0 | } else if (rctx->pad_mode == RSA_PKCS1_PADDING) { |
268 | 0 | unsigned int sltmp; |
269 | 0 | ret = RSA_sign(EVP_MD_type(md), tbs, tbslen, sig, &sltmp, rsa); |
270 | 0 | if (ret <= 0) |
271 | 0 | return ret; |
272 | 0 | ret = sltmp; |
273 | 0 | } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) { |
274 | 0 | if (!setup_tbuf(rctx, ctx)) |
275 | 0 | return -1; |
276 | 0 | if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa, rctx->tbuf, tbs, |
277 | 0 | md, mgf1md, rctx->saltlen)) |
278 | 0 | return -1; |
279 | 0 | ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf, |
280 | 0 | sig, rsa, RSA_NO_PADDING); |
281 | 0 | } else |
282 | 0 | return -1; |
283 | 0 | } else |
284 | 0 | ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa, |
285 | 0 | rctx->pad_mode); |
286 | 0 | if (ret < 0) |
287 | 0 | return ret; |
288 | 0 | *siglen = ret; |
289 | 0 | return 1; |
290 | 0 | } |
291 | | |
292 | | static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx, |
293 | | unsigned char *rout, size_t *routlen, |
294 | | const unsigned char *sig, size_t siglen) |
295 | 0 | { |
296 | 0 | int ret; |
297 | 0 | RSA_PKEY_CTX *rctx = ctx->data; |
298 | |
|
299 | 0 | if (rctx->md) { |
300 | 0 | if (rctx->pad_mode == RSA_X931_PADDING) { |
301 | 0 | if (!setup_tbuf(rctx, ctx)) |
302 | 0 | return -1; |
303 | 0 | ret = RSA_public_decrypt(siglen, sig, |
304 | 0 | rctx->tbuf, ctx->pkey->pkey.rsa, |
305 | 0 | RSA_X931_PADDING); |
306 | 0 | if (ret < 1) |
307 | 0 | return 0; |
308 | 0 | ret--; |
309 | 0 | if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) { |
310 | 0 | RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER, |
311 | 0 | RSA_R_ALGORITHM_MISMATCH); |
312 | 0 | return 0; |
313 | 0 | } |
314 | 0 | if (ret != EVP_MD_size(rctx->md)) { |
315 | 0 | RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER, |
316 | 0 | RSA_R_INVALID_DIGEST_LENGTH); |
317 | 0 | return 0; |
318 | 0 | } |
319 | 0 | if (rout) |
320 | 0 | memcpy(rout, rctx->tbuf, ret); |
321 | 0 | } else if (rctx->pad_mode == RSA_PKCS1_PADDING) { |
322 | 0 | size_t sltmp; |
323 | 0 | ret = int_rsa_verify(EVP_MD_type(rctx->md), |
324 | 0 | NULL, 0, rout, &sltmp, |
325 | 0 | sig, siglen, ctx->pkey->pkey.rsa); |
326 | 0 | if (ret <= 0) |
327 | 0 | return 0; |
328 | 0 | ret = sltmp; |
329 | 0 | } else |
330 | 0 | return -1; |
331 | 0 | } else |
332 | 0 | ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa, |
333 | 0 | rctx->pad_mode); |
334 | 0 | if (ret < 0) |
335 | 0 | return ret; |
336 | 0 | *routlen = ret; |
337 | 0 | return 1; |
338 | 0 | } |
339 | | |
340 | | static int pkey_rsa_verify(EVP_PKEY_CTX *ctx, |
341 | | const unsigned char *sig, size_t siglen, |
342 | | const unsigned char *tbs, size_t tbslen) |
343 | 0 | { |
344 | 0 | RSA_PKEY_CTX *rctx = ctx->data; |
345 | 0 | RSA *rsa = ctx->pkey->pkey.rsa; |
346 | 0 | const EVP_MD *md = rctx->md; |
347 | 0 | const EVP_MD *mgf1md = rctx->mgf1md; |
348 | 0 | size_t rslen; |
349 | |
|
350 | | #ifdef OPENSSL_FIPS |
351 | | int rv = pkey_fips_check_rsa(rsa, &md, &mgf1md); |
352 | | |
353 | | if (rv < 0) { |
354 | | RSAerr(RSA_F_PKEY_RSA_VERIFY, |
355 | | RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE); |
356 | | return -1; |
357 | | } |
358 | | #endif |
359 | 0 | if (md != NULL) { |
360 | | #ifdef OPENSSL_FIPS |
361 | | if (rv > 0) { |
362 | | return FIPS_rsa_verify_digest(rsa, tbs, tbslen, md, rctx->pad_mode, |
363 | | rctx->saltlen, mgf1md, sig, siglen); |
364 | | |
365 | | } |
366 | | #endif |
367 | 0 | if (rctx->pad_mode == RSA_PKCS1_PADDING) |
368 | 0 | return RSA_verify(EVP_MD_type(md), tbs, tbslen, |
369 | 0 | sig, siglen, rsa); |
370 | 0 | if (tbslen != (size_t)EVP_MD_size(md)) { |
371 | 0 | RSAerr(RSA_F_PKEY_RSA_VERIFY, RSA_R_INVALID_DIGEST_LENGTH); |
372 | 0 | return -1; |
373 | 0 | } |
374 | 0 | if (rctx->pad_mode == RSA_X931_PADDING) { |
375 | 0 | if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0) |
376 | 0 | return 0; |
377 | 0 | } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) { |
378 | 0 | int ret; |
379 | 0 | if (!setup_tbuf(rctx, ctx)) |
380 | 0 | return -1; |
381 | 0 | ret = RSA_public_decrypt(siglen, sig, rctx->tbuf, |
382 | 0 | rsa, RSA_NO_PADDING); |
383 | 0 | if (ret <= 0) |
384 | 0 | return 0; |
385 | 0 | ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs, md, mgf1md, |
386 | 0 | rctx->tbuf, rctx->saltlen); |
387 | 0 | if (ret <= 0) |
388 | 0 | return 0; |
389 | 0 | return 1; |
390 | 0 | } else |
391 | 0 | return -1; |
392 | 0 | } else { |
393 | 0 | if (!setup_tbuf(rctx, ctx)) |
394 | 0 | return -1; |
395 | 0 | rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf, |
396 | 0 | rsa, rctx->pad_mode); |
397 | 0 | if (rslen == 0) |
398 | 0 | return 0; |
399 | 0 | } |
400 | | |
401 | 0 | if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen)) |
402 | 0 | return 0; |
403 | | |
404 | 0 | return 1; |
405 | |
|
406 | 0 | } |
407 | | |
408 | | static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx, |
409 | | unsigned char *out, size_t *outlen, |
410 | | const unsigned char *in, size_t inlen) |
411 | 0 | { |
412 | 0 | int ret; |
413 | 0 | RSA_PKEY_CTX *rctx = ctx->data; |
414 | 0 | if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) { |
415 | 0 | int klen = RSA_size(ctx->pkey->pkey.rsa); |
416 | 0 | if (!setup_tbuf(rctx, ctx)) |
417 | 0 | return -1; |
418 | 0 | if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen, |
419 | 0 | in, inlen, |
420 | 0 | rctx->oaep_label, |
421 | 0 | rctx->oaep_labellen, |
422 | 0 | rctx->md, rctx->mgf1md)) |
423 | 0 | return -1; |
424 | 0 | ret = RSA_public_encrypt(klen, rctx->tbuf, out, |
425 | 0 | ctx->pkey->pkey.rsa, RSA_NO_PADDING); |
426 | 0 | } else |
427 | 0 | ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa, |
428 | 0 | rctx->pad_mode); |
429 | 0 | if (ret < 0) |
430 | 0 | return ret; |
431 | 0 | *outlen = ret; |
432 | 0 | return 1; |
433 | 0 | } |
434 | | |
435 | | static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx, |
436 | | unsigned char *out, size_t *outlen, |
437 | | const unsigned char *in, size_t inlen) |
438 | 0 | { |
439 | 0 | int ret; |
440 | 0 | RSA_PKEY_CTX *rctx = ctx->data; |
441 | 0 | if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) { |
442 | 0 | if (!setup_tbuf(rctx, ctx)) |
443 | 0 | return -1; |
444 | 0 | ret = RSA_private_decrypt(inlen, in, rctx->tbuf, |
445 | 0 | ctx->pkey->pkey.rsa, RSA_NO_PADDING); |
446 | 0 | if (ret <= 0) |
447 | 0 | return ret; |
448 | 0 | ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf, |
449 | 0 | ret, ret, |
450 | 0 | rctx->oaep_label, |
451 | 0 | rctx->oaep_labellen, |
452 | 0 | rctx->md, rctx->mgf1md); |
453 | 0 | } else |
454 | 0 | ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa, |
455 | 0 | rctx->pad_mode); |
456 | 0 | if (ret < 0) |
457 | 0 | return ret; |
458 | 0 | *outlen = ret; |
459 | 0 | return 1; |
460 | 0 | } |
461 | | |
462 | | static int check_padding_md(const EVP_MD *md, int padding) |
463 | 0 | { |
464 | 0 | if (!md) |
465 | 0 | return 1; |
466 | | |
467 | 0 | if (padding == RSA_NO_PADDING) { |
468 | 0 | RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE); |
469 | 0 | return 0; |
470 | 0 | } |
471 | | |
472 | 0 | if (padding == RSA_X931_PADDING) { |
473 | 0 | if (RSA_X931_hash_id(EVP_MD_type(md)) == -1) { |
474 | 0 | RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST); |
475 | 0 | return 0; |
476 | 0 | } |
477 | 0 | return 1; |
478 | 0 | } |
479 | | |
480 | 0 | return 1; |
481 | 0 | } |
482 | | |
483 | | static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) |
484 | 0 | { |
485 | 0 | RSA_PKEY_CTX *rctx = ctx->data; |
486 | 0 | switch (type) { |
487 | 0 | case EVP_PKEY_CTRL_RSA_PADDING: |
488 | 0 | if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) { |
489 | 0 | if (!check_padding_md(rctx->md, p1)) |
490 | 0 | return 0; |
491 | 0 | if (p1 == RSA_PKCS1_PSS_PADDING) { |
492 | 0 | if (!(ctx->operation & |
493 | 0 | (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY))) |
494 | 0 | goto bad_pad; |
495 | 0 | if (!rctx->md) |
496 | 0 | rctx->md = EVP_sha1(); |
497 | 0 | } |
498 | 0 | if (p1 == RSA_PKCS1_OAEP_PADDING) { |
499 | 0 | if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT)) |
500 | 0 | goto bad_pad; |
501 | 0 | if (!rctx->md) |
502 | 0 | rctx->md = EVP_sha1(); |
503 | 0 | } |
504 | 0 | rctx->pad_mode = p1; |
505 | 0 | return 1; |
506 | 0 | } |
507 | 0 | bad_pad: |
508 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, |
509 | 0 | RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE); |
510 | 0 | return -2; |
511 | | |
512 | 0 | case EVP_PKEY_CTRL_GET_RSA_PADDING: |
513 | 0 | *(int *)p2 = rctx->pad_mode; |
514 | 0 | return 1; |
515 | | |
516 | 0 | case EVP_PKEY_CTRL_RSA_PSS_SALTLEN: |
517 | 0 | case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN: |
518 | 0 | if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) { |
519 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN); |
520 | 0 | return -2; |
521 | 0 | } |
522 | 0 | if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) |
523 | 0 | *(int *)p2 = rctx->saltlen; |
524 | 0 | else { |
525 | 0 | if (p1 < -2) |
526 | 0 | return -2; |
527 | 0 | rctx->saltlen = p1; |
528 | 0 | } |
529 | 0 | return 1; |
530 | | |
531 | 0 | case EVP_PKEY_CTRL_RSA_KEYGEN_BITS: |
532 | 0 | if (p1 < 256) { |
533 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_KEYBITS); |
534 | 0 | return -2; |
535 | 0 | } |
536 | 0 | rctx->nbits = p1; |
537 | 0 | return 1; |
538 | | |
539 | 0 | case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP: |
540 | 0 | if (p2 == NULL || !BN_is_odd((BIGNUM *)p2) || BN_is_one((BIGNUM *)p2)) { |
541 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_BAD_E_VALUE); |
542 | 0 | return -2; |
543 | 0 | } |
544 | 0 | BN_free(rctx->pub_exp); |
545 | 0 | rctx->pub_exp = p2; |
546 | 0 | return 1; |
547 | | |
548 | 0 | case EVP_PKEY_CTRL_RSA_OAEP_MD: |
549 | 0 | case EVP_PKEY_CTRL_GET_RSA_OAEP_MD: |
550 | 0 | if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) { |
551 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE); |
552 | 0 | return -2; |
553 | 0 | } |
554 | 0 | if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD) |
555 | 0 | *(const EVP_MD **)p2 = rctx->md; |
556 | 0 | else |
557 | 0 | rctx->md = p2; |
558 | 0 | return 1; |
559 | | |
560 | 0 | case EVP_PKEY_CTRL_MD: |
561 | 0 | if (!check_padding_md(p2, rctx->pad_mode)) |
562 | 0 | return 0; |
563 | 0 | rctx->md = p2; |
564 | 0 | return 1; |
565 | | |
566 | 0 | case EVP_PKEY_CTRL_GET_MD: |
567 | 0 | *(const EVP_MD **)p2 = rctx->md; |
568 | 0 | return 1; |
569 | | |
570 | 0 | case EVP_PKEY_CTRL_RSA_MGF1_MD: |
571 | 0 | case EVP_PKEY_CTRL_GET_RSA_MGF1_MD: |
572 | 0 | if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING |
573 | 0 | && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) { |
574 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD); |
575 | 0 | return -2; |
576 | 0 | } |
577 | 0 | if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) { |
578 | 0 | if (rctx->mgf1md) |
579 | 0 | *(const EVP_MD **)p2 = rctx->mgf1md; |
580 | 0 | else |
581 | 0 | *(const EVP_MD **)p2 = rctx->md; |
582 | 0 | } else |
583 | 0 | rctx->mgf1md = p2; |
584 | 0 | return 1; |
585 | | |
586 | 0 | case EVP_PKEY_CTRL_RSA_OAEP_LABEL: |
587 | 0 | if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) { |
588 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE); |
589 | 0 | return -2; |
590 | 0 | } |
591 | 0 | if (rctx->oaep_label) |
592 | 0 | OPENSSL_free(rctx->oaep_label); |
593 | 0 | if (p2 && p1 > 0) { |
594 | 0 | rctx->oaep_label = p2; |
595 | 0 | rctx->oaep_labellen = p1; |
596 | 0 | } else { |
597 | 0 | rctx->oaep_label = NULL; |
598 | 0 | rctx->oaep_labellen = 0; |
599 | 0 | } |
600 | 0 | return 1; |
601 | | |
602 | 0 | case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL: |
603 | 0 | if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) { |
604 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE); |
605 | 0 | return -2; |
606 | 0 | } |
607 | 0 | *(unsigned char **)p2 = rctx->oaep_label; |
608 | 0 | return rctx->oaep_labellen; |
609 | | |
610 | 0 | case EVP_PKEY_CTRL_DIGESTINIT: |
611 | 0 | case EVP_PKEY_CTRL_PKCS7_ENCRYPT: |
612 | 0 | case EVP_PKEY_CTRL_PKCS7_DECRYPT: |
613 | 0 | case EVP_PKEY_CTRL_PKCS7_SIGN: |
614 | 0 | return 1; |
615 | 0 | #ifndef OPENSSL_NO_CMS |
616 | 0 | case EVP_PKEY_CTRL_CMS_DECRYPT: |
617 | 0 | case EVP_PKEY_CTRL_CMS_ENCRYPT: |
618 | 0 | case EVP_PKEY_CTRL_CMS_SIGN: |
619 | 0 | return 1; |
620 | 0 | #endif |
621 | 0 | case EVP_PKEY_CTRL_PEER_KEY: |
622 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, |
623 | 0 | RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
624 | 0 | return -2; |
625 | | |
626 | 0 | default: |
627 | 0 | return -2; |
628 | |
|
629 | 0 | } |
630 | 0 | } |
631 | | |
632 | | static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx, |
633 | | const char *type, const char *value) |
634 | 0 | { |
635 | 0 | if (!value) { |
636 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING); |
637 | 0 | return 0; |
638 | 0 | } |
639 | 0 | if (!strcmp(type, "rsa_padding_mode")) { |
640 | 0 | int pm; |
641 | 0 | if (!strcmp(value, "pkcs1")) |
642 | 0 | pm = RSA_PKCS1_PADDING; |
643 | 0 | else if (!strcmp(value, "sslv23")) |
644 | 0 | pm = RSA_SSLV23_PADDING; |
645 | 0 | else if (!strcmp(value, "none")) |
646 | 0 | pm = RSA_NO_PADDING; |
647 | 0 | else if (!strcmp(value, "oeap")) |
648 | 0 | pm = RSA_PKCS1_OAEP_PADDING; |
649 | 0 | else if (!strcmp(value, "oaep")) |
650 | 0 | pm = RSA_PKCS1_OAEP_PADDING; |
651 | 0 | else if (!strcmp(value, "x931")) |
652 | 0 | pm = RSA_X931_PADDING; |
653 | 0 | else if (!strcmp(value, "pss")) |
654 | 0 | pm = RSA_PKCS1_PSS_PADDING; |
655 | 0 | else { |
656 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE); |
657 | 0 | return -2; |
658 | 0 | } |
659 | 0 | return EVP_PKEY_CTX_set_rsa_padding(ctx, pm); |
660 | 0 | } |
661 | | |
662 | 0 | if (!strcmp(type, "rsa_pss_saltlen")) { |
663 | 0 | int saltlen; |
664 | 0 | saltlen = atoi(value); |
665 | 0 | return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen); |
666 | 0 | } |
667 | | |
668 | 0 | if (!strcmp(type, "rsa_keygen_bits")) { |
669 | 0 | int nbits; |
670 | 0 | nbits = atoi(value); |
671 | 0 | return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits); |
672 | 0 | } |
673 | | |
674 | 0 | if (!strcmp(type, "rsa_keygen_pubexp")) { |
675 | 0 | int ret; |
676 | 0 | BIGNUM *pubexp = NULL; |
677 | 0 | if (!BN_asc2bn(&pubexp, value)) |
678 | 0 | return 0; |
679 | 0 | ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp); |
680 | 0 | if (ret <= 0) |
681 | 0 | BN_free(pubexp); |
682 | 0 | return ret; |
683 | 0 | } |
684 | | |
685 | 0 | if (!strcmp(type, "rsa_mgf1_md")) { |
686 | 0 | const EVP_MD *md; |
687 | 0 | if (!(md = EVP_get_digestbyname(value))) { |
688 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST); |
689 | 0 | return 0; |
690 | 0 | } |
691 | 0 | return EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, md); |
692 | 0 | } |
693 | | |
694 | 0 | if (!strcmp(type, "rsa_oaep_md")) { |
695 | 0 | const EVP_MD *md; |
696 | 0 | if (!(md = EVP_get_digestbyname(value))) { |
697 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST); |
698 | 0 | return 0; |
699 | 0 | } |
700 | 0 | return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md); |
701 | 0 | } |
702 | 0 | if (!strcmp(type, "rsa_oaep_label")) { |
703 | 0 | unsigned char *lab; |
704 | 0 | long lablen; |
705 | 0 | int ret; |
706 | 0 | lab = string_to_hex(value, &lablen); |
707 | 0 | if (!lab) |
708 | 0 | return 0; |
709 | 0 | ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen); |
710 | 0 | if (ret <= 0) |
711 | 0 | OPENSSL_free(lab); |
712 | 0 | return ret; |
713 | 0 | } |
714 | | |
715 | 0 | return -2; |
716 | 0 | } |
717 | | |
718 | | static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) |
719 | 0 | { |
720 | 0 | RSA *rsa = NULL; |
721 | 0 | RSA_PKEY_CTX *rctx = ctx->data; |
722 | 0 | BN_GENCB *pcb, cb; |
723 | 0 | int ret; |
724 | 0 | if (!rctx->pub_exp) { |
725 | 0 | rctx->pub_exp = BN_new(); |
726 | 0 | if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4)) |
727 | 0 | return 0; |
728 | 0 | } |
729 | 0 | rsa = RSA_new(); |
730 | 0 | if (!rsa) |
731 | 0 | return 0; |
732 | 0 | if (ctx->pkey_gencb) { |
733 | 0 | pcb = &cb; |
734 | 0 | evp_pkey_set_cb_translate(pcb, ctx); |
735 | 0 | } else |
736 | 0 | pcb = NULL; |
737 | 0 | ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb); |
738 | 0 | if (ret > 0) |
739 | 0 | EVP_PKEY_assign_RSA(pkey, rsa); |
740 | 0 | else |
741 | 0 | RSA_free(rsa); |
742 | 0 | return ret; |
743 | 0 | } |
744 | | |
745 | | const EVP_PKEY_METHOD rsa_pkey_meth = { |
746 | | EVP_PKEY_RSA, |
747 | | EVP_PKEY_FLAG_AUTOARGLEN, |
748 | | pkey_rsa_init, |
749 | | pkey_rsa_copy, |
750 | | pkey_rsa_cleanup, |
751 | | |
752 | | 0, 0, |
753 | | |
754 | | 0, |
755 | | pkey_rsa_keygen, |
756 | | |
757 | | 0, |
758 | | pkey_rsa_sign, |
759 | | |
760 | | 0, |
761 | | pkey_rsa_verify, |
762 | | |
763 | | 0, |
764 | | pkey_rsa_verifyrecover, |
765 | | |
766 | | 0, 0, 0, 0, |
767 | | |
768 | | 0, |
769 | | pkey_rsa_encrypt, |
770 | | |
771 | | 0, |
772 | | pkey_rsa_decrypt, |
773 | | |
774 | | 0, 0, |
775 | | |
776 | | pkey_rsa_ctrl, |
777 | | pkey_rsa_ctrl_str |
778 | | }; |