Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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