You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 <openssl/bn.h>
  117. #include <openssl/buf.h>
  118. #include <openssl/dh.h>
  119. #include <openssl/err.h>
  120. #include <openssl/evp.h>
  121. #include <openssl/md5.h>
  122. #include <openssl/obj.h>
  123. #include <openssl/rand.h>
  124. #include <openssl/x509.h>
  125. #include "internal.h"
  126. int dtls1_accept(SSL *s) {
  127. BUF_MEM *buf = NULL;
  128. void (*cb)(const SSL *ssl, int type, int val) = NULL;
  129. uint32_t alg_a;
  130. int ret = -1;
  131. int new_state, state, skip = 0;
  132. assert(s->handshake_func == dtls1_accept);
  133. assert(s->server);
  134. assert(SSL_IS_DTLS(s));
  135. ERR_clear_error();
  136. ERR_clear_system_error();
  137. if (s->info_callback != NULL) {
  138. cb = s->info_callback;
  139. } else if (s->ctx->info_callback != NULL) {
  140. cb = s->ctx->info_callback;
  141. }
  142. s->in_handshake++;
  143. if (s->cert == NULL) {
  144. OPENSSL_PUT_ERROR(SSL, dtls1_accept, SSL_R_NO_CERTIFICATE_SET);
  145. return -1;
  146. }
  147. for (;;) {
  148. state = s->state;
  149. switch (s->state) {
  150. case SSL_ST_ACCEPT:
  151. case SSL_ST_BEFORE | SSL_ST_ACCEPT:
  152. if (cb != NULL) {
  153. cb(s, SSL_CB_HANDSHAKE_START, 1);
  154. }
  155. if (s->init_buf == NULL) {
  156. buf = BUF_MEM_new();
  157. if (buf == NULL || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
  158. ret = -1;
  159. goto end;
  160. }
  161. s->init_buf = buf;
  162. buf = NULL;
  163. }
  164. s->init_num = 0;
  165. if (!ssl_init_wbio_buffer(s, 1)) {
  166. ret = -1;
  167. goto end;
  168. }
  169. if (!ssl3_init_finished_mac(s)) {
  170. OPENSSL_PUT_ERROR(SSL, dtls1_accept, ERR_R_INTERNAL_ERROR);
  171. ret = -1;
  172. goto end;
  173. }
  174. s->state = SSL3_ST_SR_CLNT_HELLO_A;
  175. break;
  176. case SSL3_ST_SR_CLNT_HELLO_A:
  177. case SSL3_ST_SR_CLNT_HELLO_B:
  178. case SSL3_ST_SR_CLNT_HELLO_C:
  179. case SSL3_ST_SR_CLNT_HELLO_D:
  180. s->shutdown = 0;
  181. ret = ssl3_get_client_hello(s);
  182. if (ret <= 0) {
  183. goto end;
  184. }
  185. dtls1_stop_timer(s);
  186. s->state = SSL3_ST_SW_SRVR_HELLO_A;
  187. s->init_num = 0;
  188. break;
  189. case SSL3_ST_SW_SRVR_HELLO_A:
  190. case SSL3_ST_SW_SRVR_HELLO_B:
  191. s->renegotiate = 2;
  192. dtls1_start_timer(s);
  193. ret = ssl3_send_server_hello(s);
  194. if (ret <= 0) {
  195. goto end;
  196. }
  197. if (s->hit) {
  198. if (s->tlsext_ticket_expected) {
  199. s->state = SSL3_ST_SW_SESSION_TICKET_A;
  200. } else {
  201. s->state = SSL3_ST_SW_CHANGE_A;
  202. }
  203. } else {
  204. s->state = SSL3_ST_SW_CERT_A;
  205. }
  206. s->init_num = 0;
  207. break;
  208. case SSL3_ST_SW_CERT_A:
  209. case SSL3_ST_SW_CERT_B:
  210. if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
  211. dtls1_start_timer(s);
  212. ret = ssl3_send_server_certificate(s);
  213. if (ret <= 0) {
  214. goto end;
  215. }
  216. if (s->s3->tmp.certificate_status_expected) {
  217. s->state = SSL3_ST_SW_CERT_STATUS_A;
  218. } else {
  219. s->state = SSL3_ST_SW_KEY_EXCH_A;
  220. }
  221. } else {
  222. skip = 1;
  223. s->state = SSL3_ST_SW_KEY_EXCH_A;
  224. }
  225. s->init_num = 0;
  226. break;
  227. case SSL3_ST_SW_KEY_EXCH_A:
  228. case SSL3_ST_SW_KEY_EXCH_B:
  229. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  230. /* Send a ServerKeyExchange message if:
  231. * - The key exchange is ephemeral or anonymous
  232. * Diffie-Hellman.
  233. * - There is a PSK identity hint.
  234. *
  235. * TODO(davidben): This logic is currently duplicated
  236. * in s3_srvr.c. Fix this. In the meantime, keep them
  237. * in sync. */
  238. if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher) ||
  239. ((alg_a & SSL_aPSK) && s->psk_identity_hint)) {
  240. dtls1_start_timer(s);
  241. ret = ssl3_send_server_key_exchange(s);
  242. if (ret <= 0) {
  243. goto end;
  244. }
  245. } else {
  246. skip = 1;
  247. }
  248. s->state = SSL3_ST_SW_CERT_REQ_A;
  249. s->init_num = 0;
  250. break;
  251. case SSL3_ST_SW_CERT_REQ_A:
  252. case SSL3_ST_SW_CERT_REQ_B:
  253. if (/* don't request cert unless asked for it: */
  254. !(s->verify_mode & SSL_VERIFY_PEER) ||
  255. /* if SSL_VERIFY_CLIENT_ONCE is set,
  256. * don't request cert during re-negotiation: */
  257. ((s->session->peer != NULL) &&
  258. (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
  259. /* With normal PSK Certificates and
  260. * Certificate Requests are omitted */
  261. (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
  262. /* no cert request */
  263. skip = 1;
  264. s->s3->tmp.cert_request = 0;
  265. s->state = SSL3_ST_SW_SRVR_DONE_A;
  266. } else {
  267. s->s3->tmp.cert_request = 1;
  268. dtls1_start_timer(s);
  269. ret = ssl3_send_certificate_request(s);
  270. if (ret <= 0) {
  271. goto end;
  272. }
  273. s->state = SSL3_ST_SW_SRVR_DONE_A;
  274. s->init_num = 0;
  275. }
  276. break;
  277. case SSL3_ST_SW_SRVR_DONE_A:
  278. case SSL3_ST_SW_SRVR_DONE_B:
  279. dtls1_start_timer(s);
  280. ret = ssl3_send_server_done(s);
  281. if (ret <= 0) {
  282. goto end;
  283. }
  284. s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
  285. s->state = SSL3_ST_SW_FLUSH;
  286. s->init_num = 0;
  287. break;
  288. case SSL3_ST_SW_FLUSH:
  289. s->rwstate = SSL_WRITING;
  290. if (BIO_flush(s->wbio) <= 0) {
  291. ret = -1;
  292. goto end;
  293. }
  294. s->rwstate = SSL_NOTHING;
  295. s->state = s->s3->tmp.next_state;
  296. break;
  297. case SSL3_ST_SR_CERT_A:
  298. case SSL3_ST_SR_CERT_B:
  299. if (s->s3->tmp.cert_request) {
  300. ret = ssl3_get_client_certificate(s);
  301. if (ret <= 0) {
  302. goto end;
  303. }
  304. }
  305. s->init_num = 0;
  306. s->state = SSL3_ST_SR_KEY_EXCH_A;
  307. break;
  308. case SSL3_ST_SR_KEY_EXCH_A:
  309. case SSL3_ST_SR_KEY_EXCH_B:
  310. ret = ssl3_get_client_key_exchange(s);
  311. if (ret <= 0) {
  312. goto end;
  313. }
  314. s->state = SSL3_ST_SR_CERT_VRFY_A;
  315. s->init_num = 0;
  316. break;
  317. case SSL3_ST_SR_CERT_VRFY_A:
  318. case SSL3_ST_SR_CERT_VRFY_B:
  319. ret = ssl3_get_cert_verify(s);
  320. if (ret <= 0) {
  321. goto end;
  322. }
  323. s->state = SSL3_ST_SR_FINISHED_A;
  324. s->init_num = 0;
  325. break;
  326. case SSL3_ST_SR_FINISHED_A:
  327. case SSL3_ST_SR_FINISHED_B:
  328. s->d1->change_cipher_spec_ok = 1;
  329. ret =
  330. ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, SSL3_ST_SR_FINISHED_B);
  331. if (ret <= 0) {
  332. goto end;
  333. }
  334. dtls1_stop_timer(s);
  335. if (s->hit) {
  336. s->state = SSL_ST_OK;
  337. } else if (s->tlsext_ticket_expected) {
  338. s->state = SSL3_ST_SW_SESSION_TICKET_A;
  339. } else {
  340. s->state = SSL3_ST_SW_CHANGE_A;
  341. }
  342. s->init_num = 0;
  343. break;
  344. case SSL3_ST_SW_SESSION_TICKET_A:
  345. case SSL3_ST_SW_SESSION_TICKET_B:
  346. ret = ssl3_send_new_session_ticket(s);
  347. if (ret <= 0) {
  348. goto end;
  349. }
  350. s->state = SSL3_ST_SW_CHANGE_A;
  351. s->init_num = 0;
  352. break;
  353. case SSL3_ST_SW_CHANGE_A:
  354. case SSL3_ST_SW_CHANGE_B:
  355. s->session->cipher = s->s3->tmp.new_cipher;
  356. if (!s->enc_method->setup_key_block(s)) {
  357. ret = -1;
  358. goto end;
  359. }
  360. ret = dtls1_send_change_cipher_spec(s, SSL3_ST_SW_CHANGE_A,
  361. SSL3_ST_SW_CHANGE_B);
  362. if (ret <= 0) {
  363. goto end;
  364. }
  365. s->state = SSL3_ST_SW_FINISHED_A;
  366. s->init_num = 0;
  367. if (!s->enc_method->change_cipher_state(
  368. s, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
  369. ret = -1;
  370. goto end;
  371. }
  372. dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
  373. break;
  374. case SSL3_ST_SW_FINISHED_A:
  375. case SSL3_ST_SW_FINISHED_B:
  376. ret =
  377. ssl3_send_finished(s, SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B,
  378. s->enc_method->server_finished_label,
  379. s->enc_method->server_finished_label_len);
  380. if (ret <= 0) {
  381. goto end;
  382. }
  383. s->state = SSL3_ST_SW_FLUSH;
  384. if (s->hit) {
  385. s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
  386. } else {
  387. s->s3->tmp.next_state = SSL_ST_OK;
  388. }
  389. s->init_num = 0;
  390. break;
  391. case SSL_ST_OK:
  392. ssl3_cleanup_key_block(s);
  393. /* remove buffering on output */
  394. ssl_free_wbio_buffer(s);
  395. s->init_num = 0;
  396. s->renegotiate = 0;
  397. s->s3->initial_handshake_complete = 1;
  398. ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
  399. if (cb != NULL) {
  400. cb(s, SSL_CB_HANDSHAKE_DONE, 1);
  401. }
  402. ret = 1;
  403. /* done handshaking, next message is client hello */
  404. s->d1->handshake_read_seq = 0;
  405. /* next message is server hello */
  406. s->d1->handshake_write_seq = 0;
  407. s->d1->next_handshake_write_seq = 0;
  408. goto end;
  409. default:
  410. OPENSSL_PUT_ERROR(SSL, dtls1_accept, SSL_R_UNKNOWN_STATE);
  411. ret = -1;
  412. goto end;
  413. }
  414. if (!s->s3->tmp.reuse_message && !skip) {
  415. if (cb != NULL && s->state != state) {
  416. new_state = s->state;
  417. s->state = state;
  418. cb(s, SSL_CB_ACCEPT_LOOP, 1);
  419. s->state = new_state;
  420. }
  421. }
  422. skip = 0;
  423. }
  424. end:
  425. s->in_handshake--;
  426. BUF_MEM_free(buf);
  427. if (cb != NULL) {
  428. cb(s, SSL_CB_ACCEPT_EXIT, ret);
  429. }
  430. return ret;
  431. }