39#include <openssl/pem.h>
49#define BIO_PRINT(b,c) \
51 BIO_get_mem_ptr(b, &bptr); \
53 char *s = new char[bptr->length+1]; \
54 memcpy(s, bptr->data, bptr->length); \
55 s[bptr->length] = '\0'; \
59 PRINT("ERROR: "<<c<<" BIO internal buffer undefined!"); \
63const char *XrdCryptosslX509::cpxytype[5] = {
"",
"unknown",
"RFC",
"GSI3",
"legacy" };
71 EPNAME(
"X509::XrdCryptosslX509_file");
90 DEBUG(
"file name undefined");
95 int fd =
open(cf, O_RDONLY);
98 if (errno == ENOENT) {
99 DEBUG(
"file "<<cf<<
" does not exist - do nothing");
101 DEBUG(
"cannot open file "<<cf<<
" (errno: "<<errno<<
")");
106 if (
fstat(fd, &st) != 0) {
107 DEBUG(
"cannot stat file "<<cf<<
" (errno: "<<errno<<
")");
113 FILE *fc = fdopen(fd,
"r");
115 DEBUG(
"cannot fdopen file "<<cf<<
" (errno: "<<errno<<
")");
121 if (!PEM_read_X509(fc, &cert, 0, 0)) {
122 DEBUG(
"Unable to load certificate from file");
125 DEBUG(
"certificate successfully loaded");
143 int fd =
open(kf, O_RDONLY);
145 DEBUG(
"cannot open file "<<kf<<
" (errno: "<<errno<<
")");
148 if (
fstat(fd, &st) == -1) {
149 DEBUG(
"cannot stat private key file "<<kf<<
" (errno:"<<errno<<
")");
153 if (!S_ISREG(st.st_mode) || S_ISDIR(st.st_mode) ||
154 (st.st_mode & (S_IROTH | S_IWOTH)) != 0 ||
155 (st.st_mode & (S_IWGRP)) != 0) {
156 DEBUG(
"private key file "<<kf<<
" has wrong permissions "<<
157 (st.st_mode & 0777) <<
" (should be at most 0640)");
162 FILE *fk = fdopen(fd,
"r");
164 DEBUG(
"cannot open file "<<kf<<
" (errno: "<<errno<<
")");
169 if ((evpp = PEM_read_PrivateKey(fk,0,0,0))) {
170 DEBUG(
"RSA key completed ");
172 auto tmprsa = std::make_unique<XrdCryptosslRSA>(evpp, 1);
175 pki = tmprsa.release();
178 DEBUG(
"cannot read the key from file");
193 EPNAME(
"X509::XrdCryptosslX509_bio");
212 DEBUG(
"got undefined opaque buffer");
218 BIO *bmem = BIO_new(BIO_s_mem());
220 DEBUG(
"unable to create BIO for memory operations");
225 int nw = BIO_write(bmem,(
const void *)(buck->
buffer),buck->
size);
226 if (nw != buck->
size) {
227 DEBUG(
"problems writing data to memory BIO (nw: "<<nw<<
")");
232 if (!(cert = PEM_read_bio_X509(bmem,0,0,0))) {
233 DEBUG(
"unable to read certificate to memory BIO");
247 EVP_PKEY *evpp = X509_get_pubkey(cert);
254 DEBUG(
"could not access the public key");
262 EPNAME(
"X509::XrdCryptosslX509_x509");
281 DEBUG(
"got undefined X509 object");
295 EVP_PKEY *evpp = X509_get_pubkey(cert);
302 DEBUG(
"could not access the public key");
312 if (cert) X509_free(cert);
318void XrdCryptosslX509::CertType()
326 PRINT(
"ERROR: certificate is not initialized");
334 int numext = X509_get_ext_count(cert);
336 DEBUG(
"certificate has got no extensions");
339 TRACE(ALL,
"certificate has "<<numext<<
" extensions");
343#if OPENSSL_VERSION_NUMBER < 0x40000000L
344 X509_EXTENSION *ext = 0;
346 const X509_EXTENSION *ext = 0;
352 BASIC_CONSTRAINTS *bc = 0;
353 if ((bc = (BASIC_CONSTRAINTS *)X509_get_ext_d2i(cert, NID_basic_constraints, &crit, &idx)) &&
356 DEBUG(
"CA certificate");
359 if (bc) BASIC_CONSTRAINTS_free(bc);
365 XrdOucString common(subject, 0, subject.rfind(
"/CN=") - 1);
367 if (issuer == common) {
374 if ((idx = X509_get_ext_by_NID(cert, NID_proxyCertInfo,-1)) == -1) {
380 DEBUG(
"Found GSI 3 proxyCertInfo extension");
381 }
else if (xcp == -1) {
385 if ((ext = X509_get_ext(cert,idx)) == 0) {
386 PRINT(
"ERROR: could not get proxyCertInfo extension");
392 if (X509_EXTENSION_get_critical(ext)) {
393 PROXY_CERT_INFO_EXTENSION *pci = (PROXY_CERT_INFO_EXTENSION *)X509V3_EXT_d2i(ext);
395 if ((pci->proxyPolicy) != 0) {
396 if ((pci->proxyPolicy->policyLanguage) != 0) {
400 DEBUG(
"Found RFC 382{0,1}compliant proxyCertInfo extension");
401 if (X509_get_ext_by_NID(cert, NID_proxyCertInfo, idx) != -1) {
402 PRINT(
"WARNING: multiple proxyCertInfo extensions found: taking the first");
405 PRINT(
"ERROR: accessing policy language from proxyCertInfo extension");
408 PRINT(
"ERROR: accessing policy from proxyCertInfo extension");
410 PROXY_CERT_INFO_EXTENSION_free(pci);
412 PRINT(
"ERROR: proxyCertInfo conversion error");
415 PRINT(
"ERROR: proxyCertInfo not flagged as critical");
418 if (!pxyname || done)
return;
421 XrdOucString lastcn(subject, subject.rfind(
"/CN=") + 4, -1);
422 if (lastcn ==
"proxy" || lastcn ==
"limited proxy") {
444 auto tmprsa = std::make_unique<XrdCryptosslRSA>((EVP_PKEY*)newpki, 1);
452 pki = tmprsa.release();
495 if (subject.length() <= 0) {
499 DEBUG(
"WARNING: no certificate available - cannot extract subject name");
500 return (
const char *)0;
508 return (subject.length() > 0) ? subject.c_str() : (
const char *)0;
518 if (issuer.length() <= 0) {
522 DEBUG(
"WARNING: no certificate available - cannot extract issuer name");
523 return (
const char *)0;
531 return (issuer.length() > 0) ? issuer.c_str() : (
const char *)0;
540 EPNAME(
"X509::IssuerHash");
544 if (issueroldhash.length() <= 0) {
547 char chash[30] = {0};
548 snprintf(chash,
sizeof(chash),
549 "%08lx.0",X509_NAME_hash_old(X509_get_issuer_name(cert)));
550 issueroldhash = chash;
552 DEBUG(
"WARNING: no certificate available - cannot extract issuer hash (md5)");
556 return (issueroldhash.length() > 0) ? issueroldhash.c_str() : (
const char *)0;
560 if (issuerhash.length() <= 0) {
564 char chash[30] = {0};
565 snprintf(chash,
sizeof(chash),
566 "%08lx.0",X509_NAME_hash(X509_get_issuer_name(cert)));
569 DEBUG(
"WARNING: no certificate available - cannot extract issuer hash (default)");
574 return (issuerhash.length() > 0) ? issuerhash.c_str() : (
const char *)0;
583 EPNAME(
"X509::SubjectHash");
587 if (subjectoldhash.length() <= 0) {
590 char chash[30] = {0};
591 snprintf(chash,
sizeof(chash),
592 "%08lx.0",X509_NAME_hash_old(X509_get_subject_name(cert)));
593 subjectoldhash = chash;
595 DEBUG(
"WARNING: no certificate available - cannot extract subject hash (md5)");
599 return (subjectoldhash.length() > 0) ? subjectoldhash.c_str() : (
const char *)0;
603 if (subjecthash.length() <= 0) {
607 char chash[30] = {0};
608 snprintf(chash,
sizeof(chash),
609 "%08lx.0",X509_NAME_hash(X509_get_subject_name(cert)));
612 DEBUG(
"WARNING: no certificate available - cannot extract subject hash (default)");
617 return (subjecthash.length() > 0) ? subjecthash.c_str() : (
const char *)0;
626 if (cert && X509_get_serialNumber(cert)) {
627 BIGNUM *bn = BN_new();
628 ASN1_INTEGER_to_BN(X509_get_serialNumber(cert), bn);
629 char *sn = BN_bn2dec(bn);
630 sernum = strtoll(sn, 0, 10);
644 if (cert && X509_get_serialNumber(cert)) {
645 BIGNUM *bn = BN_new();
646 ASN1_INTEGER_to_BN(X509_get_serialNumber(cert), bn);
647 char *sn = BN_bn2hex(bn);
661 EPNAME(
"X509::GetExtension");
666 DEBUG(
"OID string not defined");
672 DEBUG(
"certificate is not initialized");
677 int numext = X509_get_ext_count(cert);
679 DEBUG(
"certificate has got no extensions");
682 DEBUG(
"certificate has "<<numext<<
" extensions");
686 int nid = OBJ_sn2nid(oid);
687 bool usenid = (nid > 0);
691#if OPENSSL_VERSION_NUMBER < 0x40000000L
692 X509_EXTENSION *wext = 0;
694 const X509_EXTENSION *wext = 0;
696 for (i = 0; i< numext; i++) {
697 wext = X509_get_ext(cert, i);
699 int enid = OBJ_obj2nid(X509_EXTENSION_get_object(wext));
705 OBJ_obj2txt(s,
sizeof(s), X509_EXTENSION_get_object(wext), 1);
715 DEBUG(
"Extension "<<oid<<
" not found");
731 DEBUG(
"serialization already performed:"
732 " return previous result ("<<bucket->size<<
" bytes)");
738 DEBUG(
"certificate is not initialized");
744 BIO *bmem = BIO_new(BIO_s_mem());
746 DEBUG(
"unable to create BIO for memory operations");
751 if (!PEM_write_bio_X509(bmem, cert)) {
752 DEBUG(
"unable to write certificate to memory BIO");
758 int blen = BIO_get_mem_data(bmem, &bdata);
759 DEBUG(
"BIO data: "<<blen<<
" bytes at 0x"<<(
int *)bdata);
765 bucket->SetBuf(bdata, blen);
766 DEBUG(
"result of serialization: "<<bucket->size<<
" bytes");
768 DEBUG(
"unable to create bucket for serialized format");
791 X509 *r = ref ? (X509 *)(ref->
Opaque()) : 0;
792 EVP_PKEY *rk = r ? X509_get_pubkey(r) : 0;
797 int rc = X509_verify(cert, rk);
802 DEBUG(
"signature not OK");
805 DEBUG(
"could not verify signature");
822 X509 *xpi = (X509 *)
Opaque();
826 PRINT(
"we are empty! Do nothing");
832#if OPENSSL_VERSION_NUMBER < 0x40000000L
833 X509_EXTENSION *xpiext = 0;
835 const X509_EXTENSION *xpiext = 0;
837 int npiext = X509_get_ext_count(xpi);
838 PRINT(
"found "<<npiext<<
" extensions ");
840 for (i = 0; i< npiext; i++) {
841 xpiext = X509_get_ext(xpi, i);
843 OBJ_obj2txt(s,
sizeof(s), X509_EXTENSION_get_object(xpiext), 1);
844 int crit = X509_EXTENSION_get_critical(xpiext);
846 PRINT(i <<
": found extension '"<<s<<
"', critical: " << crit);
849 const unsigned char *pp = ASN1_STRING_get0_data(X509_EXTENSION_get_data(xpiext));
850 long length = ASN1_STRING_length(X509_EXTENSION_get_data(xpiext));
851 int ret = FillUnknownExt(&pp, length, dumpunknown);
852 PRINT(
"ret: " << ret);
860int XrdCryptosslX509::FillUnknownExt(
const unsigned char **pp,
long length,
bool dump)
865 const unsigned char *p,*ep,*tot,*op,*opp;
867 int tag, xclass, ret = 0;
870 ASN1_OCTET_STRING *os = 0;
879 while ((p < tot) && (op < p)) {
881 j = ASN1_get_object(&p, &len, &tag, &xclass, length);
886 if (dump)
PRINT(
"ERROR: error in encoding");
894 if (j != (V_ASN1_CONSTRUCTED | 1)) {
895 if (dump)
PRINT(
"PRIM: d="<<depth<<
" hl="<<hl<<
" l="<<len);
897 if (dump)
PRINT(
"CONST: d="<<depth<<
" hl="<<hl<<
" l=inf ");
899 if (!Asn1PrintInfo(tag, xclass, j, (indent) ? depth : 0))
901 if (j & V_ASN1_CONSTRUCTED) {
903 if (dump)
PRINT(
" ");
905 if (dump)
PRINT(
"ERROR:CONST: length is greater than " <<length);
909 if ((j == 0x21) && (len == 0)) {
911 r = FillUnknownExt(&p, (
long)(tot-p), dump);
916 if ((r == 2) || (p >= tot))
921 r = FillUnknownExt(&p, (
long)len, dump);
928 }
else if (xclass != 0) {
930 if (dump)
PRINT(
" ");
933 if ((tag == V_ASN1_PRINTABLESTRING) ||
934 (tag == V_ASN1_T61STRING) ||
935 (tag == V_ASN1_IA5STRING) ||
936 (tag == V_ASN1_VISIBLESTRING) ||
937 (tag == V_ASN1_NUMERICSTRING) ||
938 (tag == V_ASN1_UTF8STRING) ||
939 (tag == V_ASN1_UTCTIME) ||
940 (tag == V_ASN1_GENERALIZEDTIME)) {
942 char *s =
new char[len + 1];
945 if (dump)
PRINT(
"GENERIC:" << s <<
" (len: "<<(
int)len<<
")");
948 if (dump)
PRINT(
"GENERIC: (len: "<<(
int)len<<
")");
950 }
else if (tag == V_ASN1_OBJECT) {
952 if (d2i_ASN1_OBJECT(&o, &opp, len+hl)) {
953 BIO *mem = BIO_new(BIO_s_mem());
954 i2a_ASN1_OBJECT(mem, o);
958 if (dump)
PRINT(
"ERROR:AOBJ: BAD OBJECT");
960 }
else if (tag == V_ASN1_BOOLEAN) {
962 if (dump)
PRINT(
"ERROR:BOOL: Bad boolean");
965 if (dump)
PRINT(
"BOOL:"<< p[0]);
966 }
else if (tag == V_ASN1_BMPSTRING) {
968 }
else if (tag == V_ASN1_OCTET_STRING) {
969 int i, printable = 1;
971 os = d2i_ASN1_OCTET_STRING(0, &opp, len + hl);
972 if (os && ASN1_STRING_length(os) > 0) {
973 opp = ASN1_STRING_get0_data(os);
975 for (i=0; i < ASN1_STRING_length(os); i++) {
976 if (( (opp[i] <
' ') && (opp[i] !=
'\n') &&
977 (opp[i] !=
'\r') && (opp[i] !=
'\t')) || (opp[i] >
'~')) {
984 char *s =
new char[ASN1_STRING_length(os) + 1];
985 memcpy(s, opp, ASN1_STRING_length(os));
986 s[ASN1_STRING_length(os)] = 0;
987 if (dump)
PRINT(
"OBJS:" << s <<
" (len: " << ASN1_STRING_length(os) <<
")");
991 if (!nl)
PRINT(
"OBJS:");
992 BIO *mem = BIO_new(BIO_s_mem());
993 if (BIO_dump_indent(mem, (
const char *)opp, ASN1_STRING_length(os), dump_indent) <= 0) {
994 if (dump)
PRINT(
"ERROR:OBJS: problems dumping to BIO");
1003 ASN1_OCTET_STRING_free(os);
1006 }
else if (tag == V_ASN1_INTEGER) {
1011 bs = d2i_ASN1_INTEGER(0, &opp, len+hl);
1013 if (dump)
PRINT(
"AINT:");
1014 if (ASN1_STRING_type(bs) == V_ASN1_NEG_INTEGER)
1015 if (dump)
PRINT(
"-");
1016 BIO *mem = BIO_new(BIO_s_mem());
1017 for (i = 0; i < ASN1_STRING_length(bs); i++) {
1018 if (BIO_printf(mem,
"%02X", ASN1_STRING_get0_data(bs)[i]) <= 0) {
1019 if (dump)
PRINT(
"ERROR:AINT: problems printf-ing to BIO");
1025 if (ASN1_STRING_length(bs) == 0)
PRINT(
"00");
1027 if (dump)
PRINT(
"ERROR:AINT: BAD INTEGER");
1029 ASN1_INTEGER_free(bs);
1030 }
else if (tag == V_ASN1_ENUMERATED) {
1031 ASN1_ENUMERATED *bs;
1035 bs = d2i_ASN1_ENUMERATED(0, &opp, len+hl);
1037 if (dump)
PRINT(
"AENU:");
1038 if (ASN1_STRING_type(bs) == V_ASN1_NEG_ENUMERATED)
1039 if (dump)
PRINT(
"-");
1040 BIO *mem = BIO_new(BIO_s_mem());
1041 for (i = 0; i < ASN1_STRING_length(bs); i++) {
1042 if (BIO_printf(mem,
"%02X", ASN1_STRING_get0_data(bs)[i]) <= 0) {
1043 if (dump)
PRINT(
"ERROR:AENU: problems printf-ing to BIO");
1049 if (ASN1_STRING_length(bs) == 0)
PRINT(
"00");
1051 if (dump)
PRINT(
"ERROR:AENU: BAD ENUMERATED");
1053 ASN1_ENUMERATED_free(bs);
1056 if (!nl && dump)
PRINT(
" ");
1059 if ((tag == V_ASN1_EOC) && (xclass == 0)) {
1068 if (o) ASN1_OBJECT_free(o);
1069 if (os) ASN1_OCTET_STRING_free(os);
1071 if (dump)
PRINT(
"ret: "<<ret);
1077int XrdCryptosslX509::Asn1PrintInfo(
int tag,
int xclass,
int constructed,
int indent)
1082 static const char fmt[]=
"%-18s";
1083 static const char fmt2[]=
"%2d %-15s";
1085 const char *p, *p2 = 0;
1087 BIO *bp = BIO_new(BIO_s_mem());
1088 if (constructed & V_ASN1_CONSTRUCTED)
1092 if (BIO_write(bp, p, 6) < 6)
1094 BIO_indent(bp, indent, 128);
1097 if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
1098 BIO_snprintf(str,
sizeof str,
"priv [ %d ] ",tag);
1099 else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
1100 BIO_snprintf(str,
sizeof str,
"cont [ %d ]",tag);
1101 else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
1102 BIO_snprintf(str,
sizeof str,
"appl [ %d ]",tag);
1104 BIO_snprintf(str,
sizeof str,
"<ASN1 %d>",tag);
1106 p = ASN1_tag2str(tag);
1109 if (BIO_printf(bp,fmt2,tag,p2) <= 0)
1112 if (BIO_printf(bp, fmt, p) <= 0)
1135 GENERAL_NAMES *gens =
static_cast<GENERAL_NAMES *
>(X509_get_ext_d2i(cert,
1136 NID_subject_alt_name, NULL, NULL));
1150 bool success =
false;
1151 for (
int idx = 0; idx < sk_GENERAL_NAME_num(gens); idx++) {
1154 gen = sk_GENERAL_NAME_value(gens, idx);
1155 if (gen->type != GEN_DNS)
1157 cstr = gen->d.dNSName;
1158 if (ASN1_STRING_type(cstr) != V_ASN1_IA5STRING)
1160 int san_fqdn_len = ASN1_STRING_length(cstr);
1161 if (san_fqdn_len > 255)
1163 memcpy(san_fqdn, ASN1_STRING_get0_data(cstr), san_fqdn_len);
1164 san_fqdn[san_fqdn_len] =
'\0';
1165 if (strlen(san_fqdn) !=
static_cast<size_t>(san_fqdn_len))
1167 DEBUG(
"Comparing SAN " << san_fqdn <<
" with " << fqdn);
1169 DEBUG(
"SAN " << san_fqdn <<
" matches with " << fqdn);
1174 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
time_t XrdCryptosslASN1toUTC(const ASN1_TIME *tsn1)
void XrdCryptosslNameOneLine(const X509_NAME *nm, XrdOucString &s)
int XrdCryptosslX509CheckProxy3(XrdCryptoX509 *, XrdOucString &)
int emsg(int rc, char *msg)
const char * SubjectHash()
virtual XrdCryptoX509data Opaque()
const char * IssuerHash()
static bool MatchHostnames(const char *match_pattern, const char *fqdn)
XrdCryptoX509data GetExtension(const char *oid)
int DumpExtensions(bool dumpunknown=0)
virtual ~XrdCryptosslX509()
XrdOucString SerialNumberString()
XrdCryptoX509data Opaque()
bool Verify(XrdCryptoX509 *ref)
virtual bool MatchesSAN(const char *, bool &)
XrdCryptosslX509(const char *cf, const char *kf=0)
void SetPKI(XrdCryptoX509data pki)