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ů.
 
 
 
 
 
 

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