XRootD
XrdTlsContext.cc
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2018 by European Organization for Nuclear Research (CERN)
3 // Author: Michal Simon <simonm@cern.ch>
4 //------------------------------------------------------------------------------
5 // XRootD is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // XRootD is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
17 //------------------------------------------------------------------------------
18 
19 #include <cstdio>
20 #include <cstdlib>
21 #include <mutex>
22 
23 // Include OpenSSL config headers first to get feature detection macros
24 #include <openssl/opensslconf.h>
25 #include <openssl/opensslv.h>
26 
27 // PKCS#11 support strategy:
28 // - OpenSSL 3.x+ (EL9+, AlmaLinux 10): Prefer OSSL_STORE API with pkcs11-provider (modern)
29 // Falls back to ENGINE if pkcs11-provider is not installed but engine_pkcs11 is
30 // - OpenSSL 1.1.x (EL8): Use ENGINE API with libp11/engine_pkcs11 (legacy compatibility)
31 
32 // OpenSSL 3.0+ Provider/OSSL_STORE support (preferred for modern systems)
33 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
34  #define XRDTLS_HAVE_OSSL_STORE 1
35  #include <openssl/store.h>
36  #include <openssl/provider.h>
37 #endif
38 
39 // ENGINE API support:
40 // - OpenSSL 1.1.x: Primary method for PKCS#11
41 #if !defined(OPENSSL_NO_ENGINE)
42  #define XRDTLS_HAVE_ENGINE 1
43  #include <openssl/engine.h>
44 #endif
45 
46 #include <openssl/bio.h>
47 #include <openssl/crypto.h>
48 #include <openssl/err.h>
49 #include <openssl/conf.h>
50 #include <openssl/ssl.h>
51 #include <sys/stat.h>
52 
53 #include "XrdOuc/XrdOucUtils.hh"
54 #include "XrdSys/XrdSysRAtomic.hh"
55 #include "XrdSys/XrdSysError.hh"
56 #include "XrdSys/XrdSysPthread.hh"
57 #include "XrdSys/XrdSysTimer.hh"
58 
59 #include "XrdTls/XrdTls.hh"
60 #include "XrdTls/XrdTlsContext.hh"
61 #include "XrdTls/XrdTlsTrace.hh"
62 
63 #if OPENSSL_VERSION_NUMBER >= 0x30400010
64 #define SSL_CTX_flush_sessions SSL_CTX_flush_sessions_ex
65 #endif
66 
67 /******************************************************************************/
68 /* G l o b a l s */
69 /******************************************************************************/
70 
71 namespace XrdTlsGlobal
72 {
74 };
75 
76 namespace
77 {
78 #ifndef OPENSSL_NO_CONF
79 bool EnsureOpenSSLConfigLoaded()
80 {
81  static std::once_flag configFlag;
82  static bool loadOK = true;
83 
84  std::call_once(configFlag, []() {
85 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
86  if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, nullptr))
87  loadOK = false;
88 #else
89  OPENSSL_config(nullptr);
90 #endif
91  });
92  return loadOK;
93 }
94 #else
95 bool EnsureOpenSSLConfigLoaded()
96 {
97  return false;
98 }
99 #endif
100 
101 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
102 // Initialize an isolated OSSL_LIB_CTX for PKCS11 operations
103 // This prevents PKCS11 from affecting other parts of the process (e.g., SciTokens library)
104 bool InitIsolatedPKCS11Context(OSSL_LIB_CTX **libCtx, OSSL_PROVIDER **pkcs11Prov, OSSL_PROVIDER **defaultProv)
105 {
106  // Create an isolated library context
107  *libCtx = OSSL_LIB_CTX_new();
108  if (!*libCtx) {
109  return false;
110  }
111 
112  // Load the OpenSSL configuration into the isolated context
113  // This allows the pkcs11-provider to be properly configured with module paths, etc.
114  const char *opensslConf = getenv("OPENSSL_CONF");
115  if (opensslConf && opensslConf[0]) {
116  // Load the config file into the isolated context
117  if (!OSSL_LIB_CTX_load_config(*libCtx, opensslConf)) {
118  // Config loading failed, cleanup and return error
119  OSSL_LIB_CTX_free(*libCtx);
120  *libCtx = nullptr;
121  return false;
122  }
123  } else {
124  // No OPENSSL_CONF set, try loading providers manually
125  // Load the default provider in the isolated context (needed for basic crypto)
126  *defaultProv = OSSL_PROVIDER_load(*libCtx, "default");
127  if (!*defaultProv) {
128  OSSL_LIB_CTX_free(*libCtx);
129  *libCtx = nullptr;
130  return false;
131  }
132 
133  // Load the pkcs11 provider in the isolated context
134  *pkcs11Prov = OSSL_PROVIDER_load(*libCtx, "pkcs11");
135  if (!*pkcs11Prov) {
136  OSSL_PROVIDER_unload(*defaultProv);
137  OSSL_LIB_CTX_free(*libCtx);
138  *libCtx = nullptr;
139  *defaultProv = nullptr;
140  return false;
141  }
142  }
143 
144  return true;
145 }
146 #endif
147 }
148 
149 /******************************************************************************/
150 /* X r d T l s C o n t e x t I m p l */
151 /******************************************************************************/
152 
154 {
156  : ctx(0), ctxnew(0), owner(p), flsCVar(0),
157  flushT(0),
158  crlRunning(false), flsRunning(false)
159 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
160  , pkcs11LibCtx(0), pkcs11Provider(0)
161 #endif
162  {}
163  ~XrdTlsContextImpl() {if (ctx) SSL_CTX_free(ctx);
164  if (ctxnew) delete ctxnew;
165  if (flsCVar) delete flsCVar;
166 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
167  if (pkcs11Provider) OSSL_PROVIDER_unload(pkcs11Provider);
168  if (pkcs11LibCtx) OSSL_LIB_CTX_free(pkcs11LibCtx);
169 #endif
170  }
171 
172  SSL_CTX *ctx;
178  short flushT;
181  time_t lastCertModTime = 0;
183  std::string sessionCacheId;
184 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
185  OSSL_LIB_CTX *pkcs11LibCtx; // Isolated context for PKCS11
186  OSSL_PROVIDER *pkcs11Provider; // PKCS11 provider handle
187 #endif
188 };
189 
190 /******************************************************************************/
191 /* C r l R e f r e s h S u p p o r t */
192 /******************************************************************************/
193 
194 namespace XrdTlsCrl
195 {
196 // Inital entry for refreshing crls
197 //
198 void *Refresh(void *parg)
199 {
200  EPNAME("Refresh");
201  int sleepTime;
202  bool doreplace;
203 
204 // Get the implementation details
205 //
206  XrdTlsContextImpl *ctxImpl = static_cast<XrdTlsContextImpl*>(parg);
207 
208 // Indicate we have started in the trace record
209 //
210  DBG_CTX("CRL refresh started.")
211 
212 // Do this forever but first get the sleep time
213 //
214 do{ctxImpl->crlMutex.ReadLock();
215  sleepTime = ctxImpl->Parm.crlRT;
216  ctxImpl->crlMutex.UnLock();
217 
218 // We may have been cancelled, in which case we just exit
219 //
220  if (!sleepTime)
221  {ctxImpl->crlMutex.WriteLock();
222  ctxImpl->crlRunning = false;
223  ctxImpl->crlMutex.UnLock();
224  DBG_CTX("CRL refresh ending by request!");
225  return (void *)0;
226  }
227 
228 // Indicate we how long before a refresh
229 //
230  DBG_CTX("CRL refresh will happen in " <<sleepTime <<" seconds.");
231 
232 // Now sleep the request amount of time
233 //
234  XrdSysTimer::Snooze(sleepTime);
235 
236  if (ctxImpl->owner->x509Verify() || ctxImpl->owner->newHostCertificateDetected()) {
237  // Check if this context is still alive. Generally, it never gets deleted.
238  //
239  ctxImpl->crlMutex.WriteLock();
240  if (!ctxImpl->owner) break;
241 
242  // We clone the original, this will give us the latest crls (i.e. refreshed).
243  // We drop the lock while doing so as this may take a long time. This is
244  // completely safe to do because we implicitly own the implementation.
245  //
246  ctxImpl->crlMutex.UnLock();
247  XrdTlsContext *newctx = ctxImpl->owner->Clone();
248 
249  // Verify that the context was properly built
250  //
251  if (!newctx || !newctx->isOK())
252  {XrdTls::Emsg("CrlRefresh:","Refresh of context failed!!!",false);
253  continue;
254  }
255 
256  // OK, set the new context to be used next time Session() is called.
257  //
258  ctxImpl->crlMutex.WriteLock();
259  doreplace = (ctxImpl->ctxnew != 0);
260  if (doreplace) delete ctxImpl->ctxnew;
261  ctxImpl->ctxnew = newctx;
262  ctxImpl->crlMutex.UnLock();
263 
264  // Do some debugging
265  //
266  if (doreplace) {DBG_CTX("CRL refresh created replacement x509 store.");}
267  else {DBG_CTX("CRL refresh created new x509 store.");}
268  }
269  } while(true);
270 
271 // If we are here the context that started us has gone away and we are done
272 //
273  bool keepctx = ctxImpl->flsRunning;
274  ctxImpl->crlRunning = false;
275  ctxImpl->crlMutex.UnLock();
276  if (!keepctx) delete ctxImpl;
277  return (void *)0;
278 }
279 }
280 
281 /******************************************************************************/
282 /* C a c h e F l u s h S u p p o r t */
283 /******************************************************************************/
284 
285 namespace XrdTlsFlush
286 {
287 /******************************************************************************/
288 /* F l u s h e r */
289 /******************************************************************************/
290 // Inital entry for refreshing crls
291 //
292 void *Flusher(void *parg)
293 {
294  EPNAME("Flusher");
295  time_t tStart, tWaited;
296  int flushT, waitT, hits, miss, sesn, tmos;
297  long tNow;
298 
299 // Get the implementation details
300 //
301  XrdTlsContextImpl *ctxImpl = static_cast<XrdTlsContextImpl*>(parg);
302 
303 // Get the interval as it may change as we are running
304 //
305  ctxImpl->crlMutex.ReadLock();
306  waitT = flushT = ctxImpl->flushT;
307  ctxImpl->crlMutex.UnLock();
308 
309 // Indicate we have started in the trace record
310 //
311  DBG_CTX("Cache flusher started; interval="<<flushT<<" seconds.");
312 
313 // Do this forever
314 //
315 do{tStart = time(0);
316  ctxImpl->flsCVar->Wait(waitT);
317  tWaited= time(0) - tStart;
318 
319 // Check if this context is still alive. Generally, it never gets deleted.
320 //
321  ctxImpl->crlMutex.ReadLock();
322  if (!ctxImpl->owner) break;
323 
324 // If the interval changed, see if we should wait a bit longer
325 //
326  if (flushT != ctxImpl->flushT && tWaited < ctxImpl->flushT-1)
327  {waitT = ctxImpl->flushT - tWaited;
328  ctxImpl->crlMutex.UnLock();
329  continue;
330  }
331 
332 // Get the new values and drop the lock
333 //
334  waitT = flushT = ctxImpl->flushT;
335  ctxImpl->crlMutex.UnLock();
336 
337 // Get some relevant statistics
338 //
339  sesn = SSL_CTX_sess_number(ctxImpl->ctx);
340  hits = SSL_CTX_sess_hits(ctxImpl->ctx);
341  miss = SSL_CTX_sess_misses(ctxImpl->ctx);
342  tmos = SSL_CTX_sess_timeouts(ctxImpl->ctx);
343 
344 // Flush the cache
345 //
346  tNow = time(0);
347  SSL_CTX_flush_sessions(ctxImpl->ctx, tNow);
348 
349 // Print some stuff should debugging be on
350 //
351  if (TRACING(XrdTls::dbgCTX))
352  {char mBuff[512];
353  snprintf(mBuff, sizeof(mBuff), "sess=%d hits=%d miss=%d timeouts=%d",
354  sesn, hits, miss, tmos);
355  DBG_CTX("Cache flushed; " <<mBuff);
356  }
357  } while(true);
358 
359 // If we are here the context that started us has gone away and we are done
360 //
361  bool keepctx = ctxImpl->crlRunning;
362  ctxImpl->flsRunning = false;
363  ctxImpl->crlMutex.UnLock();
364  if (!keepctx) delete ctxImpl;
365  return (void *)0;
366 }
367 
368 /******************************************************************************/
369 /* S e t u p _ F l u s h e r */
370 /******************************************************************************/
371 
372 bool Setup_Flusher(XrdTlsContextImpl *pImpl, int flushT)
373 {
374  pthread_t tid;
375  int rc;
376 
377 // Set the new flush interval
378 //
379  pImpl->crlMutex.WriteLock();
380  pImpl->flushT = flushT;
381  pImpl->crlMutex.UnLock();
382 
383 // If the flush thread is already running, then wake it up to get the new value
384 //
385  if (pImpl->flsRunning)
386  {pImpl->flsCVar->Signal();
387  return true;
388  }
389 
390 // Start the flusher thread
391 //
392  pImpl->flsCVar = new XrdSysCondVar();
393  if ((rc = XrdSysThread::Run(&tid, XrdTlsFlush::Flusher, (void *)pImpl,
394  0, "Cache Flusher")))
395  {char eBuff[512];
396  snprintf(eBuff, sizeof(eBuff),
397  "Unable to start cache flusher thread; rc=%d", rc);
398  XrdTls::Emsg("SessCache:", eBuff, false);
399  return false;
400  }
401 
402 // Finish up
403 //
404  pImpl->flsRunning = true;
405  SSL_CTX_set_session_cache_mode(pImpl->ctx, SSL_SESS_CACHE_NO_AUTO_CLEAR);
406  return true;
407 }
408 }
409 
410 /******************************************************************************/
411 /* S S L T h r e a d i n g S u p p o r t */
412 /******************************************************************************/
413 
414 // The following may confusing because SSL MT support is somewhat bizarre.
415 // Versions < 1.0 require a numeric thread_id and lock callbasks.
416 // Versions < 1.1 require a lock_callbacks but the thread_is callback is
417 // optional. While the numeric thread_id callback can be used
418 // it's deprecated and fancier pointer/numeric call should be
419 // used. In our case, we use the deprecated version.
420 // Versions >- 1.1 Do not need any callbacks as all threading functions are
421 // internally defined to use native MT functions.
422 
423 template<bool is32>
424 struct tlsmix;
425 
426 template<>
427 struct tlsmix<false> {
428  static unsigned long mixer(unsigned long x) {
429  // mixer based on splitmix64
430  x ^= x >> 30;
431  x *= 0xbf58476d1ce4e5b9UL;
432  x ^= x >> 27;
433  x *= 0x94d049bb133111ebUL;
434  x ^= x >> 31;
435  return x;
436  }
437 };
438 
439 template<>
440 struct tlsmix<true> {
441  static unsigned long mixer(unsigned long x) {
442  // mixer based on murmurhash3
443  x ^= x >> 16;
444  x *= 0x85ebca6bU;
445  x ^= x >> 13;
446  x *= 0xc2b2ae35U;
447  x ^= x >> 16;
448  return x;
449  }
450 };
451 
452 unsigned long sslTLS_id_callback(void)
453 {
454  // base thread-id on the id given by XrdSysThread;
455  // but openssl 1.0 uses thread-id as a key for looking
456  // up per thread crypto ERR structures in a hash-table.
457  // So mix bits so that the table's hash function gives
458  // better distribution.
459 
460  unsigned long x = (unsigned long)XrdSysThread::ID();
461  return tlsmix<sizeof(unsigned long)==4>::mixer(x);
462 }
463 
465 
466 void sslTLS_lock(int mode, int n, const char *file, int line)
467 {
468 // Use exclusive locks. At some point, SSL categorizes these as read and
469 // write locks but it's not clear when this actually occurs, sigh.
470 //
471  if (mode & CRYPTO_LOCK) MutexVector[n].Lock();
472  else MutexVector[n].UnLock();
473 }
474 
475 /******************************************************************************/
476 /* F i l e L o c a l D e f i n i t i o n s */
477 /******************************************************************************/
478 
479 namespace
480 {
481 // The following is the default TLS cipher list. We use the recommended list from Mozilla:
482 // https://ssl-config.mozilla.org/#server=apache&config=intermediate&openssl=1.1.0
483 const char *sslCiphers = "ECDHE-ECDSA-AES128-GCM-SHA256:"
484  "ECDHE-RSA-AES128-GCM-SHA256:"
485  "ECDHE-ECDSA-AES256-GCM-SHA384:"
486  "ECDHE-RSA-AES256-GCM-SHA384:"
487  "ECDHE-ECDSA-CHACHA20-POLY1305:"
488  "ECDHE-RSA-CHACHA20-POLY1305:"
489  "DHE-RSA-AES128-GCM-SHA256:"
490  "DHE-RSA-AES256-GCM-SHA384";
491 
492 XrdSysMutex dbgMutex, tlsMutex;
493 XrdSys::RAtomic<bool> initDbgDone{ false };
494 bool initTlsDone{ false };
495 
496 /******************************************************************************/
497 /* I n i t T L S */
498 /******************************************************************************/
499 
500 void InitTLS() // This is strictly a one-time call!
501 {
502  XrdSysMutexHelper tlsHelper(tlsMutex);
503 
504 // Make sure we are not trying to load the ssl library more than once. This can
505 // happen when a server and a client instance happen to be both defined.
506 //
507  if (initTlsDone) return;
508  initTlsDone = true;
509 
510 // SSL library initialisation
511 //
512  SSL_library_init();
513  OpenSSL_add_all_algorithms();
514  SSL_load_error_strings();
515  OpenSSL_add_all_ciphers();
516 #if OPENSSL_VERSION_NUMBER < 0x30000000L
517  ERR_load_BIO_strings();
518 #endif
519  ERR_load_crypto_strings();
520 }
521 
522 /******************************************************************************/
523 /* F a t a l */
524 /******************************************************************************/
525 
526 void Fatal(std::string *eMsg, const char *msg, bool sslmsg=false)
527 {
528 // If there is an outboard error string object, return the message there.
529 //
530  if (eMsg) *eMsg = msg;
531 
532 // Now route the message to the message callback function. If this is an ssl
533 // related error we also flush the ssl error queue to prevent suprises.
534 //
535  XrdTls::Emsg("TLS_Context:", msg, sslmsg);
536 }
537 
538 /******************************************************************************/
539 /* G e t T l s M e t h o d */
540 /******************************************************************************/
541 
542 const char *GetTlsMethod(const SSL_METHOD *&meth)
543 {
544  meth = TLS_method();
545  if (meth == 0) return "No negotiable TLS method available.";
546  return 0;
547 }
548 
549 /******************************************************************************/
550 /* V e r P a t h s */
551 /******************************************************************************/
552 
553 bool VerPaths(const char *cert, const char *pkey,
554  const char *cadr, const char *cafl, std::string &eMsg)
555 {
556  static const mode_t cert_mode = S_IRUSR | S_IWUSR | S_IRWXG | S_IROTH;
557  static const mode_t pkey_mode = S_IRUSR | S_IWUSR;
558  static const mode_t cadr_mode = S_IRWXU | S_IRGRP | S_IXGRP
559  | S_IROTH | S_IXOTH;
560  static const mode_t cafl_mode = S_IRUSR | S_IWUSR | S_IRWXG | S_IROTH;
561  const char *emsg;
562 
563 // If the ca cert directory is present make sure it's a directory and
564 // only the ower can write to that directory (anyone can read from it).
565 //
566  if (cadr && (emsg = XrdOucUtils::ValPath(cadr, cadr_mode, true)))
567  {eMsg = "Unable to use CA cert directory ";
568  eMsg += cadr; eMsg += "; "; eMsg += emsg;
569  return false;
570  }
571 
572 // If a ca cert file is present make sure it's a file and only the owner can
573 // write it (anyone can read it).
574 //
575  if (cafl && (emsg = XrdOucUtils::ValPath(cafl, cafl_mode, false)))
576  {eMsg = "Unable to use CA cert file ";
577  eMsg += cafl; eMsg += "; "; eMsg += emsg;
578  return false;
579  }
580 
581 // If a private key is provided as a filesystem path, verify it's a file and only the
582 // owner has access to it. If it's a PKCS#11 URI, it will be validated later.
583 //
584  if (pkey && pkey[0] == '/' && (emsg = XrdOucUtils::ValPath(pkey, pkey_mode, false)))
585  {eMsg = "Unable to use key file ";
586  eMsg += pkey; eMsg += "; "; eMsg += emsg;
587  return false;
588  }
589 
590 // If a cert file is present then make sure it's a file. If a keyfile is
591 // present then anyone can read it but only the owner can write it.
592 // Otherwise, only the owner can gave access to it (it contains the key).
593 //
594  if (cert)
595  {mode_t cmode = (pkey ? cert_mode : pkey_mode);
596  if ((emsg = XrdOucUtils::ValPath(cert, cmode, false)))
597  {if (pkey) eMsg = "Unable to use cert file ";
598  else eMsg = "Unable to use cert+key file ";
599  eMsg += cert; eMsg += "; "; eMsg += emsg;
600  return false;
601  }
602  }
603 
604 // All tests succeeded.
605 //
606  return true;
607 }
608 
609 /******************************************************************************/
610 /* V e r C B */
611 /******************************************************************************/
612 
613 extern "C"
614 {
619 int verifyPeerCB(int aOK, X509_STORE_CTX *x509P) {
620  if (!aOK) {
621  SSL *ssl = (SSL*)X509_STORE_CTX_get_ex_data(x509P, SSL_get_ex_data_X509_STORE_CTX_idx());
622  SSL_CTX *sslCtx = SSL_get_SSL_CTX(ssl);
623  XrdTlsContext *self = (XrdTlsContext*)SSL_CTX_get_ex_data(sslCtx, XrdTlsContext::ctxIndex);
624  bool LogVF = (self->GetParams()->opts & XrdTlsContext::logVF) != 0;
625  bool crlAllowMissingCA = (self->GetParams()->opts & XrdTlsContext::crlAM) != 0;
626  if (crlAllowMissingCA) {
627  int err = X509_STORE_CTX_get_error(x509P);
628  if (err == X509_V_ERR_UNABLE_TO_GET_CRL) {
629  X509_STORE_CTX_set_error(x509P, X509_V_OK);
630  return 1;
631  }
632  }
633  if (LogVF) {
634  X509 *cert = X509_STORE_CTX_get_current_cert(x509P);
635  int depth = X509_STORE_CTX_get_error_depth(x509P);
636  int err = X509_STORE_CTX_get_error(x509P);
637  char name[512], info[1024];
638 
639  X509_NAME_oneline(X509_get_subject_name(cert), name, sizeof(name));
640  snprintf(info,sizeof(info),"Cert verification failed for DN=%s",name);
641  XrdTls::Emsg("CertVerify:", info, false);
642 
643  X509_NAME_oneline(X509_get_issuer_name(cert), name, sizeof(name));
644  snprintf(info,sizeof(info),"Failing cert issuer=%s", name);
645  XrdTls::Emsg("CertVerify:", info, false);
646 
647  snprintf(info, sizeof(info), "Error %d at depth %d [%s]", err, depth,
648  X509_verify_cert_error_string(err));
649  XrdTls::Emsg("CertVerify:", info, true);
650  }
651  }
652  return aOK;
653 }
654 
655 }
656 
657 } // Anonymous namespace end
658 
659 /******************************************************************************/
660 /* C o n s t r u c t o r */
661 /******************************************************************************/
662 
663 #define KILL_CTX(x) if (x) {SSL_CTX_free(x); x = 0;}
664 
665 #define FATAL(msg) {Fatal(eMsg, msg); KILL_CTX(pImpl->ctx); return;}
666 
667 #define FATAL_SSL(msg) {Fatal(eMsg, msg, true); KILL_CTX(pImpl->ctx); return;}
668 
669 int XrdTlsContext::ctxIndex = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
670 
671 XrdTlsContext::XrdTlsContext(const char *cert, const char *key,
672  const char *caDir, const char *caFile,
673  uint64_t opts, std::string *eMsg)
674  : pImpl( new XrdTlsContextImpl(this) )
675 {
676  class ctx_helper
677  {public:
678 
679  void Keep() {ctxLoc = 0;}
680 
681  ctx_helper(SSL_CTX **ctxP) : ctxLoc(ctxP) {}
682  ~ctx_helper() {if (ctxLoc && *ctxLoc)
683  {SSL_CTX_free(*ctxLoc); *ctxLoc = 0;}
684  }
685  private:
686  SSL_CTX **ctxLoc;
687  } ctx_tracker(&pImpl->ctx);
688 
689  static const uint64_t sslOpts = SSL_OP_ALL
690  | SSL_OP_NO_SSLv2
691  | SSL_OP_NO_SSLv3
692  | SSL_OP_NO_COMPRESSION
693  | SSL_OP_NO_RENEGOTIATION
694 #ifdef SSL_OP_IGNORE_UNEXPECTED_EOF
695  | SSL_OP_IGNORE_UNEXPECTED_EOF
696 #endif
697  ;
698 
699  std::string certFN, eText;
700  const char *emsg;
701 
702 // Assume we will fail
703 //
704  pImpl->ctx = 0;
705 
706 // Verify that initialzation has occurred. This is not heavy weight as
707 // there will usually be no more than two instances of this object.
708 //
709  if (!initDbgDone)
710  {XrdSysMutexHelper dbgHelper(dbgMutex);
711  if (!initDbgDone)
712  {const char *dbg;
713  if (!(opts & servr) && (dbg = getenv("XRDTLS_DEBUG")))
714  {int dbgOpts = 0;
715  if (strstr(dbg, "ctx")) dbgOpts |= XrdTls::dbgCTX;
716  if (strstr(dbg, "sok")) dbgOpts |= XrdTls::dbgSOK;
717  if (strstr(dbg, "sio")) dbgOpts |= XrdTls::dbgSIO;
718  if (!dbgOpts) dbgOpts = XrdTls::dbgALL;
720  }
721  if ((emsg = Init())) FATAL(emsg);
722  initDbgDone = true;
723  }
724  }
725 
726 // If no CA cert information is specified and this is not a server context,
727 // then get the paths from the environment. They must exist as we need to
728 // verify peer certs in order to verify target host names client-side. We
729 // also use this setupt to see if we should use a specific cert and key.
730 //
731  if (!(opts & servr))
732  {if (!caDir && !caFile)
733  {caDir = getenv("X509_CERT_DIR");
734  caFile = getenv("X509_CERT_FILE");
735  if (!caDir && !caFile)
736  FATAL("No CA cert specified; host identity cannot be verified.");
737  }
738  if (!key) key = getenv("X509_USER_KEY");
739  if (!cert) cert = getenv("X509_USER_PROXY");
740  if (!cert)
741  {struct stat Stat;
742  long long int uid = static_cast<long long int>(getuid());
743  certFN = std::string("/tmp/x509up_u") + std::to_string(uid);
744  if (!stat(certFN.c_str(), &Stat)) cert = certFN.c_str();
745  }
746  }
747 
748 // Before we try to use any specified files, make sure they exist, are of
749 // the right type and do not have excessive access privileges.
750 // .a
751  if (!VerPaths(cert, key, caDir, caFile, eText)) FATAL( eText.c_str());
752 
753 // Copy parameters to out parm structure.
754 //
755  if (cert) {
756  pImpl->Parm.cert = cert;
757  //This call should not fail as a stat is already performed in the call of VerPaths() above
759  }
760  if (key) pImpl->Parm.pkey = key;
761  if (caDir) pImpl->Parm.cadir = caDir;
762  if (caFile) pImpl->Parm.cafile = caFile;
763  pImpl->Parm.opts = opts;
764  if (opts & crlRF) {
765  // What we store in crlRF is the time in minutes, convert it back to seconds
766  pImpl->Parm.crlRT = static_cast<int>((opts & crlRF) >> crlRS) * 60;
767  }
768 
769 // Get the correct method to use for TLS and check if successful create a
770 // server context that uses the method.
771 //
772  const SSL_METHOD *meth;
773  emsg = GetTlsMethod(meth);
774  if (emsg) FATAL(emsg);
775 
776  pImpl->ctx = SSL_CTX_new(meth);
777 
778 // Make sure we have a context here
779 //
780  if (pImpl->ctx == 0) FATAL_SSL("Unable to allocate TLS context!");
781 
782  //Add the XrdTlsContext object as extra information for OpenSSL callback re-use
783  SSL_CTX_set_ex_data(pImpl->ctx, ctxIndex, this);
784 
785 // Always prohibit SSLv2 & SSLv3 as these are not secure.
786 //
787  SSL_CTX_set_options(pImpl->ctx, sslOpts);
788 
789 // Handle session re-negotiation automatically
790 //
791 // SSL_CTX_set_mode(pImpl->ctx, sslMode);
792 
793 // Turn off the session cache as it's useless with peer cert chains
794 //
795  SSL_CTX_set_session_cache_mode(pImpl->ctx, SSL_SESS_CACHE_OFF);
796 
797 // Establish the CA cert locations, if specified. Then set the verification
798 // depth and turn on peer cert validation. For now, we don't set a callback.
799 // In the future we may to grab debugging information.
800 //
801  if ((caDir || caFile) && !(opts & clcOF))
802  {if (!SSL_CTX_load_verify_locations(pImpl->ctx, caFile, caDir))
803  FATAL_SSL("Unable to load the CA cert file or directory.");
804 
805  int vDepth = (opts & vdept) >> vdepS;
806  SSL_CTX_set_verify_depth(pImpl->ctx, (vDepth ? vDepth : 9));
807 
808  bool LogVF = (opts & logVF) != 0;
809  bool crlAllowMissingCA = (opts & crlAM) != 0;
810 
811  if (crlAllowMissingCA || LogVF) {
812  SSL_CTX_set_verify(pImpl->ctx, SSL_VERIFY_PEER, verifyPeerCB);
813  } else {
814  SSL_CTX_set_verify(pImpl->ctx, SSL_VERIFY_PEER, 0);
815  }
816 
817  unsigned long xFlags = (opts & nopxy ? 0 : X509_V_FLAG_ALLOW_PROXY_CERTS);
818  if (opts & crlON)
819  {xFlags |= X509_V_FLAG_CRL_CHECK;
820  if (opts & crlFC) xFlags |= X509_V_FLAG_CRL_CHECK_ALL;
821  }
822  if (opts) X509_STORE_set_flags(SSL_CTX_get_cert_store(pImpl->ctx),xFlags);
823  } else {
824  SSL_CTX_set_verify(pImpl->ctx, SSL_VERIFY_NONE, 0);
825  }
826 
827 // Set cipher list
828 //
829  if (!SSL_CTX_set_cipher_list(pImpl->ctx, sslCiphers))
830  FATAL_SSL("Unable to set SSL cipher list; no supported ciphers.");
831 
832 // If we need to enable eliptic-curve support, do so now. Note that for
833 // OpenSSL 1.1.0+ this is automatically done for us.
834 //
835 #if SSL_CTRL_SET_ECDH_AUTO
836  SSL_CTX_set_ecdh_auto(pImpl->ctx, 1);
837 #endif
838 
839 // We normally handle renegotiation during reads and writes or selective
840 // prohibit on a SSL socket basis. The calle may request this be applied
841 // to all SSL's generated from this context. If so, do it here.
842 //
843  if (opts & artON) SSL_CTX_set_mode(pImpl->ctx, SSL_MODE_AUTO_RETRY);
844 
845 // If there is no cert then assume this is a generic context for a client
846 //
847  if (cert == 0)
848  {ctx_tracker.Keep();
849  return;
850  }
851 
852 // We have a cert. If the key is missing then we assume the key is in the
853 // cert file (ssl will complain if it isn't).
854 //
855  if (!key) key = cert;
856 
857 // Load certificate
858 //
859  if (SSL_CTX_use_certificate_chain_file(pImpl->ctx, cert) != 1)
860  FATAL_SSL("Unable to create TLS context; invalid certificate.");
861 
862 // Load the private key
863 //
864  if (key[0] == 'p') {
865  if (!EnsureOpenSSLConfigLoaded())
866  FATAL_SSL("Unable to load OpenSSL configuration; cannot initialize pkcs11.");
867 
868 #ifdef XRDTLS_HAVE_OSSL_STORE
869  // OpenSSL 3.x+: Use OSSL_STORE API with pkcs11-provider in an ISOLATED context
870  // This prevents PKCS11 from affecting other TLS operations (e.g., SciTokens library)
871 
872  OSSL_PROVIDER *defaultProv = nullptr;
873 
874  // Initialize isolated PKCS11 context
875  // This loads the OPENSSL_CONF into an isolated context so PKCS11 doesn't affect
876  // other libraries in the process (like SciTokens)
877  if (!InitIsolatedPKCS11Context(&pImpl->pkcs11LibCtx, &pImpl->pkcs11Provider, &defaultProv)) {
878  FATAL_SSL("Failed to initialize isolated PKCS11 context. Check OPENSSL_CONF and pkcs11-provider installation.");
879  }
880 
881  // Open PKCS11 URI using the isolated context
882  OSSL_STORE_CTX *store_ctx = OSSL_STORE_open_ex(key, pImpl->pkcs11LibCtx, nullptr,
883  nullptr, nullptr, nullptr, nullptr, nullptr);
884  if (!store_ctx) {
885  if (defaultProv) OSSL_PROVIDER_unload(defaultProv);
886  FATAL_SSL("Failed to open PKCS11 URI in isolated context.");
887  }
888 
889  EVP_PKEY *priv_key = nullptr;
890  while (!OSSL_STORE_eof(store_ctx)) {
891  OSSL_STORE_INFO *info = OSSL_STORE_load(store_ctx);
892  if (info) {
893  int type = OSSL_STORE_INFO_get_type(info);
894  if (type == OSSL_STORE_INFO_PKEY) {
895  priv_key = OSSL_STORE_INFO_get1_PKEY(info);
896  OSSL_STORE_INFO_free(info);
897  break;
898  }
899  OSSL_STORE_INFO_free(info);
900  }
901  }
902  OSSL_STORE_close(store_ctx);
903  if (defaultProv) OSSL_PROVIDER_unload(defaultProv);
904 
905  if (!priv_key)
906  FATAL_SSL("Failed to load private key from PKCS11 URI in isolated context.");
907 
908  if (SSL_CTX_use_PrivateKey(pImpl->ctx, priv_key) != 1) {
909  EVP_PKEY_free(priv_key);
910  FATAL_SSL("Failed to have SSL context use private key");
911  }
912  EVP_PKEY_free(priv_key);
913 
914 #elif defined(XRDTLS_HAVE_ENGINE)
915  // OpenSSL 1.1.x (EL8): Use ENGINE API with libp11/engine_pkcs11
916  ENGINE *e = ENGINE_by_id("pkcs11");
917  if (e) {
918  const char* modulePath = getenv("PKCS11_MODULE_PATH");
919  if (modulePath && modulePath[0]) {
920  if (!ENGINE_ctrl_cmd_string(e, "MODULE_PATH", modulePath, 0)) {
921  ENGINE_free(e);
922  FATAL_SSL("Unable to configure pkcs11 engine MODULE_PATH");
923  }
924  }
925  if(!ENGINE_init(e)) {
926  ENGINE_free(e);
927  FATAL_SSL("Unable to initialize pkcs11 engine");
928  }
929  } else {
930  FATAL_SSL("Unable to create pkcs11 engine");
931  }
932  auto priv_key = ENGINE_load_private_key(e, key, nullptr, nullptr);
933 
934  if (!priv_key) {
935  FATAL_SSL("Failed to load private key through engine");
936  }
937  if (SSL_CTX_use_PrivateKey(pImpl->ctx, priv_key) != 1)
938  FATAL_SSL("Failed to have SSL context use private key");
939  EVP_PKEY_free(priv_key);
940 #else
941  FATAL_SSL("PKCS11 support not available.");
942 #endif
943 
944  } else if (SSL_CTX_use_PrivateKey_file(pImpl->ctx, key, SSL_FILETYPE_PEM) != 1 )
945  FATAL_SSL("Unable to create TLS context; invalid private key.");
946 
947 // Make sure the key and certificate file match.
948 //
949  if (SSL_CTX_check_private_key(pImpl->ctx) != 1 )
950  FATAL_SSL("Unable to create TLS context; cert-key mismatch.");
951 
952 // All went well, start the CRL refresh thread and keep the context.
953 //
954  if(opts & rfCRL) {
955  SetCrlRefresh();
956  }
957  ctx_tracker.Keep();
958 }
959 
960 /******************************************************************************/
961 /* D e s t r u c t o r */
962 /******************************************************************************/
963 
965 {
966 // We can delet eour implementation of there is no refresh thread running. If
967 // there is then the refresh thread has to delete the implementation.
968 //
969  if (pImpl->crlRunning | pImpl->flsRunning)
970  {pImpl->crlMutex.WriteLock();
971  pImpl->owner = 0;
972  pImpl->crlMutex.UnLock();
973  } else delete pImpl;
974 }
975 
976 /******************************************************************************/
977 /* C l o n e */
978 /******************************************************************************/
979 
980 XrdTlsContext *XrdTlsContext::Clone(bool full,bool startCRLRefresh)
981 {
982  XrdTlsContext::CTX_Params &my = pImpl->Parm;
983  const char *cert = (my.cert.size() ? my.cert.c_str() : 0);
984  const char *pkey = (my.pkey.size() ? my.pkey.c_str() : 0);
985  const char *caD = (my.cadir.size() ? my.cadir.c_str() : 0);
986  const char *caF = (my.cafile.size() ? my.cafile.c_str() : 0);
987 
988 // If this is a non-full context, get rid of any verification
989 //
990  if (!full) caD = caF = 0;
991 
992 // Cloning simply means getting a object with the old parameters.
993 //
994  uint64_t myOpts = my.opts;
995  if(startCRLRefresh){
997  } else {
999  }
1000  XrdTlsContext *xtc = new XrdTlsContext(cert, pkey, caD, caF, myOpts);
1001 
1002 // Verify that the context was built
1003 //
1004  if (xtc->isOK()) {
1005  if(pImpl->sessionCacheOpts != -1){
1006  //A SessionCache() call was done for the current context, so apply it for this new cloned context
1007  xtc->SessionCache(pImpl->sessionCacheOpts,pImpl->sessionCacheId.c_str(),pImpl->sessionCacheId.size());
1008  }
1009  return xtc;
1010  }
1011 
1012 // We failed, cleanup.
1013 //
1014  delete xtc;
1015  return 0;
1016 }
1017 
1018 /******************************************************************************/
1019 /* C o n t e x t */
1020 /******************************************************************************/
1021 
1023 {
1024  return pImpl->ctx;
1025 }
1026 
1027 /******************************************************************************/
1028 /* G e t P a r a m s */
1029 /******************************************************************************/
1030 
1032 {
1033  return &pImpl->Parm;
1034 }
1035 
1036 /******************************************************************************/
1037 /* I n i t */
1038 /******************************************************************************/
1039 
1040 const char *XrdTlsContext::Init()
1041 {
1042 
1043 // Disallow use if this object unless SSL provides thread-safety!
1044 //
1045 #ifndef OPENSSL_THREADS
1046  return "Installed OpenSSL lacks the required thread support!";
1047 #endif
1048 
1049 // Initialize the library (one time call)
1050 //
1051  InitTLS();
1052  return 0;
1053 }
1054 
1055 /******************************************************************************/
1056 /* i s O K */
1057 /******************************************************************************/
1058 
1060 {
1061  return pImpl->ctx != 0;
1062 }
1063 
1064 /******************************************************************************/
1065 /* S e s s i o n */
1066 /******************************************************************************/
1067 
1068 // Note: The reason we handle the x509 store update here is because allow the
1069 // SSL context to be exported and then have no lock control over it. This may
1070 // happen for transient purposes other than creating sessions. Once we
1071 // disallow direct access to the context, the exchange can happen in the
1072 // refresh thread which simplifies this whole process.
1073 
1075 {
1076  EPNAME("Session");
1077  SSL *ssl;
1078 
1079 // Check if we have a refreshed context. If so, we need to replace the X509
1080 // store in the current context with the new one before we create the session.
1081 //
1082  pImpl->crlMutex.ReadLock();
1083  if (!(pImpl->ctxnew))
1084  {ssl = SSL_new(pImpl->ctx);
1085  pImpl->crlMutex.UnLock();
1086  return ssl;
1087  }
1088 
1089 // Things have changed, so we need to take the long route here. We need to
1090 // replace the x509 cache with the current cache. Get a R/W lock now.
1091 //
1092  pImpl->crlMutex.UnLock();
1093  pImpl->crlMutex.WriteLock();
1094 
1095 // If some other thread beat us to the punch, just return what we have.
1096 //
1097  if (!(pImpl->ctxnew))
1098  {ssl = SSL_new(pImpl->ctx);
1099  pImpl->crlMutex.UnLock();
1100  return ssl;
1101  }
1102 
1103 // Do some tracing
1104 //
1105  DBG_CTX("Replacing x509 store with new contents.");
1106 
1107 // Get the new store and set it in our context. Setting the store is black
1108 // magic. For OpenSSL < 1.1, Two stores need to be set with the "set1" variant.
1109 // Newer version only require SSL_CTX_set1_cert_store() to be used.
1110 //
1111  //We have a new context generated by Refresh, so we must use it.
1112  XrdTlsContext * ctxnew = pImpl->ctxnew;
1113 
1114  /*X509_STORE *newX509 = SSL_CTX_get_cert_store(ctxnew->pImpl->ctx);
1115  SSL_CTX_set1_verify_cert_store(pImpl->ctx, newX509);
1116  SSL_CTX_set1_chain_cert_store(pImpl->ctx, newX509);*/
1117  //The above two macros actually do not replace the certificate that has
1118  //to be used for that SSL session, so we will create the session with the SSL_CTX * of
1119  //the TlsContext created by Refresh()
1120  //First, free the current SSL_CTX, if it is used by any transfer, it will just decrease
1121  //the reference counter of it. There is therefore no risk of double free...
1122  SSL_CTX_free(pImpl->ctx);
1123  pImpl->ctx = ctxnew->pImpl->ctx;
1124 
1125  //Update ex_data to point to this (the surviving owner), not the
1126  //cloned context which is about to be deleted.
1127  SSL_CTX_set_ex_data(pImpl->ctx, ctxIndex, this);
1128 
1129  //In the destructor of XrdTlsContextImpl, SSL_CTX_Free() is
1130  //called if ctx is != 0. As this new ctx is used by the session
1131  //we just created, we don't want that to happen. We therefore set it to 0.
1132  //The SSL_free called on the session will cleanup the context for us.
1133  ctxnew->pImpl->ctx = 0;
1134 
1135 // Save the generated context and clear it's presence
1136 //
1137  XrdTlsContext *ctxold = pImpl->ctxnew;
1138  pImpl->ctxnew = 0;
1139 
1140 // Generate a new session (might as well to keep the lock we have)
1141 //
1142  ssl = SSL_new(pImpl->ctx);
1143 
1144 // OK, now we can drop all the locks and get rid of the old context
1145 //
1146  pImpl->crlMutex.UnLock();
1147  delete ctxold;
1148  return ssl;
1149 }
1150 
1151 /******************************************************************************/
1152 /* S e s s i o n C a c h e */
1153 /******************************************************************************/
1154 
1155 int XrdTlsContext::SessionCache(int opts, const char *id, int idlen)
1156 {
1157  static const int doSet = scSrvr | scClnt | scOff;
1158  long sslopt = 0;
1159  int flushT = opts & scFMax;
1160 
1161  pImpl->sessionCacheOpts = opts;
1162  pImpl->sessionCacheId = id;
1163 
1164 // If initialization failed there is nothing to do
1165 //
1166  if (pImpl->ctx == 0) return 0;
1167 
1168 // Set options as appropriate
1169 //
1170  if (opts & doSet)
1171  {if (opts & scOff) sslopt = SSL_SESS_CACHE_OFF;
1172  else {if (opts & scSrvr) sslopt = SSL_SESS_CACHE_SERVER;
1173  if (opts & scClnt) sslopt |= SSL_SESS_CACHE_CLIENT;
1174  }
1175  }
1176 
1177 // Check if we should set any cache options or simply get them
1178 //
1179  if (!(opts & doSet)) sslopt = SSL_CTX_get_session_cache_mode(pImpl->ctx);
1180  else {sslopt = SSL_CTX_set_session_cache_mode(pImpl->ctx, sslopt);
1181  if (opts & scOff) SSL_CTX_set_options(pImpl->ctx, SSL_OP_NO_TICKET);
1182  }
1183 
1184 // Compute what he previous cache options were
1185 //
1186  opts = scNone;
1187  if (sslopt & SSL_SESS_CACHE_SERVER) opts |= scSrvr;
1188  if (sslopt & SSL_SESS_CACHE_CLIENT) opts |= scClnt;
1189  if (!opts) opts = scOff;
1190  if (sslopt & SSL_SESS_CACHE_NO_AUTO_CLEAR) opts |= scKeep;
1191  opts |= (static_cast<int>(pImpl->flushT) & scFMax);
1192 
1193 // Set the id is so wanted
1194 //
1195  if (id && idlen > 0)
1196  {if (!SSL_CTX_set_session_id_context(pImpl->ctx,
1197  (unsigned const char *)id,
1198  (unsigned int)idlen)) opts |= scIdErr;
1199  }
1200 
1201 // If a flush interval was specified and it is different from what we have
1202 // then reset the flush interval.
1203 //
1204  if (flushT && flushT != pImpl->flushT)
1205  XrdTlsFlush::Setup_Flusher(pImpl, flushT);
1206 
1207 // All done
1208 //
1209  return opts;
1210 }
1211 
1212 /******************************************************************************/
1213 /* S e t C o n t e x t C i p h e r s */
1214 /******************************************************************************/
1215 
1216 bool XrdTlsContext::SetContextCiphers(const char *ciphers)
1217 {
1218  if (pImpl->ctx && SSL_CTX_set_cipher_list(pImpl->ctx, ciphers)) return true;
1219 
1220  char eBuff[2048];
1221  snprintf(eBuff,sizeof(eBuff),"Unable to set context ciphers '%s'",ciphers);
1222  Fatal(0, eBuff, true);
1223  return false;
1224 }
1225 
1226 /******************************************************************************/
1227 /* S e t D e f a u l t C i p h e r s */
1228 /******************************************************************************/
1229 
1230 void XrdTlsContext::SetDefaultCiphers(const char *ciphers)
1231 {
1232  sslCiphers = ciphers;
1233 }
1234 
1235 /******************************************************************************/
1236 /* S e t C r l R e f r e s h */
1237 /******************************************************************************/
1238 
1240 {
1241  pthread_t tid;
1242  int rc;
1243 
1244 // If it's negative or equal to 0, use the current setting
1245 //
1246  if (refsec <= 0)
1247  {pImpl->crlMutex.WriteLock();
1248  refsec = pImpl->Parm.crlRT;
1249  pImpl->crlMutex.UnLock();
1250  if (!refsec) refsec = XrdTlsContext::DEFAULT_CRL_REF_INT_SEC;
1251  }
1252 
1253 // Make sure this is at least 60 seconds between refreshes
1254 //
1255 // if (refsec < 60) refsec = 60;
1256 
1257 // We will set the new interval and start a refresh thread if not running.
1258 //
1259  pImpl->crlMutex.WriteLock();
1260  pImpl->Parm.crlRT = refsec;
1261  if (!pImpl->crlRunning)
1262  {if ((rc = XrdSysThread::Run(&tid, XrdTlsCrl::Refresh, (void *)pImpl,
1263  0, "CRL Refresh")))
1264  {char eBuff[512];
1265  snprintf(eBuff, sizeof(eBuff),
1266  "Unable to start CRL refresh thread; rc=%d", rc);
1267  XrdTls::Emsg("CrlRefresh:", eBuff, false);
1268  pImpl->crlMutex.UnLock();
1269  return false;
1270  } else pImpl->crlRunning = true;
1271  pImpl->crlMutex.UnLock();
1272  }
1273 
1274 // All done
1275 //
1276  return true;
1277 }
1278 
1279 /******************************************************************************/
1280 /* x 5 0 9 V e r i f y */
1281 /******************************************************************************/
1282 
1284 {
1285  return !(pImpl->Parm.cadir.empty()) || !(pImpl->Parm.cafile.empty());
1286 }
1287 
1289  const std::string certPath = pImpl->Parm.cert;
1290  if(certPath.empty()) {
1291  //No certificate provided, should not happen though
1292  return false;
1293  }
1294  time_t modificationTime;
1295  if(!XrdOucUtils::getModificationTime(certPath.c_str(),modificationTime)){
1296  if (pImpl->lastCertModTime != modificationTime) {
1297  //The certificate file has changed
1298  pImpl->lastCertModTime = modificationTime;
1299  return true;
1300  }
1301  }
1302  return false;
1303 }
1304 
1306  if (setting)
1307  {pImpl->Parm.opts &= ~clcOF;
1308  bool LogVF = (pImpl->Parm.opts & logVF) != 0;
1309  bool crlAllowMissingCA = (pImpl->Parm.opts & crlAM) != 0;
1310 
1311  if (LogVF || crlAllowMissingCA)
1312  SSL_CTX_set_verify(pImpl->ctx, SSL_VERIFY_PEER, verifyPeerCB);
1313  else
1314  SSL_CTX_set_verify(pImpl->ctx, SSL_VERIFY_PEER, 0);
1315  } else
1316  {pImpl->Parm.opts |= clcOF;
1317  SSL_CTX_set_verify(pImpl->ctx, SSL_VERIFY_NONE, 0);
1318  }
1319 }
#define EPNAME(x)
Definition: XrdBwmTrace.hh:56
struct stat Stat
Definition: XrdCks.cc:49
void Fatal(const char *op, const char *target)
Definition: XrdCrc32c.cc:58
#define stat(a, b)
Definition: XrdPosix.hh:105
#define eMsg(x)
struct myOpts opts
int emsg(int rc, char *msg)
if(Avsz)
void sslTLS_lock(int mode, int n, const char *file, int line)
#define FATAL_SSL(msg)
#define FATAL(msg)
XrdSysMutex * MutexVector
unsigned long sslTLS_id_callback(void)
#define DBG_CTX(y)
Definition: XrdTlsTrace.hh:39
#define TRACING(x)
Definition: XrdTrace.hh:70
static int getModificationTime(const char *path, time_t &modificationTime)
static const char * ValPath(const char *path, mode_t allow, bool isdir)
static int Run(pthread_t *, void *(*proc)(void *), void *arg, int opts=0, const char *desc=0)
static pthread_t ID(void)
static void Snooze(int seconds)
Definition: XrdSysTimer.cc:168
static const int scIdErr
Info: Id not set, is too long.
XrdTlsContext * Clone(bool full=true, bool startCRLRefresh=false)
~XrdTlsContext()
Destructor.
static const uint64_t vdept
Mask to isolate vdept.
static const int crlRS
Bits to shift vdept.
int SessionCache(int opts=scNone, const char *id=0, int idlen=0)
static void SetDefaultCiphers(const char *ciphers)
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)
static const uint64_t clcOF
Disable client certificate request.
static const int scClnt
Turn on cache client mode.
static const int DEFAULT_CRL_REF_INT_SEC
Default CRL refresh interval in seconds.
static const uint64_t servr
This is a server context.
static const uint64_t rfCRL
Turn on the CRL refresh thread.
static const int scKeep
Info: TLS-controlled flush disabled.
static const uint64_t nopxy
Do not allow proxy certs.
static const int scNone
Do not change any option settings.
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.
const CTX_Params * GetParams()
static const int scOff
Turn off cache.
static const char * Init()
bool newHostCertificateDetected()
bool SetContextCiphers(const char *ciphers)
static const int scFMax
static const uint64_t crlAM
Allow CA validation when CRL is missing (CRL soft-fail)
bool SetCrlRefresh(int refsec=-1)
static const int scSrvr
Turn on cache server mode (default)
void SetTlsClientAuth(bool setting)
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 void Emsg(const char *tid, const char *msg=0, bool flush=true)
Definition: XrdTls.cc: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
bool InitTLS()
Definition: XrdClTls.cc:96
void * Refresh(void *parg)
void * Flusher(void *parg)
bool Setup_Flusher(XrdTlsContextImpl *pImpl, int flushT)
XrdSysTrace SysTrace("TLS", 0)
XrdTlsContextImpl(XrdTlsContext *p)
std::string sessionCacheId
XrdTlsContext * owner
XrdTlsContext::CTX_Params Parm
XrdTlsContext * ctxnew
XrdSysCondVar * flsCVar
XrdSysRWLock crlMutex
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.
static unsigned long mixer(unsigned long x)
static unsigned long mixer(unsigned long x)