Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

Set rwstate consistently. We reset it to SSL_NOTHING at the start of ever SSL_get_error-using operation. Then we only set it to a non-NOTHING value in the rest of the stack on error paths. Currently, ssl->rwstate is set all over the place. Sometimes the pattern is: ssl->rwstate = SSL_WRITING; if (BIO_write(...) <= 0) { goto err; } ssl->rwstate = SSL_NOTHING; Sometimes we only set it to the non-NOTHING value on error. if (BIO_write(...) <= 0) { ssl->rwstate = SSL_WRITING; } ssl->rwstate = SSL_NOTHING; Sometimes we just set it to SSL_NOTHING far from any callback in random places. The third case is arbitrary and clearly should be removed. But, in the second case, we sometimes forget to undo it afterwards. This is largely harmless since an error in the error queue overrides rwstate, but we don't always put something in the error queue (falling back to SSL_ERROR_SYSCALL for "I'm not sure why it failed. Perhaps it was one of your callbacks? Check your errno equivalent."), but in that case a stray rwstate value will cause it to be wrong. We could fix the cases where we fail to set SSL_NOTHING on success cases, but this doesn't account for there being multiple SSL_get_error operations. The consumer may have an SSL_read and an SSL_write running concurrently. Instead, it seems the best option is to lift the SSL_NOTHING reset to the operations and set SSL_WRITING and friends as in the second case. (Someday hopefully we can fix this to just be an enum that is internally returned. It can convert to something stateful at the API layer.) Change-Id: I54665ec066a64eb0e48a06e2fcd0d2681a42df7f Reviewed-on: https://boringssl-review.googlesource.com/7453 Reviewed-by: David Benjamin <davidben@google.com>
8 anni fa
Clear the error queue on entry to core SSL operations. OpenSSL historically made some poor API decisions. Rather than returning a status enum in SSL_read, etc., these functions must be paired with SSL_get_error which determines the cause of the last error's failure. This requires SSL_read communicate with SSL_get_error with some stateful flag, rwstate. Further, probably as workarounds for bugs elsewhere, SSL_get_error does not trust rwstate. Among other quirks, if the error queue is non-empty, SSL_get_error overrides rwstate and returns a value based on that. This requires that SSL_read, etc., be called with an empty error queue. (Or we hit one of the spurious ERR_clear_error calls in the handshake state machine, likely added as further self-workarounds.) Since requiring callers consistently clear the error queue everywhere is unreasonable (crbug.com/567501), clear ERR_clear_error *once* at the entry point. Until/unless[*] we make SSL_get_error sane, this is the most reasonable way to get to the point that clearing the error queue on error is optional. With those in place, the calls in the handshake state machine are no longer needed. (I suspect all the ERR_clear_system_error calls can also go, but I'll investigate and think about that separately.) [*] I'm not even sure it's possible anymore, thanks to the possibility of BIO_write pushing to the error queue. BUG=567501,593963 Change-Id: I564ace199e5a4a74b2554ad3335e99cd17120741 Reviewed-on: https://boringssl-review.googlesource.com/7455 Reviewed-by: Steven Valdez <svaldez@google.com> Reviewed-by: David Benjamin <davidben@google.com>
8 anni fa
Clear the error queue on entry to core SSL operations. OpenSSL historically made some poor API decisions. Rather than returning a status enum in SSL_read, etc., these functions must be paired with SSL_get_error which determines the cause of the last error's failure. This requires SSL_read communicate with SSL_get_error with some stateful flag, rwstate. Further, probably as workarounds for bugs elsewhere, SSL_get_error does not trust rwstate. Among other quirks, if the error queue is non-empty, SSL_get_error overrides rwstate and returns a value based on that. This requires that SSL_read, etc., be called with an empty error queue. (Or we hit one of the spurious ERR_clear_error calls in the handshake state machine, likely added as further self-workarounds.) Since requiring callers consistently clear the error queue everywhere is unreasonable (crbug.com/567501), clear ERR_clear_error *once* at the entry point. Until/unless[*] we make SSL_get_error sane, this is the most reasonable way to get to the point that clearing the error queue on error is optional. With those in place, the calls in the handshake state machine are no longer needed. (I suspect all the ERR_clear_system_error calls can also go, but I'll investigate and think about that separately.) [*] I'm not even sure it's possible anymore, thanks to the possibility of BIO_write pushing to the error queue. BUG=567501,593963 Change-Id: I564ace199e5a4a74b2554ad3335e99cd17120741 Reviewed-on: https://boringssl-review.googlesource.com/7455 Reviewed-by: Steven Valdez <svaldez@google.com> Reviewed-by: David Benjamin <davidben@google.com>
8 anni fa
Clear the error queue on entry to core SSL operations. OpenSSL historically made some poor API decisions. Rather than returning a status enum in SSL_read, etc., these functions must be paired with SSL_get_error which determines the cause of the last error's failure. This requires SSL_read communicate with SSL_get_error with some stateful flag, rwstate. Further, probably as workarounds for bugs elsewhere, SSL_get_error does not trust rwstate. Among other quirks, if the error queue is non-empty, SSL_get_error overrides rwstate and returns a value based on that. This requires that SSL_read, etc., be called with an empty error queue. (Or we hit one of the spurious ERR_clear_error calls in the handshake state machine, likely added as further self-workarounds.) Since requiring callers consistently clear the error queue everywhere is unreasonable (crbug.com/567501), clear ERR_clear_error *once* at the entry point. Until/unless[*] we make SSL_get_error sane, this is the most reasonable way to get to the point that clearing the error queue on error is optional. With those in place, the calls in the handshake state machine are no longer needed. (I suspect all the ERR_clear_system_error calls can also go, but I'll investigate and think about that separately.) [*] I'm not even sure it's possible anymore, thanks to the possibility of BIO_write pushing to the error queue. BUG=567501,593963 Change-Id: I564ace199e5a4a74b2554ad3335e99cd17120741 Reviewed-on: https://boringssl-review.googlesource.com/7455 Reviewed-by: Steven Valdez <svaldez@google.com> Reviewed-by: David Benjamin <davidben@google.com>
8 anni fa
Clear the error queue on entry to core SSL operations. OpenSSL historically made some poor API decisions. Rather than returning a status enum in SSL_read, etc., these functions must be paired with SSL_get_error which determines the cause of the last error's failure. This requires SSL_read communicate with SSL_get_error with some stateful flag, rwstate. Further, probably as workarounds for bugs elsewhere, SSL_get_error does not trust rwstate. Among other quirks, if the error queue is non-empty, SSL_get_error overrides rwstate and returns a value based on that. This requires that SSL_read, etc., be called with an empty error queue. (Or we hit one of the spurious ERR_clear_error calls in the handshake state machine, likely added as further self-workarounds.) Since requiring callers consistently clear the error queue everywhere is unreasonable (crbug.com/567501), clear ERR_clear_error *once* at the entry point. Until/unless[*] we make SSL_get_error sane, this is the most reasonable way to get to the point that clearing the error queue on error is optional. With those in place, the calls in the handshake state machine are no longer needed. (I suspect all the ERR_clear_system_error calls can also go, but I'll investigate and think about that separately.) [*] I'm not even sure it's possible anymore, thanks to the possibility of BIO_write pushing to the error queue. BUG=567501,593963 Change-Id: I564ace199e5a4a74b2554ad3335e99cd17120741 Reviewed-on: https://boringssl-review.googlesource.com/7455 Reviewed-by: Steven Valdez <svaldez@google.com> Reviewed-by: David Benjamin <davidben@google.com>
8 anni fa
Clear the error queue on entry to core SSL operations. OpenSSL historically made some poor API decisions. Rather than returning a status enum in SSL_read, etc., these functions must be paired with SSL_get_error which determines the cause of the last error's failure. This requires SSL_read communicate with SSL_get_error with some stateful flag, rwstate. Further, probably as workarounds for bugs elsewhere, SSL_get_error does not trust rwstate. Among other quirks, if the error queue is non-empty, SSL_get_error overrides rwstate and returns a value based on that. This requires that SSL_read, etc., be called with an empty error queue. (Or we hit one of the spurious ERR_clear_error calls in the handshake state machine, likely added as further self-workarounds.) Since requiring callers consistently clear the error queue everywhere is unreasonable (crbug.com/567501), clear ERR_clear_error *once* at the entry point. Until/unless[*] we make SSL_get_error sane, this is the most reasonable way to get to the point that clearing the error queue on error is optional. With those in place, the calls in the handshake state machine are no longer needed. (I suspect all the ERR_clear_system_error calls can also go, but I'll investigate and think about that separately.) [*] I'm not even sure it's possible anymore, thanks to the possibility of BIO_write pushing to the error queue. BUG=567501,593963 Change-Id: I564ace199e5a4a74b2554ad3335e99cd17120741 Reviewed-on: https://boringssl-review.googlesource.com/7455 Reviewed-by: Steven Valdez <svaldez@google.com> Reviewed-by: David Benjamin <davidben@google.com>
8 anni fa
Set rwstate consistently. We reset it to SSL_NOTHING at the start of ever SSL_get_error-using operation. Then we only set it to a non-NOTHING value in the rest of the stack on error paths. Currently, ssl->rwstate is set all over the place. Sometimes the pattern is: ssl->rwstate = SSL_WRITING; if (BIO_write(...) <= 0) { goto err; } ssl->rwstate = SSL_NOTHING; Sometimes we only set it to the non-NOTHING value on error. if (BIO_write(...) <= 0) { ssl->rwstate = SSL_WRITING; } ssl->rwstate = SSL_NOTHING; Sometimes we just set it to SSL_NOTHING far from any callback in random places. The third case is arbitrary and clearly should be removed. But, in the second case, we sometimes forget to undo it afterwards. This is largely harmless since an error in the error queue overrides rwstate, but we don't always put something in the error queue (falling back to SSL_ERROR_SYSCALL for "I'm not sure why it failed. Perhaps it was one of your callbacks? Check your errno equivalent."), but in that case a stray rwstate value will cause it to be wrong. We could fix the cases where we fail to set SSL_NOTHING on success cases, but this doesn't account for there being multiple SSL_get_error operations. The consumer may have an SSL_read and an SSL_write running concurrently. Instead, it seems the best option is to lift the SSL_NOTHING reset to the operations and set SSL_WRITING and friends as in the second case. (Someday hopefully we can fix this to just be an enum that is internally returned. It can convert to something stateful at the API layer.) Change-Id: I54665ec066a64eb0e48a06e2fcd0d2681a42df7f Reviewed-on: https://boringssl-review.googlesource.com/7453 Reviewed-by: David Benjamin <davidben@google.com>
8 anni fa
Clear the error queue on entry to core SSL operations. OpenSSL historically made some poor API decisions. Rather than returning a status enum in SSL_read, etc., these functions must be paired with SSL_get_error which determines the cause of the last error's failure. This requires SSL_read communicate with SSL_get_error with some stateful flag, rwstate. Further, probably as workarounds for bugs elsewhere, SSL_get_error does not trust rwstate. Among other quirks, if the error queue is non-empty, SSL_get_error overrides rwstate and returns a value based on that. This requires that SSL_read, etc., be called with an empty error queue. (Or we hit one of the spurious ERR_clear_error calls in the handshake state machine, likely added as further self-workarounds.) Since requiring callers consistently clear the error queue everywhere is unreasonable (crbug.com/567501), clear ERR_clear_error *once* at the entry point. Until/unless[*] we make SSL_get_error sane, this is the most reasonable way to get to the point that clearing the error queue on error is optional. With those in place, the calls in the handshake state machine are no longer needed. (I suspect all the ERR_clear_system_error calls can also go, but I'll investigate and think about that separately.) [*] I'm not even sure it's possible anymore, thanks to the possibility of BIO_write pushing to the error queue. BUG=567501,593963 Change-Id: I564ace199e5a4a74b2554ad3335e99cd17120741 Reviewed-on: https://boringssl-review.googlesource.com/7455 Reviewed-by: Steven Valdez <svaldez@google.com> Reviewed-by: David Benjamin <davidben@google.com>
8 anni fa
Clear the error queue on entry to core SSL operations. OpenSSL historically made some poor API decisions. Rather than returning a status enum in SSL_read, etc., these functions must be paired with SSL_get_error which determines the cause of the last error's failure. This requires SSL_read communicate with SSL_get_error with some stateful flag, rwstate. Further, probably as workarounds for bugs elsewhere, SSL_get_error does not trust rwstate. Among other quirks, if the error queue is non-empty, SSL_get_error overrides rwstate and returns a value based on that. This requires that SSL_read, etc., be called with an empty error queue. (Or we hit one of the spurious ERR_clear_error calls in the handshake state machine, likely added as further self-workarounds.) Since requiring callers consistently clear the error queue everywhere is unreasonable (crbug.com/567501), clear ERR_clear_error *once* at the entry point. Until/unless[*] we make SSL_get_error sane, this is the most reasonable way to get to the point that clearing the error queue on error is optional. With those in place, the calls in the handshake state machine are no longer needed. (I suspect all the ERR_clear_system_error calls can also go, but I'll investigate and think about that separately.) [*] I'm not even sure it's possible anymore, thanks to the possibility of BIO_write pushing to the error queue. BUG=567501,593963 Change-Id: I564ace199e5a4a74b2554ad3335e99cd17120741 Reviewed-on: https://boringssl-review.googlesource.com/7455 Reviewed-by: Steven Valdez <svaldez@google.com> Reviewed-by: David Benjamin <davidben@google.com>
8 anni fa
Clear the error queue on entry to core SSL operations. OpenSSL historically made some poor API decisions. Rather than returning a status enum in SSL_read, etc., these functions must be paired with SSL_get_error which determines the cause of the last error's failure. This requires SSL_read communicate with SSL_get_error with some stateful flag, rwstate. Further, probably as workarounds for bugs elsewhere, SSL_get_error does not trust rwstate. Among other quirks, if the error queue is non-empty, SSL_get_error overrides rwstate and returns a value based on that. This requires that SSL_read, etc., be called with an empty error queue. (Or we hit one of the spurious ERR_clear_error calls in the handshake state machine, likely added as further self-workarounds.) Since requiring callers consistently clear the error queue everywhere is unreasonable (crbug.com/567501), clear ERR_clear_error *once* at the entry point. Until/unless[*] we make SSL_get_error sane, this is the most reasonable way to get to the point that clearing the error queue on error is optional. With those in place, the calls in the handshake state machine are no longer needed. (I suspect all the ERR_clear_system_error calls can also go, but I'll investigate and think about that separately.) [*] I'm not even sure it's possible anymore, thanks to the possibility of BIO_write pushing to the error queue. BUG=567501,593963 Change-Id: I564ace199e5a4a74b2554ad3335e99cd17120741 Reviewed-on: https://boringssl-review.googlesource.com/7455 Reviewed-by: Steven Valdez <svaldez@google.com> Reviewed-by: David Benjamin <davidben@google.com>
8 anni fa
Clear the error queue on entry to core SSL operations. OpenSSL historically made some poor API decisions. Rather than returning a status enum in SSL_read, etc., these functions must be paired with SSL_get_error which determines the cause of the last error's failure. This requires SSL_read communicate with SSL_get_error with some stateful flag, rwstate. Further, probably as workarounds for bugs elsewhere, SSL_get_error does not trust rwstate. Among other quirks, if the error queue is non-empty, SSL_get_error overrides rwstate and returns a value based on that. This requires that SSL_read, etc., be called with an empty error queue. (Or we hit one of the spurious ERR_clear_error calls in the handshake state machine, likely added as further self-workarounds.) Since requiring callers consistently clear the error queue everywhere is unreasonable (crbug.com/567501), clear ERR_clear_error *once* at the entry point. Until/unless[*] we make SSL_get_error sane, this is the most reasonable way to get to the point that clearing the error queue on error is optional. With those in place, the calls in the handshake state machine are no longer needed. (I suspect all the ERR_clear_system_error calls can also go, but I'll investigate and think about that separately.) [*] I'm not even sure it's possible anymore, thanks to the possibility of BIO_write pushing to the error queue. BUG=567501,593963 Change-Id: I564ace199e5a4a74b2554ad3335e99cd17120741 Reviewed-on: https://boringssl-review.googlesource.com/7455 Reviewed-by: Steven Valdez <svaldez@google.com> Reviewed-by: David Benjamin <davidben@google.com>
8 anni fa
Set rwstate consistently. We reset it to SSL_NOTHING at the start of ever SSL_get_error-using operation. Then we only set it to a non-NOTHING value in the rest of the stack on error paths. Currently, ssl->rwstate is set all over the place. Sometimes the pattern is: ssl->rwstate = SSL_WRITING; if (BIO_write(...) <= 0) { goto err; } ssl->rwstate = SSL_NOTHING; Sometimes we only set it to the non-NOTHING value on error. if (BIO_write(...) <= 0) { ssl->rwstate = SSL_WRITING; } ssl->rwstate = SSL_NOTHING; Sometimes we just set it to SSL_NOTHING far from any callback in random places. The third case is arbitrary and clearly should be removed. But, in the second case, we sometimes forget to undo it afterwards. This is largely harmless since an error in the error queue overrides rwstate, but we don't always put something in the error queue (falling back to SSL_ERROR_SYSCALL for "I'm not sure why it failed. Perhaps it was one of your callbacks? Check your errno equivalent."), but in that case a stray rwstate value will cause it to be wrong. We could fix the cases where we fail to set SSL_NOTHING on success cases, but this doesn't account for there being multiple SSL_get_error operations. The consumer may have an SSL_read and an SSL_write running concurrently. Instead, it seems the best option is to lift the SSL_NOTHING reset to the operations and set SSL_WRITING and friends as in the second case. (Someday hopefully we can fix this to just be an enum that is internally returned. It can convert to something stateful at the API layer.) Change-Id: I54665ec066a64eb0e48a06e2fcd0d2681a42df7f Reviewed-on: https://boringssl-review.googlesource.com/7453 Reviewed-by: David Benjamin <davidben@google.com>
8 anni fa
Clear the error queue on entry to core SSL operations. OpenSSL historically made some poor API decisions. Rather than returning a status enum in SSL_read, etc., these functions must be paired with SSL_get_error which determines the cause of the last error's failure. This requires SSL_read communicate with SSL_get_error with some stateful flag, rwstate. Further, probably as workarounds for bugs elsewhere, SSL_get_error does not trust rwstate. Among other quirks, if the error queue is non-empty, SSL_get_error overrides rwstate and returns a value based on that. This requires that SSL_read, etc., be called with an empty error queue. (Or we hit one of the spurious ERR_clear_error calls in the handshake state machine, likely added as further self-workarounds.) Since requiring callers consistently clear the error queue everywhere is unreasonable (crbug.com/567501), clear ERR_clear_error *once* at the entry point. Until/unless[*] we make SSL_get_error sane, this is the most reasonable way to get to the point that clearing the error queue on error is optional. With those in place, the calls in the handshake state machine are no longer needed. (I suspect all the ERR_clear_system_error calls can also go, but I'll investigate and think about that separately.) [*] I'm not even sure it's possible anymore, thanks to the possibility of BIO_write pushing to the error queue. BUG=567501,593963 Change-Id: I564ace199e5a4a74b2554ad3335e99cd17120741 Reviewed-on: https://boringssl-review.googlesource.com/7455 Reviewed-by: Steven Valdez <svaldez@google.com> Reviewed-by: David Benjamin <davidben@google.com>
8 anni fa
Clear the error queue on entry to core SSL operations. OpenSSL historically made some poor API decisions. Rather than returning a status enum in SSL_read, etc., these functions must be paired with SSL_get_error which determines the cause of the last error's failure. This requires SSL_read communicate with SSL_get_error with some stateful flag, rwstate. Further, probably as workarounds for bugs elsewhere, SSL_get_error does not trust rwstate. Among other quirks, if the error queue is non-empty, SSL_get_error overrides rwstate and returns a value based on that. This requires that SSL_read, etc., be called with an empty error queue. (Or we hit one of the spurious ERR_clear_error calls in the handshake state machine, likely added as further self-workarounds.) Since requiring callers consistently clear the error queue everywhere is unreasonable (crbug.com/567501), clear ERR_clear_error *once* at the entry point. Until/unless[*] we make SSL_get_error sane, this is the most reasonable way to get to the point that clearing the error queue on error is optional. With those in place, the calls in the handshake state machine are no longer needed. (I suspect all the ERR_clear_system_error calls can also go, but I'll investigate and think about that separately.) [*] I'm not even sure it's possible anymore, thanks to the possibility of BIO_write pushing to the error queue. BUG=567501,593963 Change-Id: I564ace199e5a4a74b2554ad3335e99cd17120741 Reviewed-on: https://boringssl-review.googlesource.com/7455 Reviewed-by: Steven Valdez <svaldez@google.com> Reviewed-by: David Benjamin <davidben@google.com>
8 anni fa
Set rwstate consistently. We reset it to SSL_NOTHING at the start of ever SSL_get_error-using operation. Then we only set it to a non-NOTHING value in the rest of the stack on error paths. Currently, ssl->rwstate is set all over the place. Sometimes the pattern is: ssl->rwstate = SSL_WRITING; if (BIO_write(...) <= 0) { goto err; } ssl->rwstate = SSL_NOTHING; Sometimes we only set it to the non-NOTHING value on error. if (BIO_write(...) <= 0) { ssl->rwstate = SSL_WRITING; } ssl->rwstate = SSL_NOTHING; Sometimes we just set it to SSL_NOTHING far from any callback in random places. The third case is arbitrary and clearly should be removed. But, in the second case, we sometimes forget to undo it afterwards. This is largely harmless since an error in the error queue overrides rwstate, but we don't always put something in the error queue (falling back to SSL_ERROR_SYSCALL for "I'm not sure why it failed. Perhaps it was one of your callbacks? Check your errno equivalent."), but in that case a stray rwstate value will cause it to be wrong. We could fix the cases where we fail to set SSL_NOTHING on success cases, but this doesn't account for there being multiple SSL_get_error operations. The consumer may have an SSL_read and an SSL_write running concurrently. Instead, it seems the best option is to lift the SSL_NOTHING reset to the operations and set SSL_WRITING and friends as in the second case. (Someday hopefully we can fix this to just be an enum that is internally returned. It can convert to something stateful at the API layer.) Change-Id: I54665ec066a64eb0e48a06e2fcd0d2681a42df7f Reviewed-on: https://boringssl-review.googlesource.com/7453 Reviewed-by: David Benjamin <davidben@google.com>
8 anni fa
Clear the error queue on entry to core SSL operations. OpenSSL historically made some poor API decisions. Rather than returning a status enum in SSL_read, etc., these functions must be paired with SSL_get_error which determines the cause of the last error's failure. This requires SSL_read communicate with SSL_get_error with some stateful flag, rwstate. Further, probably as workarounds for bugs elsewhere, SSL_get_error does not trust rwstate. Among other quirks, if the error queue is non-empty, SSL_get_error overrides rwstate and returns a value based on that. This requires that SSL_read, etc., be called with an empty error queue. (Or we hit one of the spurious ERR_clear_error calls in the handshake state machine, likely added as further self-workarounds.) Since requiring callers consistently clear the error queue everywhere is unreasonable (crbug.com/567501), clear ERR_clear_error *once* at the entry point. Until/unless[*] we make SSL_get_error sane, this is the most reasonable way to get to the point that clearing the error queue on error is optional. With those in place, the calls in the handshake state machine are no longer needed. (I suspect all the ERR_clear_system_error calls can also go, but I'll investigate and think about that separately.) [*] I'm not even sure it's possible anymore, thanks to the possibility of BIO_write pushing to the error queue. BUG=567501,593963 Change-Id: I564ace199e5a4a74b2554ad3335e99cd17120741 Reviewed-on: https://boringssl-review.googlesource.com/7455 Reviewed-by: Steven Valdez <svaldez@google.com> Reviewed-by: David Benjamin <davidben@google.com>
8 anni fa
Clear the error queue on entry to core SSL operations. OpenSSL historically made some poor API decisions. Rather than returning a status enum in SSL_read, etc., these functions must be paired with SSL_get_error which determines the cause of the last error's failure. This requires SSL_read communicate with SSL_get_error with some stateful flag, rwstate. Further, probably as workarounds for bugs elsewhere, SSL_get_error does not trust rwstate. Among other quirks, if the error queue is non-empty, SSL_get_error overrides rwstate and returns a value based on that. This requires that SSL_read, etc., be called with an empty error queue. (Or we hit one of the spurious ERR_clear_error calls in the handshake state machine, likely added as further self-workarounds.) Since requiring callers consistently clear the error queue everywhere is unreasonable (crbug.com/567501), clear ERR_clear_error *once* at the entry point. Until/unless[*] we make SSL_get_error sane, this is the most reasonable way to get to the point that clearing the error queue on error is optional. With those in place, the calls in the handshake state machine are no longer needed. (I suspect all the ERR_clear_system_error calls can also go, but I'll investigate and think about that separately.) [*] I'm not even sure it's possible anymore, thanks to the possibility of BIO_write pushing to the error queue. BUG=567501,593963 Change-Id: I564ace199e5a4a74b2554ad3335e99cd17120741 Reviewed-on: https://boringssl-review.googlesource.com/7455 Reviewed-by: Steven Valdez <svaldez@google.com> Reviewed-by: David Benjamin <davidben@google.com>
8 anni fa
Implement draft-ietf-tls-curve25519-01 in C. The new curve is not enabled by default. As EC_GROUP/EC_POINT is a bit too complex for X25519, this introduces an SSL_ECDH_METHOD abstraction which wraps just the raw ECDH operation. It also tidies up some of the curve code which kept converting back and force between NIDs and curve IDs. Now everything transits as curve IDs except for API entry points (SSL_set1_curves) which take NIDs. Those convert immediately and act on curve IDs from then on. Note that, like the Go implementation, this slightly tweaks the order of operations. The client sees the server public key before sending its own. To keep the abstraction simple, SSL_ECDH_METHOD expects to generate a keypair before consuming the peer's public key. Instead, the client handshake stashes the serialized peer public value and defers parsing it until it comes time to send ClientKeyExchange. (This is analogous to what it was doing before where it stashed the parsed peer public value instead.) It still uses TLS 1.2 terminology everywhere, but this abstraction should also be compatible with TLS 1.3 which unifies (EC)DH-style key exchanges. (Accordingly, this abstraction intentionally does not handle parsing the ClientKeyExchange/ServerKeyExchange framing or attempt to handle asynchronous plain RSA or the authentication bits.) BUG=571231 Change-Id: Iba09dddee5bcdfeb2b70185308e8ab0632717932 Reviewed-on: https://boringssl-review.googlesource.com/6780 Reviewed-by: Adam Langley <agl@google.com>
8 anni fa
8 anni fa
8 anni fa
8 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997
  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-2007 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. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. * ECC cipher suite support in OpenSSL originally developed by
  113. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  114. */
  115. /* ====================================================================
  116. * Copyright 2005 Nokia. All rights reserved.
  117. *
  118. * The portions of the attached software ("Contribution") is developed by
  119. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  120. * license.
  121. *
  122. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  123. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  124. * support (see RFC 4279) to OpenSSL.
  125. *
  126. * No patent licenses or other rights except those expressly stated in
  127. * the OpenSSL open source license shall be deemed granted or received
  128. * expressly, by implication, estoppel, or otherwise.
  129. *
  130. * No assurances are provided by Nokia that the Contribution does not
  131. * infringe the patent or other intellectual property rights of any third
  132. * party or that the license provides you with all the necessary rights
  133. * to make use of the Contribution.
  134. *
  135. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  136. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  137. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  138. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  139. * OTHERWISE. */
  140. #include <openssl/ssl.h>
  141. #include <assert.h>
  142. #include <stdlib.h>
  143. #include <string.h>
  144. #include <openssl/bytestring.h>
  145. #include <openssl/crypto.h>
  146. #include <openssl/dh.h>
  147. #include <openssl/err.h>
  148. #include <openssl/lhash.h>
  149. #include <openssl/mem.h>
  150. #include <openssl/rand.h>
  151. #include <openssl/x509v3.h>
  152. #include "internal.h"
  153. #include "../crypto/internal.h"
  154. #if defined(OPENSSL_WINDOWS)
  155. #include <sys/timeb.h>
  156. #else
  157. #include <sys/socket.h>
  158. #include <sys/time.h>
  159. #endif
  160. /* |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
  161. * to avoid downstream churn. */
  162. OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL)
  163. /* The following errors are no longer emitted, but are used in nginx without
  164. * #ifdefs. */
  165. OPENSSL_DECLARE_ERROR_REASON(SSL, BLOCK_CIPHER_PAD_IS_WRONG)
  166. OPENSSL_DECLARE_ERROR_REASON(SSL, NO_CIPHERS_SPECIFIED)
  167. /* Some error codes are special. Ensure the make_errors.go script never
  168. * regresses this. */
  169. OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
  170. SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
  171. ssl_alert_reason_code_mismatch);
  172. /* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */
  173. static const size_t kMaxHandshakeSize = (1u << 24) - 1;
  174. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
  175. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  176. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
  177. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  178. int SSL_library_init(void) {
  179. CRYPTO_library_init();
  180. return 1;
  181. }
  182. static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
  183. const uint8_t *session_id = sess->session_id;
  184. uint8_t tmp_storage[sizeof(uint32_t)];
  185. if (sess->session_id_length < sizeof(tmp_storage)) {
  186. memset(tmp_storage, 0, sizeof(tmp_storage));
  187. memcpy(tmp_storage, sess->session_id, sess->session_id_length);
  188. session_id = tmp_storage;
  189. }
  190. uint32_t hash =
  191. ((uint32_t)session_id[0]) |
  192. ((uint32_t)session_id[1] << 8) |
  193. ((uint32_t)session_id[2] << 16) |
  194. ((uint32_t)session_id[3] << 24);
  195. return hash;
  196. }
  197. /* NB: If this function (or indeed the hash function which uses a sort of
  198. * coarser function than this one) is changed, ensure
  199. * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
  200. * able to construct an SSL_SESSION that will collide with any existing session
  201. * with a matching session ID. */
  202. static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
  203. if (a->ssl_version != b->ssl_version) {
  204. return 1;
  205. }
  206. if (a->session_id_length != b->session_id_length) {
  207. return 1;
  208. }
  209. return memcmp(a->session_id, b->session_id, a->session_id_length);
  210. }
  211. SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
  212. SSL_CTX *ret = NULL;
  213. if (method == NULL) {
  214. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
  215. return NULL;
  216. }
  217. if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
  218. OPENSSL_PUT_ERROR(SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
  219. goto err;
  220. }
  221. ret = OPENSSL_malloc(sizeof(SSL_CTX));
  222. if (ret == NULL) {
  223. goto err;
  224. }
  225. memset(ret, 0, sizeof(SSL_CTX));
  226. ret->method = method->method;
  227. CRYPTO_MUTEX_init(&ret->lock);
  228. ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
  229. ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
  230. /* We take the system default */
  231. ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  232. ret->references = 1;
  233. ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
  234. ret->verify_mode = SSL_VERIFY_NONE;
  235. ret->cert = ssl_cert_new();
  236. if (ret->cert == NULL) {
  237. goto err;
  238. }
  239. ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
  240. if (ret->sessions == NULL) {
  241. goto err;
  242. }
  243. ret->cert_store = X509_STORE_new();
  244. if (ret->cert_store == NULL) {
  245. goto err;
  246. }
  247. ssl_create_cipher_list(ret->method, &ret->cipher_list,
  248. &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
  249. if (ret->cipher_list == NULL ||
  250. sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
  251. OPENSSL_PUT_ERROR(SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
  252. goto err2;
  253. }
  254. ret->param = X509_VERIFY_PARAM_new();
  255. if (!ret->param) {
  256. goto err;
  257. }
  258. ret->client_CA = sk_X509_NAME_new_null();
  259. if (ret->client_CA == NULL) {
  260. goto err;
  261. }
  262. CRYPTO_new_ex_data(&ret->ex_data);
  263. ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  264. /* Setup RFC4507 ticket keys */
  265. if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
  266. !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
  267. !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
  268. ret->options |= SSL_OP_NO_TICKET;
  269. }
  270. ret->min_version = ret->method->min_version;
  271. ret->max_version = ret->method->max_version;
  272. /* Lock the SSL_CTX to the specified version, for compatibility with legacy
  273. * uses of SSL_METHOD. */
  274. if (method->version != 0) {
  275. SSL_CTX_set_max_version(ret, method->version);
  276. SSL_CTX_set_min_version(ret, method->version);
  277. } else if (!method->method->is_dtls) {
  278. /* TODO(svaldez): Enable TLS 1.3 by default once fully implemented. */
  279. SSL_CTX_set_max_version(ret, TLS1_2_VERSION);
  280. }
  281. return ret;
  282. err:
  283. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  284. err2:
  285. SSL_CTX_free(ret);
  286. return NULL;
  287. }
  288. int SSL_CTX_up_ref(SSL_CTX *ctx) {
  289. CRYPTO_refcount_inc(&ctx->references);
  290. return 1;
  291. }
  292. void SSL_CTX_free(SSL_CTX *ctx) {
  293. if (ctx == NULL ||
  294. !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
  295. return;
  296. }
  297. X509_VERIFY_PARAM_free(ctx->param);
  298. /* Free internal session cache. However: the remove_cb() may reference the
  299. * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
  300. * sessions were flushed. As the ex_data handling routines might also touch
  301. * the session cache, the most secure solution seems to be: empty (flush) the
  302. * cache, then free ex_data, then finally free the cache. (See ticket
  303. * [openssl.org #212].) */
  304. SSL_CTX_flush_sessions(ctx, 0);
  305. CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
  306. CRYPTO_MUTEX_cleanup(&ctx->lock);
  307. lh_SSL_SESSION_free(ctx->sessions);
  308. X509_STORE_free(ctx->cert_store);
  309. ssl_cipher_preference_list_free(ctx->cipher_list);
  310. sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
  311. ssl_cipher_preference_list_free(ctx->cipher_list_tls10);
  312. ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
  313. ssl_cert_free(ctx->cert);
  314. sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
  315. SSL_CUSTOM_EXTENSION_free);
  316. sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
  317. SSL_CUSTOM_EXTENSION_free);
  318. sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
  319. sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
  320. OPENSSL_free(ctx->psk_identity_hint);
  321. OPENSSL_free(ctx->supported_group_list);
  322. OPENSSL_free(ctx->alpn_client_proto_list);
  323. OPENSSL_free(ctx->ocsp_response);
  324. OPENSSL_free(ctx->signed_cert_timestamp_list);
  325. EVP_PKEY_free(ctx->tlsext_channel_id_private);
  326. OPENSSL_free(ctx);
  327. }
  328. SSL *SSL_new(SSL_CTX *ctx) {
  329. if (ctx == NULL) {
  330. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
  331. return NULL;
  332. }
  333. if (ctx->method == NULL) {
  334. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
  335. return NULL;
  336. }
  337. SSL *ssl = OPENSSL_malloc(sizeof(SSL));
  338. if (ssl == NULL) {
  339. goto err;
  340. }
  341. memset(ssl, 0, sizeof(SSL));
  342. ssl->min_version = ctx->min_version;
  343. ssl->max_version = ctx->max_version;
  344. ssl->state = SSL_ST_INIT;
  345. /* RFC 6347 states that implementations SHOULD use an initial timer value of
  346. * 1 second. */
  347. ssl->initial_timeout_duration_ms = 1000;
  348. ssl->options = ctx->options;
  349. ssl->mode = ctx->mode;
  350. ssl->max_cert_list = ctx->max_cert_list;
  351. ssl->cert = ssl_cert_dup(ctx->cert);
  352. if (ssl->cert == NULL) {
  353. goto err;
  354. }
  355. ssl->msg_callback = ctx->msg_callback;
  356. ssl->msg_callback_arg = ctx->msg_callback_arg;
  357. ssl->verify_mode = ctx->verify_mode;
  358. ssl->sid_ctx_length = ctx->sid_ctx_length;
  359. assert(ssl->sid_ctx_length <= sizeof ssl->sid_ctx);
  360. memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx));
  361. ssl->verify_callback = ctx->default_verify_callback;
  362. ssl->param = X509_VERIFY_PARAM_new();
  363. if (!ssl->param) {
  364. goto err;
  365. }
  366. X509_VERIFY_PARAM_inherit(ssl->param, ctx->param);
  367. ssl->quiet_shutdown = ctx->quiet_shutdown;
  368. ssl->max_send_fragment = ctx->max_send_fragment;
  369. CRYPTO_refcount_inc(&ctx->references);
  370. ssl->ctx = ctx;
  371. CRYPTO_refcount_inc(&ctx->references);
  372. ssl->initial_ctx = ctx;
  373. if (ctx->supported_group_list) {
  374. ssl->supported_group_list =
  375. BUF_memdup(ctx->supported_group_list,
  376. ctx->supported_group_list_len * 2);
  377. if (!ssl->supported_group_list) {
  378. goto err;
  379. }
  380. ssl->supported_group_list_len = ctx->supported_group_list_len;
  381. }
  382. if (ssl->ctx->alpn_client_proto_list) {
  383. ssl->alpn_client_proto_list = BUF_memdup(
  384. ssl->ctx->alpn_client_proto_list, ssl->ctx->alpn_client_proto_list_len);
  385. if (ssl->alpn_client_proto_list == NULL) {
  386. goto err;
  387. }
  388. ssl->alpn_client_proto_list_len = ssl->ctx->alpn_client_proto_list_len;
  389. }
  390. ssl->method = ctx->method;
  391. if (!ssl->method->ssl_new(ssl)) {
  392. goto err;
  393. }
  394. ssl->rwstate = SSL_NOTHING;
  395. CRYPTO_new_ex_data(&ssl->ex_data);
  396. ssl->psk_identity_hint = NULL;
  397. if (ctx->psk_identity_hint) {
  398. ssl->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
  399. if (ssl->psk_identity_hint == NULL) {
  400. goto err;
  401. }
  402. }
  403. ssl->psk_client_callback = ctx->psk_client_callback;
  404. ssl->psk_server_callback = ctx->psk_server_callback;
  405. ssl->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
  406. if (ctx->tlsext_channel_id_private) {
  407. EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
  408. ssl->tlsext_channel_id_private = ctx->tlsext_channel_id_private;
  409. }
  410. ssl->signed_cert_timestamps_enabled =
  411. ssl->ctx->signed_cert_timestamps_enabled;
  412. ssl->ocsp_stapling_enabled = ssl->ctx->ocsp_stapling_enabled;
  413. return ssl;
  414. err:
  415. SSL_free(ssl);
  416. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  417. return NULL;
  418. }
  419. void SSL_free(SSL *ssl) {
  420. if (ssl == NULL) {
  421. return;
  422. }
  423. X509_VERIFY_PARAM_free(ssl->param);
  424. CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
  425. ssl_free_wbio_buffer(ssl);
  426. assert(ssl->bbio == NULL);
  427. BIO_free_all(ssl->rbio);
  428. BIO_free_all(ssl->wbio);
  429. BUF_MEM_free(ssl->init_buf);
  430. /* add extra stuff */
  431. ssl_cipher_preference_list_free(ssl->cipher_list);
  432. sk_SSL_CIPHER_free(ssl->cipher_list_by_id);
  433. SSL_SESSION_free(ssl->session);
  434. ssl_cert_free(ssl->cert);
  435. OPENSSL_free(ssl->tlsext_hostname);
  436. SSL_CTX_free(ssl->initial_ctx);
  437. OPENSSL_free(ssl->supported_group_list);
  438. OPENSSL_free(ssl->alpn_client_proto_list);
  439. EVP_PKEY_free(ssl->tlsext_channel_id_private);
  440. OPENSSL_free(ssl->psk_identity_hint);
  441. sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
  442. sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
  443. if (ssl->method != NULL) {
  444. ssl->method->ssl_free(ssl);
  445. }
  446. SSL_CTX_free(ssl->ctx);
  447. OPENSSL_free(ssl);
  448. }
  449. void SSL_set_connect_state(SSL *ssl) {
  450. ssl->server = 0;
  451. ssl->handshake_func = ssl3_connect;
  452. }
  453. void SSL_set_accept_state(SSL *ssl) {
  454. ssl->server = 1;
  455. ssl->handshake_func = ssl3_accept;
  456. }
  457. void SSL_set0_rbio(SSL *ssl, BIO *rbio) {
  458. BIO_free_all(ssl->rbio);
  459. ssl->rbio = rbio;
  460. }
  461. void SSL_set0_wbio(SSL *ssl, BIO *wbio) {
  462. /* If the output buffering BIO is still in place, remove it. */
  463. if (ssl->bbio != NULL) {
  464. ssl->wbio = BIO_pop(ssl->wbio);
  465. }
  466. BIO_free_all(ssl->wbio);
  467. ssl->wbio = wbio;
  468. /* Re-attach |bbio| to the new |wbio|. */
  469. if (ssl->bbio != NULL) {
  470. ssl->wbio = BIO_push(ssl->bbio, ssl->wbio);
  471. }
  472. }
  473. void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
  474. /* For historical reasons, this function has many different cases in ownership
  475. * handling. */
  476. /* If nothing has changed, do nothing */
  477. if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) {
  478. return;
  479. }
  480. /* If the two arguments are equal, one fewer reference is granted than
  481. * taken. */
  482. if (rbio != NULL && rbio == wbio) {
  483. BIO_up_ref(rbio);
  484. }
  485. /* If only the wbio is changed, adopt only one reference. */
  486. if (rbio == SSL_get_rbio(ssl)) {
  487. SSL_set0_wbio(ssl, wbio);
  488. return;
  489. }
  490. /* There is an asymmetry here for historical reasons. If only the rbio is
  491. * changed AND the rbio and wbio were originally different, then we only adopt
  492. * one reference. */
  493. if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) {
  494. SSL_set0_rbio(ssl, rbio);
  495. return;
  496. }
  497. /* Otherwise, adopt both references. */
  498. SSL_set0_rbio(ssl, rbio);
  499. SSL_set0_wbio(ssl, wbio);
  500. }
  501. BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
  502. BIO *SSL_get_wbio(const SSL *ssl) {
  503. if (ssl->bbio != NULL) {
  504. /* If |bbio| is active, the true caller-configured BIO is its |next_bio|. */
  505. assert(ssl->bbio == ssl->wbio);
  506. return ssl->bbio->next_bio;
  507. }
  508. return ssl->wbio;
  509. }
  510. int SSL_do_handshake(SSL *ssl) {
  511. ssl->rwstate = SSL_NOTHING;
  512. /* Functions which use SSL_get_error must clear the error queue on entry. */
  513. ERR_clear_error();
  514. ERR_clear_system_error();
  515. if (ssl->handshake_func == NULL) {
  516. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
  517. return -1;
  518. }
  519. if (!SSL_in_init(ssl)) {
  520. return 1;
  521. }
  522. return ssl->handshake_func(ssl);
  523. }
  524. int SSL_connect(SSL *ssl) {
  525. if (ssl->handshake_func == NULL) {
  526. /* Not properly initialized yet */
  527. SSL_set_connect_state(ssl);
  528. }
  529. return SSL_do_handshake(ssl);
  530. }
  531. int SSL_accept(SSL *ssl) {
  532. if (ssl->handshake_func == NULL) {
  533. /* Not properly initialized yet */
  534. SSL_set_accept_state(ssl);
  535. }
  536. return SSL_do_handshake(ssl);
  537. }
  538. static int ssl_do_renegotiate(SSL *ssl) {
  539. /* We do not accept renegotiations as a server. */
  540. if (ssl->server) {
  541. goto no_renegotiation;
  542. }
  543. if (ssl->s3->tmp.message_type != SSL3_MT_HELLO_REQUEST ||
  544. ssl->init_num != 0) {
  545. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  546. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
  547. return 0;
  548. }
  549. switch (ssl->renegotiate_mode) {
  550. case ssl_renegotiate_ignore:
  551. /* Ignore the HelloRequest. */
  552. return 1;
  553. case ssl_renegotiate_once:
  554. if (ssl->s3->total_renegotiations != 0) {
  555. goto no_renegotiation;
  556. }
  557. break;
  558. case ssl_renegotiate_never:
  559. goto no_renegotiation;
  560. case ssl_renegotiate_freely:
  561. break;
  562. }
  563. /* Renegotiation is only supported at quiescent points in the application
  564. * protocol, namely in HTTPS, just before reading the HTTP response. Require
  565. * the record-layer be idle and avoid complexities of sending a handshake
  566. * record while an application_data record is being written. */
  567. if (ssl_write_buffer_is_pending(ssl)) {
  568. goto no_renegotiation;
  569. }
  570. /* Begin a new handshake. */
  571. ssl->s3->total_renegotiations++;
  572. ssl->state = SSL_ST_INIT;
  573. return 1;
  574. no_renegotiation:
  575. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
  576. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
  577. return 0;
  578. }
  579. static int ssl_do_post_handshake(SSL *ssl) {
  580. if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  581. return ssl_do_renegotiate(ssl);
  582. }
  583. return tls13_post_handshake(ssl);
  584. }
  585. static int ssl_read_impl(SSL *ssl, void *buf, int num, int peek) {
  586. ssl->rwstate = SSL_NOTHING;
  587. /* Functions which use SSL_get_error must clear the error queue on entry. */
  588. ERR_clear_error();
  589. ERR_clear_system_error();
  590. if (ssl->handshake_func == NULL) {
  591. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  592. return -1;
  593. }
  594. for (;;) {
  595. /* Complete the current handshake, if any. False Start will cause
  596. * |SSL_do_handshake| to return mid-handshake, so this may require multiple
  597. * iterations. */
  598. while (SSL_in_init(ssl)) {
  599. int ret = SSL_do_handshake(ssl);
  600. if (ret < 0) {
  601. return ret;
  602. }
  603. if (ret == 0) {
  604. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  605. return -1;
  606. }
  607. }
  608. int got_handshake;
  609. int ret = ssl->method->read_app_data(ssl, &got_handshake, buf, num, peek);
  610. if (ret > 0 || !got_handshake) {
  611. ssl->s3->key_update_count = 0;
  612. return ret;
  613. }
  614. /* Handle the post-handshake message and try again. */
  615. if (!ssl_do_post_handshake(ssl)) {
  616. return -1;
  617. }
  618. ssl->method->release_current_message(ssl, 1 /* free buffer */);
  619. }
  620. }
  621. int SSL_read(SSL *ssl, void *buf, int num) {
  622. return ssl_read_impl(ssl, buf, num, 0 /* consume bytes */);
  623. }
  624. int SSL_peek(SSL *ssl, void *buf, int num) {
  625. return ssl_read_impl(ssl, buf, num, 1 /* peek */);
  626. }
  627. int SSL_write(SSL *ssl, const void *buf, int num) {
  628. ssl->rwstate = SSL_NOTHING;
  629. /* Functions which use SSL_get_error must clear the error queue on entry. */
  630. ERR_clear_error();
  631. ERR_clear_system_error();
  632. if (ssl->handshake_func == NULL) {
  633. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  634. return -1;
  635. }
  636. if (ssl->s3->send_shutdown != ssl_shutdown_none) {
  637. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  638. return -1;
  639. }
  640. /* If necessary, complete the handshake implicitly. */
  641. if (SSL_in_init(ssl) && !SSL_in_false_start(ssl)) {
  642. int ret = SSL_do_handshake(ssl);
  643. if (ret < 0) {
  644. return ret;
  645. }
  646. if (ret == 0) {
  647. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  648. return -1;
  649. }
  650. }
  651. return ssl->method->write_app_data(ssl, buf, num);
  652. }
  653. int SSL_shutdown(SSL *ssl) {
  654. ssl->rwstate = SSL_NOTHING;
  655. /* Functions which use SSL_get_error must clear the error queue on entry. */
  656. ERR_clear_error();
  657. ERR_clear_system_error();
  658. if (ssl->handshake_func == NULL) {
  659. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  660. return -1;
  661. }
  662. /* We can't shutdown properly if we are in the middle of a handshake. */
  663. if (SSL_in_init(ssl)) {
  664. OPENSSL_PUT_ERROR(SSL, SSL_R_SHUTDOWN_WHILE_IN_INIT);
  665. return -1;
  666. }
  667. if (ssl->quiet_shutdown) {
  668. /* Do nothing if configured not to send a close_notify. */
  669. ssl->s3->send_shutdown = ssl_shutdown_close_notify;
  670. ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
  671. return 1;
  672. }
  673. /* This function completes in two stages. It sends a close_notify and then it
  674. * waits for a close_notify to come in. Perform exactly one action and return
  675. * whether or not it succeeds. */
  676. if (ssl->s3->send_shutdown != ssl_shutdown_close_notify) {
  677. /* Send a close_notify. */
  678. if (ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
  679. return -1;
  680. }
  681. } else if (ssl->s3->alert_dispatch) {
  682. /* Finish sending the close_notify. */
  683. if (ssl->method->dispatch_alert(ssl) <= 0) {
  684. return -1;
  685. }
  686. } else if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
  687. /* Wait for the peer's close_notify. */
  688. ssl->method->read_close_notify(ssl);
  689. if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
  690. return -1;
  691. }
  692. }
  693. /* Return 0 for unidirectional shutdown and 1 for bidirectional shutdown. */
  694. return ssl->s3->recv_shutdown == ssl_shutdown_close_notify;
  695. }
  696. int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) {
  697. if (ssl->s3->alert_dispatch) {
  698. if (ssl->s3->send_alert[0] != SSL3_AL_FATAL ||
  699. ssl->s3->send_alert[1] != alert) {
  700. /* We are already attempting to write a different alert. */
  701. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  702. return -1;
  703. }
  704. return ssl->method->dispatch_alert(ssl);
  705. }
  706. return ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  707. }
  708. int SSL_get_error(const SSL *ssl, int ret_code) {
  709. int reason;
  710. uint32_t err;
  711. BIO *bio;
  712. if (ret_code > 0) {
  713. return SSL_ERROR_NONE;
  714. }
  715. /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
  716. * where we do encode the error */
  717. err = ERR_peek_error();
  718. if (err != 0) {
  719. if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
  720. return SSL_ERROR_SYSCALL;
  721. }
  722. return SSL_ERROR_SSL;
  723. }
  724. if (ret_code == 0) {
  725. if (ssl->s3->recv_shutdown == ssl_shutdown_close_notify) {
  726. return SSL_ERROR_ZERO_RETURN;
  727. }
  728. /* An EOF was observed which violates the protocol, and the underlying
  729. * transport does not participate in the error queue. Bubble up to the
  730. * caller. */
  731. return SSL_ERROR_SYSCALL;
  732. }
  733. if (SSL_want_session(ssl)) {
  734. return SSL_ERROR_PENDING_SESSION;
  735. }
  736. if (SSL_want_certificate(ssl)) {
  737. return SSL_ERROR_PENDING_CERTIFICATE;
  738. }
  739. if (SSL_want_read(ssl)) {
  740. bio = SSL_get_rbio(ssl);
  741. if (BIO_should_read(bio)) {
  742. return SSL_ERROR_WANT_READ;
  743. }
  744. if (BIO_should_write(bio)) {
  745. /* This one doesn't make too much sense ... We never try to write to the
  746. * rbio, and an application program where rbio and wbio are separate
  747. * couldn't even know what it should wait for. However if we ever set
  748. * ssl->rwstate incorrectly (so that we have SSL_want_read(ssl) instead of
  749. * SSL_want_write(ssl)) and rbio and wbio *are* the same, this test works
  750. * around that bug; so it might be safer to keep it. */
  751. return SSL_ERROR_WANT_WRITE;
  752. }
  753. if (BIO_should_io_special(bio)) {
  754. reason = BIO_get_retry_reason(bio);
  755. if (reason == BIO_RR_CONNECT) {
  756. return SSL_ERROR_WANT_CONNECT;
  757. }
  758. if (reason == BIO_RR_ACCEPT) {
  759. return SSL_ERROR_WANT_ACCEPT;
  760. }
  761. return SSL_ERROR_SYSCALL; /* unknown */
  762. }
  763. }
  764. if (SSL_want_write(ssl)) {
  765. bio = SSL_get_wbio(ssl);
  766. if (BIO_should_write(bio)) {
  767. return SSL_ERROR_WANT_WRITE;
  768. }
  769. if (BIO_should_read(bio)) {
  770. /* See above (SSL_want_read(ssl) with BIO_should_write(bio)) */
  771. return SSL_ERROR_WANT_READ;
  772. }
  773. if (BIO_should_io_special(bio)) {
  774. reason = BIO_get_retry_reason(bio);
  775. if (reason == BIO_RR_CONNECT) {
  776. return SSL_ERROR_WANT_CONNECT;
  777. }
  778. if (reason == BIO_RR_ACCEPT) {
  779. return SSL_ERROR_WANT_ACCEPT;
  780. }
  781. return SSL_ERROR_SYSCALL;
  782. }
  783. }
  784. if (SSL_want_x509_lookup(ssl)) {
  785. return SSL_ERROR_WANT_X509_LOOKUP;
  786. }
  787. if (SSL_want_channel_id_lookup(ssl)) {
  788. return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
  789. }
  790. if (SSL_want_private_key_operation(ssl)) {
  791. return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
  792. }
  793. return SSL_ERROR_SYSCALL;
  794. }
  795. void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
  796. ctx->min_version = ctx->method->version_from_wire(version);
  797. }
  798. void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
  799. ctx->max_version = ctx->method->version_from_wire(version);
  800. }
  801. void SSL_set_min_version(SSL *ssl, uint16_t version) {
  802. ssl->min_version = ssl->method->version_from_wire(version);
  803. }
  804. void SSL_set_max_version(SSL *ssl, uint16_t version) {
  805. ssl->max_version = ssl->method->version_from_wire(version);
  806. }
  807. void SSL_set_fallback_version(SSL *ssl, uint16_t version) {
  808. ssl->fallback_version = ssl->method->version_from_wire(version);
  809. }
  810. uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
  811. ctx->options |= options;
  812. return ctx->options;
  813. }
  814. uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
  815. ctx->options &= ~options;
  816. return ctx->options;
  817. }
  818. uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
  819. uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
  820. ssl->options |= options;
  821. return ssl->options;
  822. }
  823. uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
  824. ssl->options &= ~options;
  825. return ssl->options;
  826. }
  827. uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
  828. uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
  829. ctx->mode |= mode;
  830. return ctx->mode;
  831. }
  832. uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
  833. ctx->mode &= ~mode;
  834. return ctx->mode;
  835. }
  836. uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
  837. uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
  838. ssl->mode |= mode;
  839. return ssl->mode;
  840. }
  841. uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
  842. ssl->mode &= ~mode;
  843. return ssl->mode;
  844. }
  845. uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
  846. X509 *SSL_get_peer_certificate(const SSL *ssl) {
  847. if (ssl == NULL) {
  848. return NULL;
  849. }
  850. SSL_SESSION *session = SSL_get_session(ssl);
  851. if (session == NULL || session->peer == NULL) {
  852. return NULL;
  853. }
  854. X509_up_ref(session->peer);
  855. return session->peer;
  856. }
  857. STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
  858. if (ssl == NULL) {
  859. return NULL;
  860. }
  861. SSL_SESSION *session = SSL_get_session(ssl);
  862. if (session == NULL) {
  863. return NULL;
  864. }
  865. return session->cert_chain;
  866. }
  867. int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
  868. size_t max_out) {
  869. /* The tls-unique value is the first Finished message in the handshake, which
  870. * is the client's in a full handshake and the server's for a resumption. See
  871. * https://tools.ietf.org/html/rfc5929#section-3.1. */
  872. const uint8_t *finished = ssl->s3->previous_client_finished;
  873. size_t finished_len = ssl->s3->previous_client_finished_len;
  874. if (ssl->session != NULL) {
  875. /* tls-unique is broken for resumed sessions unless EMS is used. */
  876. if (!ssl->session->extended_master_secret) {
  877. goto err;
  878. }
  879. finished = ssl->s3->previous_server_finished;
  880. finished_len = ssl->s3->previous_server_finished_len;
  881. }
  882. if (!ssl->s3->initial_handshake_complete ||
  883. ssl->version < TLS1_VERSION) {
  884. goto err;
  885. }
  886. *out_len = finished_len;
  887. if (finished_len > max_out) {
  888. *out_len = max_out;
  889. }
  890. memcpy(out, finished, *out_len);
  891. return 1;
  892. err:
  893. *out_len = 0;
  894. memset(out, 0, max_out);
  895. return 0;
  896. }
  897. int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
  898. unsigned sid_ctx_len) {
  899. if (sid_ctx_len > sizeof(ctx->sid_ctx)) {
  900. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  901. return 0;
  902. }
  903. ctx->sid_ctx_length = sid_ctx_len;
  904. memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
  905. return 1;
  906. }
  907. int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
  908. unsigned sid_ctx_len) {
  909. if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
  910. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  911. return 0;
  912. }
  913. ssl->sid_ctx_length = sid_ctx_len;
  914. memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
  915. return 1;
  916. }
  917. int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) {
  918. return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
  919. }
  920. int SSL_set_purpose(SSL *ssl, int purpose) {
  921. return X509_VERIFY_PARAM_set_purpose(ssl->param, purpose);
  922. }
  923. int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) {
  924. return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
  925. }
  926. int SSL_set_trust(SSL *ssl, int trust) {
  927. return X509_VERIFY_PARAM_set_trust(ssl->param, trust);
  928. }
  929. int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) {
  930. return X509_VERIFY_PARAM_set1(ctx->param, param);
  931. }
  932. int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) {
  933. return X509_VERIFY_PARAM_set1(ssl->param, param);
  934. }
  935. void ssl_cipher_preference_list_free(
  936. struct ssl_cipher_preference_list_st *cipher_list) {
  937. if (cipher_list == NULL) {
  938. return;
  939. }
  940. sk_SSL_CIPHER_free(cipher_list->ciphers);
  941. OPENSSL_free(cipher_list->in_group_flags);
  942. OPENSSL_free(cipher_list);
  943. }
  944. X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
  945. X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
  946. void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
  947. int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
  948. int SSL_get_rfd(const SSL *ssl) {
  949. int ret = -1;
  950. BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
  951. if (b != NULL) {
  952. BIO_get_fd(b, &ret);
  953. }
  954. return ret;
  955. }
  956. int SSL_get_wfd(const SSL *ssl) {
  957. int ret = -1;
  958. BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
  959. if (b != NULL) {
  960. BIO_get_fd(b, &ret);
  961. }
  962. return ret;
  963. }
  964. int SSL_set_fd(SSL *ssl, int fd) {
  965. BIO *bio = BIO_new(BIO_s_socket());
  966. if (bio == NULL) {
  967. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  968. return 0;
  969. }
  970. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  971. SSL_set_bio(ssl, bio, bio);
  972. return 1;
  973. }
  974. int SSL_set_wfd(SSL *ssl, int fd) {
  975. BIO *rbio = SSL_get_rbio(ssl);
  976. if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET ||
  977. BIO_get_fd(rbio, NULL) != fd) {
  978. BIO *bio = BIO_new(BIO_s_socket());
  979. if (bio == NULL) {
  980. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  981. return 0;
  982. }
  983. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  984. SSL_set0_wbio(ssl, bio);
  985. } else {
  986. /* Copy the rbio over to the wbio. */
  987. BIO_up_ref(rbio);
  988. SSL_set0_wbio(ssl, rbio);
  989. }
  990. return 1;
  991. }
  992. int SSL_set_rfd(SSL *ssl, int fd) {
  993. BIO *wbio = SSL_get_wbio(ssl);
  994. if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET ||
  995. BIO_get_fd(wbio, NULL) != fd) {
  996. BIO *bio = BIO_new(BIO_s_socket());
  997. if (bio == NULL) {
  998. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  999. return 0;
  1000. }
  1001. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1002. SSL_set0_rbio(ssl, bio);
  1003. } else {
  1004. /* Copy the wbio over to the rbio. */
  1005. BIO_up_ref(wbio);
  1006. SSL_set0_rbio(ssl, wbio);
  1007. }
  1008. return 1;
  1009. }
  1010. size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
  1011. size_t ret = 0;
  1012. if (ssl->s3 != NULL) {
  1013. ret = ssl->s3->tmp.finish_md_len;
  1014. if (count > ret) {
  1015. count = ret;
  1016. }
  1017. memcpy(buf, ssl->s3->tmp.finish_md, count);
  1018. }
  1019. return ret;
  1020. }
  1021. size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
  1022. size_t ret = 0;
  1023. if (ssl->s3 != NULL) {
  1024. ret = ssl->s3->tmp.peer_finish_md_len;
  1025. if (count > ret) {
  1026. count = ret;
  1027. }
  1028. memcpy(buf, ssl->s3->tmp.peer_finish_md, count);
  1029. }
  1030. return ret;
  1031. }
  1032. int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; }
  1033. int SSL_get_verify_depth(const SSL *ssl) {
  1034. return X509_VERIFY_PARAM_get_depth(ssl->param);
  1035. }
  1036. int SSL_get_extms_support(const SSL *ssl) {
  1037. if (!ssl->s3->have_version) {
  1038. return 0;
  1039. }
  1040. return ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
  1041. ssl->s3->tmp.extended_master_secret == 1;
  1042. }
  1043. int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) {
  1044. return ssl->verify_callback;
  1045. }
  1046. int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
  1047. int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
  1048. return X509_VERIFY_PARAM_get_depth(ctx->param);
  1049. }
  1050. int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(
  1051. int ok, X509_STORE_CTX *store_ctx) {
  1052. return ctx->default_verify_callback;
  1053. }
  1054. void SSL_set_verify(SSL *ssl, int mode,
  1055. int (*callback)(int ok, X509_STORE_CTX *store_ctx)) {
  1056. ssl->verify_mode = mode;
  1057. if (callback != NULL) {
  1058. ssl->verify_callback = callback;
  1059. }
  1060. }
  1061. void SSL_set_verify_depth(SSL *ssl, int depth) {
  1062. X509_VERIFY_PARAM_set_depth(ssl->param, depth);
  1063. }
  1064. int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
  1065. int SSL_get_read_ahead(const SSL *ssl) { return 0; }
  1066. void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
  1067. void SSL_set_read_ahead(SSL *ssl, int yes) { }
  1068. int SSL_pending(const SSL *ssl) {
  1069. if (ssl->s3->rrec.type != SSL3_RT_APPLICATION_DATA) {
  1070. return 0;
  1071. }
  1072. return ssl->s3->rrec.length;
  1073. }
  1074. /* Fix this so it checks all the valid key/cert options */
  1075. int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
  1076. if (ctx->cert->x509 == NULL) {
  1077. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
  1078. return 0;
  1079. }
  1080. if (ctx->cert->privatekey == NULL) {
  1081. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  1082. return 0;
  1083. }
  1084. return X509_check_private_key(ctx->cert->x509, ctx->cert->privatekey);
  1085. }
  1086. /* Fix this function so that it takes an optional type parameter */
  1087. int SSL_check_private_key(const SSL *ssl) {
  1088. if (ssl->cert->x509 == NULL) {
  1089. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
  1090. return 0;
  1091. }
  1092. if (ssl->cert->privatekey == NULL) {
  1093. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  1094. return 0;
  1095. }
  1096. return X509_check_private_key(ssl->cert->x509, ssl->cert->privatekey);
  1097. }
  1098. long SSL_get_default_timeout(const SSL *ssl) {
  1099. return SSL_DEFAULT_SESSION_TIMEOUT;
  1100. }
  1101. int SSL_renegotiate(SSL *ssl) {
  1102. /* Caller-initiated renegotiation is not supported. */
  1103. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1104. return 0;
  1105. }
  1106. int SSL_renegotiate_pending(SSL *ssl) {
  1107. return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
  1108. }
  1109. int SSL_total_renegotiations(const SSL *ssl) {
  1110. return ssl->s3->total_renegotiations;
  1111. }
  1112. size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
  1113. return ctx->max_cert_list;
  1114. }
  1115. void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
  1116. if (max_cert_list > kMaxHandshakeSize) {
  1117. max_cert_list = kMaxHandshakeSize;
  1118. }
  1119. ctx->max_cert_list = (uint32_t)max_cert_list;
  1120. }
  1121. size_t SSL_get_max_cert_list(const SSL *ssl) {
  1122. return ssl->max_cert_list;
  1123. }
  1124. void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
  1125. if (max_cert_list > kMaxHandshakeSize) {
  1126. max_cert_list = kMaxHandshakeSize;
  1127. }
  1128. ssl->max_cert_list = (uint32_t)max_cert_list;
  1129. }
  1130. int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
  1131. if (max_send_fragment < 512) {
  1132. max_send_fragment = 512;
  1133. }
  1134. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1135. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1136. }
  1137. ctx->max_send_fragment = (uint16_t)max_send_fragment;
  1138. return 1;
  1139. }
  1140. int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
  1141. if (max_send_fragment < 512) {
  1142. max_send_fragment = 512;
  1143. }
  1144. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1145. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1146. }
  1147. ssl->max_send_fragment = (uint16_t)max_send_fragment;
  1148. return 1;
  1149. }
  1150. int SSL_set_mtu(SSL *ssl, unsigned mtu) {
  1151. if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) {
  1152. return 0;
  1153. }
  1154. ssl->d1->mtu = mtu;
  1155. return 1;
  1156. }
  1157. int SSL_get_secure_renegotiation_support(const SSL *ssl) {
  1158. return ssl->s3->send_connection_binding;
  1159. }
  1160. LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
  1161. size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
  1162. return lh_SSL_SESSION_num_items(ctx->sessions);
  1163. }
  1164. unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
  1165. unsigned long ret = ctx->session_cache_size;
  1166. ctx->session_cache_size = size;
  1167. return ret;
  1168. }
  1169. unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
  1170. return ctx->session_cache_size;
  1171. }
  1172. int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
  1173. int ret = ctx->session_cache_mode;
  1174. ctx->session_cache_mode = mode;
  1175. return ret;
  1176. }
  1177. int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
  1178. return ctx->session_cache_mode;
  1179. }
  1180. int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
  1181. if (out == NULL) {
  1182. return 48;
  1183. }
  1184. if (len != 48) {
  1185. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
  1186. return 0;
  1187. }
  1188. uint8_t *out_bytes = out;
  1189. memcpy(out_bytes, ctx->tlsext_tick_key_name, 16);
  1190. memcpy(out_bytes + 16, ctx->tlsext_tick_hmac_key, 16);
  1191. memcpy(out_bytes + 32, ctx->tlsext_tick_aes_key, 16);
  1192. return 1;
  1193. }
  1194. int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
  1195. if (in == NULL) {
  1196. return 48;
  1197. }
  1198. if (len != 48) {
  1199. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
  1200. return 0;
  1201. }
  1202. const uint8_t *in_bytes = in;
  1203. memcpy(ctx->tlsext_tick_key_name, in_bytes, 16);
  1204. memcpy(ctx->tlsext_tick_hmac_key, in_bytes + 16, 16);
  1205. memcpy(ctx->tlsext_tick_aes_key, in_bytes + 32, 16);
  1206. return 1;
  1207. }
  1208. int SSL_CTX_set_tlsext_ticket_key_cb(
  1209. SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
  1210. EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
  1211. int encrypt)) {
  1212. ctx->tlsext_ticket_key_cb = callback;
  1213. return 1;
  1214. }
  1215. int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
  1216. return tls1_set_curves(&ctx->supported_group_list,
  1217. &ctx->supported_group_list_len, curves,
  1218. curves_len);
  1219. }
  1220. int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
  1221. return tls1_set_curves(&ssl->supported_group_list,
  1222. &ssl->supported_group_list_len, curves,
  1223. curves_len);
  1224. }
  1225. uint16_t SSL_get_curve_id(const SSL *ssl) {
  1226. /* TODO(davidben): This checks the wrong session if there is a renegotiation in
  1227. * progress. */
  1228. SSL_SESSION *session = SSL_get_session(ssl);
  1229. if (session == NULL ||
  1230. session->cipher == NULL ||
  1231. !SSL_CIPHER_is_ECDHE(session->cipher)) {
  1232. return 0;
  1233. }
  1234. return (uint16_t)session->key_exchange_info;
  1235. }
  1236. int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
  1237. DH_free(ctx->cert->dh_tmp);
  1238. ctx->cert->dh_tmp = DHparams_dup(dh);
  1239. if (ctx->cert->dh_tmp == NULL) {
  1240. OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
  1241. return 0;
  1242. }
  1243. return 1;
  1244. }
  1245. int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
  1246. DH_free(ssl->cert->dh_tmp);
  1247. ssl->cert->dh_tmp = DHparams_dup(dh);
  1248. if (ssl->cert->dh_tmp == NULL) {
  1249. OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
  1250. return 0;
  1251. }
  1252. return 1;
  1253. }
  1254. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
  1255. if (ssl == NULL) {
  1256. return NULL;
  1257. }
  1258. if (ssl->cipher_list != NULL) {
  1259. return ssl->cipher_list->ciphers;
  1260. }
  1261. if (ssl->version >= TLS1_1_VERSION && ssl->ctx->cipher_list_tls11 != NULL) {
  1262. return ssl->ctx->cipher_list_tls11->ciphers;
  1263. }
  1264. if (ssl->version >= TLS1_VERSION && ssl->ctx->cipher_list_tls10 != NULL) {
  1265. return ssl->ctx->cipher_list_tls10->ciphers;
  1266. }
  1267. if (ssl->ctx->cipher_list != NULL) {
  1268. return ssl->ctx->cipher_list->ciphers;
  1269. }
  1270. return NULL;
  1271. }
  1272. /* return a STACK of the ciphers available for the SSL and in order of
  1273. * algorithm id */
  1274. STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *ssl) {
  1275. if (ssl == NULL) {
  1276. return NULL;
  1277. }
  1278. if (ssl->cipher_list_by_id != NULL) {
  1279. return ssl->cipher_list_by_id;
  1280. }
  1281. if (ssl->ctx->cipher_list_by_id != NULL) {
  1282. return ssl->ctx->cipher_list_by_id;
  1283. }
  1284. return NULL;
  1285. }
  1286. const char *SSL_get_cipher_list(const SSL *ssl, int n) {
  1287. const SSL_CIPHER *c;
  1288. STACK_OF(SSL_CIPHER) *sk;
  1289. if (ssl == NULL) {
  1290. return NULL;
  1291. }
  1292. sk = SSL_get_ciphers(ssl);
  1293. if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
  1294. return NULL;
  1295. }
  1296. c = sk_SSL_CIPHER_value(sk, n);
  1297. if (c == NULL) {
  1298. return NULL;
  1299. }
  1300. return c->name;
  1301. }
  1302. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
  1303. STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
  1304. ctx->method, &ctx->cipher_list, &ctx->cipher_list_by_id, str);
  1305. if (cipher_list == NULL) {
  1306. return 0;
  1307. }
  1308. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1309. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1310. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1311. return 0;
  1312. }
  1313. return 1;
  1314. }
  1315. int SSL_CTX_set_cipher_list_tls10(SSL_CTX *ctx, const char *str) {
  1316. STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
  1317. ctx->method, &ctx->cipher_list_tls10, NULL, str);
  1318. if (cipher_list == NULL) {
  1319. return 0;
  1320. }
  1321. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1322. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1323. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1324. return 0;
  1325. }
  1326. return 1;
  1327. }
  1328. int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
  1329. STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
  1330. ctx->method, &ctx->cipher_list_tls11, NULL, str);
  1331. if (cipher_list == NULL) {
  1332. return 0;
  1333. }
  1334. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1335. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1336. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1337. return 0;
  1338. }
  1339. return 1;
  1340. }
  1341. int SSL_set_cipher_list(SSL *ssl, const char *str) {
  1342. STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
  1343. ssl->ctx->method, &ssl->cipher_list, &ssl->cipher_list_by_id, str);
  1344. if (cipher_list == NULL) {
  1345. return 0;
  1346. }
  1347. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1348. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1349. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1350. return 0;
  1351. }
  1352. return 1;
  1353. }
  1354. STACK_OF(SSL_CIPHER) *
  1355. ssl_parse_client_cipher_list(const struct ssl_early_callback_ctx *ctx) {
  1356. CBS cipher_suites;
  1357. CBS_init(&cipher_suites, ctx->cipher_suites, ctx->cipher_suites_len);
  1358. STACK_OF(SSL_CIPHER) *sk = sk_SSL_CIPHER_new_null();
  1359. if (sk == NULL) {
  1360. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1361. goto err;
  1362. }
  1363. while (CBS_len(&cipher_suites) > 0) {
  1364. uint16_t cipher_suite;
  1365. if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
  1366. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  1367. goto err;
  1368. }
  1369. const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
  1370. if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
  1371. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1372. goto err;
  1373. }
  1374. }
  1375. return sk;
  1376. err:
  1377. sk_SSL_CIPHER_free(sk);
  1378. return NULL;
  1379. }
  1380. const char *SSL_get_servername(const SSL *ssl, const int type) {
  1381. if (type != TLSEXT_NAMETYPE_host_name) {
  1382. return NULL;
  1383. }
  1384. /* Historically, |SSL_get_servername| was also the configuration getter
  1385. * corresponding to |SSL_set_tlsext_host_name|. */
  1386. if (ssl->tlsext_hostname != NULL) {
  1387. return ssl->tlsext_hostname;
  1388. }
  1389. SSL_SESSION *session = SSL_get_session(ssl);
  1390. if (session == NULL) {
  1391. return NULL;
  1392. }
  1393. return session->tlsext_hostname;
  1394. }
  1395. int SSL_get_servername_type(const SSL *ssl) {
  1396. SSL_SESSION *session = SSL_get_session(ssl);
  1397. if (session == NULL || session->tlsext_hostname == NULL) {
  1398. return -1;
  1399. }
  1400. return TLSEXT_NAMETYPE_host_name;
  1401. }
  1402. void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
  1403. ctx->signed_cert_timestamps_enabled = 1;
  1404. }
  1405. int SSL_enable_signed_cert_timestamps(SSL *ssl) {
  1406. ssl->signed_cert_timestamps_enabled = 1;
  1407. return 1;
  1408. }
  1409. void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
  1410. ctx->ocsp_stapling_enabled = 1;
  1411. }
  1412. int SSL_enable_ocsp_stapling(SSL *ssl) {
  1413. ssl->ocsp_stapling_enabled = 1;
  1414. return 1;
  1415. }
  1416. void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
  1417. size_t *out_len) {
  1418. SSL_SESSION *session = SSL_get_session(ssl);
  1419. *out_len = 0;
  1420. *out = NULL;
  1421. if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
  1422. return;
  1423. }
  1424. *out = session->tlsext_signed_cert_timestamp_list;
  1425. *out_len = session->tlsext_signed_cert_timestamp_list_length;
  1426. }
  1427. void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
  1428. size_t *out_len) {
  1429. SSL_SESSION *session = SSL_get_session(ssl);
  1430. *out_len = 0;
  1431. *out = NULL;
  1432. if (ssl->server || !session || !session->ocsp_response) {
  1433. return;
  1434. }
  1435. *out = session->ocsp_response;
  1436. *out_len = session->ocsp_response_length;
  1437. }
  1438. int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
  1439. size_t list_len) {
  1440. OPENSSL_free(ctx->signed_cert_timestamp_list);
  1441. ctx->signed_cert_timestamp_list_length = 0;
  1442. ctx->signed_cert_timestamp_list = BUF_memdup(list, list_len);
  1443. if (ctx->signed_cert_timestamp_list == NULL) {
  1444. return 0;
  1445. }
  1446. ctx->signed_cert_timestamp_list_length = list_len;
  1447. return 1;
  1448. }
  1449. int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
  1450. size_t response_len) {
  1451. OPENSSL_free(ctx->ocsp_response);
  1452. ctx->ocsp_response_length = 0;
  1453. ctx->ocsp_response = BUF_memdup(response, response_len);
  1454. if (ctx->ocsp_response == NULL) {
  1455. return 0;
  1456. }
  1457. ctx->ocsp_response_length = response_len;
  1458. return 1;
  1459. }
  1460. int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
  1461. OPENSSL_free(ssl->tlsext_hostname);
  1462. ssl->tlsext_hostname = NULL;
  1463. if (name == NULL) {
  1464. return 1;
  1465. }
  1466. size_t len = strlen(name);
  1467. if (len == 0 || len > TLSEXT_MAXLEN_host_name) {
  1468. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
  1469. return 0;
  1470. }
  1471. ssl->tlsext_hostname = BUF_strdup(name);
  1472. if (ssl->tlsext_hostname == NULL) {
  1473. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1474. return 0;
  1475. }
  1476. return 1;
  1477. }
  1478. int SSL_CTX_set_tlsext_servername_callback(
  1479. SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
  1480. ctx->tlsext_servername_callback = callback;
  1481. return 1;
  1482. }
  1483. int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
  1484. ctx->tlsext_servername_arg = arg;
  1485. return 1;
  1486. }
  1487. int SSL_select_next_proto(uint8_t **out, uint8_t *out_len,
  1488. const uint8_t *server, unsigned server_len,
  1489. const uint8_t *client, unsigned client_len) {
  1490. unsigned int i, j;
  1491. const uint8_t *result;
  1492. int status = OPENSSL_NPN_UNSUPPORTED;
  1493. /* For each protocol in server preference order, see if we support it. */
  1494. for (i = 0; i < server_len;) {
  1495. for (j = 0; j < client_len;) {
  1496. if (server[i] == client[j] &&
  1497. memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
  1498. /* We found a match */
  1499. result = &server[i];
  1500. status = OPENSSL_NPN_NEGOTIATED;
  1501. goto found;
  1502. }
  1503. j += client[j];
  1504. j++;
  1505. }
  1506. i += server[i];
  1507. i++;
  1508. }
  1509. /* There's no overlap between our protocols and the server's list. */
  1510. result = client;
  1511. status = OPENSSL_NPN_NO_OVERLAP;
  1512. found:
  1513. *out = (uint8_t *)result + 1;
  1514. *out_len = result[0];
  1515. return status;
  1516. }
  1517. void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
  1518. unsigned *out_len) {
  1519. *out_data = ssl->s3->next_proto_negotiated;
  1520. if (*out_data == NULL) {
  1521. *out_len = 0;
  1522. } else {
  1523. *out_len = ssl->s3->next_proto_negotiated_len;
  1524. }
  1525. }
  1526. void SSL_CTX_set_next_protos_advertised_cb(
  1527. SSL_CTX *ctx,
  1528. int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
  1529. void *arg) {
  1530. ctx->next_protos_advertised_cb = cb;
  1531. ctx->next_protos_advertised_cb_arg = arg;
  1532. }
  1533. void SSL_CTX_set_next_proto_select_cb(
  1534. SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
  1535. const uint8_t *in, unsigned in_len, void *arg),
  1536. void *arg) {
  1537. ctx->next_proto_select_cb = cb;
  1538. ctx->next_proto_select_cb_arg = arg;
  1539. }
  1540. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
  1541. unsigned protos_len) {
  1542. OPENSSL_free(ctx->alpn_client_proto_list);
  1543. ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1544. if (!ctx->alpn_client_proto_list) {
  1545. return 1;
  1546. }
  1547. ctx->alpn_client_proto_list_len = protos_len;
  1548. return 0;
  1549. }
  1550. int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
  1551. OPENSSL_free(ssl->alpn_client_proto_list);
  1552. ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1553. if (!ssl->alpn_client_proto_list) {
  1554. return 1;
  1555. }
  1556. ssl->alpn_client_proto_list_len = protos_len;
  1557. return 0;
  1558. }
  1559. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
  1560. int (*cb)(SSL *ssl, const uint8_t **out,
  1561. uint8_t *out_len, const uint8_t *in,
  1562. unsigned in_len, void *arg),
  1563. void *arg) {
  1564. ctx->alpn_select_cb = cb;
  1565. ctx->alpn_select_cb_arg = arg;
  1566. }
  1567. void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
  1568. unsigned *out_len) {
  1569. *out_data = NULL;
  1570. if (ssl->s3) {
  1571. *out_data = ssl->s3->alpn_selected;
  1572. }
  1573. if (*out_data == NULL) {
  1574. *out_len = 0;
  1575. } else {
  1576. *out_len = ssl->s3->alpn_selected_len;
  1577. }
  1578. }
  1579. int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
  1580. ctx->tlsext_channel_id_enabled = 1;
  1581. return 1;
  1582. }
  1583. int SSL_enable_tls_channel_id(SSL *ssl) {
  1584. ssl->tlsext_channel_id_enabled = 1;
  1585. return 1;
  1586. }
  1587. static int is_p256_key(EVP_PKEY *private_key) {
  1588. const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key);
  1589. return ec_key != NULL &&
  1590. EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) ==
  1591. NID_X9_62_prime256v1;
  1592. }
  1593. int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
  1594. if (!is_p256_key(private_key)) {
  1595. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
  1596. return 0;
  1597. }
  1598. EVP_PKEY_free(ctx->tlsext_channel_id_private);
  1599. EVP_PKEY_up_ref(private_key);
  1600. ctx->tlsext_channel_id_private = private_key;
  1601. ctx->tlsext_channel_id_enabled = 1;
  1602. return 1;
  1603. }
  1604. int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
  1605. if (!is_p256_key(private_key)) {
  1606. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
  1607. return 0;
  1608. }
  1609. EVP_PKEY_free(ssl->tlsext_channel_id_private);
  1610. EVP_PKEY_up_ref(private_key);
  1611. ssl->tlsext_channel_id_private = private_key;
  1612. ssl->tlsext_channel_id_enabled = 1;
  1613. return 1;
  1614. }
  1615. size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
  1616. if (!ssl->s3->tlsext_channel_id_valid) {
  1617. return 0;
  1618. }
  1619. memcpy(out, ssl->s3->tlsext_channel_id, (max_out < 64) ? max_out : 64);
  1620. return 64;
  1621. }
  1622. void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
  1623. int (*cb)(X509_STORE_CTX *store_ctx,
  1624. void *arg),
  1625. void *arg) {
  1626. ctx->app_verify_callback = cb;
  1627. ctx->app_verify_arg = arg;
  1628. }
  1629. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
  1630. int (*cb)(int, X509_STORE_CTX *)) {
  1631. ctx->verify_mode = mode;
  1632. ctx->default_verify_callback = cb;
  1633. }
  1634. void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
  1635. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  1636. }
  1637. void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
  1638. void *arg) {
  1639. ssl_cert_set_cert_cb(ctx->cert, cb, arg);
  1640. }
  1641. void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
  1642. ssl_cert_set_cert_cb(ssl->cert, cb, arg);
  1643. }
  1644. size_t SSL_get0_certificate_types(SSL *ssl, const uint8_t **out_types) {
  1645. if (ssl->server) {
  1646. *out_types = NULL;
  1647. return 0;
  1648. }
  1649. *out_types = ssl->s3->tmp.certificate_types;
  1650. return ssl->s3->tmp.num_certificate_types;
  1651. }
  1652. void ssl_get_compatible_server_ciphers(SSL *ssl, uint32_t *out_mask_k,
  1653. uint32_t *out_mask_a) {
  1654. uint32_t mask_k = 0;
  1655. uint32_t mask_a = 0;
  1656. if (ssl->cert->x509 != NULL && ssl_has_private_key(ssl)) {
  1657. int type = ssl_private_key_type(ssl);
  1658. if (type == NID_rsaEncryption) {
  1659. mask_k |= SSL_kRSA;
  1660. mask_a |= SSL_aRSA;
  1661. } else if (ssl_is_ecdsa_key_type(type)) {
  1662. /* An ECC certificate may be usable for ECDSA cipher suites depending on
  1663. * the key usage extension and on the client's group preferences. */
  1664. X509 *x = ssl->cert->x509;
  1665. /* This call populates extension flags (ex_flags). */
  1666. X509_check_purpose(x, -1, 0);
  1667. int ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
  1668. ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
  1669. : 1;
  1670. if (ecdsa_ok && tls1_check_ec_cert(ssl, x)) {
  1671. mask_a |= SSL_aECDSA;
  1672. }
  1673. }
  1674. }
  1675. if (ssl->cert->dh_tmp != NULL || ssl->cert->dh_tmp_cb != NULL) {
  1676. mask_k |= SSL_kDHE;
  1677. }
  1678. /* Check for a shared group to consider ECDHE ciphers. */
  1679. uint16_t unused;
  1680. if (tls1_get_shared_group(ssl, &unused)) {
  1681. mask_k |= SSL_kECDHE;
  1682. }
  1683. /* CECPQ1 ciphers are always acceptable if supported by both sides. */
  1684. mask_k |= SSL_kCECPQ1;
  1685. /* PSK requires a server callback. */
  1686. if (ssl->psk_server_callback != NULL) {
  1687. mask_k |= SSL_kPSK;
  1688. mask_a |= SSL_aPSK;
  1689. }
  1690. *out_mask_k = mask_k;
  1691. *out_mask_a = mask_a;
  1692. }
  1693. void ssl_update_cache(SSL *ssl, int mode) {
  1694. SSL_CTX *ctx = ssl->initial_ctx;
  1695. /* Never cache sessions with empty session IDs. */
  1696. if (ssl->s3->established_session->session_id_length == 0 ||
  1697. (ctx->session_cache_mode & mode) != mode) {
  1698. return;
  1699. }
  1700. /* Clients never use the internal session cache. */
  1701. int use_internal_cache = ssl->server && !(ctx->session_cache_mode &
  1702. SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1703. /* A client may see new sessions on abbreviated handshakes if the server
  1704. * decides to renew the ticket. Once the handshake is completed, it should be
  1705. * inserted into the cache. */
  1706. if (ssl->s3->established_session != ssl->session ||
  1707. (!ssl->server && ssl->tlsext_ticket_expected)) {
  1708. if (use_internal_cache) {
  1709. SSL_CTX_add_session(ctx, ssl->s3->established_session);
  1710. }
  1711. if (ctx->new_session_cb != NULL) {
  1712. SSL_SESSION_up_ref(ssl->s3->established_session);
  1713. if (!ctx->new_session_cb(ssl, ssl->s3->established_session)) {
  1714. /* |new_session_cb|'s return value signals whether it took ownership. */
  1715. SSL_SESSION_free(ssl->s3->established_session);
  1716. }
  1717. }
  1718. }
  1719. if (use_internal_cache &&
  1720. !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) {
  1721. /* Automatically flush the internal session cache every 255 connections. */
  1722. int flush_cache = 0;
  1723. CRYPTO_MUTEX_lock_write(&ctx->lock);
  1724. ctx->handshakes_since_cache_flush++;
  1725. if (ctx->handshakes_since_cache_flush >= 255) {
  1726. flush_cache = 1;
  1727. ctx->handshakes_since_cache_flush = 0;
  1728. }
  1729. CRYPTO_MUTEX_unlock_write(&ctx->lock);
  1730. if (flush_cache) {
  1731. struct timeval now;
  1732. ssl_get_current_time(ssl, &now);
  1733. SSL_CTX_flush_sessions(ctx, (long)now.tv_sec);
  1734. }
  1735. }
  1736. }
  1737. static const char *ssl_get_version(int version) {
  1738. switch (version) {
  1739. case TLS1_3_VERSION:
  1740. return "TLSv1.3";
  1741. case TLS1_2_VERSION:
  1742. return "TLSv1.2";
  1743. case TLS1_1_VERSION:
  1744. return "TLSv1.1";
  1745. case TLS1_VERSION:
  1746. return "TLSv1";
  1747. case SSL3_VERSION:
  1748. return "SSLv3";
  1749. case DTLS1_VERSION:
  1750. return "DTLSv1";
  1751. case DTLS1_2_VERSION:
  1752. return "DTLSv1.2";
  1753. default:
  1754. return "unknown";
  1755. }
  1756. }
  1757. const char *SSL_get_version(const SSL *ssl) {
  1758. return ssl_get_version(ssl->version);
  1759. }
  1760. const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
  1761. return ssl_get_version(session->ssl_version);
  1762. }
  1763. X509 *SSL_get_certificate(const SSL *ssl) {
  1764. if (ssl->cert != NULL) {
  1765. return ssl->cert->x509;
  1766. }
  1767. return NULL;
  1768. }
  1769. EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
  1770. if (ssl->cert != NULL) {
  1771. return ssl->cert->privatekey;
  1772. }
  1773. return NULL;
  1774. }
  1775. X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
  1776. if (ctx->cert != NULL) {
  1777. return ctx->cert->x509;
  1778. }
  1779. return NULL;
  1780. }
  1781. EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
  1782. if (ctx->cert != NULL) {
  1783. return ctx->cert->privatekey;
  1784. }
  1785. return NULL;
  1786. }
  1787. const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
  1788. if (ssl->s3->aead_write_ctx == NULL) {
  1789. return NULL;
  1790. }
  1791. return ssl->s3->aead_write_ctx->cipher;
  1792. }
  1793. int SSL_session_reused(const SSL *ssl) {
  1794. return ssl->s3->session_reused;
  1795. }
  1796. const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
  1797. const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
  1798. int *SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
  1799. int ssl_is_wbio_buffered(const SSL *ssl) {
  1800. return ssl->bbio != NULL;
  1801. }
  1802. int ssl_init_wbio_buffer(SSL *ssl) {
  1803. if (ssl->bbio != NULL) {
  1804. /* Already buffered. */
  1805. assert(ssl->bbio == ssl->wbio);
  1806. return 1;
  1807. }
  1808. BIO *bbio = BIO_new(BIO_f_buffer());
  1809. if (bbio == NULL ||
  1810. !BIO_set_read_buffer_size(bbio, 1)) {
  1811. BIO_free(bbio);
  1812. return 0;
  1813. }
  1814. ssl->bbio = bbio;
  1815. ssl->wbio = BIO_push(bbio, ssl->wbio);
  1816. return 1;
  1817. }
  1818. void ssl_free_wbio_buffer(SSL *ssl) {
  1819. if (ssl->bbio == NULL) {
  1820. return;
  1821. }
  1822. assert(ssl->bbio == ssl->wbio);
  1823. ssl->wbio = BIO_pop(ssl->wbio);
  1824. BIO_free(ssl->bbio);
  1825. ssl->bbio = NULL;
  1826. }
  1827. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
  1828. ctx->quiet_shutdown = (mode != 0);
  1829. }
  1830. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
  1831. return ctx->quiet_shutdown;
  1832. }
  1833. void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
  1834. ssl->quiet_shutdown = (mode != 0);
  1835. }
  1836. int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
  1837. void SSL_set_shutdown(SSL *ssl, int mode) {
  1838. /* It is an error to clear any bits that have already been set. (We can't try
  1839. * to get a second close_notify or send two.) */
  1840. assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
  1841. if (mode & SSL_RECEIVED_SHUTDOWN &&
  1842. ssl->s3->recv_shutdown == ssl_shutdown_none) {
  1843. ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
  1844. }
  1845. if (mode & SSL_SENT_SHUTDOWN &&
  1846. ssl->s3->send_shutdown == ssl_shutdown_none) {
  1847. ssl->s3->send_shutdown = ssl_shutdown_close_notify;
  1848. }
  1849. }
  1850. int SSL_get_shutdown(const SSL *ssl) {
  1851. int ret = 0;
  1852. if (ssl->s3->recv_shutdown != ssl_shutdown_none) {
  1853. /* Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
  1854. * and fatal alert. */
  1855. ret |= SSL_RECEIVED_SHUTDOWN;
  1856. }
  1857. if (ssl->s3->send_shutdown == ssl_shutdown_close_notify) {
  1858. /* Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify. */
  1859. ret |= SSL_SENT_SHUTDOWN;
  1860. }
  1861. return ret;
  1862. }
  1863. int SSL_version(const SSL *ssl) { return ssl->version; }
  1864. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
  1865. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
  1866. if (ssl->ctx == ctx) {
  1867. return ssl->ctx;
  1868. }
  1869. if (ctx == NULL) {
  1870. ctx = ssl->initial_ctx;
  1871. }
  1872. ssl_cert_free(ssl->cert);
  1873. ssl->cert = ssl_cert_dup(ctx->cert);
  1874. CRYPTO_refcount_inc(&ctx->references);
  1875. SSL_CTX_free(ssl->ctx); /* decrement reference count */
  1876. ssl->ctx = ctx;
  1877. ssl->sid_ctx_length = ctx->sid_ctx_length;
  1878. assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
  1879. memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
  1880. return ssl->ctx;
  1881. }
  1882. int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
  1883. return X509_STORE_set_default_paths(ctx->cert_store);
  1884. }
  1885. int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file,
  1886. const char *ca_dir) {
  1887. return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir);
  1888. }
  1889. void SSL_set_info_callback(SSL *ssl,
  1890. void (*cb)(const SSL *ssl, int type, int value)) {
  1891. ssl->info_callback = cb;
  1892. }
  1893. void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
  1894. int value) {
  1895. return ssl->info_callback;
  1896. }
  1897. int SSL_state(const SSL *ssl) { return ssl->state; }
  1898. void SSL_set_state(SSL *ssl, int state) { }
  1899. char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
  1900. if (len <= 0) {
  1901. return NULL;
  1902. }
  1903. buf[0] = '\0';
  1904. return buf;
  1905. }
  1906. void SSL_set_verify_result(SSL *ssl, long result) {
  1907. if (result != X509_V_OK) {
  1908. abort();
  1909. }
  1910. }
  1911. long SSL_get_verify_result(const SSL *ssl) {
  1912. SSL_SESSION *session = SSL_get_session(ssl);
  1913. if (session == NULL) {
  1914. return X509_V_ERR_INVALID_CALL;
  1915. }
  1916. return session->verify_result;
  1917. }
  1918. int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  1919. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
  1920. int index;
  1921. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
  1922. dup_func, free_func)) {
  1923. return -1;
  1924. }
  1925. return index;
  1926. }
  1927. int SSL_set_ex_data(SSL *ssl, int idx, void *arg) {
  1928. return CRYPTO_set_ex_data(&ssl->ex_data, idx, arg);
  1929. }
  1930. void *SSL_get_ex_data(const SSL *ssl, int idx) {
  1931. return CRYPTO_get_ex_data(&ssl->ex_data, idx);
  1932. }
  1933. int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  1934. CRYPTO_EX_dup *dup_func,
  1935. CRYPTO_EX_free *free_func) {
  1936. int index;
  1937. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
  1938. dup_func, free_func)) {
  1939. return -1;
  1940. }
  1941. return index;
  1942. }
  1943. int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *arg) {
  1944. return CRYPTO_set_ex_data(&ctx->ex_data, idx, arg);
  1945. }
  1946. void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
  1947. return CRYPTO_get_ex_data(&ctx->ex_data, idx);
  1948. }
  1949. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
  1950. return ctx->cert_store;
  1951. }
  1952. void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
  1953. X509_STORE_free(ctx->cert_store);
  1954. ctx->cert_store = store;
  1955. }
  1956. int SSL_want(const SSL *ssl) { return ssl->rwstate; }
  1957. void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
  1958. RSA *(*cb)(SSL *ssl, int is_export,
  1959. int keylength)) {
  1960. }
  1961. void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
  1962. int keylength)) {
  1963. }
  1964. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
  1965. DH *(*callback)(SSL *ssl, int is_export,
  1966. int keylength)) {
  1967. ctx->cert->dh_tmp_cb = callback;
  1968. }
  1969. void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
  1970. int keylength)) {
  1971. ssl->cert->dh_tmp_cb = callback;
  1972. }
  1973. unsigned SSL_get_dhe_group_size(const SSL *ssl) {
  1974. /* TODO(davidben): This checks the wrong session if there is a renegotiation in
  1975. * progress. */
  1976. SSL_SESSION *session = SSL_get_session(ssl);
  1977. if (session == NULL ||
  1978. session->cipher == NULL ||
  1979. !SSL_CIPHER_is_DHE(session->cipher)) {
  1980. return 0;
  1981. }
  1982. return session->key_exchange_info;
  1983. }
  1984. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
  1985. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1986. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  1987. return 0;
  1988. }
  1989. OPENSSL_free(ctx->psk_identity_hint);
  1990. if (identity_hint != NULL) {
  1991. ctx->psk_identity_hint = BUF_strdup(identity_hint);
  1992. if (ctx->psk_identity_hint == NULL) {
  1993. return 0;
  1994. }
  1995. } else {
  1996. ctx->psk_identity_hint = NULL;
  1997. }
  1998. return 1;
  1999. }
  2000. int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
  2001. if (ssl == NULL) {
  2002. return 0;
  2003. }
  2004. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  2005. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  2006. return 0;
  2007. }
  2008. /* Clear currently configured hint, if any. */
  2009. OPENSSL_free(ssl->psk_identity_hint);
  2010. ssl->psk_identity_hint = NULL;
  2011. if (identity_hint != NULL) {
  2012. ssl->psk_identity_hint = BUF_strdup(identity_hint);
  2013. if (ssl->psk_identity_hint == NULL) {
  2014. return 0;
  2015. }
  2016. }
  2017. return 1;
  2018. }
  2019. const char *SSL_get_psk_identity_hint(const SSL *ssl) {
  2020. if (ssl == NULL) {
  2021. return NULL;
  2022. }
  2023. return ssl->psk_identity_hint;
  2024. }
  2025. const char *SSL_get_psk_identity(const SSL *ssl) {
  2026. if (ssl == NULL) {
  2027. return NULL;
  2028. }
  2029. SSL_SESSION *session = SSL_get_session(ssl);
  2030. if (session == NULL) {
  2031. return NULL;
  2032. }
  2033. return session->psk_identity;
  2034. }
  2035. void SSL_set_psk_client_callback(
  2036. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  2037. unsigned max_identity_len, uint8_t *psk,
  2038. unsigned max_psk_len)) {
  2039. ssl->psk_client_callback = cb;
  2040. }
  2041. void SSL_CTX_set_psk_client_callback(
  2042. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  2043. unsigned max_identity_len, uint8_t *psk,
  2044. unsigned max_psk_len)) {
  2045. ctx->psk_client_callback = cb;
  2046. }
  2047. void SSL_set_psk_server_callback(
  2048. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
  2049. unsigned max_psk_len)) {
  2050. ssl->psk_server_callback = cb;
  2051. }
  2052. void SSL_CTX_set_psk_server_callback(
  2053. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
  2054. uint8_t *psk, unsigned max_psk_len)) {
  2055. ctx->psk_server_callback = cb;
  2056. }
  2057. void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
  2058. void (*cb)(int write_p, int version,
  2059. int content_type, const void *buf,
  2060. size_t len, SSL *ssl, void *arg)) {
  2061. ctx->msg_callback = cb;
  2062. }
  2063. void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
  2064. ctx->msg_callback_arg = arg;
  2065. }
  2066. void SSL_set_msg_callback(SSL *ssl,
  2067. void (*cb)(int write_p, int version, int content_type,
  2068. const void *buf, size_t len, SSL *ssl,
  2069. void *arg)) {
  2070. ssl->msg_callback = cb;
  2071. }
  2072. void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
  2073. ssl->msg_callback_arg = arg;
  2074. }
  2075. void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
  2076. void (*cb)(const SSL *ssl, const char *line)) {
  2077. ctx->keylog_callback = cb;
  2078. }
  2079. void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl,
  2080. const char *line) {
  2081. return ctx->keylog_callback;
  2082. }
  2083. void SSL_CTX_set_current_time_cb(SSL_CTX *ctx,
  2084. void (*cb)(const SSL *ssl,
  2085. struct timeval *out_clock)) {
  2086. ctx->current_time_cb = cb;
  2087. }
  2088. static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
  2089. static const char hextable[] = "0123456789abcdef";
  2090. uint8_t *out;
  2091. if (!CBB_add_space(cbb, &out, in_len * 2)) {
  2092. return 0;
  2093. }
  2094. for (size_t i = 0; i < in_len; i++) {
  2095. *(out++) = (uint8_t)hextable[in[i] >> 4];
  2096. *(out++) = (uint8_t)hextable[in[i] & 0xf];
  2097. }
  2098. return 1;
  2099. }
  2100. int ssl_log_rsa_client_key_exchange(const SSL *ssl,
  2101. const uint8_t *encrypted_premaster,
  2102. size_t encrypted_premaster_len,
  2103. const uint8_t *premaster,
  2104. size_t premaster_len) {
  2105. if (ssl->ctx->keylog_callback == NULL) {
  2106. return 1;
  2107. }
  2108. if (encrypted_premaster_len < 8) {
  2109. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2110. return 0;
  2111. }
  2112. CBB cbb;
  2113. uint8_t *out;
  2114. size_t out_len;
  2115. if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1) ||
  2116. !CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
  2117. /* Only the first 8 bytes of the encrypted premaster secret are
  2118. * logged. */
  2119. !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
  2120. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  2121. !cbb_add_hex(&cbb, premaster, premaster_len) ||
  2122. !CBB_add_u8(&cbb, 0 /* NUL */) ||
  2123. !CBB_finish(&cbb, &out, &out_len)) {
  2124. CBB_cleanup(&cbb);
  2125. return 0;
  2126. }
  2127. ssl->ctx->keylog_callback(ssl, (const char *)out);
  2128. OPENSSL_free(out);
  2129. return 1;
  2130. }
  2131. int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret,
  2132. size_t secret_len) {
  2133. if (ssl->ctx->keylog_callback == NULL) {
  2134. return 1;
  2135. }
  2136. CBB cbb;
  2137. uint8_t *out;
  2138. size_t out_len;
  2139. if (!CBB_init(&cbb, strlen(label) + 1 + SSL3_RANDOM_SIZE * 2 + 1 +
  2140. secret_len * 2 + 1) ||
  2141. !CBB_add_bytes(&cbb, (const uint8_t *)label, strlen(label)) ||
  2142. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  2143. !cbb_add_hex(&cbb, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  2144. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  2145. !cbb_add_hex(&cbb, secret, secret_len) ||
  2146. !CBB_add_u8(&cbb, 0 /* NUL */) ||
  2147. !CBB_finish(&cbb, &out, &out_len)) {
  2148. CBB_cleanup(&cbb);
  2149. return 0;
  2150. }
  2151. ssl->ctx->keylog_callback(ssl, (const char *)out);
  2152. OPENSSL_free(out);
  2153. return 1;
  2154. }
  2155. int SSL_is_init_finished(const SSL *ssl) {
  2156. return ssl->state == SSL_ST_OK;
  2157. }
  2158. int SSL_in_init(const SSL *ssl) {
  2159. return (ssl->state & SSL_ST_INIT) != 0;
  2160. }
  2161. int SSL_in_false_start(const SSL *ssl) {
  2162. return ssl->s3->tmp.in_false_start;
  2163. }
  2164. int SSL_cutthrough_complete(const SSL *ssl) {
  2165. return SSL_in_false_start(ssl);
  2166. }
  2167. void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
  2168. size_t *ssl_session_size) {
  2169. *ssl_size = sizeof(SSL);
  2170. *ssl_ctx_size = sizeof(SSL_CTX);
  2171. *ssl_session_size = sizeof(SSL_SESSION);
  2172. }
  2173. int ssl3_can_false_start(const SSL *ssl) {
  2174. const SSL_CIPHER *const cipher = SSL_get_current_cipher(ssl);
  2175. /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
  2176. return !SSL_is_dtls(ssl) &&
  2177. SSL_version(ssl) == TLS1_2_VERSION &&
  2178. (ssl->s3->alpn_selected || ssl->s3->next_proto_neg_seen) &&
  2179. cipher != NULL &&
  2180. (cipher->algorithm_mkey == SSL_kECDHE ||
  2181. cipher->algorithm_mkey == SSL_kCECPQ1) &&
  2182. cipher->algorithm_mac == SSL_AEAD;
  2183. }
  2184. const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
  2185. switch (version) {
  2186. case SSL3_VERSION:
  2187. return &SSLv3_enc_data;
  2188. case TLS1_VERSION:
  2189. case TLS1_1_VERSION:
  2190. case TLS1_2_VERSION:
  2191. case TLS1_3_VERSION:
  2192. return &TLSv1_enc_data;
  2193. default:
  2194. return NULL;
  2195. }
  2196. }
  2197. const struct {
  2198. uint16_t version;
  2199. uint32_t flag;
  2200. } kVersions[] = {
  2201. {SSL3_VERSION, SSL_OP_NO_SSLv3},
  2202. {TLS1_VERSION, SSL_OP_NO_TLSv1},
  2203. {TLS1_1_VERSION, SSL_OP_NO_TLSv1_1},
  2204. {TLS1_2_VERSION, SSL_OP_NO_TLSv1_2},
  2205. {TLS1_3_VERSION, SSL_OP_NO_TLSv1_3},
  2206. };
  2207. static const size_t kVersionsLen = OPENSSL_ARRAY_SIZE(kVersions);
  2208. int ssl_get_full_version_range(const SSL *ssl, uint16_t *out_min_version,
  2209. uint16_t *out_fallback_version,
  2210. uint16_t *out_max_version) {
  2211. /* For historical reasons, |SSL_OP_NO_DTLSv1| aliases |SSL_OP_NO_TLSv1|, but
  2212. * DTLS 1.0 should be mapped to TLS 1.1. */
  2213. uint32_t options = ssl->options;
  2214. if (SSL_is_dtls(ssl)) {
  2215. options &= ~SSL_OP_NO_TLSv1_1;
  2216. if (options & SSL_OP_NO_DTLSv1) {
  2217. options |= SSL_OP_NO_TLSv1_1;
  2218. }
  2219. }
  2220. uint16_t min_version = ssl->min_version;
  2221. uint16_t max_version = ssl->max_version;
  2222. /* Bound the range to only those implemented in this protocol. */
  2223. if (min_version < ssl->method->min_version) {
  2224. min_version = ssl->method->min_version;
  2225. }
  2226. if (max_version > ssl->method->max_version) {
  2227. max_version = ssl->method->max_version;
  2228. }
  2229. /* OpenSSL's API for controlling versions entails blacklisting individual
  2230. * protocols. This has two problems. First, on the client, the protocol can
  2231. * only express a contiguous range of versions. Second, a library consumer
  2232. * trying to set a maximum version cannot disable protocol versions that get
  2233. * added in a future version of the library.
  2234. *
  2235. * To account for both of these, OpenSSL interprets the client-side bitmask
  2236. * as a min/max range by picking the lowest contiguous non-empty range of
  2237. * enabled protocols. Note that this means it is impossible to set a maximum
  2238. * version of the higest supported TLS version in a future-proof way. */
  2239. int any_enabled = 0;
  2240. for (size_t i = 0; i < kVersionsLen; i++) {
  2241. /* Only look at the versions already enabled. */
  2242. if (min_version > kVersions[i].version) {
  2243. continue;
  2244. }
  2245. if (max_version < kVersions[i].version) {
  2246. break;
  2247. }
  2248. if (!(options & kVersions[i].flag)) {
  2249. /* The minimum version is the first enabled version. */
  2250. if (!any_enabled) {
  2251. any_enabled = 1;
  2252. min_version = kVersions[i].version;
  2253. }
  2254. continue;
  2255. }
  2256. /* If there is a disabled version after the first enabled one, all versions
  2257. * after it are implicitly disabled. */
  2258. if (any_enabled) {
  2259. max_version = kVersions[i-1].version;
  2260. break;
  2261. }
  2262. }
  2263. uint16_t fallback_version = max_version;
  2264. if (ssl->fallback_version != 0 && ssl->fallback_version < fallback_version) {
  2265. fallback_version = ssl->fallback_version;
  2266. }
  2267. if (!any_enabled || fallback_version < min_version) {
  2268. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
  2269. return 0;
  2270. }
  2271. *out_min_version = min_version;
  2272. *out_fallback_version = fallback_version;
  2273. *out_max_version = max_version;
  2274. return 1;
  2275. }
  2276. int ssl_get_version_range(const SSL *ssl, uint16_t *out_min_version,
  2277. uint16_t *out_effective_max_version) {
  2278. /* This function returns the effective maximum version and not the fallback
  2279. * version. */
  2280. uint16_t real_max_version_unused;
  2281. return ssl_get_full_version_range(ssl, out_min_version,
  2282. out_effective_max_version,
  2283. &real_max_version_unused);
  2284. }
  2285. uint16_t ssl3_protocol_version(const SSL *ssl) {
  2286. assert(ssl->s3->have_version);
  2287. return ssl->method->version_from_wire(ssl->version);
  2288. }
  2289. int SSL_is_server(const SSL *ssl) { return ssl->server; }
  2290. int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; }
  2291. void SSL_CTX_set_select_certificate_cb(
  2292. SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
  2293. ctx->select_certificate_cb = cb;
  2294. }
  2295. void SSL_CTX_set_dos_protection_cb(
  2296. SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
  2297. ctx->dos_protection_cb = cb;
  2298. }
  2299. void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
  2300. ssl->renegotiate_mode = mode;
  2301. }
  2302. void SSL_set_reject_peer_renegotiations(SSL *ssl, int reject) {
  2303. SSL_set_renegotiate_mode(
  2304. ssl, reject ? ssl_renegotiate_never : ssl_renegotiate_freely);
  2305. }
  2306. int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
  2307. const uint8_t **out_write_iv, size_t *out_iv_len) {
  2308. if (ssl->s3->aead_read_ctx == NULL || ssl->s3->aead_write_ctx == NULL) {
  2309. return 0;
  2310. }
  2311. size_t write_iv_len;
  2312. if (!EVP_AEAD_CTX_get_iv(&ssl->s3->aead_read_ctx->ctx, out_read_iv,
  2313. out_iv_len) ||
  2314. !EVP_AEAD_CTX_get_iv(&ssl->s3->aead_write_ctx->ctx, out_write_iv,
  2315. &write_iv_len) ||
  2316. *out_iv_len != write_iv_len) {
  2317. return 0;
  2318. }
  2319. return 1;
  2320. }
  2321. static uint64_t be_to_u64(const uint8_t in[8]) {
  2322. return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) |
  2323. (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) |
  2324. (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) |
  2325. (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]);
  2326. }
  2327. uint64_t SSL_get_read_sequence(const SSL *ssl) {
  2328. /* TODO(davidben): Internally represent sequence numbers as uint64_t. */
  2329. if (SSL_is_dtls(ssl)) {
  2330. /* max_seq_num already includes the epoch. */
  2331. assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
  2332. return ssl->d1->bitmap.max_seq_num;
  2333. }
  2334. return be_to_u64(ssl->s3->read_sequence);
  2335. }
  2336. uint64_t SSL_get_write_sequence(const SSL *ssl) {
  2337. uint64_t ret = be_to_u64(ssl->s3->write_sequence);
  2338. if (SSL_is_dtls(ssl)) {
  2339. assert((ret >> 48) == 0);
  2340. ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
  2341. }
  2342. return ret;
  2343. }
  2344. uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) {
  2345. return ssl->s3->tmp.peer_signature_algorithm;
  2346. }
  2347. size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2348. if (max_out == 0) {
  2349. return sizeof(ssl->s3->client_random);
  2350. }
  2351. if (max_out > sizeof(ssl->s3->client_random)) {
  2352. max_out = sizeof(ssl->s3->client_random);
  2353. }
  2354. memcpy(out, ssl->s3->client_random, max_out);
  2355. return max_out;
  2356. }
  2357. size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2358. if (max_out == 0) {
  2359. return sizeof(ssl->s3->server_random);
  2360. }
  2361. if (max_out > sizeof(ssl->s3->server_random)) {
  2362. max_out = sizeof(ssl->s3->server_random);
  2363. }
  2364. memcpy(out, ssl->s3->server_random, max_out);
  2365. return max_out;
  2366. }
  2367. const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
  2368. if (!SSL_in_init(ssl)) {
  2369. return NULL;
  2370. }
  2371. return ssl->s3->tmp.new_cipher;
  2372. }
  2373. void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
  2374. ctx->retain_only_sha256_of_client_certs = !!enabled;
  2375. }
  2376. int SSL_clear(SSL *ssl) {
  2377. if (ssl->method == NULL) {
  2378. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_METHOD_SPECIFIED);
  2379. return 0;
  2380. }
  2381. /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
  2382. * |SSL_clear| because it is per-connection state rather than configuration
  2383. * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
  2384. * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
  2385. * |ssl3_new|. */
  2386. ssl->state = SSL_ST_INIT;
  2387. ssl->rwstate = SSL_NOTHING;
  2388. BUF_MEM_free(ssl->init_buf);
  2389. ssl->init_buf = NULL;
  2390. ssl->init_msg = NULL;
  2391. ssl->init_num = 0;
  2392. /* The ssl->d1->mtu is simultaneously configuration (preserved across
  2393. * clear) and connection-specific state (gets reset).
  2394. *
  2395. * TODO(davidben): Avoid this. */
  2396. unsigned mtu = 0;
  2397. if (ssl->d1 != NULL) {
  2398. mtu = ssl->d1->mtu;
  2399. }
  2400. ssl->method->ssl_free(ssl);
  2401. if (!ssl->method->ssl_new(ssl)) {
  2402. return 0;
  2403. }
  2404. if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
  2405. ssl->d1->mtu = mtu;
  2406. }
  2407. ssl->client_version = ssl->version;
  2408. return 1;
  2409. }
  2410. void ssl_do_info_callback(const SSL *ssl, int type, int value) {
  2411. void (*cb)(const SSL *ssl, int type, int value) = NULL;
  2412. if (ssl->info_callback != NULL) {
  2413. cb = ssl->info_callback;
  2414. } else if (ssl->ctx->info_callback != NULL) {
  2415. cb = ssl->ctx->info_callback;
  2416. }
  2417. if (cb != NULL) {
  2418. cb(ssl, type, value);
  2419. }
  2420. }
  2421. void ssl_do_msg_callback(SSL *ssl, int is_write, int version, int content_type,
  2422. const void *buf, size_t len) {
  2423. if (ssl->msg_callback != NULL) {
  2424. ssl->msg_callback(is_write, version, content_type, buf, len, ssl,
  2425. ssl->msg_callback_arg);
  2426. }
  2427. }
  2428. int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
  2429. int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
  2430. int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
  2431. int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
  2432. int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
  2433. int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
  2434. int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
  2435. int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
  2436. int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
  2437. int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
  2438. int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
  2439. int SSL_num_renegotiations(const SSL *ssl) {
  2440. return SSL_total_renegotiations(ssl);
  2441. }
  2442. int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
  2443. int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
  2444. int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
  2445. int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
  2446. void ERR_load_SSL_strings(void) {}
  2447. void SSL_load_error_strings(void) {}
  2448. int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
  2449. int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
  2450. if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
  2451. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  2452. return 0;
  2453. }
  2454. int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
  2455. return SSL_CTX_set1_curves(ctx, &nid, 1);
  2456. }
  2457. int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
  2458. if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
  2459. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  2460. return 0;
  2461. }
  2462. int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
  2463. return SSL_set1_curves(ssl, &nid, 1);
  2464. }
  2465. void ssl_get_current_time(const SSL *ssl, struct timeval *out_clock) {
  2466. if (ssl->ctx->current_time_cb != NULL) {
  2467. ssl->ctx->current_time_cb(ssl, out_clock);
  2468. return;
  2469. }
  2470. #if defined(OPENSSL_WINDOWS)
  2471. struct _timeb time;
  2472. _ftime(&time);
  2473. out_clock->tv_sec = time.time;
  2474. out_clock->tv_usec = time.millitm * 1000;
  2475. #else
  2476. gettimeofday(out_clock, NULL);
  2477. #endif
  2478. }