1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
/* Root certificates, for libreswan
*
* Copyright (C) 2015,2018 Matt Rogers <[email protected]>
* Copyright (C) 2017-2018 Paul Wouters <[email protected]>
* Copyright (C) 2018-2019 Andrew Cagney <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <https://www.gnu.org/licenses/gpl2.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
*/
#include <features.h>
#include <cert.h>
#include "defs.h"
#include "root_certs.h"
#include "server.h"
#include "pluto_timing.h"
#include "log.h"
static struct root_certs *root_cert_db;
static void root_certs_free(void *obj, where_t unused_where UNUSED)
{
struct root_certs *root_certs = obj;
dbg("destroying root certificate cache");
CERT_DestroyCertList(root_certs->trustcl);
pfreeany(root_certs);
}
struct root_certs *root_certs_addref_where(where_t where)
{
passert(in_main_thread());
/* extend or set cert cache lifetime */
schedule_oneshot_timer(EVENT_FREE_ROOT_CERTS, FREE_ROOT_CERTS_TIMEOUT);
if (root_cert_db != NULL) {
return addref_where(root_cert_db, where);
}
dbg("loading root certificate cache");
/*
* Always allocate the ROOT_CERTS structure. If things fail,
* it will contain an empty list of certificates (but avoid
* possibly expensive attempts to re-load).
*/
root_cert_db = refcnt_alloc(struct root_certs, root_certs_free, where);
/*
* Start with two references: the ROOT_CERT_DB; and the result
* of this function.
*/
struct root_certs *root_certs = addref_where(root_cert_db, where); /* function result */
root_certs->trustcl = CERT_NewCertList();
struct logger logger = global_logger;
PK11SlotInfo *slot = lsw_nss_get_authenticated_slot(&logger);
if (slot == NULL) {
/* already logged */
return root_certs; /* empty, but non-null, list */
}
/*
* This is the killer when it comes to performance.
*/
threadtime_t get_time = threadtime_start();
CERTCertList *allcerts = PK11_ListCertsInSlot(slot);
threadtime_stop(&get_time, SOS_NOBODY, "%s() calling PK11_ListCertsInSlot()", __func__);
if (allcerts == NULL) {
return root_certs;
}
/*
* XXX: would a better call be
* CERT_FilterCertListByUsage(allcerts, certUsageAnyCA,
* PR_TRUE)? Timing tests suggest it makes little difference,
* and the result is being cached anyway.
*/
threadtime_t ca_time = threadtime_start();
for (CERTCertListNode *node = CERT_LIST_HEAD(allcerts);
!CERT_LIST_END(node, allcerts);
node = CERT_LIST_NEXT(node)) {
if (!CERT_IsCACert(node->cert, NULL)) {
dbg("discarding non-CA cert %s", node->cert->subjectName);
continue;
}
if (!node->cert->isRoot) {
dbg("discarding non-root CA cert %s", node->cert->subjectName);
continue;
}
dbg("adding the CA root cert %s", node->cert->subjectName);
CERTCertificate *dup = CERT_DupCertificate(node->cert);
CERT_AddCertToListTail(root_certs->trustcl, dup);
}
CERT_DestroyCertList(allcerts);
threadtime_stop(&ca_time, SOS_NOBODY, "%s() filtering CAs", __func__);
return root_certs;
}
void root_certs_delref_where(struct root_certs **root_certs, where_t where)
{
delref_where(root_certs, where);
}
bool root_certs_empty(const struct root_certs *root_certs)
{
return (!pexpect(root_certs != NULL) ||
root_certs->trustcl == NULL ||
CERT_LIST_EMPTY(root_certs->trustcl));
}
void init_root_certs(void)
{
/*
* Set up the timer for deleting the root certs, but don't
* schedule it (it gets scheduled when the root certs are
* allocated).
*/
init_oneshot_timer(EVENT_FREE_ROOT_CERTS, free_root_certs);
}
void free_root_certs(struct logger *logger)
{
passert(in_main_thread());
/*
* This function can be called during shutdown when there are
* no certs.
*/
if (root_cert_db == NULL) {
return;
}
/*
* Deal with a helper thread being stuck (because it is being debugged?);
* need to peek at the refcnt.
*
* There is a race condition: the reference count may be changed between
* the call to refcnt_peek and the root_certs_delref's deletion.
* The consequence is benign: only a delay of FREE_ROOT_CERTS_TIMEOUT.
*/
if (refcnt_peek(&root_cert_db->refcnt) > 1) {
llog(RC_LOG, logger, "root certs still in use; suspect stuck thread");
/* extend or set cert cache lifetime */
schedule_oneshot_timer(EVENT_FREE_ROOT_CERTS, FREE_ROOT_CERTS_TIMEOUT);
} else {
root_certs_delref(&root_cert_db);
pexpect(root_cert_db == NULL);
}
}
|