Coverage Report

Created: 2025-06-11 06:41

/src/boringssl/crypto/dsa/dsa_asn1.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <openssl/dsa.h>
16
17
#include <assert.h>
18
19
#include <openssl/bn.h>
20
#include <openssl/bytestring.h>
21
#include <openssl/err.h>
22
#include <openssl/mem.h>
23
24
#include "internal.h"
25
#include "../bytestring/internal.h"
26
27
28
// This function is in dsa_asn1.c rather than dsa.c because it is reachable from
29
// |EVP_PKEY| parsers. This makes it easier for the static linker to drop most
30
// of the DSA implementation.
31
3.76k
int dsa_check_key(const DSA *dsa) {
32
3.76k
  if (!dsa->p || !dsa->q || !dsa->g) {
33
0
    OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
34
0
    return 0;
35
0
  }
36
37
  // Fully checking for invalid DSA groups is expensive, so security and
38
  // correctness of the signature scheme depend on how |dsa| was computed. I.e.
39
  // we leave "assurance of domain parameter validity" from FIPS 186-4 to the
40
  // caller. However, we check bounds on all values to avoid DoS vectors even
41
  // when domain parameters are invalid. In particular, signing will infinite
42
  // loop if |g| is zero.
43
3.76k
  if (BN_is_negative(dsa->p) || BN_is_negative(dsa->q) || BN_is_zero(dsa->p) ||
44
3.76k
      BN_is_zero(dsa->q) || !BN_is_odd(dsa->p) || !BN_is_odd(dsa->q) ||
45
      // |q| must be a prime divisor of |p - 1|, which implies |q < p|.
46
3.76k
      BN_cmp(dsa->q, dsa->p) >= 0 ||
47
      // |g| is in the multiplicative group of |p|.
48
3.76k
      BN_is_negative(dsa->g) || BN_is_zero(dsa->g) ||
49
3.76k
      BN_cmp(dsa->g, dsa->p) >= 0) {
50
519
    OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS);
51
519
    return 0;
52
519
  }
53
54
  // FIPS 186-4 allows only three different sizes for q.
55
3.24k
  unsigned q_bits = BN_num_bits(dsa->q);
56
3.24k
  if (q_bits != 160 && q_bits != 224 && q_bits != 256) {
57
910
    OPENSSL_PUT_ERROR(DSA, DSA_R_BAD_Q_VALUE);
58
910
    return 0;
59
910
  }
60
61
  // Bound |dsa->p| to avoid a DoS vector. Note this limit is much larger than
62
  // the one in FIPS 186-4, which only allows L = 1024, 2048, and 3072.
63
2.33k
  if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
64
23
    OPENSSL_PUT_ERROR(DSA, DSA_R_MODULUS_TOO_LARGE);
65
23
    return 0;
66
23
  }
67
68
2.30k
  if (dsa->pub_key != NULL) {
69
    // The public key is also in the multiplicative group of |p|.
70
10
    if (BN_is_negative(dsa->pub_key) || BN_is_zero(dsa->pub_key) ||
71
10
        BN_cmp(dsa->pub_key, dsa->p) >= 0) {
72
4
      OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS);
73
4
      return 0;
74
4
    }
75
10
  }
76
77
2.30k
  if (dsa->priv_key != NULL) {
78
    // The private key is a non-zero element of the scalar field, determined by
79
    // |q|.
80
1.08k
    if (BN_is_negative(dsa->priv_key) ||
81
1.08k
        constant_time_declassify_int(BN_is_zero(dsa->priv_key)) ||
82
1.08k
        constant_time_declassify_int(BN_cmp(dsa->priv_key, dsa->q) >= 0)) {
83
16
      OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS);
84
16
      return 0;
85
16
    }
86
1.08k
  }
87
88
2.28k
  return 1;
89
2.30k
}
90
91
9.55k
static int parse_integer(CBS *cbs, BIGNUM **out) {
92
9.55k
  assert(*out == NULL);
93
9.55k
  *out = BN_new();
94
9.55k
  if (*out == NULL) {
95
0
    return 0;
96
0
  }
97
9.55k
  return BN_parse_asn1_unsigned(cbs, *out);
98
9.55k
}
99
100
2.42k
static int marshal_integer(CBB *cbb, BIGNUM *bn) {
101
2.42k
  if (bn == NULL) {
102
    // A DSA object may be missing some components.
103
0
    OPENSSL_PUT_ERROR(DSA, ERR_R_PASSED_NULL_PARAMETER);
104
0
    return 0;
105
0
  }
106
2.42k
  return BN_marshal_asn1(cbb, bn);
107
2.42k
}
108
109
0
DSA_SIG *DSA_SIG_parse(CBS *cbs) {
110
0
  DSA_SIG *ret = DSA_SIG_new();
111
0
  if (ret == NULL) {
112
0
    return NULL;
113
0
  }
114
0
  CBS child;
115
0
  if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
116
0
      !parse_integer(&child, &ret->r) ||
117
0
      !parse_integer(&child, &ret->s) ||
118
0
      CBS_len(&child) != 0) {
119
0
    OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
120
0
    DSA_SIG_free(ret);
121
0
    return NULL;
122
0
  }
123
0
  return ret;
124
0
}
125
126
0
int DSA_SIG_marshal(CBB *cbb, const DSA_SIG *sig) {
127
0
  CBB child;
128
0
  if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
129
0
      !marshal_integer(&child, sig->r) ||
130
0
      !marshal_integer(&child, sig->s) ||
131
0
      !CBB_flush(cbb)) {
132
0
    OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR);
133
0
    return 0;
134
0
  }
135
0
  return 1;
136
0
}
137
138
0
DSA *DSA_parse_public_key(CBS *cbs) {
139
0
  bssl::UniquePtr<DSA> ret(DSA_new());
140
0
  if (ret == nullptr) {
141
0
    return nullptr;
142
0
  }
143
0
  CBS child;
144
0
  if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
145
0
      !parse_integer(&child, &ret->pub_key) ||
146
0
      !parse_integer(&child, &ret->p) ||
147
0
      !parse_integer(&child, &ret->q) ||
148
0
      !parse_integer(&child, &ret->g) ||
149
0
      CBS_len(&child) != 0) {
150
0
    OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
151
0
    return nullptr;
152
0
  }
153
0
  if (!dsa_check_key(ret.get())) {
154
0
    return nullptr;
155
0
  }
156
0
  return ret.release();
157
0
}
158
159
0
int DSA_marshal_public_key(CBB *cbb, const DSA *dsa) {
160
0
  CBB child;
161
0
  if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
162
0
      !marshal_integer(&child, dsa->pub_key) ||
163
0
      !marshal_integer(&child, dsa->p) ||
164
0
      !marshal_integer(&child, dsa->q) ||
165
0
      !marshal_integer(&child, dsa->g) ||
166
0
      !CBB_flush(cbb)) {
167
0
    OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR);
168
0
    return 0;
169
0
  }
170
0
  return 1;
171
0
}
172
173
4.92k
DSA *DSA_parse_parameters(CBS *cbs) {
174
4.92k
  bssl::UniquePtr<DSA> ret(DSA_new());
175
4.92k
  if (ret == nullptr) {
176
0
    return nullptr;
177
0
  }
178
4.92k
  CBS child;
179
4.92k
  if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
180
4.92k
      !parse_integer(&child, &ret->p) ||
181
4.92k
      !parse_integer(&child, &ret->q) ||
182
4.92k
      !parse_integer(&child, &ret->g) ||
183
4.92k
      CBS_len(&child) != 0) {
184
2.59k
    OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
185
2.59k
    return nullptr;
186
2.59k
  }
187
2.33k
  if (!dsa_check_key(ret.get())) {
188
1.11k
    return nullptr;
189
1.11k
  }
190
1.21k
  return ret.release();
191
2.33k
}
192
193
808
int DSA_marshal_parameters(CBB *cbb, const DSA *dsa) {
194
808
  CBB child;
195
808
  if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
196
808
      !marshal_integer(&child, dsa->p) ||
197
808
      !marshal_integer(&child, dsa->q) ||
198
808
      !marshal_integer(&child, dsa->g) ||
199
808
      !CBB_flush(cbb)) {
200
0
    OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR);
201
0
    return 0;
202
0
  }
203
808
  return 1;
204
808
}
205
206
441
DSA *DSA_parse_private_key(CBS *cbs) {
207
441
  bssl::UniquePtr<DSA> ret(DSA_new());
208
441
  if (ret == nullptr) {
209
0
    return nullptr;
210
0
  }
211
212
441
  CBS child;
213
441
  uint64_t version;
214
441
  if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
215
441
      !CBS_get_asn1_uint64(&child, &version)) {
216
1
    OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
217
1
    return nullptr;
218
1
  }
219
220
440
  if (version != 0) {
221
86
    OPENSSL_PUT_ERROR(DSA, DSA_R_BAD_VERSION);
222
86
    return nullptr;
223
86
  }
224
225
354
  if (!parse_integer(&child, &ret->p) ||
226
354
      !parse_integer(&child, &ret->q) ||
227
354
      !parse_integer(&child, &ret->g) ||
228
354
      !parse_integer(&child, &ret->pub_key) ||
229
354
      !parse_integer(&child, &ret->priv_key) ||
230
354
      CBS_len(&child) != 0) {
231
6
    OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
232
6
    return nullptr;
233
6
  }
234
348
  if (!dsa_check_key(ret.get())) {
235
345
    return nullptr;
236
345
  }
237
238
3
  return ret.release();
239
348
}
240
241
0
int DSA_marshal_private_key(CBB *cbb, const DSA *dsa) {
242
0
  CBB child;
243
0
  if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
244
0
      !CBB_add_asn1_uint64(&child, 0 /* version */) ||
245
0
      !marshal_integer(&child, dsa->p) ||
246
0
      !marshal_integer(&child, dsa->q) ||
247
0
      !marshal_integer(&child, dsa->g) ||
248
0
      !marshal_integer(&child, dsa->pub_key) ||
249
0
      !marshal_integer(&child, dsa->priv_key) ||
250
0
      !CBB_flush(cbb)) {
251
0
    OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR);
252
0
    return 0;
253
0
  }
254
0
  return 1;
255
0
}
256
257
0
DSA_SIG *d2i_DSA_SIG(DSA_SIG **out_sig, const uint8_t **inp, long len) {
258
0
  if (len < 0) {
259
0
    return NULL;
260
0
  }
261
0
  CBS cbs;
262
0
  CBS_init(&cbs, *inp, (size_t)len);
263
0
  DSA_SIG *ret = DSA_SIG_parse(&cbs);
264
0
  if (ret == NULL) {
265
0
    return NULL;
266
0
  }
267
0
  if (out_sig != NULL) {
268
0
    DSA_SIG_free(*out_sig);
269
0
    *out_sig = ret;
270
0
  }
271
0
  *inp = CBS_data(&cbs);
272
0
  return ret;
273
0
}
274
275
0
int i2d_DSA_SIG(const DSA_SIG *in, uint8_t **outp) {
276
0
  CBB cbb;
277
0
  if (!CBB_init(&cbb, 0) ||
278
0
      !DSA_SIG_marshal(&cbb, in)) {
279
0
    CBB_cleanup(&cbb);
280
0
    return -1;
281
0
  }
282
0
  return CBB_finish_i2d(&cbb, outp);
283
0
}
284
285
0
DSA *d2i_DSAPublicKey(DSA **out, const uint8_t **inp, long len) {
286
0
  if (len < 0) {
287
0
    return NULL;
288
0
  }
289
0
  CBS cbs;
290
0
  CBS_init(&cbs, *inp, (size_t)len);
291
0
  DSA *ret = DSA_parse_public_key(&cbs);
292
0
  if (ret == NULL) {
293
0
    return NULL;
294
0
  }
295
0
  if (out != NULL) {
296
0
    DSA_free(*out);
297
0
    *out = ret;
298
0
  }
299
0
  *inp = CBS_data(&cbs);
300
0
  return ret;
301
0
}
302
303
0
int i2d_DSAPublicKey(const DSA *in, uint8_t **outp) {
304
0
  CBB cbb;
305
0
  if (!CBB_init(&cbb, 0) ||
306
0
      !DSA_marshal_public_key(&cbb, in)) {
307
0
    CBB_cleanup(&cbb);
308
0
    return -1;
309
0
  }
310
0
  return CBB_finish_i2d(&cbb, outp);
311
0
}
312
313
0
DSA *d2i_DSAPrivateKey(DSA **out, const uint8_t **inp, long len) {
314
0
  if (len < 0) {
315
0
    return NULL;
316
0
  }
317
0
  CBS cbs;
318
0
  CBS_init(&cbs, *inp, (size_t)len);
319
0
  DSA *ret = DSA_parse_private_key(&cbs);
320
0
  if (ret == NULL) {
321
0
    return NULL;
322
0
  }
323
0
  if (out != NULL) {
324
0
    DSA_free(*out);
325
0
    *out = ret;
326
0
  }
327
0
  *inp = CBS_data(&cbs);
328
0
  return ret;
329
0
}
330
331
0
int i2d_DSAPrivateKey(const DSA *in, uint8_t **outp) {
332
0
  CBB cbb;
333
0
  if (!CBB_init(&cbb, 0) ||
334
0
      !DSA_marshal_private_key(&cbb, in)) {
335
0
    CBB_cleanup(&cbb);
336
0
    return -1;
337
0
  }
338
0
  return CBB_finish_i2d(&cbb, outp);
339
0
}
340
341
0
DSA *d2i_DSAparams(DSA **out, const uint8_t **inp, long len) {
342
0
  if (len < 0) {
343
0
    return NULL;
344
0
  }
345
0
  CBS cbs;
346
0
  CBS_init(&cbs, *inp, (size_t)len);
347
0
  DSA *ret = DSA_parse_parameters(&cbs);
348
0
  if (ret == NULL) {
349
0
    return NULL;
350
0
  }
351
0
  if (out != NULL) {
352
0
    DSA_free(*out);
353
0
    *out = ret;
354
0
  }
355
0
  *inp = CBS_data(&cbs);
356
0
  return ret;
357
0
}
358
359
0
int i2d_DSAparams(const DSA *in, uint8_t **outp) {
360
0
  CBB cbb;
361
0
  if (!CBB_init(&cbb, 0) ||
362
0
      !DSA_marshal_parameters(&cbb, in)) {
363
0
    CBB_cleanup(&cbb);
364
0
    return -1;
365
0
  }
366
0
  return CBB_finish_i2d(&cbb, outp);
367
0
}