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

randombit / botan / 13629762785

03 Mar 2025 11:24AM UTC coverage: 91.686% (-0.008%) from 91.694%
13629762785

push

github

web-flow
Merge pull request #4740 from randombit/jack/more-build-h-cleanup

Small build.h cleanups/removals

95827 of 104516 relevant lines covered (91.69%)

11258003.53 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_algos.h>
16
#include <botan/tls_extensions.h>
17
#include <botan/internal/stl_util.h>
18
#include <botan/internal/target_info.h>
19
#include <botan/internal/tls_handshake_io.h>
20
#include <botan/internal/tls_handshake_state.h>
21
#include <botan/internal/tls_reader.h>
22

23
namespace Botan::TLS {
24

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

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

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

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

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

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

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

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

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

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

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

79
   return buf;
399✔
80
}
×
81

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

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

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

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

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

99
#else
100
   return signature_valid;
164✔
101

102
#endif
103
}
328✔
104

105
#if defined(BOTAN_HAS_TLS_13)
106

107
namespace {
108

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

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

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

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

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

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

135
}  // namespace
136

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

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

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

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

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

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

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

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

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

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

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

207
#endif  // BOTAN_HAS_TLS_13
208

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