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.
 
 
 
 
 
 

746 regels
20 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. if (s->version != DTLS1_BAD_VER)
  291. ssl3_init_finished_mac(s);
  292. break;
  293. case SSL3_ST_SW_SRVR_HELLO_A:
  294. case SSL3_ST_SW_SRVR_HELLO_B:
  295. s->renegotiate = 2;
  296. dtls1_start_timer(s);
  297. ret=ssl3_send_server_hello(s);
  298. if (ret <= 0) goto end;
  299. if (s->hit)
  300. {
  301. if (s->tlsext_ticket_expected)
  302. s->state=SSL3_ST_SW_SESSION_TICKET_A;
  303. else
  304. s->state=SSL3_ST_SW_CHANGE_A;
  305. }
  306. else
  307. s->state=SSL3_ST_SW_CERT_A;
  308. s->init_num=0;
  309. break;
  310. case SSL3_ST_SW_CERT_A:
  311. case SSL3_ST_SW_CERT_B:
  312. /* Check if it is anon DH or normal PSK */
  313. if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
  314. && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
  315. {
  316. dtls1_start_timer(s);
  317. ret=ssl3_send_server_certificate(s);
  318. if (ret <= 0) goto end;
  319. if (s->tlsext_status_expected)
  320. s->state=SSL3_ST_SW_CERT_STATUS_A;
  321. else
  322. s->state=SSL3_ST_SW_KEY_EXCH_A;
  323. }
  324. else
  325. {
  326. skip = 1;
  327. s->state=SSL3_ST_SW_KEY_EXCH_A;
  328. }
  329. s->init_num=0;
  330. break;
  331. case SSL3_ST_SW_KEY_EXCH_A:
  332. case SSL3_ST_SW_KEY_EXCH_B:
  333. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  334. /* Send a ServerKeyExchange message if:
  335. * - The key exchange is ephemeral or anonymous
  336. * Diffie-Hellman.
  337. * - There is a PSK identity hint.
  338. *
  339. * TODO(davidben): This logic is currently duplicated
  340. * in s3_srvr.c. Fix this. In the meantime, keep them
  341. * in sync.
  342. */
  343. if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher) ||
  344. ((alg_a & SSL_aPSK) && s->session->psk_identity_hint))
  345. {
  346. dtls1_start_timer(s);
  347. ret=ssl3_send_server_key_exchange(s);
  348. if (ret <= 0) goto end;
  349. }
  350. else
  351. skip=1;
  352. s->state=SSL3_ST_SW_CERT_REQ_A;
  353. s->init_num=0;
  354. break;
  355. case SSL3_ST_SW_CERT_REQ_A:
  356. case SSL3_ST_SW_CERT_REQ_B:
  357. if (/* don't request cert unless asked for it: */
  358. !(s->verify_mode & SSL_VERIFY_PEER) ||
  359. /* if SSL_VERIFY_CLIENT_ONCE is set,
  360. * don't request cert during re-negotiation: */
  361. ((s->session->peer != NULL) &&
  362. (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
  363. /* never request cert in anonymous ciphersuites
  364. * (see section "Certificate request" in SSL 3 drafts
  365. * and in RFC 2246): */
  366. ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
  367. /* ... except when the application insists on verification
  368. * (against the specs, but s3_clnt.c accepts this for SSL 3) */
  369. !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
  370. /* With normal PSK Certificates and
  371. * Certificate Requests are omitted */
  372. (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
  373. {
  374. /* no cert request */
  375. skip=1;
  376. s->s3->tmp.cert_request=0;
  377. s->state=SSL3_ST_SW_SRVR_DONE_A;
  378. }
  379. else
  380. {
  381. s->s3->tmp.cert_request=1;
  382. dtls1_start_timer(s);
  383. ret=ssl3_send_certificate_request(s);
  384. if (ret <= 0) goto end;
  385. #ifndef NETSCAPE_HANG_BUG
  386. s->state=SSL3_ST_SW_SRVR_DONE_A;
  387. #else
  388. s->state=SSL3_ST_SW_FLUSH;
  389. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  390. #endif
  391. s->init_num=0;
  392. }
  393. break;
  394. case SSL3_ST_SW_SRVR_DONE_A:
  395. case SSL3_ST_SW_SRVR_DONE_B:
  396. dtls1_start_timer(s);
  397. ret=ssl3_send_server_done(s);
  398. if (ret <= 0) goto end;
  399. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  400. s->state=SSL3_ST_SW_FLUSH;
  401. s->init_num=0;
  402. break;
  403. case SSL3_ST_SW_FLUSH:
  404. s->rwstate=SSL_WRITING;
  405. if (BIO_flush(s->wbio) <= 0)
  406. {
  407. /* If the write error was fatal, stop trying */
  408. if (!BIO_should_retry(s->wbio))
  409. {
  410. s->rwstate=SSL_NOTHING;
  411. s->state=s->s3->tmp.next_state;
  412. }
  413. ret= -1;
  414. goto end;
  415. }
  416. s->rwstate=SSL_NOTHING;
  417. s->state=s->s3->tmp.next_state;
  418. break;
  419. case SSL3_ST_SR_CERT_A:
  420. case SSL3_ST_SR_CERT_B:
  421. /* Check for second client hello (MS SGC) */
  422. ret = ssl3_check_client_hello(s);
  423. if (ret <= 0)
  424. goto end;
  425. if (ret == 2)
  426. {
  427. dtls1_stop_timer(s);
  428. s->state = SSL3_ST_SR_CLNT_HELLO_C;
  429. }
  430. else {
  431. if (s->s3->tmp.cert_request)
  432. {
  433. ret=ssl3_get_client_certificate(s);
  434. if (ret <= 0) goto end;
  435. }
  436. s->init_num=0;
  437. s->state=SSL3_ST_SR_KEY_EXCH_A;
  438. }
  439. break;
  440. case SSL3_ST_SR_KEY_EXCH_A:
  441. case SSL3_ST_SR_KEY_EXCH_B:
  442. ret=ssl3_get_client_key_exchange(s);
  443. if (ret <= 0) goto end;
  444. s->state=SSL3_ST_SR_CERT_VRFY_A;
  445. s->init_num=0;
  446. if (ret == 2)
  447. {
  448. /* For the ECDH ciphersuites when
  449. * the client sends its ECDH pub key in
  450. * a certificate, the CertificateVerify
  451. * message is not sent.
  452. */
  453. s->state=SSL3_ST_SR_FINISHED_A;
  454. s->init_num = 0;
  455. }
  456. else if (SSL_USE_SIGALGS(s))
  457. {
  458. s->state=SSL3_ST_SR_CERT_VRFY_A;
  459. s->init_num=0;
  460. if (!s->session->peer)
  461. break;
  462. /* For sigalgs freeze the handshake buffer
  463. * at this point and digest cached records.
  464. */
  465. if (!s->s3->handshake_buffer)
  466. {
  467. OPENSSL_PUT_ERROR(SSL, dtls1_accept, ERR_R_INTERNAL_ERROR);
  468. return -1;
  469. }
  470. s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE;
  471. if (!ssl3_digest_cached_records(s))
  472. return -1;
  473. }
  474. else
  475. {
  476. s->state=SSL3_ST_SR_CERT_VRFY_A;
  477. s->init_num=0;
  478. /* We need to get hashes here so if there is
  479. * a client cert, it can be verified */
  480. s->method->ssl3_enc->cert_verify_mac(s,
  481. NID_md5,
  482. &(s->s3->tmp.cert_verify_md[0]));
  483. s->method->ssl3_enc->cert_verify_mac(s,
  484. NID_sha1,
  485. &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
  486. }
  487. break;
  488. case SSL3_ST_SR_CERT_VRFY_A:
  489. case SSL3_ST_SR_CERT_VRFY_B:
  490. s->d1->change_cipher_spec_ok = 1;
  491. /* we should decide if we expected this one */
  492. ret=ssl3_get_cert_verify(s);
  493. if (ret <= 0) goto end;
  494. s->state=SSL3_ST_SR_FINISHED_A;
  495. s->init_num=0;
  496. break;
  497. case SSL3_ST_SR_FINISHED_A:
  498. case SSL3_ST_SR_FINISHED_B:
  499. s->d1->change_cipher_spec_ok = 1;
  500. ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
  501. SSL3_ST_SR_FINISHED_B);
  502. if (ret <= 0) goto end;
  503. dtls1_stop_timer(s);
  504. if (s->hit)
  505. s->state=SSL_ST_OK;
  506. else if (s->tlsext_ticket_expected)
  507. s->state=SSL3_ST_SW_SESSION_TICKET_A;
  508. else
  509. s->state=SSL3_ST_SW_CHANGE_A;
  510. s->init_num=0;
  511. break;
  512. case SSL3_ST_SW_SESSION_TICKET_A:
  513. case SSL3_ST_SW_SESSION_TICKET_B:
  514. ret=ssl3_send_newsession_ticket(s);
  515. if (ret <= 0) goto end;
  516. s->state=SSL3_ST_SW_CHANGE_A;
  517. s->init_num=0;
  518. break;
  519. case SSL3_ST_SW_CERT_STATUS_A:
  520. case SSL3_ST_SW_CERT_STATUS_B:
  521. ret=ssl3_send_cert_status(s);
  522. if (ret <= 0) goto end;
  523. s->state=SSL3_ST_SW_KEY_EXCH_A;
  524. s->init_num=0;
  525. break;
  526. case SSL3_ST_SW_CHANGE_A:
  527. case SSL3_ST_SW_CHANGE_B:
  528. s->session->cipher=s->s3->tmp.new_cipher;
  529. if (!s->method->ssl3_enc->setup_key_block(s))
  530. { ret= -1; goto end; }
  531. ret=dtls1_send_change_cipher_spec(s,
  532. SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
  533. if (ret <= 0) goto end;
  534. s->state=SSL3_ST_SW_FINISHED_A;
  535. s->init_num=0;
  536. if (!s->method->ssl3_enc->change_cipher_state(s,
  537. SSL3_CHANGE_CIPHER_SERVER_WRITE))
  538. {
  539. ret= -1;
  540. goto end;
  541. }
  542. dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
  543. break;
  544. case SSL3_ST_SW_FINISHED_A:
  545. case SSL3_ST_SW_FINISHED_B:
  546. ret=ssl3_send_finished(s,
  547. SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
  548. s->method->ssl3_enc->server_finished_label,
  549. s->method->ssl3_enc->server_finished_label_len);
  550. if (ret <= 0) goto end;
  551. s->state=SSL3_ST_SW_FLUSH;
  552. if (s->hit)
  553. {
  554. s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
  555. }
  556. else
  557. {
  558. s->s3->tmp.next_state=SSL_ST_OK;
  559. }
  560. s->init_num=0;
  561. break;
  562. case SSL_ST_OK:
  563. /* clean a few things up */
  564. ssl3_cleanup_key_block(s);
  565. #if 0
  566. BUF_MEM_free(s->init_buf);
  567. s->init_buf=NULL;
  568. #endif
  569. /* remove buffering on output */
  570. ssl_free_wbio_buffer(s);
  571. s->init_num=0;
  572. if (s->renegotiate == 2) /* skipped if we just sent a HelloRequest */
  573. {
  574. s->renegotiate=0;
  575. s->new_session=0;
  576. ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
  577. s->ctx->stats.sess_accept_good++;
  578. /* s->server=1; */
  579. s->handshake_func=dtls1_accept;
  580. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  581. }
  582. ret = 1;
  583. /* done handshaking, next message is client hello */
  584. s->d1->handshake_read_seq = 0;
  585. /* next message is server hello */
  586. s->d1->handshake_write_seq = 0;
  587. s->d1->next_handshake_write_seq = 0;
  588. goto end;
  589. /* break; */
  590. default:
  591. OPENSSL_PUT_ERROR(SSL, dtls1_accept, SSL_R_UNKNOWN_STATE);
  592. ret= -1;
  593. goto end;
  594. /* break; */
  595. }
  596. if (!s->s3->tmp.reuse_message && !skip)
  597. {
  598. if (s->debug)
  599. {
  600. if ((ret=BIO_flush(s->wbio)) <= 0)
  601. goto end;
  602. }
  603. if ((cb != NULL) && (s->state != state))
  604. {
  605. new_state=s->state;
  606. s->state=state;
  607. cb(s,SSL_CB_ACCEPT_LOOP,1);
  608. s->state=new_state;
  609. }
  610. }
  611. skip=0;
  612. }
  613. end:
  614. /* BIO_flush(s->wbio); */
  615. s->in_handshake--;
  616. if (cb != NULL)
  617. cb(s,SSL_CB_ACCEPT_EXIT,ret);
  618. return(ret);
  619. }
  620. int dtls1_send_hello_verify_request(SSL *s)
  621. {
  622. unsigned int msg_len;
  623. unsigned char *msg, *buf, *p;
  624. if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A)
  625. {
  626. buf = (unsigned char *)s->init_buf->data;
  627. msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
  628. /* Always use DTLS 1.0 version: see RFC 6347 */
  629. *(p++) = DTLS1_VERSION >> 8;
  630. *(p++) = DTLS1_VERSION & 0xFF;
  631. if (s->ctx->app_gen_cookie_cb == NULL ||
  632. s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
  633. &(s->d1->cookie_len)) == 0)
  634. {
  635. OPENSSL_PUT_ERROR(SSL, dtls1_send_hello_verify_request, ERR_R_INTERNAL_ERROR);
  636. return 0;
  637. }
  638. *(p++) = (unsigned char) s->d1->cookie_len;
  639. memcpy(p, s->d1->cookie, s->d1->cookie_len);
  640. p += s->d1->cookie_len;
  641. msg_len = p - msg;
  642. dtls1_set_message_header(s, buf,
  643. DTLS1_MT_HELLO_VERIFY_REQUEST, msg_len, 0, msg_len);
  644. s->state=DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B;
  645. /* number of bytes to write */
  646. s->init_num=p-buf;
  647. s->init_off=0;
  648. }
  649. /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */
  650. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  651. }