Coverage Report

Created: 2023-09-25 06:45

/src/openssl111/crypto/dsa/dsa_ameth.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2006-2019 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/x509.h>
13
#include <openssl/asn1.h>
14
#include "dsa_local.h"
15
#include <openssl/bn.h>
16
#include <openssl/cms.h>
17
#include "crypto/asn1.h"
18
#include "crypto/evp.h"
19
20
static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
21
78.8k
{
22
78.8k
    const unsigned char *p, *pm;
23
78.8k
    int pklen, pmlen;
24
78.8k
    int ptype;
25
78.8k
    const void *pval;
26
78.8k
    const ASN1_STRING *pstr;
27
78.8k
    X509_ALGOR *palg;
28
78.8k
    ASN1_INTEGER *public_key = NULL;
29
30
78.8k
    DSA *dsa = NULL;
31
32
78.8k
    if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
33
0
        return 0;
34
78.8k
    X509_ALGOR_get0(NULL, &ptype, &pval, palg);
35
36
78.8k
    if (ptype == V_ASN1_SEQUENCE) {
37
33.1k
        pstr = pval;
38
33.1k
        pm = pstr->data;
39
33.1k
        pmlen = pstr->length;
40
41
33.1k
        if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL) {
42
8.29k
            DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
43
8.29k
            goto err;
44
8.29k
        }
45
46
45.6k
    } else if ((ptype == V_ASN1_NULL) || (ptype == V_ASN1_UNDEF)) {
47
18.5k
        if ((dsa = DSA_new()) == NULL) {
48
0
            DSAerr(DSA_F_DSA_PUB_DECODE, ERR_R_MALLOC_FAILURE);
49
0
            goto err;
50
0
        }
51
27.1k
    } else {
52
27.1k
        DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_PARAMETER_ENCODING_ERROR);
53
27.1k
        goto err;
54
27.1k
    }
55
56
43.3k
    if ((public_key = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) {
57
17.0k
        DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
58
17.0k
        goto err;
59
17.0k
    }
60
61
26.3k
    if ((dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL) {
62
0
        DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_BN_DECODE_ERROR);
63
0
        goto err;
64
0
    }
65
66
26.3k
    ASN1_INTEGER_free(public_key);
67
26.3k
    EVP_PKEY_assign_DSA(pkey, dsa);
68
26.3k
    return 1;
69
70
52.5k
 err:
71
52.5k
    ASN1_INTEGER_free(public_key);
72
52.5k
    DSA_free(dsa);
73
52.5k
    return 0;
74
75
26.3k
}
76
77
static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
78
0
{
79
0
    DSA *dsa;
80
0
    int ptype;
81
0
    unsigned char *penc = NULL;
82
0
    int penclen;
83
0
    ASN1_STRING *str = NULL;
84
0
    ASN1_INTEGER *pubint = NULL;
85
0
    ASN1_OBJECT *aobj;
86
87
0
    dsa = pkey->pkey.dsa;
88
0
    if (pkey->save_parameters && dsa->p && dsa->q && dsa->g) {
89
0
        str = ASN1_STRING_new();
90
0
        if (str == NULL) {
91
0
            DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
92
0
            goto err;
93
0
        }
94
0
        str->length = i2d_DSAparams(dsa, &str->data);
95
0
        if (str->length <= 0) {
96
0
            DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
97
0
            goto err;
98
0
        }
99
0
        ptype = V_ASN1_SEQUENCE;
100
0
    } else
101
0
        ptype = V_ASN1_UNDEF;
102
103
0
    pubint = BN_to_ASN1_INTEGER(dsa->pub_key, NULL);
104
105
0
    if (pubint == NULL) {
106
0
        DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
107
0
        goto err;
108
0
    }
109
110
0
    penclen = i2d_ASN1_INTEGER(pubint, &penc);
111
0
    ASN1_INTEGER_free(pubint);
112
113
0
    if (penclen <= 0) {
114
0
        DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
115
0
        goto err;
116
0
    }
117
118
0
    aobj = OBJ_nid2obj(EVP_PKEY_DSA);
119
0
    if (aobj == NULL)
120
0
        goto err;
121
122
0
    if (X509_PUBKEY_set0_param(pk, aobj, ptype, str, penc, penclen))
123
0
        return 1;
124
125
0
 err:
126
0
    OPENSSL_free(penc);
127
0
    ASN1_STRING_free(str);
128
129
0
    return 0;
130
0
}
131
132
/*
133
 * In PKCS#8 DSA: you just get a private key integer and parameters in the
134
 * AlgorithmIdentifier the pubkey must be recalculated.
135
 */
136
137
static int dsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
138
153
{
139
153
    const unsigned char *p, *pm;
140
153
    int pklen, pmlen;
141
153
    int ptype;
142
153
    const void *pval;
143
153
    const ASN1_STRING *pstr;
144
153
    const X509_ALGOR *palg;
145
153
    ASN1_INTEGER *privkey = NULL;
146
153
    BN_CTX *ctx = NULL;
147
148
153
    DSA *dsa = NULL;
149
150
153
    int ret = 0;
151
152
153
    if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
153
0
        return 0;
154
153
    X509_ALGOR_get0(NULL, &ptype, &pval, palg);
155
156
153
    if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
157
1
        goto decerr;
158
152
    if (privkey->type == V_ASN1_NEG_INTEGER || ptype != V_ASN1_SEQUENCE)
159
3
        goto decerr;
160
161
149
    pstr = pval;
162
149
    pm = pstr->data;
163
149
    pmlen = pstr->length;
164
149
    if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL)
165
1
        goto decerr;
166
    /* We have parameters now set private key */
167
148
    if ((dsa->priv_key = BN_secure_new()) == NULL
168
148
        || !ASN1_INTEGER_to_BN(privkey, dsa->priv_key)) {
169
0
        DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
170
0
        goto dsaerr;
171
0
    }
172
    /* Calculate public key */
173
148
    if ((dsa->pub_key = BN_new()) == NULL) {
174
0
        DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
175
0
        goto dsaerr;
176
0
    }
177
148
    if ((ctx = BN_CTX_new()) == NULL) {
178
0
        DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
179
0
        goto dsaerr;
180
0
    }
181
182
148
    BN_set_flags(dsa->priv_key, BN_FLG_CONSTTIME);
183
148
    if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) {
184
1
        DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
185
1
        goto dsaerr;
186
1
    }
187
188
147
    EVP_PKEY_assign_DSA(pkey, dsa);
189
190
147
    ret = 1;
191
147
    goto done;
192
193
5
 decerr:
194
5
    DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_DECODE_ERROR);
195
6
 dsaerr:
196
6
    DSA_free(dsa);
197
153
 done:
198
153
    BN_CTX_free(ctx);
199
153
    ASN1_STRING_clear_free(privkey);
200
153
    return ret;
201
6
}
202
203
static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
204
0
{
205
0
    ASN1_STRING *params = NULL;
206
0
    ASN1_INTEGER *prkey = NULL;
207
0
    unsigned char *dp = NULL;
208
0
    int dplen;
209
210
0
    if (!pkey->pkey.dsa || !pkey->pkey.dsa->priv_key) {
211
0
        DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_MISSING_PARAMETERS);
212
0
        goto err;
213
0
    }
214
215
0
    params = ASN1_STRING_new();
216
217
0
    if (params == NULL) {
218
0
        DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
219
0
        goto err;
220
0
    }
221
222
0
    params->length = i2d_DSAparams(pkey->pkey.dsa, &params->data);
223
0
    if (params->length <= 0) {
224
0
        DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
225
0
        goto err;
226
0
    }
227
0
    params->type = V_ASN1_SEQUENCE;
228
229
    /* Get private key into integer */
230
0
    prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
231
232
0
    if (!prkey) {
233
0
        DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_BN_ERROR);
234
0
        goto err;
235
0
    }
236
237
0
    dplen = i2d_ASN1_INTEGER(prkey, &dp);
238
239
0
    ASN1_STRING_clear_free(prkey);
240
0
    prkey = NULL;
241
242
0
    if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0,
243
0
                         V_ASN1_SEQUENCE, params, dp, dplen))
244
0
        goto err;
245
246
0
    return 1;
247
248
0
 err:
249
0
    OPENSSL_free(dp);
250
0
    ASN1_STRING_free(params);
251
0
    ASN1_STRING_clear_free(prkey);
252
0
    return 0;
253
0
}
254
255
static int int_dsa_size(const EVP_PKEY *pkey)
256
439
{
257
439
    return DSA_size(pkey->pkey.dsa);
258
439
}
259
260
static int dsa_bits(const EVP_PKEY *pkey)
261
0
{
262
0
    return DSA_bits(pkey->pkey.dsa);
263
0
}
264
265
static int dsa_security_bits(const EVP_PKEY *pkey)
266
6.11k
{
267
6.11k
    return DSA_security_bits(pkey->pkey.dsa);
268
6.11k
}
269
270
static int dsa_missing_parameters(const EVP_PKEY *pkey)
271
13.1k
{
272
13.1k
    DSA *dsa;
273
13.1k
    dsa = pkey->pkey.dsa;
274
13.1k
    if (dsa == NULL || dsa->p == NULL || dsa->q == NULL || dsa->g == NULL)
275
3
        return 1;
276
13.1k
    return 0;
277
13.1k
}
278
279
static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
280
0
{
281
0
    BIGNUM *a;
282
283
0
    if (to->pkey.dsa == NULL) {
284
0
        to->pkey.dsa = DSA_new();
285
0
        if (to->pkey.dsa == NULL)
286
0
            return 0;
287
0
    }
288
289
0
    if ((a = BN_dup(from->pkey.dsa->p)) == NULL)
290
0
        return 0;
291
0
    BN_free(to->pkey.dsa->p);
292
0
    to->pkey.dsa->p = a;
293
294
0
    if ((a = BN_dup(from->pkey.dsa->q)) == NULL)
295
0
        return 0;
296
0
    BN_free(to->pkey.dsa->q);
297
0
    to->pkey.dsa->q = a;
298
299
0
    if ((a = BN_dup(from->pkey.dsa->g)) == NULL)
300
0
        return 0;
301
0
    BN_free(to->pkey.dsa->g);
302
0
    to->pkey.dsa->g = a;
303
0
    return 1;
304
0
}
305
306
static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
307
12.2k
{
308
12.2k
    if (BN_cmp(a->pkey.dsa->p, b->pkey.dsa->p) ||
309
12.2k
        BN_cmp(a->pkey.dsa->q, b->pkey.dsa->q) ||
310
12.2k
        BN_cmp(a->pkey.dsa->g, b->pkey.dsa->g))
311
0
        return 0;
312
12.2k
    else
313
12.2k
        return 1;
314
12.2k
}
315
316
static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
317
6.11k
{
318
6.11k
    if (BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) != 0)
319
0
        return 0;
320
6.11k
    else
321
6.11k
        return 1;
322
6.11k
}
323
324
static void int_dsa_free(EVP_PKEY *pkey)
325
147k
{
326
147k
    DSA_free(pkey->pkey.dsa);
327
147k
}
328
329
static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype)
330
274
{
331
274
    int ret = 0;
332
274
    const char *ktype = NULL;
333
274
    const BIGNUM *priv_key, *pub_key;
334
335
274
    if (ptype == 2)
336
172
        priv_key = x->priv_key;
337
102
    else
338
102
        priv_key = NULL;
339
340
274
    if (ptype > 0)
341
193
        pub_key = x->pub_key;
342
81
    else
343
81
        pub_key = NULL;
344
345
274
    if (ptype == 2)
346
172
        ktype = "Private-Key";
347
102
    else if (ptype == 1)
348
21
        ktype = "Public-Key";
349
81
    else
350
81
        ktype = "DSA-Parameters";
351
352
274
    if (priv_key) {
353
164
        if (!BIO_indent(bp, off, 128))
354
0
            goto err;
355
164
        if (BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p))
356
164
            <= 0)
357
0
            goto err;
358
164
    }
359
360
274
    if (!ASN1_bn_print(bp, "priv:", priv_key, NULL, off))
361
0
        goto err;
362
274
    if (!ASN1_bn_print(bp, "pub: ", pub_key, NULL, off))
363
0
        goto err;
364
274
    if (!ASN1_bn_print(bp, "P:   ", x->p, NULL, off))
365
0
        goto err;
366
274
    if (!ASN1_bn_print(bp, "Q:   ", x->q, NULL, off))
367
0
        goto err;
368
274
    if (!ASN1_bn_print(bp, "G:   ", x->g, NULL, off))
369
0
        goto err;
370
274
    ret = 1;
371
274
 err:
372
274
    return ret;
373
274
}
374
375
static int dsa_param_decode(EVP_PKEY *pkey,
376
                            const unsigned char **pder, int derlen)
377
0
{
378
0
    DSA *dsa;
379
380
0
    if ((dsa = d2i_DSAparams(NULL, pder, derlen)) == NULL) {
381
0
        DSAerr(DSA_F_DSA_PARAM_DECODE, ERR_R_DSA_LIB);
382
0
        return 0;
383
0
    }
384
0
    EVP_PKEY_assign_DSA(pkey, dsa);
385
0
    return 1;
386
0
}
387
388
static int dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
389
0
{
390
0
    return i2d_DSAparams(pkey->pkey.dsa, pder);
391
0
}
392
393
static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
394
                           ASN1_PCTX *ctx)
395
81
{
396
81
    return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
397
81
}
398
399
static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
400
                         ASN1_PCTX *ctx)
401
21
{
402
21
    return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
403
21
}
404
405
static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
406
                          ASN1_PCTX *ctx)
407
246
{
408
246
    return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
409
246
}
410
411
static int old_dsa_priv_decode(EVP_PKEY *pkey,
412
                               const unsigned char **pder, int derlen)
413
7.00k
{
414
7.00k
    DSA *dsa;
415
416
7.00k
    if ((dsa = d2i_DSAPrivateKey(NULL, pder, derlen)) == NULL) {
417
884
        DSAerr(DSA_F_OLD_DSA_PRIV_DECODE, ERR_R_DSA_LIB);
418
884
        return 0;
419
884
    }
420
6.12k
    EVP_PKEY_assign_DSA(pkey, dsa);
421
6.12k
    return 1;
422
7.00k
}
423
424
static int old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
425
224
{
426
224
    return i2d_DSAPrivateKey(pkey->pkey.dsa, pder);
427
224
}
428
429
static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
430
                         const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
431
772
{
432
772
    DSA_SIG *dsa_sig;
433
772
    const unsigned char *p;
434
435
772
    if (!sig) {
436
301
        if (BIO_puts(bp, "\n") <= 0)
437
0
            return 0;
438
301
        else
439
301
            return 1;
440
301
    }
441
471
    p = sig->data;
442
471
    dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
443
471
    if (dsa_sig) {
444
362
        int rv = 0;
445
362
        const BIGNUM *r, *s;
446
447
362
        DSA_SIG_get0(dsa_sig, &r, &s);
448
449
362
        if (BIO_write(bp, "\n", 1) != 1)
450
0
            goto err;
451
452
362
        if (!ASN1_bn_print(bp, "r:   ", r, NULL, indent))
453
0
            goto err;
454
362
        if (!ASN1_bn_print(bp, "s:   ", s, NULL, indent))
455
0
            goto err;
456
362
        rv = 1;
457
362
 err:
458
362
        DSA_SIG_free(dsa_sig);
459
362
        return rv;
460
362
    }
461
109
    return X509_signature_dump(bp, sig, indent);
462
471
}
463
464
static int dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
465
0
{
466
0
    switch (op) {
467
0
    case ASN1_PKEY_CTRL_PKCS7_SIGN:
468
0
        if (arg1 == 0) {
469
0
            int snid, hnid;
470
0
            X509_ALGOR *alg1, *alg2;
471
0
            PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
472
0
            if (alg1 == NULL || alg1->algorithm == NULL)
473
0
                return -1;
474
0
            hnid = OBJ_obj2nid(alg1->algorithm);
475
0
            if (hnid == NID_undef)
476
0
                return -1;
477
0
            if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
478
0
                return -1;
479
0
            X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
480
0
        }
481
0
        return 1;
482
0
#ifndef OPENSSL_NO_CMS
483
0
    case ASN1_PKEY_CTRL_CMS_SIGN:
484
0
        if (arg1 == 0) {
485
0
            int snid, hnid;
486
0
            X509_ALGOR *alg1, *alg2;
487
0
            CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
488
0
            if (alg1 == NULL || alg1->algorithm == NULL)
489
0
                return -1;
490
0
            hnid = OBJ_obj2nid(alg1->algorithm);
491
0
            if (hnid == NID_undef)
492
0
                return -1;
493
0
            if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
494
0
                return -1;
495
0
            X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
496
0
        }
497
0
        return 1;
498
499
0
    case ASN1_PKEY_CTRL_CMS_RI_TYPE:
500
0
        *(int *)arg2 = CMS_RECIPINFO_NONE;
501
0
        return 1;
502
0
#endif
503
504
0
    case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
505
0
        *(int *)arg2 = NID_sha256;
506
0
        return 1;
507
508
0
    default:
509
0
        return -2;
510
511
0
    }
512
513
0
}
514
515
/* NB these are sorted in pkey_id order, lowest first */
516
517
const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[5] = {
518
519
    {
520
     EVP_PKEY_DSA2,
521
     EVP_PKEY_DSA,
522
     ASN1_PKEY_ALIAS},
523
524
    {
525
     EVP_PKEY_DSA1,
526
     EVP_PKEY_DSA,
527
     ASN1_PKEY_ALIAS},
528
529
    {
530
     EVP_PKEY_DSA4,
531
     EVP_PKEY_DSA,
532
     ASN1_PKEY_ALIAS},
533
534
    {
535
     EVP_PKEY_DSA3,
536
     EVP_PKEY_DSA,
537
     ASN1_PKEY_ALIAS},
538
539
    {
540
     EVP_PKEY_DSA,
541
     EVP_PKEY_DSA,
542
     0,
543
544
     "DSA",
545
     "OpenSSL DSA method",
546
547
     dsa_pub_decode,
548
     dsa_pub_encode,
549
     dsa_pub_cmp,
550
     dsa_pub_print,
551
552
     dsa_priv_decode,
553
     dsa_priv_encode,
554
     dsa_priv_print,
555
556
     int_dsa_size,
557
     dsa_bits,
558
     dsa_security_bits,
559
560
     dsa_param_decode,
561
     dsa_param_encode,
562
     dsa_missing_parameters,
563
     dsa_copy_parameters,
564
     dsa_cmp_parameters,
565
     dsa_param_print,
566
     dsa_sig_print,
567
568
     int_dsa_free,
569
     dsa_pkey_ctrl,
570
     old_dsa_priv_decode,
571
     old_dsa_priv_encode}
572
};