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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
|
/*
* Copyright (C) 2002,2003 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <gnutls_int.h>
#include "gnutls_auth_int.h"
#include "gnutls_errors.h"
#include <gnutls_cert.h>
#include <auth_cert.h>
#include "gnutls_dh.h"
#include "gnutls_num.h"
#include "libtasn1.h"
#include "gnutls_datum.h"
#include <gnutls_random.h>
#include <gnutls_pk.h>
#include <gnutls_algorithms.h>
#include <gnutls_global.h>
#include <gnutls_record.h>
#include <gnutls_sig.h>
#include <gnutls_state.h>
#include <gnutls_pk.h>
#include <gnutls_str.h>
#include <debug.h>
#include <x509_b64.h>
#include <gnutls_privkey.h>
#include <gnutls_x509.h>
#include "x509/common.h"
#include "x509/x509.h"
#include "x509/verify.h"
#include "x509/compat.h"
#include "x509/mpi.h"
#include "x509/pkcs7.h"
/*
* some x509 certificate parsing functions.
*/
#define CLEAR_CERTS for(x=0;x<peer_certificate_list_size;x++) { \
if (peer_certificate_list[x]) \
gnutls_x509_crt_deinit(peer_certificate_list[x]); \
} \
gnutls_free( peer_certificate_list)
/*-
* _gnutls_x509_cert_verify_peers - This function returns the peer's certificate status
* @session: is a gnutls session
*
* This function will try to verify the peer's certificate and return it's status (TRUSTED, REVOKED etc.).
* The return value (status) should be one of the gnutls_certificate_status enumerated elements.
* However you must also check the peer's name in order to check if the verified certificate belongs to the
* actual peer. Returns a negative error code in case of an error, or GNUTLS_E_NO_CERTIFICATE_FOUND if no certificate was sent.
*
-*/
int _gnutls_x509_cert_verify_peers(gnutls_session session)
{
CERTIFICATE_AUTH_INFO info;
const gnutls_certificate_credentials cred;
unsigned int verify;
gnutls_x509_crt *peer_certificate_list;
int peer_certificate_list_size, i, x, ret;
CHECK_AUTH(GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
info = _gnutls_get_auth_info(session);
if (info == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
cred = _gnutls_get_cred(session->key, GNUTLS_CRD_CERTIFICATE, NULL);
if (cred == NULL) {
gnutls_assert();
return GNUTLS_E_INSUFICIENT_CREDENTIALS;
}
if (info->raw_certificate_list == NULL || info->ncerts == 0)
return GNUTLS_E_NO_CERTIFICATE_FOUND;
/* generate a list of gnutls_certs based on the auth info
* raw certs.
*/
peer_certificate_list_size = info->ncerts;
peer_certificate_list =
gnutls_calloc(1,
peer_certificate_list_size *
sizeof(gnutls_x509_crt));
if (peer_certificate_list == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
for (i = 0; i < peer_certificate_list_size; i++) {
ret = gnutls_x509_crt_init( &peer_certificate_list[i]);
if (ret < 0) {
gnutls_assert();
CLEAR_CERTS;
return ret;
}
ret =
gnutls_x509_crt_import(peer_certificate_list[i],
&info->
raw_certificate_list[i], GNUTLS_X509_FMT_DER);
if (ret < 0) {
gnutls_assert();
CLEAR_CERTS;
return ret;
}
}
/* Verify certificate
*/
ret =
gnutls_x509_crt_list_verify(peer_certificate_list,
peer_certificate_list_size,
cred->x509_ca_list, cred->x509_ncas,
cred->x509_crl_list, cred->x509_ncrls, 0, &verify);
CLEAR_CERTS;
if (ret < 0) {
gnutls_assert();
return ret;
}
return verify;
}
/*
* Read certificates and private keys, from files, memory etc.
*/
/* returns error if the certificate has different algorithm than
* the given key parameters.
*/
static int _gnutls_check_key_cert_match( gnutls_certificate_credentials res)
{
int pk = res->cert_list[res->ncerts-1][0].subject_pk_algorithm;
if (res->pkey[res->ncerts-1].pk_algorithm != (unsigned int)pk) {
gnutls_assert();
return GNUTLS_E_CERTIFICATE_KEY_MISMATCH;
}
return 0;
}
#define MAX_FILE_SIZE 100*1024
/* Reads a DER encoded certificate list from memory and stores it to
* a gnutls_cert structure. This is only called if PKCS7 read fails.
* returns the number of certificates parsed (1)
*/
static int parse_der_cert_mem( gnutls_cert** cert_list, int* ncerts,
const char *input_cert, int input_cert_size)
{
int i;
gnutls_datum tmp;
int ret;
i = *ncerts + 1;
*cert_list =
(gnutls_cert *) gnutls_realloc_fast( *cert_list,
i *
sizeof(gnutls_cert));
if ( *cert_list == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
tmp.data = (opaque*)input_cert;
tmp.size = input_cert_size;
ret = _gnutls_x509_cert2gnutls_cert(
&cert_list[0][i - 1], &tmp, 0);
if ( ret < 0) {
gnutls_assert();
return ret;
}
*ncerts = i;
return 1; /* one certificate parsed */
}
#define CERT_PEM 1
/* Reads a PKCS7 base64 encoded certificate list from memory and stores it to
* a gnutls_cert structure.
* returns the number of certificate parsed
*/
static int parse_pkcs7_cert_mem( gnutls_cert** cert_list, int* ncerts, const
char *input_cert, int input_cert_size, int flags)
{
int i, j, count;
gnutls_datum tmp, tmp2;
int ret;
opaque pcert[MAX_X509_CERT_SIZE];
int pcert_size;
gnutls_pkcs7 pkcs7;
ret = gnutls_pkcs7_init( &pkcs7);
if (ret < 0) {
gnutls_assert();
return ret;
}
if (flags & CERT_PEM)
ret = gnutls_pkcs7_import( pkcs7, &tmp, GNUTLS_X509_FMT_PEM);
else
ret = gnutls_pkcs7_import( pkcs7, &tmp, GNUTLS_X509_FMT_DER);
if (ret < 0) {
/* if we failed to read the structure,
* then just try to decode a plain DER
* certificate.
*/
gnutls_assert();
gnutls_pkcs7_deinit(pkcs7);
return parse_der_cert_mem( cert_list, ncerts,
input_cert, input_cert_size);
}
i = *ncerts + 1;
/* tmp now contains the decoded certificate list */
tmp.data = (opaque*)input_cert;
tmp.size = input_cert_size;
ret = gnutls_pkcs7_get_certificate_count( pkcs7);
if (ret < 0) {
gnutls_assert();
gnutls_pkcs7_deinit(pkcs7);
return ret;
}
count = ret;
j = count - 1;
do {
pcert_size = sizeof(pcert);
ret = gnutls_pkcs7_get_certificate( pkcs7, j, pcert, &pcert_size);
j--;
/* if the current certificate is too long, just ignore
* it. */
if (ret==GNUTLS_E_MEMORY_ERROR) {
count--;
continue;
}
if (ret >= 0) {
*cert_list =
(gnutls_cert *) gnutls_realloc_fast( *cert_list,
i * sizeof(gnutls_cert));
if ( *cert_list == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
tmp2.data = pcert;
tmp2.size = pcert_size;
ret = _gnutls_x509_cert2gnutls_cert(
&cert_list[0][i - 1], &tmp2, 0);
if ( ret < 0) {
gnutls_assert();
gnutls_pkcs7_deinit(pkcs7);
return ret;
}
i++;
}
} while (ret >= 0 && j >= 0);
*ncerts = i - 1;
gnutls_pkcs7_deinit(pkcs7);
return count;
}
/* Reads a base64 encoded certificate list from memory and stores it to
* a gnutls_cert structure. Returns the number of certificate parsed.
*/
static int parse_pem_cert_mem( gnutls_cert** cert_list, int* ncerts,
const char *input_cert, int input_cert_size)
{
int siz, siz2, i;
const char *ptr;
opaque *ptr2;
gnutls_datum tmp;
int ret, count;
if ( (ptr = strstr( input_cert, PEM_PKCS7_SEP)) != NULL)
{
siz = strlen( ptr);
ret = parse_pkcs7_cert_mem( cert_list, ncerts, ptr,
siz, CERT_PEM);
return ret;
}
/* move to the certificate
*/
ptr = strstr( input_cert, PEM_CERT_SEP);
if (ptr == NULL) ptr = strstr( input_cert, PEM_CERT_SEP2);
if (ptr == NULL) {
gnutls_assert();
return GNUTLS_E_BASE64_DECODING_ERROR;
}
siz = strlen( ptr);
i = *ncerts + 1;
count = 0;
do {
siz2 = _gnutls_fbase64_decode(NULL, ptr, siz, &ptr2);
siz -= siz2;
if (siz2 < 0) {
gnutls_assert();
return GNUTLS_E_BASE64_DECODING_ERROR;
}
*cert_list =
(gnutls_cert *) gnutls_realloc_fast( *cert_list,
i *
sizeof(gnutls_cert));
if ( *cert_list == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
tmp.data = ptr2;
tmp.size = siz2;
ret = _gnutls_x509_cert2gnutls_cert(
&cert_list[0][i - 1], &tmp, 0);
if ( ret < 0) {
gnutls_assert();
return ret;
}
/* now we move ptr after the pem header
*/
ptr++;
/* find the next certificate (if any)
*/
ptr = strstr(ptr, PEM_CERT_SEP);
if (ptr == NULL) ptr = strstr( input_cert, PEM_CERT_SEP2);
i++;
count++;
} while ( ptr != NULL);
*ncerts = i - 1;
return count;
}
/* Reads a DER or PEM certificate from memory
*/
static
int read_cert_mem(gnutls_certificate_credentials res, const char *cert, int cert_size,
gnutls_x509_crt_format type)
{
int ret;
/* allocate space for the certificate to add
*/
res->cert_list = gnutls_realloc_fast( res->cert_list,
(1+ res->ncerts)*sizeof(gnutls_cert*));
if ( res->cert_list==NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
res->cert_list_length = gnutls_realloc_fast( res->cert_list_length,
(1+ res->ncerts)*sizeof(int));
if (res->cert_list_length==NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
res->cert_list[res->ncerts] = NULL; /* for realloc */
res->cert_list_length[res->ncerts] = 0;
if (type==GNUTLS_X509_FMT_DER)
ret = parse_pkcs7_cert_mem( &res->cert_list[res->ncerts],
&res->cert_list_length[res->ncerts], cert, cert_size, 0);
else
ret = parse_pem_cert_mem( &res->cert_list[res->ncerts], &res->cert_list_length[res->ncerts],
cert, cert_size);
if (ret < 0) {
gnutls_assert();
return ret;
}
return ret;
}
/* This will check if the given DER key is a PKCS-1 RSA key.
* Returns 0 if the key is an RSA one.
*/
int _gnutls_der_check_if_rsa_key(const gnutls_datum * key_struct)
{
ASN1_TYPE c2;
int result;
if (key_struct->size == 0 || key_struct->data == NULL) {
gnutls_assert();
return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
}
if ((result=_gnutls_asn1_create_element
(_gnutls_get_gnutls_asn(), "GNUTLS.RSAPrivateKey", &c2,
"rsakey")) != ASN1_SUCCESS)
{
gnutls_assert();
return _gnutls_asn2err(result);
}
result = asn1_der_decoding(&c2, key_struct->data, key_struct->size, NULL);
asn1_delete_structure(&c2);
if (result != ASN1_SUCCESS) {
/* couldn't decode DER */
gnutls_assert();
return _gnutls_asn2err(result);
}
return 0;
}
/* This will check if the given DER key is an openssl formated DSA key.
* Returns 0 if the key is a DSA one.
*/
int _gnutls_der_check_if_dsa_key(const gnutls_datum * key_struct)
{
ASN1_TYPE c2;
int result;
if (key_struct->size == 0 || key_struct->data == NULL) {
gnutls_assert();
return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
}
if ((result=_gnutls_asn1_create_element
(_gnutls_get_gnutls_asn(), "GNUTLS.DSAPrivateKey", &c2,
"rsakey")) != ASN1_SUCCESS)
{
gnutls_assert();
return _gnutls_asn2err(result);
}
result = asn1_der_decoding(&c2, key_struct->data, key_struct->size, NULL);
asn1_delete_structure(&c2);
if (result != ASN1_SUCCESS) {
/* couldn't decode DER */
gnutls_assert();
return _gnutls_asn2err(result);
}
return 0;
}
/* Reads a PEM encoded PKCS-1 RSA private key from memory
* 2002-01-26: Added ability to read DSA keys.
* type indicates the certificate format.
*/
static int read_key_mem(gnutls_certificate_credentials res, const char *key, int key_size,
gnutls_x509_crt_format type)
{
int ret;
opaque *b64 = NULL;
gnutls_datum tmp;
gnutls_pk_algorithm pk;
/* allocate space for the pkey list
*/
res->pkey = gnutls_realloc_fast( res->pkey, (res->ncerts+1)*sizeof(gnutls_private_key));
if (res->pkey==NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
/* read PKCS-1 private key */
if (type==GNUTLS_X509_FMT_DER) { /* DER */
int cv;
tmp.data = (opaque*)key;
tmp.size = key_size;
/* The only way to distinguish the keys
* is to count the sequence of integers.
*/
pk = GNUTLS_PK_UNKNOWN;
cv = _gnutls_der_check_if_rsa_key( &tmp);
if (cv==0)
pk = GNUTLS_PK_RSA;
else {
cv = _gnutls_der_check_if_dsa_key( &tmp);
if (cv == 0) pk = GNUTLS_PK_DSA;
}
} else { /* PEM */
/* If we find the "DSA PRIVATE" string in the
* pem encoded certificate then it's a DSA key.
*/
if (strstr( key, "DSA PRIVATE")!=NULL) {
pk = GNUTLS_PK_DSA;
key = strstr( key, PEM_KEY_DSA_SEP);
if (key == NULL) {
gnutls_assert();
return GNUTLS_E_BASE64_DECODING_ERROR;
} key_size = strlen( key);
} else {
pk = GNUTLS_PK_RSA;
key = strstr( key, PEM_KEY_RSA_SEP);
if (key == NULL) {
gnutls_assert();
return GNUTLS_E_BASE64_DECODING_ERROR;
}
key_size = strlen( key);
}
ret = _gnutls_fbase64_decode( NULL, key, key_size, &b64);
if (ret < 0) {
gnutls_assert();
return GNUTLS_E_BASE64_DECODING_ERROR;
}
tmp.data = b64;
tmp.size = ret;
}
switch (pk) { /* decode the key */
case GNUTLS_PK_RSA:
if ((ret =
_gnutls_PKCS1key2gnutlsKey(&res->pkey[res->ncerts],
tmp)) < 0) {
gnutls_assert();
gnutls_free(b64);
return ret;
}
break;
case GNUTLS_PK_DSA:
if ((ret =
_gnutls_DSAkey2gnutlsKey(&res->pkey[res->ncerts],
tmp)) < 0) {
gnutls_assert();
gnutls_free(b64);
return ret;
}
break;
default:
gnutls_assert();
gnutls_free(b64);
return GNUTLS_E_INTERNAL_ERROR;
}
/* this doesn't hurt in the DER case, since
* b64 is NULL
*/
gnutls_free(b64);
return 0;
}
/* Reads a certificate file
*/
static int read_cert_file(gnutls_certificate_credentials res, const char *certfile,
gnutls_x509_crt_format type)
{
int siz;
char x[MAX_FILE_SIZE];
FILE *fd1;
fd1 = fopen(certfile, "rb");
if (fd1 == NULL)
return GNUTLS_E_FILE_ERROR;
siz = fread(x, 1, sizeof(x)-1, fd1);
fclose(fd1);
x[siz] = 0;
return read_cert_mem( res, x, siz, type);
}
/* Reads PKCS-1 RSA private key file or a DSA file (in the format openssl
* stores it).
*/
static int read_key_file(gnutls_certificate_credentials res, const char *keyfile,
gnutls_x509_crt_format type)
{
int siz;
char x[MAX_FILE_SIZE];
FILE *fd2;
fd2 = fopen(keyfile, "rb");
if (fd2 == NULL)
return GNUTLS_E_FILE_ERROR;
siz = fread(x, 1, sizeof(x)-1, fd2);
fclose(fd2);
x[siz] = 0;
return read_key_mem( res, x, siz, type);
}
/**
* gnutls_certificate_set_x509_key_mem - Used to set keys in a gnutls_certificate_credentials structure
* @res: is an &gnutls_certificate_credentials structure.
* @CERT: contains a certificate list (path) for the specified private key
* @KEY: is the private key
* @type: is PEM or DER
*
* This function sets a certificate/private key pair in the
* gnutls_certificate_credentials structure. This function may be called
* more than once (in case multiple keys/certificates exist for the
* server).
*
* Currently are supported: RSA PKCS-1 encoded private keys,
* DSA private keys.
*
* DSA private keys are encoded the OpenSSL way, which is an ASN.1
* DER sequence of 6 INTEGERs - version, p, q, g, pub, priv.
*
* Note that the keyUsage (2 5 29 15) PKIX extension in X.509 certificates
* is supported. This means that certificates intended for signing cannot
* be used for ciphersuites that require encryption.
*
* If the certificate and the private key are given in PEM encoding
* then the strings that hold their values must be null terminated.
*
**/
int gnutls_certificate_set_x509_key_mem(gnutls_certificate_credentials res, const gnutls_datum* CERT,
const gnutls_datum* KEY, gnutls_x509_crt_format type)
{
int ret;
/* this should be first
*/
if ((ret = read_key_mem( res, KEY->data, KEY->size, type)) < 0)
return ret;
if ((ret = read_cert_mem( res, CERT->data, CERT->size, type)) < 0)
return ret;
if ((ret=_gnutls_check_key_cert_match( res)) < 0) {
gnutls_assert();
return ret;
}
return 0;
}
/**
* gnutls_certificate_set_x509_key_file - Used to set keys in a gnutls_certificate_credentials structure
* @res: is an &gnutls_certificate_credentials structure.
* @CERTFILE: is a file that containing the certificate list (path) for
* the specified private key, in PKCS7 format, or a list of certificates
* @KEYFILE: is a file that contains the private key
* @type: is PEM or DER
*
* This function sets a certificate/private key pair in the
* gnutls_certificate_credentials structure. This function may be called
* more than once (in case multiple keys/certificates exist for the
* server).
*
* Currently only PKCS-1 encoded RSA and DSA private keys are accepted by
* this function.
*
**/
int gnutls_certificate_set_x509_key_file(gnutls_certificate_credentials res, const char *CERTFILE,
const char *KEYFILE, gnutls_x509_crt_format type)
{
int ret;
/* this should be first
*/
if ((ret = read_key_file(res, KEYFILE, type)) < 0)
return ret;
if ((ret = read_cert_file(res, CERTFILE, type)) < 0)
return ret;
res->ncerts++;
if ((ret=_gnutls_check_key_cert_match( res)) < 0) {
gnutls_assert();
return ret;
}
return 0;
}
static int generate_rdn_seq( gnutls_certificate_credentials res) {
gnutls_const_datum tmp;
gnutls_datum _tmp;
int ret;
uint size, i;
opaque *pdata;
/* Generate the RDN sequence
* This will be sent to clients when a certificate
* request message is sent.
*/
/* FIXME: in case of a client it is not needed
* to do that. This would save time and memory.
* However we don't have that information available
* here.
*/
size = 0;
for (i = 0; i < res->x509_ncas; i++) {
if ((ret = _gnutls_x509_crt_get_raw_issuer_dn(
res->x509_ca_list[i], &tmp)) < 0) {
gnutls_assert();
return ret;
}
size += (2 + tmp.size);
}
if (res->x509_rdn_sequence.data != NULL)
gnutls_free( res->x509_rdn_sequence.data);
res->x509_rdn_sequence.data = gnutls_malloc(size);
if (res->x509_rdn_sequence.data == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
res->x509_rdn_sequence.size = size;
pdata = res->x509_rdn_sequence.data;
for (i = 0; i < res->x509_ncas; i++) {
if ((ret = _gnutls_x509_crt_get_raw_issuer_dn(
res->x509_ca_list[i], &tmp)) < 0) {
gnutls_free(res->x509_rdn_sequence.data);
res->x509_rdn_sequence.size = 0;
res->x509_rdn_sequence.data = NULL;
gnutls_assert();
return ret;
}
_tmp.data = (char*) tmp.data;
_tmp.size = tmp.size;
_gnutls_write_datum16(pdata, _tmp);
pdata += (2 + tmp.size);
}
return 0;
}
/* Returns 0 if it's ok to use the gnutls_kx_algorithm with this
* certificate (uses the KeyUsage field).
*/
int _gnutls_check_key_usage( const gnutls_cert* cert,
gnutls_kx_algorithm alg)
{
unsigned int keyUsage = 0;
int encipher_type;
if ( cert==NULL) {
gnutls_assert();
return GNUTLS_E_INTERNAL_ERROR;
}
/* FIXME: check here */
if (_gnutls_map_kx_get_cred(alg, 1) == GNUTLS_CRD_CERTIFICATE ||
_gnutls_map_kx_get_cred(alg, 0) == GNUTLS_CRD_CERTIFICATE)
{
keyUsage = cert->keyUsage;
encipher_type = _gnutls_kx_encipher_type( alg);
if (keyUsage != 0 && encipher_type != CIPHER_IGN) {
/* If keyUsage has been set in the certificate
*/
if ( encipher_type == CIPHER_ENCRYPT) {
/* If the key exchange method requires an encipher
* type algorithm, and key's usage does not permit
* encipherment, then fail.
*/
if (!(keyUsage & KEY_KEY_ENCIPHERMENT))
return
GNUTLS_E_KEY_USAGE_VIOLATION;
}
if ( encipher_type == CIPHER_SIGN) {
/* The same as above, but for sign only keys
*/
if (!(keyUsage & KEY_DIGITAL_SIGNATURE))
return
GNUTLS_E_KEY_USAGE_VIOLATION;
}
}
}
return 0;
}
static int parse_pem_ca_mem( gnutls_x509_crt** cert_list, int* ncerts,
const char *input_cert, int input_cert_size)
{
int siz, i;
const char *ptr;
gnutls_datum tmp;
int ret, count;
/* move to the certificate
*/
ptr = strstr( input_cert, PEM_CERT_SEP);
if (ptr == NULL) ptr = strstr( input_cert, PEM_CERT_SEP2);
if (ptr == NULL) {
gnutls_assert();
return GNUTLS_E_BASE64_DECODING_ERROR;
}
siz = strlen( ptr);
i = *ncerts + 1;
count = 0;
do {
*cert_list =
(gnutls_x509_crt *) gnutls_realloc_fast( *cert_list,
i *
sizeof(gnutls_x509_crt));
if ( *cert_list == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
ret = gnutls_x509_crt_init( &cert_list[0][i - 1]);
if ( ret < 0) {
gnutls_assert();
return ret;
}
tmp.data = (char*)ptr;
tmp.size = siz;
ret =
gnutls_x509_crt_import(
cert_list[0][i - 1],
&tmp, GNUTLS_X509_FMT_PEM);
if (ret < 0) {
gnutls_assert();
return ret;
}
/* now we move ptr after the pem header
*/
ptr++;
/* find the next certificate (if any)
*/
ptr = strstr(ptr, PEM_CERT_SEP);
if (ptr == NULL) ptr = strstr( input_cert, PEM_CERT_SEP2);
i++;
count++;
} while ( ptr != NULL);
*ncerts = i - 1;
return count;
}
/* Reads a DER encoded certificate list from memory and stores it to
* a gnutls_cert structure. This is only called if PKCS7 read fails.
* returns the number of certificates parsed (1)
*/
static int parse_der_ca_mem( gnutls_x509_crt** cert_list, int* ncerts,
const char *input_cert, int input_cert_size)
{
int i;
gnutls_datum tmp;
int ret;
i = *ncerts + 1;
*cert_list =
(gnutls_x509_crt *) gnutls_realloc_fast( *cert_list,
i *
sizeof(gnutls_x509_crt));
if ( *cert_list == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
tmp.data = (opaque*)input_cert;
tmp.size = input_cert_size;
ret = gnutls_x509_crt_init( &cert_list[0][i - 1]);
if ( ret < 0) {
gnutls_assert();
return ret;
}
ret =
gnutls_x509_crt_import(
cert_list[0][i - 1],
&tmp, GNUTLS_X509_FMT_DER);
if (ret < 0) {
gnutls_assert();
return ret;
}
*ncerts = i;
return 1; /* one certificate parsed */
}
/**
* gnutls_certificate_set_x509_trust_mem - Used to add trusted CAs in a gnutls_certificate_credentials structure
* @res: is an &gnutls_certificate_credentials structure.
* @CA: is a list of trusted CAs or a DER certificate
* @type: is DER or PEM
*
* This function adds the trusted CAs in order to verify client
* certificates. This function may be called multiple times.
*
**/
int gnutls_certificate_set_x509_trust_mem(gnutls_certificate_credentials res,
const gnutls_datum *CA, gnutls_x509_crt_format type)
{
int ret, ret2;
if (type==GNUTLS_X509_FMT_DER)
return parse_der_ca_mem( &res->x509_ca_list, &res->x509_ncas,
CA->data, CA->size);
else
return parse_pem_ca_mem( &res->x509_ca_list, &res->x509_ncas,
CA->data, CA->size);
if ((ret2 = generate_rdn_seq(res)) < 0)
return ret2;
return ret;
}
/**
* gnutls_certificate_set_x509_trust_file - Used to add trusted CAs in a gnutls_certificate_credentials structure
* @res: is an &gnutls_certificate_credentials structure.
* @CAFILE: is a file containing the list of trusted CAs (DER or PEM list)
* @type: is PEM or DER
*
* This function sets the trusted CAs in order to verify client
* certificates. This function may be called multiple times, and the
* given certificates will be appended to the trusted certificate list.
* Returns the number of certificate processed.
*
**/
int gnutls_certificate_set_x509_trust_file(gnutls_certificate_credentials res,
const char *CAFILE, gnutls_x509_crt_format type)
{
int ret, ret2;
int siz;
char x[MAX_FILE_SIZE];
FILE *fd1;
/* FIXME: does not work on long files
*/
fd1 = fopen(CAFILE, "rb");
if (fd1 == NULL) {
gnutls_assert();
return GNUTLS_E_FILE_ERROR;
}
siz = fread(x, 1, sizeof(x)-1, fd1);
fclose(fd1);
x[siz] = 0;
if (type==GNUTLS_X509_FMT_DER)
ret = parse_der_ca_mem( &res->x509_ca_list, &res->x509_ncas,
x, siz);
else
ret = parse_pem_ca_mem( &res->x509_ca_list, &res->x509_ncas,
x, siz);
if (ret < 0) {
gnutls_assert();
return ret;
}
if ((ret2 = generate_rdn_seq(res)) < 0)
return ret2;
return ret;
}
static int parse_pem_crl_mem( gnutls_x509_crl** crl_list, int* ncrls,
const char *input_crl, int input_crl_size)
{
int siz, i;
const char *ptr;
gnutls_datum tmp;
int ret, count;
/* move to the certificate
*/
ptr = strstr( input_crl, PEM_CRL_SEP);
if (ptr == NULL) {
gnutls_assert();
return GNUTLS_E_BASE64_DECODING_ERROR;
}
siz = strlen( ptr);
i = *ncrls + 1;
count = 0;
do {
*crl_list =
(gnutls_x509_crl *) gnutls_realloc_fast( *crl_list,
i *
sizeof(gnutls_x509_crl));
if ( *crl_list == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
ret = gnutls_x509_crl_init( &crl_list[0][i - 1]);
if ( ret < 0) {
gnutls_assert();
return ret;
}
tmp.data = (char*)ptr;
tmp.size = siz;
ret =
gnutls_x509_crl_import(
crl_list[0][i - 1],
&tmp, GNUTLS_X509_FMT_PEM);
if (ret < 0) {
gnutls_assert();
return ret;
}
/* now we move ptr after the pem header
*/
ptr++;
/* find the next certificate (if any)
*/
ptr = strstr(ptr, PEM_CRL_SEP);
i++;
count++;
} while ( ptr != NULL);
*ncrls = i - 1;
return count;
}
/* Reads a DER encoded certificate list from memory and stores it to
* a gnutls_cert structure. This is only called if PKCS7 read fails.
* returns the number of certificates parsed (1)
*/
static int parse_der_crl_mem( gnutls_x509_crl** crl_list, int* ncrls,
const char *input_crl, int input_crl_size)
{
int i;
gnutls_datum tmp;
int ret;
i = *ncrls + 1;
*crl_list =
(gnutls_x509_crl *) gnutls_realloc_fast( *crl_list,
i *
sizeof(gnutls_x509_crl));
if ( *crl_list == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
tmp.data = (opaque*)input_crl;
tmp.size = input_crl_size;
ret = gnutls_x509_crl_init( &crl_list[0][i - 1]);
if ( ret < 0) {
gnutls_assert();
return ret;
}
ret =
gnutls_x509_crl_import(
crl_list[0][i - 1],
&tmp, GNUTLS_X509_FMT_DER);
if (ret < 0) {
gnutls_assert();
return ret;
}
*ncrls = i;
return 1; /* one certificate parsed */
}
/* Reads a DER or PEM CRL from memory
*/
static
int read_crl_mem(gnutls_certificate_credentials res, const char *crl, int crl_size,
gnutls_x509_crt_format type)
{
int ret;
/* allocate space for the certificate to add
*/
res->x509_crl_list = gnutls_realloc_fast( res->x509_crl_list,
(1+ res->x509_ncrls)*sizeof(gnutls_x509_crl));
if ( res->x509_crl_list==NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
if (type==GNUTLS_X509_FMT_DER)
ret = parse_der_crl_mem( &res->x509_crl_list,
&res->x509_ncrls, crl, crl_size);
else
ret = parse_pem_crl_mem( &res->x509_crl_list,
&res->x509_ncrls, crl, crl_size);
if (ret < 0) {
gnutls_assert();
return ret;
}
return ret;
}
/**
* gnutls_certificate_set_x509_crl_mem - Used to add CRLs in a gnutls_certificate_credentials structure
* @res: is an &gnutls_certificate_credentials structure.
* @CRL: is a list of trusted CRLs. They should have been verified before.
* @type: is DER or PEM
*
* This function adds the trusted CRLs in order to verify client or server
* certificates. This function may be called multiple times.
*
**/
int gnutls_certificate_set_x509_crl_mem(gnutls_certificate_credentials res,
const gnutls_datum *CRL, gnutls_x509_crt_format type)
{
int ret;
if ((ret = read_crl_mem(res, CRL->data, CRL->size, type)) < 0)
return ret;
return ret;
}
/**
* gnutls_certificate_set_x509_crl_file - Used to add CRLs in a gnutls_certificate_credentials structure
* @res: is an &gnutls_certificate_credentials structure.
* @crlfile: is a file containing the list of verified CRLs (DER or PEM list)
* @type: is PEM or DER
*
* This function sets the trusted CRLs in order to verify client or server
* certificates. This function may be called multiple times, and the
* given CRLs will be appended to the crl list.
* Returns the number of certificate processed.
*
**/
int gnutls_certificate_set_x509_crl_file(gnutls_certificate_credentials res,
const char *crlfile, gnutls_x509_crt_format type)
{
int ret;
int siz;
char x[MAX_FILE_SIZE];
FILE *fd1;
/* FIXME: does not work on long files
*/
fd1 = fopen(crlfile, "rb");
if (fd1 == NULL) {
gnutls_assert();
return GNUTLS_E_FILE_ERROR;
}
siz = fread(x, 1, sizeof(x)-1, fd1);
fclose(fd1);
x[siz] = 0;
if (type==GNUTLS_X509_FMT_DER)
ret = parse_der_crl_mem( &res->x509_crl_list, &res->x509_ncrls,
x, siz);
else
ret = parse_pem_crl_mem( &res->x509_crl_list, &res->x509_ncrls,
x, siz);
if (ret < 0) {
gnutls_assert();
return ret;
}
return ret;
}
|