/*! \file */
#include "keygen.h"
+#include <ctype.h>
#include <stdarg.h>
#include <stdlib.h>
}
}
+/*%
+ * Reject key names that would not embed safely into a named.conf
+ * 'key "<name>" { ... };' clause. Allowed: alphanumerics, '.', '-', '_'.
+ */
+void
+validate_keyname(const char *keyname) {
+ if (keyname == NULL || keyname[0] == '\0') {
+ fatal("key name must not be empty");
+ }
+ for (const char *p = keyname; *p != '\0'; p++) {
+ unsigned char c = (unsigned char)*p;
+ if (!isalnum(c) && c != '.' && c != '-' && c != '_') {
+ fatal("key name '%s' contains invalid character; "
+ "only alphanumerics, '.', '-', and '_' are "
+ "allowed",
+ keyname);
+ }
+ }
+}
+
/*%
* Generate a key of size 'keysize' and place it in 'key_txtbuffer'
*/
#include <dns/secalg.h>
+void
+validate_keyname(const char *keyname);
+
void
generate_key(isc_mem_t *mctx, dns_secalg_t alg, int keysize,
isc_buffer_t *key_txtbuffer);
usage(EXIT_FAILURE);
}
+ validate_keyname(keyname);
+
if (alg == DST_ALG_HMACMD5) {
fprintf(stderr, "warning: use of hmac-md5 for RNDC keys "
"is deprecated; hmac-sha256 is now "
}
}
+ validate_keyname(keyname);
+
isc_buffer_init(&key_txtbuffer, &key_txtsecret, sizeof(key_txtsecret));
generate_key(isc_g_mctx, alg, keysize, &key_txtbuffer);
--- /dev/null
+#!/bin/sh
+
+# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
+#
+# SPDX-License-Identifier: MPL-2.0
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, you can obtain one at https://mozilla.org/MPL/2.0/.
+#
+# See the COPYRIGHT file distributed with this work for additional
+# information regarding copyright ownership.
+
+set -e
+
+# tsig-keygen and ddns-confgen are the same binary; the install layout
+# provides ddns-confgen as a symlink, but the build tree does not. Create
+# one here so the test can exercise the ddns-confgen mode.
+ln -sf "$TSIGKEYGEN" ddns-confgen
--- /dev/null
+# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
+#
+# SPDX-License-Identifier: MPL-2.0
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, you can obtain one at https://mozilla.org/MPL/2.0/.
+#
+# See the COPYRIGHT file distributed with this work for additional
+# information regarding copyright ownership.
+
+import os
+import subprocess
+
+import pytest
+
+import isctest
+
+INJECTION = (
+ 'backdoor" { algorithm hmac-sha256; '
+ 'secret "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="; }; key "rndc-key'
+)
+
+
+def test_rndc_confgen_default():
+ cmd = isctest.run.cmd([os.environ["RNDCCONFGEN"]])
+ assert b'key "rndc-key" {' in cmd.proc.stdout
+
+
+def test_rndc_confgen_keyname_with_dots():
+ cmd = isctest.run.cmd([os.environ["RNDCCONFGEN"], "-k", "key.example.com"])
+ assert b'key "key.example.com" {' in cmd.proc.stdout
+
+
+def test_rndc_confgen_rejects_injection():
+ with pytest.raises(subprocess.CalledProcessError):
+ isctest.run.cmd([os.environ["RNDCCONFGEN"], "-k", INJECTION])
+
+
+def test_tsig_keygen_default():
+ cmd = isctest.run.cmd([os.environ["TSIGKEYGEN"]])
+ assert b'key "tsig-key" {' in cmd.proc.stdout
+
+
+def test_tsig_keygen_rejects_injection_positional():
+ with pytest.raises(subprocess.CalledProcessError):
+ isctest.run.cmd([os.environ["TSIGKEYGEN"], INJECTION])
+
+
+DDNSCONFGEN = "./ddns-confgen"
+
+
+def test_ddns_confgen_default():
+ cmd = isctest.run.cmd([DDNSCONFGEN, "-q"])
+ assert b'key "ddns-key" {' in cmd.proc.stdout
+
+
+@pytest.mark.parametrize(
+ "args",
+ [
+ ["-k", INJECTION],
+ ["-y", INJECTION],
+ ["-z", INJECTION],
+ ["-s", INJECTION],
+ ],
+)
+def test_ddns_confgen_rejects_injection(args):
+ with pytest.raises(subprocess.CalledProcessError):
+ isctest.run.cmd([DDNSCONFGEN, "-q", *args])