Coverage Report

Created: 2025-12-31 06:58

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