You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1942 regels
80 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 <openssl/aead.h>
  145. #include <openssl/ssl.h>
  146. #include <openssl/stack.h>
  147. #if defined(OPENSSL_WINDOWS)
  148. /* Windows defines struct timeval in winsock2.h. */
  149. OPENSSL_MSVC_PRAGMA(warning(push, 3))
  150. #include <winsock2.h>
  151. OPENSSL_MSVC_PRAGMA(warning(pop))
  152. #else
  153. #include <sys/time.h>
  154. #endif
  155. #if defined(__cplusplus)
  156. extern "C" {
  157. #endif
  158. /* Cipher suites. */
  159. /* Bits for |algorithm_mkey| (key exchange algorithm). */
  160. #define SSL_kRSA 0x00000001L
  161. #define SSL_kDHE 0x00000002L
  162. #define SSL_kECDHE 0x00000004L
  163. /* SSL_kPSK is only set for plain PSK, not ECDHE_PSK. */
  164. #define SSL_kPSK 0x00000008L
  165. #define SSL_kGENERIC 0x00000010L
  166. /* Bits for |algorithm_auth| (server authentication). */
  167. #define SSL_aRSA 0x00000001L
  168. #define SSL_aECDSA 0x00000002L
  169. /* SSL_aPSK is set for both PSK and ECDHE_PSK. */
  170. #define SSL_aPSK 0x00000004L
  171. #define SSL_aGENERIC 0x00000008L
  172. #define SSL_aCERT (SSL_aRSA | SSL_aECDSA)
  173. /* Bits for |algorithm_enc| (symmetric encryption). */
  174. #define SSL_3DES 0x00000001L
  175. #define SSL_AES128 0x00000002L
  176. #define SSL_AES256 0x00000004L
  177. #define SSL_AES128GCM 0x00000008L
  178. #define SSL_AES256GCM 0x00000010L
  179. #define SSL_CHACHA20POLY1305_OLD 0x00000020L
  180. #define SSL_eNULL 0x00000040L
  181. #define SSL_CHACHA20POLY1305 0x00000080L
  182. #define SSL_AES (SSL_AES128 | SSL_AES256 | SSL_AES128GCM | SSL_AES256GCM)
  183. /* Bits for |algorithm_mac| (symmetric authentication). */
  184. #define SSL_MD5 0x00000001L
  185. #define SSL_SHA1 0x00000002L
  186. #define SSL_SHA256 0x00000004L
  187. #define SSL_SHA384 0x00000008L
  188. /* SSL_AEAD is set for all AEADs. */
  189. #define SSL_AEAD 0x00000010L
  190. /* Bits for |algorithm_prf| (handshake digest). */
  191. #define SSL_HANDSHAKE_MAC_DEFAULT 0x1
  192. #define SSL_HANDSHAKE_MAC_SHA256 0x2
  193. #define SSL_HANDSHAKE_MAC_SHA384 0x4
  194. /* SSL_MAX_DIGEST is the number of digest types which exist. When adding a new
  195. * one, update the table in ssl_cipher.c. */
  196. #define SSL_MAX_DIGEST 4
  197. /* ssl_cipher_get_evp_aead sets |*out_aead| to point to the correct EVP_AEAD
  198. * object for |cipher| protocol version |version|. It sets |*out_mac_secret_len|
  199. * and |*out_fixed_iv_len| to the MAC key length and fixed IV length,
  200. * respectively. The MAC key length is zero except for legacy block and stream
  201. * ciphers. It returns 1 on success and 0 on error. */
  202. int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
  203. size_t *out_mac_secret_len,
  204. size_t *out_fixed_iv_len,
  205. const SSL_CIPHER *cipher, uint16_t version);
  206. /* ssl_get_handshake_digest returns the |EVP_MD| corresponding to
  207. * |algorithm_prf|. It returns SHA-1 for |SSL_HANDSHAKE_DEFAULT|. The caller is
  208. * responsible for maintaining the additional MD5 digest and switching to
  209. * SHA-256 in TLS 1.2. */
  210. const EVP_MD *ssl_get_handshake_digest(uint32_t algorithm_prf);
  211. /* ssl_create_cipher_list evaluates |rule_str| according to the ciphers in
  212. * |ssl_method|. It sets |*out_cipher_list| to a newly-allocated
  213. * |ssl_cipher_preference_list_st| containing the result. It returns
  214. * |(*out_cipher_list)->ciphers| on success and NULL on
  215. * failure. */
  216. STACK_OF(SSL_CIPHER) *
  217. ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
  218. struct ssl_cipher_preference_list_st **out_cipher_list,
  219. const char *rule_str);
  220. /* ssl_cipher_get_value returns the cipher suite id of |cipher|. */
  221. uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher);
  222. /* ssl_cipher_get_key_type returns the |EVP_PKEY_*| value corresponding to the
  223. * server key used in |cipher| or |EVP_PKEY_NONE| if there is none. */
  224. int ssl_cipher_get_key_type(const SSL_CIPHER *cipher);
  225. /* ssl_cipher_uses_certificate_auth returns one if |cipher| authenticates the
  226. * server and, optionally, the client with a certificate. Otherwise it returns
  227. * zero. */
  228. int ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher);
  229. /* ssl_cipher_requires_server_key_exchange returns 1 if |cipher| requires a
  230. * ServerKeyExchange message. Otherwise it returns 0.
  231. *
  232. * This function may return zero while still allowing |cipher| an optional
  233. * ServerKeyExchange. This is the case for plain PSK ciphers. */
  234. int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher);
  235. /* ssl_cipher_get_record_split_len, for TLS 1.0 CBC mode ciphers, returns the
  236. * length of an encrypted 1-byte record, for use in record-splitting. Otherwise
  237. * it returns zero. */
  238. size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher);
  239. /* Encryption layer. */
  240. /* SSL_AEAD_CTX contains information about an AEAD that is being used to encrypt
  241. * an SSL connection. */
  242. typedef struct ssl_aead_ctx_st {
  243. const SSL_CIPHER *cipher;
  244. EVP_AEAD_CTX ctx;
  245. /* fixed_nonce contains any bytes of the nonce that are fixed for all
  246. * records. */
  247. uint8_t fixed_nonce[12];
  248. uint8_t fixed_nonce_len, variable_nonce_len;
  249. /* variable_nonce_included_in_record is non-zero if the variable nonce
  250. * for a record is included as a prefix before the ciphertext. */
  251. char variable_nonce_included_in_record;
  252. /* random_variable_nonce is non-zero if the variable nonce is
  253. * randomly generated, rather than derived from the sequence
  254. * number. */
  255. char random_variable_nonce;
  256. /* omit_length_in_ad is non-zero if the length should be omitted in the
  257. * AEAD's ad parameter. */
  258. char omit_length_in_ad;
  259. /* omit_version_in_ad is non-zero if the version should be omitted
  260. * in the AEAD's ad parameter. */
  261. char omit_version_in_ad;
  262. /* omit_ad is non-zero if the AEAD's ad parameter should be omitted. */
  263. char omit_ad;
  264. /* xor_fixed_nonce is non-zero if the fixed nonce should be XOR'd into the
  265. * variable nonce rather than prepended. */
  266. char xor_fixed_nonce;
  267. } SSL_AEAD_CTX;
  268. /* SSL_AEAD_CTX_new creates a newly-allocated |SSL_AEAD_CTX| using the supplied
  269. * key material. It returns NULL on error. Only one of |SSL_AEAD_CTX_open| or
  270. * |SSL_AEAD_CTX_seal| may be used with the resulting object, depending on
  271. * |direction|. |version| is the normalized protocol version, so DTLS 1.0 is
  272. * represented as 0x0301, not 0xffef. */
  273. SSL_AEAD_CTX *SSL_AEAD_CTX_new(enum evp_aead_direction_t direction,
  274. uint16_t version, const SSL_CIPHER *cipher,
  275. const uint8_t *enc_key, size_t enc_key_len,
  276. const uint8_t *mac_key, size_t mac_key_len,
  277. const uint8_t *fixed_iv, size_t fixed_iv_len);
  278. /* SSL_AEAD_CTX_free frees |ctx|. */
  279. void SSL_AEAD_CTX_free(SSL_AEAD_CTX *ctx);
  280. /* SSL_AEAD_CTX_explicit_nonce_len returns the length of the explicit nonce for
  281. * |ctx|, if any. |ctx| may be NULL to denote the null cipher. */
  282. size_t SSL_AEAD_CTX_explicit_nonce_len(SSL_AEAD_CTX *ctx);
  283. /* SSL_AEAD_CTX_max_overhead returns the maximum overhead of calling
  284. * |SSL_AEAD_CTX_seal|. |ctx| may be NULL to denote the null cipher. */
  285. size_t SSL_AEAD_CTX_max_overhead(SSL_AEAD_CTX *ctx);
  286. /* SSL_AEAD_CTX_open authenticates and decrypts |in_len| bytes from |in|
  287. * in-place. On success, it sets |*out| to the plaintext in |in| and returns
  288. * one. Otherwise, it returns zero. |ctx| may be NULL to denote the null cipher.
  289. * The output will always be |explicit_nonce_len| bytes ahead of |in|. */
  290. int SSL_AEAD_CTX_open(SSL_AEAD_CTX *ctx, CBS *out, uint8_t type,
  291. uint16_t wire_version, const uint8_t seqnum[8],
  292. uint8_t *in, size_t in_len);
  293. /* SSL_AEAD_CTX_seal encrypts and authenticates |in_len| bytes from |in| and
  294. * writes the result to |out|. It returns one on success and zero on
  295. * error. |ctx| may be NULL to denote the null cipher.
  296. *
  297. * If |in| and |out| alias then |out| + |explicit_nonce_len| must be == |in|. */
  298. int SSL_AEAD_CTX_seal(SSL_AEAD_CTX *ctx, uint8_t *out, size_t *out_len,
  299. size_t max_out, uint8_t type, uint16_t wire_version,
  300. const uint8_t seqnum[8], const uint8_t *in,
  301. size_t in_len);
  302. /* DTLS replay bitmap. */
  303. /* DTLS1_BITMAP maintains a sliding window of 64 sequence numbers to detect
  304. * replayed packets. It should be initialized by zeroing every field. */
  305. typedef struct dtls1_bitmap_st {
  306. /* map is a bit mask of the last 64 sequence numbers. Bit
  307. * |1<<i| corresponds to |max_seq_num - i|. */
  308. uint64_t map;
  309. /* max_seq_num is the largest sequence number seen so far as a 64-bit
  310. * integer. */
  311. uint64_t max_seq_num;
  312. } DTLS1_BITMAP;
  313. /* Record layer. */
  314. /* ssl_record_sequence_update increments the sequence number in |seq|. It
  315. * returns one on success and zero on wraparound. */
  316. int ssl_record_sequence_update(uint8_t *seq, size_t seq_len);
  317. /* ssl_record_prefix_len returns the length of the prefix before the ciphertext
  318. * of a record for |ssl|.
  319. *
  320. * TODO(davidben): Expose this as part of public API once the high-level
  321. * buffer-free APIs are available. */
  322. size_t ssl_record_prefix_len(const SSL *ssl);
  323. enum ssl_open_record_t {
  324. ssl_open_record_success,
  325. ssl_open_record_discard,
  326. ssl_open_record_partial,
  327. ssl_open_record_close_notify,
  328. ssl_open_record_fatal_alert,
  329. ssl_open_record_error,
  330. };
  331. /* tls_open_record decrypts a record from |in| in-place.
  332. *
  333. * If the input did not contain a complete record, it returns
  334. * |ssl_open_record_partial|. It sets |*out_consumed| to the total number of
  335. * bytes necessary. It is guaranteed that a successful call to |tls_open_record|
  336. * will consume at least that many bytes.
  337. *
  338. * Otherwise, it sets |*out_consumed| to the number of bytes of input
  339. * consumed. Note that input may be consumed on all return codes if a record was
  340. * decrypted.
  341. *
  342. * On success, it returns |ssl_open_record_success|. It sets |*out_type| to the
  343. * record type and |*out| to the record body in |in|. Note that |*out| may be
  344. * empty.
  345. *
  346. * If a record was successfully processed but should be discarded, it returns
  347. * |ssl_open_record_discard|.
  348. *
  349. * If a record was successfully processed but is a close_notify or fatal alert,
  350. * it returns |ssl_open_record_close_notify| or |ssl_open_record_fatal_alert|.
  351. *
  352. * On failure, it returns |ssl_open_record_error| and sets |*out_alert| to an
  353. * alert to emit. */
  354. enum ssl_open_record_t tls_open_record(SSL *ssl, uint8_t *out_type, CBS *out,
  355. size_t *out_consumed, uint8_t *out_alert,
  356. uint8_t *in, size_t in_len);
  357. /* dtls_open_record implements |tls_open_record| for DTLS. It never returns
  358. * |ssl_open_record_partial| but otherwise behaves analogously. */
  359. enum ssl_open_record_t dtls_open_record(SSL *ssl, uint8_t *out_type, CBS *out,
  360. size_t *out_consumed,
  361. uint8_t *out_alert, uint8_t *in,
  362. size_t in_len);
  363. /* ssl_seal_align_prefix_len returns the length of the prefix before the start
  364. * of the bulk of the ciphertext when sealing a record with |ssl|. Callers may
  365. * use this to align buffers.
  366. *
  367. * Note when TLS 1.0 CBC record-splitting is enabled, this includes the one byte
  368. * record and is the offset into second record's ciphertext. Thus this value may
  369. * differ from |ssl_record_prefix_len| and sealing a small record may result in
  370. * a smaller output than this value.
  371. *
  372. * TODO(davidben): Expose this as part of public API once the high-level
  373. * buffer-free APIs are available. */
  374. size_t ssl_seal_align_prefix_len(const SSL *ssl);
  375. /* tls_seal_record seals a new record of type |type| and body |in| and writes it
  376. * to |out|. At most |max_out| bytes will be written. It returns one on success
  377. * and zero on error. If enabled, |tls_seal_record| implements TLS 1.0 CBC 1/n-1
  378. * record splitting and may write two records concatenated.
  379. *
  380. * For a large record, the bulk of the ciphertext will begin
  381. * |ssl_seal_align_prefix_len| bytes into out. Aligning |out| appropriately may
  382. * improve performance. It writes at most |in_len| + |SSL_max_seal_overhead|
  383. * bytes to |out|.
  384. *
  385. * |in| and |out| may not alias. */
  386. int tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
  387. uint8_t type, const uint8_t *in, size_t in_len);
  388. enum dtls1_use_epoch_t {
  389. dtls1_use_previous_epoch,
  390. dtls1_use_current_epoch,
  391. };
  392. /* dtls_seal_record implements |tls_seal_record| for DTLS. |use_epoch| selects
  393. * which epoch's cipher state to use. */
  394. int dtls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
  395. uint8_t type, const uint8_t *in, size_t in_len,
  396. enum dtls1_use_epoch_t use_epoch);
  397. /* ssl_process_alert processes |in| as an alert and updates |ssl|'s shutdown
  398. * state. It returns one of |ssl_open_record_discard|, |ssl_open_record_error|,
  399. * |ssl_open_record_close_notify|, or |ssl_open_record_fatal_alert| as
  400. * appropriate. */
  401. enum ssl_open_record_t ssl_process_alert(SSL *ssl, uint8_t *out_alert,
  402. const uint8_t *in, size_t in_len);
  403. /* Private key operations. */
  404. /* ssl_has_private_key returns one if |ssl| has a private key
  405. * configured and zero otherwise. */
  406. int ssl_has_private_key(const SSL *ssl);
  407. /* ssl_is_ecdsa_key_type returns one if |type| is an ECDSA key type and zero
  408. * otherwise. */
  409. int ssl_is_ecdsa_key_type(int type);
  410. /* ssl_private_key_* call the corresponding function on the
  411. * |SSL_PRIVATE_KEY_METHOD| for |ssl|, if configured. Otherwise, they implement
  412. * the operation with |EVP_PKEY|. */
  413. int ssl_private_key_type(SSL *ssl);
  414. size_t ssl_private_key_max_signature_len(SSL *ssl);
  415. enum ssl_private_key_result_t ssl_private_key_sign(
  416. SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
  417. uint16_t signature_algorithm, const uint8_t *in, size_t in_len);
  418. enum ssl_private_key_result_t ssl_private_key_decrypt(
  419. SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
  420. const uint8_t *in, size_t in_len);
  421. enum ssl_private_key_result_t ssl_private_key_complete(SSL *ssl, uint8_t *out,
  422. size_t *out_len,
  423. size_t max_out);
  424. /* ssl_private_key_supports_signature_algorithm returns one if |ssl|'s private
  425. * key supports |signature_algorithm| and zero otherwise. */
  426. int ssl_private_key_supports_signature_algorithm(SSL *ssl,
  427. uint16_t signature_algorithm);
  428. /* ssl_public_key_verify verifies that the |signature| is valid for the public
  429. * key |pkey| and input |in|, using the |signature_algorithm| specified. */
  430. int ssl_public_key_verify(
  431. SSL *ssl, const uint8_t *signature, size_t signature_len,
  432. uint16_t signature_algorithm, EVP_PKEY *pkey,
  433. const uint8_t *in, size_t in_len);
  434. /* Custom extensions */
  435. /* ssl_custom_extension (a.k.a. SSL_CUSTOM_EXTENSION) is a structure that
  436. * contains information about custom-extension callbacks. */
  437. struct ssl_custom_extension {
  438. SSL_custom_ext_add_cb add_callback;
  439. void *add_arg;
  440. SSL_custom_ext_free_cb free_callback;
  441. SSL_custom_ext_parse_cb parse_callback;
  442. void *parse_arg;
  443. uint16_t value;
  444. };
  445. void SSL_CUSTOM_EXTENSION_free(SSL_CUSTOM_EXTENSION *custom_extension);
  446. int custom_ext_add_clienthello(SSL_HANDSHAKE *hs, CBB *extensions);
  447. int custom_ext_parse_serverhello(SSL_HANDSHAKE *hs, int *out_alert,
  448. uint16_t value, const CBS *extension);
  449. int custom_ext_parse_clienthello(SSL_HANDSHAKE *hs, int *out_alert,
  450. uint16_t value, const CBS *extension);
  451. int custom_ext_add_serverhello(SSL_HANDSHAKE *hs, CBB *extensions);
  452. /* Handshake hash.
  453. *
  454. * The TLS handshake maintains a transcript of all handshake messages. At
  455. * various points in the protocol, this is either a handshake buffer, a rolling
  456. * hash (selected by cipher suite) or both. */
  457. /* ssl3_init_handshake_buffer initializes the handshake buffer and resets the
  458. * handshake hash. It returns one success and zero on failure. */
  459. int ssl3_init_handshake_buffer(SSL *ssl);
  460. /* ssl3_init_handshake_hash initializes the handshake hash based on the pending
  461. * cipher and the contents of the handshake buffer. Subsequent calls to
  462. * |ssl3_update_handshake_hash| will update the rolling hash. It returns one on
  463. * success and zero on failure. It is an error to call this function after the
  464. * handshake buffer is released. */
  465. int ssl3_init_handshake_hash(SSL *ssl);
  466. /* ssl3_free_handshake_buffer releases the handshake buffer. Subsequent calls
  467. * to |ssl3_update_handshake_hash| will not update the handshake buffer. */
  468. void ssl3_free_handshake_buffer(SSL *ssl);
  469. /* ssl3_free_handshake_hash releases the handshake hash. */
  470. void ssl3_free_handshake_hash(SSL *ssl);
  471. /* ssl3_update_handshake_hash adds |in| to the handshake buffer and handshake
  472. * hash, whichever is enabled. It returns one on success and zero on failure. */
  473. int ssl3_update_handshake_hash(SSL *ssl, const uint8_t *in, size_t in_len);
  474. /* ECDH groups. */
  475. typedef struct ssl_ecdh_ctx_st SSL_ECDH_CTX;
  476. /* An SSL_ECDH_METHOD is an implementation of ECDH-like key exchanges for
  477. * TLS. */
  478. typedef struct ssl_ecdh_method_st {
  479. int nid;
  480. uint16_t group_id;
  481. const char name[8];
  482. /* cleanup releases state in |ctx|. */
  483. void (*cleanup)(SSL_ECDH_CTX *ctx);
  484. /* offer generates a keypair and writes the public value to
  485. * |out_public_key|. It returns one on success and zero on error. */
  486. int (*offer)(SSL_ECDH_CTX *ctx, CBB *out_public_key);
  487. /* accept performs a key exchange against the |peer_key| generated by |offer|.
  488. * On success, it returns one, writes the public value to |out_public_key|,
  489. * and sets |*out_secret| and |*out_secret_len| to a newly-allocated buffer
  490. * containing the shared secret. The caller must release this buffer with
  491. * |OPENSSL_free|. On failure, it returns zero and sets |*out_alert| to an
  492. * alert to send to the peer. */
  493. int (*accept)(SSL_ECDH_CTX *ctx, CBB *out_public_key, uint8_t **out_secret,
  494. size_t *out_secret_len, uint8_t *out_alert,
  495. const uint8_t *peer_key, size_t peer_key_len);
  496. /* finish performs a key exchange against the |peer_key| generated by
  497. * |accept|. On success, it returns one and sets |*out_secret| and
  498. * |*out_secret_len| to a newly-allocated buffer containing the shared
  499. * secret. The caller must release this buffer with |OPENSSL_free|. On
  500. * failure, it returns zero and sets |*out_alert| to an alert to send to the
  501. * peer. */
  502. int (*finish)(SSL_ECDH_CTX *ctx, uint8_t **out_secret, size_t *out_secret_len,
  503. uint8_t *out_alert, const uint8_t *peer_key,
  504. size_t peer_key_len);
  505. /* get_key initializes |out| with a length-prefixed key from |cbs|. It returns
  506. * one on success and zero on error. */
  507. int (*get_key)(CBS *cbs, CBS *out);
  508. /* add_key initializes |out_contents| to receive a key. Typically it will then
  509. * be passed to |offer| or |accept|. It returns one on success and zero on
  510. * error. */
  511. int (*add_key)(CBB *cbb, CBB *out_contents);
  512. } SSL_ECDH_METHOD;
  513. struct ssl_ecdh_ctx_st {
  514. const SSL_ECDH_METHOD *method;
  515. void *data;
  516. };
  517. /* ssl_nid_to_group_id looks up the group corresponding to |nid|. On success, it
  518. * sets |*out_group_id| to the group ID and returns one. Otherwise, it returns
  519. * zero. */
  520. int ssl_nid_to_group_id(uint16_t *out_group_id, int nid);
  521. /* ssl_name_to_group_id looks up the group corresponding to the |name| string
  522. * of length |len|. On success, it sets |*out_group_id| to the group ID and
  523. * returns one. Otherwise, it returns zero. */
  524. int ssl_name_to_group_id(uint16_t *out_group_id, const char *name, size_t len);
  525. /* SSL_ECDH_CTX_init sets up |ctx| for use with curve |group_id|. It returns one
  526. * on success and zero on error. */
  527. int SSL_ECDH_CTX_init(SSL_ECDH_CTX *ctx, uint16_t group_id);
  528. /* SSL_ECDH_CTX_init_for_dhe sets up |ctx| for use with legacy DHE-based ciphers
  529. * where the server specifies a group. It takes ownership of |params|. */
  530. void SSL_ECDH_CTX_init_for_dhe(SSL_ECDH_CTX *ctx, DH *params);
  531. /* SSL_ECDH_CTX_cleanup releases memory associated with |ctx|. It is legal to
  532. * call it in the zero state. */
  533. void SSL_ECDH_CTX_cleanup(SSL_ECDH_CTX *ctx);
  534. /* SSL_ECDH_CTX_get_id returns the group ID for |ctx|. */
  535. uint16_t SSL_ECDH_CTX_get_id(const SSL_ECDH_CTX *ctx);
  536. /* SSL_ECDH_CTX_get_key calls the |get_key| method of |SSL_ECDH_METHOD|. */
  537. int SSL_ECDH_CTX_get_key(SSL_ECDH_CTX *ctx, CBS *cbs, CBS *out);
  538. /* SSL_ECDH_CTX_add_key calls the |add_key| method of |SSL_ECDH_METHOD|. */
  539. int SSL_ECDH_CTX_add_key(SSL_ECDH_CTX *ctx, CBB *cbb, CBB *out_contents);
  540. /* SSL_ECDH_CTX_offer calls the |offer| method of |SSL_ECDH_METHOD|. */
  541. int SSL_ECDH_CTX_offer(SSL_ECDH_CTX *ctx, CBB *out_public_key);
  542. /* SSL_ECDH_CTX_accept calls the |accept| method of |SSL_ECDH_METHOD|. */
  543. int SSL_ECDH_CTX_accept(SSL_ECDH_CTX *ctx, CBB *out_public_key,
  544. uint8_t **out_secret, size_t *out_secret_len,
  545. uint8_t *out_alert, const uint8_t *peer_key,
  546. size_t peer_key_len);
  547. /* SSL_ECDH_CTX_finish the |finish| method of |SSL_ECDH_METHOD|. */
  548. int SSL_ECDH_CTX_finish(SSL_ECDH_CTX *ctx, uint8_t **out_secret,
  549. size_t *out_secret_len, uint8_t *out_alert,
  550. const uint8_t *peer_key, size_t peer_key_len);
  551. /* Handshake messages. */
  552. /* SSL_MAX_HANDSHAKE_FLIGHT is the number of messages, including
  553. * ChangeCipherSpec, in the longest handshake flight. Currently this is the
  554. * client's second leg in a full handshake when client certificates, NPN, and
  555. * Channel ID, are all enabled. */
  556. #define SSL_MAX_HANDSHAKE_FLIGHT 7
  557. /* ssl_max_handshake_message_len returns the maximum number of bytes permitted
  558. * in a handshake message for |ssl|. */
  559. size_t ssl_max_handshake_message_len(const SSL *ssl);
  560. /* dtls_clear_incoming_messages releases all buffered incoming messages. */
  561. void dtls_clear_incoming_messages(SSL *ssl);
  562. /* dtls_has_incoming_messages returns one if there are buffered incoming
  563. * messages ahead of the current message and zero otherwise. */
  564. int dtls_has_incoming_messages(const SSL *ssl);
  565. typedef struct dtls_outgoing_message_st {
  566. uint8_t *data;
  567. uint32_t len;
  568. uint16_t epoch;
  569. char is_ccs;
  570. } DTLS_OUTGOING_MESSAGE;
  571. /* dtls_clear_outgoing_messages releases all buffered outgoing messages. */
  572. void dtls_clear_outgoing_messages(SSL *ssl);
  573. /* Callbacks. */
  574. /* ssl_do_info_callback calls |ssl|'s info callback, if set. */
  575. void ssl_do_info_callback(const SSL *ssl, int type, int value);
  576. /* ssl_do_msg_callback calls |ssl|'s message callback, if set. */
  577. void ssl_do_msg_callback(SSL *ssl, int is_write, int content_type,
  578. const void *buf, size_t len);
  579. /* Transport buffers. */
  580. /* ssl_read_buffer returns a pointer to contents of the read buffer. */
  581. uint8_t *ssl_read_buffer(SSL *ssl);
  582. /* ssl_read_buffer_len returns the length of the read buffer. */
  583. size_t ssl_read_buffer_len(const SSL *ssl);
  584. /* ssl_read_buffer_extend_to extends the read buffer to the desired length. For
  585. * TLS, it reads to the end of the buffer until the buffer is |len| bytes
  586. * long. For DTLS, it reads a new packet and ignores |len|. It returns one on
  587. * success, zero on EOF, and a negative number on error.
  588. *
  589. * It is an error to call |ssl_read_buffer_extend_to| in DTLS when the buffer is
  590. * non-empty. */
  591. int ssl_read_buffer_extend_to(SSL *ssl, size_t len);
  592. /* ssl_read_buffer_consume consumes |len| bytes from the read buffer. It
  593. * advances the data pointer and decrements the length. The memory consumed will
  594. * remain valid until the next call to |ssl_read_buffer_extend| or it is
  595. * discarded with |ssl_read_buffer_discard|. */
  596. void ssl_read_buffer_consume(SSL *ssl, size_t len);
  597. /* ssl_read_buffer_discard discards the consumed bytes from the read buffer. If
  598. * the buffer is now empty, it releases memory used by it. */
  599. void ssl_read_buffer_discard(SSL *ssl);
  600. /* ssl_read_buffer_clear releases all memory associated with the read buffer and
  601. * zero-initializes it. */
  602. void ssl_read_buffer_clear(SSL *ssl);
  603. /* ssl_write_buffer_is_pending returns one if the write buffer has pending data
  604. * and zero if is empty. */
  605. int ssl_write_buffer_is_pending(const SSL *ssl);
  606. /* ssl_write_buffer_init initializes the write buffer. On success, it sets
  607. * |*out_ptr| to the start of the write buffer with space for up to |max_len|
  608. * bytes. It returns one on success and zero on failure. Call
  609. * |ssl_write_buffer_set_len| to complete initialization. */
  610. int ssl_write_buffer_init(SSL *ssl, uint8_t **out_ptr, size_t max_len);
  611. /* ssl_write_buffer_set_len is called after |ssl_write_buffer_init| to complete
  612. * initialization after |len| bytes are written to the buffer. */
  613. void ssl_write_buffer_set_len(SSL *ssl, size_t len);
  614. /* ssl_write_buffer_flush flushes the write buffer to the transport. It returns
  615. * one on success and <= 0 on error. For DTLS, whether or not the write
  616. * succeeds, the write buffer will be cleared. */
  617. int ssl_write_buffer_flush(SSL *ssl);
  618. /* ssl_write_buffer_clear releases all memory associated with the write buffer
  619. * and zero-initializes it. */
  620. void ssl_write_buffer_clear(SSL *ssl);
  621. /* Certificate functions. */
  622. /* ssl_has_certificate returns one if a certificate and private key are
  623. * configured and zero otherwise. */
  624. int ssl_has_certificate(const SSL *ssl);
  625. /* ssl_parse_x509 parses a X509 certificate from |cbs|. It returns NULL
  626. * on error. */
  627. X509 *ssl_parse_x509(CBS *cbs);
  628. /* ssl_session_x509_cache_objects fills out |sess->x509_peer| and
  629. * |sess->x509_chain| from |sess->certs| and erases
  630. * |sess->x509_chain_without_leaf|. It returns one on success or zero on
  631. * error. */
  632. int ssl_session_x509_cache_objects(SSL_SESSION *sess);
  633. /* ssl_parse_cert_chain parses a certificate list from |cbs| in the format used
  634. * by a TLS Certificate message. On success, it returns a newly-allocated
  635. * |CRYPTO_BUFFER| list and advances |cbs|. Otherwise, it returns NULL and sets
  636. * |*out_alert| to an alert to send to the peer.
  637. *
  638. * If the list is non-empty then |*out_pubkey| will be set to a freshly
  639. * allocated public-key from the leaf certificate.
  640. *
  641. * If the list is non-empty and |out_leaf_sha256| is non-NULL, it writes the
  642. * SHA-256 hash of the leaf to |out_leaf_sha256|. */
  643. STACK_OF(CRYPTO_BUFFER) *ssl_parse_cert_chain(uint8_t *out_alert,
  644. EVP_PKEY **out_pubkey,
  645. uint8_t *out_leaf_sha256,
  646. CBS *cbs,
  647. CRYPTO_BUFFER_POOL *pool);
  648. /* ssl_add_cert_to_cbb adds |x509| to |cbb|. It returns one on success and zero
  649. * on error. */
  650. int ssl_add_cert_to_cbb(CBB *cbb, X509 *x509);
  651. /* ssl_add_cert_chain adds |ssl|'s certificate chain to |cbb| in the format used
  652. * by a TLS Certificate message. If there is no certificate chain, it emits an
  653. * empty certificate list. It returns one on success and zero on error. */
  654. int ssl_add_cert_chain(SSL *ssl, CBB *cbb);
  655. /* ssl_cert_parse_pubkey extracts the public key from the DER-encoded, X.509
  656. * certificate in |in|. It returns an allocated |EVP_PKEY| or else returns NULL
  657. * and pushes to the error queue. */
  658. EVP_PKEY *ssl_cert_parse_pubkey(const CBS *in);
  659. /* ssl_parse_client_CA_list parses a CA list from |cbs| in the format used by a
  660. * TLS CertificateRequest message. On success, it returns a newly-allocated
  661. * |X509_NAME| list and advances |cbs|. Otherwise, it returns NULL and sets
  662. * |*out_alert| to an alert to send to the peer. */
  663. STACK_OF(X509_NAME) *
  664. ssl_parse_client_CA_list(SSL *ssl, uint8_t *out_alert, CBS *cbs);
  665. /* ssl_add_client_CA_list adds the configured CA list to |cbb| in the format
  666. * used by a TLS CertificateRequest message. It returns one on success and zero
  667. * on error. */
  668. int ssl_add_client_CA_list(SSL *ssl, CBB *cbb);
  669. /* ssl_check_leaf_certificate returns one if |leaf| is a suitable leaf server
  670. * certificate for |ssl|. Otherwise, it returns zero and pushes an error on the
  671. * error queue. */
  672. int ssl_check_leaf_certificate(SSL *ssl, X509 *leaf);
  673. /* TLS 1.3 key derivation. */
  674. /* tls13_init_key_schedule initializes the handshake hash and key derivation
  675. * state. The cipher suite and PRF hash must have been selected at this point.
  676. * It returns one on success and zero on error. */
  677. int tls13_init_key_schedule(SSL_HANDSHAKE *hs);
  678. /* tls13_advance_key_schedule incorporates |in| into the key schedule with
  679. * HKDF-Extract. It returns one on success and zero on error. */
  680. int tls13_advance_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *in,
  681. size_t len);
  682. /* tls13_get_context_hash writes Hash(Handshake Context) to |out| which must
  683. * have room for at least |EVP_MAX_MD_SIZE| bytes. On success, it returns one
  684. * and sets |*out_len| to the number of bytes written. Otherwise, it returns
  685. * zero. */
  686. int tls13_get_context_hash(SSL *ssl, uint8_t *out, size_t *out_len);
  687. /* tls13_set_traffic_key sets the read or write traffic keys to
  688. * |traffic_secret|. It returns one on success and zero on error. */
  689. int tls13_set_traffic_key(SSL *ssl, enum evp_aead_direction_t direction,
  690. const uint8_t *traffic_secret,
  691. size_t traffic_secret_len);
  692. /* tls13_set_handshake_traffic derives the handshake traffic secret and
  693. * switches both read and write traffic to it. It returns one on success and
  694. * zero on error. */
  695. int tls13_set_handshake_traffic(SSL_HANDSHAKE *hs);
  696. /* tls13_rotate_traffic_key derives the next read or write traffic secret. It
  697. * returns one on success and zero on error. */
  698. int tls13_rotate_traffic_key(SSL *ssl, enum evp_aead_direction_t direction);
  699. /* tls13_derive_application_secrets derives the initial application data traffic
  700. * and exporter secrets based on the handshake transcripts and |master_secret|.
  701. * It returns one on success and zero on error. */
  702. int tls13_derive_application_secrets(SSL_HANDSHAKE *hs);
  703. /* tls13_derive_resumption_secret derives the |resumption_secret|. */
  704. int tls13_derive_resumption_secret(SSL_HANDSHAKE *hs);
  705. /* tls13_export_keying_material provides an exporter interface to use the
  706. * |exporter_secret|. */
  707. int tls13_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len,
  708. const char *label, size_t label_len,
  709. const uint8_t *context, size_t context_len,
  710. int use_context);
  711. /* tls13_finished_mac calculates the MAC of the handshake transcript to verify
  712. * the integrity of the Finished message, and stores the result in |out| and
  713. * length in |out_len|. |is_server| is 1 if this is for the Server Finished and
  714. * 0 for the Client Finished. */
  715. int tls13_finished_mac(SSL_HANDSHAKE *hs, uint8_t *out,
  716. size_t *out_len, int is_server);
  717. /* tls13_write_psk_binder calculates the PSK binder value and replaces the last
  718. * bytes of |msg| with the resulting value. It returns 1 on success, and 0 on
  719. * failure. */
  720. int tls13_write_psk_binder(SSL *ssl, uint8_t *msg, size_t len);
  721. /* tls13_verify_psk_binder verifies that the handshake transcript, truncated
  722. * up to the binders has a valid signature using the value of |session|'s
  723. * resumption secret. It returns 1 on success, and 0 on failure. */
  724. int tls13_verify_psk_binder(SSL *ssl, SSL_SESSION *session, CBS *binders);
  725. /* Handshake functions. */
  726. enum ssl_hs_wait_t {
  727. ssl_hs_error,
  728. ssl_hs_ok,
  729. ssl_hs_read_message,
  730. ssl_hs_write_message,
  731. ssl_hs_flush,
  732. ssl_hs_flush_and_read_message,
  733. ssl_hs_x509_lookup,
  734. ssl_hs_channel_id_lookup,
  735. ssl_hs_private_key_operation,
  736. };
  737. struct ssl_handshake_st {
  738. /* ssl is a non-owning pointer to the parent |SSL| object. */
  739. SSL *ssl;
  740. /* do_tls13_handshake runs the TLS 1.3 handshake. On completion, it returns
  741. * |ssl_hs_ok|. Otherwise, it returns a value corresponding to what operation
  742. * is needed to progress. */
  743. enum ssl_hs_wait_t (*do_tls13_handshake)(SSL_HANDSHAKE *hs);
  744. /* wait contains the operation |do_tls13_handshake| is currently blocking on
  745. * or |ssl_hs_ok| if none. */
  746. enum ssl_hs_wait_t wait;
  747. /* state contains one of the SSL3_ST_* values. */
  748. int state;
  749. /* next_state is used when SSL_ST_FLUSH_DATA is entered */
  750. int next_state;
  751. /* tls13_state is the internal state for the TLS 1.3 handshake. Its values
  752. * depend on |do_tls13_handshake| but the starting state is always zero. */
  753. int tls13_state;
  754. size_t hash_len;
  755. uint8_t secret[EVP_MAX_MD_SIZE];
  756. uint8_t client_traffic_secret_0[EVP_MAX_MD_SIZE];
  757. uint8_t server_traffic_secret_0[EVP_MAX_MD_SIZE];
  758. union {
  759. /* sent is a bitset where the bits correspond to elements of kExtensions
  760. * in t1_lib.c. Each bit is set if that extension was sent in a
  761. * ClientHello. It's not used by servers. */
  762. uint32_t sent;
  763. /* received is a bitset, like |sent|, but is used by servers to record
  764. * which extensions were received from a client. */
  765. uint32_t received;
  766. } extensions;
  767. union {
  768. /* sent is a bitset where the bits correspond to elements of
  769. * |client_custom_extensions| in the |SSL_CTX|. Each bit is set if that
  770. * extension was sent in a ClientHello. It's not used by servers. */
  771. uint16_t sent;
  772. /* received is a bitset, like |sent|, but is used by servers to record
  773. * which custom extensions were received from a client. The bits here
  774. * correspond to |server_custom_extensions|. */
  775. uint16_t received;
  776. } custom_extensions;
  777. /* retry_group is the group ID selected by the server in HelloRetryRequest in
  778. * TLS 1.3. */
  779. uint16_t retry_group;
  780. /* ecdh_ctx is the current ECDH instance. */
  781. SSL_ECDH_CTX ecdh_ctx;
  782. /* cookie is the value of the cookie received from the server, if any. */
  783. uint8_t *cookie;
  784. size_t cookie_len;
  785. /* key_share_bytes is the value of the previously sent KeyShare extension by
  786. * the client in TLS 1.3. */
  787. uint8_t *key_share_bytes;
  788. size_t key_share_bytes_len;
  789. /* public_key, for servers, is the key share to be sent to the client in TLS
  790. * 1.3. */
  791. uint8_t *public_key;
  792. size_t public_key_len;
  793. /* peer_sigalgs are the signature algorithms that the peer supports. These are
  794. * taken from the contents of the signature algorithms extension for a server
  795. * or from the CertificateRequest for a client. */
  796. uint16_t *peer_sigalgs;
  797. /* num_peer_sigalgs is the number of entries in |peer_sigalgs|. */
  798. size_t num_peer_sigalgs;
  799. /* peer_supported_group_list contains the supported group IDs advertised by
  800. * the peer. This is only set on the server's end. The server does not
  801. * advertise this extension to the client. */
  802. uint16_t *peer_supported_group_list;
  803. size_t peer_supported_group_list_len;
  804. /* peer_key is the peer's ECDH key for a TLS 1.2 client. */
  805. uint8_t *peer_key;
  806. size_t peer_key_len;
  807. /* server_params, in TLS 1.2, stores the ServerKeyExchange parameters to be
  808. * signed while the signature is being computed. */
  809. uint8_t *server_params;
  810. size_t server_params_len;
  811. /* peer_psk_identity_hint, on the client, is the psk_identity_hint sent by the
  812. * server when using a TLS 1.2 PSK key exchange. */
  813. char *peer_psk_identity_hint;
  814. /* ca_names, on the client, contains the list of CAs received in a
  815. * CertificateRequest message. */
  816. STACK_OF(X509_NAME) *ca_names;
  817. /* certificate_types, on the client, contains the set of certificate types
  818. * received in a CertificateRequest message. */
  819. uint8_t *certificate_types;
  820. size_t num_certificate_types;
  821. /* hostname, on the server, is the value of the SNI extension. */
  822. char *hostname;
  823. /* peer_pubkey is the public key parsed from the peer's leaf certificate. */
  824. EVP_PKEY *peer_pubkey;
  825. /* key_block is the record-layer key block for TLS 1.2 and earlier. */
  826. uint8_t *key_block;
  827. uint8_t key_block_len;
  828. /* session_tickets_sent, in TLS 1.3, is the number of tickets the server has
  829. * sent. */
  830. uint8_t session_tickets_sent;
  831. /* scts_requested is one if the SCT extension is in the ClientHello. */
  832. unsigned scts_requested:1;
  833. /* needs_psk_binder if the ClientHello has a placeholder PSK binder to be
  834. * filled in. */
  835. unsigned needs_psk_binder:1;
  836. unsigned received_hello_retry_request:1;
  837. /* accept_psk_mode stores whether the client's PSK mode is compatible with our
  838. * preferences. */
  839. unsigned accept_psk_mode:1;
  840. /* cert_request is one if a client certificate was requested and zero
  841. * otherwise. */
  842. unsigned cert_request:1;
  843. /* certificate_status_expected is one if OCSP stapling was negotiated and the
  844. * server is expected to send a CertificateStatus message. (This is used on
  845. * both the client and server sides.) */
  846. unsigned certificate_status_expected:1;
  847. /* ocsp_stapling_requested is one if a client requested OCSP stapling. */
  848. unsigned ocsp_stapling_requested:1;
  849. /* should_ack_sni is used by a server and indicates that the SNI extension
  850. * should be echoed in the ServerHello. */
  851. unsigned should_ack_sni:1;
  852. /* in_false_start is one if there is a pending client handshake in False
  853. * Start. The client may write data at this point. */
  854. unsigned in_false_start:1;
  855. /* next_proto_neg_seen is one of NPN was negotiated. */
  856. unsigned next_proto_neg_seen:1;
  857. /* ticket_expected is one if a TLS 1.2 NewSessionTicket message is to be sent
  858. * or received. */
  859. unsigned ticket_expected:1;
  860. /* client_version is the value sent or received in the ClientHello version. */
  861. uint16_t client_version;
  862. } /* SSL_HANDSHAKE */;
  863. SSL_HANDSHAKE *ssl_handshake_new(SSL *ssl);
  864. /* ssl_handshake_free releases all memory associated with |hs|. */
  865. void ssl_handshake_free(SSL_HANDSHAKE *hs);
  866. /* tls13_handshake runs the TLS 1.3 handshake. It returns one on success and <=
  867. * 0 on error. */
  868. int tls13_handshake(SSL_HANDSHAKE *hs);
  869. /* The following are implementations of |do_tls13_handshake| for the client and
  870. * server. */
  871. enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs);
  872. enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs);
  873. /* tls13_post_handshake processes a post-handshake message. It returns one on
  874. * success and zero on failure. */
  875. int tls13_post_handshake(SSL *ssl);
  876. /* tls13_check_message_type checks if the current message has type |type|. If so
  877. * it returns one. Otherwise, it sends an alert and returns zero. */
  878. int tls13_check_message_type(SSL *ssl, int type);
  879. int tls13_process_certificate(SSL *ssl, int allow_anonymous);
  880. int tls13_process_certificate_verify(SSL *ssl);
  881. int tls13_process_finished(SSL_HANDSHAKE *hs);
  882. int tls13_prepare_certificate(SSL_HANDSHAKE *hs);
  883. enum ssl_private_key_result_t tls13_prepare_certificate_verify(
  884. SSL_HANDSHAKE *hs, int is_first_run);
  885. int tls13_prepare_finished(SSL_HANDSHAKE *hs);
  886. int tls13_process_new_session_ticket(SSL *ssl);
  887. int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
  888. size_t *out_secret_len,
  889. uint8_t *out_alert, CBS *contents);
  890. int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
  891. uint8_t **out_secret,
  892. size_t *out_secret_len,
  893. uint8_t *out_alert, CBS *contents);
  894. int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out);
  895. int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
  896. uint8_t *out_alert, CBS *contents);
  897. int ssl_ext_pre_shared_key_parse_clienthello(SSL_HANDSHAKE *hs,
  898. SSL_SESSION **out_session,
  899. CBS *out_binders,
  900. uint8_t *out_alert, CBS *contents);
  901. int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out);
  902. /* ssl_is_sct_list_valid does a shallow parse of the SCT list in |contents| and
  903. * returns one iff it's valid. */
  904. int ssl_is_sct_list_valid(const CBS *contents);
  905. int ssl_write_client_hello(SSL_HANDSHAKE *hs);
  906. /* ssl_clear_tls13_state releases client state only needed for TLS 1.3. It
  907. * should be called once the version is known to be TLS 1.2 or earlier. */
  908. void ssl_clear_tls13_state(SSL_HANDSHAKE *hs);
  909. enum ssl_cert_verify_context_t {
  910. ssl_cert_verify_server,
  911. ssl_cert_verify_client,
  912. ssl_cert_verify_channel_id,
  913. };
  914. /* tls13_get_cert_verify_signature_input generates the message to be signed for
  915. * TLS 1.3's CertificateVerify message. |cert_verify_context| determines the
  916. * type of signature. It sets |*out| and |*out_len| to a newly allocated buffer
  917. * containing the result. The caller must free it with |OPENSSL_free| to release
  918. * it. This function returns one on success and zero on failure. */
  919. int tls13_get_cert_verify_signature_input(
  920. SSL *ssl, uint8_t **out, size_t *out_len,
  921. enum ssl_cert_verify_context_t cert_verify_context);
  922. /* ssl_negotiate_alpn negotiates the ALPN extension, if applicable. It returns
  923. * one on successful negotiation or if nothing was negotiated. It returns zero
  924. * and sets |*out_alert| to an alert on error. */
  925. int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  926. const SSL_CLIENT_HELLO *client_hello);
  927. typedef struct {
  928. uint16_t type;
  929. int *out_present;
  930. CBS *out_data;
  931. } SSL_EXTENSION_TYPE;
  932. /* ssl_parse_extensions parses a TLS extensions block out of |cbs| and advances
  933. * it. It writes the parsed extensions to pointers denoted by |ext_types|. On
  934. * success, it fills in the |out_present| and |out_data| fields and returns one.
  935. * Otherwise, it sets |*out_alert| to an alert to send and returns zero. Unknown
  936. * extensions are rejected. */
  937. int ssl_parse_extensions(const CBS *cbs, uint8_t *out_alert,
  938. const SSL_EXTENSION_TYPE *ext_types,
  939. size_t num_ext_types);
  940. /* SSLKEYLOGFILE functions. */
  941. /* ssl_log_rsa_client_key_exchange logs |premaster|, if logging is enabled for
  942. * |ssl|. It returns one on success and zero on failure. The entry is identified
  943. * by the first 8 bytes of |encrypted_premaster|. */
  944. int ssl_log_rsa_client_key_exchange(const SSL *ssl,
  945. const uint8_t *encrypted_premaster,
  946. size_t encrypted_premaster_len,
  947. const uint8_t *premaster,
  948. size_t premaster_len);
  949. /* ssl_log_secret logs |secret| with label |label|, if logging is enabled for
  950. * |ssl|. It returns one on success and zero on failure. */
  951. int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret,
  952. size_t secret_len);
  953. /* ClientHello functions. */
  954. int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out, const uint8_t *in,
  955. size_t in_len);
  956. int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
  957. CBS *out, uint16_t extension_type);
  958. int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
  959. uint16_t id);
  960. /* GREASE. */
  961. enum ssl_grease_index_t {
  962. ssl_grease_cipher = 0,
  963. ssl_grease_group,
  964. ssl_grease_extension1,
  965. ssl_grease_extension2,
  966. ssl_grease_version,
  967. ssl_grease_ticket_extension,
  968. };
  969. /* ssl_get_grease_value returns a GREASE value for |ssl|. For a given
  970. * connection, the values for each index will be deterministic. This allows the
  971. * same ClientHello be sent twice for a HelloRetryRequest or the same group be
  972. * advertised in both supported_groups and key_shares. */
  973. uint16_t ssl_get_grease_value(const SSL *ssl, enum ssl_grease_index_t index);
  974. /* Signature algorithms. */
  975. /* tls1_parse_peer_sigalgs parses |sigalgs| as the list of peer signature
  976. * algorithms and saves them on |hs|. It returns one on success and zero on
  977. * error. */
  978. int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *sigalgs);
  979. /* tls1_choose_signature_algorithm sets |*out| to a signature algorithm for use
  980. * with |hs|'s private key based on the peer's preferences and the algorithms
  981. * supported. It returns one on success and zero on error. */
  982. int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out);
  983. /* tls12_get_verify_sigalgs sets |*out| to the signature algorithms acceptable
  984. * for the peer signature and returns the length of the list. */
  985. size_t tls12_get_verify_sigalgs(const SSL *ssl, const uint16_t **out);
  986. /* tls12_check_peer_sigalg checks if |sigalg| is acceptable for the peer
  987. * signature. It returns one on success and zero on error, setting |*out_alert|
  988. * to an alert to send. */
  989. int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg);
  990. /* Underdocumented functions.
  991. *
  992. * Functions below here haven't been touched up and may be underdocumented. */
  993. #define TLSEXT_CHANNEL_ID_SIZE 128
  994. /* From RFC4492, used in encoding the curve type in ECParameters */
  995. #define NAMED_CURVE_TYPE 3
  996. enum ssl_hash_message_t {
  997. ssl_dont_hash_message,
  998. ssl_hash_message,
  999. };
  1000. typedef struct cert_st {
  1001. EVP_PKEY *privatekey;
  1002. X509 *x509_leaf;
  1003. STACK_OF(X509) *x509_chain;
  1004. /* key_method, if non-NULL, is a set of callbacks to call for private key
  1005. * operations. */
  1006. const SSL_PRIVATE_KEY_METHOD *key_method;
  1007. /* For clients the following masks are of *disabled* key and auth algorithms
  1008. * based on the current configuration.
  1009. *
  1010. * TODO(davidben): Remove these. They get checked twice: when sending the
  1011. * ClientHello and when processing the ServerHello. */
  1012. uint32_t mask_k;
  1013. uint32_t mask_a;
  1014. DH *dh_tmp;
  1015. DH *(*dh_tmp_cb)(SSL *ssl, int is_export, int keysize);
  1016. /* sigalgs, if non-NULL, is the set of signature algorithms supported by
  1017. * |privatekey| in decreasing order of preference. */
  1018. uint16_t *sigalgs;
  1019. size_t num_sigalgs;
  1020. /* Certificate setup callback: if set is called whenever a
  1021. * certificate may be required (client or server). the callback
  1022. * can then examine any appropriate parameters and setup any
  1023. * certificates required. This allows advanced applications
  1024. * to select certificates on the fly: for example based on
  1025. * supported signature algorithms or curves. */
  1026. int (*cert_cb)(SSL *ssl, void *arg);
  1027. void *cert_cb_arg;
  1028. /* Optional X509_STORE for certificate validation. If NULL the parent SSL_CTX
  1029. * store is used instead. */
  1030. X509_STORE *verify_store;
  1031. } CERT;
  1032. /* SSL_METHOD is a compatibility structure to support the legacy version-locked
  1033. * methods. */
  1034. struct ssl_method_st {
  1035. /* version, if non-zero, is the only protocol version acceptable to an
  1036. * SSL_CTX initialized from this method. */
  1037. uint16_t version;
  1038. /* method is the underlying SSL_PROTOCOL_METHOD that initializes the
  1039. * SSL_CTX. */
  1040. const SSL_PROTOCOL_METHOD *method;
  1041. };
  1042. /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
  1043. struct ssl_protocol_method_st {
  1044. /* is_dtls is one if the protocol is DTLS and zero otherwise. */
  1045. char is_dtls;
  1046. /* min_version is the minimum implemented version. */
  1047. uint16_t min_version;
  1048. /* max_version is the maximum implemented version. */
  1049. uint16_t max_version;
  1050. /* version_from_wire maps |wire_version| to a protocol version. On success, it
  1051. * sets |*out_version| to the result and returns one. If the version is
  1052. * unknown, it returns zero. */
  1053. int (*version_from_wire)(uint16_t *out_version, uint16_t wire_version);
  1054. /* version_to_wire maps |version| to the wire representation. It is an error
  1055. * to call it with an invalid version. */
  1056. uint16_t (*version_to_wire)(uint16_t version);
  1057. int (*ssl_new)(SSL *ssl);
  1058. void (*ssl_free)(SSL *ssl);
  1059. /* ssl_get_message reads the next handshake message. If |msg_type| is not -1,
  1060. * the message must have the specified type. On success, it returns one and
  1061. * sets |ssl->s3->tmp.message_type|, |ssl->init_msg|, and |ssl->init_num|.
  1062. * Otherwise, it returns <= 0. */
  1063. int (*ssl_get_message)(SSL *ssl, int msg_type,
  1064. enum ssl_hash_message_t hash_message);
  1065. /* get_current_message sets |*out| to the current handshake message. This
  1066. * includes the protocol-specific message header. */
  1067. void (*get_current_message)(const SSL *ssl, CBS *out);
  1068. /* release_current_message is called to release the current handshake message.
  1069. * If |free_buffer| is one, buffers will also be released. */
  1070. void (*release_current_message)(SSL *ssl, int free_buffer);
  1071. /* read_app_data reads up to |len| bytes of application data into |buf|. On
  1072. * success, it returns the number of bytes read. Otherwise, it returns <= 0
  1073. * and sets |*out_got_handshake| to whether the failure was due to a
  1074. * post-handshake handshake message. If so, it fills in the current message as
  1075. * in |ssl_get_message|. */
  1076. int (*read_app_data)(SSL *ssl, int *out_got_handshake, uint8_t *buf, int len,
  1077. int peek);
  1078. int (*read_change_cipher_spec)(SSL *ssl);
  1079. void (*read_close_notify)(SSL *ssl);
  1080. int (*write_app_data)(SSL *ssl, const void *buf_, int len);
  1081. int (*dispatch_alert)(SSL *ssl);
  1082. /* supports_cipher returns one if |cipher| is supported by this protocol and
  1083. * zero otherwise. */
  1084. int (*supports_cipher)(const SSL_CIPHER *cipher);
  1085. /* init_message begins a new handshake message of type |type|. |cbb| is the
  1086. * root CBB to be passed into |finish_message|. |*body| is set to a child CBB
  1087. * the caller should write to. It returns one on success and zero on error. */
  1088. int (*init_message)(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
  1089. /* finish_message finishes a handshake message. It sets |*out_msg| to a
  1090. * newly-allocated buffer with the serialized message. The caller must
  1091. * release it with |OPENSSL_free| when done. It returns one on success and
  1092. * zero on error. */
  1093. int (*finish_message)(SSL *ssl, CBB *cbb, uint8_t **out_msg, size_t *out_len);
  1094. /* queue_message queues a handshake message and prepares it to be written. It
  1095. * takes ownership of |msg| and releases it with |OPENSSL_free| when done. It
  1096. * returns one on success and zero on error. */
  1097. int (*queue_message)(SSL *ssl, uint8_t *msg, size_t len);
  1098. /* write_message writes the next message to the transport. It returns one on
  1099. * success and <= 0 on error. */
  1100. int (*write_message)(SSL *ssl);
  1101. /* send_change_cipher_spec sends a ChangeCipherSpec message. */
  1102. int (*send_change_cipher_spec)(SSL *ssl);
  1103. /* expect_flight is called when the handshake expects a flight of messages from
  1104. * the peer. */
  1105. void (*expect_flight)(SSL *ssl);
  1106. /* received_flight is called when the handshake has received a flight of
  1107. * messages from the peer. */
  1108. void (*received_flight)(SSL *ssl);
  1109. /* set_read_state sets |ssl|'s read cipher state to |aead_ctx|. It takes
  1110. * ownership of |aead_ctx|. It returns one on success and zero if changing the
  1111. * read state is forbidden at this point. */
  1112. int (*set_read_state)(SSL *ssl, SSL_AEAD_CTX *aead_ctx);
  1113. /* set_write_state sets |ssl|'s write cipher state to |aead_ctx|. It takes
  1114. * ownership of |aead_ctx|. It returns one on success and zero if changing the
  1115. * write state is forbidden at this point. */
  1116. int (*set_write_state)(SSL *ssl, SSL_AEAD_CTX *aead_ctx);
  1117. };
  1118. /* This is for the SSLv3/TLSv1.0 differences in crypto/hash stuff It is a bit
  1119. * of a mess of functions, but hell, think of it as an opaque structure. */
  1120. typedef struct ssl3_enc_method {
  1121. /* prf computes the PRF function for |ssl|. It writes |out_len| bytes to
  1122. * |out|, using |secret| as the secret and |label| as the label. |seed1| and
  1123. * |seed2| are concatenated to form the seed parameter. It returns one on
  1124. * success and zero on failure. */
  1125. int (*prf)(const SSL *ssl, uint8_t *out, size_t out_len,
  1126. const uint8_t *secret, size_t secret_len, const char *label,
  1127. size_t label_len, const uint8_t *seed1, size_t seed1_len,
  1128. const uint8_t *seed2, size_t seed2_len);
  1129. int (*final_finish_mac)(SSL *ssl, int from_server, uint8_t *out);
  1130. } SSL3_ENC_METHOD;
  1131. typedef struct ssl3_record_st {
  1132. /* type is the record type. */
  1133. uint8_t type;
  1134. /* length is the number of unconsumed bytes in the record. */
  1135. uint16_t length;
  1136. /* data is a non-owning pointer to the first unconsumed byte of the record. */
  1137. uint8_t *data;
  1138. } SSL3_RECORD;
  1139. typedef struct ssl3_buffer_st {
  1140. /* buf is the memory allocated for this buffer. */
  1141. uint8_t *buf;
  1142. /* offset is the offset into |buf| which the buffer contents start at. */
  1143. uint16_t offset;
  1144. /* len is the length of the buffer contents from |buf| + |offset|. */
  1145. uint16_t len;
  1146. /* cap is how much memory beyond |buf| + |offset| is available. */
  1147. uint16_t cap;
  1148. } SSL3_BUFFER;
  1149. /* An ssl_shutdown_t describes the shutdown state of one end of the connection,
  1150. * whether it is alive or has been shutdown via close_notify or fatal alert. */
  1151. enum ssl_shutdown_t {
  1152. ssl_shutdown_none = 0,
  1153. ssl_shutdown_close_notify = 1,
  1154. ssl_shutdown_fatal_alert = 2,
  1155. };
  1156. typedef struct ssl3_state_st {
  1157. uint8_t read_sequence[8];
  1158. uint8_t write_sequence[8];
  1159. uint8_t server_random[SSL3_RANDOM_SIZE];
  1160. uint8_t client_random[SSL3_RANDOM_SIZE];
  1161. /* read_buffer holds data from the transport to be processed. */
  1162. SSL3_BUFFER read_buffer;
  1163. /* write_buffer holds data to be written to the transport. */
  1164. SSL3_BUFFER write_buffer;
  1165. SSL3_RECORD rrec; /* each decoded record goes in here */
  1166. /* partial write - check the numbers match */
  1167. unsigned int wnum; /* number of bytes sent so far */
  1168. int wpend_tot; /* number bytes written */
  1169. int wpend_type;
  1170. int wpend_ret; /* number of bytes submitted */
  1171. const uint8_t *wpend_buf;
  1172. /* handshake_buffer, if non-NULL, contains the handshake transcript. */
  1173. BUF_MEM *handshake_buffer;
  1174. /* handshake_hash, if initialized with an |EVP_MD|, maintains the handshake
  1175. * hash. For TLS 1.1 and below, it is the SHA-1 half. */
  1176. EVP_MD_CTX handshake_hash;
  1177. /* handshake_md5, if initialized with an |EVP_MD|, maintains the MD5 half of
  1178. * the handshake hash for TLS 1.1 and below. */
  1179. EVP_MD_CTX handshake_md5;
  1180. /* recv_shutdown is the shutdown state for the receive half of the
  1181. * connection. */
  1182. enum ssl_shutdown_t recv_shutdown;
  1183. /* recv_shutdown is the shutdown state for the send half of the connection. */
  1184. enum ssl_shutdown_t send_shutdown;
  1185. int alert_dispatch;
  1186. int total_renegotiations;
  1187. /* early_data_skipped is the amount of early data that has been skipped by the
  1188. * record layer. */
  1189. uint16_t early_data_skipped;
  1190. /* empty_record_count is the number of consecutive empty records received. */
  1191. uint8_t empty_record_count;
  1192. /* warning_alert_count is the number of consecutive warning alerts
  1193. * received. */
  1194. uint8_t warning_alert_count;
  1195. /* key_update_count is the number of consecutive KeyUpdates received. */
  1196. uint8_t key_update_count;
  1197. /* skip_early_data instructs the record layer to skip unexpected early data
  1198. * messages when 0RTT is rejected. */
  1199. unsigned skip_early_data:1;
  1200. /* have_version is true if the connection's final version is known. Otherwise
  1201. * the version has not been negotiated yet. */
  1202. unsigned have_version:1;
  1203. /* v2_hello_done is true if the peer's V2ClientHello, if any, has been handled
  1204. * and future messages should use the record layer. */
  1205. unsigned v2_hello_done:1;
  1206. /* initial_handshake_complete is true if the initial handshake has
  1207. * completed. */
  1208. unsigned initial_handshake_complete:1;
  1209. /* session_reused indicates whether a session was resumed. */
  1210. unsigned session_reused:1;
  1211. unsigned send_connection_binding:1;
  1212. /* In a client, this means that the server supported Channel ID and that a
  1213. * Channel ID was sent. In a server it means that we echoed support for
  1214. * Channel IDs and that tlsext_channel_id will be valid after the
  1215. * handshake. */
  1216. unsigned tlsext_channel_id_valid:1;
  1217. uint8_t send_alert[2];
  1218. /* pending_message is the current outgoing handshake message. */
  1219. uint8_t *pending_message;
  1220. uint32_t pending_message_len;
  1221. /* aead_read_ctx is the current read cipher state. */
  1222. SSL_AEAD_CTX *aead_read_ctx;
  1223. /* aead_write_ctx is the current write cipher state. */
  1224. SSL_AEAD_CTX *aead_write_ctx;
  1225. /* enc_method is the method table corresponding to the current protocol
  1226. * version. */
  1227. const SSL3_ENC_METHOD *enc_method;
  1228. /* hs is the handshake state for the current handshake or NULL if there isn't
  1229. * one. */
  1230. SSL_HANDSHAKE *hs;
  1231. uint8_t write_traffic_secret[EVP_MAX_MD_SIZE];
  1232. uint8_t read_traffic_secret[EVP_MAX_MD_SIZE];
  1233. uint8_t exporter_secret[EVP_MAX_MD_SIZE];
  1234. uint8_t write_traffic_secret_len;
  1235. uint8_t read_traffic_secret_len;
  1236. uint8_t exporter_secret_len;
  1237. /* Connection binding to prevent renegotiation attacks */
  1238. uint8_t previous_client_finished[12];
  1239. uint8_t previous_client_finished_len;
  1240. uint8_t previous_server_finished_len;
  1241. uint8_t previous_server_finished[12];
  1242. /* State pertaining to the pending handshake.
  1243. *
  1244. * TODO(davidben): Move everything not needed after the handshake completes to
  1245. * |hs| and remove this. */
  1246. struct {
  1247. /* used to hold the new cipher we are going to use */
  1248. const SSL_CIPHER *new_cipher;
  1249. int message_type;
  1250. int reuse_message;
  1251. uint8_t new_mac_secret_len;
  1252. uint8_t new_key_len;
  1253. uint8_t new_fixed_iv_len;
  1254. /* extended_master_secret indicates whether the extended master secret
  1255. * computation is used in this handshake. Note that this is different from
  1256. * whether it was used for the current session. If this is a resumption
  1257. * handshake then EMS might be negotiated in the client and server hello
  1258. * messages, but it doesn't matter if the session that's being resumed
  1259. * didn't use it to create the master secret initially. */
  1260. char extended_master_secret;
  1261. /* peer_signature_algorithm is the signature algorithm used to authenticate
  1262. * the peer, or zero if not applicable. */
  1263. uint16_t peer_signature_algorithm;
  1264. } tmp;
  1265. /* new_session is the new mutable session being established by the current
  1266. * handshake. It should not be cached. */
  1267. SSL_SESSION *new_session;
  1268. /* established_session is the session established by the connection. This
  1269. * session is only filled upon the completion of the handshake and is
  1270. * immutable. */
  1271. SSL_SESSION *established_session;
  1272. /* Next protocol negotiation. For the client, this is the protocol that we
  1273. * sent in NextProtocol and is set when handling ServerHello extensions.
  1274. *
  1275. * For a server, this is the client's selected_protocol from NextProtocol and
  1276. * is set when handling the NextProtocol message, before the Finished
  1277. * message. */
  1278. uint8_t *next_proto_negotiated;
  1279. size_t next_proto_negotiated_len;
  1280. /* ALPN information
  1281. * (we are in the process of transitioning from NPN to ALPN.) */
  1282. /* In a server these point to the selected ALPN protocol after the
  1283. * ClientHello has been processed. In a client these contain the protocol
  1284. * that the server selected once the ServerHello has been processed. */
  1285. uint8_t *alpn_selected;
  1286. size_t alpn_selected_len;
  1287. /* For a server:
  1288. * If |tlsext_channel_id_valid| is true, then this contains the
  1289. * verified Channel ID from the client: a P256 point, (x,y), where
  1290. * each are big-endian values. */
  1291. uint8_t tlsext_channel_id[64];
  1292. } SSL3_STATE;
  1293. /* lengths of messages */
  1294. #define DTLS1_COOKIE_LENGTH 256
  1295. #define DTLS1_RT_HEADER_LENGTH 13
  1296. #define DTLS1_HM_HEADER_LENGTH 12
  1297. #define DTLS1_CCS_HEADER_LENGTH 1
  1298. #define DTLS1_AL_HEADER_LENGTH 2
  1299. struct hm_header_st {
  1300. uint8_t type;
  1301. uint32_t msg_len;
  1302. uint16_t seq;
  1303. uint32_t frag_off;
  1304. uint32_t frag_len;
  1305. };
  1306. /* An hm_fragment is an incoming DTLS message, possibly not yet assembled. */
  1307. typedef struct hm_fragment_st {
  1308. /* type is the type of the message. */
  1309. uint8_t type;
  1310. /* seq is the sequence number of this message. */
  1311. uint16_t seq;
  1312. /* msg_len is the length of the message body. */
  1313. uint32_t msg_len;
  1314. /* data is a pointer to the message, including message header. It has length
  1315. * |DTLS1_HM_HEADER_LENGTH| + |msg_len|. */
  1316. uint8_t *data;
  1317. /* reassembly is a bitmask of |msg_len| bits corresponding to which parts of
  1318. * the message have been received. It is NULL if the message is complete. */
  1319. uint8_t *reassembly;
  1320. } hm_fragment;
  1321. typedef struct dtls1_state_st {
  1322. /* send_cookie is true if we are resending the ClientHello
  1323. * with a cookie from a HelloVerifyRequest. */
  1324. unsigned int send_cookie;
  1325. uint8_t cookie[DTLS1_COOKIE_LENGTH];
  1326. size_t cookie_len;
  1327. /* The current data and handshake epoch. This is initially undefined, and
  1328. * starts at zero once the initial handshake is completed. */
  1329. uint16_t r_epoch;
  1330. uint16_t w_epoch;
  1331. /* records being received in the current epoch */
  1332. DTLS1_BITMAP bitmap;
  1333. uint16_t handshake_write_seq;
  1334. uint16_t handshake_read_seq;
  1335. /* save last sequence number for retransmissions */
  1336. uint8_t last_write_sequence[8];
  1337. /* incoming_messages is a ring buffer of incoming handshake messages that have
  1338. * yet to be processed. The front of the ring buffer is message number
  1339. * |handshake_read_seq|, at position |handshake_read_seq| %
  1340. * |SSL_MAX_HANDSHAKE_FLIGHT|. */
  1341. hm_fragment *incoming_messages[SSL_MAX_HANDSHAKE_FLIGHT];
  1342. /* outgoing_messages is the queue of outgoing messages from the last handshake
  1343. * flight. */
  1344. DTLS_OUTGOING_MESSAGE outgoing_messages[SSL_MAX_HANDSHAKE_FLIGHT];
  1345. uint8_t outgoing_messages_len;
  1346. unsigned int mtu; /* max DTLS packet size */
  1347. /* num_timeouts is the number of times the retransmit timer has fired since
  1348. * the last time it was reset. */
  1349. unsigned int num_timeouts;
  1350. /* Indicates when the last handshake msg or heartbeat sent will
  1351. * timeout. */
  1352. struct timeval next_timeout;
  1353. /* timeout_duration_ms is the timeout duration in milliseconds. */
  1354. unsigned timeout_duration_ms;
  1355. } DTLS1_STATE;
  1356. extern const SSL3_ENC_METHOD TLSv1_enc_data;
  1357. extern const SSL3_ENC_METHOD SSLv3_enc_data;
  1358. /* From draft-ietf-tls-tls13-18, used in determining PSK modes. */
  1359. #define SSL_PSK_KE 0x0
  1360. #define SSL_PSK_DHE_KE 0x1
  1361. /* From draft-ietf-tls-tls13-16, used in determining whether to respond with a
  1362. * KeyUpdate. */
  1363. #define SSL_KEY_UPDATE_NOT_REQUESTED 0
  1364. #define SSL_KEY_UPDATE_REQUESTED 1
  1365. CERT *ssl_cert_new(void);
  1366. CERT *ssl_cert_dup(CERT *cert);
  1367. void ssl_cert_clear_certs(CERT *c);
  1368. void ssl_cert_free(CERT *c);
  1369. int ssl_get_new_session(SSL_HANDSHAKE *hs, int is_server);
  1370. int ssl_encrypt_ticket(SSL *ssl, CBB *out, const SSL_SESSION *session);
  1371. /* ssl_session_is_context_valid returns one if |session|'s session ID context
  1372. * matches the one set on |ssl| and zero otherwise. */
  1373. int ssl_session_is_context_valid(const SSL *ssl, const SSL_SESSION *session);
  1374. /* ssl_session_is_time_valid returns one if |session| is still valid and zero if
  1375. * it has expired. */
  1376. int ssl_session_is_time_valid(const SSL *ssl, const SSL_SESSION *session);
  1377. /* ssl_session_is_resumable returns one if |session| is resumable for |ssl| and
  1378. * zero otherwise. */
  1379. int ssl_session_is_resumable(const SSL *ssl, const SSL_SESSION *session);
  1380. void ssl_set_session(SSL *ssl, SSL_SESSION *session);
  1381. enum ssl_session_result_t {
  1382. ssl_session_success,
  1383. ssl_session_error,
  1384. ssl_session_retry,
  1385. };
  1386. /* ssl_get_prev_session looks up the previous session based on |client_hello|.
  1387. * On success, it sets |*out_session| to the session or NULL if none was found.
  1388. * If the session could not be looked up synchronously, it returns
  1389. * |ssl_session_retry| and should be called again. Otherwise, it returns
  1390. * |ssl_session_error|. */
  1391. enum ssl_session_result_t ssl_get_prev_session(
  1392. SSL *ssl, SSL_SESSION **out_session, int *out_tickets_supported,
  1393. int *out_renew_ticket, const SSL_CLIENT_HELLO *client_hello);
  1394. /* The following flags determine which parts of the session are duplicated. */
  1395. #define SSL_SESSION_DUP_AUTH_ONLY 0x0
  1396. #define SSL_SESSION_INCLUDE_TICKET 0x1
  1397. #define SSL_SESSION_INCLUDE_NONAUTH 0x2
  1398. #define SSL_SESSION_DUP_ALL \
  1399. (SSL_SESSION_INCLUDE_TICKET | SSL_SESSION_INCLUDE_NONAUTH)
  1400. /* SSL_SESSION_dup returns a newly-allocated |SSL_SESSION| with a copy of the
  1401. * fields in |session| or NULL on error. The new session is non-resumable and
  1402. * must be explicitly marked resumable once it has been filled in. */
  1403. OPENSSL_EXPORT SSL_SESSION *SSL_SESSION_dup(SSL_SESSION *session,
  1404. int dup_flags);
  1405. /* ssl_session_refresh_time updates |session|'s start time to the current time,
  1406. * adjusting the timeout so the expiration time is unchanged. */
  1407. void ssl_session_refresh_time(SSL *ssl, SSL_SESSION *session);
  1408. void ssl_cipher_preference_list_free(
  1409. struct ssl_cipher_preference_list_st *cipher_list);
  1410. /* ssl_get_cipher_preferences returns the cipher preference list for TLS 1.2 and
  1411. * below. */
  1412. const struct ssl_cipher_preference_list_st *ssl_get_cipher_preferences(
  1413. const SSL *ssl);
  1414. int ssl_verify_cert_chain(SSL *ssl, long *out_verify_result,
  1415. STACK_OF(X509) * cert_chain);
  1416. void ssl_update_cache(SSL_HANDSHAKE *hs, int mode);
  1417. int ssl_verify_alarm_type(long type);
  1418. int ssl3_get_finished(SSL_HANDSHAKE *hs);
  1419. int ssl3_send_change_cipher_spec(SSL *ssl);
  1420. int ssl3_send_alert(SSL *ssl, int level, int desc);
  1421. int ssl3_get_message(SSL *ssl, int msg_type,
  1422. enum ssl_hash_message_t hash_message);
  1423. void ssl3_get_current_message(const SSL *ssl, CBS *out);
  1424. void ssl3_release_current_message(SSL *ssl, int free_buffer);
  1425. /* ssl3_cert_verify_hash writes the SSL 3.0 CertificateVerify hash into the
  1426. * bytes pointed to by |out| and writes the number of bytes to |*out_len|. |out|
  1427. * must have room for |EVP_MAX_MD_SIZE| bytes. It sets |*out_md| to the hash
  1428. * function used. It returns one on success and zero on failure. */
  1429. int ssl3_cert_verify_hash(SSL *ssl, const EVP_MD **out_md, uint8_t *out,
  1430. size_t *out_len, uint16_t signature_algorithm);
  1431. int ssl3_send_finished(SSL_HANDSHAKE *hs, int a, int b);
  1432. int ssl3_dispatch_alert(SSL *ssl);
  1433. int ssl3_read_app_data(SSL *ssl, int *out_got_handshake, uint8_t *buf, int len,
  1434. int peek);
  1435. int ssl3_read_change_cipher_spec(SSL *ssl);
  1436. void ssl3_read_close_notify(SSL *ssl);
  1437. int ssl3_read_handshake_bytes(SSL *ssl, uint8_t *buf, int len);
  1438. int ssl3_write_app_data(SSL *ssl, const void *buf, int len);
  1439. int ssl3_write_bytes(SSL *ssl, int type, const void *buf, int len);
  1440. int ssl3_output_cert_chain(SSL *ssl);
  1441. int ssl3_new(SSL *ssl);
  1442. void ssl3_free(SSL *ssl);
  1443. int ssl3_accept(SSL_HANDSHAKE *hs);
  1444. int ssl3_connect(SSL_HANDSHAKE *hs);
  1445. int ssl3_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
  1446. int ssl3_finish_message(SSL *ssl, CBB *cbb, uint8_t **out_msg, size_t *out_len);
  1447. int ssl3_queue_message(SSL *ssl, uint8_t *msg, size_t len);
  1448. int ssl3_write_message(SSL *ssl);
  1449. int dtls1_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
  1450. int dtls1_finish_message(SSL *ssl, CBB *cbb, uint8_t **out_msg,
  1451. size_t *out_len);
  1452. int dtls1_queue_message(SSL *ssl, uint8_t *msg, size_t len);
  1453. int dtls1_write_message(SSL *ssl);
  1454. /* ssl_complete_message calls |finish_message| and |queue_message| on |cbb| to
  1455. * queue the message for writing. */
  1456. int ssl_complete_message(SSL *ssl, CBB *cbb);
  1457. /* ssl_hash_current_message incorporates the current handshake message into the
  1458. * handshake hash. It returns one on success and zero on allocation failure. */
  1459. int ssl_hash_current_message(SSL *ssl);
  1460. /* dtls1_get_record reads a new input record. On success, it places it in
  1461. * |ssl->s3->rrec| and returns one. Otherwise it returns <= 0 on error or if
  1462. * more data is needed. */
  1463. int dtls1_get_record(SSL *ssl);
  1464. int dtls1_read_app_data(SSL *ssl, int *out_got_handshake, uint8_t *buf, int len,
  1465. int peek);
  1466. int dtls1_read_change_cipher_spec(SSL *ssl);
  1467. void dtls1_read_close_notify(SSL *ssl);
  1468. int dtls1_write_app_data(SSL *ssl, const void *buf, int len);
  1469. /* dtls1_write_record sends a record. It returns one on success and <= 0 on
  1470. * error. */
  1471. int dtls1_write_record(SSL *ssl, int type, const uint8_t *buf, size_t len,
  1472. enum dtls1_use_epoch_t use_epoch);
  1473. int dtls1_send_change_cipher_spec(SSL *ssl);
  1474. int dtls1_send_finished(SSL *ssl, int a, int b, const char *sender, int slen);
  1475. int dtls1_retransmit_outgoing_messages(SSL *ssl);
  1476. void dtls1_clear_record_buffer(SSL *ssl);
  1477. int dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr,
  1478. CBS *out_body);
  1479. int dtls1_check_timeout_num(SSL *ssl);
  1480. int dtls1_handshake_write(SSL *ssl);
  1481. void dtls1_start_timer(SSL *ssl);
  1482. void dtls1_stop_timer(SSL *ssl);
  1483. int dtls1_is_timer_expired(SSL *ssl);
  1484. void dtls1_double_timeout(SSL *ssl);
  1485. unsigned int dtls1_min_mtu(void);
  1486. int dtls1_new(SSL *ssl);
  1487. int dtls1_accept(SSL *ssl);
  1488. int dtls1_connect(SSL *ssl);
  1489. void dtls1_free(SSL *ssl);
  1490. int dtls1_get_message(SSL *ssl, int mt, enum ssl_hash_message_t hash_message);
  1491. void dtls1_get_current_message(const SSL *ssl, CBS *out);
  1492. void dtls1_release_current_message(SSL *ssl, int free_buffer);
  1493. int dtls1_dispatch_alert(SSL *ssl);
  1494. /* ssl_is_wbio_buffered returns one if |ssl|'s write BIO is buffered and zero
  1495. * otherwise. */
  1496. int ssl_is_wbio_buffered(const SSL *ssl);
  1497. int ssl_init_wbio_buffer(SSL *ssl);
  1498. void ssl_free_wbio_buffer(SSL *ssl);
  1499. int tls1_change_cipher_state(SSL_HANDSHAKE *hs, int which);
  1500. int tls1_handshake_digest(SSL *ssl, uint8_t *out, size_t out_len);
  1501. int tls1_generate_master_secret(SSL *ssl, uint8_t *out, const uint8_t *premaster,
  1502. size_t premaster_len);
  1503. /* tls1_get_grouplist sets |*out_group_ids| and |*out_group_ids_len| to the
  1504. * locally-configured group preference list. */
  1505. void tls1_get_grouplist(SSL *ssl, const uint16_t **out_group_ids,
  1506. size_t *out_group_ids_len);
  1507. /* tls1_check_group_id returns one if |group_id| is consistent with
  1508. * locally-configured group preferences. */
  1509. int tls1_check_group_id(SSL *ssl, uint16_t group_id);
  1510. /* tls1_get_shared_group sets |*out_group_id| to the first preferred shared
  1511. * group between client and server preferences and returns one. If none may be
  1512. * found, it returns zero. */
  1513. int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id);
  1514. /* tls1_set_curves converts the array of |ncurves| NIDs pointed to by |curves|
  1515. * into a newly allocated array of TLS group IDs. On success, the function
  1516. * returns one and writes the array to |*out_group_ids| and its size to
  1517. * |*out_group_ids_len|. Otherwise, it returns zero. */
  1518. int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
  1519. const int *curves, size_t ncurves);
  1520. /* tls1_set_curves_list converts the string of curves pointed to by |curves|
  1521. * into a newly allocated array of TLS group IDs. On success, the function
  1522. * returns one and writes the array to |*out_group_ids| and its size to
  1523. * |*out_group_ids_len|. Otherwise, it returns zero. */
  1524. int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
  1525. const char *curves);
  1526. /* ssl_add_clienthello_tlsext writes ClientHello extensions to |out|. It
  1527. * returns one on success and zero on failure. The |header_len| argument is the
  1528. * length of the ClientHello written so far and is used to compute the padding
  1529. * length. (It does not include the record header.) */
  1530. int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len);
  1531. int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out);
  1532. int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
  1533. const SSL_CLIENT_HELLO *client_hello);
  1534. int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs);
  1535. #define tlsext_tick_md EVP_sha256
  1536. /* tls_process_ticket processes a session ticket from the client. On success,
  1537. * it sets |*out_session| to the decrypted session or NULL if the ticket was
  1538. * rejected. If the ticket was valid, it sets |*out_renew_ticket| to whether
  1539. * the ticket should be renewed. It returns one on success and zero on fatal
  1540. * error. */
  1541. int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
  1542. int *out_renew_ticket, const uint8_t *ticket,
  1543. size_t ticket_len, const uint8_t *session_id,
  1544. size_t session_id_len);
  1545. /* tls1_verify_channel_id processes the current message as a Channel ID message,
  1546. * and verifies the signature. If the key is valid, it saves the Channel ID and
  1547. * returns one. Otherwise, it returns zero. */
  1548. int tls1_verify_channel_id(SSL *ssl);
  1549. /* tls1_write_channel_id generates a Channel ID message and puts the output in
  1550. * |cbb|. |ssl->tlsext_channel_id_private| must already be set before calling.
  1551. * This function returns one on success and zero on error. */
  1552. int tls1_write_channel_id(SSL *ssl, CBB *cbb);
  1553. /* tls1_channel_id_hash computes the hash to be signed by Channel ID and writes
  1554. * it to |out|, which must contain at least |EVP_MAX_MD_SIZE| bytes. It returns
  1555. * one on success and zero on failure. */
  1556. int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len);
  1557. int tls1_record_handshake_hashes_for_channel_id(SSL *ssl);
  1558. /* ssl_do_channel_id_callback checks runs |ssl->ctx->channel_id_cb| if
  1559. * necessary. It returns one on success and zero on fatal error. Note that, on
  1560. * success, |ssl->tlsext_channel_id_private| may be unset, in which case the
  1561. * operation should be retried later. */
  1562. int ssl_do_channel_id_callback(SSL *ssl);
  1563. /* ssl3_can_false_start returns one if |ssl| is allowed to False Start and zero
  1564. * otherwise. */
  1565. int ssl3_can_false_start(const SSL *ssl);
  1566. /* ssl3_get_enc_method returns the SSL3_ENC_METHOD corresponding to
  1567. * |version|. */
  1568. const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version);
  1569. /* ssl_get_version_range sets |*out_min_version| and |*out_max_version| to the
  1570. * minimum and maximum enabled protocol versions, respectively. */
  1571. int ssl_get_version_range(const SSL *ssl, uint16_t *out_min_version,
  1572. uint16_t *out_max_version);
  1573. /* ssl3_protocol_version returns |ssl|'s protocol version. It is an error to
  1574. * call this function before the version is determined. */
  1575. uint16_t ssl3_protocol_version(const SSL *ssl);
  1576. uint32_t ssl_get_algorithm_prf(const SSL *ssl);
  1577. void ssl_set_client_disabled(SSL *ssl);
  1578. void ssl_get_current_time(const SSL *ssl, struct timeval *out_clock);
  1579. #if defined(__cplusplus)
  1580. } /* extern C */
  1581. #endif
  1582. #endif /* OPENSSL_HEADER_SSL_INTERNAL_H */