Botan  2.8.0
Crypto and TLS for C++11
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 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8 
9 #include <botan/tls_messages.h>
10 #include <botan/tls_extensions.h>
11 #include <botan/internal/tls_reader.h>
12 #include <botan/internal/tls_handshake_io.h>
13 #include <botan/internal/tls_handshake_state.h>
14 
15 namespace Botan {
16 
17 namespace TLS {
18 
19 /*
20 * Create a new Certificate Verify message
21 */
23  Handshake_State& state,
24  const Policy& policy,
26  const Private_Key* priv_key)
27  {
28  BOTAN_ASSERT_NONNULL(priv_key);
29 
30  std::pair<std::string, Signature_Format> format =
31  state.choose_sig_format(*priv_key, m_scheme, true, policy);
32 
33  m_signature =
34  state.callbacks().tls_sign_message(*priv_key, rng, format.first, format.second,
35  state.hash().get_contents());
36 
37  state.hash().update(io.send(*this));
38  }
39 
40 /*
41 * Deserialize a Certificate Verify message
42 */
43 Certificate_Verify::Certificate_Verify(const std::vector<uint8_t>& buf,
44  Protocol_Version version)
45  {
46  TLS_Data_Reader reader("CertificateVerify", buf);
47 
49  {
50  m_scheme = static_cast<Signature_Scheme>(reader.get_uint16_t());
51  }
52 
53  m_signature = reader.get_range<uint8_t>(2, 0, 65535);
54  }
55 
56 /*
57 * Serialize a Certificate Verify message
58 */
59 std::vector<uint8_t> Certificate_Verify::serialize() const
60  {
61  std::vector<uint8_t> buf;
62 
63  if(m_scheme != Signature_Scheme::NONE)
64  {
65  const uint16_t scheme_code = static_cast<uint16_t>(m_scheme);
66  buf.push_back(get_byte(0, scheme_code));
67  buf.push_back(get_byte(1, scheme_code));
68  }
69 
70  const uint16_t sig_len = static_cast<uint16_t>(m_signature.size());
71  buf.push_back(get_byte(0, sig_len));
72  buf.push_back(get_byte(1, sig_len));
73  buf += m_signature;
74 
75  return buf;
76  }
77 
78 /*
79 * Verify a Certificate Verify message
80 */
82  const Handshake_State& state,
83  const Policy& policy) const
84  {
85  std::unique_ptr<Public_Key> key(cert.subject_public_key());
86 
87  policy.check_peer_key_acceptable(*key);
88 
89  std::pair<std::string, Signature_Format> format =
90  state.parse_sig_format(*key.get(), m_scheme, true, policy);
91 
92  const bool signature_valid =
93  state.callbacks().tls_verify_message(*key, format.first, format.second,
94  state.hash().get_contents(), m_signature);
95 
96 #if defined(BOTAN_UNSAFE_FUZZER_MODE)
97  return true;
98 #else
99  return signature_valid;
100 #endif
101  }
102 
103 }
104 
105 }
virtual std::vector< uint8_t > send(const Handshake_Message &msg)=0
Certificate_Verify(Handshake_IO &io, Handshake_State &state, const Policy &policy, RandomNumberGenerator &rng, const Private_Key *key)
std::pair< std::string, Signature_Format > parse_sig_format(const Public_Key &key, Signature_Scheme scheme, bool for_client_auth, const Policy &policy) const
virtual bool tls_verify_message(const Public_Key &key, const std::string &emsa, Signature_Format format, const std::vector< uint8_t > &msg, const std::vector< uint8_t > &sig)
virtual void check_peer_key_acceptable(const Public_Key &public_key) const
Definition: tls_policy.cpp:229
Signature_Scheme
Definition: tls_algos.h:84
std::pair< std::string, Signature_Format > choose_sig_format(const Private_Key &key, Signature_Scheme &scheme, bool for_client_auth, const Policy &policy) const
Public_Key * subject_public_key() const
Definition: x509cert.h:49
bool supports_negotiable_signature_algorithms() const
Definition: tls_version.cpp:60
virtual std::vector< uint8_t > tls_sign_message(const Private_Key &key, RandomNumberGenerator &rng, const std::string &emsa, Signature_Format format, const std::vector< uint8_t > &msg)
#define BOTAN_ASSERT_NONNULL(ptr)
Definition: assert.h:107
std::vector< T > get_range(size_t len_bytes, size_t min_elems, size_t max_elems)
Definition: tls_reader.h:94
void update(const uint8_t in[], size_t length)
Definition: alg_id.cpp:13
bool verify(const X509_Certificate &cert, const Handshake_State &state, const Policy &policy) const
uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:39
const std::vector< uint8_t > & get_contents() const