Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

tls_callbacks.cpp
Go to the documentation of this file.
1 /*
2 * TLS Callbacks
3 * (C) 2016 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_callbacks.h>
10 #include <botan/tls_policy.h>
11 #include <botan/tls_algos.h>
12 #include <botan/x509path.h>
13 #include <botan/ocsp.h>
14 #include <botan/dh.h>
15 #include <botan/ecdh.h>
16 #include <botan/tls_exceptn.h>
17 #include <botan/internal/ct_utils.h>
18
19 #if defined(BOTAN_HAS_CURVE_25519)
20 #include <botan/curve25519.h>
21 #endif
22
23 namespace Botan {
24
25 void TLS::Callbacks::tls_inspect_handshake_msg(const Handshake_Message&)
26 {
27 // default is no op
28 }
29
30 std::string TLS::Callbacks::tls_server_choose_app_protocol(const std::vector<std::string>&)
31 {
32 return "";
33 }
34
35 std::string TLS::Callbacks::tls_peer_network_identity()
36 {
37 return "";
38 }
39
40 void TLS::Callbacks::tls_modify_extensions(Extensions&, Connection_Side)
41 {
42 }
43
44 void TLS::Callbacks::tls_examine_extensions(const Extensions&, Connection_Side)
45 {
46 }
47
48 std::string TLS::Callbacks::tls_decode_group_param(Group_Params group_param)
49 {
50 return group_param_to_string(group_param);
51 }
52
53 void TLS::Callbacks::tls_verify_cert_chain(
54 const std::vector<X509_Certificate>& cert_chain,
55 const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_responses,
56 const std::vector<Certificate_Store*>& trusted_roots,
57 Usage_Type usage,
58 const std::string& hostname,
59 const TLS::Policy& policy)
60 {
61 if(cert_chain.empty())
62 throw Invalid_Argument("Certificate chain was empty");
63
64 Path_Validation_Restrictions restrictions(policy.require_cert_revocation_info(),
65 policy.minimum_signature_strength());
66
67 Path_Validation_Result result =
68 x509_path_validate(cert_chain,
69 restrictions,
70 trusted_roots,
71 (usage == Usage_Type::TLS_SERVER_AUTH ? hostname : ""),
72 usage,
73 std::chrono::system_clock::now(),
74 tls_verify_cert_chain_ocsp_timeout(),
75 ocsp_responses);
76
77 if(!result.successful_validation())
78 {
79 throw TLS_Exception(Alert::BAD_CERTIFICATE,
80 "Certificate validation failure: " + result.result_string());
81 }
82 }
83
84 std::vector<uint8_t> TLS::Callbacks::tls_sign_message(
85 const Private_Key& key,
86 RandomNumberGenerator& rng,
87 const std::string& emsa,
88 Signature_Format format,
89 const std::vector<uint8_t>& msg)
90 {
91 PK_Signer signer(key, rng, emsa, format);
92
93 return signer.sign_message(msg, rng);
94 }
95
96 bool TLS::Callbacks::tls_verify_message(
97 const Public_Key& key,
98 const std::string& emsa,
99 Signature_Format format,
100 const std::vector<uint8_t>& msg,
101 const std::vector<uint8_t>& sig)
102 {
103 PK_Verifier verifier(key, emsa, format);
104
105 return verifier.verify_message(msg, sig);
106 }
107
108 std::pair<secure_vector<uint8_t>, std::vector<uint8_t>> TLS::Callbacks::tls_dh_agree(
109 const std::vector<uint8_t>& modulus,
110 const std::vector<uint8_t>& generator,
111 const std::vector<uint8_t>& peer_public_value,
112 const Policy& policy,
113 RandomNumberGenerator& rng)
114 {
115 BigInt p = BigInt::decode(modulus);
116 BigInt g = BigInt::decode(generator);
117 BigInt Y = BigInt::decode(peer_public_value);
118
119 /*
120 * A basic check for key validity. As we do not know q here we
121 * cannot check that Y is in the right subgroup. However since
122 * our key is ephemeral there does not seem to be any
123 * advantage to bogus keys anyway.
124 */
125 if(Y <= 1 || Y >= p - 1)
126 throw TLS_Exception(Alert::ILLEGAL_PARAMETER,
127 "Server sent bad DH key for DHE exchange");
128
129 DL_Group group(p, g);
130
131 if(!group.verify_group(rng, false))
132 throw TLS_Exception(Alert::INSUFFICIENT_SECURITY,
133 "DH group validation failed");
134
135 DH_PublicKey peer_key(group, Y);
136
137 policy.check_peer_key_acceptable(peer_key);
138
139 DH_PrivateKey priv_key(rng, group);
140 PK_Key_Agreement ka(priv_key, rng, "Raw");
141 secure_vector<uint8_t> dh_secret = CT::strip_leading_zeros(
142 ka.derive_key(0, peer_key.public_value()).bits_of());
143
144 return std::make_pair(dh_secret, priv_key.public_value());
145 }
146
147 std::pair<secure_vector<uint8_t>, std::vector<uint8_t>> TLS::Callbacks::tls_ecdh_agree(
148 const std::string& curve_name,
149 const std::vector<uint8_t>& peer_public_value,
150 const Policy& policy,
151 RandomNumberGenerator& rng,
152 bool compressed)
153 {
154 secure_vector<uint8_t> ecdh_secret;
155 std::vector<uint8_t> our_public_value;
156
157 if(curve_name == "x25519")
158 {
159 #if defined(BOTAN_HAS_CURVE_25519)
160 if(peer_public_value.size() != 32)
161 {
162 throw TLS_Exception(Alert::HANDSHAKE_FAILURE, "Invalid X25519 key size");
163 }
164
165 Curve25519_PublicKey peer_key(peer_public_value);
166 policy.check_peer_key_acceptable(peer_key);
167 Curve25519_PrivateKey priv_key(rng);
168 PK_Key_Agreement ka(priv_key, rng, "Raw");
169 ecdh_secret = ka.derive_key(0, peer_key.public_value()).bits_of();
170
171 // X25519 is always compressed but sent as "uncompressed" in TLS
172 our_public_value = priv_key.public_value();
173 #else
174 throw Internal_Error("Negotiated X25519 somehow, but it is disabled");
175 #endif
176 }
177 else
178 {
179 EC_Group group(OID::from_string(curve_name));
180 ECDH_PublicKey peer_key(group, group.OS2ECP(peer_public_value));
181 policy.check_peer_key_acceptable(peer_key);
182 ECDH_PrivateKey priv_key(rng, group);
183 PK_Key_Agreement ka(priv_key, rng, "Raw");
184 ecdh_secret = ka.derive_key(0, peer_key.public_value()).bits_of();
185 our_public_value = priv_key.public_value(compressed ? PointGFp::COMPRESSED : PointGFp::UNCOMPRESSED);
186 }
187
188 return std::make_pair(ecdh_secret, our_public_value);
189 }
190
191 }
Botan::TLS::Callbacks::tls_decode_group_param
virtual std::string tls_decode_group_param(Group_Params group_param)
Definition: tls_callbacks.cpp:48
Botan::PointGFp::COMPRESSED
Definition: point_gfp.h:52
Botan::Invalid_Argument
Definition: exceptn.h:136
Botan::TLS::Callbacks::tls_dh_agree
virtual std::pair< secure_vector< uint8_t >, std::vector< uint8_t > > tls_dh_agree(const std::vector< uint8_t > &modulus, const std::vector< uint8_t > &generator, const std::vector< uint8_t > &peer_public_value, const Policy &policy, RandomNumberGenerator &rng)
Definition: tls_callbacks.cpp:108
Botan::PointGFp::UNCOMPRESSED
Definition: point_gfp.h:51
Botan::TLS::Callbacks::tls_ecdh_agree
virtual std::pair< secure_vector< uint8_t >, std::vector< uint8_t > > tls_ecdh_agree(const std::string &curve_name, const std::vector< uint8_t > &peer_public_value, const Policy &policy, RandomNumberGenerator &rng, bool compressed)
Definition: tls_callbacks.cpp:147
Botan::BigInt
Definition: bigint.h:24
Botan::Path_Validation_Restrictions
Definition: x509path.h:34
Botan::TLS::Policy::require_cert_revocation_info
virtual bool require_cert_revocation_info() const
Definition: tls_policy.cpp:208
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::TLS::Callbacks::tls_server_choose_app_protocol
virtual std::string tls_server_choose_app_protocol(const std::vector< std::string > &client_protos)
Definition: tls_callbacks.cpp:30
Botan::PK_Verifier::verify_message
bool verify_message(const uint8_t msg[], size_t msg_length, const uint8_t sig[], size_t sig_length)
Definition: pubkey.cpp:324
Botan::RandomNumberGenerator
Definition: rng.h:24
Botan::Private_Key
Definition: pk_keys.h:181
Botan::ECDH_PublicKey
Definition: ecdh.h:20
Y
fe Y
Definition: ge.cpp:28
Botan::PK_Key_Agreement::derive_key
SymmetricKey derive_key(size_t key_len, const uint8_t in[], size_t in_len, const uint8_t params[], size_t params_len) const
Definition: pubkey.cpp:218
Botan::Signature_Format
Signature_Format
Definition: pk_keys.h:24
Botan::TLS::group_param_to_string
std::string group_param_to_string(Group_Params group)
Definition: tls_algos.cpp:155
Botan::Internal_Error
Definition: exceptn.h:341
Botan::Curve25519_PublicKey::public_value
std::vector< uint8_t > public_value() const
Definition: curve25519.h:30
Botan::TLS::Callbacks::tls_verify_cert_chain
virtual void tls_verify_cert_chain(const std::vector< X509_Certificate > &cert_chain, const std::vector< std::shared_ptr< const OCSP::Response >> &ocsp_responses, const std::vector< Certificate_Store * > &trusted_roots, Usage_Type usage, const std::string &hostname, const TLS::Policy &policy)
Definition: tls_callbacks.cpp:53
Botan::CT::strip_leading_zeros
secure_vector< uint8_t > strip_leading_zeros(const uint8_t in[], size_t length)
Definition: ct_utils.cpp:65
Botan::PK_Signer::sign_message
std::vector< uint8_t > sign_message(const uint8_t in[], size_t length, RandomNumberGenerator &rng)
Definition: pubkey.h:214
Botan::TLS::Connection_Side
Connection_Side
Definition: tls_magic.h:32
Botan::TLS::Alert::BAD_CERTIFICATE
Definition: tls_alert.h:36
Botan::x509_path_validate
Path_Validation_Result x509_path_validate(const std::vector< X509_Certificate > &end_certs, const Path_Validation_Restrictions &restrictions, const std::vector< Certificate_Store * > &trusted_roots, const std::string &hostname, Usage_Type usage, std::chrono::system_clock::time_point ref_time, std::chrono::milliseconds ocsp_timeout, const std::vector< std::shared_ptr< const OCSP::Response >> &ocsp_resp)
Definition: x509path.cpp:836
Botan::TLS::Alert::HANDSHAKE_FAILURE
Definition: tls_alert.h:34
Botan::DL_Group
Definition: dl_group.h:23
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:65
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::Curve25519_PublicKey
Definition: curve25519.h:15
Botan::ECDH_PrivateKey::public_value
std::vector< uint8_t > public_value() const override
Definition: ecdh.h:92
Botan::TLS::Handshake_Message
Definition: tls_handshake_msg.h:26
Botan::TLS::Alert::INSUFFICIENT_SECURITY
Definition: tls_alert.h:48
Botan::TLS::Callbacks::tls_peer_network_identity
virtual std::string tls_peer_network_identity()
Definition: tls_callbacks.cpp:35
Botan::DL_Group::verify_group
bool verify_group(RandomNumberGenerator &rng, bool strong=true) const
Definition: dl_group.cpp:372
Botan::TLS::Extensions
Definition: tls_extensions.h:491
Botan::Path_Validation_Result
Definition: x509path.h:125
Botan
Definition: alg_id.cpp:13
Botan::PK_Verifier
Definition: pubkey.h:298
Botan::TLS::Callbacks::tls_examine_extensions
virtual void tls_examine_extensions(const Extensions &extn, Connection_Side which_side)
Definition: tls_callbacks.cpp:44
Botan::EC_Group
Definition: ec_group.h:40
Botan::Public_Key
Definition: pk_keys.h:29
Botan::Usage_Type::TLS_SERVER_AUTH
Botan::PK_Key_Agreement
Definition: pubkey.h:413
Botan::DH_PublicKey
Definition: dh.h:18
Botan::TLS::Callbacks::tls_modify_extensions
virtual void tls_modify_extensions(Extensions &extn, Connection_Side which_side)
Definition: tls_callbacks.cpp:40
Botan::TLS::Policy
Definition: tls_policy.h:28
Botan::DH_PublicKey::public_value
std::vector< uint8_t > public_value() const
Definition: dh.cpp:27
Botan::BigInt::decode
static BigInt decode(const uint8_t buf[], size_t length)
Definition: bigint.h:798
Botan::Curve25519_PrivateKey
Definition: curve25519.h:58
Botan::Curve25519_PrivateKey::public_value
std::vector< uint8_t > public_value() const override
Definition: curve25519.h:83
Botan::TLS::Alert::ILLEGAL_PARAMETER
Definition: tls_alert.h:41
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::EC_Group::OS2ECP
PointGFp OS2ECP(const uint8_t bits[], size_t len) const
Definition: ec_group.cpp:537
Botan::TLS::Policy::minimum_signature_strength
virtual size_t minimum_signature_strength() const
Definition: tls_policy.cpp:203
Botan::TLS::Callbacks::tls_inspect_handshake_msg
virtual void tls_inspect_handshake_msg(const Handshake_Message &message)
Definition: tls_callbacks.cpp:25
Botan::ECDH_PrivateKey
Definition: ecdh.h:66
Botan::PK_Signer
Definition: pubkey.h:166
Botan::DH_PrivateKey
Definition: dh.h:49
Botan::DH_PrivateKey::public_value
std::vector< uint8_t > public_value() const override
Definition: dh.cpp:72
Botan::TLS::Group_Params
Group_Params
Definition: tls_algos.h:123
Botan::Usage_Type
Usage_Type
Definition: x509cert.h:25
Botan::TLS::TLS_Exception
Definition: tls_exceptn.h:21
Botan::OID::from_string
static OID from_string(const std::string &str)
Definition: asn1_oid.cpp:62