XRootD
Loading...
Searching...
No Matches
XrdTlsTempCA.cc
Go to the documentation of this file.
1/******************************************************************************/
2/* */
3/* X r d T l s T e m p C A . c c */
4/* */
5/* (c) 2021 by the Board of Trustees of the Leland Stanford, Jr., University */
6/* Produced by Brian Bockelman */
7/* */
8/* This file is part of the XRootD software suite. */
9/* */
10/* XRootD is free software: you can redistribute it and/or modify it under */
11/* the terms of the GNU Lesser General Public License as published by the */
12/* Free Software Foundation, either version 3 of the License, or (at your */
13/* option) any later version. */
14/* */
15/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
16/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
17/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
18/* License for more details. */
19/* */
20/* You should have received a copy of the GNU Lesser General Public License */
21/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
22/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
23/* */
24/* The copyright holder's institutional names and contributor's names may not */
25/* be used to endorse or promote products derived from this software without */
26/* specific prior written permission of the institution or contributor. */
27/******************************************************************************/
28
29
30#include <cstdlib>
31#include <fcntl.h>
32#include <dirent.h>
33#include <poll.h>
34
35#include <unordered_set>
36#include <memory>
37
38#include "XrdSys/XrdSysError.hh"
39#include "XrdSys/XrdSysFD.hh"
44#include "XrdVersion.hh"
45
46#include "XrdTlsTempCA.hh"
47
48#include <sstream>
49#include <vector>
50#include <atomic>
51
52namespace {
53
54typedef std::unique_ptr<FILE, int(*)(FILE*)> file_smart_ptr;
55
56
57static uint64_t monotonic_time_s() {
58 struct timespec tp;
59 clock_gettime(CLOCK_MONOTONIC, &tp);
60 return tp.tv_sec + (tp.tv_nsec >= 500000000);
61}
62
67class Set {
68public:
69 Set(int output_fd, XrdSysError & err) : m_log(err),m_output_fp(file_smart_ptr(fdopen(XrdSysFD_Dup(output_fd), "w"), &fclose)){
70 if(!m_output_fp.get()) {
71 m_output_fp.reset();
72 }
73 }
74 virtual ~Set() = default;
75protected:
76 // Reference to the logging that can be used by the inheriting classes.
77 XrdSysError &m_log;
78 // Pointer to the CA or CRL output file
79 file_smart_ptr m_output_fp;
80};
81
82class CASet : public Set {
83public:
84 CASet(int output_fd, XrdSysError &err):Set(output_fd,err){}
85
97 bool processFile(file_smart_ptr &fd, const std::string &fname);
98
99private:
100
101 // Grid CA directories tend to keep everything in triplicate;
102 // we keep a unique hash of all known CAs so we write out each
103 // one only once.
104 std::unordered_set<std::string> m_known_cas;
105};
106
107
108bool
109CASet::processFile(file_smart_ptr &fp, const std::string &fname)
110{
111 XrdCryptoX509Chain chain;
112 // Not checking return value here; function returns `0` on error and
113 // if no certificate is found.
114 XrdCryptosslX509ParseFile(fp.get(), &chain, fname.c_str());
115
116 auto ca = chain.Begin();
117 if (!m_output_fp.get()) {
118 m_log.Emsg("CAset", "No output file has been opened", fname.c_str());
119 chain.Cleanup();
120 return false;
121 }
122 while (ca) {
123 auto hash_ptr = ca->SubjectHash();
124 if (!hash_ptr) {
125 continue;
126 }
127 auto iter = m_known_cas.find(hash_ptr);
128 if (iter != m_known_cas.end()) {
129 //m_log.Emsg("CAset", "Skipping known CA with hash", fname.c_str(), hash_ptr);
130 ca = chain.Next();
131 continue;
132 }
133 //m_log.Emsg("CAset", "New CA with hash", fname.c_str(), hash_ptr);
134 m_known_cas.insert(hash_ptr);
135
136 if (XrdCryptosslX509ToFile(ca, m_output_fp.get(), fname.c_str())) {
137 m_log.Emsg("CAset", "Failed to write out CA", fname.c_str());
138 chain.Cleanup();
139 return false;
140 }
141 ca = chain.Next();
142 }
143 fflush(m_output_fp.get());
144 chain.Cleanup();
145
146 return true;
147}
148
149
150class CRLSet : public Set {
151public:
152 CRLSet(int output_fd, XrdSysError &err):Set(output_fd,err){}
164 bool processFile(file_smart_ptr &fd, const std::string &fname);
170 bool atLeastOneValidCRLFound() const;
177 bool processCRLWithCriticalExt();
178
179private:
180
181 // Grid CA directories tend to keep everything in triplicate;
182 // we keep a unique hash of all known CRLs so we write out each
183 // one only once.
184 std::unordered_set<std::string> m_known_crls;
185 std::atomic<bool> m_atLeastOneValidCRLFound;
186 //Store the CRLs containing critical extensions to defer their insertion
187 //at the end of the bundled CRL file. Issue https://github.com/xrootd/xrootd/issues/2065
188 std::vector<std::unique_ptr<XrdCryptosslX509Crl>> m_crls_critical_extension;
189};
190
191
192bool
193CRLSet::processFile(file_smart_ptr &fp, const std::string &fname)
194{
195 if (!m_output_fp.get()) {
196 m_log.Emsg("CRLSet", "No output file has been opened", fname.c_str());
197 return false;
198 }
199 // Assume we can safely ignore a failure to parse; we load every file in
200 // the directory and that will naturally include a number of non-CRL files.
201 for (std::unique_ptr<XrdCryptosslX509Crl> xrd_crl(new XrdCryptosslX509Crl(fp.get(), fname.c_str()));
202 xrd_crl->IsValid();
203 xrd_crl = std::unique_ptr<XrdCryptosslX509Crl>(new XrdCryptosslX509Crl(fp.get(), fname.c_str())))
204 {
205 auto hash_ptr = xrd_crl->IssuerHash(1);
206 if (!hash_ptr) {
207 continue;
208 }
209 m_atLeastOneValidCRLFound = true;
210 auto iter = m_known_crls.find(hash_ptr);
211 if (iter != m_known_crls.end()) {
212 //m_log.Emsg("CRLset", "Skipping known CRL with hash", fname.c_str(), hash_ptr);
213 continue;
214 }
215 //m_log.Emsg("CRLset", "New CRL with hash", fname.c_str(), hash_ptr);
216 m_known_crls.insert(hash_ptr);
217
218 if(xrd_crl->hasCriticalExtension()) {
219 // Issue https://github.com/xrootd/xrootd/issues/2065
220 // This CRL will be put at the end of the bundled file
221 m_crls_critical_extension.emplace_back(std::move(xrd_crl));
222 } else {
223 // No critical extension found on that CRL, just insert it on the CRL bundled file
224 if (!xrd_crl->ToFile(m_output_fp.get())) {
225 m_log.Emsg("CRLset", "Failed to write out CRL", fname.c_str());
226 fflush(m_output_fp.get());
227 return false;
228 }
229 }
230 }
231 fflush(m_output_fp.get());
232
233 return true;
234}
235
236bool CRLSet::atLeastOneValidCRLFound() const {
237 return m_atLeastOneValidCRLFound;
238}
239
240bool CRLSet::processCRLWithCriticalExt() {
241 if(!m_crls_critical_extension.empty()) {
242 if (!m_output_fp.get()) {
243 m_log.Emsg("CRLSet", "No output file has been opened to add CRLs with critical extension");
244 return false;
245 }
246 for (const auto &crl: m_crls_critical_extension) {
247 if (!crl->ToFile(m_output_fp.get())) {
248 m_log.Emsg("CRLset", "Failed to write out CRL with critical extension", crl->ParentFile());
249 fflush(m_output_fp.get());
250 return false;
251 }
252 }
253 fflush(m_output_fp.get());
254 }
255 return true;
256}
257
258}
259
260
261std::unique_ptr<XrdTlsTempCA::TempCAGuard>
262XrdTlsTempCA::TempCAGuard::create(XrdSysError &err, const std::string &ca_tmp_dir) {
263
264 if (-1 == mkdir(ca_tmp_dir.c_str(), S_IRWXU) && errno != EEXIST) {
265 err.Emsg("TempCA", "Unable to create CA temp directory", ca_tmp_dir.c_str(), strerror(errno));
266 }
267
268 std::stringstream ss;
269 ss << ca_tmp_dir << "/ca_file.XXXXXX.pem";
270 std::vector<char> ca_fname;
271 ca_fname.resize(ss.str().size() + 1);
272 memcpy(ca_fname.data(), ss.str().c_str(), ss.str().size());
273
274 int ca_fd = mkstemps(ca_fname.data(), 4);
275 if (ca_fd < 0) {
276 err.Emsg("TempCA", "Failed to create temp file:", strerror(errno));
277 return std::unique_ptr<TempCAGuard>();
278 }
279
280 std::stringstream ss2;
281 ss2 << ca_tmp_dir << "/crl_file.XXXXXX.pem";
282 std::vector<char> crl_fname;
283 crl_fname.resize(ss2.str().size() + 1);
284 memcpy(crl_fname.data(), ss2.str().c_str(), ss2.str().size());
285
286 int crl_fd = mkstemps(crl_fname.data(), 4);
287 if (crl_fd < 0) {
288 err.Emsg("TempCA", "Failed to create temp file:", strerror(errno));
289 return std::unique_ptr<TempCAGuard>();
290 }
291 return std::unique_ptr<TempCAGuard>(new TempCAGuard(ca_fd, crl_fd, ca_tmp_dir, ca_fname.data(), crl_fname.data()));
292}
293
294
296 if (m_ca_fd >= 0) {
297 unlink(m_ca_fname.c_str());
298 close(m_ca_fd);
299 }
300 if (m_crl_fd >= 0) {
301 unlink(m_crl_fname.c_str());
302 close(m_crl_fd);
303 }
304}
305
306
307bool
309 if (m_ca_fd < 0 || m_ca_tmp_dir.empty()) {return false;}
310 close(m_ca_fd);
311 m_ca_fd = -1;
312 std::string ca_fname = m_ca_tmp_dir + "/ca_file.pem";
313 if (-1 == rename(m_ca_fname.c_str(), ca_fname.c_str())) {
314 return false;
315 }
316 m_ca_fname = ca_fname;
317
318 if (m_crl_fd < 0 || m_ca_tmp_dir.empty()) {return false;}
319 close(m_crl_fd);
320 m_crl_fd = -1;
321 std::string crl_fname = m_ca_tmp_dir + "/crl_file.pem";
322 if (-1 == rename(m_crl_fname.c_str(), crl_fname.c_str())) {
323 return false;
324 }
325 m_crl_fname = crl_fname;
326
327 return true;
328}
329
330
331XrdTlsTempCA::TempCAGuard::TempCAGuard(int ca_fd, int crl_fd, const std::string &ca_tmp_dir, const std::string &ca_fname, const std::string &crl_fname)
332 : m_ca_fd(ca_fd), m_crl_fd(crl_fd), m_ca_tmp_dir(ca_tmp_dir), m_ca_fname(ca_fname), m_crl_fname(crl_fname)
333 {}
334
335
336XrdTlsTempCA::XrdTlsTempCA(XrdSysError *err, std::string ca_dir, bool build_store)
337 : m_log(*err),
338 m_ca_dir(ca_dir),
339 m_build_store(build_store)
340{
341 // Setup communication pipes; we write one byte to the child to tell it to shutdown;
342 // it'll write one byte back to acknowledge before our destructor exits.
343 int pipes[2];
344 if (-1 == XrdSysFD_Pipe(pipes)) {
345 m_log.Emsg("XrdTlsTempCA", "Failed to create communication pipes", strerror(errno));
346 return;
347 }
348 m_maintenance_pipe_r = pipes[0];
349 m_maintenance_pipe_w = pipes[1];
350 if (-1 == XrdSysFD_Pipe(pipes)) {
351 m_log.Emsg("XrdTlsTempCA", "Failed to create communication pipes", strerror(errno));
352 return;
353 }
354 m_maintenance_thread_pipe_r = pipes[0];
355 m_maintenance_thread_pipe_w = pipes[1];
356 if (!Maintenance()) {return;}
357
358 pthread_t tid;
359 auto rc = XrdSysThread::Run(&tid, XrdTlsTempCA::MaintenanceThread,
360 static_cast<void*>(this), 0, "CA/CRL refresh");
361 if (rc) {
362 m_log.Emsg("XrdTlsTempCA", "Failed to launch CA monitoring thread");
363 m_ca_file.reset();
364 m_crl_file.reset();
365 }
366}
367
368
370{
371 char indicator[1];
372 if (m_maintenance_pipe_w >= 0) {
373 indicator[0] = '1';
374 int rval;
375 do {rval = write(m_maintenance_pipe_w, indicator, 1);} while (rval != -1 || errno == EINTR);
376 if (m_maintenance_thread_pipe_r >= 0) {
377 do {rval = read(m_maintenance_thread_pipe_r, indicator, 1);} while (rval != -1 || errno == EINTR);
378 close(m_maintenance_thread_pipe_r);
379 close(m_maintenance_thread_pipe_w);
380 }
381 close(m_maintenance_pipe_r);
382 close(m_maintenance_pipe_w);
383 }
384}
385
386
387std::shared_ptr<X509_STORE>
388XrdTlsTempCA::BuildCAStore(const std::string &ca_fname, const std::string &crl_fname,
389 bool use_crls)
390{
391 std::shared_ptr<X509_STORE> store(X509_STORE_new(), &X509_STORE_free);
392 if (!store) {
393 m_log.Emsg("TempCA", "Failed to allocate a certificate store");
394 return nullptr;
395 }
396
397 if (1 != X509_STORE_load_locations(store.get(), ca_fname.c_str(), nullptr)) {
398 m_log.Emsg("TempCA", "Failed to load the CA bundle into the certificate store",
399 ca_fname.c_str());
400 return nullptr;
401 }
402
403 // The verification flags below mirror what libcurl applies when it is handed
404 // these same files through CURLOPT_CAINFO / CURLOPT_CRLFILE; see
405 // ossl_populate_x509_store() in its lib/vtls/openssl.c. Consumers install this
406 // store in place of the one libcurl built, so any flag left out here is lost.
407 unsigned long x509flags;
408
409 if (use_crls) {
410 X509_LOOKUP *lookup = X509_STORE_add_lookup(store.get(), X509_LOOKUP_file());
411 if (!lookup) {
412 m_log.Emsg("TempCA", "Failed to add a file lookup to the certificate store");
413 return nullptr;
414 }
415 if (X509_load_crl_file(lookup, crl_fname.c_str(), X509_FILETYPE_PEM) <= 0) {
416 m_log.Emsg("TempCA", "Failed to load the CRL bundle into the certificate store",
417 crl_fname.c_str());
418 return nullptr;
419 }
420 x509flags = X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL;
421 } else {
422 // Treat non-self-signed certificates in the store as trust anchors, so that
423 // a server can be verified from an intermediate alone. This is not an
424 // OpenSSL default, but libcurl enables it unless asked not to, so leaving it
425 // out would reject chains that are accepted today. It is deliberately not
426 // combined with CRL checking, which OpenSSL does not support:
427 // https://github.com/openssl/openssl/issues/5081
428 x509flags = X509_V_FLAG_PARTIAL_CHAIN;
429 }
430
431 X509_STORE_set_flags(store.get(), x509flags);
432
433 // Purely an optimization; OpenSSL sorts the store itself when it needs to.
434 // Adding objects to a store leaves its lookup stack unsorted, and OpenSSL
435 // sorts lazily on first use -- under a write lock, while every other thread
436 // verifying against this store waits. Sorting here means the concurrent
437 // verifications that follow a reload only ever need the read lock.
438 sk_X509_OBJECT_sort(X509_STORE_get0_objects(store.get()));
439
440 return store;
441}
442
443
444bool
445XrdTlsTempCA::Maintenance()
446{
447 m_log.Emsg("TempCA", "Reloading the list of CAs and CRLs in directory");
448
449 auto adminpath = getenv("XRDADMINPATH");
450 if (!adminpath) {
451 m_log.Emsg("TempCA", "Admin path is not set!");
452 return false;
453 }
454 std::string ca_tmp_dir = std::string(adminpath) + "/.xrdtls";
455
456 std::unique_ptr<TempCAGuard> new_file(TempCAGuard::create(m_log, ca_tmp_dir));
457 if (!new_file) {
458 m_log.Emsg("TempCA", "Failed to create a new temp CA / CRL file");
459 return false;
460 }
461
462 int fddir = XrdSysFD_Open(m_ca_dir.c_str(), O_DIRECTORY);
463 if (fddir < 0) {
464 m_log.Emsg("TempCA", "Failed to open the CA directory", m_ca_dir.c_str());
465 return false;
466 }
467
468 DIR *dirp = fdopendir(fddir);
469 if (!dirp) {
470 m_log.Emsg("Maintenance", "Failed to allocate a directory pointer");
471 return false;
472 }
473
474 struct dirent *result;
475 bool atLeastOneCRLFound = false;
476 errno = 0;
477 {
478 CASet ca_builder(new_file->getCAFD(), m_log);
479 CRLSet crl_builder(new_file->getCRLFD(), m_log);
480 while ((result = readdir(dirp))) {
481 //m_log.Emsg("Will parse file for CA certificates", result->d_name);
482 if (result->d_name[0] == '.') {continue;}
483 if (result->d_type != DT_REG)
484 {if (result->d_type != DT_UNKNOWN && result->d_type != DT_LNK)
485 continue;
486 struct stat Stat;
487 if (fstatat(fddir, result->d_name, &Stat, 0))
488 {m_log.Emsg("Maintenance", "Failed to stat certificate file",
489 result->d_name, strerror(errno));
490 continue;
491 }
492 if (!S_ISREG(Stat.st_mode)) continue;
493 }
494 int fd = XrdSysFD_Openat(fddir, result->d_name, O_RDONLY);
495 if (fd < 0) {
496 m_log.Emsg("Maintenance", "Failed to open certificate file", result->d_name, strerror(errno));
497 closedir(dirp);
498 return false;
499 }
500 file_smart_ptr fp(fdopen(fd, "r"), &fclose);
501
502 if (!ca_builder.processFile(fp, result->d_name)) {
503 m_log.Emsg("Maintenance", "Failed to process file for CAs", result->d_name);
504 }
505 rewind(fp.get());
506 if (!crl_builder.processFile(fp, result->d_name)) {
507 m_log.Emsg("Maintenance", "Failed to process file for CRLs", result->d_name);
508 }
509 errno = 0;
510 }
511 if (errno) {
512 m_log.Emsg("Maintenance", "Failure during readdir", strerror(errno));
513 closedir(dirp);
514 return false;
515 }
516 closedir(dirp);
517
518 if (!crl_builder.processCRLWithCriticalExt()) {
519 m_log.Emsg("Maintenance", "Failed to insert CRLs with critical extension for CRLs", result->d_name);
520 }
521 atLeastOneCRLFound = crl_builder.atLeastOneValidCRLFound();
522 }
523
524 if (!new_file->commit()) {
525 m_log.Emsg("Maintenance", "Failed to finalize new CA / CRL files");
526 return false;
527 }
528 //m_log.Emsg("Maintenance", "Successfully created CA and CRL files", new_file->getCAFilename().c_str(),
529 // new_file->getCRLFilename().c_str());
530 const std::string ca_fname = new_file->getCAFilename();
531 const std::string crl_fname = new_file->getCRLFilename();
532
533 std::shared_ptr<X509_STORE> new_store;
534 if (m_build_store) {
535 // An empty CRL bundle makes every verification fail, so CRL checking is only
536 // enabled once we know at least one CRL was written out.
537 // See https://github.com/xrootd/xrootd/issues/1543
538 struct stat crl_stat;
539 const bool use_crls = atLeastOneCRLFound
540 && !stat(crl_fname.c_str(), &crl_stat)
541 && crl_stat.st_size > 0;
542 if (!use_crls) {
543 std::stringstream ss;
544 ss << "No valid CRL file has been found in the file " << crl_fname
545 << ". Disabling CRL checking.";
546 m_log.Emsg("Maintenance", ss.str().c_str());
547 }
548
549 // Parse the bundles once here rather than once per consumer. This is the
550 // expensive part of a reload, so it is deliberately done before taking the
551 // lock that publishes the result.
552 new_store = BuildCAStore(ca_fname, crl_fname, use_crls);
553 if (!new_store) {
554 // Deliberately publish nothing and let the maintenance thread retry on
555 // the short interval, leaving consumers on the previous store. The
556 // tempting alternative -- carrying on and letting each consumer load the
557 // bundles for itself -- silently reinstates the per-transfer parsing that
558 // this store exists to avoid, turning a CA refresh problem into memory
559 // exhaustion. See https://github.com/xrootd/xrootd/issues/2873
560 m_log.Emsg("Maintenance", "Failed to build the certificate store; "
561 "retaining the previously loaded CAs and CRLs");
562 return false;
563 }
564 }
565
566 XrdSysMutexHelper lock(m_mutex);
567 m_ca_file.reset(new std::string(ca_fname));
568 m_crl_file.reset(new std::string(crl_fname));
569 m_atLeastOneCRLFound = atLeastOneCRLFound;
570 m_ca_store = std::move(new_store);
571
572 return true;
573}
574
575
576void *XrdTlsTempCA::MaintenanceThread(void *myself_raw)
577{
578 auto myself = static_cast<XrdTlsTempCA *>(myself_raw);
579
580 auto now = monotonic_time_s();
581 auto next_update = now + m_update_interval;
582 while (true) {
583 now = monotonic_time_s();
584 auto remaining = next_update - now;
585 struct pollfd fds;
586 fds.fd = myself->m_maintenance_pipe_r;
587 fds.events = POLLIN;
588 auto rval = poll(&fds, 1, remaining*1000);
589 if (rval == -1) {
590 if (rval == EINTR) continue;
591 else break;
592 } else if (rval == 0) { // timeout! Let's run maintenance.
593 if (myself->Maintenance()) {
594 next_update = monotonic_time_s() + m_update_interval;
595 } else {
596 next_update = monotonic_time_s() + m_update_interval_failure;
597 }
598 } else { // FD ready; let's shutdown
599 if (fds.revents & POLLIN) {
600 char indicator[1];
601 do {rval = read(myself->m_maintenance_pipe_r, indicator, 1);} while (rval != -1 || errno == EINTR);
602 }
603 }
604 }
605 if (errno) {
606 myself->m_log.Emsg("Maintenance", "Failed to poll for events from parent object");
607 }
608 char indicator = '1';
609 int rval;
610 do {rval = write(myself->m_maintenance_thread_pipe_w, &indicator, 1);} while (rval != -1 || errno == EINTR);
611
612 return nullptr;
613}
struct stat Stat
Definition XrdCks.cc:49
int XrdCryptosslX509ToFile(XrdCryptoX509 *x509, FILE *file, const char *fname)
int XrdCryptosslX509ParseFile(const char *fname, XrdCryptoX509Chain *chain, const char *fkey)
int fclose(FILE *stream)
int fflush(FILE *stream)
#define close(a)
Definition XrdPosix.hh:48
#define write(a, b, c)
Definition XrdPosix.hh:115
#define mkdir(a, b)
Definition XrdPosix.hh:74
#define closedir(a)
Definition XrdPosix.hh:50
#define unlink(a)
Definition XrdPosix.hh:113
#define stat(a, b)
Definition XrdPosix.hh:101
#define rename(a, b)
Definition XrdPosix.hh:92
#define readdir(a)
Definition XrdPosix.hh:86
#define read(a, b, c)
Definition XrdPosix.hh:82
XrdCryptoX509 * Next()
XrdCryptoX509 * Begin()
void Cleanup(bool keepCA=0)
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
static int Run(pthread_t *, void *(*proc)(void *), void *arg, int opts=0, const char *desc=0)
static std::unique_ptr< TempCAGuard > create(XrdSysError &, const std::string &ca_tmp_dir)
TempCAGuard(const TempCAGuard &)=delete
XrdTlsTempCA(XrdSysError *log, std::string ca_dir, bool build_store=true)