]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/rsa/rsa_pmeth.c
fc2799af7c185035d52b8c743eb93e858ffb7c90
[thirdparty/openssl.git] / crypto / rsa / rsa_pmeth.c
1 /*
2 * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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 /*
11 * RSA low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14 #include "internal/deprecated.h"
15
16 #include "internal/constant_time.h"
17
18 #include <stdio.h>
19 #include "internal/cryptlib.h"
20 #include <openssl/asn1t.h>
21 #include <openssl/x509.h>
22 #include <openssl/rsa.h>
23 #include <openssl/bn.h>
24 #include <openssl/evp.h>
25 #include <openssl/x509v3.h>
26 #include <openssl/cms.h>
27 #include "crypto/evp.h"
28 #include "crypto/rsa.h"
29 #include "rsa_local.h"
30
31 /* RSA pkey context structure */
32
33 typedef struct {
34 /* Key gen parameters */
35 int nbits;
36 BIGNUM *pub_exp;
37 int primes;
38 /* Keygen callback info */
39 int gentmp[2];
40 /* RSA padding mode */
41 int pad_mode;
42 /* message digest */
43 const EVP_MD *md;
44 /* message digest for MGF1 */
45 const EVP_MD *mgf1md;
46 /* PSS salt length */
47 int saltlen;
48 /* Minimum salt length or -1 if no PSS parameter restriction */
49 int min_saltlen;
50 /* Temp buffer */
51 unsigned char *tbuf;
52 /* OAEP label */
53 unsigned char *oaep_label;
54 size_t oaep_labellen;
55 } RSA_PKEY_CTX;
56
57 /* True if PSS parameters are restricted */
58 #define rsa_pss_restricted(rctx) (rctx->min_saltlen != -1)
59
60 static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
61 {
62 RSA_PKEY_CTX *rctx = OPENSSL_zalloc(sizeof(*rctx));
63
64 if (rctx == NULL)
65 return 0;
66 rctx->nbits = 2048;
67 rctx->primes = RSA_DEFAULT_PRIME_NUM;
68 if (pkey_ctx_is_pss(ctx))
69 rctx->pad_mode = RSA_PKCS1_PSS_PADDING;
70 else
71 rctx->pad_mode = RSA_PKCS1_PADDING;
72 /* Maximum for sign, auto for verify */
73 rctx->saltlen = RSA_PSS_SALTLEN_AUTO;
74 rctx->min_saltlen = -1;
75 ctx->data = rctx;
76 ctx->keygen_info = rctx->gentmp;
77 ctx->keygen_info_count = 2;
78
79 return 1;
80 }
81
82 static int pkey_rsa_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
83 {
84 RSA_PKEY_CTX *dctx, *sctx;
85
86 if (!pkey_rsa_init(dst))
87 return 0;
88 sctx = src->data;
89 dctx = dst->data;
90 dctx->nbits = sctx->nbits;
91 if (sctx->pub_exp) {
92 dctx->pub_exp = BN_dup(sctx->pub_exp);
93 if (!dctx->pub_exp)
94 return 0;
95 }
96 dctx->pad_mode = sctx->pad_mode;
97 dctx->md = sctx->md;
98 dctx->mgf1md = sctx->mgf1md;
99 dctx->saltlen = sctx->saltlen;
100 if (sctx->oaep_label) {
101 OPENSSL_free(dctx->oaep_label);
102 dctx->oaep_label = OPENSSL_memdup(sctx->oaep_label, sctx->oaep_labellen);
103 if (!dctx->oaep_label)
104 return 0;
105 dctx->oaep_labellen = sctx->oaep_labellen;
106 }
107 return 1;
108 }
109
110 static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
111 {
112 if (ctx->tbuf != NULL)
113 return 1;
114 if ((ctx->tbuf = OPENSSL_malloc(RSA_size(pk->pkey->pkey.rsa))) == NULL) {
115 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
116 return 0;
117 }
118 return 1;
119 }
120
121 static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
122 {
123 RSA_PKEY_CTX *rctx = ctx->data;
124 if (rctx) {
125 BN_free(rctx->pub_exp);
126 OPENSSL_free(rctx->tbuf);
127 OPENSSL_free(rctx->oaep_label);
128 OPENSSL_free(rctx);
129 }
130 }
131
132 static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
133 size_t *siglen, const unsigned char *tbs,
134 size_t tbslen)
135 {
136 int ret;
137 RSA_PKEY_CTX *rctx = ctx->data;
138 RSA *rsa = ctx->pkey->pkey.rsa;
139
140 if (rctx->md) {
141 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
142 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
143 return -1;
144 }
145
146 if (EVP_MD_type(rctx->md) == NID_mdc2) {
147 unsigned int sltmp;
148 if (rctx->pad_mode != RSA_PKCS1_PADDING)
149 return -1;
150 ret = RSA_sign_ASN1_OCTET_STRING(0,
151 tbs, tbslen, sig, &sltmp, rsa);
152
153 if (ret <= 0)
154 return ret;
155 ret = sltmp;
156 } else if (rctx->pad_mode == RSA_X931_PADDING) {
157 if ((size_t)RSA_size(rsa) < tbslen + 1) {
158 ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
159 return -1;
160 }
161 if (!setup_tbuf(rctx, ctx)) {
162 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
163 return -1;
164 }
165 memcpy(rctx->tbuf, tbs, tbslen);
166 rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(rctx->md));
167 ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
168 sig, rsa, RSA_X931_PADDING);
169 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
170 unsigned int sltmp;
171 ret = RSA_sign(EVP_MD_type(rctx->md),
172 tbs, tbslen, sig, &sltmp, rsa);
173 if (ret <= 0)
174 return ret;
175 ret = sltmp;
176 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
177 if (!setup_tbuf(rctx, ctx))
178 return -1;
179 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
180 rctx->tbuf, tbs,
181 rctx->md, rctx->mgf1md,
182 rctx->saltlen))
183 return -1;
184 ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
185 sig, rsa, RSA_NO_PADDING);
186 } else {
187 return -1;
188 }
189 } else {
190 ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
191 rctx->pad_mode);
192 }
193 if (ret < 0)
194 return ret;
195 *siglen = ret;
196 return 1;
197 }
198
199 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
200 unsigned char *rout, size_t *routlen,
201 const unsigned char *sig, size_t siglen)
202 {
203 int ret;
204 RSA_PKEY_CTX *rctx = ctx->data;
205
206 if (rctx->md) {
207 if (rctx->pad_mode == RSA_X931_PADDING) {
208 if (!setup_tbuf(rctx, ctx))
209 return -1;
210 ret = RSA_public_decrypt(siglen, sig,
211 rctx->tbuf, ctx->pkey->pkey.rsa,
212 RSA_X931_PADDING);
213 if (ret < 1)
214 return 0;
215 ret--;
216 if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) {
217 ERR_raise(ERR_LIB_RSA, RSA_R_ALGORITHM_MISMATCH);
218 return 0;
219 }
220 if (ret != EVP_MD_size(rctx->md)) {
221 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
222 return 0;
223 }
224 if (rout)
225 memcpy(rout, rctx->tbuf, ret);
226 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
227 size_t sltmp;
228 ret = int_rsa_verify(EVP_MD_type(rctx->md),
229 NULL, 0, rout, &sltmp,
230 sig, siglen, ctx->pkey->pkey.rsa);
231 if (ret <= 0)
232 return 0;
233 ret = sltmp;
234 } else {
235 return -1;
236 }
237 } else {
238 ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
239 rctx->pad_mode);
240 }
241 if (ret < 0)
242 return ret;
243 *routlen = ret;
244 return 1;
245 }
246
247 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
248 const unsigned char *sig, size_t siglen,
249 const unsigned char *tbs, size_t tbslen)
250 {
251 RSA_PKEY_CTX *rctx = ctx->data;
252 RSA *rsa = ctx->pkey->pkey.rsa;
253 size_t rslen;
254
255 if (rctx->md) {
256 if (rctx->pad_mode == RSA_PKCS1_PADDING)
257 return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
258 sig, siglen, rsa);
259 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
260 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
261 return -1;
262 }
263 if (rctx->pad_mode == RSA_X931_PADDING) {
264 if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
265 return 0;
266 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
267 int ret;
268 if (!setup_tbuf(rctx, ctx))
269 return -1;
270 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
271 rsa, RSA_NO_PADDING);
272 if (ret <= 0)
273 return 0;
274 ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
275 rctx->md, rctx->mgf1md,
276 rctx->tbuf, rctx->saltlen);
277 if (ret <= 0)
278 return 0;
279 return 1;
280 } else {
281 return -1;
282 }
283 } else {
284 if (!setup_tbuf(rctx, ctx))
285 return -1;
286 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
287 rsa, rctx->pad_mode);
288 if (rslen == 0)
289 return 0;
290 }
291
292 if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
293 return 0;
294
295 return 1;
296
297 }
298
299 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
300 unsigned char *out, size_t *outlen,
301 const unsigned char *in, size_t inlen)
302 {
303 int ret;
304 RSA_PKEY_CTX *rctx = ctx->data;
305
306 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
307 int klen = RSA_size(ctx->pkey->pkey.rsa);
308 if (!setup_tbuf(rctx, ctx))
309 return -1;
310 if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
311 in, inlen,
312 rctx->oaep_label,
313 rctx->oaep_labellen,
314 rctx->md, rctx->mgf1md))
315 return -1;
316 ret = RSA_public_encrypt(klen, rctx->tbuf, out,
317 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
318 } else {
319 ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
320 rctx->pad_mode);
321 }
322 if (ret < 0)
323 return ret;
324 *outlen = ret;
325 return 1;
326 }
327
328 static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
329 unsigned char *out, size_t *outlen,
330 const unsigned char *in, size_t inlen)
331 {
332 int ret;
333 RSA_PKEY_CTX *rctx = ctx->data;
334
335 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
336 if (!setup_tbuf(rctx, ctx))
337 return -1;
338 ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
339 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
340 if (ret <= 0)
341 return ret;
342 ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf,
343 ret, ret,
344 rctx->oaep_label,
345 rctx->oaep_labellen,
346 rctx->md, rctx->mgf1md);
347 } else {
348 ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
349 rctx->pad_mode);
350 }
351 *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret);
352 ret = constant_time_select_int(constant_time_msb(ret), ret, 1);
353 return ret;
354 }
355
356 static int check_padding_md(const EVP_MD *md, int padding)
357 {
358 int mdnid;
359
360 if (!md)
361 return 1;
362
363 mdnid = EVP_MD_type(md);
364
365 if (padding == RSA_NO_PADDING) {
366 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
367 return 0;
368 }
369
370 if (padding == RSA_X931_PADDING) {
371 if (RSA_X931_hash_id(mdnid) == -1) {
372 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_X931_DIGEST);
373 return 0;
374 }
375 } else {
376 switch(mdnid) {
377 /* List of all supported RSA digests */
378 case NID_sha1:
379 case NID_sha224:
380 case NID_sha256:
381 case NID_sha384:
382 case NID_sha512:
383 case NID_sha512_224:
384 case NID_sha512_256:
385 case NID_md5:
386 case NID_md5_sha1:
387 case NID_md2:
388 case NID_md4:
389 case NID_mdc2:
390 case NID_ripemd160:
391 case NID_sha3_224:
392 case NID_sha3_256:
393 case NID_sha3_384:
394 case NID_sha3_512:
395 return 1;
396
397 default:
398 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST);
399 return 0;
400
401 }
402 }
403
404 return 1;
405 }
406
407 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
408 {
409 RSA_PKEY_CTX *rctx = ctx->data;
410
411 switch (type) {
412 case EVP_PKEY_CTRL_RSA_PADDING:
413 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
414 if (!check_padding_md(rctx->md, p1))
415 return 0;
416 if (p1 == RSA_PKCS1_PSS_PADDING) {
417 if (!(ctx->operation &
418 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
419 goto bad_pad;
420 if (!rctx->md)
421 rctx->md = EVP_sha1();
422 } else if (pkey_ctx_is_pss(ctx)) {
423 goto bad_pad;
424 }
425 if (p1 == RSA_PKCS1_OAEP_PADDING) {
426 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
427 goto bad_pad;
428 if (!rctx->md)
429 rctx->md = EVP_sha1();
430 }
431 rctx->pad_mode = p1;
432 return 1;
433 }
434 bad_pad:
435 ERR_raise(ERR_LIB_RSA, RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
436 return -2;
437
438 case EVP_PKEY_CTRL_GET_RSA_PADDING:
439 *(int *)p2 = rctx->pad_mode;
440 return 1;
441
442 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
443 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
444 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
445 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_SALTLEN);
446 return -2;
447 }
448 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) {
449 *(int *)p2 = rctx->saltlen;
450 } else {
451 if (p1 < RSA_PSS_SALTLEN_MAX)
452 return -2;
453 if (rsa_pss_restricted(rctx)) {
454 if (p1 == RSA_PSS_SALTLEN_AUTO
455 && ctx->operation == EVP_PKEY_OP_VERIFY) {
456 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_SALTLEN);
457 return -2;
458 }
459 if ((p1 == RSA_PSS_SALTLEN_DIGEST
460 && rctx->min_saltlen > EVP_MD_size(rctx->md))
461 || (p1 >= 0 && p1 < rctx->min_saltlen)) {
462 ERR_raise(ERR_LIB_RSA, RSA_R_PSS_SALTLEN_TOO_SMALL);
463 return 0;
464 }
465 }
466 rctx->saltlen = p1;
467 }
468 return 1;
469
470 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
471 if (p1 < RSA_MIN_MODULUS_BITS) {
472 ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
473 return -2;
474 }
475 rctx->nbits = p1;
476 return 1;
477
478 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
479 if (p2 == NULL || !BN_is_odd((BIGNUM *)p2) || BN_is_one((BIGNUM *)p2)) {
480 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
481 return -2;
482 }
483 BN_free(rctx->pub_exp);
484 rctx->pub_exp = p2;
485 return 1;
486
487 case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES:
488 if (p1 < RSA_DEFAULT_PRIME_NUM || p1 > RSA_MAX_PRIME_NUM) {
489 ERR_raise(ERR_LIB_RSA, RSA_R_KEY_PRIME_NUM_INVALID);
490 return -2;
491 }
492 rctx->primes = p1;
493 return 1;
494
495 case EVP_PKEY_CTRL_RSA_OAEP_MD:
496 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
497 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
498 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
499 return -2;
500 }
501 if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
502 *(const EVP_MD **)p2 = rctx->md;
503 else
504 rctx->md = p2;
505 return 1;
506
507 case EVP_PKEY_CTRL_MD:
508 if (!check_padding_md(p2, rctx->pad_mode))
509 return 0;
510 if (rsa_pss_restricted(rctx)) {
511 if (EVP_MD_type(rctx->md) == EVP_MD_type(p2))
512 return 1;
513 ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_NOT_ALLOWED);
514 return 0;
515 }
516 rctx->md = p2;
517 return 1;
518
519 case EVP_PKEY_CTRL_GET_MD:
520 *(const EVP_MD **)p2 = rctx->md;
521 return 1;
522
523 case EVP_PKEY_CTRL_RSA_MGF1_MD:
524 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
525 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
526 && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
527 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MGF1_MD);
528 return -2;
529 }
530 if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
531 if (rctx->mgf1md)
532 *(const EVP_MD **)p2 = rctx->mgf1md;
533 else
534 *(const EVP_MD **)p2 = rctx->md;
535 } else {
536 if (rsa_pss_restricted(rctx)) {
537 if (EVP_MD_type(rctx->mgf1md) == EVP_MD_type(p2))
538 return 1;
539 ERR_raise(ERR_LIB_RSA, RSA_R_MGF1_DIGEST_NOT_ALLOWED);
540 return 0;
541 }
542 rctx->mgf1md = p2;
543 }
544 return 1;
545
546 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
547 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
548 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
549 return -2;
550 }
551 OPENSSL_free(rctx->oaep_label);
552 if (p2 && p1 > 0) {
553 rctx->oaep_label = p2;
554 rctx->oaep_labellen = p1;
555 } else {
556 rctx->oaep_label = NULL;
557 rctx->oaep_labellen = 0;
558 }
559 return 1;
560
561 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
562 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
563 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
564 return -2;
565 }
566 *(unsigned char **)p2 = rctx->oaep_label;
567 return rctx->oaep_labellen;
568
569 case EVP_PKEY_CTRL_DIGESTINIT:
570 case EVP_PKEY_CTRL_PKCS7_SIGN:
571 #ifndef OPENSSL_NO_CMS
572 case EVP_PKEY_CTRL_CMS_SIGN:
573 #endif
574 return 1;
575
576 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
577 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
578 #ifndef OPENSSL_NO_CMS
579 case EVP_PKEY_CTRL_CMS_DECRYPT:
580 case EVP_PKEY_CTRL_CMS_ENCRYPT:
581 #endif
582 if (!pkey_ctx_is_pss(ctx))
583 return 1;
584 /* fall through */
585 case EVP_PKEY_CTRL_PEER_KEY:
586 ERR_raise(ERR_LIB_RSA, RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
587 return -2;
588
589 default:
590 return -2;
591
592 }
593 }
594
595 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
596 const char *type, const char *value)
597 {
598 if (value == NULL) {
599 ERR_raise(ERR_LIB_RSA, RSA_R_VALUE_MISSING);
600 return 0;
601 }
602 if (strcmp(type, "rsa_padding_mode") == 0) {
603 int pm;
604
605 if (strcmp(value, "pkcs1") == 0) {
606 pm = RSA_PKCS1_PADDING;
607 } else if (strcmp(value, "sslv23") == 0) {
608 pm = RSA_SSLV23_PADDING;
609 } else if (strcmp(value, "none") == 0) {
610 pm = RSA_NO_PADDING;
611 } else if (strcmp(value, "oeap") == 0) {
612 pm = RSA_PKCS1_OAEP_PADDING;
613 } else if (strcmp(value, "oaep") == 0) {
614 pm = RSA_PKCS1_OAEP_PADDING;
615 } else if (strcmp(value, "x931") == 0) {
616 pm = RSA_X931_PADDING;
617 } else if (strcmp(value, "pss") == 0) {
618 pm = RSA_PKCS1_PSS_PADDING;
619 } else {
620 ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
621 return -2;
622 }
623 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
624 }
625
626 if (strcmp(type, "rsa_pss_saltlen") == 0) {
627 int saltlen;
628
629 if (!strcmp(value, "digest"))
630 saltlen = RSA_PSS_SALTLEN_DIGEST;
631 else if (!strcmp(value, "max"))
632 saltlen = RSA_PSS_SALTLEN_MAX;
633 else if (!strcmp(value, "auto"))
634 saltlen = RSA_PSS_SALTLEN_AUTO;
635 else
636 saltlen = atoi(value);
637 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
638 }
639
640 if (strcmp(type, "rsa_keygen_bits") == 0) {
641 int nbits = atoi(value);
642
643 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
644 }
645
646 if (strcmp(type, "rsa_keygen_pubexp") == 0) {
647 int ret;
648
649 BIGNUM *pubexp = NULL;
650 if (!BN_asc2bn(&pubexp, value))
651 return 0;
652 ret = EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, pubexp);
653 BN_free(pubexp);
654 return ret;
655 }
656
657 if (strcmp(type, "rsa_keygen_primes") == 0) {
658 int nprimes = atoi(value);
659
660 return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, nprimes);
661 }
662
663 if (strcmp(type, "rsa_mgf1_md") == 0)
664 return EVP_PKEY_CTX_md(ctx,
665 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
666 EVP_PKEY_CTRL_RSA_MGF1_MD, value);
667
668 if (pkey_ctx_is_pss(ctx)) {
669
670 if (strcmp(type, "rsa_pss_keygen_mgf1_md") == 0)
671 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
672 EVP_PKEY_CTRL_RSA_MGF1_MD, value);
673
674 if (strcmp(type, "rsa_pss_keygen_md") == 0)
675 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
676 EVP_PKEY_CTRL_MD, value);
677
678 if (strcmp(type, "rsa_pss_keygen_saltlen") == 0) {
679 int saltlen = atoi(value);
680
681 return EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx, saltlen);
682 }
683 }
684
685 if (strcmp(type, "rsa_oaep_md") == 0)
686 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_CRYPT,
687 EVP_PKEY_CTRL_RSA_OAEP_MD, value);
688
689 if (strcmp(type, "rsa_oaep_label") == 0) {
690 unsigned char *lab;
691 long lablen;
692 int ret;
693
694 lab = OPENSSL_hexstr2buf(value, &lablen);
695 if (!lab)
696 return 0;
697 ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
698 if (ret <= 0)
699 OPENSSL_free(lab);
700 return ret;
701 }
702
703 return -2;
704 }
705
706 /* Set PSS parameters when generating a key, if necessary */
707 static int rsa_set_pss_param(RSA *rsa, EVP_PKEY_CTX *ctx)
708 {
709 RSA_PKEY_CTX *rctx = ctx->data;
710
711 if (!pkey_ctx_is_pss(ctx))
712 return 1;
713 /* If all parameters are default values don't set pss */
714 if (rctx->md == NULL && rctx->mgf1md == NULL && rctx->saltlen == -2)
715 return 1;
716 rsa->pss = rsa_pss_params_create(rctx->md, rctx->mgf1md,
717 rctx->saltlen == -2 ? 0 : rctx->saltlen);
718 if (rsa->pss == NULL)
719 return 0;
720 return 1;
721 }
722
723 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
724 {
725 RSA *rsa = NULL;
726 RSA_PKEY_CTX *rctx = ctx->data;
727 BN_GENCB *pcb;
728 int ret;
729
730 if (rctx->pub_exp == NULL) {
731 rctx->pub_exp = BN_new();
732 if (rctx->pub_exp == NULL || !BN_set_word(rctx->pub_exp, RSA_F4))
733 return 0;
734 }
735 rsa = RSA_new();
736 if (rsa == NULL)
737 return 0;
738 if (ctx->pkey_gencb) {
739 pcb = BN_GENCB_new();
740 if (pcb == NULL) {
741 RSA_free(rsa);
742 return 0;
743 }
744 evp_pkey_set_cb_translate(pcb, ctx);
745 } else {
746 pcb = NULL;
747 }
748 ret = RSA_generate_multi_prime_key(rsa, rctx->nbits, rctx->primes,
749 rctx->pub_exp, pcb);
750 BN_GENCB_free(pcb);
751 if (ret > 0 && !rsa_set_pss_param(rsa, ctx)) {
752 RSA_free(rsa);
753 return 0;
754 }
755 if (ret > 0)
756 EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, rsa);
757 else
758 RSA_free(rsa);
759 return ret;
760 }
761
762 static const EVP_PKEY_METHOD rsa_pkey_meth = {
763 EVP_PKEY_RSA,
764 EVP_PKEY_FLAG_AUTOARGLEN,
765 pkey_rsa_init,
766 pkey_rsa_copy,
767 pkey_rsa_cleanup,
768
769 0, 0,
770
771 0,
772 pkey_rsa_keygen,
773
774 0,
775 pkey_rsa_sign,
776
777 0,
778 pkey_rsa_verify,
779
780 0,
781 pkey_rsa_verifyrecover,
782
783 0, 0, 0, 0,
784
785 0,
786 pkey_rsa_encrypt,
787
788 0,
789 pkey_rsa_decrypt,
790
791 0, 0,
792
793 pkey_rsa_ctrl,
794 pkey_rsa_ctrl_str
795 };
796
797 const EVP_PKEY_METHOD *ossl_rsa_pkey_method(void)
798 {
799 return &rsa_pkey_meth;
800 }
801
802 /*
803 * Called for PSS sign or verify initialisation: checks PSS parameter
804 * sanity and sets any restrictions on key usage.
805 */
806
807 static int pkey_pss_init(EVP_PKEY_CTX *ctx)
808 {
809 RSA *rsa;
810 RSA_PKEY_CTX *rctx = ctx->data;
811 const EVP_MD *md;
812 const EVP_MD *mgf1md;
813 int min_saltlen, max_saltlen;
814
815 /* Should never happen */
816 if (!pkey_ctx_is_pss(ctx))
817 return 0;
818 rsa = ctx->pkey->pkey.rsa;
819 /* If no restrictions just return */
820 if (rsa->pss == NULL)
821 return 1;
822 /* Get and check parameters */
823 if (!rsa_pss_get_param(rsa->pss, &md, &mgf1md, &min_saltlen))
824 return 0;
825
826 /* See if minimum salt length exceeds maximum possible */
827 max_saltlen = RSA_size(rsa) - EVP_MD_size(md);
828 if ((RSA_bits(rsa) & 0x7) == 1)
829 max_saltlen--;
830 if (min_saltlen > max_saltlen) {
831 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_SALT_LENGTH);
832 return 0;
833 }
834
835 rctx->min_saltlen = min_saltlen;
836
837 /*
838 * Set PSS restrictions as defaults: we can then block any attempt to
839 * use invalid values in pkey_rsa_ctrl
840 */
841
842 rctx->md = md;
843 rctx->mgf1md = mgf1md;
844 rctx->saltlen = min_saltlen;
845
846 return 1;
847 }
848
849 static const EVP_PKEY_METHOD rsa_pss_pkey_meth = {
850 EVP_PKEY_RSA_PSS,
851 EVP_PKEY_FLAG_AUTOARGLEN,
852 pkey_rsa_init,
853 pkey_rsa_copy,
854 pkey_rsa_cleanup,
855
856 0, 0,
857
858 0,
859 pkey_rsa_keygen,
860
861 pkey_pss_init,
862 pkey_rsa_sign,
863
864 pkey_pss_init,
865 pkey_rsa_verify,
866
867 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
868
869 pkey_rsa_ctrl,
870 pkey_rsa_ctrl_str
871 };
872
873 const EVP_PKEY_METHOD *ossl_rsa_pss_pkey_method(void)
874 {
875 return &rsa_pss_pkey_meth;
876 }