Botan  2.18.2
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  reader.assert_done();
55  }
56 
57 /*
58 * Serialize a Certificate Verify message
59 */
60 std::vector<uint8_t> Certificate_Verify::serialize() const
61  {
62  std::vector<uint8_t> buf;
63 
64  if(m_scheme != Signature_Scheme::NONE)
65  {
66  const uint16_t scheme_code = static_cast<uint16_t>(m_scheme);
67  buf.push_back(get_byte(0, scheme_code));
68  buf.push_back(get_byte(1, scheme_code));
69  }
70 
71  if(m_signature.size() > 0xFFFF)
72  throw Encoding_Error("Certificate_Verify signature too long to encode");
73 
74  const uint16_t sig_len = static_cast<uint16_t>(m_signature.size());
75  buf.push_back(get_byte(0, sig_len));
76  buf.push_back(get_byte(1, sig_len));
77  buf += m_signature;
78 
79  return buf;
80  }
81 
82 /*
83 * Verify a Certificate Verify message
84 */
86  const Handshake_State& state,
87  const Policy& policy) const
88  {
89  std::unique_ptr<Public_Key> key(cert.subject_public_key());
90 
91  policy.check_peer_key_acceptable(*key);
92 
93  std::pair<std::string, Signature_Format> format =
94  state.parse_sig_format(*key.get(), m_scheme, true, policy);
95 
96  const bool signature_valid =
97  state.callbacks().tls_verify_message(*key, format.first, format.second,
98  state.hash().get_contents(), m_signature);
99 
100 #if defined(BOTAN_UNSAFE_FUZZER_MODE)
101  BOTAN_UNUSED(signature_valid);
102  return true;
103 #else
104  return signature_valid;
105 #endif
106  }
107 
108 }
109 
110 }
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:231
Signature_Scheme
Definition: tls_algos.h:86
std::pair< std::string, Signature_Format > choose_sig_format(const Private_Key &key, Signature_Scheme &scheme, bool for_client_auth, const Policy &policy) const
constexpr uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:41
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
#define BOTAN_UNUSED(...)
Definition: assert.h:142
Public_Key * subject_public_key() const
Definition: x509cert.cpp:714
bool verify(const X509_Certificate &cert, const Handshake_State &state, const Policy &policy) const
const std::vector< uint8_t > & get_contents() const