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 unsigned char *p = X509_EXTENSION_get_data(ext)->data;
169 PROXY_CERT_INFO_EXTENSION *pci = 0;
171 pci = d2i_PROXY_CERT_INFO_EXTENSION(0, (
const unsigned char **)(&p), X509_EXTENSION_get_data(ext)->length);
173 pci = d2i_PROXY_CERT_INFO_EXTENSION_OLD(0, (
const unsigned char **)(&p), X509_EXTENSION_get_data(ext)->length);
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 unsigned char *p = X509_EXTENSION_get_data(ext)->data;
212 PROXY_CERT_INFO_EXTENSION *pci = 0;
214 pci = d2i_PROXY_CERT_INFO_EXTENSION(0, (
const unsigned char **)(&p), X509_EXTENSION_get_data(ext)->length);
216 pci = d2i_PROXY_CERT_INFO_EXTENSION_OLD(0, (
const unsigned char **)(&p), X509_EXTENSION_get_data(ext)->length);
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 X509_EXTENSION_get_data(ext)->length = i2d_PROXY_CERT_INFO_EXTENSION(pci, 0);
423 if (!(X509_EXTENSION_get_data(ext)->data = (
unsigned char *)malloc(X509_EXTENSION_get_data(ext)->length+1))) {
424 PRINT(
"could not allocate data field for extension");
427 unsigned char *pp = X509_EXTENSION_get_data(ext)->data;
428 if ((i2d_PROXY_CERT_INFO_EXTENSION(pci, &pp)) <= 0) {
429 PRINT(
"problem converting data for extension");
433 STACK_OF(X509_EXTENSION) *esk = sk_X509_EXTENSION_new_null();
435 PRINT(
"could not create stack for extensions");
440 if (sk_X509_EXTENSION_push(esk, ext) == 0) {
441 PRINT(
"could not push the extension in the stack");
445 if (!(X509_REQ_add_extensions(preq, esk))) {
446 PRINT(
"problem adding extension");
451 if (!(X509_REQ_sign(preq, ekPX, EVP_sha256()))) {
452 PRINT(
"problems signing the request");
457 X509 *xPX = X509_new();
459 PRINT(
"could not create certificate object for proxies");
464 if (X509_set_version(xPX, 2L) != 1) {
465 PRINT(
"could not set version");
470 if (ASN1_INTEGER_set(X509_get_serialNumber(xPX), serial) != 1) {
471 PRINT(
"could not set serial number");
476 if (X509_set_subject_name(xPX, psubj) != 1) {
477 PRINT(
"could not set subject name");
480 X509_NAME_free(psubj);
483 if (X509_set_issuer_name(xPX, X509_get_subject_name(xEEC)) != 1) {
484 PRINT(
"could not set issuer name");
489 if (X509_set_pubkey(xPX, ekPX) != 1) {
490 PRINT(
"could not set issuer name");
495 if (!X509_gmtime_adj(X509_get_notBefore(xPX), 0)) {
496 PRINT(
"could not set notBefore");
501 if (!X509_gmtime_adj(X509_get_notAfter(xPX), valid)) {
502 PRINT(
"could not set notAfter");
507 X509_EXTENSION *xEECext = 0;
508 int nEECext = X509_get_ext_count(xEEC);
509 DEBUG(
"number of extensions found in the original certificate: "<< nEECext);
511 bool haskeyusage = 0;
512 for (i = 0; i< nEECext; i++) {
513 xEECext = X509_get_ext(xEEC, i);
515 OBJ_obj2txt(s,
sizeof(s), X509_EXTENSION_get_object(xEECext), 1);
517 if (!haskeyusage && !strcmp(s,
KEY_USAGE_OID)) haskeyusage = 1;
521 X509_EXTENSION *xEECextdup = X509_EXTENSION_dup(xEECext);
522 if (X509_add_ext(xPX, xEECextdup, -1) == 0) {
523 PRINT(
"could not push the extension '"<<s<<
"' in the stack");
527 int crit = X509_EXTENSION_get_critical(xEECextdup);
528 DEBUG(
"added extension '"<<s<<
"', critical: " << crit);
533 PRINT(
">>> WARNING: critical extension 'Key Usage' not found in original certificate! ");
534 PRINT(
">>> WARNING: this proxy may not be accepted by some parsers. ");
538 if (X509_add_ext(xPX, ext, -1) != 1) {
539 PRINT(
"could not add extension");
545 if (!(X509_sign(xPX, ekEEC, EVP_sha256()))) {
546 PRINT(
"problems signing the certificate");
553 PRINT(
"could not create container for proxy certificate");
561 PRINT(
"could not create container for EEC certificate");
567 PRINT(
"could not creatr out PKI");
576 FILE *fp =
fopen(fnp,
"w");
579 PRINT(
"cannot open file to save the proxy certificate (file: "<<fnp<<
")");
582 else if ( (ifp = fileno(fp)) == -1) {
583 PRINT(
"got invalid file descriptor for the proxy certificate (file: "<<
589 else if (fchmod(ifp, 0600) == -1) {
590 PRINT(
"cannot set permissions on file: "<<fnp<<
" (errno: "<<errno<<
")");
594 else if (!rc && PEM_write_X509(fp, xPX) != 1) {
595 PRINT(
"error while writing proxy certificate");
599 else if (!rc && PEM_write_PrivateKey(fp, ekPX, 0, 0, 0, 0, 0) != 1) {
600 PRINT(
"error while writing proxy private key");
604 else if (!rc && PEM_write_X509(fp, xEEC) != 1) {
605 PRINT(
"error while writing EEC certificate");
615 EVP_PKEY_free(ekEEC);
617 sk_X509_EXTENSION_free(esk);
632 EPNAME(
"X509CreateProxyReq");
635 if (!xcpi || !(xcpi->
Opaque())) {
636 PRINT(
"input proxy certificate not specified");
641 X509 *xpi = (X509 *)(xcpi->
Opaque());
645 PRINT(
"EEC certificate has expired");
661 xro.reset(X509_REQ_new());
663 PRINT(
"cannot to create cert request");
669 ekro.reset(X509_get_pubkey(xpi));
670 int bits = EVP_PKEY_bits(ekro.get());
676 BIGNUM *e = BN_new();
678 PRINT(
"proxy key could not be generated - return");
681 BN_set_word(e, 0x10001);
682 EVP_PKEY_CTX *pkctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, 0);
683 EVP_PKEY_keygen_init(pkctx);
684 EVP_PKEY_CTX_set_rsa_keygen_bits(pkctx, bits);
685#if OPENSSL_VERSION_NUMBER >= 0x30000000L
686 EVP_PKEY_CTX_set1_rsa_keygen_pubexp(pkctx, e);
689 EVP_PKEY_CTX_set_rsa_keygen_pubexp(pkctx, e);
692 EVP_PKEY *tmppk =
nullptr;
693 EVP_PKEY_keygen(pkctx, &tmppk);
696 EVP_PKEY_CTX_free(pkctx);
700 PRINT(
"proxy key could not be generated - return");
703 X509_REQ_set_pubkey(xro.get(), ekro.get());
713 psubj.reset(X509_NAME_dup(X509_get_subject_name(xpi)));
714 if (xcro && *xcro && *((
int *)(*xcro)) <= 10100) {
716 int ne = X509_NAME_entry_count(psubj.get());
718 X509_NAME_ENTRY *cne = X509_NAME_delete_entry(psubj.get(), ne-1);
720 X509_NAME_ENTRY_free(cne);
722 DEBUG(
"problems modifying subject name");
728 unsigned char sn[20] = {0};
729 sprintf((
char *)sn,
"%d", serial);
730 if (!X509_NAME_add_entry_by_txt(psubj.get(), (
char *)
"CN", MBSTRING_ASC,
732 PRINT(
"could not add CN - (serial: "<<serial<<
", sn: "<<sn<<
")");
737 if (X509_REQ_set_subject_name(xro.get(), psubj.get()) != 1) {
738 PRINT(
"could not set subject name - return");
744 pci.reset(PROXY_CERT_INFO_EXTENSION_new());
746 PRINT(
"could not create structure for extension - return");
749 pci->proxyPolicy->policyLanguage = OBJ_txt2obj(
"1.3.6.1.5.5.7.21.1", 1);
752 esk.reset(sk_X509_EXTENSION_new_null());
754 PRINT(
"could not create stack for extensions");
759 X509_EXTENSION *xpiext = 0;
760 int npiext = X509_get_ext_count(xpi);
762 bool haskeyusage = 0;
764 for (i = 0; i< npiext; i++) {
765 xpiext = X509_get_ext(xpi, i);
767 OBJ_obj2txt(s,
sizeof(s), X509_EXTENSION_get_object(xpiext), 1);
769 if (!haskeyusage && !strcmp(s,
KEY_USAGE_OID)) haskeyusage = 1;
775 unsigned char *p = X509_EXTENSION_get_data(xpiext)->data;
776 PROXY_CERT_INFO_EXTENSION *inpci = 0;
778 inpci = d2i_PROXY_CERT_INFO_EXTENSION(0, (
const unsigned char **)(&p), X509_EXTENSION_get_data(xpiext)->length);
780 inpci = d2i_PROXY_CERT_INFO_EXTENSION_OLD(0, (
const unsigned char **)(&p), X509_EXTENSION_get_data(xpiext)->length);
782 inpci->pcPathLengthConstraint)
783 indepthlen = ASN1_INTEGER_get(inpci->pcPathLengthConstraint);
784 DEBUG(
"IN depth length: "<<indepthlen);
785 PROXY_CERT_INFO_EXTENSION_free(inpci);
788 X509_EXTENSION *xpiextdup = X509_EXTENSION_dup(xpiext);
789 if (sk_X509_EXTENSION_push(esk.get(), xpiextdup) == 0) {
790 PRINT(
"could not push the extension '"<<s<<
"' in the stack");
791 X509_EXTENSION_free(xpiextdup);
795 int crit = X509_EXTENSION_get_critical(xpiextdup);
796 DEBUG(
"added extension '"<<s<<
"', critical: " << crit);
804 PRINT(
">>> WARNING: critical extension 'Key Usage' not found in original certificate! ");
805 PRINT(
">>> WARNING: this proxy may not be accepted by some parsers. ");
809 if (indepthlen > -1) {
810 if ((pci->pcPathLengthConstraint = ASN1_INTEGER_new())) {
811 int depthlen = (indepthlen > 0) ? (indepthlen-1) : 0;
812 ASN1_INTEGER_set(pci->pcPathLengthConstraint, depthlen);
814 PRINT(
"could not set the path length contrain");
820 ext.reset(X509_EXTENSION_new());
822 PRINT(
"could not create extension object");
826 X509_EXTENSION_get_data(ext.get())->length = i2d_PROXY_CERT_INFO_EXTENSION(pci.get(), 0);
827 if (!(X509_EXTENSION_get_data(ext.get())->data = (
unsigned char *)malloc(X509_EXTENSION_get_data(ext.get())->length+1))) {
828 PRINT(
"could not allocate data field for extension");
831 unsigned char *pp = X509_EXTENSION_get_data(ext.get())->data;
832 if ((i2d_PROXY_CERT_INFO_EXTENSION(pci.get(), &pp)) <= 0) {
833 PRINT(
"problem converting data for extension");
840 if (!obj || X509_EXTENSION_set_object(ext.get(), obj) != 1) {
841 PRINT(
"could not set extension name");
842 ASN1_OBJECT_free(obj);
845 ASN1_OBJECT_free(obj);
849 if (X509_EXTENSION_set_critical(ext.get(), 1) != 1) {
850 PRINT(
"could not set extension critical flag");
853 if (sk_X509_EXTENSION_push(esk.get(), ext.get()) == 0) {
854 PRINT(
"could not push the extension in the stack");
861 if (!(X509_REQ_add_extensions(xro.get(), esk.get()))) {
862 PRINT(
"problem adding extension");
867 if (!(X509_REQ_sign(xro.get(), ekro.get(), EVP_sha256()))) {
868 PRINT(
"problems signing the request");
891 EPNAME(
"X509SignProxyReq");
894 if (!xcpi || !kcpi || !xcri || !xcpo) {
895 PRINT(
"invalid inputs");
900 int timeleft = xcpi->
NotAfter() - (int)time(0);
902 PRINT(
"EEC certificate has expired");
906 X509 *xpi = (X509 *)(xcpi->
Opaque());
910 PRINT(
"inconsistent key loaded");
924#if OPENSSL_VERSION_NUMBER >= 0x30000000L
925 ekpi.reset(EVP_PKEY_dup((EVP_PKEY *)(kcpi->
Opaque())));
927 PRINT(
"could not create a EVP_PKEY * instance - return");
931 RSA *kpi = EVP_PKEY_get0_RSA((EVP_PKEY *)(kcpi->
Opaque()));
934 ekpi.reset(EVP_PKEY_new());
936 PRINT(
"could not create a EVP_PKEY * instance - return");
939 EVP_PKEY_set1_RSA(ekpi.get(), kpi);
943 X509_REQ *xri = (X509_REQ *)(xcri->
Opaque());
950 PRINT(
"names undefined");
958 if (neecr.length() <= 0 || neecr.length() <= 0 || neecp != neecr) {
959 if (xcri->
Version() <= 10100) {
961 neecp.erase(psbj.
rfind(
"/CN="));
962 if (neecr.length() <= 0 || neecr.length() <= 0 || neecp != neecr) {
963 PRINT(
"Request subject not in the form '<EEC subject> + /CN=<serial>'");
965 PRINT(
" Proxy: "<<neecp);
966 PRINT(
" SubRq: "<<neecr);
970 PRINT(
"Request subject not in the form '<issuer subject> + /CN=<serial>'");
972 PRINT(
" Proxy: "<<neecp);
973 PRINT(
" SubRq: "<<neecr);
980 unsigned int serial = (
unsigned int)(strtol(sserial.c_str(), 0, 10));
983 xpo.reset(X509_new());
985 PRINT(
"could not create certificate object for proxies");
990 if (X509_set_version(xpo.get(), 2L) != 1) {
991 PRINT(
"could not set version");
996 if (ASN1_INTEGER_set(X509_get_serialNumber(xpo.get()), serial) != 1) {
997 PRINT(
"could not set serial number");
1002 if (X509_set_subject_name(xpo.get(), X509_REQ_get_subject_name(xri)) != 1) {
1003 PRINT(
"could not set subject name");
1008 if (X509_set_issuer_name(xpo.get(), X509_get_subject_name(xpi)) != 1) {
1009 PRINT(
"could not set issuer name");
1014 if (X509_set_pubkey(xpo.get(), X509_REQ_get_pubkey(xri)) != 1) {
1015 PRINT(
"could not set public key");
1020 if (!X509_gmtime_adj(X509_get_notBefore(xpo.get()), 0)) {
1021 PRINT(
"could not set notBefore");
1026 if (!X509_gmtime_adj(X509_get_notAfter(xpo.get()), timeleft)) {
1027 PRINT(
"could not set notAfter");
1033 X509_EXTENSION *xpiext = 0, *xriext = 0;
1034 int npiext = X509_get_ext_count(xpi);
1036 bool haskeyusage = 0;
1037 int indepthlen = -1;
1038 for (i = 0; i< npiext; i++) {
1039 xpiext = X509_get_ext(xpi, i);
1041 ASN1_OBJECT *obj = X509_EXTENSION_get_object(xpiext);
1043 OBJ_obj2txt(s,
sizeof(s), obj, 1);
1046 unsigned char *p = X509_EXTENSION_get_data(xpiext)->data;
1047 PROXY_CERT_INFO_EXTENSION *inpci = 0;
1049 inpci = d2i_PROXY_CERT_INFO_EXTENSION(0, (
const unsigned char **)(&p), X509_EXTENSION_get_data(xpiext)->length);
1051 inpci = d2i_PROXY_CERT_INFO_EXTENSION_OLD(0, (
const unsigned char **)(&p), X509_EXTENSION_get_data(xpiext)->length);
1053 inpci->pcPathLengthConstraint)
1054 indepthlen = ASN1_INTEGER_get(inpci->pcPathLengthConstraint);
1055 DEBUG(
"IN depth length: "<<indepthlen);
1056 PROXY_CERT_INFO_EXTENSION_free(inpci);
1059 if (!haskeyusage && !strcmp(s,
KEY_USAGE_OID)) haskeyusage = 1;
1062 PRINT(
"subject alternative name extension not allowed! Skipping request");
1069 PRINT(
"more than one ProxyCertInfo extension! Skipping request");
1075 X509_EXTENSION *xpiextdup = X509_EXTENSION_dup(xpiext);
1076 if (X509_add_ext(xpo.get(), xpiextdup, -1) == 0) {
1077 PRINT(
"could not push the extension '"<<s<<
"' in the stack");
1078 X509_EXTENSION_free( xpiextdup );
1082 int crit = X509_EXTENSION_get_critical(xpiextdup);
1083 DEBUG(
"added extension '"<<s<<
"', critical: " << crit);
1084 X509_EXTENSION_free( xpiextdup );
1092 xrisk.reset(X509_REQ_get_extensions(xri));
1095 int nriext = sk_X509_EXTENSION_num(xrisk.get());
1096 if (nriext == 0 || !haskeyusage) {
1097 PRINT(
"wrong extensions in request: "<< nriext<<
", "<<haskeyusage);
1102 int reqdepthlen = -1;
1104 unsigned char *p = X509_EXTENSION_get_data(xriext)->data;
1105 PROXY_CERT_INFO_EXTENSION *reqpci =
1106 d2i_PROXY_CERT_INFO_EXTENSION(0, (
const unsigned char **)(&p), X509_EXTENSION_get_data(xriext)->length);
1108 reqpci->pcPathLengthConstraint)
1109 reqdepthlen = ASN1_INTEGER_get(reqpci->pcPathLengthConstraint);
1110 PROXY_CERT_INFO_EXTENSION_free(reqpci);
1112 DEBUG(
"REQ depth length: "<<reqdepthlen);
1115 int outdepthlen = (reqdepthlen < indepthlen) ? reqdepthlen :
1119 pci.reset(PROXY_CERT_INFO_EXTENSION_new());
1121 PRINT(
"could not create structure for extension - return");
1124 pci->proxyPolicy->policyLanguage = OBJ_txt2obj(
"1.3.6.1.5.5.7.21.1", 1);
1127 if (outdepthlen > -1) {
1128 if ((pci->pcPathLengthConstraint = ASN1_INTEGER_new())) {
1129 int depthlen = (outdepthlen > 0) ? (outdepthlen-1) : 0;
1130 ASN1_INTEGER_set(pci->pcPathLengthConstraint, depthlen);
1132 PRINT(
"could not set the path length contrain");
1137 ext.reset(X509_EXTENSION_new());
1139 PRINT(
"could not create extension object");
1143 X509_EXTENSION_get_data(ext.get())->length = i2d_PROXY_CERT_INFO_EXTENSION(pci.get(), 0);
1144 if (!(X509_EXTENSION_get_data(ext.get())->data = (
unsigned char *)malloc(X509_EXTENSION_get_data(ext.get())->length+1))) {
1145 PRINT(
"could not allocate data field for extension");
1148 unsigned char *pp = X509_EXTENSION_get_data(ext.get())->data;
1149 if ((i2d_PROXY_CERT_INFO_EXTENSION(pci.get(), &pp)) <= 0) {
1150 PRINT(
"problem converting data for extension");
1157 if (!obj || X509_EXTENSION_set_object(ext.get(), obj) != 1) {
1158 PRINT(
"could not set extension name");
1159 ASN1_OBJECT_free( obj );
1162 ASN1_OBJECT_free( obj );
1166 if (X509_EXTENSION_set_critical(ext.get(), 1) != 1) {
1167 PRINT(
"could not set extension critical flag");
1172 if (X509_add_ext(xpo.get(), ext.get(), -1) == 0) {
1173 PRINT(
"could not add extension");
1179 if (!(X509_sign(xpo.get(), ekpi.get(), EVP_sha256()))) {
1180 PRINT(
"problems signing the certificate");
1203 EPNAME(
"X509GetVOMSAttr");
1208 PRINT(
"invalid inputs");
1213 X509 *xpi = (X509 *)(xcpi->
Opaque());
1218 X509_EXTENSION *xpiext = 0;
1219 int npiext = X509_get_ext_count(xpi);
1221 for (i = 0; i< npiext; i++) {
1222 xpiext = X509_get_ext(xpi, i);
1224 OBJ_obj2txt(s,
sizeof(s), X509_EXTENSION_get_object(xpiext), 1);
1226 DEBUG(
"found extension '"<<s<<
"'");
1230 const unsigned char *pp = (
const unsigned char *) X509_EXTENSION_get_data(xpiext)->data;
1231 long length = X509_EXTENSION_get_data(xpiext)->length;
1233 DEBUG(
"ret: " << ret <<
" - vat: " << vat);
1248 const unsigned char *p,*ep,*tot,*op,*opp;
1250 int tag, xclass, ret = 0;
1253 ASN1_OCTET_STRING *os = 0;
1259 while ((p < tot) && (op < p)) {
1261 j = ASN1_get_object(&p, &len, &tag, &xclass, length);
1266 PRINT(
"ERROR: error in encoding");
1274 if (j & V_ASN1_CONSTRUCTED) {
1277 PRINT(
"ERROR:CONST: length is greater than " <<length);
1281 if ((j == 0x21) && (len == 0)) {
1288 if ((r == 2) || (p >= tot))
1302 if (tag == V_ASN1_OBJECT) {
1304 if (d2i_ASN1_OBJECT(&o, &opp, len+hl)) {
1305 BIO *mem = BIO_new(BIO_s_mem());
1306 i2a_ASN1_OBJECT(mem, o);
1311 DEBUG(
"AOBJ:"<<objstr<<
" (getvat: "<<getvat<<
")");
1313 PRINT(
"ERROR:AOBJ: BAD OBJECT");
1315 }
else if (tag == V_ASN1_OCTET_STRING) {
1316 int i, printable = 1;
1318 os = d2i_ASN1_OCTET_STRING(0, &opp, len + hl);
1319 if (os && os->length > 0) {
1322 for (i=0; i<os->length; i++) {
1323 if (( (opp[i] <
' ') && (opp[i] !=
'\n') &&
1324 (opp[i] !=
'\r') && (opp[i] !=
'\t')) || (opp[i] >
'~')) {
1332 if (vat.
length() > 0) vat +=
",";
1333 vat += (
const char *)opp;
1336 DEBUG(
"OBJS:" << (
const char *)opp <<
" (len: "<<os->length<<
")");
1340 ASN1_OCTET_STRING_free(os);
1346 if ((tag == V_ASN1_EOC) && (xclass == 0)) {
1359 if (o) ASN1_OBJECT_free(o);
1360 if (os) ASN1_OCTET_STRING_free(os);
1362 DEBUG(
"ret: "<<ret<<
" - getvat: "<<getvat);
1375 EPNAME(
"X509CheckProxy3");
1378 X509 *cert = (X509 *)(xcpi->
Opaque());
1381 int numext = X509_get_ext_count(cert);
1383 emsg =
"certificate has got no extensions";
1386 TRACE(ALL,
"certificate has "<<numext<<
" extensions");
1388 X509_EXTENSION *ext = 0;
1389 PROXY_CERT_INFO_EXTENSION *pci = 0;
1390 for (
int i = 0; i < numext; i++) {
1392 X509_EXTENSION *xext = X509_get_ext(cert, i);
1396 OBJ_obj2txt(s,
sizeof(s), X509_EXTENSION_get_object(xext), 1);
1397 DEBUG(i <<
": got: "<< s);
1402 unsigned char *p = X509_EXTENSION_get_data(ext)->data;
1403 pci = d2i_PROXY_CERT_INFO_EXTENSION(0, (
const unsigned char **)(&p), X509_EXTENSION_get_data(ext)->length);
1405 PRINT(
"WARNING: multiple proxyCertInfo extensions found: taking the first");
1411 unsigned char *p = X509_EXTENSION_get_data(ext)->data;
1412 pci = d2i_PROXY_CERT_INFO_EXTENSION_OLD(0, (
const unsigned char **)(&p), X509_EXTENSION_get_data(ext)->length);
1414 PRINT(
"WARNING: multiple proxyCertInfo extensions found: taking the first");
1421 emsg =
"proxyCertInfo extension not found";
1425 emsg =
"proxyCertInfo extension could not be deserialized";
1430 if ((pci->proxyPolicy) == 0) {
1431 emsg =
"could not access policy from proxyCertInfo extension";
1435 if ((pci->proxyPolicy->policyLanguage) == 0) {
1436 emsg =
"could not access policy language from proxyCertInfo extension";
#define XrdCryptoMinRSABits
#define XrdCryptoDefRSABits
#define gsiProxyCertInfo_OID
#define gsiProxyCertInfo_OLD_OID
void XrdCryptosslNameOneLine(X509_NAME *nm, XrdOucString &s)
time_t XrdCryptosslASN1toUTC(const ASN1_TIME *tsn1)
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()