-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgrst_canl_x509.c
2891 lines (2368 loc) · 90.4 KB
/
grst_canl_x509.c
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
/*
Copyright (c) 2002-10, Andrew McNab, University of Manchester
All rights reserved.
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
o Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
o Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------
For more information about GridSite: http://www.gridsite.org/
---------------------------------------------------------------
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <stdarg.h>
#include <dirent.h>
#include <string.h>
#include <pwd.h>
#include <errno.h>
#include <getopt.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef GRST_NO_OPENSSL
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/des.h>
#include <openssl/rand.h>
#include <openssl/ssl.h>
#include <pthread.h>
#endif
#include <canl.h>
#include <canl_cred.h>
#include "gridsite.h"
#define GRST_KEYSIZE 1024
#define GRST_PROXYCACHE "/../proxycache/"
#define GRST_BACKDATE_SECONDS 300
#define END_ENTITY_ROBOT_OID "1.2.840.113612.5.2.3.3.1"
static int GRSTx509CreateProxyRequest_int(char **reqtxt, char **keytxt,
char *ocspurl, int keysize);
static int GRSTx509MakeProxyRequest_int(char **reqtxt, char *proxydir,
char *delegation_id, char *user_dn, int keysize);
static int GRSTx509ProxyKeyMatch(char **pkfile, char *pkdir, STACK_OF(X509) *certstack);
static char *
asn1_string2string(ASN1_STRING *str)
{
BIO *bio;
int len, ret;
char *result = NULL;
bio = BIO_new(BIO_s_mem());
if (bio == NULL)
return NULL;
ASN1_STRING_print_ex(bio, str, ASN1_STRFLGS_RFC2253);
len = BIO_pending(bio);
if (len <= 0)
goto end;
result = calloc(1, len + 1);
if (result == NULL)
goto end;
ret = BIO_read(bio, result, len);
if (ret != len) {
free(result);
result = NULL;
goto end;
}
end:
if (bio)
BIO_free(bio);
return result;
}
static int
is_robot_subject(const char *p)
{
if (strlen(p) <= 6)
return 0;
if (strncmp(p, "Robot", 5) != 0)
return 0;
if (('0' <= p[5] && p[5] <= '9') ||
('A' <= p[5] && p[5] <= 'Z') ||
('a' <= p[5] && p[5] <= 'z'))
return 0;
return 1;
}
static int
is_robot_certificate(X509 *cert)
{
int i, ret, found;
char *p;
char buf[64];
X509_NAME_ENTRY *ne;
X509_NAME *subject;
ASN1_STRING *value;
CERTIFICATEPOLICIES *policies = NULL;
POLICYINFO *policy;
subject = X509_get_subject_name(cert);
if (subject == NULL)
return 0;
found = 0;
i = -1;
while ((i = X509_NAME_get_index_by_NID(subject, NID_commonName, i)) >= 0) {
ne = X509_NAME_get_entry(subject, i);
value = X509_NAME_ENTRY_get_data(ne);
p = asn1_string2string(value);
if (p == NULL)
continue;
ret = is_robot_subject(p);
free(p);
if (ret == 1) {
found = 1;
break;
}
}
if (!found)
return 0;
policies = X509_get_ext_d2i(cert, NID_certificate_policies, &i, NULL);
if (policies == NULL)
return 0;
found = 0;
for (i = 0; i < sk_POLICYINFO_num(policies); i++) {
policy = sk_POLICYINFO_value(policies, i);
memset(buf, 0, sizeof(buf));
OBJ_obj2txt(buf, sizeof(buf), policy->policyid, 1);
if (strcmp(buf, END_ENTITY_ROBOT_OID) == 0) {
found = 1;
break;
}
}
if (!found)
return 0;
return 1;
}
static GRSTx509Cert *
add_grst_cred(GRSTx509Cert *last_cred)
{
GRSTx509Cert *cert;
cert = calloc(1, sizeof(*cert));
return cert;
}
int
GRSTasn1FindField(const char *oid, char *coords,
char *asn1string,
struct GRSTasn1TagList taglist[], int lasttag,
int *result);
static void
ssl_init_crypto(void)
{
OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
}
/* Safely initialize OpenSSL digests */
static void GRSTx509SafeOpenSSLInitialization(void)
{
static pthread_once_t digests_once = PTHREAD_ONCE_INIT;
(void) pthread_once(&digests_once, ssl_init_crypto);
}
/// Compare X509 Distinguished Name strings
int GRSTx509NameCmp(char *a, char *b)
///
/// This function attempts to do with string representations what
/// would ideally be done with OIDs/values. In particular, we equate
/// "/Email=" == "/emailAddress=" to deal with this important change
/// between OpenSSL 0.9.6 and 0.9.7.
/// Other than that, it is currently the same as ordinary strcasecmp(3)
/// (for consistency with EDG/LCG/EGEE gridmapdir case insensitivity.)
{
int ret;
char *aa, *bb, *p;
if ((a == NULL) || (b == NULL)) return 1; /* NULL never matches */
aa = strdup(a);
while ((p = strstr(aa, "/emailAddress=")) != NULL)
{
memmove(&p[6], &p[13], strlen(&p[13]) + 1);
p[1] = 'E';
}
bb = strdup(b);
while ((p = strstr(bb, "/emailAddress=")) != NULL)
{
memmove(&p[6], &p[13], strlen(&p[13]) + 1);
p[1] = 'E';
}
ret = strcasecmp(aa, bb);
free(aa);
free(bb);
return ret;
}
/// Check critical extensions
/*TODO MBD*/
int GRSTx509KnownCriticalExts(X509 *cert)
///
/// Returning GRST_RET_OK if all of extensions are known to us or
/// OpenSSL; GRST_REF_FAILED otherwise.
///
/// Since this function relies on functionality (X509_supported_extension)
/// introduced in 0.9.7, then we do nothing and report an error
/// (GRST_RET_FAILED) if one of the associated defines
/// (X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION) is absent.
{
int i;
char s[80];
X509_EXTENSION *ex;
#ifdef X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION
for (i = 0; i < X509_get_ext_count(cert); ++i)
{
ex = X509_get_ext(cert, i);
if (X509_EXTENSION_get_critical(ex) &&
!X509_supported_extension(ex))
{
OBJ_obj2txt(s, sizeof(s), X509_EXTENSION_get_object(ex), 1);
if ((strcmp(s, GRST_PROXYCERTINFO_OID) != 0) &&
(strcmp(s, GRST_PROXYCERTINFO_OLD_OID) != 0))
return GRST_RET_FAILED;
}
}
return GRST_RET_OK;
#else
return GRST_RET_FAILED;
#endif
}
/// Check if certificate can be used as a CA to sign standard X509 certs
int GRSTx509IsCA(X509 *cert)
///
/// Return GRST_RET_OK if true; GRST_RET_FAILED if not.
{
int purpose_id;
purpose_id = X509_PURPOSE_get_by_sname("sslclient");
/* final argument to X509_check_purpose() is whether to check for CAness */
if (X509_check_purpose(cert, purpose_id + X509_PURPOSE_MIN, 1))
return GRST_RET_OK;
else return GRST_RET_FAILED;
}
int GRSTx509ChainFree(GRSTx509Chain *chain)
{
GRSTx509Cert *grst_cert, *next_grst_cert;
if (chain == NULL) return GRST_RET_OK;
next_grst_cert = chain->firstcert;
while (next_grst_cert != NULL)
{
grst_cert = next_grst_cert;
if (grst_cert->issuer != NULL) free(grst_cert->issuer);
if (grst_cert->dn != NULL) free(grst_cert->dn);
if (grst_cert->value != NULL) free(grst_cert->value);
if (grst_cert->ocsp != NULL) free(grst_cert->ocsp);
next_grst_cert = grst_cert->next;
free(grst_cert);
}
free(chain);
return GRST_RET_OK;
}
/// Check a specific signature against a specific (VOMS) cert
static int GRSTx509VerifySig(time_t *time1_time, time_t *time2_time,
unsigned char *txt, int txt_len,
unsigned char *sig, int sig_len,
X509 *cert, const EVP_MD *md_type)
///
/// Returns GRST_RET_OK if signature is ok, other values if not.
{
int ret;
EVP_PKEY *prvkey;
EVP_MD_CTX *ctx = NULL;
time_t voms_service_time1, voms_service_time2;
prvkey = X509_extract_key(cert);
if (prvkey == NULL) return GRST_RET_FAILED;
ctx = EVP_MD_CTX_new();
if (ctx == NULL)
return GRST_RET_FAILED;
GRSTx509SafeOpenSSLInitialization();
#if OPENSSL_VERSION_NUMBER >= 0x0090701fL
EVP_MD_CTX_init(ctx);
EVP_VerifyInit_ex(ctx, md_type, NULL);
#else
EVP_VerifyInit(ctx, md_type);
#endif
EVP_VerifyUpdate(ctx, txt, txt_len);
ret = EVP_VerifyFinal(ctx, sig, sig_len, prvkey);
#if OPENSSL_VERSION_NUMBER >= 0x0090701fL
EVP_MD_CTX_free(ctx);
#endif
EVP_PKEY_free(prvkey);
if (ret != 1) return GRST_RET_FAILED;
voms_service_time1 =
GRSTasn1TimeToTimeT(ASN1_STRING_data(X509_get_notBefore(cert)),0);
if (voms_service_time1 > *time1_time)
*time1_time = voms_service_time1;
voms_service_time2 =
GRSTasn1TimeToTimeT(ASN1_STRING_data(X509_get_notAfter(cert)),0);
if (voms_service_time2 < *time2_time)
*time2_time = voms_service_time2;
return GRST_RET_OK ; /* verified */
}
/// Check the signature of the VOMS attributes
static int GRSTx509VerifyVomsSig(time_t *time1_time, time_t *time2_time,
unsigned char *asn1string,
struct GRSTasn1TagList taglist[],
int lasttag,
char *vomsdir, int acnumber)
///
/// Returns GRST_RET_OK if signature is ok, other values if not.
{
#define GRST_ASN1_COORDS_VOMS_DN "-1-1-%d-1-3-1-1-1-%%d-1-%%d"
#define GRST_ASN1_COORDS_VOMS_INFO "-1-1-%d-1"
#define GRST_ASN1_COORDS_VOMS_HASH "-1-1-%d-2-1"
#define GRST_ASN1_COORDS_VOMS_SIG "-1-1-%d-3"
int ihash, isig, iinfo;
char *certpath, *certpath2, acvomsdn[200], dn_coords[200],
info_coords[200], sig_coords[200], hash_coords[200];
const unsigned char *p;
DIR *vomsDIR, *vomsDIR2;
struct dirent *vomsdirent, *vomsdirent2;
X509 *cert;
FILE *fp;
const EVP_MD *md_type = NULL;
struct stat statbuf;
time_t voms_service_time1 = GRST_MAX_TIME_T, voms_service_time2 = 0,
tmp_time1, tmp_time2;
ASN1_OBJECT *hash_obj = NULL;
int ret;
if ((vomsdir == NULL) || (vomsdir[0] == '\0')) return GRST_RET_FAILED;
snprintf(dn_coords, sizeof(dn_coords),
GRST_ASN1_COORDS_VOMS_DN, acnumber);
if (GRSTasn1GetX509Name(acvomsdn, sizeof(acvomsdn), dn_coords,
asn1string, taglist, lasttag) != GRST_RET_OK) return GRST_RET_FAILED;
snprintf(info_coords, sizeof(info_coords),
GRST_ASN1_COORDS_VOMS_INFO, acnumber);
iinfo = GRSTasn1SearchTaglist(taglist, lasttag, info_coords);
snprintf(hash_coords, sizeof(hash_coords),
GRST_ASN1_COORDS_VOMS_HASH, acnumber);
ihash = GRSTasn1SearchTaglist(taglist, lasttag, hash_coords);
snprintf(sig_coords, sizeof(sig_coords),
GRST_ASN1_COORDS_VOMS_SIG, acnumber);
isig = GRSTasn1SearchTaglist(taglist, lasttag, sig_coords);
if ((iinfo < 0) || (ihash < 0) || (isig < 0)) return GRST_RET_FAILED;
/* determine hash algorithm's type */
p = &asn1string[taglist[ihash].start];
d2i_ASN1_OBJECT(&hash_obj, &p,
taglist[ihash].length+taglist[ihash].headerlength);
md_type = EVP_get_digestbyname(OBJ_nid2sn(OBJ_obj2nid(hash_obj)));
if (hash_obj)
ASN1_OBJECT_free(hash_obj);
if (md_type == NULL) return GRST_RET_FAILED;
vomsDIR = opendir(vomsdir);
if (vomsDIR == NULL) return GRST_RET_FAILED;
while ((vomsdirent = readdir(vomsDIR)) != NULL)
{
if (vomsdirent->d_name[0] == '.') continue;
ret = asprintf(&certpath, "%s/%s", vomsdir, vomsdirent->d_name);
if (ret == -1)
continue;
stat(certpath, &statbuf);
if (S_ISDIR(statbuf.st_mode))
{
vomsDIR2 = opendir(certpath);
GRSTerrorLog(GRST_LOG_DEBUG,
"Descend VOMS subdirectory %s", certpath);
free(certpath);
if (vomsDIR2 == NULL) continue;
while ((vomsdirent2 = readdir(vomsDIR2)) != NULL)
{
if (vomsdirent2->d_name[0] == '.') continue;
ret = asprintf(&certpath2, "%s/%s/%s",
vomsdir, vomsdirent->d_name, vomsdirent2->d_name);
if (ret == -1)
continue;
fp = fopen(certpath2, "r");
GRSTerrorLog(GRST_LOG_DEBUG,
"Examine VOMS cert %s", certpath2);
free(certpath2);
if (fp == NULL) continue;
cert = PEM_read_X509(fp, NULL, NULL, NULL);
fclose(fp);
if (cert == NULL) continue;
tmp_time1 = 0;
tmp_time2 = GRST_MAX_TIME_T;
if (GRSTx509VerifySig(&tmp_time1, &tmp_time2,
&asn1string[taglist[iinfo].start],
taglist[iinfo].length+taglist[iinfo].headerlength,
&asn1string[taglist[isig].start+
taglist[isig].headerlength+1],
taglist[isig].length - 1,
cert, md_type) == GRST_RET_OK)
{
GRSTerrorLog(GRST_LOG_DEBUG, "Matched VOMS cert file %s", vomsdirent2->d_name);
/* Store more permissive time ranges for now */
if (tmp_time1 < voms_service_time1)
voms_service_time1 = tmp_time1;
if (tmp_time2 > voms_service_time2)
voms_service_time2 = tmp_time2;
}
X509_free(cert);
}
closedir(vomsDIR2);
}
else
{
fp = fopen(certpath, "r");
GRSTerrorLog(GRST_LOG_DEBUG, "Examine VOMS cert %s", certpath);
free(certpath);
if (fp == NULL) continue;
cert = PEM_read_X509(fp, NULL, NULL, NULL);
fclose(fp);
if (cert == NULL) continue;
tmp_time1 = 0;
tmp_time2 = GRST_MAX_TIME_T;
if (GRSTx509VerifySig(&tmp_time1, &tmp_time2,
&asn1string[taglist[iinfo].start],
taglist[iinfo].length+taglist[iinfo].headerlength,
&asn1string[taglist[isig].start+
taglist[isig].headerlength+1],
taglist[isig].length - 1,
cert, md_type) == GRST_RET_OK)
{
GRSTerrorLog(GRST_LOG_DEBUG, "Matched VOMS cert file %s", vomsdirent->d_name);
/* Store more permissive time ranges for now */
if (tmp_time1 < voms_service_time1)
voms_service_time1 = tmp_time1;
if (tmp_time2 > voms_service_time2)
voms_service_time2 = tmp_time2;
}
X509_free(cert);
}
}
closedir(vomsDIR);
if ((voms_service_time1 == GRST_MAX_TIME_T) || (voms_service_time2 == 0))
return GRST_RET_FAILED;
/* now we tighten up the VOMS AC time range using the most permissive
pair of VOMS certificate ranges (in case of multiple, possibly
overlapping, matching certificates in the VOMS certs store) */
if (voms_service_time1 > *time1_time) *time1_time = voms_service_time1;
if (voms_service_time2 < *time2_time) *time2_time = voms_service_time2;
return GRST_RET_OK;
}
/// Check the signature of the VOMS attributes using the LSC file cert
static int GRSTx509VerifyVomsSigCert(time_t *time1_time, time_t *time2_time,
unsigned char *asn1string,
struct GRSTasn1TagList taglist[],
int lasttag,
char *vomsdir, int acnumber,
int ivomscert,
char *capath,
char *acvomsdn,
char *voname)
///
/// Returns GRST_RET_OK if signature is ok, other values if not.
{
#define GRST_ASN1_COORDS_VOMS_DN "-1-1-%d-1-3-1-1-1-%%d-1-%%d"
#define GRST_ASN1_COORDS_VOMS_INFO "-1-1-%d-1"
#define GRST_ASN1_COORDS_VOMS_SIG "-1-1-%d-3"
int ret, isig, iinfo, chain_errors = GRST_RET_OK, ok = 0,
cadn_len, vomsdn_len, lsc_found = 0, ihash;
char *lscpath, *p, *cacertpath,
*vodir, *vomscert_cadn, *vomscert_vomsdn,
*lsc_cadn, *lsc_vomsdn;
const unsigned char *q;
unsigned long issuerhash = 0;
DIR *voDIR;
struct dirent *vodirent;
X509 *cacert = NULL, *vomscert = NULL;
FILE *fp;
struct stat statbuf;
time_t tmp_time;
ASN1_OBJECT *hash_obj = NULL;
char coords[200];
const EVP_MD *md_type = NULL;
time_t voms_service_time1 = 0, voms_service_time2 = GRST_MAX_TIME_T;
if ((vomsdir == NULL) || (vomsdir[0] == '\0')) return GRST_RET_FAILED;
q = &asn1string[taglist[ivomscert].start + 12];
vomscert = d2i_X509(NULL, (const unsigned char **) &q,
taglist[ivomscert].length - 8);
if (vomscert == NULL)
{
GRSTerrorLog(GRST_LOG_DEBUG, "Failed to read included VOMS cert in GRSTx509VerifyVomsSigCert()");
return GRST_RET_FAILED;
}
GRSTerrorLog(GRST_LOG_DEBUG, "Found included VOMS cert in GRSTx509VerifyVomsSigCert()");
snprintf(coords, sizeof(coords), GRST_ASN1_COORDS_VOMS_INFO, acnumber);
iinfo = GRSTasn1SearchTaglist(taglist, lasttag, coords);
snprintf(coords, sizeof(coords), GRST_ASN1_COORDS_VOMS_HASH, acnumber);
ihash = GRSTasn1SearchTaglist(taglist, lasttag, coords);
snprintf(coords, sizeof(coords), GRST_ASN1_COORDS_VOMS_SIG, acnumber);
isig = GRSTasn1SearchTaglist(taglist, lasttag, coords);
if ((iinfo < 0) || (ihash < 0) || (isig < 0)) goto end;
q = &asn1string[taglist[ihash].start];
d2i_ASN1_OBJECT(&hash_obj, &q,
taglist[ihash].length+taglist[ihash].headerlength);
md_type = EVP_get_digestbyname(OBJ_nid2sn(OBJ_obj2nid(hash_obj)));
if (hash_obj)
ASN1_OBJECT_free(hash_obj);
if (md_type == NULL) goto end;
/* check issuer CA certificate */
issuerhash = X509_NAME_hash(X509_get_issuer_name(vomscert));
ret = asprintf(&cacertpath, "%s/%.8lx.0", capath, issuerhash);
if (ret == -1)
goto end;
/* check voms cert DN matches DN from AC */
vomscert_vomsdn = X509_NAME_oneline(X509_get_subject_name(vomscert),NULL,0);
if (GRSTx509NameCmp(vomscert_vomsdn, acvomsdn) != 0)
{
free(vomscert_vomsdn);
GRSTerrorLog(GRST_LOG_DEBUG, "Included VOMS cert DN does not match AC issuer DN!");
goto end;
}
free(vomscert_vomsdn);
GRSTerrorLog(GRST_LOG_DEBUG, "Look for CA root file %s", cacertpath);
fp = fopen(cacertpath, "r");
free(cacertpath);
if (fp == NULL) goto end;
else
{
cacert = PEM_read_X509(fp, NULL, NULL, NULL);
fclose(fp);
if (cacert != NULL)
{
GRSTerrorLog(GRST_LOG_DEBUG, " Loaded CA root cert from file");
}
else
{
GRSTerrorLog(GRST_LOG_DEBUG, " Failed to load CA root cert file");
goto end;
}
}
/* check times CA cert times, and reject if necessary */
tmp_time = GRSTasn1TimeToTimeT(
ASN1_STRING_data(X509_get_notBefore(cacert)), 0);
if (tmp_time > *time1_time) chain_errors |= GRST_CERT_BAD_TIME;
tmp_time = GRSTasn1TimeToTimeT(
ASN1_STRING_data(X509_get_notAfter(cacert)), 0);
if (tmp_time < *time2_time) chain_errors |= GRST_CERT_BAD_TIME;
/* check times VOMS cert times, and tighten if necessary */
tmp_time = GRSTasn1TimeToTimeT(
ASN1_STRING_data(X509_get_notBefore(vomscert)), 0);
if (tmp_time > *time1_time) chain_errors |= GRST_CERT_BAD_TIME;
tmp_time = GRSTasn1TimeToTimeT(
ASN1_STRING_data(X509_get_notAfter(vomscert)), 0);
if (tmp_time < *time2_time) chain_errors |= GRST_CERT_BAD_TIME;
ret = X509_check_issued(cacert, vomscert);
GRSTerrorLog(GRST_LOG_DEBUG, "X509_check_issued returns %d", ret);
vomscert_cadn = X509_NAME_oneline(X509_get_issuer_name(vomscert),NULL,0);
if (ret != X509_V_OK) {
chain_errors |= GRST_CERT_BAD_SIG;
goto end;
}
ret = asprintf(&vodir, "%s/%s", vomsdir, voname);
if (ret == -1)
goto end;
voDIR = opendir(vodir);
if (voDIR == NULL) goto end;
cadn_len = strlen(vomscert_cadn);
vomsdn_len = strlen(acvomsdn);
lsc_cadn = malloc(cadn_len + 2);
lsc_vomsdn = malloc(vomsdn_len + 2);
while (((vodirent = readdir(voDIR)) != NULL) && !lsc_found)
{
if (vodirent->d_name[0] == '.') continue;
if (strlen(vodirent->d_name) < 5) continue;
if (strcmp(&(vodirent->d_name[strlen(vodirent->d_name)-4]), ".lsc") != 0) continue;
ret = asprintf(&lscpath, "%s/%s", vodir, vodirent->d_name);
if (ret == -1)
goto end;
stat(lscpath, &statbuf);
GRSTerrorLog(GRST_LOG_DEBUG, "Check LSC file %s for %s,%s",
lscpath, acvomsdn, vomscert_cadn);
if ((fp = fopen(lscpath, "r")) != NULL)
{
lsc_cadn[0] = '\0';
lsc_vomsdn[0] = '\0';
if ((fgets(lsc_vomsdn, vomsdn_len + 2, fp) != NULL)
&& (fgets(lsc_cadn, cadn_len + 2, fp) != NULL))
{
if ((p = index(lsc_cadn, '\n')) != NULL) *p = '\0';
if ((p = index(lsc_vomsdn, '\n')) != NULL) *p = '\0';
if ((GRSTx509NameCmp(lsc_cadn, vomscert_cadn) == 0) &&
(GRSTx509NameCmp(lsc_vomsdn, acvomsdn) == 0))
{
GRSTerrorLog(GRST_LOG_DEBUG, "Matched LSC file %s", lscpath);
lsc_found = 1;
}
}
fclose(fp);
}
free(lscpath);
}
closedir(voDIR);
free(vodir);
free(vomscert_cadn);
free(lsc_cadn);
free(lsc_vomsdn);
if (!lsc_found) {
chain_errors |= GRST_CERT_BAD_SIG;
goto end;
}
ret = GRSTx509VerifySig(&voms_service_time1, &voms_service_time2,
&asn1string[taglist[iinfo].start],
taglist[iinfo].length+taglist[iinfo].headerlength,
&asn1string[taglist[isig].start+
taglist[isig].headerlength+1],
taglist[isig].length - 1,
vomscert, md_type);
if (ret != GRST_RET_OK) {
chain_errors |= GRST_CERT_BAD_SIG;
goto end;
}
if (voms_service_time1 > *time1_time) *time1_time = voms_service_time1;
if (voms_service_time2 < *time2_time) *time2_time = voms_service_time2;
ok = 1;
end:
if (cacert)
X509_free(cacert);
if (vomscert)
X509_free(vomscert);
return (!ok || chain_errors ? GRST_RET_FAILED : GRST_RET_OK);
}
/// Get the VOMS attributes in the given extension
static int GRSTx509ChainVomsAdd(GRSTx509Cert **grst_cert,
time_t time1_time, time_t time2_time,
int delegation,
X509_EXTENSION *ex,
GRSTx509Cert *user_cert, char *vomsdir, char *capath)
///
/// Add any VOMS credentials found into the chain. Always returns GRST_RET_OK
/// - even for invalid credentials, which are flagged in errors field
{
#define MAXTAG 500
#define GRST_ASN1_COORDS_FQAN "-1-1-%d-1-7-1-2-1-2-%d"
#define GRST_ASN1_COORDS_ISSUER_DN "-1-1-%d-1-2-1-1-1-1-%%d-1-%%d"
#define GRST_ASN1_COORDS_ISSUER_SERIAL "-1-1-%d-1-2-1-2"
#define GRST_ASN1_COORDS_VOMS_DN "-1-1-%d-1-3-1-1-1-%%d-1-%%d"
#define GRST_ASN1_COORDS_TIME1 "-1-1-%d-1-6-1"
#define GRST_ASN1_COORDS_TIME2 "-1-1-%d-1-6-2"
#define GRST_ASN1_COORDS_VOMSCERT "-1-1-%d-1-8-%%d-%%d"
ASN1_OCTET_STRING *asn1data;
char *asn1string, acissuerdn[200], acvomsdn[200],
dn_coords[200], fqan_coords[200], time1_coords[200],
time2_coords[200], vomscert_coords[200], *voname = NULL,
serial_coords[200];
long asn1length;
int lasttag=-1, itag, i, j, acnumber = 1, chain_errors = 0,
ivomscert, tmp_chain_errors, ret;
char *acissuerserial = NULL;
struct GRSTasn1TagList taglist[MAXTAG+1];
time_t actime1 = 0, actime2 = 0, time_now,
tmp_time1, tmp_time2;
ASN1_INTEGER acissuerserialASN1;
asn1data = X509_EXTENSION_get_data(ex);
asn1string = ASN1_STRING_data(asn1data);
asn1length = ASN1_STRING_length(asn1data);
GRSTasn1ParseDump(NULL, asn1string, asn1length, taglist, MAXTAG, &lasttag);
for (acnumber = 1; ; ++acnumber) /* go through ACs one by one */
{
chain_errors = 0;
snprintf(dn_coords, sizeof(dn_coords), GRST_ASN1_COORDS_ISSUER_DN, acnumber);
if (GRSTasn1GetX509Name(acissuerdn, sizeof(acissuerdn), dn_coords,
asn1string, taglist, lasttag) != GRST_RET_OK)
break;
snprintf(dn_coords, sizeof(dn_coords), GRST_ASN1_COORDS_VOMS_DN, acnumber);
if (GRSTasn1GetX509Name(acvomsdn, sizeof(acvomsdn), dn_coords,
asn1string, taglist, lasttag) != GRST_RET_OK)
break;
if ((GRSTx509NameCmp(user_cert->dn, acissuerdn) != 0) && /* old */
(GRSTx509NameCmp(user_cert->issuer, acissuerdn) != 0)) /* new */
chain_errors |= GRST_CERT_BAD_CHAIN;
/* check serial numbers */
snprintf(serial_coords, sizeof(serial_coords),
GRST_ASN1_COORDS_ISSUER_SERIAL, acnumber);
itag = GRSTasn1SearchTaglist(taglist, lasttag, serial_coords);
if (itag > -1)
{
acissuerserialASN1.length = taglist[itag].length;
acissuerserialASN1.type = V_ASN1_INTEGER;
acissuerserialASN1.data = &asn1string[taglist[itag].start+taglist[itag].headerlength];
acissuerserial = i2s_ASN1_INTEGER(NULL, &acissuerserialASN1);
/*
p = &asn1string[taglist[itag].start+taglist[itag].headerlength];
if (taglist[itag].length == 2)
acissuerserial = p[1] + p[0] * 0x100;
else if (taglist[itag].length == 3)
acissuerserial = p[2] + p[1] * 0x100 + p[0] * 0x10000;
else if (taglist[itag].length == 4)
acissuerserial = p[3] + p[2] * 0x100 + p[1] * 0x10000 +
p[0] * 0x1000000;
*/
}
if (strcmp(acissuerserial, user_cert->serial) != 0)
chain_errors |= GRST_CERT_BAD_CHAIN;
if (acissuerserial)
free(acissuerserial);
/* get times */
snprintf(time1_coords, sizeof(time1_coords), GRST_ASN1_COORDS_TIME1, acnumber);
itag = GRSTasn1SearchTaglist(taglist, lasttag, time1_coords);
if (itag > -1) actime1 = GRSTasn1TimeToTimeT(
&asn1string[taglist[itag].start+
taglist[itag].headerlength],
taglist[itag].length);
else actime1 = 0;
snprintf(time2_coords, sizeof(time2_coords), GRST_ASN1_COORDS_TIME2, acnumber);
itag = GRSTasn1SearchTaglist(taglist, lasttag, time2_coords);
if (itag > -1) actime2 = GRSTasn1TimeToTimeT(
&asn1string[taglist[itag].start+
taglist[itag].headerlength],
taglist[itag].length);
else actime2 = 0;
if (actime1 > time1_time) time1_time = actime1;
if (actime2 < time2_time) time2_time = actime2;
time(&time_now);
if ((time1_time > time_now + 300) || (time2_time < time_now))
chain_errors |= GRST_CERT_BAD_TIME;
/* get first FQAN and use to get VO name */
snprintf(fqan_coords, sizeof(fqan_coords), GRST_ASN1_COORDS_FQAN, acnumber, 1);
itag = GRSTasn1SearchTaglist(taglist, lasttag, fqan_coords);
if ((itag > -1) &&
(asn1string[taglist[itag].start+taglist[itag].headerlength] == '/'))
{
for (j=1;
(asn1string[taglist[itag].start+taglist[itag].headerlength+j] != '/') &&
(j < taglist[itag].length);
++j) ;
ret = asprintf(&voname, "%.*s", j-1,
&(asn1string[taglist[itag].start+taglist[itag].headerlength+1]));
if (ret == -1)
return GRST_RET_FAILED;
}
snprintf(vomscert_coords, sizeof(vomscert_coords),
GRST_ASN1_COORDS_VOMSCERT, acnumber);
ret = GRSTasn1FindField(GRST_VOMS_PK_CERT_LIST_OID, vomscert_coords, asn1string,
taglist, lasttag, &ivomscert);
/* try using internal VOMS issuer cert */
tmp_chain_errors = GRST_CERT_BAD_SIG;
tmp_time1 = time1_time;
tmp_time2 = time2_time;
if ((ivomscert > -1) &&
(voname != NULL) &&
(GRSTx509VerifyVomsSigCert(&tmp_time1, &tmp_time2,
asn1string, taglist, lasttag, vomsdir, acnumber,
ivomscert, capath, acvomsdn,
voname) == GRST_RET_OK))
{
tmp_chain_errors = 0;
time1_time = tmp_time1;
time2_time = tmp_time2;
}
if (voname != NULL)
{
free(voname);
voname = NULL;
}
if ((tmp_chain_errors != 0) &&
(GRSTx509VerifyVomsSig(&time1_time, &time2_time,
asn1string, taglist, lasttag, vomsdir, acnumber)
!= GRST_RET_OK))
chain_errors |= GRST_CERT_BAD_SIG;
for (i=1; ; ++i) /* now go through FQANs */
{
snprintf(fqan_coords, sizeof(fqan_coords), GRST_ASN1_COORDS_FQAN, acnumber, i);
itag = GRSTasn1SearchTaglist(taglist, lasttag, fqan_coords);
if (itag > -1)
{
GRSTx509Cert *cred;
cred = add_grst_cred(*grst_cert);
if (cred == NULL)
return GRST_RET_FAILED;
cred->notbefore = time1_time;
cred->notafter = time2_time;
ret = asprintf(&cred->value, "%.*s",
taglist[itag].length,
&asn1string[taglist[itag].start+
taglist[itag].headerlength]);
if (ret == -1) {
free(cred);
return GRST_RET_FAILED;
}