39 #include <openssl/pem.h>
46 #include <sys/types.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!"); \
63 const 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");
126 DEBUG(
"certificate successfully loaded");
144 int fd =
open(kf, O_RDONLY);
146 DEBUG(
"cannot open file "<<kf<<
" (errno: "<<errno<<
")");
149 if (
fstat(fd, &st) == -1) {
150 DEBUG(
"cannot stat private key file "<<kf<<
" (errno:"<<errno<<
")");
154 if (!S_ISREG(st.st_mode) || S_ISDIR(st.st_mode) ||
155 (st.st_mode & (S_IROTH | S_IWOTH)) != 0 ||
156 (st.st_mode & (S_IWGRP)) != 0) {
157 DEBUG(
"private key file "<<kf<<
" has wrong permissions "<<
158 (st.st_mode & 0777) <<
" (should be at most 0640)");
163 FILE *fk = fdopen(fd,
"r");
165 DEBUG(
"cannot open file "<<kf<<
" (errno: "<<errno<<
")");
170 if ((evpp = PEM_read_PrivateKey(fk,0,0,0))) {
171 DEBUG(
"RSA key completed ");
173 auto tmprsa = std::make_unique<XrdCryptosslRSA>(evpp, 1);
176 pki = tmprsa.release();
179 DEBUG(
"cannot read the key from file");
194 EPNAME(
"X509::XrdCryptosslX509_bio");
213 DEBUG(
"got undefined opaque buffer");
219 BIO *bmem = BIO_new(BIO_s_mem());
221 DEBUG(
"unable to create BIO for memory operations");
226 int nw = BIO_write(bmem,(
const void *)(buck->
buffer),buck->
size);
227 if (nw != buck->
size) {
228 DEBUG(
"problems writing data to memory BIO (nw: "<<nw<<
")");
233 if (!(cert = PEM_read_bio_X509(bmem,0,0,0))) {
234 DEBUG(
"unable to read certificate to memory BIO");
248 EVP_PKEY *evpp = X509_get_pubkey(cert);
255 DEBUG(
"could not access the public key");
263 EPNAME(
"X509::XrdCryptosslX509_x509");
282 DEBUG(
"got undefined X509 object");
296 EVP_PKEY *evpp = X509_get_pubkey(cert);
303 DEBUG(
"could not access the public key");
313 if (cert) X509_free(cert);
319 void XrdCryptosslX509::CertType()
327 PRINT(
"ERROR: certificate is not initialized");
335 int numext = X509_get_ext_count(cert);
337 DEBUG(
"certificate has got no extensions");
340 TRACE(ALL,
"certificate has "<<numext<<
" extensions");
344 X509_EXTENSION *ext = 0;
349 BASIC_CONSTRAINTS *bc = 0;
350 if ((bc = (BASIC_CONSTRAINTS *)X509_get_ext_d2i(cert, NID_basic_constraints, &crit, &idx)) &&
353 DEBUG(
"CA certificate");
356 if (bc) BASIC_CONSTRAINTS_free(bc);
364 if (issuer == common) {
371 if ((idx = X509_get_ext_by_NID(cert, NID_proxyCertInfo,-1)) == -1) {
377 DEBUG(
"Found GSI 3 proxyCertInfo extension");
378 }
else if (xcp == -1) {
382 if ((ext = X509_get_ext(cert,idx)) == 0) {
383 PRINT(
"ERROR: could not get proxyCertInfo extension");
389 if (X509_EXTENSION_get_critical(ext)) {
390 PROXY_CERT_INFO_EXTENSION *pci = (PROXY_CERT_INFO_EXTENSION *)X509V3_EXT_d2i(ext);
392 if ((pci->proxyPolicy) != 0) {
393 if ((pci->proxyPolicy->policyLanguage) != 0) {
397 DEBUG(
"Found RFC 382{0,1}compliant proxyCertInfo extension");
398 if (X509_get_ext_by_NID(cert, NID_proxyCertInfo, idx) != -1) {
399 PRINT(
"WARNING: multiple proxyCertInfo extensions found: taking the first");
402 PRINT(
"ERROR: accessing policy language from proxyCertInfo extension");
405 PRINT(
"ERROR: accessing policy from proxyCertInfo extension");
407 PROXY_CERT_INFO_EXTENSION_free(pci);
409 PRINT(
"ERROR: proxyCertInfo conversion error");
412 PRINT(
"ERROR: proxyCertInfo not flagged as critical");
415 if (!pxyname || done)
return;
419 if (lastcn ==
"proxy" || lastcn ==
"limited proxy") {
441 auto tmprsa = std::make_unique<XrdCryptosslRSA>((EVP_PKEY*)newpki, 1);
449 pki = tmprsa.release();
492 if (subject.
length() <= 0) {
496 DEBUG(
"WARNING: no certificate available - cannot extract subject name");
497 return (
const char *)0;
505 return (subject.
length() > 0) ? subject.
c_str() : (
const char *)0;
515 if (issuer.
length() <= 0) {
519 DEBUG(
"WARNING: no certificate available - cannot extract issuer name");
520 return (
const char *)0;
528 return (issuer.
length() > 0) ? issuer.
c_str() : (
const char *)0;
537 EPNAME(
"X509::IssuerHash");
541 if (issueroldhash.
length() <= 0) {
544 char chash[30] = {0};
545 snprintf(chash,
sizeof(chash),
546 "%08lx.0",X509_NAME_hash_old(X509_get_issuer_name(cert)));
547 issueroldhash = chash;
549 DEBUG(
"WARNING: no certificate available - cannot extract issuer hash (md5)");
553 return (issueroldhash.
length() > 0) ? issueroldhash.
c_str() : (
const char *)0;
557 if (issuerhash.
length() <= 0) {
561 char chash[30] = {0};
562 snprintf(chash,
sizeof(chash),
563 "%08lx.0",X509_NAME_hash(X509_get_issuer_name(cert)));
566 DEBUG(
"WARNING: no certificate available - cannot extract issuer hash (default)");
571 return (issuerhash.
length() > 0) ? issuerhash.
c_str() : (
const char *)0;
580 EPNAME(
"X509::SubjectHash");
584 if (subjectoldhash.
length() <= 0) {
587 char chash[30] = {0};
588 snprintf(chash,
sizeof(chash),
589 "%08lx.0",X509_NAME_hash_old(X509_get_subject_name(cert)));
590 subjectoldhash = chash;
592 DEBUG(
"WARNING: no certificate available - cannot extract subject hash (md5)");
596 return (subjectoldhash.
length() > 0) ? subjectoldhash.
c_str() : (
const char *)0;
600 if (subjecthash.
length() <= 0) {
604 char chash[30] = {0};
605 snprintf(chash,
sizeof(chash),
606 "%08lx.0",X509_NAME_hash(X509_get_subject_name(cert)));
609 DEBUG(
"WARNING: no certificate available - cannot extract subject hash (default)");
614 return (subjecthash.
length() > 0) ? subjecthash.
c_str() : (
const char *)0;
623 if (cert && X509_get_serialNumber(cert)) {
624 BIGNUM *bn = BN_new();
625 ASN1_INTEGER_to_BN(X509_get_serialNumber(cert), bn);
626 char *sn = BN_bn2dec(bn);
627 sernum = strtoll(sn, 0, 10);
641 if (cert && X509_get_serialNumber(cert)) {
642 BIGNUM *bn = BN_new();
643 ASN1_INTEGER_to_BN(X509_get_serialNumber(cert), bn);
644 char *sn = BN_bn2hex(bn);
658 EPNAME(
"X509::GetExtension");
663 DEBUG(
"OID string not defined");
669 DEBUG(
"certificate is not initialized");
674 int numext = X509_get_ext_count(cert);
676 DEBUG(
"certificate has got no extensions");
679 DEBUG(
"certificate has "<<numext<<
" extensions");
683 int nid = OBJ_sn2nid(oid);
684 bool usenid = (nid > 0);
688 X509_EXTENSION *wext = 0;
689 for (i = 0; i< numext; i++) {
690 wext = X509_get_ext(cert, i);
692 int enid = OBJ_obj2nid(X509_EXTENSION_get_object(wext));
698 OBJ_obj2txt(s,
sizeof(s), X509_EXTENSION_get_object(wext), 1);
708 DEBUG(
"Extension "<<oid<<
" not found");
724 DEBUG(
"serialization already performed:"
725 " return previous result ("<<bucket->
size<<
" bytes)");
731 DEBUG(
"certificate is not initialized");
737 BIO *bmem = BIO_new(BIO_s_mem());
739 DEBUG(
"unable to create BIO for memory operations");
744 if (!PEM_write_bio_X509(bmem, cert)) {
745 DEBUG(
"unable to write certificate to memory BIO");
751 int blen = BIO_get_mem_data(bmem, &bdata);
752 DEBUG(
"BIO data: "<<blen<<
" bytes at 0x"<<(
int *)bdata);
758 bucket->
SetBuf(bdata, blen);
759 DEBUG(
"result of serialization: "<<bucket->
size<<
" bytes");
761 DEBUG(
"unable to create bucket for serialized format");
784 X509 *r = ref ? (X509 *)(ref->
Opaque()) : 0;
785 EVP_PKEY *rk = r ? X509_get_pubkey(r) : 0;
790 int rc = X509_verify(cert, rk);
795 DEBUG(
"signature not OK");
798 DEBUG(
"could not verify signature");
815 X509 *xpi = (X509 *)
Opaque();
819 PRINT(
"we are empty! Do nothing");
825 X509_EXTENSION *xpiext = 0;
826 int npiext = X509_get_ext_count(xpi);
827 PRINT(
"found "<<npiext<<
" extensions ");
829 for (i = 0; i< npiext; i++) {
830 xpiext = X509_get_ext(xpi, i);
832 OBJ_obj2txt(s,
sizeof(s), X509_EXTENSION_get_object(xpiext), 1);
833 int crit = X509_EXTENSION_get_critical(xpiext);
835 PRINT(i <<
": found extension '"<<s<<
"', critical: " << crit);
838 const unsigned char *pp = (
const unsigned char *) X509_EXTENSION_get_data(xpiext)->data;
839 long length = X509_EXTENSION_get_data(xpiext)->length;
840 int ret = FillUnknownExt(&pp, length, dumpunknown);
841 PRINT(
"ret: " << ret);
849 int XrdCryptosslX509::FillUnknownExt(
const unsigned char **pp,
long length,
bool dump)
854 const unsigned char *p,*ep,*tot,*op,*opp;
856 int tag, xclass, ret = 0;
859 ASN1_OCTET_STRING *os = 0;
868 while ((p < tot) && (op < p)) {
870 j = ASN1_get_object(&p, &len, &tag, &xclass, length);
875 if (dump)
PRINT(
"ERROR: error in encoding");
883 if (j != (V_ASN1_CONSTRUCTED | 1)) {
884 if (dump)
PRINT(
"PRIM: d="<<depth<<
" hl="<<hl<<
" l="<<len);
886 if (dump)
PRINT(
"CONST: d="<<depth<<
" hl="<<hl<<
" l=inf ");
888 if (!Asn1PrintInfo(tag, xclass, j, (indent) ? depth : 0))
890 if (j & V_ASN1_CONSTRUCTED) {
892 if (dump)
PRINT(
" ");
894 if (dump)
PRINT(
"ERROR:CONST: length is greater than " <<length);
898 if ((j == 0x21) && (len == 0)) {
900 r = FillUnknownExt(&p, (
long)(tot-p), dump);
905 if ((r == 2) || (p >= tot))
910 r = FillUnknownExt(&p, (
long)len, dump);
917 }
else if (xclass != 0) {
919 if (dump)
PRINT(
" ");
922 if ((tag == V_ASN1_PRINTABLESTRING) ||
923 (tag == V_ASN1_T61STRING) ||
924 (tag == V_ASN1_IA5STRING) ||
925 (tag == V_ASN1_VISIBLESTRING) ||
926 (tag == V_ASN1_NUMERICSTRING) ||
927 (tag == V_ASN1_UTF8STRING) ||
928 (tag == V_ASN1_UTCTIME) ||
929 (tag == V_ASN1_GENERALIZEDTIME)) {
931 char *s =
new char[len + 1];
934 if (dump)
PRINT(
"GENERIC:" << s <<
" (len: "<<(
int)len<<
")");
937 if (dump)
PRINT(
"GENERIC: (len: "<<(
int)len<<
")");
939 }
else if (tag == V_ASN1_OBJECT) {
941 if (d2i_ASN1_OBJECT(&o, &opp, len+hl)) {
942 BIO *mem = BIO_new(BIO_s_mem());
943 i2a_ASN1_OBJECT(mem, o);
947 if (dump)
PRINT(
"ERROR:AOBJ: BAD OBJECT");
949 }
else if (tag == V_ASN1_BOOLEAN) {
951 if (dump)
PRINT(
"ERROR:BOOL: Bad boolean");
954 if (dump)
PRINT(
"BOOL:"<< p[0]);
955 }
else if (tag == V_ASN1_BMPSTRING) {
957 }
else if (tag == V_ASN1_OCTET_STRING) {
958 int i, printable = 1;
960 os = d2i_ASN1_OCTET_STRING(0, &opp, len + hl);
961 if (os && os->length > 0) {
964 for (i=0; i<os->length; i++) {
965 if (( (opp[i] <
' ') && (opp[i] !=
'\n') &&
966 (opp[i] !=
'\r') && (opp[i] !=
'\t')) || (opp[i] >
'~')) {
973 char *s =
new char[os->length + 1];
974 memcpy(s, opp, os->length);
976 if (dump)
PRINT(
"OBJS:" << s <<
" (len: "<<os->length<<
")");
980 if (!nl)
PRINT(
"OBJS:");
981 BIO *mem = BIO_new(BIO_s_mem());
982 if (BIO_dump_indent(mem, (
const char *)opp, os->length, dump_indent) <= 0) {
983 if (dump)
PRINT(
"ERROR:OBJS: problems dumping to BIO");
992 ASN1_OCTET_STRING_free(os);
995 }
else if (tag == V_ASN1_INTEGER) {
1000 bs = d2i_ASN1_INTEGER(0, &opp, len+hl);
1002 if (dump)
PRINT(
"AINT:");
1003 if (bs->type == V_ASN1_NEG_INTEGER)
1004 if (dump)
PRINT(
"-");
1005 BIO *mem = BIO_new(BIO_s_mem());
1006 for (i = 0; i < bs->length; i++) {
1007 if (BIO_printf(mem,
"%02X", bs->data[i]) <= 0) {
1008 if (dump)
PRINT(
"ERROR:AINT: problems printf-ing to BIO");
1014 if (bs->length == 0)
PRINT(
"00");
1016 if (dump)
PRINT(
"ERROR:AINT: BAD INTEGER");
1018 ASN1_INTEGER_free(bs);
1019 }
else if (tag == V_ASN1_ENUMERATED) {
1020 ASN1_ENUMERATED *bs;
1024 bs = d2i_ASN1_ENUMERATED(0, &opp, len+hl);
1026 if (dump)
PRINT(
"AENU:");
1027 if (bs->type == V_ASN1_NEG_ENUMERATED)
1028 if (dump)
PRINT(
"-");
1029 BIO *mem = BIO_new(BIO_s_mem());
1030 for (i = 0; i < bs->length; i++) {
1031 if (BIO_printf(mem,
"%02X", bs->data[i]) <= 0) {
1032 if (dump)
PRINT(
"ERROR:AENU: problems printf-ing to BIO");
1038 if (bs->length == 0)
PRINT(
"00");
1040 if (dump)
PRINT(
"ERROR:AENU: BAD ENUMERATED");
1042 ASN1_ENUMERATED_free(bs);
1045 if (!nl && dump)
PRINT(
" ");
1048 if ((tag == V_ASN1_EOC) && (xclass == 0)) {
1057 if (o) ASN1_OBJECT_free(o);
1058 if (os) ASN1_OCTET_STRING_free(os);
1060 if (dump)
PRINT(
"ret: "<<ret);
1066 int XrdCryptosslX509::Asn1PrintInfo(
int tag,
int xclass,
int constructed,
int indent)
1071 static const char fmt[]=
"%-18s";
1072 static const char fmt2[]=
"%2d %-15s";
1074 const char *p, *p2 = 0;
1076 BIO *bp = BIO_new(BIO_s_mem());
1077 if (constructed & V_ASN1_CONSTRUCTED)
1081 if (BIO_write(bp, p, 6) < 6)
1083 BIO_indent(bp, indent, 128);
1086 if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
1087 BIO_snprintf(str,
sizeof str,
"priv [ %d ] ",tag);
1088 else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
1089 BIO_snprintf(str,
sizeof str,
"cont [ %d ]",tag);
1090 else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
1091 BIO_snprintf(str,
sizeof str,
"appl [ %d ]",tag);
1093 BIO_snprintf(str,
sizeof str,
"<ASN1 %d>",tag);
1095 p = ASN1_tag2str(tag);
1098 if (BIO_printf(bp,fmt2,tag,p2) <= 0)
1101 if (BIO_printf(bp, fmt, p) <= 0)
1124 GENERAL_NAMES *gens =
static_cast<GENERAL_NAMES *
>(X509_get_ext_d2i(cert,
1125 NID_subject_alt_name, NULL, NULL));
1139 bool success =
false;
1140 for (
int idx = 0; idx < sk_GENERAL_NAME_num(gens); idx++) {
1143 gen = sk_GENERAL_NAME_value(gens, idx);
1144 if (gen->type != GEN_DNS)
1146 cstr = gen->d.dNSName;
1147 if (ASN1_STRING_type(cstr) != V_ASN1_IA5STRING)
1149 int san_fqdn_len = ASN1_STRING_length(cstr);
1150 if (san_fqdn_len > 255)
1152 memcpy(san_fqdn, ASN1_STRING_get0_data(cstr), san_fqdn_len);
1153 san_fqdn[san_fqdn_len] =
'\0';
1154 if (strlen(san_fqdn) !=
static_cast<size_t>(san_fqdn_len))
1156 DEBUG(
"Comparing SAN " << san_fqdn <<
" with " << fqdn);
1158 DEBUG(
"SAN " << san_fqdn <<
" matches with " << fqdn);
1163 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 * IssuerHash()
virtual XrdCryptoX509data Opaque()
const char * SubjectHash()
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)
const char * c_str() const
int rfind(const char c, int start=STR_NPOS)
int SetBuf(const char *nb=0, int ns=0)