d1_clnt.c 17 KB

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