]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
nts: allow ntstrustedcerts to specify directory
authorMiroslav Lichvar <mlichvar@redhat.com>
Thu, 11 Feb 2021 14:43:49 +0000 (15:43 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 11 Feb 2021 15:13:39 +0000 (16:13 +0100)
If the specified path is a directory, load all certificates in the
directory.

conf.c
conf.h
doc/chrony.conf.adoc
nts_ke_client.c
nts_ke_session.c

diff --git a/conf.c b/conf.c
index 3ae025403e784395f5c2ba8317baee5aacaa3ed8..573094e8b73561ede6577ac44eb6f602b1422116 100644 (file)
--- a/conf.c
+++ b/conf.c
@@ -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);
 }
 
 /* ================================================== */
diff --git a/conf.h b/conf.h
index 025ca607569e72e875c0305bf5925bd37c29005a..0ca5017167da560872014bb972e7b444486cc65e 100644 (file)
--- a/conf.h
+++ b/conf.h
@@ -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);
 
index a1de81487f583ca4b9f1d8acd356194403623fa9..2abac69068314f8e011edbbdfe5055e783a93649 100644 (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).
-+
-This directive can be used multiple times to specify multiple files with
-trusted certificates.
+[[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 and/or
+directories with trusted certificates.
 
 [[nosystemcert]]*nosystemcert*::
 This directive disables the system's default trusted CAs.
index 59a9730adc75709d98ad8a72eecb1ae56f74802e..0b13627ec4ec5087d7b0340e4f4edbe38b4a4656 100644 (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)
index 131ad90e187b075a60991591278c038ce0399c02..9936d37ff5ad224160232d394788b10b4f28ff99 100644 (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]);
       }
     }
   }