Use libtasn1 v2.4.
[gnutls.git] / lib / gnutls_session.c
blobb4ef604670cc831c54264d21f2dda5a08b79d2a1
1 /*
2 * Copyright (C) 2000, 2003, 2004, 2005, 2007, 2008, 2009, 2010 Free
3 * Software Foundation, Inc.
5 * Author: Nikos Mavrogiannopoulos
7 * This file is part of GNUTLS.
9 * The GNUTLS library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 * USA
25 #include "gnutls_int.h"
26 #include "gnutls_errors.h"
27 #include "debug.h"
28 #include <gnutls_session_pack.h>
29 #include <gnutls_datum.h>
31 /**
32 * gnutls_session_get_data - Returns all session parameters.
33 * @session: is a #gnutls_session_t structure.
34 * @session_data: is a pointer to space to hold the session.
35 * @session_data_size: is the session_data's size, or it will be set by the function.
37 * Returns all session parameters, in order to support resuming. The
38 * client should call this, and keep the returned session, if he
39 * wants to resume that current version later by calling
40 * gnutls_session_set_data() This function must be called after a
41 * successful handshake.
43 * Resuming sessions is really useful and speedups connections after
44 * a successful one.
46 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
47 * an error code is returned.
48 **/
49 int
50 gnutls_session_get_data (gnutls_session_t session,
51 void *session_data, size_t * session_data_size)
54 gnutls_datum_t psession;
55 int ret;
57 if (session->internals.resumable == RESUME_FALSE)
58 return GNUTLS_E_INVALID_SESSION;
60 psession.data = session_data;
62 ret = _gnutls_session_pack (session, &psession);
63 if (ret < 0)
65 gnutls_assert ();
66 return ret;
68 *session_data_size = psession.size;
70 if (psession.size > *session_data_size)
72 ret = GNUTLS_E_SHORT_MEMORY_BUFFER;
73 goto error;
76 if (session_data != NULL)
77 memcpy (session_data, psession.data, psession.size);
79 ret = 0;
81 error:
82 _gnutls_free_datum (&psession);
83 return ret;
86 /**
87 * gnutls_session_get_data2 - Returns all session parameters.
88 * @session: is a #gnutls_session_t structure.
89 * @data: is a pointer to a datum that will hold the session.
91 * Returns all session parameters, in order to support resuming. The
92 * client should call this, and keep the returned session, if he wants
93 * to resume that current version later by calling
94 * gnutls_session_set_data(). This function must be called after a
95 * successful handshake. The returned datum must be freed with
96 * gnutls_free().
98 * Resuming sessions is really useful and speedups connections after
99 * a successful one.
101 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
102 * an error code is returned.
105 gnutls_session_get_data2 (gnutls_session_t session, gnutls_datum_t * data)
108 int ret;
110 if (data == NULL)
112 return GNUTLS_E_INVALID_REQUEST;
115 if (session->internals.resumable == RESUME_FALSE)
116 return GNUTLS_E_INVALID_SESSION;
118 ret = _gnutls_session_pack (session, data);
119 if (ret < 0)
121 gnutls_assert ();
122 return ret;
125 return 0;
130 * gnutls_session_get_id - Returns session id.
131 * @session: is a #gnutls_session_t structure.
132 * @session_id: is a pointer to space to hold the session id.
133 * @session_id_size: is the session id's size, or it will be set by the function.
135 * Returns the current session id. This can be used if you want to
136 * check if the next session you tried to resume was actually
137 * resumed. This is because resumed sessions have the same sessionID
138 * with the original session.
140 * Session id is some data set by the server, that identify the
141 * current session. In TLS 1.0 and SSL 3.0 session id is always less
142 * than 32 bytes.
144 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
145 * an error code is returned.
148 gnutls_session_get_id (gnutls_session_t session,
149 void *session_id, size_t * session_id_size)
151 size_t given_session_id_size = *session_id_size;
153 *session_id_size = session->security_parameters.session_id_size;
155 /* just return the session size */
156 if (session_id == NULL)
158 return 0;
161 if (given_session_id_size < session->security_parameters.session_id_size)
163 return GNUTLS_E_SHORT_MEMORY_BUFFER;
166 memcpy (session_id, &session->security_parameters.session_id,
167 *session_id_size);
169 return 0;
173 * gnutls_session_set_data - Sets all session parameters
174 * @session: is a #gnutls_session_t structure.
175 * @session_data: is a pointer to space to hold the session.
176 * @session_data_size: is the session's size
178 * Sets all session parameters, in order to resume a previously
179 * established session. The session data given must be the one
180 * returned by gnutls_session_get_data(). This function should be
181 * called before gnutls_handshake().
183 * Keep in mind that session resuming is advisory. The server may
184 * choose not to resume the session, thus a full handshake will be
185 * performed.
187 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
188 * an error code is returned.
191 gnutls_session_set_data (gnutls_session_t session,
192 const void *session_data, size_t session_data_size)
194 int ret;
195 gnutls_datum_t psession;
197 psession.data = (opaque *) session_data;
198 psession.size = session_data_size;
200 if (session_data == NULL || session_data_size == 0)
202 gnutls_assert ();
203 return GNUTLS_E_INVALID_REQUEST;
205 ret = _gnutls_session_unpack (session, &psession);
206 if (ret < 0)
208 gnutls_assert ();
209 return ret;
212 return 0;