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.
 
 
 
 
 
 

569 line
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_RENEGOTIATE:
  148. s->renegotiate = 1;
  149. s->state = SSL_ST_CONNECT;
  150. /* break */
  151. case SSL_ST_CONNECT:
  152. case SSL_ST_BEFORE | SSL_ST_CONNECT:
  153. if (cb != NULL) {
  154. cb(s, SSL_CB_HANDSHAKE_START, 1);
  155. }
  156. if (s->init_buf == NULL) {
  157. buf = BUF_MEM_new();
  158. if (buf == NULL ||
  159. !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
  160. ret = -1;
  161. goto end;
  162. }
  163. s->init_buf = buf;
  164. buf = NULL;
  165. }
  166. if (!ssl3_setup_buffers(s) ||
  167. !ssl_init_wbio_buffer(s, 0)) {
  168. ret = -1;
  169. goto end;
  170. }
  171. /* don't push the buffering BIO quite yet */
  172. s->state = SSL3_ST_CW_CLNT_HELLO_A;
  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. ret = -1;
  416. goto end;
  417. }
  418. s->rwstate = SSL_NOTHING;
  419. s->state = s->s3->tmp.next_state;
  420. break;
  421. case SSL_ST_OK:
  422. /* clean a few things up */
  423. ssl3_cleanup_key_block(s);
  424. /* Remove write buffering now. */
  425. ssl_free_wbio_buffer(s);
  426. s->init_num = 0;
  427. s->renegotiate = 0;
  428. s->new_session = 0;
  429. ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
  430. ret = 1;
  431. if (cb != NULL) {
  432. cb(s, SSL_CB_HANDSHAKE_DONE, 1);
  433. }
  434. /* done with handshaking */
  435. s->d1->handshake_read_seq = 0;
  436. s->d1->next_handshake_write_seq = 0;
  437. goto end;
  438. default:
  439. OPENSSL_PUT_ERROR(SSL, dtls1_connect, SSL_R_UNKNOWN_STATE);
  440. ret = -1;
  441. goto end;
  442. }
  443. /* did we do anything? */
  444. if (!s->s3->tmp.reuse_message && !skip) {
  445. if ((cb != NULL) && (s->state != state)) {
  446. new_state = s->state;
  447. s->state = state;
  448. cb(s, SSL_CB_CONNECT_LOOP, 1);
  449. s->state = new_state;
  450. }
  451. }
  452. skip = 0;
  453. }
  454. end:
  455. s->in_handshake--;
  456. if (buf != NULL) {
  457. BUF_MEM_free(buf);
  458. }
  459. if (cb != NULL) {
  460. cb(s, SSL_CB_CONNECT_EXIT, ret);
  461. }
  462. return ret;
  463. }
  464. static int dtls1_get_hello_verify(SSL *s) {
  465. long n;
  466. int al, ok = 0;
  467. CBS hello_verify_request, cookie;
  468. uint16_t server_version;
  469. n = s->method->ssl_get_message(
  470. s, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
  471. -1,
  472. /* Use the same maximum size as ssl3_get_server_hello. */
  473. 20000, ssl_hash_message, &ok);
  474. if (!ok) {
  475. return n;
  476. }
  477. if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
  478. s->d1->send_cookie = 0;
  479. s->s3->tmp.reuse_message = 1;
  480. return 1;
  481. }
  482. CBS_init(&hello_verify_request, s->init_msg, n);
  483. if (!CBS_get_u16(&hello_verify_request, &server_version) ||
  484. !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
  485. CBS_len(&hello_verify_request) != 0) {
  486. al = SSL_AD_DECODE_ERROR;
  487. OPENSSL_PUT_ERROR(SSL, dtls1_get_hello_verify, SSL_R_DECODE_ERROR);
  488. goto f_err;
  489. }
  490. if (CBS_len(&cookie) > sizeof(s->d1->cookie)) {
  491. al = SSL_AD_ILLEGAL_PARAMETER;
  492. goto f_err;
  493. }
  494. memcpy(s->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
  495. s->d1->cookie_len = CBS_len(&cookie);
  496. s->d1->send_cookie = 1;
  497. return 1;
  498. f_err:
  499. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  500. return -1;
  501. }