Add.
[gnutls.git] / lib / gnutls_session.c
blob1d500e28ec3605e6a51cbc9e7b4b0020aa7b20f1
1 /*
2 * Copyright (C) 2000, 2003, 2004, 2005 Free Software Foundation
4 * Author: Nikos Mavroyanopoulos
6 * This file is part of GNUTLS.
8 * The GNUTLS library 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 2.1 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
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 * USA
24 #include "gnutls_int.h"
25 #include "gnutls_errors.h"
26 #include "debug.h"
27 #include <gnutls_session_pack.h>
29 /**
30 * gnutls_session_get_data - Returns all session parameters.
31 * @session: is a #gnutls_session_t structure.
32 * @session_data: is a pointer to space to hold the session.
33 * @session_data_size: is the session_data's size, or it will be set by the function.
35 * Returns all session parameters, in order to support resuming.
36 * The client should call this, and keep the returned session, if he wants to
37 * resume that current version later by calling gnutls_session_set_data()
38 * This function must be called after a successful handshake.
40 * Resuming sessions is really useful and speedups connections after a succesful one.
41 **/
42 int
43 gnutls_session_get_data (gnutls_session_t session,
44 void *session_data, size_t * session_data_size)
47 gnutls_datum_t psession;
48 int ret;
50 if (session->internals.resumable == RESUME_FALSE)
51 return GNUTLS_E_INVALID_SESSION;
53 psession.data = session_data;
55 ret = _gnutls_session_pack (session, &psession);
56 if (ret < 0)
58 gnutls_assert ();
59 return ret;
61 *session_data_size = psession.size;
63 if (psession.size > *session_data_size)
64 return GNUTLS_E_SHORT_MEMORY_BUFFER;
66 if (session_data != NULL)
67 memcpy (session_data, psession.data, psession.size);
69 return 0;
72 /**
73 * gnutls_session_get_data2 - Returns all session parameters.
74 * @session: is a #gnutls_session_t structure.
75 * @session_data: is a pointer to a datum that will hold the session.
77 * Returns all session parameters, in order to support resuming.
78 * The client should call this, and keep the returned session, if he wants to
79 * resume that current version later by calling gnutls_session_set_data()
80 * This function must be called after a successful handshake. The returned
81 * datum must be freed with gnutls_free().
83 * Resuming sessions is really useful and speedups connections after a succesful one.
84 **/
85 int
86 gnutls_session_get_data2 (gnutls_session_t session, gnutls_datum * data)
89 int ret;
91 if (data == NULL)
93 return GNUTLS_E_INVALID_REQUEST;
96 if (session->internals.resumable == RESUME_FALSE)
97 return GNUTLS_E_INVALID_SESSION;
99 ret = _gnutls_session_pack (session, data);
100 if (ret < 0)
102 gnutls_assert ();
103 return ret;
106 return 0;
111 * gnutls_session_get_id - Returns session id.
112 * @session: is a #gnutls_session_t structure.
113 * @session_id: is a pointer to space to hold the session id.
114 * @session_id_size: is the session id's size, or it will be set by the function.
116 * Returns the current session id. This can be used if you want to check if
117 * the next session you tried to resume was actually resumed.
118 * This is because resumed sessions have the same sessionID with the
119 * original session.
121 * Session id is some data set by the server, that identify the current session.
122 * In TLS 1.0 and SSL 3.0 session id is always less than 32 bytes.
124 * Returns zero on success.
127 gnutls_session_get_id (gnutls_session_t session,
128 void *session_id, size_t * session_id_size)
130 size_t given_session_id_size = *session_id_size;
132 *session_id_size = session->security_parameters.session_id_size;
134 /* just return the session size */
135 if (session_id == NULL)
137 return 0;
140 if (given_session_id_size < session->security_parameters.session_id_size)
142 return GNUTLS_E_SHORT_MEMORY_BUFFER;
145 memcpy (session_id, &session->security_parameters.session_id,
146 *session_id_size);
148 return 0;
152 * gnutls_session_set_data - Sets all session parameters
153 * @session: is a #gnutls_session_t structure.
154 * @session_data: is a pointer to space to hold the session.
155 * @session_data_size: is the session's size
157 * Sets all session parameters, in order to resume a previously established
158 * session. The session data given must be the one returned by gnutls_session_get_data().
159 * This function should be called before gnutls_handshake().
161 * Keep in mind that session resuming is advisory. The server may
162 * choose not to resume the session, thus a full handshake will be
163 * performed.
165 * Returns a negative value on error.
169 gnutls_session_set_data (gnutls_session_t session,
170 const void *session_data, size_t session_data_size)
172 int ret;
173 gnutls_datum_t psession;
175 psession.data = (opaque *) session_data;
176 psession.size = session_data_size;
178 if (session_data == NULL || session_data_size == 0)
180 gnutls_assert ();
181 return GNUTLS_E_INVALID_REQUEST;
183 ret = _gnutls_session_unpack (session, &psession);
184 if (ret < 0)
186 gnutls_assert ();
187 return ret;
190 return 0;