Botan  2.8.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/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/oids.h>
17 #include <botan/tls_exceptn.h>
18 #include <botan/internal/ct_utils.h>
19 
20 #if defined(BOTAN_HAS_CURVE_25519)
21  #include <botan/curve25519.h>
22 #endif
23 
24 namespace Botan {
25 
27  {
28  // default is no op
29  }
30 
31 std::string TLS::Callbacks::tls_server_choose_app_protocol(const std::vector<std::string>&)
32  {
33  return "";
34  }
35 
37  {
38  }
39 
41  {
42  }
43 
45  {
46  return group_param_to_string(group_param);
47  }
48 
50  const std::vector<X509_Certificate>& cert_chain,
51  const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_responses,
52  const std::vector<Certificate_Store*>& trusted_roots,
53  Usage_Type usage,
54  const std::string& hostname,
55  const TLS::Policy& policy)
56  {
57  if(cert_chain.empty())
58  throw Invalid_Argument("Certificate chain was empty");
59 
62 
63  Path_Validation_Result result =
64  x509_path_validate(cert_chain,
65  restrictions,
66  trusted_roots,
67  (usage == Usage_Type::TLS_SERVER_AUTH ? hostname : ""),
68  usage,
69  std::chrono::system_clock::now(),
70  tls_verify_cert_chain_ocsp_timeout(),
71  ocsp_responses);
72 
73  if(!result.successful_validation())
74  throw Exception("Certificate validation failure: " + result.result_string());
75  }
76 
77 std::vector<uint8_t> TLS::Callbacks::tls_sign_message(
78  const Private_Key& key,
80  const std::string& emsa,
81  Signature_Format format,
82  const std::vector<uint8_t>& msg)
83  {
84  PK_Signer signer(key, rng, emsa, format);
85 
86  return signer.sign_message(msg, rng);
87  }
88 
90  const Public_Key& key,
91  const std::string& emsa,
92  Signature_Format format,
93  const std::vector<uint8_t>& msg,
94  const std::vector<uint8_t>& sig)
95  {
96  PK_Verifier verifier(key, emsa, format);
97 
98  return verifier.verify_message(msg, sig);
99  }
100 
101 std::pair<secure_vector<uint8_t>, std::vector<uint8_t>> TLS::Callbacks::tls_dh_agree(
102  const std::vector<uint8_t>& modulus,
103  const std::vector<uint8_t>& generator,
104  const std::vector<uint8_t>& peer_public_value,
105  const Policy& policy,
107  {
108  BigInt p = BigInt::decode(modulus);
109  BigInt g = BigInt::decode(generator);
110  BigInt Y = BigInt::decode(peer_public_value);
111 
112  /*
113  * A basic check for key validity. As we do not know q here we
114  * cannot check that Y is in the right subgroup. However since
115  * our key is ephemeral there does not seem to be any
116  * advantage to bogus keys anyway.
117  */
118  if(Y <= 1 || Y >= p - 1)
120  "Server sent bad DH key for DHE exchange");
121 
122  DL_Group group(p, g);
123 
124  if(!group.verify_group(rng, false))
126  "DH group validation failed");
127 
128  DH_PublicKey peer_key(group, Y);
129 
130  policy.check_peer_key_acceptable(peer_key);
131 
132  DH_PrivateKey priv_key(rng, group);
133  PK_Key_Agreement ka(priv_key, rng, "Raw");
135  ka.derive_key(0, peer_key.public_value()).bits_of());
136 
137  return std::make_pair(dh_secret, priv_key.public_value());
138  }
139 
140 std::pair<secure_vector<uint8_t>, std::vector<uint8_t>> TLS::Callbacks::tls_ecdh_agree(
141  const std::string& curve_name,
142  const std::vector<uint8_t>& peer_public_value,
143  const Policy& policy,
145  bool compressed)
146  {
147  secure_vector<uint8_t> ecdh_secret;
148  std::vector<uint8_t> our_public_value;
149 
150  if(curve_name == "x25519")
151  {
152 #if defined(BOTAN_HAS_CURVE_25519)
153  if(peer_public_value.size() != 32)
154  {
155  throw TLS_Exception(Alert::HANDSHAKE_FAILURE, "Invalid X25519 key size");
156  }
157 
158  Curve25519_PublicKey peer_key(peer_public_value);
159  policy.check_peer_key_acceptable(peer_key);
160  Curve25519_PrivateKey priv_key(rng);
161  PK_Key_Agreement ka(priv_key, rng, "Raw");
162  ecdh_secret = ka.derive_key(0, peer_key.public_value()).bits_of();
163 
164  // X25519 is always compressed but sent as "uncompressed" in TLS
165  our_public_value = priv_key.public_value();
166 #else
167  throw Internal_Error("Negotiated X25519 somehow, but it is disabled");
168 #endif
169  }
170  else
171  {
172  EC_Group group(OIDS::lookup(curve_name));
173  ECDH_PublicKey peer_key(group, group.OS2ECP(peer_public_value));
174  policy.check_peer_key_acceptable(peer_key);
175  ECDH_PrivateKey priv_key(rng, group);
176  PK_Key_Agreement ka(priv_key, rng, "Raw");
177  ecdh_secret = ka.derive_key(0, peer_key.public_value()).bits_of();
178  our_public_value = priv_key.public_value(compressed ? PointGFp::COMPRESSED : PointGFp::UNCOMPRESSED);
179  }
180 
181  return std::make_pair(ecdh_secret, our_public_value);
182  }
183 
184 }
std::vector< uint8_t > public_value() const
Definition: dh.cpp:27
virtual std::string tls_decode_group_param(Group_Params group_param)
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:324
virtual void check_peer_key_acceptable(const Public_Key &public_key) const
Definition: tls_policy.cpp:229
fe Y
Definition: ge.cpp:28
Signature_Format
Definition: pubkey.h:27
std::string group_param_to_string(Group_Params group)
Definition: tls_algos.cpp:155
std::vector< uint8_t > sign_message(const uint8_t in[], size_t length, RandomNumberGenerator &rng)
Definition: pubkey.h:218
bool verify_group(RandomNumberGenerator &rng, bool strong=true) const
Definition: dl_group.cpp:372
virtual bool require_cert_revocation_info() const
Definition: tls_policy.cpp:206
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
PointGFp OS2ECP(const uint8_t bits[], size_t len) const
Definition: ec_group.cpp:538
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:218
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:830
virtual void tls_examine_extensions(const Extensions &extn, Connection_Side which_side)
virtual size_t minimum_signature_strength() const
Definition: tls_policy.cpp:201
virtual void tls_modify_extensions(Extensions &extn, Connection_Side which_side)
static BigInt decode(const uint8_t buf[], size_t length)
Definition: bigint.h:713
std::vector< uint8_t > public_value() const override
Definition: curve25519.h:83
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:113
Usage_Type
Definition: x509cert.h:25
secure_vector< uint8_t > strip_leading_zeros(const uint8_t in[], size_t length)
Definition: ct_utils.h:191