Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

578 Zeilen
17 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 <assert.h>
  115. #include <stdio.h>
  116. #include <openssl/bn.h>
  117. #include <openssl/buf.h>
  118. #include <openssl/dh.h>
  119. #include <openssl/evp.h>
  120. #include <openssl/md5.h>
  121. #include <openssl/mem.h>
  122. #include <openssl/obj.h>
  123. #include <openssl/rand.h>
  124. #include "ssl_locl.h"
  125. static int dtls1_get_hello_verify(SSL *s);
  126. int dtls1_connect(SSL *s) {
  127. BUF_MEM *buf = NULL;
  128. void (*cb)(const SSL *ssl, int type, int val) = NULL;
  129. int ret = -1;
  130. int new_state, state, skip = 0;
  131. assert(s->handshake_func == dtls1_connect);
  132. assert(!s->server);
  133. assert(SSL_IS_DTLS(s));
  134. ERR_clear_error();
  135. ERR_clear_system_error();
  136. if (s->info_callback != NULL) {
  137. cb = s->info_callback;
  138. } else if (s->ctx->info_callback != NULL) {
  139. cb = s->ctx->info_callback;
  140. }
  141. s->in_handshake++;
  142. for (;;) {
  143. state = s->state;
  144. switch (s->state) {
  145. case SSL_ST_RENEGOTIATE:
  146. s->renegotiate = 1;
  147. s->state = SSL_ST_CONNECT;
  148. s->ctx->stats.sess_connect_renegotiate++;
  149. /* break */
  150. case SSL_ST_CONNECT:
  151. case SSL_ST_BEFORE | SSL_ST_CONNECT:
  152. if (cb != NULL) {
  153. cb(s, SSL_CB_HANDSHAKE_START, 1);
  154. }
  155. if (s->init_buf == NULL) {
  156. buf = BUF_MEM_new();
  157. if (buf == NULL ||
  158. !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
  159. ret = -1;
  160. goto end;
  161. }
  162. s->init_buf = buf;
  163. buf = NULL;
  164. }
  165. if (!ssl3_setup_buffers(s) ||
  166. !ssl_init_wbio_buffer(s, 0)) {
  167. ret = -1;
  168. goto end;
  169. }
  170. /* don't push the buffering BIO quite yet */
  171. s->state = SSL3_ST_CW_CLNT_HELLO_A;
  172. s->ctx->stats.sess_connect++;
  173. s->init_num = 0;
  174. s->d1->send_cookie = 0;
  175. s->hit = 0;
  176. break;
  177. case SSL3_ST_CW_CLNT_HELLO_A:
  178. case SSL3_ST_CW_CLNT_HELLO_B:
  179. s->shutdown = 0;
  180. /* every DTLS ClientHello resets Finished MAC */
  181. if (!ssl3_init_finished_mac(s)) {
  182. OPENSSL_PUT_ERROR(SSL, dtls1_connect, ERR_R_INTERNAL_ERROR);
  183. ret = -1;
  184. goto end;
  185. }
  186. dtls1_start_timer(s);
  187. ret = ssl3_send_client_hello(s);
  188. if (ret <= 0) {
  189. goto end;
  190. }
  191. if (s->d1->send_cookie) {
  192. s->state = SSL3_ST_CW_FLUSH;
  193. s->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A;
  194. } else {
  195. s->state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
  196. }
  197. s->init_num = 0;
  198. /* turn on buffering for the next lot of output */
  199. if (s->bbio != s->wbio) {
  200. s->wbio = BIO_push(s->bbio, s->wbio);
  201. }
  202. break;
  203. case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
  204. case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
  205. ret = dtls1_get_hello_verify(s);
  206. if (ret <= 0) {
  207. goto end;
  208. }
  209. if (s->d1->send_cookie) {
  210. /* start again, with a cookie */
  211. dtls1_stop_timer(s);
  212. s->state = SSL3_ST_CW_CLNT_HELLO_A;
  213. } else {
  214. s->state = SSL3_ST_CR_SRVR_HELLO_A;
  215. }
  216. s->init_num = 0;
  217. break;
  218. case SSL3_ST_CR_SRVR_HELLO_A:
  219. case SSL3_ST_CR_SRVR_HELLO_B:
  220. ret = ssl3_get_server_hello(s);
  221. if (ret <= 0) {
  222. goto end;
  223. }
  224. if (s->hit) {
  225. s->state = SSL3_ST_CR_FINISHED_A;
  226. if (s->tlsext_ticket_expected) {
  227. /* receive renewed session ticket */
  228. s->state = SSL3_ST_CR_SESSION_TICKET_A;
  229. }
  230. } else {
  231. s->state = SSL3_ST_CR_CERT_A;
  232. }
  233. s->init_num = 0;
  234. break;
  235. case SSL3_ST_CR_CERT_A:
  236. case SSL3_ST_CR_CERT_B:
  237. if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
  238. ret = ssl3_get_server_certificate(s);
  239. if (ret <= 0) {
  240. goto end;
  241. }
  242. if (s->s3->tmp.certificate_status_expected) {
  243. s->state = SSL3_ST_CR_CERT_STATUS_A;
  244. } else {
  245. s->state = SSL3_ST_CR_KEY_EXCH_A;
  246. }
  247. } else {
  248. skip = 1;
  249. s->state = SSL3_ST_CR_KEY_EXCH_A;
  250. }
  251. s->init_num = 0;
  252. break;
  253. case SSL3_ST_CR_KEY_EXCH_A:
  254. case SSL3_ST_CR_KEY_EXCH_B:
  255. ret = ssl3_get_server_key_exchange(s);
  256. if (ret <= 0) {
  257. goto end;
  258. }
  259. s->state = SSL3_ST_CR_CERT_REQ_A;
  260. s->init_num = 0;
  261. /* at this point we check that we have the
  262. * required stuff from the server */
  263. if (!ssl3_check_cert_and_algorithm(s)) {
  264. ret = -1;
  265. goto end;
  266. }
  267. break;
  268. case SSL3_ST_CR_CERT_REQ_A:
  269. case SSL3_ST_CR_CERT_REQ_B:
  270. ret = ssl3_get_certificate_request(s);
  271. if (ret <= 0) {
  272. goto end;
  273. }
  274. s->state = SSL3_ST_CR_SRVR_DONE_A;
  275. s->init_num = 0;
  276. break;
  277. case SSL3_ST_CR_SRVR_DONE_A:
  278. case SSL3_ST_CR_SRVR_DONE_B:
  279. ret = ssl3_get_server_done(s);
  280. if (ret <= 0) {
  281. goto end;
  282. }
  283. dtls1_stop_timer(s);
  284. if (s->s3->tmp.cert_req) {
  285. s->s3->tmp.next_state = SSL3_ST_CW_CERT_A;
  286. } else {
  287. s->s3->tmp.next_state = SSL3_ST_CW_KEY_EXCH_A;
  288. }
  289. s->init_num = 0;
  290. s->state = s->s3->tmp.next_state;
  291. break;
  292. case SSL3_ST_CW_CERT_A:
  293. case SSL3_ST_CW_CERT_B:
  294. case SSL3_ST_CW_CERT_C:
  295. case SSL3_ST_CW_CERT_D:
  296. dtls1_start_timer(s);
  297. ret = ssl3_send_client_certificate(s);
  298. if (ret <= 0) {
  299. goto end;
  300. }
  301. s->state = SSL3_ST_CW_KEY_EXCH_A;
  302. s->init_num = 0;
  303. break;
  304. case SSL3_ST_CW_KEY_EXCH_A:
  305. case SSL3_ST_CW_KEY_EXCH_B:
  306. dtls1_start_timer(s);
  307. ret = ssl3_send_client_key_exchange(s);
  308. if (ret <= 0) {
  309. goto end;
  310. }
  311. /* For TLS, cert_req is set to 2, so a cert chain
  312. * of nothing is sent, but no verify packet is sent */
  313. if (s->s3->tmp.cert_req == 1) {
  314. s->state = SSL3_ST_CW_CERT_VRFY_A;
  315. } else {
  316. s->state = SSL3_ST_CW_CHANGE_A;
  317. s->s3->change_cipher_spec = 0;
  318. }
  319. s->init_num = 0;
  320. break;
  321. case SSL3_ST_CW_CERT_VRFY_A:
  322. case SSL3_ST_CW_CERT_VRFY_B:
  323. dtls1_start_timer(s);
  324. ret = ssl3_send_cert_verify(s);
  325. if (ret <= 0) {
  326. goto end;
  327. }
  328. s->state = SSL3_ST_CW_CHANGE_A;
  329. s->init_num = 0;
  330. s->s3->change_cipher_spec = 0;
  331. break;
  332. case SSL3_ST_CW_CHANGE_A:
  333. case SSL3_ST_CW_CHANGE_B:
  334. if (!s->hit) {
  335. dtls1_start_timer(s);
  336. }
  337. ret = dtls1_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A,
  338. SSL3_ST_CW_CHANGE_B);
  339. if (ret <= 0) {
  340. goto end;
  341. }
  342. s->state = SSL3_ST_CW_FINISHED_A;
  343. s->init_num = 0;
  344. s->session->cipher = s->s3->tmp.new_cipher;
  345. if (!s->enc_method->setup_key_block(s) ||
  346. !s->enc_method->change_cipher_state(
  347. s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
  348. ret = -1;
  349. goto end;
  350. }
  351. dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
  352. break;
  353. case SSL3_ST_CW_FINISHED_A:
  354. case SSL3_ST_CW_FINISHED_B:
  355. if (!s->hit) {
  356. dtls1_start_timer(s);
  357. }
  358. ret =
  359. ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
  360. s->enc_method->client_finished_label,
  361. s->enc_method->client_finished_label_len);
  362. if (ret <= 0) {
  363. goto end;
  364. }
  365. s->state = SSL3_ST_CW_FLUSH;
  366. if (s->hit) {
  367. s->s3->tmp.next_state = SSL_ST_OK;
  368. } else {
  369. /* Allow NewSessionTicket if ticket expected */
  370. if (s->tlsext_ticket_expected) {
  371. s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
  372. } else {
  373. s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
  374. }
  375. }
  376. s->init_num = 0;
  377. break;
  378. case SSL3_ST_CR_SESSION_TICKET_A:
  379. case SSL3_ST_CR_SESSION_TICKET_B:
  380. ret = ssl3_get_new_session_ticket(s);
  381. if (ret <= 0) {
  382. goto end;
  383. }
  384. s->state = SSL3_ST_CR_FINISHED_A;
  385. s->init_num = 0;
  386. break;
  387. case SSL3_ST_CR_CERT_STATUS_A:
  388. case SSL3_ST_CR_CERT_STATUS_B:
  389. ret = ssl3_get_cert_status(s);
  390. if (ret <= 0) {
  391. goto end;
  392. }
  393. s->state = SSL3_ST_CR_KEY_EXCH_A;
  394. s->init_num = 0;
  395. break;
  396. case SSL3_ST_CR_FINISHED_A:
  397. case SSL3_ST_CR_FINISHED_B:
  398. s->d1->change_cipher_spec_ok = 1;
  399. ret =
  400. ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
  401. if (ret <= 0) {
  402. goto end;
  403. }
  404. dtls1_stop_timer(s);
  405. if (s->hit) {
  406. s->state = SSL3_ST_CW_CHANGE_A;
  407. } else {
  408. s->state = SSL_ST_OK;
  409. }
  410. s->init_num = 0;
  411. break;
  412. case SSL3_ST_CW_FLUSH:
  413. s->rwstate = SSL_WRITING;
  414. if (BIO_flush(s->wbio) <= 0) {
  415. /* If the write error was fatal, stop trying */
  416. if (!BIO_should_retry(s->wbio)) {
  417. s->rwstate = SSL_NOTHING;
  418. s->state = s->s3->tmp.next_state;
  419. }
  420. ret = -1;
  421. goto end;
  422. }
  423. s->rwstate = SSL_NOTHING;
  424. s->state = s->s3->tmp.next_state;
  425. break;
  426. case SSL_ST_OK:
  427. /* clean a few things up */
  428. ssl3_cleanup_key_block(s);
  429. /* Remove write buffering now. */
  430. ssl_free_wbio_buffer(s);
  431. s->init_num = 0;
  432. s->renegotiate = 0;
  433. s->new_session = 0;
  434. ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
  435. if (s->hit) {
  436. s->ctx->stats.sess_hit++;
  437. }
  438. ret = 1;
  439. s->ctx->stats.sess_connect_good++;
  440. if (cb != NULL)
  441. cb(s, SSL_CB_HANDSHAKE_DONE, 1);
  442. /* done with handshaking */
  443. s->d1->handshake_read_seq = 0;
  444. s->d1->next_handshake_write_seq = 0;
  445. goto end;
  446. default:
  447. OPENSSL_PUT_ERROR(SSL, dtls1_connect, SSL_R_UNKNOWN_STATE);
  448. ret = -1;
  449. goto end;
  450. }
  451. /* did we do anything? */
  452. if (!s->s3->tmp.reuse_message && !skip) {
  453. if ((cb != NULL) && (s->state != state)) {
  454. new_state = s->state;
  455. s->state = state;
  456. cb(s, SSL_CB_CONNECT_LOOP, 1);
  457. s->state = new_state;
  458. }
  459. }
  460. skip = 0;
  461. }
  462. end:
  463. s->in_handshake--;
  464. if (buf != NULL) {
  465. BUF_MEM_free(buf);
  466. }
  467. if (cb != NULL) {
  468. cb(s, SSL_CB_CONNECT_EXIT, ret);
  469. }
  470. return ret;
  471. }
  472. static int dtls1_get_hello_verify(SSL *s) {
  473. long n;
  474. int al, ok = 0;
  475. CBS hello_verify_request, cookie;
  476. uint16_t server_version;
  477. n = s->method->ssl_get_message(
  478. s, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
  479. -1,
  480. /* Use the same maximum size as ssl3_get_server_hello. */
  481. 20000, SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
  482. if (!ok) {
  483. return n;
  484. }
  485. if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
  486. s->d1->send_cookie = 0;
  487. s->s3->tmp.reuse_message = 1;
  488. return 1;
  489. }
  490. CBS_init(&hello_verify_request, s->init_msg, n);
  491. if (!CBS_get_u16(&hello_verify_request, &server_version) ||
  492. !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
  493. CBS_len(&hello_verify_request) != 0) {
  494. al = SSL_AD_DECODE_ERROR;
  495. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
  496. goto f_err;
  497. }
  498. if (CBS_len(&cookie) > sizeof(s->d1->cookie)) {
  499. al = SSL_AD_ILLEGAL_PARAMETER;
  500. goto f_err;
  501. }
  502. memcpy(s->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
  503. s->d1->cookie_len = CBS_len(&cookie);
  504. s->d1->send_cookie = 1;
  505. return 1;
  506. f_err:
  507. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  508. return -1;
  509. }