documented fix
[gnutls.git] / lib / gnutls_session.c
blobfbd807ba589ccbef6cf63ef977c489794eff9e38
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: is the session id's size, or it will be set by the function.
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. This is because resumed sessions have the same sessionID
136 * with the original session.
138 * Session id is some data set 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_set_data:
172 * @session: is a #gnutls_session_t structure.
173 * @session_data: is a pointer to space to hold the session.
174 * @session_data_size: is the session's size
176 * Sets all session parameters, in order to resume a previously
177 * established session. The session data given must be the one
178 * returned by gnutls_session_get_data(). This function should be
179 * called before gnutls_handshake().
181 * Keep in mind that session resuming is advisory. The server may
182 * choose not to resume the session, thus a full handshake will be
183 * performed.
185 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
186 * an error code is returned.
189 gnutls_session_set_data (gnutls_session_t session,
190 const void *session_data, size_t session_data_size)
192 int ret;
193 gnutls_datum_t psession;
195 psession.data = (uint8_t *) session_data;
196 psession.size = session_data_size;
198 if (session_data == NULL || session_data_size == 0)
200 gnutls_assert ();
201 return GNUTLS_E_INVALID_REQUEST;
203 ret = _gnutls_session_unpack (session, &psession);
204 if (ret < 0)
206 gnutls_assert ();
207 return ret;
210 return 0;