Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/dsa/dsa_pmeth.c
Line
Count
Source
1
/*
2
 * Copyright 2006-2025 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 "internal/cryptlib.h"
18
#include <openssl/asn1t.h>
19
#include <openssl/x509.h>
20
#include <openssl/evp.h>
21
#include <openssl/bn.h>
22
#include "crypto/evp.h"
23
#include "dsa_local.h"
24
25
/* DSA pkey context structure */
26
27
typedef struct {
28
    /* Parameter gen parameters */
29
    int nbits; /* size of p in bits (default: 2048) */
30
    int qbits; /* size of q in bits (default: 224) */
31
    const EVP_MD *pmd; /* MD for parameter generation */
32
    /* Keygen callback info */
33
    int gentmp[2];
34
    /* message digest */
35
    const EVP_MD *md; /* MD for the signature */
36
} DSA_PKEY_CTX;
37
38
static int pkey_dsa_init(EVP_PKEY_CTX *ctx)
39
0
{
40
0
    DSA_PKEY_CTX *dctx = OPENSSL_malloc(sizeof(*dctx));
41
42
0
    if (dctx == NULL)
43
0
        return 0;
44
0
    dctx->nbits = 2048;
45
0
    dctx->qbits = 224;
46
0
    dctx->pmd = NULL;
47
0
    dctx->md = NULL;
48
49
0
    ctx->data = dctx;
50
0
    ctx->keygen_info = dctx->gentmp;
51
0
    ctx->keygen_info_count = 2;
52
53
0
    return 1;
54
0
}
55
56
static int pkey_dsa_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
57
0
{
58
0
    DSA_PKEY_CTX *dctx, *sctx;
59
60
0
    if (!pkey_dsa_init(dst))
61
0
        return 0;
62
0
    sctx = src->data;
63
0
    dctx = dst->data;
64
0
    dctx->nbits = sctx->nbits;
65
0
    dctx->qbits = sctx->qbits;
66
0
    dctx->pmd = sctx->pmd;
67
0
    dctx->md = sctx->md;
68
0
    return 1;
69
0
}
70
71
static void pkey_dsa_cleanup(EVP_PKEY_CTX *ctx)
72
0
{
73
0
    DSA_PKEY_CTX *dctx = ctx->data;
74
0
    OPENSSL_free(dctx);
75
0
}
76
77
static int pkey_dsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
78
    size_t *siglen, const unsigned char *tbs,
79
    size_t tbslen)
80
0
{
81
0
    int ret, md_size;
82
0
    unsigned int sltmp;
83
0
    DSA_PKEY_CTX *dctx = ctx->data;
84
    /*
85
     * Discard const. Its marked as const because this may be a cached copy of
86
     * the "real" key. These calls don't make any modifications that need to
87
     * be reflected back in the "original" key.
88
     */
89
0
    DSA *dsa = (DSA *)EVP_PKEY_get0_DSA(ctx->pkey);
90
91
0
    if (dctx->md != NULL) {
92
0
        md_size = EVP_MD_get_size(dctx->md);
93
0
        if (md_size <= 0)
94
0
            return 0;
95
0
        if (tbslen != (size_t)md_size)
96
0
            return 0;
97
0
    }
98
99
0
    ret = DSA_sign(0, tbs, (int)tbslen, sig, &sltmp, dsa);
100
101
0
    if (ret <= 0)
102
0
        return ret;
103
0
    *siglen = sltmp;
104
0
    return 1;
105
0
}
106
107
static int pkey_dsa_verify(EVP_PKEY_CTX *ctx,
108
    const unsigned char *sig, size_t siglen,
109
    const unsigned char *tbs, size_t tbslen)
110
0
{
111
0
    int ret, md_size;
112
0
    DSA_PKEY_CTX *dctx = ctx->data;
113
    /*
114
     * Discard const. Its marked as const because this may be a cached copy of
115
     * the "real" key. These calls don't make any modifications that need to
116
     * be reflected back in the "original" key.
117
     */
118
0
    DSA *dsa = (DSA *)EVP_PKEY_get0_DSA(ctx->pkey);
119
120
0
    if (dctx->md != NULL) {
121
0
        md_size = EVP_MD_get_size(dctx->md);
122
0
        if (md_size <= 0)
123
0
            return 0;
124
0
        if (tbslen != (size_t)md_size)
125
0
            return 0;
126
0
    }
127
128
0
    ret = DSA_verify(0, tbs, (int)tbslen, sig, (int)siglen, dsa);
129
130
0
    return ret;
131
0
}
132
133
static int pkey_dsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
134
0
{
135
0
    DSA_PKEY_CTX *dctx = ctx->data;
136
137
0
    switch (type) {
138
0
    case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS:
139
0
        if (p1 < 256)
140
0
            return -2;
141
0
        dctx->nbits = p1;
142
0
        return 1;
143
144
0
    case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS:
145
0
        if (p1 != 160 && p1 != 224 && p1 && p1 != 256)
146
0
            return -2;
147
0
        dctx->qbits = p1;
148
0
        return 1;
149
150
0
    case EVP_PKEY_CTRL_DSA_PARAMGEN_MD:
151
0
        if (EVP_MD_get_type((const EVP_MD *)p2) != NID_sha1 && EVP_MD_get_type((const EVP_MD *)p2) != NID_sha224 && EVP_MD_get_type((const EVP_MD *)p2) != NID_sha256) {
152
0
            ERR_raise(ERR_LIB_DSA, DSA_R_INVALID_DIGEST_TYPE);
153
0
            return 0;
154
0
        }
155
0
        dctx->pmd = p2;
156
0
        return 1;
157
158
0
    case EVP_PKEY_CTRL_MD:
159
0
        if (EVP_MD_get_type((const EVP_MD *)p2) != NID_sha1 && EVP_MD_get_type((const EVP_MD *)p2) != NID_dsa && EVP_MD_get_type((const EVP_MD *)p2) != NID_dsaWithSHA && EVP_MD_get_type((const EVP_MD *)p2) != NID_sha224 && EVP_MD_get_type((const EVP_MD *)p2) != NID_sha256 && EVP_MD_get_type((const EVP_MD *)p2) != NID_sha384 && EVP_MD_get_type((const EVP_MD *)p2) != NID_sha512 && EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_224 && EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_256 && EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_384 && EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_512) {
160
0
            ERR_raise(ERR_LIB_DSA, DSA_R_INVALID_DIGEST_TYPE);
161
0
            return 0;
162
0
        }
163
0
        dctx->md = p2;
164
0
        return 1;
165
166
0
    case EVP_PKEY_CTRL_GET_MD:
167
0
        *(const EVP_MD **)p2 = dctx->md;
168
0
        return 1;
169
170
0
    case EVP_PKEY_CTRL_DIGESTINIT:
171
0
    case EVP_PKEY_CTRL_PKCS7_SIGN:
172
0
    case EVP_PKEY_CTRL_CMS_SIGN:
173
0
        return 1;
174
175
0
    case EVP_PKEY_CTRL_PEER_KEY:
176
0
        ERR_raise(ERR_LIB_DSA, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
177
0
        return -2;
178
0
    default:
179
0
        return -2;
180
0
    }
181
0
}
182
183
static int pkey_dsa_ctrl_str(EVP_PKEY_CTX *ctx,
184
    const char *type, const char *value)
185
0
{
186
0
    if (strcmp(type, "dsa_paramgen_bits") == 0) {
187
0
        int nbits;
188
0
        nbits = atoi(value);
189
0
        return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits);
190
0
    }
191
0
    if (strcmp(type, "dsa_paramgen_q_bits") == 0) {
192
0
        int qbits = atoi(value);
193
0
        return EVP_PKEY_CTX_set_dsa_paramgen_q_bits(ctx, qbits);
194
0
    }
195
0
    if (strcmp(type, "dsa_paramgen_md") == 0) {
196
0
        const EVP_MD *md = EVP_get_digestbyname(value);
197
198
0
        if (md == NULL) {
199
0
            ERR_raise(ERR_LIB_DSA, DSA_R_INVALID_DIGEST_TYPE);
200
0
            return 0;
201
0
        }
202
0
        return EVP_PKEY_CTX_set_dsa_paramgen_md(ctx, md);
203
0
    }
204
0
    return -2;
205
0
}
206
207
static int pkey_dsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
208
0
{
209
0
    DSA *dsa = NULL;
210
0
    DSA_PKEY_CTX *dctx = ctx->data;
211
0
    BN_GENCB *pcb;
212
0
    int ret, res;
213
214
0
    if (ctx->pkey_gencb) {
215
0
        pcb = BN_GENCB_new();
216
0
        if (pcb == NULL)
217
0
            return 0;
218
0
        evp_pkey_set_cb_translate(pcb, ctx);
219
0
    } else
220
0
        pcb = NULL;
221
0
    dsa = DSA_new();
222
0
    if (dsa == NULL) {
223
0
        BN_GENCB_free(pcb);
224
0
        return 0;
225
0
    }
226
0
    if (dctx->md != NULL)
227
0
        ossl_ffc_set_digest(&dsa->params, EVP_MD_get0_name(dctx->md), NULL);
228
229
0
    ret = ossl_ffc_params_FIPS186_4_generate(NULL, &dsa->params,
230
0
        FFC_PARAM_TYPE_DSA, dctx->nbits,
231
0
        dctx->qbits, &res, pcb);
232
0
    BN_GENCB_free(pcb);
233
0
    if (ret > 0)
234
0
        EVP_PKEY_assign_DSA(pkey, dsa);
235
0
    else
236
0
        DSA_free(dsa);
237
0
    return ret;
238
0
}
239
240
static int pkey_dsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
241
0
{
242
0
    DSA *dsa = NULL;
243
244
0
    if (ctx->pkey == NULL) {
245
0
        ERR_raise(ERR_LIB_DSA, DSA_R_NO_PARAMETERS_SET);
246
0
        return 0;
247
0
    }
248
0
    dsa = DSA_new();
249
0
    if (dsa == NULL)
250
0
        return 0;
251
0
    EVP_PKEY_assign_DSA(pkey, dsa);
252
    /* Note: if error return, pkey is freed by parent routine */
253
0
    if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
254
0
        return 0;
255
0
    return DSA_generate_key((DSA *)EVP_PKEY_get0_DSA(pkey));
256
0
}
257
258
static const EVP_PKEY_METHOD dsa_pkey_meth = {
259
    EVP_PKEY_DSA,
260
    EVP_PKEY_FLAG_AUTOARGLEN,
261
    pkey_dsa_init,
262
    pkey_dsa_copy,
263
    pkey_dsa_cleanup,
264
265
    0,
266
    pkey_dsa_paramgen,
267
268
    0,
269
    pkey_dsa_keygen,
270
271
    0,
272
    pkey_dsa_sign,
273
274
    0,
275
    pkey_dsa_verify,
276
277
    0, 0,
278
279
    0, 0, 0, 0,
280
281
    0, 0,
282
283
    0, 0,
284
285
    0, 0,
286
287
    pkey_dsa_ctrl,
288
    pkey_dsa_ctrl_str
289
};
290
291
const EVP_PKEY_METHOD *ossl_dsa_pkey_method(void)
292
0
{
293
0
    return &dsa_pkey_meth;
294
0
}