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