Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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 */
22 Certificate_Verify::Certificate_Verify(Handshake_IO& io,
23 Handshake_State& state,
24 const Policy& policy,
25 RandomNumberGenerator& rng,
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
48 if(version.supports_negotiable_signature_algorithms())
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 */
85 bool Certificate_Verify::verify(const X509_Certificate& cert,
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 }
Botan::TLS::Handshake_IO::send
virtual std::vector< uint8_t > send(const Handshake_Message &msg)=0
Botan::TLS::Handshake_Hash::get_contents
const std::vector< uint8_t > & get_contents() const
Definition: tls_handshake_hash.h:33
Botan::TLS::Handshake_State::choose_sig_format
std::pair< std::string, Signature_Format > choose_sig_format(const Private_Key &key, Signature_Scheme &scheme, bool for_client_auth, const Policy &policy) const
Definition: tls_handshake_state.cpp:399
Botan::TLS::Certificate_Verify::Certificate_Verify
Certificate_Verify(Handshake_IO &io, Handshake_State &state, const Policy &policy, RandomNumberGenerator &rng, const Private_Key *key)
Definition: msg_cert_verify.cpp:22
Botan::TLS::Callbacks::tls_verify_message
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)
Definition: tls_callbacks.cpp:96
Botan::RandomNumberGenerator
Definition: rng.h:24
Botan::TLS::Signature_Scheme::NONE
Botan::Private_Key
Definition: pk_keys.h:181
Botan::TLS::Signature_Scheme
Signature_Scheme
Definition: tls_algos.h:86
Botan::get_byte
constexpr uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:41
Botan::TLS::Protocol_Version
Definition: tls_version.h:21
Botan::TLS::TLS_Data_Reader::get_uint16_t
uint16_t get_uint16_t()
Definition: tls_reader.h:62
Botan::TLS::Handshake_State::hash
Handshake_Hash & hash()
Definition: tls_handshake_state.h:169
Botan::X509_Certificate::subject_public_key
Public_Key * subject_public_key() const
Definition: x509cert.h:50
Botan::X509_Certificate
Definition: x509cert.h:40
Botan::TLS::Callbacks::tls_sign_message
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)
Definition: tls_callbacks.cpp:84
Botan::TLS::Handshake_State::callbacks
Callbacks & callbacks() const
Definition: tls_handshake_state.h:163
BOTAN_ASSERT_NONNULL
#define BOTAN_ASSERT_NONNULL(ptr)
Definition: assert.h:107
Botan::TLS::TLS_Data_Reader::get_range
std::vector< T > get_range(size_t len_bytes, size_t min_elems, size_t max_elems)
Definition: tls_reader.h:94
Botan::TLS::Handshake_Hash::update
void update(const uint8_t in[], size_t length)
Definition: tls_handshake_hash.h:24
Botan::TLS::Handshake_State::parse_sig_format
std::pair< std::string, Signature_Format > parse_sig_format(const Public_Key &key, Signature_Scheme scheme, bool for_client_auth, const Policy &policy) const
Definition: tls_handshake_state.cpp:495
Botan::TLS::Handshake_IO
Definition: tls_handshake_io.h:29
Botan
Definition: alg_id.cpp:13
BOTAN_UNUSED
#define BOTAN_UNUSED(...)
Definition: assert.h:142
Botan::TLS::Policy
Definition: tls_policy.h:28
Botan::TLS::TLS_Data_Reader
Definition: tls_reader.h:24
Botan::Encoding_Error
Definition: exceptn.h:184
Botan::TLS::Policy::check_peer_key_acceptable
virtual void check_peer_key_acceptable(const Public_Key &public_key) const
Definition: tls_policy.cpp:231
Botan::TLS::Certificate_Verify::verify
bool verify(const X509_Certificate &cert, const Handshake_State &state, const Policy &policy) const
Definition: msg_cert_verify.cpp:85
Botan::TLS::Handshake_State
Definition: tls_handshake_state.h:49
Botan::TLS::TLS_Data_Reader::assert_done
void assert_done() const
Definition: tls_reader.h:30
Botan::TLS::Protocol_Version::supports_negotiable_signature_algorithms
bool supports_negotiable_signature_algorithms() const
Definition: tls_version.cpp:60