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

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