Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

3133 рядки
125 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. * ECC cipher suite support in OpenSSL originally developed by
  113. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  114. */
  115. /* ====================================================================
  116. * Copyright 2005 Nokia. All rights reserved.
  117. *
  118. * The portions of the attached software ("Contribution") is developed by
  119. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  120. * license.
  121. *
  122. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  123. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  124. * support (see RFC 4279) to OpenSSL.
  125. *
  126. * No patent licenses or other rights except those expressly stated in
  127. * the OpenSSL open source license shall be deemed granted or received
  128. * expressly, by implication, estoppel, or otherwise.
  129. *
  130. * No assurances are provided by Nokia that the Contribution does not
  131. * infringe the patent or other intellectual property rights of any third
  132. * party or that the license provides you with all the necessary rights
  133. * to make use of the Contribution.
  134. *
  135. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  136. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  137. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  138. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  139. * OTHERWISE.
  140. */
  141. #ifndef OPENSSL_HEADER_SSL_INTERNAL_H
  142. #define OPENSSL_HEADER_SSL_INTERNAL_H
  143. #include <openssl/base.h>
  144. #include <stdlib.h>
  145. #include <limits>
  146. #include <new>
  147. #include <type_traits>
  148. #include <utility>
  149. #include <openssl/aead.h>
  150. #include <openssl/err.h>
  151. #include <openssl/lhash.h>
  152. #include <openssl/mem.h>
  153. #include <openssl/ssl.h>
  154. #include <openssl/span.h>
  155. #include <openssl/stack.h>
  156. #include "../crypto/err/internal.h"
  157. #include "../crypto/internal.h"
  158. #if defined(OPENSSL_WINDOWS)
  159. // Windows defines struct timeval in winsock2.h.
  160. OPENSSL_MSVC_PRAGMA(warning(push, 3))
  161. #include <winsock2.h>
  162. OPENSSL_MSVC_PRAGMA(warning(pop))
  163. #else
  164. #include <sys/time.h>
  165. #endif
  166. namespace bssl {
  167. struct SSL_HANDSHAKE;
  168. struct SSL_PROTOCOL_METHOD;
  169. // C++ utilities.
  170. // New behaves like |new| but uses |OPENSSL_malloc| for memory allocation. It
  171. // returns nullptr on allocation error. It only implements single-object
  172. // allocation and not new T[n].
  173. //
  174. // Note: unlike |new|, this does not support non-public constructors.
  175. template <typename T, typename... Args>
  176. T *New(Args &&... args) {
  177. void *t = OPENSSL_malloc(sizeof(T));
  178. if (t == nullptr) {
  179. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  180. return nullptr;
  181. }
  182. return new (t) T(std::forward<Args>(args)...);
  183. }
  184. // Delete behaves like |delete| but uses |OPENSSL_free| to release memory.
  185. //
  186. // Note: unlike |delete| this does not support non-public destructors.
  187. template <typename T>
  188. void Delete(T *t) {
  189. if (t != nullptr) {
  190. t->~T();
  191. OPENSSL_free(t);
  192. }
  193. }
  194. // All types with kAllowUniquePtr set may be used with UniquePtr. Other types
  195. // may be C structs which require a |BORINGSSL_MAKE_DELETER| registration.
  196. namespace internal {
  197. template <typename T>
  198. struct DeleterImpl<T, typename std::enable_if<T::kAllowUniquePtr>::type> {
  199. static void Free(T *t) { Delete(t); }
  200. };
  201. }
  202. // MakeUnique behaves like |std::make_unique| but returns nullptr on allocation
  203. // error.
  204. template <typename T, typename... Args>
  205. UniquePtr<T> MakeUnique(Args &&... args) {
  206. return UniquePtr<T>(New<T>(std::forward<Args>(args)...));
  207. }
  208. #if defined(BORINGSSL_ALLOW_CXX_RUNTIME)
  209. #define HAS_VIRTUAL_DESTRUCTOR
  210. #define PURE_VIRTUAL = 0
  211. #else
  212. // HAS_VIRTUAL_DESTRUCTOR should be declared in any base class which defines a
  213. // virtual destructor. This avoids a dependency on |_ZdlPv| and prevents the
  214. // class from being used with |delete|.
  215. #define HAS_VIRTUAL_DESTRUCTOR \
  216. void operator delete(void *) { abort(); }
  217. // PURE_VIRTUAL should be used instead of = 0 when defining pure-virtual
  218. // functions. This avoids a dependency on |__cxa_pure_virtual| but loses
  219. // compile-time checking.
  220. #define PURE_VIRTUAL { abort(); }
  221. #endif
  222. // CONSTEXPR_ARRAY works around a VS 2015 bug where ranged for loops don't work
  223. // on constexpr arrays.
  224. #if defined(_MSC_VER) && !defined(__clang__) && _MSC_VER < 1910
  225. #define CONSTEXPR_ARRAY const
  226. #else
  227. #define CONSTEXPR_ARRAY constexpr
  228. #endif
  229. // Array<T> is an owning array of elements of |T|.
  230. template <typename T>
  231. class Array {
  232. public:
  233. // Array's default constructor creates an empty array.
  234. Array() {}
  235. Array(const Array &) = delete;
  236. Array(Array &&other) { *this = std::move(other); }
  237. ~Array() { Reset(); }
  238. Array &operator=(const Array &) = delete;
  239. Array &operator=(Array &&other) {
  240. Reset();
  241. other.Release(&data_, &size_);
  242. return *this;
  243. }
  244. const T *data() const { return data_; }
  245. T *data() { return data_; }
  246. size_t size() const { return size_; }
  247. bool empty() const { return size_ == 0; }
  248. const T &operator[](size_t i) const { return data_[i]; }
  249. T &operator[](size_t i) { return data_[i]; }
  250. T *begin() { return data_; }
  251. const T *cbegin() const { return data_; }
  252. T *end() { return data_ + size_; }
  253. const T *cend() const { return data_ + size_; }
  254. void Reset() { Reset(nullptr, 0); }
  255. // Reset releases the current contents of the array and takes ownership of the
  256. // raw pointer supplied by the caller.
  257. void Reset(T *new_data, size_t new_size) {
  258. for (size_t i = 0; i < size_; i++) {
  259. data_[i].~T();
  260. }
  261. OPENSSL_free(data_);
  262. data_ = new_data;
  263. size_ = new_size;
  264. }
  265. // Release releases ownership of the array to a raw pointer supplied by the
  266. // caller.
  267. void Release(T **out, size_t *out_size) {
  268. *out = data_;
  269. *out_size = size_;
  270. data_ = nullptr;
  271. size_ = 0;
  272. }
  273. // Init replaces the array with a newly-allocated array of |new_size|
  274. // default-constructed copies of |T|. It returns true on success and false on
  275. // error.
  276. //
  277. // Note that if |T| is a primitive type like |uint8_t|, it is uninitialized.
  278. bool Init(size_t new_size) {
  279. Reset();
  280. if (new_size == 0) {
  281. return true;
  282. }
  283. if (new_size > std::numeric_limits<size_t>::max() / sizeof(T)) {
  284. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  285. return false;
  286. }
  287. data_ = reinterpret_cast<T*>(OPENSSL_malloc(new_size * sizeof(T)));
  288. if (data_ == nullptr) {
  289. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  290. return false;
  291. }
  292. size_ = new_size;
  293. for (size_t i = 0; i < size_; i++) {
  294. new (&data_[i]) T;
  295. }
  296. return true;
  297. }
  298. // CopyFrom replaces the array with a newly-allocated copy of |in|. It returns
  299. // true on success and false on error.
  300. bool CopyFrom(Span<const T> in) {
  301. if (!Init(in.size())) {
  302. return false;
  303. }
  304. OPENSSL_memcpy(data_, in.data(), sizeof(T) * in.size());
  305. return true;
  306. }
  307. private:
  308. T *data_ = nullptr;
  309. size_t size_ = 0;
  310. };
  311. // CBBFinishArray behaves like |CBB_finish| but stores the result in an Array.
  312. OPENSSL_EXPORT bool CBBFinishArray(CBB *cbb, Array<uint8_t> *out);
  313. // Protocol versions.
  314. //
  315. // Due to DTLS's historical wire version differences and to support multiple
  316. // variants of the same protocol during development, we maintain two notions of
  317. // version.
  318. //
  319. // The "version" or "wire version" is the actual 16-bit value that appears on
  320. // the wire. It uniquely identifies a version and is also used at API
  321. // boundaries. The set of supported versions differs between TLS and DTLS. Wire
  322. // versions are opaque values and may not be compared numerically.
  323. //
  324. // The "protocol version" identifies the high-level handshake variant being
  325. // used. DTLS versions map to the corresponding TLS versions. Draft TLS 1.3
  326. // variants all map to TLS 1.3. Protocol versions are sequential and may be
  327. // compared numerically.
  328. // ssl_protocol_version_from_wire sets |*out| to the protocol version
  329. // corresponding to wire version |version| and returns true. If |version| is not
  330. // a valid TLS or DTLS version, it returns false.
  331. //
  332. // Note this simultaneously handles both DTLS and TLS. Use one of the
  333. // higher-level functions below for most operations.
  334. bool ssl_protocol_version_from_wire(uint16_t *out, uint16_t version);
  335. // ssl_get_version_range sets |*out_min_version| and |*out_max_version| to the
  336. // minimum and maximum enabled protocol versions, respectively.
  337. bool ssl_get_version_range(const SSL *ssl, uint16_t *out_min_version,
  338. uint16_t *out_max_version);
  339. // ssl_supports_version returns whether |hs| supports |version|.
  340. bool ssl_supports_version(SSL_HANDSHAKE *hs, uint16_t version);
  341. // ssl_add_supported_versions writes the supported versions of |hs| to |cbb|, in
  342. // decreasing preference order.
  343. bool ssl_add_supported_versions(SSL_HANDSHAKE *hs, CBB *cbb);
  344. // ssl_negotiate_version negotiates a common version based on |hs|'s preferences
  345. // and the peer preference list in |peer_versions|. On success, it returns true
  346. // and sets |*out_version| to the selected version. Otherwise, it returns false
  347. // and sets |*out_alert| to an alert to send.
  348. bool ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  349. uint16_t *out_version, const CBS *peer_versions);
  350. // ssl_protocol_version returns |ssl|'s protocol version. It is an error to
  351. // call this function before the version is determined.
  352. uint16_t ssl_protocol_version(const SSL *ssl);
  353. // ssl_is_draft28 returns whether the version corresponds to a draft28 TLS 1.3
  354. // variant.
  355. bool ssl_is_draft28(uint16_t version);
  356. // Cipher suites.
  357. } // namespace bssl
  358. struct ssl_cipher_st {
  359. // name is the OpenSSL name for the cipher.
  360. const char *name;
  361. // standard_name is the IETF name for the cipher.
  362. const char *standard_name;
  363. // id is the cipher suite value bitwise OR-d with 0x03000000.
  364. uint32_t id;
  365. // algorithm_* determine the cipher suite. See constants below for the values.
  366. uint32_t algorithm_mkey;
  367. uint32_t algorithm_auth;
  368. uint32_t algorithm_enc;
  369. uint32_t algorithm_mac;
  370. uint32_t algorithm_prf;
  371. };
  372. namespace bssl {
  373. // Bits for |algorithm_mkey| (key exchange algorithm).
  374. #define SSL_kRSA 0x00000001u
  375. #define SSL_kECDHE 0x00000002u
  376. // SSL_kPSK is only set for plain PSK, not ECDHE_PSK.
  377. #define SSL_kPSK 0x00000004u
  378. #define SSL_kGENERIC 0x00000008u
  379. // Bits for |algorithm_auth| (server authentication).
  380. #define SSL_aRSA 0x00000001u
  381. #define SSL_aECDSA 0x00000002u
  382. // SSL_aPSK is set for both PSK and ECDHE_PSK.
  383. #define SSL_aPSK 0x00000004u
  384. #define SSL_aGENERIC 0x00000008u
  385. #define SSL_aCERT (SSL_aRSA | SSL_aECDSA)
  386. // Bits for |algorithm_enc| (symmetric encryption).
  387. #define SSL_3DES 0x00000001u
  388. #define SSL_AES128 0x00000002u
  389. #define SSL_AES256 0x00000004u
  390. #define SSL_AES128GCM 0x00000008u
  391. #define SSL_AES256GCM 0x00000010u
  392. #define SSL_eNULL 0x00000020u
  393. #define SSL_CHACHA20POLY1305 0x00000040u
  394. #define SSL_AES (SSL_AES128 | SSL_AES256 | SSL_AES128GCM | SSL_AES256GCM)
  395. // Bits for |algorithm_mac| (symmetric authentication).
  396. #define SSL_SHA1 0x00000001u
  397. #define SSL_SHA256 0x00000002u
  398. #define SSL_SHA384 0x00000004u
  399. // SSL_AEAD is set for all AEADs.
  400. #define SSL_AEAD 0x00000008u
  401. // Bits for |algorithm_prf| (handshake digest).
  402. #define SSL_HANDSHAKE_MAC_DEFAULT 0x1
  403. #define SSL_HANDSHAKE_MAC_SHA256 0x2
  404. #define SSL_HANDSHAKE_MAC_SHA384 0x4
  405. // An SSLCipherPreferenceList contains a list of SSL_CIPHERs with equal-
  406. // preference groups. For TLS clients, the groups are moot because the server
  407. // picks the cipher and groups cannot be expressed on the wire. However, for
  408. // servers, the equal-preference groups allow the client's preferences to be
  409. // partially respected. (This only has an effect with
  410. // SSL_OP_CIPHER_SERVER_PREFERENCE).
  411. //
  412. // The equal-preference groups are expressed by grouping SSL_CIPHERs together.
  413. // All elements of a group have the same priority: no ordering is expressed
  414. // within a group.
  415. //
  416. // The values in |ciphers| are in one-to-one correspondence with
  417. // |in_group_flags|. (That is, sk_SSL_CIPHER_num(ciphers) is the number of
  418. // bytes in |in_group_flags|.) The bytes in |in_group_flags| are either 1, to
  419. // indicate that the corresponding SSL_CIPHER is not the last element of a
  420. // group, or 0 to indicate that it is.
  421. //
  422. // For example, if |in_group_flags| contains all zeros then that indicates a
  423. // traditional, fully-ordered preference. Every SSL_CIPHER is the last element
  424. // of the group (i.e. they are all in a one-element group).
  425. //
  426. // For a more complex example, consider:
  427. // ciphers: A B C D E F
  428. // in_group_flags: 1 1 0 0 1 0
  429. //
  430. // That would express the following, order:
  431. //
  432. // A E
  433. // B -> D -> F
  434. // C
  435. struct SSLCipherPreferenceList {
  436. static constexpr bool kAllowUniquePtr = true;
  437. SSLCipherPreferenceList() = default;
  438. ~SSLCipherPreferenceList();
  439. bool Init(UniquePtr<STACK_OF(SSL_CIPHER)> ciphers,
  440. Span<const bool> in_group_flags);
  441. UniquePtr<STACK_OF(SSL_CIPHER)> ciphers;
  442. bool *in_group_flags = nullptr;
  443. };
  444. // ssl_cipher_get_evp_aead sets |*out_aead| to point to the correct EVP_AEAD
  445. // object for |cipher| protocol version |version|. It sets |*out_mac_secret_len|
  446. // and |*out_fixed_iv_len| to the MAC key length and fixed IV length,
  447. // respectively. The MAC key length is zero except for legacy block and stream
  448. // ciphers. It returns true on success and false on error.
  449. bool ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
  450. size_t *out_mac_secret_len,
  451. size_t *out_fixed_iv_len, const SSL_CIPHER *cipher,
  452. uint16_t version, int is_dtls);
  453. // ssl_get_handshake_digest returns the |EVP_MD| corresponding to |version| and
  454. // |cipher|.
  455. const EVP_MD *ssl_get_handshake_digest(uint16_t version,
  456. const SSL_CIPHER *cipher);
  457. // ssl_create_cipher_list evaluates |rule_str|. It sets |*out_cipher_list| to a
  458. // newly-allocated |SSLCipherPreferenceList| containing the result. It returns
  459. // true on success and false on failure. If |strict| is true, nonsense will be
  460. // rejected. If false, nonsense will be silently ignored. An empty result is
  461. // considered an error regardless of |strict|.
  462. bool ssl_create_cipher_list(SSLCipherPreferenceList **out_cipher_list,
  463. const char *rule_str, bool strict);
  464. // ssl_cipher_get_value returns the cipher suite id of |cipher|.
  465. uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher);
  466. // ssl_cipher_auth_mask_for_key returns the mask of cipher |algorithm_auth|
  467. // values suitable for use with |key| in TLS 1.2 and below.
  468. uint32_t ssl_cipher_auth_mask_for_key(const EVP_PKEY *key);
  469. // ssl_cipher_uses_certificate_auth returns whether |cipher| authenticates the
  470. // server and, optionally, the client with a certificate.
  471. bool ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher);
  472. // ssl_cipher_requires_server_key_exchange returns whether |cipher| requires a
  473. // ServerKeyExchange message.
  474. //
  475. // This function may return false while still allowing |cipher| an optional
  476. // ServerKeyExchange. This is the case for plain PSK ciphers.
  477. bool ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher);
  478. // ssl_cipher_get_record_split_len, for TLS 1.0 CBC mode ciphers, returns the
  479. // length of an encrypted 1-byte record, for use in record-splitting. Otherwise
  480. // it returns zero.
  481. size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher);
  482. // Transcript layer.
  483. // SSLTranscript maintains the handshake transcript as a combination of a
  484. // buffer and running hash.
  485. class SSLTranscript {
  486. public:
  487. SSLTranscript();
  488. ~SSLTranscript();
  489. // Init initializes the handshake transcript. If called on an existing
  490. // transcript, it resets the transcript and hash. It returns true on success
  491. // and false on failure.
  492. bool Init();
  493. // InitHash initializes the handshake hash based on the PRF and contents of
  494. // the handshake transcript. Subsequent calls to |Update| will update the
  495. // rolling hash. It returns one on success and zero on failure. It is an error
  496. // to call this function after the handshake buffer is released.
  497. bool InitHash(uint16_t version, const SSL_CIPHER *cipher);
  498. // UpdateForHelloRetryRequest resets the rolling hash with the
  499. // HelloRetryRequest construction. It returns true on success and false on
  500. // failure. It is an error to call this function before the handshake buffer
  501. // is released.
  502. bool UpdateForHelloRetryRequest();
  503. // CopyHashContext copies the hash context into |ctx| and returns true on
  504. // success.
  505. bool CopyHashContext(EVP_MD_CTX *ctx);
  506. Span<const uint8_t> buffer() {
  507. return MakeConstSpan(reinterpret_cast<const uint8_t *>(buffer_->data),
  508. buffer_->length);
  509. }
  510. // FreeBuffer releases the handshake buffer. Subsequent calls to
  511. // |Update| will not update the handshake buffer.
  512. void FreeBuffer();
  513. // DigestLen returns the length of the PRF hash.
  514. size_t DigestLen() const;
  515. // Digest returns the PRF hash. For TLS 1.1 and below, this is
  516. // |EVP_md5_sha1|.
  517. const EVP_MD *Digest() const;
  518. // Update adds |in| to the handshake buffer and handshake hash, whichever is
  519. // enabled. It returns true on success and false on failure.
  520. bool Update(Span<const uint8_t> in);
  521. // GetHash writes the handshake hash to |out| which must have room for at
  522. // least |DigestLen| bytes. On success, it returns true and sets |*out_len| to
  523. // the number of bytes written. Otherwise, it returns false.
  524. bool GetHash(uint8_t *out, size_t *out_len);
  525. // GetSSL3CertVerifyHash writes the SSL 3.0 CertificateVerify hash into the
  526. // bytes pointed to by |out| and writes the number of bytes to
  527. // |*out_len|. |out| must have room for |EVP_MAX_MD_SIZE| bytes. It returns
  528. // one on success and zero on failure.
  529. bool GetSSL3CertVerifyHash(uint8_t *out, size_t *out_len,
  530. const SSL_SESSION *session,
  531. uint16_t signature_algorithm);
  532. // GetFinishedMAC computes the MAC for the Finished message into the bytes
  533. // pointed by |out| and writes the number of bytes to |*out_len|. |out| must
  534. // have room for |EVP_MAX_MD_SIZE| bytes. It returns true on success and false
  535. // on failure.
  536. bool GetFinishedMAC(uint8_t *out, size_t *out_len, const SSL_SESSION *session,
  537. bool from_server);
  538. private:
  539. // buffer_, if non-null, contains the handshake transcript.
  540. UniquePtr<BUF_MEM> buffer_;
  541. // hash, if initialized with an |EVP_MD|, maintains the handshake hash. For
  542. // TLS 1.1 and below, it is the SHA-1 half.
  543. ScopedEVP_MD_CTX hash_;
  544. // md5, if initialized with an |EVP_MD|, maintains the MD5 half of the
  545. // handshake hash for TLS 1.1 and below.
  546. ScopedEVP_MD_CTX md5_;
  547. };
  548. // tls1_prf computes the PRF function for |ssl|. It fills |out|, using |secret|
  549. // as the secret and |label| as the label. |seed1| and |seed2| are concatenated
  550. // to form the seed parameter. It returns true on success and false on failure.
  551. bool tls1_prf(const EVP_MD *digest, Span<uint8_t> out,
  552. Span<const uint8_t> secret, Span<const char> label,
  553. Span<const uint8_t> seed1, Span<const uint8_t> seed2);
  554. // Encryption layer.
  555. // SSLAEADContext contains information about an AEAD that is being used to
  556. // encrypt an SSL connection.
  557. class SSLAEADContext {
  558. public:
  559. SSLAEADContext(uint16_t version, bool is_dtls, const SSL_CIPHER *cipher);
  560. ~SSLAEADContext();
  561. static constexpr bool kAllowUniquePtr = true;
  562. SSLAEADContext(const SSLAEADContext &&) = delete;
  563. SSLAEADContext &operator=(const SSLAEADContext &&) = delete;
  564. // CreateNullCipher creates an |SSLAEADContext| for the null cipher.
  565. static UniquePtr<SSLAEADContext> CreateNullCipher(bool is_dtls);
  566. // Create creates an |SSLAEADContext| using the supplied key material. It
  567. // returns nullptr on error. Only one of |Open| or |Seal| may be used with the
  568. // resulting object, depending on |direction|. |version| is the normalized
  569. // protocol version, so DTLS 1.0 is represented as 0x0301, not 0xffef.
  570. static UniquePtr<SSLAEADContext> Create(enum evp_aead_direction_t direction,
  571. uint16_t version, int is_dtls,
  572. const SSL_CIPHER *cipher,
  573. Span<const uint8_t> enc_key,
  574. Span<const uint8_t> mac_key,
  575. Span<const uint8_t> fixed_iv);
  576. // SetVersionIfNullCipher sets the version the SSLAEADContext for the null
  577. // cipher, to make version-specific determinations in the record layer prior
  578. // to a cipher being selected.
  579. void SetVersionIfNullCipher(uint16_t version);
  580. // ProtocolVersion returns the protocol version associated with this
  581. // SSLAEADContext. It can only be called once |version_| has been set to a
  582. // valid value.
  583. uint16_t ProtocolVersion() const;
  584. // RecordVersion returns the record version that should be used with this
  585. // SSLAEADContext for record construction and crypto.
  586. uint16_t RecordVersion() const;
  587. const SSL_CIPHER *cipher() const { return cipher_; }
  588. // is_null_cipher returns true if this is the null cipher.
  589. bool is_null_cipher() const { return !cipher_; }
  590. // ExplicitNonceLen returns the length of the explicit nonce.
  591. size_t ExplicitNonceLen() const;
  592. // MaxOverhead returns the maximum overhead of calling |Seal|.
  593. size_t MaxOverhead() const;
  594. // SuffixLen calculates the suffix length written by |SealScatter| and writes
  595. // it to |*out_suffix_len|. It returns true on success and false on error.
  596. // |in_len| and |extra_in_len| should equal the argument of the same names
  597. // passed to |SealScatter|.
  598. bool SuffixLen(size_t *out_suffix_len, size_t in_len,
  599. size_t extra_in_len) const;
  600. // CiphertextLen calculates the total ciphertext length written by
  601. // |SealScatter| and writes it to |*out_len|. It returns true on success and
  602. // false on error. |in_len| and |extra_in_len| should equal the argument of
  603. // the same names passed to |SealScatter|.
  604. bool CiphertextLen(size_t *out_len, size_t in_len, size_t extra_in_len) const;
  605. // Open authenticates and decrypts |in| in-place. On success, it sets |*out|
  606. // to the plaintext in |in| and returns true. Otherwise, it returns
  607. // false. The output will always be |ExplicitNonceLen| bytes ahead of |in|.
  608. bool Open(Span<uint8_t> *out, uint8_t type, uint16_t record_version,
  609. const uint8_t seqnum[8], Span<const uint8_t> header,
  610. Span<uint8_t> in);
  611. // Seal encrypts and authenticates |in_len| bytes from |in| and writes the
  612. // result to |out|. It returns true on success and false on error.
  613. //
  614. // If |in| and |out| alias then |out| + |ExplicitNonceLen| must be == |in|.
  615. bool Seal(uint8_t *out, size_t *out_len, size_t max_out, uint8_t type,
  616. uint16_t record_version, const uint8_t seqnum[8],
  617. Span<const uint8_t> header, const uint8_t *in, size_t in_len);
  618. // SealScatter encrypts and authenticates |in_len| bytes from |in| and splits
  619. // the result between |out_prefix|, |out| and |out_suffix|. It returns one on
  620. // success and zero on error.
  621. //
  622. // On successful return, exactly |ExplicitNonceLen| bytes are written to
  623. // |out_prefix|, |in_len| bytes to |out|, and |SuffixLen| bytes to
  624. // |out_suffix|.
  625. //
  626. // |extra_in| may point to an additional plaintext buffer. If present,
  627. // |extra_in_len| additional bytes are encrypted and authenticated, and the
  628. // ciphertext is written to the beginning of |out_suffix|. |SuffixLen| should
  629. // be used to size |out_suffix| accordingly.
  630. //
  631. // If |in| and |out| alias then |out| must be == |in|. Other arguments may not
  632. // alias anything.
  633. bool SealScatter(uint8_t *out_prefix, uint8_t *out, uint8_t *out_suffix,
  634. uint8_t type, uint16_t record_version,
  635. const uint8_t seqnum[8], Span<const uint8_t> header,
  636. const uint8_t *in, size_t in_len, const uint8_t *extra_in,
  637. size_t extra_in_len);
  638. bool GetIV(const uint8_t **out_iv, size_t *out_iv_len) const;
  639. private:
  640. // GetAdditionalData returns the additional data, writing into |storage| if
  641. // necessary.
  642. Span<const uint8_t> GetAdditionalData(uint8_t storage[13], uint8_t type,
  643. uint16_t record_version,
  644. const uint8_t seqnum[8],
  645. size_t plaintext_len,
  646. Span<const uint8_t> header);
  647. const SSL_CIPHER *cipher_;
  648. ScopedEVP_AEAD_CTX ctx_;
  649. // fixed_nonce_ contains any bytes of the nonce that are fixed for all
  650. // records.
  651. uint8_t fixed_nonce_[12];
  652. uint8_t fixed_nonce_len_ = 0, variable_nonce_len_ = 0;
  653. // version_ is the wire version that should be used with this AEAD.
  654. uint16_t version_;
  655. // is_dtls_ is whether DTLS is being used with this AEAD.
  656. bool is_dtls_;
  657. // variable_nonce_included_in_record_ is true if the variable nonce
  658. // for a record is included as a prefix before the ciphertext.
  659. bool variable_nonce_included_in_record_ : 1;
  660. // random_variable_nonce_ is true if the variable nonce is
  661. // randomly generated, rather than derived from the sequence
  662. // number.
  663. bool random_variable_nonce_ : 1;
  664. // xor_fixed_nonce_ is true if the fixed nonce should be XOR'd into the
  665. // variable nonce rather than prepended.
  666. bool xor_fixed_nonce_ : 1;
  667. // omit_length_in_ad_ is true if the length should be omitted in the
  668. // AEAD's ad parameter.
  669. bool omit_length_in_ad_ : 1;
  670. // omit_version_in_ad_ is true if the version should be omitted
  671. // in the AEAD's ad parameter.
  672. bool omit_version_in_ad_ : 1;
  673. // omit_ad_ is true if the AEAD's ad parameter should be omitted.
  674. bool omit_ad_ : 1;
  675. // ad_is_header_ is true if the AEAD's ad parameter is the record header.
  676. bool ad_is_header_ : 1;
  677. };
  678. // DTLS replay bitmap.
  679. // DTLS1_BITMAP maintains a sliding window of 64 sequence numbers to detect
  680. // replayed packets. It should be initialized by zeroing every field.
  681. struct DTLS1_BITMAP {
  682. // map is a bit mask of the last 64 sequence numbers. Bit
  683. // |1<<i| corresponds to |max_seq_num - i|.
  684. uint64_t map = 0;
  685. // max_seq_num is the largest sequence number seen so far as a 64-bit
  686. // integer.
  687. uint64_t max_seq_num = 0;
  688. };
  689. // Record layer.
  690. // ssl_record_sequence_update increments the sequence number in |seq|. It
  691. // returns one on success and zero on wraparound.
  692. int ssl_record_sequence_update(uint8_t *seq, size_t seq_len);
  693. // ssl_record_prefix_len returns the length of the prefix before the ciphertext
  694. // of a record for |ssl|.
  695. //
  696. // TODO(davidben): Expose this as part of public API once the high-level
  697. // buffer-free APIs are available.
  698. size_t ssl_record_prefix_len(const SSL *ssl);
  699. enum ssl_open_record_t {
  700. ssl_open_record_success,
  701. ssl_open_record_discard,
  702. ssl_open_record_partial,
  703. ssl_open_record_close_notify,
  704. ssl_open_record_error,
  705. };
  706. // tls_open_record decrypts a record from |in| in-place.
  707. //
  708. // If the input did not contain a complete record, it returns
  709. // |ssl_open_record_partial|. It sets |*out_consumed| to the total number of
  710. // bytes necessary. It is guaranteed that a successful call to |tls_open_record|
  711. // will consume at least that many bytes.
  712. //
  713. // Otherwise, it sets |*out_consumed| to the number of bytes of input
  714. // consumed. Note that input may be consumed on all return codes if a record was
  715. // decrypted.
  716. //
  717. // On success, it returns |ssl_open_record_success|. It sets |*out_type| to the
  718. // record type and |*out| to the record body in |in|. Note that |*out| may be
  719. // empty.
  720. //
  721. // If a record was successfully processed but should be discarded, it returns
  722. // |ssl_open_record_discard|.
  723. //
  724. // If a record was successfully processed but is a close_notify, it returns
  725. // |ssl_open_record_close_notify|.
  726. //
  727. // On failure or fatal alert, it returns |ssl_open_record_error| and sets
  728. // |*out_alert| to an alert to emit, or zero if no alert should be emitted.
  729. enum ssl_open_record_t tls_open_record(SSL *ssl, uint8_t *out_type,
  730. Span<uint8_t> *out, size_t *out_consumed,
  731. uint8_t *out_alert, Span<uint8_t> in);
  732. // dtls_open_record implements |tls_open_record| for DTLS. It only returns
  733. // |ssl_open_record_partial| if |in| was empty and sets |*out_consumed| to
  734. // zero. The caller should read one packet and try again.
  735. enum ssl_open_record_t dtls_open_record(SSL *ssl, uint8_t *out_type,
  736. Span<uint8_t> *out,
  737. size_t *out_consumed,
  738. uint8_t *out_alert, Span<uint8_t> in);
  739. // ssl_seal_align_prefix_len returns the length of the prefix before the start
  740. // of the bulk of the ciphertext when sealing a record with |ssl|. Callers may
  741. // use this to align buffers.
  742. //
  743. // Note when TLS 1.0 CBC record-splitting is enabled, this includes the one byte
  744. // record and is the offset into second record's ciphertext. Thus sealing a
  745. // small record may result in a smaller output than this value.
  746. //
  747. // TODO(davidben): Is this alignment valuable? Record-splitting makes this a
  748. // mess.
  749. size_t ssl_seal_align_prefix_len(const SSL *ssl);
  750. // tls_seal_record seals a new record of type |type| and body |in| and writes it
  751. // to |out|. At most |max_out| bytes will be written. It returns one on success
  752. // and zero on error. If enabled, |tls_seal_record| implements TLS 1.0 CBC 1/n-1
  753. // record splitting and may write two records concatenated.
  754. //
  755. // For a large record, the bulk of the ciphertext will begin
  756. // |ssl_seal_align_prefix_len| bytes into out. Aligning |out| appropriately may
  757. // improve performance. It writes at most |in_len| + |SSL_max_seal_overhead|
  758. // bytes to |out|.
  759. //
  760. // |in| and |out| may not alias.
  761. int tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
  762. uint8_t type, const uint8_t *in, size_t in_len);
  763. enum dtls1_use_epoch_t {
  764. dtls1_use_previous_epoch,
  765. dtls1_use_current_epoch,
  766. };
  767. // dtls_max_seal_overhead returns the maximum overhead, in bytes, of sealing a
  768. // record.
  769. size_t dtls_max_seal_overhead(const SSL *ssl, enum dtls1_use_epoch_t use_epoch);
  770. // dtls_seal_prefix_len returns the number of bytes of prefix to reserve in
  771. // front of the plaintext when sealing a record in-place.
  772. size_t dtls_seal_prefix_len(const SSL *ssl, enum dtls1_use_epoch_t use_epoch);
  773. // dtls_seal_record implements |tls_seal_record| for DTLS. |use_epoch| selects
  774. // which epoch's cipher state to use. Unlike |tls_seal_record|, |in| and |out|
  775. // may alias but, if they do, |in| must be exactly |dtls_seal_prefix_len| bytes
  776. // ahead of |out|.
  777. int dtls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
  778. uint8_t type, const uint8_t *in, size_t in_len,
  779. enum dtls1_use_epoch_t use_epoch);
  780. // ssl_process_alert processes |in| as an alert and updates |ssl|'s shutdown
  781. // state. It returns one of |ssl_open_record_discard|, |ssl_open_record_error|,
  782. // |ssl_open_record_close_notify|, or |ssl_open_record_fatal_alert| as
  783. // appropriate.
  784. enum ssl_open_record_t ssl_process_alert(SSL *ssl, uint8_t *out_alert,
  785. Span<const uint8_t> in);
  786. // Private key operations.
  787. // ssl_has_private_key returns one if |ssl| has a private key
  788. // configured and zero otherwise.
  789. int ssl_has_private_key(const SSL *ssl);
  790. // ssl_private_key_* perform the corresponding operation on
  791. // |SSL_PRIVATE_KEY_METHOD|. If there is a custom private key configured, they
  792. // call the corresponding function or |complete| depending on whether there is a
  793. // pending operation. Otherwise, they implement the operation with
  794. // |EVP_PKEY|.
  795. enum ssl_private_key_result_t ssl_private_key_sign(
  796. SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out,
  797. uint16_t sigalg, Span<const uint8_t> in);
  798. enum ssl_private_key_result_t ssl_private_key_decrypt(SSL_HANDSHAKE *hs,
  799. uint8_t *out,
  800. size_t *out_len,
  801. size_t max_out,
  802. Span<const uint8_t> in);
  803. // ssl_private_key_supports_signature_algorithm returns whether |hs|'s private
  804. // key supports |sigalg|.
  805. bool ssl_private_key_supports_signature_algorithm(SSL_HANDSHAKE *hs,
  806. uint16_t sigalg);
  807. // ssl_public_key_verify verifies that the |signature| is valid for the public
  808. // key |pkey| and input |in|, using the signature algorithm |sigalg|.
  809. bool ssl_public_key_verify(SSL *ssl, Span<const uint8_t> signature,
  810. uint16_t sigalg, EVP_PKEY *pkey,
  811. Span<const uint8_t> in);
  812. // Custom extensions
  813. } // namespace bssl
  814. // |SSL_CUSTOM_EXTENSION| is a structure that contains information about
  815. // custom-extension callbacks. It is defined unnamespaced for compatibility with
  816. // |STACK_OF(SSL_CUSTOM_EXTENSION)|.
  817. typedef struct ssl_custom_extension {
  818. SSL_custom_ext_add_cb add_callback;
  819. void *add_arg;
  820. SSL_custom_ext_free_cb free_callback;
  821. SSL_custom_ext_parse_cb parse_callback;
  822. void *parse_arg;
  823. uint16_t value;
  824. } SSL_CUSTOM_EXTENSION;
  825. DEFINE_STACK_OF(SSL_CUSTOM_EXTENSION)
  826. namespace bssl {
  827. void SSL_CUSTOM_EXTENSION_free(SSL_CUSTOM_EXTENSION *custom_extension);
  828. int custom_ext_add_clienthello(SSL_HANDSHAKE *hs, CBB *extensions);
  829. int custom_ext_parse_serverhello(SSL_HANDSHAKE *hs, int *out_alert,
  830. uint16_t value, const CBS *extension);
  831. int custom_ext_parse_clienthello(SSL_HANDSHAKE *hs, int *out_alert,
  832. uint16_t value, const CBS *extension);
  833. int custom_ext_add_serverhello(SSL_HANDSHAKE *hs, CBB *extensions);
  834. // Key shares.
  835. // SSLKeyShare abstracts over Diffie-Hellman-like key exchanges.
  836. class SSLKeyShare {
  837. public:
  838. virtual ~SSLKeyShare() {}
  839. static constexpr bool kAllowUniquePtr = true;
  840. HAS_VIRTUAL_DESTRUCTOR
  841. // Create returns a SSLKeyShare instance for use with group |group_id| or
  842. // nullptr on error.
  843. static UniquePtr<SSLKeyShare> Create(uint16_t group_id);
  844. // Create deserializes an SSLKeyShare instance previously serialized by
  845. // |Serialize|.
  846. static UniquePtr<SSLKeyShare> Create(CBS *in);
  847. // GroupID returns the group ID.
  848. virtual uint16_t GroupID() const PURE_VIRTUAL;
  849. // Offer generates a keypair and writes the public value to
  850. // |out_public_key|. It returns true on success and false on error.
  851. virtual bool Offer(CBB *out_public_key) PURE_VIRTUAL;
  852. // Accept performs a key exchange against the |peer_key| generated by |offer|.
  853. // On success, it returns true, writes the public value to |out_public_key|,
  854. // and sets |*out_secret| the shared secret. On failure, it returns false and
  855. // sets |*out_alert| to an alert to send to the peer.
  856. //
  857. // The default implementation calls |Offer| and then |Finish|, assuming a key
  858. // exchange protocol where the peers are symmetric.
  859. virtual bool Accept(CBB *out_public_key, Array<uint8_t> *out_secret,
  860. uint8_t *out_alert, Span<const uint8_t> peer_key);
  861. // Finish performs a key exchange against the |peer_key| generated by
  862. // |Accept|. On success, it returns true and sets |*out_secret| to the shared
  863. // secret. On failure, it returns zero and sets |*out_alert| to an alert to
  864. // send to the peer.
  865. virtual bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert,
  866. Span<const uint8_t> peer_key) PURE_VIRTUAL;
  867. // Serialize writes the state of the key exchange to |out|, returning true if
  868. // successful and false otherwise.
  869. virtual bool Serialize(CBB *out) { return false; }
  870. // Deserialize initializes the state of the key exchange from |in|, returning
  871. // true if successful and false otherwise. It is called by |Create|.
  872. virtual bool Deserialize(CBS *in) { return false; }
  873. };
  874. // ssl_nid_to_group_id looks up the group corresponding to |nid|. On success, it
  875. // sets |*out_group_id| to the group ID and returns one. Otherwise, it returns
  876. // zero.
  877. int ssl_nid_to_group_id(uint16_t *out_group_id, int nid);
  878. // ssl_name_to_group_id looks up the group corresponding to the |name| string
  879. // of length |len|. On success, it sets |*out_group_id| to the group ID and
  880. // returns one. Otherwise, it returns zero.
  881. int ssl_name_to_group_id(uint16_t *out_group_id, const char *name, size_t len);
  882. // Handshake messages.
  883. struct SSLMessage {
  884. bool is_v2_hello;
  885. uint8_t type;
  886. CBS body;
  887. // raw is the entire serialized handshake message, including the TLS or DTLS
  888. // message header.
  889. CBS raw;
  890. };
  891. // SSL_MAX_HANDSHAKE_FLIGHT is the number of messages, including
  892. // ChangeCipherSpec, in the longest handshake flight. Currently this is the
  893. // client's second leg in a full handshake when client certificates, NPN, and
  894. // Channel ID, are all enabled.
  895. #define SSL_MAX_HANDSHAKE_FLIGHT 7
  896. extern const uint8_t kHelloRetryRequest[SSL3_RANDOM_SIZE];
  897. extern const uint8_t kDraftDowngradeRandom[8];
  898. // ssl_max_handshake_message_len returns the maximum number of bytes permitted
  899. // in a handshake message for |ssl|.
  900. size_t ssl_max_handshake_message_len(const SSL *ssl);
  901. // tls_can_accept_handshake_data returns whether |ssl| is able to accept more
  902. // data into handshake buffer.
  903. bool tls_can_accept_handshake_data(const SSL *ssl, uint8_t *out_alert);
  904. // tls_has_unprocessed_handshake_data returns whether there is buffered
  905. // handshake data that has not been consumed by |get_message|.
  906. bool tls_has_unprocessed_handshake_data(const SSL *ssl);
  907. // dtls_has_unprocessed_handshake_data behaves like
  908. // |tls_has_unprocessed_handshake_data| for DTLS.
  909. bool dtls_has_unprocessed_handshake_data(const SSL *ssl);
  910. struct DTLS_OUTGOING_MESSAGE {
  911. DTLS_OUTGOING_MESSAGE() {}
  912. DTLS_OUTGOING_MESSAGE(const DTLS_OUTGOING_MESSAGE &) = delete;
  913. DTLS_OUTGOING_MESSAGE &operator=(const DTLS_OUTGOING_MESSAGE &) = delete;
  914. ~DTLS_OUTGOING_MESSAGE() { Clear(); }
  915. void Clear();
  916. uint8_t *data = nullptr;
  917. uint32_t len = 0;
  918. uint16_t epoch = 0;
  919. bool is_ccs = false;
  920. };
  921. // dtls_clear_outgoing_messages releases all buffered outgoing messages.
  922. void dtls_clear_outgoing_messages(SSL *ssl);
  923. // Callbacks.
  924. // ssl_do_info_callback calls |ssl|'s info callback, if set.
  925. void ssl_do_info_callback(const SSL *ssl, int type, int value);
  926. // ssl_do_msg_callback calls |ssl|'s message callback, if set.
  927. void ssl_do_msg_callback(SSL *ssl, int is_write, int content_type,
  928. Span<const uint8_t> in);
  929. // Transport buffers.
  930. class SSLBuffer {
  931. public:
  932. SSLBuffer() {}
  933. ~SSLBuffer() { Clear(); }
  934. SSLBuffer(const SSLBuffer &) = delete;
  935. SSLBuffer &operator=(const SSLBuffer &) = delete;
  936. uint8_t *data() { return buf_ + offset_; }
  937. size_t size() const { return size_; }
  938. bool empty() const { return size_ == 0; }
  939. size_t cap() const { return cap_; }
  940. Span<uint8_t> span() { return MakeSpan(data(), size()); }
  941. Span<uint8_t> remaining() {
  942. return MakeSpan(data() + size(), cap() - size());
  943. }
  944. // Clear releases the buffer.
  945. void Clear();
  946. // EnsureCap ensures the buffer has capacity at least |new_cap|, aligned such
  947. // that data written after |header_len| is aligned to a
  948. // |SSL3_ALIGN_PAYLOAD|-byte boundary. It returns true on success and false
  949. // on error.
  950. bool EnsureCap(size_t header_len, size_t new_cap);
  951. // DidWrite extends the buffer by |len|. The caller must have filled in to
  952. // this point.
  953. void DidWrite(size_t len);
  954. // Consume consumes |len| bytes from the front of the buffer. The memory
  955. // consumed will remain valid until the next call to |DiscardConsumed| or
  956. // |Clear|.
  957. void Consume(size_t len);
  958. // DiscardConsumed discards the consumed bytes from the buffer. If the buffer
  959. // is now empty, it releases memory used by it.
  960. void DiscardConsumed();
  961. private:
  962. // buf_ is the memory allocated for this buffer.
  963. uint8_t *buf_ = nullptr;
  964. // offset_ is the offset into |buf_| which the buffer contents start at.
  965. uint16_t offset_ = 0;
  966. // size_ is the size of the buffer contents from |buf_| + |offset_|.
  967. uint16_t size_ = 0;
  968. // cap_ is how much memory beyond |buf_| + |offset_| is available.
  969. uint16_t cap_ = 0;
  970. };
  971. // ssl_read_buffer_extend_to extends the read buffer to the desired length. For
  972. // TLS, it reads to the end of the buffer until the buffer is |len| bytes
  973. // long. For DTLS, it reads a new packet and ignores |len|. It returns one on
  974. // success, zero on EOF, and a negative number on error.
  975. //
  976. // It is an error to call |ssl_read_buffer_extend_to| in DTLS when the buffer is
  977. // non-empty.
  978. int ssl_read_buffer_extend_to(SSL *ssl, size_t len);
  979. // ssl_handle_open_record handles the result of passing |ssl->s3->read_buffer|
  980. // to a record-processing function. If |ret| is a success or if the caller
  981. // should retry, it returns one and sets |*out_retry|. Otherwise, it returns <=
  982. // 0.
  983. int ssl_handle_open_record(SSL *ssl, bool *out_retry, ssl_open_record_t ret,
  984. size_t consumed, uint8_t alert);
  985. // ssl_write_buffer_flush flushes the write buffer to the transport. It returns
  986. // one on success and <= 0 on error. For DTLS, whether or not the write
  987. // succeeds, the write buffer will be cleared.
  988. int ssl_write_buffer_flush(SSL *ssl);
  989. // Certificate functions.
  990. // ssl_has_certificate returns one if a certificate and private key are
  991. // configured and zero otherwise.
  992. int ssl_has_certificate(const SSL *ssl);
  993. // ssl_parse_cert_chain parses a certificate list from |cbs| in the format used
  994. // by a TLS Certificate message. On success, it advances |cbs| and returns
  995. // true. Otherwise, it returns false and sets |*out_alert| to an alert to send
  996. // to the peer.
  997. //
  998. // If the list is non-empty then |*out_chain| and |*out_pubkey| will be set to
  999. // the certificate chain and the leaf certificate's public key
  1000. // respectively. Otherwise, both will be set to nullptr.
  1001. //
  1002. // If the list is non-empty and |out_leaf_sha256| is non-NULL, it writes the
  1003. // SHA-256 hash of the leaf to |out_leaf_sha256|.
  1004. bool ssl_parse_cert_chain(uint8_t *out_alert,
  1005. UniquePtr<STACK_OF(CRYPTO_BUFFER)> *out_chain,
  1006. UniquePtr<EVP_PKEY> *out_pubkey,
  1007. uint8_t *out_leaf_sha256, CBS *cbs,
  1008. CRYPTO_BUFFER_POOL *pool);
  1009. // ssl_add_cert_chain adds |ssl|'s certificate chain to |cbb| in the format used
  1010. // by a TLS Certificate message. If there is no certificate chain, it emits an
  1011. // empty certificate list. It returns one on success and zero on error.
  1012. int ssl_add_cert_chain(SSL *ssl, CBB *cbb);
  1013. // ssl_cert_check_digital_signature_key_usage parses the DER-encoded, X.509
  1014. // certificate in |in| and returns one if doesn't specify a key usage or, if it
  1015. // does, if it includes digitalSignature. Otherwise it pushes to the error
  1016. // queue and returns zero.
  1017. int ssl_cert_check_digital_signature_key_usage(const CBS *in);
  1018. // ssl_cert_parse_pubkey extracts the public key from the DER-encoded, X.509
  1019. // certificate in |in|. It returns an allocated |EVP_PKEY| or else returns
  1020. // nullptr and pushes to the error queue.
  1021. UniquePtr<EVP_PKEY> ssl_cert_parse_pubkey(const CBS *in);
  1022. // ssl_parse_client_CA_list parses a CA list from |cbs| in the format used by a
  1023. // TLS CertificateRequest message. On success, it returns a newly-allocated
  1024. // |CRYPTO_BUFFER| list and advances |cbs|. Otherwise, it returns nullptr and
  1025. // sets |*out_alert| to an alert to send to the peer.
  1026. UniquePtr<STACK_OF(CRYPTO_BUFFER)> ssl_parse_client_CA_list(SSL *ssl,
  1027. uint8_t *out_alert,
  1028. CBS *cbs);
  1029. // ssl_has_client_CAs returns there are configured CAs.
  1030. bool ssl_has_client_CAs(SSL *ssl);
  1031. // ssl_add_client_CA_list adds the configured CA list to |cbb| in the format
  1032. // used by a TLS CertificateRequest message. It returns one on success and zero
  1033. // on error.
  1034. int ssl_add_client_CA_list(SSL *ssl, CBB *cbb);
  1035. // ssl_check_leaf_certificate returns one if |pkey| and |leaf| are suitable as
  1036. // a server's leaf certificate for |hs|. Otherwise, it returns zero and pushes
  1037. // an error on the error queue.
  1038. int ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey,
  1039. const CRYPTO_BUFFER *leaf);
  1040. // ssl_on_certificate_selected is called once the certificate has been selected.
  1041. // It finalizes the certificate and initializes |hs->local_pubkey|. It returns
  1042. // one on success and zero on error.
  1043. int ssl_on_certificate_selected(SSL_HANDSHAKE *hs);
  1044. // TLS 1.3 key derivation.
  1045. // tls13_init_key_schedule initializes the handshake hash and key derivation
  1046. // state, and incorporates the PSK. The cipher suite and PRF hash must have been
  1047. // selected at this point. It returns one on success and zero on error.
  1048. int tls13_init_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *psk,
  1049. size_t psk_len);
  1050. // tls13_init_early_key_schedule initializes the handshake hash and key
  1051. // derivation state from the resumption secret and incorporates the PSK to
  1052. // derive the early secrets. It returns one on success and zero on error.
  1053. int tls13_init_early_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *psk,
  1054. size_t psk_len);
  1055. // tls13_advance_key_schedule incorporates |in| into the key schedule with
  1056. // HKDF-Extract. It returns one on success and zero on error.
  1057. int tls13_advance_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *in,
  1058. size_t len);
  1059. // tls13_set_traffic_key sets the read or write traffic keys to
  1060. // |traffic_secret|. It returns one on success and zero on error.
  1061. int tls13_set_traffic_key(SSL *ssl, enum evp_aead_direction_t direction,
  1062. const uint8_t *traffic_secret,
  1063. size_t traffic_secret_len);
  1064. // tls13_derive_early_secrets derives the early traffic secret. It returns one
  1065. // on success and zero on error.
  1066. int tls13_derive_early_secrets(SSL_HANDSHAKE *hs);
  1067. // tls13_derive_handshake_secrets derives the handshake traffic secret. It
  1068. // returns one on success and zero on error.
  1069. int tls13_derive_handshake_secrets(SSL_HANDSHAKE *hs);
  1070. // tls13_rotate_traffic_key derives the next read or write traffic secret. It
  1071. // returns one on success and zero on error.
  1072. int tls13_rotate_traffic_key(SSL *ssl, enum evp_aead_direction_t direction);
  1073. // tls13_derive_application_secrets derives the initial application data traffic
  1074. // and exporter secrets based on the handshake transcripts and |master_secret|.
  1075. // It returns one on success and zero on error.
  1076. int tls13_derive_application_secrets(SSL_HANDSHAKE *hs);
  1077. // tls13_derive_resumption_secret derives the |resumption_secret|.
  1078. int tls13_derive_resumption_secret(SSL_HANDSHAKE *hs);
  1079. // tls13_export_keying_material provides an exporter interface to use the
  1080. // |exporter_secret|.
  1081. int tls13_export_keying_material(SSL *ssl, Span<uint8_t> out,
  1082. Span<const uint8_t> secret,
  1083. Span<const char> label,
  1084. Span<const uint8_t> context);
  1085. // tls13_finished_mac calculates the MAC of the handshake transcript to verify
  1086. // the integrity of the Finished message, and stores the result in |out| and
  1087. // length in |out_len|. |is_server| is 1 if this is for the Server Finished and
  1088. // 0 for the Client Finished.
  1089. int tls13_finished_mac(SSL_HANDSHAKE *hs, uint8_t *out,
  1090. size_t *out_len, int is_server);
  1091. // tls13_derive_session_psk calculates the PSK for this session based on the
  1092. // resumption master secret and |nonce|. It returns true on success, and false
  1093. // on failure.
  1094. bool tls13_derive_session_psk(SSL_SESSION *session, Span<const uint8_t> nonce);
  1095. // tls13_write_psk_binder calculates the PSK binder value and replaces the last
  1096. // bytes of |msg| with the resulting value. It returns 1 on success, and 0 on
  1097. // failure.
  1098. int tls13_write_psk_binder(SSL_HANDSHAKE *hs, uint8_t *msg, size_t len);
  1099. // tls13_verify_psk_binder verifies that the handshake transcript, truncated
  1100. // up to the binders has a valid signature using the value of |session|'s
  1101. // resumption secret. It returns 1 on success, and 0 on failure.
  1102. int tls13_verify_psk_binder(SSL_HANDSHAKE *hs, SSL_SESSION *session,
  1103. const SSLMessage &msg, CBS *binders);
  1104. // Handshake functions.
  1105. enum ssl_hs_wait_t {
  1106. ssl_hs_error,
  1107. ssl_hs_ok,
  1108. ssl_hs_read_server_hello,
  1109. ssl_hs_read_message,
  1110. ssl_hs_flush,
  1111. ssl_hs_certificate_selection_pending,
  1112. ssl_hs_handoff,
  1113. ssl_hs_handback,
  1114. ssl_hs_x509_lookup,
  1115. ssl_hs_channel_id_lookup,
  1116. ssl_hs_private_key_operation,
  1117. ssl_hs_pending_session,
  1118. ssl_hs_pending_ticket,
  1119. ssl_hs_early_return,
  1120. ssl_hs_early_data_rejected,
  1121. ssl_hs_read_end_of_early_data,
  1122. ssl_hs_read_change_cipher_spec,
  1123. ssl_hs_certificate_verify,
  1124. };
  1125. enum ssl_grease_index_t {
  1126. ssl_grease_cipher = 0,
  1127. ssl_grease_group,
  1128. ssl_grease_extension1,
  1129. ssl_grease_extension2,
  1130. ssl_grease_version,
  1131. ssl_grease_ticket_extension,
  1132. ssl_grease_last_index = ssl_grease_ticket_extension,
  1133. };
  1134. enum tls12_server_hs_state_t {
  1135. state12_start_accept = 0,
  1136. state12_read_client_hello,
  1137. state12_select_certificate,
  1138. state12_tls13,
  1139. state12_select_parameters,
  1140. state12_send_server_hello,
  1141. state12_send_server_certificate,
  1142. state12_send_server_key_exchange,
  1143. state12_send_server_hello_done,
  1144. state12_read_client_certificate,
  1145. state12_verify_client_certificate,
  1146. state12_read_client_key_exchange,
  1147. state12_read_client_certificate_verify,
  1148. state12_read_change_cipher_spec,
  1149. state12_process_change_cipher_spec,
  1150. state12_read_next_proto,
  1151. state12_read_channel_id,
  1152. state12_read_client_finished,
  1153. state12_send_server_finished,
  1154. state12_finish_server_handshake,
  1155. state12_done,
  1156. };
  1157. struct SSL_HANDSHAKE {
  1158. explicit SSL_HANDSHAKE(SSL *ssl);
  1159. ~SSL_HANDSHAKE();
  1160. static constexpr bool kAllowUniquePtr = true;
  1161. // ssl is a non-owning pointer to the parent |SSL| object.
  1162. SSL *ssl;
  1163. // wait contains the operation the handshake is currently blocking on or
  1164. // |ssl_hs_ok| if none.
  1165. enum ssl_hs_wait_t wait = ssl_hs_ok;
  1166. // state is the internal state for the TLS 1.2 and below handshake. Its
  1167. // values depend on |do_handshake| but the starting state is always zero.
  1168. int state = 0;
  1169. // tls13_state is the internal state for the TLS 1.3 handshake. Its values
  1170. // depend on |do_handshake| but the starting state is always zero.
  1171. int tls13_state = 0;
  1172. // min_version is the minimum accepted protocol version, taking account both
  1173. // |SSL_OP_NO_*| and |SSL_CTX_set_min_proto_version| APIs.
  1174. uint16_t min_version = 0;
  1175. // max_version is the maximum accepted protocol version, taking account both
  1176. // |SSL_OP_NO_*| and |SSL_CTX_set_max_proto_version| APIs.
  1177. uint16_t max_version = 0;
  1178. size_t hash_len = 0;
  1179. uint8_t secret[EVP_MAX_MD_SIZE] = {0};
  1180. uint8_t early_traffic_secret[EVP_MAX_MD_SIZE] = {0};
  1181. uint8_t client_handshake_secret[EVP_MAX_MD_SIZE] = {0};
  1182. uint8_t server_handshake_secret[EVP_MAX_MD_SIZE] = {0};
  1183. uint8_t client_traffic_secret_0[EVP_MAX_MD_SIZE] = {0};
  1184. uint8_t server_traffic_secret_0[EVP_MAX_MD_SIZE] = {0};
  1185. uint8_t expected_client_finished[EVP_MAX_MD_SIZE] = {0};
  1186. union {
  1187. // sent is a bitset where the bits correspond to elements of kExtensions
  1188. // in t1_lib.c. Each bit is set if that extension was sent in a
  1189. // ClientHello. It's not used by servers.
  1190. uint32_t sent = 0;
  1191. // received is a bitset, like |sent|, but is used by servers to record
  1192. // which extensions were received from a client.
  1193. uint32_t received;
  1194. } extensions;
  1195. union {
  1196. // sent is a bitset where the bits correspond to elements of
  1197. // |client_custom_extensions| in the |SSL_CTX|. Each bit is set if that
  1198. // extension was sent in a ClientHello. It's not used by servers.
  1199. uint16_t sent = 0;
  1200. // received is a bitset, like |sent|, but is used by servers to record
  1201. // which custom extensions were received from a client. The bits here
  1202. // correspond to |server_custom_extensions|.
  1203. uint16_t received;
  1204. } custom_extensions;
  1205. // retry_group is the group ID selected by the server in HelloRetryRequest in
  1206. // TLS 1.3.
  1207. uint16_t retry_group = 0;
  1208. // error, if |wait| is |ssl_hs_error|, is the error the handshake failed on.
  1209. UniquePtr<ERR_SAVE_STATE> error;
  1210. // key_share is the current key exchange instance.
  1211. UniquePtr<SSLKeyShare> key_share;
  1212. // transcript is the current handshake transcript.
  1213. SSLTranscript transcript;
  1214. // cookie is the value of the cookie received from the server, if any.
  1215. Array<uint8_t> cookie;
  1216. // key_share_bytes is the value of the previously sent KeyShare extension by
  1217. // the client in TLS 1.3.
  1218. Array<uint8_t> key_share_bytes;
  1219. // ecdh_public_key, for servers, is the key share to be sent to the client in
  1220. // TLS 1.3.
  1221. Array<uint8_t> ecdh_public_key;
  1222. // peer_sigalgs are the signature algorithms that the peer supports. These are
  1223. // taken from the contents of the signature algorithms extension for a server
  1224. // or from the CertificateRequest for a client.
  1225. Array<uint16_t> peer_sigalgs;
  1226. // peer_supported_group_list contains the supported group IDs advertised by
  1227. // the peer. This is only set on the server's end. The server does not
  1228. // advertise this extension to the client.
  1229. Array<uint16_t> peer_supported_group_list;
  1230. // peer_key is the peer's ECDH key for a TLS 1.2 client.
  1231. Array<uint8_t> peer_key;
  1232. // negotiated_token_binding_version is used by a server to store the
  1233. // on-the-wire encoding of the Token Binding protocol version to advertise in
  1234. // the ServerHello/EncryptedExtensions if the Token Binding extension is to be
  1235. // sent.
  1236. uint16_t negotiated_token_binding_version;
  1237. // server_params, in a TLS 1.2 server, stores the ServerKeyExchange
  1238. // parameters. It has client and server randoms prepended for signing
  1239. // convenience.
  1240. Array<uint8_t> server_params;
  1241. // peer_psk_identity_hint, on the client, is the psk_identity_hint sent by the
  1242. // server when using a TLS 1.2 PSK key exchange.
  1243. UniquePtr<char> peer_psk_identity_hint;
  1244. // ca_names, on the client, contains the list of CAs received in a
  1245. // CertificateRequest message.
  1246. UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names;
  1247. // cached_x509_ca_names contains a cache of parsed versions of the elements of
  1248. // |ca_names|. This pointer is left non-owning so only
  1249. // |ssl_crypto_x509_method| needs to link against crypto/x509.
  1250. STACK_OF(X509_NAME) *cached_x509_ca_names = nullptr;
  1251. // certificate_types, on the client, contains the set of certificate types
  1252. // received in a CertificateRequest message.
  1253. Array<uint8_t> certificate_types;
  1254. // local_pubkey is the public key we are authenticating as.
  1255. UniquePtr<EVP_PKEY> local_pubkey;
  1256. // peer_pubkey is the public key parsed from the peer's leaf certificate.
  1257. UniquePtr<EVP_PKEY> peer_pubkey;
  1258. // new_session is the new mutable session being established by the current
  1259. // handshake. It should not be cached.
  1260. UniquePtr<SSL_SESSION> new_session;
  1261. // early_session is the session corresponding to the current 0-RTT state on
  1262. // the client if |in_early_data| is true.
  1263. UniquePtr<SSL_SESSION> early_session;
  1264. // new_cipher is the cipher being negotiated in this handshake.
  1265. const SSL_CIPHER *new_cipher = nullptr;
  1266. // key_block is the record-layer key block for TLS 1.2 and earlier.
  1267. Array<uint8_t> key_block;
  1268. // scts_requested is true if the SCT extension is in the ClientHello.
  1269. bool scts_requested:1;
  1270. // needs_psk_binder is true if the ClientHello has a placeholder PSK binder to
  1271. // be filled in.
  1272. bool needs_psk_binder:1;
  1273. bool received_hello_retry_request:1;
  1274. bool sent_hello_retry_request:1;
  1275. bool received_custom_extension:1;
  1276. // handshake_finalized is true once the handshake has completed, at which
  1277. // point accessors should use the established state.
  1278. bool handshake_finalized:1;
  1279. // accept_psk_mode stores whether the client's PSK mode is compatible with our
  1280. // preferences.
  1281. bool accept_psk_mode:1;
  1282. // cert_request is true if a client certificate was requested.
  1283. bool cert_request:1;
  1284. // certificate_status_expected is true if OCSP stapling was negotiated and the
  1285. // server is expected to send a CertificateStatus message. (This is used on
  1286. // both the client and server sides.)
  1287. bool certificate_status_expected:1;
  1288. // ocsp_stapling_requested is true if a client requested OCSP stapling.
  1289. bool ocsp_stapling_requested:1;
  1290. // should_ack_sni is used by a server and indicates that the SNI extension
  1291. // should be echoed in the ServerHello.
  1292. bool should_ack_sni:1;
  1293. // in_false_start is true if there is a pending client handshake in False
  1294. // Start. The client may write data at this point.
  1295. bool in_false_start:1;
  1296. // in_early_data is true if there is a pending handshake that has progressed
  1297. // enough to send and receive early data.
  1298. bool in_early_data:1;
  1299. // early_data_offered is true if the client sent the early_data extension.
  1300. bool early_data_offered:1;
  1301. // can_early_read is true if application data may be read at this point in the
  1302. // handshake.
  1303. bool can_early_read:1;
  1304. // can_early_write is true if application data may be written at this point in
  1305. // the handshake.
  1306. bool can_early_write:1;
  1307. // next_proto_neg_seen is one of NPN was negotiated.
  1308. bool next_proto_neg_seen:1;
  1309. // ticket_expected is true if a TLS 1.2 NewSessionTicket message is to be sent
  1310. // or received.
  1311. bool ticket_expected:1;
  1312. // extended_master_secret is true if the extended master secret extension is
  1313. // negotiated in this handshake.
  1314. bool extended_master_secret:1;
  1315. // pending_private_key_op is true if there is a pending private key operation
  1316. // in progress.
  1317. bool pending_private_key_op:1;
  1318. // grease_seeded is true if |grease_seed| has been initialized.
  1319. bool grease_seeded:1;
  1320. // client_version is the value sent or received in the ClientHello version.
  1321. uint16_t client_version = 0;
  1322. // early_data_read is the amount of early data that has been read by the
  1323. // record layer.
  1324. uint16_t early_data_read = 0;
  1325. // early_data_written is the amount of early data that has been written by the
  1326. // record layer.
  1327. uint16_t early_data_written = 0;
  1328. // session_id is the session ID in the ClientHello, used for the experimental
  1329. // TLS 1.3 variant.
  1330. uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH] = {0};
  1331. uint8_t session_id_len = 0;
  1332. // grease_seed is the entropy for GREASE values. It is valid if
  1333. // |grease_seeded| is true.
  1334. uint8_t grease_seed[ssl_grease_last_index + 1] = {0};
  1335. // dummy_pq_padding_len, in a server, is the length of the extension that
  1336. // should be echoed in a ServerHello, or zero if no extension should be
  1337. // echoed.
  1338. uint16_t dummy_pq_padding_len = 0;
  1339. };
  1340. UniquePtr<SSL_HANDSHAKE> ssl_handshake_new(SSL *ssl);
  1341. // ssl_check_message_type checks if |msg| has type |type|. If so it returns
  1342. // one. Otherwise, it sends an alert and returns zero.
  1343. bool ssl_check_message_type(SSL *ssl, const SSLMessage &msg, int type);
  1344. // ssl_run_handshake runs the TLS handshake. It returns one on success and <= 0
  1345. // on error. It sets |out_early_return| to one if we've completed the handshake
  1346. // early.
  1347. int ssl_run_handshake(SSL_HANDSHAKE *hs, bool *out_early_return);
  1348. // The following are implementations of |do_handshake| for the client and
  1349. // server.
  1350. enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs);
  1351. enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs);
  1352. enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs);
  1353. enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs);
  1354. // The following functions return human-readable representations of the TLS
  1355. // handshake states for debugging.
  1356. const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs);
  1357. const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs);
  1358. const char *tls13_client_handshake_state(SSL_HANDSHAKE *hs);
  1359. const char *tls13_server_handshake_state(SSL_HANDSHAKE *hs);
  1360. // tls13_post_handshake processes a post-handshake message. It returns one on
  1361. // success and zero on failure.
  1362. int tls13_post_handshake(SSL *ssl, const SSLMessage &msg);
  1363. int tls13_process_certificate(SSL_HANDSHAKE *hs, const SSLMessage &msg,
  1364. int allow_anonymous);
  1365. int tls13_process_certificate_verify(SSL_HANDSHAKE *hs, const SSLMessage &msg);
  1366. // tls13_process_finished processes |msg| as a Finished message from the
  1367. // peer. If |use_saved_value| is one, the verify_data is compared against
  1368. // |hs->expected_client_finished| rather than computed fresh.
  1369. int tls13_process_finished(SSL_HANDSHAKE *hs, const SSLMessage &msg,
  1370. int use_saved_value);
  1371. int tls13_add_certificate(SSL_HANDSHAKE *hs);
  1372. // tls13_add_certificate_verify adds a TLS 1.3 CertificateVerify message to the
  1373. // handshake. If it returns |ssl_private_key_retry|, it should be called again
  1374. // to retry when the signing operation is completed.
  1375. enum ssl_private_key_result_t tls13_add_certificate_verify(SSL_HANDSHAKE *hs);
  1376. int tls13_add_finished(SSL_HANDSHAKE *hs);
  1377. int tls13_process_new_session_ticket(SSL *ssl, const SSLMessage &msg);
  1378. bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
  1379. Array<uint8_t> *out_secret,
  1380. uint8_t *out_alert, CBS *contents);
  1381. bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
  1382. Array<uint8_t> *out_secret,
  1383. uint8_t *out_alert, CBS *contents);
  1384. bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out);
  1385. bool ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
  1386. uint8_t *out_alert,
  1387. CBS *contents);
  1388. bool ssl_ext_pre_shared_key_parse_clienthello(
  1389. SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
  1390. uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents);
  1391. bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out);
  1392. // ssl_is_sct_list_valid does a shallow parse of the SCT list in |contents| and
  1393. // returns one iff it's valid.
  1394. int ssl_is_sct_list_valid(const CBS *contents);
  1395. int ssl_write_client_hello(SSL_HANDSHAKE *hs);
  1396. enum ssl_cert_verify_context_t {
  1397. ssl_cert_verify_server,
  1398. ssl_cert_verify_client,
  1399. ssl_cert_verify_channel_id,
  1400. };
  1401. // tls13_get_cert_verify_signature_input generates the message to be signed for
  1402. // TLS 1.3's CertificateVerify message. |cert_verify_context| determines the
  1403. // type of signature. It sets |*out| to a newly allocated buffer containing the
  1404. // result. This function returns true on success and false on failure.
  1405. bool tls13_get_cert_verify_signature_input(
  1406. SSL_HANDSHAKE *hs, Array<uint8_t> *out,
  1407. enum ssl_cert_verify_context_t cert_verify_context);
  1408. // ssl_is_alpn_protocol_allowed returns whether |protocol| is a valid server
  1409. // selection for |ssl|'s client preferences.
  1410. bool ssl_is_alpn_protocol_allowed(const SSL *ssl, Span<const uint8_t> protocol);
  1411. // ssl_negotiate_alpn negotiates the ALPN extension, if applicable. It returns
  1412. // true on successful negotiation or if nothing was negotiated. It returns false
  1413. // and sets |*out_alert| to an alert on error.
  1414. bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1415. const SSL_CLIENT_HELLO *client_hello);
  1416. struct SSL_EXTENSION_TYPE {
  1417. uint16_t type;
  1418. bool *out_present;
  1419. CBS *out_data;
  1420. };
  1421. // ssl_parse_extensions parses a TLS extensions block out of |cbs| and advances
  1422. // it. It writes the parsed extensions to pointers denoted by |ext_types|. On
  1423. // success, it fills in the |out_present| and |out_data| fields and returns one.
  1424. // Otherwise, it sets |*out_alert| to an alert to send and returns zero. Unknown
  1425. // extensions are rejected unless |ignore_unknown| is 1.
  1426. int ssl_parse_extensions(const CBS *cbs, uint8_t *out_alert,
  1427. const SSL_EXTENSION_TYPE *ext_types,
  1428. size_t num_ext_types, int ignore_unknown);
  1429. // ssl_verify_peer_cert verifies the peer certificate for |hs|.
  1430. enum ssl_verify_result_t ssl_verify_peer_cert(SSL_HANDSHAKE *hs);
  1431. enum ssl_hs_wait_t ssl_get_finished(SSL_HANDSHAKE *hs);
  1432. bool ssl_send_finished(SSL_HANDSHAKE *hs);
  1433. bool ssl_output_cert_chain(SSL *ssl);
  1434. // SSLKEYLOGFILE functions.
  1435. // ssl_log_secret logs |secret| with label |label|, if logging is enabled for
  1436. // |ssl|. It returns one on success and zero on failure.
  1437. int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret,
  1438. size_t secret_len);
  1439. // ClientHello functions.
  1440. int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out,
  1441. const SSLMessage &msg);
  1442. int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
  1443. CBS *out, uint16_t extension_type);
  1444. int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
  1445. uint16_t id);
  1446. // GREASE.
  1447. // ssl_get_grease_value returns a GREASE value for |hs|. For a given
  1448. // connection, the values for each index will be deterministic. This allows the
  1449. // same ClientHello be sent twice for a HelloRetryRequest or the same group be
  1450. // advertised in both supported_groups and key_shares.
  1451. uint16_t ssl_get_grease_value(SSL_HANDSHAKE *hs, enum ssl_grease_index_t index);
  1452. // Signature algorithms.
  1453. // tls1_parse_peer_sigalgs parses |sigalgs| as the list of peer signature
  1454. // algorithms and saves them on |hs|. It returns true on success and false on
  1455. // error.
  1456. bool tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *sigalgs);
  1457. // tls1_get_legacy_signature_algorithm sets |*out| to the signature algorithm
  1458. // that should be used with |pkey| in TLS 1.1 and earlier. It returns true on
  1459. // success and false if |pkey| may not be used at those versions.
  1460. bool tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey);
  1461. // tls1_choose_signature_algorithm sets |*out| to a signature algorithm for use
  1462. // with |hs|'s private key based on the peer's preferences and the algorithms
  1463. // supported. It returns true on success and false on error.
  1464. bool tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out);
  1465. // tls12_add_verify_sigalgs adds the signature algorithms acceptable for the
  1466. // peer signature to |out|. It returns true on success and false on error. If
  1467. // |for_certs| is true, the potentially more restrictive list of algorithms for
  1468. // certificates is used. Otherwise, the online signature one is used.
  1469. bool tls12_add_verify_sigalgs(const SSL *ssl, CBB *out, bool for_certs);
  1470. // tls12_check_peer_sigalg checks if |sigalg| is acceptable for the peer
  1471. // signature. It returns true on success and false on error, setting
  1472. // |*out_alert| to an alert to send.
  1473. bool tls12_check_peer_sigalg(const SSL *ssl, uint8_t *out_alert,
  1474. uint16_t sigalg);
  1475. // tls12_has_different_verify_sigalgs_for_certs returns whether |ssl| has a
  1476. // different, more restrictive, list of signature algorithms acceptable for the
  1477. // certificate than the online signature.
  1478. bool tls12_has_different_verify_sigalgs_for_certs(const SSL *ssl);
  1479. // Underdocumented functions.
  1480. //
  1481. // Functions below here haven't been touched up and may be underdocumented.
  1482. #define TLSEXT_CHANNEL_ID_SIZE 128
  1483. // From RFC4492, used in encoding the curve type in ECParameters
  1484. #define NAMED_CURVE_TYPE 3
  1485. struct CERT {
  1486. static constexpr bool kAllowUniquePtr = true;
  1487. explicit CERT(const SSL_X509_METHOD *x509_method);
  1488. ~CERT();
  1489. UniquePtr<EVP_PKEY> privatekey;
  1490. // chain contains the certificate chain, with the leaf at the beginning. The
  1491. // first element of |chain| may be NULL to indicate that the leaf certificate
  1492. // has not yet been set.
  1493. // If |chain| != NULL -> len(chain) >= 1
  1494. // If |chain[0]| == NULL -> len(chain) >= 2.
  1495. // |chain[1..]| != NULL
  1496. UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
  1497. // x509_chain may contain a parsed copy of |chain[1..]|. This is only used as
  1498. // a cache in order to implement “get0” functions that return a non-owning
  1499. // pointer to the certificate chain.
  1500. STACK_OF(X509) *x509_chain = nullptr;
  1501. // x509_leaf may contain a parsed copy of the first element of |chain|. This
  1502. // is only used as a cache in order to implement “get0” functions that return
  1503. // a non-owning pointer to the certificate chain.
  1504. X509 *x509_leaf = nullptr;
  1505. // x509_stash contains the last |X509| object append to the chain. This is a
  1506. // workaround for some third-party code that continue to use an |X509| object
  1507. // even after passing ownership with an “add0” function.
  1508. X509 *x509_stash = nullptr;
  1509. // key_method, if non-NULL, is a set of callbacks to call for private key
  1510. // operations.
  1511. const SSL_PRIVATE_KEY_METHOD *key_method = nullptr;
  1512. // x509_method contains pointers to functions that might deal with |X509|
  1513. // compatibility, or might be a no-op, depending on the application.
  1514. const SSL_X509_METHOD *x509_method = nullptr;
  1515. // sigalgs, if non-empty, is the set of signature algorithms supported by
  1516. // |privatekey| in decreasing order of preference.
  1517. Array<uint16_t> sigalgs;
  1518. // Certificate setup callback: if set is called whenever a
  1519. // certificate may be required (client or server). the callback
  1520. // can then examine any appropriate parameters and setup any
  1521. // certificates required. This allows advanced applications
  1522. // to select certificates on the fly: for example based on
  1523. // supported signature algorithms or curves.
  1524. int (*cert_cb)(SSL *ssl, void *arg) = nullptr;
  1525. void *cert_cb_arg = nullptr;
  1526. // Optional X509_STORE for certificate validation. If NULL the parent SSL_CTX
  1527. // store is used instead.
  1528. X509_STORE *verify_store = nullptr;
  1529. // Signed certificate timestamp list to be sent to the client, if requested
  1530. UniquePtr<CRYPTO_BUFFER> signed_cert_timestamp_list;
  1531. // OCSP response to be sent to the client, if requested.
  1532. UniquePtr<CRYPTO_BUFFER> ocsp_response;
  1533. // sid_ctx partitions the session space within a shared session cache or
  1534. // ticket key. Only sessions with a matching value will be accepted.
  1535. uint8_t sid_ctx_length = 0;
  1536. uint8_t sid_ctx[SSL_MAX_SID_CTX_LENGTH] = {0};
  1537. // If enable_early_data is true, early data can be sent and accepted.
  1538. bool enable_early_data:1;
  1539. };
  1540. // |SSL_PROTOCOL_METHOD| abstracts between TLS and DTLS.
  1541. struct SSL_PROTOCOL_METHOD {
  1542. bool is_dtls;
  1543. bool (*ssl_new)(SSL *ssl);
  1544. void (*ssl_free)(SSL *ssl);
  1545. // get_message sets |*out| to the current handshake message and returns true
  1546. // if one has been received. It returns false if more input is needed.
  1547. bool (*get_message)(SSL *ssl, SSLMessage *out);
  1548. // next_message is called to release the current handshake message.
  1549. void (*next_message)(SSL *ssl);
  1550. // Use the |ssl_open_handshake| wrapper.
  1551. ssl_open_record_t (*open_handshake)(SSL *ssl, size_t *out_consumed,
  1552. uint8_t *out_alert, Span<uint8_t> in);
  1553. // Use the |ssl_open_change_cipher_spec| wrapper.
  1554. ssl_open_record_t (*open_change_cipher_spec)(SSL *ssl, size_t *out_consumed,
  1555. uint8_t *out_alert,
  1556. Span<uint8_t> in);
  1557. // Use the |ssl_open_app_data| wrapper.
  1558. ssl_open_record_t (*open_app_data)(SSL *ssl, Span<uint8_t> *out,
  1559. size_t *out_consumed, uint8_t *out_alert,
  1560. Span<uint8_t> in);
  1561. int (*write_app_data)(SSL *ssl, bool *out_needs_handshake, const uint8_t *buf,
  1562. int len);
  1563. int (*dispatch_alert)(SSL *ssl);
  1564. // init_message begins a new handshake message of type |type|. |cbb| is the
  1565. // root CBB to be passed into |finish_message|. |*body| is set to a child CBB
  1566. // the caller should write to. It returns true on success and false on error.
  1567. bool (*init_message)(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
  1568. // finish_message finishes a handshake message. It sets |*out_msg| to the
  1569. // serialized message. It returns true on success and false on error.
  1570. bool (*finish_message)(SSL *ssl, CBB *cbb, bssl::Array<uint8_t> *out_msg);
  1571. // add_message adds a handshake message to the pending flight. It returns
  1572. // true on success and false on error.
  1573. bool (*add_message)(SSL *ssl, bssl::Array<uint8_t> msg);
  1574. // add_change_cipher_spec adds a ChangeCipherSpec record to the pending
  1575. // flight. It returns true on success and false on error.
  1576. bool (*add_change_cipher_spec)(SSL *ssl);
  1577. // add_alert adds an alert to the pending flight. It returns true on success
  1578. // and false on error.
  1579. bool (*add_alert)(SSL *ssl, uint8_t level, uint8_t desc);
  1580. // flush_flight flushes the pending flight to the transport. It returns one on
  1581. // success and <= 0 on error.
  1582. int (*flush_flight)(SSL *ssl);
  1583. // on_handshake_complete is called when the handshake is complete.
  1584. void (*on_handshake_complete)(SSL *ssl);
  1585. // set_read_state sets |ssl|'s read cipher state to |aead_ctx|. It returns
  1586. // true on success and false if changing the read state is forbidden at this
  1587. // point.
  1588. bool (*set_read_state)(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx);
  1589. // set_write_state sets |ssl|'s write cipher state to |aead_ctx|. It returns
  1590. // true on success and false if changing the write state is forbidden at this
  1591. // point.
  1592. bool (*set_write_state)(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx);
  1593. };
  1594. // The following wrappers call |open_*| but handle |read_shutdown| correctly.
  1595. // ssl_open_handshake processes a record from |in| for reading a handshake
  1596. // message.
  1597. ssl_open_record_t ssl_open_handshake(SSL *ssl, size_t *out_consumed,
  1598. uint8_t *out_alert, Span<uint8_t> in);
  1599. // ssl_open_change_cipher_spec processes a record from |in| for reading a
  1600. // ChangeCipherSpec.
  1601. ssl_open_record_t ssl_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
  1602. uint8_t *out_alert,
  1603. Span<uint8_t> in);
  1604. // ssl_open_app_data processes a record from |in| for reading application data.
  1605. // On success, it returns |ssl_open_record_success| and sets |*out| to the
  1606. // input. If it encounters a post-handshake message, it returns
  1607. // |ssl_open_record_discard|. The caller should then retry, after processing any
  1608. // messages received with |get_message|.
  1609. ssl_open_record_t ssl_open_app_data(SSL *ssl, Span<uint8_t> *out,
  1610. size_t *out_consumed, uint8_t *out_alert,
  1611. Span<uint8_t> in);
  1612. // ssl_crypto_x509_method provides the |SSL_X509_METHOD| functions using
  1613. // crypto/x509.
  1614. extern const SSL_X509_METHOD ssl_crypto_x509_method;
  1615. // ssl_noop_x509_method provides the |SSL_X509_METHOD| functions that avoid
  1616. // crypto/x509.
  1617. extern const SSL_X509_METHOD ssl_noop_x509_method;
  1618. struct tlsext_ticket_key {
  1619. static constexpr bool kAllowUniquePtr = true;
  1620. uint8_t name[SSL_TICKET_KEY_NAME_LEN];
  1621. uint8_t hmac_key[16];
  1622. uint8_t aes_key[16];
  1623. // next_rotation_tv_sec is the time (in seconds from the epoch) when the
  1624. // current key should be superseded by a new key, or the time when a previous
  1625. // key should be dropped. If zero, then the key should not be automatically
  1626. // rotated.
  1627. uint64_t next_rotation_tv_sec;
  1628. };
  1629. } // namespace bssl
  1630. DECLARE_LHASH_OF(SSL_SESSION)
  1631. namespace bssl {
  1632. // SSLContext backs the public |SSL_CTX| type. Due to compatibility constraints,
  1633. // it is a base class for |ssl_ctx_st|.
  1634. struct SSLContext {
  1635. const SSL_PROTOCOL_METHOD *method;
  1636. const SSL_X509_METHOD *x509_method;
  1637. // lock is used to protect various operations on this object.
  1638. CRYPTO_MUTEX lock;
  1639. // conf_max_version is the maximum acceptable protocol version configured by
  1640. // |SSL_CTX_set_max_proto_version|. Note this version is normalized in DTLS
  1641. // and is further constrainted by |SSL_OP_NO_*|.
  1642. uint16_t conf_max_version;
  1643. // conf_min_version is the minimum acceptable protocol version configured by
  1644. // |SSL_CTX_set_min_proto_version|. Note this version is normalized in DTLS
  1645. // and is further constrainted by |SSL_OP_NO_*|.
  1646. uint16_t conf_min_version;
  1647. // tls13_variant is the variant of TLS 1.3 we are using for this
  1648. // configuration.
  1649. enum tls13_variant_t tls13_variant;
  1650. SSLCipherPreferenceList *cipher_list;
  1651. X509_STORE *cert_store;
  1652. LHASH_OF(SSL_SESSION) *sessions;
  1653. // Most session-ids that will be cached, default is
  1654. // SSL_SESSION_CACHE_MAX_SIZE_DEFAULT. 0 is unlimited.
  1655. unsigned long session_cache_size;
  1656. SSL_SESSION *session_cache_head;
  1657. SSL_SESSION *session_cache_tail;
  1658. // handshakes_since_cache_flush is the number of successful handshakes since
  1659. // the last cache flush.
  1660. int handshakes_since_cache_flush;
  1661. // This can have one of 2 values, ored together,
  1662. // SSL_SESS_CACHE_CLIENT,
  1663. // SSL_SESS_CACHE_SERVER,
  1664. // Default is SSL_SESSION_CACHE_SERVER, which means only
  1665. // SSL_accept which cache SSL_SESSIONS.
  1666. int session_cache_mode;
  1667. // session_timeout is the default lifetime for new sessions in TLS 1.2 and
  1668. // earlier, in seconds.
  1669. uint32_t session_timeout;
  1670. // session_psk_dhe_timeout is the default lifetime for new sessions in TLS
  1671. // 1.3, in seconds.
  1672. uint32_t session_psk_dhe_timeout;
  1673. // If this callback is not null, it will be called each time a session id is
  1674. // added to the cache. If this function returns 1, it means that the
  1675. // callback will do a SSL_SESSION_free() when it has finished using it.
  1676. // Otherwise, on 0, it means the callback has finished with it. If
  1677. // remove_session_cb is not null, it will be called when a session-id is
  1678. // removed from the cache. After the call, OpenSSL will SSL_SESSION_free()
  1679. // it.
  1680. int (*new_session_cb)(SSL *ssl, SSL_SESSION *sess);
  1681. void (*remove_session_cb)(SSL_CTX *ctx, SSL_SESSION *sess);
  1682. SSL_SESSION *(*get_session_cb)(SSL *ssl, const uint8_t *data, int len,
  1683. int *copy);
  1684. CRYPTO_refcount_t references;
  1685. // if defined, these override the X509_verify_cert() calls
  1686. int (*app_verify_callback)(X509_STORE_CTX *store_ctx, void *arg);
  1687. void *app_verify_arg;
  1688. enum ssl_verify_result_t (*custom_verify_callback)(SSL *ssl,
  1689. uint8_t *out_alert);
  1690. // Default password callback.
  1691. pem_password_cb *default_passwd_callback;
  1692. // Default password callback user data.
  1693. void *default_passwd_callback_userdata;
  1694. // get client cert callback
  1695. int (*client_cert_cb)(SSL *ssl, X509 **out_x509, EVP_PKEY **out_pkey);
  1696. // get channel id callback
  1697. void (*channel_id_cb)(SSL *ssl, EVP_PKEY **out_pkey);
  1698. CRYPTO_EX_DATA ex_data;
  1699. // custom_*_extensions stores any callback sets for custom extensions. Note
  1700. // that these pointers will be NULL if the stack would otherwise be empty.
  1701. STACK_OF(SSL_CUSTOM_EXTENSION) *client_custom_extensions;
  1702. STACK_OF(SSL_CUSTOM_EXTENSION) *server_custom_extensions;
  1703. // Default values used when no per-SSL value is defined follow
  1704. void (*info_callback)(const SSL *ssl, int type, int value);
  1705. // what we put in client cert requests
  1706. STACK_OF(CRYPTO_BUFFER) *client_CA;
  1707. // cached_x509_client_CA is a cache of parsed versions of the elements of
  1708. // |client_CA|.
  1709. STACK_OF(X509_NAME) *cached_x509_client_CA;
  1710. // Default values to use in SSL structures follow (these are copied by
  1711. // SSL_new)
  1712. uint32_t options;
  1713. uint32_t mode;
  1714. uint32_t max_cert_list;
  1715. CERT *cert;
  1716. // callback that allows applications to peek at protocol messages
  1717. void (*msg_callback)(int write_p, int version, int content_type,
  1718. const void *buf, size_t len, SSL *ssl, void *arg);
  1719. void *msg_callback_arg;
  1720. int verify_mode;
  1721. int (*default_verify_callback)(
  1722. int ok, X509_STORE_CTX *ctx); // called 'verify_callback' in the SSL
  1723. X509_VERIFY_PARAM *param;
  1724. // select_certificate_cb is called before most ClientHello processing and
  1725. // before the decision whether to resume a session is made. See
  1726. // |ssl_select_cert_result_t| for details of the return values.
  1727. enum ssl_select_cert_result_t (*select_certificate_cb)(
  1728. const SSL_CLIENT_HELLO *);
  1729. // dos_protection_cb is called once the resumption decision for a ClientHello
  1730. // has been made. It returns one to continue the handshake or zero to
  1731. // abort.
  1732. int (*dos_protection_cb) (const SSL_CLIENT_HELLO *);
  1733. // Maximum amount of data to send in one fragment. actual record size can be
  1734. // more than this due to padding and MAC overheads.
  1735. uint16_t max_send_fragment;
  1736. // TLS extensions servername callback
  1737. int (*tlsext_servername_callback)(SSL *, int *, void *);
  1738. void *tlsext_servername_arg;
  1739. // RFC 4507 session ticket keys. |tlsext_ticket_key_current| may be NULL
  1740. // before the first handshake and |tlsext_ticket_key_prev| may be NULL at any
  1741. // time. Automatically generated ticket keys are rotated as needed at
  1742. // handshake time. Hence, all access must be synchronized through |lock|.
  1743. struct tlsext_ticket_key *tlsext_ticket_key_current;
  1744. struct tlsext_ticket_key *tlsext_ticket_key_prev;
  1745. // Callback to support customisation of ticket key setting
  1746. int (*tlsext_ticket_key_cb)(SSL *ssl, uint8_t *name, uint8_t *iv,
  1747. EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc);
  1748. // Server-only: psk_identity_hint is the default identity hint to send in
  1749. // PSK-based key exchanges.
  1750. char *psk_identity_hint;
  1751. unsigned int (*psk_client_callback)(SSL *ssl, const char *hint,
  1752. char *identity,
  1753. unsigned int max_identity_len,
  1754. uint8_t *psk, unsigned int max_psk_len);
  1755. unsigned int (*psk_server_callback)(SSL *ssl, const char *identity,
  1756. uint8_t *psk, unsigned int max_psk_len);
  1757. // Next protocol negotiation information
  1758. // (for experimental NPN extension).
  1759. // For a server, this contains a callback function by which the set of
  1760. // advertised protocols can be provided.
  1761. int (*next_protos_advertised_cb)(SSL *ssl, const uint8_t **out,
  1762. unsigned *out_len, void *arg);
  1763. void *next_protos_advertised_cb_arg;
  1764. // For a client, this contains a callback function that selects the
  1765. // next protocol from the list provided by the server.
  1766. int (*next_proto_select_cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
  1767. const uint8_t *in, unsigned in_len, void *arg);
  1768. void *next_proto_select_cb_arg;
  1769. // ALPN information
  1770. // (we are in the process of transitioning from NPN to ALPN.)
  1771. // For a server, this contains a callback function that allows the
  1772. // server to select the protocol for the connection.
  1773. // out: on successful return, this must point to the raw protocol
  1774. // name (without the length prefix).
  1775. // outlen: on successful return, this contains the length of |*out|.
  1776. // in: points to the client's list of supported protocols in
  1777. // wire-format.
  1778. // inlen: the length of |in|.
  1779. int (*alpn_select_cb)(SSL *ssl, const uint8_t **out, uint8_t *out_len,
  1780. const uint8_t *in, unsigned in_len, void *arg);
  1781. void *alpn_select_cb_arg;
  1782. // For a client, this contains the list of supported protocols in wire
  1783. // format.
  1784. uint8_t *alpn_client_proto_list;
  1785. unsigned alpn_client_proto_list_len;
  1786. // SRTP profiles we are willing to do from RFC 5764
  1787. STACK_OF(SRTP_PROTECTION_PROFILE) *srtp_profiles;
  1788. // Supported group values inherited by SSL structure
  1789. size_t supported_group_list_len;
  1790. uint16_t *supported_group_list;
  1791. // The client's Channel ID private key.
  1792. EVP_PKEY *tlsext_channel_id_private;
  1793. // keylog_callback, if not NULL, is the key logging callback. See
  1794. // |SSL_CTX_set_keylog_callback|.
  1795. void (*keylog_callback)(const SSL *ssl, const char *line);
  1796. // current_time_cb, if not NULL, is the function to use to get the current
  1797. // time. It sets |*out_clock| to the current time. The |ssl| argument is
  1798. // always NULL. See |SSL_CTX_set_current_time_cb|.
  1799. void (*current_time_cb)(const SSL *ssl, struct timeval *out_clock);
  1800. // pool is used for all |CRYPTO_BUFFER|s in case we wish to share certificate
  1801. // memory.
  1802. CRYPTO_BUFFER_POOL *pool;
  1803. // ticket_aead_method contains function pointers for opening and sealing
  1804. // session tickets.
  1805. const SSL_TICKET_AEAD_METHOD *ticket_aead_method;
  1806. // verify_sigalgs, if not empty, is the set of signature algorithms
  1807. // accepted from the peer in decreasing order of preference.
  1808. uint16_t *verify_sigalgs;
  1809. size_t num_verify_sigalgs;
  1810. // retain_only_sha256_of_client_certs is true if we should compute the SHA256
  1811. // hash of the peer's certificate and then discard it to save memory and
  1812. // session space. Only effective on the server side.
  1813. bool retain_only_sha256_of_client_certs:1;
  1814. // quiet_shutdown is true if the connection should not send a close_notify on
  1815. // shutdown.
  1816. bool quiet_shutdown:1;
  1817. // ocsp_stapling_enabled is only used by client connections and indicates
  1818. // whether OCSP stapling will be requested.
  1819. bool ocsp_stapling_enabled:1;
  1820. // If true, a client will request certificate timestamps.
  1821. bool signed_cert_timestamps_enabled:1;
  1822. // tlsext_channel_id_enabled is whether Channel ID is enabled. For a server,
  1823. // means that we'll accept Channel IDs from clients. For a client, means that
  1824. // we'll advertise support.
  1825. bool tlsext_channel_id_enabled:1;
  1826. // grease_enabled is whether draft-davidben-tls-grease-01 is enabled.
  1827. bool grease_enabled:1;
  1828. // allow_unknown_alpn_protos is whether the client allows unsolicited ALPN
  1829. // protocols from the peer.
  1830. bool allow_unknown_alpn_protos:1;
  1831. // ed25519_enabled is whether Ed25519 is advertised in the handshake.
  1832. bool ed25519_enabled:1;
  1833. // rsa_pss_rsae_certs_enabled is whether rsa_pss_rsae_* are supported by the
  1834. // certificate verifier.
  1835. bool rsa_pss_rsae_certs_enabled:1;
  1836. // false_start_allowed_without_alpn is whether False Start (if
  1837. // |SSL_MODE_ENABLE_FALSE_START| is enabled) is allowed without ALPN.
  1838. bool false_start_allowed_without_alpn:1;
  1839. // handoff indicates that a server should stop after receiving the
  1840. // ClientHello and pause the handshake in such a way that |SSL_get_error|
  1841. // returns |SSL_HANDOFF|.
  1842. bool handoff:1;
  1843. };
  1844. // An ssl_shutdown_t describes the shutdown state of one end of the connection,
  1845. // whether it is alive or has been shutdown via close_notify or fatal alert.
  1846. enum ssl_shutdown_t {
  1847. ssl_shutdown_none = 0,
  1848. ssl_shutdown_close_notify = 1,
  1849. ssl_shutdown_error = 2,
  1850. };
  1851. struct SSL3_STATE {
  1852. static constexpr bool kAllowUniquePtr = true;
  1853. SSL3_STATE();
  1854. ~SSL3_STATE();
  1855. uint8_t read_sequence[8] = {0};
  1856. uint8_t write_sequence[8] = {0};
  1857. uint8_t server_random[SSL3_RANDOM_SIZE] = {0};
  1858. uint8_t client_random[SSL3_RANDOM_SIZE] = {0};
  1859. // read_buffer holds data from the transport to be processed.
  1860. SSLBuffer read_buffer;
  1861. // write_buffer holds data to be written to the transport.
  1862. SSLBuffer write_buffer;
  1863. // pending_app_data is the unconsumed application data. It points into
  1864. // |read_buffer|.
  1865. Span<uint8_t> pending_app_data;
  1866. // partial write - check the numbers match
  1867. unsigned int wnum = 0; // number of bytes sent so far
  1868. int wpend_tot = 0; // number bytes written
  1869. int wpend_type = 0;
  1870. int wpend_ret = 0; // number of bytes submitted
  1871. const uint8_t *wpend_buf = nullptr;
  1872. // read_shutdown is the shutdown state for the read half of the connection.
  1873. enum ssl_shutdown_t read_shutdown = ssl_shutdown_none;
  1874. // write_shutdown is the shutdown state for the write half of the connection.
  1875. enum ssl_shutdown_t write_shutdown = ssl_shutdown_none;
  1876. // read_error, if |read_shutdown| is |ssl_shutdown_error|, is the error for
  1877. // the receive half of the connection.
  1878. UniquePtr<ERR_SAVE_STATE> read_error;
  1879. int alert_dispatch = 0;
  1880. int total_renegotiations = 0;
  1881. // This holds a variable that indicates what we were doing when a 0 or -1 is
  1882. // returned. This is needed for non-blocking IO so we know what request
  1883. // needs re-doing when in SSL_accept or SSL_connect
  1884. int rwstate = SSL_NOTHING;
  1885. // early_data_skipped is the amount of early data that has been skipped by the
  1886. // record layer.
  1887. uint16_t early_data_skipped = 0;
  1888. // empty_record_count is the number of consecutive empty records received.
  1889. uint8_t empty_record_count = 0;
  1890. // warning_alert_count is the number of consecutive warning alerts
  1891. // received.
  1892. uint8_t warning_alert_count = 0;
  1893. // key_update_count is the number of consecutive KeyUpdates received.
  1894. uint8_t key_update_count = 0;
  1895. // The negotiated Token Binding key parameter. Only valid if
  1896. // |token_binding_negotiated| is set.
  1897. uint8_t negotiated_token_binding_param = 0;
  1898. // skip_early_data instructs the record layer to skip unexpected early data
  1899. // messages when 0RTT is rejected.
  1900. bool skip_early_data:1;
  1901. // have_version is true if the connection's final version is known. Otherwise
  1902. // the version has not been negotiated yet.
  1903. bool have_version:1;
  1904. // v2_hello_done is true if the peer's V2ClientHello, if any, has been handled
  1905. // and future messages should use the record layer.
  1906. bool v2_hello_done:1;
  1907. // is_v2_hello is true if the current handshake message was derived from a
  1908. // V2ClientHello rather than received from the peer directly.
  1909. bool is_v2_hello:1;
  1910. // has_message is true if the current handshake message has been returned
  1911. // at least once by |get_message| and false otherwise.
  1912. bool has_message:1;
  1913. // initial_handshake_complete is true if the initial handshake has
  1914. // completed.
  1915. bool initial_handshake_complete:1;
  1916. // session_reused indicates whether a session was resumed.
  1917. bool session_reused:1;
  1918. bool send_connection_binding:1;
  1919. // In a client, this means that the server supported Channel ID and that a
  1920. // Channel ID was sent. In a server it means that we echoed support for
  1921. // Channel IDs and that tlsext_channel_id will be valid after the
  1922. // handshake.
  1923. bool tlsext_channel_id_valid:1;
  1924. // key_update_pending is true if we have a KeyUpdate acknowledgment
  1925. // outstanding.
  1926. bool key_update_pending:1;
  1927. // wpend_pending is true if we have a pending write outstanding.
  1928. bool wpend_pending:1;
  1929. // early_data_accepted is true if early data was accepted by the server.
  1930. bool early_data_accepted:1;
  1931. // draft_downgrade is whether the TLS 1.3 anti-downgrade logic would have
  1932. // fired, were it not a draft.
  1933. bool draft_downgrade:1;
  1934. // token_binding_negotiated is set if Token Binding was negotiated.
  1935. bool token_binding_negotiated:1;
  1936. // hs_buf is the buffer of handshake data to process.
  1937. UniquePtr<BUF_MEM> hs_buf;
  1938. // pending_flight is the pending outgoing flight. This is used to flush each
  1939. // handshake flight in a single write. |write_buffer| must be written out
  1940. // before this data.
  1941. UniquePtr<BUF_MEM> pending_flight;
  1942. // pending_flight_offset is the number of bytes of |pending_flight| which have
  1943. // been successfully written.
  1944. uint32_t pending_flight_offset = 0;
  1945. // ticket_age_skew is the difference, in seconds, between the client-sent
  1946. // ticket age and the server-computed value in TLS 1.3 server connections
  1947. // which resumed a session.
  1948. int32_t ticket_age_skew = 0;
  1949. // aead_read_ctx is the current read cipher state.
  1950. UniquePtr<SSLAEADContext> aead_read_ctx;
  1951. // aead_write_ctx is the current write cipher state.
  1952. UniquePtr<SSLAEADContext> aead_write_ctx;
  1953. // hs is the handshake state for the current handshake or NULL if there isn't
  1954. // one.
  1955. UniquePtr<SSL_HANDSHAKE> hs;
  1956. uint8_t write_traffic_secret[EVP_MAX_MD_SIZE] = {0};
  1957. uint8_t read_traffic_secret[EVP_MAX_MD_SIZE] = {0};
  1958. uint8_t exporter_secret[EVP_MAX_MD_SIZE] = {0};
  1959. uint8_t early_exporter_secret[EVP_MAX_MD_SIZE] = {0};
  1960. uint8_t write_traffic_secret_len = 0;
  1961. uint8_t read_traffic_secret_len = 0;
  1962. uint8_t exporter_secret_len = 0;
  1963. uint8_t early_exporter_secret_len = 0;
  1964. // Connection binding to prevent renegotiation attacks
  1965. uint8_t previous_client_finished[12] = {0};
  1966. uint8_t previous_client_finished_len = 0;
  1967. uint8_t previous_server_finished_len = 0;
  1968. uint8_t previous_server_finished[12] = {0};
  1969. uint8_t send_alert[2] = {0};
  1970. // established_session is the session established by the connection. This
  1971. // session is only filled upon the completion of the handshake and is
  1972. // immutable.
  1973. UniquePtr<SSL_SESSION> established_session;
  1974. // Next protocol negotiation. For the client, this is the protocol that we
  1975. // sent in NextProtocol and is set when handling ServerHello extensions.
  1976. //
  1977. // For a server, this is the client's selected_protocol from NextProtocol and
  1978. // is set when handling the NextProtocol message, before the Finished
  1979. // message.
  1980. Array<uint8_t> next_proto_negotiated;
  1981. // ALPN information
  1982. // (we are in the process of transitioning from NPN to ALPN.)
  1983. // In a server these point to the selected ALPN protocol after the
  1984. // ClientHello has been processed. In a client these contain the protocol
  1985. // that the server selected once the ServerHello has been processed.
  1986. Array<uint8_t> alpn_selected;
  1987. // hostname, on the server, is the value of the SNI extension.
  1988. UniquePtr<char> hostname;
  1989. // For a server:
  1990. // If |tlsext_channel_id_valid| is true, then this contains the
  1991. // verified Channel ID from the client: a P256 point, (x,y), where
  1992. // each are big-endian values.
  1993. uint8_t tlsext_channel_id[64] = {0};
  1994. // Contains the QUIC transport params received by the peer.
  1995. Array<uint8_t> peer_quic_transport_params;
  1996. // srtp_profile is the selected SRTP protection profile for
  1997. // DTLS-SRTP.
  1998. const SRTP_PROTECTION_PROFILE *srtp_profile = nullptr;
  1999. };
  2000. // lengths of messages
  2001. #define DTLS1_COOKIE_LENGTH 256
  2002. #define DTLS1_RT_HEADER_LENGTH 13
  2003. #define DTLS1_HM_HEADER_LENGTH 12
  2004. #define DTLS1_CCS_HEADER_LENGTH 1
  2005. #define DTLS1_AL_HEADER_LENGTH 2
  2006. struct hm_header_st {
  2007. uint8_t type;
  2008. uint32_t msg_len;
  2009. uint16_t seq;
  2010. uint32_t frag_off;
  2011. uint32_t frag_len;
  2012. };
  2013. // An hm_fragment is an incoming DTLS message, possibly not yet assembled.
  2014. struct hm_fragment {
  2015. static constexpr bool kAllowUniquePtr = true;
  2016. hm_fragment() {}
  2017. hm_fragment(const hm_fragment &) = delete;
  2018. hm_fragment &operator=(const hm_fragment &) = delete;
  2019. ~hm_fragment();
  2020. // type is the type of the message.
  2021. uint8_t type = 0;
  2022. // seq is the sequence number of this message.
  2023. uint16_t seq = 0;
  2024. // msg_len is the length of the message body.
  2025. uint32_t msg_len = 0;
  2026. // data is a pointer to the message, including message header. It has length
  2027. // |DTLS1_HM_HEADER_LENGTH| + |msg_len|.
  2028. uint8_t *data = nullptr;
  2029. // reassembly is a bitmask of |msg_len| bits corresponding to which parts of
  2030. // the message have been received. It is NULL if the message is complete.
  2031. uint8_t *reassembly = nullptr;
  2032. };
  2033. struct OPENSSL_timeval {
  2034. uint64_t tv_sec;
  2035. uint32_t tv_usec;
  2036. };
  2037. struct DTLS1_STATE {
  2038. static constexpr bool kAllowUniquePtr = true;
  2039. DTLS1_STATE();
  2040. ~DTLS1_STATE();
  2041. // has_change_cipher_spec is true if we have received a ChangeCipherSpec from
  2042. // the peer in this epoch.
  2043. bool has_change_cipher_spec:1;
  2044. // outgoing_messages_complete is true if |outgoing_messages| has been
  2045. // completed by an attempt to flush it. Future calls to |add_message| and
  2046. // |add_change_cipher_spec| will start a new flight.
  2047. bool outgoing_messages_complete:1;
  2048. // flight_has_reply is true if the current outgoing flight is complete and has
  2049. // processed at least one message. This is used to detect whether we or the
  2050. // peer sent the final flight.
  2051. bool flight_has_reply:1;
  2052. uint8_t cookie[DTLS1_COOKIE_LENGTH] = {0};
  2053. size_t cookie_len = 0;
  2054. // The current data and handshake epoch. This is initially undefined, and
  2055. // starts at zero once the initial handshake is completed.
  2056. uint16_t r_epoch = 0;
  2057. uint16_t w_epoch = 0;
  2058. // records being received in the current epoch
  2059. DTLS1_BITMAP bitmap;
  2060. uint16_t handshake_write_seq = 0;
  2061. uint16_t handshake_read_seq = 0;
  2062. // save last sequence number for retransmissions
  2063. uint8_t last_write_sequence[8] = {0};
  2064. UniquePtr<SSLAEADContext> last_aead_write_ctx;
  2065. // incoming_messages is a ring buffer of incoming handshake messages that have
  2066. // yet to be processed. The front of the ring buffer is message number
  2067. // |handshake_read_seq|, at position |handshake_read_seq| %
  2068. // |SSL_MAX_HANDSHAKE_FLIGHT|.
  2069. UniquePtr<hm_fragment> incoming_messages[SSL_MAX_HANDSHAKE_FLIGHT];
  2070. // outgoing_messages is the queue of outgoing messages from the last handshake
  2071. // flight.
  2072. DTLS_OUTGOING_MESSAGE outgoing_messages[SSL_MAX_HANDSHAKE_FLIGHT];
  2073. uint8_t outgoing_messages_len = 0;
  2074. // outgoing_written is the number of outgoing messages that have been
  2075. // written.
  2076. uint8_t outgoing_written = 0;
  2077. // outgoing_offset is the number of bytes of the next outgoing message have
  2078. // been written.
  2079. uint32_t outgoing_offset = 0;
  2080. unsigned mtu = 0; // max DTLS packet size
  2081. // num_timeouts is the number of times the retransmit timer has fired since
  2082. // the last time it was reset.
  2083. unsigned num_timeouts = 0;
  2084. // Indicates when the last handshake msg or heartbeat sent will
  2085. // timeout.
  2086. struct OPENSSL_timeval next_timeout = {0, 0};
  2087. // timeout_duration_ms is the timeout duration in milliseconds.
  2088. unsigned timeout_duration_ms = 0;
  2089. };
  2090. // SSLConnection backs the public |SSL| type. Due to compatibility constraints,
  2091. // it is a base class for |ssl_st|.
  2092. struct SSLConnection {
  2093. // method is the method table corresponding to the current protocol (DTLS or
  2094. // TLS).
  2095. const SSL_PROTOCOL_METHOD *method;
  2096. // version is the protocol version.
  2097. uint16_t version;
  2098. // conf_max_version is the maximum acceptable protocol version configured by
  2099. // |SSL_set_max_proto_version|. Note this version is normalized in DTLS and is
  2100. // further constrainted by |SSL_OP_NO_*|.
  2101. uint16_t conf_max_version;
  2102. // conf_min_version is the minimum acceptable protocol version configured by
  2103. // |SSL_set_min_proto_version|. Note this version is normalized in DTLS and is
  2104. // further constrainted by |SSL_OP_NO_*|.
  2105. uint16_t conf_min_version;
  2106. uint16_t max_send_fragment;
  2107. // There are 2 BIO's even though they are normally both the same. This is so
  2108. // data can be read and written to different handlers
  2109. BIO *rbio; // used by SSL_read
  2110. BIO *wbio; // used by SSL_write
  2111. // do_handshake runs the handshake. On completion, it returns |ssl_hs_ok|.
  2112. // Otherwise, it returns a value corresponding to what operation is needed to
  2113. // progress.
  2114. enum ssl_hs_wait_t (*do_handshake)(SSL_HANDSHAKE *hs);
  2115. SSL3_STATE *s3; // SSLv3 variables
  2116. DTLS1_STATE *d1; // DTLSv1 variables
  2117. // callback that allows applications to peek at protocol messages
  2118. void (*msg_callback)(int write_p, int version, int content_type,
  2119. const void *buf, size_t len, SSL *ssl, void *arg);
  2120. void *msg_callback_arg;
  2121. X509_VERIFY_PARAM *param;
  2122. // crypto
  2123. SSLCipherPreferenceList *cipher_list;
  2124. // session info
  2125. // This is used to hold the local certificate used (i.e. the server
  2126. // certificate for a server or the client certificate for a client).
  2127. CERT *cert;
  2128. // initial_timeout_duration_ms is the default DTLS timeout duration in
  2129. // milliseconds. It's used to initialize the timer any time it's restarted.
  2130. unsigned initial_timeout_duration_ms;
  2131. // tls13_variant is the variant of TLS 1.3 we are using for this
  2132. // configuration.
  2133. enum tls13_variant_t tls13_variant;
  2134. // session is the configured session to be offered by the client. This session
  2135. // is immutable.
  2136. SSL_SESSION *session;
  2137. int (*verify_callback)(int ok,
  2138. X509_STORE_CTX *ctx); // fail if callback returns 0
  2139. enum ssl_verify_result_t (*custom_verify_callback)(SSL *ssl,
  2140. uint8_t *out_alert);
  2141. void (*info_callback)(const SSL *ssl, int type, int value);
  2142. // Server-only: psk_identity_hint is the identity hint to send in
  2143. // PSK-based key exchanges.
  2144. char *psk_identity_hint;
  2145. unsigned int (*psk_client_callback)(SSL *ssl, const char *hint,
  2146. char *identity,
  2147. unsigned int max_identity_len,
  2148. uint8_t *psk, unsigned int max_psk_len);
  2149. unsigned int (*psk_server_callback)(SSL *ssl, const char *identity,
  2150. uint8_t *psk, unsigned int max_psk_len);
  2151. SSL_CTX *ctx;
  2152. // extra application data
  2153. CRYPTO_EX_DATA ex_data;
  2154. // for server side, keep the list of CA_dn we can use
  2155. STACK_OF(CRYPTO_BUFFER) *client_CA;
  2156. // cached_x509_client_CA is a cache of parsed versions of the elements of
  2157. // |client_CA|.
  2158. STACK_OF(X509_NAME) *cached_x509_client_CA;
  2159. uint32_t options; // protocol behaviour
  2160. uint32_t mode; // API behaviour
  2161. uint32_t max_cert_list;
  2162. uint16_t dummy_pq_padding_len;
  2163. char *tlsext_hostname;
  2164. size_t supported_group_list_len;
  2165. uint16_t *supported_group_list; // our list
  2166. // session_ctx is the |SSL_CTX| used for the session cache and related
  2167. // settings.
  2168. SSL_CTX *session_ctx;
  2169. // srtp_profiles is the list of configured SRTP protection profiles for
  2170. // DTLS-SRTP.
  2171. STACK_OF(SRTP_PROTECTION_PROFILE) *srtp_profiles;
  2172. // The client's Channel ID private key.
  2173. EVP_PKEY *tlsext_channel_id_private;
  2174. // For a client, this contains the list of supported protocols in wire
  2175. // format.
  2176. uint8_t *alpn_client_proto_list;
  2177. unsigned alpn_client_proto_list_len;
  2178. // Contains a list of supported Token Binding key parameters.
  2179. uint8_t *token_binding_params;
  2180. size_t token_binding_params_len;
  2181. // Contains the QUIC transport params that this endpoint will send.
  2182. uint8_t *quic_transport_params;
  2183. size_t quic_transport_params_len;
  2184. // renegotiate_mode controls how peer renegotiation attempts are handled.
  2185. enum ssl_renegotiate_mode_t renegotiate_mode;
  2186. // verify_mode is a bitmask of |SSL_VERIFY_*| values.
  2187. uint8_t verify_mode;
  2188. // server is true iff the this SSL* is the server half. Note: before the SSL*
  2189. // is initialized by either SSL_set_accept_state or SSL_set_connect_state,
  2190. // the side is not determined. In this state, server is always false.
  2191. bool server:1;
  2192. // quiet_shutdown is true if the connection should not send a close_notify on
  2193. // shutdown.
  2194. bool quiet_shutdown:1;
  2195. // Enable signed certificate time stamps. Currently client only.
  2196. bool signed_cert_timestamps_enabled:1;
  2197. // ocsp_stapling_enabled is only used by client connections and indicates
  2198. // whether OCSP stapling will be requested.
  2199. bool ocsp_stapling_enabled:1;
  2200. // tlsext_channel_id_enabled is copied from the |SSL_CTX|. For a server,
  2201. // means that we'll accept Channel IDs from clients. For a client, means that
  2202. // we'll advertise support.
  2203. bool tlsext_channel_id_enabled:1;
  2204. // retain_only_sha256_of_client_certs is true if we should compute the SHA256
  2205. // hash of the peer's certificate and then discard it to save memory and
  2206. // session space. Only effective on the server side.
  2207. bool retain_only_sha256_of_client_certs:1;
  2208. // handoff indicates that a server should stop after receiving the
  2209. // ClientHello and pause the handshake in such a way that |SSL_get_error|
  2210. // returns |SSL_HANDOFF|. This is copied in |SSL_new| from the |SSL_CTX|
  2211. // element of the same name and may be cleared if the handoff is declined.
  2212. bool handoff:1;
  2213. // handback indicates that a server should pause the handshake after
  2214. // finishing operations that require private key material, in such a way that
  2215. // |SSL_get_error| returns |SSL_HANDBACK|. It is set by |SSL_apply_handoff|.
  2216. bool handback : 1;
  2217. // did_dummy_pq_padding is only valid for a client. In that context, it is
  2218. // true iff the client observed the server echoing a dummy PQ padding
  2219. // extension.
  2220. bool did_dummy_pq_padding:1;
  2221. };
  2222. // From draft-ietf-tls-tls13-18, used in determining PSK modes.
  2223. #define SSL_PSK_DHE_KE 0x1
  2224. // From draft-ietf-tls-tls13-16, used in determining whether to respond with a
  2225. // KeyUpdate.
  2226. #define SSL_KEY_UPDATE_NOT_REQUESTED 0
  2227. #define SSL_KEY_UPDATE_REQUESTED 1
  2228. // kMaxEarlyDataAccepted is the advertised number of plaintext bytes of early
  2229. // data that will be accepted. This value should be slightly below
  2230. // kMaxEarlyDataSkipped in tls_record.c, which is measured in ciphertext.
  2231. static const size_t kMaxEarlyDataAccepted = 14336;
  2232. UniquePtr<CERT> ssl_cert_dup(CERT *cert);
  2233. void ssl_cert_clear_certs(CERT *cert);
  2234. int ssl_set_cert(CERT *cert, UniquePtr<CRYPTO_BUFFER> buffer);
  2235. int ssl_is_key_type_supported(int key_type);
  2236. // ssl_compare_public_and_private_key returns one if |pubkey| is the public
  2237. // counterpart to |privkey|. Otherwise it returns zero and pushes a helpful
  2238. // message on the error queue.
  2239. int ssl_compare_public_and_private_key(const EVP_PKEY *pubkey,
  2240. const EVP_PKEY *privkey);
  2241. int ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey);
  2242. int ssl_get_new_session(SSL_HANDSHAKE *hs, int is_server);
  2243. int ssl_encrypt_ticket(SSL *ssl, CBB *out, const SSL_SESSION *session);
  2244. int ssl_ctx_rotate_ticket_encryption_key(SSL_CTX *ctx);
  2245. // ssl_session_new returns a newly-allocated blank |SSL_SESSION| or nullptr on
  2246. // error.
  2247. UniquePtr<SSL_SESSION> ssl_session_new(const SSL_X509_METHOD *x509_method);
  2248. // SSL_SESSION_parse parses an |SSL_SESSION| from |cbs| and advances |cbs| over
  2249. // the parsed data.
  2250. UniquePtr<SSL_SESSION> SSL_SESSION_parse(CBS *cbs,
  2251. const SSL_X509_METHOD *x509_method,
  2252. CRYPTO_BUFFER_POOL *pool);
  2253. // ssl_session_serialize writes |in| to |cbb| as if it were serialising a
  2254. // session for Session-ID resumption. It returns one on success and zero on
  2255. // error.
  2256. int ssl_session_serialize(const SSL_SESSION *in, CBB *cbb);
  2257. // ssl_session_is_context_valid returns one if |session|'s session ID context
  2258. // matches the one set on |ssl| and zero otherwise.
  2259. int ssl_session_is_context_valid(const SSL *ssl, const SSL_SESSION *session);
  2260. // ssl_session_is_time_valid returns one if |session| is still valid and zero if
  2261. // it has expired.
  2262. int ssl_session_is_time_valid(const SSL *ssl, const SSL_SESSION *session);
  2263. // ssl_session_is_resumable returns one if |session| is resumable for |hs| and
  2264. // zero otherwise.
  2265. int ssl_session_is_resumable(const SSL_HANDSHAKE *hs,
  2266. const SSL_SESSION *session);
  2267. // ssl_session_protocol_version returns the protocol version associated with
  2268. // |session|. Note that despite the name, this is not the same as
  2269. // |SSL_SESSION_get_protocol_version|. The latter is based on upstream's name.
  2270. uint16_t ssl_session_protocol_version(const SSL_SESSION *session);
  2271. // ssl_session_get_digest returns the digest used in |session|.
  2272. const EVP_MD *ssl_session_get_digest(const SSL_SESSION *session);
  2273. void ssl_set_session(SSL *ssl, SSL_SESSION *session);
  2274. // ssl_get_prev_session looks up the previous session based on |client_hello|.
  2275. // On success, it sets |*out_session| to the session or nullptr if none was
  2276. // found. If the session could not be looked up synchronously, it returns
  2277. // |ssl_hs_pending_session| and should be called again. If a ticket could not be
  2278. // decrypted immediately it returns |ssl_hs_pending_ticket| and should also
  2279. // be called again. Otherwise, it returns |ssl_hs_error|.
  2280. enum ssl_hs_wait_t ssl_get_prev_session(SSL *ssl,
  2281. UniquePtr<SSL_SESSION> *out_session,
  2282. bool *out_tickets_supported,
  2283. bool *out_renew_ticket,
  2284. const SSL_CLIENT_HELLO *client_hello);
  2285. // The following flags determine which parts of the session are duplicated.
  2286. #define SSL_SESSION_DUP_AUTH_ONLY 0x0
  2287. #define SSL_SESSION_INCLUDE_TICKET 0x1
  2288. #define SSL_SESSION_INCLUDE_NONAUTH 0x2
  2289. #define SSL_SESSION_DUP_ALL \
  2290. (SSL_SESSION_INCLUDE_TICKET | SSL_SESSION_INCLUDE_NONAUTH)
  2291. // SSL_SESSION_dup returns a newly-allocated |SSL_SESSION| with a copy of the
  2292. // fields in |session| or nullptr on error. The new session is non-resumable and
  2293. // must be explicitly marked resumable once it has been filled in.
  2294. OPENSSL_EXPORT UniquePtr<SSL_SESSION> SSL_SESSION_dup(SSL_SESSION *session,
  2295. int dup_flags);
  2296. // ssl_session_rebase_time updates |session|'s start time to the current time,
  2297. // adjusting the timeout so the expiration time is unchanged.
  2298. void ssl_session_rebase_time(SSL *ssl, SSL_SESSION *session);
  2299. // ssl_session_renew_timeout calls |ssl_session_rebase_time| and renews
  2300. // |session|'s timeout to |timeout| (measured from the current time). The
  2301. // renewal is clamped to the session's auth_timeout.
  2302. void ssl_session_renew_timeout(SSL *ssl, SSL_SESSION *session,
  2303. uint32_t timeout);
  2304. // ssl_get_cipher_preferences returns the cipher preference list for TLS 1.2 and
  2305. // below.
  2306. const SSLCipherPreferenceList *ssl_get_cipher_preferences(const SSL *ssl);
  2307. void ssl_update_cache(SSL_HANDSHAKE *hs, int mode);
  2308. int ssl_send_alert(SSL *ssl, int level, int desc);
  2309. bool ssl3_get_message(SSL *ssl, SSLMessage *out);
  2310. ssl_open_record_t ssl3_open_handshake(SSL *ssl, size_t *out_consumed,
  2311. uint8_t *out_alert, Span<uint8_t> in);
  2312. void ssl3_next_message(SSL *ssl);
  2313. int ssl3_dispatch_alert(SSL *ssl);
  2314. ssl_open_record_t ssl3_open_app_data(SSL *ssl, Span<uint8_t> *out,
  2315. size_t *out_consumed, uint8_t *out_alert,
  2316. Span<uint8_t> in);
  2317. ssl_open_record_t ssl3_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
  2318. uint8_t *out_alert,
  2319. Span<uint8_t> in);
  2320. int ssl3_write_app_data(SSL *ssl, bool *out_needs_handshake, const uint8_t *buf,
  2321. int len);
  2322. bool ssl3_new(SSL *ssl);
  2323. void ssl3_free(SSL *ssl);
  2324. bool ssl3_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
  2325. bool ssl3_finish_message(SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg);
  2326. bool ssl3_add_message(SSL *ssl, Array<uint8_t> msg);
  2327. bool ssl3_add_change_cipher_spec(SSL *ssl);
  2328. bool ssl3_add_alert(SSL *ssl, uint8_t level, uint8_t desc);
  2329. int ssl3_flush_flight(SSL *ssl);
  2330. bool dtls1_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
  2331. bool dtls1_finish_message(SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg);
  2332. bool dtls1_add_message(SSL *ssl, Array<uint8_t> msg);
  2333. bool dtls1_add_change_cipher_spec(SSL *ssl);
  2334. bool dtls1_add_alert(SSL *ssl, uint8_t level, uint8_t desc);
  2335. int dtls1_flush_flight(SSL *ssl);
  2336. // ssl_add_message_cbb finishes the handshake message in |cbb| and adds it to
  2337. // the pending flight. It returns true on success and false on error.
  2338. bool ssl_add_message_cbb(SSL *ssl, CBB *cbb);
  2339. // ssl_hash_message incorporates |msg| into the handshake hash. It returns true
  2340. // on success and false on allocation failure.
  2341. bool ssl_hash_message(SSL_HANDSHAKE *hs, const SSLMessage &msg);
  2342. ssl_open_record_t dtls1_open_app_data(SSL *ssl, Span<uint8_t> *out,
  2343. size_t *out_consumed, uint8_t *out_alert,
  2344. Span<uint8_t> in);
  2345. ssl_open_record_t dtls1_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
  2346. uint8_t *out_alert,
  2347. Span<uint8_t> in);
  2348. int dtls1_write_app_data(SSL *ssl, bool *out_needs_handshake,
  2349. const uint8_t *buf, int len);
  2350. // dtls1_write_record sends a record. It returns one on success and <= 0 on
  2351. // error.
  2352. int dtls1_write_record(SSL *ssl, int type, const uint8_t *buf, size_t len,
  2353. enum dtls1_use_epoch_t use_epoch);
  2354. int dtls1_retransmit_outgoing_messages(SSL *ssl);
  2355. bool dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr,
  2356. CBS *out_body);
  2357. bool dtls1_check_timeout_num(SSL *ssl);
  2358. void dtls1_start_timer(SSL *ssl);
  2359. void dtls1_stop_timer(SSL *ssl);
  2360. bool dtls1_is_timer_expired(SSL *ssl);
  2361. unsigned int dtls1_min_mtu(void);
  2362. bool dtls1_new(SSL *ssl);
  2363. void dtls1_free(SSL *ssl);
  2364. bool dtls1_get_message(SSL *ssl, SSLMessage *out);
  2365. ssl_open_record_t dtls1_open_handshake(SSL *ssl, size_t *out_consumed,
  2366. uint8_t *out_alert, Span<uint8_t> in);
  2367. void dtls1_next_message(SSL *ssl);
  2368. int dtls1_dispatch_alert(SSL *ssl);
  2369. // tls1_configure_aead configures either the read or write direction AEAD (as
  2370. // determined by |direction|) using the keys generated by the TLS KDF. The
  2371. // |key_block_cache| argument is used to store the generated key block, if
  2372. // empty. Otherwise it's assumed that the key block is already contained within
  2373. // it. Returns one on success or zero on error.
  2374. int tls1_configure_aead(SSL *ssl, evp_aead_direction_t direction,
  2375. Array<uint8_t> *key_block_cache,
  2376. const SSL_CIPHER *cipher,
  2377. Span<const uint8_t> iv_override);
  2378. int tls1_change_cipher_state(SSL_HANDSHAKE *hs, evp_aead_direction_t direction);
  2379. int tls1_generate_master_secret(SSL_HANDSHAKE *hs, uint8_t *out,
  2380. Span<const uint8_t> premaster);
  2381. // tls1_get_grouplist returns the locally-configured group preference list.
  2382. Span<const uint16_t> tls1_get_grouplist(const SSL *ssl);
  2383. // tls1_check_group_id returns one if |group_id| is consistent with
  2384. // locally-configured group preferences.
  2385. int tls1_check_group_id(const SSL *ssl, uint16_t group_id);
  2386. // tls1_get_shared_group sets |*out_group_id| to the first preferred shared
  2387. // group between client and server preferences and returns one. If none may be
  2388. // found, it returns zero.
  2389. int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id);
  2390. // tls1_set_curves converts the array of |ncurves| NIDs pointed to by |curves|
  2391. // into a newly allocated array of TLS group IDs. On success, the function
  2392. // returns one and writes the array to |*out_group_ids| and its size to
  2393. // |*out_group_ids_len|. Otherwise, it returns zero.
  2394. int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
  2395. const int *curves, size_t ncurves);
  2396. // tls1_set_curves_list converts the string of curves pointed to by |curves|
  2397. // into a newly allocated array of TLS group IDs. On success, the function
  2398. // returns one and writes the array to |*out_group_ids| and its size to
  2399. // |*out_group_ids_len|. Otherwise, it returns zero.
  2400. int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
  2401. const char *curves);
  2402. // ssl_add_clienthello_tlsext writes ClientHello extensions to |out|. It
  2403. // returns one on success and zero on failure. The |header_len| argument is the
  2404. // length of the ClientHello written so far and is used to compute the padding
  2405. // length. (It does not include the record header.)
  2406. int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len);
  2407. int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out);
  2408. int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
  2409. const SSL_CLIENT_HELLO *client_hello);
  2410. int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs);
  2411. #define tlsext_tick_md EVP_sha256
  2412. // ssl_process_ticket processes a session ticket from the client. It returns
  2413. // one of:
  2414. // |ssl_ticket_aead_success|: |*out_session| is set to the parsed session and
  2415. // |*out_renew_ticket| is set to whether the ticket should be renewed.
  2416. // |ssl_ticket_aead_ignore_ticket|: |*out_renew_ticket| is set to whether a
  2417. // fresh ticket should be sent, but the given ticket cannot be used.
  2418. // |ssl_ticket_aead_retry|: the ticket could not be immediately decrypted.
  2419. // Retry later.
  2420. // |ssl_ticket_aead_error|: an error occured that is fatal to the connection.
  2421. enum ssl_ticket_aead_result_t ssl_process_ticket(
  2422. SSL *ssl, UniquePtr<SSL_SESSION> *out_session, bool *out_renew_ticket,
  2423. const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
  2424. size_t session_id_len);
  2425. // tls1_verify_channel_id processes |msg| as a Channel ID message, and verifies
  2426. // the signature. If the key is valid, it saves the Channel ID and returns
  2427. // one. Otherwise, it returns zero.
  2428. int tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg);
  2429. // tls1_write_channel_id generates a Channel ID message and puts the output in
  2430. // |cbb|. |ssl->tlsext_channel_id_private| must already be set before calling.
  2431. // This function returns true on success and false on error.
  2432. bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb);
  2433. // tls1_channel_id_hash computes the hash to be signed by Channel ID and writes
  2434. // it to |out|, which must contain at least |EVP_MAX_MD_SIZE| bytes. It returns
  2435. // one on success and zero on failure.
  2436. int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len);
  2437. int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs);
  2438. // ssl_do_channel_id_callback checks runs |ssl->ctx->channel_id_cb| if
  2439. // necessary. It returns one on success and zero on fatal error. Note that, on
  2440. // success, |ssl->tlsext_channel_id_private| may be unset, in which case the
  2441. // operation should be retried later.
  2442. int ssl_do_channel_id_callback(SSL *ssl);
  2443. // ssl_can_write returns one if |ssl| is allowed to write and zero otherwise.
  2444. int ssl_can_write(const SSL *ssl);
  2445. // ssl_can_read returns one if |ssl| is allowed to read and zero otherwise.
  2446. int ssl_can_read(const SSL *ssl);
  2447. void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock);
  2448. void ssl_ctx_get_current_time(const SSL_CTX *ctx,
  2449. struct OPENSSL_timeval *out_clock);
  2450. // ssl_reset_error_state resets state for |SSL_get_error|.
  2451. void ssl_reset_error_state(SSL *ssl);
  2452. // ssl_set_read_error sets |ssl|'s read half into an error state, saving the
  2453. // current state of the error queue.
  2454. void ssl_set_read_error(SSL* ssl);
  2455. } // namespace bssl
  2456. // Opaque C types.
  2457. //
  2458. // The following types are exported to C code as public typedefs, so they must
  2459. // be defined outside of the namespace.
  2460. // ssl_method_st backs the public |SSL_METHOD| type. It is a compatibility
  2461. // structure to support the legacy version-locked methods.
  2462. struct ssl_method_st {
  2463. // version, if non-zero, is the only protocol version acceptable to an
  2464. // SSL_CTX initialized from this method.
  2465. uint16_t version;
  2466. // method is the underlying SSL_PROTOCOL_METHOD that initializes the
  2467. // SSL_CTX.
  2468. const bssl::SSL_PROTOCOL_METHOD *method;
  2469. // x509_method contains pointers to functions that might deal with |X509|
  2470. // compatibility, or might be a no-op, depending on the application.
  2471. const SSL_X509_METHOD *x509_method;
  2472. };
  2473. struct ssl_x509_method_st {
  2474. // check_client_CA_list returns one if |names| is a good list of X.509
  2475. // distinguished names and zero otherwise. This is used to ensure that we can
  2476. // reject unparsable values at handshake time when using crypto/x509.
  2477. int (*check_client_CA_list)(STACK_OF(CRYPTO_BUFFER) *names);
  2478. // cert_clear frees and NULLs all X509 certificate-related state.
  2479. void (*cert_clear)(bssl::CERT *cert);
  2480. // cert_free frees all X509-related state.
  2481. void (*cert_free)(bssl::CERT *cert);
  2482. // cert_flush_cached_chain drops any cached |X509|-based certificate chain
  2483. // from |cert|.
  2484. // cert_dup duplicates any needed fields from |cert| to |new_cert|.
  2485. void (*cert_dup)(bssl::CERT *new_cert, const bssl::CERT *cert);
  2486. void (*cert_flush_cached_chain)(bssl::CERT *cert);
  2487. // cert_flush_cached_chain drops any cached |X509|-based leaf certificate
  2488. // from |cert|.
  2489. void (*cert_flush_cached_leaf)(bssl::CERT *cert);
  2490. // session_cache_objects fills out |sess->x509_peer| and |sess->x509_chain|
  2491. // from |sess->certs| and erases |sess->x509_chain_without_leaf|. It returns
  2492. // one on success or zero on error.
  2493. int (*session_cache_objects)(SSL_SESSION *session);
  2494. // session_dup duplicates any needed fields from |session| to |new_session|.
  2495. // It returns one on success or zero on error.
  2496. int (*session_dup)(SSL_SESSION *new_session, const SSL_SESSION *session);
  2497. // session_clear frees any X509-related state from |session|.
  2498. void (*session_clear)(SSL_SESSION *session);
  2499. // session_verify_cert_chain verifies the certificate chain in |session|,
  2500. // sets |session->verify_result| and returns one on success or zero on
  2501. // error.
  2502. int (*session_verify_cert_chain)(SSL_SESSION *session, SSL *ssl,
  2503. uint8_t *out_alert);
  2504. // hs_flush_cached_ca_names drops any cached |X509_NAME|s from |hs|.
  2505. void (*hs_flush_cached_ca_names)(bssl::SSL_HANDSHAKE *hs);
  2506. // ssl_new does any neccessary initialisation of |ssl|. It returns one on
  2507. // success or zero on error.
  2508. int (*ssl_new)(SSL *ssl);
  2509. // ssl_free frees anything created by |ssl_new|.
  2510. void (*ssl_free)(SSL *ssl);
  2511. // ssl_flush_cached_client_CA drops any cached |X509_NAME|s from |ssl|.
  2512. void (*ssl_flush_cached_client_CA)(SSL *ssl);
  2513. // ssl_auto_chain_if_needed runs the deprecated auto-chaining logic if
  2514. // necessary. On success, it updates |ssl|'s certificate configuration as
  2515. // needed and returns one. Otherwise, it returns zero.
  2516. int (*ssl_auto_chain_if_needed)(SSL *ssl);
  2517. // ssl_ctx_new does any neccessary initialisation of |ctx|. It returns one on
  2518. // success or zero on error.
  2519. int (*ssl_ctx_new)(SSL_CTX *ctx);
  2520. // ssl_ctx_free frees anything created by |ssl_ctx_new|.
  2521. void (*ssl_ctx_free)(SSL_CTX *ctx);
  2522. // ssl_ctx_flush_cached_client_CA drops any cached |X509_NAME|s from |ctx|.
  2523. void (*ssl_ctx_flush_cached_client_CA)(SSL_CTX *ssl);
  2524. };
  2525. // The following types back public C-exposed types which must live in the global
  2526. // namespace. We use subclassing so the implementations may be C++ types with
  2527. // methods and destructor without polluting the global namespace.
  2528. struct ssl_ctx_st : public bssl::SSLContext {};
  2529. struct ssl_st : public bssl::SSLConnection {};
  2530. #endif // OPENSSL_HEADER_SSL_INTERNAL_H