No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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