doc updates
[gnutls.git] / lib / gnutls_session.c
blob2f7bfd163974d2733b34d2e9b1b6a82a981b9b15
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 needed to be stored to support resumption.
35 * The client should call this, and store the returned session data. A session
36 * may be resumed later by calling gnutls_session_set_data().
37 * This function must be called after a successful handshake.
39 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
40 * an error code is returned.
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;
62 if (psession.size > *session_data_size)
64 *session_data_size = psession.size;
65 ret = GNUTLS_E_SHORT_MEMORY_BUFFER;
66 goto error;
68 *session_data_size = psession.size;
70 if (session_data != NULL)
71 memcpy (session_data, psession.data, psession.size);
73 ret = 0;
75 error:
76 _gnutls_free_datum (&psession);
77 return ret;
80 /**
81 * gnutls_session_get_data2:
82 * @session: is a #gnutls_session_t structure.
83 * @data: is a pointer to a datum that will hold the session.
85 * Returns all session parameters needed to be stored to support resumption.
86 * The client should call this, and store the returned session data. A session
87 * may be resumed later by calling gnutls_session_set_data().
88 * This function must be called after a successful handshake.
89 * The returned @data are allocated and must be released using gnutls_free().
91 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
92 * an error code is returned.
93 **/
94 int
95 gnutls_session_get_data2 (gnutls_session_t session, gnutls_datum_t * data)
98 int ret;
100 if (data == NULL)
102 return GNUTLS_E_INVALID_REQUEST;
105 if (session->internals.resumable == RESUME_FALSE)
106 return GNUTLS_E_INVALID_SESSION;
108 ret = _gnutls_session_pack (session, data);
109 if (ret < 0)
111 gnutls_assert ();
112 return ret;
115 return 0;
120 * gnutls_session_get_id:
121 * @session: is a #gnutls_session_t structure.
122 * @session_id: is a pointer to space to hold the session id.
123 * @session_id_size: initially should contain the maximum @session_id size and will be updated.
125 * Returns the current session ID. This can be used if you want to
126 * check if the next session you tried to resume was actually
127 * resumed. That is because resumed sessions share the same session ID
128 * with the original session.
130 * The session ID is selected by the server, that identify the
131 * current session. In TLS 1.0 and SSL 3.0 session id is always less
132 * than 32 bytes.
134 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
135 * an error code is returned.
138 gnutls_session_get_id (gnutls_session_t session,
139 void *session_id, size_t * session_id_size)
141 size_t given_session_id_size = *session_id_size;
143 *session_id_size = session->security_parameters.session_id_size;
145 /* just return the session size */
146 if (session_id == NULL)
148 return 0;
151 if (given_session_id_size < session->security_parameters.session_id_size)
153 return GNUTLS_E_SHORT_MEMORY_BUFFER;
156 memcpy (session_id, &session->security_parameters.session_id,
157 *session_id_size);
159 return 0;
163 * gnutls_session_get_id2:
164 * @session: is a #gnutls_session_t structure.
165 * @session_id: will point to the session ID.
167 * Returns the current session ID. The returned data should be
168 * treated as constant.
170 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
171 * an error code is returned.
173 * Since: 3.1.4
176 gnutls_session_get_id2 (gnutls_session_t session,
177 gnutls_datum_t *session_id)
179 session_id->size = session->security_parameters.session_id_size;
180 session_id->data = session->security_parameters.session_id;
182 return 0;
186 * gnutls_session_set_data:
187 * @session: is a #gnutls_session_t structure.
188 * @session_data: is a pointer to space to hold the session.
189 * @session_data_size: is the session's size
191 * Sets all session parameters, in order to resume a previously
192 * established session. The session data given must be the one
193 * returned by gnutls_session_get_data(). This function should be
194 * called before gnutls_handshake().
196 * Keep in mind that session resuming is advisory. The server may
197 * choose not to resume the session, thus a full handshake will be
198 * performed.
200 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
201 * an error code is returned.
204 gnutls_session_set_data (gnutls_session_t session,
205 const void *session_data, size_t session_data_size)
207 int ret;
208 gnutls_datum_t psession;
210 psession.data = (uint8_t *) session_data;
211 psession.size = session_data_size;
213 if (session_data == NULL || session_data_size == 0)
215 gnutls_assert ();
216 return GNUTLS_E_INVALID_REQUEST;
218 ret = _gnutls_session_unpack (session, &psession);
219 if (ret < 0)
221 gnutls_assert ();
222 return ret;
225 return 0;