選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

dtls_record.c 12 KiB

Factor out the buffering and low-level record code. This begins decoupling the transport from the SSL state machine. The buffering logic is hidden behind an opaque API. Fields like ssl->packet and ssl->packet_length are gone. ssl3_get_record and dtls1_get_record now call low-level tls_open_record and dtls_open_record functions that unpack a single record independent of who owns the buffer. Both may be called in-place. This removes ssl->rstate which was redundant with the buffer length. Future work will push the buffer up the stack until it is above the handshake. Then we can expose SSL_open and SSL_seal APIs which act like *_open_record but return a slightly larger enum due to other events being possible. Likewise the handshake state machine will be detached from its buffer. The existing SSL_read, SSL_write, etc., APIs will be implemented on top of SSL_open, etc., combined with ssl_read_buffer_* and ssl_write_buffer_*. (Which is why ssl_read_buffer_extend still tries to abstract between TLS's and DTLS's fairly different needs.) The new buffering logic does not support read-ahead (removed previously) since it lacks a memmove on ssl_read_buffer_discard for TLS, but this could be added if desired. The old buffering logic wasn't quite right anyway; it tried to avoid the memmove in some cases and could get stuck too far into the buffer and not accept records. (The only time the memmove is optional is in DTLS or if enough of the record header is available to know that the entire next record would fit in the buffer.) The new logic also now actually decrypts the ciphertext in-place again, rather than almost in-place when there's an explicit nonce/IV. (That accidentally switched in https://boringssl-review.googlesource.com/#/c/4792/; see 3d59e04bce96474099ba76786a2337e99ae14505.) BUG=468889 Change-Id: I403c1626253c46897f47c7ae93aeab1064b767b2 Reviewed-on: https://boringssl-review.googlesource.com/5715 Reviewed-by: Adam Langley <agl@google.com>
9年前
Factor out the buffering and low-level record code. This begins decoupling the transport from the SSL state machine. The buffering logic is hidden behind an opaque API. Fields like ssl->packet and ssl->packet_length are gone. ssl3_get_record and dtls1_get_record now call low-level tls_open_record and dtls_open_record functions that unpack a single record independent of who owns the buffer. Both may be called in-place. This removes ssl->rstate which was redundant with the buffer length. Future work will push the buffer up the stack until it is above the handshake. Then we can expose SSL_open and SSL_seal APIs which act like *_open_record but return a slightly larger enum due to other events being possible. Likewise the handshake state machine will be detached from its buffer. The existing SSL_read, SSL_write, etc., APIs will be implemented on top of SSL_open, etc., combined with ssl_read_buffer_* and ssl_write_buffer_*. (Which is why ssl_read_buffer_extend still tries to abstract between TLS's and DTLS's fairly different needs.) The new buffering logic does not support read-ahead (removed previously) since it lacks a memmove on ssl_read_buffer_discard for TLS, but this could be added if desired. The old buffering logic wasn't quite right anyway; it tried to avoid the memmove in some cases and could get stuck too far into the buffer and not accept records. (The only time the memmove is optional is in DTLS or if enough of the record header is available to know that the entire next record would fit in the buffer.) The new logic also now actually decrypts the ciphertext in-place again, rather than almost in-place when there's an explicit nonce/IV. (That accidentally switched in https://boringssl-review.googlesource.com/#/c/4792/; see 3d59e04bce96474099ba76786a2337e99ae14505.) BUG=468889 Change-Id: I403c1626253c46897f47c7ae93aeab1064b767b2 Reviewed-on: https://boringssl-review.googlesource.com/5715 Reviewed-by: Adam Langley <agl@google.com>
9年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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/bytestring.h>
  115. #include <openssl/err.h>
  116. #include "internal.h"
  117. /* to_u64_be treats |in| as a 8-byte big-endian integer and returns the value as
  118. * a |uint64_t|. */
  119. static uint64_t to_u64_be(const uint8_t in[8]) {
  120. uint64_t ret = 0;
  121. unsigned i;
  122. for (i = 0; i < 8; i++) {
  123. ret <<= 8;
  124. ret |= in[i];
  125. }
  126. return ret;
  127. }
  128. /* dtls1_bitmap_should_discard returns one if |seq_num| has been seen in |bitmap|
  129. * or is stale. Otherwise it returns zero. */
  130. static int dtls1_bitmap_should_discard(DTLS1_BITMAP *bitmap,
  131. const uint8_t seq_num[8]) {
  132. const unsigned kWindowSize = sizeof(bitmap->map) * 8;
  133. uint64_t seq_num_u = to_u64_be(seq_num);
  134. if (seq_num_u > bitmap->max_seq_num) {
  135. return 0;
  136. }
  137. uint64_t idx = bitmap->max_seq_num - seq_num_u;
  138. return idx >= kWindowSize || (bitmap->map & (((uint64_t)1) << idx));
  139. }
  140. /* dtls1_bitmap_record updates |bitmap| to record receipt of sequence number
  141. * |seq_num|. It slides the window forward if needed. It is an error to call
  142. * this function on a stale sequence number. */
  143. static void dtls1_bitmap_record(DTLS1_BITMAP *bitmap,
  144. const uint8_t seq_num[8]) {
  145. const unsigned kWindowSize = sizeof(bitmap->map) * 8;
  146. uint64_t seq_num_u = to_u64_be(seq_num);
  147. /* Shift the window if necessary. */
  148. if (seq_num_u > bitmap->max_seq_num) {
  149. uint64_t shift = seq_num_u - bitmap->max_seq_num;
  150. if (shift >= kWindowSize) {
  151. bitmap->map = 0;
  152. } else {
  153. bitmap->map <<= shift;
  154. }
  155. bitmap->max_seq_num = seq_num_u;
  156. }
  157. uint64_t idx = bitmap->max_seq_num - seq_num_u;
  158. if (idx < kWindowSize) {
  159. bitmap->map |= ((uint64_t)1) << idx;
  160. }
  161. }
  162. enum ssl_open_record_t dtls_open_record(
  163. SSL *ssl, uint8_t *out_type, uint8_t *out, size_t *out_len,
  164. size_t *out_consumed, uint8_t *out_alert, size_t max_out, const uint8_t *in,
  165. size_t in_len) {
  166. CBS cbs;
  167. CBS_init(&cbs, in, in_len);
  168. /* Decode the record. */
  169. uint8_t type;
  170. uint16_t version;
  171. uint8_t sequence[8];
  172. CBS body;
  173. if (!CBS_get_u8(&cbs, &type) ||
  174. !CBS_get_u16(&cbs, &version) ||
  175. !CBS_copy_bytes(&cbs, sequence, 8) ||
  176. !CBS_get_u16_length_prefixed(&cbs, &body) ||
  177. (ssl->s3->have_version && version != ssl->version) ||
  178. (version >> 8) != DTLS1_VERSION_MAJOR ||
  179. CBS_len(&body) > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
  180. /* The record header was incomplete or malformed. Drop the entire packet. */
  181. *out_consumed = in_len;
  182. return ssl_open_record_discard;
  183. }
  184. if (ssl->msg_callback != NULL) {
  185. ssl->msg_callback(0 /* read */, 0, SSL3_RT_HEADER, in,
  186. DTLS1_RT_HEADER_LENGTH, ssl, ssl->msg_callback_arg);
  187. }
  188. uint16_t epoch = (((uint16_t)sequence[0]) << 8) | sequence[1];
  189. if (epoch != ssl->d1->r_epoch ||
  190. dtls1_bitmap_should_discard(&ssl->d1->bitmap, sequence)) {
  191. /* Drop this record. It's from the wrong epoch or is a replay. Note that if
  192. * |epoch| is the next epoch, the record could be buffered for later. For
  193. * simplicity, drop it and expect retransmit to handle it later; DTLS must
  194. * handle packet loss anyway. */
  195. *out_consumed = in_len - CBS_len(&cbs);
  196. return ssl_open_record_discard;
  197. }
  198. /* Decrypt the body. */
  199. size_t plaintext_len;
  200. if (!SSL_AEAD_CTX_open(ssl->aead_read_ctx, out, &plaintext_len, max_out,
  201. type, version, sequence, CBS_data(&body),
  202. CBS_len(&body))) {
  203. /* Bad packets are silently dropped in DTLS. See section 4.2.1 of RFC 6347.
  204. * Clear the error queue of any errors decryption may have added. Drop the
  205. * entire packet as it must not have come from the peer.
  206. *
  207. * TODO(davidben): This doesn't distinguish malloc failures from encryption
  208. * failures. */
  209. ERR_clear_error();
  210. *out_consumed = in_len - CBS_len(&cbs);
  211. return ssl_open_record_discard;
  212. }
  213. /* Check the plaintext length. */
  214. if (plaintext_len > SSL3_RT_MAX_PLAIN_LENGTH) {
  215. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  216. *out_alert = SSL_AD_RECORD_OVERFLOW;
  217. return ssl_open_record_error;
  218. }
  219. dtls1_bitmap_record(&ssl->d1->bitmap, sequence);
  220. /* TODO(davidben): Limit the number of empty records as in TLS? This is only
  221. * useful if we also limit discarded packets. */
  222. *out_type = type;
  223. *out_len = plaintext_len;
  224. *out_consumed = in_len - CBS_len(&cbs);
  225. return ssl_open_record_success;
  226. }
  227. int dtls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
  228. uint8_t type, const uint8_t *in, size_t in_len,
  229. enum dtls1_use_epoch_t use_epoch) {
  230. /* Determine the parameters for the current epoch. */
  231. uint16_t epoch = ssl->d1->w_epoch;
  232. SSL_AEAD_CTX *aead = ssl->aead_write_ctx;
  233. uint8_t *seq = ssl->s3->write_sequence;
  234. if (use_epoch == dtls1_use_previous_epoch) {
  235. /* DTLS renegotiation is unsupported, so only epochs 0 (NULL cipher) and 1
  236. * (negotiated cipher) exist. */
  237. assert(ssl->d1->w_epoch == 1);
  238. epoch = ssl->d1->w_epoch - 1;
  239. aead = NULL;
  240. seq = ssl->d1->last_write_sequence;
  241. }
  242. if (max_out < DTLS1_RT_HEADER_LENGTH) {
  243. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  244. return 0;
  245. }
  246. /* Check the record header does not alias any part of the input.
  247. * |SSL_AEAD_CTX_seal| will internally enforce other aliasing requirements. */
  248. if (in < out + DTLS1_RT_HEADER_LENGTH && out < in + in_len) {
  249. OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
  250. return 0;
  251. }
  252. out[0] = type;
  253. uint16_t wire_version = ssl->s3->have_version ? ssl->version : DTLS1_VERSION;
  254. out[1] = wire_version >> 8;
  255. out[2] = wire_version & 0xff;
  256. out[3] = epoch >> 8;
  257. out[4] = epoch & 0xff;
  258. memcpy(&out[5], &seq[2], 6);
  259. size_t ciphertext_len;
  260. if (!SSL_AEAD_CTX_seal(aead, out + DTLS1_RT_HEADER_LENGTH, &ciphertext_len,
  261. max_out - DTLS1_RT_HEADER_LENGTH, type, wire_version,
  262. &out[3] /* seq */, in, in_len) ||
  263. !ssl3_record_sequence_update(&seq[2], 6)) {
  264. return 0;
  265. }
  266. if (ciphertext_len >= 1 << 16) {
  267. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  268. return 0;
  269. }
  270. out[11] = ciphertext_len >> 8;
  271. out[12] = ciphertext_len & 0xff;
  272. *out_len = DTLS1_RT_HEADER_LENGTH + ciphertext_len;
  273. if (ssl->msg_callback) {
  274. ssl->msg_callback(1 /* write */, 0, SSL3_RT_HEADER, out,
  275. DTLS1_RT_HEADER_LENGTH, ssl, ssl->msg_callback_arg);
  276. }
  277. return 1;
  278. }