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 X509_EXTENSION *ext = 0;
348 BASIC_CONSTRAINTS *bc = 0;
349 if ((bc = (BASIC_CONSTRAINTS *)X509_get_ext_d2i(cert, NID_basic_constraints, &crit, &idx)) &&
352 DEBUG(
"CA certificate");
355 if (bc) BASIC_CONSTRAINTS_free(bc);
361 XrdOucString common(subject, 0, subject.rfind(
"/CN=") - 1);
363 if (issuer == common) {
370 if ((idx = X509_get_ext_by_NID(cert, NID_proxyCertInfo,-1)) == -1) {
376 DEBUG(
"Found GSI 3 proxyCertInfo extension");
377 }
else if (xcp == -1) {
381 if ((ext = X509_get_ext(cert,idx)) == 0) {
382 PRINT(
"ERROR: could not get proxyCertInfo extension");
388 if (X509_EXTENSION_get_critical(ext)) {
389 PROXY_CERT_INFO_EXTENSION *pci = (PROXY_CERT_INFO_EXTENSION *)X509V3_EXT_d2i(ext);
391 if ((pci->proxyPolicy) != 0) {
392 if ((pci->proxyPolicy->policyLanguage) != 0) {
396 DEBUG(
"Found RFC 382{0,1}compliant proxyCertInfo extension");
397 if (X509_get_ext_by_NID(cert, NID_proxyCertInfo, idx) != -1) {
398 PRINT(
"WARNING: multiple proxyCertInfo extensions found: taking the first");
401 PRINT(
"ERROR: accessing policy language from proxyCertInfo extension");
404 PRINT(
"ERROR: accessing policy from proxyCertInfo extension");
406 PROXY_CERT_INFO_EXTENSION_free(pci);
408 PRINT(
"ERROR: proxyCertInfo conversion error");
411 PRINT(
"ERROR: proxyCertInfo not flagged as critical");
414 if (!pxyname || done)
return;
417 XrdOucString lastcn(subject, subject.rfind(
"/CN=") + 4, -1);
418 if (lastcn ==
"proxy" || lastcn ==
"limited proxy") {
440 auto tmprsa = std::make_unique<XrdCryptosslRSA>((EVP_PKEY*)newpki, 1);
448 pki = tmprsa.release();
491 if (subject.length() <= 0) {
495 DEBUG(
"WARNING: no certificate available - cannot extract subject name");
496 return (
const char *)0;
504 return (subject.length() > 0) ? subject.c_str() : (
const char *)0;
514 if (issuer.length() <= 0) {
518 DEBUG(
"WARNING: no certificate available - cannot extract issuer name");
519 return (
const char *)0;
527 return (issuer.length() > 0) ? issuer.c_str() : (
const char *)0;
536 EPNAME(
"X509::IssuerHash");
540 if (issueroldhash.length() <= 0) {
543 char chash[30] = {0};
544 snprintf(chash,
sizeof(chash),
545 "%08lx.0",X509_NAME_hash_old(X509_get_issuer_name(cert)));
546 issueroldhash = chash;
548 DEBUG(
"WARNING: no certificate available - cannot extract issuer hash (md5)");
552 return (issueroldhash.length() > 0) ? issueroldhash.c_str() : (
const char *)0;
556 if (issuerhash.length() <= 0) {
560 char chash[30] = {0};
561 snprintf(chash,
sizeof(chash),
562 "%08lx.0",X509_NAME_hash(X509_get_issuer_name(cert)));
565 DEBUG(
"WARNING: no certificate available - cannot extract issuer hash (default)");
570 return (issuerhash.length() > 0) ? issuerhash.c_str() : (
const char *)0;
579 EPNAME(
"X509::SubjectHash");
583 if (subjectoldhash.length() <= 0) {
586 char chash[30] = {0};
587 snprintf(chash,
sizeof(chash),
588 "%08lx.0",X509_NAME_hash_old(X509_get_subject_name(cert)));
589 subjectoldhash = chash;
591 DEBUG(
"WARNING: no certificate available - cannot extract subject hash (md5)");
595 return (subjectoldhash.length() > 0) ? subjectoldhash.c_str() : (
const char *)0;
599 if (subjecthash.length() <= 0) {
603 char chash[30] = {0};
604 snprintf(chash,
sizeof(chash),
605 "%08lx.0",X509_NAME_hash(X509_get_subject_name(cert)));
608 DEBUG(
"WARNING: no certificate available - cannot extract subject hash (default)");
613 return (subjecthash.length() > 0) ? subjecthash.c_str() : (
const char *)0;
622 if (cert && X509_get_serialNumber(cert)) {
623 BIGNUM *bn = BN_new();
624 ASN1_INTEGER_to_BN(X509_get_serialNumber(cert), bn);
625 char *sn = BN_bn2dec(bn);
626 sernum = strtoll(sn, 0, 10);
640 if (cert && X509_get_serialNumber(cert)) {
641 BIGNUM *bn = BN_new();
642 ASN1_INTEGER_to_BN(X509_get_serialNumber(cert), bn);
643 char *sn = BN_bn2hex(bn);
657 EPNAME(
"X509::GetExtension");
662 DEBUG(
"OID string not defined");
668 DEBUG(
"certificate is not initialized");
673 int numext = X509_get_ext_count(cert);
675 DEBUG(
"certificate has got no extensions");
678 DEBUG(
"certificate has "<<numext<<
" extensions");
682 int nid = OBJ_sn2nid(oid);
683 bool usenid = (nid > 0);
687 X509_EXTENSION *wext = 0;
688 for (i = 0; i< numext; i++) {
689 wext = X509_get_ext(cert, i);
691 int enid = OBJ_obj2nid(X509_EXTENSION_get_object(wext));
697 OBJ_obj2txt(s,
sizeof(s), X509_EXTENSION_get_object(wext), 1);
707 DEBUG(
"Extension "<<oid<<
" not found");
723 DEBUG(
"serialization already performed:"
724 " return previous result ("<<bucket->size<<
" bytes)");
730 DEBUG(
"certificate is not initialized");
736 BIO *bmem = BIO_new(BIO_s_mem());
738 DEBUG(
"unable to create BIO for memory operations");
743 if (!PEM_write_bio_X509(bmem, cert)) {
744 DEBUG(
"unable to write certificate to memory BIO");
750 int blen = BIO_get_mem_data(bmem, &bdata);
751 DEBUG(
"BIO data: "<<blen<<
" bytes at 0x"<<(
int *)bdata);
757 bucket->SetBuf(bdata, blen);
758 DEBUG(
"result of serialization: "<<bucket->size<<
" bytes");
760 DEBUG(
"unable to create bucket for serialized format");
783 X509 *r = ref ? (X509 *)(ref->
Opaque()) : 0;
784 EVP_PKEY *rk = r ? X509_get_pubkey(r) : 0;
789 int rc = X509_verify(cert, rk);
794 DEBUG(
"signature not OK");
797 DEBUG(
"could not verify signature");
814 X509 *xpi = (X509 *)
Opaque();
818 PRINT(
"we are empty! Do nothing");
824 X509_EXTENSION *xpiext = 0;
825 int npiext = X509_get_ext_count(xpi);
826 PRINT(
"found "<<npiext<<
" extensions ");
828 for (i = 0; i< npiext; i++) {
829 xpiext = X509_get_ext(xpi, i);
831 OBJ_obj2txt(s,
sizeof(s), X509_EXTENSION_get_object(xpiext), 1);
832 int crit = X509_EXTENSION_get_critical(xpiext);
834 PRINT(i <<
": found extension '"<<s<<
"', critical: " << crit);
837 const unsigned char *pp = (
const unsigned char *) X509_EXTENSION_get_data(xpiext)->data;
838 long length = X509_EXTENSION_get_data(xpiext)->length;
839 int ret = FillUnknownExt(&pp, length, dumpunknown);
840 PRINT(
"ret: " << ret);
848int XrdCryptosslX509::FillUnknownExt(
const unsigned char **pp,
long length,
bool dump)
853 const unsigned char *p,*ep,*tot,*op,*opp;
855 int tag, xclass, ret = 0;
858 ASN1_OCTET_STRING *os = 0;
867 while ((p < tot) && (op < p)) {
869 j = ASN1_get_object(&p, &len, &tag, &xclass, length);
874 if (dump)
PRINT(
"ERROR: error in encoding");
882 if (j != (V_ASN1_CONSTRUCTED | 1)) {
883 if (dump)
PRINT(
"PRIM: d="<<depth<<
" hl="<<hl<<
" l="<<len);
885 if (dump)
PRINT(
"CONST: d="<<depth<<
" hl="<<hl<<
" l=inf ");
887 if (!Asn1PrintInfo(tag, xclass, j, (indent) ? depth : 0))
889 if (j & V_ASN1_CONSTRUCTED) {
891 if (dump)
PRINT(
" ");
893 if (dump)
PRINT(
"ERROR:CONST: length is greater than " <<length);
897 if ((j == 0x21) && (len == 0)) {
899 r = FillUnknownExt(&p, (
long)(tot-p), dump);
904 if ((r == 2) || (p >= tot))
909 r = FillUnknownExt(&p, (
long)len, dump);
916 }
else if (xclass != 0) {
918 if (dump)
PRINT(
" ");
921 if ((tag == V_ASN1_PRINTABLESTRING) ||
922 (tag == V_ASN1_T61STRING) ||
923 (tag == V_ASN1_IA5STRING) ||
924 (tag == V_ASN1_VISIBLESTRING) ||
925 (tag == V_ASN1_NUMERICSTRING) ||
926 (tag == V_ASN1_UTF8STRING) ||
927 (tag == V_ASN1_UTCTIME) ||
928 (tag == V_ASN1_GENERALIZEDTIME)) {
930 char *s =
new char[len + 1];
933 if (dump)
PRINT(
"GENERIC:" << s <<
" (len: "<<(
int)len<<
")");
936 if (dump)
PRINT(
"GENERIC: (len: "<<(
int)len<<
")");
938 }
else if (tag == V_ASN1_OBJECT) {
940 if (d2i_ASN1_OBJECT(&o, &opp, len+hl)) {
941 BIO *mem = BIO_new(BIO_s_mem());
942 i2a_ASN1_OBJECT(mem, o);
946 if (dump)
PRINT(
"ERROR:AOBJ: BAD OBJECT");
948 }
else if (tag == V_ASN1_BOOLEAN) {
950 if (dump)
PRINT(
"ERROR:BOOL: Bad boolean");
953 if (dump)
PRINT(
"BOOL:"<< p[0]);
954 }
else if (tag == V_ASN1_BMPSTRING) {
956 }
else if (tag == V_ASN1_OCTET_STRING) {
957 int i, printable = 1;
959 os = d2i_ASN1_OCTET_STRING(0, &opp, len + hl);
960 if (os && os->length > 0) {
963 for (i=0; i<os->length; i++) {
964 if (( (opp[i] <
' ') && (opp[i] !=
'\n') &&
965 (opp[i] !=
'\r') && (opp[i] !=
'\t')) || (opp[i] >
'~')) {
972 char *s =
new char[os->length + 1];
973 memcpy(s, opp, os->length);
975 if (dump)
PRINT(
"OBJS:" << s <<
" (len: "<<os->length<<
")");
979 if (!nl)
PRINT(
"OBJS:");
980 BIO *mem = BIO_new(BIO_s_mem());
981 if (BIO_dump_indent(mem, (
const char *)opp, os->length, dump_indent) <= 0) {
982 if (dump)
PRINT(
"ERROR:OBJS: problems dumping to BIO");
991 ASN1_OCTET_STRING_free(os);
994 }
else if (tag == V_ASN1_INTEGER) {
999 bs = d2i_ASN1_INTEGER(0, &opp, len+hl);
1001 if (dump)
PRINT(
"AINT:");
1002 if (bs->type == V_ASN1_NEG_INTEGER)
1003 if (dump)
PRINT(
"-");
1004 BIO *mem = BIO_new(BIO_s_mem());
1005 for (i = 0; i < bs->length; i++) {
1006 if (BIO_printf(mem,
"%02X", bs->data[i]) <= 0) {
1007 if (dump)
PRINT(
"ERROR:AINT: problems printf-ing to BIO");
1013 if (bs->length == 0)
PRINT(
"00");
1015 if (dump)
PRINT(
"ERROR:AINT: BAD INTEGER");
1017 ASN1_INTEGER_free(bs);
1018 }
else if (tag == V_ASN1_ENUMERATED) {
1019 ASN1_ENUMERATED *bs;
1023 bs = d2i_ASN1_ENUMERATED(0, &opp, len+hl);
1025 if (dump)
PRINT(
"AENU:");
1026 if (bs->type == V_ASN1_NEG_ENUMERATED)
1027 if (dump)
PRINT(
"-");
1028 BIO *mem = BIO_new(BIO_s_mem());
1029 for (i = 0; i < bs->length; i++) {
1030 if (BIO_printf(mem,
"%02X", bs->data[i]) <= 0) {
1031 if (dump)
PRINT(
"ERROR:AENU: problems printf-ing to BIO");
1037 if (bs->length == 0)
PRINT(
"00");
1039 if (dump)
PRINT(
"ERROR:AENU: BAD ENUMERATED");
1041 ASN1_ENUMERATED_free(bs);
1044 if (!nl && dump)
PRINT(
" ");
1047 if ((tag == V_ASN1_EOC) && (xclass == 0)) {
1056 if (o) ASN1_OBJECT_free(o);
1057 if (os) ASN1_OCTET_STRING_free(os);
1059 if (dump)
PRINT(
"ret: "<<ret);
1065int XrdCryptosslX509::Asn1PrintInfo(
int tag,
int xclass,
int constructed,
int indent)
1070 static const char fmt[]=
"%-18s";
1071 static const char fmt2[]=
"%2d %-15s";
1073 const char *p, *p2 = 0;
1075 BIO *bp = BIO_new(BIO_s_mem());
1076 if (constructed & V_ASN1_CONSTRUCTED)
1080 if (BIO_write(bp, p, 6) < 6)
1082 BIO_indent(bp, indent, 128);
1085 if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
1086 BIO_snprintf(str,
sizeof str,
"priv [ %d ] ",tag);
1087 else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
1088 BIO_snprintf(str,
sizeof str,
"cont [ %d ]",tag);
1089 else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
1090 BIO_snprintf(str,
sizeof str,
"appl [ %d ]",tag);
1092 BIO_snprintf(str,
sizeof str,
"<ASN1 %d>",tag);
1094 p = ASN1_tag2str(tag);
1097 if (BIO_printf(bp,fmt2,tag,p2) <= 0)
1100 if (BIO_printf(bp, fmt, p) <= 0)
1123 GENERAL_NAMES *gens =
static_cast<GENERAL_NAMES *
>(X509_get_ext_d2i(cert,
1124 NID_subject_alt_name, NULL, NULL));
1138 bool success =
false;
1139 for (
int idx = 0; idx < sk_GENERAL_NAME_num(gens); idx++) {
1142 gen = sk_GENERAL_NAME_value(gens, idx);
1143 if (gen->type != GEN_DNS)
1145 cstr = gen->d.dNSName;
1146 if (ASN1_STRING_type(cstr) != V_ASN1_IA5STRING)
1148 int san_fqdn_len = ASN1_STRING_length(cstr);
1149 if (san_fqdn_len > 255)
1151 memcpy(san_fqdn, ASN1_STRING_get0_data(cstr), san_fqdn_len);
1152 san_fqdn[san_fqdn_len] =
'\0';
1153 if (strlen(san_fqdn) !=
static_cast<size_t>(san_fqdn_len))
1155 DEBUG(
"Comparing SAN " << san_fqdn <<
" with " << fqdn);
1157 DEBUG(
"SAN " << san_fqdn <<
" matches with " << fqdn);
1162 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
void XrdCryptosslNameOneLine(X509_NAME *nm, XrdOucString &s)
time_t XrdCryptosslASN1toUTC(const ASN1_TIME *tsn1)
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)