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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
/*
* Copyright (C) 2000-2011 Free Software Foundation, Inc.
*
* Author: Nikos Mavrogiannopoulos
*
* This file is part of GnuTLS.
*
* The GnuTLS is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
/* This file contains functions that manipulate a database backend for
* resumed sessions.
*/
#include "gnutls_int.h"
#include "gnutls_errors.h"
#include <gnutls_db.h>
#include <gnutls_session_pack.h>
#include <gnutls_datum.h>
/**
* gnutls_db_set_retrieve_function:
* @session: is a #gnutls_session_t structure.
* @retr_func: is the function.
*
* Sets the function that will be used to retrieve data from the
* resumed sessions database. This function must return a
* gnutls_datum_t containing the data on success, or a gnutls_datum_t
* containing null and 0 on failure.
*
* The datum's data must be allocated using the function
* gnutls_malloc().
*
* The first argument to @retr_func will be null unless
* gnutls_db_set_ptr() has been called.
**/
void
gnutls_db_set_retrieve_function (gnutls_session_t session,
gnutls_db_retr_func retr_func)
{
session->internals.db_retrieve_func = retr_func;
}
/**
* gnutls_db_set_remove_function:
* @session: is a #gnutls_session_t structure.
* @rem_func: is the function.
*
* Sets the function that will be used to remove data from the
* resumed sessions database. This function must return 0 on success.
*
* The first argument to @rem_func will be null unless
* gnutls_db_set_ptr() has been called.
**/
void
gnutls_db_set_remove_function (gnutls_session_t session,
gnutls_db_remove_func rem_func)
{
session->internals.db_remove_func = rem_func;
}
/**
* gnutls_db_set_store_function:
* @session: is a #gnutls_session_t structure.
* @store_func: is the function
*
* Sets the function that will be used to store data in the resumed
* sessions database. This function must return 0 on success.
*
* The first argument to @store_func will be null unless
* gnutls_db_set_ptr() has been called.
**/
void
gnutls_db_set_store_function (gnutls_session_t session,
gnutls_db_store_func store_func)
{
session->internals.db_store_func = store_func;
}
/**
* gnutls_db_set_ptr:
* @session: is a #gnutls_session_t structure.
* @ptr: is the pointer
*
* Sets the pointer that will be provided to db store, retrieve and
* delete functions, as the first argument.
**/
void
gnutls_db_set_ptr (gnutls_session_t session, void *ptr)
{
session->internals.db_ptr = ptr;
}
/**
* gnutls_db_get_ptr:
* @session: is a #gnutls_session_t structure.
*
* Get db function pointer.
*
* Returns: the pointer that will be sent to db store, retrieve and
* delete functions, as the first argument.
**/
void *
gnutls_db_get_ptr (gnutls_session_t session)
{
return session->internals.db_ptr;
}
/**
* gnutls_db_set_cache_expiration:
* @session: is a #gnutls_session_t structure.
* @seconds: is the number of seconds.
*
* Set the expiration time for resumed sessions. The default is 3600
* (one hour) at the time of this writing.
**/
void
gnutls_db_set_cache_expiration (gnutls_session_t session, int seconds)
{
session->internals.expire_time = seconds;
}
/**
* gnutls_db_check_entry:
* @session: is a #gnutls_session_t structure.
* @session_entry: is the session data (not key)
*
* Check if database entry has expired. This function is to be used
* when you want to clear unnecessary sessions which occupy space in
* your backend.
*
* Returns: Returns %GNUTLS_E_EXPIRED, if the database entry has
* expired or 0 otherwise.
**/
int
gnutls_db_check_entry (gnutls_session_t session, gnutls_datum_t session_entry)
{
time_t timestamp;
timestamp = gnutls_time (0);
if (session_entry.data != NULL)
if (timestamp -
((security_parameters_st *) (session_entry.data))->timestamp <=
session->internals.expire_time
|| ((security_parameters_st *) (session_entry.data))->timestamp >
timestamp
|| ((security_parameters_st *) (session_entry.data))->timestamp == 0)
return GNUTLS_E_EXPIRED;
return 0;
}
/* Checks if both db_store and db_retrieve functions have
* been set up.
*/
static int
db_func_is_ok (gnutls_session_t session)
{
if (session->internals.db_store_func != NULL &&
session->internals.db_retrieve_func != NULL &&
session->internals.db_remove_func != NULL)
return 0;
else
return GNUTLS_E_DB_ERROR;
}
/* Stores session data to the db backend.
*/
static int
store_session (gnutls_session_t session,
gnutls_datum_t session_id,
gnutls_datum_t session_data)
{
int ret = 0;
if (session->internals.resumable == RESUME_FALSE)
{
gnutls_assert ();
return GNUTLS_E_INVALID_SESSION;
}
if (db_func_is_ok (session) != 0)
{
return GNUTLS_E_DB_ERROR;
}
if (session_id.data == NULL || session_id.size == 0)
{
gnutls_assert ();
return GNUTLS_E_INVALID_SESSION;
}
if (session_data.data == NULL || session_data.size == 0)
{
gnutls_assert ();
return GNUTLS_E_INVALID_SESSION;
}
/* if we can't read why bother writing? */
if (session->internals.db_store_func != NULL)
ret = session->internals.db_store_func (session->internals.db_ptr,
session_id, session_data);
return (ret == 0 ? ret : GNUTLS_E_DB_ERROR);
}
int
_gnutls_server_register_current_session (gnutls_session_t session)
{
gnutls_datum_t key;
gnutls_datum_t content;
int ret = 0;
key.data = session->security_parameters.session_id;
key.size = session->security_parameters.session_id_size;
if (session->internals.resumable == RESUME_FALSE)
{
gnutls_assert ();
return GNUTLS_E_INVALID_SESSION;
}
if (session->security_parameters.session_id == NULL
|| session->security_parameters.session_id_size == 0)
{
gnutls_assert ();
return GNUTLS_E_INVALID_SESSION;
}
ret = _gnutls_session_pack (session, &content);
if (ret < 0)
{
gnutls_assert ();
return ret;
}
ret = store_session (session, key, content);
_gnutls_free_datum (&content);
return ret;
}
/* Retrieves session data from the db backend.
*/
static gnutls_datum_t
retrieve_session (gnutls_session_t session, gnutls_datum_t session_id)
{
gnutls_datum_t ret = { NULL, 0 };
if (session_id.data == NULL || session_id.size == 0)
{
gnutls_assert ();
return ret;
}
if (session->internals.db_retrieve_func != NULL)
ret = session->internals.db_retrieve_func (session->internals.db_ptr,
session_id);
return ret;
}
int
_gnutls_server_restore_session (gnutls_session_t session,
uint8_t * session_id, int session_id_size)
{
gnutls_datum_t data;
gnutls_datum_t key;
int ret;
key.data = session_id;
key.size = session_id_size;
if (db_func_is_ok (session) != 0)
{
gnutls_assert ();
return GNUTLS_E_INVALID_SESSION;
}
data = retrieve_session (session, key);
if (data.data == NULL)
{
gnutls_assert ();
return GNUTLS_E_INVALID_SESSION;
}
/* expiration check is performed inside */
ret = gnutls_session_set_data (session, data.data, data.size);
if (ret < 0)
{
gnutls_assert ();
return ret;
}
gnutls_free (data.data);
return 0;
}
/**
* gnutls_db_remove_session:
* @session: is a #gnutls_session_t structure.
*
* This function will remove the current session data from the
* session database. This will prevent future handshakes reusing
* these session data. This function should be called if a session
* was terminated abnormally, and before gnutls_deinit() is called.
*
* Normally gnutls_deinit() will remove abnormally terminated
* sessions.
**/
void
gnutls_db_remove_session (gnutls_session_t session)
{
gnutls_datum_t session_id;
int ret = 0;
session_id.data = session->security_parameters.session_id;
session_id.size = session->security_parameters.session_id_size;
if (db_func_is_ok (session) != 0)
{
gnutls_assert ();
return /* GNUTLS_E_DB_ERROR */;
}
if (session_id.data == NULL || session_id.size == 0)
{
gnutls_assert ();
return /* GNUTLS_E_INVALID_SESSION */;
}
/* if we can't read why bother writing? */
if (session->internals.db_remove_func != NULL)
{
ret = session->internals.db_remove_func (session->internals.db_ptr,
session_id);
if (ret != 0)
gnutls_assert ();
}
}
|