summaryrefslogtreecommitdiff
path: root/lib/ssl/src/ssl_server_session_cache.erl
blob: a89959413cef9e312c1007c4f378731611df406e (plain)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2020-2020. 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%
%%

%%----------------------------------------------------------------------
%% Purpose: Handle server side pre TLS-1.3 reuse session storage.
%% This implements the RFC session reuse and not the session ticket extension
%% that inspired the RFC session tickets of TLS-1.3.
%%----------------------------------------------------------------------

-module(ssl_server_session_cache).
-behaviour(gen_server).

-include_lib("kernel/include/logger.hrl").
-include("ssl_handshake.hrl").
-include("ssl_internal.hrl").

%% API
-export([start_link/2,
         new_session_id/1,
         register_session/2,
         reuse_session/2
        ]).

%% gen_server callbacks
-export([init/1,
         handle_call/3,
         handle_cast/2,
         handle_info/2,
         terminate/2
         %%code_change/3,
         %%format_status/2
        ]).

-record(state, {store_cb,
                lifetime,
                db,
                max,
                session_order,
                id_generator,
                listner
               }).

%%%===================================================================
%%% API
%%%===================================================================

-spec start_link(pid(), map()) -> {ok, Pid :: pid()} |
                      {error, Error :: {already_started, pid()}} |
                      {error, Error :: term()} |
                      ignore.
start_link(ssl_unknown_listener = Listner, Map) ->
    gen_server:start_link({local, Listner}, ?MODULE, [Listner, Map], []);
start_link(Listner, Map) ->
    gen_server:start_link(?MODULE, [Listner, Map], []).

%%--------------------------------------------------------------------
-spec new_session_id(Pid::pid()) -> ssl:session_id().
%%
%% Description: Creates a session id for the server.
%%--------------------------------------------------------------------
new_session_id(Pid) ->
    case call(Pid, new_session_id) of
        {no_server, _} ->
            crypto:strong_rand_bytes(32);
        Result ->
            Result
    end.

%%--------------------------------------------------------------------
-spec reuse_session(pid(), ssl:session_id()) ->  #session{} | not_reusable.
%%
%% Description: Returns session to reuse
%%--------------------------------------------------------------------
reuse_session(Pid, SessionId) ->
    case call(Pid, {reuse_session, SessionId}) of
        {no_server, _} ->
            not_reusable;
        Result ->
            Result
    end.
%%--------------------------------------------------------------------
-spec register_session(pid(), term()) -> ok.
%%
%% Description: Makes a session available for reuse
%%--------------------------------------------------------------------
register_session(Pid, Session) ->
    gen_server:cast(Pid, {register_session, Session}).


%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
-spec init(Args :: term()) -> {ok, State :: term()}.
init([Listner, #{lifetime := Lifetime,
                 session_cb := Cb,
                 session_cb_init_args := InitArgs,
                 max := Max
                }]) ->
    process_flag(trap_exit, true),
    Monitor = monitor_listener(Listner),
    DbRef = init(Cb, [{role, server} | InitArgs]),
    State = #state{store_cb = Cb,
                   lifetime = Lifetime,
                   db = DbRef,
                   max = Max,
                   session_order = gb_trees:empty(),
                   id_generator = crypto:strong_rand_bytes(16),
                   listner = Monitor
                  },
    {ok, State}.

-spec handle_call(Request :: term(), From :: {pid(), term()}, State :: term()) ->
                         {reply, Reply :: term(), NewState :: term()} .
handle_call(new_session_id, _From, #state{id_generator = IdGen} = State) ->
    SessionId = session_id(IdGen),
    {reply, SessionId, State};
handle_call({reuse_session, SessionId}, _From,  #state{store_cb = Cb,
                                                       db = Store0,
                                                       lifetime = Lifetime,
                                                       session_order = Order0} = State0) ->
    case lookup(Cb, Store0, SessionId) of
        undefined ->
            {reply, not_reusable, State0};
        #session{internal_id = InId} = Session ->
            case ssl_session:valid_session(Session, Lifetime) of
                true ->
                    {reply, Session, State0};
                false ->
                    {Store, Order} = invalidate_session(Cb, Store0, Order0, SessionId, InId),
                    {reply, not_reusable, State0#state{db = Store, session_order = Order}}
            end
    end.

-spec handle_cast(Request :: term(), State :: term()) ->
           {noreply, NewState :: term()}.
handle_cast({register_session, #session{session_id = SessionId, time_stamp = TimeStamp} = Session0},
            #state{store_cb = Cb,
                   db = Store0,
                   max = Max,
                   lifetime = Lifetime,
                   session_order = Order0}
            = State0) ->
    InternalId = {TimeStamp, erlang:unique_integer([monotonic])},
    Session = Session0#session{internal_id = InternalId},
    State = case size(Cb, Store0) of
                Max ->
                    %% Throw away oldest session table may not grow larger than max
                    {_, OldSessId, Order1} = gb_trees:take_smallest(Order0),
                    Store1 = delete(Cb, Store0, OldSessId),
                    %% Insert new session
                    Order = gb_trees:insert(InternalId, SessionId, Order1),
                    Store = update(Cb, Store1, SessionId, Session),
                    State0#state{db = Store, session_order = Order};
                Size when Size > 0 ->
                    {_, OldSessId, Order1} = gb_trees:take_smallest(Order0),
                    OldestSession = lookup(Cb, Store0, OldSessId),
                    case ssl_session:valid_session(OldestSession, Lifetime) of
                        true ->
                            Store = update(Cb, Store0, SessionId, Session#session{time_stamp = TimeStamp}),
                            State0#state{db = Store,
                                         session_order = gb_trees:insert(InternalId, SessionId, Order0)};
                        false ->
                            %% Throw away oldest session as it is not valid anymore
                            Store1 = delete(Cb, Store0, OldSessId),
                            Store = update(Cb, Store1, SessionId, Session#session{time_stamp = TimeStamp}),
                            State0#state{db = Store,
                                         session_order =  gb_trees:insert(InternalId, SessionId, Order1)}
                    end;
                0 ->
                    Store = update(Cb, Store0, SessionId, Session#session{time_stamp = TimeStamp}),
                    State0#state{db = Store,
                                 session_order = gb_trees:insert(InternalId, SessionId, Order0)}
            end,
    {noreply, State}.

-spec handle_info(Info :: timeout() | term(), State :: term()) ->
          {noreply, NewState :: term()}.
handle_info({'DOWN', Monitor, _, _, _}, #state{listner = Monitor} = State) ->
     {stop, normal, State};
handle_info(_, State) ->
    {noreply, State}.

terminate(_, _) ->
    ok.

%%%===================================================================
%%% Internal functions
%%%===================================================================
call(Pid, Msg) ->
    try gen_server:call(Pid, Msg, infinity)
    catch
	exit:Reason ->
	    {no_server, Reason}
    end.

session_id(Key) ->
    Unique1 = erlang:unique_integer(),
    Unique2 = erlang:unique_integer(),
    %% Obfuscate to avoid DoS attack possiblities
    %% This id should be unpredictable an 32 bytes
    %% and unique but have no other cryptographic requirements.
    Bin1 = crypto:crypto_one_time(aes_128_ecb, Key, <<Unique1:128>>, true),
    Bin2 = crypto:crypto_one_time(aes_128_ecb, Key, <<Unique2:128>>, true),
    <<Bin1/binary, Bin2/binary>>.

invalidate_session(Cb, Store0, Order, SessionId, InternalId) ->
    Store = delete(Cb, Store0, SessionId),
    {Store, gb_trees:delete(InternalId, Order)}.

init(Cb, Options) ->
    Cb:init(Options).

lookup(Cb, Cache, Key) ->
    Cb:lookup(Cache, Key).

update(ssl_server_session_cache_db = Cb, Cache, Key, Session) ->
    Cb:update(Cache, Key, Session);
update(Cb, Cache, Key, Session) ->
    Cb:update(Cache, Key, Session),
    Cache.

delete(ssl_server_session_cache_db = Cb, Cache, Key) ->
    Cb:delete(Cache, Key);
delete(Cb, Cache, Key) ->
    Cb:delete(Cache, Key),
    Cache.

size(Cb,Cache) ->
    try Cb:size(Cache) of
        Size ->
            Size
    catch
        error:undef ->
            Cb:foldl(fun(_, Acc) -> Acc + 1 end, 0, Cache)
    end.

monitor_listener(ssl_unknown_listener) ->
    %% Backwards compatible Erlang node
    %% global process.
    undefined;
monitor_listener(Listen) ->
    inet:monitor(Listen).