XRootD
XrdTlsContext Class Reference

#include <XrdTlsContext.hh>

+ Collaboration diagram for XrdTlsContext:

Classes

struct  CTX_Params
 

Public Member Functions

 XrdTlsContext (const char *cert=0, const char *key=0, const char *cadir=0, const char *cafile=0, uint64_t opts=0, std::string *eMsg=0)
 
 XrdTlsContext (const XrdTlsContext &ctx)=delete
 Disallow any copies of this object. More...
 
 XrdTlsContext (XrdTlsContext &&ctx)=delete
 
 ~XrdTlsContext ()
 Destructor. More...
 
XrdTlsContextClone (bool full=true, bool startCRLRefresh=false)
 
void * Context ()
 
const CTX_ParamsGetParams ()
 
bool isOK ()
 
bool newHostCertificateDetected ()
 
XrdTlsContextoperator= (const XrdTlsContext &ctx)=delete
 
XrdTlsContextoperator= (XrdTlsContext &&ctx)=delete
 
void * Session ()
 
int SessionCache (int opts=scNone, const char *id=0, int idlen=0)
 
bool SetContextCiphers (const char *ciphers)
 
bool SetCrlRefresh (int refsec=-1)
 
void SetTlsClientAuth (bool setting)
 
bool x509Verify ()
 

Static Public Member Functions

static const char * Init ()
 
static void SetDefaultCiphers (const char *ciphers)
 

Static Public Attributes

static const uint64_t artON = 0x0000002000000000
 Auto retry Handshake. More...
 
static const uint64_t clcOF = 0x0000010000000000
 Disable client certificate request. More...
 
static const uint64_t crlAM = 0x0000001000000000
 Allow CA validation when CRL is missing (CRL soft-fail) More...
 
static const uint64_t crlFC = 0x000000C000000000
 Full crl chain checking. More...
 
static const uint64_t crlON = 0x0000008000000000
 Enables crl checking. More...
 
static const uint64_t crlRF = 0x00000000ffff0000
 Mask to isolate crl refresh in min. More...
 
static const int crlRS = 16
 Bits to shift vdept. More...
 
static int ctxIndex = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL)
 
static const int DEFAULT_CRL_REF_INT_SEC = 8 * 60 * 60
 Default CRL refresh interval in seconds. More...
 
static const uint64_t dnsok = 0x0000000200000000
 Trust DNS for host name. More...
 
static const uint64_t hsto = 0x00000000000000ff
 Mask to isolate the hsto. More...
 
static const uint64_t logVF = 0x0000000800000000
 Log verify failures. More...
 
static const uint64_t nopxy = 0x0000000100000000
 Do not allow proxy certs. More...
 
static const uint64_t rfCRL = 0x0000004000000000
 Turn on the CRL refresh thread. More...
 
static const int scClnt = 0x00040000
 Turn on cache client mode. More...
 
static const int scFMax = 0x00007fff
 
static const int scIdErr = 0x80000000
 Info: Id not set, is too long. More...
 
static const int scKeep = 0x40000000
 Info: TLS-controlled flush disabled. More...
 
static const int scNone = 0x00000000
 Do not change any option settings. More...
 
static const int scOff = 0x00010000
 Turn off cache. More...
 
static const int scSrvr = 0x00020000
 Turn on cache server mode (default) More...
 
static const uint64_t servr = 0x0000000400000000
 This is a server context. More...
 
static const int vdepS = 8
 Bits to shift vdept. More...
 
static const uint64_t vdept = 0x000000000000ff00
 Mask to isolate vdept. More...
 

Detailed Description

Definition at line 36 of file XrdTlsContext.hh.

Constructor & Destructor Documentation

◆ XrdTlsContext() [1/3]

XrdTlsContext::XrdTlsContext ( const char *  cert = 0,
const char *  key = 0,
const char *  cadir = 0,
const char *  cafile = 0,
uint64_t  opts = 0,
std::string *  eMsg = 0 
)

Definition at line 710 of file XrdTlsContext.cc.

713  : pImpl( new XrdTlsContextImpl(this) )
714 {
715  class ctx_helper
716  {public:
717 
718  void Keep() {ctxLoc = 0;}
719 
720  ctx_helper(SSL_CTX **ctxP) : ctxLoc(ctxP) {}
721  ~ctx_helper() {if (ctxLoc && *ctxLoc)
722  {SSL_CTX_free(*ctxLoc); *ctxLoc = 0;}
723  }
724  private:
725  SSL_CTX **ctxLoc;
726  } ctx_tracker(&pImpl->ctx);
727 
728  static const uint64_t sslOpts = SSL_OP_ALL
729  | SSL_OP_NO_SSLv2
730  | SSL_OP_NO_SSLv3
731  | SSL_OP_NO_COMPRESSION
732 #ifdef SSL_OP_IGNORE_UNEXPECTED_EOF
733  | SSL_OP_IGNORE_UNEXPECTED_EOF
734 #endif
735 #if OPENSSL_VERSION_NUMBER >= 0x10101000L
736  | SSL_OP_NO_RENEGOTIATION
737 #endif
738  ;
739 
740  std::string certFN, eText;
741  const char *emsg;
742 
743 // Assume we will fail
744 //
745  pImpl->ctx = 0;
746 
747 // Verify that initialzation has occurred. This is not heavy weight as
748 // there will usually be no more than two instances of this object.
749 //
750  if (!initDbgDone)
751  {XrdSysMutexHelper dbgHelper(dbgMutex);
752  if (!initDbgDone)
753  {const char *dbg;
754  if (!(opts & servr) && (dbg = getenv("XRDTLS_DEBUG")))
755  {int dbgOpts = 0;
756  if (strstr(dbg, "ctx")) dbgOpts |= XrdTls::dbgCTX;
757  if (strstr(dbg, "sok")) dbgOpts |= XrdTls::dbgSOK;
758  if (strstr(dbg, "sio")) dbgOpts |= XrdTls::dbgSIO;
759  if (!dbgOpts) dbgOpts = XrdTls::dbgALL;
761  }
762  if ((emsg = Init())) FATAL(emsg);
763  initDbgDone = true;
764  }
765  }
766 
767 // If no CA cert information is specified and this is not a server context,
768 // then get the paths from the environment. They must exist as we need to
769 // verify peer certs in order to verify target host names client-side. We
770 // also use this setupt to see if we should use a specific cert and key.
771 //
772  if (!(opts & servr))
773  {if (!caDir && !caFile)
774  {caDir = getenv("X509_CERT_DIR");
775  caFile = getenv("X509_CERT_FILE");
776  if (!caDir && !caFile)
777  FATAL("No CA cert specified; host identity cannot be verified.");
778  }
779  if (!key) key = getenv("X509_USER_KEY");
780  if (!cert) cert = getenv("X509_USER_PROXY");
781  if (!cert)
782  {struct stat Stat;
783  long long int uid = static_cast<long long int>(getuid());
784  certFN = std::string("/tmp/x509up_u") + std::to_string(uid);
785  if (!stat(certFN.c_str(), &Stat)) cert = certFN.c_str();
786  }
787  }
788 
789 // Before we try to use any specified files, make sure they exist, are of
790 // the right type and do not have excessive access privileges.
791 // .a
792  if (!VerPaths(cert, key, caDir, caFile, eText)) FATAL( eText.c_str());
793 
794 // Copy parameters to out parm structure.
795 //
796  if (cert) {
797  pImpl->Parm.cert = cert;
798  //This call should not fail as a stat is already performed in the call of VerPaths() above
800  }
801  if (key) pImpl->Parm.pkey = key;
802  if (caDir) pImpl->Parm.cadir = caDir;
803  if (caFile) pImpl->Parm.cafile = caFile;
804  pImpl->Parm.opts = opts;
805  if (opts & crlRF) {
806  // What we store in crlRF is the time in minutes, convert it back to seconds
807  pImpl->Parm.crlRT = static_cast<int>((opts & crlRF) >> crlRS) * 60;
808  }
809 
810 // Get the correct method to use for TLS and check if successful create a
811 // server context that uses the method.
812 //
813  const SSL_METHOD *meth;
814  emsg = GetTlsMethod(meth);
815  if (emsg) FATAL(emsg);
816 
817  pImpl->ctx = SSL_CTX_new(meth);
818 
819 // Make sure we have a context here
820 //
821  if (pImpl->ctx == 0) FATAL_SSL("Unable to allocate TLS context!");
822 
823  //Add the XrdTlsContext object as extra information for OpenSSL callback re-use
824  SSL_CTX_set_ex_data(pImpl->ctx, ctxIndex, this);
825 
826 // Always prohibit SSLv2 & SSLv3 as these are not secure.
827 //
828  SSL_CTX_set_options(pImpl->ctx, sslOpts);
829 
830 // Handle session re-negotiation automatically
831 //
832 // SSL_CTX_set_mode(pImpl->ctx, sslMode);
833 
834 // Turn off the session cache as it's useless with peer cert chains
835 //
836  SSL_CTX_set_session_cache_mode(pImpl->ctx, SSL_SESS_CACHE_OFF);
837 
838 // Establish the CA cert locations, if specified. Then set the verification
839 // depth and turn on peer cert validation. For now, we don't set a callback.
840 // In the future we may to grab debugging information.
841 //
842  if ((caDir || caFile) && !(opts & clcOF))
843  {if (!SSL_CTX_load_verify_locations(pImpl->ctx, caFile, caDir))
844  FATAL_SSL("Unable to load the CA cert file or directory.");
845 
846  int vDepth = (opts & vdept) >> vdepS;
847  SSL_CTX_set_verify_depth(pImpl->ctx, (vDepth ? vDepth : 9));
848 
849  bool LogVF = (opts & logVF) != 0;
850  bool crlAllowMissingCA = (opts & crlAM) != 0;
851 
852  if (crlAllowMissingCA || LogVF) {
853  SSL_CTX_set_verify(pImpl->ctx, SSL_VERIFY_PEER, verifyPeerCB);
854  } else {
855  SSL_CTX_set_verify(pImpl->ctx, SSL_VERIFY_PEER, 0);
856  }
857 
858  unsigned long xFlags = (opts & nopxy ? 0 : X509_V_FLAG_ALLOW_PROXY_CERTS);
859  if (opts & crlON)
860  {xFlags |= X509_V_FLAG_CRL_CHECK;
861  if (opts & crlFC) xFlags |= X509_V_FLAG_CRL_CHECK_ALL;
862  }
863  if (opts) X509_STORE_set_flags(SSL_CTX_get_cert_store(pImpl->ctx),xFlags);
864  } else {
865  SSL_CTX_set_verify(pImpl->ctx, SSL_VERIFY_NONE, 0);
866  }
867 
868 // Set cipher list
869 //
870  if (!SSL_CTX_set_cipher_list(pImpl->ctx, sslCiphers))
871  FATAL_SSL("Unable to set SSL cipher list; no supported ciphers.");
872 
873 // If we need to enable eliptic-curve support, do so now. Note that for
874 // OpenSSL 1.1.0+ this is automatically done for us.
875 //
876 #if SSL_CTRL_SET_ECDH_AUTO
877  SSL_CTX_set_ecdh_auto(pImpl->ctx, 1);
878 #endif
879 
880 // We normally handle renegotiation during reads and writes or selective
881 // prohibit on a SSL socket basis. The calle may request this be applied
882 // to all SSL's generated from this context. If so, do it here.
883 //
884  if (opts & artON) SSL_CTX_set_mode(pImpl->ctx, SSL_MODE_AUTO_RETRY);
885 
886 // If there is no cert then assume this is a generic context for a client
887 //
888  if (cert == 0)
889  {ctx_tracker.Keep();
890  return;
891  }
892 
893 // We have a cert. If the key is missing then we assume the key is in the
894 // cert file (ssl will complain if it isn't).
895 //
896  if (!key) key = cert;
897 
898 // Load certificate
899 //
900  if (SSL_CTX_use_certificate_chain_file(pImpl->ctx, cert) != 1)
901  FATAL_SSL("Unable to create TLS context; invalid certificate.");
902 
903 // Load the private key
904 //
905  if (key[0] == 'p') {
906  if (!EnsureOpenSSLConfigLoaded())
907  FATAL_SSL("Unable to load OpenSSL configuration; cannot initialize pkcs11.");
908 
909 #ifdef XRDTLS_HAVE_OSSL_STORE
910  // OpenSSL 3.x+: Use OSSL_STORE API with pkcs11-provider in an ISOLATED context
911  // This prevents PKCS11 from affecting other TLS operations (e.g., SciTokens library)
912 
913  OSSL_PROVIDER *defaultProv = nullptr;
914 
915  // Initialize isolated PKCS11 context
916  // This loads the OPENSSL_CONF into an isolated context so PKCS11 doesn't affect
917  // other libraries in the process (like SciTokens)
918  if (!InitIsolatedPKCS11Context(&pImpl->pkcs11LibCtx, &pImpl->pkcs11Provider, &defaultProv)) {
919  FATAL_SSL("Failed to initialize isolated PKCS11 context. Check OPENSSL_CONF and pkcs11-provider installation.");
920  }
921 
922  // Open PKCS11 URI using the isolated context
923  OSSL_STORE_CTX *store_ctx = OSSL_STORE_open_ex(key, pImpl->pkcs11LibCtx, nullptr,
924  nullptr, nullptr, nullptr, nullptr, nullptr);
925  if (!store_ctx) {
926  if (defaultProv) OSSL_PROVIDER_unload(defaultProv);
927  FATAL_SSL("Failed to open PKCS11 URI in isolated context.");
928  }
929 
930  EVP_PKEY *priv_key = nullptr;
931  while (!OSSL_STORE_eof(store_ctx)) {
932  OSSL_STORE_INFO *info = OSSL_STORE_load(store_ctx);
933  if (info) {
934  int type = OSSL_STORE_INFO_get_type(info);
935  if (type == OSSL_STORE_INFO_PKEY) {
936  priv_key = OSSL_STORE_INFO_get1_PKEY(info);
937  OSSL_STORE_INFO_free(info);
938  break;
939  }
940  OSSL_STORE_INFO_free(info);
941  }
942  }
943  OSSL_STORE_close(store_ctx);
944  if (defaultProv) OSSL_PROVIDER_unload(defaultProv);
945 
946  if (!priv_key)
947  FATAL_SSL("Failed to load private key from PKCS11 URI in isolated context.");
948 
949  if (SSL_CTX_use_PrivateKey(pImpl->ctx, priv_key) != 1) {
950  EVP_PKEY_free(priv_key);
951  FATAL_SSL("Failed to have SSL context use private key");
952  }
953  EVP_PKEY_free(priv_key);
954 
955 #elif defined(XRDTLS_HAVE_ENGINE)
956  // OpenSSL 1.1.x (EL8): Use ENGINE API with libp11/engine_pkcs11
957  ENGINE *e = ENGINE_by_id("pkcs11");
958  if (e) {
959  const char* modulePath = getenv("PKCS11_MODULE_PATH");
960  if (modulePath && modulePath[0]) {
961  if (!ENGINE_ctrl_cmd_string(e, "MODULE_PATH", modulePath, 0)) {
962  ENGINE_free(e);
963  FATAL_SSL("Unable to configure pkcs11 engine MODULE_PATH");
964  }
965  }
966  if(!ENGINE_init(e)) {
967  ENGINE_free(e);
968  FATAL_SSL("Unable to initialize pkcs11 engine");
969  }
970  } else {
971  FATAL_SSL("Unable to create pkcs11 engine");
972  }
973  auto priv_key = ENGINE_load_private_key(e, key, nullptr, nullptr);
974 
975  if (!priv_key) {
976  FATAL_SSL("Failed to load private key through engine");
977  }
978  if (SSL_CTX_use_PrivateKey(pImpl->ctx, priv_key) != 1)
979  FATAL_SSL("Failed to have SSL context use private key");
980  EVP_PKEY_free(priv_key);
981 #else
982  FATAL_SSL("PKCS11 support not available.");
983 #endif
984 
985  } else if (SSL_CTX_use_PrivateKey_file(pImpl->ctx, key, SSL_FILETYPE_PEM) != 1 )
986  FATAL_SSL("Unable to create TLS context; invalid private key.");
987 
988 // Make sure the key and certificate file match.
989 //
990  if (SSL_CTX_check_private_key(pImpl->ctx) != 1 )
991  FATAL_SSL("Unable to create TLS context; cert-key mismatch.");
992 
993 // All went well, start the CRL refresh thread and keep the context.
994 //
995  if(opts & rfCRL) {
996  SetCrlRefresh();
997  }
998  ctx_tracker.Keep();
999 }
struct stat Stat
Definition: XrdCks.cc:49
#define stat(a, b)
Definition: XrdPosix.hh:101
struct myOpts opts
int emsg(int rc, char *msg)
#define FATAL_SSL(msg)
#define FATAL(msg)
static int getModificationTime(const char *path, time_t &modificationTime)
static const uint64_t vdept
Mask to isolate vdept.
static const int crlRS
Bits to shift vdept.
static const uint64_t clcOF
Disable client certificate request.
static const uint64_t servr
This is a server context.
static const uint64_t rfCRL
Turn on the CRL refresh thread.
static const uint64_t nopxy
Do not allow proxy certs.
static const uint64_t logVF
Log verify failures.
static const uint64_t crlFC
Full crl chain checking.
static int ctxIndex
static const uint64_t crlON
Enables crl checking.
static const uint64_t artON
Auto retry Handshake.
static const int vdepS
Bits to shift vdept.
static const char * Init()
static const uint64_t crlAM
Allow CA validation when CRL is missing (CRL soft-fail)
bool SetCrlRefresh(int refsec=-1)
static const uint64_t crlRF
Mask to isolate crl refresh in min.
static const int dbgSIO
Turn debugging in for socket I/O.
Definition: XrdTls.hh:102
static const int dbgSOK
Turn debugging in for socket operations.
Definition: XrdTls.hh:101
static const int dbgOUT
Force msgs to stderr for easier client debug.
Definition: XrdTls.hh:104
static const int dbgALL
Turn debugging for everything.
Definition: XrdTls.hh:103
static const int dbgCTX
Turn debugging in for context operations.
Definition: XrdTls.hh:100
static void SetDebug(int opts, XrdSysLogger *logP=0)
Definition: XrdTls.cc:177
XrdTlsContext::CTX_Params Parm
std::string cafile
-> ca cert file.
uint64_t opts
Options as passed to the constructor.
std::string cadir
-> ca cert directory.
int crlRT
crl refresh interval time in seconds
std::string pkey
-> private key path.
std::string cert
-> certificate path.

References artON, XrdTlsContext::CTX_Params::cadir, XrdTlsContext::CTX_Params::cafile, XrdTlsContext::CTX_Params::cert, clcOF, crlAM, crlFC, crlON, crlRF, crlRS, XrdTlsContext::CTX_Params::crlRT, XrdTlsContextImpl::ctx, ctxIndex, XrdTls::dbgALL, XrdTls::dbgCTX, XrdTls::dbgOUT, XrdTls::dbgSIO, XrdTls::dbgSOK, emsg(), FATAL, FATAL_SSL, XrdOucUtils::getModificationTime(), Init(), XrdTlsContextImpl::lastCertModTime, logVF, nopxy, opts, XrdTlsContext::CTX_Params::opts, XrdTlsContextImpl::Parm, XrdTlsContext::CTX_Params::pkey, rfCRL, servr, SetCrlRefresh(), XrdTls::SetDebug(), Stat, stat, vdepS, and vdept.

Referenced by Clone().

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

◆ ~XrdTlsContext()

XrdTlsContext::~XrdTlsContext ( )

Destructor.

Definition at line 1005 of file XrdTlsContext.cc.

1006 {
1007 // We can delet eour implementation of there is no refresh thread running. If
1008 // there is then the refresh thread has to delete the implementation.
1009 //
1010  if (pImpl->crlRunning | pImpl->flsRunning)
1011  {pImpl->crlMutex.WriteLock();
1012  pImpl->owner = 0;
1013  pImpl->crlMutex.UnLock();
1014  } else delete pImpl;
1015 }
XrdTlsContext * owner
XrdSysRWLock crlMutex

References XrdTlsContextImpl::crlMutex, XrdTlsContextImpl::crlRunning, XrdTlsContextImpl::flsRunning, XrdTlsContextImpl::owner, XrdSysRWLock::UnLock(), and XrdSysRWLock::WriteLock().

Referenced by Clone().

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

◆ XrdTlsContext() [2/3]

XrdTlsContext::XrdTlsContext ( const XrdTlsContext ctx)
delete

Disallow any copies of this object.

◆ XrdTlsContext() [3/3]

XrdTlsContext::XrdTlsContext ( XrdTlsContext &&  ctx)
delete

Member Function Documentation

◆ Clone()

XrdTlsContext * XrdTlsContext::Clone ( bool  full = true,
bool  startCRLRefresh = false 
)

Clone a new context from this context.

Parameters
fullWhen true the complete context is cloned. When false, a context with no peer verification is cloned.
Returns
Upon success, the pointer to a new XrdTlsContext is returned. Upon failure, a nil pointer is returned.
Note
The cloned context is identical to the one created by the original constructor. Note that while the crl refresh interval is set, the refresh thread needs to be started by calling crlRefresh(). Also, the session cache is set to off with no identifier.

Definition at line 1021 of file XrdTlsContext.cc.

1022 {
1023  XrdTlsContext::CTX_Params &my = pImpl->Parm;
1024  const char *cert = (my.cert.size() ? my.cert.c_str() : 0);
1025  const char *pkey = (my.pkey.size() ? my.pkey.c_str() : 0);
1026  const char *caD = (my.cadir.size() ? my.cadir.c_str() : 0);
1027  const char *caF = (my.cafile.size() ? my.cafile.c_str() : 0);
1028 
1029 // If this is a non-full context, get rid of any verification
1030 //
1031  if (!full) caD = caF = 0;
1032 
1033 // Cloning simply means getting a object with the old parameters.
1034 //
1035  uint64_t myOpts = my.opts;
1036  if(startCRLRefresh){
1038  } else {
1040  }
1041  XrdTlsContext *xtc = new XrdTlsContext(cert, pkey, caD, caF, myOpts);
1042 
1043 // Verify that the context was built
1044 //
1045  if (xtc->isOK()) {
1046  if(pImpl->sessionCacheOpts != -1){
1047  //A SessionCache() call was done for the current context, so apply it for this new cloned context
1048  xtc->SessionCache(pImpl->sessionCacheOpts,pImpl->sessionCacheId.c_str(),pImpl->sessionCacheId.size());
1049  }
1050  return xtc;
1051  }
1052 
1053 // We failed, cleanup.
1054 //
1055  delete xtc;
1056  return 0;
1057 }
~XrdTlsContext()
Destructor.
int SessionCache(int opts=scNone, const char *id=0, int idlen=0)
XrdTlsContext(const char *cert=0, const char *key=0, const char *cadir=0, const char *cafile=0, uint64_t opts=0, std::string *eMsg=0)
std::string sessionCacheId

References XrdTlsContext(), ~XrdTlsContext(), XrdTlsContext::CTX_Params::cadir, XrdTlsContext::CTX_Params::cafile, XrdTlsContext::CTX_Params::cert, isOK(), XrdTlsContext::CTX_Params::opts, XrdTlsContextImpl::Parm, XrdTlsContext::CTX_Params::pkey, rfCRL, SessionCache(), XrdTlsContextImpl::sessionCacheId, and XrdTlsContextImpl::sessionCacheOpts.

Referenced by XrdTlsCrl::Refresh().

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

◆ Context()

void * XrdTlsContext::Context ( )

Get the underlying context (should not be used).

Returns
Pointer to the underlying context.

Definition at line 1063 of file XrdTlsContext.cc.

1064 {
1065  return pImpl->ctx;
1066 }

References XrdTlsContextImpl::ctx.

◆ GetParams()

const XrdTlsContext::CTX_Params * XrdTlsContext::GetParams ( )

Definition at line 1072 of file XrdTlsContext.cc.

1073 {
1074  return &pImpl->Parm;
1075 }

References XrdTlsContextImpl::Parm.

Referenced by XrdTlsSocket::Init().

+ Here is the caller graph for this function:

◆ Init()

const char * XrdTlsContext::Init ( )
static

Simply initialize the TLS library.

Returns
=0 Library initialized. !0 Library not initialized, return string indicates why.
Note
Init() is implicitly called by the contructor. Use this method to use the TLS libraries without instantiating a context.

Definition at line 1081 of file XrdTlsContext.cc.

1082 {
1083 
1084 // Disallow use if this object unless SSL provides thread-safety!
1085 //
1086 #ifndef OPENSSL_THREADS
1087  return "Installed OpenSSL lacks the required thread support!";
1088 #endif
1089 
1090 // Initialize the library (one time call)
1091 //
1092  InitTLS();
1093  return 0;
1094 }
bool InitTLS()
Definition: XrdClTls.cc:96

References XrdCl::InitTLS().

Referenced by XrdCryptosslFactory::XrdCryptosslFactory(), XrdTlsContext(), and XrdCryptoLite_New_bf32().

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

◆ isOK()

bool XrdTlsContext::isOK ( )

Determine if this object was correctly built.

Returns
True if this object is usuable and false otherwise.

Definition at line 1100 of file XrdTlsContext.cc.

1101 {
1102  return pImpl->ctx != 0;
1103 }

References XrdTlsContextImpl::ctx.

Referenced by Clone(), and XrdTlsCrl::Refresh().

+ Here is the caller graph for this function:

◆ newHostCertificateDetected()

bool XrdTlsContext::newHostCertificateDetected ( )

Definition at line 1351 of file XrdTlsContext.cc.

1351  {
1352  const std::string certPath = pImpl->Parm.cert;
1353  if(certPath.empty()) {
1354  //No certificate provided, should not happen though
1355  return false;
1356  }
1357  time_t modificationTime;
1358  if(!XrdOucUtils::getModificationTime(certPath.c_str(),modificationTime)){
1359  if (pImpl->lastCertModTime != modificationTime) {
1360  //The certificate file has changed
1361  pImpl->lastCertModTime = modificationTime;
1362  return true;
1363  }
1364  }
1365  return false;
1366 }

References XrdTlsContext::CTX_Params::cert, XrdOucUtils::getModificationTime(), XrdTlsContextImpl::lastCertModTime, and XrdTlsContextImpl::Parm.

Referenced by XrdTlsCrl::Refresh().

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

◆ operator=() [1/2]

XrdTlsContext& XrdTlsContext::operator= ( const XrdTlsContext ctx)
delete

◆ operator=() [2/2]

XrdTlsContext& XrdTlsContext::operator= ( XrdTlsContext &&  ctx)
delete

◆ Session()

void * XrdTlsContext::Session ( )

Apply this context to obtain a new SSL session.

Returns
A pointer to a new SSL session if successful and nil otherwise.

Definition at line 1115 of file XrdTlsContext.cc.

1116 {
1117 #if OPENSSL_VERSION_NUMBER >= 0x10002000L
1118 
1119  EPNAME("Session");
1120  SSL *ssl;
1121 
1122 // Check if we have a refreshed context. If so, we need to replace the X509
1123 // store in the current context with the new one before we create the session.
1124 //
1125  pImpl->crlMutex.ReadLock();
1126  if (!(pImpl->ctxnew))
1127  {ssl = SSL_new(pImpl->ctx);
1128  pImpl->crlMutex.UnLock();
1129  return ssl;
1130  }
1131 
1132 // Things have changed, so we need to take the long route here. We need to
1133 // replace the x509 cache with the current cache. Get a R/W lock now.
1134 //
1135  pImpl->crlMutex.UnLock();
1136  pImpl->crlMutex.WriteLock();
1137 
1138 // If some other thread beat us to the punch, just return what we have.
1139 //
1140  if (!(pImpl->ctxnew))
1141  {ssl = SSL_new(pImpl->ctx);
1142  pImpl->crlMutex.UnLock();
1143  return ssl;
1144  }
1145 
1146 // Do some tracing
1147 //
1148  DBG_CTX("Replacing x509 store with new contents.");
1149 
1150 // Get the new store and set it in our context. Setting the store is black
1151 // magic. For OpenSSL < 1.1, Two stores need to be set with the "set1" variant.
1152 // Newer version only require SSL_CTX_set1_cert_store() to be used.
1153 //
1154  //We have a new context generated by Refresh, so we must use it.
1155  XrdTlsContext * ctxnew = pImpl->ctxnew;
1156 
1157  /*X509_STORE *newX509 = SSL_CTX_get_cert_store(ctxnew->pImpl->ctx);
1158  SSL_CTX_set1_verify_cert_store(pImpl->ctx, newX509);
1159  SSL_CTX_set1_chain_cert_store(pImpl->ctx, newX509);*/
1160  //The above two macros actually do not replace the certificate that has
1161  //to be used for that SSL session, so we will create the session with the SSL_CTX * of
1162  //the TlsContext created by Refresh()
1163  //First, free the current SSL_CTX, if it is used by any transfer, it will just decrease
1164  //the reference counter of it. There is therefore no risk of double free...
1165  SSL_CTX_free(pImpl->ctx);
1166  pImpl->ctx = ctxnew->pImpl->ctx;
1167 
1168  //Update ex_data to point to this (the surviving owner), not the
1169  //cloned context which is about to be deleted.
1170  SSL_CTX_set_ex_data(pImpl->ctx, ctxIndex, this);
1171 
1172  //In the destructor of XrdTlsContextImpl, SSL_CTX_Free() is
1173  //called if ctx is != 0. As this new ctx is used by the session
1174  //we just created, we don't want that to happen. We therefore set it to 0.
1175  //The SSL_free called on the session will cleanup the context for us.
1176  ctxnew->pImpl->ctx = 0;
1177 
1178 // Save the generated context and clear it's presence
1179 //
1180  XrdTlsContext *ctxold = pImpl->ctxnew;
1181  pImpl->ctxnew = 0;
1182 
1183 // Generate a new session (might as well to keep the lock we have)
1184 //
1185  ssl = SSL_new(pImpl->ctx);
1186 
1187 // OK, now we can drop all the locks and get rid of the old context
1188 //
1189  pImpl->crlMutex.UnLock();
1190  delete ctxold;
1191  return ssl;
1192 
1193 #else
1194 // If we did not compile crl refresh code, we can simply return the OpenSSL
1195 // session using our context. Otherwise, we need to see if we have a refreshed
1196 // context and if so, carry forward the X509_store to our original context.
1197 //
1198  return SSL_new(pImpl->ctx);
1199 #endif
1200 }
#define EPNAME(x)
Definition: XrdBwmTrace.hh:56
#define DBG_CTX(y)
Definition: XrdTlsTrace.hh:39
XrdTlsContext * ctxnew

References XrdTlsContextImpl::crlMutex, XrdTlsContextImpl::ctx, ctxIndex, XrdTlsContextImpl::ctxnew, DBG_CTX, EPNAME, XrdSysRWLock::ReadLock(), XrdSysRWLock::UnLock(), and XrdSysRWLock::WriteLock().

Referenced by XrdTlsSocket::Init(), and XrdHttpProtocol::Process().

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

◆ SessionCache()

int XrdTlsContext::SessionCache ( int  opts = scNone,
const char *  id = 0,
int  idlen = 0 
)

Definition at line 1206 of file XrdTlsContext.cc.

1207 {
1208  static const int doSet = scSrvr | scClnt | scOff;
1209  long sslopt = 0;
1210  int flushT = opts & scFMax;
1211 
1212  pImpl->sessionCacheOpts = opts;
1213  pImpl->sessionCacheId = id;
1214 
1215 // If initialization failed there is nothing to do
1216 //
1217  if (pImpl->ctx == 0) return 0;
1218 
1219 // Set options as appropriate
1220 //
1221  if (opts & doSet)
1222  {if (opts & scOff) sslopt = SSL_SESS_CACHE_OFF;
1223  else {if (opts & scSrvr) sslopt = SSL_SESS_CACHE_SERVER;
1224  if (opts & scClnt) sslopt |= SSL_SESS_CACHE_CLIENT;
1225  }
1226  }
1227 
1228 // Check if we should set any cache options or simply get them
1229 //
1230  if (!(opts & doSet)) sslopt = SSL_CTX_get_session_cache_mode(pImpl->ctx);
1231  else {sslopt = SSL_CTX_set_session_cache_mode(pImpl->ctx, sslopt);
1232  if (opts & scOff) SSL_CTX_set_options(pImpl->ctx, SSL_OP_NO_TICKET);
1233  }
1234 
1235 // Compute what he previous cache options were
1236 //
1237  opts = scNone;
1238  if (sslopt & SSL_SESS_CACHE_SERVER) opts |= scSrvr;
1239  if (sslopt & SSL_SESS_CACHE_CLIENT) opts |= scClnt;
1240  if (!opts) opts = scOff;
1241  if (sslopt & SSL_SESS_CACHE_NO_AUTO_CLEAR) opts |= scKeep;
1242  opts |= (static_cast<int>(pImpl->flushT) & scFMax);
1243 
1244 // Set the id is so wanted
1245 //
1246  if (id && idlen > 0)
1247  {if (!SSL_CTX_set_session_id_context(pImpl->ctx,
1248  (unsigned const char *)id,
1249  (unsigned int)idlen)) opts |= scIdErr;
1250  }
1251 
1252 // If a flush interval was specified and it is different from what we have
1253 // then reset the flush interval.
1254 //
1255  if (flushT && flushT != pImpl->flushT)
1256  XrdTlsFlush::Setup_Flusher(pImpl, flushT);
1257 
1258 // All done
1259 //
1260  return opts;
1261 }
static const int scIdErr
Info: Id not set, is too long.
static const int scClnt
Turn on cache client mode.
static const int scKeep
Info: TLS-controlled flush disabled.
static const int scNone
Do not change any option settings.
static const int scOff
Turn off cache.
static const int scFMax
static const int scSrvr
Turn on cache server mode (default)
bool Setup_Flusher(XrdTlsContextImpl *pImpl, int flushT)

References XrdTlsContextImpl::ctx, XrdTlsContextImpl::flushT, opts, scClnt, scFMax, scIdErr, scKeep, scNone, scOff, scSrvr, XrdTlsContextImpl::sessionCacheId, XrdTlsContextImpl::sessionCacheOpts, and XrdTlsFlush::Setup_Flusher().

Referenced by Clone().

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

◆ SetContextCiphers()

bool XrdTlsContext::SetContextCiphers ( const char *  ciphers)

Set allowed ciphers for this context.

Parameters
ciphersThe colon separated list of allowable ciphers.
Returns
True if at least one cipher can be used; false otherwise. When false is reurned, this context is no longer usable.

Definition at line 1267 of file XrdTlsContext.cc.

1268 {
1269  if (pImpl->ctx && SSL_CTX_set_cipher_list(pImpl->ctx, ciphers)) return true;
1270 
1271  char eBuff[2048];
1272  snprintf(eBuff,sizeof(eBuff),"Unable to set context ciphers '%s'",ciphers);
1273  Fatal(0, eBuff, true);
1274  return false;
1275 }
void Fatal(const char *op, const char *target)
Definition: XrdCrc32c.cc:58

References XrdTlsContextImpl::ctx, and Fatal().

+ Here is the call graph for this function:

◆ SetCrlRefresh()

bool XrdTlsContext::SetCrlRefresh ( int  refsec = -1)

Set CRL refresh time. By default, CRL's are not refreshed.

Parameters
refsec>0: The number of seconds between refreshes. A value less than 60 sets it to 60. =0: Stops automatic refreshing. <0: Starts automatic refreshing with the current setting if it has not already been started.
Returns
True if the CRL refresh thread was started; false otherwise.

Definition at line 1290 of file XrdTlsContext.cc.

1291 {
1292 #if OPENSSL_VERSION_NUMBER >= 0x10002000L
1293 
1294  pthread_t tid;
1295  int rc;
1296 
1297 // If it's negative or equal to 0, use the current setting
1298 //
1299  if (refsec <= 0)
1300  {pImpl->crlMutex.WriteLock();
1301  refsec = pImpl->Parm.crlRT;
1302  pImpl->crlMutex.UnLock();
1303  if (!refsec) refsec = XrdTlsContext::DEFAULT_CRL_REF_INT_SEC;
1304  }
1305 
1306 // Make sure this is at least 60 seconds between refreshes
1307 //
1308 // if (refsec < 60) refsec = 60;
1309 
1310 // We will set the new interval and start a refresh thread if not running.
1311 //
1312  pImpl->crlMutex.WriteLock();
1313  pImpl->Parm.crlRT = refsec;
1314  if (!pImpl->crlRunning)
1315  {if ((rc = XrdSysThread::Run(&tid, XrdTlsCrl::Refresh, (void *)pImpl,
1316  0, "CRL Refresh")))
1317  {char eBuff[512];
1318  snprintf(eBuff, sizeof(eBuff),
1319  "Unable to start CRL refresh thread; rc=%d", rc);
1320  XrdTls::Emsg("CrlRefresh:", eBuff, false);
1321  pImpl->crlMutex.UnLock();
1322  return false;
1323  } else pImpl->crlRunning = true;
1324  pImpl->crlMutex.UnLock();
1325  }
1326 
1327 // All done
1328 //
1329  return true;
1330 
1331 #else
1332 // We use features present on OpenSSL 1.02 and above to implement crl refresh.
1333 // Older version are too difficult to deal with. Issue a message if this
1334 // feature is being enabled on an old version.
1335 //
1336  XrdTls::Emsg("CrlRefresh:", "Refreshing CRLs only supported in "
1337  "OpenSSL version >= 1.02; CRL refresh disabled!", false);
1338  return false;
1339 #endif
1340 }
static int Run(pthread_t *, void *(*proc)(void *), void *arg, int opts=0, const char *desc=0)
static const int DEFAULT_CRL_REF_INT_SEC
Default CRL refresh interval in seconds.
static void Emsg(const char *tid, const char *msg=0, bool flush=true)
Definition: XrdTls.cc:104
void * Refresh(void *parg)

References XrdTlsContextImpl::crlMutex, XrdTlsContext::CTX_Params::crlRT, XrdTlsContextImpl::crlRunning, DEFAULT_CRL_REF_INT_SEC, XrdTls::Emsg(), XrdTlsContextImpl::Parm, XrdTlsCrl::Refresh(), XrdSysThread::Run(), XrdSysRWLock::UnLock(), and XrdSysRWLock::WriteLock().

Referenced by XrdTlsContext().

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

◆ SetDefaultCiphers()

void XrdTlsContext::SetDefaultCiphers ( const char *  ciphers)
static

Set allowed default ciphers.

Parameters
ciphersThe colon separated list of allowable ciphers.

Definition at line 1281 of file XrdTlsContext.cc.

1282 {
1283  sslCiphers = ciphers;
1284 }

◆ SetTlsClientAuth()

void XrdTlsContext::SetTlsClientAuth ( bool  setting)

Indicate how the server should handle TLS client authentication.

Parameters
settingtrue: All clients will be asked to send a TLS client certificate. false: No clients will be asked to send a TLS client certificate.

Note the TLS connection will not fail if the client is asked for a cert but none are provided.

Definition at line 1368 of file XrdTlsContext.cc.

1368  {
1369  if (setting)
1370  {pImpl->Parm.opts &= ~clcOF;
1371  bool LogVF = (pImpl->Parm.opts & logVF) != 0;
1372  bool crlAllowMissingCA = (pImpl->Parm.opts & crlAM) != 0;
1373 
1374  if (LogVF || crlAllowMissingCA)
1375  SSL_CTX_set_verify(pImpl->ctx, SSL_VERIFY_PEER, verifyPeerCB);
1376  else
1377  SSL_CTX_set_verify(pImpl->ctx, SSL_VERIFY_PEER, 0);
1378  } else
1379  {pImpl->Parm.opts |= clcOF;
1380  SSL_CTX_set_verify(pImpl->ctx, SSL_VERIFY_NONE, 0);
1381  }
1382 }

References clcOF, crlAM, XrdTlsContextImpl::ctx, logVF, XrdTlsContext::CTX_Params::opts, and XrdTlsContextImpl::Parm.

◆ x509Verify()

bool XrdTlsContext::x509Verify ( )

Check if certificates are being verified.

Returns
True if certificates are being verified, false otherwise.

Definition at line 1346 of file XrdTlsContext.cc.

1347 {
1348  return !(pImpl->Parm.cadir.empty()) || !(pImpl->Parm.cafile.empty());
1349 }

References XrdTlsContext::CTX_Params::cadir, XrdTlsContext::CTX_Params::cafile, and XrdTlsContextImpl::Parm.

Referenced by XrdTlsSocket::Init(), and XrdTlsCrl::Refresh().

+ Here is the caller graph for this function:

Member Data Documentation

◆ artON

const uint64_t XrdTlsContext::artON = 0x0000002000000000
static

Auto retry Handshake.

Definition at line 257 of file XrdTlsContext.hh.

Referenced by XrdTlsContext().

◆ clcOF

const uint64_t XrdTlsContext::clcOF = 0x0000010000000000
static

Disable client certificate request.

Definition at line 258 of file XrdTlsContext.hh.

Referenced by XrdTlsContext(), and SetTlsClientAuth().

◆ crlAM

const uint64_t XrdTlsContext::crlAM = 0x0000001000000000
static

Allow CA validation when CRL is missing (CRL soft-fail)

Definition at line 255 of file XrdTlsContext.hh.

Referenced by XrdTlsContext(), and SetTlsClientAuth().

◆ crlFC

const uint64_t XrdTlsContext::crlFC = 0x000000C000000000
static

Full crl chain checking.

Definition at line 253 of file XrdTlsContext.hh.

Referenced by XrdTlsContext().

◆ crlON

const uint64_t XrdTlsContext::crlON = 0x0000008000000000
static

Enables crl checking.

Definition at line 252 of file XrdTlsContext.hh.

Referenced by XrdTlsContext().

◆ crlRF

const uint64_t XrdTlsContext::crlRF = 0x00000000ffff0000
static

Mask to isolate crl refresh in min.

Definition at line 254 of file XrdTlsContext.hh.

Referenced by XrdTlsContext().

◆ crlRS

const int XrdTlsContext::crlRS = 16
static

Bits to shift vdept.

Definition at line 256 of file XrdTlsContext.hh.

Referenced by XrdTlsContext().

◆ ctxIndex

int XrdTlsContext::ctxIndex = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL)
static

Definition at line 261 of file XrdTlsContext.hh.

Referenced by XrdTlsContext(), and Session().

◆ DEFAULT_CRL_REF_INT_SEC

const int XrdTlsContext::DEFAULT_CRL_REF_INT_SEC = 8 * 60 * 60
static

Default CRL refresh interval in seconds.

Definition at line 66 of file XrdTlsContext.hh.

Referenced by SetCrlRefresh().

◆ dnsok

const uint64_t XrdTlsContext::dnsok = 0x0000000200000000
static

Trust DNS for host name.

Definition at line 249 of file XrdTlsContext.hh.

Referenced by XrdTlsSocket::Init().

◆ hsto

const uint64_t XrdTlsContext::hsto = 0x00000000000000ff
static

Mask to isolate the hsto.

Constructor. Note that you should use isOK() to determine if construction was successful. A false return indicates failure.

Parameters
certPointer to the certificate file to be used. If nil, a generic context is created for client use.
keyPointer to the private key flle to be used. It must correspond to the certificate file. If nil, it is assumed that the key is contained in the cert file.
cadirpath to the directory containing the CA certificates.
cafilepath to the file containing the CA certificates.
optsProcessing options (or'd bitwise): artON - Auto retry handshakes (i.e. block on handshake) crlON - Perform crl check on the leaf node crlFC - Apply crl check to full chain crlRF - Initial crl refresh interval in minutes. dnsok - trust DNS when verifying hostname. hsto - the handshake timeout value in seconds. logVF - Turn on verification failure logging. nopxy - Do not allow proxy cert (normally allowed) servr - This is a server-side context and x509 peer certificate validation may be turned off. vdept - The maximum depth of the certificate chain that must be validated (max is 255).
eMsgIf non-zero, the reason for the failure is returned,
Note
a) If neither cadir nor cafile is specified, certificate validation is not performed if and only if the servr option is specified. Otherwise, the cadir value is obtained from the X509_CERT_DIR envar and the cafile value is obtained from the X509_CERT_File envar. If both are nil, context creation fails. b) Additionally for client-side contructions, if cert or key is not specified their locations come from X509_USER_PROXY and X509_USER_KEY. These may be nil in which case a generic context is created with a local key-pair and no certificate. c) You should immediately call isOK() after instantiating this object. A return value of false means that construction failed. d) Failure messages are routed to the message callback function during construction. e) While the crl refresh interval is set you must engage it by calling crlRefresh() so as to avoid unnecessary refresh threads.

Definition at line 244 of file XrdTlsContext.hh.

Referenced by XrdTlsSocket::Init().

◆ logVF

const uint64_t XrdTlsContext::logVF = 0x0000000800000000
static

Log verify failures.

Definition at line 247 of file XrdTlsContext.hh.

Referenced by XrdConfig::XrdConfig(), XrdTlsContext(), and SetTlsClientAuth().

◆ nopxy

const uint64_t XrdTlsContext::nopxy = 0x0000000100000000
static

Do not allow proxy certs.

Definition at line 250 of file XrdTlsContext.hh.

Referenced by XrdTlsContext().

◆ rfCRL

const uint64_t XrdTlsContext::rfCRL = 0x0000004000000000
static

Turn on the CRL refresh thread.

Definition at line 251 of file XrdTlsContext.hh.

Referenced by XrdTlsContext(), and Clone().

◆ scClnt

const int XrdTlsContext::scClnt = 0x00040000
static

Turn on cache client mode.

Definition at line 135 of file XrdTlsContext.hh.

Referenced by SessionCache().

◆ scFMax

const int XrdTlsContext::scFMax = 0x00007fff
static

Maximum flush interval in seconds When 0 keeps the current setting

Definition at line 138 of file XrdTlsContext.hh.

Referenced by SessionCache().

◆ scIdErr

const int XrdTlsContext::scIdErr = 0x80000000
static

Info: Id not set, is too long.

Definition at line 137 of file XrdTlsContext.hh.

Referenced by SessionCache().

◆ scKeep

const int XrdTlsContext::scKeep = 0x40000000
static

Info: TLS-controlled flush disabled.

Definition at line 136 of file XrdTlsContext.hh.

Referenced by SessionCache().

◆ scNone

const int XrdTlsContext::scNone = 0x00000000
static

Do not change any option settings.

Get or set session cache parameters for generated sessions.

Parameters
optsOne or more bit or'd options (see below).
idThe identifier to be used (may be nil to keep setting).
idlenThe length of the identifier (may be zero as above).
Returns
The cache settings prior to any changes are returned. When setting the id, the scIdErr may be returned if the name is too long. If the context has been pprroperly initialized, zero is returned. By default, the session cache is disabled as it is impossible to verify a peer certificate chain when a cached session is reused.

Definition at line 132 of file XrdTlsContext.hh.

Referenced by SessionCache().

◆ scOff

const int XrdTlsContext::scOff = 0x00010000
static

Turn off cache.

Definition at line 133 of file XrdTlsContext.hh.

Referenced by SessionCache().

◆ scSrvr

const int XrdTlsContext::scSrvr = 0x00020000
static

Turn on cache server mode (default)

Definition at line 134 of file XrdTlsContext.hh.

Referenced by SessionCache().

◆ servr

const uint64_t XrdTlsContext::servr = 0x0000000400000000
static

This is a server context.

Definition at line 248 of file XrdTlsContext.hh.

Referenced by XrdConfig::XrdConfig(), and XrdTlsContext().

◆ vdepS

const int XrdTlsContext::vdepS = 8
static

Bits to shift vdept.

Definition at line 246 of file XrdTlsContext.hh.

Referenced by XrdTlsContext().

◆ vdept

const uint64_t XrdTlsContext::vdept = 0x000000000000ff00
static

Mask to isolate vdept.

Definition at line 245 of file XrdTlsContext.hh.

Referenced by XrdTlsContext().


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