Added gnutls_session_get_id2().
[gnutls.git] / lib / gnutls_session.c
blob80d984cbebe7eb2355118a8e9643db4eb731082e
1 /*
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/>
22 #include "gnutls_int.h"
23 #include "gnutls_errors.h"
24 #include "debug.h"
25 #include <gnutls_session_pack.h>
26 #include <gnutls_datum.h>
28 /**
29 * gnutls_session_get_data:
30 * @session: is a #gnutls_session_t structure.
31 * @session_data: is a pointer to space to hold the session.
32 * @session_data_size: is the session_data's size, or it will be set by the function.
34 * Returns all session parameters, in order to support resuming. The
35 * client should call this, and keep the returned session, if he
36 * wants to resume that current version later by calling
37 * gnutls_session_set_data() This function must be called after a
38 * successful handshake.
40 * Resuming sessions is really useful and speedups connections after
41 * a successful one.
43 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
44 * an error code is returned.
45 **/
46 int
47 gnutls_session_get_data (gnutls_session_t session,
48 void *session_data, size_t * session_data_size)
51 gnutls_datum_t psession;
52 int ret;
54 if (session->internals.resumable == RESUME_FALSE)
55 return GNUTLS_E_INVALID_SESSION;
57 psession.data = session_data;
59 ret = _gnutls_session_pack (session, &psession);
60 if (ret < 0)
62 gnutls_assert ();
63 return ret;
66 if (psession.size > *session_data_size)
68 *session_data_size = psession.size;
69 ret = GNUTLS_E_SHORT_MEMORY_BUFFER;
70 goto error;
72 *session_data_size = psession.size;
74 if (session_data != NULL)
75 memcpy (session_data, psession.data, psession.size);
77 ret = 0;
79 error:
80 _gnutls_free_datum (&psession);
81 return ret;
84 /**
85 * gnutls_session_get_data2:
86 * @session: is a #gnutls_session_t structure.
87 * @data: is a pointer to a datum that will hold the session.
89 * Returns all session parameters, in order to support resuming. The
90 * client should call this, and keep the returned session, if he wants
91 * to resume that current version later by calling
92 * gnutls_session_set_data(). This function must be called after a
93 * successful handshake. The returned datum must be freed with
94 * gnutls_free().
96 * Resuming sessions is really useful and speedups connections after
97 * a successful one.
99 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
100 * an error code is returned.
103 gnutls_session_get_data2 (gnutls_session_t session, gnutls_datum_t * data)
106 int ret;
108 if (data == NULL)
110 return GNUTLS_E_INVALID_REQUEST;
113 if (session->internals.resumable == RESUME_FALSE)
114 return GNUTLS_E_INVALID_SESSION;
116 ret = _gnutls_session_pack (session, data);
117 if (ret < 0)
119 gnutls_assert ();
120 return ret;
123 return 0;
128 * gnutls_session_get_id:
129 * @session: is a #gnutls_session_t structure.
130 * @session_id: is a pointer to space to hold the session id.
131 * @session_id_size: initially should contain the maximum @session_id size and will be updated.
133 * Returns the current session ID. This can be used if you want to
134 * check if the next session you tried to resume was actually
135 * resumed. That is because resumed sessions share the same session ID
136 * with the original session.
138 * The session ID is selected by the server, that identify the
139 * current session. In TLS 1.0 and SSL 3.0 session id is always less
140 * than 32 bytes.
142 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
143 * an error code is returned.
146 gnutls_session_get_id (gnutls_session_t session,
147 void *session_id, size_t * session_id_size)
149 size_t given_session_id_size = *session_id_size;
151 *session_id_size = session->security_parameters.session_id_size;
153 /* just return the session size */
154 if (session_id == NULL)
156 return 0;
159 if (given_session_id_size < session->security_parameters.session_id_size)
161 return GNUTLS_E_SHORT_MEMORY_BUFFER;
164 memcpy (session_id, &session->security_parameters.session_id,
165 *session_id_size);
167 return 0;
171 * gnutls_session_get_id2:
172 * @session: is a #gnutls_session_t structure.
173 * @session_id: will point to the session ID.
175 * Returns the current session ID. The returned data should be
176 * treated as constant.
178 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
179 * an error code is returned.
182 gnutls_session_get_id2 (gnutls_session_t session,
183 gnutls_datum_t *session_id)
185 session_id->size = session->security_parameters.session_id_size;
186 session_id->data = session->security_parameters.session_id;
188 return 0;
192 * gnutls_session_set_data:
193 * @session: is a #gnutls_session_t structure.
194 * @session_data: is a pointer to space to hold the session.
195 * @session_data_size: is the session's size
197 * Sets all session parameters, in order to resume a previously
198 * established session. The session data given must be the one
199 * returned by gnutls_session_get_data(). This function should be
200 * called before gnutls_handshake().
202 * Keep in mind that session resuming is advisory. The server may
203 * choose not to resume the session, thus a full handshake will be
204 * performed.
206 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
207 * an error code is returned.
210 gnutls_session_set_data (gnutls_session_t session,
211 const void *session_data, size_t session_data_size)
213 int ret;
214 gnutls_datum_t psession;
216 psession.data = (uint8_t *) session_data;
217 psession.size = session_data_size;
219 if (session_data == NULL || session_data_size == 0)
221 gnutls_assert ();
222 return GNUTLS_E_INVALID_REQUEST;
224 ret = _gnutls_session_unpack (session, &psession);
225 if (ret < 0)
227 gnutls_assert ();
228 return ret;
231 return 0;