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

417 lines
14 KiB

  1. /* DTLS implementation written by Nagendra Modadugu
  2. * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. */
  3. /* ====================================================================
  4. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * 3. All advertising materials mentioning features or use of this
  19. * software must display the following acknowledgment:
  20. * "This product includes software developed by the OpenSSL Project
  21. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  22. *
  23. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  24. * endorse or promote products derived from this software without
  25. * prior written permission. For written permission, please contact
  26. * openssl-core@openssl.org.
  27. *
  28. * 5. Products derived from this software may not be called "OpenSSL"
  29. * nor may "OpenSSL" appear in their names without prior written
  30. * permission of the OpenSSL Project.
  31. *
  32. * 6. Redistributions of any form whatsoever must retain the following
  33. * acknowledgment:
  34. * "This product includes software developed by the OpenSSL Project
  35. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  38. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  39. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  40. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  41. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  42. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  43. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  44. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  45. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  46. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  47. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  48. * OF THE POSSIBILITY OF SUCH DAMAGE.
  49. * ====================================================================
  50. *
  51. * This product includes cryptographic software written by Eric Young
  52. * (eay@cryptsoft.com). This product includes software written by Tim
  53. * Hudson (tjh@cryptsoft.com).
  54. *
  55. */
  56. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  57. * All rights reserved.
  58. *
  59. * This package is an SSL implementation written
  60. * by Eric Young (eay@cryptsoft.com).
  61. * The implementation was written so as to conform with Netscapes SSL.
  62. *
  63. * This library is free for commercial and non-commercial use as long as
  64. * the following conditions are aheared to. The following conditions
  65. * apply to all code found in this distribution, be it the RC4, RSA,
  66. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  67. * included with this distribution is covered by the same copyright terms
  68. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  69. *
  70. * Copyright remains Eric Young's, and as such any Copyright notices in
  71. * the code are not to be removed.
  72. * If this package is used in a product, Eric Young should be given attribution
  73. * as the author of the parts of the library used.
  74. * This can be in the form of a textual message at program startup or
  75. * in documentation (online or textual) provided with the package.
  76. *
  77. * Redistribution and use in source and binary forms, with or without
  78. * modification, are permitted provided that the following conditions
  79. * are met:
  80. * 1. Redistributions of source code must retain the copyright
  81. * notice, this list of conditions and the following disclaimer.
  82. * 2. Redistributions in binary form must reproduce the above copyright
  83. * notice, this list of conditions and the following disclaimer in the
  84. * documentation and/or other materials provided with the distribution.
  85. * 3. All advertising materials mentioning features or use of this software
  86. * must display the following acknowledgement:
  87. * "This product includes cryptographic software written by
  88. * Eric Young (eay@cryptsoft.com)"
  89. * The word 'cryptographic' can be left out if the rouines from the library
  90. * being used are not cryptographic related :-).
  91. * 4. If you include any Windows specific code (or a derivative thereof) from
  92. * the apps directory (application code) you must include an acknowledgement:
  93. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  94. *
  95. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  96. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  97. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  98. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  99. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  100. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  101. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  102. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  103. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  104. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  105. * SUCH DAMAGE.
  106. *
  107. * The licence and distribution terms for any publically available version or
  108. * derivative of this code cannot be changed. i.e. this code cannot simply be
  109. * copied and put under another distribution licence
  110. * [including the GNU Public Licence.] */
  111. #include <openssl/ssl.h>
  112. #include <assert.h>
  113. #include <string.h>
  114. #include <openssl/bio.h>
  115. #include <openssl/buf.h>
  116. #include <openssl/bytestring.h>
  117. #include <openssl/mem.h>
  118. #include <openssl/evp.h>
  119. #include <openssl/err.h>
  120. #include <openssl/rand.h>
  121. #include "../crypto/internal.h"
  122. #include "internal.h"
  123. int dtls1_get_record(SSL *ssl) {
  124. again:
  125. switch (ssl->s3->recv_shutdown) {
  126. case ssl_shutdown_none:
  127. break;
  128. case ssl_shutdown_fatal_alert:
  129. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  130. return -1;
  131. case ssl_shutdown_close_notify:
  132. return 0;
  133. }
  134. /* Read a new packet if there is no unconsumed one. */
  135. if (ssl_read_buffer_len(ssl) == 0) {
  136. int read_ret = ssl_read_buffer_extend_to(ssl, 0 /* unused */);
  137. if (read_ret < 0 && dtls1_is_timer_expired(ssl)) {
  138. /* Historically, timeouts were handled implicitly if the caller did not
  139. * handle them.
  140. *
  141. * TODO(davidben): This was to support blocking sockets but affected
  142. * non-blocking sockets. Can it be removed? */
  143. int timeout_ret = DTLSv1_handle_timeout(ssl);
  144. if (timeout_ret <= 0) {
  145. return timeout_ret;
  146. }
  147. goto again;
  148. }
  149. if (read_ret <= 0) {
  150. return read_ret;
  151. }
  152. }
  153. assert(ssl_read_buffer_len(ssl) > 0);
  154. CBS body;
  155. uint8_t type, alert;
  156. size_t consumed;
  157. enum ssl_open_record_t open_ret =
  158. dtls_open_record(ssl, &type, &body, &consumed, &alert,
  159. ssl_read_buffer(ssl), ssl_read_buffer_len(ssl));
  160. ssl_read_buffer_consume(ssl, consumed);
  161. switch (open_ret) {
  162. case ssl_open_record_partial:
  163. /* Impossible in DTLS. */
  164. break;
  165. case ssl_open_record_success:
  166. if (CBS_len(&body) > 0xffff) {
  167. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  168. return -1;
  169. }
  170. SSL3_RECORD *rr = &ssl->s3->rrec;
  171. rr->type = type;
  172. rr->length = (uint16_t)CBS_len(&body);
  173. rr->data = (uint8_t *)CBS_data(&body);
  174. return 1;
  175. case ssl_open_record_discard:
  176. goto again;
  177. case ssl_open_record_close_notify:
  178. return 0;
  179. case ssl_open_record_fatal_alert:
  180. return -1;
  181. case ssl_open_record_error:
  182. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  183. return -1;
  184. }
  185. assert(0);
  186. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  187. return -1;
  188. }
  189. int dtls1_read_app_data(SSL *ssl, int *out_got_handshake, uint8_t *buf, int len,
  190. int peek) {
  191. assert(!SSL_in_init(ssl));
  192. *out_got_handshake = 0;
  193. SSL3_RECORD *rr = &ssl->s3->rrec;
  194. again:
  195. if (rr->length == 0) {
  196. int ret = dtls1_get_record(ssl);
  197. if (ret <= 0) {
  198. return ret;
  199. }
  200. }
  201. if (rr->type == SSL3_RT_HANDSHAKE) {
  202. /* Parse the first fragment header to determine if this is a pre-CCS or
  203. * post-CCS handshake record. DTLS resets handshake message numbers on each
  204. * handshake, so renegotiations and retransmissions are ambiguous. */
  205. CBS cbs, body;
  206. struct hm_header_st msg_hdr;
  207. CBS_init(&cbs, rr->data, rr->length);
  208. if (!dtls1_parse_fragment(&cbs, &msg_hdr, &body)) {
  209. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  210. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD);
  211. return -1;
  212. }
  213. if (msg_hdr.type == SSL3_MT_FINISHED &&
  214. msg_hdr.seq == ssl->d1->handshake_read_seq - 1) {
  215. if (msg_hdr.frag_off == 0) {
  216. /* Retransmit our last flight of messages. If the peer sends the second
  217. * Finished, they may not have received ours. Only do this for the
  218. * first fragment, in case the Finished was fragmented. */
  219. if (dtls1_check_timeout_num(ssl) < 0) {
  220. return -1;
  221. }
  222. dtls1_retransmit_outgoing_messages(ssl);
  223. }
  224. rr->length = 0;
  225. goto again;
  226. }
  227. /* Otherwise, this is a pre-CCS handshake message from an unsupported
  228. * renegotiation attempt. Fall through to the error path. */
  229. }
  230. if (rr->type != SSL3_RT_APPLICATION_DATA) {
  231. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  232. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
  233. return -1;
  234. }
  235. /* Discard empty records. */
  236. if (rr->length == 0) {
  237. goto again;
  238. }
  239. if (len <= 0) {
  240. return len;
  241. }
  242. if ((unsigned)len > rr->length) {
  243. len = rr->length;
  244. }
  245. OPENSSL_memcpy(buf, rr->data, len);
  246. if (!peek) {
  247. /* TODO(davidben): Should the record be truncated instead? This is a
  248. * datagram transport. See https://crbug.com/boringssl/65. */
  249. rr->length -= len;
  250. rr->data += len;
  251. if (rr->length == 0) {
  252. /* The record has been consumed, so we may now clear the buffer. */
  253. ssl_read_buffer_discard(ssl);
  254. }
  255. }
  256. return len;
  257. }
  258. int dtls1_read_change_cipher_spec(SSL *ssl) {
  259. SSL3_RECORD *rr = &ssl->s3->rrec;
  260. again:
  261. if (rr->length == 0) {
  262. int ret = dtls1_get_record(ssl);
  263. if (ret <= 0) {
  264. return ret;
  265. }
  266. }
  267. /* Drop handshake records silently. The epochs match, so this must be a
  268. * retransmit of a message we already received. */
  269. if (rr->type == SSL3_RT_HANDSHAKE) {
  270. rr->length = 0;
  271. goto again;
  272. }
  273. /* Other record types are illegal in this epoch. Note all application data
  274. * records come in the encrypted epoch. */
  275. if (rr->type != SSL3_RT_CHANGE_CIPHER_SPEC) {
  276. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  277. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
  278. return -1;
  279. }
  280. if (rr->length != 1 || rr->data[0] != SSL3_MT_CCS) {
  281. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
  282. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  283. return -1;
  284. }
  285. ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data,
  286. rr->length);
  287. rr->length = 0;
  288. ssl_read_buffer_discard(ssl);
  289. return 1;
  290. }
  291. void dtls1_read_close_notify(SSL *ssl) {
  292. /* Bidirectional shutdown doesn't make sense for an unordered transport. DTLS
  293. * alerts also aren't delivered reliably, so we may even time out because the
  294. * peer never received our close_notify. Report to the caller that the channel
  295. * has fully shut down. */
  296. if (ssl->s3->recv_shutdown == ssl_shutdown_none) {
  297. ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
  298. }
  299. }
  300. int dtls1_write_app_data(SSL *ssl, int *out_needs_handshake, const uint8_t *buf,
  301. int len) {
  302. assert(!SSL_in_init(ssl));
  303. *out_needs_handshake = 0;
  304. if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
  305. OPENSSL_PUT_ERROR(SSL, SSL_R_DTLS_MESSAGE_TOO_BIG);
  306. return -1;
  307. }
  308. if (len < 0) {
  309. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH);
  310. return -1;
  311. }
  312. if (len == 0) {
  313. return 0;
  314. }
  315. int ret = dtls1_write_record(ssl, SSL3_RT_APPLICATION_DATA, buf, (size_t)len,
  316. dtls1_use_current_epoch);
  317. if (ret <= 0) {
  318. return ret;
  319. }
  320. return len;
  321. }
  322. int dtls1_write_record(SSL *ssl, int type, const uint8_t *buf, size_t len,
  323. enum dtls1_use_epoch_t use_epoch) {
  324. assert(len <= SSL3_RT_MAX_PLAIN_LENGTH);
  325. /* There should never be a pending write buffer in DTLS. One can't write half
  326. * a datagram, so the write buffer is always dropped in
  327. * |ssl_write_buffer_flush|. */
  328. assert(!ssl_write_buffer_is_pending(ssl));
  329. if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
  330. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  331. return -1;
  332. }
  333. size_t max_out = len + SSL_max_seal_overhead(ssl);
  334. uint8_t *out;
  335. size_t ciphertext_len;
  336. if (!ssl_write_buffer_init(ssl, &out, max_out) ||
  337. !dtls_seal_record(ssl, out, &ciphertext_len, max_out, type, buf, len,
  338. use_epoch)) {
  339. ssl_write_buffer_clear(ssl);
  340. return -1;
  341. }
  342. ssl_write_buffer_set_len(ssl, ciphertext_len);
  343. int ret = ssl_write_buffer_flush(ssl);
  344. if (ret <= 0) {
  345. return ret;
  346. }
  347. return 1;
  348. }
  349. int dtls1_dispatch_alert(SSL *ssl) {
  350. int ret = dtls1_write_record(ssl, SSL3_RT_ALERT, &ssl->s3->send_alert[0], 2,
  351. dtls1_use_current_epoch);
  352. if (ret <= 0) {
  353. return ret;
  354. }
  355. ssl->s3->alert_dispatch = 0;
  356. /* If the alert is fatal, flush the BIO now. */
  357. if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) {
  358. BIO_flush(ssl->wbio);
  359. }
  360. ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_ALERT, ssl->s3->send_alert,
  361. 2);
  362. int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1];
  363. ssl_do_info_callback(ssl, SSL_CB_WRITE_ALERT, alert);
  364. return 1;
  365. }