Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

466 righe
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, 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_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, dtls1_accept, 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. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  228. /* Send a ServerKeyExchange message if:
  229. * - The key exchange is ephemeral or anonymous
  230. * Diffie-Hellman.
  231. * - There is a PSK identity hint.
  232. *
  233. * TODO(davidben): This logic is currently duplicated
  234. * in s3_srvr.c. Fix this. In the meantime, keep them
  235. * in sync. */
  236. if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher) ||
  237. ((alg_a & SSL_aPSK) && s->psk_identity_hint)) {
  238. dtls1_start_timer(s);
  239. ret = ssl3_send_server_key_exchange(s);
  240. if (ret <= 0) {
  241. goto end;
  242. }
  243. } else {
  244. skip = 1;
  245. }
  246. s->state = SSL3_ST_SW_CERT_REQ_A;
  247. s->init_num = 0;
  248. break;
  249. case SSL3_ST_SW_CERT_REQ_A:
  250. case SSL3_ST_SW_CERT_REQ_B:
  251. if (s->s3->tmp.cert_request) {
  252. dtls1_start_timer(s);
  253. ret = ssl3_send_certificate_request(s);
  254. if (ret <= 0) {
  255. goto end;
  256. }
  257. } else {
  258. skip = 1;
  259. }
  260. s->state = SSL3_ST_SW_SRVR_DONE_A;
  261. s->init_num = 0;
  262. break;
  263. case SSL3_ST_SW_SRVR_DONE_A:
  264. case SSL3_ST_SW_SRVR_DONE_B:
  265. dtls1_start_timer(s);
  266. ret = ssl3_send_server_done(s);
  267. if (ret <= 0) {
  268. goto end;
  269. }
  270. s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
  271. s->state = SSL3_ST_SW_FLUSH;
  272. s->init_num = 0;
  273. break;
  274. case SSL3_ST_SW_FLUSH:
  275. s->rwstate = SSL_WRITING;
  276. if (BIO_flush(s->wbio) <= 0) {
  277. ret = -1;
  278. goto end;
  279. }
  280. s->rwstate = SSL_NOTHING;
  281. s->state = s->s3->tmp.next_state;
  282. break;
  283. case SSL3_ST_SR_CERT_A:
  284. case SSL3_ST_SR_CERT_B:
  285. if (s->s3->tmp.cert_request) {
  286. ret = ssl3_get_client_certificate(s);
  287. if (ret <= 0) {
  288. goto end;
  289. }
  290. }
  291. s->init_num = 0;
  292. s->state = SSL3_ST_SR_KEY_EXCH_A;
  293. break;
  294. case SSL3_ST_SR_KEY_EXCH_A:
  295. case SSL3_ST_SR_KEY_EXCH_B:
  296. ret = ssl3_get_client_key_exchange(s);
  297. if (ret <= 0) {
  298. goto end;
  299. }
  300. s->state = SSL3_ST_SR_CERT_VRFY_A;
  301. s->init_num = 0;
  302. break;
  303. case SSL3_ST_SR_CERT_VRFY_A:
  304. case SSL3_ST_SR_CERT_VRFY_B:
  305. ret = ssl3_get_cert_verify(s);
  306. if (ret <= 0) {
  307. goto end;
  308. }
  309. s->state = SSL3_ST_SR_FINISHED_A;
  310. s->init_num = 0;
  311. break;
  312. case SSL3_ST_SR_FINISHED_A:
  313. case SSL3_ST_SR_FINISHED_B:
  314. s->d1->change_cipher_spec_ok = 1;
  315. ret =
  316. ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, SSL3_ST_SR_FINISHED_B);
  317. if (ret <= 0) {
  318. goto end;
  319. }
  320. dtls1_stop_timer(s);
  321. if (s->hit) {
  322. s->state = SSL_ST_OK;
  323. } else if (s->tlsext_ticket_expected) {
  324. s->state = SSL3_ST_SW_SESSION_TICKET_A;
  325. } else {
  326. s->state = SSL3_ST_SW_CHANGE_A;
  327. }
  328. s->init_num = 0;
  329. break;
  330. case SSL3_ST_SW_SESSION_TICKET_A:
  331. case SSL3_ST_SW_SESSION_TICKET_B:
  332. ret = ssl3_send_new_session_ticket(s);
  333. if (ret <= 0) {
  334. goto end;
  335. }
  336. s->state = SSL3_ST_SW_CHANGE_A;
  337. s->init_num = 0;
  338. break;
  339. case SSL3_ST_SW_CHANGE_A:
  340. case SSL3_ST_SW_CHANGE_B:
  341. s->session->cipher = s->s3->tmp.new_cipher;
  342. if (!s->enc_method->setup_key_block(s)) {
  343. ret = -1;
  344. goto end;
  345. }
  346. ret = dtls1_send_change_cipher_spec(s, SSL3_ST_SW_CHANGE_A,
  347. SSL3_ST_SW_CHANGE_B);
  348. if (ret <= 0) {
  349. goto end;
  350. }
  351. s->state = SSL3_ST_SW_FINISHED_A;
  352. s->init_num = 0;
  353. if (!s->enc_method->change_cipher_state(
  354. s, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
  355. ret = -1;
  356. goto end;
  357. }
  358. dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
  359. break;
  360. case SSL3_ST_SW_FINISHED_A:
  361. case SSL3_ST_SW_FINISHED_B:
  362. ret =
  363. ssl3_send_finished(s, SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B,
  364. s->enc_method->server_finished_label,
  365. s->enc_method->server_finished_label_len);
  366. if (ret <= 0) {
  367. goto end;
  368. }
  369. s->state = SSL3_ST_SW_FLUSH;
  370. if (s->hit) {
  371. s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
  372. } else {
  373. s->s3->tmp.next_state = SSL_ST_OK;
  374. }
  375. s->init_num = 0;
  376. break;
  377. case SSL_ST_OK:
  378. ssl3_cleanup_key_block(s);
  379. /* remove buffering on output */
  380. ssl_free_wbio_buffer(s);
  381. s->init_num = 0;
  382. s->s3->initial_handshake_complete = 1;
  383. ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
  384. if (cb != NULL) {
  385. cb(s, SSL_CB_HANDSHAKE_DONE, 1);
  386. }
  387. ret = 1;
  388. /* done handshaking, next message is client hello */
  389. s->d1->handshake_read_seq = 0;
  390. /* next message is server hello */
  391. s->d1->handshake_write_seq = 0;
  392. s->d1->next_handshake_write_seq = 0;
  393. goto end;
  394. default:
  395. OPENSSL_PUT_ERROR(SSL, dtls1_accept, SSL_R_UNKNOWN_STATE);
  396. ret = -1;
  397. goto end;
  398. }
  399. if (!s->s3->tmp.reuse_message && !skip) {
  400. if (cb != NULL && s->state != state) {
  401. new_state = s->state;
  402. s->state = state;
  403. cb(s, SSL_CB_ACCEPT_LOOP, 1);
  404. s->state = new_state;
  405. }
  406. }
  407. skip = 0;
  408. }
  409. end:
  410. s->in_handshake--;
  411. BUF_MEM_free(buf);
  412. if (cb != NULL) {
  413. cb(s, SSL_CB_ACCEPT_EXIT, ret);
  414. }
  415. return ret;
  416. }