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.
 
 
 
 
 
 

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