Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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