Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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