]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Add code for creating keystore from config
authorMatthijs Mekking <matthijs@isc.org>
Tue, 25 Jan 2022 09:08:43 +0000 (10:08 +0100)
committerMatthijs Mekking <matthijs@isc.org>
Thu, 25 Jan 2024 13:38:11 +0000 (14:38 +0100)
Add code for configuring keystore objects. Add this to the "kaspconf"
code, as it is related to 'dnssec-policy' and it is too small to create
a separate file for it.

lib/dns/include/dns/types.h
lib/isccfg/include/isccfg/kaspconf.h
lib/isccfg/kaspconf.c

index 89e6b15f782ef43ad5fdc2148b45de067a9d3c45..462e36f2af407a14f297f678d9a61da2aeac74c2 100644 (file)
@@ -106,8 +106,10 @@ typedef struct dns_kasp_nsec3param dns_kasp_nsec3param_t;
 typedef uint16_t                  dns_keyflags_t;
 typedef struct dns_keynode        dns_keynode_t;
 typedef ISC_LIST(dns_keynode_t) dns_keynodelist_t;
-typedef struct dns_keytable       dns_keytable_t;
-typedef uint16_t                  dns_keytag_t;
+typedef struct dns_keytable dns_keytable_t;
+typedef uint16_t           dns_keytag_t;
+typedef struct dns_keystore dns_keystore_t;
+typedef ISC_LIST(dns_keystore_t) dns_keystorelist_t;
 typedef struct dns_loadctx        dns_loadctx_t;
 typedef struct dns_loadmgr        dns_loadmgr_t;
 typedef struct dns_masterrawheader dns_masterrawheader_t;
index 744a3276955eedf894eb93b093f2e752efcffec3..a005a3916502e1397c7874bdeb46b1357073e277 100644 (file)
@@ -56,4 +56,32 @@ cfg_kasp_fromconfig(const cfg_obj_t *config, dns_kasp_t *default_kasp,
  *\li  Other errors are possible.
  */
 
+isc_result_t
+cfg_keystore_fromconfig(const cfg_obj_t *config, isc_mem_t *mctx,
+                       isc_log_t *logctx, dns_keystorelist_t *keystorelist,
+                       dns_keystore_t    **kspp);
+/*%<
+ * Create and configure a key store. If a 'keystorelist' is provided, a lookup
+ * happens and if a keystore already exists with the same name, no new one is
+ * created, and no attach to 'kspp' happens.
+ *
+ * Requires:
+ *
+ *\li  config != NULL
+
+ *\li  'mctx' is a valid memory context.
+ *
+ *\li  'logctx' is a valid logging context.
+ *
+ *\li  kspp != NULL && *kspp == NULL
+ *
+ * Returns:
+ *
+ *\li  #ISC_R_SUCCESS  If creating and configuring the keystore succeeds.
+ *\li  #ISC_R_EXISTS   If 'keystorelist' already has a keystore with 'name'.
+ *\li  #ISC_R_NOMEMORY
+ *
+ *\li  Other errors are possible.
+ */
+
 ISC_LANG_ENDDECLS
index 1a2b0da47c7edeb1cfc8bd65aabd6d7eee6cc5d3..2757209cdc482529843ade8f6f065653423f2ade 100644 (file)
@@ -24,6 +24,7 @@
 #include <isc/util.h>
 
 #include <dns/kasp.h>
+#include <dns/keystore.h>
 #include <dns/keyvalues.h>
 #include <dns/log.h>
 #include <dns/nsec3.h>
@@ -89,6 +90,23 @@ get_duration(const cfg_obj_t **maps, const char *option, const char *dfl) {
        return (cfg_obj_asduration(obj));
 }
 
+/*
+ * Utility function for configuring strings.
+ */
+static const char *
+get_string(const cfg_obj_t **maps, const char *option) {
+       const cfg_obj_t *obj;
+       isc_result_t result;
+       obj = NULL;
+
+       result = confget(maps, option, &obj);
+       if (result == ISC_R_NOTFOUND) {
+               return (NULL);
+       }
+       INSIST(result == ISC_R_SUCCESS);
+       return (cfg_obj_asstring(obj));
+}
+
 /*
  * Create a new kasp key derived from configuration.
  */
@@ -655,3 +673,69 @@ cleanup:
        dns_kasp_detach(&kasp);
        return (result);
 }
+
+isc_result_t
+cfg_keystore_fromconfig(const cfg_obj_t *config, isc_mem_t *mctx,
+                       isc_log_t *logctx, dns_keystorelist_t *keystorelist,
+                       dns_keystore_t **kspp) {
+       isc_result_t result;
+       const cfg_obj_t *maps[2];
+       const cfg_obj_t *koptions = NULL;
+       const char *name = NULL;
+       dns_keystore_t *keystore = NULL;
+       int i = 0;
+
+       REQUIRE(config != NULL);
+       REQUIRE(kspp != NULL && *kspp == NULL);
+
+       name = cfg_obj_asstring(cfg_tuple_get(config, "name"));
+       INSIST(name != NULL);
+
+       result = dns_keystorelist_find(keystorelist, name, &keystore);
+
+       if (result == ISC_R_SUCCESS) {
+               cfg_obj_log(config, logctx, ISC_LOG_ERROR,
+                           "key-store: duplicate key-store found '%s'", name);
+               dns_keystore_detach(&keystore);
+               return (ISC_R_EXISTS);
+       }
+       if (result != ISC_R_NOTFOUND) {
+               cfg_obj_log(config, logctx, ISC_LOG_ERROR,
+                           "key-store: lookup '%s' failed: %s", name,
+                           isc_result_totext(result));
+               return (result);
+       }
+
+       /*
+        * No key-store with configured name was found in list, create new one.
+        */
+       INSIST(keystore == NULL);
+       result = dns_keystore_create(mctx, name, &keystore);
+       if (result != ISC_R_SUCCESS) {
+               return (result);
+       }
+       INSIST(keystore != NULL);
+
+       /* Now configure. */
+       INSIST(DNS_KEYSTORE_VALID(keystore));
+
+       if (config != NULL) {
+               koptions = cfg_tuple_get(config, "options");
+               maps[i++] = koptions;
+       }
+       maps[i] = NULL;
+
+       /* Configuration */
+       dns_keystore_setdirectory(keystore, get_string(maps, "directory"));
+       dns_keystore_setpkcs11uri(keystore, get_string(maps, "uri"));
+
+       /* Append it to the list for future lookups. */
+       ISC_LIST_APPEND(*keystorelist, keystore, link);
+       INSIST(!(ISC_LIST_EMPTY(*keystorelist)));
+
+       /* Success: Attach the keystore to the pointer and return. */
+       dns_keystore_attach(keystore, kspp);
+
+       /* Don't detach as keystore is on '*keystorelist' */
+       return (ISC_R_SUCCESS);
+}