You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

d1_clnt.c 17 KiB

Set rwstate consistently. We reset it to SSL_NOTHING at the start of ever SSL_get_error-using operation. Then we only set it to a non-NOTHING value in the rest of the stack on error paths. Currently, ssl->rwstate is set all over the place. Sometimes the pattern is: ssl->rwstate = SSL_WRITING; if (BIO_write(...) <= 0) { goto err; } ssl->rwstate = SSL_NOTHING; Sometimes we only set it to the non-NOTHING value on error. if (BIO_write(...) <= 0) { ssl->rwstate = SSL_WRITING; } ssl->rwstate = SSL_NOTHING; Sometimes we just set it to SSL_NOTHING far from any callback in random places. The third case is arbitrary and clearly should be removed. But, in the second case, we sometimes forget to undo it afterwards. This is largely harmless since an error in the error queue overrides rwstate, but we don't always put something in the error queue (falling back to SSL_ERROR_SYSCALL for "I'm not sure why it failed. Perhaps it was one of your callbacks? Check your errno equivalent."), but in that case a stray rwstate value will cause it to be wrong. We could fix the cases where we fail to set SSL_NOTHING on success cases, but this doesn't account for there being multiple SSL_get_error operations. The consumer may have an SSL_read and an SSL_write running concurrently. Instead, it seems the best option is to lift the SSL_NOTHING reset to the operations and set SSL_WRITING and friends as in the second case. (Someday hopefully we can fix this to just be an enum that is internally returned. It can convert to something stateful at the API layer.) Change-Id: I54665ec066a64eb0e48a06e2fcd0d2681a42df7f Reviewed-on: https://boringssl-review.googlesource.com/7453 Reviewed-by: David Benjamin <davidben@google.com>
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. * DTLS implementation written by Nagendra Modadugu
  3. * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * openssl-core@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  59. * All rights reserved.
  60. *
  61. * This package is an SSL implementation written
  62. * by Eric Young (eay@cryptsoft.com).
  63. * The implementation was written so as to conform with Netscapes SSL.
  64. *
  65. * This library is free for commercial and non-commercial use as long as
  66. * the following conditions are aheared to. The following conditions
  67. * apply to all code found in this distribution, be it the RC4, RSA,
  68. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  69. * included with this distribution is covered by the same copyright terms
  70. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  71. *
  72. * Copyright remains Eric Young's, and as such any Copyright notices in
  73. * the code are not to be removed.
  74. * If this package is used in a product, Eric Young should be given attribution
  75. * as the author of the parts of the library used.
  76. * This can be in the form of a textual message at program startup or
  77. * in documentation (online or textual) provided with the package.
  78. *
  79. * Redistribution and use in source and binary forms, with or without
  80. * modification, are permitted provided that the following conditions
  81. * are met:
  82. * 1. Redistributions of source code must retain the copyright
  83. * notice, this list of conditions and the following disclaimer.
  84. * 2. Redistributions in binary form must reproduce the above copyright
  85. * notice, this list of conditions and the following disclaimer in the
  86. * documentation and/or other materials provided with the distribution.
  87. * 3. All advertising materials mentioning features or use of this software
  88. * must display the following acknowledgement:
  89. * "This product includes cryptographic software written by
  90. * Eric Young (eay@cryptsoft.com)"
  91. * The word 'cryptographic' can be left out if the rouines from the library
  92. * being used are not cryptographic related :-).
  93. * 4. If you include any Windows specific code (or a derivative thereof) from
  94. * the apps directory (application code) you must include an acknowledgement:
  95. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  96. *
  97. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  98. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  99. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  100. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  101. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  102. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  103. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  104. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  105. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  106. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  107. * SUCH DAMAGE.
  108. *
  109. * The licence and distribution terms for any publically available version or
  110. * derivative of this code cannot be changed. i.e. this code cannot simply be
  111. * copied and put under another distribution licence
  112. * [including the GNU Public Licence.]
  113. */
  114. #include <openssl/ssl.h>
  115. #include <assert.h>
  116. #include <string.h>
  117. #include <openssl/bn.h>
  118. #include <openssl/buf.h>
  119. #include <openssl/dh.h>
  120. #include <openssl/evp.h>
  121. #include <openssl/err.h>
  122. #include <openssl/md5.h>
  123. #include <openssl/mem.h>
  124. #include <openssl/rand.h>
  125. #include "internal.h"
  126. static int dtls1_get_hello_verify(SSL *ssl);
  127. int dtls1_connect(SSL *ssl) {
  128. BUF_MEM *buf = NULL;
  129. void (*cb)(const SSL *ssl, int type, int value) = NULL;
  130. int ret = -1;
  131. int new_state, state, skip = 0;
  132. assert(ssl->handshake_func == dtls1_connect);
  133. assert(!ssl->server);
  134. assert(SSL_IS_DTLS(ssl));
  135. ERR_clear_system_error();
  136. if (ssl->info_callback != NULL) {
  137. cb = ssl->info_callback;
  138. } else if (ssl->ctx->info_callback != NULL) {
  139. cb = ssl->ctx->info_callback;
  140. }
  141. for (;;) {
  142. state = ssl->state;
  143. switch (ssl->state) {
  144. case SSL_ST_CONNECT:
  145. if (cb != NULL) {
  146. cb(ssl, SSL_CB_HANDSHAKE_START, 1);
  147. }
  148. if (ssl->init_buf == NULL) {
  149. buf = BUF_MEM_new();
  150. if (buf == NULL ||
  151. !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
  152. ret = -1;
  153. goto end;
  154. }
  155. ssl->init_buf = buf;
  156. buf = NULL;
  157. }
  158. if (!ssl_init_wbio_buffer(ssl, 0)) {
  159. ret = -1;
  160. goto end;
  161. }
  162. /* don't push the buffering BIO quite yet */
  163. ssl->state = SSL3_ST_CW_CLNT_HELLO_A;
  164. ssl->init_num = 0;
  165. ssl->d1->send_cookie = 0;
  166. ssl->hit = 0;
  167. break;
  168. case SSL3_ST_CW_CLNT_HELLO_A:
  169. case SSL3_ST_CW_CLNT_HELLO_B:
  170. ssl->shutdown = 0;
  171. dtls1_start_timer(ssl);
  172. ret = ssl3_send_client_hello(ssl);
  173. if (ret <= 0) {
  174. goto end;
  175. }
  176. if (ssl->d1->send_cookie) {
  177. ssl->state = SSL3_ST_CW_FLUSH;
  178. ssl->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A;
  179. } else {
  180. ssl->state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
  181. }
  182. ssl->init_num = 0;
  183. /* turn on buffering for the next lot of output */
  184. if (ssl->bbio != ssl->wbio) {
  185. ssl->wbio = BIO_push(ssl->bbio, ssl->wbio);
  186. }
  187. break;
  188. case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
  189. case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
  190. ret = dtls1_get_hello_verify(ssl);
  191. if (ret <= 0) {
  192. goto end;
  193. }
  194. if (ssl->d1->send_cookie) {
  195. /* start again, with a cookie */
  196. dtls1_stop_timer(ssl);
  197. ssl->state = SSL3_ST_CW_CLNT_HELLO_A;
  198. } else {
  199. ssl->state = SSL3_ST_CR_SRVR_HELLO_A;
  200. }
  201. ssl->init_num = 0;
  202. break;
  203. case SSL3_ST_CR_SRVR_HELLO_A:
  204. case SSL3_ST_CR_SRVR_HELLO_B:
  205. ret = ssl3_get_server_hello(ssl);
  206. if (ret <= 0) {
  207. goto end;
  208. }
  209. if (ssl->hit) {
  210. ssl->state = SSL3_ST_CR_CHANGE;
  211. if (ssl->tlsext_ticket_expected) {
  212. /* receive renewed session ticket */
  213. ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
  214. }
  215. } else {
  216. ssl->state = SSL3_ST_CR_CERT_A;
  217. }
  218. ssl->init_num = 0;
  219. break;
  220. case SSL3_ST_CR_CERT_A:
  221. case SSL3_ST_CR_CERT_B:
  222. if (ssl_cipher_has_server_public_key(ssl->s3->tmp.new_cipher)) {
  223. ret = ssl3_get_server_certificate(ssl);
  224. if (ret <= 0) {
  225. goto end;
  226. }
  227. if (ssl->s3->tmp.certificate_status_expected) {
  228. ssl->state = SSL3_ST_CR_CERT_STATUS_A;
  229. } else {
  230. ssl->state = SSL3_ST_VERIFY_SERVER_CERT;
  231. }
  232. } else {
  233. skip = 1;
  234. ssl->state = SSL3_ST_CR_KEY_EXCH_A;
  235. }
  236. ssl->init_num = 0;
  237. break;
  238. case SSL3_ST_VERIFY_SERVER_CERT:
  239. ret = ssl3_verify_server_cert(ssl);
  240. if (ret <= 0) {
  241. goto end;
  242. }
  243. ssl->state = SSL3_ST_CR_KEY_EXCH_A;
  244. ssl->init_num = 0;
  245. break;
  246. case SSL3_ST_CR_KEY_EXCH_A:
  247. case SSL3_ST_CR_KEY_EXCH_B:
  248. ret = ssl3_get_server_key_exchange(ssl);
  249. if (ret <= 0) {
  250. goto end;
  251. }
  252. ssl->state = SSL3_ST_CR_CERT_REQ_A;
  253. ssl->init_num = 0;
  254. break;
  255. case SSL3_ST_CR_CERT_REQ_A:
  256. case SSL3_ST_CR_CERT_REQ_B:
  257. ret = ssl3_get_certificate_request(ssl);
  258. if (ret <= 0) {
  259. goto end;
  260. }
  261. ssl->state = SSL3_ST_CR_SRVR_DONE_A;
  262. ssl->init_num = 0;
  263. break;
  264. case SSL3_ST_CR_SRVR_DONE_A:
  265. case SSL3_ST_CR_SRVR_DONE_B:
  266. ret = ssl3_get_server_done(ssl);
  267. if (ret <= 0) {
  268. goto end;
  269. }
  270. dtls1_stop_timer(ssl);
  271. if (ssl->s3->tmp.cert_req) {
  272. ssl->s3->tmp.next_state = SSL3_ST_CW_CERT_A;
  273. } else {
  274. ssl->s3->tmp.next_state = SSL3_ST_CW_KEY_EXCH_A;
  275. }
  276. ssl->init_num = 0;
  277. ssl->state = ssl->s3->tmp.next_state;
  278. break;
  279. case SSL3_ST_CW_CERT_A:
  280. case SSL3_ST_CW_CERT_B:
  281. case SSL3_ST_CW_CERT_C:
  282. case SSL3_ST_CW_CERT_D:
  283. dtls1_start_timer(ssl);
  284. ret = ssl3_send_client_certificate(ssl);
  285. if (ret <= 0) {
  286. goto end;
  287. }
  288. ssl->state = SSL3_ST_CW_KEY_EXCH_A;
  289. ssl->init_num = 0;
  290. break;
  291. case SSL3_ST_CW_KEY_EXCH_A:
  292. case SSL3_ST_CW_KEY_EXCH_B:
  293. dtls1_start_timer(ssl);
  294. ret = ssl3_send_client_key_exchange(ssl);
  295. if (ret <= 0) {
  296. goto end;
  297. }
  298. /* For TLS, cert_req is set to 2, so a cert chain
  299. * of nothing is sent, but no verify packet is sent */
  300. if (ssl->s3->tmp.cert_req == 1) {
  301. ssl->state = SSL3_ST_CW_CERT_VRFY_A;
  302. } else {
  303. ssl->state = SSL3_ST_CW_CHANGE_A;
  304. }
  305. ssl->init_num = 0;
  306. break;
  307. case SSL3_ST_CW_CERT_VRFY_A:
  308. case SSL3_ST_CW_CERT_VRFY_B:
  309. case SSL3_ST_CW_CERT_VRFY_C:
  310. dtls1_start_timer(ssl);
  311. ret = ssl3_send_cert_verify(ssl);
  312. if (ret <= 0) {
  313. goto end;
  314. }
  315. ssl->state = SSL3_ST_CW_CHANGE_A;
  316. ssl->init_num = 0;
  317. break;
  318. case SSL3_ST_CW_CHANGE_A:
  319. case SSL3_ST_CW_CHANGE_B:
  320. if (!ssl->hit) {
  321. dtls1_start_timer(ssl);
  322. }
  323. ret = dtls1_send_change_cipher_spec(ssl, SSL3_ST_CW_CHANGE_A,
  324. SSL3_ST_CW_CHANGE_B);
  325. if (ret <= 0) {
  326. goto end;
  327. }
  328. ssl->state = SSL3_ST_CW_FINISHED_A;
  329. ssl->init_num = 0;
  330. if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
  331. ret = -1;
  332. goto end;
  333. }
  334. break;
  335. case SSL3_ST_CW_FINISHED_A:
  336. case SSL3_ST_CW_FINISHED_B:
  337. if (!ssl->hit) {
  338. dtls1_start_timer(ssl);
  339. }
  340. ret = ssl3_send_finished(ssl, SSL3_ST_CW_FINISHED_A,
  341. SSL3_ST_CW_FINISHED_B);
  342. if (ret <= 0) {
  343. goto end;
  344. }
  345. ssl->state = SSL3_ST_CW_FLUSH;
  346. if (ssl->hit) {
  347. ssl->s3->tmp.next_state = SSL_ST_OK;
  348. } else {
  349. /* Allow NewSessionTicket if ticket expected */
  350. if (ssl->tlsext_ticket_expected) {
  351. ssl->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
  352. } else {
  353. ssl->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
  354. }
  355. }
  356. ssl->init_num = 0;
  357. break;
  358. case SSL3_ST_CR_SESSION_TICKET_A:
  359. case SSL3_ST_CR_SESSION_TICKET_B:
  360. ret = ssl3_get_new_session_ticket(ssl);
  361. if (ret <= 0) {
  362. goto end;
  363. }
  364. ssl->state = SSL3_ST_CR_CHANGE;
  365. ssl->init_num = 0;
  366. break;
  367. case SSL3_ST_CR_CERT_STATUS_A:
  368. case SSL3_ST_CR_CERT_STATUS_B:
  369. ret = ssl3_get_cert_status(ssl);
  370. if (ret <= 0) {
  371. goto end;
  372. }
  373. ssl->state = SSL3_ST_VERIFY_SERVER_CERT;
  374. ssl->init_num = 0;
  375. break;
  376. case SSL3_ST_CR_CHANGE:
  377. ret = ssl->method->ssl_read_change_cipher_spec(ssl);
  378. if (ret <= 0) {
  379. goto end;
  380. }
  381. if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
  382. ret = -1;
  383. goto end;
  384. }
  385. ssl->state = SSL3_ST_CR_FINISHED_A;
  386. break;
  387. case SSL3_ST_CR_FINISHED_A:
  388. case SSL3_ST_CR_FINISHED_B:
  389. ret =
  390. ssl3_get_finished(ssl, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
  391. if (ret <= 0) {
  392. goto end;
  393. }
  394. dtls1_stop_timer(ssl);
  395. if (ssl->hit) {
  396. ssl->state = SSL3_ST_CW_CHANGE_A;
  397. } else {
  398. ssl->state = SSL_ST_OK;
  399. }
  400. ssl->init_num = 0;
  401. break;
  402. case SSL3_ST_CW_FLUSH:
  403. if (BIO_flush(ssl->wbio) <= 0) {
  404. ssl->rwstate = SSL_WRITING;
  405. ret = -1;
  406. goto end;
  407. }
  408. ssl->state = ssl->s3->tmp.next_state;
  409. break;
  410. case SSL_ST_OK:
  411. /* clean a few things up */
  412. ssl3_cleanup_key_block(ssl);
  413. /* Remove write buffering now. */
  414. ssl_free_wbio_buffer(ssl);
  415. ssl->init_num = 0;
  416. ssl->s3->initial_handshake_complete = 1;
  417. ssl_update_cache(ssl, SSL_SESS_CACHE_CLIENT);
  418. ret = 1;
  419. if (cb != NULL) {
  420. cb(ssl, SSL_CB_HANDSHAKE_DONE, 1);
  421. }
  422. /* done with handshaking */
  423. ssl->d1->handshake_read_seq = 0;
  424. ssl->d1->next_handshake_write_seq = 0;
  425. goto end;
  426. default:
  427. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
  428. ret = -1;
  429. goto end;
  430. }
  431. /* did we do anything? */
  432. if (!ssl->s3->tmp.reuse_message && !skip) {
  433. if ((cb != NULL) && (ssl->state != state)) {
  434. new_state = ssl->state;
  435. ssl->state = state;
  436. cb(ssl, SSL_CB_CONNECT_LOOP, 1);
  437. ssl->state = new_state;
  438. }
  439. }
  440. skip = 0;
  441. }
  442. end:
  443. BUF_MEM_free(buf);
  444. if (cb != NULL) {
  445. cb(ssl, SSL_CB_CONNECT_EXIT, ret);
  446. }
  447. return ret;
  448. }
  449. static int dtls1_get_hello_verify(SSL *ssl) {
  450. long n;
  451. int al, ok = 0;
  452. CBS hello_verify_request, cookie;
  453. uint16_t server_version;
  454. n = ssl->method->ssl_get_message(
  455. ssl, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A,
  456. DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B, -1,
  457. /* Use the same maximum size as ssl3_get_server_hello. */
  458. 20000, ssl_hash_message, &ok);
  459. if (!ok) {
  460. return n;
  461. }
  462. if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
  463. ssl->d1->send_cookie = 0;
  464. ssl->s3->tmp.reuse_message = 1;
  465. return 1;
  466. }
  467. CBS_init(&hello_verify_request, ssl->init_msg, n);
  468. if (!CBS_get_u16(&hello_verify_request, &server_version) ||
  469. !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
  470. CBS_len(&hello_verify_request) != 0) {
  471. al = SSL_AD_DECODE_ERROR;
  472. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  473. goto f_err;
  474. }
  475. if (CBS_len(&cookie) > sizeof(ssl->d1->cookie)) {
  476. al = SSL_AD_ILLEGAL_PARAMETER;
  477. goto f_err;
  478. }
  479. memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
  480. ssl->d1->cookie_len = CBS_len(&cookie);
  481. ssl->d1->send_cookie = 1;
  482. return 1;
  483. f_err:
  484. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  485. return -1;
  486. }