Botan  2.4.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_hash_algo, m_sig_algo, 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_hash_algo = Signature_Algorithms::hash_algo_name(reader.get_byte());
51  m_sig_algo = Signature_Algorithms::sig_algo_name(reader.get_byte());
52  }
53 
54  m_signature = reader.get_range<uint8_t>(2, 0, 65535);
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_hash_algo.empty() && !m_sig_algo.empty())
65  {
66  buf.push_back(Signature_Algorithms::hash_algo_code(m_hash_algo));
67  buf.push_back(Signature_Algorithms::sig_algo_code(m_sig_algo));
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_hash_algo, m_sig_algo,
91  true, policy);
92 
93  const bool signature_valid =
94  state.callbacks().tls_verify_message(*key, format.first, format.second,
95  state.hash().get_contents(), m_signature);
96 
97 #if defined(BOTAN_UNSAFE_FUZZER_MODE)
98  return true;
99 #else
100  return signature_valid;
101 #endif
102  }
103 
104 }
105 
106 }
virtual std::vector< uint8_t > send(const Handshake_Message &msg)=0
static uint8_t sig_algo_code(const std::string &name)
Certificate_Verify(Handshake_IO &io, Handshake_State &state, const Policy &policy, RandomNumberGenerator &rng, const Private_Key *key)
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)
std::pair< std::string, Signature_Format > parse_sig_format(const Public_Key &key, const std::string &hash_algo, const std::string &sig_algo, bool for_client_auth, const Policy &policy) const
virtual void check_peer_key_acceptable(const Public_Key &public_key) const
Definition: tls_policy.cpp:236
Public_Key * subject_public_key() const
Definition: x509cert.h:49
std::pair< std::string, Signature_Format > choose_sig_format(const Private_Key &key, std::string &hash_algo, std::string &sig_algo, bool for_client_auth, const Policy &policy) const
bool supports_negotiable_signature_algorithms() const
Definition: tls_version.cpp:60
static uint8_t hash_algo_code(const std::string &name)
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:81
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
static std::string sig_algo_name(uint8_t code)
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
static std::string hash_algo_name(uint8_t code)