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

randombit / botan / 19012754211

02 Nov 2025 01:10PM UTC coverage: 90.677% (+0.006%) from 90.671%
19012754211

push

github

web-flow
Merge pull request #5137 from randombit/jack/clang-tidy-includes

Remove various unused includes flagged by clang-tidy misc-include-cleaner

100457 of 110786 relevant lines covered (90.68%)

12189873.8 hits per line

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

95.35
/src/lib/tls/msg_cert_verify.cpp
1
/*
2
* Certificate Verify Message
3
* (C) 2004,2006,2011,2012 Jack Lloyd
4
*     2017 Harry Reimann, Rohde & Schwarz Cybersecurity
5
*     2021 Elektrobit Automotive GmbH
6
*     2022 René Meusel, Hannes Rantzsch - neXenio GmbH
7
*
8
* Botan is released under the Simplified BSD License (see license.txt)
9
*/
10

11
#include <botan/tls_messages.h>
12

13
#include <botan/credentials_manager.h>
14
#include <botan/pk_keys.h>
15
#include <botan/tls_extensions.h>
16
#include <botan/internal/stl_util.h>
17
#include <botan/internal/target_info.h>
18
#include <botan/internal/tls_handshake_io.h>
19
#include <botan/internal/tls_handshake_state.h>
20
#include <botan/internal/tls_reader.h>
21

22
namespace Botan::TLS {
23

24
/*
25
* Create a new Certificate Verify message for TLS 1.2
26
*/
27
Certificate_Verify_12::Certificate_Verify_12(Handshake_IO& io,
92✔
28
                                             Handshake_State& state,
29
                                             const Policy& policy,
30
                                             RandomNumberGenerator& rng,
31
                                             const Private_Key* priv_key) {
92✔
32
   BOTAN_ASSERT_NONNULL(priv_key);
92✔
33

34
   std::pair<std::string, Signature_Format> format = state.choose_sig_format(*priv_key, m_scheme, true, policy);
92✔
35

36
   m_signature =
89✔
37
      state.callbacks().tls_sign_message(*priv_key, rng, format.first, format.second, state.hash().get_contents());
89✔
38

39
   state.hash().update(io.send(*this));
267✔
40
}
92✔
41

42
/*
43
* Deserialize a Certificate Verify message
44
*/
45
Certificate_Verify::Certificate_Verify(const std::vector<uint8_t>& buf) {
528✔
46
   TLS_Data_Reader reader("CertificateVerify", buf);
528✔
47

48
   m_scheme = Signature_Scheme(reader.get_uint16_t());
528✔
49
   m_signature = reader.get_range<uint8_t>(2, 0, 65535);
527✔
50
   reader.assert_done();
526✔
51

52
   if(!m_scheme.is_set()) {
522✔
53
      throw Decoding_Error("Counterparty did not send hash/sig IDS");
×
54
   }
55
}
528✔
56

57
/*
58
* Serialize a Certificate Verify message
59
*/
60
std::vector<uint8_t> Certificate_Verify::serialize() const {
399✔
61
   BOTAN_ASSERT_NOMSG(m_scheme.is_set());
399✔
62
   std::vector<uint8_t> buf;
399✔
63
   buf.reserve(2 + 2 + m_signature.size());  // work around GCC warning
399✔
64

65
   const auto code = m_scheme.wire_code();
399✔
66
   buf.push_back(get_byte<0>(code));
399✔
67
   buf.push_back(get_byte<1>(code));
399✔
68

69
   if(m_signature.size() > 0xFFFF) {
399✔
70
      throw Encoding_Error("Certificate_Verify signature too long to encode");
×
71
   }
72

73
   const uint16_t sig_len = static_cast<uint16_t>(m_signature.size());
399✔
74
   buf.push_back(get_byte<0>(sig_len));
399✔
75
   buf.push_back(get_byte<1>(sig_len));
399✔
76
   buf += m_signature;
399✔
77

78
   return buf;
399✔
79
}
×
80

81
bool Certificate_Verify_12::verify(const X509_Certificate& cert,
168✔
82
                                   const Handshake_State& state,
83
                                   const Policy& policy) const {
84
   auto key = cert.subject_public_key();
168✔
85

86
   policy.check_peer_key_acceptable(*key);
168✔
87

88
   std::pair<std::string, Signature_Format> format =
168✔
89
      state.parse_sig_format(*key, m_scheme, state.client_hello()->signature_schemes(), true, policy);
172✔
90

91
   const bool signature_valid =
164✔
92
      state.callbacks().tls_verify_message(*key, format.first, format.second, state.hash().get_contents(), m_signature);
164✔
93

94
#if defined(BOTAN_UNSAFE_FUZZER_MODE)
95
   BOTAN_UNUSED(signature_valid);
96
   return true;
97

98
#else
99
   return signature_valid;
164✔
100

101
#endif
102
}
328✔
103

104
#if defined(BOTAN_HAS_TLS_13)
105

106
namespace {
107

108
std::vector<uint8_t> message(Connection_Side side, const Transcript_Hash& hash) {
620✔
109
   std::vector<uint8_t> msg(64, 0x20);
620✔
110
   msg.reserve(64 + 33 + 1 + hash.size());
620✔
111

112
   const std::string context_string = (side == TLS::Connection_Side::Server) ? "TLS 1.3, server CertificateVerify"
620✔
113
                                                                             : "TLS 1.3, client CertificateVerify";
713✔
114

115
   msg.insert(msg.end(), context_string.cbegin(), context_string.cend());
620✔
116
   msg.push_back(0x00);
620✔
117

118
   msg.insert(msg.end(), hash.cbegin(), hash.cend());
620✔
119
   return msg;
620✔
120
}
620✔
121

122
Signature_Scheme choose_signature_scheme(const Private_Key& key,
302✔
123
                                         const std::vector<Signature_Scheme>& allowed_schemes,
124
                                         const std::vector<Signature_Scheme>& peer_allowed_schemes) {
125
   for(Signature_Scheme scheme : allowed_schemes) {
555✔
126
      if(scheme.is_available() && scheme.is_suitable_for(key) && value_exists(peer_allowed_schemes, scheme)) {
860✔
127
         return scheme;
297✔
128
      }
129
   }
130

131
   throw TLS_Exception(Alert::HandshakeFailure, "Failed to agree on a signature algorithm");
5✔
132
}
133

134
}  // namespace
135

136
/*
137
* Create a new Certificate Verify message for TLS 1.3
138
*/
139
Certificate_Verify_13::Certificate_Verify_13(const Certificate_13& certificate_msg,
302✔
140
                                             const std::vector<Signature_Scheme>& peer_allowed_schemes,
141
                                             std::string_view hostname,
142
                                             const Transcript_Hash& hash,
143
                                             Connection_Side whoami,
144
                                             Credentials_Manager& creds_mgr,
145
                                             const Policy& policy,
146
                                             Callbacks& callbacks,
147
                                             RandomNumberGenerator& rng) :
302✔
148
      m_side(whoami) {
302✔
149
   BOTAN_ASSERT_NOMSG(!certificate_msg.empty());
302✔
150

151
   const std::string op_type((m_side == Connection_Side::Client) ? "tls-client" : "tls-server");
558✔
152
   const auto context = std::string(hostname);
610✔
153

154
   const auto private_key = (certificate_msg.has_certificate_chain())
302✔
155
                               ? creds_mgr.private_key_for(certificate_msg.leaf(), op_type, context)
302✔
156
                               : creds_mgr.private_key_for(*certificate_msg.public_key(), op_type, context);
310✔
157
   if(!private_key) {
302✔
158
      throw TLS_Exception(Alert::InternalError, "Application did not provide a private key for its credential");
×
159
   }
160

161
   m_scheme = choose_signature_scheme(*private_key, policy.allowed_signature_schemes(), peer_allowed_schemes);
302✔
162
   BOTAN_ASSERT_NOMSG(m_scheme.is_available());
297✔
163
   BOTAN_ASSERT_NOMSG(m_scheme.is_compatible_with(Protocol_Version::TLS_V13));
297✔
164

165
   m_signature = callbacks.tls_sign_message(
593✔
166
      *private_key, rng, m_scheme.padding_string(), m_scheme.format().value(), message(m_side, hash));
896✔
167
}
314✔
168

169
Certificate_Verify_13::Certificate_Verify_13(const std::vector<uint8_t>& buf, const Connection_Side side) :
353✔
170
      Certificate_Verify(buf), m_side(side) {
353✔
171
   if(!m_scheme.is_available()) {
351✔
172
      throw TLS_Exception(Alert::IllegalParameter, "Peer sent unknown signature scheme");
2✔
173
   }
174

175
   if(!m_scheme.is_compatible_with(Protocol_Version::TLS_V13)) {
349✔
176
      throw TLS_Exception(Alert::IllegalParameter, "Peer sent signature algorithm that is not suitable for TLS 1.3");
12✔
177
   }
178
}
351✔
179

180
/*
181
* Verify a Certificate Verify message
182
*/
183
bool Certificate_Verify_13::verify(const Public_Key& public_key,
330✔
184
                                   Callbacks& callbacks,
185
                                   const Transcript_Hash& transcript_hash) const {
186
   BOTAN_ASSERT_NOMSG(m_scheme.is_available());
330✔
187

188
   // RFC 8446 4.2.3
189
   //    The keys found in certificates MUST [...] be of appropriate type for
190
   //    the signature algorithms they are used with.
191
   if(m_scheme.key_algorithm_identifier() != public_key.algorithm_identifier()) {
330✔
192
      throw TLS_Exception(Alert::IllegalParameter, "Signature algorithm does not match certificate's public key");
7✔
193
   }
194

195
   const bool signature_valid = callbacks.tls_verify_message(
323✔
196
      public_key, m_scheme.padding_string(), m_scheme.format().value(), message(m_side, transcript_hash), m_signature);
646✔
197

198
   #if defined(BOTAN_UNSAFE_FUZZER_MODE)
199
   BOTAN_UNUSED(signature_valid);
200
   return true;
201
   #else
202
   return signature_valid;
323✔
203
   #endif
204
}
205

206
#endif  // BOTAN_HAS_TLS_13
207

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