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

randombit / botan / 21753596263

06 Feb 2026 02:13PM UTC coverage: 90.063% (-0.01%) from 90.073%
21753596263

Pull #5289

github

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

102237 of 113517 relevant lines covered (90.06%)

11402137.11 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 {
734✔
24
   return Handshake_Type::CertificateRequest;
734✔
25
}
26

27
namespace {
28

29
std::string cert_type_code_to_name(uint8_t code) {
406✔
30
   switch(code) {
406✔
31
      case 1:
196✔
32
         return "RSA";
196✔
33
      case 64:
197✔
34
         return "ECDSA";
197✔
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) {
558✔
41
   if(name == "RSA") {
558✔
42
      return 1;
279✔
43
   }
44
   if(name == "ECDSA") {
279✔
45
      return 64;
279✔
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,
279✔
57
                                               Handshake_Hash& hash,
58
                                               const Policy& policy,
59
                                               const std::vector<X509_DN>& ca_certs) :
279✔
60
      m_names(ca_certs), m_cert_key_types({"RSA", "ECDSA"}) {
1,395✔
61
   m_schemes = policy.acceptable_signature_schemes();
279✔
62
   hash.update(io.send(*this));
558✔
63
}
837✔
64

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

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

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

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

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

84
      m_cert_key_types.emplace_back(cert_type_name);
393✔
85
   }
406✔
86

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

89
   if(algs.size() % 2 != 0) {
197✔
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,889✔
94
      m_schemes.emplace_back(make_uint16(algs[i], algs[i + 1]));
1,697✔
95
   }
96

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

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

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

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

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

117
const std::vector<X509_DN>& Certificate_Request_12::acceptable_CAs() const {
193✔
118
   return m_names;
193✔
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 {
279✔
129
   std::vector<uint8_t> buf;
279✔
130

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

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

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

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

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

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

150
      append_tls_length_value(encoded_names, encoder.get_contents(), 2);
686✔
151
   }
343✔
152

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

155
   return buf;
279✔
156
}
558✔
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