]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/rsa/rsa_pmeth.c
Run util/openssl-format-source -v -c .
[thirdparty/openssl.git] / crypto / rsa / rsa_pmeth.c
CommitLineData
09b88a4a 1/* crypto/rsa/rsa_pmeth.c */
ae5c8664
MC
2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4 * 2006.
0b6f3c66
DSH
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
ae5c8664 14 * notice, this list of conditions and the following disclaimer.
0b6f3c66
DSH
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>
1e26a8ba 65#include <openssl/bn.h>
b2a97be7 66#include <openssl/evp.h>
25f93585 67#include <openssl/x509v3.h>
9d972207 68#ifndef OPENSSL_NO_CMS
ae5c8664 69# include <openssl/cms.h>
9d972207 70#endif
53dd05d8 71#ifdef OPENSSL_FIPS
ae5c8664 72# include <openssl/fips.h>
53dd05d8 73#endif
0b6f3c66 74#include "evp_locl.h"
777c47ac 75#include "rsa_locl.h"
b2a97be7 76
07e970c7
DSH
77/* RSA pkey context structure */
78
ae5c8664
MC
79typedef 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;
07e970c7
DSH
99
100static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
ae5c8664
MC
101{
102 RSA_PKEY_CTX *rctx;
103 rctx = OPENSSL_malloc(sizeof(RSA_PKEY_CTX));
104 if (!rctx)
105 return 0;
106 rctx->nbits = 1024;
107 rctx->pub_exp = NULL;
108 rctx->pad_mode = RSA_PKCS1_PADDING;
109 rctx->md = NULL;
110 rctx->mgf1md = NULL;
111 rctx->tbuf = NULL;
112
113 rctx->saltlen = -2;
114
115 rctx->oaep_label = NULL;
116 rctx->oaep_labellen = 0;
117
118 ctx->data = rctx;
119 ctx->keygen_info = rctx->gentmp;
120 ctx->keygen_info_count = 2;
121
122 return 1;
123}
b2a97be7 124
8bdcef40 125static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
ae5c8664
MC
126{
127 RSA_PKEY_CTX *dctx, *sctx;
128 if (!pkey_rsa_init(dst))
129 return 0;
130 sctx = src->data;
131 dctx = dst->data;
132 dctx->nbits = sctx->nbits;
133 if (sctx->pub_exp) {
134 dctx->pub_exp = BN_dup(sctx->pub_exp);
135 if (!dctx->pub_exp)
136 return 0;
137 }
138 dctx->pad_mode = sctx->pad_mode;
139 dctx->md = sctx->md;
140 dctx->mgf1md = sctx->mgf1md;
141 if (sctx->oaep_label) {
142 if (dctx->oaep_label)
143 OPENSSL_free(dctx->oaep_label);
144 dctx->oaep_label = BUF_memdup(sctx->oaep_label, sctx->oaep_labellen);
145 if (!dctx->oaep_label)
146 return 0;
147 dctx->oaep_labellen = sctx->oaep_labellen;
148 }
149 return 1;
150}
8bdcef40 151
b2a97be7 152static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
ae5c8664
MC
153{
154 if (ctx->tbuf)
155 return 1;
156 ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
157 if (!ctx->tbuf)
158 return 0;
159 return 1;
160}
07e970c7
DSH
161
162static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
ae5c8664
MC
163{
164 RSA_PKEY_CTX *rctx = ctx->data;
165 if (rctx) {
166 if (rctx->pub_exp)
167 BN_free(rctx->pub_exp);
168 if (rctx->tbuf)
169 OPENSSL_free(rctx->tbuf);
170 if (rctx->oaep_label)
171 OPENSSL_free(rctx->oaep_label);
172 OPENSSL_free(rctx);
173 }
174}
175
2e51a4ca 176#ifdef OPENSSL_FIPS
ae5c8664
MC
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.
53dd05d8
DSH
181 */
182
183static int pkey_fips_check_ctx(EVP_PKEY_CTX *ctx)
ae5c8664
MC
184{
185 RSA_PKEY_CTX *rctx = ctx->data;
186 RSA *rsa = ctx->pkey->pkey.rsa;
187 int rv = -1;
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 (rctx->md) {
195 const EVP_MD *fmd;
196 fmd = FIPS_get_digestbynid(EVP_MD_type(rctx->md));
197 if (!fmd || !(fmd->flags & EVP_MD_FLAG_FIPS))
198 return rv;
199 }
200 if (rctx->mgf1md && !(rctx->mgf1md->flags & EVP_MD_FLAG_FIPS)) {
201 const EVP_MD *fmd;
202 fmd = FIPS_get_digestbynid(EVP_MD_type(rctx->mgf1md));
203 if (!fmd || !(fmd->flags & EVP_MD_FLAG_FIPS))
204 return rv;
205 }
206 return 1;
207}
2e51a4ca 208#endif
53dd05d8 209
ae5c8664
MC
210static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
211 size_t *siglen, const unsigned char *tbs,
212 size_t tbslen)
213{
214 int ret;
215 RSA_PKEY_CTX *rctx = ctx->data;
216 RSA *rsa = ctx->pkey->pkey.rsa;
b2a97be7 217
53dd05d8 218#ifdef OPENSSL_FIPS
ae5c8664
MC
219 ret = pkey_fips_check_ctx(ctx);
220 if (ret < 0) {
221 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
222 return -1;
223 }
53dd05d8
DSH
224#endif
225
ae5c8664
MC
226 if (rctx->md) {
227 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
228 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_INVALID_DIGEST_LENGTH);
229 return -1;
230 }
53dd05d8 231#ifdef OPENSSL_FIPS
ae5c8664
MC
232 if (ret > 0) {
233 unsigned int slen;
234 ret = FIPS_rsa_sign_digest(rsa, tbs, tbslen, rctx->md,
235 rctx->pad_mode,
236 rctx->saltlen,
237 rctx->mgf1md, sig, &slen);
238 if (ret > 0)
239 *siglen = slen;
240 else
241 *siglen = 0;
242 return ret;
243 }
53dd05d8 244#endif
0cd7a032 245
ae5c8664
MC
246 if (EVP_MD_type(rctx->md) == NID_mdc2) {
247 unsigned int sltmp;
248 if (rctx->pad_mode != RSA_PKCS1_PADDING)
249 return -1;
250 ret = RSA_sign_ASN1_OCTET_STRING(NID_mdc2,
251 tbs, tbslen, sig, &sltmp, rsa);
252
253 if (ret <= 0)
254 return ret;
255 ret = sltmp;
256 } else if (rctx->pad_mode == RSA_X931_PADDING) {
257 if (!setup_tbuf(rctx, ctx))
258 return -1;
259 memcpy(rctx->tbuf, tbs, tbslen);
260 rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(rctx->md));
261 ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
262 sig, rsa, RSA_X931_PADDING);
263 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
264 unsigned int sltmp;
265 ret = RSA_sign(EVP_MD_type(rctx->md),
266 tbs, tbslen, sig, &sltmp, rsa);
267 if (ret <= 0)
268 return ret;
269 ret = sltmp;
270 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
271 if (!setup_tbuf(rctx, ctx))
272 return -1;
273 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
274 rctx->tbuf, tbs,
275 rctx->md, rctx->mgf1md,
276 rctx->saltlen))
277 return -1;
278 ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
279 sig, rsa, RSA_NO_PADDING);
280 } else
281 return -1;
282 } else
283 ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
284 rctx->pad_mode);
285 if (ret < 0)
286 return ret;
287 *siglen = ret;
288 return 1;
289}
07e970c7
DSH
290
291static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
ae5c8664
MC
292 unsigned char *rout, size_t *routlen,
293 const unsigned char *sig, size_t siglen)
294{
295 int ret;
296 RSA_PKEY_CTX *rctx = ctx->data;
297
298 if (rctx->md) {
299 if (rctx->pad_mode == RSA_X931_PADDING) {
300 if (!setup_tbuf(rctx, ctx))
301 return -1;
302 ret = RSA_public_decrypt(siglen, sig,
303 rctx->tbuf, ctx->pkey->pkey.rsa,
304 RSA_X931_PADDING);
305 if (ret < 1)
306 return 0;
307 ret--;
308 if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) {
309 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
310 RSA_R_ALGORITHM_MISMATCH);
311 return 0;
312 }
313 if (ret != EVP_MD_size(rctx->md)) {
314 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
315 RSA_R_INVALID_DIGEST_LENGTH);
316 return 0;
317 }
318 if (rout)
319 memcpy(rout, rctx->tbuf, ret);
320 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
321 size_t sltmp;
322 ret = int_rsa_verify(EVP_MD_type(rctx->md),
323 NULL, 0, rout, &sltmp,
324 sig, siglen, ctx->pkey->pkey.rsa);
325 if (ret <= 0)
326 return 0;
327 ret = sltmp;
328 } else
329 return -1;
330 } else
331 ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
332 rctx->pad_mode);
333 if (ret < 0)
334 return ret;
335 *routlen = ret;
336 return 1;
337}
07e970c7 338
4f59b658 339static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
ae5c8664
MC
340 const unsigned char *sig, size_t siglen,
341 const unsigned char *tbs, size_t tbslen)
342{
343 RSA_PKEY_CTX *rctx = ctx->data;
344 RSA *rsa = ctx->pkey->pkey.rsa;
345 size_t rslen;
53dd05d8 346#ifdef OPENSSL_FIPS
ae5c8664
MC
347 int rv;
348 rv = pkey_fips_check_ctx(ctx);
349 if (rv < 0) {
350 RSAerr(RSA_F_PKEY_RSA_VERIFY,
351 RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
352 return -1;
353 }
53dd05d8 354#endif
ae5c8664 355 if (rctx->md) {
53dd05d8 356#ifdef OPENSSL_FIPS
ae5c8664
MC
357 if (rv > 0) {
358 return FIPS_rsa_verify_digest(rsa,
359 tbs, tbslen,
360 rctx->md,
361 rctx->pad_mode,
362 rctx->saltlen,
363 rctx->mgf1md, sig, siglen);
364
365 }
53dd05d8 366#endif
ae5c8664
MC
367 if (rctx->pad_mode == RSA_PKCS1_PADDING)
368 return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
369 sig, siglen, rsa);
370 if (rctx->pad_mode == RSA_X931_PADDING) {
371 if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
372 return 0;
373 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
374 int ret;
375 if (!setup_tbuf(rctx, ctx))
376 return -1;
377 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
378 rsa, RSA_NO_PADDING);
379 if (ret <= 0)
380 return 0;
381 ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
382 rctx->md, rctx->mgf1md,
383 rctx->tbuf, rctx->saltlen);
384 if (ret <= 0)
385 return 0;
386 return 1;
387 } else
388 return -1;
389 } else {
390 if (!setup_tbuf(rctx, ctx))
391 return -1;
392 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
393 rsa, rctx->pad_mode);
394 if (rslen == 0)
395 return 0;
396 }
397
398 if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
399 return 0;
400
401 return 1;
402
403}
4f59b658 404
eaff5a14 405static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
ae5c8664
MC
406 unsigned char *out, size_t *outlen,
407 const unsigned char *in, size_t inlen)
408{
409 int ret;
410 RSA_PKEY_CTX *rctx = ctx->data;
411 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
412 int klen = RSA_size(ctx->pkey->pkey.rsa);
413 if (!setup_tbuf(rctx, ctx))
414 return -1;
415 if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
416 in, inlen,
417 rctx->oaep_label,
418 rctx->oaep_labellen,
419 rctx->md, rctx->mgf1md))
420 return -1;
421 ret = RSA_public_encrypt(klen, rctx->tbuf, out,
422 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
423 } else
424 ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
425 rctx->pad_mode);
426 if (ret < 0)
427 return ret;
428 *outlen = ret;
429 return 1;
430}
8cd44e36 431
eaff5a14 432static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
ae5c8664
MC
433 unsigned char *out, size_t *outlen,
434 const unsigned char *in, size_t inlen)
435{
436 int ret;
437 RSA_PKEY_CTX *rctx = ctx->data;
438 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
439 int i;
440 if (!setup_tbuf(rctx, ctx))
441 return -1;
442 ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
443 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
444 if (ret <= 0)
445 return ret;
446 for (i = 0; i < ret; i++) {
447 if (rctx->tbuf[i])
448 break;
449 }
450 ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf + i,
451 ret - i, ret,
452 rctx->oaep_label,
453 rctx->oaep_labellen,
454 rctx->md, rctx->mgf1md);
455 } else
456 ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
457 rctx->pad_mode);
458 if (ret < 0)
459 return ret;
460 *outlen = ret;
461 return 1;
462}
07e970c7 463
75d44c04 464static int check_padding_md(const EVP_MD *md, int padding)
ae5c8664
MC
465{
466 if (!md)
467 return 1;
468
469 if (padding == RSA_NO_PADDING) {
470 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
471 return 0;
472 }
473
474 if (padding == RSA_X931_PADDING) {
475 if (RSA_X931_hash_id(EVP_MD_type(md)) == -1) {
476 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
477 return 0;
478 }
479 return 1;
480 }
481
482 return 1;
483}
b2a97be7 484
4a3dc3c0 485static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
ae5c8664
MC
486{
487 RSA_PKEY_CTX *rctx = ctx->data;
488 switch (type) {
489 case EVP_PKEY_CTRL_RSA_PADDING:
490 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
491 if (!check_padding_md(rctx->md, p1))
492 return 0;
493 if (p1 == RSA_PKCS1_PSS_PADDING) {
494 if (!(ctx->operation &
495 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
496 goto bad_pad;
497 if (!rctx->md)
498 rctx->md = EVP_sha1();
499 }
500 if (p1 == RSA_PKCS1_OAEP_PADDING) {
501 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
502 goto bad_pad;
503 if (!rctx->md)
504 rctx->md = EVP_sha1();
505 }
506 rctx->pad_mode = p1;
507 return 1;
508 }
509 bad_pad:
510 RSAerr(RSA_F_PKEY_RSA_CTRL,
511 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
512 return -2;
513
514 case EVP_PKEY_CTRL_GET_RSA_PADDING:
515 *(int *)p2 = rctx->pad_mode;
516 return 1;
517
518 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
519 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
520 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
521 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
522 return -2;
523 }
524 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN)
525 *(int *)p2 = rctx->saltlen;
526 else {
527 if (p1 < -2)
528 return -2;
529 rctx->saltlen = p1;
530 }
531 return 1;
532
533 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
534 if (p1 < 256) {
535 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_KEYBITS);
536 return -2;
537 }
538 rctx->nbits = p1;
539 return 1;
540
541 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
542 if (!p2)
543 return -2;
544 BN_free(rctx->pub_exp);
545 rctx->pub_exp = p2;
546 return 1;
547
548 case EVP_PKEY_CTRL_RSA_OAEP_MD:
549 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
550 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
551 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
552 return -2;
553 }
554 if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
555 *(const EVP_MD **)p2 = rctx->md;
556 else
557 rctx->md = p2;
558 return 1;
559
560 case EVP_PKEY_CTRL_MD:
561 if (!check_padding_md(p2, rctx->pad_mode))
562 return 0;
563 rctx->md = p2;
564 return 1;
565
566 case EVP_PKEY_CTRL_GET_MD:
567 *(const EVP_MD **)p2 = rctx->md;
568 return 1;
569
570 case EVP_PKEY_CTRL_RSA_MGF1_MD:
571 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
572 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
573 && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
574 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
575 return -2;
576 }
577 if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
578 if (rctx->mgf1md)
579 *(const EVP_MD **)p2 = rctx->mgf1md;
580 else
581 *(const EVP_MD **)p2 = rctx->md;
582 } else
583 rctx->mgf1md = p2;
584 return 1;
585
586 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
587 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
588 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
589 return -2;
590 }
591 if (rctx->oaep_label)
592 OPENSSL_free(rctx->oaep_label);
593 if (p2 && p1 > 0) {
594 rctx->oaep_label = p2;
595 rctx->oaep_labellen = p1;
596 } else {
597 rctx->oaep_label = NULL;
598 rctx->oaep_labellen = 0;
599 }
600 return 1;
601
602 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
603 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
604 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
605 return -2;
606 }
607 *(unsigned char **)p2 = rctx->oaep_label;
608 return rctx->oaep_labellen;
609
610 case EVP_PKEY_CTRL_DIGESTINIT:
611 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
612 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
613 case EVP_PKEY_CTRL_PKCS7_SIGN:
614 return 1;
8931b30d 615#ifndef OPENSSL_NO_CMS
ae5c8664
MC
616 case EVP_PKEY_CTRL_CMS_DECRYPT:
617 case EVP_PKEY_CTRL_CMS_ENCRYPT:
618 case EVP_PKEY_CTRL_CMS_SIGN:
619 return 1;
9d972207 620#endif
ae5c8664
MC
621 case EVP_PKEY_CTRL_PEER_KEY:
622 RSAerr(RSA_F_PKEY_RSA_CTRL,
623 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
624 return -2;
625
626 default:
627 return -2;
399a6f0b 628
ae5c8664
MC
629 }
630}
4a3dc3c0 631
4a3dc3c0 632static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
ae5c8664
MC
633 const char *type, const char *value)
634{
635 if (!value) {
636 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
637 return 0;
638 }
639 if (!strcmp(type, "rsa_padding_mode")) {
640 int pm;
641 if (!strcmp(value, "pkcs1"))
642 pm = RSA_PKCS1_PADDING;
643 else if (!strcmp(value, "sslv23"))
644 pm = RSA_SSLV23_PADDING;
645 else if (!strcmp(value, "none"))
646 pm = RSA_NO_PADDING;
647 else if (!strcmp(value, "oeap"))
648 pm = RSA_PKCS1_OAEP_PADDING;
649 else if (!strcmp(value, "oaep"))
650 pm = RSA_PKCS1_OAEP_PADDING;
651 else if (!strcmp(value, "x931"))
652 pm = RSA_X931_PADDING;
653 else if (!strcmp(value, "pss"))
654 pm = RSA_PKCS1_PSS_PADDING;
655 else {
656 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
657 return -2;
658 }
659 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
660 }
661
662 if (!strcmp(type, "rsa_pss_saltlen")) {
663 int saltlen;
664 saltlen = atoi(value);
665 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
666 }
667
668 if (!strcmp(type, "rsa_keygen_bits")) {
669 int nbits;
670 nbits = atoi(value);
671 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
672 }
673
674 if (!strcmp(type, "rsa_keygen_pubexp")) {
675 int ret;
676 BIGNUM *pubexp = NULL;
677 if (!BN_asc2bn(&pubexp, value))
678 return 0;
679 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
680 if (ret <= 0)
681 BN_free(pubexp);
682 return ret;
683 }
684
685 if (!strcmp(type, "rsa_mgf1_md")) {
686 const EVP_MD *md;
687 if (!(md = EVP_get_digestbyname(value))) {
688 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
689 return 0;
690 }
691 return EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, md);
692 }
693
694 if (!strcmp(type, "rsa_oaep_md")) {
695 const EVP_MD *md;
696 if (!(md = EVP_get_digestbyname(value))) {
697 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
698 return 0;
699 }
700 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md);
701 }
702 if (!strcmp(type, "rsa_oaep_label")) {
703 unsigned char *lab;
704 long lablen;
705 int ret;
706 lab = string_to_hex(value, &lablen);
707 if (!lab)
708 return 0;
709 ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
710 if (ret <= 0)
711 OPENSSL_free(lab);
712 return ret;
713 }
714
715 return -2;
716}
4a3dc3c0 717
f5cda4cb 718static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
ae5c8664
MC
719{
720 RSA *rsa = NULL;
721 RSA_PKEY_CTX *rctx = ctx->data;
722 BN_GENCB *pcb, cb;
723 int ret;
724 if (!rctx->pub_exp) {
725 rctx->pub_exp = BN_new();
726 if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4))
727 return 0;
728 }
729 rsa = RSA_new();
730 if (!rsa)
731 return 0;
732 if (ctx->pkey_gencb) {
733 pcb = &cb;
734 evp_pkey_set_cb_translate(pcb, ctx);
735 } else
736 pcb = NULL;
737 ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
738 if (ret > 0)
739 EVP_PKEY_assign_RSA(pkey, rsa);
740 else
741 RSA_free(rsa);
742 return ret;
743}
744
745const 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};