Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

handshake_client.cc 61 KiB

Move libssl's internals into the bssl namespace. This is horrible, but everything else I tried was worse. The goal with this CL is to take the extern "C" out of ssl/internal.h and move most symbols to namespace bssl, so we can start using C++ helpers and destructors without worry. Complications: - Public API functions must be extern "C" and match their declaration in ssl.h, which is unnamespaced. C++ really does not want you to interleave namespaced and unnamespaced things. One can actually write a namespaced extern "C" function, but this means, from C++'s perspective, the function is namespaced. Trying to namespace the public header would worked but ended up too deep a rabbithole. - Our STACK_OF macros do not work right in namespaces. - The typedefs for our exposed but opaque types are visible in the header files and copied into consuming projects as forward declarations. We ultimately want to give SSL a destructor, but clobbering an unnamespaced ssl_st::~ssl_st seems bad manners. - MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL. This CL opts for: - ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This informs the public headers to create forward declarations which are compatible with our namespaces. - For now, C++-defined type FOO ends up at bssl::FOO with a typedef outside. Later I imagine we'll rename many of them. - Internal functions get namespace bssl, so we stop worrying about stomping the tls1_prf symbol. Exported C functions are stuck as they are. Rather than try anything weird, bite the bullet and reorder files which have a mix of public and private functions. I expect that over time, the public functions will become fairly small as we move logic to more idiomatic C++. Files without any public C functions can just be written normally. - To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle in advance of them being made idiomatic C++. Bug: 132 Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581 Reviewed-on: https://boringssl-review.googlesource.com/18124 Reviewed-by: David Benjamin <davidben@google.com>
vor 7 Jahren
Move libssl's internals into the bssl namespace. This is horrible, but everything else I tried was worse. The goal with this CL is to take the extern "C" out of ssl/internal.h and move most symbols to namespace bssl, so we can start using C++ helpers and destructors without worry. Complications: - Public API functions must be extern "C" and match their declaration in ssl.h, which is unnamespaced. C++ really does not want you to interleave namespaced and unnamespaced things. One can actually write a namespaced extern "C" function, but this means, from C++'s perspective, the function is namespaced. Trying to namespace the public header would worked but ended up too deep a rabbithole. - Our STACK_OF macros do not work right in namespaces. - The typedefs for our exposed but opaque types are visible in the header files and copied into consuming projects as forward declarations. We ultimately want to give SSL a destructor, but clobbering an unnamespaced ssl_st::~ssl_st seems bad manners. - MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL. This CL opts for: - ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This informs the public headers to create forward declarations which are compatible with our namespaces. - For now, C++-defined type FOO ends up at bssl::FOO with a typedef outside. Later I imagine we'll rename many of them. - Internal functions get namespace bssl, so we stop worrying about stomping the tls1_prf symbol. Exported C functions are stuck as they are. Rather than try anything weird, bite the bullet and reorder files which have a mix of public and private functions. I expect that over time, the public functions will become fairly small as we move logic to more idiomatic C++. Files without any public C functions can just be written normally. - To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle in advance of them being made idiomatic C++. Bug: 132 Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581 Reviewed-on: https://boringssl-review.googlesource.com/18124 Reviewed-by: David Benjamin <davidben@google.com>
vor 7 Jahren
Revise version negotiation logic on the C side. This is in preparation for upcoming experiments which will require supporting multiple experimental versions of TLS 1.3 with, on the server, the ability to enable multiple variants at once. This means the version <-> wire bijection no longer exists, even when limiting to a single SSL*. Thus version_to_wire is removed and instead we treat the wire version as the canonical version value. There is a mapping from valid wire versions to protocol versions which describe the high-level handshake protocol in use. This mapping is not injective, so uses of version_from_wire are rewritten differently. All the version-munging logic is moved to ssl_versions.c with a master preference list of all TLS and DTLS versions. The legacy version negotiation is converted to the new scheme. The version lists and negotiation are driven by the preference lists and a ssl_supports_version API. To simplify the mess around SSL_SESSION and versions, version_from_wire is now DTLS/TLS-agnostic, with any filtering being done by ssl_supports_version. This is screwy but allows parsing SSL_SESSIONs to sanity-check it and reject all bogus versions in SSL_SESSION. This reduces a mess of error cases. As part of this, the weird logic where ssl->version is set early when sending the ClientHello is removed. The one place where we were relying on this behavior is tweaked to query hs->max_version instead. Change-Id: Ic91b348481ceba94d9ae06d6781187c11adc15b0 Reviewed-on: https://boringssl-review.googlesource.com/17524 Reviewed-by: David Benjamin <davidben@google.com> Commit-Queue: David Benjamin <davidben@google.com>
vor 7 Jahren
Revise version negotiation logic on the C side. This is in preparation for upcoming experiments which will require supporting multiple experimental versions of TLS 1.3 with, on the server, the ability to enable multiple variants at once. This means the version <-> wire bijection no longer exists, even when limiting to a single SSL*. Thus version_to_wire is removed and instead we treat the wire version as the canonical version value. There is a mapping from valid wire versions to protocol versions which describe the high-level handshake protocol in use. This mapping is not injective, so uses of version_from_wire are rewritten differently. All the version-munging logic is moved to ssl_versions.c with a master preference list of all TLS and DTLS versions. The legacy version negotiation is converted to the new scheme. The version lists and negotiation are driven by the preference lists and a ssl_supports_version API. To simplify the mess around SSL_SESSION and versions, version_from_wire is now DTLS/TLS-agnostic, with any filtering being done by ssl_supports_version. This is screwy but allows parsing SSL_SESSIONs to sanity-check it and reject all bogus versions in SSL_SESSION. This reduces a mess of error cases. As part of this, the weird logic where ssl->version is set early when sending the ClientHello is removed. The one place where we were relying on this behavior is tweaked to query hs->max_version instead. Change-Id: Ic91b348481ceba94d9ae06d6781187c11adc15b0 Reviewed-on: https://boringssl-review.googlesource.com/17524 Reviewed-by: David Benjamin <davidben@google.com> Commit-Queue: David Benjamin <davidben@google.com>
vor 7 Jahren
Revise version negotiation logic on the C side. This is in preparation for upcoming experiments which will require supporting multiple experimental versions of TLS 1.3 with, on the server, the ability to enable multiple variants at once. This means the version <-> wire bijection no longer exists, even when limiting to a single SSL*. Thus version_to_wire is removed and instead we treat the wire version as the canonical version value. There is a mapping from valid wire versions to protocol versions which describe the high-level handshake protocol in use. This mapping is not injective, so uses of version_from_wire are rewritten differently. All the version-munging logic is moved to ssl_versions.c with a master preference list of all TLS and DTLS versions. The legacy version negotiation is converted to the new scheme. The version lists and negotiation are driven by the preference lists and a ssl_supports_version API. To simplify the mess around SSL_SESSION and versions, version_from_wire is now DTLS/TLS-agnostic, with any filtering being done by ssl_supports_version. This is screwy but allows parsing SSL_SESSIONs to sanity-check it and reject all bogus versions in SSL_SESSION. This reduces a mess of error cases. As part of this, the weird logic where ssl->version is set early when sending the ClientHello is removed. The one place where we were relying on this behavior is tweaked to query hs->max_version instead. Change-Id: Ic91b348481ceba94d9ae06d6781187c11adc15b0 Reviewed-on: https://boringssl-review.googlesource.com/17524 Reviewed-by: David Benjamin <davidben@google.com> Commit-Queue: David Benjamin <davidben@google.com>
vor 7 Jahren
Revise version negotiation logic on the C side. This is in preparation for upcoming experiments which will require supporting multiple experimental versions of TLS 1.3 with, on the server, the ability to enable multiple variants at once. This means the version <-> wire bijection no longer exists, even when limiting to a single SSL*. Thus version_to_wire is removed and instead we treat the wire version as the canonical version value. There is a mapping from valid wire versions to protocol versions which describe the high-level handshake protocol in use. This mapping is not injective, so uses of version_from_wire are rewritten differently. All the version-munging logic is moved to ssl_versions.c with a master preference list of all TLS and DTLS versions. The legacy version negotiation is converted to the new scheme. The version lists and negotiation are driven by the preference lists and a ssl_supports_version API. To simplify the mess around SSL_SESSION and versions, version_from_wire is now DTLS/TLS-agnostic, with any filtering being done by ssl_supports_version. This is screwy but allows parsing SSL_SESSIONs to sanity-check it and reject all bogus versions in SSL_SESSION. This reduces a mess of error cases. As part of this, the weird logic where ssl->version is set early when sending the ClientHello is removed. The one place where we were relying on this behavior is tweaked to query hs->max_version instead. Change-Id: Ic91b348481ceba94d9ae06d6781187c11adc15b0 Reviewed-on: https://boringssl-review.googlesource.com/17524 Reviewed-by: David Benjamin <davidben@google.com> Commit-Queue: David Benjamin <davidben@google.com>
vor 7 Jahren
Revise version negotiation logic on the C side. This is in preparation for upcoming experiments which will require supporting multiple experimental versions of TLS 1.3 with, on the server, the ability to enable multiple variants at once. This means the version <-> wire bijection no longer exists, even when limiting to a single SSL*. Thus version_to_wire is removed and instead we treat the wire version as the canonical version value. There is a mapping from valid wire versions to protocol versions which describe the high-level handshake protocol in use. This mapping is not injective, so uses of version_from_wire are rewritten differently. All the version-munging logic is moved to ssl_versions.c with a master preference list of all TLS and DTLS versions. The legacy version negotiation is converted to the new scheme. The version lists and negotiation are driven by the preference lists and a ssl_supports_version API. To simplify the mess around SSL_SESSION and versions, version_from_wire is now DTLS/TLS-agnostic, with any filtering being done by ssl_supports_version. This is screwy but allows parsing SSL_SESSIONs to sanity-check it and reject all bogus versions in SSL_SESSION. This reduces a mess of error cases. As part of this, the weird logic where ssl->version is set early when sending the ClientHello is removed. The one place where we were relying on this behavior is tweaked to query hs->max_version instead. Change-Id: Ic91b348481ceba94d9ae06d6781187c11adc15b0 Reviewed-on: https://boringssl-review.googlesource.com/17524 Reviewed-by: David Benjamin <davidben@google.com> Commit-Queue: David Benjamin <davidben@google.com>
vor 7 Jahren
Revise version negotiation logic on the C side. This is in preparation for upcoming experiments which will require supporting multiple experimental versions of TLS 1.3 with, on the server, the ability to enable multiple variants at once. This means the version <-> wire bijection no longer exists, even when limiting to a single SSL*. Thus version_to_wire is removed and instead we treat the wire version as the canonical version value. There is a mapping from valid wire versions to protocol versions which describe the high-level handshake protocol in use. This mapping is not injective, so uses of version_from_wire are rewritten differently. All the version-munging logic is moved to ssl_versions.c with a master preference list of all TLS and DTLS versions. The legacy version negotiation is converted to the new scheme. The version lists and negotiation are driven by the preference lists and a ssl_supports_version API. To simplify the mess around SSL_SESSION and versions, version_from_wire is now DTLS/TLS-agnostic, with any filtering being done by ssl_supports_version. This is screwy but allows parsing SSL_SESSIONs to sanity-check it and reject all bogus versions in SSL_SESSION. This reduces a mess of error cases. As part of this, the weird logic where ssl->version is set early when sending the ClientHello is removed. The one place where we were relying on this behavior is tweaked to query hs->max_version instead. Change-Id: Ic91b348481ceba94d9ae06d6781187c11adc15b0 Reviewed-on: https://boringssl-review.googlesource.com/17524 Reviewed-by: David Benjamin <davidben@google.com> Commit-Queue: David Benjamin <davidben@google.com>
vor 7 Jahren
Tighten up EMS resumption behaviour. The client and server both have to decide on behaviour when resuming a session where the EMS state of the session doesn't match the EMS state as exchanged in the handshake. Original handshake | No Yes ------+-------------------------------------------------------------- | R | Server: ok [1] Server: abort [3] e No | Client: ok [2] Client: abort [4] s | u | m | e | Yes | Server: don't resume No problem | Client: abort; server | shouldn't have resumed [1] Servers want to accept legacy clients. The draft[5] says that resumptions SHOULD be rejected so that Triple-Handshake can't be done, but we'll rather enforce that EMS was used when using tls-unique etc. [2] The draft[5] says that even the initial handshake should be aborted if the server doesn't support EMS, but we need to be able to talk to the world. [3] This is a very weird case where a client has regressed without flushing the session cache. Hopefully we can be strict and reject these. [4] This can happen when a server-farm shares a session cache but frontends are not all updated at once. If Chrome is strict here then hopefully we can prevent any servers from existing that will try to resume an EMS session that they don't understand. OpenSSL appears to be ok here: https://www.ietf.org/mail-archive/web/tls/current/msg16570.html [5] https://tools.ietf.org/html/draft-ietf-tls-session-hash-05#section-5.2 BUG=492200 Change-Id: Ie1225a3960d49117b05eefa5a36263d8e556e467 Reviewed-on: https://boringssl-review.googlesource.com/4981 Reviewed-by: Adam Langley <agl@google.com>
vor 9 Jahren
Tighten up EMS resumption behaviour. The client and server both have to decide on behaviour when resuming a session where the EMS state of the session doesn't match the EMS state as exchanged in the handshake. Original handshake | No Yes ------+-------------------------------------------------------------- | R | Server: ok [1] Server: abort [3] e No | Client: ok [2] Client: abort [4] s | u | m | e | Yes | Server: don't resume No problem | Client: abort; server | shouldn't have resumed [1] Servers want to accept legacy clients. The draft[5] says that resumptions SHOULD be rejected so that Triple-Handshake can't be done, but we'll rather enforce that EMS was used when using tls-unique etc. [2] The draft[5] says that even the initial handshake should be aborted if the server doesn't support EMS, but we need to be able to talk to the world. [3] This is a very weird case where a client has regressed without flushing the session cache. Hopefully we can be strict and reject these. [4] This can happen when a server-farm shares a session cache but frontends are not all updated at once. If Chrome is strict here then hopefully we can prevent any servers from existing that will try to resume an EMS session that they don't understand. OpenSSL appears to be ok here: https://www.ietf.org/mail-archive/web/tls/current/msg16570.html [5] https://tools.ietf.org/html/draft-ietf-tls-session-hash-05#section-5.2 BUG=492200 Change-Id: Ie1225a3960d49117b05eefa5a36263d8e556e467 Reviewed-on: https://boringssl-review.googlesource.com/4981 Reviewed-by: Adam Langley <agl@google.com>
vor 9 Jahren
Tighten up EMS resumption behaviour. The client and server both have to decide on behaviour when resuming a session where the EMS state of the session doesn't match the EMS state as exchanged in the handshake. Original handshake | No Yes ------+-------------------------------------------------------------- | R | Server: ok [1] Server: abort [3] e No | Client: ok [2] Client: abort [4] s | u | m | e | Yes | Server: don't resume No problem | Client: abort; server | shouldn't have resumed [1] Servers want to accept legacy clients. The draft[5] says that resumptions SHOULD be rejected so that Triple-Handshake can't be done, but we'll rather enforce that EMS was used when using tls-unique etc. [2] The draft[5] says that even the initial handshake should be aborted if the server doesn't support EMS, but we need to be able to talk to the world. [3] This is a very weird case where a client has regressed without flushing the session cache. Hopefully we can be strict and reject these. [4] This can happen when a server-farm shares a session cache but frontends are not all updated at once. If Chrome is strict here then hopefully we can prevent any servers from existing that will try to resume an EMS session that they don't understand. OpenSSL appears to be ok here: https://www.ietf.org/mail-archive/web/tls/current/msg16570.html [5] https://tools.ietf.org/html/draft-ietf-tls-session-hash-05#section-5.2 BUG=492200 Change-Id: Ie1225a3960d49117b05eefa5a36263d8e556e467 Reviewed-on: https://boringssl-review.googlesource.com/4981 Reviewed-by: Adam Langley <agl@google.com>
vor 9 Jahren
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>
vor 8 Jahren
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>
vor 8 Jahren
Don't use the buffer BIO in TLS. On the TLS side, we introduce a running buffer of ciphertext. Queuing up pending data consists of encrypting the record into the buffer. This effectively reimplements what the buffer BIO was doing previously, but this resizes to fit the whole flight. As part of this, rename all the functions to add to the pending flight to be more uniform. This CL proposes "add_foo" to add to the pending flight and "flush_flight" to drain it. We add an add_alert hook for alerts but, for now, only the SSL 3.0 warning alert (sent mid-handshake) uses this mechanism. Later work will push this down to the rest of the write path so closure alerts use it too, as in DTLS. The intended end state is that all the ssl_buffer.c and wpend_ret logic will only be used for application data and eventually optionally replaced by the in-place API, while all "incidental" data will be handled internally. For now, the two buffers are mutually exclusive. Moving closure alerts to "incidentals" will change this, but flushing application data early is tricky due to wpend_ret. (If we call ssl_write_buffer_flush, do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That too is all left alone in this change. To keep the diff down, write_message is retained for now and will be removed from the state machines in a follow-up change. BUG=72 Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d Reviewed-on: https://boringssl-review.googlesource.com/13224 Reviewed-by: Adam Langley <agl@google.com>
vor 7 Jahren
Don't use the buffer BIO in TLS. On the TLS side, we introduce a running buffer of ciphertext. Queuing up pending data consists of encrypting the record into the buffer. This effectively reimplements what the buffer BIO was doing previously, but this resizes to fit the whole flight. As part of this, rename all the functions to add to the pending flight to be more uniform. This CL proposes "add_foo" to add to the pending flight and "flush_flight" to drain it. We add an add_alert hook for alerts but, for now, only the SSL 3.0 warning alert (sent mid-handshake) uses this mechanism. Later work will push this down to the rest of the write path so closure alerts use it too, as in DTLS. The intended end state is that all the ssl_buffer.c and wpend_ret logic will only be used for application data and eventually optionally replaced by the in-place API, while all "incidental" data will be handled internally. For now, the two buffers are mutually exclusive. Moving closure alerts to "incidentals" will change this, but flushing application data early is tricky due to wpend_ret. (If we call ssl_write_buffer_flush, do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That too is all left alone in this change. To keep the diff down, write_message is retained for now and will be removed from the state machines in a follow-up change. BUG=72 Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d Reviewed-on: https://boringssl-review.googlesource.com/13224 Reviewed-by: Adam Langley <agl@google.com>
vor 7 Jahren
Move libssl's internals into the bssl namespace. This is horrible, but everything else I tried was worse. The goal with this CL is to take the extern "C" out of ssl/internal.h and move most symbols to namespace bssl, so we can start using C++ helpers and destructors without worry. Complications: - Public API functions must be extern "C" and match their declaration in ssl.h, which is unnamespaced. C++ really does not want you to interleave namespaced and unnamespaced things. One can actually write a namespaced extern "C" function, but this means, from C++'s perspective, the function is namespaced. Trying to namespace the public header would worked but ended up too deep a rabbithole. - Our STACK_OF macros do not work right in namespaces. - The typedefs for our exposed but opaque types are visible in the header files and copied into consuming projects as forward declarations. We ultimately want to give SSL a destructor, but clobbering an unnamespaced ssl_st::~ssl_st seems bad manners. - MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL. This CL opts for: - ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This informs the public headers to create forward declarations which are compatible with our namespaces. - For now, C++-defined type FOO ends up at bssl::FOO with a typedef outside. Later I imagine we'll rename many of them. - Internal functions get namespace bssl, so we stop worrying about stomping the tls1_prf symbol. Exported C functions are stuck as they are. Rather than try anything weird, bite the bullet and reorder files which have a mix of public and private functions. I expect that over time, the public functions will become fairly small as we move logic to more idiomatic C++. Files without any public C functions can just be written normally. - To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle in advance of them being made idiomatic C++. Bug: 132 Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581 Reviewed-on: https://boringssl-review.googlesource.com/18124 Reviewed-by: David Benjamin <davidben@google.com>
vor 7 Jahren
Move libssl's internals into the bssl namespace. This is horrible, but everything else I tried was worse. The goal with this CL is to take the extern "C" out of ssl/internal.h and move most symbols to namespace bssl, so we can start using C++ helpers and destructors without worry. Complications: - Public API functions must be extern "C" and match their declaration in ssl.h, which is unnamespaced. C++ really does not want you to interleave namespaced and unnamespaced things. One can actually write a namespaced extern "C" function, but this means, from C++'s perspective, the function is namespaced. Trying to namespace the public header would worked but ended up too deep a rabbithole. - Our STACK_OF macros do not work right in namespaces. - The typedefs for our exposed but opaque types are visible in the header files and copied into consuming projects as forward declarations. We ultimately want to give SSL a destructor, but clobbering an unnamespaced ssl_st::~ssl_st seems bad manners. - MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL. This CL opts for: - ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This informs the public headers to create forward declarations which are compatible with our namespaces. - For now, C++-defined type FOO ends up at bssl::FOO with a typedef outside. Later I imagine we'll rename many of them. - Internal functions get namespace bssl, so we stop worrying about stomping the tls1_prf symbol. Exported C functions are stuck as they are. Rather than try anything weird, bite the bullet and reorder files which have a mix of public and private functions. I expect that over time, the public functions will become fairly small as we move logic to more idiomatic C++. Files without any public C functions can just be written normally. - To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle in advance of them being made idiomatic C++. Bug: 132 Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581 Reviewed-on: https://boringssl-review.googlesource.com/18124 Reviewed-by: David Benjamin <davidben@google.com>
vor 7 Jahren
Simplify ssl_private_key_* state machine points. The original motivation behind the sign/complete split was to avoid needlessly hashing the input on each pass through the state machine, but we're payload-based now and, in all cases, the payload is either cheap to compute or readily available. (Even the hashing worry was probably unnecessary.) Tweak ssl_private_key_{sign,decrypt} to automatically call ssl_private_key_complete as needed and take advantage of this in the handshake state machines: - TLS 1.3 signing now computes the payload each pass. The payload is small and we're already allocating a comparable-sized buffer each iteration to hold the signature. This shouldn't be a big deal. - TLS 1.2 decryption code still needs two states due to reading the message (fixed in new state machine style), but otherwise it just performs cheap idempotent tasks again. The PSK code is reshuffled to guarantee the callback is not called twice (though this was impossible anyway because we don't support RSA_PSK). - TLS 1.2 CertificateVerify signing is easy as the transcript is readily available. The buffer is released very slightly later, but it shouldn't matter. - TLS 1.2 ServerKeyExchange signing required some reshuffling. Assembling the ServerKeyExchange parameters is moved to the previous state. The signing payload has some randoms prepended. This is cheap enough, but a nuisance in C. Pre-prepend the randoms in hs->server_params. With this change, we are *nearly* rid of the A/B => same function pattern. BUG=128 Change-Id: Iec4fe0be7cfc88a6de027ba2760fae70794ea810 Reviewed-on: https://boringssl-review.googlesource.com/17265 Commit-Queue: David Benjamin <davidben@google.com> Commit-Queue: Steven Valdez <svaldez@google.com> Reviewed-by: Steven Valdez <svaldez@google.com>
vor 7 Jahren
Simplify ssl_private_key_* state machine points. The original motivation behind the sign/complete split was to avoid needlessly hashing the input on each pass through the state machine, but we're payload-based now and, in all cases, the payload is either cheap to compute or readily available. (Even the hashing worry was probably unnecessary.) Tweak ssl_private_key_{sign,decrypt} to automatically call ssl_private_key_complete as needed and take advantage of this in the handshake state machines: - TLS 1.3 signing now computes the payload each pass. The payload is small and we're already allocating a comparable-sized buffer each iteration to hold the signature. This shouldn't be a big deal. - TLS 1.2 decryption code still needs two states due to reading the message (fixed in new state machine style), but otherwise it just performs cheap idempotent tasks again. The PSK code is reshuffled to guarantee the callback is not called twice (though this was impossible anyway because we don't support RSA_PSK). - TLS 1.2 CertificateVerify signing is easy as the transcript is readily available. The buffer is released very slightly later, but it shouldn't matter. - TLS 1.2 ServerKeyExchange signing required some reshuffling. Assembling the ServerKeyExchange parameters is moved to the previous state. The signing payload has some randoms prepended. This is cheap enough, but a nuisance in C. Pre-prepend the randoms in hs->server_params. With this change, we are *nearly* rid of the A/B => same function pattern. BUG=128 Change-Id: Iec4fe0be7cfc88a6de027ba2760fae70794ea810 Reviewed-on: https://boringssl-review.googlesource.com/17265 Commit-Queue: David Benjamin <davidben@google.com> Commit-Queue: Steven Valdez <svaldez@google.com> Reviewed-by: Steven Valdez <svaldez@google.com>
vor 7 Jahren
Simplify ssl_private_key_* state machine points. The original motivation behind the sign/complete split was to avoid needlessly hashing the input on each pass through the state machine, but we're payload-based now and, in all cases, the payload is either cheap to compute or readily available. (Even the hashing worry was probably unnecessary.) Tweak ssl_private_key_{sign,decrypt} to automatically call ssl_private_key_complete as needed and take advantage of this in the handshake state machines: - TLS 1.3 signing now computes the payload each pass. The payload is small and we're already allocating a comparable-sized buffer each iteration to hold the signature. This shouldn't be a big deal. - TLS 1.2 decryption code still needs two states due to reading the message (fixed in new state machine style), but otherwise it just performs cheap idempotent tasks again. The PSK code is reshuffled to guarantee the callback is not called twice (though this was impossible anyway because we don't support RSA_PSK). - TLS 1.2 CertificateVerify signing is easy as the transcript is readily available. The buffer is released very slightly later, but it shouldn't matter. - TLS 1.2 ServerKeyExchange signing required some reshuffling. Assembling the ServerKeyExchange parameters is moved to the previous state. The signing payload has some randoms prepended. This is cheap enough, but a nuisance in C. Pre-prepend the randoms in hs->server_params. With this change, we are *nearly* rid of the A/B => same function pattern. BUG=128 Change-Id: Iec4fe0be7cfc88a6de027ba2760fae70794ea810 Reviewed-on: https://boringssl-review.googlesource.com/17265 Commit-Queue: David Benjamin <davidben@google.com> Commit-Queue: Steven Valdez <svaldez@google.com> Reviewed-by: Steven Valdez <svaldez@google.com>
vor 7 Jahren
Simplify ssl_private_key_* state machine points. The original motivation behind the sign/complete split was to avoid needlessly hashing the input on each pass through the state machine, but we're payload-based now and, in all cases, the payload is either cheap to compute or readily available. (Even the hashing worry was probably unnecessary.) Tweak ssl_private_key_{sign,decrypt} to automatically call ssl_private_key_complete as needed and take advantage of this in the handshake state machines: - TLS 1.3 signing now computes the payload each pass. The payload is small and we're already allocating a comparable-sized buffer each iteration to hold the signature. This shouldn't be a big deal. - TLS 1.2 decryption code still needs two states due to reading the message (fixed in new state machine style), but otherwise it just performs cheap idempotent tasks again. The PSK code is reshuffled to guarantee the callback is not called twice (though this was impossible anyway because we don't support RSA_PSK). - TLS 1.2 CertificateVerify signing is easy as the transcript is readily available. The buffer is released very slightly later, but it shouldn't matter. - TLS 1.2 ServerKeyExchange signing required some reshuffling. Assembling the ServerKeyExchange parameters is moved to the previous state. The signing payload has some randoms prepended. This is cheap enough, but a nuisance in C. Pre-prepend the randoms in hs->server_params. With this change, we are *nearly* rid of the A/B => same function pattern. BUG=128 Change-Id: Iec4fe0be7cfc88a6de027ba2760fae70794ea810 Reviewed-on: https://boringssl-review.googlesource.com/17265 Commit-Queue: David Benjamin <davidben@google.com> Commit-Queue: Steven Valdez <svaldez@google.com> Reviewed-by: Steven Valdez <svaldez@google.com>
vor 7 Jahren
Simplify ssl_private_key_* state machine points. The original motivation behind the sign/complete split was to avoid needlessly hashing the input on each pass through the state machine, but we're payload-based now and, in all cases, the payload is either cheap to compute or readily available. (Even the hashing worry was probably unnecessary.) Tweak ssl_private_key_{sign,decrypt} to automatically call ssl_private_key_complete as needed and take advantage of this in the handshake state machines: - TLS 1.3 signing now computes the payload each pass. The payload is small and we're already allocating a comparable-sized buffer each iteration to hold the signature. This shouldn't be a big deal. - TLS 1.2 decryption code still needs two states due to reading the message (fixed in new state machine style), but otherwise it just performs cheap idempotent tasks again. The PSK code is reshuffled to guarantee the callback is not called twice (though this was impossible anyway because we don't support RSA_PSK). - TLS 1.2 CertificateVerify signing is easy as the transcript is readily available. The buffer is released very slightly later, but it shouldn't matter. - TLS 1.2 ServerKeyExchange signing required some reshuffling. Assembling the ServerKeyExchange parameters is moved to the previous state. The signing payload has some randoms prepended. This is cheap enough, but a nuisance in C. Pre-prepend the randoms in hs->server_params. With this change, we are *nearly* rid of the A/B => same function pattern. BUG=128 Change-Id: Iec4fe0be7cfc88a6de027ba2760fae70794ea810 Reviewed-on: https://boringssl-review.googlesource.com/17265 Commit-Queue: David Benjamin <davidben@google.com> Commit-Queue: Steven Valdez <svaldez@google.com> Reviewed-by: Steven Valdez <svaldez@google.com>
vor 7 Jahren
Simplify ssl_private_key_* state machine points. The original motivation behind the sign/complete split was to avoid needlessly hashing the input on each pass through the state machine, but we're payload-based now and, in all cases, the payload is either cheap to compute or readily available. (Even the hashing worry was probably unnecessary.) Tweak ssl_private_key_{sign,decrypt} to automatically call ssl_private_key_complete as needed and take advantage of this in the handshake state machines: - TLS 1.3 signing now computes the payload each pass. The payload is small and we're already allocating a comparable-sized buffer each iteration to hold the signature. This shouldn't be a big deal. - TLS 1.2 decryption code still needs two states due to reading the message (fixed in new state machine style), but otherwise it just performs cheap idempotent tasks again. The PSK code is reshuffled to guarantee the callback is not called twice (though this was impossible anyway because we don't support RSA_PSK). - TLS 1.2 CertificateVerify signing is easy as the transcript is readily available. The buffer is released very slightly later, but it shouldn't matter. - TLS 1.2 ServerKeyExchange signing required some reshuffling. Assembling the ServerKeyExchange parameters is moved to the previous state. The signing payload has some randoms prepended. This is cheap enough, but a nuisance in C. Pre-prepend the randoms in hs->server_params. With this change, we are *nearly* rid of the A/B => same function pattern. BUG=128 Change-Id: Iec4fe0be7cfc88a6de027ba2760fae70794ea810 Reviewed-on: https://boringssl-review.googlesource.com/17265 Commit-Queue: David Benjamin <davidben@google.com> Commit-Queue: Steven Valdez <svaldez@google.com> Reviewed-by: Steven Valdez <svaldez@google.com>
vor 7 Jahren
Simplify ssl_private_key_* state machine points. The original motivation behind the sign/complete split was to avoid needlessly hashing the input on each pass through the state machine, but we're payload-based now and, in all cases, the payload is either cheap to compute or readily available. (Even the hashing worry was probably unnecessary.) Tweak ssl_private_key_{sign,decrypt} to automatically call ssl_private_key_complete as needed and take advantage of this in the handshake state machines: - TLS 1.3 signing now computes the payload each pass. The payload is small and we're already allocating a comparable-sized buffer each iteration to hold the signature. This shouldn't be a big deal. - TLS 1.2 decryption code still needs two states due to reading the message (fixed in new state machine style), but otherwise it just performs cheap idempotent tasks again. The PSK code is reshuffled to guarantee the callback is not called twice (though this was impossible anyway because we don't support RSA_PSK). - TLS 1.2 CertificateVerify signing is easy as the transcript is readily available. The buffer is released very slightly later, but it shouldn't matter. - TLS 1.2 ServerKeyExchange signing required some reshuffling. Assembling the ServerKeyExchange parameters is moved to the previous state. The signing payload has some randoms prepended. This is cheap enough, but a nuisance in C. Pre-prepend the randoms in hs->server_params. With this change, we are *nearly* rid of the A/B => same function pattern. BUG=128 Change-Id: Iec4fe0be7cfc88a6de027ba2760fae70794ea810 Reviewed-on: https://boringssl-review.googlesource.com/17265 Commit-Queue: David Benjamin <davidben@google.com> Commit-Queue: Steven Valdez <svaldez@google.com> Reviewed-by: Steven Valdez <svaldez@google.com>
vor 7 Jahren
Move libssl's internals into the bssl namespace. This is horrible, but everything else I tried was worse. The goal with this CL is to take the extern "C" out of ssl/internal.h and move most symbols to namespace bssl, so we can start using C++ helpers and destructors without worry. Complications: - Public API functions must be extern "C" and match their declaration in ssl.h, which is unnamespaced. C++ really does not want you to interleave namespaced and unnamespaced things. One can actually write a namespaced extern "C" function, but this means, from C++'s perspective, the function is namespaced. Trying to namespace the public header would worked but ended up too deep a rabbithole. - Our STACK_OF macros do not work right in namespaces. - The typedefs for our exposed but opaque types are visible in the header files and copied into consuming projects as forward declarations. We ultimately want to give SSL a destructor, but clobbering an unnamespaced ssl_st::~ssl_st seems bad manners. - MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL. This CL opts for: - ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This informs the public headers to create forward declarations which are compatible with our namespaces. - For now, C++-defined type FOO ends up at bssl::FOO with a typedef outside. Later I imagine we'll rename many of them. - Internal functions get namespace bssl, so we stop worrying about stomping the tls1_prf symbol. Exported C functions are stuck as they are. Rather than try anything weird, bite the bullet and reorder files which have a mix of public and private functions. I expect that over time, the public functions will become fairly small as we move logic to more idiomatic C++. Files without any public C functions can just be written normally. - To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle in advance of them being made idiomatic C++. Bug: 132 Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581 Reviewed-on: https://boringssl-review.googlesource.com/18124 Reviewed-by: David Benjamin <davidben@google.com>
vor 7 Jahren
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  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. *
  113. * Portions of the attached software ("Contribution") are developed by
  114. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  115. *
  116. * The Contribution is licensed pursuant to the OpenSSL open source
  117. * license provided above.
  118. *
  119. * ECC cipher suite support in OpenSSL originally written by
  120. * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
  121. *
  122. */
  123. /* ====================================================================
  124. * Copyright 2005 Nokia. All rights reserved.
  125. *
  126. * The portions of the attached software ("Contribution") is developed by
  127. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  128. * license.
  129. *
  130. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  131. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  132. * support (see RFC 4279) to OpenSSL.
  133. *
  134. * No patent licenses or other rights except those expressly stated in
  135. * the OpenSSL open source license shall be deemed granted or received
  136. * expressly, by implication, estoppel, or otherwise.
  137. *
  138. * No assurances are provided by Nokia that the Contribution does not
  139. * infringe the patent or other intellectual property rights of any third
  140. * party or that the license provides you with all the necessary rights
  141. * to make use of the Contribution.
  142. *
  143. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  144. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  145. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  146. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  147. * OTHERWISE.
  148. */
  149. #include <openssl/ssl.h>
  150. #include <assert.h>
  151. #include <limits.h>
  152. #include <string.h>
  153. #include <utility>
  154. #include <openssl/aead.h>
  155. #include <openssl/bn.h>
  156. #include <openssl/buf.h>
  157. #include <openssl/bytestring.h>
  158. #include <openssl/ec_key.h>
  159. #include <openssl/ecdsa.h>
  160. #include <openssl/err.h>
  161. #include <openssl/evp.h>
  162. #include <openssl/md5.h>
  163. #include <openssl/mem.h>
  164. #include <openssl/rand.h>
  165. #include "../crypto/internal.h"
  166. #include "internal.h"
  167. namespace bssl {
  168. enum ssl_client_hs_state_t {
  169. state_start_connect = 0,
  170. state_enter_early_data,
  171. state_read_hello_verify_request,
  172. state_read_server_hello,
  173. state_tls13,
  174. state_read_server_certificate,
  175. state_read_certificate_status,
  176. state_verify_server_certificate,
  177. state_read_server_key_exchange,
  178. state_read_certificate_request,
  179. state_read_server_hello_done,
  180. state_send_client_certificate,
  181. state_send_client_key_exchange,
  182. state_send_client_certificate_verify,
  183. state_send_client_finished,
  184. state_finish_flight,
  185. state_read_session_ticket,
  186. state_process_change_cipher_spec,
  187. state_read_server_finished,
  188. state_finish_client_handshake,
  189. state_done,
  190. };
  191. // ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
  192. // disabled algorithms.
  193. static void ssl_get_client_disabled(SSL *ssl, uint32_t *out_mask_a,
  194. uint32_t *out_mask_k) {
  195. *out_mask_a = 0;
  196. *out_mask_k = 0;
  197. // PSK requires a client callback.
  198. if (ssl->psk_client_callback == NULL) {
  199. *out_mask_a |= SSL_aPSK;
  200. *out_mask_k |= SSL_kPSK;
  201. }
  202. }
  203. static int ssl_write_client_cipher_list(SSL_HANDSHAKE *hs, CBB *out) {
  204. SSL *const ssl = hs->ssl;
  205. uint32_t mask_a, mask_k;
  206. ssl_get_client_disabled(ssl, &mask_a, &mask_k);
  207. CBB child;
  208. if (!CBB_add_u16_length_prefixed(out, &child)) {
  209. return 0;
  210. }
  211. // Add a fake cipher suite. See draft-davidben-tls-grease-01.
  212. if (ssl->ctx->grease_enabled &&
  213. !CBB_add_u16(&child, ssl_get_grease_value(hs, ssl_grease_cipher))) {
  214. return 0;
  215. }
  216. // Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
  217. // hardware support.
  218. if (hs->max_version >= TLS1_3_VERSION) {
  219. if (!EVP_has_aes_hardware() &&
  220. !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
  221. return 0;
  222. }
  223. if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
  224. !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
  225. return 0;
  226. }
  227. if (EVP_has_aes_hardware() &&
  228. !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
  229. return 0;
  230. }
  231. }
  232. if (hs->min_version < TLS1_3_VERSION) {
  233. int any_enabled = 0;
  234. for (const SSL_CIPHER *cipher : SSL_get_ciphers(ssl)) {
  235. // Skip disabled ciphers
  236. if ((cipher->algorithm_mkey & mask_k) ||
  237. (cipher->algorithm_auth & mask_a)) {
  238. continue;
  239. }
  240. if (SSL_CIPHER_get_min_version(cipher) > hs->max_version ||
  241. SSL_CIPHER_get_max_version(cipher) < hs->min_version) {
  242. continue;
  243. }
  244. any_enabled = 1;
  245. if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
  246. return 0;
  247. }
  248. }
  249. // If all ciphers were disabled, return the error to the caller.
  250. if (!any_enabled && hs->max_version < TLS1_3_VERSION) {
  251. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
  252. return 0;
  253. }
  254. }
  255. // For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
  256. // added.
  257. if (hs->max_version == SSL3_VERSION &&
  258. !ssl->s3->initial_handshake_complete) {
  259. if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
  260. return 0;
  261. }
  262. }
  263. if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
  264. if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
  265. return 0;
  266. }
  267. }
  268. return CBB_flush(out);
  269. }
  270. int ssl_write_client_hello(SSL_HANDSHAKE *hs) {
  271. SSL *const ssl = hs->ssl;
  272. ScopedCBB cbb;
  273. CBB body;
  274. if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO)) {
  275. return 0;
  276. }
  277. CBB child;
  278. if (!CBB_add_u16(&body, hs->client_version) ||
  279. !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  280. !CBB_add_u8_length_prefixed(&body, &child)) {
  281. return 0;
  282. }
  283. // Do not send a session ID on renegotiation.
  284. if (!ssl->s3->initial_handshake_complete &&
  285. !CBB_add_bytes(&child, hs->session_id, hs->session_id_len)) {
  286. return 0;
  287. }
  288. if (SSL_is_dtls(ssl)) {
  289. if (!CBB_add_u8_length_prefixed(&body, &child) ||
  290. !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
  291. return 0;
  292. }
  293. }
  294. size_t header_len =
  295. SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
  296. if (!ssl_write_client_cipher_list(hs, &body) ||
  297. !CBB_add_u8(&body, 1 /* one compression method */) ||
  298. !CBB_add_u8(&body, 0 /* null compression */) ||
  299. !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) {
  300. return 0;
  301. }
  302. Array<uint8_t> msg;
  303. if (!ssl->method->finish_message(ssl, cbb.get(), &msg)) {
  304. return 0;
  305. }
  306. // Now that the length prefixes have been computed, fill in the placeholder
  307. // PSK binder.
  308. if (hs->needs_psk_binder &&
  309. !tls13_write_psk_binder(hs, msg.data(), msg.size())) {
  310. return 0;
  311. }
  312. return ssl->method->add_message(ssl, std::move(msg));
  313. }
  314. static bool parse_supported_versions(SSL_HANDSHAKE *hs, uint16_t *version,
  315. const CBS *in) {
  316. // If the outer version is not TLS 1.2, or there is no extensions block, use
  317. // the outer version.
  318. if (*version != TLS1_2_VERSION || CBS_len(in) == 0) {
  319. return true;
  320. }
  321. SSL *const ssl = hs->ssl;
  322. CBS copy = *in, extensions;
  323. if (!CBS_get_u16_length_prefixed(&copy, &extensions) ||
  324. CBS_len(&copy) != 0) {
  325. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  326. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  327. return false;
  328. }
  329. bool have_supported_versions;
  330. CBS supported_versions;
  331. const SSL_EXTENSION_TYPE ext_types[] = {
  332. {TLSEXT_TYPE_supported_versions, &have_supported_versions,
  333. &supported_versions},
  334. };
  335. uint8_t alert = SSL_AD_DECODE_ERROR;
  336. if (!ssl_parse_extensions(&extensions, &alert, ext_types,
  337. OPENSSL_ARRAY_SIZE(ext_types),
  338. 1 /* ignore unknown */)) {
  339. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  340. return false;
  341. }
  342. // Override the outer version with the extension, if present.
  343. if (have_supported_versions &&
  344. (!CBS_get_u16(&supported_versions, version) ||
  345. CBS_len(&supported_versions) != 0)) {
  346. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  347. return false;
  348. }
  349. return true;
  350. }
  351. static enum ssl_hs_wait_t do_start_connect(SSL_HANDSHAKE *hs) {
  352. SSL *const ssl = hs->ssl;
  353. ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
  354. // |session_reused| must be reset in case this is a renegotiation.
  355. ssl->s3->session_reused = false;
  356. // Freeze the version range.
  357. if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) {
  358. return ssl_hs_error;
  359. }
  360. // SSL 3.0 ClientHellos should use SSL 3.0 not TLS 1.0, for the record-layer
  361. // version.
  362. if (hs->max_version == SSL3_VERSION) {
  363. ssl->s3->aead_write_ctx->SetVersionIfNullCipher(SSL3_VERSION);
  364. }
  365. // Always advertise the ClientHello version from the original maximum version,
  366. // even on renegotiation. The static RSA key exchange uses this field, and
  367. // some servers fail when it changes across handshakes.
  368. if (SSL_is_dtls(hs->ssl)) {
  369. hs->client_version =
  370. hs->max_version >= TLS1_2_VERSION ? DTLS1_2_VERSION : DTLS1_VERSION;
  371. } else {
  372. hs->client_version =
  373. hs->max_version >= TLS1_2_VERSION ? TLS1_2_VERSION : hs->max_version;
  374. }
  375. // If the configured session has expired or was created at a disabled
  376. // version, drop it.
  377. if (ssl->session != NULL) {
  378. if (ssl->session->is_server ||
  379. !ssl_supports_version(hs, ssl->session->ssl_version) ||
  380. (ssl->session->session_id_length == 0 &&
  381. ssl->session->tlsext_ticklen == 0) ||
  382. ssl->session->not_resumable ||
  383. !ssl_session_is_time_valid(ssl, ssl->session)) {
  384. ssl_set_session(ssl, NULL);
  385. }
  386. }
  387. if (!RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
  388. return ssl_hs_error;
  389. }
  390. // Initialize a random session ID for the experimental TLS 1.3 variant
  391. // requiring a session id.
  392. if (ssl->session != nullptr &&
  393. !ssl->s3->initial_handshake_complete &&
  394. ssl->session->session_id_length > 0) {
  395. hs->session_id_len = ssl->session->session_id_length;
  396. OPENSSL_memcpy(hs->session_id, ssl->session->session_id,
  397. hs->session_id_len);
  398. } else if (hs->max_version >= TLS1_3_VERSION) {
  399. hs->session_id_len = sizeof(hs->session_id);
  400. if (!RAND_bytes(hs->session_id, hs->session_id_len)) {
  401. return ssl_hs_error;
  402. }
  403. }
  404. if (!ssl_write_client_hello(hs)) {
  405. return ssl_hs_error;
  406. }
  407. hs->state = state_enter_early_data;
  408. return ssl_hs_flush;
  409. }
  410. static enum ssl_hs_wait_t do_enter_early_data(SSL_HANDSHAKE *hs) {
  411. SSL *const ssl = hs->ssl;
  412. if (SSL_is_dtls(ssl)) {
  413. hs->state = state_read_hello_verify_request;
  414. return ssl_hs_ok;
  415. }
  416. if (!hs->early_data_offered) {
  417. hs->state = state_read_server_hello;
  418. return ssl_hs_ok;
  419. }
  420. ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->session->ssl_version);
  421. if (!ssl->method->add_change_cipher_spec(ssl)) {
  422. return ssl_hs_error;
  423. }
  424. if (!tls13_init_early_key_schedule(hs, ssl->session->master_key,
  425. ssl->session->master_key_length) ||
  426. !tls13_derive_early_secrets(hs) ||
  427. !tls13_set_traffic_key(ssl, evp_aead_seal, hs->early_traffic_secret,
  428. hs->hash_len)) {
  429. return ssl_hs_error;
  430. }
  431. // Stash the early data session, so connection properties may be queried out
  432. // of it.
  433. hs->in_early_data = true;
  434. SSL_SESSION_up_ref(ssl->session);
  435. hs->early_session.reset(ssl->session);
  436. hs->can_early_write = true;
  437. hs->state = state_read_server_hello;
  438. return ssl_hs_early_return;
  439. }
  440. static enum ssl_hs_wait_t do_read_hello_verify_request(SSL_HANDSHAKE *hs) {
  441. SSL *const ssl = hs->ssl;
  442. assert(SSL_is_dtls(ssl));
  443. SSLMessage msg;
  444. if (!ssl->method->get_message(ssl, &msg)) {
  445. return ssl_hs_read_message;
  446. }
  447. if (msg.type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
  448. hs->state = state_read_server_hello;
  449. return ssl_hs_ok;
  450. }
  451. CBS hello_verify_request = msg.body, cookie;
  452. uint16_t server_version;
  453. if (!CBS_get_u16(&hello_verify_request, &server_version) ||
  454. !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
  455. CBS_len(&cookie) > sizeof(ssl->d1->cookie) ||
  456. CBS_len(&hello_verify_request) != 0) {
  457. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  458. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  459. return ssl_hs_error;
  460. }
  461. OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
  462. ssl->d1->cookie_len = CBS_len(&cookie);
  463. ssl->method->next_message(ssl);
  464. // DTLS resets the handshake buffer after HelloVerifyRequest.
  465. if (!hs->transcript.Init()) {
  466. return ssl_hs_error;
  467. }
  468. if (!ssl_write_client_hello(hs)) {
  469. return ssl_hs_error;
  470. }
  471. hs->state = state_read_server_hello;
  472. return ssl_hs_flush;
  473. }
  474. static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) {
  475. SSL *const ssl = hs->ssl;
  476. SSLMessage msg;
  477. if (!ssl->method->get_message(ssl, &msg)) {
  478. return ssl_hs_read_server_hello;
  479. }
  480. if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) {
  481. return ssl_hs_error;
  482. }
  483. CBS server_hello = msg.body, server_random, session_id;
  484. uint16_t server_version, cipher_suite;
  485. uint8_t compression_method;
  486. if (!CBS_get_u16(&server_hello, &server_version) ||
  487. !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
  488. !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
  489. CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
  490. !CBS_get_u16(&server_hello, &cipher_suite) ||
  491. !CBS_get_u8(&server_hello, &compression_method)) {
  492. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  493. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  494. return ssl_hs_error;
  495. }
  496. // Use the supported_versions extension if applicable.
  497. if (!parse_supported_versions(hs, &server_version, &server_hello)) {
  498. return ssl_hs_error;
  499. }
  500. if (!ssl_supports_version(hs, server_version)) {
  501. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
  502. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
  503. return ssl_hs_error;
  504. }
  505. assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
  506. if (!ssl->s3->have_version) {
  507. ssl->version = server_version;
  508. // At this point, the connection's version is known and ssl->version is
  509. // fixed. Begin enforcing the record-layer version.
  510. ssl->s3->have_version = true;
  511. ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
  512. } else if (server_version != ssl->version) {
  513. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
  514. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
  515. return ssl_hs_error;
  516. }
  517. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  518. hs->state = state_tls13;
  519. return ssl_hs_ok;
  520. }
  521. // Clear some TLS 1.3 state that no longer needs to be retained.
  522. hs->key_share.reset();
  523. hs->key_share_bytes.Reset();
  524. // A TLS 1.2 server would not know to skip the early data we offered. Report
  525. // an error code sooner. The caller may use this error code to implement the
  526. // fallback described in draft-ietf-tls-tls13-18 appendix C.3.
  527. if (hs->early_data_offered) {
  528. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA);
  529. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
  530. return ssl_hs_error;
  531. }
  532. // Copy over the server random.
  533. OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
  534. SSL3_RANDOM_SIZE);
  535. // Measure, but do not enforce, the TLS 1.3 anti-downgrade feature, with a
  536. // different value.
  537. //
  538. // For draft TLS 1.3 versions, it is not safe to deploy this feature. However,
  539. // some TLS terminators are non-compliant and copy the origin server's value,
  540. // so we wish to measure eventual compatibility impact.
  541. if (!ssl->s3->initial_handshake_complete &&
  542. hs->max_version >= TLS1_3_VERSION &&
  543. OPENSSL_memcmp(ssl->s3->server_random + SSL3_RANDOM_SIZE -
  544. sizeof(kDraftDowngradeRandom),
  545. kDraftDowngradeRandom,
  546. sizeof(kDraftDowngradeRandom)) == 0) {
  547. ssl->s3->draft_downgrade = true;
  548. }
  549. if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
  550. ssl->session->session_id_length != 0 &&
  551. CBS_mem_equal(&session_id, ssl->session->session_id,
  552. ssl->session->session_id_length)) {
  553. ssl->s3->session_reused = true;
  554. } else {
  555. // The server may also have echoed back the TLS 1.3 compatibility mode
  556. // session ID. As we know this is not a session the server knows about, any
  557. // server resuming it is in error. Reject the first connection
  558. // deterministicly, rather than installing an invalid session into the
  559. // session cache. https://crbug.com/796910
  560. if (hs->session_id_len != 0 &&
  561. CBS_mem_equal(&session_id, hs->session_id, hs->session_id_len)) {
  562. OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_ECHOED_INVALID_SESSION_ID);
  563. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  564. return ssl_hs_error;
  565. }
  566. // The session wasn't resumed. Create a fresh SSL_SESSION to
  567. // fill out.
  568. ssl_set_session(ssl, NULL);
  569. if (!ssl_get_new_session(hs, 0 /* client */)) {
  570. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  571. return ssl_hs_error;
  572. }
  573. // Note: session_id could be empty.
  574. hs->new_session->session_id_length = CBS_len(&session_id);
  575. OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id),
  576. CBS_len(&session_id));
  577. }
  578. const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite);
  579. if (cipher == NULL) {
  580. // unknown cipher
  581. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
  582. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  583. return ssl_hs_error;
  584. }
  585. // The cipher must be allowed in the selected version and enabled.
  586. uint32_t mask_a, mask_k;
  587. ssl_get_client_disabled(ssl, &mask_a, &mask_k);
  588. if ((cipher->algorithm_mkey & mask_k) || (cipher->algorithm_auth & mask_a) ||
  589. SSL_CIPHER_get_min_version(cipher) > ssl_protocol_version(ssl) ||
  590. SSL_CIPHER_get_max_version(cipher) < ssl_protocol_version(ssl) ||
  591. !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, cipher)) {
  592. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
  593. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  594. return ssl_hs_error;
  595. }
  596. if (ssl->session != NULL) {
  597. if (ssl->session->ssl_version != ssl->version) {
  598. OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
  599. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  600. return ssl_hs_error;
  601. }
  602. if (ssl->session->cipher != cipher) {
  603. OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  604. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  605. return ssl_hs_error;
  606. }
  607. if (!ssl_session_is_context_valid(ssl, ssl->session)) {
  608. // This is actually a client application bug.
  609. OPENSSL_PUT_ERROR(SSL,
  610. SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  611. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  612. return ssl_hs_error;
  613. }
  614. } else {
  615. hs->new_session->cipher = cipher;
  616. }
  617. hs->new_cipher = cipher;
  618. // Now that the cipher is known, initialize the handshake hash and hash the
  619. // ServerHello.
  620. if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
  621. !ssl_hash_message(hs, msg)) {
  622. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  623. return ssl_hs_error;
  624. }
  625. // If doing a full handshake, the server may request a client certificate
  626. // which requires hashing the handshake transcript. Otherwise, the handshake
  627. // buffer may be released.
  628. if (ssl->session != NULL ||
  629. !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  630. hs->transcript.FreeBuffer();
  631. }
  632. // Only the NULL compression algorithm is supported.
  633. if (compression_method != 0) {
  634. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  635. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  636. return ssl_hs_error;
  637. }
  638. // TLS extensions
  639. if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
  640. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  641. return ssl_hs_error;
  642. }
  643. // There should be nothing left over in the record.
  644. if (CBS_len(&server_hello) != 0) {
  645. // wrong packet length
  646. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  647. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  648. return ssl_hs_error;
  649. }
  650. if (ssl->session != NULL &&
  651. hs->extended_master_secret != ssl->session->extended_master_secret) {
  652. if (ssl->session->extended_master_secret) {
  653. OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
  654. } else {
  655. OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
  656. }
  657. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  658. return ssl_hs_error;
  659. }
  660. if (ssl->s3->token_binding_negotiated &&
  661. (!hs->extended_master_secret || !ssl->s3->send_connection_binding)) {
  662. OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_TB_WITHOUT_EMS_OR_RI);
  663. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
  664. return ssl_hs_error;
  665. }
  666. ssl->method->next_message(ssl);
  667. if (ssl->session != NULL) {
  668. hs->state = state_read_session_ticket;
  669. return ssl_hs_ok;
  670. }
  671. hs->state = state_read_server_certificate;
  672. return ssl_hs_ok;
  673. }
  674. static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
  675. enum ssl_hs_wait_t wait = tls13_client_handshake(hs);
  676. if (wait == ssl_hs_ok) {
  677. hs->state = state_finish_client_handshake;
  678. return ssl_hs_ok;
  679. }
  680. return wait;
  681. }
  682. static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) {
  683. SSL *const ssl = hs->ssl;
  684. if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  685. hs->state = state_read_certificate_status;
  686. return ssl_hs_ok;
  687. }
  688. SSLMessage msg;
  689. if (!ssl->method->get_message(ssl, &msg)) {
  690. return ssl_hs_read_message;
  691. }
  692. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
  693. !ssl_hash_message(hs, msg)) {
  694. return ssl_hs_error;
  695. }
  696. CBS body = msg.body;
  697. uint8_t alert = SSL_AD_DECODE_ERROR;
  698. UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
  699. if (!ssl_parse_cert_chain(&alert, &chain, &hs->peer_pubkey, NULL, &body,
  700. ssl->ctx->pool)) {
  701. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  702. return ssl_hs_error;
  703. }
  704. sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
  705. hs->new_session->certs = chain.release();
  706. if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0 ||
  707. CBS_len(&body) != 0 ||
  708. !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
  709. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  710. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  711. return ssl_hs_error;
  712. }
  713. if (!ssl_check_leaf_certificate(
  714. hs, hs->peer_pubkey.get(),
  715. sk_CRYPTO_BUFFER_value(hs->new_session->certs, 0))) {
  716. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  717. return ssl_hs_error;
  718. }
  719. ssl->method->next_message(ssl);
  720. hs->state = state_read_certificate_status;
  721. return ssl_hs_ok;
  722. }
  723. static enum ssl_hs_wait_t do_read_certificate_status(SSL_HANDSHAKE *hs) {
  724. SSL *const ssl = hs->ssl;
  725. if (!hs->certificate_status_expected) {
  726. hs->state = state_verify_server_certificate;
  727. return ssl_hs_ok;
  728. }
  729. SSLMessage msg;
  730. if (!ssl->method->get_message(ssl, &msg)) {
  731. return ssl_hs_read_message;
  732. }
  733. if (msg.type != SSL3_MT_CERTIFICATE_STATUS) {
  734. // A server may send status_request in ServerHello and then change its mind
  735. // about sending CertificateStatus.
  736. hs->state = state_verify_server_certificate;
  737. return ssl_hs_ok;
  738. }
  739. if (!ssl_hash_message(hs, msg)) {
  740. return ssl_hs_error;
  741. }
  742. CBS certificate_status = msg.body, ocsp_response;
  743. uint8_t status_type;
  744. if (!CBS_get_u8(&certificate_status, &status_type) ||
  745. status_type != TLSEXT_STATUSTYPE_ocsp ||
  746. !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
  747. CBS_len(&ocsp_response) == 0 ||
  748. CBS_len(&certificate_status) != 0) {
  749. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  750. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  751. return ssl_hs_error;
  752. }
  753. CRYPTO_BUFFER_free(hs->new_session->ocsp_response);
  754. hs->new_session->ocsp_response =
  755. CRYPTO_BUFFER_new_from_CBS(&ocsp_response, ssl->ctx->pool);
  756. if (hs->new_session->ocsp_response == nullptr) {
  757. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  758. return ssl_hs_error;
  759. }
  760. ssl->method->next_message(ssl);
  761. hs->state = state_verify_server_certificate;
  762. return ssl_hs_ok;
  763. }
  764. static enum ssl_hs_wait_t do_verify_server_certificate(SSL_HANDSHAKE *hs) {
  765. if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  766. hs->state = state_read_server_key_exchange;
  767. return ssl_hs_ok;
  768. }
  769. switch (ssl_verify_peer_cert(hs)) {
  770. case ssl_verify_ok:
  771. break;
  772. case ssl_verify_invalid:
  773. return ssl_hs_error;
  774. case ssl_verify_retry:
  775. hs->state = state_verify_server_certificate;
  776. return ssl_hs_certificate_verify;
  777. }
  778. hs->state = state_read_server_key_exchange;
  779. return ssl_hs_ok;
  780. }
  781. static enum ssl_hs_wait_t do_read_server_key_exchange(SSL_HANDSHAKE *hs) {
  782. SSL *const ssl = hs->ssl;
  783. SSLMessage msg;
  784. if (!ssl->method->get_message(ssl, &msg)) {
  785. return ssl_hs_read_message;
  786. }
  787. if (msg.type != SSL3_MT_SERVER_KEY_EXCHANGE) {
  788. // Some ciphers (pure PSK) have an optional ServerKeyExchange message.
  789. if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) {
  790. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  791. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  792. return ssl_hs_error;
  793. }
  794. hs->state = state_read_certificate_request;
  795. return ssl_hs_ok;
  796. }
  797. if (!ssl_hash_message(hs, msg)) {
  798. return ssl_hs_error;
  799. }
  800. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  801. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  802. CBS server_key_exchange = msg.body;
  803. if (alg_a & SSL_aPSK) {
  804. CBS psk_identity_hint;
  805. // Each of the PSK key exchanges begins with a psk_identity_hint.
  806. if (!CBS_get_u16_length_prefixed(&server_key_exchange,
  807. &psk_identity_hint)) {
  808. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  809. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  810. return ssl_hs_error;
  811. }
  812. // Store the PSK identity hint for the ClientKeyExchange. Assume that the
  813. // maximum length of a PSK identity hint can be as long as the maximum
  814. // length of a PSK identity. Also do not allow NULL characters; identities
  815. // are saved as C strings.
  816. //
  817. // TODO(davidben): Should invalid hints be ignored? It's a hint rather than
  818. // a specific identity.
  819. if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
  820. CBS_contains_zero_byte(&psk_identity_hint)) {
  821. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  822. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  823. return ssl_hs_error;
  824. }
  825. // Save non-empty identity hints as a C string. Empty identity hints we
  826. // treat as missing. Plain PSK makes it possible to send either no hint
  827. // (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
  828. // empty hint. Having different capabilities is odd, so we interpret empty
  829. // and missing as identical.
  830. char *raw = nullptr;
  831. if (CBS_len(&psk_identity_hint) != 0 &&
  832. !CBS_strdup(&psk_identity_hint, &raw)) {
  833. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  834. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  835. return ssl_hs_error;
  836. }
  837. hs->peer_psk_identity_hint.reset(raw);
  838. }
  839. if (alg_k & SSL_kECDHE) {
  840. // Parse the server parameters.
  841. uint8_t group_type;
  842. uint16_t group_id;
  843. CBS point;
  844. if (!CBS_get_u8(&server_key_exchange, &group_type) ||
  845. group_type != NAMED_CURVE_TYPE ||
  846. !CBS_get_u16(&server_key_exchange, &group_id) ||
  847. !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
  848. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  849. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  850. return ssl_hs_error;
  851. }
  852. hs->new_session->group_id = group_id;
  853. // Ensure the group is consistent with preferences.
  854. if (!tls1_check_group_id(ssl, group_id)) {
  855. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
  856. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  857. return ssl_hs_error;
  858. }
  859. // Initialize ECDH and save the peer public key for later.
  860. hs->key_share = SSLKeyShare::Create(group_id);
  861. if (!hs->key_share ||
  862. !hs->peer_key.CopyFrom(point)) {
  863. return ssl_hs_error;
  864. }
  865. } else if (!(alg_k & SSL_kPSK)) {
  866. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  867. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  868. return ssl_hs_error;
  869. }
  870. // At this point, |server_key_exchange| contains the signature, if any, while
  871. // |msg.body| contains the entire message. From that, derive a CBS containing
  872. // just the parameter.
  873. CBS parameter;
  874. CBS_init(&parameter, CBS_data(&msg.body),
  875. CBS_len(&msg.body) - CBS_len(&server_key_exchange));
  876. // ServerKeyExchange should be signed by the server's public key.
  877. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  878. uint16_t signature_algorithm = 0;
  879. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  880. if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
  881. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  882. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  883. return ssl_hs_error;
  884. }
  885. uint8_t alert = SSL_AD_DECODE_ERROR;
  886. if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
  887. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  888. return ssl_hs_error;
  889. }
  890. hs->new_session->peer_signature_algorithm = signature_algorithm;
  891. } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
  892. hs->peer_pubkey.get())) {
  893. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
  894. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
  895. return ssl_hs_error;
  896. }
  897. // The last field in |server_key_exchange| is the signature.
  898. CBS signature;
  899. if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
  900. CBS_len(&server_key_exchange) != 0) {
  901. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  902. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  903. return ssl_hs_error;
  904. }
  905. ScopedCBB transcript;
  906. Array<uint8_t> transcript_data;
  907. if (!CBB_init(transcript.get(),
  908. 2 * SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
  909. !CBB_add_bytes(transcript.get(), ssl->s3->client_random,
  910. SSL3_RANDOM_SIZE) ||
  911. !CBB_add_bytes(transcript.get(), ssl->s3->server_random,
  912. SSL3_RANDOM_SIZE) ||
  913. !CBB_add_bytes(transcript.get(), CBS_data(&parameter),
  914. CBS_len(&parameter)) ||
  915. !CBBFinishArray(transcript.get(), &transcript_data)) {
  916. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  917. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  918. return ssl_hs_error;
  919. }
  920. bool sig_ok = ssl_public_key_verify(ssl, signature, signature_algorithm,
  921. hs->peer_pubkey.get(), transcript_data);
  922. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  923. sig_ok = true;
  924. ERR_clear_error();
  925. #endif
  926. if (!sig_ok) {
  927. // bad signature
  928. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
  929. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  930. return ssl_hs_error;
  931. }
  932. } else {
  933. // PSK ciphers are the only supported certificate-less ciphers.
  934. assert(alg_a == SSL_aPSK);
  935. if (CBS_len(&server_key_exchange) > 0) {
  936. OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
  937. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  938. return ssl_hs_error;
  939. }
  940. }
  941. ssl->method->next_message(ssl);
  942. hs->state = state_read_certificate_request;
  943. return ssl_hs_ok;
  944. }
  945. static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) {
  946. SSL *const ssl = hs->ssl;
  947. if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  948. hs->state = state_read_server_hello_done;
  949. return ssl_hs_ok;
  950. }
  951. SSLMessage msg;
  952. if (!ssl->method->get_message(ssl, &msg)) {
  953. return ssl_hs_read_message;
  954. }
  955. if (msg.type == SSL3_MT_SERVER_HELLO_DONE) {
  956. // If we get here we don't need the handshake buffer as we won't be doing
  957. // client auth.
  958. hs->transcript.FreeBuffer();
  959. hs->state = state_read_server_hello_done;
  960. return ssl_hs_ok;
  961. }
  962. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_REQUEST) ||
  963. !ssl_hash_message(hs, msg)) {
  964. return ssl_hs_error;
  965. }
  966. // Get the certificate types.
  967. CBS body = msg.body, certificate_types;
  968. if (!CBS_get_u8_length_prefixed(&body, &certificate_types)) {
  969. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  970. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  971. return ssl_hs_error;
  972. }
  973. if (!hs->certificate_types.CopyFrom(certificate_types)) {
  974. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  975. return ssl_hs_error;
  976. }
  977. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  978. CBS supported_signature_algorithms;
  979. if (!CBS_get_u16_length_prefixed(&body, &supported_signature_algorithms) ||
  980. !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
  981. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  982. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  983. return ssl_hs_error;
  984. }
  985. }
  986. uint8_t alert = SSL_AD_DECODE_ERROR;
  987. UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names =
  988. ssl_parse_client_CA_list(ssl, &alert, &body);
  989. if (!ca_names) {
  990. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  991. return ssl_hs_error;
  992. }
  993. if (CBS_len(&body) != 0) {
  994. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  995. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  996. return ssl_hs_error;
  997. }
  998. hs->cert_request = true;
  999. hs->ca_names = std::move(ca_names);
  1000. ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
  1001. ssl->method->next_message(ssl);
  1002. hs->state = state_read_server_hello_done;
  1003. return ssl_hs_ok;
  1004. }
  1005. static enum ssl_hs_wait_t do_read_server_hello_done(SSL_HANDSHAKE *hs) {
  1006. SSL *const ssl = hs->ssl;
  1007. SSLMessage msg;
  1008. if (!ssl->method->get_message(ssl, &msg)) {
  1009. return ssl_hs_read_message;
  1010. }
  1011. if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO_DONE) ||
  1012. !ssl_hash_message(hs, msg)) {
  1013. return ssl_hs_error;
  1014. }
  1015. // ServerHelloDone is empty.
  1016. if (CBS_len(&msg.body) != 0) {
  1017. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1018. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1019. return ssl_hs_error;
  1020. }
  1021. ssl->method->next_message(ssl);
  1022. hs->state = state_send_client_certificate;
  1023. return ssl_hs_ok;
  1024. }
  1025. static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) {
  1026. SSL *const ssl = hs->ssl;
  1027. // The peer didn't request a certificate.
  1028. if (!hs->cert_request) {
  1029. hs->state = state_send_client_key_exchange;
  1030. return ssl_hs_ok;
  1031. }
  1032. // Call cert_cb to update the certificate.
  1033. if (ssl->cert->cert_cb != NULL) {
  1034. int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
  1035. if (rv == 0) {
  1036. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1037. OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
  1038. return ssl_hs_error;
  1039. }
  1040. if (rv < 0) {
  1041. hs->state = state_send_client_certificate;
  1042. return ssl_hs_x509_lookup;
  1043. }
  1044. }
  1045. if (!ssl_has_certificate(ssl)) {
  1046. // Without a client certificate, the handshake buffer may be released.
  1047. hs->transcript.FreeBuffer();
  1048. // In SSL 3.0, the Certificate message is replaced with a warning alert.
  1049. if (ssl->version == SSL3_VERSION) {
  1050. if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING,
  1051. SSL_AD_NO_CERTIFICATE)) {
  1052. return ssl_hs_error;
  1053. }
  1054. hs->state = state_send_client_key_exchange;
  1055. return ssl_hs_ok;
  1056. }
  1057. }
  1058. if (!ssl_on_certificate_selected(hs) ||
  1059. !ssl_output_cert_chain(ssl)) {
  1060. return ssl_hs_error;
  1061. }
  1062. hs->state = state_send_client_key_exchange;
  1063. return ssl_hs_ok;
  1064. }
  1065. static_assert(sizeof(size_t) >= sizeof(unsigned),
  1066. "size_t is smaller than unsigned");
  1067. static enum ssl_hs_wait_t do_send_client_key_exchange(SSL_HANDSHAKE *hs) {
  1068. SSL *const ssl = hs->ssl;
  1069. ScopedCBB cbb;
  1070. CBB body;
  1071. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  1072. SSL3_MT_CLIENT_KEY_EXCHANGE)) {
  1073. return ssl_hs_error;
  1074. }
  1075. Array<uint8_t> pms;
  1076. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  1077. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  1078. // If using a PSK key exchange, prepare the pre-shared key.
  1079. unsigned psk_len = 0;
  1080. uint8_t psk[PSK_MAX_PSK_LEN];
  1081. if (alg_a & SSL_aPSK) {
  1082. if (ssl->psk_client_callback == NULL) {
  1083. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
  1084. return ssl_hs_error;
  1085. }
  1086. char identity[PSK_MAX_IDENTITY_LEN + 1];
  1087. OPENSSL_memset(identity, 0, sizeof(identity));
  1088. psk_len =
  1089. ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint.get(),
  1090. identity, sizeof(identity), psk, sizeof(psk));
  1091. if (psk_len == 0) {
  1092. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
  1093. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1094. return ssl_hs_error;
  1095. }
  1096. assert(psk_len <= PSK_MAX_PSK_LEN);
  1097. OPENSSL_free(hs->new_session->psk_identity);
  1098. hs->new_session->psk_identity = BUF_strdup(identity);
  1099. if (hs->new_session->psk_identity == NULL) {
  1100. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1101. return ssl_hs_error;
  1102. }
  1103. // Write out psk_identity.
  1104. CBB child;
  1105. if (!CBB_add_u16_length_prefixed(&body, &child) ||
  1106. !CBB_add_bytes(&child, (const uint8_t *)identity,
  1107. OPENSSL_strnlen(identity, sizeof(identity))) ||
  1108. !CBB_flush(&body)) {
  1109. return ssl_hs_error;
  1110. }
  1111. }
  1112. // Depending on the key exchange method, compute |pms|.
  1113. if (alg_k & SSL_kRSA) {
  1114. if (!pms.Init(SSL_MAX_MASTER_KEY_LENGTH)) {
  1115. return ssl_hs_error;
  1116. }
  1117. RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey.get());
  1118. if (rsa == NULL) {
  1119. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1120. return ssl_hs_error;
  1121. }
  1122. pms[0] = hs->client_version >> 8;
  1123. pms[1] = hs->client_version & 0xff;
  1124. if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
  1125. return ssl_hs_error;
  1126. }
  1127. CBB child, *enc_pms = &body;
  1128. size_t enc_pms_len;
  1129. // In TLS, there is a length prefix.
  1130. if (ssl->version > SSL3_VERSION) {
  1131. if (!CBB_add_u16_length_prefixed(&body, &child)) {
  1132. return ssl_hs_error;
  1133. }
  1134. enc_pms = &child;
  1135. }
  1136. uint8_t *ptr;
  1137. if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
  1138. !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms.data(),
  1139. pms.size(), RSA_PKCS1_PADDING) ||
  1140. !CBB_did_write(enc_pms, enc_pms_len) ||
  1141. !CBB_flush(&body)) {
  1142. return ssl_hs_error;
  1143. }
  1144. } else if (alg_k & SSL_kECDHE) {
  1145. // Generate a keypair and serialize the public half.
  1146. CBB child;
  1147. if (!CBB_add_u8_length_prefixed(&body, &child)) {
  1148. return ssl_hs_error;
  1149. }
  1150. // Compute the premaster.
  1151. uint8_t alert = SSL_AD_DECODE_ERROR;
  1152. if (!hs->key_share->Accept(&child, &pms, &alert, hs->peer_key)) {
  1153. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  1154. return ssl_hs_error;
  1155. }
  1156. if (!CBB_flush(&body)) {
  1157. return ssl_hs_error;
  1158. }
  1159. // The key exchange state may now be discarded.
  1160. hs->key_share.reset();
  1161. hs->peer_key.Reset();
  1162. } else if (alg_k & SSL_kPSK) {
  1163. // For plain PSK, other_secret is a block of 0s with the same length as
  1164. // the pre-shared key.
  1165. if (!pms.Init(psk_len)) {
  1166. return ssl_hs_error;
  1167. }
  1168. OPENSSL_memset(pms.data(), 0, pms.size());
  1169. } else {
  1170. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1171. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1172. return ssl_hs_error;
  1173. }
  1174. // For a PSK cipher suite, other_secret is combined with the pre-shared
  1175. // key.
  1176. if (alg_a & SSL_aPSK) {
  1177. ScopedCBB pms_cbb;
  1178. CBB child;
  1179. if (!CBB_init(pms_cbb.get(), 2 + psk_len + 2 + pms.size()) ||
  1180. !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
  1181. !CBB_add_bytes(&child, pms.data(), pms.size()) ||
  1182. !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
  1183. !CBB_add_bytes(&child, psk, psk_len) ||
  1184. !CBBFinishArray(pms_cbb.get(), &pms)) {
  1185. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1186. return ssl_hs_error;
  1187. }
  1188. }
  1189. // The message must be added to the finished hash before calculating the
  1190. // master secret.
  1191. if (!ssl_add_message_cbb(ssl, cbb.get())) {
  1192. return ssl_hs_error;
  1193. }
  1194. hs->new_session->master_key_length =
  1195. tls1_generate_master_secret(hs, hs->new_session->master_key, pms);
  1196. if (hs->new_session->master_key_length == 0) {
  1197. return ssl_hs_error;
  1198. }
  1199. hs->new_session->extended_master_secret = hs->extended_master_secret;
  1200. hs->state = state_send_client_certificate_verify;
  1201. return ssl_hs_ok;
  1202. }
  1203. static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) {
  1204. SSL *const ssl = hs->ssl;
  1205. if (!hs->cert_request || !ssl_has_certificate(ssl)) {
  1206. hs->state = state_send_client_finished;
  1207. return ssl_hs_ok;
  1208. }
  1209. assert(ssl_has_private_key(ssl));
  1210. ScopedCBB cbb;
  1211. CBB body, child;
  1212. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  1213. SSL3_MT_CERTIFICATE_VERIFY)) {
  1214. return ssl_hs_error;
  1215. }
  1216. uint16_t signature_algorithm;
  1217. if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
  1218. return ssl_hs_error;
  1219. }
  1220. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  1221. // Write out the digest type in TLS 1.2.
  1222. if (!CBB_add_u16(&body, signature_algorithm)) {
  1223. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1224. return ssl_hs_error;
  1225. }
  1226. }
  1227. // Set aside space for the signature.
  1228. const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
  1229. uint8_t *ptr;
  1230. if (!CBB_add_u16_length_prefixed(&body, &child) ||
  1231. !CBB_reserve(&child, &ptr, max_sig_len)) {
  1232. return ssl_hs_error;
  1233. }
  1234. size_t sig_len = max_sig_len;
  1235. // The SSL3 construction for CertificateVerify does not decompose into a
  1236. // single final digest and signature, and must be special-cased.
  1237. if (ssl_protocol_version(ssl) == SSL3_VERSION) {
  1238. if (ssl->cert->key_method != NULL) {
  1239. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
  1240. return ssl_hs_error;
  1241. }
  1242. uint8_t digest[EVP_MAX_MD_SIZE];
  1243. size_t digest_len;
  1244. if (!hs->transcript.GetSSL3CertVerifyHash(
  1245. digest, &digest_len, hs->new_session.get(), signature_algorithm)) {
  1246. return ssl_hs_error;
  1247. }
  1248. UniquePtr<EVP_PKEY_CTX> pctx(
  1249. EVP_PKEY_CTX_new(ssl->cert->privatekey.get(), nullptr));
  1250. if (!pctx ||
  1251. !EVP_PKEY_sign_init(pctx.get()) ||
  1252. !EVP_PKEY_sign(pctx.get(), ptr, &sig_len, digest, digest_len)) {
  1253. return ssl_hs_error;
  1254. }
  1255. } else {
  1256. switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
  1257. signature_algorithm,
  1258. hs->transcript.buffer())) {
  1259. case ssl_private_key_success:
  1260. break;
  1261. case ssl_private_key_failure:
  1262. return ssl_hs_error;
  1263. case ssl_private_key_retry:
  1264. hs->state = state_send_client_certificate_verify;
  1265. return ssl_hs_private_key_operation;
  1266. }
  1267. }
  1268. if (!CBB_did_write(&child, sig_len) ||
  1269. !ssl_add_message_cbb(ssl, cbb.get())) {
  1270. return ssl_hs_error;
  1271. }
  1272. // The handshake buffer is no longer necessary.
  1273. hs->transcript.FreeBuffer();
  1274. hs->state = state_send_client_finished;
  1275. return ssl_hs_ok;
  1276. }
  1277. static enum ssl_hs_wait_t do_send_client_finished(SSL_HANDSHAKE *hs) {
  1278. SSL *const ssl = hs->ssl;
  1279. // Resolve Channel ID first, before any non-idempotent operations.
  1280. if (ssl->s3->tlsext_channel_id_valid) {
  1281. if (!ssl_do_channel_id_callback(ssl)) {
  1282. return ssl_hs_error;
  1283. }
  1284. if (ssl->tlsext_channel_id_private == NULL) {
  1285. hs->state = state_send_client_finished;
  1286. return ssl_hs_channel_id_lookup;
  1287. }
  1288. }
  1289. if (!ssl->method->add_change_cipher_spec(ssl) ||
  1290. !tls1_change_cipher_state(hs, evp_aead_seal)) {
  1291. return ssl_hs_error;
  1292. }
  1293. if (hs->next_proto_neg_seen) {
  1294. static const uint8_t kZero[32] = {0};
  1295. size_t padding_len =
  1296. 32 - ((ssl->s3->next_proto_negotiated.size() + 2) % 32);
  1297. ScopedCBB cbb;
  1298. CBB body, child;
  1299. if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_NEXT_PROTO) ||
  1300. !CBB_add_u8_length_prefixed(&body, &child) ||
  1301. !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated.data(),
  1302. ssl->s3->next_proto_negotiated.size()) ||
  1303. !CBB_add_u8_length_prefixed(&body, &child) ||
  1304. !CBB_add_bytes(&child, kZero, padding_len) ||
  1305. !ssl_add_message_cbb(ssl, cbb.get())) {
  1306. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1307. return ssl_hs_error;
  1308. }
  1309. }
  1310. if (ssl->s3->tlsext_channel_id_valid) {
  1311. ScopedCBB cbb;
  1312. CBB body;
  1313. if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) ||
  1314. !tls1_write_channel_id(hs, &body) ||
  1315. !ssl_add_message_cbb(ssl, cbb.get())) {
  1316. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1317. return ssl_hs_error;
  1318. }
  1319. }
  1320. if (!ssl_send_finished(hs)) {
  1321. return ssl_hs_error;
  1322. }
  1323. hs->state = state_finish_flight;
  1324. return ssl_hs_flush;
  1325. }
  1326. static bool can_false_start(const SSL_HANDSHAKE *hs) {
  1327. SSL *const ssl = hs->ssl;
  1328. // False Start only for TLS 1.2 with an ECDHE+AEAD cipher.
  1329. if (SSL_is_dtls(ssl) ||
  1330. SSL_version(ssl) != TLS1_2_VERSION ||
  1331. hs->new_cipher->algorithm_mkey != SSL_kECDHE ||
  1332. hs->new_cipher->algorithm_mac != SSL_AEAD) {
  1333. return false;
  1334. }
  1335. // Additionally require ALPN or NPN by default.
  1336. //
  1337. // TODO(davidben): Can this constraint be relaxed globally now that cipher
  1338. // suite requirements have been relaxed?
  1339. if (!ssl->ctx->false_start_allowed_without_alpn &&
  1340. ssl->s3->alpn_selected.empty() &&
  1341. ssl->s3->next_proto_negotiated.empty()) {
  1342. return false;
  1343. }
  1344. return true;
  1345. }
  1346. static enum ssl_hs_wait_t do_finish_flight(SSL_HANDSHAKE *hs) {
  1347. SSL *const ssl = hs->ssl;
  1348. if (ssl->session != NULL) {
  1349. hs->state = state_finish_client_handshake;
  1350. return ssl_hs_ok;
  1351. }
  1352. // This is a full handshake. If it involves ChannelID, then record the
  1353. // handshake hashes at this point in the session so that any resumption of
  1354. // this session with ChannelID can sign those hashes.
  1355. if (!tls1_record_handshake_hashes_for_channel_id(hs)) {
  1356. return ssl_hs_error;
  1357. }
  1358. hs->state = state_read_session_ticket;
  1359. if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
  1360. can_false_start(hs) &&
  1361. // No False Start on renegotiation (would complicate the state machine).
  1362. !ssl->s3->initial_handshake_complete) {
  1363. hs->in_false_start = true;
  1364. hs->can_early_write = true;
  1365. return ssl_hs_early_return;
  1366. }
  1367. return ssl_hs_ok;
  1368. }
  1369. static enum ssl_hs_wait_t do_read_session_ticket(SSL_HANDSHAKE *hs) {
  1370. SSL *const ssl = hs->ssl;
  1371. if (!hs->ticket_expected) {
  1372. hs->state = state_process_change_cipher_spec;
  1373. return ssl_hs_read_change_cipher_spec;
  1374. }
  1375. SSLMessage msg;
  1376. if (!ssl->method->get_message(ssl, &msg)) {
  1377. return ssl_hs_read_message;
  1378. }
  1379. if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEW_SESSION_TICKET) ||
  1380. !ssl_hash_message(hs, msg)) {
  1381. return ssl_hs_error;
  1382. }
  1383. CBS new_session_ticket = msg.body, ticket;
  1384. uint32_t tlsext_tick_lifetime_hint;
  1385. if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
  1386. !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
  1387. CBS_len(&new_session_ticket) != 0) {
  1388. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1389. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1390. return ssl_hs_error;
  1391. }
  1392. if (CBS_len(&ticket) == 0) {
  1393. // RFC 5077 allows a server to change its mind and send no ticket after
  1394. // negotiating the extension. The value of |ticket_expected| is checked in
  1395. // |ssl_update_cache| so is cleared here to avoid an unnecessary update.
  1396. hs->ticket_expected = false;
  1397. ssl->method->next_message(ssl);
  1398. hs->state = state_process_change_cipher_spec;
  1399. return ssl_hs_read_change_cipher_spec;
  1400. }
  1401. SSL_SESSION *session = hs->new_session.get();
  1402. UniquePtr<SSL_SESSION> renewed_session;
  1403. if (ssl->session != NULL) {
  1404. // The server is sending a new ticket for an existing session. Sessions are
  1405. // immutable once established, so duplicate all but the ticket of the
  1406. // existing session.
  1407. renewed_session =
  1408. SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
  1409. if (!renewed_session) {
  1410. // This should never happen.
  1411. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1412. return ssl_hs_error;
  1413. }
  1414. session = renewed_session.get();
  1415. }
  1416. // |tlsext_tick_lifetime_hint| is measured from when the ticket was issued.
  1417. ssl_session_rebase_time(ssl, session);
  1418. if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
  1419. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1420. return ssl_hs_error;
  1421. }
  1422. session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
  1423. // Generate a session ID for this session based on the session ticket. We use
  1424. // the session ID mechanism for detecting ticket resumption. This also fits in
  1425. // with assumptions elsewhere in OpenSSL.
  1426. if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
  1427. session->session_id, &session->session_id_length,
  1428. EVP_sha256(), NULL)) {
  1429. return ssl_hs_error;
  1430. }
  1431. if (renewed_session) {
  1432. session->not_resumable = 0;
  1433. SSL_SESSION_free(ssl->session);
  1434. ssl->session = renewed_session.release();
  1435. }
  1436. ssl->method->next_message(ssl);
  1437. hs->state = state_process_change_cipher_spec;
  1438. return ssl_hs_read_change_cipher_spec;
  1439. }
  1440. static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
  1441. if (!tls1_change_cipher_state(hs, evp_aead_open)) {
  1442. return ssl_hs_error;
  1443. }
  1444. hs->state = state_read_server_finished;
  1445. return ssl_hs_ok;
  1446. }
  1447. static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) {
  1448. SSL *const ssl = hs->ssl;
  1449. enum ssl_hs_wait_t wait = ssl_get_finished(hs);
  1450. if (wait != ssl_hs_ok) {
  1451. return wait;
  1452. }
  1453. if (ssl->session != NULL) {
  1454. hs->state = state_send_client_finished;
  1455. return ssl_hs_ok;
  1456. }
  1457. hs->state = state_finish_client_handshake;
  1458. return ssl_hs_ok;
  1459. }
  1460. static enum ssl_hs_wait_t do_finish_client_handshake(SSL_HANDSHAKE *hs) {
  1461. SSL *const ssl = hs->ssl;
  1462. ssl->method->on_handshake_complete(ssl);
  1463. if (ssl->session != NULL) {
  1464. SSL_SESSION_up_ref(ssl->session);
  1465. ssl->s3->established_session.reset(ssl->session);
  1466. } else {
  1467. // We make a copy of the session in order to maintain the immutability
  1468. // of the new established_session due to False Start. The caller may
  1469. // have taken a reference to the temporary session.
  1470. ssl->s3->established_session =
  1471. SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_DUP_ALL);
  1472. if (!ssl->s3->established_session) {
  1473. return ssl_hs_error;
  1474. }
  1475. // Renegotiations do not participate in session resumption.
  1476. if (!ssl->s3->initial_handshake_complete) {
  1477. ssl->s3->established_session->not_resumable = 0;
  1478. }
  1479. hs->new_session.reset();
  1480. }
  1481. hs->handshake_finalized = true;
  1482. ssl->s3->initial_handshake_complete = true;
  1483. ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
  1484. hs->state = state_done;
  1485. return ssl_hs_ok;
  1486. }
  1487. enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs) {
  1488. while (hs->state != state_done) {
  1489. enum ssl_hs_wait_t ret = ssl_hs_error;
  1490. enum ssl_client_hs_state_t state =
  1491. static_cast<enum ssl_client_hs_state_t>(hs->state);
  1492. switch (state) {
  1493. case state_start_connect:
  1494. ret = do_start_connect(hs);
  1495. break;
  1496. case state_enter_early_data:
  1497. ret = do_enter_early_data(hs);
  1498. break;
  1499. case state_read_hello_verify_request:
  1500. ret = do_read_hello_verify_request(hs);
  1501. break;
  1502. case state_read_server_hello:
  1503. ret = do_read_server_hello(hs);
  1504. break;
  1505. case state_tls13:
  1506. ret = do_tls13(hs);
  1507. break;
  1508. case state_read_server_certificate:
  1509. ret = do_read_server_certificate(hs);
  1510. break;
  1511. case state_read_certificate_status:
  1512. ret = do_read_certificate_status(hs);
  1513. break;
  1514. case state_verify_server_certificate:
  1515. ret = do_verify_server_certificate(hs);
  1516. break;
  1517. case state_read_server_key_exchange:
  1518. ret = do_read_server_key_exchange(hs);
  1519. break;
  1520. case state_read_certificate_request:
  1521. ret = do_read_certificate_request(hs);
  1522. break;
  1523. case state_read_server_hello_done:
  1524. ret = do_read_server_hello_done(hs);
  1525. break;
  1526. case state_send_client_certificate:
  1527. ret = do_send_client_certificate(hs);
  1528. break;
  1529. case state_send_client_key_exchange:
  1530. ret = do_send_client_key_exchange(hs);
  1531. break;
  1532. case state_send_client_certificate_verify:
  1533. ret = do_send_client_certificate_verify(hs);
  1534. break;
  1535. case state_send_client_finished:
  1536. ret = do_send_client_finished(hs);
  1537. break;
  1538. case state_finish_flight:
  1539. ret = do_finish_flight(hs);
  1540. break;
  1541. case state_read_session_ticket:
  1542. ret = do_read_session_ticket(hs);
  1543. break;
  1544. case state_process_change_cipher_spec:
  1545. ret = do_process_change_cipher_spec(hs);
  1546. break;
  1547. case state_read_server_finished:
  1548. ret = do_read_server_finished(hs);
  1549. break;
  1550. case state_finish_client_handshake:
  1551. ret = do_finish_client_handshake(hs);
  1552. break;
  1553. case state_done:
  1554. ret = ssl_hs_ok;
  1555. break;
  1556. }
  1557. if (hs->state != state) {
  1558. ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1);
  1559. }
  1560. if (ret != ssl_hs_ok) {
  1561. return ret;
  1562. }
  1563. }
  1564. ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
  1565. return ssl_hs_ok;
  1566. }
  1567. const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs) {
  1568. enum ssl_client_hs_state_t state =
  1569. static_cast<enum ssl_client_hs_state_t>(hs->state);
  1570. switch (state) {
  1571. case state_start_connect:
  1572. return "TLS client start_connect";
  1573. case state_enter_early_data:
  1574. return "TLS client enter_early_data";
  1575. case state_read_hello_verify_request:
  1576. return "TLS client read_hello_verify_request";
  1577. case state_read_server_hello:
  1578. return "TLS client read_server_hello";
  1579. case state_tls13:
  1580. return tls13_client_handshake_state(hs);
  1581. case state_read_server_certificate:
  1582. return "TLS client read_server_certificate";
  1583. case state_read_certificate_status:
  1584. return "TLS client read_certificate_status";
  1585. case state_verify_server_certificate:
  1586. return "TLS client verify_server_certificate";
  1587. case state_read_server_key_exchange:
  1588. return "TLS client read_server_key_exchange";
  1589. case state_read_certificate_request:
  1590. return "TLS client read_certificate_request";
  1591. case state_read_server_hello_done:
  1592. return "TLS client read_server_hello_done";
  1593. case state_send_client_certificate:
  1594. return "TLS client send_client_certificate";
  1595. case state_send_client_key_exchange:
  1596. return "TLS client send_client_key_exchange";
  1597. case state_send_client_certificate_verify:
  1598. return "TLS client send_client_certificate_verify";
  1599. case state_send_client_finished:
  1600. return "TLS client send_client_finished";
  1601. case state_finish_flight:
  1602. return "TLS client finish_flight";
  1603. case state_read_session_ticket:
  1604. return "TLS client read_session_ticket";
  1605. case state_process_change_cipher_spec:
  1606. return "TLS client process_change_cipher_spec";
  1607. case state_read_server_finished:
  1608. return "TLS client read_server_finished";
  1609. case state_finish_client_handshake:
  1610. return "TLS client finish_client_handshake";
  1611. case state_done:
  1612. return "TLS client done";
  1613. }
  1614. return "TLS client unknown";
  1615. }
  1616. }