1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
/*
* TLS Callbacks
* (C) 2016 Jack Lloyd
* 2017 Harry Reimann, Rohde & Schwarz Cybersecurity
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/tls_callbacks.h>
#include <botan/tls_policy.h>
#include <botan/tls_algos.h>
#include <botan/x509path.h>
#include <botan/ocsp.h>
#include <botan/dh.h>
#include <botan/ecdh.h>
#include <botan/oids.h>
#include <botan/tls_exceptn.h>
#include <botan/internal/ct_utils.h>
#if defined(BOTAN_HAS_CURVE_25519)
#include <botan/curve25519.h>
#endif
namespace Botan {
void TLS::Callbacks::tls_inspect_handshake_msg(const Handshake_Message&)
{
// default is no op
}
std::string TLS::Callbacks::tls_server_choose_app_protocol(const std::vector<std::string>&)
{
return "";
}
void TLS::Callbacks::tls_modify_extensions(Extensions&, Connection_Side)
{
}
void TLS::Callbacks::tls_examine_extensions(const Extensions&, Connection_Side)
{
}
std::string TLS::Callbacks::tls_decode_group_param(Group_Params group_param)
{
return group_param_to_string(group_param);
}
void TLS::Callbacks::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)
{
if(cert_chain.empty())
throw Invalid_Argument("Certificate chain was empty");
Path_Validation_Restrictions restrictions(policy.require_cert_revocation_info(),
policy.minimum_signature_strength());
Path_Validation_Result result =
x509_path_validate(cert_chain,
restrictions,
trusted_roots,
(usage == Usage_Type::TLS_SERVER_AUTH ? hostname : ""),
usage,
std::chrono::system_clock::now(),
tls_verify_cert_chain_ocsp_timeout(),
ocsp_responses);
if(!result.successful_validation())
throw Exception("Certificate validation failure: " + result.result_string());
}
std::vector<uint8_t> TLS::Callbacks::tls_sign_message(
const Private_Key& key,
RandomNumberGenerator& rng,
const std::string& emsa,
Signature_Format format,
const std::vector<uint8_t>& msg)
{
PK_Signer signer(key, rng, emsa, format);
return signer.sign_message(msg, rng);
}
bool TLS::Callbacks::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)
{
PK_Verifier verifier(key, emsa, format);
return verifier.verify_message(msg, sig);
}
std::pair<secure_vector<uint8_t>, std::vector<uint8_t>> TLS::Callbacks::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)
{
BigInt p = BigInt::decode(modulus);
BigInt g = BigInt::decode(generator);
BigInt Y = BigInt::decode(peer_public_value);
/*
* A basic check for key validity. As we do not know q here we
* cannot check that Y is in the right subgroup. However since
* our key is ephemeral there does not seem to be any
* advantage to bogus keys anyway.
*/
if(Y <= 1 || Y >= p - 1)
throw TLS_Exception(Alert::INSUFFICIENT_SECURITY,
"Server sent bad DH key for DHE exchange");
DL_Group group(p, g);
if(!group.verify_group(rng, false))
throw TLS_Exception(Alert::INSUFFICIENT_SECURITY,
"DH group validation failed");
DH_PublicKey peer_key(group, Y);
policy.check_peer_key_acceptable(peer_key);
DH_PrivateKey priv_key(rng, group);
PK_Key_Agreement ka(priv_key, rng, "Raw");
secure_vector<uint8_t> dh_secret = CT::strip_leading_zeros(
ka.derive_key(0, peer_key.public_value()).bits_of());
return std::make_pair(dh_secret, priv_key.public_value());
}
std::pair<secure_vector<uint8_t>, std::vector<uint8_t>> TLS::Callbacks::tls_ecdh_agree(
const std::string& curve_name,
const std::vector<uint8_t>& peer_public_value,
const Policy& policy,
RandomNumberGenerator& rng,
bool compressed)
{
secure_vector<uint8_t> ecdh_secret;
std::vector<uint8_t> our_public_value;
if(curve_name == "x25519")
{
#if defined(BOTAN_HAS_CURVE_25519)
if(peer_public_value.size() != 32)
{
throw TLS_Exception(Alert::HANDSHAKE_FAILURE, "Invalid X25519 key size");
}
Curve25519_PublicKey peer_key(peer_public_value);
policy.check_peer_key_acceptable(peer_key);
Curve25519_PrivateKey priv_key(rng);
PK_Key_Agreement ka(priv_key, rng, "Raw");
ecdh_secret = ka.derive_key(0, peer_key.public_value()).bits_of();
// X25519 is always compressed but sent as "uncompressed" in TLS
our_public_value = priv_key.public_value();
#else
throw Internal_Error("Negotiated X25519 somehow, but it is disabled");
#endif
}
else
{
EC_Group group(OIDS::lookup(curve_name));
ECDH_PublicKey peer_key(group, group.OS2ECP(peer_public_value));
policy.check_peer_key_acceptable(peer_key);
ECDH_PrivateKey priv_key(rng, group);
PK_Key_Agreement ka(priv_key, rng, "Raw");
ecdh_secret = ka.derive_key(0, peer_key.public_value()).bits_of();
our_public_value = priv_key.public_value(compressed ? PointGFp::COMPRESSED : PointGFp::UNCOMPRESSED);
}
return std::make_pair(ecdh_secret, our_public_value);
}
}
|