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.
 
 
 
 
 
 

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