8000 ssl: hs_keylog callback properly handle alert in intial states by IngelaAndin · Pull Request #9849 · erlang/otp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

ssl: hs_keylog callback properly handle alert in intial states #9849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/ssl/src/ssl_gen_statem.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2246,9 +2246,9 @@ maybe_keylog_hs_callback(_, _, _) ->
ok.

keylog_hs_alert(start, _) -> %% TLS 1.3: No secrets yet established
[];
{[], undefined};
keylog_hs_alert(wait_sh, _) -> %% TLS 1.3: No secrets yet established
[];
{[], undefined};
%% Server alert for certificate validation can happen when client is in connection state already.
keylog_hs_alert(connection, #state{static_env = #static_env{role = client},
connection_env =
Expand Down
41 changes: 28 additions & 13 deletions lib/ssl/src/tls_handshake_1_3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -430,20 +430,17 @@ process_certificate_request(#certificate_request_1_3{
process_certificate(#certificate_1_3{
certificate_request_context = <<>>,
certificate_list = []},
#state{ssl_options =
#state{static_env = #static_env{role = server},
ssl_options =
#{fail_if_no_peer_cert := false}} = State) ->
{ok, {State, wait_finished}};
process_certificate(#certificate_1_3{
certificate_request_context = <<>>,
certificate_list = []},
#state{ssl_options =
#state{static_env = #static_env{role = server = Role},
ssl_options =
#{fail_if_no_peer_cert := true}} = State0) ->
%% At this point the client believes that the connection is up and starts using
%% its traffic secrets. In order to be able send an proper Alert to the client
%% the server should also change its connection state and use the traffic
%% secrets.
State1 = calculate_traffic_secrets(State0),
State = ssl_record:step_encryption_state(State1),
State = handle_alert_encryption_state(Role, State0),
{error, {?ALERT_REC(?FATAL, ?CERTIFICATE_REQUIRED, certificate_required), State}};
process_certificate(#certificate_1_3{certificate_list = CertEntries},
#state{ssl_options = SslOptions,
Expand All @@ -461,7 +458,7 @@ process_certificate(#certificate_1_3{certificate_list = CertEntries},
CertEntries, CertDbHandle, CertDbRef, SslOptions, CRLDbHandle, Role,
Host, StaplingState) of
#alert{} = Alert ->
State = update_encryption_state(Role, State0),
State = handle_alert_encryption_state(Role, State0),
{error, {Alert, State}};
{PeerCert, PublicKeyInfo} ->
State = store_peer_cert(State0, PeerCert, PublicKeyInfo),
Expand Down Expand Up @@ -801,15 +798,33 @@ build_content(Context, THash) ->


%% Sets correct encryption state when sending Alerts in shared states that use different secrets.
%% - If client: use handshake secrets.
%% - If server: use traffic secrets as by this time the client's state machine
%% already stepped into the 'connection' state.
update_encryption_state(server, State0) ->
handle_alert_encryption_state(server, State0) ->
State1 = calculate_traffic_secrets(State0),
ssl_record:step_encryption_state(State1);
update_encryption_state(client, State) ->
#state{ssl_options = Options,
connection_states = ConnectionStates,
protocol_specific = PS} = State = ssl_record:step_encryption_state(State1),
KeylogFun = maps:get(keep_secrets, Options, undefined),
maybe_keylog(KeylogFun, PS, ConnectionStates),
State;
%% - If client: use handshake secrets.
handle_alert_encryption_state(client, State) ->
State.

maybe_keylog({Keylog, Fun}, ProtocolSpecific, ConnectionStates) when Keylog == keylog_hs;
Keylog == keylog ->
N = maps:get(num_key_updates, ProtocolSpecific, 0),
#{security_parameters := #security_parameters{client_random = ClientRandom,
prf_algorithm = Prf,
application_traffic_secret = TrafficSecret}}
= ssl_record:current_connection_state(ConnectionStates, write),
TrafficKeyLog = ssl_logger:keylog_traffic_1_3(server, ClientRandom,
Prf, TrafficSecret, N),

ssl_logger:keylog(TrafficKeyLog, ClientRandom, Fun);
maybe_keylog(_,_,_) ->
ok.

validate_certificate_chain(CertEntries, CertDbHandle, CertDbRef,
SslOptions, CRLDbHandle, Role, Host, StaplingState) ->
Expand Down
47 changes: 36 additions & 11 deletions lib/ssl/test/tls_1_3_version_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -575,9 +575,9 @@ client_cert_fail_alert_passive(Config) when is_list(Config) ->
{certfile, NewClientCertFile} | proplists:delete(certfile, ClientOpts0)],
ServerOpts = [{verify, verify_peer}, {fail_if_no_peer_cert, true}| ServerOpts0],
alert_passive(ServerOpts, ClientOpts, recv,
ServerNode, Hostname),
ServerNode, Hostname, unknown_ca),
alert_passive(ServerOpts, ClientOpts, setopts,
ServerNode, Hostname).
ServerNode, Hostname, unknown_ca).

tls13_client_tls11_server() ->
[{doc,"Test that a TLS 1.3 client gets old server alert from TLS 1.0 server."}].
Expand Down Expand Up @@ -609,7 +609,34 @@ keylog_on_alert(Config) when is_list(Config) ->
Me ! {alert_info, AlertInfo}
end,
alert_passive([{keep_secrets, {keylog_hs, Fun}} | ServerOpts], ClientOpts, recv,
ServerNode, Hostname),
ServerNode, Hostname, unknown_ca),

receive_server_keylog_for_client_cert_alert(),

alert_passive(ServerOpts, [{keep_secrets, {keylog_hs, Fun}} | ClientOpts], recv,
ServerNode, Hostname, unknown_ca),

receive_client_keylog_for_client_cert_alert(),

ClientNoCert = proplists:delete(keyfile, proplists:delete(certfile, ClientOpts0)),
alert_passive([{keep_secrets, {keylog_hs, Fun}} | ServerOpts], [{active, false} | ClientNoCert], recv,
ServerNode, Hostname, certificate_required),

receive_server_keylog_for_client_cert_alert().

receive_server_keylog_for_client_cert_alert() ->
%% This alert will be decrypted with application secrets
%% as client is already in connection
receive
{alert_info, #{items := SKeyLog1}} ->
case SKeyLog1 of
["SERVER_TRAFFIC_SECRET_0"++_] ->
ok;
S1Other ->
ct:fail({server_received, S1Other})
end
end,

receive
{alert_info, #{items := SKeyLog}} ->
case SKeyLog of
Expand All @@ -618,20 +645,18 @@ keylog_on_alert(Config) when is_list(Config) ->
SOther ->
ct:fail({server_received, SOther})
end
end,
end.

alert_passive(ServerOpts, [{keep_secrets, {keylog_hs, Fun}} | ClientOpts], recv,
ServerNode, Hostname),
receive_client_keylog_for_client_cert_alert() ->
receive
{alert_info, #{items := CKeyLog}} ->
case CKeyLog of
["CLIENT_HANDSHAKE_TRAFFIC_SECRET"++_,_,_|_] ->
["CLIENT_HANDSHAKE_TRAFFIC_SECRET"++_,_,_,_|_] ->
ok;
COther ->
COther ->
ct:fail({client_received, COther})
end
end.

%%--------------------------------------------------------------------
%% Internal functions and callbacks -----------------------------------
%%--------------------------------------------------------------------
Expand Down Expand Up @@ -663,7 +688,7 @@ check_session_id(Socket, Expected) ->
end.

alert_passive(ServerOpts, ClientOpts, Function,
ServerNode, Hostname) ->
ServerNode, Hostname, AlertAtom) ->
Server = ssl_test_lib:start_server([{node, ServerNode}, {port, 0},
{from, self()},
{mfa, {ssl_test_lib, no_result, []}},
Expand All @@ -673,7 +698,7 @@ alert_passive(ServerOpts, ClientOpts, Function,
ct:sleep(500),
case Function of
recv ->
{error, {tls_alert, {unknown_ca,_}}} = ssl:recv(Socket, 0);
{error, {tls_alert, {AlertAtom,_}}} = ssl:recv(Socket, 0);
setopts ->
{error, {tls_alert, {unknown_ca,_}}} = ssl:setopts(Socket, [{active, once}])
end.
Expand Down
Loading
0