dsa_ameth.c 15.2 KB
Newer Older
1
/*
R
Rich Salz 已提交
2
 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
R
Rich Salz 已提交
5 6 7
 * 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
 */

P
Pauli 已提交
10 11 12 13 14 15
/*
 * DSA low level APIs are deprecated for public use, but still ok for
 * internal use.
 */
#include "internal/deprecated.h"

16 17 18
#include <stdio.h>
#include <openssl/x509.h>
#include <openssl/asn1.h>
19
#include <openssl/bn.h>
R
Rich Salz 已提交
20
#include <openssl/cms.h>
21 22
#include <openssl/core_names.h>
#include "internal/cryptlib.h"
23 24
#include "crypto/asn1.h"
#include "crypto/evp.h"
25
#include "internal/param_build.h"
26
#include "dsa_local.h"
27 28

static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
29 30 31 32
{
    const unsigned char *p, *pm;
    int pklen, pmlen;
    int ptype;
D
Dr. Stephen Henson 已提交
33 34
    const void *pval;
    const ASN1_STRING *pstr;
35 36 37 38 39 40 41 42 43 44 45 46 47 48
    X509_ALGOR *palg;
    ASN1_INTEGER *public_key = NULL;

    DSA *dsa = NULL;

    if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
        return 0;
    X509_ALGOR_get0(NULL, &ptype, &pval, palg);

    if (ptype == V_ASN1_SEQUENCE) {
        pstr = pval;
        pm = pstr->data;
        pmlen = pstr->length;

49
        if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL) {
50 51 52 53 54
            DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
            goto err;
        }

    } else if ((ptype == V_ASN1_NULL) || (ptype == V_ASN1_UNDEF)) {
55
        if ((dsa = DSA_new()) == NULL) {
56 57 58 59 60 61 62 63
            DSAerr(DSA_F_DSA_PUB_DECODE, ERR_R_MALLOC_FAILURE);
            goto err;
        }
    } else {
        DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_PARAMETER_ENCODING_ERROR);
        goto err;
    }

64
    if ((public_key = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) {
65 66 67 68
        DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
        goto err;
    }

69
    if ((dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL) {
70 71 72 73
        DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_BN_DECODE_ERROR);
        goto err;
    }

74
    dsa->dirty_cnt++;
75 76 77 78 79
    ASN1_INTEGER_free(public_key);
    EVP_PKEY_assign_DSA(pkey, dsa);
    return 1;

 err:
R
Rich Salz 已提交
80
    ASN1_INTEGER_free(public_key);
R
Rich Salz 已提交
81
    DSA_free(dsa);
82 83 84
    return 0;

}
85

86
static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
87 88 89 90 91
{
    DSA *dsa;
    int ptype;
    unsigned char *penc = NULL;
    int penclen;
M
Matt Caswell 已提交
92
    ASN1_STRING *str = NULL;
93
    ASN1_INTEGER *pubint = NULL;
94
    ASN1_OBJECT *aobj;
95 96

    dsa = pkey->pkey.dsa;
97 98 99 100
    if (pkey->save_parameters
        && dsa->params.p != NULL
        && dsa->params.q != NULL
        && dsa->params.g != NULL) {
101
        str = ASN1_STRING_new();
102
        if (str == NULL) {
M
Matt Caswell 已提交
103 104 105
            DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
            goto err;
        }
106 107 108 109 110 111 112 113 114
        str->length = i2d_DSAparams(dsa, &str->data);
        if (str->length <= 0) {
            DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
            goto err;
        }
        ptype = V_ASN1_SEQUENCE;
    } else
        ptype = V_ASN1_UNDEF;

115
    pubint = BN_to_ASN1_INTEGER(dsa->pub_key, NULL);
116

117 118 119 120 121 122 123
    if (pubint == NULL) {
        DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
        goto err;
    }

    penclen = i2d_ASN1_INTEGER(pubint, &penc);
    ASN1_INTEGER_free(pubint);
124 125 126 127 128 129

    if (penclen <= 0) {
        DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
        goto err;
    }

130 131 132 133 134
    aobj = OBJ_nid2obj(EVP_PKEY_DSA);
    if (aobj == NULL)
        goto err;

    if (X509_PUBKEY_set0_param(pk, aobj, ptype, str, penc, penclen))
135 136 137
        return 1;

 err:
R
Rich Salz 已提交
138
    OPENSSL_free(penc);
R
Rich Salz 已提交
139
    ASN1_STRING_free(str);
140 141 142 143 144 145

    return 0;
}

/*
 * In PKCS#8 DSA: you just get a private key integer and parameters in the
146 147
 * AlgorithmIdentifier the pubkey must be recalculated.
 */
148

149
static int dsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
150
{
151
    const unsigned char *p, *pm;
152 153
    int pklen, pmlen;
    int ptype;
D
Dr. Stephen Henson 已提交
154 155
    const void *pval;
    const ASN1_STRING *pstr;
156
    const X509_ALGOR *palg;
157 158 159 160 161
    ASN1_INTEGER *privkey = NULL;
    BN_CTX *ctx = NULL;

    DSA *dsa = NULL;

162 163
    int ret = 0;

164 165 166 167
    if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
        return 0;
    X509_ALGOR_get0(NULL, &ptype, &pval, palg);

168 169
    if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
        goto decerr;
170
    if (privkey->type == V_ASN1_NEG_INTEGER || ptype != V_ASN1_SEQUENCE)
171
        goto decerr;
172 173 174 175

    pstr = pval;
    pm = pstr->data;
    pmlen = pstr->length;
176
    if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL)
177 178
        goto decerr;
    /* We have parameters now set private key */
R
Rich Salz 已提交
179 180
    if ((dsa->priv_key = BN_secure_new()) == NULL
        || !ASN1_INTEGER_to_BN(privkey, dsa->priv_key)) {
181 182 183 184
        DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
        goto dsaerr;
    }
    /* Calculate public key */
185
    if ((dsa->pub_key = BN_new()) == NULL) {
186 187 188
        DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
        goto dsaerr;
    }
189
    if ((ctx = BN_CTX_new()) == NULL) {
190 191 192 193
        DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
        goto dsaerr;
    }

194
    BN_set_flags(dsa->priv_key, BN_FLG_CONSTTIME);
195 196
    if (!BN_mod_exp(dsa->pub_key, dsa->params.g, dsa->priv_key, dsa->params.p,
                    ctx)) {
197 198 199 200
        DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
        goto dsaerr;
    }

201
    dsa->dirty_cnt++;
202 203
    EVP_PKEY_assign_DSA(pkey, dsa);

204 205
    ret = 1;
    goto done;
206 207

 decerr:
D
typo  
Dr. Stephen Henson 已提交
208
    DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_DECODE_ERROR);
209
 dsaerr:
210 211
    DSA_free(dsa);
 done:
212
    BN_CTX_free(ctx);
R
Rich Salz 已提交
213
    ASN1_STRING_clear_free(privkey);
214
    return ret;
215
}
216

217
static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
218
{
219 220 221 222 223
    ASN1_STRING *params = NULL;
    ASN1_INTEGER *prkey = NULL;
    unsigned char *dp = NULL;
    int dplen;

224
    if (pkey->pkey.dsa  == NULL|| pkey->pkey.dsa->priv_key == NULL) {
225 226 227 228 229 230
        DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_MISSING_PARAMETERS);
        goto err;
    }

    params = ASN1_STRING_new();

231
    if (params == NULL) {
232 233 234 235 236 237 238 239 240 241 242 243 244 245
        DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
        goto err;
    }

    params->length = i2d_DSAparams(pkey->pkey.dsa, &params->data);
    if (params->length <= 0) {
        DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
        goto err;
    }
    params->type = V_ASN1_SEQUENCE;

    /* Get private key into integer */
    prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);

246
    if (prkey == NULL) {
247 248 249 250 251 252
        DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_BN_ERROR);
        goto err;
    }

    dplen = i2d_ASN1_INTEGER(prkey, &dp);

253
    ASN1_STRING_clear_free(prkey);
M
Martin Vejnar 已提交
254
    prkey = NULL;
255 256 257 258 259 260 261 262

    if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0,
                         V_ASN1_SEQUENCE, params, dp, dplen))
        goto err;

    return 1;

 err:
R
Rich Salz 已提交
263
    OPENSSL_free(dp);
R
Rich Salz 已提交
264
    ASN1_STRING_free(params);
R
Rich Salz 已提交
265
    ASN1_STRING_clear_free(prkey);
266
    return 0;
267 268
}

269
static int int_dsa_size(const EVP_PKEY *pkey)
270
{
K
KaoruToda 已提交
271
    return DSA_size(pkey->pkey.dsa);
272
}
273 274

static int dsa_bits(const EVP_PKEY *pkey)
275
{
D
Dr. Stephen Henson 已提交
276
    return DSA_bits(pkey->pkey.dsa);
277
}
278

279
static int dsa_security_bits(const EVP_PKEY *pkey)
280 281 282
{
    return DSA_security_bits(pkey->pkey.dsa);
}
283

284
static int dsa_missing_parameters(const EVP_PKEY *pkey)
285 286 287
{
    DSA *dsa;
    dsa = pkey->pkey.dsa;
288 289 290 291
    return dsa == NULL
        || dsa->params.p == NULL
        || dsa->params.q == NULL
        || dsa->params.g == NULL;
292
}
293 294

static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
295
{
296 297 298 299 300
    if (to->pkey.dsa == NULL) {
        to->pkey.dsa = DSA_new();
        if (to->pkey.dsa == NULL)
            return 0;
    }
301
    if (!ffc_params_copy(&to->pkey.dsa->params, &from->pkey.dsa->params))
302 303
        return 0;

304
    to->pkey.dsa->dirty_cnt++;
305 306
    return 1;
}
307 308

static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
309
{
310
    return ffc_params_cmp(&a->pkey.dsa->params, &b->pkey.dsa->params, 1);
311
}
312

313
static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
314
{
315
    return BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) == 0;
316
}
317

318
static void int_dsa_free(EVP_PKEY *pkey)
319 320 321
{
    DSA_free(pkey->pkey.dsa);
}
322

323
static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype)
324 325 326 327
{
    int ret = 0;
    const char *ktype = NULL;
    const BIGNUM *priv_key, *pub_key;
328 329
    int mod_len = 0;

330 331
    if (x->params.p != NULL)
        mod_len = DSA_bits(x);
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

    if (ptype == 2)
        priv_key = x->priv_key;
    else
        priv_key = NULL;

    if (ptype > 0)
        pub_key = x->pub_key;
    else
        pub_key = NULL;

    if (ptype == 2)
        ktype = "Private-Key";
    else if (ptype == 1)
        ktype = "Public-Key";
    else
        ktype = "DSA-Parameters";

350
    if (priv_key != NULL) {
351 352
        if (!BIO_indent(bp, off, 128))
            goto err;
353
        if (BIO_printf(bp, "%s: (%d bit)\n", ktype, mod_len) <= 0)
354
            goto err;
355 356 357
    } else {
        if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0)
            goto err;
358 359
    }

360
    if (!ASN1_bn_print(bp, "priv:", priv_key, NULL, off))
361
        goto err;
362
    if (!ASN1_bn_print(bp, "pub: ", pub_key, NULL, off))
363
        goto err;
364
    if (!ffc_params_print(bp, &x->params, off))
365 366 367
        goto err;
    ret = 1;
 err:
K
KaoruToda 已提交
368
    return ret;
369
}
370

371
static int dsa_param_decode(EVP_PKEY *pkey,
372 373 374
                            const unsigned char **pder, int derlen)
{
    DSA *dsa;
375 376

    if ((dsa = d2i_DSAparams(NULL, pder, derlen)) == NULL) {
377 378 379
        DSAerr(DSA_F_DSA_PARAM_DECODE, ERR_R_DSA_LIB);
        return 0;
    }
380
    dsa->dirty_cnt++;
381 382 383
    EVP_PKEY_assign_DSA(pkey, dsa);
    return 1;
}
384 385

static int dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
386 387 388
{
    return i2d_DSAparams(pkey->pkey.dsa, pder);
}
389 390

static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
391 392 393 394
                           ASN1_PCTX *ctx)
{
    return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
}
395 396

static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
397 398 399 400
                         ASN1_PCTX *ctx)
{
    return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
}
401 402

static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
403 404 405 406
                          ASN1_PCTX *ctx)
{
    return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
}
407

408
static int old_dsa_priv_decode(EVP_PKEY *pkey,
409 410 411
                               const unsigned char **pder, int derlen)
{
    DSA *dsa;
412 413

    if ((dsa = d2i_DSAPrivateKey(NULL, pder, derlen)) == NULL) {
414 415 416
        DSAerr(DSA_F_OLD_DSA_PRIV_DECODE, ERR_R_DSA_LIB);
        return 0;
    }
417
    dsa->dirty_cnt++;
418 419 420
    EVP_PKEY_assign_DSA(pkey, dsa);
    return 1;
}
421 422

static int old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
423 424 425
{
    return i2d_DSAPrivateKey(pkey->pkey.dsa, pder);
}
426

427
static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
428 429 430 431
                         const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
{
    DSA_SIG *dsa_sig;
    const unsigned char *p;
432

433
    if (sig == NULL) {
434 435 436 437 438 439 440
        if (BIO_puts(bp, "\n") <= 0)
            return 0;
        else
            return 1;
    }
    p = sig->data;
    dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
441
    if (dsa_sig != NULL) {
442
        int rv = 0;
443
        const BIGNUM *r, *s;
D
Dr. Stephen Henson 已提交
444

445
        DSA_SIG_get0(dsa_sig, &r, &s);
446 447 448 449

        if (BIO_write(bp, "\n", 1) != 1)
            goto err;

D
Dr. Stephen Henson 已提交
450
        if (!ASN1_bn_print(bp, "r:   ", r, NULL, indent))
451
            goto err;
D
Dr. Stephen Henson 已提交
452
        if (!ASN1_bn_print(bp, "s:   ", s, NULL, indent))
453 454 455 456 457 458
            goto err;
        rv = 1;
 err:
        DSA_SIG_free(dsa_sig);
        return rv;
    }
459 460
    if (BIO_puts(bp, "\n") <= 0)
        return 0;
461 462
    return X509_signature_dump(bp, sig, indent);
}
463

464
static int dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
{
    switch (op) {
    case ASN1_PKEY_CTRL_PKCS7_SIGN:
        if (arg1 == 0) {
            int snid, hnid;
            X509_ALGOR *alg1, *alg2;
            PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
            if (alg1 == NULL || alg1->algorithm == NULL)
                return -1;
            hnid = OBJ_obj2nid(alg1->algorithm);
            if (hnid == NID_undef)
                return -1;
            if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
                return -1;
            X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
        }
        return 1;
D
Dr. Stephen Henson 已提交
482
#ifndef OPENSSL_NO_CMS
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
    case ASN1_PKEY_CTRL_CMS_SIGN:
        if (arg1 == 0) {
            int snid, hnid;
            X509_ALGOR *alg1, *alg2;
            CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
            if (alg1 == NULL || alg1->algorithm == NULL)
                return -1;
            hnid = OBJ_obj2nid(alg1->algorithm);
            if (hnid == NID_undef)
                return -1;
            if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
                return -1;
            X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
        }
        return 1;

    case ASN1_PKEY_CTRL_CMS_RI_TYPE:
        *(int *)arg2 = CMS_RECIPINFO_NONE;
        return 1;
D
Dr. Stephen Henson 已提交
502
#endif
503

504 505
    case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
        *(int *)arg2 = NID_sha256;
506
        return 1;
507

508 509
    default:
        return -2;
510

511
    }
512

513
}
514

515 516 517 518 519
static size_t dsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
{
    return pkey->pkey.dsa->dirty_cnt;
}

520 521
static int dsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
                              EVP_KEYMGMT *to_keymgmt)
522
{
523
    DSA *dsa = from->pkey.dsa;
524 525 526 527 528
    OSSL_PARAM_BLD tmpl;
    const BIGNUM *p = DSA_get0_p(dsa), *g = DSA_get0_g(dsa);
    const BIGNUM *q = DSA_get0_q(dsa), *pub_key = DSA_get0_pub_key(dsa);
    const BIGNUM *priv_key = DSA_get0_priv_key(dsa);
    OSSL_PARAM *params;
529
    int rv;
530 531

    if (p == NULL || q == NULL || g == NULL)
532
        return 0;
533 534 535 536 537

    ossl_param_bld_init(&tmpl);
    if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_FFC_P, p)
        || !ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_FFC_Q, q)
        || !ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_FFC_G, g))
538
        return 0;
539
    if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_PUB_KEY,
540 541 542
                                pub_key))
        return 0;
    if (priv_key != NULL) {
543
        if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_PRIV_KEY,
544 545
                                    priv_key))
            return 0;
546 547
    }

548 549
    if ((params = ossl_param_bld_to_param(&tmpl)) == NULL)
        return 0;
550 551

    /* We export, the provider imports */
552 553
    rv = evp_keymgmt_import(to_keymgmt, to_keydata, OSSL_KEYMGMT_SELECT_ALL,
                            params);
554 555

    ossl_param_bld_free(params);
556 557

    return rv;
558 559
}

560 561
/* NB these are sorted in pkey_id order, lowest first */

K
Kurt Roeckx 已提交
562
const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[5] = {
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615

    {
     EVP_PKEY_DSA2,
     EVP_PKEY_DSA,
     ASN1_PKEY_ALIAS},

    {
     EVP_PKEY_DSA1,
     EVP_PKEY_DSA,
     ASN1_PKEY_ALIAS},

    {
     EVP_PKEY_DSA4,
     EVP_PKEY_DSA,
     ASN1_PKEY_ALIAS},

    {
     EVP_PKEY_DSA3,
     EVP_PKEY_DSA,
     ASN1_PKEY_ALIAS},

    {
     EVP_PKEY_DSA,
     EVP_PKEY_DSA,
     0,

     "DSA",
     "OpenSSL DSA method",

     dsa_pub_decode,
     dsa_pub_encode,
     dsa_pub_cmp,
     dsa_pub_print,

     dsa_priv_decode,
     dsa_priv_encode,
     dsa_priv_print,

     int_dsa_size,
     dsa_bits,
     dsa_security_bits,

     dsa_param_decode,
     dsa_param_encode,
     dsa_missing_parameters,
     dsa_copy_parameters,
     dsa_cmp_parameters,
     dsa_param_print,
     dsa_sig_print,

     int_dsa_free,
     dsa_pkey_ctrl,
     old_dsa_priv_decode,
616 617 618 619 620 621 622 623 624
     old_dsa_priv_encode,

     NULL, NULL, NULL,
     NULL, NULL, NULL,
     NULL, NULL, NULL, NULL,

     dsa_pkey_dirty_cnt,
     dsa_pkey_export_to
    }
625
};