25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

d1_pkt.c 19 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 yıl önce
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 yıl önce
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 yıl önce
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 yıl önce
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 yıl önce
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 yıl önce
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 yıl önce
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 yıl önce
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 yıl önce
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 yıl önce
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 yıl önce
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 yıl önce
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 yıl önce
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 yıl önce
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 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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 <stdio.h>
  114. #include <string.h>
  115. #include <openssl/buf.h>
  116. #include <openssl/mem.h>
  117. #include <openssl/evp.h>
  118. #include <openssl/err.h>
  119. #include <openssl/rand.h>
  120. #include "internal.h"
  121. static int do_dtls1_write(SSL *ssl, int type, const uint8_t *buf,
  122. unsigned int len, enum dtls1_use_epoch_t use_epoch);
  123. /* dtls1_get_record reads a new input record. On success, it places it in
  124. * |ssl->s3->rrec| and returns one. Otherwise it returns <= 0 on error or if
  125. * more data is needed. */
  126. static int dtls1_get_record(SSL *ssl) {
  127. again:
  128. /* Read a new packet if there is no unconsumed one. */
  129. if (ssl_read_buffer_len(ssl) == 0) {
  130. int ret = ssl_read_buffer_extend_to(ssl, 0 /* unused */);
  131. if (ret <= 0) {
  132. return ret;
  133. }
  134. }
  135. assert(ssl_read_buffer_len(ssl) > 0);
  136. /* Ensure the packet is large enough to decrypt in-place. */
  137. if (ssl_read_buffer_len(ssl) < ssl_record_prefix_len(ssl)) {
  138. ssl_read_buffer_clear(ssl);
  139. goto again;
  140. }
  141. uint8_t *out = ssl_read_buffer(ssl) + ssl_record_prefix_len(ssl);
  142. size_t max_out = ssl_read_buffer_len(ssl) - ssl_record_prefix_len(ssl);
  143. uint8_t type, alert;
  144. size_t len, consumed;
  145. switch (dtls_open_record(ssl, &type, out, &len, &consumed, &alert, max_out,
  146. ssl_read_buffer(ssl), ssl_read_buffer_len(ssl))) {
  147. case ssl_open_record_success:
  148. ssl_read_buffer_consume(ssl, consumed);
  149. if (len > 0xffff) {
  150. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  151. return -1;
  152. }
  153. SSL3_RECORD *rr = &ssl->s3->rrec;
  154. rr->type = type;
  155. rr->length = (uint16_t)len;
  156. rr->data = out;
  157. return 1;
  158. case ssl_open_record_discard:
  159. ssl_read_buffer_consume(ssl, consumed);
  160. goto again;
  161. case ssl_open_record_error:
  162. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  163. return -1;
  164. case ssl_open_record_partial:
  165. /* Impossible in DTLS. */
  166. break;
  167. }
  168. assert(0);
  169. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  170. return -1;
  171. }
  172. int dtls1_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek) {
  173. return dtls1_read_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len, peek);
  174. }
  175. int dtls1_read_change_cipher_spec(SSL *ssl) {
  176. uint8_t byte;
  177. int ret = dtls1_read_bytes(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, &byte,
  178. 1 /* len */, 0 /* no peek */);
  179. if (ret <= 0) {
  180. return ret;
  181. }
  182. assert(ret == 1);
  183. if (ssl->s3->rrec.length != 0 || byte != SSL3_MT_CCS) {
  184. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
  185. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  186. return -1;
  187. }
  188. if (ssl->msg_callback != NULL) {
  189. ssl->msg_callback(0, ssl->version, SSL3_RT_CHANGE_CIPHER_SPEC, &byte, 1,
  190. ssl, ssl->msg_callback_arg);
  191. }
  192. return 1;
  193. }
  194. void dtls1_read_close_notify(SSL *ssl) {
  195. /* Bidirectional shutdown doesn't make sense for an unordered transport. DTLS
  196. * alerts also aren't delivered reliably, so we may even time out because the
  197. * peer never received our close_notify. Report to the caller that the channel
  198. * has fully shut down. */
  199. ssl->shutdown |= SSL_RECEIVED_SHUTDOWN;
  200. }
  201. /* Return up to 'len' payload bytes received in 'type' records.
  202. * 'type' is one of the following:
  203. *
  204. * - SSL3_RT_HANDSHAKE (when dtls1_get_message calls us)
  205. * - SSL3_RT_CHANGE_CIPHER_SPEC (when dtls1_read_change_cipher_spec calls us)
  206. * - SSL3_RT_APPLICATION_DATA (when dtls1_read_app_data calls us)
  207. *
  208. * If we don't have stored data to work from, read a DTLS record first (possibly
  209. * multiple records if we still don't have anything to return).
  210. *
  211. * This function must handle any surprises the peer may have for us, such as
  212. * Alert records (e.g. close_notify) and out of records. */
  213. int dtls1_read_bytes(SSL *ssl, int type, unsigned char *buf, int len, int peek) {
  214. int al, i, ret;
  215. unsigned int n;
  216. SSL3_RECORD *rr;
  217. void (*cb)(const SSL *ssl, int type, int value) = NULL;
  218. if ((type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE &&
  219. type != SSL3_RT_CHANGE_CIPHER_SPEC) ||
  220. (peek && type != SSL3_RT_APPLICATION_DATA)) {
  221. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  222. return -1;
  223. }
  224. if (!ssl->in_handshake && SSL_in_init(ssl)) {
  225. /* type == SSL3_RT_APPLICATION_DATA */
  226. i = ssl->handshake_func(ssl);
  227. if (i < 0) {
  228. return i;
  229. }
  230. if (i == 0) {
  231. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  232. return -1;
  233. }
  234. }
  235. start:
  236. ssl->rwstate = SSL_NOTHING;
  237. /* ssl->s3->rrec.type - is the type of record
  238. * ssl->s3->rrec.data - data
  239. * ssl->s3->rrec.off - offset into 'data' for next read
  240. * ssl->s3->rrec.length - number of bytes. */
  241. rr = &ssl->s3->rrec;
  242. /* Check for timeout */
  243. if (DTLSv1_handle_timeout(ssl) > 0) {
  244. goto start;
  245. }
  246. /* get new packet if necessary */
  247. if (rr->length == 0) {
  248. ret = dtls1_get_record(ssl);
  249. if (ret <= 0) {
  250. ret = dtls1_read_failed(ssl, ret);
  251. /* anything other than a timeout is an error */
  252. if (ret <= 0) {
  253. return ret;
  254. } else {
  255. goto start;
  256. }
  257. }
  258. }
  259. /* we now have a packet which can be read and processed */
  260. /* If the other end has shut down, throw anything we read away (even in
  261. * 'peek' mode) */
  262. if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
  263. rr->length = 0;
  264. ssl->rwstate = SSL_NOTHING;
  265. return 0;
  266. }
  267. if (type == rr->type) {
  268. /* Make sure that we are not getting application data when we
  269. * are doing a handshake for the first time. */
  270. if (SSL_in_init(ssl) && (type == SSL3_RT_APPLICATION_DATA) &&
  271. (ssl->s3->aead_read_ctx == NULL)) {
  272. /* TODO(davidben): Is this check redundant with the handshake_func
  273. * check? */
  274. al = SSL_AD_UNEXPECTED_MESSAGE;
  275. OPENSSL_PUT_ERROR(SSL, SSL_R_APP_DATA_IN_HANDSHAKE);
  276. goto f_err;
  277. }
  278. /* Discard empty records. */
  279. if (rr->length == 0) {
  280. goto start;
  281. }
  282. if (len <= 0) {
  283. return len;
  284. }
  285. if ((unsigned int)len > rr->length) {
  286. n = rr->length;
  287. } else {
  288. n = (unsigned int)len;
  289. }
  290. memcpy(buf, rr->data, n);
  291. if (!peek) {
  292. rr->length -= n;
  293. rr->data += n;
  294. if (rr->length == 0) {
  295. /* The record has been consumed, so we may now clear the buffer. */
  296. ssl_read_buffer_discard(ssl);
  297. }
  298. }
  299. return n;
  300. }
  301. /* If we get here, then type != rr->type. */
  302. /* If an alert record, process one alert out of the record. Note that we allow
  303. * a single record to contain multiple alerts. */
  304. if (rr->type == SSL3_RT_ALERT) {
  305. /* Alerts may not be fragmented. */
  306. if (rr->length < 2) {
  307. al = SSL_AD_DECODE_ERROR;
  308. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
  309. goto f_err;
  310. }
  311. if (ssl->msg_callback) {
  312. ssl->msg_callback(0, ssl->version, SSL3_RT_ALERT, rr->data, 2, ssl,
  313. ssl->msg_callback_arg);
  314. }
  315. const uint8_t alert_level = rr->data[0];
  316. const uint8_t alert_descr = rr->data[1];
  317. rr->length -= 2;
  318. rr->data += 2;
  319. if (ssl->info_callback != NULL) {
  320. cb = ssl->info_callback;
  321. } else if (ssl->ctx->info_callback != NULL) {
  322. cb = ssl->ctx->info_callback;
  323. }
  324. if (cb != NULL) {
  325. uint16_t alert = (alert_level << 8) | alert_descr;
  326. cb(ssl, SSL_CB_READ_ALERT, alert);
  327. }
  328. if (alert_level == SSL3_AL_WARNING) {
  329. ssl->s3->warn_alert = alert_descr;
  330. if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
  331. ssl->shutdown |= SSL_RECEIVED_SHUTDOWN;
  332. return 0;
  333. }
  334. } else if (alert_level == SSL3_AL_FATAL) {
  335. char tmp[16];
  336. ssl->rwstate = SSL_NOTHING;
  337. ssl->s3->fatal_alert = alert_descr;
  338. OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr);
  339. BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
  340. ERR_add_error_data(2, "SSL alert number ", tmp);
  341. ssl->shutdown |= SSL_RECEIVED_SHUTDOWN;
  342. SSL_CTX_remove_session(ssl->ctx, ssl->session);
  343. return 0;
  344. } else {
  345. al = SSL_AD_ILLEGAL_PARAMETER;
  346. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE);
  347. goto f_err;
  348. }
  349. goto start;
  350. }
  351. /* Cross-epoch records are discarded, but we may receive out-of-order
  352. * application data between ChangeCipherSpec and Finished or a ChangeCipherSpec
  353. * before the appropriate point in the handshake. Those must be silently
  354. * discarded.
  355. *
  356. * However, only allow the out-of-order records in the correct epoch.
  357. * Application data must come in the encrypted epoch, and ChangeCipherSpec in
  358. * the unencrypted epoch (we never renegotiate). Other cases fall through and
  359. * fail with a fatal error. */
  360. if ((rr->type == SSL3_RT_APPLICATION_DATA &&
  361. ssl->s3->aead_read_ctx != NULL) ||
  362. (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC &&
  363. ssl->s3->aead_read_ctx == NULL)) {
  364. rr->length = 0;
  365. goto start;
  366. }
  367. if (rr->type == SSL3_RT_HANDSHAKE) {
  368. if (type != SSL3_RT_APPLICATION_DATA) {
  369. /* Out-of-order handshake record while looking for ChangeCipherSpec. Drop
  370. * it silently. */
  371. assert(type == SSL3_RT_CHANGE_CIPHER_SPEC);
  372. rr->length = 0;
  373. goto start;
  374. }
  375. /* Parse the first fragment header to determine if this is a pre-CCS or
  376. * post-CCS handshake record. DTLS resets handshake message numbers on each
  377. * handshake, so renegotiations and retransmissions are ambiguous. */
  378. if (rr->length < DTLS1_HM_HEADER_LENGTH) {
  379. al = SSL_AD_DECODE_ERROR;
  380. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD);
  381. goto f_err;
  382. }
  383. struct hm_header_st msg_hdr;
  384. dtls1_get_message_header(rr->data, &msg_hdr);
  385. if (msg_hdr.type == SSL3_MT_FINISHED) {
  386. if (msg_hdr.frag_off == 0) {
  387. /* Retransmit our last flight of messages. If the peer sends the second
  388. * Finished, they may not have received ours. Only do this for the
  389. * first fragment, in case the Finished was fragmented. */
  390. if (dtls1_check_timeout_num(ssl) < 0) {
  391. return -1;
  392. }
  393. dtls1_retransmit_buffered_messages(ssl);
  394. }
  395. rr->length = 0;
  396. goto start;
  397. }
  398. /* Otherwise, this is a pre-CCS handshake message from an unsupported
  399. * renegotiation attempt. Fall through to the error path. */
  400. }
  401. al = SSL_AD_UNEXPECTED_MESSAGE;
  402. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
  403. f_err:
  404. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  405. return -1;
  406. }
  407. int dtls1_write_app_data(SSL *ssl, const void *buf_, int len) {
  408. int i;
  409. if (SSL_in_init(ssl) && !ssl->in_handshake) {
  410. i = ssl->handshake_func(ssl);
  411. if (i < 0) {
  412. return i;
  413. }
  414. if (i == 0) {
  415. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  416. return -1;
  417. }
  418. }
  419. if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
  420. OPENSSL_PUT_ERROR(SSL, SSL_R_DTLS_MESSAGE_TOO_BIG);
  421. return -1;
  422. }
  423. i = dtls1_write_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf_, len,
  424. dtls1_use_current_epoch);
  425. return i;
  426. }
  427. /* Call this to write data in records of type 'type' It will return <= 0 if not
  428. * all data has been sent or non-blocking IO. */
  429. int dtls1_write_bytes(SSL *ssl, int type, const void *buf, int len,
  430. enum dtls1_use_epoch_t use_epoch) {
  431. int i;
  432. assert(len <= SSL3_RT_MAX_PLAIN_LENGTH);
  433. ssl->rwstate = SSL_NOTHING;
  434. i = do_dtls1_write(ssl, type, buf, len, use_epoch);
  435. return i;
  436. }
  437. static int do_dtls1_write(SSL *ssl, int type, const uint8_t *buf,
  438. unsigned int len, enum dtls1_use_epoch_t use_epoch) {
  439. /* There should never be a pending write buffer in DTLS. One can't write half
  440. * a datagram, so the write buffer is always dropped in
  441. * |ssl_write_buffer_flush|. */
  442. assert(!ssl_write_buffer_is_pending(ssl));
  443. /* If we have an alert to send, lets send it */
  444. if (ssl->s3->alert_dispatch) {
  445. int ret = ssl->method->ssl_dispatch_alert(ssl);
  446. if (ret <= 0) {
  447. return ret;
  448. }
  449. /* if it went, fall through and send more stuff */
  450. }
  451. if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
  452. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  453. return -1;
  454. }
  455. if (len == 0) {
  456. return 0;
  457. }
  458. size_t max_out = len + ssl_max_seal_overhead(ssl);
  459. uint8_t *out;
  460. size_t ciphertext_len;
  461. if (!ssl_write_buffer_init(ssl, &out, max_out) ||
  462. !dtls_seal_record(ssl, out, &ciphertext_len, max_out, type, buf, len,
  463. use_epoch)) {
  464. ssl_write_buffer_clear(ssl);
  465. return -1;
  466. }
  467. ssl_write_buffer_set_len(ssl, ciphertext_len);
  468. int ret = ssl_write_buffer_flush(ssl);
  469. if (ret <= 0) {
  470. return ret;
  471. }
  472. return (int)len;
  473. }
  474. int dtls1_dispatch_alert(SSL *ssl) {
  475. int i, j;
  476. void (*cb)(const SSL *ssl, int type, int value) = NULL;
  477. uint8_t buf[DTLS1_AL_HEADER_LENGTH];
  478. uint8_t *ptr = &buf[0];
  479. ssl->s3->alert_dispatch = 0;
  480. memset(buf, 0x00, sizeof(buf));
  481. *ptr++ = ssl->s3->send_alert[0];
  482. *ptr++ = ssl->s3->send_alert[1];
  483. i = do_dtls1_write(ssl, SSL3_RT_ALERT, &buf[0], sizeof(buf),
  484. dtls1_use_current_epoch);
  485. if (i <= 0) {
  486. ssl->s3->alert_dispatch = 1;
  487. } else {
  488. if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) {
  489. (void)BIO_flush(ssl->wbio);
  490. }
  491. if (ssl->msg_callback) {
  492. ssl->msg_callback(1, ssl->version, SSL3_RT_ALERT, ssl->s3->send_alert, 2,
  493. ssl, ssl->msg_callback_arg);
  494. }
  495. if (ssl->info_callback != NULL) {
  496. cb = ssl->info_callback;
  497. } else if (ssl->ctx->info_callback != NULL) {
  498. cb = ssl->ctx->info_callback;
  499. }
  500. if (cb != NULL) {
  501. j = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1];
  502. cb(ssl, SSL_CB_WRITE_ALERT, j);
  503. }
  504. }
  505. return i;
  506. }