• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 21768358452

06 Feb 2026 10:35PM UTC coverage: 90.064% (-0.003%) from 90.067%
21768358452

Pull #5289

github

web-flow
Merge f589db195 into 8ea0ca252
Pull Request #5289: Further misc header reductions, forward declarations, etc

102238 of 113517 relevant lines covered (90.06%)

11357432.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.63
/src/lib/tls/tls12/msg_certificate_req_12.cpp
1
/*
2
* Certificate Request Message
3
* (C) 2004-2006,2012 Jack Lloyd
4
*     2021 Elektrobit Automotive GmbH
5
*     2022 René Meusel, Hannes Rantzsch - neXenio GmbH
6
*
7
* Botan is released under the Simplified BSD License (see license.txt)
8
*/
9

10
#include <botan/tls_messages_12.h>
11

12
#include <botan/ber_dec.h>
13
#include <botan/der_enc.h>
14
#include <botan/tls_extensions.h>
15
#include <botan/tls_policy.h>
16
#include <botan/internal/fmt.h>
17
#include <botan/internal/tls_handshake_hash.h>
18
#include <botan/internal/tls_handshake_io.h>
19
#include <botan/internal/tls_reader.h>
20

21
namespace Botan::TLS {
22

23
Handshake_Type Certificate_Request_12::type() const {
729✔
24
   return Handshake_Type::CertificateRequest;
729✔
25
}
26

27
namespace {
28

29
std::string cert_type_code_to_name(uint8_t code) {
398✔
30
   switch(code) {
398✔
31
      case 1:
192✔
32
         return "RSA";
192✔
33
      case 64:
193✔
34
         return "ECDSA";
193✔
35
      default:
13✔
36
         return "";  // DH or something else
13✔
37
   }
38
}
39

40
uint8_t cert_type_name_to_code(std::string_view name) {
550✔
41
   if(name == "RSA") {
550✔
42
      return 1;
275✔
43
   }
44
   if(name == "ECDSA") {
275✔
45
      return 64;
275✔
46
   }
47

48
   throw Invalid_Argument(fmt("Unknown/unhandled TLS cert type {}", name));
×
49
}
50

51
}  // namespace
52

53
/**
54
* Create a new Certificate Request message
55
*/
56
Certificate_Request_12::Certificate_Request_12(Handshake_IO& io,
275✔
57
                                               Handshake_Hash& hash,
58
                                               const Policy& policy,
59
                                               const std::vector<X509_DN>& ca_certs) :
275✔
60
      m_names(ca_certs), m_cert_key_types({"RSA", "ECDSA"}) {
1,375✔
61
   m_schemes = policy.acceptable_signature_schemes();
275✔
62
   hash.update(io.send(*this));
550✔
63
}
825✔
64

65
/**
66
* Deserialize a Certificate Request message
67
*/
68
Certificate_Request_12::Certificate_Request_12(const std::vector<uint8_t>& buf) {
198✔
69
   if(buf.size() < 4) {
198✔
70
      throw Decoding_Error("Certificate_Req: Bad certificate request");
1✔
71
   }
72

73
   TLS_Data_Reader reader("CertificateRequest", buf);
197✔
74

75
   const auto cert_type_codes = reader.get_range_vector<uint8_t>(1, 1, 255);
197✔
76

77
   for(const auto cert_type_code : cert_type_codes) {
595✔
78
      const std::string cert_type_name = cert_type_code_to_name(cert_type_code);
398✔
79

80
      if(cert_type_name.empty()) {  // something we don't know
398✔
81
         continue;
13✔
82
      }
83

84
      m_cert_key_types.emplace_back(cert_type_name);
385✔
85
   }
398✔
86

87
   const std::vector<uint8_t> algs = reader.get_range_vector<uint8_t>(2, 2, 65534);
197✔
88

89
   if(algs.size() % 2 != 0) {
193✔
90
      throw Decoding_Error("Bad length for signature IDs in certificate request");
1✔
91
   }
92

93
   for(size_t i = 0; i != algs.size(); i += 2) {
1,849✔
94
      m_schemes.emplace_back(make_uint16(algs[i], algs[i + 1]));
1,661✔
95
   }
96

97
   const uint16_t purported_size = reader.get_uint16_t();
192✔
98

99
   if(reader.remaining_bytes() != purported_size) {
192✔
100
      throw Decoding_Error("Inconsistent length in certificate request");
3✔
101
   }
102

103
   while(reader.has_remaining()) {
355✔
104
      std::vector<uint8_t> name_bits = reader.get_range_vector<uint8_t>(2, 0, 65535);
166✔
105

106
      BER_Decoder decoder(name_bits.data(), name_bits.size());
166✔
107
      X509_DN name;
166✔
108
      decoder.decode(name);
166✔
109
      m_names.emplace_back(name);
166✔
110
   }
332✔
111
}
409✔
112

113
const std::vector<std::string>& Certificate_Request_12::acceptable_cert_types() const {
189✔
114
   return m_cert_key_types;
189✔
115
}
116

117
const std::vector<X509_DN>& Certificate_Request_12::acceptable_CAs() const {
189✔
118
   return m_names;
189✔
119
}
120

121
const std::vector<Signature_Scheme>& Certificate_Request_12::signature_schemes() const {
256✔
122
   return m_schemes;
256✔
123
}
124

125
/**
126
* Serialize a Certificate Request message
127
*/
128
std::vector<uint8_t> Certificate_Request_12::serialize() const {
275✔
129
   std::vector<uint8_t> buf;
275✔
130

131
   std::vector<uint8_t> cert_types;
275✔
132

133
   cert_types.reserve(m_cert_key_types.size());
275✔
134
   for(const auto& cert_key_type : m_cert_key_types) {
825✔
135
      cert_types.push_back(cert_type_name_to_code(cert_key_type));
550✔
136
   }
137

138
   append_tls_length_value(buf, cert_types, 1);
275✔
139

140
   if(!m_schemes.empty()) {
275✔
141
      buf += Signature_Algorithms(m_schemes).serialize(Connection_Side::Server);
550✔
142
   }
143

144
   std::vector<uint8_t> encoded_names;
275✔
145

146
   for(const auto& name : m_names) {
610✔
147
      DER_Encoder encoder;
335✔
148
      encoder.encode(name);
335✔
149

150
      append_tls_length_value(encoded_names, encoder.get_contents(), 2);
670✔
151
   }
335✔
152

153
   append_tls_length_value(buf, encoded_names, 2);
275✔
154

155
   return buf;
275✔
156
}
550✔
157
}  // namespace Botan::TLS
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc