選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

internal.h 51 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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/pqueue.h>
  146. #include <openssl/ssl.h>
  147. #include <openssl/stack.h>
  148. #if defined(OPENSSL_WINDOWS)
  149. /* Windows defines struct timeval in winsock2.h. */
  150. #pragma warning(push, 3)
  151. #include <winsock2.h>
  152. #pragma warning(pop)
  153. #else
  154. #include <sys/types.h>
  155. #endif
  156. /* Cipher suites. */
  157. /* Bits for |algorithm_mkey| (key exchange algorithm). */
  158. #define SSL_kRSA 0x00000001L
  159. #define SSL_kDHE 0x00000002L
  160. #define SSL_kECDHE 0x00000004L
  161. /* SSL_kPSK is only set for plain PSK, not ECDHE_PSK. */
  162. #define SSL_kPSK 0x00000008L
  163. /* Bits for |algorithm_auth| (server authentication). */
  164. #define SSL_aRSA 0x00000001L
  165. #define SSL_aECDSA 0x00000002L
  166. /* SSL_aPSK is set for both PSK and ECDHE_PSK. */
  167. #define SSL_aPSK 0x00000004L
  168. /* Bits for |algorithm_enc| (symmetric encryption). */
  169. #define SSL_3DES 0x00000001L
  170. #define SSL_RC4 0x00000002L
  171. #define SSL_AES128 0x00000004L
  172. #define SSL_AES256 0x00000008L
  173. #define SSL_AES128GCM 0x00000010L
  174. #define SSL_AES256GCM 0x00000020L
  175. #define SSL_CHACHA20POLY1305 0x00000040L
  176. #define SSL_AES (SSL_AES128 | SSL_AES256 | SSL_AES128GCM | SSL_AES256GCM)
  177. /* Bits for |algorithm_mac| (symmetric authentication). */
  178. #define SSL_MD5 0x00000001L
  179. #define SSL_SHA1 0x00000002L
  180. #define SSL_SHA256 0x00000004L
  181. #define SSL_SHA384 0x00000008L
  182. /* SSL_AEAD is set for all AEADs. */
  183. #define SSL_AEAD 0x00000010L
  184. /* Bits for |algorithm_ssl| (protocol version). These denote the first protocol
  185. * version which introduced the cipher.
  186. *
  187. * TODO(davidben): These are extremely confusing, both in code and in
  188. * cipher rules. Try to remove them. */
  189. #define SSL_SSLV3 0x00000002L
  190. #define SSL_TLSV1 SSL_SSLV3
  191. #define SSL_TLSV1_2 0x00000004L
  192. /* Bits for |algorithm_prf| (handshake digest). */
  193. #define SSL_HANDSHAKE_MAC_DEFAULT 0x1
  194. #define SSL_HANDSHAKE_MAC_SHA256 0x2
  195. #define SSL_HANDSHAKE_MAC_SHA384 0x4
  196. /* SSL_MAX_DIGEST is the number of digest types which exist. When adding a new
  197. * one, update the table in ssl_cipher.c. */
  198. #define SSL_MAX_DIGEST 4
  199. /* Bits for |algo_strength|, cipher strength information. */
  200. #define SSL_MEDIUM 0x00000001L
  201. #define SSL_HIGH 0x00000002L
  202. #define SSL_FIPS 0x00000004L
  203. /* ssl_cipher_get_evp_aead sets |*out_aead| to point to the correct EVP_AEAD
  204. * object for |cipher| protocol version |version|. It sets |*out_mac_secret_len|
  205. * and |*out_fixed_iv_len| to the MAC key length and fixed IV length,
  206. * respectively. The MAC key length is zero except for legacy block and stream
  207. * ciphers. It returns 1 on success and 0 on error. */
  208. int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
  209. size_t *out_mac_secret_len,
  210. size_t *out_fixed_iv_len,
  211. const SSL_CIPHER *cipher, uint16_t version);
  212. /* ssl_get_handshake_digest returns the |EVP_MD| corresponding to
  213. * |algorithm_prf|. It returns SHA-1 for |SSL_HANDSHAKE_DEFAULT|. The caller is
  214. * responsible for maintaining the additional MD5 digest and switching to
  215. * SHA-256 in TLS 1.2. */
  216. const EVP_MD *ssl_get_handshake_digest(uint32_t algorithm_prf);
  217. /* ssl_create_cipher_list evaluates |rule_str| according to the ciphers in
  218. * |ssl_method|. It sets |*out_cipher_list| to a newly-allocated
  219. * |ssl_cipher_preference_list_st| containing the result.
  220. * |*out_cipher_list_by_id| is set to a list of selected ciphers sorted by
  221. * id. It returns |(*out_cipher_list)->ciphers| on success and NULL on
  222. * failure. */
  223. STACK_OF(SSL_CIPHER) *
  224. ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
  225. struct ssl_cipher_preference_list_st **out_cipher_list,
  226. STACK_OF(SSL_CIPHER) **out_cipher_list_by_id,
  227. const char *rule_str);
  228. /* ssl_cipher_get_value returns the cipher suite id of |cipher|. */
  229. uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher);
  230. /* ssl_cipher_get_key_type returns the |EVP_PKEY_*| value corresponding to the
  231. * server key used in |cipher| or |EVP_PKEY_NONE| if there is none. */
  232. int ssl_cipher_get_key_type(const SSL_CIPHER *cipher);
  233. /* ssl_cipher_has_server_public_key returns 1 if |cipher| involves a server
  234. * public key in the key exchange, sent in a server Certificate message.
  235. * Otherwise it returns 0. */
  236. int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher);
  237. /* ssl_cipher_requires_server_key_exchange returns 1 if |cipher| requires a
  238. * ServerKeyExchange message. Otherwise it returns 0.
  239. *
  240. * Unlike ssl_cipher_has_server_public_key, some ciphers take optional
  241. * ServerKeyExchanges. PSK and RSA_PSK only use the ServerKeyExchange to
  242. * communicate a psk_identity_hint, so it is optional. */
  243. int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher);
  244. /* Encryption layer. */
  245. /* SSL_AEAD_CTX contains information about an AEAD that is being used to encrypt
  246. * an SSL connection. */
  247. struct ssl_aead_ctx_st {
  248. const SSL_CIPHER *cipher;
  249. EVP_AEAD_CTX ctx;
  250. /* fixed_nonce contains any bytes of the nonce that are fixed for all
  251. * records. */
  252. uint8_t fixed_nonce[8];
  253. uint8_t fixed_nonce_len, variable_nonce_len;
  254. /* variable_nonce_included_in_record is non-zero if the variable nonce
  255. * for a record is included as a prefix before the ciphertext. */
  256. char variable_nonce_included_in_record;
  257. /* random_variable_nonce is non-zero if the variable nonce is
  258. * randomly generated, rather than derived from the sequence
  259. * number. */
  260. char random_variable_nonce;
  261. /* omit_length_in_ad is non-zero if the length should be omitted in the
  262. * AEAD's ad parameter. */
  263. char omit_length_in_ad;
  264. /* omit_version_in_ad is non-zero if the version should be omitted
  265. * in the AEAD's ad parameter. */
  266. char omit_version_in_ad;
  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| and
  287. * writes the result to |out|. It returns one on success and zero on
  288. * error. |ctx| may be NULL to denote the null cipher.
  289. *
  290. * If |in| and |out| alias then |out| must be <= |in| + |explicit_nonce_len|. */
  291. int SSL_AEAD_CTX_open(SSL_AEAD_CTX *ctx, uint8_t *out, size_t *out_len,
  292. size_t max_out, uint8_t type, uint16_t wire_version,
  293. const uint8_t seqnum[8], const uint8_t *in,
  294. size_t in_len);
  295. /* SSL_AEAD_CTX_seal encrypts and authenticates |in_len| bytes from |in| and
  296. * writes the result to |out|. It returns one on success and zero on
  297. * error. |ctx| may be NULL to denote the null cipher.
  298. *
  299. * If |in| and |out| alias then |out| + |explicit_nonce_len| must be <= |in| */
  300. int SSL_AEAD_CTX_seal(SSL_AEAD_CTX *ctx, uint8_t *out, size_t *out_len,
  301. size_t max_out, uint8_t type, uint16_t wire_version,
  302. const uint8_t seqnum[8], const uint8_t *in,
  303. size_t in_len);
  304. /* DTLS replay bitmap. */
  305. /* DTLS1_BITMAP maintains a sliding window of 64 sequence numbers to detect
  306. * replayed packets. It should be initialized by zeroing every field. */
  307. typedef struct dtls1_bitmap_st {
  308. /* map is a bit mask of the last 64 sequence numbers. Bit
  309. * |1<<i| corresponds to |max_seq_num - i|. */
  310. uint64_t map;
  311. /* max_seq_num is the largest sequence number seen so far as a 64-bit
  312. * integer. */
  313. uint64_t max_seq_num;
  314. } DTLS1_BITMAP;
  315. /* Private key operations. */
  316. /* ssl_has_private_key returns one if |ssl| has a private key
  317. * configured and zero otherwise. */
  318. int ssl_has_private_key(SSL *ssl);
  319. /* ssl_private_key_* call the corresponding function on the
  320. * |SSL_PRIVATE_KEY_METHOD| for |ssl|, if configured. Otherwise, they implement
  321. * the operation with |EVP_PKEY|. */
  322. int ssl_private_key_type(SSL *ssl);
  323. int ssl_private_key_supports_digest(SSL *ssl, const EVP_MD *md);
  324. size_t ssl_private_key_max_signature_len(SSL *ssl);
  325. enum ssl_private_key_result_t ssl_private_key_sign(
  326. SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, const EVP_MD *md,
  327. const uint8_t *in, size_t in_len);
  328. enum ssl_private_key_result_t ssl_private_key_sign_complete(
  329. SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out);
  330. /* Custom extensions */
  331. /* ssl_custom_extension (a.k.a. SSL_CUSTOM_EXTENSION) is a structure that
  332. * contains information about custom-extension callbacks. */
  333. struct ssl_custom_extension {
  334. SSL_custom_ext_add_cb add_callback;
  335. void *add_arg;
  336. SSL_custom_ext_free_cb free_callback;
  337. SSL_custom_ext_parse_cb parse_callback;
  338. void *parse_arg;
  339. uint16_t value;
  340. };
  341. void SSL_CUSTOM_EXTENSION_free(SSL_CUSTOM_EXTENSION *custom_extension);
  342. int custom_ext_add_clienthello(SSL *ssl, CBB *extensions);
  343. int custom_ext_parse_serverhello(SSL *ssl, int *out_alert, uint16_t value,
  344. const CBS *extension);
  345. int custom_ext_parse_clienthello(SSL *ssl, int *out_alert, uint16_t value,
  346. const CBS *extension);
  347. int custom_ext_add_serverhello(SSL *ssl, CBB *extensions);
  348. /* Handshake hash.
  349. *
  350. * The TLS handshake maintains a transcript of all handshake messages. At
  351. * various points in the protocol, this is either a handshake buffer, a rolling
  352. * hash (selected by cipher suite) or both. */
  353. /* ssl3_init_handshake_buffer initializes the handshake buffer and resets the
  354. * handshake hash. It returns one success and zero on failure. */
  355. int ssl3_init_handshake_buffer(SSL *ssl);
  356. /* ssl3_init_handshake_hash initializes the handshake hash based on the pending
  357. * cipher and the contents of the handshake buffer. Subsequent calls to
  358. * |ssl3_update_handshake_hash| will update the rolling hash. It returns one on
  359. * success and zero on failure. It is an error to call this function after the
  360. * handshake buffer is released. */
  361. int ssl3_init_handshake_hash(SSL *ssl);
  362. /* ssl3_free_handshake_buffer releases the handshake buffer. Subsequent calls
  363. * to |ssl3_update_handshake_hash| will not update the handshake buffer. */
  364. void ssl3_free_handshake_buffer(SSL *ssl);
  365. /* ssl3_free_handshake_hash releases the handshake hash. */
  366. void ssl3_free_handshake_hash(SSL *s);
  367. /* ssl3_update_handshake_hash adds |in| to the handshake buffer and handshake
  368. * hash, whichever is enabled. It returns one on success and zero on failure. */
  369. int ssl3_update_handshake_hash(SSL *ssl, const uint8_t *in, size_t in_len);
  370. /* Underdocumented functions.
  371. *
  372. * Functions below here haven't been touched up and may be underdocumented. */
  373. #define c2l(c, l) \
  374. (l = ((unsigned long)(*((c)++))), l |= (((unsigned long)(*((c)++))) << 8), \
  375. l |= (((unsigned long)(*((c)++))) << 16), \
  376. l |= (((unsigned long)(*((c)++))) << 24))
  377. /* NOTE - c is not incremented as per c2l */
  378. #define c2ln(c, l1, l2, n) \
  379. { \
  380. c += n; \
  381. l1 = l2 = 0; \
  382. switch (n) { \
  383. case 8: \
  384. l2 = ((unsigned long)(*(--(c)))) << 24; \
  385. case 7: \
  386. l2 |= ((unsigned long)(*(--(c)))) << 16; \
  387. case 6: \
  388. l2 |= ((unsigned long)(*(--(c)))) << 8; \
  389. case 5: \
  390. l2 |= ((unsigned long)(*(--(c)))); \
  391. case 4: \
  392. l1 = ((unsigned long)(*(--(c)))) << 24; \
  393. case 3: \
  394. l1 |= ((unsigned long)(*(--(c)))) << 16; \
  395. case 2: \
  396. l1 |= ((unsigned long)(*(--(c)))) << 8; \
  397. case 1: \
  398. l1 |= ((unsigned long)(*(--(c)))); \
  399. } \
  400. }
  401. #define l2c(l, c) \
  402. (*((c)++) = (uint8_t)(((l)) & 0xff), \
  403. *((c)++) = (uint8_t)(((l) >> 8) & 0xff), \
  404. *((c)++) = (uint8_t)(((l) >> 16) & 0xff), \
  405. *((c)++) = (uint8_t)(((l) >> 24) & 0xff))
  406. #define n2l(c, l) \
  407. (l = ((unsigned long)(*((c)++))) << 24, \
  408. l |= ((unsigned long)(*((c)++))) << 16, \
  409. l |= ((unsigned long)(*((c)++))) << 8, l |= ((unsigned long)(*((c)++))))
  410. #define l2n(l, c) \
  411. (*((c)++) = (uint8_t)(((l) >> 24) & 0xff), \
  412. *((c)++) = (uint8_t)(((l) >> 16) & 0xff), \
  413. *((c)++) = (uint8_t)(((l) >> 8) & 0xff), \
  414. *((c)++) = (uint8_t)(((l)) & 0xff))
  415. #define l2n8(l, c) \
  416. (*((c)++) = (uint8_t)(((l) >> 56) & 0xff), \
  417. *((c)++) = (uint8_t)(((l) >> 48) & 0xff), \
  418. *((c)++) = (uint8_t)(((l) >> 40) & 0xff), \
  419. *((c)++) = (uint8_t)(((l) >> 32) & 0xff), \
  420. *((c)++) = (uint8_t)(((l) >> 24) & 0xff), \
  421. *((c)++) = (uint8_t)(((l) >> 16) & 0xff), \
  422. *((c)++) = (uint8_t)(((l) >> 8) & 0xff), \
  423. *((c)++) = (uint8_t)(((l)) & 0xff))
  424. /* NOTE - c is not incremented as per l2c */
  425. #define l2cn(l1, l2, c, n) \
  426. { \
  427. c += n; \
  428. switch (n) { \
  429. case 8: \
  430. *(--(c)) = (uint8_t)(((l2) >> 24) & 0xff); \
  431. case 7: \
  432. *(--(c)) = (uint8_t)(((l2) >> 16) & 0xff); \
  433. case 6: \
  434. *(--(c)) = (uint8_t)(((l2) >> 8) & 0xff); \
  435. case 5: \
  436. *(--(c)) = (uint8_t)(((l2)) & 0xff); \
  437. case 4: \
  438. *(--(c)) = (uint8_t)(((l1) >> 24) & 0xff); \
  439. case 3: \
  440. *(--(c)) = (uint8_t)(((l1) >> 16) & 0xff); \
  441. case 2: \
  442. *(--(c)) = (uint8_t)(((l1) >> 8) & 0xff); \
  443. case 1: \
  444. *(--(c)) = (uint8_t)(((l1)) & 0xff); \
  445. } \
  446. }
  447. #define n2s(c, s) \
  448. ((s = (((unsigned int)(c[0])) << 8) | (((unsigned int)(c[1])))), c += 2)
  449. #define s2n(s, c) \
  450. ((c[0] = (uint8_t)(((s) >> 8) & 0xff), \
  451. c[1] = (uint8_t)(((s)) & 0xff)), \
  452. c += 2)
  453. #define n2l3(c, l) \
  454. ((l = (((unsigned long)(c[0])) << 16) | (((unsigned long)(c[1])) << 8) | \
  455. (((unsigned long)(c[2])))), \
  456. c += 3)
  457. #define l2n3(l, c) \
  458. ((c[0] = (uint8_t)(((l) >> 16) & 0xff), \
  459. c[1] = (uint8_t)(((l) >> 8) & 0xff), \
  460. c[2] = (uint8_t)(((l)) & 0xff)), \
  461. c += 3)
  462. /* LOCAL STUFF */
  463. #define TLSEXT_CHANNEL_ID_SIZE 128
  464. /* Check if an SSL structure is using DTLS */
  465. #define SSL_IS_DTLS(s) (s->method->is_dtls)
  466. /* See if we need explicit IV */
  467. #define SSL_USE_EXPLICIT_IV(s) \
  468. (s->enc_method->enc_flags & SSL_ENC_FLAG_EXPLICIT_IV)
  469. /* See if we use signature algorithms extension and signature algorithm before
  470. * signatures. */
  471. #define SSL_USE_SIGALGS(s) (s->enc_method->enc_flags & SSL_ENC_FLAG_SIGALGS)
  472. /* Allow TLS 1.2 ciphersuites: applies to DTLS 1.2 as well as TLS 1.2: may
  473. * apply to others in future. */
  474. #define SSL_USE_TLS1_2_CIPHERS(s) \
  475. (s->enc_method->enc_flags & SSL_ENC_FLAG_TLS1_2_CIPHERS)
  476. /* Determine if a client can use TLS 1.2 ciphersuites: can't rely on method
  477. * flags because it may not be set to correct version yet. */
  478. #define SSL_CLIENT_USE_TLS1_2_CIPHERS(s) \
  479. ((SSL_IS_DTLS(s) && s->client_version <= DTLS1_2_VERSION) || \
  480. (!SSL_IS_DTLS(s) && s->client_version >= TLS1_2_VERSION))
  481. /* SSL_kRSA <- RSA_ENC | (RSA_TMP & RSA_SIGN) |
  482. * <- (EXPORT & (RSA_ENC | RSA_TMP) & RSA_SIGN)
  483. * SSL_kDH <- DH_ENC & (RSA_ENC | RSA_SIGN | DSA_SIGN)
  484. * SSL_kDHE <- RSA_ENC | RSA_SIGN | DSA_SIGN
  485. * SSL_aRSA <- RSA_ENC | RSA_SIGN
  486. * SSL_aDSS <- DSA_SIGN */
  487. /* From RFC4492, used in encoding the curve type in ECParameters */
  488. #define EXPLICIT_PRIME_CURVE_TYPE 1
  489. #define EXPLICIT_CHAR2_CURVE_TYPE 2
  490. #define NAMED_CURVE_TYPE 3
  491. enum ssl_hash_message_t {
  492. ssl_dont_hash_message,
  493. ssl_hash_message,
  494. };
  495. /* Structure containing decoded values of signature algorithms extension */
  496. typedef struct tls_sigalgs_st {
  497. uint8_t rsign;
  498. uint8_t rhash;
  499. } TLS_SIGALGS;
  500. typedef struct cert_st {
  501. X509 *x509;
  502. EVP_PKEY *privatekey;
  503. /* Chain for this certificate */
  504. STACK_OF(X509) *chain;
  505. /* key_method, if non-NULL, is a set of callbacks to call for private key
  506. * operations. */
  507. const SSL_PRIVATE_KEY_METHOD *key_method;
  508. /* For clients the following masks are of *disabled* key and auth algorithms
  509. * based on the current session.
  510. *
  511. * TODO(davidben): Remove these. They get checked twice: when sending the
  512. * ClientHello and when processing the ServerHello. However, mask_ssl is a
  513. * different value both times. mask_k and mask_a are not, but is a
  514. * round-about way of checking the server's cipher was one of the advertised
  515. * ones. (Currently it checks the masks and then the list of ciphers prior to
  516. * applying the masks in ClientHello.) */
  517. uint32_t mask_k;
  518. uint32_t mask_a;
  519. uint32_t mask_ssl;
  520. DH *dh_tmp;
  521. DH *(*dh_tmp_cb)(SSL *ssl, int is_export, int keysize);
  522. /* ecdh_nid, if not |NID_undef|, is the NID of the curve to use for ephemeral
  523. * ECDH keys. If unset, |ecdh_tmp_cb| is consulted. */
  524. int ecdh_nid;
  525. /* ecdh_tmp_cb is a callback for selecting the curve to use for ephemeral ECDH
  526. * keys. If NULL, a curve is selected automatically. See
  527. * |SSL_CTX_set_tmp_ecdh_callback|. */
  528. EC_KEY *(*ecdh_tmp_cb)(SSL *ssl, int is_export, int keysize);
  529. /* Server-only: client_certificate_types is list of certificate types to
  530. * include in the CertificateRequest message.
  531. */
  532. uint8_t *client_certificate_types;
  533. size_t num_client_certificate_types;
  534. /* signature algorithms peer reports: e.g. supported signature
  535. * algorithms extension for server or as part of a certificate
  536. * request for client. */
  537. uint8_t *peer_sigalgs;
  538. /* Size of above array */
  539. size_t peer_sigalgslen;
  540. /* suppported signature algorithms.
  541. * When set on a client this is sent in the client hello as the
  542. * supported signature algorithms extension. For servers
  543. * it represents the signature algorithms we are willing to use. */
  544. uint8_t *conf_sigalgs;
  545. /* Size of above array */
  546. size_t conf_sigalgslen;
  547. /* Client authentication signature algorithms, if not set then
  548. * uses conf_sigalgs. On servers these will be the signature
  549. * algorithms sent to the client in a cerificate request for TLS 1.2.
  550. * On a client this represents the signature algortithms we are
  551. * willing to use for client authentication. */
  552. uint8_t *client_sigalgs;
  553. /* Size of above array */
  554. size_t client_sigalgslen;
  555. /* Signature algorithms shared by client and server: cached
  556. * because these are used most often. */
  557. TLS_SIGALGS *shared_sigalgs;
  558. size_t shared_sigalgslen;
  559. /* Certificate setup callback: if set is called whenever a
  560. * certificate may be required (client or server). the callback
  561. * can then examine any appropriate parameters and setup any
  562. * certificates required. This allows advanced applications
  563. * to select certificates on the fly: for example based on
  564. * supported signature algorithms or curves. */
  565. int (*cert_cb)(SSL *ssl, void *arg);
  566. void *cert_cb_arg;
  567. /* Optional X509_STORE for chain building or certificate validation
  568. * If NULL the parent SSL_CTX store is used instead. */
  569. X509_STORE *chain_store;
  570. X509_STORE *verify_store;
  571. } CERT;
  572. typedef struct sess_cert_st {
  573. /* cert_chain is the certificate chain sent by the peer. NOTE: for a client,
  574. * this does includes the server's leaf certificate, but, for a server, this
  575. * does NOT include the client's leaf. */
  576. STACK_OF(X509) *cert_chain;
  577. /* peer_cert, on a client, is the leaf certificate of the peer. */
  578. X509 *peer_cert;
  579. DH *peer_dh_tmp;
  580. EC_KEY *peer_ecdh_tmp;
  581. } SESS_CERT;
  582. /* SSL_METHOD is a compatibility structure to support the legacy version-locked
  583. * methods. */
  584. struct ssl_method_st {
  585. /* version, if non-zero, is the only protocol version acceptable to an
  586. * SSL_CTX initialized from this method. */
  587. uint16_t version;
  588. /* method is the underlying SSL_PROTOCOL_METHOD that initializes the
  589. * SSL_CTX. */
  590. const SSL_PROTOCOL_METHOD *method;
  591. };
  592. /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
  593. struct ssl_protocol_method_st {
  594. /* is_dtls is one if the protocol is DTLS and zero otherwise. */
  595. char is_dtls;
  596. int (*ssl_new)(SSL *s);
  597. void (*ssl_free)(SSL *s);
  598. int (*ssl_accept)(SSL *s);
  599. int (*ssl_connect)(SSL *s);
  600. long (*ssl_get_message)(SSL *s, int header_state, int body_state,
  601. int msg_type, long max,
  602. enum ssl_hash_message_t hash_message, int *ok);
  603. int (*ssl_read_app_data)(SSL *s, uint8_t *buf, int len, int peek);
  604. void (*ssl_read_close_notify)(SSL *s);
  605. int (*ssl_write_app_data)(SSL *s, const void *buf_, int len);
  606. int (*ssl_dispatch_alert)(SSL *s);
  607. long (*ssl_ctrl)(SSL *s, int cmd, long larg, void *parg);
  608. long (*ssl_ctx_ctrl)(SSL_CTX *ctx, int cmd, long larg, void *parg);
  609. /* supports_cipher returns one if |cipher| is supported by this protocol and
  610. * zero otherwise. */
  611. int (*supports_cipher)(const SSL_CIPHER *cipher);
  612. /* Handshake header length */
  613. unsigned int hhlen;
  614. /* Set the handshake header */
  615. int (*set_handshake_header)(SSL *s, int type, unsigned long len);
  616. /* Write out handshake message */
  617. int (*do_write)(SSL *s);
  618. };
  619. /* This is for the SSLv3/TLSv1.0 differences in crypto/hash stuff It is a bit
  620. * of a mess of functions, but hell, think of it as an opaque structure. */
  621. struct ssl3_enc_method {
  622. int (*prf)(SSL *, uint8_t *, size_t, const uint8_t *, size_t, const char *,
  623. size_t, const uint8_t *, size_t, const uint8_t *, size_t);
  624. int (*setup_key_block)(SSL *);
  625. int (*generate_master_secret)(SSL *, uint8_t *, const uint8_t *, size_t);
  626. int (*change_cipher_state)(SSL *, int);
  627. int (*final_finish_mac)(SSL *, const char *, int, uint8_t *);
  628. int (*cert_verify_mac)(SSL *, int, uint8_t *);
  629. const char *client_finished_label;
  630. int client_finished_label_len;
  631. const char *server_finished_label;
  632. int server_finished_label_len;
  633. int (*alert_value)(int);
  634. int (*export_keying_material)(SSL *, uint8_t *, size_t, const char *, size_t,
  635. const uint8_t *, size_t, int use_context);
  636. /* Various flags indicating protocol version requirements */
  637. unsigned int enc_flags;
  638. };
  639. #define SSL_HM_HEADER_LENGTH(s) s->method->hhlen
  640. #define ssl_handshake_start(s) \
  641. (((uint8_t *)s->init_buf->data) + s->method->hhlen)
  642. #define ssl_set_handshake_header(s, htype, len) \
  643. s->method->set_handshake_header(s, htype, len)
  644. #define ssl_do_write(s) s->method->do_write(s)
  645. /* Values for enc_flags */
  646. /* Uses explicit IV for CBC mode */
  647. #define SSL_ENC_FLAG_EXPLICIT_IV 0x1
  648. /* Uses signature algorithms extension */
  649. #define SSL_ENC_FLAG_SIGALGS 0x2
  650. /* Uses SHA256 default PRF */
  651. #define SSL_ENC_FLAG_SHA256_PRF 0x4
  652. /* Allow TLS 1.2 ciphersuites: applies to DTLS 1.2 as well as TLS 1.2:
  653. * may apply to others in future. */
  654. #define SSL_ENC_FLAG_TLS1_2_CIPHERS 0x8
  655. /* lengths of messages */
  656. #define DTLS1_COOKIE_LENGTH 256
  657. #define DTLS1_RT_HEADER_LENGTH 13
  658. #define DTLS1_HM_HEADER_LENGTH 12
  659. #define DTLS1_CCS_HEADER_LENGTH 1
  660. #define DTLS1_AL_HEADER_LENGTH 2
  661. /* TODO(davidben): This structure is used for both incoming messages and
  662. * outgoing messages. |is_ccs| and |epoch| are only used in the latter and
  663. * should be moved elsewhere. */
  664. struct hm_header_st {
  665. uint8_t type;
  666. uint32_t msg_len;
  667. uint16_t seq;
  668. uint32_t frag_off;
  669. uint32_t frag_len;
  670. int is_ccs;
  671. /* epoch, for buffered outgoing messages, is the epoch the message was
  672. * originally sent in. */
  673. uint16_t epoch;
  674. };
  675. /* TODO(davidben): This structure is used for both incoming messages and
  676. * outgoing messages. |fragment| and |reassembly| are only used in the former
  677. * and should be moved elsewhere. */
  678. typedef struct hm_fragment_st {
  679. struct hm_header_st msg_header;
  680. uint8_t *fragment;
  681. uint8_t *reassembly;
  682. } hm_fragment;
  683. typedef struct dtls1_state_st {
  684. /* send_cookie is true if we are resending the ClientHello
  685. * with a cookie from a HelloVerifyRequest. */
  686. unsigned int send_cookie;
  687. uint8_t cookie[DTLS1_COOKIE_LENGTH];
  688. size_t cookie_len;
  689. /* The current data and handshake epoch. This is initially undefined, and
  690. * starts at zero once the initial handshake is completed. */
  691. uint16_t r_epoch;
  692. uint16_t w_epoch;
  693. /* records being received in the current epoch */
  694. DTLS1_BITMAP bitmap;
  695. /* handshake message numbers */
  696. uint16_t handshake_write_seq;
  697. uint16_t next_handshake_write_seq;
  698. uint16_t handshake_read_seq;
  699. /* save last sequence number for retransmissions */
  700. uint8_t last_write_sequence[8];
  701. /* buffered_messages is a priority queue of incoming handshake messages that
  702. * have yet to be processed.
  703. *
  704. * TODO(davidben): This data structure may as well be a ring buffer of fixed
  705. * size. */
  706. pqueue buffered_messages;
  707. /* send_messages is a priority queue of outgoing handshake messages sent in
  708. * the most recent handshake flight.
  709. *
  710. * TODO(davidben): This data structure may as well be a STACK_OF(T). */
  711. pqueue sent_messages;
  712. unsigned int mtu; /* max DTLS packet size */
  713. struct hm_header_st w_msg_hdr;
  714. /* num_timeouts is the number of times the retransmit timer has fired since
  715. * the last time it was reset. */
  716. unsigned int num_timeouts;
  717. /* Indicates when the last handshake msg or heartbeat sent will
  718. * timeout. */
  719. struct timeval next_timeout;
  720. /* Timeout duration */
  721. unsigned short timeout_duration;
  722. unsigned int change_cipher_spec_ok;
  723. } DTLS1_STATE;
  724. extern const SSL3_ENC_METHOD TLSv1_enc_data;
  725. extern const SSL3_ENC_METHOD TLSv1_1_enc_data;
  726. extern const SSL3_ENC_METHOD TLSv1_2_enc_data;
  727. extern const SSL3_ENC_METHOD SSLv3_enc_data;
  728. extern const SRTP_PROTECTION_PROFILE kSRTPProfiles[];
  729. void ssl_clear_cipher_ctx(SSL *s);
  730. int ssl_clear_bad_session(SSL *s);
  731. CERT *ssl_cert_new(void);
  732. CERT *ssl_cert_dup(CERT *cert);
  733. void ssl_cert_clear_certs(CERT *c);
  734. void ssl_cert_free(CERT *c);
  735. SESS_CERT *ssl_sess_cert_new(void);
  736. SESS_CERT *ssl_sess_cert_dup(const SESS_CERT *sess_cert);
  737. void ssl_sess_cert_free(SESS_CERT *sess_cert);
  738. int ssl_get_new_session(SSL *s, int session);
  739. enum ssl_session_result_t {
  740. ssl_session_success,
  741. ssl_session_error,
  742. ssl_session_retry,
  743. };
  744. /* ssl_get_prev_session looks up the previous session based on |ctx|. On
  745. * success, it sets |*out_session| to the session or NULL if none was found. It
  746. * sets |*out_send_ticket| to whether a ticket should be sent at the end of the
  747. * handshake. If the session could not be looked up synchronously, it returns
  748. * |ssl_session_retry| and should be called again. Otherwise, it returns
  749. * |ssl_session_error|. */
  750. enum ssl_session_result_t ssl_get_prev_session(
  751. SSL *ssl, SSL_SESSION **out_session, int *out_send_ticket,
  752. const struct ssl_early_callback_ctx *ctx);
  753. STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs);
  754. int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p);
  755. struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
  756. struct ssl_cipher_preference_list_st *cipher_list);
  757. void ssl_cipher_preference_list_free(
  758. struct ssl_cipher_preference_list_st *cipher_list);
  759. struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
  760. STACK_OF(SSL_CIPHER) *ciphers);
  761. struct ssl_cipher_preference_list_st *ssl_get_cipher_preferences(SSL *s);
  762. int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain);
  763. int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain);
  764. int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509);
  765. int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509);
  766. void ssl_cert_set_cert_cb(CERT *cert,
  767. int (*cb)(SSL *ssl, void *arg), void *arg);
  768. int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk);
  769. int ssl_add_cert_chain(SSL *s, unsigned long *l);
  770. int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref);
  771. void ssl_update_cache(SSL *s, int mode);
  772. /* ssl_get_compatible_server_ciphers determines the key exchange and
  773. * authentication cipher suite masks compatible with the server configuration
  774. * and current ClientHello parameters of |s|. It sets |*out_mask_k| to the key
  775. * exchange mask and |*out_mask_a| to the authentication mask. */
  776. void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
  777. uint32_t *out_mask_a);
  778. STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s);
  779. int ssl_verify_alarm_type(long type);
  780. /* ssl_fill_hello_random fills a client_random or server_random field of length
  781. * |len|. It returns one on success and zero on failure. */
  782. int ssl_fill_hello_random(uint8_t *out, size_t len, int is_server);
  783. int ssl3_send_server_certificate(SSL *s);
  784. int ssl3_send_new_session_ticket(SSL *s);
  785. int ssl3_send_cert_status(SSL *s);
  786. int ssl3_get_finished(SSL *s, int state_a, int state_b);
  787. int ssl3_send_change_cipher_spec(SSL *s, int state_a, int state_b);
  788. int ssl3_prf(SSL *s, uint8_t *out, size_t out_len, const uint8_t *secret,
  789. size_t secret_len, const char *label, size_t label_len,
  790. const uint8_t *seed1, size_t seed1_len,
  791. const uint8_t *seed2, size_t seed2_len);
  792. void ssl3_cleanup_key_block(SSL *s);
  793. int ssl3_do_write(SSL *s, int type);
  794. int ssl3_send_alert(SSL *s, int level, int desc);
  795. int ssl3_get_req_cert_type(SSL *s, uint8_t *p);
  796. long ssl3_get_message(SSL *s, int header_state, int body_state, int msg_type,
  797. long max, enum ssl_hash_message_t hash_message, int *ok);
  798. /* ssl3_hash_current_message incorporates the current handshake message into the
  799. * handshake hash. It returns one on success and zero on allocation failure. */
  800. int ssl3_hash_current_message(SSL *s);
  801. /* ssl3_cert_verify_hash writes the CertificateVerify hash into the bytes
  802. * pointed to by |out| and writes the number of bytes to |*out_len|. |out| must
  803. * have room for EVP_MAX_MD_SIZE bytes. For TLS 1.2 and up, |*out_md| is used
  804. * for the hash function, otherwise the hash function depends on |pkey_type|
  805. * and is written to |*out_md|. It returns one on success and zero on
  806. * failure. */
  807. int ssl3_cert_verify_hash(SSL *s, uint8_t *out, size_t *out_len,
  808. const EVP_MD **out_md, int pkey_type);
  809. int ssl3_send_finished(SSL *s, int a, int b, const char *sender, int slen);
  810. int ssl3_supports_cipher(const SSL_CIPHER *cipher);
  811. int ssl3_dispatch_alert(SSL *s);
  812. int ssl3_expect_change_cipher_spec(SSL *s);
  813. int ssl3_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek);
  814. void ssl3_read_close_notify(SSL *ssl);
  815. int ssl3_read_bytes(SSL *s, int type, uint8_t *buf, int len, int peek);
  816. int ssl3_write_app_data(SSL *ssl, const void *buf, int len);
  817. int ssl3_write_bytes(SSL *s, int type, const void *buf, int len);
  818. int ssl3_final_finish_mac(SSL *s, const char *sender, int slen, uint8_t *p);
  819. int ssl3_cert_verify_mac(SSL *s, int md_nid, uint8_t *p);
  820. int ssl3_output_cert_chain(SSL *s);
  821. const SSL_CIPHER *ssl3_choose_cipher(
  822. SSL *ssl, STACK_OF(SSL_CIPHER) *clnt,
  823. struct ssl_cipher_preference_list_st *srvr);
  824. int ssl3_setup_read_buffer(SSL *s);
  825. int ssl3_setup_write_buffer(SSL *s);
  826. int ssl3_release_read_buffer(SSL *s);
  827. int ssl3_release_write_buffer(SSL *s);
  828. int ssl3_new(SSL *s);
  829. void ssl3_free(SSL *s);
  830. int ssl3_accept(SSL *s);
  831. int ssl3_connect(SSL *s);
  832. long ssl3_ctrl(SSL *s, int cmd, long larg, void *parg);
  833. long ssl3_ctx_ctrl(SSL_CTX *s, int cmd, long larg, void *parg);
  834. /* ssl3_record_sequence_update increments the sequence number in |seq|. It
  835. * returns one on success and zero on wraparound. */
  836. int ssl3_record_sequence_update(uint8_t *seq, size_t seq_len);
  837. int ssl3_do_change_cipher_spec(SSL *ssl);
  838. int ssl3_set_handshake_header(SSL *s, int htype, unsigned long len);
  839. int ssl3_handshake_write(SSL *s);
  840. enum dtls1_use_epoch_t {
  841. dtls1_use_previous_epoch,
  842. dtls1_use_current_epoch,
  843. };
  844. int dtls1_do_write(SSL *s, int type, enum dtls1_use_epoch_t use_epoch);
  845. int ssl3_read_n(SSL *s, int n, int extend);
  846. int dtls1_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek);
  847. void dtls1_read_close_notify(SSL *ssl);
  848. int dtls1_read_bytes(SSL *s, int type, uint8_t *buf, int len, int peek);
  849. int ssl3_write_pending(SSL *s, int type, const uint8_t *buf, unsigned int len);
  850. void dtls1_set_message_header(SSL *s, uint8_t mt, unsigned long len,
  851. unsigned short seq_num, unsigned long frag_off,
  852. unsigned long frag_len);
  853. int dtls1_write_app_data(SSL *s, const void *buf, int len);
  854. int dtls1_write_bytes(SSL *s, int type, const void *buf, int len,
  855. enum dtls1_use_epoch_t use_epoch);
  856. int dtls1_send_change_cipher_spec(SSL *s, int a, int b);
  857. int dtls1_send_finished(SSL *s, int a, int b, const char *sender, int slen);
  858. int dtls1_read_failed(SSL *s, int code);
  859. int dtls1_buffer_message(SSL *s, int ccs);
  860. int dtls1_get_queue_priority(unsigned short seq, int is_ccs);
  861. int dtls1_retransmit_buffered_messages(SSL *s);
  862. void dtls1_clear_record_buffer(SSL *s);
  863. void dtls1_get_message_header(uint8_t *data, struct hm_header_st *msg_hdr);
  864. void dtls1_reset_seq_numbers(SSL *s, int rw);
  865. int dtls1_check_timeout_num(SSL *s);
  866. int dtls1_set_handshake_header(SSL *s, int type, unsigned long len);
  867. int dtls1_handshake_write(SSL *s);
  868. int dtls1_supports_cipher(const SSL_CIPHER *cipher);
  869. void dtls1_start_timer(SSL *s);
  870. void dtls1_stop_timer(SSL *s);
  871. int dtls1_is_timer_expired(SSL *s);
  872. void dtls1_double_timeout(SSL *s);
  873. unsigned int dtls1_min_mtu(void);
  874. void dtls1_hm_fragment_free(hm_fragment *frag);
  875. /* some client-only functions */
  876. int ssl3_send_client_hello(SSL *s);
  877. int ssl3_get_server_hello(SSL *s);
  878. int ssl3_get_certificate_request(SSL *s);
  879. int ssl3_get_new_session_ticket(SSL *s);
  880. int ssl3_get_cert_status(SSL *s);
  881. int ssl3_get_server_done(SSL *s);
  882. int ssl3_send_cert_verify(SSL *s);
  883. int ssl3_send_client_certificate(SSL *s);
  884. int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey);
  885. int ssl3_send_client_key_exchange(SSL *s);
  886. int ssl3_get_server_key_exchange(SSL *s);
  887. int ssl3_get_server_certificate(SSL *s);
  888. int ssl3_send_next_proto(SSL *s);
  889. int ssl3_send_channel_id(SSL *s);
  890. int dtls1_client_hello(SSL *s);
  891. /* some server-only functions */
  892. int ssl3_get_initial_bytes(SSL *s);
  893. int ssl3_get_v2_client_hello(SSL *s);
  894. int ssl3_get_client_hello(SSL *s);
  895. int ssl3_send_server_hello(SSL *s);
  896. int ssl3_send_server_key_exchange(SSL *s);
  897. int ssl3_send_certificate_request(SSL *s);
  898. int ssl3_send_server_done(SSL *s);
  899. int ssl3_get_client_certificate(SSL *s);
  900. int ssl3_get_client_key_exchange(SSL *s);
  901. int ssl3_get_cert_verify(SSL *s);
  902. int ssl3_get_next_proto(SSL *s);
  903. int ssl3_get_channel_id(SSL *s);
  904. int dtls1_new(SSL *s);
  905. int dtls1_accept(SSL *s);
  906. int dtls1_connect(SSL *s);
  907. void dtls1_free(SSL *s);
  908. long dtls1_get_message(SSL *s, int st1, int stn, int mt, long max,
  909. enum ssl_hash_message_t hash_message, int *ok);
  910. int dtls1_get_record(SSL *s);
  911. int dtls1_dispatch_alert(SSL *s);
  912. int ssl_init_wbio_buffer(SSL *s, int push);
  913. void ssl_free_wbio_buffer(SSL *s);
  914. /* tls1_prf computes the TLS PRF function for |s| as described in RFC 5246,
  915. * section 5 and RFC 2246 section 5. It writes |out_len| bytes to |out|, using
  916. * |secret| as the secret and |label| as the label. |seed1| and |seed2| are
  917. * concatenated to form the seed parameter. It returns one on success and zero
  918. * on failure. */
  919. int tls1_prf(SSL *s, uint8_t *out, size_t out_len, const uint8_t *secret,
  920. size_t secret_len, const char *label, size_t label_len,
  921. const uint8_t *seed1, size_t seed1_len,
  922. const uint8_t *seed2, size_t seed2_len);
  923. int tls1_change_cipher_state(SSL *s, int which);
  924. int tls1_setup_key_block(SSL *s);
  925. int tls1_handshake_digest(SSL *s, uint8_t *out, size_t out_len);
  926. int tls1_final_finish_mac(SSL *s, const char *str, int slen, uint8_t *p);
  927. int tls1_cert_verify_mac(SSL *s, int md_nid, uint8_t *p);
  928. int tls1_generate_master_secret(SSL *s, uint8_t *out, const uint8_t *premaster,
  929. size_t premaster_len);
  930. int tls1_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
  931. const char *label, size_t label_len,
  932. const uint8_t *context, size_t context_len,
  933. int use_context);
  934. int tls1_alert_code(int code);
  935. int ssl3_alert_code(int code);
  936. char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx);
  937. int tls1_ec_curve_id2nid(uint16_t curve_id);
  938. int tls1_ec_nid2curve_id(uint16_t *out_curve_id, int nid);
  939. /* tls1_check_curve parses ECParameters out of |cbs|, modifying it. It
  940. * checks the curve is one of our preferences and writes the
  941. * NamedCurve value to |*out_curve_id|. It returns one on success and
  942. * zero on error. */
  943. int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id);
  944. /* tls1_get_shared_curve returns the NID of the first preferred shared curve
  945. * between client and server preferences. If none can be found, it returns
  946. * NID_undef. */
  947. int tls1_get_shared_curve(SSL *s);
  948. /* tls1_set_curves converts the array of |ncurves| NIDs pointed to by |curves|
  949. * into a newly allocated array of TLS curve IDs. On success, the function
  950. * returns one and writes the array to |*out_curve_ids| and its size to
  951. * |*out_curve_ids_len|. Otherwise, it returns zero. */
  952. int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
  953. const int *curves, size_t ncurves);
  954. /* tls1_check_ec_cert returns one if |x| is an ECC certificate with curve and
  955. * point format compatible with the client's preferences. Otherwise it returns
  956. * zero. */
  957. int tls1_check_ec_cert(SSL *s, X509 *x);
  958. /* tls1_check_ec_tmp_key returns one if the EC temporary key is compatible with
  959. * client extensions and zero otherwise. */
  960. int tls1_check_ec_tmp_key(SSL *s);
  961. int tls1_shared_list(SSL *s, const uint8_t *l1, size_t l1len, const uint8_t *l2,
  962. size_t l2len, int nmatch);
  963. uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *const buf,
  964. uint8_t *const limit, size_t header_len);
  965. uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *const buf,
  966. uint8_t *const limit);
  967. int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs);
  968. int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs);
  969. #define tlsext_tick_md EVP_sha256
  970. /* tls_process_ticket processes the session ticket extension. On success, it
  971. * sets |*out_session| to the decrypted session or NULL if the ticket was
  972. * rejected. It sets |*out_send_ticket| to whether a new ticket should be sent
  973. * at the end of the handshake. It returns one on success and zero on fatal
  974. * error. */
  975. int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
  976. int *out_send_ticket, const uint8_t *ticket,
  977. size_t ticket_len, const uint8_t *session_id,
  978. size_t session_id_len);
  979. /* tls12_get_sigandhash assembles the SignatureAndHashAlgorithm corresponding to
  980. * |ssl|'s private key and |md|. The two-byte value is written to |p|. It
  981. * returns one on success and zero on failure. */
  982. int tls12_get_sigandhash(SSL *ssl, uint8_t *p, const EVP_MD *md);
  983. int tls12_get_sigid(int pkey_type);
  984. const EVP_MD *tls12_get_hash(uint8_t hash_alg);
  985. /* tls1_channel_id_hash computes the hash to be signed by Channel ID and writes
  986. * it to |out|, which must contain at least |EVP_MAX_MD_SIZE| bytes. It returns
  987. * one on success and zero on failure. */
  988. int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len);
  989. int tls1_record_handshake_hashes_for_channel_id(SSL *s);
  990. int tls1_set_sigalgs_list(CERT *c, const char *str, int client);
  991. int tls1_set_sigalgs(CERT *c, const int *salg, size_t salglen, int client);
  992. /* ssl_ctx_log_rsa_client_key_exchange logs |premaster| to |ctx|, if logging is
  993. * enabled. It returns one on success and zero on failure. The entry is
  994. * identified by the first 8 bytes of |encrypted_premaster|. */
  995. int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
  996. const uint8_t *encrypted_premaster,
  997. size_t encrypted_premaster_len,
  998. const uint8_t *premaster,
  999. size_t premaster_len);
  1000. /* ssl_ctx_log_master_secret logs |master| to |ctx|, if logging is enabled. It
  1001. * returns one on success and zero on failure. The entry is identified by
  1002. * |client_random|. */
  1003. int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
  1004. size_t client_random_len, const uint8_t *master,
  1005. size_t master_len);
  1006. /* ssl3_can_false_start returns one if |s| is allowed to False Start and zero
  1007. * otherwise. */
  1008. int ssl3_can_false_start(const SSL *s);
  1009. /* ssl3_get_enc_method returns the SSL3_ENC_METHOD corresponding to
  1010. * |version|. */
  1011. const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version);
  1012. /* ssl3_get_max_server_version returns the maximum SSL/TLS version number
  1013. * supported by |s| as a server, or zero if all versions are disabled. */
  1014. uint16_t ssl3_get_max_server_version(const SSL *s);
  1015. /* ssl3_get_mutual_version selects the protocol version on |s| for a client
  1016. * which advertises |client_version|. If no suitable version exists, it returns
  1017. * zero. */
  1018. uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version);
  1019. /* ssl3_get_max_client_version returns the maximum protocol version configured
  1020. * for the client. It is guaranteed that the set of allowed versions at or below
  1021. * this maximum version is contiguous. If all versions are disabled, it returns
  1022. * zero. */
  1023. uint16_t ssl3_get_max_client_version(SSL *s);
  1024. /* ssl3_is_version_enabled returns one if |version| is an enabled protocol
  1025. * version for |s| and zero otherwise. */
  1026. int ssl3_is_version_enabled(SSL *s, uint16_t version);
  1027. /* ssl3_version_from_wire maps |wire_version| to a protocol version. For
  1028. * SSLv3/TLS, the version is returned as-is. For DTLS, the corresponding TLS
  1029. * version is used. Note that this mapping is not injective but preserves
  1030. * comparisons.
  1031. *
  1032. * TODO(davidben): To normalize some DTLS-specific code, move away from using
  1033. * the wire version except at API boundaries. */
  1034. uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version);
  1035. uint32_t ssl_get_algorithm_prf(SSL *s);
  1036. int tls1_process_sigalgs(SSL *s, const CBS *sigalgs);
  1037. /* tls1_choose_signing_digest returns a digest for use with |ssl|'s private key
  1038. * based on the peer's preferences the digests supported. */
  1039. const EVP_MD *tls1_choose_signing_digest(SSL *ssl);
  1040. size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs);
  1041. int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, SSL *s,
  1042. CBS *cbs, EVP_PKEY *pkey);
  1043. void ssl_set_client_disabled(SSL *s);
  1044. #endif /* OPENSSL_HEADER_SSL_INTERNAL_H */