Coverage Report

Created: 2026-06-15 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/dh/dh_asn1.cc
Line
Count
Source
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
using namespace bssl;
29
30
0
static int parse_integer(CBS *cbs, UniquePtr<BIGNUM> *out) {
31
0
  assert(*out == nullptr);
32
0
  out->reset(BN_new());
33
0
  if (*out == nullptr) {
34
0
    return 0;
35
0
  }
36
0
  return BN_parse_asn1_unsigned(cbs, out->get());
37
0
}
38
39
0
static int marshal_integer(CBB *cbb, BIGNUM *bn) {
40
0
  if (bn == nullptr) {
41
    // A DH object may be missing some components.
42
0
    OPENSSL_PUT_ERROR(DH, ERR_R_PASSED_NULL_PARAMETER);
43
0
    return 0;
44
0
  }
45
0
  return BN_marshal_asn1(cbb, bn);
46
0
}
47
48
0
DH *DH_parse_parameters(CBS *cbs) {
49
0
  UniquePtr<DH> ret(DH_new());
50
0
  if (ret == nullptr) {
51
0
    return nullptr;
52
0
  }
53
54
0
  CBS child;
55
0
  auto *impl = FromOpaque(ret.get());
56
0
  if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
57
0
      !parse_integer(&child, &impl->p) ||  //
58
0
      !parse_integer(&child, &impl->g)) {
59
0
    OPENSSL_PUT_ERROR(DH, DH_R_DECODE_ERROR);
60
0
    return nullptr;
61
0
  }
62
63
0
  uint64_t priv_length;
64
0
  if (CBS_len(&child) != 0) {
65
0
    if (!CBS_get_asn1_uint64(&child, &priv_length) ||
66
0
        priv_length > UINT_MAX) {
67
0
      OPENSSL_PUT_ERROR(DH, DH_R_DECODE_ERROR);
68
0
      return nullptr;
69
0
    }
70
0
    impl->priv_length = (unsigned)priv_length;
71
0
  }
72
73
0
  if (CBS_len(&child) != 0) {
74
0
    OPENSSL_PUT_ERROR(DH, DH_R_DECODE_ERROR);
75
0
    return nullptr;
76
0
  }
77
78
0
  if (!dh_check_params_fast(ret.get())) {
79
0
    OPENSSL_PUT_ERROR(DH, DH_R_DECODE_ERROR);
80
0
    return nullptr;
81
0
  }
82
83
0
  return ret.release();
84
0
}
85
86
0
int DH_marshal_parameters(CBB *cbb, const DH *dh) {
87
0
  CBB child;
88
0
  auto *impl = FromOpaque(dh);
89
0
  if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
90
0
      !marshal_integer(&child, impl->p.get()) ||
91
0
      !marshal_integer(&child, impl->g.get()) ||
92
0
      (impl->priv_length != 0 &&
93
0
       !CBB_add_asn1_uint64(&child, impl->priv_length)) ||
94
0
      !CBB_flush(cbb)) {
95
0
    OPENSSL_PUT_ERROR(DH, DH_R_ENCODE_ERROR);
96
0
    return 0;
97
0
  }
98
0
  return 1;
99
0
}
100
101
0
DH *d2i_DHparams(DH **out, const uint8_t **inp, long len) {
102
0
  return D2IFromCBS(out, inp, len, DH_parse_parameters);
103
0
}
104
105
0
int i2d_DHparams(const DH *in, uint8_t **outp) {
106
0
  return I2DFromCBB(
107
0
      /*initial_capacity=*/256, outp,
108
0
      [&](CBB *cbb) -> bool { return DH_marshal_parameters(cbb, in); });
109
0
}