aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/https/tls/gnutls_sig.c
blob: 07ceb21b1f34b455fcf3fd2a8d80f5c51d0518df (plain) (blame)
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
 * Copyright (C) 2001, 2004, 2005, 2006, 2007 Free Software Foundation
 *
 * 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
 *
 */

#include <gnutls_int.h>
#include <gnutls_errors.h>
#include <x509_b64.h>
#include <auth_cert.h>
#include <gnutls_cert.h>
#include <libtasn1.h>
#include <gnutls_datum.h>
#include <gnutls_mpi.h>
#include <gnutls_global.h>
#include <gnutls_pk.h>
#include <debug.h>
#include <gnutls_buffers.h>
#include <gnutls_sig.h>
#include <gnutls_kx.h>

static int _gnutls_tls_sign (mhd_gtls_session_t session,
                             gnutls_cert * cert,
                             gnutls_privkey * pkey,
                             const gnutls_datum_t * hash_concat,
                             gnutls_datum_t * signature);

/* Generates a signature of all the previous sent packets in the
 * handshake procedure. (20040227: now it works for SSL 3.0 as well)
 */
int
mhd_gtls_tls_sign_hdata (mhd_gtls_session_t session,
                         gnutls_cert * cert,
                         gnutls_privkey * pkey, gnutls_datum_t * signature)
{
  gnutls_datum_t dconcat;
  int ret;
  opaque concat[36];
  mac_hd_t td_md5;
  mac_hd_t td_sha;
  enum MHD_GNUTLS_Protocol ver = MHD_gnutls_protocol_get_version (session);

  td_sha = mhd_gnutls_hash_copy (session->internals.handshake_mac_handle_sha);
  if (td_sha == NULL)
    {
      gnutls_assert ();
      return GNUTLS_E_HASH_FAILED;
    }

  if (ver == MHD_GNUTLS_SSL3)
    {
      ret = mhd_gtls_generate_master (session, 1);
      if (ret < 0)
        {
          gnutls_assert ();
          return ret;
        }

      mhd_gnutls_mac_deinit_ssl3_handshake (td_sha, &concat[16],
                                            session->security_parameters.
                                            master_secret, TLS_MASTER_SIZE);
    }
  else
    mhd_gnutls_hash_deinit (td_sha, &concat[16]);

  switch (cert->subject_pk_algorithm)
    {
    case MHD_GNUTLS_PK_RSA:
      td_md5 =
        mhd_gnutls_hash_copy (session->internals.handshake_mac_handle_md5);
      if (td_md5 == NULL)
        {
          gnutls_assert ();
          return GNUTLS_E_HASH_FAILED;
        }

      if (ver == MHD_GNUTLS_SSL3)
        mhd_gnutls_mac_deinit_ssl3_handshake (td_md5, concat,
                                              session->security_parameters.
                                              master_secret, TLS_MASTER_SIZE);
      else
        mhd_gnutls_hash_deinit (td_md5, concat);

      dconcat.data = concat;
      dconcat.size = 36;
      break;
    default:
      gnutls_assert ();
      return GNUTLS_E_INTERNAL_ERROR;
    }
  ret = _gnutls_tls_sign (session, cert, pkey, &dconcat, signature);
  if (ret < 0)
    {
      gnutls_assert ();
    }

  return ret;
}

/* Generates a signature of all the random data and the parameters.
 * Used in DHE_* ciphersuites.
 */
int
mhd_gtls_tls_sign_params (mhd_gtls_session_t session,
                          gnutls_cert * cert,
                          gnutls_privkey * pkey,
                          gnutls_datum_t * params, gnutls_datum_t * signature)
{
  gnutls_datum_t dconcat;
  int ret;
  mac_hd_t td_sha;
  opaque concat[36];
  enum MHD_GNUTLS_Protocol ver = MHD_gnutls_protocol_get_version (session);

  td_sha = mhd_gtls_hash_init (MHD_GNUTLS_MAC_SHA1);
  if (td_sha == NULL)
    {
      gnutls_assert ();
      return GNUTLS_E_HASH_FAILED;
    }

  mhd_gnutls_hash (td_sha, session->security_parameters.client_random,
                   TLS_RANDOM_SIZE);
  mhd_gnutls_hash (td_sha, session->security_parameters.server_random,
                   TLS_RANDOM_SIZE);
  mhd_gnutls_hash (td_sha, params->data, params->size);

  switch (cert->subject_pk_algorithm)
    {
    case MHD_GNUTLS_PK_RSA:
      if (ver < MHD_GNUTLS_TLS1_2)
        {
          mac_hd_t td_md5 = mhd_gtls_hash_init (MHD_GNUTLS_MAC_MD5);
          if (td_md5 == NULL)
            {
              gnutls_assert ();
              return GNUTLS_E_HASH_FAILED;
            }

          mhd_gnutls_hash (td_md5, session->security_parameters.client_random,
                           TLS_RANDOM_SIZE);
          mhd_gnutls_hash (td_md5, session->security_parameters.server_random,
                           TLS_RANDOM_SIZE);
          mhd_gnutls_hash (td_md5, params->data, params->size);

          mhd_gnutls_hash_deinit (td_md5, concat);
          mhd_gnutls_hash_deinit (td_sha, &concat[16]);

          dconcat.size = 36;
        }
      else
        {
#if 1
          /* Use NULL parameters. */
          memcpy (concat,
                  "\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14",
                  15);
          mhd_gnutls_hash_deinit (td_sha, &concat[15]);
          dconcat.size = 35;
#else
          /* No parameters field. */
          memcpy (concat,
                  "\x30\x1f\x30\x07\x06\x05\x2b\x0e\x03\x02\x1a\x04\x14", 13);
          mhd_gnutls_hash_deinit (td_sha, &concat[13]);
          dconcat.size = 33;
#endif
        }
      dconcat.data = concat;
      break;
    default:
      gnutls_assert ();
      mhd_gnutls_hash_deinit (td_sha, NULL);
      return GNUTLS_E_INTERNAL_ERROR;
    }
  ret = _gnutls_tls_sign (session, cert, pkey, &dconcat, signature);
  if (ret < 0)
    {
      gnutls_assert ();
    }

  return ret;

}

/* This will create a PKCS1 or DSA signature, using the given parameters, and the
 * given data. The output will be allocated and be put in signature.
 */
int
mhd_gtls_sign (enum MHD_GNUTLS_PublicKeyAlgorithm algo,
               mpi_t * params,
               int params_size,
               const gnutls_datum_t * data, gnutls_datum_t * signature)
{
  int ret;

  switch (algo)
    {
    case MHD_GNUTLS_PK_RSA:
      /* encrypt */
      if ((ret =
           mhd_gtls_pkcs1_rsa_encrypt (signature, data, params, params_size,
                                       1)) < 0)
        {
          gnutls_assert ();
          return ret;
        }

      break;
    default:
      gnutls_assert ();
      return GNUTLS_E_INTERNAL_ERROR;
      break;
    }

  return 0;
}

/* This will create a PKCS1 or DSA signature, as defined in the TLS protocol.
 * Cert is the certificate of the corresponding private key. It is only checked if
 * it supports signing.
 */
static int
_gnutls_tls_sign (mhd_gtls_session_t session,
                  gnutls_cert * cert,
                  gnutls_privkey * pkey,
                  const gnutls_datum_t * hash_concat,
                  gnutls_datum_t * signature)
{

  /* If our certificate supports signing
   */

  if (cert != NULL)
    if (cert->key_usage != 0)
      if (!(cert->key_usage & KEY_DIGITAL_SIGNATURE))
        {
          gnutls_assert ();
          return GNUTLS_E_KEY_USAGE_VIOLATION;
        }

  /* External signing. */
  if (!pkey || pkey->params_size == 0)
    {
      if (!session->internals.sign_func)
        return GNUTLS_E_INSUFFICIENT_CREDENTIALS;

      return (*session->internals.sign_func) (session,
                                              session->internals.
                                              sign_func_userdata,
                                              cert->cert_type, &cert->raw,
                                              hash_concat, signature);
    }

  return mhd_gtls_sign (pkey->pk_algorithm, pkey->params, pkey->params_size,
                        hash_concat, signature);
}

static int
_gnutls_verify_sig (gnutls_cert * cert,
                    const gnutls_datum_t * hash_concat,
                    gnutls_datum_t * signature, size_t sha1pos)
{
  int ret;
  gnutls_datum_t vdata;

  if ( (cert == NULL) || (cert->version == 0) )
    {                           /* this is the only way to check
                                 * if it is initialized
                                 */
      gnutls_assert ();
      return GNUTLS_E_CERTIFICATE_ERROR;
    }

  /* If the certificate supports signing continue.
   */
  if (cert != NULL)
    if (cert->key_usage != 0)
      if (!(cert->key_usage & KEY_DIGITAL_SIGNATURE))
        {
          gnutls_assert ();
          return GNUTLS_E_KEY_USAGE_VIOLATION;
        }

  switch (cert->subject_pk_algorithm)
    {
    case MHD_GNUTLS_PK_RSA:

      vdata.data = hash_concat->data;
      vdata.size = hash_concat->size;

      /* verify signature */
      if ((ret = mhd_gtls_rsa_verify (&vdata, signature, cert->params,
                                      cert->params_size, 1)) < 0)
        {
          gnutls_assert ();
          return ret;
        }

      break;
    default:
      gnutls_assert ();
      return GNUTLS_E_INTERNAL_ERROR;
    }

  return 0;
}

/* Verifies a TLS signature (like the one in the client certificate
 * verify message).
 */
int
mhd_gtls_verify_sig_hdata (mhd_gtls_session_t session,
                           gnutls_cert * cert, gnutls_datum_t * signature)
{
  int ret;
  opaque concat[36];
  mac_hd_t td_md5;
  mac_hd_t td_sha;
  gnutls_datum_t dconcat;
  enum MHD_GNUTLS_Protocol ver = MHD_gnutls_protocol_get_version (session);

  td_md5 = mhd_gnutls_hash_copy (session->internals.handshake_mac_handle_md5);
  if (td_md5 == NULL)
    {
      gnutls_assert ();
      return GNUTLS_E_HASH_FAILED;
    }

  td_sha = mhd_gnutls_hash_copy (session->internals.handshake_mac_handle_sha);
  if (td_sha == NULL)
    {
      gnutls_assert ();
      mhd_gnutls_hash_deinit (td_md5, NULL);
      return GNUTLS_E_HASH_FAILED;
    }

  if (ver == MHD_GNUTLS_SSL3)
    {
      ret = mhd_gtls_generate_master (session, 1);
      if (ret < 0)
        {
          gnutls_assert ();
          return ret;
        }

      mhd_gnutls_mac_deinit_ssl3_handshake (td_md5, concat,
                                            session->security_parameters.
                                            master_secret, TLS_MASTER_SIZE);
      mhd_gnutls_mac_deinit_ssl3_handshake (td_sha, &concat[16],
                                            session->security_parameters.
                                            master_secret, TLS_MASTER_SIZE);
    }
  else
    {
      mhd_gnutls_hash_deinit (td_md5, concat);
      mhd_gnutls_hash_deinit (td_sha, &concat[16]);
    }

  dconcat.data = concat;
  dconcat.size = 20 + 16;       /* md5+ sha */

  ret = _gnutls_verify_sig (cert, &dconcat, signature, 16);
  if (ret < 0)
    {
      gnutls_assert ();
      return ret;
    }

  return ret;

}

/* Generates a signature of all the random data and the parameters.
 * Used in DHE_* ciphersuites.
 */
int
mhd_gtls_verify_sig_params (mhd_gtls_session_t session,
                            gnutls_cert * cert,
                            const gnutls_datum_t * params,
                            gnutls_datum_t * signature)
{
  gnutls_datum_t dconcat;
  int ret;
  mac_hd_t td_md5 = NULL;
  mac_hd_t td_sha;
  opaque concat[36];
  enum MHD_GNUTLS_Protocol ver = MHD_gnutls_protocol_get_version (session);

  if (ver < MHD_GNUTLS_TLS1_2)
    {
      td_md5 = mhd_gtls_hash_init (MHD_GNUTLS_MAC_MD5);
      if (td_md5 == NULL)
        {
          gnutls_assert ();
          return GNUTLS_E_HASH_FAILED;
        }

      mhd_gnutls_hash (td_md5, session->security_parameters.client_random,
                       TLS_RANDOM_SIZE);
      mhd_gnutls_hash (td_md5, session->security_parameters.server_random,
                       TLS_RANDOM_SIZE);
      mhd_gnutls_hash (td_md5, params->data, params->size);
    }

  td_sha = mhd_gtls_hash_init (MHD_GNUTLS_MAC_SHA1);
  if (td_sha == NULL)
    {
      gnutls_assert ();
      if (td_md5)
        mhd_gnutls_hash_deinit (td_md5, NULL);
      return GNUTLS_E_HASH_FAILED;
    }

  mhd_gnutls_hash (td_sha, session->security_parameters.client_random,
                   TLS_RANDOM_SIZE);
  mhd_gnutls_hash (td_sha, session->security_parameters.server_random,
                   TLS_RANDOM_SIZE);
  mhd_gnutls_hash (td_sha, params->data, params->size);

  if (ver < MHD_GNUTLS_TLS1_2)
    {
      mhd_gnutls_hash_deinit (td_md5, concat);
      mhd_gnutls_hash_deinit (td_sha, &concat[16]);
      dconcat.size = 36;
    }
  else
    {
#if 1
      /* Use NULL parameters. */
      memcpy (concat,
              "\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14",
              15);
      mhd_gnutls_hash_deinit (td_sha, &concat[15]);
      dconcat.size = 35;
#else
      /* No parameters field. */
      memcpy (concat,
              "\x30\x1f\x30\x07\x06\x05\x2b\x0e\x03\x02\x1a\x04\x14", 13);
      mhd_gnutls_hash_deinit (td_sha, &concat[13]);
      dconcat.size = 33;
#endif
    }

  dconcat.data = concat;

  ret = _gnutls_verify_sig (cert, &dconcat, signature, dconcat.size - 20);
  if (ret < 0)
    {
      gnutls_assert ();
      return ret;
    }

  return ret;

}