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.

s3_pkt.c 21 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 years ago
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 years ago
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 years ago
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 years ago
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 years ago
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 years ago
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 years ago
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 years ago
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 years ago
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 years ago
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 years ago
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 years ago
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 years ago
Remove renegotiation deferral logic. When the peer or caller requests a renegotiation, OpenSSL doesn't renegotiate immediately. It sets a flag to begin a renegotiation as soon as record-layer read and write buffers are clear. One reason is that OpenSSL's record layer cannot write a handshake record while an application data record is being written. The buffer consistency checks around partial writes will break. None of these cases are relevant for the client auth hack. We already require that renego come in at a quiescent part of the application protocol by forbidding handshake/app_data interleave. The new behavior is now: when a HelloRequest comes in, if the record layer is not idle, the renegotiation is rejected as if SSL_set_reject_peer_renegotiations were set. Otherwise we immediately begin the new handshake. The server may not send any application data between HelloRequest and completing the handshake. The HelloRequest may not be consumed if an SSL_write is pending. Note this does require that Chromium's HTTP stack not attempt to read the HTTP response until the request has been written, but the renegotiation logic already assumes it. Were Chromium to drive the SSL_read state machine early and the server, say, sent a HelloRequest after reading the request headers but before we've sent the whole POST body, the SSL state machine may racily enter renegotiate early, block writing the POST body on the new handshake, which would break Chromium's ERR_SSL_CLIENT_AUTH_CERT_NEEDED plumbing. BUG=429450 Change-Id: I6278240c3bceb5d2e1a2195bdb62dd9e0f4df718 Reviewed-on: https://boringssl-review.googlesource.com/4825 Reviewed-by: Adam Langley <agl@google.com>
9 years ago
Remove renegotiation deferral logic. When the peer or caller requests a renegotiation, OpenSSL doesn't renegotiate immediately. It sets a flag to begin a renegotiation as soon as record-layer read and write buffers are clear. One reason is that OpenSSL's record layer cannot write a handshake record while an application data record is being written. The buffer consistency checks around partial writes will break. None of these cases are relevant for the client auth hack. We already require that renego come in at a quiescent part of the application protocol by forbidding handshake/app_data interleave. The new behavior is now: when a HelloRequest comes in, if the record layer is not idle, the renegotiation is rejected as if SSL_set_reject_peer_renegotiations were set. Otherwise we immediately begin the new handshake. The server may not send any application data between HelloRequest and completing the handshake. The HelloRequest may not be consumed if an SSL_write is pending. Note this does require that Chromium's HTTP stack not attempt to read the HTTP response until the request has been written, but the renegotiation logic already assumes it. Were Chromium to drive the SSL_read state machine early and the server, say, sent a HelloRequest after reading the request headers but before we've sent the whole POST body, the SSL state machine may racily enter renegotiate early, block writing the POST body on the new handshake, which would break Chromium's ERR_SSL_CLIENT_AUTH_CERT_NEEDED plumbing. BUG=429450 Change-Id: I6278240c3bceb5d2e1a2195bdb62dd9e0f4df718 Reviewed-on: https://boringssl-review.googlesource.com/4825 Reviewed-by: Adam Langley <agl@google.com>
9 years ago
Remove renegotiation deferral logic. When the peer or caller requests a renegotiation, OpenSSL doesn't renegotiate immediately. It sets a flag to begin a renegotiation as soon as record-layer read and write buffers are clear. One reason is that OpenSSL's record layer cannot write a handshake record while an application data record is being written. The buffer consistency checks around partial writes will break. None of these cases are relevant for the client auth hack. We already require that renego come in at a quiescent part of the application protocol by forbidding handshake/app_data interleave. The new behavior is now: when a HelloRequest comes in, if the record layer is not idle, the renegotiation is rejected as if SSL_set_reject_peer_renegotiations were set. Otherwise we immediately begin the new handshake. The server may not send any application data between HelloRequest and completing the handshake. The HelloRequest may not be consumed if an SSL_write is pending. Note this does require that Chromium's HTTP stack not attempt to read the HTTP response until the request has been written, but the renegotiation logic already assumes it. Were Chromium to drive the SSL_read state machine early and the server, say, sent a HelloRequest after reading the request headers but before we've sent the whole POST body, the SSL state machine may racily enter renegotiate early, block writing the POST body on the new handshake, which would break Chromium's ERR_SSL_CLIENT_AUTH_CERT_NEEDED plumbing. BUG=429450 Change-Id: I6278240c3bceb5d2e1a2195bdb62dd9e0f4df718 Reviewed-on: https://boringssl-review.googlesource.com/4825 Reviewed-by: Adam Langley <agl@google.com>
9 years ago
Remove renegotiation deferral logic. When the peer or caller requests a renegotiation, OpenSSL doesn't renegotiate immediately. It sets a flag to begin a renegotiation as soon as record-layer read and write buffers are clear. One reason is that OpenSSL's record layer cannot write a handshake record while an application data record is being written. The buffer consistency checks around partial writes will break. None of these cases are relevant for the client auth hack. We already require that renego come in at a quiescent part of the application protocol by forbidding handshake/app_data interleave. The new behavior is now: when a HelloRequest comes in, if the record layer is not idle, the renegotiation is rejected as if SSL_set_reject_peer_renegotiations were set. Otherwise we immediately begin the new handshake. The server may not send any application data between HelloRequest and completing the handshake. The HelloRequest may not be consumed if an SSL_write is pending. Note this does require that Chromium's HTTP stack not attempt to read the HTTP response until the request has been written, but the renegotiation logic already assumes it. Were Chromium to drive the SSL_read state machine early and the server, say, sent a HelloRequest after reading the request headers but before we've sent the whole POST body, the SSL state machine may racily enter renegotiate early, block writing the POST body on the new handshake, which would break Chromium's ERR_SSL_CLIENT_AUTH_CERT_NEEDED plumbing. BUG=429450 Change-Id: I6278240c3bceb5d2e1a2195bdb62dd9e0f4df718 Reviewed-on: https://boringssl-review.googlesource.com/4825 Reviewed-by: Adam Langley <agl@google.com>
9 years ago
Remove renegotiation deferral logic. When the peer or caller requests a renegotiation, OpenSSL doesn't renegotiate immediately. It sets a flag to begin a renegotiation as soon as record-layer read and write buffers are clear. One reason is that OpenSSL's record layer cannot write a handshake record while an application data record is being written. The buffer consistency checks around partial writes will break. None of these cases are relevant for the client auth hack. We already require that renego come in at a quiescent part of the application protocol by forbidding handshake/app_data interleave. The new behavior is now: when a HelloRequest comes in, if the record layer is not idle, the renegotiation is rejected as if SSL_set_reject_peer_renegotiations were set. Otherwise we immediately begin the new handshake. The server may not send any application data between HelloRequest and completing the handshake. The HelloRequest may not be consumed if an SSL_write is pending. Note this does require that Chromium's HTTP stack not attempt to read the HTTP response until the request has been written, but the renegotiation logic already assumes it. Were Chromium to drive the SSL_read state machine early and the server, say, sent a HelloRequest after reading the request headers but before we've sent the whole POST body, the SSL state machine may racily enter renegotiate early, block writing the POST body on the new handshake, which would break Chromium's ERR_SSL_CLIENT_AUTH_CERT_NEEDED plumbing. BUG=429450 Change-Id: I6278240c3bceb5d2e1a2195bdb62dd9e0f4df718 Reviewed-on: https://boringssl-review.googlesource.com/4825 Reviewed-by: Adam Langley <agl@google.com>
9 years ago
Remove renegotiation deferral logic. When the peer or caller requests a renegotiation, OpenSSL doesn't renegotiate immediately. It sets a flag to begin a renegotiation as soon as record-layer read and write buffers are clear. One reason is that OpenSSL's record layer cannot write a handshake record while an application data record is being written. The buffer consistency checks around partial writes will break. None of these cases are relevant for the client auth hack. We already require that renego come in at a quiescent part of the application protocol by forbidding handshake/app_data interleave. The new behavior is now: when a HelloRequest comes in, if the record layer is not idle, the renegotiation is rejected as if SSL_set_reject_peer_renegotiations were set. Otherwise we immediately begin the new handshake. The server may not send any application data between HelloRequest and completing the handshake. The HelloRequest may not be consumed if an SSL_write is pending. Note this does require that Chromium's HTTP stack not attempt to read the HTTP response until the request has been written, but the renegotiation logic already assumes it. Were Chromium to drive the SSL_read state machine early and the server, say, sent a HelloRequest after reading the request headers but before we've sent the whole POST body, the SSL state machine may racily enter renegotiate early, block writing the POST body on the new handshake, which would break Chromium's ERR_SSL_CLIENT_AUTH_CERT_NEEDED plumbing. BUG=429450 Change-Id: I6278240c3bceb5d2e1a2195bdb62dd9e0f4df718 Reviewed-on: https://boringssl-review.googlesource.com/4825 Reviewed-by: Adam Langley <agl@google.com>
9 years ago
Remove renegotiation deferral logic. When the peer or caller requests a renegotiation, OpenSSL doesn't renegotiate immediately. It sets a flag to begin a renegotiation as soon as record-layer read and write buffers are clear. One reason is that OpenSSL's record layer cannot write a handshake record while an application data record is being written. The buffer consistency checks around partial writes will break. None of these cases are relevant for the client auth hack. We already require that renego come in at a quiescent part of the application protocol by forbidding handshake/app_data interleave. The new behavior is now: when a HelloRequest comes in, if the record layer is not idle, the renegotiation is rejected as if SSL_set_reject_peer_renegotiations were set. Otherwise we immediately begin the new handshake. The server may not send any application data between HelloRequest and completing the handshake. The HelloRequest may not be consumed if an SSL_write is pending. Note this does require that Chromium's HTTP stack not attempt to read the HTTP response until the request has been written, but the renegotiation logic already assumes it. Were Chromium to drive the SSL_read state machine early and the server, say, sent a HelloRequest after reading the request headers but before we've sent the whole POST body, the SSL state machine may racily enter renegotiate early, block writing the POST body on the new handshake, which would break Chromium's ERR_SSL_CLIENT_AUTH_CERT_NEEDED plumbing. BUG=429450 Change-Id: I6278240c3bceb5d2e1a2195bdb62dd9e0f4df718 Reviewed-on: https://boringssl-review.googlesource.com/4825 Reviewed-by: Adam Langley <agl@google.com>
9 years ago
Remove renegotiation deferral logic. When the peer or caller requests a renegotiation, OpenSSL doesn't renegotiate immediately. It sets a flag to begin a renegotiation as soon as record-layer read and write buffers are clear. One reason is that OpenSSL's record layer cannot write a handshake record while an application data record is being written. The buffer consistency checks around partial writes will break. None of these cases are relevant for the client auth hack. We already require that renego come in at a quiescent part of the application protocol by forbidding handshake/app_data interleave. The new behavior is now: when a HelloRequest comes in, if the record layer is not idle, the renegotiation is rejected as if SSL_set_reject_peer_renegotiations were set. Otherwise we immediately begin the new handshake. The server may not send any application data between HelloRequest and completing the handshake. The HelloRequest may not be consumed if an SSL_write is pending. Note this does require that Chromium's HTTP stack not attempt to read the HTTP response until the request has been written, but the renegotiation logic already assumes it. Were Chromium to drive the SSL_read state machine early and the server, say, sent a HelloRequest after reading the request headers but before we've sent the whole POST body, the SSL state machine may racily enter renegotiate early, block writing the POST body on the new handshake, which would break Chromium's ERR_SSL_CLIENT_AUTH_CERT_NEEDED plumbing. BUG=429450 Change-Id: I6278240c3bceb5d2e1a2195bdb62dd9e0f4df718 Reviewed-on: https://boringssl-review.googlesource.com/4825 Reviewed-by: Adam Langley <agl@google.com>
9 years ago
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 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com). */
  108. #include <openssl/ssl.h>
  109. #include <assert.h>
  110. #include <limits.h>
  111. #include <string.h>
  112. #include <openssl/buf.h>
  113. #include <openssl/err.h>
  114. #include <openssl/evp.h>
  115. #include <openssl/mem.h>
  116. #include <openssl/rand.h>
  117. #include "internal.h"
  118. static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len);
  119. /* kMaxWarningAlerts is the number of consecutive warning alerts that will be
  120. * processed. */
  121. static const uint8_t kMaxWarningAlerts = 4;
  122. /* ssl3_get_record reads a new input record. On success, it places it in
  123. * |ssl->s3->rrec| and returns one. Otherwise it returns <= 0 on error or if
  124. * more data is needed. */
  125. static int ssl3_get_record(SSL *ssl) {
  126. int ret;
  127. again:
  128. /* Ensure the buffer is large enough to decrypt in-place. */
  129. ret = ssl_read_buffer_extend_to(ssl, ssl_record_prefix_len(ssl));
  130. if (ret <= 0) {
  131. return ret;
  132. }
  133. assert(ssl_read_buffer_len(ssl) >= ssl_record_prefix_len(ssl));
  134. uint8_t *out = ssl_read_buffer(ssl) + ssl_record_prefix_len(ssl);
  135. size_t max_out = ssl_read_buffer_len(ssl) - ssl_record_prefix_len(ssl);
  136. uint8_t type, alert;
  137. size_t len, consumed;
  138. switch (tls_open_record(ssl, &type, out, &len, &consumed, &alert, max_out,
  139. ssl_read_buffer(ssl), ssl_read_buffer_len(ssl))) {
  140. case ssl_open_record_success:
  141. ssl_read_buffer_consume(ssl, consumed);
  142. if (len > 0xffff) {
  143. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  144. return -1;
  145. }
  146. SSL3_RECORD *rr = &ssl->s3->rrec;
  147. rr->type = type;
  148. rr->length = (uint16_t)len;
  149. rr->data = out;
  150. return 1;
  151. case ssl_open_record_partial:
  152. ret = ssl_read_buffer_extend_to(ssl, consumed);
  153. if (ret <= 0) {
  154. return ret;
  155. }
  156. goto again;
  157. case ssl_open_record_discard:
  158. ssl_read_buffer_consume(ssl, consumed);
  159. goto again;
  160. case ssl_open_record_error:
  161. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  162. return -1;
  163. }
  164. assert(0);
  165. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  166. return -1;
  167. }
  168. int ssl3_write_app_data(SSL *ssl, const void *buf, int len) {
  169. assert(!SSL_in_init(ssl) || SSL_in_false_start(ssl));
  170. return ssl3_write_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len);
  171. }
  172. /* Call this to write data in records of type |type|. It will return <= 0 if
  173. * not all data has been sent or non-blocking IO. */
  174. int ssl3_write_bytes(SSL *ssl, int type, const void *buf_, int len) {
  175. const uint8_t *buf = buf_;
  176. unsigned tot, n, nw;
  177. assert(ssl->s3->wnum <= INT_MAX);
  178. tot = ssl->s3->wnum;
  179. ssl->s3->wnum = 0;
  180. /* Ensure that if we end up with a smaller value of data to write out than
  181. * the the original len from a write which didn't complete for non-blocking
  182. * I/O and also somehow ended up avoiding the check for this in
  183. * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to
  184. * end up with (len-tot) as a large number that will then promptly send
  185. * beyond the end of the users buffer ... so we trap and report the error in
  186. * a way the user will notice. */
  187. if (len < 0 || (size_t)len < tot) {
  188. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH);
  189. return -1;
  190. }
  191. n = (len - tot);
  192. for (;;) {
  193. /* max contains the maximum number of bytes that we can put into a
  194. * record. */
  195. unsigned max = ssl->max_send_fragment;
  196. if (n > max) {
  197. nw = max;
  198. } else {
  199. nw = n;
  200. }
  201. int ret = do_ssl3_write(ssl, type, &buf[tot], nw);
  202. if (ret <= 0) {
  203. ssl->s3->wnum = tot;
  204. return ret;
  205. }
  206. if (ret == (int)n || (type == SSL3_RT_APPLICATION_DATA &&
  207. (ssl->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
  208. return tot + ret;
  209. }
  210. n -= ret;
  211. tot += ret;
  212. }
  213. }
  214. static int ssl3_write_pending(SSL *ssl, int type, const uint8_t *buf,
  215. unsigned int len) {
  216. if (ssl->s3->wpend_tot > (int)len ||
  217. (ssl->s3->wpend_buf != buf &&
  218. !(ssl->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) ||
  219. ssl->s3->wpend_type != type) {
  220. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_WRITE_RETRY);
  221. return -1;
  222. }
  223. int ret = ssl_write_buffer_flush(ssl);
  224. if (ret <= 0) {
  225. return ret;
  226. }
  227. return ssl->s3->wpend_ret;
  228. }
  229. /* do_ssl3_write writes an SSL record of the given type. */
  230. static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len) {
  231. /* If there is still data from the previous record, flush it. */
  232. if (ssl_write_buffer_is_pending(ssl)) {
  233. return ssl3_write_pending(ssl, type, buf, len);
  234. }
  235. /* If we have an alert to send, lets send it */
  236. if (ssl->s3->alert_dispatch) {
  237. int ret = ssl->method->ssl_dispatch_alert(ssl);
  238. if (ret <= 0) {
  239. return ret;
  240. }
  241. /* if it went, fall through and send more stuff */
  242. }
  243. if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
  244. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  245. return -1;
  246. }
  247. if (len == 0) {
  248. return 0;
  249. }
  250. size_t max_out = len + ssl_max_seal_overhead(ssl);
  251. if (max_out < len) {
  252. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  253. return -1;
  254. }
  255. uint8_t *out;
  256. size_t ciphertext_len;
  257. if (!ssl_write_buffer_init(ssl, &out, max_out) ||
  258. !tls_seal_record(ssl, out, &ciphertext_len, max_out, type, buf, len)) {
  259. return -1;
  260. }
  261. ssl_write_buffer_set_len(ssl, ciphertext_len);
  262. /* memorize arguments so that ssl3_write_pending can detect bad write retries
  263. * later */
  264. ssl->s3->wpend_tot = len;
  265. ssl->s3->wpend_buf = buf;
  266. ssl->s3->wpend_type = type;
  267. ssl->s3->wpend_ret = len;
  268. /* we now just need to write the buffer */
  269. return ssl3_write_pending(ssl, type, buf, len);
  270. }
  271. int ssl3_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek) {
  272. assert(!SSL_in_init(ssl));
  273. return ssl3_read_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len, peek);
  274. }
  275. int ssl3_read_change_cipher_spec(SSL *ssl) {
  276. uint8_t byte;
  277. int ret = ssl3_read_bytes(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, &byte, 1 /* len */,
  278. 0 /* no peek */);
  279. if (ret <= 0) {
  280. return ret;
  281. }
  282. assert(ret == 1);
  283. if (ssl->s3->rrec.length != 0 || byte != SSL3_MT_CCS) {
  284. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
  285. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  286. return -1;
  287. }
  288. if (ssl->msg_callback != NULL) {
  289. ssl->msg_callback(0, ssl->version, SSL3_RT_CHANGE_CIPHER_SPEC, &byte, 1,
  290. ssl, ssl->msg_callback_arg);
  291. }
  292. return 1;
  293. }
  294. void ssl3_read_close_notify(SSL *ssl) {
  295. ssl3_read_bytes(ssl, 0, NULL, 0, 0);
  296. }
  297. static int ssl3_can_renegotiate(SSL *ssl) {
  298. switch (ssl->renegotiate_mode) {
  299. case ssl_renegotiate_never:
  300. return 0;
  301. case ssl_renegotiate_once:
  302. return ssl->s3->total_renegotiations == 0;
  303. case ssl_renegotiate_freely:
  304. return 1;
  305. case ssl_renegotiate_ignore:
  306. return 1;
  307. }
  308. assert(0);
  309. return 0;
  310. }
  311. /* Return up to 'len' payload bytes received in 'type' records.
  312. * 'type' is one of the following:
  313. *
  314. * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
  315. * - SSL3_RT_CHANGE_CIPHER_SPEC (when ssl3_read_change_cipher_spec calls us)
  316. * - SSL3_RT_APPLICATION_DATA (when ssl3_read_app_data calls us)
  317. * - 0 (during a shutdown, no data has to be returned)
  318. *
  319. * If we don't have stored data to work from, read a SSL/TLS record first
  320. * (possibly multiple records if we still don't have anything to return).
  321. *
  322. * This function must handle any surprises the peer may have for us, such as
  323. * Alert records (e.g. close_notify) or renegotiation requests. */
  324. int ssl3_read_bytes(SSL *ssl, int type, uint8_t *buf, int len, int peek) {
  325. int al, i, ret;
  326. unsigned int n;
  327. SSL3_RECORD *rr;
  328. void (*cb)(const SSL *ssl, int type, int value) = NULL;
  329. if ((type && type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE &&
  330. type != SSL3_RT_CHANGE_CIPHER_SPEC) ||
  331. (peek && type != SSL3_RT_APPLICATION_DATA)) {
  332. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  333. return -1;
  334. }
  335. start:
  336. /* ssl->s3->rrec.type - is the type of record
  337. * ssl->s3->rrec.data - data
  338. * ssl->s3->rrec.off - offset into 'data' for next read
  339. * ssl->s3->rrec.length - number of bytes. */
  340. rr = &ssl->s3->rrec;
  341. /* get new packet if necessary */
  342. if (rr->length == 0) {
  343. ret = ssl3_get_record(ssl);
  344. if (ret <= 0) {
  345. return ret;
  346. }
  347. }
  348. /* we now have a packet which can be read and processed */
  349. /* If the other end has shut down, throw anything we read away (even in
  350. * 'peek' mode) */
  351. if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
  352. rr->length = 0;
  353. return 0;
  354. }
  355. if (type != 0 && type == rr->type) {
  356. ssl->s3->warning_alert_count = 0;
  357. /* Make sure that we are not getting application data when we are doing a
  358. * handshake for the first time. */
  359. if (SSL_in_init(ssl) && type == SSL3_RT_APPLICATION_DATA &&
  360. ssl->s3->aead_read_ctx == NULL) {
  361. /* TODO(davidben): Is this check redundant with the handshake_func
  362. * check? */
  363. al = SSL_AD_UNEXPECTED_MESSAGE;
  364. OPENSSL_PUT_ERROR(SSL, SSL_R_APP_DATA_IN_HANDSHAKE);
  365. goto f_err;
  366. }
  367. /* Discard empty records. */
  368. if (rr->length == 0) {
  369. goto start;
  370. }
  371. if (len <= 0) {
  372. return len;
  373. }
  374. if ((unsigned int)len > rr->length) {
  375. n = rr->length;
  376. } else {
  377. n = (unsigned int)len;
  378. }
  379. memcpy(buf, rr->data, n);
  380. if (!peek) {
  381. rr->length -= n;
  382. rr->data += n;
  383. if (rr->length == 0) {
  384. /* The record has been consumed, so we may now clear the buffer. */
  385. ssl_read_buffer_discard(ssl);
  386. }
  387. }
  388. return n;
  389. }
  390. /* Process unexpected records. */
  391. if (type == SSL3_RT_APPLICATION_DATA && rr->type == SSL3_RT_HANDSHAKE) {
  392. /* If peer renegotiations are disabled, all out-of-order handshake records
  393. * are fatal. Renegotiations as a server are never supported. */
  394. if (ssl->server || !ssl3_can_renegotiate(ssl)) {
  395. al = SSL_AD_NO_RENEGOTIATION;
  396. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
  397. goto f_err;
  398. }
  399. /* This must be a HelloRequest, possibly fragmented over multiple records.
  400. * Consume data from the handshake protocol until it is complete. */
  401. static const uint8_t kHelloRequest[] = {SSL3_MT_HELLO_REQUEST, 0, 0, 0};
  402. while (ssl->s3->hello_request_len < sizeof(kHelloRequest)) {
  403. if (rr->length == 0) {
  404. /* Get a new record. */
  405. goto start;
  406. }
  407. if (rr->data[0] != kHelloRequest[ssl->s3->hello_request_len]) {
  408. al = SSL_AD_DECODE_ERROR;
  409. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
  410. goto f_err;
  411. }
  412. rr->data++;
  413. rr->length--;
  414. ssl->s3->hello_request_len++;
  415. }
  416. ssl->s3->hello_request_len = 0;
  417. if (ssl->msg_callback) {
  418. ssl->msg_callback(0, ssl->version, SSL3_RT_HANDSHAKE, kHelloRequest,
  419. sizeof(kHelloRequest), ssl, ssl->msg_callback_arg);
  420. }
  421. if (!SSL_is_init_finished(ssl) || !ssl->s3->initial_handshake_complete) {
  422. /* This cannot happen. If a handshake is in progress, |type| must be
  423. * |SSL3_RT_HANDSHAKE|. */
  424. assert(0);
  425. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  426. goto err;
  427. }
  428. if (ssl->renegotiate_mode == ssl_renegotiate_ignore) {
  429. goto start;
  430. }
  431. /* Renegotiation is only supported at quiescent points in the application
  432. * protocol, namely in HTTPS, just before reading the HTTP response. Require
  433. * the record-layer be idle and avoid complexities of sending a handshake
  434. * record while an application_data record is being written. */
  435. if (ssl_write_buffer_is_pending(ssl)) {
  436. al = SSL_AD_NO_RENEGOTIATION;
  437. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
  438. goto f_err;
  439. }
  440. /* Begin a new handshake. */
  441. ssl->s3->total_renegotiations++;
  442. ssl->state = SSL_ST_CONNECT;
  443. i = ssl->handshake_func(ssl);
  444. if (i < 0) {
  445. return i;
  446. }
  447. if (i == 0) {
  448. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  449. return -1;
  450. }
  451. /* The handshake completed synchronously. Continue reading records. */
  452. goto start;
  453. }
  454. /* If an alert record, process the alert. */
  455. if (rr->type == SSL3_RT_ALERT) {
  456. /* Alerts records may not contain fragmented or multiple alerts. */
  457. if (rr->length != 2) {
  458. al = SSL_AD_DECODE_ERROR;
  459. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
  460. goto f_err;
  461. }
  462. if (ssl->msg_callback) {
  463. ssl->msg_callback(0, ssl->version, SSL3_RT_ALERT, rr->data, 2, ssl,
  464. ssl->msg_callback_arg);
  465. }
  466. const uint8_t alert_level = rr->data[0];
  467. const uint8_t alert_descr = rr->data[1];
  468. rr->length -= 2;
  469. rr->data += 2;
  470. if (ssl->info_callback != NULL) {
  471. cb = ssl->info_callback;
  472. } else if (ssl->ctx->info_callback != NULL) {
  473. cb = ssl->ctx->info_callback;
  474. }
  475. if (cb != NULL) {
  476. uint16_t alert = (alert_level << 8) | alert_descr;
  477. cb(ssl, SSL_CB_READ_ALERT, alert);
  478. }
  479. if (alert_level == SSL3_AL_WARNING) {
  480. if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
  481. ssl->s3->clean_shutdown = 1;
  482. ssl->shutdown |= SSL_RECEIVED_SHUTDOWN;
  483. return 0;
  484. }
  485. ssl->s3->warning_alert_count++;
  486. if (ssl->s3->warning_alert_count > kMaxWarningAlerts) {
  487. al = SSL_AD_UNEXPECTED_MESSAGE;
  488. OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_WARNING_ALERTS);
  489. goto f_err;
  490. }
  491. } else if (alert_level == SSL3_AL_FATAL) {
  492. char tmp[16];
  493. OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr);
  494. BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
  495. ERR_add_error_data(2, "SSL alert number ", tmp);
  496. ssl->shutdown |= SSL_RECEIVED_SHUTDOWN;
  497. SSL_CTX_remove_session(ssl->ctx, ssl->session);
  498. return 0;
  499. } else {
  500. al = SSL_AD_ILLEGAL_PARAMETER;
  501. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE);
  502. goto f_err;
  503. }
  504. goto start;
  505. }
  506. if (ssl->shutdown & SSL_SENT_SHUTDOWN) {
  507. /* close_notify has been sent, so discard all records other than alerts. */
  508. rr->length = 0;
  509. goto start;
  510. }
  511. al = SSL_AD_UNEXPECTED_MESSAGE;
  512. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
  513. f_err:
  514. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  515. err:
  516. return -1;
  517. }
  518. int ssl3_send_alert(SSL *ssl, int level, int desc) {
  519. /* If a fatal one, remove from cache */
  520. if (level == 2 && ssl->session != NULL) {
  521. SSL_CTX_remove_session(ssl->ctx, ssl->session);
  522. }
  523. ssl->s3->alert_dispatch = 1;
  524. ssl->s3->send_alert[0] = level;
  525. ssl->s3->send_alert[1] = desc;
  526. if (!ssl_write_buffer_is_pending(ssl)) {
  527. /* Nothing is being written out, so the alert may be dispatched
  528. * immediately. */
  529. return ssl->method->ssl_dispatch_alert(ssl);
  530. }
  531. /* else data is still being written out, we will get written some time in the
  532. * future */
  533. return -1;
  534. }
  535. int ssl3_dispatch_alert(SSL *ssl) {
  536. ssl->s3->alert_dispatch = 0;
  537. int ret = do_ssl3_write(ssl, SSL3_RT_ALERT, &ssl->s3->send_alert[0], 2);
  538. if (ret <= 0) {
  539. ssl->s3->alert_dispatch = 1;
  540. return ret;
  541. }
  542. /* If the alert is fatal, flush the BIO now. */
  543. if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) {
  544. BIO_flush(ssl->wbio);
  545. }
  546. if (ssl->msg_callback != NULL) {
  547. ssl->msg_callback(1 /* write */, ssl->version, SSL3_RT_ALERT,
  548. ssl->s3->send_alert, 2, ssl, ssl->msg_callback_arg);
  549. }
  550. void (*cb)(const SSL *ssl, int type, int value) = NULL;
  551. if (ssl->info_callback != NULL) {
  552. cb = ssl->info_callback;
  553. } else if (ssl->ctx->info_callback != NULL) {
  554. cb = ssl->ctx->info_callback;
  555. }
  556. if (cb != NULL) {
  557. int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1];
  558. cb(ssl, SSL_CB_WRITE_ALERT, alert);
  559. }
  560. return 1;
  561. }