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.
 
 
 
 
 
 

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