s3_both.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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 "../crypto/internal.h"
  127. #include "internal.h"
  128. SSL_HANDSHAKE *ssl_handshake_new(SSL *ssl) {
  129. SSL_HANDSHAKE *hs = OPENSSL_malloc(sizeof(SSL_HANDSHAKE));
  130. if (hs == NULL) {
  131. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  132. return NULL;
  133. }
  134. OPENSSL_memset(hs, 0, sizeof(SSL_HANDSHAKE));
  135. hs->ssl = ssl;
  136. hs->wait = ssl_hs_ok;
  137. hs->state = SSL_ST_INIT;
  138. if (!SSL_TRANSCRIPT_init(&hs->transcript)) {
  139. ssl_handshake_free(hs);
  140. return NULL;
  141. }
  142. return hs;
  143. }
  144. void ssl_handshake_free(SSL_HANDSHAKE *hs) {
  145. if (hs == NULL) {
  146. return;
  147. }
  148. OPENSSL_cleanse(hs->secret, sizeof(hs->secret));
  149. OPENSSL_cleanse(hs->client_handshake_secret,
  150. sizeof(hs->client_handshake_secret));
  151. OPENSSL_cleanse(hs->server_handshake_secret,
  152. sizeof(hs->server_handshake_secret));
  153. OPENSSL_cleanse(hs->client_traffic_secret_0,
  154. sizeof(hs->client_traffic_secret_0));
  155. OPENSSL_cleanse(hs->server_traffic_secret_0,
  156. sizeof(hs->server_traffic_secret_0));
  157. SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
  158. SSL_TRANSCRIPT_cleanup(&hs->transcript);
  159. OPENSSL_free(hs->cookie);
  160. OPENSSL_free(hs->key_share_bytes);
  161. OPENSSL_free(hs->public_key);
  162. SSL_SESSION_free(hs->new_session);
  163. OPENSSL_free(hs->peer_sigalgs);
  164. OPENSSL_free(hs->peer_supported_group_list);
  165. OPENSSL_free(hs->peer_key);
  166. OPENSSL_free(hs->server_params);
  167. OPENSSL_free(hs->peer_psk_identity_hint);
  168. sk_X509_NAME_pop_free(hs->ca_names, X509_NAME_free);
  169. OPENSSL_free(hs->certificate_types);
  170. if (hs->key_block != NULL) {
  171. OPENSSL_cleanse(hs->key_block, hs->key_block_len);
  172. OPENSSL_free(hs->key_block);
  173. }
  174. OPENSSL_free(hs->hostname);
  175. EVP_PKEY_free(hs->peer_pubkey);
  176. OPENSSL_free(hs);
  177. }
  178. int ssl_check_message_type(SSL *ssl, int type) {
  179. if (ssl->s3->tmp.message_type != type) {
  180. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  181. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  182. ERR_add_error_dataf("got type %d, wanted type %d",
  183. ssl->s3->tmp.message_type, type);
  184. return 0;
  185. }
  186. return 1;
  187. }
  188. static int add_record_to_flight(SSL *ssl, uint8_t type, const uint8_t *in,
  189. size_t in_len) {
  190. /* We'll never add a flight while in the process of writing it out. */
  191. assert(ssl->s3->pending_flight_offset == 0);
  192. if (ssl->s3->pending_flight == NULL) {
  193. ssl->s3->pending_flight = BUF_MEM_new();
  194. if (ssl->s3->pending_flight == NULL) {
  195. return 0;
  196. }
  197. }
  198. size_t max_out = in_len + SSL_max_seal_overhead(ssl);
  199. size_t new_cap = ssl->s3->pending_flight->length + max_out;
  200. if (max_out < in_len || new_cap < max_out) {
  201. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  202. return 0;
  203. }
  204. size_t len;
  205. if (!BUF_MEM_reserve(ssl->s3->pending_flight, new_cap) ||
  206. !tls_seal_record(ssl, (uint8_t *)ssl->s3->pending_flight->data +
  207. ssl->s3->pending_flight->length,
  208. &len, max_out, type, in, in_len)) {
  209. return 0;
  210. }
  211. ssl->s3->pending_flight->length += len;
  212. return 1;
  213. }
  214. int ssl3_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type) {
  215. /* Pick a modest size hint to save most of the |realloc| calls. */
  216. if (!CBB_init(cbb, 64) ||
  217. !CBB_add_u8(cbb, type) ||
  218. !CBB_add_u24_length_prefixed(cbb, body)) {
  219. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  220. CBB_cleanup(cbb);
  221. return 0;
  222. }
  223. return 1;
  224. }
  225. int ssl3_finish_message(SSL *ssl, CBB *cbb, uint8_t **out_msg,
  226. size_t *out_len) {
  227. if (!CBB_finish(cbb, out_msg, out_len)) {
  228. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  229. return 0;
  230. }
  231. return 1;
  232. }
  233. int ssl3_add_message(SSL *ssl, uint8_t *msg, size_t len) {
  234. /* Add the message to the current flight, splitting into several records if
  235. * needed. */
  236. int ret = 0;
  237. size_t added = 0;
  238. do {
  239. size_t todo = len - added;
  240. if (todo > ssl->max_send_fragment) {
  241. todo = ssl->max_send_fragment;
  242. }
  243. if (!add_record_to_flight(ssl, SSL3_RT_HANDSHAKE, msg + added, todo)) {
  244. goto err;
  245. }
  246. added += todo;
  247. } while (added < len);
  248. ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HANDSHAKE, msg, len);
  249. /* TODO(svaldez): Move this up a layer to fix abstraction for SSL_TRANSCRIPT
  250. * on hs. */
  251. if (ssl->s3->hs != NULL &&
  252. !SSL_TRANSCRIPT_update(&ssl->s3->hs->transcript, msg, len)) {
  253. goto err;
  254. }
  255. ret = 1;
  256. err:
  257. OPENSSL_free(msg);
  258. return ret;
  259. }
  260. int ssl3_add_change_cipher_spec(SSL *ssl) {
  261. static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS};
  262. if (!add_record_to_flight(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec,
  263. sizeof(kChangeCipherSpec))) {
  264. return 0;
  265. }
  266. ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_CHANGE_CIPHER_SPEC,
  267. kChangeCipherSpec, sizeof(kChangeCipherSpec));
  268. return 1;
  269. }
  270. int ssl3_add_alert(SSL *ssl, uint8_t level, uint8_t desc) {
  271. uint8_t alert[2] = {level, desc};
  272. if (!add_record_to_flight(ssl, SSL3_RT_ALERT, alert, sizeof(alert))) {
  273. return 0;
  274. }
  275. ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_ALERT, alert, sizeof(alert));
  276. ssl_do_info_callback(ssl, SSL_CB_WRITE_ALERT, ((int)level << 8) | desc);
  277. return 1;
  278. }
  279. int ssl_add_message_cbb(SSL *ssl, CBB *cbb) {
  280. uint8_t *msg;
  281. size_t len;
  282. if (!ssl->method->finish_message(ssl, cbb, &msg, &len) ||
  283. !ssl->method->add_message(ssl, msg, len)) {
  284. return 0;
  285. }
  286. return 1;
  287. }
  288. int ssl3_flush_flight(SSL *ssl) {
  289. if (ssl->s3->pending_flight == NULL) {
  290. return 1;
  291. }
  292. if (ssl->s3->pending_flight->length > 0xffffffff ||
  293. ssl->s3->pending_flight->length > INT_MAX) {
  294. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  295. return -1;
  296. }
  297. /* The handshake flight buffer is mutually exclusive with application data.
  298. *
  299. * TODO(davidben): This will not be true when closure alerts use this. */
  300. if (ssl_write_buffer_is_pending(ssl)) {
  301. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  302. return -1;
  303. }
  304. /* Write the pending flight. */
  305. while (ssl->s3->pending_flight_offset < ssl->s3->pending_flight->length) {
  306. int ret = BIO_write(
  307. ssl->wbio,
  308. ssl->s3->pending_flight->data + ssl->s3->pending_flight_offset,
  309. ssl->s3->pending_flight->length - ssl->s3->pending_flight_offset);
  310. if (ret <= 0) {
  311. ssl->rwstate = SSL_WRITING;
  312. return ret;
  313. }
  314. ssl->s3->pending_flight_offset += ret;
  315. }
  316. if (BIO_flush(ssl->wbio) <= 0) {
  317. ssl->rwstate = SSL_WRITING;
  318. return -1;
  319. }
  320. BUF_MEM_free(ssl->s3->pending_flight);
  321. ssl->s3->pending_flight = NULL;
  322. ssl->s3->pending_flight_offset = 0;
  323. return 1;
  324. }
  325. int ssl3_send_finished(SSL_HANDSHAKE *hs) {
  326. SSL *const ssl = hs->ssl;
  327. const SSL_SESSION *session = SSL_get_session(ssl);
  328. uint8_t finished[EVP_MAX_MD_SIZE];
  329. size_t finished_len;
  330. if (!SSL_TRANSCRIPT_finish_mac(&hs->transcript, finished, &finished_len,
  331. session, ssl->server,
  332. ssl3_protocol_version(ssl))) {
  333. return 0;
  334. }
  335. /* Log the master secret, if logging is enabled. */
  336. if (!ssl_log_secret(ssl, "CLIENT_RANDOM",
  337. session->master_key,
  338. session->master_key_length)) {
  339. return 0;
  340. }
  341. /* Copy the Finished so we can use it for renegotiation checks. */
  342. if (ssl->version != SSL3_VERSION) {
  343. if (finished_len > sizeof(ssl->s3->previous_client_finished) ||
  344. finished_len > sizeof(ssl->s3->previous_server_finished)) {
  345. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  346. return -1;
  347. }
  348. if (ssl->server) {
  349. OPENSSL_memcpy(ssl->s3->previous_server_finished, finished, finished_len);
  350. ssl->s3->previous_server_finished_len = finished_len;
  351. } else {
  352. OPENSSL_memcpy(ssl->s3->previous_client_finished, finished, finished_len);
  353. ssl->s3->previous_client_finished_len = finished_len;
  354. }
  355. }
  356. CBB cbb, body;
  357. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_FINISHED) ||
  358. !CBB_add_bytes(&body, finished, finished_len) ||
  359. !ssl_add_message_cbb(ssl, &cbb)) {
  360. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  361. CBB_cleanup(&cbb);
  362. return -1;
  363. }
  364. return 1;
  365. }
  366. int ssl3_get_finished(SSL_HANDSHAKE *hs) {
  367. SSL *const ssl = hs->ssl;
  368. int ret = ssl->method->ssl_get_message(ssl);
  369. if (ret <= 0) {
  370. return ret;
  371. }
  372. if (!ssl_check_message_type(ssl, SSL3_MT_FINISHED)) {
  373. return -1;
  374. }
  375. /* Snapshot the finished hash before incorporating the new message. */
  376. uint8_t finished[EVP_MAX_MD_SIZE];
  377. size_t finished_len;
  378. if (!SSL_TRANSCRIPT_finish_mac(&hs->transcript, finished, &finished_len,
  379. SSL_get_session(ssl), !ssl->server,
  380. ssl3_protocol_version(ssl)) ||
  381. !ssl_hash_current_message(hs)) {
  382. return -1;
  383. }
  384. int finished_ok = ssl->init_num == finished_len &&
  385. CRYPTO_memcmp(ssl->init_msg, finished, finished_len) == 0;
  386. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  387. finished_ok = 1;
  388. #endif
  389. if (!finished_ok) {
  390. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  391. OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED);
  392. return -1;
  393. }
  394. /* Copy the Finished so we can use it for renegotiation checks. */
  395. if (ssl->version != SSL3_VERSION) {
  396. if (finished_len > sizeof(ssl->s3->previous_client_finished) ||
  397. finished_len > sizeof(ssl->s3->previous_server_finished)) {
  398. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  399. return -1;
  400. }
  401. if (ssl->server) {
  402. OPENSSL_memcpy(ssl->s3->previous_client_finished, finished, finished_len);
  403. ssl->s3->previous_client_finished_len = finished_len;
  404. } else {
  405. OPENSSL_memcpy(ssl->s3->previous_server_finished, finished, finished_len);
  406. ssl->s3->previous_server_finished_len = finished_len;
  407. }
  408. }
  409. return 1;
  410. }
  411. int ssl3_output_cert_chain(SSL *ssl) {
  412. CBB cbb, body;
  413. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CERTIFICATE) ||
  414. !ssl_add_cert_chain(ssl, &body) ||
  415. !ssl_add_message_cbb(ssl, &cbb)) {
  416. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  417. CBB_cleanup(&cbb);
  418. return 0;
  419. }
  420. return 1;
  421. }
  422. size_t ssl_max_handshake_message_len(const SSL *ssl) {
  423. /* kMaxMessageLen is the default maximum message size for handshakes which do
  424. * not accept peer certificate chains. */
  425. static const size_t kMaxMessageLen = 16384;
  426. if (SSL_in_init(ssl)) {
  427. if ((!ssl->server || (ssl->verify_mode & SSL_VERIFY_PEER)) &&
  428. kMaxMessageLen < ssl->max_cert_list) {
  429. return ssl->max_cert_list;
  430. }
  431. return kMaxMessageLen;
  432. }
  433. if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  434. /* In TLS 1.2 and below, the largest acceptable post-handshake message is
  435. * a HelloRequest. */
  436. return 0;
  437. }
  438. if (ssl->server) {
  439. /* The largest acceptable post-handshake message for a server is a
  440. * KeyUpdate. We will never initiate post-handshake auth. */
  441. return 1;
  442. }
  443. /* Clients must accept NewSessionTicket and CertificateRequest, so allow the
  444. * default size. */
  445. return kMaxMessageLen;
  446. }
  447. static int extend_handshake_buffer(SSL *ssl, size_t length) {
  448. if (!BUF_MEM_reserve(ssl->init_buf, length)) {
  449. return -1;
  450. }
  451. while (ssl->init_buf->length < length) {
  452. int ret = ssl3_read_handshake_bytes(
  453. ssl, (uint8_t *)ssl->init_buf->data + ssl->init_buf->length,
  454. length - ssl->init_buf->length);
  455. if (ret <= 0) {
  456. return ret;
  457. }
  458. ssl->init_buf->length += (size_t)ret;
  459. }
  460. return 1;
  461. }
  462. static int read_v2_client_hello(SSL *ssl) {
  463. /* Read the first 5 bytes, the size of the TLS record header. This is
  464. * sufficient to detect a V2ClientHello and ensures that we never read beyond
  465. * the first record. */
  466. int ret = ssl_read_buffer_extend_to(ssl, SSL3_RT_HEADER_LENGTH);
  467. if (ret <= 0) {
  468. return ret;
  469. }
  470. const uint8_t *p = ssl_read_buffer(ssl);
  471. /* Some dedicated error codes for protocol mixups should the application wish
  472. * to interpret them differently. (These do not overlap with ClientHello or
  473. * V2ClientHello.) */
  474. if (strncmp("GET ", (const char *)p, 4) == 0 ||
  475. strncmp("POST ", (const char *)p, 5) == 0 ||
  476. strncmp("HEAD ", (const char *)p, 5) == 0 ||
  477. strncmp("PUT ", (const char *)p, 4) == 0) {
  478. OPENSSL_PUT_ERROR(SSL, SSL_R_HTTP_REQUEST);
  479. return -1;
  480. }
  481. if (strncmp("CONNE", (const char *)p, 5) == 0) {
  482. OPENSSL_PUT_ERROR(SSL, SSL_R_HTTPS_PROXY_REQUEST);
  483. return -1;
  484. }
  485. if ((p[0] & 0x80) == 0 || p[2] != SSL2_MT_CLIENT_HELLO ||
  486. p[3] != SSL3_VERSION_MAJOR) {
  487. /* Not a V2ClientHello. */
  488. return 1;
  489. }
  490. /* Determine the length of the V2ClientHello. */
  491. size_t msg_length = ((p[0] & 0x7f) << 8) | p[1];
  492. if (msg_length > (1024 * 4)) {
  493. OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
  494. return -1;
  495. }
  496. if (msg_length < SSL3_RT_HEADER_LENGTH - 2) {
  497. /* Reject lengths that are too short early. We have already read
  498. * |SSL3_RT_HEADER_LENGTH| bytes, so we should not attempt to process an
  499. * (invalid) V2ClientHello which would be shorter than that. */
  500. OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_LENGTH_MISMATCH);
  501. return -1;
  502. }
  503. /* Read the remainder of the V2ClientHello. */
  504. ret = ssl_read_buffer_extend_to(ssl, 2 + msg_length);
  505. if (ret <= 0) {
  506. return ret;
  507. }
  508. CBS v2_client_hello;
  509. CBS_init(&v2_client_hello, ssl_read_buffer(ssl) + 2, msg_length);
  510. /* The V2ClientHello without the length is incorporated into the handshake
  511. * hash. This is only ever called at the start of the handshake, so hs is
  512. * guaranteed to be non-NULL. */
  513. if (!SSL_TRANSCRIPT_update(&ssl->s3->hs->transcript,
  514. CBS_data(&v2_client_hello),
  515. CBS_len(&v2_client_hello))) {
  516. return -1;
  517. }
  518. ssl_do_msg_callback(ssl, 0 /* read */, 0 /* V2ClientHello */,
  519. CBS_data(&v2_client_hello), CBS_len(&v2_client_hello));
  520. uint8_t msg_type;
  521. uint16_t version, cipher_spec_length, session_id_length, challenge_length;
  522. CBS cipher_specs, session_id, challenge;
  523. if (!CBS_get_u8(&v2_client_hello, &msg_type) ||
  524. !CBS_get_u16(&v2_client_hello, &version) ||
  525. !CBS_get_u16(&v2_client_hello, &cipher_spec_length) ||
  526. !CBS_get_u16(&v2_client_hello, &session_id_length) ||
  527. !CBS_get_u16(&v2_client_hello, &challenge_length) ||
  528. !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) ||
  529. !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) ||
  530. !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) ||
  531. CBS_len(&v2_client_hello) != 0) {
  532. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  533. return -1;
  534. }
  535. /* msg_type has already been checked. */
  536. assert(msg_type == SSL2_MT_CLIENT_HELLO);
  537. /* The client_random is the V2ClientHello challenge. Truncate or
  538. * left-pad with zeros as needed. */
  539. size_t rand_len = CBS_len(&challenge);
  540. if (rand_len > SSL3_RANDOM_SIZE) {
  541. rand_len = SSL3_RANDOM_SIZE;
  542. }
  543. uint8_t random[SSL3_RANDOM_SIZE];
  544. OPENSSL_memset(random, 0, SSL3_RANDOM_SIZE);
  545. OPENSSL_memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge),
  546. rand_len);
  547. /* Write out an equivalent SSLv3 ClientHello. */
  548. size_t max_v3_client_hello = SSL3_HM_HEADER_LENGTH + 2 /* version */ +
  549. SSL3_RANDOM_SIZE + 1 /* session ID length */ +
  550. 2 /* cipher list length */ +
  551. CBS_len(&cipher_specs) / 3 * 2 +
  552. 1 /* compression length */ + 1 /* compression */;
  553. CBB client_hello, hello_body, cipher_suites;
  554. CBB_zero(&client_hello);
  555. if (!BUF_MEM_reserve(ssl->init_buf, max_v3_client_hello) ||
  556. !CBB_init_fixed(&client_hello, (uint8_t *)ssl->init_buf->data,
  557. ssl->init_buf->max) ||
  558. !CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) ||
  559. !CBB_add_u24_length_prefixed(&client_hello, &hello_body) ||
  560. !CBB_add_u16(&hello_body, version) ||
  561. !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) ||
  562. /* No session id. */
  563. !CBB_add_u8(&hello_body, 0) ||
  564. !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) {
  565. CBB_cleanup(&client_hello);
  566. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  567. return -1;
  568. }
  569. /* Copy the cipher suites. */
  570. while (CBS_len(&cipher_specs) > 0) {
  571. uint32_t cipher_spec;
  572. if (!CBS_get_u24(&cipher_specs, &cipher_spec)) {
  573. CBB_cleanup(&client_hello);
  574. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  575. return -1;
  576. }
  577. /* Skip SSLv2 ciphers. */
  578. if ((cipher_spec & 0xff0000) != 0) {
  579. continue;
  580. }
  581. if (!CBB_add_u16(&cipher_suites, cipher_spec)) {
  582. CBB_cleanup(&client_hello);
  583. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  584. return -1;
  585. }
  586. }
  587. /* Add the null compression scheme and finish. */
  588. if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) ||
  589. !CBB_finish(&client_hello, NULL, &ssl->init_buf->length)) {
  590. CBB_cleanup(&client_hello);
  591. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  592. return -1;
  593. }
  594. /* Consume and discard the V2ClientHello. */
  595. ssl_read_buffer_consume(ssl, 2 + msg_length);
  596. ssl_read_buffer_discard(ssl);
  597. ssl->s3->is_v2_hello = 1;
  598. /* This is the first message, so hs must be non-NULL. */
  599. ssl->s3->hs->v2_clienthello = 1;
  600. return 1;
  601. }
  602. int ssl3_get_message(SSL *ssl) {
  603. /* Re-create the handshake buffer if needed. */
  604. if (ssl->init_buf == NULL) {
  605. ssl->init_buf = BUF_MEM_new();
  606. if (ssl->init_buf == NULL) {
  607. return -1;
  608. }
  609. }
  610. if (ssl->server && !ssl->s3->v2_hello_done) {
  611. /* Bypass the record layer for the first message to handle V2ClientHello. */
  612. int ret = read_v2_client_hello(ssl);
  613. if (ret <= 0) {
  614. return ret;
  615. }
  616. ssl->s3->v2_hello_done = 1;
  617. }
  618. if (ssl->s3->tmp.reuse_message) {
  619. /* There must be a current message. */
  620. assert(ssl->init_msg != NULL);
  621. ssl->s3->tmp.reuse_message = 0;
  622. } else {
  623. ssl3_release_current_message(ssl, 0 /* don't free buffer */);
  624. }
  625. /* Read the message header, if we haven't yet. */
  626. int ret = extend_handshake_buffer(ssl, SSL3_HM_HEADER_LENGTH);
  627. if (ret <= 0) {
  628. return ret;
  629. }
  630. /* Parse out the length. Cap it so the peer cannot force us to buffer up to
  631. * 2^24 bytes. */
  632. const uint8_t *p = (uint8_t *)ssl->init_buf->data;
  633. size_t msg_len = (((uint32_t)p[1]) << 16) | (((uint32_t)p[2]) << 8) | p[3];
  634. if (msg_len > ssl_max_handshake_message_len(ssl)) {
  635. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  636. OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE);
  637. return -1;
  638. }
  639. /* Read the message body, if we haven't yet. */
  640. ret = extend_handshake_buffer(ssl, SSL3_HM_HEADER_LENGTH + msg_len);
  641. if (ret <= 0) {
  642. return ret;
  643. }
  644. /* We have now received a complete message. */
  645. ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HANDSHAKE, ssl->init_buf->data,
  646. ssl->init_buf->length);
  647. ssl->s3->tmp.message_type = ((const uint8_t *)ssl->init_buf->data)[0];
  648. ssl->init_msg = (uint8_t*)ssl->init_buf->data + SSL3_HM_HEADER_LENGTH;
  649. ssl->init_num = ssl->init_buf->length - SSL3_HM_HEADER_LENGTH;
  650. return 1;
  651. }
  652. void ssl3_get_current_message(const SSL *ssl, CBS *out) {
  653. CBS_init(out, (uint8_t *)ssl->init_buf->data, ssl->init_buf->length);
  654. }
  655. int ssl_hash_current_message(SSL_HANDSHAKE *hs) {
  656. /* V2ClientHellos are hashed implicitly. */
  657. if (hs->ssl->s3->is_v2_hello) {
  658. return 1;
  659. }
  660. CBS cbs;
  661. hs->ssl->method->get_current_message(hs->ssl, &cbs);
  662. return SSL_TRANSCRIPT_update(&hs->transcript, CBS_data(&cbs), CBS_len(&cbs));
  663. }
  664. void ssl3_release_current_message(SSL *ssl, int free_buffer) {
  665. if (ssl->init_msg != NULL) {
  666. /* |init_buf| never contains data beyond the current message. */
  667. assert(SSL3_HM_HEADER_LENGTH + ssl->init_num == ssl->init_buf->length);
  668. /* Clear the current message. */
  669. ssl->init_msg = NULL;
  670. ssl->init_num = 0;
  671. ssl->init_buf->length = 0;
  672. ssl->s3->is_v2_hello = 0;
  673. }
  674. if (free_buffer) {
  675. BUF_MEM_free(ssl->init_buf);
  676. ssl->init_buf = NULL;
  677. }
  678. }
  679. int ssl_verify_alarm_type(long type) {
  680. int al;
  681. switch (type) {
  682. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
  683. case X509_V_ERR_UNABLE_TO_GET_CRL:
  684. case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
  685. al = SSL_AD_UNKNOWN_CA;
  686. break;
  687. case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
  688. case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
  689. case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
  690. case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
  691. case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
  692. case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
  693. case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
  694. case X509_V_ERR_CERT_NOT_YET_VALID:
  695. case X509_V_ERR_CRL_NOT_YET_VALID:
  696. case X509_V_ERR_CERT_UNTRUSTED:
  697. case X509_V_ERR_CERT_REJECTED:
  698. case X509_V_ERR_HOSTNAME_MISMATCH:
  699. case X509_V_ERR_EMAIL_MISMATCH:
  700. case X509_V_ERR_IP_ADDRESS_MISMATCH:
  701. al = SSL_AD_BAD_CERTIFICATE;
  702. break;
  703. case X509_V_ERR_CERT_SIGNATURE_FAILURE:
  704. case X509_V_ERR_CRL_SIGNATURE_FAILURE:
  705. al = SSL_AD_DECRYPT_ERROR;
  706. break;
  707. case X509_V_ERR_CERT_HAS_EXPIRED:
  708. case X509_V_ERR_CRL_HAS_EXPIRED:
  709. al = SSL_AD_CERTIFICATE_EXPIRED;
  710. break;
  711. case X509_V_ERR_CERT_REVOKED:
  712. al = SSL_AD_CERTIFICATE_REVOKED;
  713. break;
  714. case X509_V_ERR_UNSPECIFIED:
  715. case X509_V_ERR_OUT_OF_MEM:
  716. case X509_V_ERR_INVALID_CALL:
  717. case X509_V_ERR_STORE_LOOKUP:
  718. al = SSL_AD_INTERNAL_ERROR;
  719. break;
  720. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  721. case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
  722. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
  723. case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
  724. case X509_V_ERR_CERT_CHAIN_TOO_LONG:
  725. case X509_V_ERR_PATH_LENGTH_EXCEEDED:
  726. case X509_V_ERR_INVALID_CA:
  727. al = SSL_AD_UNKNOWN_CA;
  728. break;
  729. case X509_V_ERR_APPLICATION_VERIFICATION:
  730. al = SSL_AD_HANDSHAKE_FAILURE;
  731. break;
  732. case X509_V_ERR_INVALID_PURPOSE:
  733. al = SSL_AD_UNSUPPORTED_CERTIFICATE;
  734. break;
  735. default:
  736. al = SSL_AD_CERTIFICATE_UNKNOWN;
  737. break;
  738. }
  739. return al;
  740. }
  741. int ssl_parse_extensions(const CBS *cbs, uint8_t *out_alert,
  742. const SSL_EXTENSION_TYPE *ext_types,
  743. size_t num_ext_types, int ignore_unknown) {
  744. /* Reset everything. */
  745. for (size_t i = 0; i < num_ext_types; i++) {
  746. *ext_types[i].out_present = 0;
  747. CBS_init(ext_types[i].out_data, NULL, 0);
  748. }
  749. CBS copy = *cbs;
  750. while (CBS_len(&copy) != 0) {
  751. uint16_t type;
  752. CBS data;
  753. if (!CBS_get_u16(&copy, &type) ||
  754. !CBS_get_u16_length_prefixed(&copy, &data)) {
  755. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  756. *out_alert = SSL_AD_DECODE_ERROR;
  757. return 0;
  758. }
  759. const SSL_EXTENSION_TYPE *ext_type = NULL;
  760. for (size_t i = 0; i < num_ext_types; i++) {
  761. if (type == ext_types[i].type) {
  762. ext_type = &ext_types[i];
  763. break;
  764. }
  765. }
  766. if (ext_type == NULL) {
  767. if (ignore_unknown) {
  768. continue;
  769. }
  770. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
  771. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  772. return 0;
  773. }
  774. /* Duplicate ext_types are forbidden. */
  775. if (*ext_type->out_present) {
  776. OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_EXTENSION);
  777. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  778. return 0;
  779. }
  780. *ext_type->out_present = 1;
  781. *ext_type->out_data = data;
  782. }
  783. return 1;
  784. }