Botan 3.4.0
Crypto and TLS for C&
msg_cert_verify.cpp
Go to the documentation of this file.
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/tls_handshake_io.h>
19#include <botan/internal/tls_handshake_state.h>
20#include <botan/internal/tls_reader.h>
21
22namespace Botan::TLS {
23
24/*
25* Create a new Certificate Verify message for TLS 1.2
26*/
28 Handshake_State& state,
29 const Policy& policy,
31 const Private_Key* priv_key) {
32 BOTAN_ASSERT_NONNULL(priv_key);
33
34 std::pair<std::string, Signature_Format> format = state.choose_sig_format(*priv_key, m_scheme, true, policy);
35
37 state.callbacks().tls_sign_message(*priv_key, rng, format.first, format.second, state.hash().get_contents());
38
39 state.hash().update(io.send(*this));
40}
41
42/*
43* Deserialize a Certificate Verify message
44*/
45Certificate_Verify::Certificate_Verify(const std::vector<uint8_t>& buf) {
46 TLS_Data_Reader reader("CertificateVerify", buf);
47
49 m_signature = reader.get_range<uint8_t>(2, 0, 65535);
50 reader.assert_done();
51
52 if(!m_scheme.is_set()) {
53 throw Decoding_Error("Counterparty did not send hash/sig IDS");
54 }
55}
56
57/*
58* Serialize a Certificate Verify message
59*/
60std::vector<uint8_t> Certificate_Verify::serialize() const {
62 std::vector<uint8_t> buf;
63 buf.reserve(2 + 2 + m_signature.size()); // work around GCC warning
64
65 const auto code = m_scheme.wire_code();
66 buf.push_back(get_byte<0>(code));
67 buf.push_back(get_byte<1>(code));
68
69 if(m_signature.size() > 0xFFFF) {
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());
74 buf.push_back(get_byte<0>(sig_len));
75 buf.push_back(get_byte<1>(sig_len));
76 buf += m_signature;
77
78 return buf;
79}
80
82 const Handshake_State& state,
83 const Policy& policy) const {
84 auto key = cert.subject_public_key();
85
86 policy.check_peer_key_acceptable(*key);
87
88 std::pair<std::string, Signature_Format> format =
89 state.parse_sig_format(*key, m_scheme, state.client_hello()->signature_schemes(), true, policy);
90
91 const bool signature_valid =
92 state.callbacks().tls_verify_message(*key, format.first, format.second, state.hash().get_contents(), m_signature);
93
94#if defined(BOTAN_UNSAFE_FUZZER_MODE)
95 BOTAN_UNUSED(signature_valid);
96 return true;
97
98#else
99 return signature_valid;
100
101#endif
102}
103
104#if defined(BOTAN_HAS_TLS_13)
105
106namespace {
107
108std::vector<uint8_t> message(Connection_Side side, const Transcript_Hash& hash) {
109 std::vector<uint8_t> msg(64, 0x20);
110 msg.reserve(64 + 33 + 1 + hash.size());
111
112 const std::string context_string = (side == TLS::Connection_Side::Server) ? "TLS 1.3, server CertificateVerify"
113 : "TLS 1.3, client CertificateVerify";
114
115 msg.insert(msg.end(), context_string.cbegin(), context_string.cend());
116 msg.push_back(0x00);
117
118 msg.insert(msg.end(), hash.cbegin(), hash.cend());
119 return msg;
120}
121
122Signature_Scheme choose_signature_scheme(const Private_Key& key,
123 const std::vector<Signature_Scheme>& allowed_schemes,
124 const std::vector<Signature_Scheme>& peer_allowed_schemes) {
125 for(Signature_Scheme scheme : allowed_schemes) {
126 if(scheme.is_available() && scheme.is_suitable_for(key) && value_exists(peer_allowed_schemes, scheme)) {
127 return scheme;
128 }
129 }
130
131 throw TLS_Exception(Alert::HandshakeFailure, "Failed to agree on a signature algorithm");
132}
133
134} // namespace
135
136/*
137* Create a new Certificate Verify message for TLS 1.3
138*/
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,
148 m_side(whoami) {
149 BOTAN_ASSERT_NOMSG(!certificate_msg.empty());
150
151 const auto op_type = (m_side == Connection_Side::Client) ? "tls-client" : "tls-server";
152 const auto context = std::string(hostname);
153
154 const auto private_key = (certificate_msg.has_certificate_chain())
155 ? creds_mgr.private_key_for(certificate_msg.leaf(), op_type, context)
156 : creds_mgr.private_key_for(*certificate_msg.public_key(), op_type, context);
157 if(!private_key) {
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);
163 BOTAN_ASSERT_NOMSG(m_scheme.is_compatible_with(Protocol_Version::TLS_V13));
164
165 m_signature = callbacks.tls_sign_message(
166 *private_key, rng, m_scheme.padding_string(), m_scheme.format().value(), message(m_side, hash));
167}
168
169Certificate_Verify_13::Certificate_Verify_13(const std::vector<uint8_t>& buf, const Connection_Side side) :
170 Certificate_Verify(buf), m_side(side) {
171 if(!m_scheme.is_available()) {
172 throw TLS_Exception(Alert::HandshakeFailure, "Peer sent unknown signature scheme");
173 }
174
175 if(!m_scheme.is_compatible_with(Protocol_Version::TLS_V13)) {
176 throw TLS_Exception(Alert::IllegalParameter, "Peer sent signature algorithm that is not suitable for TLS 1.3");
177 }
178}
179
180/*
181* Verify a Certificate Verify message
182*/
184 Callbacks& callbacks,
185 const Transcript_Hash& transcript_hash) const {
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.
192 throw TLS_Exception(Alert::IllegalParameter, "Signature algorithm does not match certificate's public key");
193 }
194
195 const bool signature_valid = callbacks.tls_verify_message(
196 public_key, m_scheme.padding_string(), m_scheme.format().value(), message(m_side, transcript_hash), m_signature);
197
198 #if defined(BOTAN_UNSAFE_FUZZER_MODE)
199 BOTAN_UNUSED(signature_valid);
200 return true;
201 #else
202 return signature_valid;
203 #endif
204}
205
206#endif // BOTAN_HAS_TLS_13
207
208} // namespace Botan::TLS
#define BOTAN_UNUSED
Definition assert.h:118
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:86
virtual std::shared_ptr< Private_Key > private_key_for(const X509_Certificate &cert, const std::string &type, const std::string &context)
virtual AlgorithmIdentifier algorithm_identifier() const =0
virtual std::vector< uint8_t > tls_sign_message(const Private_Key &key, RandomNumberGenerator &rng, std::string_view padding, Signature_Format format, const std::vector< uint8_t > &msg)
virtual bool tls_verify_message(const Public_Key &key, std::string_view padding, Signature_Format format, const std::vector< uint8_t > &msg, const std::vector< uint8_t > &sig)
const X509_Certificate & leaf() const
std::shared_ptr< const Public_Key > public_key() const
bool verify(const X509_Certificate &cert, const Handshake_State &state, const Policy &policy) const
Certificate_Verify_12(Handshake_IO &io, Handshake_State &state, const Policy &policy, RandomNumberGenerator &rng, const Private_Key *key)
bool verify(const Public_Key &public_key, Callbacks &callbacks, const Transcript_Hash &transcript_hash) const
Certificate_Verify_13(const std::vector< uint8_t > &buf, Connection_Side side)
std::vector< uint8_t > serialize() const override
std::vector< uint8_t > m_signature
const std::vector< uint8_t > & get_contents() const
void update(const uint8_t in[], size_t length)
virtual std::vector< uint8_t > send(const Handshake_Message &msg)=0
std::pair< std::string, Signature_Format > parse_sig_format(const Public_Key &key, Signature_Scheme scheme, const std::vector< Signature_Scheme > &offered_schemes, bool for_client_auth, const Policy &policy) const
void client_hello(Client_Hello_12 *client_hello)
std::pair< std::string, Signature_Format > choose_sig_format(const Private_Key &key, Signature_Scheme &scheme, bool for_client_auth, const Policy &policy) const
virtual void check_peer_key_acceptable(const Public_Key &public_key) const
virtual std::vector< Signature_Scheme > allowed_signature_schemes() const
bool is_compatible_with(const Protocol_Version &protocol_version) const noexcept
AlgorithmIdentifier key_algorithm_identifier() const noexcept
Signature_Scheme::Code wire_code() const noexcept
std::optional< Signature_Format > format() const noexcept
std::string padding_string() const noexcept
std::vector< T > get_range(size_t len_bytes, size_t min_elems, size_t max_elems)
Definition tls_reader.h:105
std::unique_ptr< Public_Key > subject_public_key() const
Definition x509cert.cpp:546
std::vector< uint8_t > Transcript_Hash
Definition tls_magic.h:81
bool value_exists(const std::vector< T > &vec, const OT &val)
Definition stl_util.h:118