File: tls_server_session_ticket_SUITE.erl

package info (click to toggle)
erlang 1%3A25.2.3%2Bdfsg-1%2Bdeb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 219,972 kB
  • sloc: erlang: 1,440,803; xml: 473,412; ansic: 392,382; cpp: 164,287; makefile: 17,392; sh: 13,842; lisp: 9,675; java: 8,578; asm: 6,426; perl: 5,527; python: 5,469; javascript: 610; pascal: 126; sed: 72; php: 3
file content (238 lines) | stat: -rw-r--r-- 9,365 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2010-2022. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
-module(tls_server_session_ticket_SUITE).
-behaviour(ct_suite).

-include_lib("common_test/include/ct.hrl").
-include_lib("ssl/src/ssl_alert.hrl").
-include_lib("ssl/src/ssl_cipher.hrl").
-include_lib("ssl/src/ssl_internal.hrl").
-include_lib("ssl/src/tls_handshake_1_3.hrl").

%% Callback functions
-export([all/0,
         groups/0,
         init_per_suite/1,
         end_per_suite/1,
         init_per_group/2,
         end_per_group/2,
         init_per_testcase/2,
         end_per_testcase/2]).
%% Testcases
-export([expired_ticket_test/0,
         expired_ticket_test/1,
         invalid_ticket_test/0,
         invalid_ticket_test/1,
         main_test/0,
         main_test/1,
         misc_test/0,
         misc_test/1]).

-define(LIFETIME, 1). % tickets expire after 1s
-define(TICKET_STORE_SIZE, 1).
-define(MASTER_SECRET, "master_secret").
-define(PRF, sha).
-define(VERSION, {3,4}).
-define(PSK, <<15,168,18,43,216,33,227,142,114,190,70,183,137,57,64,64,66,152,115,94>>).

%%--------------------------------------------------------------------
%% Common Test interface functions -----------------------------------
%%--------------------------------------------------------------------
all() ->
    [{group, stateful}, {group, stateless}, {group, stateless_antireplay}].

groups() ->
    [{stateful, [], [main_test, expired_ticket_test, invalid_ticket_test]},
     {stateless, [], [expired_ticket_test, invalid_ticket_test, main_test]},
     {stateless_antireplay, [], [main_test, misc_test]}
    ].

init_per_suite(Config0) ->
    catch crypto:stop(),
    try crypto:start() of
	ok ->
            ssl_test_lib:clean_start(),
            Config0
    catch _:_ ->
	    {skip, "Crypto did not start"}
    end.

end_per_suite(_Config) ->
    ssl:stop(),
    application:stop(crypto).

init_per_group(stateless_antireplay, Config) ->
    check_environment([{server_session_tickets, stateless},
                       {anti_replay, {10, 20, 30}}]
                      ++ Config);
init_per_group(Group = stateless, Config) ->
    check_environment([{server_session_tickets, Group} | Config]);
init_per_group(Group = stateful, Config) ->
    [{server_session_tickets, Group} | Config].

end_per_group(_GroupName, Config) ->
    Config.

init_per_testcase(_TestCase, Config)  ->
    {ok, ListenSocket} = gen_tcp:listen(0, [{active, false}]),
    {ok, Pid} = tls_server_session_ticket:start_link(
                  ListenSocket, ?config(server_session_tickets, Config),
                  ?LIFETIME, ?TICKET_STORE_SIZE, _MaxEarlyDataSize = 100,
                  ?config(anti_replay, Config)),
    [{server_pid, Pid}, {listen_socket, ListenSocket} | Config].

end_per_testcase(_TestCase, Config) ->
    Pid = ?config(server_pid, Config),
    exit(Pid, normal),
    ListenSocket = ?config(listen_socket, Config),
    ok = gen_tcp:close(ListenSocket),
    Config.

%%--------------------------------------------------------------------
%% Test Cases --------------------------------------------------------
%%--------------------------------------------------------------------
main_test() ->
    [{doc, "Ticket main scenario"}].
main_test(Config) when is_list(Config) ->
    Pid = ?config(server_pid, Config),
    % Fill in GB tree store for stateful setup
    tls_server_session_ticket:new(Pid, ?PRF, ?MASTER_SECRET),
    % Reach ticket store size limit - force GB tree pruning
    SessionTicket = #new_session_ticket{} =
        tls_server_session_ticket:new(Pid, ?PRF, ?MASTER_SECRET),
    TicketRecvTime = erlang:system_time(millisecond),
    %% Sleep more than the ticket lifetime (which is in seconds) in
    %% milliseconds, to confirm that the client reported age (which is in
    %% milliseconds) is compared correctly with the lifetime
    ct:sleep(5 * ?LIFETIME),
    {HandshakeHist, OferredPsks} = get_handshake_hist(SessionTicket, TicketRecvTime, ?PSK),
    AcceptResponse = {ok, {0, ?PSK}},
    AcceptResponse = tls_server_session_ticket:use(Pid, OferredPsks, ?PRF,
                                      [iolist_to_binary(HandshakeHist)]),
    % check replay attempt result
    ExpReplyResult = get_replay_expected_result(Config, AcceptResponse),
    ExpReplyResult = tls_server_session_ticket:use(Pid, OferredPsks, ?PRF,
                                      [iolist_to_binary(HandshakeHist)]),
    true = is_process_alive(Pid).

invalid_ticket_test() ->
    [{doc, "Verify invalid tickets handling"}].
invalid_ticket_test(Config) when is_list(Config) ->
    Pid = ?config(server_pid, Config),
    #new_session_ticket{ticket=Ticket} =
        tls_server_session_ticket:new(Pid, ?PRF, ?MASTER_SECRET),
    Ids = [#psk_identity{identity = <<"wrongidentity">>,
                         obfuscated_ticket_age = 0},
           #psk_identity{identity = Ticket,
                         obfuscated_ticket_age = 0}],
    OfferedPSKs = #offered_psks{identities = Ids,
                                binders = [<<"wrongbinder">>, <<"wrongbinder">>]},
    HandshakeHist = tls_handshake:encode_handshake(
                      get_client_hello(OfferedPSKs), ?VERSION),
    ExpectedReason = get_alert_reason(Config),
    {error, #alert{level = ?FATAL,
                   description = ?ILLEGAL_PARAMETER,
                   role = undefined,
                   reason = ExpectedReason}} =
        tls_server_session_ticket:use(Pid, OfferedPSKs, ?PRF,
                                      [iolist_to_binary(HandshakeHist)]),
    true = is_process_alive(Pid).

expired_ticket_test() ->
    [{doc, "Expired ticket scenario"}].
expired_ticket_test(Config) when is_list(Config) ->
    Pid = ?config(server_pid, Config),
    SessionTicket = tls_server_session_ticket:new(Pid, ?PRF, ?MASTER_SECRET),
    TicketRecvTime = erlang:system_time(millisecond),
    ct:sleep({seconds, 2 * ?LIFETIME}),
    {HandshakeHist, OFPSKs} = get_handshake_hist(SessionTicket, TicketRecvTime, ?PSK),
    {ok, undefined} = tls_server_session_ticket:use(Pid, OFPSKs, ?PRF,
                                      [iolist_to_binary(HandshakeHist)]),
    true = is_process_alive(Pid).

misc_test() ->
    [{doc, "Miscellaneous functionality"}].
misc_test(Config) when is_list(Config) ->
    Pid = ?config(server_pid, Config),
    ok = gen_server:cast(Pid, some_request),
    Pid ! rotate_bloom_filters,
    Pid ! general_handle_info,
    {ok, state} = tls_server_session_ticket:code_change(old_version, state, extra),
    Pid = tls_server_session_ticket:format_status(not_relevant, Pid),
    true = is_process_alive(Pid).

%%--------------------------------------------------------------------
%% Helpers -----------------------------------------------------------
%%--------------------------------------------------------------------
get_handshake_hist(#new_session_ticket{} = T, TicketRecvTime, PSK0) ->
    M = #{cipher_suite => {nothing, ?PRF},
          sni => nothing,
          psk => PSK0,
          timestamp => TicketRecvTime,
          ticket => T},
    TicketData = tls_handshake_1_3:get_ticket_data(self(), manual, [M]),
    [#ticket_data{identity = Identity}] = TicketData,
    SomeBinder = <<159, 187, 86, 6, 55, 20, 149, 208, 3, 221, 78, 126, 254, 101,
                   123, 251, 151, 189, 17, 53>>,
    OfferedPSKs0 = #offered_psks{identities = [Identity], binders = [SomeBinder]},
    Hello0 = get_client_hello(OfferedPSKs0),
    Hello1 = tls_handshake_1_3:maybe_add_binders(Hello0, TicketData, ?VERSION),
    PSK1 =  maps:get(pre_shared_key, Hello1#client_hello.extensions),
    OfferedPSKs1 = PSK1#pre_shared_key_client_hello.offered_psks,
    {tls_handshake:encode_handshake(Hello1, ?VERSION), OfferedPSKs1}.

get_client_hello(OfferedPSKs) ->
    PreSharedKey = #pre_shared_key_client_hello{offered_psks = OfferedPSKs},
    Ext0 = ssl_handshake:empty_extensions(?VERSION, client_hello),
    #client_hello{
       client_version = ?VERSION,
       random = <<1:256>>,
       session_id = <<>>,
       cipher_suites = [?TLS_AES_256_GCM_SHA384],
       compression_methods = "",
       extensions = Ext0#{pre_shared_key => PreSharedKey}}.

get_replay_expected_result(Config, AcceptResponse) ->
    case get_group(Config) of
        stateless ->
            % no protection - replayed ticket is accepted
            AcceptResponse;
        _ ->
            {ok, undefined}
    end.

get_alert_reason(Config) ->
    case get_group(Config) of
        stateful ->
            stateful;
        _ ->
            stateless
    end.

get_group(Config) ->
    proplists:get_value(name, ?config(tc_group_properties, Config)).

check_environment(T) ->
    case ssl_test_lib:sufficient_crypto_support('tlsv1.3') of
        true ->
            T;
        _ ->
            {skip, insufficient_environment}
    end.