Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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