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

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