nts: allow ntstrustedcerts to specify directory

If the specified path is a directory, load all certificates in the
directory.
This commit is contained in:
Miroslav Lichvar 2021-02-11 15:43:49 +01:00
parent 316d47e3b4
commit 26ce610155
5 changed files with 30 additions and 22 deletions

22
conf.c
View file

@ -261,7 +261,7 @@ static int nts_server_processes = 1;
static int nts_server_connections = 100;
static int nts_refresh = 2419200; /* 4 weeks */
static int nts_rotate = 604800; /* 1 week */
static ARR_Instance nts_trusted_certs_files; /* array of (char *) */
static ARR_Instance nts_trusted_certs_paths; /* array of (char *) */
/* Number of clock updates needed to enable certificate time checks */
static int no_cert_time_check = 0;
@ -392,7 +392,7 @@ CNF_Initialise(int r, int client_only)
nts_server_cert_files = ARR_CreateInstance(sizeof (char *));
nts_server_key_files = ARR_CreateInstance(sizeof (char *));
nts_trusted_certs_files = ARR_CreateInstance(sizeof (char *));
nts_trusted_certs_paths = ARR_CreateInstance(sizeof (char *));
rtc_device = Strdup(DEFAULT_RTC_DEVICE);
hwclock_file = Strdup(DEFAULT_HWCLOCK_FILE);
@ -436,8 +436,8 @@ CNF_Finalise(void)
Free(*(char **)ARR_GetElement(nts_server_cert_files, i));
for (i = 0; i < ARR_GetSize(nts_server_key_files); i++)
Free(*(char **)ARR_GetElement(nts_server_key_files, i));
for (i = 0; i < ARR_GetSize(nts_trusted_certs_files); i++)
Free(*(char **)ARR_GetElement(nts_trusted_certs_files, i));
for (i = 0; i < ARR_GetSize(nts_trusted_certs_paths); i++)
Free(*(char **)ARR_GetElement(nts_trusted_certs_paths, i));
ARR_DestroyInstance(init_sources);
ARR_DestroyInstance(ntp_sources);
@ -451,7 +451,7 @@ CNF_Finalise(void)
ARR_DestroyInstance(nts_server_cert_files);
ARR_DestroyInstance(nts_server_key_files);
ARR_DestroyInstance(nts_trusted_certs_files);
ARR_DestroyInstance(nts_trusted_certs_paths);
Free(drift_file);
Free(dumpdir);
@ -1185,10 +1185,10 @@ parse_ntsserver(char *line, ARR_Instance files)
static void
parse_ntstrustedcerts(char *line)
{
char *file = NULL;
char *path = NULL;
parse_string(line, &file);
ARR_AppendElement(nts_trusted_certs_files, &file);
parse_string(line, &path);
ARR_AppendElement(nts_trusted_certs_paths, &path);
}
/* ================================================== */
@ -2605,11 +2605,11 @@ CNF_GetNtsRotate(void)
/* ================================================== */
int
CNF_GetNtsTrustedCertsFiles(const char ***files)
CNF_GetNtsTrustedCertsPaths(const char ***paths)
{
*files = ARR_GetElements(nts_trusted_certs_files);
*paths = ARR_GetElements(nts_trusted_certs_paths);
return ARR_GetSize(nts_trusted_certs_files);
return ARR_GetSize(nts_trusted_certs_paths);
}
/* ================================================== */

2
conf.h
View file

@ -159,7 +159,7 @@ extern int CNF_GetNtsServerProcesses(void);
extern int CNF_GetNtsServerConnections(void);
extern int CNF_GetNtsRefresh(void);
extern int CNF_GetNtsRotate(void);
extern int CNF_GetNtsTrustedCertsFiles(const char ***files);
extern int CNF_GetNtsTrustedCertsPaths(const char ***paths);
extern int CNF_GetNoSystemCert(void);
extern int CNF_GetNoCertTimeCheck(void);

View file

@ -750,14 +750,14 @@ This directive specifies the maximum interval between NTS-KE handshakes (in
seconds) in order to refresh the keys authenticating NTP packets. The default
value is 2419200 (4 weeks).
[[ntstrustedcerts]]*ntstrustedcerts* _file_::
This directive specifies a file containing certificates (in the PEM format) of
trusted certificate authorities (CA) that should be used to verify certificates
of NTS servers in addition to the system's default trusted CAs (if the
*nosystemcert* directive is not present).
[[ntstrustedcerts]]*ntstrustedcerts* _file_|_directory_::
This directive specifies a file or directory containing certificates (in the
PEM format) of trusted certificate authorities (CA) that should be used to
verify certificates of NTS servers in addition to the system's default trusted
CAs (if the *nosystemcert* directive is not present).
+
This directive can be used multiple times to specify multiple files with
trusted certificates.
This directive can be used multiple times to specify multiple files and/or
directories with trusted certificates.
[[nosystemcert]]*nosystemcert*::
This directive disables the system's default trusted CAs.

View file

@ -281,7 +281,7 @@ NKC_CreateInstance(IPSockAddr *address, const char *name)
inst->destroying = 0;
inst->got_response = 0;
n_certs = CNF_GetNtsTrustedCertsFiles(&trusted_certs);
n_certs = CNF_GetNtsTrustedCertsPaths(&trusted_certs);
/* Share the credentials with other client instances */
if (!client_credentials)

View file

@ -675,10 +675,18 @@ create_credentials(const char **certs, const char **keys, int n_certs_keys,
if (trusted_certs) {
for (i = 0; i < n_trusted_certs; i++) {
r = gnutls_certificate_set_x509_trust_file(credentials, trusted_certs[i],
GNUTLS_X509_FMT_PEM);
struct stat buf;
if (stat(trusted_certs[i], &buf) == 0 && S_ISDIR(buf.st_mode))
r = gnutls_certificate_set_x509_trust_dir(credentials, trusted_certs[i],
GNUTLS_X509_FMT_PEM);
else
r = gnutls_certificate_set_x509_trust_file(credentials, trusted_certs[i],
GNUTLS_X509_FMT_PEM);
if (r < 0)
goto error;
DEBUG_LOG("Added %d trusted certs from %s", r, trusted_certs[i]);
}
}
}