38#include <openssl/asn1.h>
39#include <openssl/asn1t.h>
40#include <openssl/err.h>
41#include <openssl/evp.h>
42#include <openssl/pem.h>
43#include <openssl/rsa.h>
44#include <openssl/x509v3.h>
61 sk_X509_EXTENSION_pop_free(ske, X509_EXTENSION_free);
63using EVP_PKEY_ptr = std::unique_ptr<EVP_PKEY,
decltype(&EVP_PKEY_free)>;
64using X509_ptr = std::unique_ptr<X509,
decltype(&X509_free)>;
65using X509_NAME_ptr = std::unique_ptr<X509_NAME,
decltype(&X509_NAME_free)>;
66using X509_REQ_ptr = std::unique_ptr<X509_REQ,
decltype(&X509_REQ_free)>;
78#define KEY_USAGE_OID "2.5.29.15"
80#define SUBJ_ALT_NAME_OID "2.5.29.17"
88#define XRDGSI_VOMS_ATCAP_OID "1.3.6.1.4.1.8005.100.100.4"
89#define XRDGSI_VOMS_ACSEQ_OID "1.3.6.1.4.1.8005.100.100.5"
91#define BIO_PRINT(b,c) \
93 BIO_get_mem_ptr(b, &bptr); \
95 char *s = new char[bptr->length+1]; \
96 memcpy(s, bptr->data, bptr->length); \
97 s[bptr->length] = '\0'; \
101 PRINT("ERROR: "<<c<<" BIO internal buffer undefined!"); \
105#define BIO_GET_STRING(b,str) \
107 BIO_get_mem_ptr(b, &bptr); \
109 char *s = new char[bptr->length+1]; \
110 memcpy(s, bptr->data, bptr->length); \
111 s[bptr->length] = '\0'; \
115 PRINT("ERROR: GET_STRING: BIO internal buffer undefined!"); \
121 EVP_PKEY_CTX *ckctx = EVP_PKEY_CTX_new(pkey, 0);
122 rc = EVP_PKEY_check(ckctx);
123 EVP_PKEY_CTX_free(ckctx);
138ASN1_SEQUENCE(PROXY_CERT_INFO_EXTENSION_OLD) =
140 ASN1_SIMPLE(PROXY_CERT_INFO_EXTENSION, proxyPolicy, PROXY_POLICY),
141 ASN1_EXP_OPT(PROXY_CERT_INFO_EXTENSION, pcPathLengthConstraint, ASN1_INTEGER, 1)
142} ASN1_SEQUENCE_END_name(PROXY_CERT_INFO_EXTENSION, PROXY_CERT_INFO_EXTENSION_OLD)
144IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(PROXY_CERT_INFO_EXTENSION, PROXY_CERT_INFO_EXTENSION_OLD, PROXY_CERT_INFO_EXTENSION_OLD)
161 X509_EXTENSION *ext = (X509_EXTENSION *)extdata;
165 OBJ_obj2txt(s,
sizeof(s), X509_EXTENSION_get_object(ext), 1);
168 const unsigned char *p = ASN1_STRING_get0_data(X509_EXTENSION_get_data(ext));
169 PROXY_CERT_INFO_EXTENSION *pci = 0;
171 pci = d2i_PROXY_CERT_INFO_EXTENSION(0, &p, ASN1_STRING_length(X509_EXTENSION_get_data(ext)));
173 pci = d2i_PROXY_CERT_INFO_EXTENSION_OLD(0, &p, ASN1_STRING_length(X509_EXTENSION_get_data(ext)));
180 if (pci->pcPathLengthConstraint) {
181 pathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint);
186 *haspolicy = (pci->proxyPolicy) ? 1 : 0;
204 X509_EXTENSION *ext = (X509_EXTENSION *)extdata;
208 OBJ_obj2txt(s,
sizeof(s), X509_EXTENSION_get_object(ext), 1);
211 const unsigned char *p = ASN1_STRING_get0_data(X509_EXTENSION_get_data(ext));
212 PROXY_CERT_INFO_EXTENSION *pci = 0;
214 pci = d2i_PROXY_CERT_INFO_EXTENSION(0, &p, ASN1_STRING_length(X509_EXTENSION_get_data(ext)));
216 pci = d2i_PROXY_CERT_INFO_EXTENSION_OLD(0, &p, ASN1_STRING_length(X509_EXTENSION_get_data(ext)));
221 if (pci->pcPathLengthConstraint) {
222 ASN1_INTEGER_set(pci->pcPathLengthConstraint, pathlen);
246 EPNAME(
"X509CreateProxy");
249 if (!fnc || !fnk || !xp || !kp) {
250 PRINT(
"invalid inputs ");
256 OpenSSL_add_all_ciphers();
257 OpenSSL_add_all_digests();
258 ERR_load_crypto_strings();
262 int valid = (pxopt) ? pxopt->
valid : 43200;
263 int depthlen = (pxopt) ? pxopt->
depthlen : -1;
268 FILE *fc =
fopen(fnc,
"r");
271 if (PEM_read_X509(fc, &xEEC, 0, 0)) {
272 DEBUG(
"EEC certificate loaded from file: "<<fnc);
274 PRINT(
"unable to load EEC certificate from file: "<<fnc);
279 PRINT(
"EEC certificate cannot be opened (file: "<<fnc<<
")");
284 int now = (int)time(0);
286 PRINT(
"EEC certificate has expired");
294 FILE *fk =
fopen(fnk,
"r");
299 PRINT(
"Your identity: "<<sbj);
300 if ((PEM_read_PrivateKey(fk, &ekEEC, 0, 0))) {
301 DEBUG(
"EEC private key loaded from file: "<<fnk);
303 PRINT(
"unable to load EEC private key from file: "<<fnk);
309 PRINT(
"EEC private key file cannot be opened (file: "<<fnk<<
")");
316 PRINT(
"inconsistent key loaded");
317 EVP_PKEY_free(ekEEC);
323 X509_REQ *preq = X509_REQ_new();
325 PRINT(
"cannot to create cert request");
326 EVP_PKEY_free(ekEEC);
332 BIGNUM *e = BN_new();
334 PRINT(
"proxy key could not be generated - return");
335 EVP_PKEY_free(ekEEC);
339 BN_set_word(e, 0x10001);
341 EVP_PKEY_CTX *pkctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, 0);
342 EVP_PKEY_keygen_init(pkctx);
343 EVP_PKEY_CTX_set_rsa_keygen_bits(pkctx, bits);
344#if OPENSSL_VERSION_NUMBER >= 0x30000000L
345 EVP_PKEY_CTX_set1_rsa_keygen_pubexp(pkctx, e);
348 EVP_PKEY_CTX_set_rsa_keygen_pubexp(pkctx, e);
350 EVP_PKEY_keygen(pkctx, &ekPX);
351 EVP_PKEY_CTX_free(pkctx);
353 PRINT(
"proxy key could not be generated - return");
354 EVP_PKEY_free(ekEEC);
358 X509_REQ_set_pubkey(preq, ekPX);
368 X509_NAME *psubj = X509_NAME_dup(X509_get_subject_name(xEEC));
370 unsigned char sn[20] = {0};
371 sprintf((
char *)sn,
"%d", serial);
372 if (!X509_NAME_add_entry_by_txt(psubj, (
char *)
"CN", MBSTRING_ASC,
374 PRINT(
"could not add CN - (serial: "<<serial<<
", sn: "<<sn<<
")");
379 if (X509_REQ_set_subject_name(preq, psubj) != 1) {
380 PRINT(
"could not set subject name - return");
386 PROXY_CERT_INFO_EXTENSION *pci = PROXY_CERT_INFO_EXTENSION_new();
388 PRINT(
"could not create structure for extension - return");
391 pci->proxyPolicy->policyLanguage = OBJ_txt2obj(
"1.3.6.1.5.5.7.21.1", 1);
395 if ((pci->pcPathLengthConstraint = ASN1_INTEGER_new())) {
396 ASN1_INTEGER_set(pci->pcPathLengthConstraint, depthlen);
398 PRINT(
"could not set the path length contrain");
405 X509_EXTENSION *ext = X509_EXTENSION_new();
407 PRINT(
"could not create extension object");
412 if (!obj || X509_EXTENSION_set_object(ext, obj) != 1) {
413 PRINT(
"could not set extension name");
417 if (X509_EXTENSION_set_critical(ext, 1) != 1) {
418 PRINT(
"could not set extension critical flag");
422 int len = i2d_PROXY_CERT_INFO_EXTENSION(pci, 0);
423 unsigned char *data = (
unsigned char *) malloc(len);
425 PRINT(
"could not allocate data field for extension");
428 unsigned char *pp = data;
429 if ((i2d_PROXY_CERT_INFO_EXTENSION(pci, &pp)) <= 0) {
430 PRINT(
"problem converting data for extension");
433 ASN1_OCTET_STRING *os = ASN1_OCTET_STRING_new();
435 PRINT(
"could not allocate data field for extension");
438 if (ASN1_OCTET_STRING_set(os, data, len) == 0) {
439 PRINT(
"could not allocate data field for extension");
443 if (X509_EXTENSION_set_data(ext, os) == 0) {
444 PRINT(
"could not allocate data field for extension");
447 ASN1_STRING_free(os);
449 STACK_OF(X509_EXTENSION) *esk = sk_X509_EXTENSION_new_null();
451 PRINT(
"could not create stack for extensions");
456 if (sk_X509_EXTENSION_push(esk, ext) == 0) {
457 PRINT(
"could not push the extension in the stack");
461 if (!(X509_REQ_add_extensions(preq, esk))) {
462 PRINT(
"problem adding extension");
467 if (!(X509_REQ_sign(preq, ekPX, EVP_sha256()))) {
468 PRINT(
"problems signing the request");
473 X509 *xPX = X509_new();
475 PRINT(
"could not create certificate object for proxies");
480 if (X509_set_version(xPX, 2L) != 1) {
481 PRINT(
"could not set version");
486 if (ASN1_INTEGER_set(X509_get_serialNumber(xPX), serial) != 1) {
487 PRINT(
"could not set serial number");
492 if (X509_set_subject_name(xPX, psubj) != 1) {
493 PRINT(
"could not set subject name");
496 X509_NAME_free(psubj);
499 if (X509_set_issuer_name(xPX, X509_get_subject_name(xEEC)) != 1) {
500 PRINT(
"could not set issuer name");
505 if (X509_set_pubkey(xPX, ekPX) != 1) {
506 PRINT(
"could not set issuer name");
511 if (!X509_gmtime_adj(X509_get_notBefore(xPX), 0)) {
512 PRINT(
"could not set notBefore");
517 if (!X509_gmtime_adj(X509_get_notAfter(xPX), valid)) {
518 PRINT(
"could not set notAfter");
523#if OPENSSL_VERSION_NUMBER < 0x40000000L
524 X509_EXTENSION *xEECext = 0;
526 const X509_EXTENSION *xEECext = 0;
528 int nEECext = X509_get_ext_count(xEEC);
529 DEBUG(
"number of extensions found in the original certificate: "<< nEECext);
531 bool haskeyusage = 0;
532 for (i = 0; i< nEECext; i++) {
533 xEECext = X509_get_ext(xEEC, i);
535 OBJ_obj2txt(s,
sizeof(s), X509_EXTENSION_get_object(xEECext), 1);
537 if (!haskeyusage && !strcmp(s,
KEY_USAGE_OID)) haskeyusage = 1;
541 X509_EXTENSION *xEECextdup = X509_EXTENSION_dup(xEECext);
542 if (X509_add_ext(xPX, xEECextdup, -1) == 0) {
543 PRINT(
"could not push the extension '"<<s<<
"' in the stack");
547 int crit = X509_EXTENSION_get_critical(xEECextdup);
548 DEBUG(
"added extension '"<<s<<
"', critical: " << crit);
553 PRINT(
">>> WARNING: critical extension 'Key Usage' not found in original certificate! ");
554 PRINT(
">>> WARNING: this proxy may not be accepted by some parsers. ");
558 if (X509_add_ext(xPX, ext, -1) != 1) {
559 PRINT(
"could not add extension");
565 if (!(X509_sign(xPX, ekEEC, EVP_sha256()))) {
566 PRINT(
"problems signing the certificate");
573 PRINT(
"could not create container for proxy certificate");
581 PRINT(
"could not create container for EEC certificate");
587 PRINT(
"could not creatr out PKI");
596 FILE *fp =
fopen(fnp,
"w");
599 PRINT(
"cannot open file to save the proxy certificate (file: "<<fnp<<
")");
602 else if ( (ifp = fileno(fp)) == -1) {
603 PRINT(
"got invalid file descriptor for the proxy certificate (file: "<<
609 else if (fchmod(ifp, 0600) == -1) {
610 PRINT(
"cannot set permissions on file: "<<fnp<<
" (errno: "<<errno<<
")");
614 else if (!rc && PEM_write_X509(fp, xPX) != 1) {
615 PRINT(
"error while writing proxy certificate");
619 else if (!rc && PEM_write_PrivateKey(fp, ekPX, 0, 0, 0, 0, 0) != 1) {
620 PRINT(
"error while writing proxy private key");
624 else if (!rc && PEM_write_X509(fp, xEEC) != 1) {
625 PRINT(
"error while writing EEC certificate");
635 EVP_PKEY_free(ekEEC);
637 sk_X509_EXTENSION_free(esk);
652 EPNAME(
"X509CreateProxyReq");
655 if (!xcpi || !(xcpi->
Opaque())) {
656 PRINT(
"input proxy certificate not specified");
661 X509 *xpi = (X509 *)(xcpi->
Opaque());
665 PRINT(
"EEC certificate has expired");
681 xro.reset(X509_REQ_new());
683 PRINT(
"cannot to create cert request");
689 ekro.reset(X509_get_pubkey(xpi));
690 int bits = EVP_PKEY_bits(ekro.get());
696 BIGNUM *e = BN_new();
698 PRINT(
"proxy key could not be generated - return");
701 BN_set_word(e, 0x10001);
702 EVP_PKEY_CTX *pkctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, 0);
703 EVP_PKEY_keygen_init(pkctx);
704 EVP_PKEY_CTX_set_rsa_keygen_bits(pkctx, bits);
705#if OPENSSL_VERSION_NUMBER >= 0x30000000L
706 EVP_PKEY_CTX_set1_rsa_keygen_pubexp(pkctx, e);
709 EVP_PKEY_CTX_set_rsa_keygen_pubexp(pkctx, e);
712 EVP_PKEY *tmppk =
nullptr;
713 EVP_PKEY_keygen(pkctx, &tmppk);
716 EVP_PKEY_CTX_free(pkctx);
720 PRINT(
"proxy key could not be generated - return");
723 X509_REQ_set_pubkey(xro.get(), ekro.get());
733 psubj.reset(X509_NAME_dup(X509_get_subject_name(xpi)));
734 if (xcro && *xcro && *((
int *)(*xcro)) <= 10100) {
736 int ne = X509_NAME_entry_count(psubj.get());
738 X509_NAME_ENTRY *cne = X509_NAME_delete_entry(psubj.get(), ne-1);
740 X509_NAME_ENTRY_free(cne);
742 DEBUG(
"problems modifying subject name");
748 unsigned char sn[20] = {0};
749 sprintf((
char *)sn,
"%d", serial);
750 if (!X509_NAME_add_entry_by_txt(psubj.get(), (
char *)
"CN", MBSTRING_ASC,
752 PRINT(
"could not add CN - (serial: "<<serial<<
", sn: "<<sn<<
")");
757 if (X509_REQ_set_subject_name(xro.get(), psubj.get()) != 1) {
758 PRINT(
"could not set subject name - return");
764 pci.reset(PROXY_CERT_INFO_EXTENSION_new());
766 PRINT(
"could not create structure for extension - return");
769 pci->proxyPolicy->policyLanguage = OBJ_txt2obj(
"1.3.6.1.5.5.7.21.1", 1);
772 esk.reset(sk_X509_EXTENSION_new_null());
774 PRINT(
"could not create stack for extensions");
779#if OPENSSL_VERSION_NUMBER < 0x40000000L
780 X509_EXTENSION *xpiext = 0;
782 const X509_EXTENSION *xpiext = 0;
784 int npiext = X509_get_ext_count(xpi);
786 bool haskeyusage = 0;
788 for (i = 0; i< npiext; i++) {
789 xpiext = X509_get_ext(xpi, i);
791 OBJ_obj2txt(s,
sizeof(s), X509_EXTENSION_get_object(xpiext), 1);
793 if (!haskeyusage && !strcmp(s,
KEY_USAGE_OID)) haskeyusage = 1;
799 const unsigned char *p = ASN1_STRING_get0_data(X509_EXTENSION_get_data(xpiext));
800 PROXY_CERT_INFO_EXTENSION *inpci = 0;
802 inpci = d2i_PROXY_CERT_INFO_EXTENSION(0, &p, ASN1_STRING_length(X509_EXTENSION_get_data(xpiext)));
804 inpci = d2i_PROXY_CERT_INFO_EXTENSION_OLD(0, &p, ASN1_STRING_length(X509_EXTENSION_get_data(xpiext)));
806 inpci->pcPathLengthConstraint)
807 indepthlen = ASN1_INTEGER_get(inpci->pcPathLengthConstraint);
808 DEBUG(
"IN depth length: "<<indepthlen);
809 PROXY_CERT_INFO_EXTENSION_free(inpci);
812 X509_EXTENSION *xpiextdup = X509_EXTENSION_dup(xpiext);
813 if (sk_X509_EXTENSION_push(esk.get(), xpiextdup) == 0) {
814 PRINT(
"could not push the extension '"<<s<<
"' in the stack");
815 X509_EXTENSION_free(xpiextdup);
819 int crit = X509_EXTENSION_get_critical(xpiextdup);
820 DEBUG(
"added extension '"<<s<<
"', critical: " << crit);
828 PRINT(
">>> WARNING: critical extension 'Key Usage' not found in original certificate! ");
829 PRINT(
">>> WARNING: this proxy may not be accepted by some parsers. ");
833 if (indepthlen > -1) {
834 if ((pci->pcPathLengthConstraint = ASN1_INTEGER_new())) {
835 int depthlen = (indepthlen > 0) ? (indepthlen-1) : 0;
836 ASN1_INTEGER_set(pci->pcPathLengthConstraint, depthlen);
838 PRINT(
"could not set the path length contrain");
844 ext.reset(X509_EXTENSION_new());
846 PRINT(
"could not create extension object");
850 int len = i2d_PROXY_CERT_INFO_EXTENSION(pci.get(), 0);
851 unsigned char *data = (
unsigned char *) malloc(len);
853 PRINT(
"could not allocate data field for extension");
856 unsigned char *pp = data;
857 if ((i2d_PROXY_CERT_INFO_EXTENSION(pci.get(), &pp)) <= 0) {
858 PRINT(
"problem converting data for extension");
861 ASN1_OCTET_STRING *os = ASN1_OCTET_STRING_new();
863 PRINT(
"could not allocate data field for extension");
866 if (ASN1_OCTET_STRING_set(os, data, len) == 0) {
867 PRINT(
"could not allocate data field for extension");
871 if (X509_EXTENSION_set_data(ext.get(), os) == 0) {
872 PRINT(
"could not allocate data field for extension");
875 ASN1_STRING_free(os);
880 if (!obj || X509_EXTENSION_set_object(ext.get(), obj) != 1) {
881 PRINT(
"could not set extension name");
882 ASN1_OBJECT_free(obj);
885 ASN1_OBJECT_free(obj);
889 if (X509_EXTENSION_set_critical(ext.get(), 1) != 1) {
890 PRINT(
"could not set extension critical flag");
893 if (sk_X509_EXTENSION_push(esk.get(), ext.get()) == 0) {
894 PRINT(
"could not push the extension in the stack");
901 if (!(X509_REQ_add_extensions(xro.get(), esk.get()))) {
902 PRINT(
"problem adding extension");
907 if (!(X509_REQ_sign(xro.get(), ekro.get(), EVP_sha256()))) {
908 PRINT(
"problems signing the request");
931 EPNAME(
"X509SignProxyReq");
934 if (!xcpi || !kcpi || !xcri || !xcpo) {
935 PRINT(
"invalid inputs");
940 int timeleft = xcpi->
NotAfter() - (int)time(0);
942 PRINT(
"EEC certificate has expired");
946 X509 *xpi = (X509 *)(xcpi->
Opaque());
950 PRINT(
"inconsistent key loaded");
964#if OPENSSL_VERSION_NUMBER >= 0x30000000L
965 ekpi.reset(EVP_PKEY_dup((EVP_PKEY *)(kcpi->
Opaque())));
967 PRINT(
"could not create a EVP_PKEY * instance - return");
971 RSA *kpi = EVP_PKEY_get0_RSA((EVP_PKEY *)(kcpi->
Opaque()));
974 ekpi.reset(EVP_PKEY_new());
976 PRINT(
"could not create a EVP_PKEY * instance - return");
979 EVP_PKEY_set1_RSA(ekpi.get(), kpi);
983 X509_REQ *xri = (X509_REQ *)(xcri->
Opaque());
990 PRINT(
"names undefined");
998 if (neecr.length() <= 0 || neecr.length() <= 0 || neecp != neecr) {
999 if (xcri->
Version() <= 10100) {
1001 neecp.erase(psbj.
rfind(
"/CN="));
1002 if (neecr.length() <= 0 || neecr.length() <= 0 || neecp != neecr) {
1003 PRINT(
"Request subject not in the form '<EEC subject> + /CN=<serial>'");
1005 PRINT(
" Proxy: "<<neecp);
1006 PRINT(
" SubRq: "<<neecr);
1010 PRINT(
"Request subject not in the form '<issuer subject> + /CN=<serial>'");
1012 PRINT(
" Proxy: "<<neecp);
1013 PRINT(
" SubRq: "<<neecr);
1020 unsigned int serial = (
unsigned int)(strtol(sserial.c_str(), 0, 10));
1023 xpo.reset(X509_new());
1025 PRINT(
"could not create certificate object for proxies");
1030 if (X509_set_version(xpo.get(), 2L) != 1) {
1031 PRINT(
"could not set version");
1036 if (ASN1_INTEGER_set(X509_get_serialNumber(xpo.get()), serial) != 1) {
1037 PRINT(
"could not set serial number");
1042 if (X509_set_subject_name(xpo.get(), X509_REQ_get_subject_name(xri)) != 1) {
1043 PRINT(
"could not set subject name");
1048 if (X509_set_issuer_name(xpo.get(), X509_get_subject_name(xpi)) != 1) {
1049 PRINT(
"could not set issuer name");
1054 if (X509_set_pubkey(xpo.get(), X509_REQ_get_pubkey(xri)) != 1) {
1055 PRINT(
"could not set public key");
1060 if (!X509_gmtime_adj(X509_get_notBefore(xpo.get()), 0)) {
1061 PRINT(
"could not set notBefore");
1066 if (!X509_gmtime_adj(X509_get_notAfter(xpo.get()), timeleft)) {
1067 PRINT(
"could not set notAfter");
1073#if OPENSSL_VERSION_NUMBER < 0x40000000L
1074 X509_EXTENSION *xpiext = 0, *xriext = 0;
1076 const X509_EXTENSION *xpiext = 0, *xriext = 0;
1078 int npiext = X509_get_ext_count(xpi);
1080 bool haskeyusage = 0;
1081 int indepthlen = -1;
1082 for (i = 0; i< npiext; i++) {
1083 xpiext = X509_get_ext(xpi, i);
1085 const ASN1_OBJECT *obj = X509_EXTENSION_get_object(xpiext);
1087 OBJ_obj2txt(s,
sizeof(s), obj, 1);
1090 const unsigned char *p = ASN1_STRING_get0_data(X509_EXTENSION_get_data(xpiext));
1091 PROXY_CERT_INFO_EXTENSION *inpci = 0;
1093 inpci = d2i_PROXY_CERT_INFO_EXTENSION(0, &p, ASN1_STRING_length(X509_EXTENSION_get_data(xpiext)));
1095 inpci = d2i_PROXY_CERT_INFO_EXTENSION_OLD(0, &p, ASN1_STRING_length(X509_EXTENSION_get_data(xpiext)));
1097 inpci->pcPathLengthConstraint)
1098 indepthlen = ASN1_INTEGER_get(inpci->pcPathLengthConstraint);
1099 DEBUG(
"IN depth length: "<<indepthlen);
1100 PROXY_CERT_INFO_EXTENSION_free(inpci);
1103 if (!haskeyusage && !strcmp(s,
KEY_USAGE_OID)) haskeyusage = 1;
1106 PRINT(
"subject alternative name extension not allowed! Skipping request");
1113 PRINT(
"more than one ProxyCertInfo extension! Skipping request");
1119 X509_EXTENSION *xpiextdup = X509_EXTENSION_dup(xpiext);
1120 if (X509_add_ext(xpo.get(), xpiextdup, -1) == 0) {
1121 PRINT(
"could not push the extension '"<<s<<
"' in the stack");
1122 X509_EXTENSION_free( xpiextdup );
1126 int crit = X509_EXTENSION_get_critical(xpiextdup);
1127 DEBUG(
"added extension '"<<s<<
"', critical: " << crit);
1128 X509_EXTENSION_free( xpiextdup );
1136 xrisk.reset(X509_REQ_get_extensions(xri));
1139 int nriext = sk_X509_EXTENSION_num(xrisk.get());
1140 if (nriext == 0 || !haskeyusage) {
1141 PRINT(
"wrong extensions in request: "<< nriext<<
", "<<haskeyusage);
1146 int reqdepthlen = -1;
1148 const unsigned char *p = ASN1_STRING_get0_data(X509_EXTENSION_get_data(xriext));
1149 PROXY_CERT_INFO_EXTENSION *reqpci =
1150 d2i_PROXY_CERT_INFO_EXTENSION(0, &p, ASN1_STRING_length(X509_EXTENSION_get_data(xriext)));
1152 reqpci->pcPathLengthConstraint)
1153 reqdepthlen = ASN1_INTEGER_get(reqpci->pcPathLengthConstraint);
1154 PROXY_CERT_INFO_EXTENSION_free(reqpci);
1156 DEBUG(
"REQ depth length: "<<reqdepthlen);
1159 int outdepthlen = (reqdepthlen < indepthlen) ? reqdepthlen :
1163 pci.reset(PROXY_CERT_INFO_EXTENSION_new());
1165 PRINT(
"could not create structure for extension - return");
1168 pci->proxyPolicy->policyLanguage = OBJ_txt2obj(
"1.3.6.1.5.5.7.21.1", 1);
1171 if (outdepthlen > -1) {
1172 if ((pci->pcPathLengthConstraint = ASN1_INTEGER_new())) {
1173 int depthlen = (outdepthlen > 0) ? (outdepthlen-1) : 0;
1174 ASN1_INTEGER_set(pci->pcPathLengthConstraint, depthlen);
1176 PRINT(
"could not set the path length contrain");
1181 ext.reset(X509_EXTENSION_new());
1183 PRINT(
"could not create extension object");
1187 int len = i2d_PROXY_CERT_INFO_EXTENSION(pci.get(), 0);
1188 unsigned char *data = (
unsigned char *) malloc(len);
1190 PRINT(
"could not allocate data field for extension");
1193 unsigned char *pp = data;
1194 if ((i2d_PROXY_CERT_INFO_EXTENSION(pci.get(), &pp)) <= 0) {
1195 PRINT(
"problem converting data for extension");
1198 ASN1_OCTET_STRING *os = ASN1_OCTET_STRING_new();
1200 PRINT(
"could not allocate data field for extension");
1203 if (ASN1_OCTET_STRING_set(os, data, len) == 0) {
1204 PRINT(
"could not allocate data field for extension");
1208 if (X509_EXTENSION_set_data(ext.get(), os) == 0) {
1209 PRINT(
"could not allocate data field for extension");
1212 ASN1_STRING_free(os);
1217 if (!obj || X509_EXTENSION_set_object(ext.get(), obj) != 1) {
1218 PRINT(
"could not set extension name");
1219 ASN1_OBJECT_free( obj );
1222 ASN1_OBJECT_free( obj );
1226 if (X509_EXTENSION_set_critical(ext.get(), 1) != 1) {
1227 PRINT(
"could not set extension critical flag");
1232 if (X509_add_ext(xpo.get(), ext.get(), -1) == 0) {
1233 PRINT(
"could not add extension");
1239 if (!(X509_sign(xpo.get(), ekpi.get(), EVP_sha256()))) {
1240 PRINT(
"problems signing the certificate");
1263 EPNAME(
"X509GetVOMSAttr");
1268 PRINT(
"invalid inputs");
1273 X509 *xpi = (X509 *)(xcpi->
Opaque());
1278#if OPENSSL_VERSION_NUMBER < 0x40000000L
1279 X509_EXTENSION *xpiext = 0;
1281 const X509_EXTENSION *xpiext = 0;
1283 int npiext = X509_get_ext_count(xpi);
1285 for (i = 0; i< npiext; i++) {
1286 xpiext = X509_get_ext(xpi, i);
1288 OBJ_obj2txt(s,
sizeof(s), X509_EXTENSION_get_object(xpiext), 1);
1290 DEBUG(
"found extension '"<<s<<
"'");
1294 const unsigned char *pp = ASN1_STRING_get0_data(X509_EXTENSION_get_data(xpiext));
1295 long length = ASN1_STRING_length(X509_EXTENSION_get_data(xpiext));
1297 DEBUG(
"ret: " << ret <<
" - vat: " << vat);
1312 const unsigned char *p,*ep,*tot,*op,*opp;
1314 int tag, xclass, ret = 0;
1317 ASN1_OCTET_STRING *os = 0;
1323 while ((p < tot) && (op < p)) {
1325 j = ASN1_get_object(&p, &len, &tag, &xclass, length);
1330 PRINT(
"ERROR: error in encoding");
1338 if (j & V_ASN1_CONSTRUCTED) {
1341 PRINT(
"ERROR:CONST: length is greater than " <<length);
1345 if ((j == 0x21) && (len == 0)) {
1352 if ((r == 2) || (p >= tot))
1366 if (tag == V_ASN1_OBJECT) {
1368 if (d2i_ASN1_OBJECT(&o, &opp, len+hl)) {
1369 BIO *mem = BIO_new(BIO_s_mem());
1370 i2a_ASN1_OBJECT(mem, o);
1375 DEBUG(
"AOBJ:"<<objstr<<
" (getvat: "<<getvat<<
")");
1377 PRINT(
"ERROR:AOBJ: BAD OBJECT");
1379 }
else if (tag == V_ASN1_OCTET_STRING) {
1380 int i, printable = 1;
1382 os = d2i_ASN1_OCTET_STRING(0, &opp, len + hl);
1383 if (os && ASN1_STRING_length(os) > 0) {
1384 opp = ASN1_STRING_get0_data(os);
1386 for (i=0; i < ASN1_STRING_length(os); i++) {
1387 if (( (opp[i] <
' ') && (opp[i] !=
'\n') &&
1388 (opp[i] !=
'\r') && (opp[i] !=
'\t')) || (opp[i] >
'~')) {
1396 if (vat.
length() > 0) vat +=
",";
1397 vat += (
const char *)opp;
1400 DEBUG(
"OBJS:" << (
const char *)opp <<
" (len: " << ASN1_STRING_length(os) <<
")");
1404 ASN1_OCTET_STRING_free(os);
1410 if ((tag == V_ASN1_EOC) && (xclass == 0)) {
1423 if (o) ASN1_OBJECT_free(o);
1424 if (os) ASN1_OCTET_STRING_free(os);
1426 DEBUG(
"ret: "<<ret<<
" - getvat: "<<getvat);
1439 EPNAME(
"X509CheckProxy3");
1442 X509 *cert = (X509 *)(xcpi->
Opaque());
1445 int numext = X509_get_ext_count(cert);
1447 emsg =
"certificate has got no extensions";
1450 TRACE(ALL,
"certificate has "<<numext<<
" extensions");
1452#if OPENSSL_VERSION_NUMBER < 0x40000000L
1453 X509_EXTENSION *ext = 0;
1455 const X509_EXTENSION *ext = 0;
1457 PROXY_CERT_INFO_EXTENSION *pci = 0;
1458 for (
int i = 0; i < numext; i++) {
1460#if OPENSSL_VERSION_NUMBER < 0x40000000L
1461 X509_EXTENSION *xext = X509_get_ext(cert, i);
1463 const X509_EXTENSION *xext = X509_get_ext(cert, i);
1468 OBJ_obj2txt(s,
sizeof(s), X509_EXTENSION_get_object(xext), 1);
1469 DEBUG(i <<
": got: "<< s);
1474 const unsigned char *p = ASN1_STRING_get0_data(X509_EXTENSION_get_data(ext));
1475 pci = d2i_PROXY_CERT_INFO_EXTENSION(0, &p, ASN1_STRING_length(X509_EXTENSION_get_data(ext)));
1477 PRINT(
"WARNING: multiple proxyCertInfo extensions found: taking the first");
1483 const unsigned char *p = ASN1_STRING_get0_data(X509_EXTENSION_get_data(ext));
1484 pci = d2i_PROXY_CERT_INFO_EXTENSION_OLD(0, &p, ASN1_STRING_length(X509_EXTENSION_get_data(ext)));
1486 PRINT(
"WARNING: multiple proxyCertInfo extensions found: taking the first");
1493 emsg =
"proxyCertInfo extension not found";
1497 emsg =
"proxyCertInfo extension could not be deserialized";
1502 if ((pci->proxyPolicy) == 0) {
1503 emsg =
"could not access policy from proxyCertInfo extension";
1507 if ((pci->proxyPolicy->policyLanguage) == 0) {
1508 emsg =
"could not access policy language from proxyCertInfo extension";
#define XrdCryptoMinRSABits
#define XrdCryptoDefRSABits
#define gsiProxyCertInfo_OID
#define gsiProxyCertInfo_OLD_OID
time_t XrdCryptosslASN1toUTC(const ASN1_TIME *tsn1)
void XrdCryptosslNameOneLine(const X509_NAME *nm, XrdOucString &s)
void XrdCryptosslSetPathLenConstraint(void *ext, int pathlen)
#define kErrPX_BadExtension
int XrdCryptosslX509CheckProxy3(XrdCryptoX509 *, XrdOucString &)
#define kErrPX_SetAttribute
int XrdCryptosslX509SignProxyReq(XrdCryptoX509 *, XrdCryptoRSA *, XrdCryptoX509Req *, XrdCryptoX509 **)
bool XrdCryptosslProxyCertInfo(const void *ext, int &pathlen, bool *haspolicy=0)
#define kErrPX_NoResources
int XrdCryptosslX509CreateProxyReq(XrdCryptoX509 *, XrdCryptoX509Req **, XrdCryptoRSA **)
int XrdCryptosslX509CreateProxy(const char *, const char *, XrdProxyOpt_t *, XrdCryptogsiX509Chain *, XrdCryptoRSA **, const char *)
int XrdCryptosslX509GetVOMSAttr(XrdCryptoX509 *, XrdOucString &)
#define kErrPX_GenerateKey
#define kErrPX_SetPathDepth
#define kErrPX_ExpiredEEC
#define kErrPX_BadEECfile
static int XrdCheckRSA(EVP_PKEY *pkey)
std::unique_ptr< EVP_PKEY, decltype(&EVP_PKEY_free)> EVP_PKEY_ptr
#define BIO_GET_STRING(b, str)
int XrdCryptosslX509FillUnknownExt(const unsigned char **pp, long length)
static void stackOfX509ExtensionDelete(STACK_OF(X509_EXTENSION) *ske)
#define XRDGSI_VOMS_ATCAP_OID
int XrdCryptosslX509Asn1PrintInfo(int tag, int xclass, int constructed, int indent)
int XrdCryptosslX509FillVOMS(const unsigned char **pp, long length, bool &getvat, XrdOucString &vat)
#define XRDGSI_VOMS_ACSEQ_OID
#define SUBJ_ALT_NAME_OID
std::unique_ptr< STACK_OF(X509_EXTENSION), decltype(&stackOfX509ExtensionDelete)> STACK_OF_X509_EXTENSION_ptr
std::unique_ptr< X509, decltype(&X509_free)> X509_ptr
std::unique_ptr< X509_NAME, decltype(&X509_NAME_free)> X509_NAME_ptr
std::unique_ptr< X509_EXTENSION, decltype(&X509_EXTENSION_free)> X509_EXTENSION_ptr
std::unique_ptr< PROXY_CERT_INFO_EXTENSION, decltype(&PROXY_CERT_INFO_EXTENSION_free)> PROXY_CERT_INFO_EXTENSION_ptr
std::unique_ptr< X509_REQ, decltype(&X509_REQ_free)> X509_REQ_ptr
int emsg(int rc, char *msg)
virtual XrdCryptoRSAdata Opaque()
void PushBack(XrdCryptoX509 *c)
virtual XrdCryptoX509Reqdata Opaque()
virtual XrdCryptoX509data Opaque()
virtual bool IsValid(int when=0)
virtual time_t NotAfter()
int rfind(const char c, int start=STR_NPOS)
static unsigned int GetUInt()