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

randombit / botan / 20579846577

29 Dec 2025 06:24PM UTC coverage: 90.415% (+0.2%) from 90.243%
20579846577

push

github

web-flow
Merge pull request #5167 from randombit/jack/src-size-reductions

Changes to reduce unnecessary inclusions

101523 of 112285 relevant lines covered (90.42%)

12817276.56 hits per line

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

88.68
/src/lib/tls/tls13/msg_certificate_req_13.cpp
1
/*
2
* (C) 2022 Jack Lloyd
3
* (C) 2022 Hannes Rantzsch, René Meusel - neXenio GmbH
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/tls_messages.h>
9

10
#include <botan/credentials_manager.h>
11
#include <botan/tls_callbacks.h>
12
#include <botan/tls_exceptn.h>
13
#include <botan/tls_policy.h>
14
#include <botan/internal/tls_reader.h>
15

16
namespace Botan::TLS {
17

18
Handshake_Type Certificate_Request_13::type() const {
489✔
19
   return TLS::Handshake_Type::CertificateRequest;
489✔
20
}
21

22
Certificate_Request_13::Certificate_Request_13(const std::vector<uint8_t>& buf, const Connection_Side side) {
68✔
23
   TLS_Data_Reader reader("Certificate_Request_13", buf);
68✔
24

25
   // RFC 8446 4.3.2
26
   //    A server which is authenticating with a certificate MAY optionally
27
   //    request a certificate from the client.
28
   if(side != Connection_Side::Server) {
29
      throw TLS_Exception(Alert::UnexpectedMessage, "Received a Certificate_Request message from a client");
×
30
   }
31

32
   m_context = reader.get_tls_length_value(1);
136✔
33
   m_extensions.deserialize(reader, side, type());
68✔
34

35
   // RFC 8446 4.3.2
36
   //    The "signature_algorithms" extension MUST be specified, and other
37
   //    extensions may optionally be included if defined for this message.
38
   //    Clients MUST ignore unrecognized extensions.
39

40
   if(!m_extensions.has<Signature_Algorithms>()) {
66✔
41
      throw TLS_Exception(Alert::MissingExtension,
1✔
42
                          "Certificate_Request message did not provide a signature_algorithms extension");
1✔
43
   }
44

45
   // RFC 8446 4.2.
46
   //    The table below indicates the messages where a given extension may
47
   //    appear [...].  If an implementation receives an extension which it
48
   //    recognizes and which is not specified for the message in which it
49
   //    appears, it MUST abort the handshake with an "illegal_parameter" alert.
50
   //
51
   // For Certificate Request said table states:
52
   //    "status_request", "signature_algorithms", "signed_certificate_timestamp",
53
   //     "certificate_authorities", "oid_filters", "signature_algorithms_cert",
54
   const std::set<Extension_Code> allowed_extensions = {
65✔
55
      Extension_Code::CertificateStatusRequest,
56
      Extension_Code::SignatureAlgorithms,
57
      // Extension_Code::SignedCertificateTimestamp,  // NYI
58
      Extension_Code::CertificateAuthorities,
59
      // Extension_Code::OidFilters,                   // NYI
60
      Extension_Code::CertSignatureAlgorithms,
61
   };
65✔
62

63
   if(m_extensions.contains_implemented_extensions_other_than(allowed_extensions)) {
65✔
64
      throw TLS_Exception(Alert::IllegalParameter, "Certificate Request contained an extension that is not allowed");
×
65
   }
66
}
71✔
67

68
Certificate_Request_13::Certificate_Request_13(std::vector<X509_DN> acceptable_CAs,
97✔
69
                                               const Policy& policy,
70
                                               Callbacks& callbacks) {
97✔
71
   // RFC 8446 4.3.2
72
   //    The certificate_request_context [here: m_context] MUST be unique within
73
   //    the scope of this connection (thus preventing replay of client
74
   //    CertificateVerify messages).  This field SHALL be zero length unless
75
   //    used for the post-handshake authentication exchanges described in
76
   //    Section 4.6.2.
77
   //
78
   // TODO: Post-Handshake auth must fill m_context in an unpredictable way
79

80
   // RFC 8446 4.3.2
81
   //    [Supported signature algorithms are] expressed by sending the
82
   //    "signature_algorithms" and optionally "signature_algorithms_cert"
83
   //    extensions. [A list of certificate authorities which the server would
84
   //    accept] is expressed by sending the "certificate_authorities" extension.
85
   //
86
   //    The "signature_algorithms" extension MUST be specified, and other
87
   //    extensions may optionally be included if defined for this message.
88
   m_extensions.add(std::make_unique<Signature_Algorithms>(policy.acceptable_signature_schemes()));
194✔
89
   if(auto cert_signing_prefs = policy.acceptable_certificate_signature_schemes()) {
97✔
90
      // RFC 8446 4.2.3
91
      //    Implementations which have the same policy in both cases MAY omit
92
      //    the "signature_algorithms_cert" extension.
93
      m_extensions.add(std::make_unique<Signature_Algorithms_Cert>(std::move(cert_signing_prefs.value())));
×
94
   }
×
95

96
   if(!acceptable_CAs.empty()) {
97✔
97
      m_extensions.add(std::make_unique<Certificate_Authorities>(std::move(acceptable_CAs)));
164✔
98
   }
99

100
   // TODO: Support cert_status_request for OCSP stapling
101

102
   callbacks.tls_modify_extensions(m_extensions, Connection_Side::Server, type());
97✔
103
}
97✔
104

105
std::optional<Certificate_Request_13> Certificate_Request_13::maybe_create(const Client_Hello_13& client_hello,
272✔
106
                                                                           Credentials_Manager& cred_mgr,
107
                                                                           Callbacks& callbacks,
108
                                                                           const Policy& policy) {
109
   const auto trusted_CAs = cred_mgr.trusted_certificate_authorities("tls-server", client_hello.sni_hostname());
544✔
110

111
   std::vector<X509_DN> client_auth_CAs;
272✔
112
   for(auto* const store : trusted_CAs) {
519✔
113
      const auto subjects = store->all_subjects();
247✔
114
      client_auth_CAs.insert(client_auth_CAs.end(), subjects.begin(), subjects.end());
247✔
115
   }
247✔
116

117
   if(client_auth_CAs.empty() && !policy.request_client_certificate_authentication()) {
272✔
118
      return std::nullopt;
175✔
119
   }
120

121
   return Certificate_Request_13(std::move(client_auth_CAs), policy, callbacks);
97✔
122
}
519✔
123

124
std::vector<X509_DN> Certificate_Request_13::acceptable_CAs() const {
55✔
125
   if(m_extensions.has<Certificate_Authorities>()) {
55✔
126
      return m_extensions.get<Certificate_Authorities>()->distinguished_names();
3✔
127
   }
128
   return {};
52✔
129
}
130

131
const std::vector<Signature_Scheme>& Certificate_Request_13::signature_schemes() const {
216✔
132
   // RFC 8446 4.3.2
133
   //    The "signature_algorithms" extension MUST be specified
134
   BOTAN_ASSERT_NOMSG(m_extensions.has<Signature_Algorithms>());
216✔
135

136
   return m_extensions.get<Signature_Algorithms>()->supported_schemes();
216✔
137
}
138

139
const std::vector<Signature_Scheme>& Certificate_Request_13::certificate_signature_schemes() const {
55✔
140
   // RFC 8446 4.2.3
141
   //   If no "signature_algorithms_cert" extension is present, then the
142
   //   "signature_algorithms" extension also applies to signatures appearing
143
   //   in certificates.
144
   if(auto* sig_schemes_cert = m_extensions.get<Signature_Algorithms_Cert>()) {
55✔
145
      return sig_schemes_cert->supported_schemes();
×
146
   } else {
147
      return signature_schemes();
55✔
148
   }
149
}
150

151
std::vector<uint8_t> Certificate_Request_13::serialize() const {
101✔
152
   std::vector<uint8_t> buf;
101✔
153
   append_tls_length_value(buf, m_context, 1);
101✔
154
   buf += m_extensions.serialize(Connection_Side::Server);
101✔
155
   return buf;
101✔
156
}
×
157

158
}  // 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