Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

1046 строки
44 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 HEADER_SSL_LOCL_H
  142. #define HEADER_SSL_LOCL_H
  143. #include <openssl/base.h>
  144. #include <errno.h>
  145. #include <stdlib.h>
  146. #include <string.h>
  147. #include <time.h>
  148. #include <openssl/aead.h>
  149. #include <openssl/bio.h>
  150. #include <openssl/buf.h>
  151. #include <openssl/dsa.h>
  152. #include <openssl/err.h>
  153. #include <openssl/rsa.h>
  154. #include <openssl/ssl.h>
  155. #include <openssl/stack.h>
  156. #define c2l(c, l) \
  157. (l = ((unsigned long)(*((c)++))), l |= (((unsigned long)(*((c)++))) << 8), \
  158. l |= (((unsigned long)(*((c)++))) << 16), \
  159. l |= (((unsigned long)(*((c)++))) << 24))
  160. /* NOTE - c is not incremented as per c2l */
  161. #define c2ln(c, l1, l2, n) \
  162. { \
  163. c += n; \
  164. l1 = l2 = 0; \
  165. switch (n) { \
  166. case 8: \
  167. l2 = ((unsigned long)(*(--(c)))) << 24; \
  168. case 7: \
  169. l2 |= ((unsigned long)(*(--(c)))) << 16; \
  170. case 6: \
  171. l2 |= ((unsigned long)(*(--(c)))) << 8; \
  172. case 5: \
  173. l2 |= ((unsigned long)(*(--(c)))); \
  174. case 4: \
  175. l1 = ((unsigned long)(*(--(c)))) << 24; \
  176. case 3: \
  177. l1 |= ((unsigned long)(*(--(c)))) << 16; \
  178. case 2: \
  179. l1 |= ((unsigned long)(*(--(c)))) << 8; \
  180. case 1: \
  181. l1 |= ((unsigned long)(*(--(c)))); \
  182. } \
  183. }
  184. #define l2c(l, c) \
  185. (*((c)++) = (uint8_t)(((l)) & 0xff), \
  186. *((c)++) = (uint8_t)(((l) >> 8) & 0xff), \
  187. *((c)++) = (uint8_t)(((l) >> 16) & 0xff), \
  188. *((c)++) = (uint8_t)(((l) >> 24) & 0xff))
  189. #define n2l(c, l) \
  190. (l = ((unsigned long)(*((c)++))) << 24, \
  191. l |= ((unsigned long)(*((c)++))) << 16, \
  192. l |= ((unsigned long)(*((c)++))) << 8, l |= ((unsigned long)(*((c)++))))
  193. #define l2n(l, c) \
  194. (*((c)++) = (uint8_t)(((l) >> 24) & 0xff), \
  195. *((c)++) = (uint8_t)(((l) >> 16) & 0xff), \
  196. *((c)++) = (uint8_t)(((l) >> 8) & 0xff), \
  197. *((c)++) = (uint8_t)(((l)) & 0xff))
  198. #define l2n6(l, c) \
  199. (*((c)++) = (uint8_t)(((l) >> 40) & 0xff), \
  200. *((c)++) = (uint8_t)(((l) >> 32) & 0xff), \
  201. *((c)++) = (uint8_t)(((l) >> 24) & 0xff), \
  202. *((c)++) = (uint8_t)(((l) >> 16) & 0xff), \
  203. *((c)++) = (uint8_t)(((l) >> 8) & 0xff), \
  204. *((c)++) = (uint8_t)(((l)) & 0xff))
  205. #define l2n8(l, c) \
  206. (*((c)++) = (uint8_t)(((l) >> 56) & 0xff), \
  207. *((c)++) = (uint8_t)(((l) >> 48) & 0xff), \
  208. *((c)++) = (uint8_t)(((l) >> 40) & 0xff), \
  209. *((c)++) = (uint8_t)(((l) >> 32) & 0xff), \
  210. *((c)++) = (uint8_t)(((l) >> 24) & 0xff), \
  211. *((c)++) = (uint8_t)(((l) >> 16) & 0xff), \
  212. *((c)++) = (uint8_t)(((l) >> 8) & 0xff), \
  213. *((c)++) = (uint8_t)(((l)) & 0xff))
  214. #define n2l6(c, l) \
  215. (l = ((BN_ULLONG)(*((c)++))) << 40, l |= ((BN_ULLONG)(*((c)++))) << 32, \
  216. l |= ((BN_ULLONG)(*((c)++))) << 24, l |= ((BN_ULLONG)(*((c)++))) << 16, \
  217. l |= ((BN_ULLONG)(*((c)++))) << 8, l |= ((BN_ULLONG)(*((c)++))))
  218. /* NOTE - c is not incremented as per l2c */
  219. #define l2cn(l1, l2, c, n) \
  220. { \
  221. c += n; \
  222. switch (n) { \
  223. case 8: \
  224. *(--(c)) = (uint8_t)(((l2) >> 24) & 0xff); \
  225. case 7: \
  226. *(--(c)) = (uint8_t)(((l2) >> 16) & 0xff); \
  227. case 6: \
  228. *(--(c)) = (uint8_t)(((l2) >> 8) & 0xff); \
  229. case 5: \
  230. *(--(c)) = (uint8_t)(((l2)) & 0xff); \
  231. case 4: \
  232. *(--(c)) = (uint8_t)(((l1) >> 24) & 0xff); \
  233. case 3: \
  234. *(--(c)) = (uint8_t)(((l1) >> 16) & 0xff); \
  235. case 2: \
  236. *(--(c)) = (uint8_t)(((l1) >> 8) & 0xff); \
  237. case 1: \
  238. *(--(c)) = (uint8_t)(((l1)) & 0xff); \
  239. } \
  240. }
  241. #define n2s(c, s) \
  242. ((s = (((unsigned int)(c[0])) << 8) | (((unsigned int)(c[1])))), c += 2)
  243. #define s2n(s, c) \
  244. ((c[0] = (uint8_t)(((s) >> 8) & 0xff), \
  245. c[1] = (uint8_t)(((s)) & 0xff)), \
  246. c += 2)
  247. #define n2l3(c, l) \
  248. ((l = (((unsigned long)(c[0])) << 16) | (((unsigned long)(c[1])) << 8) | \
  249. (((unsigned long)(c[2])))), \
  250. c += 3)
  251. #define l2n3(l, c) \
  252. ((c[0] = (uint8_t)(((l) >> 16) & 0xff), \
  253. c[1] = (uint8_t)(((l) >> 8) & 0xff), \
  254. c[2] = (uint8_t)(((l)) & 0xff)), \
  255. c += 3)
  256. /* LOCAL STUFF */
  257. #define SSL_DECRYPT 0
  258. #define SSL_ENCRYPT 1
  259. #define TWO_BYTE_BIT 0x80
  260. #define SEC_ESC_BIT 0x40
  261. #define TWO_BYTE_MASK 0x7fff
  262. #define THREE_BYTE_MASK 0x3fff
  263. #define INC32(a) ((a) = ((a) + 1) & 0xffffffffL)
  264. #define DEC32(a) ((a) = ((a)-1) & 0xffffffffL)
  265. #define MAX_MAC_SIZE 20 /* up from 16 for SSLv3 */
  266. /* Define the Bitmasks for SSL_CIPHER.algorithms.
  267. *
  268. * This bits are used packed as dense as possible. If new methods/ciphers etc
  269. * will be added, the bits a likely to change, so this information is for
  270. * internal library use only, even though SSL_CIPHER.algorithms can be publicly
  271. * accessed. Use the according functions for cipher management instead.
  272. *
  273. * The bit mask handling in the selection and sorting scheme in
  274. * ssl_create_cipher_list() has only limited capabilities, reflecting that the
  275. * different entities within are mutually exclusive:
  276. * ONLY ONE BIT PER MASK CAN BE SET AT A TIME. */
  277. /* Bits for algorithm_mkey (key exchange algorithm) */
  278. #define SSL_kRSA 0x00000001L /* RSA key exchange */
  279. #define SSL_kEDH 0x00000002L /* tmp DH key no DH cert */
  280. #define SSL_kEECDH 0x00000004L /* ephemeral ECDH */
  281. #define SSL_kPSK 0x00000008L /* PSK */
  282. /* Bits for algorithm_auth (server authentication) */
  283. #define SSL_aRSA 0x00000001L /* RSA auth */
  284. #define SSL_aNULL 0x00000002L /* no auth (i.e. use ADH or AECDH) */
  285. #define SSL_aECDSA 0x00000004L /* ECDSA auth*/
  286. #define SSL_aPSK 0x00000008L /* PSK auth */
  287. /* Bits for algorithm_enc (symmetric encryption) */
  288. #define SSL_3DES 0x00000001L
  289. #define SSL_RC4 0x00000002L
  290. #define SSL_AES128 0x00000004L
  291. #define SSL_AES256 0x00000008L
  292. #define SSL_AES128GCM 0x00000010L
  293. #define SSL_AES256GCM 0x00000020L
  294. #define SSL_CHACHA20POLY1305 0x00000040L
  295. #define SSL_AES (SSL_AES128 | SSL_AES256 | SSL_AES128GCM | SSL_AES256GCM)
  296. /* Bits for algorithm_mac (symmetric authentication) */
  297. #define SSL_MD5 0x00000001L
  298. #define SSL_SHA1 0x00000002L
  299. #define SSL_SHA256 0x00000004L
  300. #define SSL_SHA384 0x00000008L
  301. /* Not a real MAC, just an indication it is part of cipher */
  302. #define SSL_AEAD 0x00000010L
  303. /* Bits for algorithm_ssl (protocol version) */
  304. #define SSL_SSLV3 0x00000002L
  305. #define SSL_TLSV1 SSL_SSLV3 /* for now */
  306. #define SSL_TLSV1_2 0x00000004L
  307. /* Bits for algorithm2 (handshake digests and other extra flags) */
  308. #define SSL_HANDSHAKE_MAC_MD5 0x10
  309. #define SSL_HANDSHAKE_MAC_SHA 0x20
  310. #define SSL_HANDSHAKE_MAC_SHA256 0x40
  311. #define SSL_HANDSHAKE_MAC_SHA384 0x80
  312. #define SSL_HANDSHAKE_MAC_DEFAULT \
  313. (SSL_HANDSHAKE_MAC_MD5 | SSL_HANDSHAKE_MAC_SHA)
  314. /* When adding new digest in the ssl_ciph.c and increment SSM_MD_NUM_IDX
  315. * make sure to update this constant too */
  316. #define SSL_MAX_DIGEST 4
  317. #define TLS1_PRF_DGST_MASK (0xff << TLS1_PRF_DGST_SHIFT)
  318. #define TLS1_PRF_DGST_SHIFT 10
  319. #define TLS1_PRF_MD5 (SSL_HANDSHAKE_MAC_MD5 << TLS1_PRF_DGST_SHIFT)
  320. #define TLS1_PRF_SHA1 (SSL_HANDSHAKE_MAC_SHA << TLS1_PRF_DGST_SHIFT)
  321. #define TLS1_PRF_SHA256 (SSL_HANDSHAKE_MAC_SHA256 << TLS1_PRF_DGST_SHIFT)
  322. #define TLS1_PRF_SHA384 (SSL_HANDSHAKE_MAC_SHA384 << TLS1_PRF_DGST_SHIFT)
  323. #define TLS1_PRF (TLS1_PRF_MD5 | TLS1_PRF_SHA1)
  324. #define TLSEXT_CHANNEL_ID_SIZE 128
  325. /* SSL_CIPHER_ALGORITHM2_AEAD is a flag in SSL_CIPHER.algorithm2 which
  326. * indicates that the cipher is implemented via an EVP_AEAD. */
  327. #define SSL_CIPHER_ALGORITHM2_AEAD (1 << 23)
  328. /* SSL_CIPHER_AEAD_FIXED_NONCE_LEN returns the number of bytes of fixed nonce
  329. * for an SSL_CIPHER* with the SSL_CIPHER_ALGORITHM2_AEAD flag. */
  330. #define SSL_CIPHER_AEAD_FIXED_NONCE_LEN(ssl_cipher) \
  331. (((ssl_cipher->algorithm2 >> 24) & 0xf) * 2)
  332. /* SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD is a flag in
  333. * SSL_CIPHER.algorithm2 which indicates that the variable part of the nonce is
  334. * included as a prefix of the record. (AES-GCM, for example, does with with an
  335. * 8-byte variable nonce.) */
  336. #define SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD (1<<22)
  337. /* SSL_CIPHER_ALGORITHM2_STATEFUL_AEAD is a flag in SSL_CIPHER.algorithm2 which
  338. * indicates that the AEAD is stateful and so doesn't take an nonce. This is
  339. * only true of legacy cipher suites. */
  340. #define SSL_CIPHER_ALGORITHM2_STATEFUL_AEAD (1<<28)
  341. /* Cipher strength information. */
  342. #define SSL_MEDIUM 0x00000001L
  343. #define SSL_HIGH 0x00000002L
  344. #define SSL_FIPS 0x00000004L
  345. /* we have used 000001ff - 23 bits left to go */
  346. /* Check if an SSL structure is using DTLS */
  347. #define SSL_IS_DTLS(s) (s->enc_method->enc_flags & SSL_ENC_FLAG_DTLS)
  348. /* See if we need explicit IV */
  349. #define SSL_USE_EXPLICIT_IV(s) \
  350. (s->enc_method->enc_flags & SSL_ENC_FLAG_EXPLICIT_IV)
  351. /* See if we use signature algorithms extension and signature algorithm before
  352. * signatures. */
  353. #define SSL_USE_SIGALGS(s) (s->enc_method->enc_flags & SSL_ENC_FLAG_SIGALGS)
  354. /* Allow TLS 1.2 ciphersuites: applies to DTLS 1.2 as well as TLS 1.2: may
  355. * apply to others in future. */
  356. #define SSL_USE_TLS1_2_CIPHERS(s) \
  357. (s->enc_method->enc_flags & SSL_ENC_FLAG_TLS1_2_CIPHERS)
  358. /* Determine if a client can use TLS 1.2 ciphersuites: can't rely on method
  359. * flags because it may not be set to correct version yet. */
  360. #define SSL_CLIENT_USE_TLS1_2_CIPHERS(s) \
  361. ((SSL_IS_DTLS(s) && s->client_version <= DTLS1_2_VERSION) || \
  362. (!SSL_IS_DTLS(s) && s->client_version >= TLS1_2_VERSION))
  363. /* Mostly for SSLv3 */
  364. #define SSL_PKEY_RSA_ENC 0
  365. #define SSL_PKEY_RSA_SIGN 1
  366. #define SSL_PKEY_ECC 2
  367. #define SSL_PKEY_NUM 3
  368. /* SSL_kRSA <- RSA_ENC | (RSA_TMP & RSA_SIGN) |
  369. * <- (EXPORT & (RSA_ENC | RSA_TMP) & RSA_SIGN)
  370. * SSL_kDH <- DH_ENC & (RSA_ENC | RSA_SIGN | DSA_SIGN)
  371. * SSL_kEDH <- RSA_ENC | RSA_SIGN | DSA_SIGN
  372. * SSL_aRSA <- RSA_ENC | RSA_SIGN
  373. * SSL_aDSS <- DSA_SIGN */
  374. #define PENDING_SESSION -10000
  375. #define CERTIFICATE_SELECTION_PENDING -10001
  376. /* From RFC4492, used in encoding the curve type in ECParameters */
  377. #define EXPLICIT_PRIME_CURVE_TYPE 1
  378. #define EXPLICIT_CHAR2_CURVE_TYPE 2
  379. #define NAMED_CURVE_TYPE 3
  380. /* Values for the |hash_message| parameter of |s->method->ssl_get_message|. */
  381. #define SSL_GET_MESSAGE_DONT_HASH_MESSAGE 0
  382. #define SSL_GET_MESSAGE_HASH_MESSAGE 1
  383. typedef struct cert_pkey_st {
  384. X509 *x509;
  385. EVP_PKEY *privatekey;
  386. /* Chain for this certificate */
  387. STACK_OF(X509) * chain;
  388. } CERT_PKEY;
  389. typedef struct cert_st {
  390. /* Current active set */
  391. CERT_PKEY *key; /* ALWAYS points to an element of the pkeys array
  392. * Probably it would make more sense to store
  393. * an index, not a pointer. */
  394. /* For clients the following masks are of *disabled* key and auth algorithms
  395. * based on the current session.
  396. *
  397. * TODO(davidben): Remove these. They get checked twice: when sending the
  398. * ClientHello and when processing the ServerHello. However, mask_ssl is a
  399. * different value both times. mask_k and mask_a are not, but is a
  400. * round-about way of checking the server's cipher was one of the advertised
  401. * ones. (Currently it checks the masks and then the list of ciphers prior to
  402. * applying the masks in ClientHello.) */
  403. unsigned long mask_k;
  404. unsigned long mask_a;
  405. unsigned long mask_ssl;
  406. DH *dh_tmp;
  407. DH *(*dh_tmp_cb)(SSL *ssl, int is_export, int keysize);
  408. EC_KEY *ecdh_tmp;
  409. /* Callback for generating ephemeral ECDH keys */
  410. EC_KEY *(*ecdh_tmp_cb)(SSL *ssl, int is_export, int keysize);
  411. /* Select ECDH parameters automatically */
  412. int ecdh_tmp_auto;
  413. /* Flags related to certificates */
  414. unsigned int cert_flags;
  415. CERT_PKEY pkeys[SSL_PKEY_NUM];
  416. /* Server-only: client_certificate_types is list of certificate types to
  417. * include in the CertificateRequest message.
  418. */
  419. uint8_t *client_certificate_types;
  420. size_t num_client_certificate_types;
  421. /* signature algorithms peer reports: e.g. supported signature
  422. * algorithms extension for server or as part of a certificate
  423. * request for client. */
  424. uint8_t *peer_sigalgs;
  425. /* Size of above array */
  426. size_t peer_sigalgslen;
  427. /* suppported signature algorithms.
  428. * When set on a client this is sent in the client hello as the
  429. * supported signature algorithms extension. For servers
  430. * it represents the signature algorithms we are willing to use. */
  431. uint8_t *conf_sigalgs;
  432. /* Size of above array */
  433. size_t conf_sigalgslen;
  434. /* Client authentication signature algorithms, if not set then
  435. * uses conf_sigalgs. On servers these will be the signature
  436. * algorithms sent to the client in a cerificate request for TLS 1.2.
  437. * On a client this represents the signature algortithms we are
  438. * willing to use for client authentication. */
  439. uint8_t *client_sigalgs;
  440. /* Size of above array */
  441. size_t client_sigalgslen;
  442. /* Signature algorithms shared by client and server: cached
  443. * because these are used most often. */
  444. TLS_SIGALGS *shared_sigalgs;
  445. size_t shared_sigalgslen;
  446. /* Certificate setup callback: if set is called whenever a
  447. * certificate may be required (client or server). the callback
  448. * can then examine any appropriate parameters and setup any
  449. * certificates required. This allows advanced applications
  450. * to select certificates on the fly: for example based on
  451. * supported signature algorithms or curves. */
  452. int (*cert_cb)(SSL *ssl, void *arg);
  453. void *cert_cb_arg;
  454. /* Optional X509_STORE for chain building or certificate validation
  455. * If NULL the parent SSL_CTX store is used instead. */
  456. X509_STORE *chain_store;
  457. X509_STORE *verify_store;
  458. /* Raw values of the cipher list from a client */
  459. uint8_t *ciphers_raw;
  460. size_t ciphers_rawlen;
  461. } CERT;
  462. typedef struct sess_cert_st {
  463. STACK_OF(X509) * cert_chain; /* as received from peer (not for SSL2) */
  464. /* The 'peer_...' members are used only by clients. */
  465. int peer_cert_type;
  466. CERT_PKEY *peer_key; /* points to an element of peer_pkeys (never NULL!) */
  467. CERT_PKEY peer_pkeys[SSL_PKEY_NUM];
  468. /* Obviously we don't have the private keys of these,
  469. * so maybe we shouldn't even use the CERT_PKEY type here. */
  470. DH *peer_dh_tmp;
  471. EC_KEY *peer_ecdh_tmp;
  472. } SESS_CERT;
  473. /* Structure containing decoded values of signature algorithms extension */
  474. struct tls_sigalgs_st {
  475. /* NID of hash algorithm */
  476. int hash_nid;
  477. /* NID of signature algorithm */
  478. int sign_nid;
  479. /* Combined hash and signature NID */
  480. int signandhash_nid;
  481. /* Raw values used in extension */
  482. uint8_t rsign;
  483. uint8_t rhash;
  484. };
  485. /* SSL_METHOD is a compatibility structure to support the legacy version-locked
  486. * methods. */
  487. struct ssl_method_st {
  488. /* version, if non-zero, is the only protocol version acceptable to an
  489. * SSL_CTX initialized from this method. */
  490. uint16_t version;
  491. /* method is the underlying SSL_PROTOCOL_METHOD that initializes the
  492. * SSL_CTX. */
  493. const SSL_PROTOCOL_METHOD *method;
  494. };
  495. /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
  496. struct ssl_protocol_method_st {
  497. int (*ssl_new)(SSL *s);
  498. void (*ssl_clear)(SSL *s);
  499. void (*ssl_free)(SSL *s);
  500. int (*ssl_accept)(SSL *s);
  501. int (*ssl_connect)(SSL *s);
  502. int (*ssl_read)(SSL *s, void *buf, int len);
  503. int (*ssl_peek)(SSL *s, void *buf, int len);
  504. int (*ssl_write)(SSL *s, const void *buf, int len);
  505. int (*ssl_shutdown)(SSL *s);
  506. int (*ssl_renegotiate)(SSL *s);
  507. int (*ssl_renegotiate_check)(SSL *s);
  508. long (*ssl_get_message)(SSL *s, int header_state, int body_state,
  509. int msg_type, long max, int hash_message, int *ok);
  510. int (*ssl_read_bytes)(SSL *s, int type, uint8_t *buf, int len, int peek);
  511. int (*ssl_write_bytes)(SSL *s, int type, const void *buf_, int len);
  512. int (*ssl_dispatch_alert)(SSL *s);
  513. long (*ssl_ctrl)(SSL *s, int cmd, long larg, void *parg);
  514. long (*ssl_ctx_ctrl)(SSL_CTX *ctx, int cmd, long larg, void *parg);
  515. int (*ssl_pending)(const SSL *s);
  516. int (*num_ciphers)(void);
  517. const SSL_CIPHER *(*get_cipher)(unsigned ncipher);
  518. int (*ssl_version)(void);
  519. long (*ssl_callback_ctrl)(SSL *s, int cb_id, void (*fp)(void));
  520. long (*ssl_ctx_callback_ctrl)(SSL_CTX *s, int cb_id, void (*fp)(void));
  521. };
  522. /* This is for the SSLv3/TLSv1.0 differences in crypto/hash stuff It is a bit
  523. * of a mess of functions, but hell, think of it as an opaque structure. */
  524. struct ssl3_enc_method {
  525. int (*enc)(SSL *, int);
  526. int (*mac)(SSL *, uint8_t *, int);
  527. int (*setup_key_block)(SSL *);
  528. int (*generate_master_secret)(SSL *, uint8_t *, uint8_t *, int);
  529. int (*change_cipher_state)(SSL *, int);
  530. int (*final_finish_mac)(SSL *, const char *, int, uint8_t *);
  531. int finish_mac_length;
  532. int (*cert_verify_mac)(SSL *, int, uint8_t *);
  533. const char *client_finished_label;
  534. int client_finished_label_len;
  535. const char *server_finished_label;
  536. int server_finished_label_len;
  537. int (*alert_value)(int);
  538. int (*export_keying_material)(SSL *, uint8_t *, size_t, const char *, size_t,
  539. const uint8_t *, size_t, int use_context);
  540. /* Various flags indicating protocol version requirements */
  541. unsigned int enc_flags;
  542. /* Handshake header length */
  543. unsigned int hhlen;
  544. /* Set the handshake header */
  545. void (*set_handshake_header)(SSL *s, int type, unsigned long len);
  546. /* Write out handshake message */
  547. int (*do_write)(SSL *s);
  548. };
  549. #define SSL_HM_HEADER_LENGTH(s) s->enc_method->hhlen
  550. #define ssl_handshake_start(s) \
  551. (((uint8_t *)s->init_buf->data) + s->enc_method->hhlen)
  552. #define ssl_set_handshake_header(s, htype, len) \
  553. s->enc_method->set_handshake_header(s, htype, len)
  554. #define ssl_do_write(s) s->enc_method->do_write(s)
  555. /* Values for enc_flags */
  556. /* Uses explicit IV for CBC mode */
  557. #define SSL_ENC_FLAG_EXPLICIT_IV 0x1
  558. /* Uses signature algorithms extension */
  559. #define SSL_ENC_FLAG_SIGALGS 0x2
  560. /* Uses SHA256 default PRF */
  561. #define SSL_ENC_FLAG_SHA256_PRF 0x4
  562. /* Is DTLS */
  563. #define SSL_ENC_FLAG_DTLS 0x8
  564. /* Allow TLS 1.2 ciphersuites: applies to DTLS 1.2 as well as TLS 1.2:
  565. * may apply to others in future. */
  566. #define SSL_ENC_FLAG_TLS1_2_CIPHERS 0x10
  567. /* ssl_aead_ctx_st contains information about an AEAD that is being used to
  568. * encrypt an SSL connection. */
  569. struct ssl_aead_ctx_st {
  570. EVP_AEAD_CTX ctx;
  571. /* fixed_nonce contains any bytes of the nonce that are fixed for all
  572. * records. */
  573. uint8_t fixed_nonce[8];
  574. uint8_t fixed_nonce_len, variable_nonce_len, tag_len;
  575. /* variable_nonce_included_in_record is non-zero if the variable nonce
  576. * for a record is included as a prefix before the ciphertext. */
  577. char variable_nonce_included_in_record;
  578. };
  579. extern const SSL_CIPHER ssl3_ciphers[];
  580. extern const SSL3_ENC_METHOD TLSv1_enc_data;
  581. extern const SSL3_ENC_METHOD TLSv1_1_enc_data;
  582. extern const SSL3_ENC_METHOD TLSv1_2_enc_data;
  583. extern const SSL3_ENC_METHOD SSLv3_enc_data;
  584. extern const SSL3_ENC_METHOD DTLSv1_enc_data;
  585. extern const SSL3_ENC_METHOD DTLSv1_2_enc_data;
  586. void ssl_clear_cipher_ctx(SSL *s);
  587. int ssl_clear_bad_session(SSL *s);
  588. CERT *ssl_cert_new(void);
  589. CERT *ssl_cert_dup(CERT *cert);
  590. int ssl_cert_inst(CERT **o);
  591. void ssl_cert_clear_certs(CERT *c);
  592. void ssl_cert_free(CERT *c);
  593. SESS_CERT *ssl_sess_cert_new(void);
  594. void ssl_sess_cert_free(SESS_CERT *sc);
  595. int ssl_set_peer_cert_type(SESS_CERT *c, int type);
  596. int ssl_get_prev_session(SSL *s, const struct ssl_early_callback_ctx *ctx);
  597. int ssl_cipher_id_cmp(const void *in_a, const void *in_b);
  598. int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp);
  599. STACK_OF(SSL_CIPHER) * ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs);
  600. int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) * sk, uint8_t *p);
  601. STACK_OF(SSL_CIPHER) *
  602. ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *meth,
  603. struct ssl_cipher_preference_list_st **pref,
  604. STACK_OF(SSL_CIPHER) * *sorted, const char *rule_str,
  605. CERT *c);
  606. struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
  607. struct ssl_cipher_preference_list_st *cipher_list);
  608. void ssl_cipher_preference_list_free(
  609. struct ssl_cipher_preference_list_st *cipher_list);
  610. struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
  611. STACK_OF(SSL_CIPHER) * ciphers);
  612. struct ssl_cipher_preference_list_st *ssl_get_cipher_preferences(SSL *s);
  613. int ssl_cipher_get_evp_aead(const SSL_SESSION *s, const EVP_AEAD **aead);
  614. int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
  615. const EVP_MD **md, int *mac_pkey_type,
  616. int *mac_secret_size);
  617. int ssl_cipher_get_mac(const SSL_SESSION *s, const EVP_MD **md,
  618. int *mac_pkey_type, int *mac_secret_size);
  619. int ssl_get_handshake_digest(int i, long *mask, const EVP_MD **md);
  620. int ssl_cipher_get_cert_index(const SSL_CIPHER *c);
  621. int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher);
  622. int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher);
  623. int ssl_cert_set0_chain(CERT *c, STACK_OF(X509) * chain);
  624. int ssl_cert_set1_chain(CERT *c, STACK_OF(X509) * chain);
  625. int ssl_cert_add0_chain_cert(CERT *c, X509 *x);
  626. int ssl_cert_add1_chain_cert(CERT *c, X509 *x);
  627. int ssl_cert_select_current(CERT *c, X509 *x);
  628. void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg);
  629. int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) * sk);
  630. int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l);
  631. int ssl_build_cert_chain(CERT *c, X509_STORE *chain_store, int flags);
  632. int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref);
  633. int ssl_undefined_function(SSL *s);
  634. int ssl_undefined_void_function(void);
  635. int ssl_undefined_const_function(const SSL *s);
  636. CERT_PKEY *ssl_get_server_send_pkey(const SSL *s);
  637. EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *c);
  638. int ssl_cert_type(EVP_PKEY *pkey);
  639. /* ssl_get_compatible_server_ciphers determines the key exchange and
  640. * authentication cipher suite masks compatible with the server configuration
  641. * and current ClientHello parameters of |s|. It sets |*out_mask_k| to the key
  642. * exchange mask and |*out_mask_a| to the authentication mask. */
  643. void ssl_get_compatible_server_ciphers(SSL *s, unsigned long *out_mask_k,
  644. unsigned long *out_mask_a);
  645. STACK_OF(SSL_CIPHER) * ssl_get_ciphers_by_id(SSL *s);
  646. int ssl_verify_alarm_type(long type);
  647. int ssl_fill_hello_random(SSL *s, int server, uint8_t *field, size_t len);
  648. const SSL_CIPHER *ssl3_get_cipher_by_value(uint16_t value);
  649. uint16_t ssl3_get_cipher_value(const SSL_CIPHER *c);
  650. int ssl3_init_finished_mac(SSL *s);
  651. int ssl3_send_server_certificate(SSL *s);
  652. int ssl3_send_new_session_ticket(SSL *s);
  653. int ssl3_send_cert_status(SSL *s);
  654. int ssl3_get_finished(SSL *s, int state_a, int state_b);
  655. int ssl3_setup_key_block(SSL *s);
  656. int ssl3_send_change_cipher_spec(SSL *s, int state_a, int state_b);
  657. int ssl3_change_cipher_state(SSL *s, int which);
  658. void ssl3_cleanup_key_block(SSL *s);
  659. int ssl3_do_write(SSL *s, int type);
  660. int ssl3_send_alert(SSL *s, int level, int desc);
  661. int ssl3_generate_master_secret(SSL *s, uint8_t *out, uint8_t *p, int len);
  662. int ssl3_get_req_cert_type(SSL *s, uint8_t *p);
  663. long ssl3_get_message(SSL *s, int header_state, int body_state, int msg_type,
  664. long max, int hash_message, int *ok);
  665. /* ssl3_hash_current_message incorporates the current handshake message into
  666. * the handshake hash. */
  667. void ssl3_hash_current_message(SSL *s);
  668. /* ssl3_cert_verify_hash writes the CertificateVerify hash into the bytes
  669. * pointed to by |out| and writes the number of bytes to |*out_len|. |out| must
  670. * have room for EVP_MAX_MD_SIZE bytes. For TLS 1.2 and up, |*out_md| is used
  671. * for the hash function, otherwise the hash function depends on the type of
  672. * |pkey| and is written to |*out_md|. It returns one on success and zero on
  673. * failure. */
  674. int ssl3_cert_verify_hash(SSL *s, uint8_t *out, size_t *out_len,
  675. const EVP_MD **out_md, EVP_PKEY *pkey);
  676. int ssl3_send_finished(SSL *s, int a, int b, const char *sender, int slen);
  677. int ssl3_num_ciphers(void);
  678. const SSL_CIPHER *ssl3_get_cipher(unsigned int u);
  679. int ssl3_renegotiate(SSL *ssl);
  680. int ssl3_renegotiate_check(SSL *ssl);
  681. int ssl3_dispatch_alert(SSL *s);
  682. int ssl3_expect_change_cipher_spec(SSL *s);
  683. int ssl3_read_bytes(SSL *s, int type, uint8_t *buf, int len, int peek);
  684. int ssl3_write_bytes(SSL *s, int type, const void *buf, int len);
  685. int ssl3_final_finish_mac(SSL *s, const char *sender, int slen, uint8_t *p);
  686. int ssl3_cert_verify_mac(SSL *s, int md_nid, uint8_t *p);
  687. void ssl3_finish_mac(SSL *s, const uint8_t *buf, int len);
  688. int ssl3_enc(SSL *s, int send_data);
  689. int n_ssl3_mac(SSL *ssl, uint8_t *md, int send_data);
  690. void ssl3_free_digest_list(SSL *s);
  691. unsigned long ssl3_output_cert_chain(SSL *s, CERT_PKEY *cpk);
  692. const SSL_CIPHER *ssl3_choose_cipher(
  693. SSL *ssl, STACK_OF(SSL_CIPHER) * clnt,
  694. struct ssl_cipher_preference_list_st *srvr);
  695. int ssl3_setup_buffers(SSL *s);
  696. int ssl3_setup_read_buffer(SSL *s);
  697. int ssl3_setup_write_buffer(SSL *s);
  698. int ssl3_release_read_buffer(SSL *s);
  699. int ssl3_release_write_buffer(SSL *s);
  700. enum should_free_handshake_buffer_t {
  701. free_handshake_buffer,
  702. dont_free_handshake_buffer,
  703. };
  704. int ssl3_digest_cached_records(SSL *s, enum should_free_handshake_buffer_t);
  705. int ssl3_new(SSL *s);
  706. void ssl3_free(SSL *s);
  707. int ssl3_accept(SSL *s);
  708. int ssl3_connect(SSL *s);
  709. int ssl3_read(SSL *s, void *buf, int len);
  710. int ssl3_peek(SSL *s, void *buf, int len);
  711. int ssl3_write(SSL *s, const void *buf, int len);
  712. int ssl3_shutdown(SSL *s);
  713. void ssl3_clear(SSL *s);
  714. long ssl3_ctrl(SSL *s, int cmd, long larg, void *parg);
  715. long ssl3_ctx_ctrl(SSL_CTX *s, int cmd, long larg, void *parg);
  716. long ssl3_callback_ctrl(SSL *s, int cmd, void (*fp)(void));
  717. long ssl3_ctx_callback_ctrl(SSL_CTX *s, int cmd, void (*fp)(void));
  718. int ssl3_pending(const SSL *s);
  719. void ssl3_record_sequence_update(uint8_t *seq);
  720. int ssl3_do_change_cipher_spec(SSL *ssl);
  721. void ssl3_set_handshake_header(SSL *s, int htype, unsigned long len);
  722. int ssl3_handshake_write(SSL *s);
  723. int dtls1_do_write(SSL *s, int type);
  724. int ssl3_read_n(SSL *s, int n, int max, int extend);
  725. int dtls1_read_bytes(SSL *s, int type, uint8_t *buf, int len, int peek);
  726. int ssl3_write_pending(SSL *s, int type, const uint8_t *buf, unsigned int len);
  727. void dtls1_set_message_header(SSL *s, uint8_t mt, unsigned long len,
  728. unsigned short seq_num, unsigned long frag_off,
  729. unsigned long frag_len);
  730. int dtls1_write_app_data_bytes(SSL *s, int type, const void *buf, int len);
  731. int dtls1_write_bytes(SSL *s, int type, const void *buf, int len);
  732. int dtls1_send_change_cipher_spec(SSL *s, int a, int b);
  733. int dtls1_send_finished(SSL *s, int a, int b, const char *sender, int slen);
  734. unsigned long dtls1_output_cert_chain(SSL *s, CERT_PKEY *cpk);
  735. int dtls1_read_failed(SSL *s, int code);
  736. int dtls1_buffer_message(SSL *s, int ccs);
  737. int dtls1_retransmit_message(SSL *s, unsigned short seq, unsigned long frag_off,
  738. int *found);
  739. int dtls1_get_queue_priority(unsigned short seq, int is_ccs);
  740. int dtls1_retransmit_buffered_messages(SSL *s);
  741. void dtls1_clear_record_buffer(SSL *s);
  742. void dtls1_get_message_header(uint8_t *data, struct hm_header_st *msg_hdr);
  743. void dtls1_get_ccs_header(uint8_t *data, struct ccs_header_st *ccs_hdr);
  744. void dtls1_reset_seq_numbers(SSL *s, int rw);
  745. int dtls1_check_timeout_num(SSL *s);
  746. int dtls1_handle_timeout(SSL *s);
  747. const SSL_CIPHER *dtls1_get_cipher(unsigned int u);
  748. void dtls1_start_timer(SSL *s);
  749. void dtls1_stop_timer(SSL *s);
  750. int dtls1_is_timer_expired(SSL *s);
  751. void dtls1_double_timeout(SSL *s);
  752. unsigned int dtls1_min_mtu(void);
  753. void dtls1_hm_fragment_free(hm_fragment *frag);
  754. /* some client-only functions */
  755. int ssl3_send_client_hello(SSL *s);
  756. int ssl3_get_server_hello(SSL *s);
  757. int ssl3_get_certificate_request(SSL *s);
  758. int ssl3_get_new_session_ticket(SSL *s);
  759. int ssl3_get_cert_status(SSL *s);
  760. int ssl3_get_server_done(SSL *s);
  761. int ssl3_send_cert_verify(SSL *s);
  762. int ssl3_send_client_certificate(SSL *s);
  763. int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey);
  764. int ssl3_send_client_key_exchange(SSL *s);
  765. int ssl3_get_server_key_exchange(SSL *s);
  766. int ssl3_get_server_certificate(SSL *s);
  767. int ssl3_check_cert_and_algorithm(SSL *s);
  768. int ssl3_send_next_proto(SSL *s);
  769. int ssl3_send_channel_id(SSL *s);
  770. int dtls1_client_hello(SSL *s);
  771. /* some server-only functions */
  772. int ssl3_get_initial_bytes(SSL *s);
  773. int ssl3_get_v2_client_hello(SSL *s);
  774. int ssl3_get_client_hello(SSL *s);
  775. int ssl3_send_server_hello(SSL *s);
  776. int ssl3_send_hello_request(SSL *s);
  777. int ssl3_send_server_key_exchange(SSL *s);
  778. int ssl3_send_certificate_request(SSL *s);
  779. int ssl3_send_server_done(SSL *s);
  780. int ssl3_get_client_certificate(SSL *s);
  781. int ssl3_get_client_key_exchange(SSL *s);
  782. int ssl3_get_cert_verify(SSL *s);
  783. int ssl3_get_next_proto(SSL *s);
  784. int ssl3_get_channel_id(SSL *s);
  785. int dtls1_new(SSL *s);
  786. int dtls1_accept(SSL *s);
  787. int dtls1_connect(SSL *s);
  788. void dtls1_free(SSL *s);
  789. void dtls1_clear(SSL *s);
  790. long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg);
  791. int dtls1_shutdown(SSL *s);
  792. long dtls1_get_message(SSL *s, int st1, int stn, int mt, long max,
  793. int hash_message, int *ok);
  794. int dtls1_get_record(SSL *s);
  795. int dtls1_dispatch_alert(SSL *s);
  796. int ssl_init_wbio_buffer(SSL *s, int push);
  797. void ssl_free_wbio_buffer(SSL *s);
  798. int tls1_change_cipher_state(SSL *s, int which);
  799. int tls1_setup_key_block(SSL *s);
  800. int tls1_enc(SSL *s, int snd);
  801. int tls1_handshake_digest(SSL *s, uint8_t *out, size_t out_len);
  802. int tls1_final_finish_mac(SSL *s, const char *str, int slen, uint8_t *p);
  803. int tls1_cert_verify_mac(SSL *s, int md_nid, uint8_t *p);
  804. int tls1_mac(SSL *ssl, uint8_t *md, int snd);
  805. int tls1_generate_master_secret(SSL *s, uint8_t *out, uint8_t *p, int len);
  806. int tls1_export_keying_material(SSL *s, uint8_t *out, size_t olen,
  807. const char *label, size_t llen,
  808. const uint8_t *p, size_t plen, int use_context);
  809. int tls1_alert_code(int code);
  810. int ssl3_alert_code(int code);
  811. int ssl_ok(SSL *s);
  812. int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s);
  813. char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx);
  814. int tls1_ec_curve_id2nid(uint16_t curve_id);
  815. uint16_t tls1_ec_nid2curve_id(int nid);
  816. /* tls1_check_curve parses ECParameters out of |cbs|, modifying it. It
  817. * checks the curve is one of our preferences and writes the
  818. * NamedCurve value to |*out_curve_id|. It returns one on success and
  819. * zero on error. */
  820. int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id);
  821. /* tls1_get_shared_curve returns the NID of the first preferred shared curve
  822. * between client and server preferences. If none can be found, it returns
  823. * NID_undef. */
  824. int tls1_get_shared_curve(SSL *s);
  825. /* tls1_set_curves converts the array of |ncurves| NIDs pointed to by |curves|
  826. * into a newly allocated array of TLS curve IDs. On success, the function
  827. * returns one and writes the array to |*out_curve_ids| and its size to
  828. * |*out_curve_ids_len|. Otherwise, it returns zero. */
  829. int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
  830. const int *curves, size_t ncurves);
  831. /* tls1_check_ec_cert returns one if |x| is an ECC certificate with curve and
  832. * point format compatible with the client's preferences. Otherwise it returns
  833. * zero. */
  834. int tls1_check_ec_cert(SSL *s, X509 *x);
  835. /* tls1_check_ec_tmp_key returns one if the EC temporary key is compatible with
  836. * client extensions and zero otherwise. */
  837. int tls1_check_ec_tmp_key(SSL *s);
  838. int tls1_shared_list(SSL *s, const uint8_t *l1, size_t l1len, const uint8_t *l2,
  839. size_t l2len, int nmatch);
  840. uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit,
  841. size_t header_len);
  842. uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit);
  843. int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs);
  844. int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs);
  845. int ssl_prepare_clienthello_tlsext(SSL *s);
  846. int ssl_prepare_serverhello_tlsext(SSL *s);
  847. #define tlsext_tick_md EVP_sha256
  848. int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
  849. SSL_SESSION **ret);
  850. int tls12_get_sigandhash(uint8_t *p, const EVP_PKEY *pk, const EVP_MD *md);
  851. int tls12_get_sigid(const EVP_PKEY *pk);
  852. const EVP_MD *tls12_get_hash(uint8_t hash_alg);
  853. int tls1_channel_id_hash(EVP_MD_CTX *ctx, SSL *s);
  854. int tls1_record_handshake_hashes_for_channel_id(SSL *s);
  855. int tls1_set_sigalgs_list(CERT *c, const char *str, int client);
  856. int tls1_set_sigalgs(CERT *c, const int *salg, size_t salglen, int client);
  857. /* ssl_ctx_log_rsa_client_key_exchange logs |premaster| to |ctx|, if logging is
  858. * enabled. It returns one on success and zero on failure. The entry is
  859. * identified by the first 8 bytes of |encrypted_premaster|. */
  860. int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
  861. const uint8_t *encrypted_premaster,
  862. size_t encrypted_premaster_len,
  863. const uint8_t *premaster,
  864. size_t premaster_len);
  865. /* ssl_ctx_log_master_secret logs |master| to |ctx|, if logging is enabled. It
  866. * returns one on success and zero on failure. The entry is identified by
  867. * |client_random|. */
  868. int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
  869. size_t client_random_len, const uint8_t *master,
  870. size_t master_len);
  871. int ssl3_can_cutthrough(const SSL *s);
  872. /* ssl3_get_enc_method returns the SSL3_ENC_METHOD corresponding to
  873. * |version|. */
  874. const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version);
  875. /* ssl3_get_max_server_version returns the maximum SSL/TLS version number
  876. * supported by |s| as a server, or zero if all versions are disabled. */
  877. uint16_t ssl3_get_max_server_version(const SSL *s);
  878. /* ssl3_get_mutual_version selects the protocol version on |s| for a client
  879. * which advertises |client_version|. If no suitable version exists, it returns
  880. * zero. */
  881. uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version);
  882. /* ssl3_get_max_client_version returns the maximum protocol version configured
  883. * for the client. It is guaranteed that the set of allowed versions at or below
  884. * this maximum version is contiguous. If all versions are disabled, it returns
  885. * zero. */
  886. uint16_t ssl3_get_max_client_version(SSL *s);
  887. /* ssl3_is_version_enabled returns one if |version| is an enabled protocol
  888. * version for |s| and zero otherwise. */
  889. int ssl3_is_version_enabled(SSL *s, uint16_t version);
  890. EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md);
  891. void ssl_clear_hash_ctx(EVP_MD_CTX **hash);
  892. int ssl_add_serverhello_renegotiate_ext(SSL *s, uint8_t *p, int *len,
  893. int maxlen);
  894. int ssl_parse_serverhello_renegotiate_ext(SSL *s, CBS *cbs, int *out_alert);
  895. int ssl_add_clienthello_renegotiate_ext(SSL *s, uint8_t *p, int *len,
  896. int maxlen);
  897. int ssl_parse_clienthello_renegotiate_ext(SSL *s, CBS *cbs, int *out_alert);
  898. long ssl_get_algorithm2(SSL *s);
  899. int tls1_process_sigalgs(SSL *s, const CBS *sigalgs);
  900. /* tls1_choose_signing_digest returns a digest for use with |pkey| based on the
  901. * peer's preferences recorded for |s| and the digests supported by |pkey|. */
  902. const EVP_MD *tls1_choose_signing_digest(SSL *s, EVP_PKEY *pkey);
  903. size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs);
  904. int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, SSL *s,
  905. CBS *cbs, EVP_PKEY *pkey);
  906. void ssl_set_client_disabled(SSL *s);
  907. int ssl_add_clienthello_use_srtp_ext(SSL *s, uint8_t *p, int *len, int maxlen);
  908. int ssl_parse_clienthello_use_srtp_ext(SSL *s, CBS *cbs, int *out_alert);
  909. int ssl_add_serverhello_use_srtp_ext(SSL *s, uint8_t *p, int *len, int maxlen);
  910. int ssl_parse_serverhello_use_srtp_ext(SSL *s, CBS *cbs, int *out_alert);
  911. /* s3_cbc.c */
  912. void ssl3_cbc_copy_mac(uint8_t *out, const SSL3_RECORD *rec, unsigned md_size,
  913. unsigned orig_len);
  914. int ssl3_cbc_remove_padding(const SSL *s, SSL3_RECORD *rec, unsigned block_size,
  915. unsigned mac_size);
  916. int tls1_cbc_remove_padding(const SSL *s, SSL3_RECORD *rec, unsigned block_size,
  917. unsigned mac_size);
  918. char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx);
  919. int ssl3_cbc_digest_record(const EVP_MD_CTX *ctx, uint8_t *md_out,
  920. size_t *md_out_size, const uint8_t header[13],
  921. const uint8_t *data, size_t data_plus_mac_size,
  922. size_t data_plus_mac_plus_padding_size,
  923. const uint8_t *mac_secret,
  924. unsigned mac_secret_length, char is_sslv3);
  925. #endif