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

randombit / botan / 11087146043

28 Sep 2024 09:28PM UTC coverage: 92.003% (+0.7%) from 91.274%
11087146043

push

github

web-flow
Create terraform.yml

82959 of 90170 relevant lines covered (92.0%)

9376319.11 hits per line

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

80.39
/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/internal/tls_reader.h>
14

15
namespace Botan::TLS {
16

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

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

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

31
   m_context = reader.get_tls_length_value(1);
120✔
32
   m_extensions.deserialize(reader, side, type());
60✔
33

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

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

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

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

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

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

93
   if(!acceptable_CAs.empty()) {
101✔
94
      m_extensions.add(std::make_unique<Certificate_Authorities>(std::move(acceptable_CAs)));
×
95
   }
96

97
   // TODO: Support cert_status_request for OCSP stapling
98

99
   // TODO: give the application a chance to modifying extensions
100
   //       (after GH #2988 is merged)
101
   // callbacks.tls_modify_extensions(m_extensions, Connection_Side::Server);
102
}
101✔
103

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

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

116
   if(client_auth_CAs.empty() && !policy.request_client_certificate_authentication()) {
267✔
117
      return std::nullopt;
166✔
118
   }
119

120
   return Certificate_Request_13(std::move(client_auth_CAs), policy, callbacks);
101✔
121
}
267✔
122

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

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

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

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

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

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