88* Botan is released under the Simplified BSD License (see license.txt)
99*/
1010
11+ #include " botan/internal/stl_util.h"
1112#include < botan/tls_messages.h>
1213
1314#include < botan/internal/tls_handshake_io.h>
1920
2021namespace Botan ::TLS {
2122
23+ namespace {
24+
25+ std::vector<uint8_t > message (Connection_Side side, const Transcript_Hash& hash)
26+ {
27+ std::vector<uint8_t > msg (64 , 0x20 );
28+ msg.reserve (64 + 32 + 1 + hash.size ());
29+
30+ const std::string context_string = (side == Botan::TLS::Connection_Side::SERVER)
31+ ? " TLS 1.3, server CertificateVerify"
32+ : " TLS 1.3, client CertificateVerify" ;
33+
34+ msg.insert (msg.end (), context_string.cbegin (), context_string.cend ());
35+ msg.push_back (0x00 );
36+
37+ msg.insert (msg.end (), hash.cbegin (), hash.cend ());
38+ return msg;
39+ }
40+
41+ Signature_Scheme choose_signature_scheme (
42+ const Private_Key& key,
43+ const std::vector<Signature_Scheme>& allowed_schemes,
44+ const std::vector<Signature_Scheme>& peer_allowed_schemes)
45+ {
46+ for (Signature_Scheme scheme : allowed_schemes)
47+ {
48+ if (scheme.is_available ()
49+ && scheme.is_suitable_for (key)
50+ && value_exists (peer_allowed_schemes, scheme))
51+ {
52+ return scheme;
53+ }
54+ }
55+
56+ throw TLS_Exception (Alert::HANDSHAKE_FAILURE, " Failed to agree on a signature algorithm" );
57+ }
58+
59+ }
60+
2261/*
23- * Create a new Certificate Verify message
62+ * Create a new Certificate Verify message for TLS 1.2
2463*/
25- Certificate_Verify::Certificate_Verify (Handshake_IO& io,
26- Handshake_State& state,
27- const Policy& policy,
28- RandomNumberGenerator& rng,
29- const Private_Key* priv_key)
64+ Certificate_Verify_12::Certificate_Verify_12 (Handshake_IO& io,
65+ Handshake_State& state,
66+ const Policy& policy,
67+ RandomNumberGenerator& rng,
68+ const Private_Key* priv_key)
3069 {
3170 BOTAN_ASSERT_NONNULL (priv_key);
3271
@@ -106,8 +145,35 @@ bool Certificate_Verify_12::verify(const X509_Certificate& cert,
106145
107146#if defined(BOTAN_HAS_TLS_13)
108147
148+ /*
149+ * Create a new Certificate Verify message for TLS 1.3
150+ */
151+ Certificate_Verify_13::Certificate_Verify_13 (
152+ const std::vector<Signature_Scheme>& peer_allowed_schemes,
153+ Connection_Side whoami,
154+ const Private_Key& key,
155+ const Policy& policy,
156+ const Transcript_Hash& hash,
157+ Callbacks& callbacks,
158+ RandomNumberGenerator& rng)
159+ : m_side(whoami)
160+ {
161+ m_scheme = choose_signature_scheme (key, policy.allowed_signature_schemes (), peer_allowed_schemes);
162+ BOTAN_ASSERT_NOMSG (m_scheme.is_available ());
163+
164+ // we need to verify that the provided private key is strong enough for TLS 1.3
165+
166+ m_signature =
167+ callbacks.tls_sign_message (key,
168+ rng,
169+ m_scheme.padding_string (),
170+ m_scheme.format ().value (),
171+ message (m_side, hash));
172+ }
173+
174+
109175Certificate_Verify_13::Certificate_Verify_13 (const std::vector<uint8_t >& buf,
110- const Connection_Side side)
176+ const Connection_Side side)
111177 : Certificate_Verify(buf)
112178 , m_side(side)
113179 {
@@ -133,24 +199,12 @@ bool Certificate_Verify_13::verify(const X509_Certificate& cert,
133199 if (m_scheme.algorithm_identifier () != cert.subject_public_key_algo ())
134200 { throw TLS_Exception (Alert::ILLEGAL_PARAMETER, " Signature algorithm does not match certificate's public key" ); }
135201
136- std::vector<uint8_t > msg (64 , 0x20 );
137- msg.reserve (64 + 32 + 1 + transcript_hash.size ());
138-
139- const std::string context_string = (m_side == Botan::TLS::Connection_Side::SERVER)
140- ? " TLS 1.3, server CertificateVerify"
141- : " TLS 1.3, client CertificateVerify" ;
142-
143- msg.insert (msg.end (), context_string.cbegin (), context_string.cend ());
144- msg.push_back (0x00 );
145-
146- msg.insert (msg.end (), transcript_hash.cbegin (), transcript_hash.cend ());
147-
148202 const auto key = cert.load_subject_public_key ();
149203 const bool signature_valid =
150204 callbacks.tls_verify_message (*key,
151205 m_scheme.padding_string (),
152206 m_scheme.format ().value (),
153- msg ,
207+ message (m_side, transcript_hash) ,
154208 m_signature);
155209
156210#if defined(BOTAN_UNSAFE_FUZZER_MODE)
0 commit comments