XRootD
XrdCryptosslX509Crl Class Reference

#include <XrdCryptosslX509Crl.hh>

+ Inheritance diagram for XrdCryptosslX509Crl:
+ Collaboration diagram for XrdCryptosslX509Crl:

Public Member Functions

 XrdCryptosslX509Crl (const char *crlf, int opt=0)
 
 XrdCryptosslX509Crl (FILE *, const char *crlf)
 
 XrdCryptosslX509Crl (XrdCryptoX509 *cacert)
 
virtual ~XrdCryptosslX509Crl ()
 
void Dump ()
 
bool hasCriticalExtension ()
 
bool IsRevoked (const char *sernum, int when=0)
 
bool IsRevoked (int serialnumber, int when=0)
 
const char * Issuer ()
 
const char * IssuerHash (int)
 
bool IsValid ()
 
time_t LastUpdate ()
 
time_t NextUpdate ()
 
XrdCryptoX509Crldata Opaque ()
 
const char * ParentFile ()
 
bool ToFile (FILE *fh)
 
bool Verify (XrdCryptoX509 *ref)
 
- Public Member Functions inherited from XrdCryptoX509Crl
 XrdCryptoX509Crl ()
 
virtual ~XrdCryptoX509Crl ()
 
virtual bool IsExpired (int when=0)
 
const char * IssuerHash ()
 

Detailed Description

Definition at line 50 of file XrdCryptosslX509Crl.hh.

Constructor & Destructor Documentation

◆ XrdCryptosslX509Crl() [1/3]

XrdCryptosslX509Crl::XrdCryptosslX509Crl ( const char *  crlf,
int  opt = 0 
)

Definition at line 51 of file XrdCryptosslX509Crl.cc.

53 {
54  // Constructor certificate from file 'cf'.
55  EPNAME("X509Crl::XrdCryptosslX509Crl_file");
56 
57  // Make sure file name is defined;
58  if (opt == 0) {
59  if (Init(cf) != 0) {
60  DEBUG("could not initialize the CRL from "<<cf);
61  return;
62  }
63  } else {
64  if (InitFromURI(cf, 0) != 0) {
65  DEBUG("could not initialize the CRL from URI"<<cf);
66  return;
67  }
68  }
69 }
#define DEBUG(x)
Definition: XrdBwmTrace.hh:54
#define EPNAME(x)
Definition: XrdBwmTrace.hh:56

References DEBUG, and EPNAME.

◆ XrdCryptosslX509Crl() [2/3]

XrdCryptosslX509Crl::XrdCryptosslX509Crl ( FILE *  fc,
const char *  crlf 
)

Definition at line 72 of file XrdCryptosslX509Crl.cc.

73 {
74  // Constructe CRL from a FILE handle `fc` with (assumed) filename `cf`.
75  EPNAME("X509Crl::XrdCryptosslX509Crl_file");
76 
77  if (Init(fc, cf)) {
78  DEBUG("could not initialize the CRL from " << cf);
79  return;
80  }
81 }

References DEBUG, and EPNAME.

◆ XrdCryptosslX509Crl() [3/3]

XrdCryptosslX509Crl::XrdCryptosslX509Crl ( XrdCryptoX509 cacert)

Definition at line 84 of file XrdCryptosslX509Crl.cc.

86 {
87  // Constructor certificate from CA certificate 'cacert'. This constructor
88  // extracts the information about the location of the CRL cerificate from the
89  // CA certificate extension 'crlDistributionPoints', downloads the file and
90  // loads it in the cache
91  EPNAME("X509Crl::XrdCryptosslX509Crl_CA");
92 
93  // The CA certificate must be defined
94  if (!cacert || cacert->type != XrdCryptoX509::kCA) {
95  DEBUG("the CA certificate is undefined or not CA! ("<<cacert<<")");
96  return;
97  }
98 
99  // Get the extension
100  X509_EXTENSION *crlext = (X509_EXTENSION *) cacert->GetExtension("crlDistributionPoints");
101  if (!crlext) {
102  DEBUG("extension 'crlDistributionPoints' not found in the CA certificate");
103  return;
104  }
105 
106  // Bio for exporting the extension
107  BIO *bext = BIO_new(BIO_s_mem());
108  ASN1_OBJECT *obj = X509_EXTENSION_get_object(crlext);
109  i2a_ASN1_OBJECT(bext, obj);
110  X509V3_EXT_print(bext, crlext, 0, 4);
111  // data length
112  char *cbio = 0;
113  int lbio = (int) BIO_get_mem_data(bext, &cbio);
114  char *buf = (char *) malloc(lbio+1);
115  // Read key from BIO to buf
116  memcpy(buf, cbio, lbio);
117  buf[lbio] = 0;
118  BIO_free(bext);
119  // Save it
120  XrdOucString uris(buf);
121  free(buf);
122 
123  DEBUG("URI string: "<< uris);
124 
125  XrdOucString uri;
126  int from = 0;
127  while ((from = uris.tokenize(uri, from, ' ')) != -1) {
128  if (uri.beginswith("URI:")) {
129  uri.replace("URI:","");
130  uri.replace("\n","");
131  if (InitFromURI(uri.c_str(), cacert->SubjectHash()) == 0) {
132  crluri = uri;
133  // We are done
134  break;
135  }
136  }
137  }
138 }
virtual XrdCryptoX509data GetExtension(const char *oid)
virtual const char * SubjectHash(int)
EX509Type type
const char * c_str() const
bool beginswith(char c)
int replace(const char *s1, const char *s2, int from=0, int to=-1)

References XrdOucString::beginswith(), XrdOucString::c_str(), DEBUG, EPNAME, XrdCryptoX509::GetExtension(), XrdCryptoX509::kCA, XrdOucString::replace(), XrdCryptoX509::SubjectHash(), XrdOucString::tokenize(), and XrdCryptoX509::type.

+ Here is the call graph for this function:

◆ ~XrdCryptosslX509Crl()

XrdCryptosslX509Crl::~XrdCryptosslX509Crl ( )
virtual

Definition at line 141 of file XrdCryptosslX509Crl.cc.

142 {
143  // Destructor
144 
145  // Cleanup CRL
146  if (crl)
147  X509_CRL_free(crl);
148 }

Member Function Documentation

◆ Dump()

void XrdCryptosslX509Crl::Dump ( )
virtual

Reimplemented from XrdCryptoX509Crl.

Definition at line 641 of file XrdCryptosslX509Crl.cc.

642 {
643  // Dump content
644  EPNAME("X509Crl::Dump");
645 
646  // Time strings
647  struct tm tst;
648  char stbeg[256] = {0};
649  time_t tbeg = LastUpdate();
650  localtime_r(&tbeg,&tst);
651  asctime_r(&tst,stbeg);
652  stbeg[strlen(stbeg)-1] = 0;
653  char stend[256] = {0};
654  time_t tend = NextUpdate();
655  localtime_r(&tend,&tst);
656  asctime_r(&tst,stend);
657  stend[strlen(stend)-1] = 0;
658 
659  PRINT("+++++++++++++++ X509 CRL dump +++++++++++++++++++++++");
660  PRINT("+");
661  PRINT("+ File: "<<ParentFile());
662  PRINT("+");
663  PRINT("+ Issuer: "<<Issuer());
664  PRINT("+ Issuer hash: "<<IssuerHash(0));
665  PRINT("+");
666  if (IsExpired()) {
667  PRINT("+ Validity: (expired!)");
668  } else {
669  PRINT("+ Validity:");
670  }
671  PRINT("+ LastUpdate: "<<tbeg<<" UTC - "<<stbeg);
672  PRINT("+ NextUpdate: "<<tend<<" UTC - "<<stend);
673  PRINT("+");
674  PRINT("+ Number of revoked certificates: "<<nrevoked);
675  PRINT("+");
676  PRINT("+++++++++++++++++++++++++++++++++++++++++++++++++");
677 }
#define PRINT(y)
virtual bool IsExpired(int when=0)
const char * IssuerHash()

References EPNAME, XrdCryptoX509Crl::IsExpired(), Issuer(), XrdCryptoX509Crl::IssuerHash(), LastUpdate(), NextUpdate(), ParentFile(), and PRINT.

+ Here is the call graph for this function:

◆ hasCriticalExtension()

bool XrdCryptosslX509Crl::hasCriticalExtension ( )

Definition at line 378 of file XrdCryptosslX509Crl.cc.

378  {
379  // If the X509_CRL_get_ext_by_critical() function returns -1, no critical extension
380  // has been found
381  return X509_CRL_get_ext_by_critical(crl,1,-1) != -1;
382 }

◆ IsRevoked() [1/2]

bool XrdCryptosslX509Crl::IsRevoked ( const char *  sernum,
int  when = 0 
)
virtual

Reimplemented from XrdCryptoX509Crl.

Definition at line 604 of file XrdCryptosslX509Crl.cc.

605 {
606  // Check if certificate with 'sernum' is in the
607  // list of revocated certificates
608  EPNAME("IsRevoked");
609 
610  // Reference time
611  int now = (when > 0) ? when : time(0);
612 
613  // Warn if CRL should be updated
614  if (now > NextUpdate()) {
615  DEBUG("WARNING: CRL is expired: you should download the updated one");
616  }
617 
618  // We must have something to check against
619  if (nrevoked <= 0) {
620  DEBUG("No certificate in the list");
621  return 0;
622  }
623 
624  // Look into the cache
625  XrdSutCacheEntry *cent = cache.Get((const char *)sernum);
626  if (cent && cent->status == kCE_ok) {
627  // Check the revocation time
628  if (now > cent->mtime) {
629  DEBUG("certificate "<<sernum<<" has been revoked");
630  cent->rwmtx.UnLock();
631  return 1;
632  }
633  cent->rwmtx.UnLock();
634  }
635 
636  // Certificate not revoked
637  return 0;
638 }
@ kCE_ok
XrdSysRWLock rwmtx
XrdSutCacheEntry * Get(const char *tag)
Definition: XrdSutCache.hh:54

References DEBUG, EPNAME, XrdSutCache::Get(), kCE_ok, XrdSutCacheEntry::mtime, NextUpdate(), XrdSutCacheEntry::rwmtx, XrdSutCacheEntry::status, and XrdSysRWLock::UnLock().

+ Here is the call graph for this function:

◆ IsRevoked() [2/2]

bool XrdCryptosslX509Crl::IsRevoked ( int  serialnumber,
int  when = 0 
)
virtual

Reimplemented from XrdCryptoX509Crl.

Definition at line 563 of file XrdCryptosslX509Crl.cc.

564 {
565  // Check if certificate with serialnumber is in the
566  // list of revocated certificates
567  EPNAME("IsRevoked");
568 
569  // Reference time
570  int now = (when > 0) ? when : time(0);
571 
572  // Warn if CRL should be updated
573  if (now > NextUpdate()) {
574  DEBUG("WARNING: CRL is expired: you should download the updated one");
575  }
576 
577  // We must have something to check against
578  if (nrevoked <= 0) {
579  DEBUG("No certificate in the list");
580  return 0;
581  }
582 
583  // Ok, build the tag
584  char tagser[20] = {0};
585  sprintf(tagser,"%x",serialnumber);
586 
587  // Look into the cache
588  XrdSutCacheEntry *cent = cache.Get((const char *)tagser);
589  if (cent && cent->status == kCE_ok) {
590  // Check the revocation time
591  if (now > cent->mtime) {
592  DEBUG("certificate "<<tagser<<" has been revoked");
593  cent->rwmtx.UnLock();
594  return 1;
595  }
596  cent->rwmtx.UnLock();
597  }
598 
599  // Certificate not revoked
600  return 0;
601 }

References DEBUG, EPNAME, XrdSutCache::Get(), kCE_ok, XrdSutCacheEntry::mtime, NextUpdate(), XrdSutCacheEntry::rwmtx, XrdSutCacheEntry::status, and XrdSysRWLock::UnLock().

+ Here is the call graph for this function:

◆ Issuer()

const char * XrdCryptosslX509Crl::Issuer ( )
virtual

Reimplemented from XrdCryptoX509Crl.

Definition at line 478 of file XrdCryptosslX509Crl.cc.

479 {
480  // Return issuer name
481  EPNAME("X509Crl::Issuer");
482 
483  // If we do not have it already, try extraction
484  if (issuer.length() <= 0) {
485 
486  // Make sure we have a CRL
487  if (!crl) {
488  DEBUG("WARNING: no CRL available - cannot extract issuer name");
489  return (const char *)0;
490  }
491 
492  // Extract issuer name
493  XrdCryptosslNameOneLine(X509_CRL_get_issuer(crl), issuer);
494  }
495 
496  // return what we have
497  return (issuer.length() > 0) ? issuer.c_str() : (const char *)0;
498 }
void XrdCryptosslNameOneLine(X509_NAME *nm, XrdOucString &s)
int length() const

References XrdOucString::c_str(), DEBUG, EPNAME, XrdOucString::length(), and XrdCryptosslNameOneLine().

Referenced by Dump().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ IssuerHash()

const char * XrdCryptosslX509Crl::IssuerHash ( int  alg)
virtual

Reimplemented from XrdCryptoX509Crl.

Definition at line 501 of file XrdCryptosslX509Crl.cc.

502 {
503  // Return hash of issuer name
504  // Use default algorithm (X509_NAME_hash) for alg = 0, old algorithm
505  // (for v>=1.0.0) when alg = 1
506  EPNAME("X509::IssuerHash");
507 
508  if (alg == 1) {
509  // md5 based
510  if (issueroldhash.length() <= 0) {
511  // Make sure we have a certificate
512  if (crl) {
513  char chash[30] = {0};
514  snprintf(chash, sizeof(chash),
515  "%08lx.0",X509_NAME_hash_old(X509_CRL_get_issuer(crl)));
516  issueroldhash = chash;
517  } else {
518  DEBUG("WARNING: no certificate available - cannot extract issuer hash (md5)");
519  }
520  }
521  // return what we have
522  return (issueroldhash.length() > 0) ? issueroldhash.c_str() : (const char *)0;
523  }
524 
525  // If we do not have it already, try extraction
526  if (issuerhash.length() <= 0) {
527 
528  // Make sure we have a certificate
529  if (crl) {
530  char chash[30] = {0};
531  snprintf(chash, sizeof(chash),
532  "%08lx.0",X509_NAME_hash(X509_CRL_get_issuer(crl)));
533  issuerhash = chash;
534  } else {
535  DEBUG("WARNING: no certificate available - cannot extract issuer hash (default)");
536  }
537  }
538 
539  // return what we have
540  return (issuerhash.length() > 0) ? issuerhash.c_str() : (const char *)0;
541 }

References XrdOucString::c_str(), DEBUG, EPNAME, and XrdOucString::length().

+ Here is the call graph for this function:

◆ IsValid()

bool XrdCryptosslX509Crl::IsValid ( )
inlinevirtual

Reimplemented from XrdCryptoX509Crl.

Definition at line 59 of file XrdCryptosslX509Crl.hh.

59 { return (crl != 0); }

◆ LastUpdate()

time_t XrdCryptosslX509Crl::LastUpdate ( )
virtual

Reimplemented from XrdCryptoX509Crl.

Definition at line 446 of file XrdCryptosslX509Crl.cc.

447 {
448  // Time of last update
449 
450  // If we do not have it already, try extraction
451  if (lastupdate < 0) {
452  // Make sure we have a CRL
453  if (crl)
454  // Extract UTC time in secs from Epoch
455  lastupdate = XrdCryptosslASN1toUTC(X509_CRL_get0_lastUpdate(crl));
456  }
457  // return what we have
458  return lastupdate;
459 }
time_t XrdCryptosslASN1toUTC(const ASN1_TIME *tsn1)

References XrdCryptosslASN1toUTC().

Referenced by Dump().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ NextUpdate()

time_t XrdCryptosslX509Crl::NextUpdate ( )
virtual

Reimplemented from XrdCryptoX509Crl.

Definition at line 462 of file XrdCryptosslX509Crl.cc.

463 {
464  // Time of next update
465 
466  // If we do not have it already, try extraction
467  if (nextupdate < 0) {
468  // Make sure we have a CRL
469  if (crl)
470  // Extract UTC time in secs from Epoch
471  nextupdate = XrdCryptosslASN1toUTC(X509_CRL_get0_nextUpdate(crl));
472  }
473  // return what we have
474  return nextupdate;
475 }

References XrdCryptosslASN1toUTC().

Referenced by Dump(), and IsRevoked().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Opaque()

XrdCryptoX509Crldata XrdCryptosslX509Crl::Opaque ( )
inlinevirtual

Reimplemented from XrdCryptoX509Crl.

Definition at line 62 of file XrdCryptosslX509Crl.hh.

62 { return (XrdCryptoX509Crldata)crl; }
void * XrdCryptoX509Crldata

◆ ParentFile()

const char* XrdCryptosslX509Crl::ParentFile ( )
inlinevirtual

Reimplemented from XrdCryptoX509Crl.

Definition at line 66 of file XrdCryptosslX509Crl.hh.

66 { return (const char *)(srcfile.c_str()); }

References XrdOucString::c_str().

Referenced by Dump().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ToFile()

bool XrdCryptosslX509Crl::ToFile ( FILE *  fh)

Definition at line 322 of file XrdCryptosslX509Crl.cc.

323 {
324  // Write the CRL's contents to a file in the PEM format.
325  EPNAME("ToFile");
326 
327  if (!crl) {
328  DEBUG("CRL object invalid; cannot write to a file");
329  return false;
330  }
331 
332  if (PEM_write_X509_CRL(fh, crl) == 0) {
333  DEBUG("Unable to write CRL to file");
334  return false;
335  }
336 
337  //
338  // Notify
339  DEBUG("CRL successfully written to file");
340 
341  return true;
342 }

References DEBUG, and EPNAME.

◆ Verify()

bool XrdCryptosslX509Crl::Verify ( XrdCryptoX509 ref)
virtual

Reimplemented from XrdCryptoX509Crl.

Definition at line 544 of file XrdCryptosslX509Crl.cc.

545 {
546  // Verify certificate signature with pub key of ref cert
547 
548  // We must have been initialized
549  if (!crl)
550  return 0;
551 
552  // We must have something to check with
553  X509 *r = ref ? (X509 *)(ref->Opaque()) : 0;
554  EVP_PKEY *rk = r ? X509_get_pubkey(r) : 0;
555  if (!rk)
556  return 0;
557 
558  // Ok: we can verify
559  return (X509_CRL_verify(crl, rk) > 0);
560 }
virtual XrdCryptoX509data Opaque()

References XrdCryptoX509::Opaque().

+ Here is the call graph for this function:

The documentation for this class was generated from the following files: