Coverage Report

Created: 2025-12-31 06:58

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