summaryrefslogtreecommitdiff
path: root/lib/ext_signature.c
blob: a2aefb85a9d19b83a4eb3d6bdc319b06c6673e09 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
 * Copyright (C) 2002, 2003, 2004, 2005, 2009, 2010 Free Software
 * Foundation, Inc.
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * 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
 *
 */

/* This file contains the code the Certificate Type TLS extension.
 * This extension is currently gnutls specific.
 */

#include "gnutls_int.h"
#include "gnutls_errors.h"
#include "gnutls_num.h"
#include <ext_signature.h>
#include <gnutls_state.h>
#include <gnutls_num.h>
#include <gnutls_algorithms.h>

/* generates a SignatureAndHashAlgorithm structure with length as prefix
 * by using the setup priorities.
 */
int
_gnutls_sign_algorithm_write_params (gnutls_session_t session, opaque * data,
				     size_t max_data_size)
{
  opaque *p = data;
  int len, i, j;
  sign_algorithm_st aid;

  len = session->internals.priorities.sign_algo.algorithms * 2;
  if (max_data_size < len + 2)
    {
      gnutls_assert ();
      return GNUTLS_E_SHORT_MEMORY_BUFFER;
    }

  _gnutls_write_uint16 (len, p);
  p += 2;

  for (i = j = 0; i < len; i += 2, j++)
    {
      aid =
	_gnutls_sign_to_tls_aid (session->internals.priorities.sign_algo.
				 priority[j]);
      *p = aid.hash_algorithm;
      p++;
      *p = aid.sign_algorithm;
      p++;

    }
  return len + 2;
}

/* Parses the Signature Algorithm structure and stores data into
 * session->security_parameters.extensions.
 */
int
_gnutls_sign_algorithm_parse_data (gnutls_session_t session,
				   const opaque * data, size_t data_size)
{
  int sig, i;

  session->security_parameters.extensions.sign_algorithms_size = 0;

  for (i = 0; i < data_size; i += 2)
    {
      sign_algorithm_st aid;

      aid.hash_algorithm = data[i];
      aid.sign_algorithm = data[i + 1];

      sig = _gnutls_tls_aid_to_sign (&aid);
      if (sig != GNUTLS_SIGN_UNKNOWN)
	{
	  session->security_parameters.extensions.
	    sign_algorithms[session->security_parameters.extensions.
			    sign_algorithms_size++] = sig;
	  if (session->security_parameters.extensions.sign_algorithms_size ==
	      MAX_SIGNATURE_ALGORITHMS)
	    break;
	}
    }

  return 0;
}

/*
 * In case of a server: if a SIGNATURE_ALGORITHMS extension type is
 * received then it stores into the session security parameters the
 * new value.
 *
 * In case of a client: If a signature_algorithms have been specified
 * then it is an error;
 */

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

  if (session->security_parameters.entity == GNUTLS_CLIENT)
    {
      /* nothing for now */
      gnutls_assert ();
      /* Although TLS 1.2 mandates that we must not accept reply
       * to this message, there are good reasons to just ignore it. Check
       * http://www.ietf.org/mail-archive/web/tls/current/msg03880.html
       */
      /* return GNUTLS_E_UNEXPECTED_PACKET; */
    }
  else
    {
      /* SERVER SIDE - we must check if the sent cert type is the right one
       */
      if (data_size > 2)
	{
	  uint16_t len;


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

	  ret = _gnutls_sign_algorithm_parse_data (session, data + 2, len);
	  if (ret < 0)
	    {
	      gnutls_assert ();
	      return ret;
	    }
	}
    }

  return 0;
}

/* returns data_size or a negative number on failure
 */
int
_gnutls_signature_algorithm_send_params (gnutls_session_t session,
					 opaque * data, size_t data_size)
{
  int ret;
  gnutls_protocol_t ver = gnutls_protocol_get_version (session);

  /* this function sends the client extension data */
  if (session->security_parameters.entity == GNUTLS_CLIENT
      && _gnutls_version_has_selectable_sighash (ver))
    {
      if (session->internals.priorities.sign_algo.algorithms > 0)
	{
	  ret =
	    _gnutls_sign_algorithm_write_params (session, data, data_size);
	  if (ret < 0)
	    {
	      gnutls_assert ();
	      return ret;
	    }
	  return ret;
	}
    }

  /* if we are here it means we don't send the extension */
  return 0;
}

/* Returns a requested by the peer signature algorithm that
 * matches the given public key algorithm. Index can be increased
 * to return the second choice etc.
 */
gnutls_sign_algorithm_t
_gnutls_session_get_sign_algo (gnutls_session_t session,
			       gnutls_pk_algorithm_t pk,
			       gnutls_digest_algorithm_t * hash)
{
  unsigned i;
  gnutls_protocol_t ver = gnutls_protocol_get_version (session);


  if (!_gnutls_version_has_selectable_sighash (ver)
      || session->security_parameters.extensions.sign_algorithms_size == 0)
    /* none set, allow all */
    {
      *hash = GNUTLS_DIG_SHA1;
      return _gnutls_x509_pk_to_sign (pk, *hash);
    }

  for (i = 0;
       i < session->security_parameters.extensions.sign_algorithms_size; i++)
    {
      if (_gnutls_sign_get_pk_algorithm
	  (session->security_parameters.extensions.sign_algorithms[i]) == pk)
	{
	  *hash =
	    _gnutls_sign_get_hash_algorithm (session->
					     security_parameters.extensions.
					     sign_algorithms[i]);
	  return session->security_parameters.extensions.sign_algorithms[i];
	}
    }

  return GNUTLS_SIGN_UNKNOWN;
}


/* Check if the given signature algorithm is accepted by
 * the peer. Returns 0 on success or a negative value
 * on error.
 */
int
_gnutls_session_sign_algo_requested (gnutls_session_t session,
				     gnutls_sign_algorithm_t sig)
{
  unsigned i;
  gnutls_protocol_t ver = gnutls_protocol_get_version (session);

  if (!_gnutls_version_has_selectable_sighash (ver)
      || session->security_parameters.extensions.sign_algorithms_size == 0)
    /* none set, allow all */
    {
      return 0;
    }

  for (i = 0;
       i < session->security_parameters.extensions.sign_algorithms_size; i++)
    {
      if (session->security_parameters.extensions.sign_algorithms[i] == sig)
	{
	  return 0;		/* ok */
	}
    }

  return GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM;
}

/* Check if the given signature algorithm is supported.
 * This means that it is enabled by the priority functions,
 * and in case of a server a matching certificate exists.
 */
int
_gnutls_session_sign_algo_enabled (gnutls_session_t session,
				   gnutls_sign_algorithm_t sig)
{
  unsigned i;
  gnutls_protocol_t ver = gnutls_protocol_get_version (session);

  if (!_gnutls_version_has_selectable_sighash (ver)
      || session->security_parameters.extensions.sign_algorithms_size == 0)
    /* none set, allow all */
    {
      return 0;
    }

  for (i = 0; i < session->internals.priorities.sign_algo.algorithms; i++)
    {
      if (session->internals.priorities.sign_algo.priority[i] == sig)
	{
	  return 0;		/* ok */
	}
    }

  return GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM;
}

/**
 * gnutls_sign_algorithm_get:
 * @session: is a #gnutls_session_t structure.
 * @indx: is an index of the signature algorithm to return
 * @algo: the returned certificate type will be stored there
 *
 * Returns the signature algorithm specified by index that was
 * requested by the peer. If the specified index has no data available
 * this function returns %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE.  If
 * the negotiated TLS version does not support signature algorithms
 * then %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE will be returned even
 * for the first index.  The first index is 0.
 *
 * This function is usefull in the certificate callback functions
 * to assist in selecting the correct certificate.
 *
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
 *   an error code is returned.
 *
 * Since: 2.10.0
 **/
int
gnutls_sign_algorithm_get_requested (gnutls_session_t session,
				     size_t indx,
				     gnutls_sign_algorithm_t * algo)
{
  gnutls_protocol_t ver = gnutls_protocol_get_version (session);

  if (!_gnutls_version_has_selectable_sighash (ver)
      || session->security_parameters.extensions.sign_algorithms_size == 0)
    {
      return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
    }

  if (indx < session->security_parameters.extensions.sign_algorithms_size)
    {
      *algo = session->security_parameters.extensions.sign_algorithms[indx];
      return 0;
    }
  else
    return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
}