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

randombit / botan / 20300409060

17 Dec 2025 10:55AM UTC coverage: 90.526% (+0.2%) from 90.36%
20300409060

Pull #5167

github

web-flow
Merge b37a2cfb3 into 3d96b675e
Pull Request #5167: Changes to reduce unnecessary inclusions

101175 of 111763 relevant lines covered (90.53%)

12570909.12 hits per line

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

98.63
/src/lib/tls/msg_cert_req.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.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/tls_handshake_hash.h>
17
#include <botan/internal/tls_handshake_io.h>
18
#include <botan/internal/tls_reader.h>
19

20
namespace Botan::TLS {
21

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

26
namespace {
27

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

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

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

50
}  // namespace
51

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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