openssl: Non-blocking Socket and Early Data
- OpenSSL version: 1.1.1a
- Configuration data
- OS Name, Version, Hardware platform: Debian Stretch on x64
- Application Details: nginx 1.15.6
Here is a excerpt from nginx log with additional debug messages:
2018/12/04 13:07:13 [debug] 18272#0: *124 SSL ALPN supported by client: h2
2018/12/04 13:07:13 [debug] 18272#0: *124 SSL ALPN supported by client: http/1.1
2018/12/04 13:07:13 [debug] 18272#0: *124 SSL ALPN selected: h2
2018/12/04 13:07:13 [debug] 18272#0: *124 SSL_read_early_data: 1, 1
2018/12/04 13:07:13 [debug] 18272#0: *124 SSL_get_current_cipher()
2018/12/04 13:07:13 [debug] 18272#0: *124 SSL: TLSv1.3, cipher: "TLS_AES_256_GCM_SHA384 TLSv1.3 Kx=any Au=any Enc=AESGCM(256) Mac=AEAD"
2018/12/04 13:07:13 [debug] 18272#0: *124 SSL_session_reused()
2018/12/04 13:07:13 [debug] 18272#0: *124 SSL reused session
[...]
2018/12/04 13:07:13 [debug] 18272#0: *125 SSL_read_early_data: 1, 529
2018/12/04 13:07:13 [debug] 18272#0: *125 SSL_read_early_data: 1, 72
2018/12/04 13:07:13 [debug] 18272#0: *125 SSL_read_early_data: 1, 61
2018/12/04 13:07:13 [debug] 18272#0: *125 SSL_read_early_data: 1, 68
2018/12/04 13:07:13 [debug] 18272#0: *125 SSL_read_early_data: 1, 63
2018/12/04 13:07:13 [debug] 18272#0: *125 SSL_read_early_data: 0, 0
2018/12/04 13:07:13 [debug] 18272#0: *125 SSL_get_error: 2
[...]
2018/12/04 13:07:13 [debug] 18272#0: *125 SSL to write: 344
2018/12/04 13:07:13 [debug] 18272#0: *125 SSL_write_early_data: 1, 344
[...]
2018/12/04 13:07:13 [debug] 18272#0: *125 SSL to write: 16384
2018/12/04 13:07:13 [debug] 18272#0: *125 SSL_write_early_data: 1, 16384
2018/12/04 13:07:13 [debug] 18272#0: *125 SSL to write: 16384
2018/12/04 13:07:13 [debug] 18272#0: *125 SSL_write_early_data: 0, 0
2018/12/04 13:07:13 [debug] 18272#0: *125 SSL_get_error: 3
[...]
2018/12/04 13:07:13 [debug] 18272#0: *125 ssl new session: 915F34E7:32:157
2018/12/04 13:07:13 [debug] 18272#0: *125 SSL_read_early_data: 2, 0
2018/12/04 13:07:13 [alert] 18272#0: *125 ignoring stale global SSL error (at 6) (SSL: error:1409E10F:SSL routines:ssl3_write_bytes:bad length:len:257, rlayer.wnum:0, wb->left:2882, rlayer.wpend_tot:16384, early_data_state:12
) while processing HTTP/2 connection
2018/12/04 13:07:13 [debug] 18272#0: *125 SSL_read: -1
2018/12/04 13:07:13 [debug] 18272#0: *125 SSL_get_error: 5
The numbers after the SSL_read_early_data is a return value and readbytes. The numbers after the SSL_write_early_data is a return value and written bytes.
My Interpretation
Call to the SSL_write_early_data returns 0 with SSL error SSL_ERROR_WANT_WRITE and so we wait until an underlying event library notifies us that the fd is ready for writing to retry the write. In the meantime a new SSL session is created and ssl3_handshake_write function is called. It calls ssl3_write_bytes, where the following check does not pass, because of shorter length
/*
* ensure that if we end up with a smaller value of data to write out
* than the original len from a write which didn't complete for
* non-blocking I/O and also somehow ended up avoiding the check for
* this in ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be
* possible to end up with (len-tot) as a large number that will then
* promptly send beyond the end of the users buffer ... so we trap and
* report the error in a way the user will notice
*/
if ((len < s->rlayer.wnum)
|| ((wb->left != 0) && (len < (s->rlayer.wnum + s->rlayer.wpend_tot)))) {
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_WRITE_BYTES,
SSL_R_BAD_LENGTH);
}
Than the end of early data record arrives which in the nginx code triggers calling ERR_peek_error and then SSL_read which fails.
About this issue
- Original URL
- State: open
- Created 8 years ago
- Reactions: 1
- Comments: 17 (16 by maintainers)
SSL_read() should return SSL_ERROR_WANT_WRITE in that case. It’s already documented that SSL_read() can return SSL_ERROR_WANT_WRITE.
If SSL_read() returns SSL_ERROR_WANT_WRITE, and there is an SSL_write() that didn’t send all data, calling SSL_read() should continue to return SSL_ERROR_WANT_WRITE as long as the data from the SSL_write and the renegiotation after it is not written.
That means that we should not try to write internally until at least all data from the SSL_write() has been encrypted and is at least queued.