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

666 рядки
18 KiB

  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 <stdio.h>
  115. #include <openssl/bn.h>
  116. #include <openssl/buf.h>
  117. #include <openssl/dh.h>
  118. #include <openssl/evp.h>
  119. #include <openssl/md5.h>
  120. #include <openssl/obj.h>
  121. #include <openssl/rand.h>
  122. #include <openssl/x509.h>
  123. #include "ssl_locl.h"
  124. static const SSL_METHOD *dtls1_get_server_method(int ver);
  125. static int dtls1_send_hello_verify_request(SSL *s);
  126. static const SSL_METHOD *dtls1_get_server_method(int ver)
  127. {
  128. if (ver == DTLS1_VERSION)
  129. return(DTLSv1_server_method());
  130. else if (ver == DTLS1_2_VERSION)
  131. return(DTLSv1_2_server_method());
  132. else
  133. return(NULL);
  134. }
  135. IMPLEMENT_dtls1_meth_func(DTLS1_VERSION,
  136. DTLSv1_server_method,
  137. dtls1_accept,
  138. ssl_undefined_function,
  139. dtls1_get_server_method,
  140. DTLSv1_enc_data)
  141. IMPLEMENT_dtls1_meth_func(DTLS1_2_VERSION,
  142. DTLSv1_2_server_method,
  143. dtls1_accept,
  144. ssl_undefined_function,
  145. dtls1_get_server_method,
  146. DTLSv1_2_enc_data)
  147. IMPLEMENT_dtls1_meth_func(DTLS_ANY_VERSION,
  148. DTLS_server_method,
  149. dtls1_accept,
  150. ssl_undefined_function,
  151. dtls1_get_server_method,
  152. DTLSv1_2_enc_data)
  153. int dtls1_accept(SSL *s)
  154. {
  155. BUF_MEM *buf;
  156. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  157. unsigned long alg_a;
  158. int ret= -1;
  159. int new_state,state,skip=0;
  160. ERR_clear_error();
  161. ERR_clear_system_error();
  162. if (s->info_callback != NULL)
  163. cb=s->info_callback;
  164. else if (s->ctx->info_callback != NULL)
  165. cb=s->ctx->info_callback;
  166. /* init things to blank */
  167. s->in_handshake++;
  168. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  169. if (s->cert == NULL)
  170. {
  171. OPENSSL_PUT_ERROR(SSL, dtls1_accept, SSL_R_NO_CERTIFICATE_SET);
  172. return(-1);
  173. }
  174. for (;;)
  175. {
  176. state=s->state;
  177. switch (s->state)
  178. {
  179. case SSL_ST_RENEGOTIATE:
  180. s->renegotiate=1;
  181. /* s->state=SSL_ST_ACCEPT; */
  182. case SSL_ST_BEFORE:
  183. case SSL_ST_ACCEPT:
  184. case SSL_ST_BEFORE|SSL_ST_ACCEPT:
  185. case SSL_ST_OK|SSL_ST_ACCEPT:
  186. s->server=1;
  187. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  188. if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00))
  189. {
  190. OPENSSL_PUT_ERROR(SSL, dtls1_accept, ERR_R_INTERNAL_ERROR);
  191. return -1;
  192. }
  193. s->type=SSL_ST_ACCEPT;
  194. if (s->init_buf == NULL)
  195. {
  196. if ((buf=BUF_MEM_new()) == NULL)
  197. {
  198. ret= -1;
  199. goto end;
  200. }
  201. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  202. {
  203. ret= -1;
  204. goto end;
  205. }
  206. s->init_buf=buf;
  207. }
  208. if (!ssl3_setup_buffers(s))
  209. {
  210. ret= -1;
  211. goto end;
  212. }
  213. s->init_num=0;
  214. if (s->state != SSL_ST_RENEGOTIATE)
  215. {
  216. /* Ok, we now need to push on a buffering BIO so that
  217. * the output is sent in a way that TCP likes :-)
  218. * ...but not with SCTP :-)
  219. */
  220. if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
  221. ssl3_init_finished_mac(s);
  222. s->state=SSL3_ST_SR_CLNT_HELLO_A;
  223. s->ctx->stats.sess_accept++;
  224. }
  225. else
  226. {
  227. /* s->state == SSL_ST_RENEGOTIATE,
  228. * we will just send a HelloRequest */
  229. s->ctx->stats.sess_accept_renegotiate++;
  230. s->state=SSL3_ST_SW_HELLO_REQ_A;
  231. }
  232. break;
  233. case SSL3_ST_SW_HELLO_REQ_A:
  234. case SSL3_ST_SW_HELLO_REQ_B:
  235. s->shutdown=0;
  236. dtls1_clear_record_buffer(s);
  237. dtls1_start_timer(s);
  238. ret=ssl3_send_hello_request(s);
  239. if (ret <= 0) goto end;
  240. s->s3->tmp.next_state=SSL3_ST_SR_CLNT_HELLO_A;
  241. s->state=SSL3_ST_SW_FLUSH;
  242. s->init_num=0;
  243. ssl3_init_finished_mac(s);
  244. break;
  245. case SSL3_ST_SW_HELLO_REQ_C:
  246. s->state=SSL_ST_OK;
  247. break;
  248. case SSL3_ST_SR_CLNT_HELLO_A:
  249. case SSL3_ST_SR_CLNT_HELLO_B:
  250. case SSL3_ST_SR_CLNT_HELLO_C:
  251. case SSL3_ST_SR_CLNT_HELLO_D:
  252. s->shutdown=0;
  253. ret=ssl3_get_client_hello(s);
  254. if (ret <= 0) goto end;
  255. dtls1_stop_timer(s);
  256. if (ret == 1 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
  257. s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A;
  258. else
  259. s->state = SSL3_ST_SW_SRVR_HELLO_A;
  260. s->init_num=0;
  261. break;
  262. case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A:
  263. case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B:
  264. ret = dtls1_send_hello_verify_request(s);
  265. if ( ret <= 0) goto end;
  266. s->state=SSL3_ST_SW_FLUSH;
  267. s->s3->tmp.next_state=SSL3_ST_SR_CLNT_HELLO_A;
  268. /* HelloVerifyRequest resets Finished MAC */
  269. ssl3_init_finished_mac(s);
  270. break;
  271. case SSL3_ST_SW_SRVR_HELLO_A:
  272. case SSL3_ST_SW_SRVR_HELLO_B:
  273. s->renegotiate = 2;
  274. dtls1_start_timer(s);
  275. ret=ssl3_send_server_hello(s);
  276. if (ret <= 0) goto end;
  277. if (s->hit)
  278. {
  279. if (s->tlsext_ticket_expected)
  280. s->state=SSL3_ST_SW_SESSION_TICKET_A;
  281. else
  282. s->state=SSL3_ST_SW_CHANGE_A;
  283. }
  284. else
  285. s->state=SSL3_ST_SW_CERT_A;
  286. s->init_num=0;
  287. break;
  288. case SSL3_ST_SW_CERT_A:
  289. case SSL3_ST_SW_CERT_B:
  290. /* Check if it is anon DH or normal PSK */
  291. if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
  292. && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
  293. {
  294. dtls1_start_timer(s);
  295. ret=ssl3_send_server_certificate(s);
  296. if (ret <= 0) goto end;
  297. if (s->s3->tmp.certificate_status_expected)
  298. s->state=SSL3_ST_SW_CERT_STATUS_A;
  299. else
  300. s->state=SSL3_ST_SW_KEY_EXCH_A;
  301. }
  302. else
  303. {
  304. skip = 1;
  305. s->state=SSL3_ST_SW_KEY_EXCH_A;
  306. }
  307. s->init_num=0;
  308. break;
  309. case SSL3_ST_SW_KEY_EXCH_A:
  310. case SSL3_ST_SW_KEY_EXCH_B:
  311. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  312. /* Send a ServerKeyExchange message if:
  313. * - The key exchange is ephemeral or anonymous
  314. * Diffie-Hellman.
  315. * - There is a PSK identity hint.
  316. *
  317. * TODO(davidben): This logic is currently duplicated
  318. * in s3_srvr.c. Fix this. In the meantime, keep them
  319. * in sync.
  320. */
  321. if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher) ||
  322. ((alg_a & SSL_aPSK) && s->psk_identity_hint))
  323. {
  324. dtls1_start_timer(s);
  325. ret=ssl3_send_server_key_exchange(s);
  326. if (ret <= 0) goto end;
  327. }
  328. else
  329. skip=1;
  330. s->state=SSL3_ST_SW_CERT_REQ_A;
  331. s->init_num=0;
  332. break;
  333. case SSL3_ST_SW_CERT_REQ_A:
  334. case SSL3_ST_SW_CERT_REQ_B:
  335. if (/* don't request cert unless asked for it: */
  336. !(s->verify_mode & SSL_VERIFY_PEER) ||
  337. /* if SSL_VERIFY_CLIENT_ONCE is set,
  338. * don't request cert during re-negotiation: */
  339. ((s->session->peer != NULL) &&
  340. (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
  341. /* never request cert in anonymous ciphersuites
  342. * (see section "Certificate request" in SSL 3 drafts
  343. * and in RFC 2246): */
  344. ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
  345. /* ... except when the application insists on verification
  346. * (against the specs, but s3_clnt.c accepts this for SSL 3) */
  347. !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
  348. /* With normal PSK Certificates and
  349. * Certificate Requests are omitted */
  350. (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
  351. {
  352. /* no cert request */
  353. skip=1;
  354. s->s3->tmp.cert_request=0;
  355. s->state=SSL3_ST_SW_SRVR_DONE_A;
  356. }
  357. else
  358. {
  359. s->s3->tmp.cert_request=1;
  360. dtls1_start_timer(s);
  361. ret=ssl3_send_certificate_request(s);
  362. if (ret <= 0) goto end;
  363. #ifndef NETSCAPE_HANG_BUG
  364. s->state=SSL3_ST_SW_SRVR_DONE_A;
  365. #else
  366. s->state=SSL3_ST_SW_FLUSH;
  367. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  368. #endif
  369. s->init_num=0;
  370. }
  371. break;
  372. case SSL3_ST_SW_SRVR_DONE_A:
  373. case SSL3_ST_SW_SRVR_DONE_B:
  374. dtls1_start_timer(s);
  375. ret=ssl3_send_server_done(s);
  376. if (ret <= 0) goto end;
  377. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  378. s->state=SSL3_ST_SW_FLUSH;
  379. s->init_num=0;
  380. break;
  381. case SSL3_ST_SW_FLUSH:
  382. s->rwstate=SSL_WRITING;
  383. if (BIO_flush(s->wbio) <= 0)
  384. {
  385. /* If the write error was fatal, stop trying */
  386. if (!BIO_should_retry(s->wbio))
  387. {
  388. s->rwstate=SSL_NOTHING;
  389. s->state=s->s3->tmp.next_state;
  390. }
  391. ret= -1;
  392. goto end;
  393. }
  394. s->rwstate=SSL_NOTHING;
  395. s->state=s->s3->tmp.next_state;
  396. break;
  397. case SSL3_ST_SR_CERT_A:
  398. case SSL3_ST_SR_CERT_B:
  399. if (s->s3->tmp.cert_request)
  400. {
  401. ret=ssl3_get_client_certificate(s);
  402. if (ret <= 0) goto end;
  403. }
  404. s->init_num=0;
  405. s->state=SSL3_ST_SR_KEY_EXCH_A;
  406. break;
  407. case SSL3_ST_SR_KEY_EXCH_A:
  408. case SSL3_ST_SR_KEY_EXCH_B:
  409. ret=ssl3_get_client_key_exchange(s);
  410. if (ret <= 0)
  411. goto end;
  412. s->state=SSL3_ST_SR_CERT_VRFY_A;
  413. s->init_num=0;
  414. break;
  415. case SSL3_ST_SR_CERT_VRFY_A:
  416. case SSL3_ST_SR_CERT_VRFY_B:
  417. s->d1->change_cipher_spec_ok = 1;
  418. /* we should decide if we expected this one */
  419. ret=ssl3_get_cert_verify(s);
  420. if (ret <= 0) goto end;
  421. s->state=SSL3_ST_SR_FINISHED_A;
  422. s->init_num=0;
  423. break;
  424. case SSL3_ST_SR_FINISHED_A:
  425. case SSL3_ST_SR_FINISHED_B:
  426. s->d1->change_cipher_spec_ok = 1;
  427. ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
  428. SSL3_ST_SR_FINISHED_B);
  429. if (ret <= 0) goto end;
  430. dtls1_stop_timer(s);
  431. if (s->hit)
  432. s->state=SSL_ST_OK;
  433. else if (s->tlsext_ticket_expected)
  434. s->state=SSL3_ST_SW_SESSION_TICKET_A;
  435. else
  436. s->state=SSL3_ST_SW_CHANGE_A;
  437. s->init_num=0;
  438. break;
  439. case SSL3_ST_SW_SESSION_TICKET_A:
  440. case SSL3_ST_SW_SESSION_TICKET_B:
  441. ret=ssl3_send_new_session_ticket(s);
  442. if (ret <= 0) goto end;
  443. s->state=SSL3_ST_SW_CHANGE_A;
  444. s->init_num=0;
  445. break;
  446. #if 0
  447. // TODO(davidben): Implement OCSP stapling on the server.
  448. case SSL3_ST_SW_CERT_STATUS_A:
  449. case SSL3_ST_SW_CERT_STATUS_B:
  450. ret=ssl3_send_cert_status(s);
  451. if (ret <= 0) goto end;
  452. s->state=SSL3_ST_SW_KEY_EXCH_A;
  453. s->init_num=0;
  454. break;
  455. #endif
  456. case SSL3_ST_SW_CHANGE_A:
  457. case SSL3_ST_SW_CHANGE_B:
  458. s->session->cipher=s->s3->tmp.new_cipher;
  459. if (!s->method->ssl3_enc->setup_key_block(s))
  460. { ret= -1; goto end; }
  461. ret=dtls1_send_change_cipher_spec(s,
  462. SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
  463. if (ret <= 0) goto end;
  464. s->state=SSL3_ST_SW_FINISHED_A;
  465. s->init_num=0;
  466. if (!s->method->ssl3_enc->change_cipher_state(s,
  467. SSL3_CHANGE_CIPHER_SERVER_WRITE))
  468. {
  469. ret= -1;
  470. goto end;
  471. }
  472. dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
  473. break;
  474. case SSL3_ST_SW_FINISHED_A:
  475. case SSL3_ST_SW_FINISHED_B:
  476. ret=ssl3_send_finished(s,
  477. SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
  478. s->method->ssl3_enc->server_finished_label,
  479. s->method->ssl3_enc->server_finished_label_len);
  480. if (ret <= 0) goto end;
  481. s->state=SSL3_ST_SW_FLUSH;
  482. if (s->hit)
  483. {
  484. s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
  485. }
  486. else
  487. {
  488. s->s3->tmp.next_state=SSL_ST_OK;
  489. }
  490. s->init_num=0;
  491. break;
  492. case SSL_ST_OK:
  493. /* clean a few things up */
  494. ssl3_cleanup_key_block(s);
  495. #if 0
  496. BUF_MEM_free(s->init_buf);
  497. s->init_buf=NULL;
  498. #endif
  499. /* remove buffering on output */
  500. ssl_free_wbio_buffer(s);
  501. s->init_num=0;
  502. if (s->renegotiate == 2) /* skipped if we just sent a HelloRequest */
  503. {
  504. s->renegotiate=0;
  505. s->new_session=0;
  506. ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
  507. s->ctx->stats.sess_accept_good++;
  508. /* s->server=1; */
  509. s->handshake_func=dtls1_accept;
  510. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  511. }
  512. ret = 1;
  513. /* done handshaking, next message is client hello */
  514. s->d1->handshake_read_seq = 0;
  515. /* next message is server hello */
  516. s->d1->handshake_write_seq = 0;
  517. s->d1->next_handshake_write_seq = 0;
  518. goto end;
  519. /* break; */
  520. default:
  521. OPENSSL_PUT_ERROR(SSL, dtls1_accept, SSL_R_UNKNOWN_STATE);
  522. ret= -1;
  523. goto end;
  524. /* break; */
  525. }
  526. if (!s->s3->tmp.reuse_message && !skip)
  527. {
  528. if (s->debug)
  529. {
  530. if ((ret=BIO_flush(s->wbio)) <= 0)
  531. goto end;
  532. }
  533. if ((cb != NULL) && (s->state != state))
  534. {
  535. new_state=s->state;
  536. s->state=state;
  537. cb(s,SSL_CB_ACCEPT_LOOP,1);
  538. s->state=new_state;
  539. }
  540. }
  541. skip=0;
  542. }
  543. end:
  544. /* BIO_flush(s->wbio); */
  545. s->in_handshake--;
  546. if (cb != NULL)
  547. cb(s,SSL_CB_ACCEPT_EXIT,ret);
  548. return(ret);
  549. }
  550. int dtls1_send_hello_verify_request(SSL *s)
  551. {
  552. unsigned int msg_len;
  553. unsigned char *msg, *buf, *p;
  554. if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A)
  555. {
  556. buf = (unsigned char *)s->init_buf->data;
  557. msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
  558. /* Always use DTLS 1.0 version: see RFC 6347 */
  559. *(p++) = DTLS1_VERSION >> 8;
  560. *(p++) = DTLS1_VERSION & 0xFF;
  561. if (s->ctx->app_gen_cookie_cb == NULL ||
  562. s->ctx->app_gen_cookie_cb(s, s->d1->cookie, &(s->d1->cookie_len)) == 0)
  563. {
  564. OPENSSL_PUT_ERROR(SSL, dtls1_send_hello_verify_request, ERR_R_INTERNAL_ERROR);
  565. return 0;
  566. }
  567. *(p++) = (unsigned char) s->d1->cookie_len;
  568. memcpy(p, s->d1->cookie, s->d1->cookie_len);
  569. p += s->d1->cookie_len;
  570. msg_len = p - msg;
  571. dtls1_set_message_header(s, buf,
  572. DTLS1_MT_HELLO_VERIFY_REQUEST, msg_len, 0, msg_len);
  573. s->state=DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B;
  574. /* number of bytes to write */
  575. s->init_num=p-buf;
  576. s->init_off=0;
  577. }
  578. /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */
  579. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE, add_to_finished_hash));
  580. }