]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/rsa/rsa_ameth.c
Remove /* foo.c */ comments
[thirdparty/openssl.git] / crypto / rsa / rsa_ameth.c
CommitLineData
0f113f3e
MC
1/*
2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
3 * 2006.
448be743
DSH
4 */
5/* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
0f113f3e 13 * notice, this list of conditions and the following disclaimer.
448be743
DSH
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
b39fc560 60#include "internal/cryptlib.h"
448be743
DSH
61#include <openssl/asn1t.h>
62#include <openssl/x509.h>
63#include <openssl/rsa.h>
1e26a8ba 64#include <openssl/bn.h>
8931b30d 65#ifndef OPENSSL_NO_CMS
0f113f3e 66# include <openssl/cms.h>
8931b30d 67#endif
5fe736e5 68#include "internal/asn1_int.h"
3aeb9348 69#include "internal/evp_int.h"
448be743 70
e968561d 71#ifndef OPENSSL_NO_CMS
0574cadf
DSH
72static int rsa_cms_sign(CMS_SignerInfo *si);
73static int rsa_cms_verify(CMS_SignerInfo *si);
74static int rsa_cms_decrypt(CMS_RecipientInfo *ri);
75static int rsa_cms_encrypt(CMS_RecipientInfo *ri);
e968561d 76#endif
0574cadf 77
6f81892e 78static int rsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
0f113f3e
MC
79{
80 unsigned char *penc = NULL;
81 int penclen;
82 penclen = i2d_RSAPublicKey(pkey->pkey.rsa, &penc);
83 if (penclen <= 0)
84 return 0;
85 if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_RSA),
86 V_ASN1_NULL, NULL, penc, penclen))
87 return 1;
88
89 OPENSSL_free(penc);
90 return 0;
91}
448be743
DSH
92
93static int rsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
0f113f3e
MC
94{
95 const unsigned char *p;
96 int pklen;
97 RSA *rsa = NULL;
75ebbd9a 98
0f113f3e
MC
99 if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, NULL, pubkey))
100 return 0;
75ebbd9a 101 if ((rsa = d2i_RSAPublicKey(NULL, &p, pklen)) == NULL) {
0f113f3e
MC
102 RSAerr(RSA_F_RSA_PUB_DECODE, ERR_R_RSA_LIB);
103 return 0;
104 }
105 EVP_PKEY_assign_RSA(pkey, rsa);
106 return 1;
107}
448be743 108
6f81892e 109static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
0f113f3e
MC
110{
111 if (BN_cmp(b->pkey.rsa->n, a->pkey.rsa->n) != 0
112 || BN_cmp(b->pkey.rsa->e, a->pkey.rsa->e) != 0)
113 return 0;
114 return 1;
115}
6f81892e 116
e4263314 117static int old_rsa_priv_decode(EVP_PKEY *pkey,
0f113f3e
MC
118 const unsigned char **pder, int derlen)
119{
120 RSA *rsa;
75ebbd9a
RS
121
122 if ((rsa = d2i_RSAPrivateKey(NULL, pder, derlen)) == NULL) {
0f113f3e
MC
123 RSAerr(RSA_F_OLD_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
124 return 0;
125 }
126 EVP_PKEY_assign_RSA(pkey, rsa);
127 return 1;
128}
448be743 129
e4263314 130static int old_rsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
0f113f3e
MC
131{
132 return i2d_RSAPrivateKey(pkey->pkey.rsa, pder);
133}
e4263314 134
6f81892e 135static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
0f113f3e
MC
136{
137 unsigned char *rk = NULL;
138 int rklen;
139 rklen = i2d_RSAPrivateKey(pkey->pkey.rsa, &rk);
140
141 if (rklen <= 0) {
142 RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
143 return 0;
144 }
145
146 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_rsaEncryption), 0,
147 V_ASN1_NULL, NULL, rk, rklen)) {
148 RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
149 return 0;
150 }
151
152 return 1;
153}
448be743 154
e4263314 155static int rsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
0f113f3e
MC
156{
157 const unsigned char *p;
158 int pklen;
159 if (!PKCS8_pkey_get0(NULL, &p, &pklen, NULL, p8))
160 return 0;
161 return old_rsa_priv_decode(pkey, &p, pklen);
162}
e4263314 163
6f81892e 164static int int_rsa_size(const EVP_PKEY *pkey)
0f113f3e
MC
165{
166 return RSA_size(pkey->pkey.rsa);
167}
6f81892e
DSH
168
169static int rsa_bits(const EVP_PKEY *pkey)
0f113f3e
MC
170{
171 return BN_num_bits(pkey->pkey.rsa->n);
172}
6f81892e 173
2514fa79 174static int rsa_security_bits(const EVP_PKEY *pkey)
0f113f3e
MC
175{
176 return RSA_security_bits(pkey->pkey.rsa);
177}
2514fa79 178
6f81892e 179static void int_rsa_free(EVP_PKEY *pkey)
0f113f3e
MC
180{
181 RSA_free(pkey->pkey.rsa);
182}
35208f36
DSH
183
184static void update_buflen(const BIGNUM *b, size_t *pbuflen)
0f113f3e
MC
185{
186 size_t i;
187 if (!b)
188 return;
189 if (*pbuflen < (i = (size_t)BN_num_bytes(b)))
190 *pbuflen = i;
191}
35208f36
DSH
192
193static int do_rsa_print(BIO *bp, const RSA *x, int off, int priv)
0f113f3e
MC
194{
195 char *str;
196 const char *s;
197 unsigned char *m = NULL;
198 int ret = 0, mod_len = 0;
199 size_t buf_len = 0;
200
201 update_buflen(x->n, &buf_len);
202 update_buflen(x->e, &buf_len);
203
204 if (priv) {
205 update_buflen(x->d, &buf_len);
206 update_buflen(x->p, &buf_len);
207 update_buflen(x->q, &buf_len);
208 update_buflen(x->dmp1, &buf_len);
209 update_buflen(x->dmq1, &buf_len);
210 update_buflen(x->iqmp, &buf_len);
211 }
212
b196e7d9 213 m = OPENSSL_malloc(buf_len + 10);
0f113f3e
MC
214 if (m == NULL) {
215 RSAerr(RSA_F_DO_RSA_PRINT, ERR_R_MALLOC_FAILURE);
216 goto err;
217 }
218
219 if (x->n != NULL)
220 mod_len = BN_num_bits(x->n);
221
222 if (!BIO_indent(bp, off, 128))
223 goto err;
224
225 if (priv && x->d) {
226 if (BIO_printf(bp, "Private-Key: (%d bit)\n", mod_len)
227 <= 0)
228 goto err;
229 str = "modulus:";
230 s = "publicExponent:";
231 } else {
232 if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len)
233 <= 0)
234 goto err;
235 str = "Modulus:";
236 s = "Exponent:";
237 }
238 if (!ASN1_bn_print(bp, str, x->n, m, off))
239 goto err;
240 if (!ASN1_bn_print(bp, s, x->e, m, off))
241 goto err;
242 if (priv) {
243 if (!ASN1_bn_print(bp, "privateExponent:", x->d, m, off))
244 goto err;
245 if (!ASN1_bn_print(bp, "prime1:", x->p, m, off))
246 goto err;
247 if (!ASN1_bn_print(bp, "prime2:", x->q, m, off))
248 goto err;
249 if (!ASN1_bn_print(bp, "exponent1:", x->dmp1, m, off))
250 goto err;
251 if (!ASN1_bn_print(bp, "exponent2:", x->dmq1, m, off))
252 goto err;
253 if (!ASN1_bn_print(bp, "coefficient:", x->iqmp, m, off))
254 goto err;
255 }
256 ret = 1;
257 err:
b548a1f1 258 OPENSSL_free(m);
0f113f3e
MC
259 return (ret);
260}
35208f36
DSH
261
262static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
0f113f3e
MC
263 ASN1_PCTX *ctx)
264{
265 return do_rsa_print(bp, pkey->pkey.rsa, indent, 0);
266}
35208f36
DSH
267
268static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
0f113f3e
MC
269 ASN1_PCTX *ctx)
270{
271 return do_rsa_print(bp, pkey->pkey.rsa, indent, 1);
272}
35208f36 273
0574cadf
DSH
274/* Given an MGF1 Algorithm ID decode to an Algorithm Identifier */
275static X509_ALGOR *rsa_mgf1_decode(X509_ALGOR *alg)
0f113f3e 276{
0f113f3e
MC
277 if (alg == NULL)
278 return NULL;
279 if (OBJ_obj2nid(alg->algorithm) != NID_mgf1)
280 return NULL;
e93c8748
DSH
281 return ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),
282 alg->parameter);
0f113f3e 283}
0574cadf 284
63b825c9 285static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg,
0f113f3e
MC
286 X509_ALGOR **pmaskHash)
287{
0f113f3e
MC
288 RSA_PSS_PARAMS *pss;
289
290 *pmaskHash = NULL;
291
e93c8748
DSH
292 pss = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_PSS_PARAMS),
293 alg->parameter);
0f113f3e
MC
294
295 if (!pss)
296 return NULL;
297
298 *pmaskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
299
300 return pss;
301}
302
303static int rsa_pss_param_print(BIO *bp, RSA_PSS_PARAMS *pss,
304 X509_ALGOR *maskHash, int indent)
305{
306 int rv = 0;
307 if (!pss) {
308 if (BIO_puts(bp, " (INVALID PSS PARAMETERS)\n") <= 0)
309 return 0;
310 return 1;
311 }
312 if (BIO_puts(bp, "\n") <= 0)
313 goto err;
314 if (!BIO_indent(bp, indent, 128))
315 goto err;
316 if (BIO_puts(bp, "Hash Algorithm: ") <= 0)
317 goto err;
318
319 if (pss->hashAlgorithm) {
320 if (i2a_ASN1_OBJECT(bp, pss->hashAlgorithm->algorithm) <= 0)
321 goto err;
322 } else if (BIO_puts(bp, "sha1 (default)") <= 0)
323 goto err;
324
325 if (BIO_puts(bp, "\n") <= 0)
326 goto err;
327
328 if (!BIO_indent(bp, indent, 128))
329 goto err;
330
331 if (BIO_puts(bp, "Mask Algorithm: ") <= 0)
332 goto err;
333 if (pss->maskGenAlgorithm) {
334 if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0)
335 goto err;
336 if (BIO_puts(bp, " with ") <= 0)
337 goto err;
338 if (maskHash) {
339 if (i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0)
340 goto err;
341 } else if (BIO_puts(bp, "INVALID") <= 0)
342 goto err;
343 } else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0)
344 goto err;
345 BIO_puts(bp, "\n");
346
347 if (!BIO_indent(bp, indent, 128))
348 goto err;
349 if (BIO_puts(bp, "Salt Length: 0x") <= 0)
350 goto err;
351 if (pss->saltLength) {
352 if (i2a_ASN1_INTEGER(bp, pss->saltLength) <= 0)
353 goto err;
354 } else if (BIO_puts(bp, "14 (default)") <= 0)
355 goto err;
356 BIO_puts(bp, "\n");
357
358 if (!BIO_indent(bp, indent, 128))
359 goto err;
360 if (BIO_puts(bp, "Trailer Field: 0x") <= 0)
361 goto err;
362 if (pss->trailerField) {
363 if (i2a_ASN1_INTEGER(bp, pss->trailerField) <= 0)
364 goto err;
365 } else if (BIO_puts(bp, "BC (default)") <= 0)
366 goto err;
367 BIO_puts(bp, "\n");
368
369 rv = 1;
370
371 err:
372 return rv;
373
374}
ff04bbe3
DSH
375
376static int rsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
0f113f3e
MC
377 const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
378{
379 if (OBJ_obj2nid(sigalg->algorithm) == NID_rsassaPss) {
380 int rv;
381 RSA_PSS_PARAMS *pss;
382 X509_ALGOR *maskHash;
383 pss = rsa_pss_decode(sigalg, &maskHash);
384 rv = rsa_pss_param_print(bp, pss, maskHash, indent);
25aaa98a 385 RSA_PSS_PARAMS_free(pss);
222561fe 386 X509_ALGOR_free(maskHash);
0f113f3e
MC
387 if (!rv)
388 return 0;
389 } else if (!sig && BIO_puts(bp, "\n") <= 0)
390 return 0;
391 if (sig)
392 return X509_signature_dump(bp, sig, indent);
393 return 1;
394}
492a9e24
DSH
395
396static int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
0f113f3e
MC
397{
398 X509_ALGOR *alg = NULL;
399 switch (op) {
400
401 case ASN1_PKEY_CTRL_PKCS7_SIGN:
402 if (arg1 == 0)
403 PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, NULL, &alg);
404 break;
405
406 case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
407 if (arg1 == 0)
408 PKCS7_RECIP_INFO_get0_alg(arg2, &alg);
409 break;
8931b30d 410#ifndef OPENSSL_NO_CMS
0f113f3e
MC
411 case ASN1_PKEY_CTRL_CMS_SIGN:
412 if (arg1 == 0)
413 return rsa_cms_sign(arg2);
414 else if (arg1 == 1)
415 return rsa_cms_verify(arg2);
416 break;
417
418 case ASN1_PKEY_CTRL_CMS_ENVELOPE:
419 if (arg1 == 0)
420 return rsa_cms_encrypt(arg2);
421 else if (arg1 == 1)
422 return rsa_cms_decrypt(arg2);
423 break;
424
425 case ASN1_PKEY_CTRL_CMS_RI_TYPE:
426 *(int *)arg2 = CMS_RECIPINFO_TRANS;
427 return 1;
8931b30d 428#endif
a78568b7 429
0f113f3e
MC
430 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
431 *(int *)arg2 = NID_sha256;
432 return 1;
03919683 433
0f113f3e
MC
434 default:
435 return -2;
492a9e24 436
0f113f3e 437 }
492a9e24 438
0f113f3e
MC
439 if (alg)
440 X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
4f1aa191 441
0f113f3e 442 return 1;
4f1aa191 443
0f113f3e 444}
492a9e24 445
0574cadf
DSH
446/* allocate and set algorithm ID from EVP_MD, default SHA1 */
447static int rsa_md_to_algor(X509_ALGOR **palg, const EVP_MD *md)
0f113f3e
MC
448{
449 if (EVP_MD_type(md) == NID_sha1)
450 return 1;
451 *palg = X509_ALGOR_new();
90945fa3 452 if (*palg == NULL)
0f113f3e
MC
453 return 0;
454 X509_ALGOR_set_md(*palg, md);
455 return 1;
456}
0574cadf
DSH
457
458/* Allocate and set MGF1 algorithm ID from EVP_MD */
459static int rsa_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md)
0f113f3e
MC
460{
461 X509_ALGOR *algtmp = NULL;
462 ASN1_STRING *stmp = NULL;
463 *palg = NULL;
464 if (EVP_MD_type(mgf1md) == NID_sha1)
465 return 1;
466 /* need to embed algorithm ID inside another */
467 if (!rsa_md_to_algor(&algtmp, mgf1md))
468 goto err;
469 if (!ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp))
470 goto err;
471 *palg = X509_ALGOR_new();
90945fa3 472 if (*palg == NULL)
0f113f3e
MC
473 goto err;
474 X509_ALGOR_set0(*palg, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp);
475 stmp = NULL;
476 err:
0dfb9398 477 ASN1_STRING_free(stmp);
222561fe 478 X509_ALGOR_free(algtmp);
0f113f3e
MC
479 if (*palg)
480 return 1;
481 return 0;
482}
0574cadf
DSH
483
484/* convert algorithm ID to EVP_MD, default SHA1 */
485static const EVP_MD *rsa_algor_to_md(X509_ALGOR *alg)
0f113f3e
MC
486{
487 const EVP_MD *md;
488 if (!alg)
489 return EVP_sha1();
490 md = EVP_get_digestbyobj(alg->algorithm);
491 if (md == NULL)
492 RSAerr(RSA_F_RSA_ALGOR_TO_MD, RSA_R_UNKNOWN_DIGEST);
493 return md;
494}
495
0574cadf
DSH
496/* convert MGF1 algorithm ID to EVP_MD, default SHA1 */
497static const EVP_MD *rsa_mgf1_to_md(X509_ALGOR *alg, X509_ALGOR *maskHash)
0f113f3e
MC
498{
499 const EVP_MD *md;
500 if (!alg)
501 return EVP_sha1();
502 /* Check mask and lookup mask hash algorithm */
503 if (OBJ_obj2nid(alg->algorithm) != NID_mgf1) {
504 RSAerr(RSA_F_RSA_MGF1_TO_MD, RSA_R_UNSUPPORTED_MASK_ALGORITHM);
505 return NULL;
506 }
507 if (!maskHash) {
508 RSAerr(RSA_F_RSA_MGF1_TO_MD, RSA_R_UNSUPPORTED_MASK_PARAMETER);
509 return NULL;
510 }
511 md = EVP_get_digestbyobj(maskHash->algorithm);
512 if (md == NULL) {
513 RSAerr(RSA_F_RSA_MGF1_TO_MD, RSA_R_UNKNOWN_MASK_DIGEST);
514 return NULL;
515 }
516 return md;
517}
518
519/*
520 * Convert EVP_PKEY_CTX is PSS mode into corresponding algorithm parameter,
0574cadf 521 * suitable for setting an AlgorithmIdentifier.
31904ecd
DSH
522 */
523
0574cadf 524static ASN1_STRING *rsa_ctx_to_pss(EVP_PKEY_CTX *pkctx)
0f113f3e
MC
525{
526 const EVP_MD *sigmd, *mgf1md;
527 RSA_PSS_PARAMS *pss = NULL;
528 ASN1_STRING *os = NULL;
529 EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(pkctx);
530 int saltlen, rv = 0;
531 if (EVP_PKEY_CTX_get_signature_md(pkctx, &sigmd) <= 0)
532 goto err;
533 if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
534 goto err;
535 if (!EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx, &saltlen))
536 goto err;
537 if (saltlen == -1)
538 saltlen = EVP_MD_size(sigmd);
539 else if (saltlen == -2) {
540 saltlen = EVP_PKEY_size(pk) - EVP_MD_size(sigmd) - 2;
541 if (((EVP_PKEY_bits(pk) - 1) & 0x7) == 0)
542 saltlen--;
543 }
544 pss = RSA_PSS_PARAMS_new();
90945fa3 545 if (pss == NULL)
0f113f3e
MC
546 goto err;
547 if (saltlen != 20) {
548 pss->saltLength = ASN1_INTEGER_new();
90945fa3 549 if (pss->saltLength == NULL)
0f113f3e
MC
550 goto err;
551 if (!ASN1_INTEGER_set(pss->saltLength, saltlen))
552 goto err;
553 }
554 if (!rsa_md_to_algor(&pss->hashAlgorithm, sigmd))
555 goto err;
556 if (!rsa_md_to_mgf1(&pss->maskGenAlgorithm, mgf1md))
557 goto err;
558 /* Finally create string with pss parameter encoding. */
559 if (!ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), &os))
560 goto err;
561 rv = 1;
562 err:
25aaa98a 563 RSA_PSS_PARAMS_free(pss);
0f113f3e
MC
564 if (rv)
565 return os;
0dfb9398 566 ASN1_STRING_free(os);
0f113f3e
MC
567 return NULL;
568}
569
570/*
571 * From PSS AlgorithmIdentifier set public key parameters. If pkey isn't NULL
572 * then the EVP_MD_CTX is setup and initalised. If it is NULL parameters are
573 * passed to pkctx instead.
0574cadf 574 */
31904ecd 575
0574cadf 576static int rsa_pss_to_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pkctx,
0f113f3e
MC
577 X509_ALGOR *sigalg, EVP_PKEY *pkey)
578{
579 int rv = -1;
580 int saltlen;
581 const EVP_MD *mgf1md = NULL, *md = NULL;
582 RSA_PSS_PARAMS *pss;
583 X509_ALGOR *maskHash;
584 /* Sanity check: make sure it is PSS */
585 if (OBJ_obj2nid(sigalg->algorithm) != NID_rsassaPss) {
586 RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
587 return -1;
588 }
589 /* Decode PSS parameters */
590 pss = rsa_pss_decode(sigalg, &maskHash);
591
592 if (pss == NULL) {
593 RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_INVALID_PSS_PARAMETERS);
594 goto err;
595 }
596 mgf1md = rsa_mgf1_to_md(pss->maskGenAlgorithm, maskHash);
597 if (!mgf1md)
598 goto err;
599 md = rsa_algor_to_md(pss->hashAlgorithm);
600 if (!md)
601 goto err;
602
603 if (pss->saltLength) {
604 saltlen = ASN1_INTEGER_get(pss->saltLength);
605
606 /*
607 * Could perform more salt length sanity checks but the main RSA
608 * routines will trap other invalid values anyway.
609 */
610 if (saltlen < 0) {
611 RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_INVALID_SALT_LENGTH);
612 goto err;
613 }
614 } else
615 saltlen = 20;
616
617 /*
618 * low-level routines support only trailer field 0xbc (value 1) and
619 * PKCS#1 says we should reject any other value anyway.
620 */
621 if (pss->trailerField && ASN1_INTEGER_get(pss->trailerField) != 1) {
622 RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_INVALID_TRAILER);
623 goto err;
624 }
625
626 /* We have all parameters now set up context */
627
628 if (pkey) {
629 if (!EVP_DigestVerifyInit(ctx, &pkctx, md, NULL, pkey))
630 goto err;
631 } else {
632 const EVP_MD *checkmd;
633 if (EVP_PKEY_CTX_get_signature_md(pkctx, &checkmd) <= 0)
634 goto err;
635 if (EVP_MD_type(md) != EVP_MD_type(checkmd)) {
636 RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_DIGEST_DOES_NOT_MATCH);
637 goto err;
638 }
639 }
640
641 if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_PSS_PADDING) <= 0)
642 goto err;
643
644 if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx, saltlen) <= 0)
645 goto err;
646
647 if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
648 goto err;
649 /* Carry on */
650 rv = 1;
651
652 err:
653 RSA_PSS_PARAMS_free(pss);
222561fe 654 X509_ALGOR_free(maskHash);
0f113f3e
MC
655 return rv;
656}
492a9e24 657
e968561d 658#ifndef OPENSSL_NO_CMS
0574cadf 659static int rsa_cms_verify(CMS_SignerInfo *si)
0f113f3e
MC
660{
661 int nid, nid2;
662 X509_ALGOR *alg;
663 EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
664 CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
665 nid = OBJ_obj2nid(alg->algorithm);
666 if (nid == NID_rsaEncryption)
667 return 1;
668 if (nid == NID_rsassaPss)
669 return rsa_pss_to_ctx(NULL, pkctx, alg, NULL);
670 /* Workaround for some implementation that use a signature OID */
671 if (OBJ_find_sigid_algs(nid, NULL, &nid2)) {
672 if (nid2 == NID_rsaEncryption)
673 return 1;
674 }
675 return 0;
676}
e968561d 677#endif
0f113f3e
MC
678
679/*
680 * Customised RSA item verification routine. This is called when a signature
681 * is encountered requiring special handling. We currently only handle PSS.
0574cadf
DSH
682 */
683
0574cadf 684static int rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
0f113f3e
MC
685 X509_ALGOR *sigalg, ASN1_BIT_STRING *sig,
686 EVP_PKEY *pkey)
687{
688 /* Sanity check: make sure it is PSS */
689 if (OBJ_obj2nid(sigalg->algorithm) != NID_rsassaPss) {
690 RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
691 return -1;
692 }
09f06923 693 if (rsa_pss_to_ctx(ctx, NULL, sigalg, pkey) > 0) {
0f113f3e
MC
694 /* Carry on */
695 return 2;
09f06923 696 }
0f113f3e
MC
697 return -1;
698}
0574cadf 699
e968561d 700#ifndef OPENSSL_NO_CMS
0574cadf 701static int rsa_cms_sign(CMS_SignerInfo *si)
0f113f3e
MC
702{
703 int pad_mode = RSA_PKCS1_PADDING;
704 X509_ALGOR *alg;
705 EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
706 ASN1_STRING *os = NULL;
707 CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
708 if (pkctx) {
709 if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
710 return 0;
711 }
712 if (pad_mode == RSA_PKCS1_PADDING) {
713 X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
714 return 1;
715 }
716 /* We don't support it */
717 if (pad_mode != RSA_PKCS1_PSS_PADDING)
718 return 0;
719 os = rsa_ctx_to_pss(pkctx);
720 if (!os)
721 return 0;
722 X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsassaPss), V_ASN1_SEQUENCE, os);
723 return 1;
724}
e968561d 725#endif
0574cadf 726
17c63d1c 727static int rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
0f113f3e
MC
728 X509_ALGOR *alg1, X509_ALGOR *alg2,
729 ASN1_BIT_STRING *sig)
730{
731 int pad_mode;
6e59a892 732 EVP_PKEY_CTX *pkctx = EVP_MD_CTX_pkey_ctx(ctx);
0f113f3e
MC
733 if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
734 return 0;
735 if (pad_mode == RSA_PKCS1_PADDING)
736 return 2;
737 if (pad_mode == RSA_PKCS1_PSS_PADDING) {
738 ASN1_STRING *os1 = NULL;
739 os1 = rsa_ctx_to_pss(pkctx);
740 if (!os1)
741 return 0;
742 /* Duplicate parameters if we have to */
743 if (alg2) {
744 ASN1_STRING *os2 = ASN1_STRING_dup(os1);
745 if (!os2) {
746 ASN1_STRING_free(os1);
747 return 0;
748 }
749 X509_ALGOR_set0(alg2, OBJ_nid2obj(NID_rsassaPss),
750 V_ASN1_SEQUENCE, os2);
751 }
752 X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_rsassaPss),
753 V_ASN1_SEQUENCE, os1);
754 return 3;
755 }
756 return 2;
757}
17c63d1c 758
e968561d 759#ifndef OPENSSL_NO_CMS
0574cadf 760static RSA_OAEP_PARAMS *rsa_oaep_decode(const X509_ALGOR *alg,
0f113f3e
MC
761 X509_ALGOR **pmaskHash)
762{
0f113f3e 763 RSA_OAEP_PARAMS *pss;
0574cadf 764
0f113f3e 765 *pmaskHash = NULL;
0574cadf 766
e93c8748
DSH
767 pss = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_OAEP_PARAMS),
768 alg->parameter);
0574cadf 769
0f113f3e
MC
770 if (!pss)
771 return NULL;
0574cadf 772
0f113f3e 773 *pmaskHash = rsa_mgf1_decode(pss->maskGenFunc);
0574cadf 774
0f113f3e
MC
775 return pss;
776}
0574cadf
DSH
777
778static int rsa_cms_decrypt(CMS_RecipientInfo *ri)
0f113f3e
MC
779{
780 EVP_PKEY_CTX *pkctx;
781 X509_ALGOR *cmsalg;
782 int nid;
783 int rv = -1;
784 unsigned char *label = NULL;
785 int labellen = 0;
786 const EVP_MD *mgf1md = NULL, *md = NULL;
787 RSA_OAEP_PARAMS *oaep;
788 X509_ALGOR *maskHash;
789 pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
790 if (!pkctx)
791 return 0;
792 if (!CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &cmsalg))
793 return -1;
794 nid = OBJ_obj2nid(cmsalg->algorithm);
795 if (nid == NID_rsaEncryption)
796 return 1;
797 if (nid != NID_rsaesOaep) {
798 RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_ENCRYPTION_TYPE);
799 return -1;
800 }
801 /* Decode OAEP parameters */
802 oaep = rsa_oaep_decode(cmsalg, &maskHash);
803
804 if (oaep == NULL) {
805 RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_OAEP_PARAMETERS);
806 goto err;
807 }
808
809 mgf1md = rsa_mgf1_to_md(oaep->maskGenFunc, maskHash);
810 if (!mgf1md)
811 goto err;
812 md = rsa_algor_to_md(oaep->hashFunc);
813 if (!md)
814 goto err;
815
816 if (oaep->pSourceFunc) {
817 X509_ALGOR *plab = oaep->pSourceFunc;
818 if (OBJ_obj2nid(plab->algorithm) != NID_pSpecified) {
819 RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_LABEL_SOURCE);
820 goto err;
821 }
822 if (plab->parameter->type != V_ASN1_OCTET_STRING) {
823 RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_LABEL);
824 goto err;
825 }
826
827 label = plab->parameter->value.octet_string->data;
828 /* Stop label being freed when OAEP parameters are freed */
829 plab->parameter->value.octet_string->data = NULL;
830 labellen = plab->parameter->value.octet_string->length;
831 }
832
833 if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0)
834 goto err;
835 if (EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, md) <= 0)
836 goto err;
837 if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
838 goto err;
839 if (EVP_PKEY_CTX_set0_rsa_oaep_label(pkctx, label, labellen) <= 0)
840 goto err;
841 /* Carry on */
842 rv = 1;
843
844 err:
845 RSA_OAEP_PARAMS_free(oaep);
222561fe 846 X509_ALGOR_free(maskHash);
0f113f3e
MC
847 return rv;
848}
0574cadf
DSH
849
850static int rsa_cms_encrypt(CMS_RecipientInfo *ri)
0f113f3e
MC
851{
852 const EVP_MD *md, *mgf1md;
853 RSA_OAEP_PARAMS *oaep = NULL;
854 ASN1_STRING *os = NULL;
855 X509_ALGOR *alg;
856 EVP_PKEY_CTX *pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
857 int pad_mode = RSA_PKCS1_PADDING, rv = 0, labellen;
858 unsigned char *label;
859 CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &alg);
860 if (pkctx) {
861 if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
862 return 0;
863 }
864 if (pad_mode == RSA_PKCS1_PADDING) {
865 X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
866 return 1;
867 }
868 /* Not supported */
869 if (pad_mode != RSA_PKCS1_OAEP_PADDING)
870 return 0;
871 if (EVP_PKEY_CTX_get_rsa_oaep_md(pkctx, &md) <= 0)
872 goto err;
873 if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
874 goto err;
875 labellen = EVP_PKEY_CTX_get0_rsa_oaep_label(pkctx, &label);
876 if (labellen < 0)
877 goto err;
878 oaep = RSA_OAEP_PARAMS_new();
90945fa3 879 if (oaep == NULL)
0f113f3e
MC
880 goto err;
881 if (!rsa_md_to_algor(&oaep->hashFunc, md))
882 goto err;
883 if (!rsa_md_to_mgf1(&oaep->maskGenFunc, mgf1md))
884 goto err;
885 if (labellen > 0) {
886 ASN1_OCTET_STRING *los = ASN1_OCTET_STRING_new();
887 oaep->pSourceFunc = X509_ALGOR_new();
90945fa3 888 if (oaep->pSourceFunc == NULL)
0f113f3e 889 goto err;
90945fa3 890 if (los == NULL)
0f113f3e
MC
891 goto err;
892 if (!ASN1_OCTET_STRING_set(los, label, labellen)) {
893 ASN1_OCTET_STRING_free(los);
894 goto err;
895 }
896 X509_ALGOR_set0(oaep->pSourceFunc, OBJ_nid2obj(NID_pSpecified),
897 V_ASN1_OCTET_STRING, los);
898 }
899 /* create string with pss parameter encoding. */
900 if (!ASN1_item_pack(oaep, ASN1_ITEM_rptr(RSA_OAEP_PARAMS), &os))
901 goto err;
902 X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaesOaep), V_ASN1_SEQUENCE, os);
903 os = NULL;
904 rv = 1;
905 err:
25aaa98a 906 RSA_OAEP_PARAMS_free(oaep);
0dfb9398 907 ASN1_STRING_free(os);
0f113f3e
MC
908 return rv;
909}
e968561d 910#endif
0f113f3e
MC
911
912const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[] = {
913 {
914 EVP_PKEY_RSA,
915 EVP_PKEY_RSA,
916 ASN1_PKEY_SIGPARAM_NULL,
917
918 "RSA",
919 "OpenSSL RSA method",
920
921 rsa_pub_decode,
922 rsa_pub_encode,
923 rsa_pub_cmp,
924 rsa_pub_print,
925
926 rsa_priv_decode,
927 rsa_priv_encode,
928 rsa_priv_print,
929
930 int_rsa_size,
931 rsa_bits,
932 rsa_security_bits,
933
934 0, 0, 0, 0, 0, 0,
935
936 rsa_sig_print,
937 int_rsa_free,
938 rsa_pkey_ctrl,
939 old_rsa_priv_decode,
940 old_rsa_priv_encode,
941 rsa_item_verify,
942 rsa_item_sign},
943
944 {
945 EVP_PKEY_RSA2,
946 EVP_PKEY_RSA,
947 ASN1_PKEY_ALIAS}
948};