summaryrefslogtreecommitdiff
path: root/lib/ext_oprfi.c
blob: 2003f75ab3e9c3e9d7b3bd338962b113cfc6c3b2 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * Copyright (C) 2007 Free Software Foundation
 *
 * Author: Simon Josefsson
 *
 * 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
 *
 */

/* Implementation of Opaque PRF Input:
 * http://tools.ietf.org/id/draft-rescorla-tls-opaque-prf-input-00.txt
 *
 */

#include <ext_oprfi.h>

#include <gnutls_errors.h>
#include <gnutls_num.h>

int
oprfi_recv_server (gnutls_session_t session,
		   const opaque * data, size_t _data_size)
{
  ssize_t data_size = _data_size;
  uint16_t len;
  int ret;

  if (!session->security_parameters.extensions.oprfi_cb)
    {
      gnutls_assert ();
      return 0;
    }

  DECR_LEN (data_size, 2);
  len = _gnutls_read_uint16 (data);
  data += 2;

  if (len != data_size)
    {
      gnutls_assert ();
      return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
    }

  /* Store incoming data. */
  session->security_parameters.extensions.oprfi_client_len = len;
  session->security_parameters.extensions.oprfi_client = gnutls_malloc (len);
  if (!session->security_parameters.extensions.oprfi_client)
    {
      gnutls_assert ();
      return GNUTLS_E_MEMORY_ERROR;
    }
  memcpy (session->security_parameters.extensions.oprfi_client, data, len);

  return 0;
}

int
oprfi_recv_client (gnutls_session_t session,
		   const opaque * data, size_t _data_size)
{
  ssize_t data_size = _data_size;
  uint16_t len;
  int ret;

  if (session->security_parameters.extensions.oprfi_client == NULL)
    {
      gnutls_assert ();
      return 0;
    }

  DECR_LEN (data_size, 2);
  len = _gnutls_read_uint16 (data);
  data += 2;

  if (len != data_size)
    {
      gnutls_assert ();
      return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
    }

  if (len != session->security_parameters.extensions.oprfi_client_len)
    {
      gnutls_assert ();
      return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
    }

  /* Store incoming data. */
  session->security_parameters.extensions.oprfi_server_len = len;
  session->security_parameters.extensions.oprfi_server = gnutls_malloc (len);
  if (!session->security_parameters.extensions.oprfi_server)
    {
      gnutls_assert ();
      return GNUTLS_E_MEMORY_ERROR;
    }
  memcpy (session->security_parameters.extensions.oprfi_server, data, len);

  return 0;
}

int
_gnutls_oprfi_recv_params (gnutls_session_t session,
			   const opaque * data, size_t data_size)
{
  if (session->security_parameters.entity == GNUTLS_CLIENT)
    return oprfi_recv_client (session, data, data_size);
  else
    return oprfi_recv_server (session, data, data_size);
}

int
oprfi_send_client (gnutls_session_t session, opaque * data, size_t _data_size)
{
  opaque *p = data;
  ssize_t data_size = _data_size;
  int oprf_size = session->security_parameters.extensions.oprfi_client_len;

  if (oprf_size == 0)
    return 0;

  DECR_LENGTH_RET (data_size, 2, GNUTLS_E_SHORT_MEMORY_BUFFER);
  _gnutls_write_uint16 (oprf_size, p);
  p += 2;

  DECR_LENGTH_RET (data_size, oprf_size, GNUTLS_E_SHORT_MEMORY_BUFFER);

  memcpy (p, session->security_parameters.extensions.oprfi_client, oprf_size);

  return 2 + oprf_size;
}

int
oprfi_send_server (gnutls_session_t session, opaque * data, size_t _data_size)
{
  opaque *p = data;
  int ret;
  ssize_t data_size = _data_size;
  size_t len;

  if (!session->security_parameters.extensions.oprfi_client ||
      !session->security_parameters.extensions.oprfi_cb)
    return 0;

  /* Allocate buffer for outgoing data. */
  session->security_parameters.extensions.oprfi_server_len =
    session->security_parameters.extensions.oprfi_client_len;
  session->security_parameters.extensions.oprfi_server =
    gnutls_malloc (session->security_parameters.extensions.oprfi_server_len);
  if (!session->security_parameters.extensions.oprfi_server)
    {
      gnutls_assert ();
      return GNUTLS_E_MEMORY_ERROR;
    }

  /* Get outgoing data. */
  ret = session->security_parameters.extensions.oprfi_cb
    (session, session->security_parameters.extensions.oprfi_userdata,
     session->security_parameters.extensions.oprfi_client_len,
     session->security_parameters.extensions.oprfi_client,
     session->security_parameters.extensions.oprfi_server);
  if (ret < 0)
    {
      gnutls_assert ();
      gnutls_free (session->security_parameters.extensions.oprfi_server);
      return ret;
    }

  DECR_LENGTH_RET (data_size, 2, GNUTLS_E_SHORT_MEMORY_BUFFER);
  _gnutls_write_uint16 (session->security_parameters.extensions.
			oprfi_server_len, p);
  p += 2;

  DECR_LENGTH_RET (data_size,
		   session->security_parameters.extensions.oprfi_server_len,
		   GNUTLS_E_SHORT_MEMORY_BUFFER);

  memcpy (p, session->security_parameters.extensions.oprfi_server,
	  session->security_parameters.extensions.oprfi_server_len);

  return 2 + session->security_parameters.extensions.oprfi_server_len;
}

int
_gnutls_oprfi_send_params (gnutls_session_t session,
			   opaque * data, size_t data_size)
{
  if (session->security_parameters.entity == GNUTLS_CLIENT)
    return oprfi_send_client (session, data, data_size);
  else
    return oprfi_send_server (session, data, data_size);
}

/**
 * gnutls_oprfi_enable_client:
 * @session: is a #gnutls_session_t structure.
 * @len: length of Opaque PRF data to use in client.
 * @data: Opaque PRF data to use in client.
 *
 * Request that the client should attempt to negotiate the Opaque PRF
 * Input TLS extension, using the given data as the client's Opaque
 * PRF input.
 *
 * The data is copied into the session context after this call, so you
 * may de-allocate it immediately after calling this function.
 **/
void
gnutls_oprfi_enable_client (gnutls_session_t session,
			    size_t len, unsigned char *data)
{
  session->security_parameters.extensions.oprfi_client_len = len;
  session->security_parameters.extensions.oprfi_client = data;
}

/**
 * gnutls_oprfi_enable_server:
 * @session: is a #gnutls_session_t structure.
 * @cb: function pointer to Opaque PRF extension server callback.
 * @userdata: hook passed to callback function for passing application state.
 *
 * Request that the server should attempt to accept the Opaque PRF
 * Input TLS extension.  If the client requests the extension, the
 * provided callback @cb will be invoked.  The callback must have the
 * following prototype:
 *
 * int callback (gnutls_session_t session, void *userdata,
 *               size_t oprfi_len, const unsigned char *in_oprfi,
 *               unsigned char *out_oprfi);
 *
 * The callback can inspect the client-provided data in the input
 * parameters, and specify its own opaque prf input data in the output
 * variable.  The function must return 0 on success, otherwise the
 * handshake will be aborted.
 **/
void
gnutls_oprfi_enable_server (gnutls_session_t session,
			    gnutls_oprfi_callback_func cb, void *userdata)
{
  session->security_parameters.extensions.oprfi_cb = cb;
  session->security_parameters.extensions.oprfi_userdata = userdata;
}