Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

567 lignes
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 (!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->init_num = 0;
  173. s->d1->send_cookie = 0;
  174. s->hit = 0;
  175. break;
  176. case SSL3_ST_CW_CLNT_HELLO_A:
  177. case SSL3_ST_CW_CLNT_HELLO_B:
  178. s->shutdown = 0;
  179. /* every DTLS ClientHello resets Finished MAC */
  180. if (!ssl3_init_finished_mac(s)) {
  181. OPENSSL_PUT_ERROR(SSL, dtls1_connect, ERR_R_INTERNAL_ERROR);
  182. ret = -1;
  183. goto end;
  184. }
  185. dtls1_start_timer(s);
  186. ret = ssl3_send_client_hello(s);
  187. if (ret <= 0) {
  188. goto end;
  189. }
  190. if (s->d1->send_cookie) {
  191. s->state = SSL3_ST_CW_FLUSH;
  192. s->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A;
  193. } else {
  194. s->state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
  195. }
  196. s->init_num = 0;
  197. /* turn on buffering for the next lot of output */
  198. if (s->bbio != s->wbio) {
  199. s->wbio = BIO_push(s->bbio, s->wbio);
  200. }
  201. break;
  202. case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
  203. case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
  204. ret = dtls1_get_hello_verify(s);
  205. if (ret <= 0) {
  206. goto end;
  207. }
  208. if (s->d1->send_cookie) {
  209. /* start again, with a cookie */
  210. dtls1_stop_timer(s);
  211. s->state = SSL3_ST_CW_CLNT_HELLO_A;
  212. } else {
  213. s->state = SSL3_ST_CR_SRVR_HELLO_A;
  214. }
  215. s->init_num = 0;
  216. break;
  217. case SSL3_ST_CR_SRVR_HELLO_A:
  218. case SSL3_ST_CR_SRVR_HELLO_B:
  219. ret = ssl3_get_server_hello(s);
  220. if (ret <= 0) {
  221. goto end;
  222. }
  223. if (s->hit) {
  224. s->state = SSL3_ST_CR_FINISHED_A;
  225. if (s->tlsext_ticket_expected) {
  226. /* receive renewed session ticket */
  227. s->state = SSL3_ST_CR_SESSION_TICKET_A;
  228. }
  229. } else {
  230. s->state = SSL3_ST_CR_CERT_A;
  231. }
  232. s->init_num = 0;
  233. break;
  234. case SSL3_ST_CR_CERT_A:
  235. case SSL3_ST_CR_CERT_B:
  236. if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
  237. ret = ssl3_get_server_certificate(s);
  238. if (ret <= 0) {
  239. goto end;
  240. }
  241. if (s->s3->tmp.certificate_status_expected) {
  242. s->state = SSL3_ST_CR_CERT_STATUS_A;
  243. } else {
  244. s->state = SSL3_ST_CR_KEY_EXCH_A;
  245. }
  246. } else {
  247. skip = 1;
  248. s->state = SSL3_ST_CR_KEY_EXCH_A;
  249. }
  250. s->init_num = 0;
  251. break;
  252. case SSL3_ST_CR_KEY_EXCH_A:
  253. case SSL3_ST_CR_KEY_EXCH_B:
  254. ret = ssl3_get_server_key_exchange(s);
  255. if (ret <= 0) {
  256. goto end;
  257. }
  258. s->state = SSL3_ST_CR_CERT_REQ_A;
  259. s->init_num = 0;
  260. /* at this point we check that we have the
  261. * required stuff from the server */
  262. if (!ssl3_check_cert_and_algorithm(s)) {
  263. ret = -1;
  264. goto end;
  265. }
  266. break;
  267. case SSL3_ST_CR_CERT_REQ_A:
  268. case SSL3_ST_CR_CERT_REQ_B:
  269. ret = ssl3_get_certificate_request(s);
  270. if (ret <= 0) {
  271. goto end;
  272. }
  273. s->state = SSL3_ST_CR_SRVR_DONE_A;
  274. s->init_num = 0;
  275. break;
  276. case SSL3_ST_CR_SRVR_DONE_A:
  277. case SSL3_ST_CR_SRVR_DONE_B:
  278. ret = ssl3_get_server_done(s);
  279. if (ret <= 0) {
  280. goto end;
  281. }
  282. dtls1_stop_timer(s);
  283. if (s->s3->tmp.cert_req) {
  284. s->s3->tmp.next_state = SSL3_ST_CW_CERT_A;
  285. } else {
  286. s->s3->tmp.next_state = SSL3_ST_CW_KEY_EXCH_A;
  287. }
  288. s->init_num = 0;
  289. s->state = s->s3->tmp.next_state;
  290. break;
  291. case SSL3_ST_CW_CERT_A:
  292. case SSL3_ST_CW_CERT_B:
  293. case SSL3_ST_CW_CERT_C:
  294. case SSL3_ST_CW_CERT_D:
  295. dtls1_start_timer(s);
  296. ret = ssl3_send_client_certificate(s);
  297. if (ret <= 0) {
  298. goto end;
  299. }
  300. s->state = SSL3_ST_CW_KEY_EXCH_A;
  301. s->init_num = 0;
  302. break;
  303. case SSL3_ST_CW_KEY_EXCH_A:
  304. case SSL3_ST_CW_KEY_EXCH_B:
  305. dtls1_start_timer(s);
  306. ret = ssl3_send_client_key_exchange(s);
  307. if (ret <= 0) {
  308. goto end;
  309. }
  310. /* For TLS, cert_req is set to 2, so a cert chain
  311. * of nothing is sent, but no verify packet is sent */
  312. if (s->s3->tmp.cert_req == 1) {
  313. s->state = SSL3_ST_CW_CERT_VRFY_A;
  314. } else {
  315. s->state = SSL3_ST_CW_CHANGE_A;
  316. s->s3->change_cipher_spec = 0;
  317. }
  318. s->init_num = 0;
  319. break;
  320. case SSL3_ST_CW_CERT_VRFY_A:
  321. case SSL3_ST_CW_CERT_VRFY_B:
  322. dtls1_start_timer(s);
  323. ret = ssl3_send_cert_verify(s);
  324. if (ret <= 0) {
  325. goto end;
  326. }
  327. s->state = SSL3_ST_CW_CHANGE_A;
  328. s->init_num = 0;
  329. s->s3->change_cipher_spec = 0;
  330. break;
  331. case SSL3_ST_CW_CHANGE_A:
  332. case SSL3_ST_CW_CHANGE_B:
  333. if (!s->hit) {
  334. dtls1_start_timer(s);
  335. }
  336. ret = dtls1_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A,
  337. SSL3_ST_CW_CHANGE_B);
  338. if (ret <= 0) {
  339. goto end;
  340. }
  341. s->state = SSL3_ST_CW_FINISHED_A;
  342. s->init_num = 0;
  343. s->session->cipher = s->s3->tmp.new_cipher;
  344. if (!s->enc_method->setup_key_block(s) ||
  345. !s->enc_method->change_cipher_state(
  346. s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
  347. ret = -1;
  348. goto end;
  349. }
  350. dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
  351. break;
  352. case SSL3_ST_CW_FINISHED_A:
  353. case SSL3_ST_CW_FINISHED_B:
  354. if (!s->hit) {
  355. dtls1_start_timer(s);
  356. }
  357. ret =
  358. ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
  359. s->enc_method->client_finished_label,
  360. s->enc_method->client_finished_label_len);
  361. if (ret <= 0) {
  362. goto end;
  363. }
  364. s->state = SSL3_ST_CW_FLUSH;
  365. if (s->hit) {
  366. s->s3->tmp.next_state = SSL_ST_OK;
  367. } else {
  368. /* Allow NewSessionTicket if ticket expected */
  369. if (s->tlsext_ticket_expected) {
  370. s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
  371. } else {
  372. s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
  373. }
  374. }
  375. s->init_num = 0;
  376. break;
  377. case SSL3_ST_CR_SESSION_TICKET_A:
  378. case SSL3_ST_CR_SESSION_TICKET_B:
  379. ret = ssl3_get_new_session_ticket(s);
  380. if (ret <= 0) {
  381. goto end;
  382. }
  383. s->state = SSL3_ST_CR_FINISHED_A;
  384. s->init_num = 0;
  385. break;
  386. case SSL3_ST_CR_CERT_STATUS_A:
  387. case SSL3_ST_CR_CERT_STATUS_B:
  388. ret = ssl3_get_cert_status(s);
  389. if (ret <= 0) {
  390. goto end;
  391. }
  392. s->state = SSL3_ST_CR_KEY_EXCH_A;
  393. s->init_num = 0;
  394. break;
  395. case SSL3_ST_CR_FINISHED_A:
  396. case SSL3_ST_CR_FINISHED_B:
  397. s->d1->change_cipher_spec_ok = 1;
  398. ret =
  399. ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
  400. if (ret <= 0) {
  401. goto end;
  402. }
  403. dtls1_stop_timer(s);
  404. if (s->hit) {
  405. s->state = SSL3_ST_CW_CHANGE_A;
  406. } else {
  407. s->state = SSL_ST_OK;
  408. }
  409. s->init_num = 0;
  410. break;
  411. case SSL3_ST_CW_FLUSH:
  412. s->rwstate = SSL_WRITING;
  413. if (BIO_flush(s->wbio) <= 0) {
  414. ret = -1;
  415. goto end;
  416. }
  417. s->rwstate = SSL_NOTHING;
  418. s->state = s->s3->tmp.next_state;
  419. break;
  420. case SSL_ST_OK:
  421. /* clean a few things up */
  422. ssl3_cleanup_key_block(s);
  423. /* Remove write buffering now. */
  424. ssl_free_wbio_buffer(s);
  425. s->init_num = 0;
  426. s->renegotiate = 0;
  427. s->new_session = 0;
  428. s->s3->initial_handshake_complete = 1;
  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. BUF_MEM_free(buf);
  457. if (cb != NULL) {
  458. cb(s, SSL_CB_CONNECT_EXIT, ret);
  459. }
  460. return ret;
  461. }
  462. static int dtls1_get_hello_verify(SSL *s) {
  463. long n;
  464. int al, ok = 0;
  465. CBS hello_verify_request, cookie;
  466. uint16_t server_version;
  467. n = s->method->ssl_get_message(
  468. s, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
  469. -1,
  470. /* Use the same maximum size as ssl3_get_server_hello. */
  471. 20000, ssl_hash_message, &ok);
  472. if (!ok) {
  473. return n;
  474. }
  475. if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
  476. s->d1->send_cookie = 0;
  477. s->s3->tmp.reuse_message = 1;
  478. return 1;
  479. }
  480. CBS_init(&hello_verify_request, s->init_msg, n);
  481. if (!CBS_get_u16(&hello_verify_request, &server_version) ||
  482. !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
  483. CBS_len(&hello_verify_request) != 0) {
  484. al = SSL_AD_DECODE_ERROR;
  485. OPENSSL_PUT_ERROR(SSL, dtls1_get_hello_verify, SSL_R_DECODE_ERROR);
  486. goto f_err;
  487. }
  488. if (CBS_len(&cookie) > sizeof(s->d1->cookie)) {
  489. al = SSL_AD_ILLEGAL_PARAMETER;
  490. goto f_err;
  491. }
  492. memcpy(s->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
  493. s->d1->cookie_len = CBS_len(&cookie);
  494. s->d1->send_cookie = 1;
  495. return 1;
  496. f_err:
  497. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  498. return -1;
  499. }