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.
 
 
 
 
 
 

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