Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/crypto/dh/dh_asn1.c
Line
Count
Source
1
/*
2
 * Copyright 2000-2026 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
 * DH 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/bn.h>
19
#include "dh_local.h"
20
#include <openssl/objects.h>
21
#include <openssl/asn1t.h>
22
#include "crypto/dh.h"
23
24
#include <crypto/asn1.h>
25
26
/* Override the default free and new methods */
27
static int dh_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
28
    void *exarg)
29
411k
{
30
411k
    if (operation == ASN1_OP_NEW_PRE) {
31
135k
        *pval = (ASN1_VALUE *)DH_new();
32
135k
        if (*pval != NULL)
33
135k
            return 2;
34
0
        return 0;
35
275k
    } else if (operation == ASN1_OP_FREE_PRE) {
36
131k
        DH_free((DH *)*pval);
37
131k
        *pval = NULL;
38
131k
        return 2;
39
144k
    } else if (operation == ASN1_OP_D2I_POST) {
40
5.23k
        DH *dh = (DH *)*pval;
41
42
5.23k
        DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
43
5.23k
        DH_set_flags(dh, DH_FLAG_TYPE_DH);
44
5.23k
        ossl_dh_cache_named_group(dh);
45
5.23k
        dh->dirty_cnt++;
46
5.23k
    }
47
144k
    return 1;
48
411k
}
49
50
ASN1_SEQUENCE_cb(DHparams, dh_cb) = {
51
    ASN1_SIMPLE(DH, params.p, BIGNUM),
52
    ASN1_SIMPLE(DH, params.g, BIGNUM),
53
    ASN1_OPT_EMBED(DH, length, ZINT32),
54
} ASN1_SEQUENCE_END_cb(DH, DHparams)
55
56
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DH, DHparams, DHparams)
57
58
/*
59
 * Internal only structures for handling X9.42 DH: this gets translated to or
60
 * from a DH structure straight away.
61
 */
62
63
typedef struct {
64
    ASN1_BIT_STRING *seed;
65
    BIGNUM *counter;
66
} int_dhvparams;
67
68
typedef struct {
69
    BIGNUM *p;
70
    BIGNUM *q;
71
    BIGNUM *g;
72
    BIGNUM *j;
73
    int_dhvparams *vparams;
74
} int_dhx942_dh;
75
76
ASN1_SEQUENCE(DHvparams) = {
77
    ASN1_SIMPLE(int_dhvparams, seed, ASN1_BIT_STRING),
78
    ASN1_SIMPLE(int_dhvparams, counter, BIGNUM)
79
497k
} static_ASN1_SEQUENCE_END_name(int_dhvparams, DHvparams)
80
81
ASN1_SEQUENCE(DHxparams)
82
    = {
83
          ASN1_SIMPLE(int_dhx942_dh, p, BIGNUM),
84
          ASN1_SIMPLE(int_dhx942_dh, g, BIGNUM),
85
          ASN1_SIMPLE(int_dhx942_dh, q, BIGNUM),
86
          ASN1_OPT(int_dhx942_dh, j, BIGNUM),
87
          ASN1_OPT(int_dhx942_dh, vparams, DHvparams),
88
193k
      } static_ASN1_SEQUENCE_END_name(int_dhx942_dh, DHxparams)
89
90
int_dhx942_dh *d2i_int_dhx(int_dhx942_dh **a, const unsigned char **pp, long length);
91
int i2d_int_dhx(const int_dhx942_dh *a, unsigned char **pp);
92
93
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(int_dhx942_dh, DHxparams, int_dhx)
94
95
DH *d2i_DHxparams(DH **a, const unsigned char **pp, long length)
96
193k
{
97
193k
    FFC_PARAMS *params;
98
193k
    int_dhx942_dh *dhx = NULL;
99
193k
    DH *dh = NULL;
100
101
193k
    dh = DH_new();
102
193k
    if (dh == NULL)
103
0
        return NULL;
104
193k
    dhx = d2i_int_dhx(NULL, pp, length);
105
193k
    if (dhx == NULL) {
106
138k
        DH_free(dh);
107
138k
        return NULL;
108
138k
    }
109
110
55.6k
    if (a != NULL) {
111
0
        DH_free(*a);
112
0
        *a = dh;
113
0
    }
114
115
55.6k
    params = &dh->params;
116
55.6k
    DH_set0_pqg(dh, dhx->p, dhx->q, dhx->g);
117
55.6k
    ossl_ffc_params_set0_j(params, dhx->j);
118
119
55.6k
    if (dhx->vparams != NULL) {
120
        /* The counter has a maximum value of 4 * numbits(p) - 1 */
121
314
        int counter = (int)BN_get_word(dhx->vparams->counter);
122
314
        ossl_ffc_params_set_validate_params(params, dhx->vparams->seed->data,
123
314
            dhx->vparams->seed->length,
124
314
            counter);
125
314
        ASN1_BIT_STRING_free(dhx->vparams->seed);
126
314
        BN_free(dhx->vparams->counter);
127
314
        OPENSSL_free(dhx->vparams);
128
314
        dhx->vparams = NULL;
129
314
    }
130
131
55.6k
    OPENSSL_free(dhx);
132
55.6k
    DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
133
55.6k
    DH_set_flags(dh, DH_FLAG_TYPE_DHX);
134
55.6k
    return dh;
135
193k
}
136
137
int i2d_DHxparams(const DH *dh, unsigned char **pp)
138
287
{
139
287
    int ret = 0;
140
287
    int_dhx942_dh dhx;
141
287
    int_dhvparams dhv = { NULL, NULL };
142
287
    ASN1_BIT_STRING seed;
143
287
    size_t seedlen = 0;
144
287
    const FFC_PARAMS *params = &dh->params;
145
287
    int counter;
146
147
287
    ossl_ffc_params_get0_pqg(params, (const BIGNUM **)&dhx.p,
148
287
        (const BIGNUM **)&dhx.q, (const BIGNUM **)&dhx.g);
149
287
    dhx.j = params->j;
150
287
    ossl_ffc_params_get_validate_params(params, &seed.data, &seedlen, &counter);
151
287
    seed.length = (int)seedlen;
152
153
287
    if (counter != -1 && seed.data != NULL && seed.length > 0) {
154
91
        seed.flags = ASN1_STRING_FLAG_BITS_LEFT;
155
91
        dhv.seed = &seed;
156
91
        dhv.counter = BN_new();
157
91
        if (dhv.counter == NULL)
158
0
            return 0;
159
91
        if (!BN_set_word(dhv.counter, (BN_ULONG)counter))
160
0
            goto err;
161
91
        dhx.vparams = &dhv;
162
196
    } else {
163
196
        dhx.vparams = NULL;
164
196
    }
165
287
    ret = i2d_int_dhx(&dhx, pp);
166
287
err:
167
287
    BN_free(dhv.counter);
168
287
    return ret;
169
287
}