summaryrefslogtreecommitdiff
path: root/lib/gnutls_session.c
blob: 1d500e28ec3605e6a51cbc9e7b4b0020aa7b20f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Copyright (C) 2000, 2003, 2004, 2005 Free Software Foundation
 *
 * Author: Nikos Mavroyanopoulos
 *
 * This file is part of GNUTLS.
 *
 * The GNUTLS library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 * USA
 *
 */
#include "gnutls_int.h"
#include "gnutls_errors.h"
#include "debug.h"
#include <gnutls_session_pack.h>

/**
  * gnutls_session_get_data - Returns all session parameters.
  * @session: is a #gnutls_session_t structure.
  * @session_data: is a pointer to space to hold the session.
  * @session_data_size: is the session_data's size, or it will be set by the function.
  *
  * Returns all session parameters, in order to support resuming.
  * The client should call this, and keep the returned session, if he wants to
  * resume that current version later by calling gnutls_session_set_data()
  * This function must be called after a successful handshake.
  *
  * Resuming sessions is really useful and speedups connections after a succesful one.
  **/
int
gnutls_session_get_data (gnutls_session_t session,
			 void *session_data, size_t * session_data_size)
{

  gnutls_datum_t psession;
  int ret;

  if (session->internals.resumable == RESUME_FALSE)
    return GNUTLS_E_INVALID_SESSION;

  psession.data = session_data;

  ret = _gnutls_session_pack (session, &psession);
  if (ret < 0)
    {
      gnutls_assert ();
      return ret;
    }
  *session_data_size = psession.size;

  if (psession.size > *session_data_size)
    return GNUTLS_E_SHORT_MEMORY_BUFFER;

  if (session_data != NULL)
    memcpy (session_data, psession.data, psession.size);

  return 0;
}

/**
  * gnutls_session_get_data2 - Returns all session parameters.
  * @session: is a #gnutls_session_t structure.
  * @session_data: is a pointer to a datum that will hold the session.
  *
  * Returns all session parameters, in order to support resuming.
  * The client should call this, and keep the returned session, if he wants to
  * resume that current version later by calling gnutls_session_set_data()
  * This function must be called after a successful handshake. The returned
  * datum must be freed with gnutls_free().
  *
  * Resuming sessions is really useful and speedups connections after a succesful one.
  **/
int
gnutls_session_get_data2 (gnutls_session_t session, gnutls_datum * data)
{

  int ret;

  if (data == NULL)
    {
      return GNUTLS_E_INVALID_REQUEST;
    }

  if (session->internals.resumable == RESUME_FALSE)
    return GNUTLS_E_INVALID_SESSION;

  ret = _gnutls_session_pack (session, data);
  if (ret < 0)
    {
      gnutls_assert ();
      return ret;
    }

  return 0;
}


/**
  * gnutls_session_get_id - Returns session id.
  * @session: is a #gnutls_session_t structure.
  * @session_id: is a pointer to space to hold the session id.
  * @session_id_size: is the session id's size, or it will be set by the function.
  *
  * Returns the current session id. This can be used if you want to check if
  * the next session you tried to resume was actually resumed.
  * This is because resumed sessions have the same sessionID with the 
  * original session.
  *
  * Session id is some data set by the server, that identify the current session. 
  * In TLS 1.0 and SSL 3.0 session id is always less than 32 bytes.
  *
  * Returns zero on success.
  **/
int
gnutls_session_get_id (gnutls_session_t session,
		       void *session_id, size_t * session_id_size)
{
  size_t given_session_id_size = *session_id_size;

  *session_id_size = session->security_parameters.session_id_size;

  /* just return the session size */
  if (session_id == NULL)
    {
      return 0;
    }

  if (given_session_id_size < session->security_parameters.session_id_size)
    {
      return GNUTLS_E_SHORT_MEMORY_BUFFER;
    }

  memcpy (session_id, &session->security_parameters.session_id,
	  *session_id_size);

  return 0;
}

/**
  * gnutls_session_set_data - Sets all session parameters
  * @session: is a #gnutls_session_t structure.
  * @session_data: is a pointer to space to hold the session.
  * @session_data_size: is the session's size
  *
  * Sets all session parameters, in order to resume a previously established
  * session. The session data given must be the one returned by gnutls_session_get_data().
  * This function should be called before gnutls_handshake().
  *
  * Keep in mind that session resuming is advisory. The server may
  * choose not to resume the session, thus a full handshake will be
  * performed.
  *
  * Returns a negative value on error.
  *
  **/
int
gnutls_session_set_data (gnutls_session_t session,
			 const void *session_data, size_t session_data_size)
{
  int ret;
  gnutls_datum_t psession;

  psession.data = (opaque *) session_data;
  psession.size = session_data_size;

  if (session_data == NULL || session_data_size == 0)
    {
      gnutls_assert ();
      return GNUTLS_E_INVALID_REQUEST;
    }
  ret = _gnutls_session_unpack (session, &psession);
  if (ret < 0)
    {
      gnutls_assert ();
      return ret;
    }

  return 0;
}