2 * Copyright (C) 2000-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * The GnuTLS is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 /* This file contains functions that manipulate a database backend for
27 #include "gnutls_int.h"
28 #include "gnutls_errors.h"
29 #include <gnutls_db.h>
30 #include <gnutls_session_pack.h>
31 #include <gnutls_datum.h>
34 * gnutls_db_set_retrieve_function:
35 * @session: is a #gnutls_session_t structure.
36 * @retr_func: is the function.
38 * Sets the function that will be used to retrieve data from the
39 * resumed sessions database. This function must return a
40 * gnutls_datum_t containing the data on success, or a gnutls_datum_t
41 * containing null and 0 on failure.
43 * The datum's data must be allocated using the function
46 * The first argument to @retr_func will be null unless
47 * gnutls_db_set_ptr() has been called.
50 gnutls_db_set_retrieve_function (gnutls_session_t session
,
51 gnutls_db_retr_func retr_func
)
53 session
->internals
.db_retrieve_func
= retr_func
;
57 * gnutls_db_set_remove_function:
58 * @session: is a #gnutls_session_t structure.
59 * @rem_func: is the function.
61 * Sets the function that will be used to remove data from the
62 * resumed sessions database. This function must return 0 on success.
64 * The first argument to @rem_func will be null unless
65 * gnutls_db_set_ptr() has been called.
68 gnutls_db_set_remove_function (gnutls_session_t session
,
69 gnutls_db_remove_func rem_func
)
71 session
->internals
.db_remove_func
= rem_func
;
75 * gnutls_db_set_store_function:
76 * @session: is a #gnutls_session_t structure.
77 * @store_func: is the function
79 * Sets the function that will be used to store data in the resumed
80 * sessions database. This function must return 0 on success.
82 * The first argument to @store_func will be null unless
83 * gnutls_db_set_ptr() has been called.
86 gnutls_db_set_store_function (gnutls_session_t session
,
87 gnutls_db_store_func store_func
)
89 session
->internals
.db_store_func
= store_func
;
94 * @session: is a #gnutls_session_t structure.
95 * @ptr: is the pointer
97 * Sets the pointer that will be provided to db store, retrieve and
98 * delete functions, as the first argument.
101 gnutls_db_set_ptr (gnutls_session_t session
, void *ptr
)
103 session
->internals
.db_ptr
= ptr
;
108 * @session: is a #gnutls_session_t structure.
110 * Get db function pointer.
112 * Returns: the pointer that will be sent to db store, retrieve and
113 * delete functions, as the first argument.
116 gnutls_db_get_ptr (gnutls_session_t session
)
118 return session
->internals
.db_ptr
;
122 * gnutls_db_set_cache_expiration:
123 * @session: is a #gnutls_session_t structure.
124 * @seconds: is the number of seconds.
126 * Set the expiration time for resumed sessions. The default is 3600
127 * (one hour) at the time of this writing.
130 gnutls_db_set_cache_expiration (gnutls_session_t session
, int seconds
)
132 session
->internals
.expire_time
= seconds
;
136 * gnutls_db_check_entry:
137 * @session: is a #gnutls_session_t structure.
138 * @session_entry: is the session data (not key)
140 * Check if database entry has expired. This function is to be used
141 * when you want to clear unnecessary sessions which occupy space in
144 * Returns: Returns %GNUTLS_E_EXPIRED, if the database entry has
145 * expired or 0 otherwise.
148 gnutls_db_check_entry (gnutls_session_t session
, gnutls_datum_t session_entry
)
152 timestamp
= gnutls_time (0);
154 if (session_entry
.data
!= NULL
)
156 ((security_parameters_st
*) (session_entry
.data
))->timestamp
<=
157 session
->internals
.expire_time
158 || ((security_parameters_st
*) (session_entry
.data
))->timestamp
>
160 || ((security_parameters_st
*) (session_entry
.data
))->timestamp
== 0)
161 return GNUTLS_E_EXPIRED
;
166 /* Checks if both db_store and db_retrieve functions have
170 db_func_is_ok (gnutls_session_t session
)
172 if (session
->internals
.db_store_func
!= NULL
&&
173 session
->internals
.db_retrieve_func
!= NULL
&&
174 session
->internals
.db_remove_func
!= NULL
)
177 return GNUTLS_E_DB_ERROR
;
180 /* Stores session data to the db backend.
183 store_session (gnutls_session_t session
,
184 gnutls_datum_t session_id
,
185 gnutls_datum_t session_data
)
189 if (session
->internals
.resumable
== RESUME_FALSE
)
192 return GNUTLS_E_INVALID_SESSION
;
195 if (db_func_is_ok (session
) != 0)
197 return GNUTLS_E_DB_ERROR
;
200 if (session_id
.data
== NULL
|| session_id
.size
== 0)
203 return GNUTLS_E_INVALID_SESSION
;
206 if (session_data
.data
== NULL
|| session_data
.size
== 0)
209 return GNUTLS_E_INVALID_SESSION
;
212 /* if we can't read why bother writing? */
213 if (session
->internals
.db_store_func
!= NULL
)
214 ret
= session
->internals
.db_store_func (session
->internals
.db_ptr
,
215 session_id
, session_data
);
217 return (ret
== 0 ? ret
: GNUTLS_E_DB_ERROR
);
221 _gnutls_server_register_current_session (gnutls_session_t session
)
224 gnutls_datum_t content
;
227 key
.data
= session
->security_parameters
.session_id
;
228 key
.size
= session
->security_parameters
.session_id_size
;
230 if (session
->internals
.resumable
== RESUME_FALSE
)
233 return GNUTLS_E_INVALID_SESSION
;
236 if (session
->security_parameters
.session_id
== NULL
237 || session
->security_parameters
.session_id_size
== 0)
240 return GNUTLS_E_INVALID_SESSION
;
243 ret
= _gnutls_session_pack (session
, &content
);
250 ret
= store_session (session
, key
, content
);
251 _gnutls_free_datum (&content
);
256 /* Retrieves session data from the db backend.
258 static gnutls_datum_t
259 retrieve_session (gnutls_session_t session
, gnutls_datum_t session_id
)
261 gnutls_datum_t ret
= { NULL
, 0 };
263 if (session_id
.data
== NULL
|| session_id
.size
== 0)
269 if (session
->internals
.db_retrieve_func
!= NULL
)
270 ret
= session
->internals
.db_retrieve_func (session
->internals
.db_ptr
,
277 _gnutls_server_restore_session (gnutls_session_t session
,
278 uint8_t * session_id
, int session_id_size
)
284 key
.data
= session_id
;
285 key
.size
= session_id_size
;
287 if (db_func_is_ok (session
) != 0)
290 return GNUTLS_E_INVALID_SESSION
;
293 data
= retrieve_session (session
, key
);
295 if (data
.data
== NULL
)
298 return GNUTLS_E_INVALID_SESSION
;
301 /* expiration check is performed inside */
302 ret
= gnutls_session_set_data (session
, data
.data
, data
.size
);
309 gnutls_free (data
.data
);
315 * gnutls_db_remove_session:
316 * @session: is a #gnutls_session_t structure.
318 * This function will remove the current session data from the
319 * session database. This will prevent future handshakes reusing
320 * these session data. This function should be called if a session
321 * was terminated abnormally, and before gnutls_deinit() is called.
323 * Normally gnutls_deinit() will remove abnormally terminated
327 gnutls_db_remove_session (gnutls_session_t session
)
329 gnutls_datum_t session_id
;
332 session_id
.data
= session
->security_parameters
.session_id
;
333 session_id
.size
= session
->security_parameters
.session_id_size
;
335 if (db_func_is_ok (session
) != 0)
338 return /* GNUTLS_E_DB_ERROR */;
341 if (session_id
.data
== NULL
|| session_id
.size
== 0)
344 return /* GNUTLS_E_INVALID_SESSION */;
347 /* if we can't read why bother writing? */
348 if (session
->internals
.db_remove_func
!= NULL
)
350 ret
= session
->internals
.db_remove_func (session
->internals
.db_ptr
,