25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

468 lines
14 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 <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, 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. if (cb != NULL) {
  152. cb(s, SSL_CB_HANDSHAKE_START, 1);
  153. }
  154. if (s->init_buf == NULL) {
  155. buf = BUF_MEM_new();
  156. if (buf == NULL || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
  157. ret = -1;
  158. goto end;
  159. }
  160. s->init_buf = buf;
  161. buf = NULL;
  162. }
  163. s->init_num = 0;
  164. if (!ssl_init_wbio_buffer(s, 1)) {
  165. ret = -1;
  166. goto end;
  167. }
  168. if (!ssl3_init_finished_mac(s)) {
  169. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  170. ret = -1;
  171. goto end;
  172. }
  173. s->state = SSL3_ST_SR_CLNT_HELLO_A;
  174. break;
  175. case SSL3_ST_SR_CLNT_HELLO_A:
  176. case SSL3_ST_SR_CLNT_HELLO_B:
  177. case SSL3_ST_SR_CLNT_HELLO_C:
  178. case SSL3_ST_SR_CLNT_HELLO_D:
  179. s->shutdown = 0;
  180. ret = ssl3_get_client_hello(s);
  181. if (ret <= 0) {
  182. goto end;
  183. }
  184. dtls1_stop_timer(s);
  185. s->state = SSL3_ST_SW_SRVR_HELLO_A;
  186. s->init_num = 0;
  187. break;
  188. case SSL3_ST_SW_SRVR_HELLO_A:
  189. case SSL3_ST_SW_SRVR_HELLO_B:
  190. dtls1_start_timer(s);
  191. ret = ssl3_send_server_hello(s);
  192. if (ret <= 0) {
  193. goto end;
  194. }
  195. if (s->hit) {
  196. if (s->tlsext_ticket_expected) {
  197. s->state = SSL3_ST_SW_SESSION_TICKET_A;
  198. } else {
  199. s->state = SSL3_ST_SW_CHANGE_A;
  200. }
  201. } else {
  202. s->state = SSL3_ST_SW_CERT_A;
  203. }
  204. s->init_num = 0;
  205. break;
  206. case SSL3_ST_SW_CERT_A:
  207. case SSL3_ST_SW_CERT_B:
  208. if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
  209. dtls1_start_timer(s);
  210. ret = ssl3_send_server_certificate(s);
  211. if (ret <= 0) {
  212. goto end;
  213. }
  214. if (s->s3->tmp.certificate_status_expected) {
  215. s->state = SSL3_ST_SW_CERT_STATUS_A;
  216. } else {
  217. s->state = SSL3_ST_SW_KEY_EXCH_A;
  218. }
  219. } else {
  220. skip = 1;
  221. s->state = SSL3_ST_SW_KEY_EXCH_A;
  222. }
  223. s->init_num = 0;
  224. break;
  225. case SSL3_ST_SW_KEY_EXCH_A:
  226. case SSL3_ST_SW_KEY_EXCH_B:
  227. case SSL3_ST_SW_KEY_EXCH_C:
  228. case SSL3_ST_SW_KEY_EXCH_D:
  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 (s->s3->tmp.cert_request) {
  254. dtls1_start_timer(s);
  255. ret = ssl3_send_certificate_request(s);
  256. if (ret <= 0) {
  257. goto end;
  258. }
  259. } else {
  260. skip = 1;
  261. }
  262. s->state = SSL3_ST_SW_SRVR_DONE_A;
  263. s->init_num = 0;
  264. break;
  265. case SSL3_ST_SW_SRVR_DONE_A:
  266. case SSL3_ST_SW_SRVR_DONE_B:
  267. dtls1_start_timer(s);
  268. ret = ssl3_send_server_done(s);
  269. if (ret <= 0) {
  270. goto end;
  271. }
  272. s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
  273. s->state = SSL3_ST_SW_FLUSH;
  274. s->init_num = 0;
  275. break;
  276. case SSL3_ST_SW_FLUSH:
  277. s->rwstate = SSL_WRITING;
  278. if (BIO_flush(s->wbio) <= 0) {
  279. ret = -1;
  280. goto end;
  281. }
  282. s->rwstate = SSL_NOTHING;
  283. s->state = s->s3->tmp.next_state;
  284. break;
  285. case SSL3_ST_SR_CERT_A:
  286. case SSL3_ST_SR_CERT_B:
  287. if (s->s3->tmp.cert_request) {
  288. ret = ssl3_get_client_certificate(s);
  289. if (ret <= 0) {
  290. goto end;
  291. }
  292. }
  293. s->init_num = 0;
  294. s->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. ret = ssl3_get_client_key_exchange(s);
  299. if (ret <= 0) {
  300. goto end;
  301. }
  302. s->state = SSL3_ST_SR_CERT_VRFY_A;
  303. s->init_num = 0;
  304. break;
  305. case SSL3_ST_SR_CERT_VRFY_A:
  306. case SSL3_ST_SR_CERT_VRFY_B:
  307. ret = ssl3_get_cert_verify(s);
  308. if (ret <= 0) {
  309. goto end;
  310. }
  311. s->state = SSL3_ST_SR_FINISHED_A;
  312. s->init_num = 0;
  313. break;
  314. case SSL3_ST_SR_FINISHED_A:
  315. case SSL3_ST_SR_FINISHED_B:
  316. s->d1->change_cipher_spec_ok = 1;
  317. ret =
  318. ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, SSL3_ST_SR_FINISHED_B);
  319. if (ret <= 0) {
  320. goto end;
  321. }
  322. dtls1_stop_timer(s);
  323. if (s->hit) {
  324. s->state = SSL_ST_OK;
  325. } else if (s->tlsext_ticket_expected) {
  326. s->state = SSL3_ST_SW_SESSION_TICKET_A;
  327. } else {
  328. s->state = SSL3_ST_SW_CHANGE_A;
  329. }
  330. s->init_num = 0;
  331. break;
  332. case SSL3_ST_SW_SESSION_TICKET_A:
  333. case SSL3_ST_SW_SESSION_TICKET_B:
  334. ret = ssl3_send_new_session_ticket(s);
  335. if (ret <= 0) {
  336. goto end;
  337. }
  338. s->state = SSL3_ST_SW_CHANGE_A;
  339. s->init_num = 0;
  340. break;
  341. case SSL3_ST_SW_CHANGE_A:
  342. case SSL3_ST_SW_CHANGE_B:
  343. s->session->cipher = s->s3->tmp.new_cipher;
  344. if (!s->enc_method->setup_key_block(s)) {
  345. ret = -1;
  346. goto end;
  347. }
  348. ret = dtls1_send_change_cipher_spec(s, SSL3_ST_SW_CHANGE_A,
  349. SSL3_ST_SW_CHANGE_B);
  350. if (ret <= 0) {
  351. goto end;
  352. }
  353. s->state = SSL3_ST_SW_FINISHED_A;
  354. s->init_num = 0;
  355. if (!s->enc_method->change_cipher_state(
  356. s, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
  357. ret = -1;
  358. goto end;
  359. }
  360. dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
  361. break;
  362. case SSL3_ST_SW_FINISHED_A:
  363. case SSL3_ST_SW_FINISHED_B:
  364. ret =
  365. ssl3_send_finished(s, SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B,
  366. s->enc_method->server_finished_label,
  367. s->enc_method->server_finished_label_len);
  368. if (ret <= 0) {
  369. goto end;
  370. }
  371. s->state = SSL3_ST_SW_FLUSH;
  372. if (s->hit) {
  373. s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
  374. } else {
  375. s->s3->tmp.next_state = SSL_ST_OK;
  376. }
  377. s->init_num = 0;
  378. break;
  379. case SSL_ST_OK:
  380. ssl3_cleanup_key_block(s);
  381. /* remove buffering on output */
  382. ssl_free_wbio_buffer(s);
  383. s->init_num = 0;
  384. s->s3->initial_handshake_complete = 1;
  385. ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
  386. if (cb != NULL) {
  387. cb(s, SSL_CB_HANDSHAKE_DONE, 1);
  388. }
  389. ret = 1;
  390. /* done handshaking, next message is client hello */
  391. s->d1->handshake_read_seq = 0;
  392. /* next message is server hello */
  393. s->d1->handshake_write_seq = 0;
  394. s->d1->next_handshake_write_seq = 0;
  395. goto end;
  396. default:
  397. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
  398. ret = -1;
  399. goto end;
  400. }
  401. if (!s->s3->tmp.reuse_message && !skip) {
  402. if (cb != NULL && s->state != state) {
  403. new_state = s->state;
  404. s->state = state;
  405. cb(s, SSL_CB_ACCEPT_LOOP, 1);
  406. s->state = new_state;
  407. }
  408. }
  409. skip = 0;
  410. }
  411. end:
  412. s->in_handshake--;
  413. BUF_MEM_free(buf);
  414. if (cb != NULL) {
  415. cb(s, SSL_CB_ACCEPT_EXIT, ret);
  416. }
  417. return ret;
  418. }