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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
|
/*
* Copyright (C) 2001-2012 Free Software Foundation, Inc.
* Copyright (C) 2017 Red Hat, Inc.
*
* Author: Nikos Mavrogiannopoulos
*
* This file is part of GnuTLS.
*
* The GnuTLS 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 program. If not, see <http://www.gnu.org/licenses/>
*
*/
#include "gnutls_int.h"
#include "errors.h"
#include <x509_b64.h>
#include <auth/cert.h>
#include <algorithms.h>
#include <datum.h>
#include <mpi.h>
#include <global.h>
#include <pk.h>
#include <debug.h>
#include <buffers.h>
#include <tls-sig.h>
#include <kx.h>
#include <libtasn1.h>
#include <ext/signature.h>
#include <state.h>
#include <x509/common.h>
#include <abstract_int.h>
static
int check_key_usage_for_sig(gnutls_session_t session, unsigned key_usage, unsigned our_cert)
{
const char *lstr;
unsigned allow_key_usage_violation;
if (our_cert) {
lstr = "Local";
allow_key_usage_violation = session->internals.priorities->allow_server_key_usage_violation;
} else {
lstr = "Peer's";
allow_key_usage_violation = session->internals.allow_key_usage_violation;
}
if (key_usage != 0) {
if (!(key_usage & GNUTLS_KEY_DIGITAL_SIGNATURE)) {
gnutls_assert();
if (likely(allow_key_usage_violation == 0)) {
_gnutls_audit_log(session,
"%s certificate does not allow digital signatures. Key usage violation detected.\n", lstr);
return GNUTLS_E_KEY_USAGE_VIOLATION;
} else {
_gnutls_audit_log(session,
"%s certificate does not allow digital signatures. Key usage violation detected (ignored).\n", lstr);
}
}
}
return 0;
}
#if ENABLE_GOST
static void
gost_swap_signature(gnutls_datum_t * signature)
{
unsigned u;
for (u = 0; u < signature->size / 2; u ++) {
uint8_t t = signature->data[u];
signature->data[u] = signature->data[signature->size - 1 - u];
signature->data[signature->size - 1 - u] = t;
}
}
#endif
/* Generates a signature of all the random data and the parameters.
* Used in *DHE_* ciphersuites for TLS 1.2.
*/
static int
_gnutls_handshake_sign_data12(gnutls_session_t session,
gnutls_pcert_st * cert, gnutls_privkey_t pkey,
gnutls_datum_t * params,
gnutls_datum_t * signature,
gnutls_sign_algorithm_t sign_algo)
{
gnutls_datum_t dconcat;
int ret;
_gnutls_handshake_log
("HSK[%p]: signing TLS 1.2 handshake data: using %s\n", session,
gnutls_sign_algorithm_get_name(sign_algo));
dconcat.size = GNUTLS_RANDOM_SIZE*2 + params->size;
dconcat.data = gnutls_malloc(dconcat.size);
if (dconcat.data == NULL)
return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
memcpy(dconcat.data, session->security_parameters.client_random, GNUTLS_RANDOM_SIZE);
memcpy(dconcat.data+GNUTLS_RANDOM_SIZE, session->security_parameters.server_random, GNUTLS_RANDOM_SIZE);
memcpy(dconcat.data+GNUTLS_RANDOM_SIZE*2, params->data, params->size);
ret = gnutls_privkey_sign_data2(pkey, sign_algo,
0, &dconcat, signature);
if (ret < 0) {
gnutls_assert();
}
gnutls_free(dconcat.data);
return ret;
}
static int
_gnutls_handshake_sign_data10(gnutls_session_t session,
gnutls_pcert_st * cert, gnutls_privkey_t pkey,
gnutls_datum_t * params,
gnutls_datum_t * signature,
gnutls_sign_algorithm_t sign_algo)
{
gnutls_datum_t dconcat;
int ret;
digest_hd_st td_sha;
uint8_t concat[MAX_SIG_SIZE];
const mac_entry_st *me;
gnutls_pk_algorithm_t pk_algo;
if (gnutls_privkey_get_pk_algorithm(pkey, NULL) == GNUTLS_PK_RSA)
me = hash_to_entry(GNUTLS_DIG_MD5_SHA1);
else
me = hash_to_entry(
gnutls_sign_get_hash_algorithm(sign_algo));
if (me == NULL)
return gnutls_assert_val(GNUTLS_E_UNKNOWN_HASH_ALGORITHM);
pk_algo = gnutls_sign_get_pk_algorithm(sign_algo);
if (pk_algo == GNUTLS_PK_UNKNOWN)
return gnutls_assert_val(GNUTLS_E_UNKNOWN_PK_ALGORITHM);
_gnutls_handshake_log
("HSK[%p]: signing handshake data: using %s\n", session,
gnutls_sign_algorithm_get_name(sign_algo));
ret = _gnutls_hash_init(&td_sha, me);
if (ret < 0) {
gnutls_assert();
return ret;
}
_gnutls_hash(&td_sha, session->security_parameters.client_random,
GNUTLS_RANDOM_SIZE);
_gnutls_hash(&td_sha, session->security_parameters.server_random,
GNUTLS_RANDOM_SIZE);
_gnutls_hash(&td_sha, params->data, params->size);
_gnutls_hash_deinit(&td_sha, concat);
dconcat.data = concat;
dconcat.size = _gnutls_hash_get_algo_len(me);
ret = gnutls_privkey_sign_hash(pkey, me->id, GNUTLS_PRIVKEY_SIGN_FLAG_TLS1_RSA,
&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
_gnutls_handshake_sign_data(gnutls_session_t session,
gnutls_pcert_st * cert, gnutls_privkey_t pkey,
gnutls_datum_t * params,
gnutls_datum_t * signature,
gnutls_sign_algorithm_t * sign_algo)
{
const version_entry_st *ver = get_version(session);
unsigned key_usage = 0;
int ret;
*sign_algo = session->security_parameters.server_sign_algo;
if (*sign_algo == GNUTLS_SIGN_UNKNOWN) {
gnutls_assert();
return GNUTLS_E_UNWANTED_ALGORITHM;
}
gnutls_pubkey_get_key_usage(cert->pubkey, &key_usage);
ret = check_key_usage_for_sig(session, key_usage, 1);
if (ret < 0)
return gnutls_assert_val(ret);
if (_gnutls_version_has_selectable_sighash(ver))
return _gnutls_handshake_sign_data12(session, cert, pkey, params, signature, *sign_algo);
else
return _gnutls_handshake_sign_data10(session, cert, pkey, params, signature, *sign_algo);
}
/* Generates a signature of all the random data and the parameters.
* Used in DHE_* ciphersuites.
*/
static int
_gnutls_handshake_verify_data10(gnutls_session_t session,
unsigned verify_flags,
gnutls_pcert_st * cert,
const gnutls_datum_t * params,
gnutls_datum_t * signature,
gnutls_sign_algorithm_t sign_algo)
{
gnutls_datum_t dconcat;
int ret;
digest_hd_st td_sha;
uint8_t concat[MAX_SIG_SIZE];
gnutls_digest_algorithm_t hash_algo;
const mac_entry_st *me;
gnutls_pk_algorithm_t pk_algo;
pk_algo = gnutls_pubkey_get_pk_algorithm(cert->pubkey, NULL);
if (pk_algo == GNUTLS_PK_RSA)
verify_flags |= GNUTLS_PUBKEY_VERIFY_FLAG_TLS1_RSA;
hash_algo = _gnutls_pk_get_tls10_digest(pk_algo);
if (sign_algo == GNUTLS_SIGN_UNKNOWN) {
sign_algo = gnutls_pk_to_sign(pk_algo, hash_algo);
}
me = hash_to_entry(hash_algo);
ret = _gnutls_hash_init(&td_sha, me);
if (ret < 0) {
gnutls_assert();
return ret;
}
_gnutls_hash(&td_sha, session->security_parameters.client_random,
GNUTLS_RANDOM_SIZE);
_gnutls_hash(&td_sha, session->security_parameters.server_random,
GNUTLS_RANDOM_SIZE);
_gnutls_hash(&td_sha, params->data, params->size);
_gnutls_hash_deinit(&td_sha, concat);
dconcat.data = concat;
dconcat.size = _gnutls_hash_get_algo_len(me);
ret = gnutls_pubkey_verify_hash2(cert->pubkey, sign_algo,
GNUTLS_VERIFY_ALLOW_SIGN_WITH_SHA1|verify_flags,
&dconcat, signature);
if (ret < 0)
return gnutls_assert_val(ret);
return ret;
}
static int
_gnutls_handshake_verify_data12(gnutls_session_t session,
unsigned verify_flags,
gnutls_pcert_st * cert,
const gnutls_datum_t * params,
gnutls_datum_t * signature,
gnutls_sign_algorithm_t sign_algo)
{
gnutls_datum_t dconcat;
int ret;
const version_entry_st *ver = get_version(session);
_gnutls_handshake_log
("HSK[%p]: verify TLS 1.2 handshake data: using %s\n", session,
gnutls_sign_algorithm_get_name(sign_algo));
ret =
_gnutls_pubkey_compatible_with_sig(session,
cert->pubkey, ver,
sign_algo);
if (ret < 0)
return gnutls_assert_val(ret);
ret =
_gnutls_session_sign_algo_enabled(session, sign_algo);
if (ret < 0)
return gnutls_assert_val(ret);
dconcat.size = GNUTLS_RANDOM_SIZE*2+params->size;
dconcat.data = gnutls_malloc(dconcat.size);
if (dconcat.data == NULL)
return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
memcpy(dconcat.data, session->security_parameters.client_random, GNUTLS_RANDOM_SIZE);
memcpy(dconcat.data+GNUTLS_RANDOM_SIZE, session->security_parameters.server_random, GNUTLS_RANDOM_SIZE);
memcpy(dconcat.data+GNUTLS_RANDOM_SIZE*2, params->data, params->size);
/* Here we intentionally enable flag GNUTLS_VERIFY_ALLOW_BROKEN
* because we have checked whether the currently used signature
* algorithm is allowed in the session. */
ret = gnutls_pubkey_verify_data2(cert->pubkey, sign_algo, verify_flags|GNUTLS_VERIFY_ALLOW_BROKEN,
&dconcat, signature);
if (ret < 0)
gnutls_assert();
gnutls_free(dconcat.data);
return ret;
}
int
_gnutls_handshake_verify_data(gnutls_session_t session,
unsigned verify_flags,
gnutls_pcert_st * cert,
const gnutls_datum_t * params,
gnutls_datum_t * signature,
gnutls_sign_algorithm_t sign_algo)
{
unsigned key_usage;
int ret;
const version_entry_st *ver = get_version(session);
if (cert == NULL) {
gnutls_assert();
return GNUTLS_E_CERTIFICATE_ERROR;
}
gnutls_pubkey_get_key_usage(cert->pubkey, &key_usage);
ret = check_key_usage_for_sig(session, key_usage, 0);
if (ret < 0)
return gnutls_assert_val(ret);
gnutls_sign_algorithm_set_server(session, sign_algo);
if (_gnutls_version_has_selectable_sighash(ver))
return _gnutls_handshake_verify_data12(session, verify_flags, cert, params, signature, sign_algo);
else
return _gnutls_handshake_verify_data10(session, verify_flags, cert, params, signature, sign_algo);
}
/* Client certificate verify calculations
*/
/* this is _gnutls_handshake_verify_crt_vrfy for TLS 1.2
*/
static int
_gnutls_handshake_verify_crt_vrfy12(gnutls_session_t session,
unsigned verify_flags,
gnutls_pcert_st * cert,
gnutls_datum_t * signature,
gnutls_sign_algorithm_t sign_algo)
{
int ret;
gnutls_datum_t dconcat;
ret = _gnutls_session_sign_algo_enabled(session, sign_algo);
if (ret < 0)
return gnutls_assert_val(ret);
dconcat.data = session->internals.handshake_hash_buffer.data;
dconcat.size = session->internals.handshake_hash_buffer_prev_len;
/* Here we intentionally enable flag GNUTLS_VERIFY_ALLOW_BROKEN
* because we have checked whether the currently used signature
* algorithm is allowed in the session. */
ret = gnutls_pubkey_verify_data2(cert->pubkey, sign_algo, verify_flags|GNUTLS_VERIFY_ALLOW_BROKEN,
&dconcat, signature);
if (ret < 0)
gnutls_assert();
return ret;
}
/* Verifies a SSL 3.0 signature (like the one in the client certificate
* verify message).
*/
#ifdef ENABLE_SSL3
static int
_gnutls_handshake_verify_crt_vrfy3(gnutls_session_t session,
unsigned verify_flags,
gnutls_pcert_st * cert,
gnutls_datum_t * signature,
gnutls_sign_algorithm_t sign_algo)
{
int ret;
uint8_t concat[MAX_SIG_SIZE];
digest_hd_st td_sha;
gnutls_datum_t dconcat;
gnutls_pk_algorithm_t pk =
gnutls_pubkey_get_pk_algorithm(cert->pubkey, NULL);
ret = _gnutls_generate_master(session, 1);
if (ret < 0) {
return gnutls_assert_val(ret);
}
dconcat.data = concat;
dconcat.size = 0;
if (pk == GNUTLS_PK_RSA) {
digest_hd_st td_md5;
ret = _gnutls_hash_init(&td_md5,
hash_to_entry(GNUTLS_DIG_MD5));
if (ret < 0)
return gnutls_assert_val(ret);
_gnutls_hash(&td_md5,
session->internals.handshake_hash_buffer.data,
session->internals.handshake_hash_buffer_prev_len);
ret = _gnutls_mac_deinit_ssl3_handshake(&td_md5, concat,
session->security_parameters.
master_secret,
GNUTLS_MASTER_SIZE);
if (ret < 0)
return gnutls_assert_val(ret);
verify_flags |= GNUTLS_PUBKEY_VERIFY_FLAG_TLS1_RSA;
dconcat.size = 16;
}
ret = _gnutls_hash_init(&td_sha, hash_to_entry(GNUTLS_DIG_SHA1));
if (ret < 0) {
gnutls_assert();
return GNUTLS_E_HASH_FAILED;
}
_gnutls_hash(&td_sha,
session->internals.handshake_hash_buffer.data,
session->internals.handshake_hash_buffer_prev_len);
ret =
_gnutls_mac_deinit_ssl3_handshake(&td_sha,
dconcat.data + dconcat.size,
session->security_parameters.
master_secret,
GNUTLS_MASTER_SIZE);
if (ret < 0) {
return gnutls_assert_val(ret);
}
dconcat.size += 20;
ret = gnutls_pubkey_verify_hash2(cert->pubkey, GNUTLS_SIGN_UNKNOWN,
GNUTLS_VERIFY_ALLOW_SIGN_WITH_SHA1|verify_flags,
&dconcat, signature);
if (ret < 0)
return gnutls_assert_val(ret);
return ret;
}
#endif
/* Verifies a TLS signature (like the one in the client certificate
* verify message).
*/
int
_gnutls_handshake_verify_crt_vrfy(gnutls_session_t session,
unsigned verify_flags,
gnutls_pcert_st * cert,
gnutls_datum_t * signature,
gnutls_sign_algorithm_t sign_algo)
{
int ret;
uint8_t concat[MAX_SIG_SIZE];
digest_hd_st td_sha;
gnutls_datum_t dconcat;
const version_entry_st *ver = get_version(session);
gnutls_pk_algorithm_t pk_algo;
gnutls_digest_algorithm_t hash_algo;
const mac_entry_st *me;
unsigned key_usage;
if (cert == NULL) {
gnutls_assert();
return GNUTLS_E_CERTIFICATE_ERROR;
}
gnutls_pubkey_get_key_usage(cert->pubkey, &key_usage);
ret = check_key_usage_for_sig(session, key_usage, 0);
if (ret < 0)
return gnutls_assert_val(ret);
_gnutls_handshake_log("HSK[%p]: verify cert vrfy: using %s\n",
session,
gnutls_sign_algorithm_get_name(sign_algo));
if (unlikely(ver == NULL))
return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
gnutls_sign_algorithm_set_client(session, sign_algo);
/* TLS 1.2 */
if (_gnutls_version_has_selectable_sighash(ver))
return _gnutls_handshake_verify_crt_vrfy12(session,
verify_flags,
cert,
signature,
sign_algo);
#ifdef ENABLE_SSL3
if (ver->id == GNUTLS_SSL3)
return _gnutls_handshake_verify_crt_vrfy3(session,
verify_flags,
cert,
signature,
sign_algo);
#endif
/* TLS 1.0 and TLS 1.1 */
pk_algo = gnutls_pubkey_get_pk_algorithm(cert->pubkey, NULL);
if (pk_algo == GNUTLS_PK_RSA)
verify_flags |= GNUTLS_PUBKEY_VERIFY_FLAG_TLS1_RSA;
hash_algo = _gnutls_pk_get_tls10_digest(pk_algo);
me = hash_to_entry(hash_algo);
sign_algo = gnutls_pk_to_sign(pk_algo, hash_algo);
ret = _gnutls_hash_init(&td_sha, me);
if (ret < 0) {
gnutls_assert();
return ret;
}
_gnutls_hash(&td_sha,
session->internals.handshake_hash_buffer.data,
session->internals.handshake_hash_buffer_prev_len);
_gnutls_hash_deinit(&td_sha, concat);
dconcat.data = concat;
dconcat.size = _gnutls_hash_get_algo_len(me);
#if ENABLE_GOST
/* See comment above */
if (IS_GOST(pk_algo))
gost_swap_signature(signature);
#endif
ret = gnutls_pubkey_verify_hash2(cert->pubkey, sign_algo,
GNUTLS_VERIFY_ALLOW_SIGN_WITH_SHA1|verify_flags,
&dconcat, signature);
if (ret < 0)
gnutls_assert();
return ret;
}
/* the same as _gnutls_handshake_sign_crt_vrfy except that it is made for TLS 1.2.
* Returns the used signature algorihm, or a negative error code.
*/
static int
_gnutls_handshake_sign_crt_vrfy12(gnutls_session_t session,
gnutls_pcert_st * cert,
gnutls_privkey_t pkey,
gnutls_datum_t * signature)
{
gnutls_datum_t dconcat;
gnutls_sign_algorithm_t sign_algo;
int ret;
sign_algo = _gnutls_session_get_sign_algo(session, cert, pkey, 1);
if (sign_algo == GNUTLS_SIGN_UNKNOWN) {
gnutls_assert();
return GNUTLS_E_UNWANTED_ALGORITHM;
}
gnutls_sign_algorithm_set_client(session, sign_algo);
_gnutls_debug_log("sign handshake cert vrfy: picked %s\n",
gnutls_sign_algorithm_get_name(sign_algo));
dconcat.data = session->internals.handshake_hash_buffer.data;
dconcat.size = session->internals.handshake_hash_buffer.length;
ret = gnutls_privkey_sign_data2(pkey, sign_algo,
0, &dconcat, signature);
if (ret < 0) {
gnutls_assert();
return ret;
}
return sign_algo;
}
#ifdef ENABLE_SSL3
static int
_gnutls_handshake_sign_crt_vrfy3(gnutls_session_t session,
gnutls_pcert_st * cert,
gnutls_privkey_t pkey,
gnutls_datum_t * signature)
{
gnutls_datum_t dconcat;
int ret;
uint8_t concat[MAX_SIG_SIZE];
digest_hd_st td_sha;
gnutls_pk_algorithm_t pk =
gnutls_privkey_get_pk_algorithm(pkey, NULL);
ret = _gnutls_generate_master(session, 1);
if (ret < 0) {
gnutls_assert();
return ret;
}
dconcat.data = concat;
dconcat.size = 0;
if (pk == GNUTLS_PK_RSA) {
digest_hd_st td_md5;
ret =
_gnutls_hash_init(&td_md5,
hash_to_entry(GNUTLS_DIG_MD5));
if (ret < 0)
return gnutls_assert_val(ret);
_gnutls_hash(&td_md5,
session->internals.handshake_hash_buffer.data,
session->internals.handshake_hash_buffer.
length);
ret = _gnutls_mac_deinit_ssl3_handshake(&td_md5,
dconcat.data,
session->security_parameters.
master_secret,
GNUTLS_MASTER_SIZE);
if (ret < 0)
return gnutls_assert_val(ret);
dconcat.size = 16;
}
ret = _gnutls_hash_init(&td_sha, hash_to_entry(GNUTLS_DIG_SHA1));
if (ret < 0) {
gnutls_assert();
return ret;
}
_gnutls_hash(&td_sha,
session->internals.handshake_hash_buffer.data,
session->internals.handshake_hash_buffer.length);
ret =
_gnutls_mac_deinit_ssl3_handshake(&td_sha,
dconcat.data + dconcat.size,
session->security_parameters.
master_secret,
GNUTLS_MASTER_SIZE);
if (ret < 0)
return gnutls_assert_val(ret);
dconcat.size += 20;
ret = gnutls_privkey_sign_hash(pkey, GNUTLS_DIG_SHA1,
GNUTLS_PRIVKEY_SIGN_FLAG_TLS1_RSA,
&dconcat, signature);
if (ret < 0)
return gnutls_assert_val(ret);
return GNUTLS_SIGN_UNKNOWN;
}
#endif
/* Generates a signature of all the previous sent packets in the
* handshake procedure.
* 20040227: now it works for SSL 3.0 as well
* 20091031: works for TLS 1.2 too!
*
* For TLS1.x, x<2 returns negative for failure and zero or unspecified for success.
* For TLS1.2 returns the signature algorithm used on success, or a negative error code;
*
* Returns the used signature algorihm, or a negative error code.
*/
int
_gnutls_handshake_sign_crt_vrfy(gnutls_session_t session,
gnutls_pcert_st * cert,
gnutls_privkey_t pkey,
gnutls_datum_t * signature)
{
gnutls_datum_t dconcat;
int ret;
uint8_t concat[MAX_SIG_SIZE];
digest_hd_st td_sha;
const version_entry_st *ver = get_version(session);
gnutls_pk_algorithm_t pk =
gnutls_privkey_get_pk_algorithm(pkey, NULL);
gnutls_digest_algorithm_t hash_algo;
const mac_entry_st *me;
unsigned key_usage = 0;
if (unlikely(ver == NULL))
return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
gnutls_pubkey_get_key_usage(cert->pubkey, &key_usage);
ret = check_key_usage_for_sig(session, key_usage, 1);
if (ret < 0)
return gnutls_assert_val(ret);
/* TLS 1.2 */
if (_gnutls_version_has_selectable_sighash(ver))
return _gnutls_handshake_sign_crt_vrfy12(session, cert,
pkey, signature);
/* ensure 1024 bit DSA keys are used */
ret =
_gnutls_pubkey_compatible_with_sig(session, cert->pubkey, ver,
GNUTLS_SIGN_UNKNOWN);
if (ret < 0)
return gnutls_assert_val(ret);
/* TLS 1.1 or earlier */
#ifdef ENABLE_SSL3
if (ver->id == GNUTLS_SSL3)
return _gnutls_handshake_sign_crt_vrfy3(session, cert,
pkey, signature);
#endif
hash_algo = _gnutls_pk_get_tls10_digest(pk);
if (unlikely(hash_algo == GNUTLS_DIG_UNKNOWN))
return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
me = hash_to_entry(hash_algo);
ret = _gnutls_hash_init(&td_sha, me);
if (ret < 0) {
gnutls_assert();
return ret;
}
_gnutls_hash(&td_sha,
session->internals.handshake_hash_buffer.data,
session->internals.handshake_hash_buffer.length);
_gnutls_hash_deinit(&td_sha, concat);
dconcat.data = concat;
dconcat.size = _gnutls_hash_get_algo_len(me);
ret = gnutls_privkey_sign_hash(pkey, me->id, GNUTLS_PRIVKEY_SIGN_FLAG_TLS1_RSA,
&dconcat, signature);
if (ret < 0) {
gnutls_assert();
return ret;
}
#if ENABLE_GOST
/* CertificateVerify is the only place, where they got signature byte
* order different from the rest of the GOST DSA usage. Invert here,
* rather than hacking pk_sign/pk_verify. */
if (IS_GOST(pk))
gost_swap_signature(signature);
#endif
return GNUTLS_SIGN_UNKNOWN;
}
|