Coverage Report

Created: 2025-06-24 07:00

/src/boringssl/crypto/dh/dh_asn1.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2000-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/dh.h>
16
17
#include <assert.h>
18
#include <limits.h>
19
20
#include <openssl/bn.h>
21
#include <openssl/bytestring.h>
22
#include <openssl/err.h>
23
24
#include "../bytestring/internal.h"
25
#include "../fipsmodule/dh/internal.h"
26
27
28
0
static int parse_integer(CBS *cbs, BIGNUM **out) {
29
0
  assert(*out == NULL);
30
0
  *out = BN_new();
31
0
  if (*out == NULL) {
32
0
    return 0;
33
0
  }
34
0
  return BN_parse_asn1_unsigned(cbs, *out);
35
0
}
36
37
0
static int marshal_integer(CBB *cbb, BIGNUM *bn) {
38
0
  if (bn == NULL) {
39
    // A DH object may be missing some components.
40
0
    OPENSSL_PUT_ERROR(DH, ERR_R_PASSED_NULL_PARAMETER);
41
0
    return 0;
42
0
  }
43
0
  return BN_marshal_asn1(cbb, bn);
44
0
}
45
46
0
DH *DH_parse_parameters(CBS *cbs) {
47
0
  bssl::UniquePtr<DH> ret(DH_new());
48
0
  if (ret == nullptr) {
49
0
    return nullptr;
50
0
  }
51
52
0
  CBS child;
53
0
  if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
54
0
      !parse_integer(&child, &ret->p) ||
55
0
      !parse_integer(&child, &ret->g)) {
56
0
    OPENSSL_PUT_ERROR(DH, DH_R_DECODE_ERROR);
57
0
    return nullptr;
58
0
  }
59
60
0
  uint64_t priv_length;
61
0
  if (CBS_len(&child) != 0) {
62
0
    if (!CBS_get_asn1_uint64(&child, &priv_length) ||
63
0
        priv_length > UINT_MAX) {
64
0
      OPENSSL_PUT_ERROR(DH, DH_R_DECODE_ERROR);
65
0
      return nullptr;
66
0
    }
67
0
    ret->priv_length = (unsigned)priv_length;
68
0
  }
69
70
0
  if (CBS_len(&child) != 0) {
71
0
    OPENSSL_PUT_ERROR(DH, DH_R_DECODE_ERROR);
72
0
    return nullptr;
73
0
  }
74
75
0
  if (!dh_check_params_fast(ret.get())) {
76
0
    OPENSSL_PUT_ERROR(DH, DH_R_DECODE_ERROR);
77
0
    return nullptr;
78
0
  }
79
80
0
  return ret.release();
81
0
}
82
83
0
int DH_marshal_parameters(CBB *cbb, const DH *dh) {
84
0
  CBB child;
85
0
  if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
86
0
      !marshal_integer(&child, dh->p) ||
87
0
      !marshal_integer(&child, dh->g) ||
88
0
      (dh->priv_length != 0 &&
89
0
       !CBB_add_asn1_uint64(&child, dh->priv_length)) ||
90
0
      !CBB_flush(cbb)) {
91
0
    OPENSSL_PUT_ERROR(DH, DH_R_ENCODE_ERROR);
92
0
    return 0;
93
0
  }
94
0
  return 1;
95
0
}
96
97
0
DH *d2i_DHparams(DH **out, const uint8_t **inp, long len) {
98
0
  if (len < 0) {
99
0
    return NULL;
100
0
  }
101
0
  CBS cbs;
102
0
  CBS_init(&cbs, *inp, (size_t)len);
103
0
  DH *ret = DH_parse_parameters(&cbs);
104
0
  if (ret == NULL) {
105
0
    return NULL;
106
0
  }
107
0
  if (out != NULL) {
108
0
    DH_free(*out);
109
0
    *out = ret;
110
0
  }
111
0
  *inp = CBS_data(&cbs);
112
0
  return ret;
113
0
}
114
115
0
int i2d_DHparams(const DH *in, uint8_t **outp) {
116
0
  CBB cbb;
117
0
  if (!CBB_init(&cbb, 0) ||
118
0
      !DH_marshal_parameters(&cbb, in)) {
119
0
    CBB_cleanup(&cbb);
120
0
    return -1;
121
0
  }
122
0
  return CBB_finish_i2d(&cbb, outp);
123
0
}