/
XrdCryptosslgsiAux.cc
1402 lines (1301 loc) · 47.5 KB
/
XrdCryptosslgsiAux.cc
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
/******************************************************************************/
/* */
/* X r d C r y p t o s s l g s i A u x . h h */
/* */
/* (c) 2005, G. Ganis / CERN */
/* */
/* This file is part of the XRootD software suite. */
/* */
/* XRootD 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 3 of the License, or (at your */
/* option) any later version. */
/* */
/* XRootD 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 XRootD in a file called COPYING.LESSER (LGPL license) and file */
/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
/* */
/* The copyright holder's institutional names and contributor's names may not */
/* be used to endorse or promote products derived from this software without */
/* specific prior written permission of the institution or contributor. */
/* */
/******************************************************************************/
/* ************************************************************************** */
/* */
/* GSI utility functions */
/* */
/* ************************************************************************** */
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <openssl/asn1.h>
#include <openssl/asn1t.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/x509v3.h>
#include "XrdSut/XrdSutRndm.hh"
#include "XrdCrypto/XrdCryptogsiX509Chain.hh"
#include "XrdCrypto/XrdCryptosslAux.hh"
#include "XrdCrypto/XrdCryptosslRSA.hh"
#include "XrdCrypto/XrdCryptosslTrace.hh"
#include "XrdCrypto/XrdCryptosslX509.hh"
#include "XrdCrypto/XrdCryptosslX509Req.hh"
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
// //
// Extensions OID relevant for proxies //
// //
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
// X509v3 Key Usage: critical
#define KEY_USAGE_OID "2.5.29.15"
// X509v3 Subject Alternative Name: must be absent
#define SUBJ_ALT_NAME_OID "2.5.29.17"
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
// //
// VOMS relevant stuff //
// //
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
#define XRDGSI_VOMS_ATCAP_OID "1.3.6.1.4.1.8005.100.100.4"
#define XRDGSI_VOMS_ACSEQ_OID "1.3.6.1.4.1.8005.100.100.5"
#define BIO_PRINT(b,c) \
BUF_MEM *bptr; \
BIO_get_mem_ptr(b, &bptr); \
if (bptr) { \
char *s = new char[bptr->length+1]; \
memcpy(s, bptr->data, bptr->length); \
s[bptr->length] = '\0'; \
PRINT(c << s); \
delete [] s; \
} else { \
PRINT("ERROR: "<<c<<" BIO internal buffer undefined!"); \
} \
if (b) BIO_free(b);
#define BIO_GET_STRING(b,str) \
BUF_MEM *bptr; \
BIO_get_mem_ptr(b, &bptr); \
if (bptr) { \
char *s = new char[bptr->length+1]; \
memcpy(s, bptr->data, bptr->length); \
s[bptr->length] = '\0'; \
str = s; \
delete [] s; \
} else { \
PRINT("ERROR: GET_STRING: BIO internal buffer undefined!"); \
} \
if (b) BIO_free(b);
#if OPENSSL_VERSION_NUMBER >= 0x0090800f
# define XRDGSI_CONST const
#else
# define XRDGSI_CONST
#endif
#if OPENSSL_VERSION_NUMBER < 0x10100000L
static RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
{
if (pkey->type != EVP_PKEY_RSA) {
return NULL;
}
return pkey->pkey.rsa;
}
#endif
int XrdCryptosslX509Asn1PrintInfo(int tag, int xclass, int constructed, int indent);
int XrdCryptosslX509FillUnknownExt(XRDGSI_CONST unsigned char **pp, long length);
int XrdCryptosslX509FillVOMS(XRDGSI_CONST unsigned char **pp,
long length, bool &getvat, XrdOucString &vat);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
// //
// Handlers of the ProxyCertInfo extension following RFC3820 //
// //
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
ASN1_SEQUENCE(PROXY_CERT_INFO_EXTENSION_OLD) =
{
ASN1_SIMPLE(PROXY_CERT_INFO_EXTENSION, proxyPolicy, PROXY_POLICY),
ASN1_EXP_OPT(PROXY_CERT_INFO_EXTENSION, pcPathLengthConstraint, ASN1_INTEGER, 1)
} ASN1_SEQUENCE_END_name(PROXY_CERT_INFO_EXTENSION, PROXY_CERT_INFO_EXTENSION_OLD)
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(PROXY_CERT_INFO_EXTENSION, PROXY_CERT_INFO_EXTENSION_OLD, PROXY_CERT_INFO_EXTENSION_OLD)
//___________________________________________________________________________
bool XrdCryptosslProxyCertInfo(const void *extdata, int &pathlen, bool *haspolicy)
{
//
// Check presence of a proxyCertInfo and retrieve the path length constraint.
// Written following RFC3820, examples in openssl-<vers>/crypto source code.
// in gridsite code and Globus proxycertinfo.h / .c.
// if 'haspolicy' is defined, the existence of a policy field is checked;
// the content ignored for the time being.
// Make sure we got an extension
if (!extdata) {
return 0;
}
// Structure the buffer
X509_EXTENSION *ext = (X509_EXTENSION *)extdata;
// Check ProxyCertInfo OID
char s[80] = {0};
OBJ_obj2txt(s, sizeof(s), X509_EXTENSION_get_object(ext), 1);
// Now extract the path length constraint, if any
unsigned char *p = X509_EXTENSION_get_data(ext)->data;
PROXY_CERT_INFO_EXTENSION *pci = 0;
if (!strcmp(s, gsiProxyCertInfo_OID))
pci = d2i_PROXY_CERT_INFO_EXTENSION(0, (XRDGSI_CONST unsigned char **)(&p), X509_EXTENSION_get_data(ext)->length);
else if (!strcmp(s, gsiProxyCertInfo_OLD_OID))
pci = d2i_PROXY_CERT_INFO_EXTENSION_OLD(0, (XRDGSI_CONST unsigned char **)(&p), X509_EXTENSION_get_data(ext)->length);
if (!pci) {
return 0;
}
// Default length is -1, i.e. check disabled
pathlen = -1;
if (pci->pcPathLengthConstraint) {
pathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint);
}
// If required, check the existence of a policy field
if (haspolicy) {
*haspolicy = (pci->proxyPolicy) ? 1 : 0;
}
// We are done
return 1;
}
//___________________________________________________________________________
void XrdCryptosslSetPathLenConstraint(void *extdata, int pathlen)
{
//
// Set the patch length constraint valur in proxyCertInfo extension ext
// to 'pathlen'.
// Make sure we got an extension
if (!extdata)
return;
// Structure the buffer
X509_EXTENSION *ext = (X509_EXTENSION *)extdata;
// Check ProxyCertInfo OID
char s[80] = {0};
OBJ_obj2txt(s, sizeof(s), X509_EXTENSION_get_object(ext), 1);
// Now extract the path length constraint, if any
unsigned char *p = X509_EXTENSION_get_data(ext)->data;
PROXY_CERT_INFO_EXTENSION *pci = 0;
if (!strcmp(s, gsiProxyCertInfo_OID))
pci = d2i_PROXY_CERT_INFO_EXTENSION(0, (XRDGSI_CONST unsigned char **)(&p), X509_EXTENSION_get_data(ext)->length);
else if (!strcmp(s, gsiProxyCertInfo_OLD_OID))
pci = d2i_PROXY_CERT_INFO_EXTENSION_OLD(0, (XRDGSI_CONST unsigned char **)(&p), X509_EXTENSION_get_data(ext)->length);
if (!pci)
return;
// Set the new length
if (pci->pcPathLengthConstraint) {
ASN1_INTEGER_set(pci->pcPathLengthConstraint, pathlen);
}
// We are done
return;
}
//____________________________________________________________________________
int XrdCryptosslX509CreateProxy(const char *fnc, const char *fnk,
XrdProxyOpt_t *pxopt,
XrdCryptogsiX509Chain *xp, XrdCryptoRSA **kp,
const char *fnp)
{
// Create a proxy certificate following the GSI specification (RFC 3820)
// for the EEC certificate in file 'fnc', private key in 'fnk'.
// A chain containing the proxy certificate and the EEC is returned in 'xp'
// and its full RSA key in 'kp'.
// The structure pxopt can be used to change the default options about
// number of bits for the key, duration validity and max path signature depth.
// If 'fpn' is defined, a PEM file is created with, in order, the proxy
// certificate, the related private key and the EEC certificate (standard
// GSI format).
// Policy fields in the CertProxyExtension not yet included.
// Return 0 in case of success, < 0 otherwise
EPNAME("X509CreateProxy");
// Make sure the files are specified
if (!fnc || !fnk || !xp || !kp) {
PRINT("invalid inputs ");
return -1;
}
//
// Init OpenSSL
OpenSSL_add_all_ciphers();
OpenSSL_add_all_digests();
ERR_load_crypto_strings();
// Use default options, if not specified
int bits = (pxopt && pxopt->bits >= 512) ? pxopt->bits : 512;
int valid = (pxopt) ? pxopt->valid : 43200; // 12 hours
int depthlen = (pxopt) ? pxopt->depthlen : -1; // unlimited
//
// Get EEC certificate from fnc
X509 *xEEC = 0;
FILE *fc = fopen(fnc, "r");
if (fc) {
// Read out the certificate
if (PEM_read_X509(fc, &xEEC, 0, 0)) {
DEBUG("EEC certificate loaded from file: "<<fnc);
} else {
PRINT("unable to load EEC certificate from file: "<<fnc);
fclose(fc);
return -kErrPX_BadEECfile;
}
} else {
PRINT("EEC certificate cannot be opened (file: "<<fnc<<")");
return -kErrPX_BadEECfile;
}
fclose(fc);
// Make sure the certificate is not expired
int now = (int)time(0);
if (now > XrdCryptosslASN1toUTC(X509_get_notAfter(xEEC))) {
PRINT("EEC certificate has expired");
X509_free(xEEC);
return -kErrPX_ExpiredEEC;
}
//
// Get EEC private key from fnk
EVP_PKEY *ekEEC = 0;
FILE *fk = fopen(fnk, "r");
if (fk) {
// Read out the private key
XrdOucString sbj;
XrdCryptosslNameOneLine(X509_get_subject_name(xEEC), sbj);
PRINT("Your identity: "<<sbj);
if ((PEM_read_PrivateKey(fk, &ekEEC, 0, 0))) {
DEBUG("EEC private key loaded from file: "<<fnk);
} else {
PRINT("unable to load EEC private key from file: "<<fnk);
fclose(fk);
X509_free(xEEC);
return -kErrPX_BadEECfile;
}
} else {
PRINT("EEC private key file cannot be opened (file: "<<fnk<<")");
X509_free(xEEC);
return -kErrPX_BadEECfile;
}
fclose(fk);
// Check key consistency
if ((RSA_check_key(EVP_PKEY_get0_RSA(ekEEC)) == 0)) {
PRINT("inconsistent key loaded");
EVP_PKEY_free(ekEEC);
X509_free(xEEC);
return -kErrPX_BadEECkey;
}
//
// Create a new request
X509_REQ *preq = X509_REQ_new();
if (!preq) {
PRINT("cannot to create cert request");
EVP_PKEY_free(ekEEC);
X509_free(xEEC);
return -kErrPX_NoResources;
}
//
// Create the new PKI for the proxy (exponent 65537)
RSA *kPX = RSA_new();
if (!kPX) {
PRINT("proxy key could not be generated - return");
EVP_PKEY_free(ekEEC);
X509_free(xEEC);
return -kErrPX_GenerateKey;
}
BIGNUM *e = BN_new();
if (!e) {
PRINT("proxy key could not be generated - return");
RSA_free(kPX);
EVP_PKEY_free(ekEEC);
X509_free(xEEC);
return -kErrPX_GenerateKey;
}
BN_set_word(e, 0x10001);
if (RSA_generate_key_ex(kPX, bits, e, NULL) != 1) {
PRINT("proxy key could not be generated - return");
BN_free(e);
RSA_free(kPX);
EVP_PKEY_free(ekEEC);
X509_free(xEEC);
return -kErrPX_GenerateKey;
}
BN_free(e);
//
// Set the key into the request
EVP_PKEY *ekPX = EVP_PKEY_new();
if (!ekPX) {
PRINT("could not create a EVP_PKEY * instance - return");
RSA_free(kPX);
EVP_PKEY_free(ekEEC);
X509_free(xEEC);
return -kErrPX_NoResources;
}
EVP_PKEY_assign_RSA(ekPX, kPX);
X509_REQ_set_pubkey(preq, ekPX);
//
// Generate a serial number. Specification says that this *should*
// unique, so we just draw an unsigned random integer
unsigned int serial = XrdSutRndm::GetUInt();
//
// The subject name is the certificate subject + /CN=<rand_uint>
// with <rand_uint> is a random unsigned int used also as serial
// number.
// Duplicate user subject name
X509_NAME *psubj = X509_NAME_dup(X509_get_subject_name(xEEC));
// Create an entry with the common name
unsigned char sn[20] = {0};
sprintf((char *)sn, "%d", serial);
if (!X509_NAME_add_entry_by_txt(psubj, (char *)"CN", MBSTRING_ASC,
sn, -1, -1, 0)) {
PRINT("could not add CN - (serial: "<<serial<<", sn: "<<sn<<")");
return -kErrPX_SetAttribute;
}
//
// Set the name
if (X509_REQ_set_subject_name(preq, psubj) != 1) {
PRINT("could not set subject name - return");
return -kErrPX_SetAttribute;
}
//
// Create the extension CertProxyInfo
PROXY_CERT_INFO_EXTENSION *pci = PROXY_CERT_INFO_EXTENSION_new();
if (!pci) {
PRINT("could not create structure for extension - return");
return -kErrPX_NoResources;
}
pci->proxyPolicy->policyLanguage = OBJ_txt2obj("1.3.6.1.5.5.7.21.1", 1);
//
// Set the new length
if (depthlen > -1) {
if ((pci->pcPathLengthConstraint = ASN1_INTEGER_new())) {
ASN1_INTEGER_set(pci->pcPathLengthConstraint, depthlen);
} else {
PRINT("could not set the path length contrain");
return -kErrPX_SetPathDepth;
}
}
//
// create extension
X509_EXTENSION *ext = X509_EXTENSION_new();
if (!ext) {
PRINT("could not create extension object");
return -kErrPX_NoResources;
}
// Set extension name.
ASN1_OBJECT *obj = OBJ_txt2obj(gsiProxyCertInfo_OID, 1);
if (!obj || X509_EXTENSION_set_object(ext, obj) != 1) {
PRINT("could not set extension name");
return -kErrPX_SetAttribute;
}
// flag as critical
if (X509_EXTENSION_set_critical(ext, 1) != 1) {
PRINT("could not set extension critical flag");
return -kErrPX_SetAttribute;
}
// Extract data in format for extension
X509_EXTENSION_get_data(ext)->length = i2d_PROXY_CERT_INFO_EXTENSION(pci, 0);
if (!(X509_EXTENSION_get_data(ext)->data = (unsigned char *)malloc(X509_EXTENSION_get_data(ext)->length+1))) {
PRINT("could not allocate data field for extension");
return -kErrPX_NoResources;
}
unsigned char *pp = X509_EXTENSION_get_data(ext)->data;
if ((i2d_PROXY_CERT_INFO_EXTENSION(pci, &pp)) <= 0) {
PRINT("problem converting data for extension");
return -kErrPX_Error;
}
// Create a stack
STACK_OF(X509_EXTENSION) *esk = sk_X509_EXTENSION_new_null();
if (!esk) {
PRINT("could not create stack for extensions");
return -kErrPX_NoResources;
}
//
// Now we add the new extension
if (sk_X509_EXTENSION_push(esk, ext) == 0) {
PRINT("could not push the extension in the stack");
return -kErrPX_Error;
}
// Add extension
if (!(X509_REQ_add_extensions(preq, esk))) {
PRINT("problem adding extension");
return -kErrPX_SetAttribute;
}
//
// Sign the request
if (!(X509_REQ_sign(preq, ekPX, EVP_sha1()))) {
PRINT("problems signing the request");
return -kErrPX_Signing;
}
//
// Create new proxy cert
X509 *xPX = X509_new();
if (!xPX) {
PRINT("could not create certificate object for proxies");
return -kErrPX_NoResources;
}
// Set version number
if (X509_set_version(xPX, 2L) != 1) {
PRINT("could not set version");
return -kErrPX_SetAttribute;
}
// Set serial number
if (ASN1_INTEGER_set(X509_get_serialNumber(xPX), serial) != 1) {
PRINT("could not set serial number");
return -kErrPX_SetAttribute;
}
// Set subject name
if (X509_set_subject_name(xPX, psubj) != 1) {
PRINT("could not set subject name");
return -kErrPX_SetAttribute;
}
// Set issuer name
if (X509_set_issuer_name(xPX, X509_get_subject_name(xEEC)) != 1) {
PRINT("could not set issuer name");
return -kErrPX_SetAttribute;
}
// Set public key
if (X509_set_pubkey(xPX, ekPX) != 1) {
PRINT("could not set issuer name");
return -kErrPX_SetAttribute;
}
// Set proxy validity: notBefore now
if (!X509_gmtime_adj(X509_get_notBefore(xPX), 0)) {
PRINT("could not set notBefore");
return -kErrPX_SetAttribute;
}
// Set proxy validity: notAfter expire_secs from now
if (!X509_gmtime_adj(X509_get_notAfter(xPX), valid)) {
PRINT("could not set notAfter");
return -kErrPX_SetAttribute;
}
// First duplicate the extensions of the EE certificate
X509_EXTENSION *xEECext = 0;
int nEECext = X509_get_ext_count(xEEC);
DEBUG("number of extensions found in the original certificate: "<< nEECext);
int i = 0;
bool haskeyusage = 0;
for (i = 0; i< nEECext; i++) {
xEECext = X509_get_ext(xEEC, i);
char s[256];
OBJ_obj2txt(s, sizeof(s), X509_EXTENSION_get_object(xEECext), 1);
// Flag key usage extension
if (!haskeyusage && !strcmp(s, KEY_USAGE_OID)) haskeyusage = 1;
// Skip subject alternative name extension
if (!strcmp(s, SUBJ_ALT_NAME_OID)) continue;
// Duplicate and add to the stack
X509_EXTENSION *xEECextdup = X509_EXTENSION_dup(xEECext);
if (X509_add_ext(xPX, xEECextdup, -1) == 0) {
PRINT("could not push the extension '"<<s<<"' in the stack");
return -kErrPX_Error;
}
// Notify what we added
int crit = X509_EXTENSION_get_critical(xEECextdup);
DEBUG("added extension '"<<s<<"', critical: " << crit);
}
// Warn if the critical oen is missing
if (!haskeyusage) {
PRINT(">>> WARNING: critical extension 'Key Usage' not found in original certificate! ");
PRINT(">>> WARNING: this proxy may not be accepted by some parsers. ");
}
// Add the extension
if (X509_add_ext(xPX, ext, -1) != 1) {
PRINT("could not add extension");
return -kErrPX_SetAttribute;
}
//
// Sign the certificate
if (!(X509_sign(xPX, ekEEC, EVP_sha1()))) {
PRINT("problems signing the certificate");
return -kErrPX_Signing;
}
// Fill outputs
XrdCryptoX509 *xcPX = new XrdCryptosslX509(xPX);
if (!xcPX) {
PRINT("could not create container for proxy certificate");
return -kErrPX_NoResources;
}
// We need the full key
((XrdCryptosslX509 *)xcPX)->SetPKI((XrdCryptoX509data)ekPX);
xp->PushBack(xcPX);
XrdCryptoX509 *xcEEC = new XrdCryptosslX509(xEEC);
if (!xcEEC) {
PRINT("could not create container for EEC certificate");
return -kErrPX_NoResources;
}
xp->PushBack(xcEEC);
*kp = new XrdCryptosslRSA(ekPX);
if (!(*kp)) {
PRINT("could not creatr out PKI");
return -kErrPX_NoResources;
}
//
// Write to a file if requested
int rc = 0;
if (fnp) {
// Open the file in write mode
FILE *fp = fopen(fnp,"w");
if (!fp) {
PRINT("cannot open file to save the proxy certificate (file: "<<fnp<<")");
fclose(fp);
rc = -kErrPX_ProxyFile;
}
int ifp = fileno(fp);
if (ifp == -1) {
PRINT("got invalid file descriptor for the proxy certificate (file: "<<
fnp<<")");
fclose(fp);
rc = -kErrPX_ProxyFile;
}
// Set permissions to 0600
if (fchmod(ifp, 0600) == -1) {
PRINT("cannot set permissions on file: "<<fnp<<" (errno: "<<errno<<")");
fclose(fp);
rc = -kErrPX_ProxyFile;
}
if (!rc && PEM_write_X509(fp, xPX) != 1) {
PRINT("error while writing proxy certificate");
fclose(fp);
rc = -kErrPX_ProxyFile;
}
if (!rc && PEM_write_RSAPrivateKey(fp, kPX, 0, 0, 0, 0, 0) != 1) {
PRINT("error while writing proxy private key");
fclose(fp);
rc = -kErrPX_ProxyFile;
}
if (!rc && PEM_write_X509(fp, xEEC) != 1) {
PRINT("error while writing EEC certificate");
fclose(fp);
rc = -kErrPX_ProxyFile;
}
fclose(fp);
// Change
}
// Cleanup
EVP_PKEY_free(ekEEC);
X509_REQ_free(preq);
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
sk_X509_EXTENSION_free(esk);
#else /* OPENSSL */
sk_free(esk);
#endif /* OPENSSL */
// We are done
return rc;
}
//____________________________________________________________________________
int XrdCryptosslX509CreateProxyReq(XrdCryptoX509 *xcpi,
XrdCryptoX509Req **xcro, XrdCryptoRSA **kcro)
{
// Create a proxy certificate request following the GSI specification
// (RFC 3820) for the proxy certificate 'xpi'.
// The proxy certificate is returned in 'xpo' and its full RSA key in 'kpo'.
// Policy fields in the CertProxyExtension not yet included.
// Return 0 in case of success, < 0 otherwise
EPNAME("X509CreateProxyReq");
// Make sure we got an proxy certificate as input
if (!xcpi || !(xcpi->Opaque())) {
PRINT("input proxy certificate not specified");
return -1;
}
// Point to the cerificate
X509 *xpi = (X509 *)(xcpi->Opaque());
// Make sure the certificate is not expired
if (!(xcpi->IsValid())) {
PRINT("EEC certificate has expired");
return -kErrPX_ExpiredEEC;
}
//
// Create a new request
X509_REQ *xro = X509_REQ_new();
if (!xro) {
PRINT("cannot to create cert request");
return -kErrPX_NoResources;
}
//
// Use same num of bits as the signing certificate, but
// less than 512
int bits = EVP_PKEY_bits(X509_get_pubkey(xpi));
bits = (bits < 512) ? 512 : bits;
//
// Create the new PKI for the proxy (exponent 65537)
RSA *kro = RSA_new();
if (!kro) {
PRINT("proxy key could not be generated - return");
return -kErrPX_GenerateKey;
}
BIGNUM *e = BN_new();
if (!e) {
PRINT("proxy key could not be generated - return");
RSA_free(kro);
return -kErrPX_GenerateKey;
}
BN_set_word(e, 0x10001);
if (RSA_generate_key_ex(kro, bits, e, NULL) != 1) {
RSA_free(kro);
BN_free(e);
PRINT("proxy key could not be generated - return");
return -kErrPX_GenerateKey;
}
BN_free(e);
//
// Set the key into the request
EVP_PKEY *ekro = EVP_PKEY_new();
if (!ekro) {
PRINT("could not create a EVP_PKEY * instance - return");
return -kErrPX_NoResources;
}
EVP_PKEY_assign_RSA(ekro, kro);
X509_REQ_set_pubkey(xro, ekro);
//
// Generate a serial number. Specification says that this *should*
// unique, so we just draw an unsigned random integer
unsigned int serial = XrdSutRndm::GetUInt();
//
// The subject name is the certificate subject + /CN=<rand_uint>
// with <rand_uint> is a random unsigned int used also as serial
// number.
// Duplicate user subject name
X509_NAME *psubj = X509_NAME_dup(X509_get_subject_name(xpi));
if (xcro && *xcro && *((int *)(*xcro)) <= 10100) {
// Delete existing proxy CN addition; for backward compatibility
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
int ne = X509_NAME_entry_count(psubj);
#else /* OPENSSL */
int ne = psubj->entries->num;
#endif /* OPENSSL */
if (ne >= 0) {
X509_NAME_ENTRY *cne = X509_NAME_delete_entry(psubj, ne-1);
if (cne) {
X509_NAME_ENTRY_free(cne);
} else {
DEBUG("problems modifying subject name");
}
}
*xcro = 0;
}
// Create an entry with the common name
unsigned char sn[20] = {0};
sprintf((char *)sn, "%d", serial);
if (!X509_NAME_add_entry_by_txt(psubj, (char *)"CN", MBSTRING_ASC,
sn, -1, -1, 0)) {
PRINT("could not add CN - (serial: "<<serial<<", sn: "<<sn<<")");
return -kErrPX_SetAttribute;
}
//
// Set the name
if (X509_REQ_set_subject_name(xro, psubj) != 1) {
PRINT("could not set subject name - return");
return -kErrPX_SetAttribute;
}
//
// Create the extension CertProxyInfo
PROXY_CERT_INFO_EXTENSION *pci = PROXY_CERT_INFO_EXTENSION_new();
if (!pci) {
PRINT("could not create structure for extension - return");
return -kErrPX_NoResources;
}
pci->proxyPolicy->policyLanguage = OBJ_txt2obj("1.3.6.1.5.5.7.21.1", 1);
//
// Create a stack
STACK_OF(X509_EXTENSION) *esk = sk_X509_EXTENSION_new_null();
if (!esk) {
PRINT("could not create stack for extensions");
return -kErrPX_NoResources;
}
//
// Get signature path depth from present proxy
X509_EXTENSION *xpiext = 0;
int npiext = X509_get_ext_count(xpi);
int i = 0;
bool haskeyusage = 0;
int indepthlen = -1;
for (i = 0; i< npiext; i++) {
xpiext = X509_get_ext(xpi, i);
char s[256];
OBJ_obj2txt(s, sizeof(s), X509_EXTENSION_get_object(xpiext), 1);
// Flag key usage extension
if (!haskeyusage && !strcmp(s, KEY_USAGE_OID)) haskeyusage = 1;
// Skip subject alternative name extension
if (!strcmp(s, SUBJ_ALT_NAME_OID)) continue;
// Get signature path depth from present proxy
if (!strcmp(s, gsiProxyCertInfo_OID) ||
!strcmp(s, gsiProxyCertInfo_OLD_OID)) {
unsigned char *p = X509_EXTENSION_get_data(xpiext)->data;
PROXY_CERT_INFO_EXTENSION *inpci = 0;
if (!strcmp(s, gsiProxyCertInfo_OID))
inpci = d2i_PROXY_CERT_INFO_EXTENSION(0, (XRDGSI_CONST unsigned char **)(&p), X509_EXTENSION_get_data(xpiext)->length);
else
inpci = d2i_PROXY_CERT_INFO_EXTENSION_OLD(0, (XRDGSI_CONST unsigned char **)(&p), X509_EXTENSION_get_data(xpiext)->length);
if (inpci &&
inpci->pcPathLengthConstraint)
indepthlen = ASN1_INTEGER_get(inpci->pcPathLengthConstraint);
DEBUG("IN depth length: "<<indepthlen);
} else {
// Duplicate and add to the stack
X509_EXTENSION *xpiextdup = X509_EXTENSION_dup(xpiext);
if (sk_X509_EXTENSION_push(esk, xpiextdup) == 0) {
PRINT("could not push the extension '"<<s<<"' in the stack");
return -kErrPX_Error;
}
// Notify what we added
int crit = X509_EXTENSION_get_critical(xpiextdup);
DEBUG("added extension '"<<s<<"', critical: " << crit);
}
// Do not free the extension: its owned by the certificate
xpiext = 0;
}
//
// Warn if the critical oen is missing
if (!haskeyusage) {
PRINT(">>> WARNING: critical extension 'Key Usage' not found in original certificate! ");
PRINT(">>> WARNING: this proxy may not be accepted by some parsers. ");
}
//
// Set the new length
if (indepthlen > -1) {
if ((pci->pcPathLengthConstraint = ASN1_INTEGER_new())) {
int depthlen = (indepthlen > 0) ? (indepthlen-1) : 0;
ASN1_INTEGER_set(pci->pcPathLengthConstraint, depthlen);
} else {
PRINT("could not set the path length contrain");
return -kErrPX_SetPathDepth;
}
}
//
// create extension
X509_EXTENSION *ext = X509_EXTENSION_new();
if (!ext) {
PRINT("could not create extension object");
return -kErrPX_NoResources;
}
// Extract data in format for extension
X509_EXTENSION_get_data(ext)->length = i2d_PROXY_CERT_INFO_EXTENSION(pci, 0);
if (!(X509_EXTENSION_get_data(ext)->data = (unsigned char *)malloc(X509_EXTENSION_get_data(ext)->length+1))) {
PRINT("could not allocate data field for extension");
return -kErrPX_NoResources;
}
unsigned char *pp = X509_EXTENSION_get_data(ext)->data;
if ((i2d_PROXY_CERT_INFO_EXTENSION(pci, &pp)) <= 0) {
PRINT("problem converting data for extension");
return -kErrPX_Error;
}
// Set extension name.
ASN1_OBJECT *obj = OBJ_txt2obj(gsiProxyCertInfo_OID, 1);
if (!obj || X509_EXTENSION_set_object(ext, obj) != 1) {
PRINT("could not set extension name");
return -kErrPX_SetAttribute;
}
// flag as critical
if (X509_EXTENSION_set_critical(ext, 1) != 1) {
PRINT("could not set extension critical flag");
return -kErrPX_SetAttribute;
}
if (sk_X509_EXTENSION_push(esk, ext) == 0) {
PRINT("could not push the extension in the stack");
return -kErrPX_Error;
}
// Add extensions
if (!(X509_REQ_add_extensions(xro, esk))) {
PRINT("problem adding extension");
return -kErrPX_SetAttribute;
}
//
// Sign the request
if (!(X509_REQ_sign(xro, ekro, EVP_sha1()))) {
PRINT("problems signing the request");
return -kErrPX_Signing;
}
// Prepare output
*xcro = new XrdCryptosslX509Req(xro);
*kcro = new XrdCryptosslRSA(ekro);
// Cleanup
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
sk_X509_EXTENSION_free(esk);
#else /* OPENSSL */
sk_free(esk);
#endif /* OPENSSL */
// We are done
return 0;
}
//____________________________________________________________________________
int XrdCryptosslX509SignProxyReq(XrdCryptoX509 *xcpi, XrdCryptoRSA *kcpi,
XrdCryptoX509Req *xcri, XrdCryptoX509 **xcpo)
{
// Sign a proxy certificate request.
// Return 0 in case of success, < 0 otherwise
EPNAME("X509SignProxyReq");
// Make sure we got the right inputs
if (!xcpi || !kcpi || !xcri || !xcpo) {
PRINT("invalid inputs");
return -1;
}
// Make sure the certificate is not expired
int timeleft = xcpi->NotAfter() - (int)time(0) + XrdCryptoTZCorr();
if (timeleft < 0) {
PRINT("EEC certificate has expired");
return -kErrPX_ExpiredEEC;
}
// Point to the cerificate
X509 *xpi = (X509 *)(xcpi->Opaque());
// Check key consistency
if (kcpi->status != XrdCryptoRSA::kComplete) {
PRINT("inconsistent key loaded");
return -kErrPX_BadEECkey;
}
// Point to the cerificate
RSA *kpi = EVP_PKEY_get0_RSA((EVP_PKEY *)(kcpi->Opaque()));
//
// Set the key into the request
EVP_PKEY *ekpi = EVP_PKEY_new();
if (!ekpi) {
PRINT("could not create a EVP_PKEY * instance - return");
return -kErrPX_NoResources;
}
EVP_PKEY_set1_RSA(ekpi, kpi);
// Get request in raw form
X509_REQ *xri = (X509_REQ *)(xcri->Opaque());
// Extract subject names
XrdOucString psbj, rsbj;
XrdCryptosslNameOneLine(X509_get_subject_name(xpi), psbj);
XrdCryptosslNameOneLine(X509_REQ_get_subject_name(xri), rsbj);
if (psbj.length() <= 0 || rsbj.length() <= 0) {
PRINT("names undefined");
return -kErrPX_BadNames;
}
// Check the subject name: the new proxy one must be in the form
// '<issuer subject> + /CN=<serial>'
XrdOucString neecp(psbj);
XrdOucString neecr(rsbj,0,rsbj.rfind("/CN=")-1);
if (neecr.length() <= 0 || neecr.length() <= 0 || neecp != neecr) {
if (xcri->Version() <= 10100) {
// Support previous format
neecp.erase(psbj.rfind("/CN="));
if (neecr.length() <= 0 || neecr.length() <= 0 || neecp != neecr) {
PRINT("Request subject not in the form '<EEC subject> + /CN=<serial>'");
PRINT(" Versn: "<<xcri->Version());
PRINT(" Proxy: "<<neecp);
PRINT(" SubRq: "<<neecr);
return -kErrPX_BadNames;
}
} else {
PRINT("Request subject not in the form '<issuer subject> + /CN=<serial>'");
PRINT(" Versn: "<<xcri->Version());
PRINT(" Proxy: "<<neecp);
PRINT(" SubRq: "<<neecr);
return -kErrPX_BadNames;
}
}
// Extract serial number
XrdOucString sserial(rsbj,rsbj.rfind("/CN=")+4);
unsigned int serial = (unsigned int)(strtol(sserial.c_str(), 0, 10));
//
// Create new proxy cert
X509 *xpo = X509_new();
if (!xpo) {
PRINT("could not create certificate object for proxies");
return -kErrPX_NoResources;
}
// Set version number
if (X509_set_version(xpo, 2L) != 1) {
PRINT("could not set version");
return -kErrPX_SetAttribute;
}
// Set serial number
if (ASN1_INTEGER_set(X509_get_serialNumber(xpo), serial) != 1) {
PRINT("could not set serial number");
return -kErrPX_SetAttribute;
}
// Set subject name
if (X509_set_subject_name(xpo, X509_REQ_get_subject_name(xri)) != 1) {
PRINT("could not set subject name");
return -kErrPX_SetAttribute;
}
// Set issuer name
if (X509_set_issuer_name(xpo, X509_get_subject_name(xpi)) != 1) {
PRINT("could not set issuer name");
return -kErrPX_SetAttribute;
}
// Set public key
if (X509_set_pubkey(xpo, X509_REQ_get_pubkey(xri)) != 1) {
PRINT("could not set public key");
return -kErrPX_SetAttribute;
}
// Set proxy validity: notBefore now
if (!X509_gmtime_adj(X509_get_notBefore(xpo), 0)) {
PRINT("could not set notBefore");
return -kErrPX_SetAttribute;
}
// Set proxy validity: notAfter timeleft from now
if (!X509_gmtime_adj(X509_get_notAfter(xpo), timeleft)) {
PRINT("could not set notAfter");
return -kErrPX_SetAttribute;
}
//
// Get signature path depth from input proxy
X509_EXTENSION *xpiext = 0, *xriext = 0;