rsa_ameth.c 26.3 KB
Newer Older
1
/*
R
Rich Salz 已提交
2
 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
3
 *
R
Rich Salz 已提交
4 5 6 7
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
8 9 10
 */

#include <stdio.h>
11
#include "internal/cryptlib.h"
12 13
#include <openssl/asn1t.h>
#include <openssl/x509.h>
14
#include <openssl/bn.h>
R
Rich Salz 已提交
15
#include <openssl/cms.h>
16
#include "internal/asn1_int.h"
D
Dr. Stephen Henson 已提交
17
#include "internal/evp_int.h"
R
Richard Levitte 已提交
18
#include "rsa_locl.h"
19

D
David Bar 已提交
20
#ifndef OPENSSL_NO_CMS
21 22 23 24
static int rsa_cms_sign(CMS_SignerInfo *si);
static int rsa_cms_verify(CMS_SignerInfo *si);
static int rsa_cms_decrypt(CMS_RecipientInfo *ri);
static int rsa_cms_encrypt(CMS_RecipientInfo *ri);
D
David Bar 已提交
25
#endif
26

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/* Set any parameters associated with pkey */
static int rsa_param_encode(const EVP_PKEY *pkey,
                            ASN1_STRING **pstr, int *pstrtype)
{
    const RSA *rsa = pkey->pkey.rsa;
    *pstr = NULL;
    /* If RSA it's just NULL type */
    if (pkey->ameth->pkey_id == EVP_PKEY_RSA) {
        *pstrtype = V_ASN1_NULL;
        return 1;
    }
    /* If no PSS parameters we omit parameters entirely */
    if (rsa->pss == NULL) {
        *pstrtype = V_ASN1_UNDEF;
        return 1;
    }
    /* Encode PSS parameters */
    if (!ASN1_item_pack(rsa->pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), pstr)) {
        ASN1_STRING_free(*pstr);
        *pstr = NULL;
        return 0;
    }

    *pstrtype = V_ASN1_SEQUENCE;
    return 1;
}
/* Decode any parameters and set them in RSA structure */
static int rsa_param_decode(RSA *rsa, const X509_ALGOR *alg)
{
    const ASN1_OBJECT *algoid;
    const void *algp;
    int algptype;

    X509_ALGOR_get0(&algoid, &algptype, &algp, alg);
    if (OBJ_obj2nid(algoid) == EVP_PKEY_RSA)
        return 1;
    if (algptype == V_ASN1_UNDEF)
        return 1;
    if (algptype != V_ASN1_SEQUENCE)
        return 0;
    rsa->pss = ASN1_item_unpack(algp, ASN1_ITEM_rptr(RSA_PSS_PARAMS));
    if (rsa->pss == NULL)
        return 0;
    return 1;
}

73
static int rsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
74 75 76
{
    unsigned char *penc = NULL;
    int penclen;
77 78 79 80
    ASN1_STRING *str;
    int strtype;
    if (!rsa_param_encode(pkey, &str, &strtype))
        return 0;
81 82 83
    penclen = i2d_RSAPublicKey(pkey->pkey.rsa, &penc);
    if (penclen <= 0)
        return 0;
84 85
    if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id),
                               strtype, str, penc, penclen))
86 87 88 89 90
        return 1;

    OPENSSL_free(penc);
    return 0;
}
91 92

static int rsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
93 94 95
{
    const unsigned char *p;
    int pklen;
96
    X509_ALGOR *alg;
97
    RSA *rsa = NULL;
98

99
    if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &alg, pubkey))
100
        return 0;
101
    if ((rsa = d2i_RSAPublicKey(NULL, &p, pklen)) == NULL) {
102 103 104
        RSAerr(RSA_F_RSA_PUB_DECODE, ERR_R_RSA_LIB);
        return 0;
    }
105 106 107 108
    if (!rsa_param_decode(rsa, alg)) {
        RSA_free(rsa);
        return 0;
    }
109
    EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa);
110 111
    return 1;
}
112

113
static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
114 115 116 117 118 119
{
    if (BN_cmp(b->pkey.rsa->n, a->pkey.rsa->n) != 0
        || BN_cmp(b->pkey.rsa->e, a->pkey.rsa->e) != 0)
        return 0;
    return 1;
}
120

121
static int old_rsa_priv_decode(EVP_PKEY *pkey,
122 123 124
                               const unsigned char **pder, int derlen)
{
    RSA *rsa;
125 126

    if ((rsa = d2i_RSAPrivateKey(NULL, pder, derlen)) == NULL) {
127 128 129
        RSAerr(RSA_F_OLD_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
        return 0;
    }
130
    EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa);
131 132
    return 1;
}
133

134
static int old_rsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
135 136 137
{
    return i2d_RSAPrivateKey(pkey->pkey.rsa, pder);
}
138

139
static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
140 141 142
{
    unsigned char *rk = NULL;
    int rklen;
143 144 145 146
    ASN1_STRING *str;
    int strtype;
    if (!rsa_param_encode(pkey, &str, &strtype))
        return 0;
147 148 149 150 151 152 153
    rklen = i2d_RSAPrivateKey(pkey->pkey.rsa, &rk);

    if (rklen <= 0) {
        RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
        return 0;
    }

154
    if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(pkey->ameth->pkey_id), 0,
155
                         strtype, str, rk, rklen)) {
156 157 158 159 160 161
        RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
        return 0;
    }

    return 1;
}
162

163
static int rsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
164 165
{
    const unsigned char *p;
166
    RSA *rsa;
167
    int pklen;
168 169 170 171 172 173 174 175 176 177 178
    const X509_ALGOR *alg;

    if (!PKCS8_pkey_get0(NULL, &p, &pklen, &alg, p8))
        return 0;
    rsa = d2i_RSAPrivateKey(NULL, &p, pklen);
    if (rsa == NULL) {
        RSAerr(RSA_F_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
        return 0;
    }
    if (!rsa_param_decode(rsa, alg)) {
        RSA_free(rsa);
179
        return 0;
180 181 182
    }
    EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa);
    return 1;
183
}
184

185
static int int_rsa_size(const EVP_PKEY *pkey)
186 187 188
{
    return RSA_size(pkey->pkey.rsa);
}
189 190

static int rsa_bits(const EVP_PKEY *pkey)
191 192 193
{
    return BN_num_bits(pkey->pkey.rsa->n);
}
194

195
static int rsa_security_bits(const EVP_PKEY *pkey)
196 197 198
{
    return RSA_security_bits(pkey->pkey.rsa);
}
199

200
static void int_rsa_free(EVP_PKEY *pkey)
201 202 203
{
    RSA_free(pkey->pkey.rsa);
}
204

D
Dr. Stephen Henson 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
static X509_ALGOR *rsa_mgf1_decode(X509_ALGOR *alg)
{
    if (OBJ_obj2nid(alg->algorithm) != NID_mgf1)
        return NULL;
    return ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),
                                     alg->parameter);
}

static int rsa_pss_param_print(BIO *bp, int pss_key, RSA_PSS_PARAMS *pss,
                               int indent)
{
    int rv = 0;
    X509_ALGOR *maskHash = NULL;
    if (!BIO_indent(bp, indent, 128))
        goto err;
    if (pss_key) {
        if (pss == NULL) {
            if (BIO_puts(bp, "No PSS parameter restrictions\n") <= 0)
                return 0;
            return 1;
        } else {
            if (BIO_puts(bp, "PSS parameter restrictions:") <= 0)
                return 0;
        }
    } else if (pss == NULL) {
        if (BIO_puts(bp,"(INVALID PSS PARAMETERS)\n") <= 0)
            return 0;
        return 1;
    }
    if (BIO_puts(bp, "\n") <= 0)
        goto err;
    if (pss_key)
        indent += 2;
    if (!BIO_indent(bp, indent, 128))
        goto err;
    if (BIO_puts(bp, "Hash Algorithm: ") <= 0)
        goto err;

    if (pss->hashAlgorithm) {
        if (i2a_ASN1_OBJECT(bp, pss->hashAlgorithm->algorithm) <= 0)
            goto err;
    } else if (BIO_puts(bp, "sha1 (default)") <= 0)
        goto err;

    if (BIO_puts(bp, "\n") <= 0)
        goto err;

    if (!BIO_indent(bp, indent, 128))
        goto err;

    if (BIO_puts(bp, "Mask Algorithm: ") <= 0)
        goto err;
    if (pss->maskGenAlgorithm) {
        if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0)
            goto err;
        if (BIO_puts(bp, " with ") <= 0)
            goto err;
        maskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
        if (maskHash != NULL) {
            if (i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0)
                goto err;
        } else if (BIO_puts(bp, "INVALID") <= 0)
            goto err;
    } else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0)
        goto err;
    BIO_puts(bp, "\n");

    if (!BIO_indent(bp, indent, 128))
        goto err;
    if (BIO_printf(bp, "%s Salt Length: 0x", pss_key ? "Minimum" : "") <= 0)
        goto err;
    if (pss->saltLength) {
        if (i2a_ASN1_INTEGER(bp, pss->saltLength) <= 0)
            goto err;
    } else if (BIO_puts(bp, "14 (default)") <= 0)
        goto err;
    BIO_puts(bp, "\n");

    if (!BIO_indent(bp, indent, 128))
        goto err;
    if (BIO_puts(bp, "Trailer Field: 0x") <= 0)
        goto err;
    if (pss->trailerField) {
        if (i2a_ASN1_INTEGER(bp, pss->trailerField) <= 0)
            goto err;
    } else if (BIO_puts(bp, "BC (default)") <= 0)
        goto err;
    BIO_puts(bp, "\n");

    rv = 1;

 err:
    X509_ALGOR_free(maskHash);
    return rv;

}

static int pkey_rsa_print(BIO *bp, const EVP_PKEY *pkey, int off, int priv)
303
{
D
Dr. Stephen Henson 已提交
304
    const RSA *x = pkey->pkey.rsa;
305 306 307 308 309 310 311 312 313 314
    char *str;
    const char *s;
    int ret = 0, mod_len = 0;

    if (x->n != NULL)
        mod_len = BN_num_bits(x->n);

    if (!BIO_indent(bp, off, 128))
        goto err;

315
    if (BIO_printf(bp, "%s ", pkey_is_pss(pkey) ?  "RSA-PSS" : "RSA") <= 0)
D
Dr. Stephen Henson 已提交
316 317
        goto err;

318
    if (priv && x->d) {
319
        if (BIO_printf(bp, "Private-Key: (%d bit)\n", mod_len) <= 0)
320 321 322 323
            goto err;
        str = "modulus:";
        s = "publicExponent:";
    } else {
324
        if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0)
325 326 327 328
            goto err;
        str = "Modulus:";
        s = "Exponent:";
    }
329
    if (!ASN1_bn_print(bp, str, x->n, NULL, off))
330
        goto err;
331
    if (!ASN1_bn_print(bp, s, x->e, NULL, off))
332 333
        goto err;
    if (priv) {
334
        if (!ASN1_bn_print(bp, "privateExponent:", x->d, NULL, off))
335
            goto err;
336
        if (!ASN1_bn_print(bp, "prime1:", x->p, NULL, off))
337
            goto err;
338
        if (!ASN1_bn_print(bp, "prime2:", x->q, NULL, off))
339
            goto err;
340
        if (!ASN1_bn_print(bp, "exponent1:", x->dmp1, NULL, off))
341
            goto err;
342
        if (!ASN1_bn_print(bp, "exponent2:", x->dmq1, NULL, off))
343
            goto err;
344
        if (!ASN1_bn_print(bp, "coefficient:", x->iqmp, NULL, off))
345 346
            goto err;
    }
347
    if (pkey_is_pss(pkey) && !rsa_pss_param_print(bp, 1, x->pss, off))
D
Dr. Stephen Henson 已提交
348
        goto err;
349 350
    ret = 1;
 err:
D
Dr. Stephen Henson 已提交
351
    return ret;
352
}
353 354

static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
355 356
                         ASN1_PCTX *ctx)
{
D
Dr. Stephen Henson 已提交
357
    return pkey_rsa_print(bp, pkey, indent, 0);
358
}
359 360

static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
361 362
                          ASN1_PCTX *ctx)
{
D
Dr. Stephen Henson 已提交
363
    return pkey_rsa_print(bp, pkey, indent, 1);
364
}
365

D
Dr. Stephen Henson 已提交
366
static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg)
367 368 369
{
    RSA_PSS_PARAMS *pss;

D
Dr. Stephen Henson 已提交
370 371
    pss = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_PSS_PARAMS),
                                    alg->parameter);
372

D
Dr. Stephen Henson 已提交
373
    if (pss == NULL)
374 375
        return NULL;

D
Dr. Stephen Henson 已提交
376 377 378 379 380 381 382
    if (pss->maskGenAlgorithm != NULL) {
        pss->maskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
        if (pss->maskHash == NULL) {
            RSA_PSS_PARAMS_free(pss);
            return NULL;
        }
    }
383 384 385 386

    return pss;
}

387
static int rsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
388 389
                         const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
{
D
Dr. Stephen Henson 已提交
390
    if (OBJ_obj2nid(sigalg->algorithm) == EVP_PKEY_RSA_PSS) {
391 392
        int rv;
        RSA_PSS_PARAMS *pss;
D
Dr. Stephen Henson 已提交
393
        pss = rsa_pss_decode(sigalg);
D
Dr. Stephen Henson 已提交
394
        rv = rsa_pss_param_print(bp, 0, pss, indent);
R
Rich Salz 已提交
395
        RSA_PSS_PARAMS_free(pss);
396 397 398 399 400 401 402 403
        if (!rv)
            return 0;
    } else if (!sig && BIO_puts(bp, "\n") <= 0)
        return 0;
    if (sig)
        return X509_signature_dump(bp, sig, indent);
    return 1;
}
404 405

static int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
406 407 408 409 410 411 412 413 414 415
{
    X509_ALGOR *alg = NULL;
    switch (op) {

    case ASN1_PKEY_CTRL_PKCS7_SIGN:
        if (arg1 == 0)
            PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, NULL, &alg);
        break;

    case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
416 417
        if (pkey_is_pss(pkey))
            return -2;
418 419 420
        if (arg1 == 0)
            PKCS7_RECIP_INFO_get0_alg(arg2, &alg);
        break;
D
Dr. Stephen Henson 已提交
421
#ifndef OPENSSL_NO_CMS
422 423 424 425 426 427 428 429
    case ASN1_PKEY_CTRL_CMS_SIGN:
        if (arg1 == 0)
            return rsa_cms_sign(arg2);
        else if (arg1 == 1)
            return rsa_cms_verify(arg2);
        break;

    case ASN1_PKEY_CTRL_CMS_ENVELOPE:
430 431
        if (pkey_is_pss(pkey))
            return -2;
432 433 434 435 436 437 438
        if (arg1 == 0)
            return rsa_cms_encrypt(arg2);
        else if (arg1 == 1)
            return rsa_cms_decrypt(arg2);
        break;

    case ASN1_PKEY_CTRL_CMS_RI_TYPE:
439 440
        if (pkey_is_pss(pkey))
            return -2;
441 442
        *(int *)arg2 = CMS_RECIPINFO_TRANS;
        return 1;
D
Dr. Stephen Henson 已提交
443
#endif
444

445 446 447
    case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
        *(int *)arg2 = NID_sha256;
        return 1;
448

449 450
    default:
        return -2;
451

452
    }
453

454 455
    if (alg)
        X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
456

457
    return 1;
458

459
}
460

461 462
/* allocate and set algorithm ID from EVP_MD, default SHA1 */
static int rsa_md_to_algor(X509_ALGOR **palg, const EVP_MD *md)
463
{
464
    if (md == NULL || EVP_MD_type(md) == NID_sha1)
465 466
        return 1;
    *palg = X509_ALGOR_new();
467
    if (*palg == NULL)
468 469 470 471
        return 0;
    X509_ALGOR_set_md(*palg, md);
    return 1;
}
472 473 474

/* Allocate and set MGF1 algorithm ID from EVP_MD */
static int rsa_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md)
475 476 477 478
{
    X509_ALGOR *algtmp = NULL;
    ASN1_STRING *stmp = NULL;
    *palg = NULL;
479
    if (mgf1md == NULL || EVP_MD_type(mgf1md) == NID_sha1)
480 481 482 483 484 485 486
        return 1;
    /* need to embed algorithm ID inside another */
    if (!rsa_md_to_algor(&algtmp, mgf1md))
        goto err;
    if (!ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp))
         goto err;
    *palg = X509_ALGOR_new();
487
    if (*palg == NULL)
488 489 490 491
        goto err;
    X509_ALGOR_set0(*palg, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp);
    stmp = NULL;
 err:
R
Rich Salz 已提交
492
    ASN1_STRING_free(stmp);
R
Rich Salz 已提交
493
    X509_ALGOR_free(algtmp);
494 495 496 497
    if (*palg)
        return 1;
    return 0;
}
498 499 500

/* convert algorithm ID to EVP_MD, default SHA1 */
static const EVP_MD *rsa_algor_to_md(X509_ALGOR *alg)
501 502 503 504 505 506 507 508 509 510 511
{
    const EVP_MD *md;
    if (!alg)
        return EVP_sha1();
    md = EVP_get_digestbyobj(alg->algorithm);
    if (md == NULL)
        RSAerr(RSA_F_RSA_ALGOR_TO_MD, RSA_R_UNKNOWN_DIGEST);
    return md;
}

/*
512
 * Convert EVP_PKEY_CTX in PSS mode into corresponding algorithm parameter,
513
 * suitable for setting an AlgorithmIdentifier.
514 515
 */

516
static RSA_PSS_PARAMS *rsa_ctx_to_pss(EVP_PKEY_CTX *pkctx)
517 518 519
{
    const EVP_MD *sigmd, *mgf1md;
    EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(pkctx);
520
    int saltlen;
521
    if (EVP_PKEY_CTX_get_signature_md(pkctx, &sigmd) <= 0)
522
        return NULL;
523
    if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
524
        return NULL;
525
    if (!EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx, &saltlen))
526
        return NULL;
527 528 529 530 531 532 533
    if (saltlen == -1)
        saltlen = EVP_MD_size(sigmd);
    else if (saltlen == -2) {
        saltlen = EVP_PKEY_size(pk) - EVP_MD_size(sigmd) - 2;
        if (((EVP_PKEY_bits(pk) - 1) & 0x7) == 0)
            saltlen--;
    }
534 535 536 537 538 539 540 541

    return rsa_pss_params_create(sigmd, mgf1md, saltlen);
}

RSA_PSS_PARAMS *rsa_pss_params_create(const EVP_MD *sigmd,
                                      const EVP_MD *mgf1md, int saltlen)
{
    RSA_PSS_PARAMS *pss = RSA_PSS_PARAMS_new();
542
    if (pss == NULL)
543 544 545
        goto err;
    if (saltlen != 20) {
        pss->saltLength = ASN1_INTEGER_new();
546
        if (pss->saltLength == NULL)
547 548 549 550 551 552
            goto err;
        if (!ASN1_INTEGER_set(pss->saltLength, saltlen))
            goto err;
    }
    if (!rsa_md_to_algor(&pss->hashAlgorithm, sigmd))
        goto err;
553 554
    if (mgf1md == NULL)
            mgf1md = sigmd;
555 556
    if (!rsa_md_to_mgf1(&pss->maskGenAlgorithm, mgf1md))
        goto err;
557
    return pss;
558
 err:
R
Rich Salz 已提交
559
    RSA_PSS_PARAMS_free(pss);
560 561 562
    return NULL;
}

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
static ASN1_STRING *rsa_ctx_to_pss_string(EVP_PKEY_CTX *pkctx)
{
    RSA_PSS_PARAMS *pss = rsa_ctx_to_pss(pkctx);
    ASN1_STRING *os = NULL;
    if (pss == NULL)
        return NULL;

    if (!ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), &os)) {
        ASN1_STRING_free(os);
        os = NULL;
    }
    RSA_PSS_PARAMS_free(pss);
    return os;
}

578 579
/*
 * From PSS AlgorithmIdentifier set public key parameters. If pkey isn't NULL
F
FdaSilvaYY 已提交
580
 * then the EVP_MD_CTX is setup and initialised. If it is NULL parameters are
581
 * passed to pkctx instead.
582
 */
583

584
static int rsa_pss_to_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pkctx,
585 586 587 588 589 590 591
                          X509_ALGOR *sigalg, EVP_PKEY *pkey)
{
    int rv = -1;
    int saltlen;
    const EVP_MD *mgf1md = NULL, *md = NULL;
    RSA_PSS_PARAMS *pss;
    /* Sanity check: make sure it is PSS */
D
Dr. Stephen Henson 已提交
592
    if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
593 594 595 596
        RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
        return -1;
    }
    /* Decode PSS parameters */
D
Dr. Stephen Henson 已提交
597
    pss = rsa_pss_decode(sigalg);
598 599 600 601 602

    if (pss == NULL) {
        RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_INVALID_PSS_PARAMETERS);
        goto err;
    }
D
Dr. Stephen Henson 已提交
603
    mgf1md = rsa_algor_to_md(pss->maskHash);
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
    if (!mgf1md)
        goto err;
    md = rsa_algor_to_md(pss->hashAlgorithm);
    if (!md)
        goto err;

    if (pss->saltLength) {
        saltlen = ASN1_INTEGER_get(pss->saltLength);

        /*
         * Could perform more salt length sanity checks but the main RSA
         * routines will trap other invalid values anyway.
         */
        if (saltlen < 0) {
            RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_INVALID_SALT_LENGTH);
            goto err;
        }
    } else
        saltlen = 20;

    /*
     * low-level routines support only trailer field 0xbc (value 1) and
     * PKCS#1 says we should reject any other value anyway.
     */
    if (pss->trailerField && ASN1_INTEGER_get(pss->trailerField) != 1) {
        RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_INVALID_TRAILER);
        goto err;
    }

    /* We have all parameters now set up context */

    if (pkey) {
        if (!EVP_DigestVerifyInit(ctx, &pkctx, md, NULL, pkey))
            goto err;
    } else {
        const EVP_MD *checkmd;
        if (EVP_PKEY_CTX_get_signature_md(pkctx, &checkmd) <= 0)
            goto err;
        if (EVP_MD_type(md) != EVP_MD_type(checkmd)) {
            RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_DIGEST_DOES_NOT_MATCH);
            goto err;
        }
    }

    if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_PSS_PADDING) <= 0)
        goto err;

    if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx, saltlen) <= 0)
        goto err;

    if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
        goto err;
    /* Carry on */
    rv = 1;

 err:
    RSA_PSS_PARAMS_free(pss);
    return rv;
}
663

D
David Bar 已提交
664
#ifndef OPENSSL_NO_CMS
665
static int rsa_cms_verify(CMS_SignerInfo *si)
666 667 668 669 670 671 672 673
{
    int nid, nid2;
    X509_ALGOR *alg;
    EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
    CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
    nid = OBJ_obj2nid(alg->algorithm);
    if (nid == NID_rsaEncryption)
        return 1;
D
Dr. Stephen Henson 已提交
674
    if (nid == EVP_PKEY_RSA_PSS)
675 676 677 678 679 680 681 682
        return rsa_pss_to_ctx(NULL, pkctx, alg, NULL);
    /* Workaround for some implementation that use a signature OID */
    if (OBJ_find_sigid_algs(nid, NULL, &nid2)) {
        if (nid2 == NID_rsaEncryption)
            return 1;
    }
    return 0;
}
D
David Bar 已提交
683
#endif
684 685 686 687

/*
 * Customised RSA item verification routine. This is called when a signature
 * is encountered requiring special handling. We currently only handle PSS.
688 689 690
 */

static int rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
691 692 693 694
                           X509_ALGOR *sigalg, ASN1_BIT_STRING *sig,
                           EVP_PKEY *pkey)
{
    /* Sanity check: make sure it is PSS */
D
Dr. Stephen Henson 已提交
695
    if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
696 697 698
        RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
        return -1;
    }
699
    if (rsa_pss_to_ctx(ctx, NULL, sigalg, pkey) > 0) {
700 701
        /* Carry on */
        return 2;
702
    }
703 704
    return -1;
}
705

D
David Bar 已提交
706
#ifndef OPENSSL_NO_CMS
707
static int rsa_cms_sign(CMS_SignerInfo *si)
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
{
    int pad_mode = RSA_PKCS1_PADDING;
    X509_ALGOR *alg;
    EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
    ASN1_STRING *os = NULL;
    CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
    if (pkctx) {
        if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
            return 0;
    }
    if (pad_mode == RSA_PKCS1_PADDING) {
        X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
        return 1;
    }
    /* We don't support it */
    if (pad_mode != RSA_PKCS1_PSS_PADDING)
        return 0;
725
    os = rsa_ctx_to_pss_string(pkctx);
726 727
    if (!os)
        return 0;
D
Dr. Stephen Henson 已提交
728
    X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_PKEY_RSA_PSS), V_ASN1_SEQUENCE, os);
729 730
    return 1;
}
D
David Bar 已提交
731
#endif
732

D
Dr. Stephen Henson 已提交
733
static int rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
734 735 736 737
                         X509_ALGOR *alg1, X509_ALGOR *alg2,
                         ASN1_BIT_STRING *sig)
{
    int pad_mode;
738
    EVP_PKEY_CTX *pkctx = EVP_MD_CTX_pkey_ctx(ctx);
739 740 741 742 743 744
    if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
        return 0;
    if (pad_mode == RSA_PKCS1_PADDING)
        return 2;
    if (pad_mode == RSA_PKCS1_PSS_PADDING) {
        ASN1_STRING *os1 = NULL;
745
        os1 = rsa_ctx_to_pss_string(pkctx);
746 747 748 749 750 751 752 753 754
        if (!os1)
            return 0;
        /* Duplicate parameters if we have to */
        if (alg2) {
            ASN1_STRING *os2 = ASN1_STRING_dup(os1);
            if (!os2) {
                ASN1_STRING_free(os1);
                return 0;
            }
D
Dr. Stephen Henson 已提交
755
            X509_ALGOR_set0(alg2, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
756 757
                            V_ASN1_SEQUENCE, os2);
        }
D
Dr. Stephen Henson 已提交
758
        X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
759 760 761 762 763
                        V_ASN1_SEQUENCE, os1);
        return 3;
    }
    return 2;
}
D
Dr. Stephen Henson 已提交
764

D
David Bar 已提交
765
#ifndef OPENSSL_NO_CMS
D
Dr. Stephen Henson 已提交
766
static RSA_OAEP_PARAMS *rsa_oaep_decode(const X509_ALGOR *alg)
767
{
D
Dr. Stephen Henson 已提交
768
    RSA_OAEP_PARAMS *oaep;
769

D
Dr. Stephen Henson 已提交
770
    oaep = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_OAEP_PARAMS),
D
Dr. Stephen Henson 已提交
771
                                    alg->parameter);
772

D
Dr. Stephen Henson 已提交
773
    if (oaep == NULL)
774
        return NULL;
775

D
Dr. Stephen Henson 已提交
776 777 778 779 780 781 782 783
    if (oaep->maskGenFunc != NULL) {
        oaep->maskHash = rsa_mgf1_decode(oaep->maskGenFunc);
        if (oaep->maskHash == NULL) {
            RSA_OAEP_PARAMS_free(oaep);
            return NULL;
        }
    }
    return oaep;
784
}
785 786

static int rsa_cms_decrypt(CMS_RecipientInfo *ri)
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
{
    EVP_PKEY_CTX *pkctx;
    X509_ALGOR *cmsalg;
    int nid;
    int rv = -1;
    unsigned char *label = NULL;
    int labellen = 0;
    const EVP_MD *mgf1md = NULL, *md = NULL;
    RSA_OAEP_PARAMS *oaep;
    pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
    if (!pkctx)
        return 0;
    if (!CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &cmsalg))
        return -1;
    nid = OBJ_obj2nid(cmsalg->algorithm);
    if (nid == NID_rsaEncryption)
        return 1;
    if (nid != NID_rsaesOaep) {
        RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_ENCRYPTION_TYPE);
        return -1;
    }
    /* Decode OAEP parameters */
D
Dr. Stephen Henson 已提交
809
    oaep = rsa_oaep_decode(cmsalg);
810 811 812 813 814 815

    if (oaep == NULL) {
        RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_OAEP_PARAMETERS);
        goto err;
    }

D
Dr. Stephen Henson 已提交
816
    mgf1md = rsa_algor_to_md(oaep->maskHash);
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
    if (!mgf1md)
        goto err;
    md = rsa_algor_to_md(oaep->hashFunc);
    if (!md)
        goto err;

    if (oaep->pSourceFunc) {
        X509_ALGOR *plab = oaep->pSourceFunc;
        if (OBJ_obj2nid(plab->algorithm) != NID_pSpecified) {
            RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_LABEL_SOURCE);
            goto err;
        }
        if (plab->parameter->type != V_ASN1_OCTET_STRING) {
            RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_LABEL);
            goto err;
        }

        label = plab->parameter->value.octet_string->data;
        /* Stop label being freed when OAEP parameters are freed */
        plab->parameter->value.octet_string->data = NULL;
        labellen = plab->parameter->value.octet_string->length;
    }

    if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0)
        goto err;
    if (EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, md) <= 0)
        goto err;
    if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
        goto err;
    if (EVP_PKEY_CTX_set0_rsa_oaep_label(pkctx, label, labellen) <= 0)
        goto err;
    /* Carry on */
    rv = 1;

 err:
    RSA_OAEP_PARAMS_free(oaep);
    return rv;
}
855 856

static int rsa_cms_encrypt(CMS_RecipientInfo *ri)
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
{
    const EVP_MD *md, *mgf1md;
    RSA_OAEP_PARAMS *oaep = NULL;
    ASN1_STRING *os = NULL;
    X509_ALGOR *alg;
    EVP_PKEY_CTX *pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
    int pad_mode = RSA_PKCS1_PADDING, rv = 0, labellen;
    unsigned char *label;
    CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &alg);
    if (pkctx) {
        if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
            return 0;
    }
    if (pad_mode == RSA_PKCS1_PADDING) {
        X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
        return 1;
    }
    /* Not supported */
    if (pad_mode != RSA_PKCS1_OAEP_PADDING)
        return 0;
    if (EVP_PKEY_CTX_get_rsa_oaep_md(pkctx, &md) <= 0)
        goto err;
    if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
        goto err;
    labellen = EVP_PKEY_CTX_get0_rsa_oaep_label(pkctx, &label);
    if (labellen < 0)
        goto err;
    oaep = RSA_OAEP_PARAMS_new();
885
    if (oaep == NULL)
886 887 888 889 890 891
        goto err;
    if (!rsa_md_to_algor(&oaep->hashFunc, md))
        goto err;
    if (!rsa_md_to_mgf1(&oaep->maskGenFunc, mgf1md))
        goto err;
    if (labellen > 0) {
892
        ASN1_OCTET_STRING *los;
893
        oaep->pSourceFunc = X509_ALGOR_new();
894
        if (oaep->pSourceFunc == NULL)
895
            goto err;
896
        los = ASN1_OCTET_STRING_new();
897
        if (los == NULL)
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
            goto err;
        if (!ASN1_OCTET_STRING_set(los, label, labellen)) {
            ASN1_OCTET_STRING_free(los);
            goto err;
        }
        X509_ALGOR_set0(oaep->pSourceFunc, OBJ_nid2obj(NID_pSpecified),
                        V_ASN1_OCTET_STRING, los);
    }
    /* create string with pss parameter encoding. */
    if (!ASN1_item_pack(oaep, ASN1_ITEM_rptr(RSA_OAEP_PARAMS), &os))
         goto err;
    X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaesOaep), V_ASN1_SEQUENCE, os);
    os = NULL;
    rv = 1;
 err:
R
Rich Salz 已提交
913
    RSA_OAEP_PARAMS_free(oaep);
R
Rich Salz 已提交
914
    ASN1_STRING_free(os);
915 916
    return rv;
}
D
David Bar 已提交
917
#endif
918

K
Kurt Roeckx 已提交
919
const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[2] = {
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
    {
     EVP_PKEY_RSA,
     EVP_PKEY_RSA,
     ASN1_PKEY_SIGPARAM_NULL,

     "RSA",
     "OpenSSL RSA method",

     rsa_pub_decode,
     rsa_pub_encode,
     rsa_pub_cmp,
     rsa_pub_print,

     rsa_priv_decode,
     rsa_priv_encode,
     rsa_priv_print,

     int_rsa_size,
     rsa_bits,
     rsa_security_bits,

     0, 0, 0, 0, 0, 0,

     rsa_sig_print,
     int_rsa_free,
     rsa_pkey_ctrl,
     old_rsa_priv_decode,
     old_rsa_priv_encode,
     rsa_item_verify,
     rsa_item_sign},

    {
     EVP_PKEY_RSA2,
     EVP_PKEY_RSA,
     ASN1_PKEY_ALIAS}
};
D
Dr. Stephen Henson 已提交
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986

const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth = {
     EVP_PKEY_RSA_PSS,
     EVP_PKEY_RSA_PSS,
     ASN1_PKEY_SIGPARAM_NULL,

     "RSA-PSS",
     "OpenSSL RSA-PSS method",

     rsa_pub_decode,
     rsa_pub_encode,
     rsa_pub_cmp,
     rsa_pub_print,

     rsa_priv_decode,
     rsa_priv_encode,
     rsa_priv_print,

     int_rsa_size,
     rsa_bits,
     rsa_security_bits,

     0, 0, 0, 0, 0, 0,

     rsa_sig_print,
     int_rsa_free,
     rsa_pkey_ctrl,
     0, 0,
     rsa_item_verify,
     rsa_item_sign,
};