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

Simplify handshake message size limits. A handshake message can go up to 2^24 bytes = 16MB which is a little large for the peer to force us to buffer. Accordingly, we bound the size of a handshake message. Rather than have a global limit, the existing logic uses a different limit at each state in the handshake state machine and, for certificates, allows configuring the maximum certificate size. This is nice in that we engage larger limits iff the relevant state is reachable from the handshake. Servers without client auth get a tighter limit "for free". However, this doesn't work for DTLS due to out-of-order messages and we use a simpler scheme for DTLS. This scheme also is tricky on optional messages and makes the handshake <-> message layer communication complex. Apart from an ignored 20,000 byte limit on ServerHello, the largest non-certificate limit is the common 16k limit on ClientHello. So this complexity wasn't buying us anything. Unify everything on the DTLS scheme except, so as not to regress bounds on client-auth-less servers, also correctly check for whether client auth is configured. The value of 16k was chosen based on this value. (The 20,000 byte ServerHello limit makes no sense. We can easily bound the ServerHello because servers may not send extensions we don't implement. But it gets overshadowed by the certificate anyway.) Change-Id: I00309b16d809a3c2a1543f99fd29c4163e3add81 Reviewed-on: https://boringssl-review.googlesource.com/7941 Reviewed-by: David Benjamin <davidben@google.com>
8 роки тому
Simplify handshake message size limits. A handshake message can go up to 2^24 bytes = 16MB which is a little large for the peer to force us to buffer. Accordingly, we bound the size of a handshake message. Rather than have a global limit, the existing logic uses a different limit at each state in the handshake state machine and, for certificates, allows configuring the maximum certificate size. This is nice in that we engage larger limits iff the relevant state is reachable from the handshake. Servers without client auth get a tighter limit "for free". However, this doesn't work for DTLS due to out-of-order messages and we use a simpler scheme for DTLS. This scheme also is tricky on optional messages and makes the handshake <-> message layer communication complex. Apart from an ignored 20,000 byte limit on ServerHello, the largest non-certificate limit is the common 16k limit on ClientHello. So this complexity wasn't buying us anything. Unify everything on the DTLS scheme except, so as not to regress bounds on client-auth-less servers, also correctly check for whether client auth is configured. The value of 16k was chosen based on this value. (The 20,000 byte ServerHello limit makes no sense. We can easily bound the ServerHello because servers may not send extensions we don't implement. But it gets overshadowed by the certificate anyway.) Change-Id: I00309b16d809a3c2a1543f99fd29c4163e3add81 Reviewed-on: https://boringssl-review.googlesource.com/7941 Reviewed-by: David Benjamin <davidben@google.com>
8 роки тому
Simplify handshake message size limits. A handshake message can go up to 2^24 bytes = 16MB which is a little large for the peer to force us to buffer. Accordingly, we bound the size of a handshake message. Rather than have a global limit, the existing logic uses a different limit at each state in the handshake state machine and, for certificates, allows configuring the maximum certificate size. This is nice in that we engage larger limits iff the relevant state is reachable from the handshake. Servers without client auth get a tighter limit "for free". However, this doesn't work for DTLS due to out-of-order messages and we use a simpler scheme for DTLS. This scheme also is tricky on optional messages and makes the handshake <-> message layer communication complex. Apart from an ignored 20,000 byte limit on ServerHello, the largest non-certificate limit is the common 16k limit on ClientHello. So this complexity wasn't buying us anything. Unify everything on the DTLS scheme except, so as not to regress bounds on client-auth-less servers, also correctly check for whether client auth is configured. The value of 16k was chosen based on this value. (The 20,000 byte ServerHello limit makes no sense. We can easily bound the ServerHello because servers may not send extensions we don't implement. But it gets overshadowed by the certificate anyway.) Change-Id: I00309b16d809a3c2a1543f99fd29c4163e3add81 Reviewed-on: https://boringssl-review.googlesource.com/7941 Reviewed-by: David Benjamin <davidben@google.com>
8 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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-2002 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. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  110. * ECC cipher suite support in OpenSSL originally developed by
  111. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */
  112. #include <openssl/ssl.h>
  113. #include <assert.h>
  114. #include <limits.h>
  115. #include <string.h>
  116. #include <openssl/buf.h>
  117. #include <openssl/bytestring.h>
  118. #include <openssl/err.h>
  119. #include <openssl/evp.h>
  120. #include <openssl/mem.h>
  121. #include <openssl/md5.h>
  122. #include <openssl/nid.h>
  123. #include <openssl/rand.h>
  124. #include <openssl/sha.h>
  125. #include <openssl/x509.h>
  126. #include "internal.h"
  127. SSL_HANDSHAKE *ssl_handshake_new(enum ssl_hs_wait_t (*do_handshake)(SSL *ssl)) {
  128. SSL_HANDSHAKE *hs = OPENSSL_malloc(sizeof(SSL_HANDSHAKE));
  129. if (hs == NULL) {
  130. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  131. return NULL;
  132. }
  133. memset(hs, 0, sizeof(SSL_HANDSHAKE));
  134. hs->do_handshake = do_handshake;
  135. hs->wait = ssl_hs_ok;
  136. return hs;
  137. }
  138. void ssl_handshake_clear_groups(SSL_HANDSHAKE *hs) {
  139. if (hs->groups == NULL) {
  140. return;
  141. }
  142. for (size_t i = 0; i < hs->groups_len; i++) {
  143. SSL_ECDH_CTX_cleanup(&hs->groups[i]);
  144. }
  145. OPENSSL_free(hs->groups);
  146. hs->groups = NULL;
  147. hs->groups_len = 0;
  148. }
  149. void ssl_handshake_free(SSL_HANDSHAKE *hs) {
  150. if (hs == NULL) {
  151. return;
  152. }
  153. OPENSSL_cleanse(hs->secret, sizeof(hs->secret));
  154. OPENSSL_cleanse(hs->traffic_secret_0, sizeof(hs->traffic_secret_0));
  155. ssl_handshake_clear_groups(hs);
  156. OPENSSL_free(hs->key_share_bytes);
  157. OPENSSL_free(hs->public_key);
  158. OPENSSL_free(hs->peer_sigalgs);
  159. OPENSSL_free(hs->peer_psk_identity_hint);
  160. OPENSSL_free(hs);
  161. }
  162. /* ssl3_do_write sends |ssl->init_buf| in records of type 'type'
  163. * (SSL3_RT_HANDSHAKE or SSL3_RT_CHANGE_CIPHER_SPEC). It returns 1 on success
  164. * and <= 0 on error. */
  165. static int ssl3_do_write(SSL *ssl, int type, const uint8_t *data, size_t len) {
  166. int ret = ssl3_write_bytes(ssl, type, data, len);
  167. if (ret <= 0) {
  168. return ret;
  169. }
  170. /* ssl3_write_bytes writes the data in its entirety. */
  171. assert((size_t)ret == len);
  172. ssl_do_msg_callback(ssl, 1 /* write */, ssl->version, type, data, len);
  173. return 1;
  174. }
  175. int ssl3_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type) {
  176. CBB_zero(cbb);
  177. if (ssl->s3->pending_message != NULL) {
  178. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  179. return 0;
  180. }
  181. /* Pick a modest size hint to save most of the |realloc| calls. */
  182. if (!CBB_init(cbb, 64) ||
  183. !CBB_add_u8(cbb, type) ||
  184. !CBB_add_u24_length_prefixed(cbb, body)) {
  185. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  186. return 0;
  187. }
  188. return 1;
  189. }
  190. int ssl3_finish_message(SSL *ssl, CBB *cbb) {
  191. if (ssl->s3->pending_message != NULL) {
  192. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  193. return 0;
  194. }
  195. uint8_t *msg = NULL;
  196. size_t len;
  197. if (!CBB_finish(cbb, &msg, &len) ||
  198. len > 0xffffffffu) {
  199. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  200. OPENSSL_free(msg);
  201. return 0;
  202. }
  203. ssl3_update_handshake_hash(ssl, msg, len);
  204. ssl->s3->pending_message = msg;
  205. ssl->s3->pending_message_len = (uint32_t)len;
  206. return 1;
  207. }
  208. int ssl3_write_message(SSL *ssl) {
  209. if (ssl->s3->pending_message == NULL) {
  210. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  211. return 0;
  212. }
  213. int ret = ssl3_do_write(ssl, SSL3_RT_HANDSHAKE, ssl->s3->pending_message,
  214. ssl->s3->pending_message_len);
  215. if (ret <= 0) {
  216. return ret;
  217. }
  218. OPENSSL_free(ssl->s3->pending_message);
  219. ssl->s3->pending_message = NULL;
  220. ssl->s3->pending_message_len = 0;
  221. return 1;
  222. }
  223. int ssl3_send_finished(SSL *ssl, int a, int b) {
  224. if (ssl->state == b) {
  225. return ssl->method->write_message(ssl);
  226. }
  227. int n = ssl->s3->enc_method->final_finish_mac(ssl, ssl->server,
  228. ssl->s3->tmp.finish_md);
  229. if (n == 0) {
  230. return 0;
  231. }
  232. ssl->s3->tmp.finish_md_len = n;
  233. /* Log the master secret, if logging is enabled. */
  234. if (!ssl_log_secret(ssl, "CLIENT_RANDOM",
  235. SSL_get_session(ssl)->master_key,
  236. SSL_get_session(ssl)->master_key_length)) {
  237. return 0;
  238. }
  239. /* Copy the finished so we can use it for renegotiation checks */
  240. if (ssl->server) {
  241. assert(n <= EVP_MAX_MD_SIZE);
  242. memcpy(ssl->s3->previous_server_finished, ssl->s3->tmp.finish_md, n);
  243. ssl->s3->previous_server_finished_len = n;
  244. } else {
  245. assert(n <= EVP_MAX_MD_SIZE);
  246. memcpy(ssl->s3->previous_client_finished, ssl->s3->tmp.finish_md, n);
  247. ssl->s3->previous_client_finished_len = n;
  248. }
  249. CBB cbb, body;
  250. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_FINISHED) ||
  251. !CBB_add_bytes(&body, ssl->s3->tmp.finish_md,
  252. ssl->s3->tmp.finish_md_len) ||
  253. !ssl->method->finish_message(ssl, &cbb)) {
  254. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  255. CBB_cleanup(&cbb);
  256. return -1;
  257. }
  258. ssl->state = b;
  259. return ssl->method->write_message(ssl);
  260. }
  261. /* ssl3_take_mac calculates the Finished MAC for the handshakes messages seen
  262. * so far. */
  263. static void ssl3_take_mac(SSL *ssl) {
  264. /* If no new cipher setup then return immediately: other functions will set
  265. * the appropriate error. */
  266. if (ssl->s3->tmp.new_cipher == NULL) {
  267. return;
  268. }
  269. ssl->s3->tmp.peer_finish_md_len = ssl->s3->enc_method->final_finish_mac(
  270. ssl, !ssl->server, ssl->s3->tmp.peer_finish_md);
  271. }
  272. int ssl3_get_finished(SSL *ssl) {
  273. int al;
  274. int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_FINISHED,
  275. ssl_dont_hash_message);
  276. if (ret <= 0) {
  277. return ret;
  278. }
  279. /* Snapshot the finished hash before incorporating the new message. */
  280. ssl3_take_mac(ssl);
  281. if (!ssl->method->hash_current_message(ssl)) {
  282. goto err;
  283. }
  284. size_t finished_len = ssl->s3->tmp.peer_finish_md_len;
  285. int finished_ok = ssl->init_num == finished_len &&
  286. CRYPTO_memcmp(ssl->init_msg, ssl->s3->tmp.peer_finish_md,
  287. finished_len) == 0;
  288. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  289. finished_ok = 1;
  290. #endif
  291. if (!finished_ok) {
  292. al = SSL_AD_DECRYPT_ERROR;
  293. OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED);
  294. goto f_err;
  295. }
  296. /* Copy the finished so we can use it for renegotiation checks */
  297. if (ssl->server) {
  298. assert(finished_len <= EVP_MAX_MD_SIZE);
  299. memcpy(ssl->s3->previous_client_finished, ssl->s3->tmp.peer_finish_md,
  300. finished_len);
  301. ssl->s3->previous_client_finished_len = finished_len;
  302. } else {
  303. assert(finished_len <= EVP_MAX_MD_SIZE);
  304. memcpy(ssl->s3->previous_server_finished, ssl->s3->tmp.peer_finish_md,
  305. finished_len);
  306. ssl->s3->previous_server_finished_len = finished_len;
  307. }
  308. return 1;
  309. f_err:
  310. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  311. err:
  312. return 0;
  313. }
  314. int ssl3_send_change_cipher_spec(SSL *ssl) {
  315. static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS};
  316. return ssl3_do_write(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec,
  317. sizeof(kChangeCipherSpec));
  318. }
  319. int ssl3_output_cert_chain(SSL *ssl) {
  320. CBB cbb, body;
  321. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CERTIFICATE) ||
  322. !ssl_add_cert_chain(ssl, &body) ||
  323. !ssl->method->finish_message(ssl, &cbb)) {
  324. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  325. CBB_cleanup(&cbb);
  326. return 0;
  327. }
  328. return 1;
  329. }
  330. size_t ssl_max_handshake_message_len(const SSL *ssl) {
  331. /* kMaxMessageLen is the default maximum message size for handshakes which do
  332. * not accept peer certificate chains. */
  333. static const size_t kMaxMessageLen = 16384;
  334. if (SSL_in_init(ssl)) {
  335. if ((!ssl->server || (ssl->verify_mode & SSL_VERIFY_PEER)) &&
  336. kMaxMessageLen < ssl->max_cert_list) {
  337. return ssl->max_cert_list;
  338. }
  339. return kMaxMessageLen;
  340. }
  341. if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  342. /* In TLS 1.2 and below, the largest acceptable post-handshake message is
  343. * a HelloRequest. */
  344. return 0;
  345. }
  346. if (ssl->server) {
  347. /* The largest acceptable post-handshake message for a server is a
  348. * KeyUpdate. We will never initiate post-handshake auth. */
  349. return 0;
  350. }
  351. /* Clients must accept NewSessionTicket and CertificateRequest, so allow the
  352. * default size. */
  353. return kMaxMessageLen;
  354. }
  355. static int extend_handshake_buffer(SSL *ssl, size_t length) {
  356. if (!BUF_MEM_reserve(ssl->init_buf, length)) {
  357. return -1;
  358. }
  359. while (ssl->init_buf->length < length) {
  360. int ret = ssl3_read_handshake_bytes(
  361. ssl, (uint8_t *)ssl->init_buf->data + ssl->init_buf->length,
  362. length - ssl->init_buf->length);
  363. if (ret <= 0) {
  364. return ret;
  365. }
  366. ssl->init_buf->length += (size_t)ret;
  367. }
  368. return 1;
  369. }
  370. static int read_v2_client_hello(SSL *ssl, int *out_is_v2_client_hello) {
  371. /* Read the first 5 bytes, the size of the TLS record header. This is
  372. * sufficient to detect a V2ClientHello and ensures that we never read beyond
  373. * the first record. */
  374. int ret = ssl_read_buffer_extend_to(ssl, SSL3_RT_HEADER_LENGTH);
  375. if (ret <= 0) {
  376. return ret;
  377. }
  378. const uint8_t *p = ssl_read_buffer(ssl);
  379. /* Some dedicated error codes for protocol mixups should the application wish
  380. * to interpret them differently. (These do not overlap with ClientHello or
  381. * V2ClientHello.) */
  382. if (strncmp("GET ", (const char *)p, 4) == 0 ||
  383. strncmp("POST ", (const char *)p, 5) == 0 ||
  384. strncmp("HEAD ", (const char *)p, 5) == 0 ||
  385. strncmp("PUT ", (const char *)p, 4) == 0) {
  386. OPENSSL_PUT_ERROR(SSL, SSL_R_HTTP_REQUEST);
  387. return -1;
  388. }
  389. if (strncmp("CONNE", (const char *)p, 5) == 0) {
  390. OPENSSL_PUT_ERROR(SSL, SSL_R_HTTPS_PROXY_REQUEST);
  391. return -1;
  392. }
  393. if ((p[0] & 0x80) == 0 || p[2] != SSL2_MT_CLIENT_HELLO ||
  394. p[3] != SSL3_VERSION_MAJOR) {
  395. /* Not a V2ClientHello. */
  396. *out_is_v2_client_hello = 0;
  397. return 1;
  398. }
  399. /* Determine the length of the V2ClientHello. */
  400. size_t msg_length = ((p[0] & 0x7f) << 8) | p[1];
  401. if (msg_length > (1024 * 4)) {
  402. OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
  403. return -1;
  404. }
  405. if (msg_length < SSL3_RT_HEADER_LENGTH - 2) {
  406. /* Reject lengths that are too short early. We have already read
  407. * |SSL3_RT_HEADER_LENGTH| bytes, so we should not attempt to process an
  408. * (invalid) V2ClientHello which would be shorter than that. */
  409. OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_LENGTH_MISMATCH);
  410. return -1;
  411. }
  412. /* Read the remainder of the V2ClientHello. */
  413. ret = ssl_read_buffer_extend_to(ssl, 2 + msg_length);
  414. if (ret <= 0) {
  415. return ret;
  416. }
  417. CBS v2_client_hello;
  418. CBS_init(&v2_client_hello, ssl_read_buffer(ssl) + 2, msg_length);
  419. /* The V2ClientHello without the length is incorporated into the handshake
  420. * hash. */
  421. if (!ssl3_update_handshake_hash(ssl, CBS_data(&v2_client_hello),
  422. CBS_len(&v2_client_hello))) {
  423. return -1;
  424. }
  425. ssl_do_msg_callback(ssl, 0 /* read */, SSL2_VERSION, 0,
  426. CBS_data(&v2_client_hello), CBS_len(&v2_client_hello));
  427. uint8_t msg_type;
  428. uint16_t version, cipher_spec_length, session_id_length, challenge_length;
  429. CBS cipher_specs, session_id, challenge;
  430. if (!CBS_get_u8(&v2_client_hello, &msg_type) ||
  431. !CBS_get_u16(&v2_client_hello, &version) ||
  432. !CBS_get_u16(&v2_client_hello, &cipher_spec_length) ||
  433. !CBS_get_u16(&v2_client_hello, &session_id_length) ||
  434. !CBS_get_u16(&v2_client_hello, &challenge_length) ||
  435. !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) ||
  436. !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) ||
  437. !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) ||
  438. CBS_len(&v2_client_hello) != 0) {
  439. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  440. return -1;
  441. }
  442. /* msg_type has already been checked. */
  443. assert(msg_type == SSL2_MT_CLIENT_HELLO);
  444. /* The client_random is the V2ClientHello challenge. Truncate or
  445. * left-pad with zeros as needed. */
  446. size_t rand_len = CBS_len(&challenge);
  447. if (rand_len > SSL3_RANDOM_SIZE) {
  448. rand_len = SSL3_RANDOM_SIZE;
  449. }
  450. uint8_t random[SSL3_RANDOM_SIZE];
  451. memset(random, 0, SSL3_RANDOM_SIZE);
  452. memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge),
  453. rand_len);
  454. /* Write out an equivalent SSLv3 ClientHello. */
  455. size_t max_v3_client_hello = SSL3_HM_HEADER_LENGTH + 2 /* version */ +
  456. SSL3_RANDOM_SIZE + 1 /* session ID length */ +
  457. 2 /* cipher list length */ +
  458. CBS_len(&cipher_specs) / 3 * 2 +
  459. 1 /* compression length */ + 1 /* compression */;
  460. CBB client_hello, hello_body, cipher_suites;
  461. CBB_zero(&client_hello);
  462. if (!BUF_MEM_reserve(ssl->init_buf, max_v3_client_hello) ||
  463. !CBB_init_fixed(&client_hello, (uint8_t *)ssl->init_buf->data,
  464. ssl->init_buf->max) ||
  465. !CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) ||
  466. !CBB_add_u24_length_prefixed(&client_hello, &hello_body) ||
  467. !CBB_add_u16(&hello_body, version) ||
  468. !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) ||
  469. /* No session id. */
  470. !CBB_add_u8(&hello_body, 0) ||
  471. !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) {
  472. CBB_cleanup(&client_hello);
  473. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  474. return -1;
  475. }
  476. /* Copy the cipher suites. */
  477. while (CBS_len(&cipher_specs) > 0) {
  478. uint32_t cipher_spec;
  479. if (!CBS_get_u24(&cipher_specs, &cipher_spec)) {
  480. CBB_cleanup(&client_hello);
  481. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  482. return -1;
  483. }
  484. /* Skip SSLv2 ciphers. */
  485. if ((cipher_spec & 0xff0000) != 0) {
  486. continue;
  487. }
  488. if (!CBB_add_u16(&cipher_suites, cipher_spec)) {
  489. CBB_cleanup(&client_hello);
  490. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  491. return -1;
  492. }
  493. }
  494. /* Add the null compression scheme and finish. */
  495. if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) ||
  496. !CBB_finish(&client_hello, NULL, &ssl->init_buf->length)) {
  497. CBB_cleanup(&client_hello);
  498. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  499. return -1;
  500. }
  501. /* Consume and discard the V2ClientHello. */
  502. ssl_read_buffer_consume(ssl, 2 + msg_length);
  503. ssl_read_buffer_discard(ssl);
  504. *out_is_v2_client_hello = 1;
  505. return 1;
  506. }
  507. int ssl3_get_message(SSL *ssl, int msg_type,
  508. enum ssl_hash_message_t hash_message) {
  509. again:
  510. /* Re-create the handshake buffer if needed. */
  511. if (ssl->init_buf == NULL) {
  512. ssl->init_buf = BUF_MEM_new();
  513. if (ssl->init_buf == NULL) {
  514. return -1;
  515. }
  516. }
  517. if (ssl->server && !ssl->s3->v2_hello_done) {
  518. /* Bypass the record layer for the first message to handle V2ClientHello. */
  519. assert(hash_message == ssl_hash_message);
  520. int is_v2_client_hello = 0;
  521. int ret = read_v2_client_hello(ssl, &is_v2_client_hello);
  522. if (ret <= 0) {
  523. return ret;
  524. }
  525. if (is_v2_client_hello) {
  526. /* V2ClientHello is hashed separately. */
  527. hash_message = ssl_dont_hash_message;
  528. }
  529. ssl->s3->v2_hello_done = 1;
  530. }
  531. if (ssl->s3->tmp.reuse_message) {
  532. /* A ssl_dont_hash_message call cannot be combined with reuse_message; the
  533. * ssl_dont_hash_message would have to have been applied to the previous
  534. * call. */
  535. assert(hash_message == ssl_hash_message);
  536. assert(ssl->init_msg != NULL);
  537. ssl->s3->tmp.reuse_message = 0;
  538. hash_message = ssl_dont_hash_message;
  539. } else {
  540. ssl3_release_current_message(ssl, 0 /* don't free buffer */);
  541. }
  542. /* Read the message header, if we haven't yet. */
  543. int ret = extend_handshake_buffer(ssl, SSL3_HM_HEADER_LENGTH);
  544. if (ret <= 0) {
  545. return ret;
  546. }
  547. /* Parse out the length. Cap it so the peer cannot force us to buffer up to
  548. * 2^24 bytes. */
  549. const uint8_t *p = (uint8_t *)ssl->init_buf->data;
  550. size_t msg_len = (((uint32_t)p[1]) << 16) | (((uint32_t)p[2]) << 8) | p[3];
  551. if (msg_len > ssl_max_handshake_message_len(ssl)) {
  552. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  553. OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE);
  554. return -1;
  555. }
  556. /* Read the message body, if we haven't yet. */
  557. ret = extend_handshake_buffer(ssl, SSL3_HM_HEADER_LENGTH + msg_len);
  558. if (ret <= 0) {
  559. return ret;
  560. }
  561. /* We have now received a complete message. */
  562. ssl_do_msg_callback(ssl, 0 /* read */, ssl->version, SSL3_RT_HANDSHAKE,
  563. ssl->init_buf->data, ssl->init_buf->length);
  564. ssl->s3->tmp.message_type = ((const uint8_t *)ssl->init_buf->data)[0];
  565. ssl->init_msg = (uint8_t*)ssl->init_buf->data + SSL3_HM_HEADER_LENGTH;
  566. ssl->init_num = ssl->init_buf->length - SSL3_HM_HEADER_LENGTH;
  567. /* Ignore stray HelloRequest messages in the handshake before TLS 1.3. Per RFC
  568. * 5246, section 7.4.1.1, the server may send HelloRequest at any time. */
  569. if (!ssl->server && SSL_in_init(ssl) &&
  570. (!ssl->s3->have_version || ssl3_protocol_version(ssl) < TLS1_3_VERSION) &&
  571. ssl->s3->tmp.message_type == SSL3_MT_HELLO_REQUEST &&
  572. ssl->init_num == 0) {
  573. goto again;
  574. }
  575. if (msg_type >= 0 && ssl->s3->tmp.message_type != msg_type) {
  576. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  577. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  578. return -1;
  579. }
  580. /* Feed this message into MAC computation. */
  581. if (hash_message == ssl_hash_message && !ssl3_hash_current_message(ssl)) {
  582. return -1;
  583. }
  584. return 1;
  585. }
  586. int ssl3_hash_current_message(SSL *ssl) {
  587. return ssl3_update_handshake_hash(ssl, (uint8_t *)ssl->init_buf->data,
  588. ssl->init_buf->length);
  589. }
  590. void ssl3_release_current_message(SSL *ssl, int free_buffer) {
  591. if (ssl->init_msg != NULL) {
  592. /* |init_buf| never contains data beyond the current message. */
  593. assert(SSL3_HM_HEADER_LENGTH + ssl->init_num == ssl->init_buf->length);
  594. /* Clear the current message. */
  595. ssl->init_msg = NULL;
  596. ssl->init_num = 0;
  597. ssl->init_buf->length = 0;
  598. }
  599. if (free_buffer) {
  600. BUF_MEM_free(ssl->init_buf);
  601. ssl->init_buf = NULL;
  602. }
  603. }
  604. int ssl_verify_alarm_type(long type) {
  605. int al;
  606. switch (type) {
  607. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
  608. case X509_V_ERR_UNABLE_TO_GET_CRL:
  609. case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
  610. al = SSL_AD_UNKNOWN_CA;
  611. break;
  612. case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
  613. case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
  614. case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
  615. case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
  616. case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
  617. case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
  618. case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
  619. case X509_V_ERR_CERT_NOT_YET_VALID:
  620. case X509_V_ERR_CRL_NOT_YET_VALID:
  621. case X509_V_ERR_CERT_UNTRUSTED:
  622. case X509_V_ERR_CERT_REJECTED:
  623. case X509_V_ERR_HOSTNAME_MISMATCH:
  624. case X509_V_ERR_EMAIL_MISMATCH:
  625. case X509_V_ERR_IP_ADDRESS_MISMATCH:
  626. al = SSL_AD_BAD_CERTIFICATE;
  627. break;
  628. case X509_V_ERR_CERT_SIGNATURE_FAILURE:
  629. case X509_V_ERR_CRL_SIGNATURE_FAILURE:
  630. al = SSL_AD_DECRYPT_ERROR;
  631. break;
  632. case X509_V_ERR_CERT_HAS_EXPIRED:
  633. case X509_V_ERR_CRL_HAS_EXPIRED:
  634. al = SSL_AD_CERTIFICATE_EXPIRED;
  635. break;
  636. case X509_V_ERR_CERT_REVOKED:
  637. al = SSL_AD_CERTIFICATE_REVOKED;
  638. break;
  639. case X509_V_ERR_UNSPECIFIED:
  640. case X509_V_ERR_OUT_OF_MEM:
  641. case X509_V_ERR_INVALID_CALL:
  642. case X509_V_ERR_STORE_LOOKUP:
  643. al = SSL_AD_INTERNAL_ERROR;
  644. break;
  645. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  646. case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
  647. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
  648. case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
  649. case X509_V_ERR_CERT_CHAIN_TOO_LONG:
  650. case X509_V_ERR_PATH_LENGTH_EXCEEDED:
  651. case X509_V_ERR_INVALID_CA:
  652. al = SSL_AD_UNKNOWN_CA;
  653. break;
  654. case X509_V_ERR_APPLICATION_VERIFICATION:
  655. al = SSL_AD_HANDSHAKE_FAILURE;
  656. break;
  657. case X509_V_ERR_INVALID_PURPOSE:
  658. al = SSL_AD_UNSUPPORTED_CERTIFICATE;
  659. break;
  660. default:
  661. al = SSL_AD_CERTIFICATE_UNKNOWN;
  662. break;
  663. }
  664. return al;
  665. }