Botan  2.4.0
Crypto and TLS for C++11
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/x509path.h>
12 #include <botan/ocsp.h>
13 #include <botan/dh.h>
14 #include <botan/ecdh.h>
15 #include <botan/oids.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 
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 
36  const std::vector<X509_Certificate>& cert_chain,
37  const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_responses,
38  const std::vector<Certificate_Store*>& trusted_roots,
39  Usage_Type usage,
40  const std::string& hostname,
41  const TLS::Policy& policy)
42  {
43  if(cert_chain.empty())
44  throw Invalid_Argument("Certificate chain was empty");
45 
48 
49  Path_Validation_Result result =
50  x509_path_validate(cert_chain,
51  restrictions,
52  trusted_roots,
53  (usage == Usage_Type::TLS_SERVER_AUTH ? hostname : ""),
54  usage,
55  std::chrono::system_clock::now(),
57  ocsp_responses);
58 
59  if(!result.successful_validation())
60  throw Exception("Certificate validation failure: " + result.result_string());
61  }
62 
63 std::vector<uint8_t> TLS::Callbacks::tls_sign_message(
64  const Private_Key& key,
66  const std::string& emsa,
67  Signature_Format format,
68  const std::vector<uint8_t>& msg)
69  {
70  PK_Signer signer(key, rng, emsa, format);
71 
72  return signer.sign_message(msg, rng);
73  }
74 
76  const Public_Key& key,
77  const std::string& emsa,
78  Signature_Format format,
79  const std::vector<uint8_t>& msg,
80  const std::vector<uint8_t>& sig)
81  {
82  PK_Verifier verifier(key, emsa, format);
83 
84  return verifier.verify_message(msg, sig);
85  }
86 
87 std::pair<secure_vector<uint8_t>, std::vector<uint8_t>> TLS::Callbacks::tls_dh_agree(
88  const std::vector<uint8_t>& modulus,
89  const std::vector<uint8_t>& generator,
90  const std::vector<uint8_t>& peer_public_value,
91  const Policy& policy,
93  {
94  BigInt p = BigInt::decode(modulus);
95  BigInt g = BigInt::decode(generator);
96  BigInt Y = BigInt::decode(peer_public_value);
97 
98  /*
99  * A basic check for key validity. As we do not know q here we
100  * cannot check that Y is in the right subgroup. However since
101  * our key is ephemeral there does not seem to be any
102  * advantage to bogus keys anyway.
103  */
104  if(Y <= 1 || Y >= p - 1)
106  "Server sent bad DH key for DHE exchange");
107 
108  DL_Group group(p, g);
109 
110  if(!group.verify_group(rng, false))
112  "DH group validation failed");
113 
114  DH_PublicKey peer_key(group, Y);
115 
116  policy.check_peer_key_acceptable(peer_key);
117 
118  DH_PrivateKey priv_key(rng, group);
119  PK_Key_Agreement ka(priv_key, rng, "Raw");
121  ka.derive_key(0, peer_key.public_value()).bits_of());
122 
123  return std::make_pair(dh_secret, priv_key.public_value());
124  }
125 
126 std::pair<secure_vector<uint8_t>, std::vector<uint8_t>> TLS::Callbacks::tls_ecdh_agree(
127  const std::string& curve_name,
128  const std::vector<uint8_t>& peer_public_value,
129  const Policy& policy,
131  bool compressed)
132  {
133  secure_vector<uint8_t> ecdh_secret;
134  std::vector<uint8_t> our_public_value;
135 
136  if(curve_name == "x25519")
137  {
138 #if defined(BOTAN_HAS_CURVE_25519)
139  if(peer_public_value.size() != 32)
140  {
141  throw TLS_Exception(Alert::HANDSHAKE_FAILURE, "Invalid X25519 key size");
142  }
143 
144  Curve25519_PublicKey peer_key(peer_public_value);
145  policy.check_peer_key_acceptable(peer_key);
146  Curve25519_PrivateKey priv_key(rng);
147  PK_Key_Agreement ka(priv_key, rng, "Raw");
148  ecdh_secret = ka.derive_key(0, peer_key.public_value()).bits_of();
149 
150  // X25519 is always compressed but sent as "uncompressed" in TLS
151  our_public_value = priv_key.public_value();
152 #else
153  throw Internal_Error("Negotiated X25519 somehow, but it is disabled");
154 #endif
155  }
156  else
157  {
158  EC_Group group(OIDS::lookup(curve_name));
159  ECDH_PublicKey peer_key(group, OS2ECP(peer_public_value, group.get_curve()));
160  policy.check_peer_key_acceptable(peer_key);
161  ECDH_PrivateKey priv_key(rng, group);
162  PK_Key_Agreement ka(priv_key, rng, "Raw");
163  ecdh_secret = ka.derive_key(0, peer_key.public_value()).bits_of();
164  our_public_value = priv_key.public_value(compressed ? PointGFp::COMPRESSED : PointGFp::UNCOMPRESSED);
165  }
166 
167  return std::make_pair(ecdh_secret, our_public_value);
168  }
169 
170 }
std::vector< uint8_t > public_value() const
Definition: dh.cpp:28
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)
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)
std::vector< uint8_t > public_value() const
Definition: curve25519.h:30
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 std::string tls_server_choose_app_protocol(const std::vector< std::string > &client_protos)
bool verify_message(const uint8_t msg[], size_t msg_length, const uint8_t sig[], size_t sig_length)
Definition: pubkey.cpp:281
virtual void check_peer_key_acceptable(const Public_Key &public_key) const
Definition: tls_policy.cpp:236
fe Y
Definition: ge.cpp:28
Signature_Format
Definition: pubkey.h:27
std::vector< uint8_t > public_value() const
Definition: ecdh.h:50
std::vector< uint8_t > sign_message(const uint8_t in[], size_t length, RandomNumberGenerator &rng)
Definition: pubkey.h:207
virtual bool require_cert_revocation_info() const
Definition: tls_policy.cpp:213
bool verify_group(RandomNumberGenerator &rng, bool strong) const
Definition: dl_group.cpp:159
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)
std::vector< uint8_t > public_value() const override
Definition: ecdh.h:92
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)
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:203
virtual std::chrono::milliseconds tls_verify_cert_chain_ocsp_timeout() const
Definition: alg_id.cpp:13
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:820
virtual size_t minimum_signature_strength() const
Definition: tls_policy.cpp:208
std::vector< uint8_t > public_value() const override
Definition: curve25519.h:83
const CurveGFp & get_curve() const
Definition: ec_group.h:90
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
virtual void tls_inspect_handshake_msg(const Handshake_Message &message)
std::vector< uint8_t > public_value() const override
Definition: dh.cpp:72
std::string lookup(const OID &oid)
Definition: oids.cpp:18
static BigInt decode(const uint8_t buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:114
PointGFp OS2ECP(const uint8_t data[], size_t data_len, const CurveGFp &curve)
Definition: point_gfp.cpp:543
Usage_Type
Definition: x509cert.h:25
secure_vector< uint8_t > strip_leading_zeros(const uint8_t in[], size_t length)
Definition: ct_utils.h:170