Blame SOURCES/bind-9.9-allow_external_dnskey.patch

900526
From 0c91911b4d1e872b87eaf6431ed47fe24d18dd43 Mon Sep 17 00:00:00 2001
900526
From: Mark Andrews <marka@isc.org>
900526
Date: Wed, 4 Sep 2013 13:53:02 +1000
900526
Subject: [PATCH] 3642.   [func]          Allow externally generated DNSKEY to
900526
 be imported                         into the DNSKEY management framework.  A
900526
 new tool                         dnssec-importkey is used to this. [RT
900526
 #34698]
900526
900526
---
900526
 CHANGES                                |   4 +
900526
 bin/dnssec/.gitignore                  |   1 +
900526
 bin/dnssec/Makefile.in                 |   8 +-
900526
 bin/dnssec/dnssec-importkey.8          | 112 +++++++++
900526
 bin/dnssec/dnssec-importkey.c          | 399 +++++++++++++++++++++++++++++++++
900526
 bin/dnssec/dnssec-importkey.docbook    | 185 +++++++++++++++
900526
 bin/dnssec/dnssec-importkey.html       | 112 +++++++++
900526
 bin/dnssec/dnssec-settime.c            |   4 +-
900526
 bin/tests/system/conf.sh.in            |   1 +
900526
 bin/tests/system/inline/clean.sh       |   3 +
900526
 bin/tests/system/inline/ns1/root.db.in |   3 +
900526
 bin/tests/system/inline/ns3/named.conf |   8 +
900526
 bin/tests/system/inline/ns3/sign.sh    |  29 +++
900526
 bin/tests/system/inline/setup.sh       |   1 +
900526
 bin/tests/system/inline/tests.sh       |  17 +-
900526
 lib/dns/dnssec.c                       |  96 ++++----
900526
 lib/dns/dst_api.c                      |  10 +
900526
 lib/dns/dst_internal.h                 |   1 +
900526
 lib/dns/dst_parse.c                    |  63 ++++--
900526
 lib/dns/dst_result.c                   |   2 +-
900526
 lib/dns/include/dns/master.h           |   1 +
900526
 lib/dns/include/dst/dst.h              |   6 +
900526
 lib/dns/master.c                       |   4 +-
900526
 lib/dns/openssldsa_link.c              |  19 ++
900526
 lib/dns/opensslecdsa_link.c            |  53 +++--
900526
 lib/dns/opensslgost_link.c             |  27 ++-
900526
 lib/dns/opensslrsa_link.c              |  18 +-
900526
 lib/dns/zone.c                         |   4 +
900526
 28 files changed, 1108 insertions(+), 83 deletions(-)
900526
 create mode 100644 bin/dnssec/dnssec-importkey.8
900526
 create mode 100644 bin/dnssec/dnssec-importkey.c
900526
 create mode 100644 bin/dnssec/dnssec-importkey.docbook
900526
 create mode 100644 bin/dnssec/dnssec-importkey.html
900526
900526
diff --git a/bin/dnssec/Makefile.in b/bin/dnssec/Makefile.in
900526
index 4f8bceb..ecb0fae 100644
900526
--- a/bin/dnssec/Makefile.in
900526
+++ b/bin/dnssec/Makefile.in
900526
@@ -45,13 +45,13 @@ NOSYMLIBS =	${DNSLIBS} ${ISCNOSYMLIBS} @LIBS@
900526
 TARGETS =	dnssec-keygen@EXEEXT@ dnssec-signzone@EXEEXT@ \
900526
 		dnssec-keyfromlabel@EXEEXT@ dnssec-dsfromkey@EXEEXT@ \
900526
 		dnssec-revoke@EXEEXT@ dnssec-settime@EXEEXT@ \
900526
-		dnssec-verify@EXEEXT@
900526
+		dnssec-verify@EXEEXT@ dnssec-importkey@EXEEXT@
900526
 
900526
 OBJS =		dnssectool.@O@
900526
 
900526
 SRCS =		dnssec-dsfromkey.c dnssec-keyfromlabel.c dnssec-keygen.c \
900526
 		dnssec-revoke.c dnssec-settime.c dnssec-signzone.c \
900526
-		dnssec-verify.c dnssectool.c
900526
+		dnssec-verify.c dnssec-importkey.c dnssectool.c 
900526
 
900526
 MANPAGES =	dnssec-dsfromkey.8 dnssec-keyfromlabel.8 dnssec-keygen.8 \
900526
 		dnssec-revoke.8 dnssec-settime.8 dnssec-signzone.8 \
900526
@@ -102,6 +102,10 @@ dnssec-settime@EXEEXT@: dnssec-settime.@O@ ${OBJS} ${DEPLIBS}
900526
 	${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \
900526
 	dnssec-settime.@O@ ${OBJS} ${LIBS}
900526
 
900526
+dnssec-importkey@EXEEXT@: dnssec-importkey.@O@ ${OBJS} ${DEPLIBS}
900526
+	${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \
900526
+	dnssec-importkey.@O@ ${OBJS} ${LIBS}
900526
+
900526
 doc man:: ${MANOBJS}
900526
 
900526
 docclean manclean maintainer-clean::
900526
diff --git a/bin/dnssec/dnssec-importkey.8 b/bin/dnssec/dnssec-importkey.8
900526
new file mode 100644
900526
index 0000000..33a3ef4
900526
--- /dev/null
900526
+++ b/bin/dnssec/dnssec-importkey.8
900526
@@ -0,0 +1,112 @@
900526
+.\" Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
900526
+.\" 
900526
+.\" Permission to use, copy, modify, and/or distribute this software for any
900526
+.\" purpose with or without fee is hereby granted, provided that the above
900526
+.\" copyright notice and this permission notice appear in all copies.
900526
+.\" 
900526
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
900526
+.\" REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
900526
+.\" AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
900526
+.\" INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
900526
+.\" LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
900526
+.\" OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
900526
+.\" PERFORMANCE OF THIS SOFTWARE.
900526
+.\"
900526
+.\" $Id$
900526
+.\"
900526
+.hy 0
900526
+.ad l
900526
+'\" t
900526
+.\"     Title: dnssec-importkey
900526
+.\"    Author: [see the "AUTHOR" section]
900526
+.\" Generator: DocBook XSL Stylesheets v1.78.1 <http://docbook.sf.net/>
900526
+.\"      Date: August 30, 2013
900526
+.\"    Manual: BIND9
900526
+.\"    Source: BIND9
900526
+.\"  Language: English
900526
+.\"
900526
+.TH "DNSSEC\-IMPORTKEY" "8" "August 30, 2013" "BIND9" "BIND9"
900526
+.\" -----------------------------------------------------------------
900526
+.\" * Define some portability stuff
900526
+.\" -----------------------------------------------------------------
900526
+.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
900526
+.\" http://bugs.debian.org/507673
900526
+.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
900526
+.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
900526
+.ie \n(.g .ds Aq \(aq
900526
+.el       .ds Aq '
900526
+.\" -----------------------------------------------------------------
900526
+.\" * set default formatting
900526
+.\" -----------------------------------------------------------------
900526
+.\" disable hyphenation
900526
+.nh
900526
+.\" disable justification (adjust text to left margin only)
900526
+.ad l
900526
+.\" -----------------------------------------------------------------
900526
+.\" * MAIN CONTENT STARTS HERE *
900526
+.\" -----------------------------------------------------------------
900526
+.SH "NAME"
900526
+dnssec-importkey \- Import DNSKEY records from external systems so they can be managed\&.
900526
+.SH "SYNOPSIS"
900526
+.HP 17
900526
+\fBdnssec\-importkey\fR [\fB\-f\ \fR\fB\fIfilename\fR\fR] [\fB\-K\ \fR\fB\fIdirectory\fR\fR] [\fB\-P\ \fR\fB\fIdate/offset\fR\fR] [\fB\-D\ \fR\fB\fIdate/offset\fR\fR] [\fB\-h\fR] [\fB\-v\ \fR\fB\fIlevel\fR\fR] [\fBkeyname\fR]
900526
+.SH "DESCRIPTION"
900526
+.PP
900526
+\fBdnssec\-importkey\fR
900526
+read a DNSKEY record and generated a \&.key/\&.private key pair\&. Publication (\fB\-P\fR) and deletions (\fB\-D\fR) times can be set for the key\&.
900526
+.SH "OPTIONS"
900526
+.PP
900526
+\-f \fIfilename\fR
900526
+.RS 4
900526
+Filename to read the key from\&.
900526
+.RE
900526
+.PP
900526
+\-K \fIdirectory\fR
900526
+.RS 4
900526
+Sets the directory in which the key files are to reside\&.
900526
+.RE
900526
+.PP
900526
+\-L \fIttl\fR
900526
+.RS 4
900526
+Sets the default TTL to use for this key when it is converted into a DNSKEY RR\&. If the key is imported into a zone, this is the TTL that will be used for it, unless there was already a DNSKEY RRset in place, in which case the existing TTL would take precedence\&. importkey the default TTL to
900526
+0
900526
+or
900526
+none
900526
+removes it\&.
900526
+.RE
900526
+.PP
900526
+\-h
900526
+.RS 4
900526
+Emit usage message and exit\&.
900526
+.RE
900526
+.PP
900526
+\-v \fIlevel\fR
900526
+.RS 4
900526
+Sets the debugging level\&.
900526
+.RE
900526
+.SH "TIMING OPTIONS"
900526
+.PP
900526
+Dates can be expressed in the format YYYYMMDD or YYYYMMDDHHMMSS\&. If the argument begins with a \*(Aq+\*(Aq or \*(Aq\-\*(Aq, it is interpreted as an offset from the present time\&. For convenience, if such an offset is followed by one of the suffixes \*(Aqy\*(Aq, \*(Aqmo\*(Aq, \*(Aqw\*(Aq, \*(Aqd\*(Aq, \*(Aqh\*(Aq, or \*(Aqmi\*(Aq, then the offset is computed in years (defined as 365 24\-hour days, ignoring leap years), months (defined as 30 24\-hour days), weeks, days, hours, or minutes, respectively\&. Without a suffix, the offset is computed in seconds\&. To unset a date, use \*(Aqnone\*(Aq\&.
900526
+.PP
900526
+\-P \fIdate/offset\fR
900526
+.RS 4
900526
+Sets the date on which a key is to be published to the zone\&. After that date, the key will be included in the zone but will not be used to sign it\&.
900526
+.RE
900526
+.PP
900526
+\-D \fIdate/offset\fR
900526
+.RS 4
900526
+Sets the date on which the key is to be deleted\&. After that date, the key will no longer be included in the zone\&. (It may remain in the key repository, however\&.)
900526
+.RE
900526
+.SH "SEE ALSO"
900526
+.PP
900526
+\fBdnssec-keygen\fR(8),
900526
+\fBdnssec-signzone\fR(8),
900526
+BIND 9 Administrator Reference Manual,
900526
+RFC 5011\&.
900526
+.SH "AUTHOR"
900526
+.PP
900526
+Internet Systems Consortium
900526
+.SH "COPYRIGHT"
900526
+.br
900526
+Copyright \(co 2013 Internet Systems Consortium, Inc. ("ISC")
900526
+.br
900526
diff --git a/bin/dnssec/dnssec-importkey.c b/bin/dnssec/dnssec-importkey.c
900526
new file mode 100644
900526
index 0000000..3491828
900526
--- /dev/null
900526
+++ b/bin/dnssec/dnssec-importkey.c
900526
@@ -0,0 +1,399 @@
900526
+/*
900526
+ * Copyright (C) 2008-2012  Internet Systems Consortium, Inc. ("ISC")
900526
+ *
900526
+ * Permission to use, copy, modify, and/or distribute this software for any
900526
+ * purpose with or without fee is hereby granted, provided that the above
900526
+ * copyright notice and this permission notice appear in all copies.
900526
+ *
900526
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
900526
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
900526
+ * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
900526
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
900526
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
900526
+ * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
900526
+ * PERFORMANCE OF THIS SOFTWARE.
900526
+ */
900526
+
900526
+/*! \file */
900526
+
900526
+#include <config.h>
900526
+
900526
+#include <stdlib.h>
900526
+
900526
+#include <isc/buffer.h>
900526
+#include <isc/commandline.h>
900526
+#include <isc/entropy.h>
900526
+#include <isc/hash.h>
900526
+#include <isc/mem.h>
900526
+#include <isc/print.h>
900526
+#include <isc/string.h>
900526
+#include <isc/util.h>
900526
+
900526
+#include <dns/callbacks.h>
900526
+#include <dns/db.h>
900526
+#include <dns/dbiterator.h>
900526
+#include <dns/ds.h>
900526
+#include <dns/fixedname.h>
900526
+#include <dns/keyvalues.h>
900526
+#include <dns/log.h>
900526
+#include <dns/master.h>
900526
+#include <dns/name.h>
900526
+#include <dns/rdata.h>
900526
+#include <dns/rdataclass.h>
900526
+#include <dns/rdataset.h>
900526
+#include <dns/rdatasetiter.h>
900526
+#include <dns/rdatatype.h>
900526
+#include <dns/result.h>
900526
+
900526
+#include <dst/dst.h>
900526
+
900526
+#include "dnssectool.h"
900526
+
900526
+#ifndef PATH_MAX
900526
+#define PATH_MAX 1024   /* AIX, WIN32, and others don't define this. */
900526
+#endif
900526
+
900526
+const char *program = "dnssec-importkey";
900526
+int verbose;
900526
+
900526
+static dns_rdataclass_t rdclass;
900526
+static dns_fixedname_t	fixed;
900526
+static dns_name_t	*name = NULL;
900526
+static isc_mem_t	*mctx = NULL;
900526
+static isc_boolean_t	setpub = ISC_FALSE, setdel = ISC_FALSE;
900526
+static isc_stdtime_t	pub = 0, del = 0;
900526
+
900526
+static isc_result_t
900526
+initname(char *setname) {
900526
+	isc_result_t result;
900526
+	isc_buffer_t buf;
900526
+
900526
+	dns_fixedname_init(&fixed);
900526
+	name = dns_fixedname_name(&fixed);
900526
+
900526
+	isc_buffer_init(&buf, setname, strlen(setname));
900526
+	isc_buffer_add(&buf, strlen(setname));
900526
+	result = dns_name_fromtext(name, &buf, dns_rootname, 0, NULL);
900526
+	return (result);
900526
+}
900526
+
900526
+static void
900526
+db_load_from_stream(dns_db_t *db, FILE *fp) {
900526
+	isc_result_t result;
900526
+	dns_rdatacallbacks_t callbacks;
900526
+
900526
+	dns_rdatacallbacks_init(&callbacks);
900526
+	result = dns_db_beginload(db, &callbacks.add, &callbacks.add_private);
900526
+	if (result != ISC_R_SUCCESS)
900526
+		fatal("dns_db_beginload failed: %s", isc_result_totext(result));
900526
+
900526
+	result = dns_master_loadstream(fp, name, name, rdclass, 0,
900526
+				       &callbacks, mctx);
900526
+	if (result != ISC_R_SUCCESS)
900526
+		fatal("can't load from input: %s", isc_result_totext(result));
900526
+
900526
+	result = dns_db_endload(db, &callbacks.add_private);
900526
+	if (result != ISC_R_SUCCESS)
900526
+		fatal("dns_db_endload failed: %s", isc_result_totext(result));
900526
+}
900526
+
900526
+static isc_result_t
900526
+loadset(const char *filename, dns_rdataset_t *rdataset) {
900526
+	isc_result_t	 result;
900526
+	dns_db_t	 *db = NULL;
900526
+	dns_dbnode_t	 *node = NULL;
900526
+	char setname[DNS_NAME_FORMATSIZE];
900526
+
900526
+	dns_name_format(name, setname, sizeof(setname));
900526
+
900526
+	result = dns_db_create(mctx, "rbt", name, dns_dbtype_zone,
900526
+			       rdclass, 0, NULL, &db);
900526
+	if (result != ISC_R_SUCCESS)
900526
+		fatal("can't create database");
900526
+
900526
+	if (strcmp(filename, "-") == 0) {
900526
+		db_load_from_stream(db, stdin);
900526
+		filename = "input";
900526
+	} else {
900526
+		result = dns_db_load3(db, filename, dns_masterformat_text,
900526
+				      DNS_MASTER_NOTTL);
900526
+		if (result != ISC_R_SUCCESS && result != DNS_R_SEENINCLUDE)
900526
+			fatal("can't load %s: %s", filename,
900526
+			      isc_result_totext(result));
900526
+	}
900526
+
900526
+	result = dns_db_findnode(db, name, ISC_FALSE, &node);
900526
+	if (result != ISC_R_SUCCESS)
900526
+		fatal("can't find %s node in %s", setname, filename);
900526
+
900526
+	result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_dnskey,
900526
+				     0, 0, rdataset, NULL);
900526
+
900526
+	if (result == ISC_R_NOTFOUND)
900526
+		fatal("no DNSKEY RR for %s in %s", setname, filename);
900526
+	else if (result != ISC_R_SUCCESS)
900526
+		fatal("dns_db_findrdataset");
900526
+
900526
+	if (node != NULL)
900526
+		dns_db_detachnode(db, &node);
900526
+	if (db != NULL)
900526
+		dns_db_detach(&db);
900526
+	return (result);
900526
+}
900526
+
900526
+static void
900526
+loadkey(char *filename, unsigned char *key_buf, unsigned int key_buf_size,
900526
+	dns_rdata_t *rdata)
900526
+{
900526
+	isc_result_t  result;
900526
+	dst_key_t     *key = NULL;
900526
+	isc_buffer_t  keyb;
900526
+	isc_region_t  r;
900526
+
900526
+	dns_rdata_init(rdata);
900526
+
900526
+	isc_buffer_init(&keyb, key_buf, key_buf_size);
900526
+
900526
+	result = dst_key_fromnamedfile(filename, NULL, DST_TYPE_PUBLIC,
900526
+				       mctx, &key);
900526
+	if (result != ISC_R_SUCCESS)
900526
+		fatal("invalid keyfile name %s: %s",
900526
+		      filename, isc_result_totext(result));
900526
+
900526
+	if (verbose > 2) {
900526
+		char keystr[DST_KEY_FORMATSIZE];
900526
+
900526
+		dst_key_format(key, keystr, sizeof(keystr));
900526
+		fprintf(stderr, "%s: %s\n", program, keystr);
900526
+	}
900526
+
900526
+	result = dst_key_todns(key, &keyb);
900526
+	if (result != ISC_R_SUCCESS)
900526
+		fatal("can't decode key");
900526
+
900526
+	isc_buffer_usedregion(&keyb, &r);
900526
+	dns_rdata_fromregion(rdata, dst_key_class(key),
900526
+			     dns_rdatatype_dnskey, &r);
900526
+
900526
+	rdclass = dst_key_class(key);
900526
+
900526
+	dns_fixedname_init(&fixed);
900526
+	name = dns_fixedname_name(&fixed);
900526
+	result = dns_name_copy(dst_key_name(key), name, NULL);
900526
+	if (result != ISC_R_SUCCESS)
900526
+		fatal("can't copy name");
900526
+
900526
+	dst_key_free(&key);
900526
+}
900526
+
900526
+static void
900526
+emit(const char *dir, dns_rdata_t *rdata) {
900526
+	isc_result_t result;
900526
+	char keystr[DST_KEY_FORMATSIZE];
900526
+	char newname[1024];
900526
+	isc_buffer_t buf;
900526
+	dst_key_t *key = NULL;
900526
+
900526
+	isc_buffer_init(&buf, rdata->data, rdata->length);
900526
+	isc_buffer_add(&buf, rdata->length);
900526
+	result = dst_key_fromdns(name, rdclass, &buf, mctx, &key);
900526
+	if (result != ISC_R_SUCCESS) {
900526
+		fatal("dst_key_fromdns: %s", isc_result_totext(result));
900526
+	}
900526
+
900526
+	dst_key_setexternal(key, ISC_TRUE);
900526
+	if (setpub)
900526
+		dst_key_settime(key, DST_TIME_PUBLISH, pub);
900526
+	if (setdel)
900526
+		dst_key_settime(key, DST_TIME_DELETE, del);
900526
+
900526
+	isc_buffer_init(&buf, newname, sizeof(newname));
900526
+	result = dst_key_buildfilename(key, DST_TYPE_PUBLIC, dir, &buf;;
900526
+	if (result != ISC_R_SUCCESS) {
900526
+		fatal("Failed to build public key filename: %s",
900526
+		      isc_result_totext(result));
900526
+	}
900526
+
900526
+	result = dst_key_tofile(key, DST_TYPE_PUBLIC|DST_TYPE_PRIVATE,
900526
+				dir);
900526
+	if (result != ISC_R_SUCCESS) {
900526
+		dst_key_format(key, keystr, sizeof(keystr));
900526
+		fatal("Failed to write key %s: %s", keystr,
900526
+		      isc_result_totext(result));
900526
+	}
900526
+
900526
+	printf("%s\n", newname);
900526
+
900526
+	isc_buffer_clear(&buf;;
900526
+	result = dst_key_buildfilename(key, DST_TYPE_PRIVATE, dir, &buf;;
900526
+	if (result != ISC_R_SUCCESS) {
900526
+		fatal("Failed to build private key filename: %s",
900526
+		      isc_result_totext(result));
900526
+	}
900526
+	printf("%s\n", newname);
900526
+	dst_key_free(&key);
900526
+}
900526
+
900526
+ISC_PLATFORM_NORETURN_PRE static void
900526
+usage(void) ISC_PLATFORM_NORETURN_POST;
900526
+
900526
+static void
900526
+usage(void) {
900526
+	fprintf(stderr, "Usage:\n");
900526
+	fprintf(stderr,	"    %s options [-K dir] file\n\n", program);
900526
+	fprintf(stderr, "Version: %s\n", VERSION);
900526
+	fprintf(stderr, "Options:\n");
900526
+	fprintf(stderr, "    -v <verbose level>\n");
900526
+	fprintf(stderr, "    -K <directory>: directory in which to store "
900526
+			"the keyset files\n");
900526
+	fprintf(stderr, "    -f file: read keyset from zone file\n");
900526
+
900526
+	exit (-1);
900526
+}
900526
+
900526
+int
900526
+main(int argc, char **argv) {
900526
+	char		*classname = NULL;
900526
+	char		*filename = NULL, *dir = NULL, *namestr;
900526
+	char		*endp;
900526
+	int		ch;
900526
+	isc_result_t	result;
900526
+	isc_log_t	*log = NULL;
900526
+	isc_entropy_t	*ectx = NULL;
900526
+	dns_rdataset_t	rdataset;
900526
+	dns_rdata_t	rdata;
900526
+	isc_stdtime_t   now;
900526
+
900526
+	dns_rdata_init(&rdata);
900526
+	isc_stdtime_get(&now;;
900526
+
900526
+	if (argc == 1)
900526
+		usage();
900526
+
900526
+	result = isc_mem_create(0, 0, &mctx);
900526
+	if (result != ISC_R_SUCCESS)
900526
+		fatal("out of memory");
900526
+
900526
+	dns_result_register();
900526
+
900526
+	isc_commandline_errprint = ISC_FALSE;
900526
+
900526
+	while ((ch = isc_commandline_parse(argc, argv, "D:f:hK:P:v:")) != -1) {
900526
+		switch (ch) {
900526
+                case 'D':
900526
+                        if (setdel)
900526
+                                fatal("-D specified more than once");
900526
+
900526
+			setdel = ISC_TRUE;
900526
+			del = strtotime(isc_commandline_argument, now, now);
900526
+			break;
900526
+		case 'K':
900526
+			dir = isc_commandline_argument;
900526
+			if (strlen(dir) == 0U)
900526
+				fatal("directory must be non-empty string");
900526
+			break;
900526
+                case 'P':
900526
+                        if (setpub)
900526
+                                fatal("-P specified more than once");
900526
+
900526
+			setpub = ISC_TRUE;
900526
+			pub = strtotime(isc_commandline_argument, now, now);
900526
+                        break;
900526
+		case 'f':
900526
+			filename = isc_commandline_argument;
900526
+			break;
900526
+		case 'v':
900526
+			verbose = strtol(isc_commandline_argument, &endp, 0);
900526
+			if (*endp != '\0')
900526
+				fatal("-v must be followed by a number");
900526
+			break;
900526
+		case '?':
900526
+			if (isc_commandline_option != '?')
900526
+				fprintf(stderr, "%s: invalid argument -%c\n",
900526
+					program, isc_commandline_option);
900526
+			/* FALLTHROUGH */
900526
+		case 'h':
900526
+			usage();
900526
+
900526
+		default:
900526
+			fprintf(stderr, "%s: unhandled option -%c\n",
900526
+				program, isc_commandline_option);
900526
+			exit(1);
900526
+		}
900526
+	}
900526
+
900526
+	rdclass = strtoclass(classname);
900526
+
900526
+	if (argc < isc_commandline_index + 1 && filename == NULL)
900526
+		fatal("the key file name was not specified");
900526
+	if (argc > isc_commandline_index + 1)
900526
+		fatal("extraneous arguments");
900526
+
900526
+	if (ectx == NULL)
900526
+		setup_entropy(mctx, NULL, &ectx);
900526
+	result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
900526
+	if (result != ISC_R_SUCCESS)
900526
+		fatal("could not initialize hash");
900526
+	result = dst_lib_init(mctx, ectx,
900526
+			      ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
900526
+	if (result != ISC_R_SUCCESS)
900526
+		fatal("could not initialize dst: %s",
900526
+		      isc_result_totext(result));
900526
+	isc_entropy_stopcallbacksources(ectx);
900526
+
900526
+	setup_logging(verbose, mctx, &log;;
900526
+
900526
+	dns_rdataset_init(&rdataset);
900526
+
900526
+	if (filename != NULL) {
900526
+		if (argc < isc_commandline_index + 1 && filename != NULL) {
900526
+			/* using zone name as the zone file name */
900526
+			namestr = filename;
900526
+		} else
900526
+			namestr = argv[isc_commandline_index];
900526
+
900526
+		result = initname(namestr);
900526
+		if (result != ISC_R_SUCCESS)
900526
+			fatal("could not initialize name %s", namestr);
900526
+
900526
+		result = loadset(filename, &rdataset);
900526
+
900526
+		if (result != ISC_R_SUCCESS)
900526
+			fatal("could not load DNSKEY set: %s\n",
900526
+			      isc_result_totext(result));
900526
+
900526
+		for (result = dns_rdataset_first(&rdataset);
900526
+		     result == ISC_R_SUCCESS;
900526
+		     result = dns_rdataset_next(&rdataset)) {
900526
+
900526
+			dns_rdata_init(&rdata);
900526
+			dns_rdataset_current(&rdataset, &rdata);
900526
+			emit(dir, &rdata);
900526
+		}
900526
+	} else {
900526
+		unsigned char key_buf[DST_KEY_MAXSIZE];
900526
+
900526
+		loadkey(argv[isc_commandline_index], key_buf,
900526
+			DST_KEY_MAXSIZE, &rdata);
900526
+
900526
+		emit(dir, &rdata);
900526
+	}
900526
+
900526
+	if (dns_rdataset_isassociated(&rdataset))
900526
+		dns_rdataset_disassociate(&rdataset);
900526
+	cleanup_logging(&log;;
900526
+	dst_lib_destroy();
900526
+	isc_hash_destroy();
900526
+	cleanup_entropy(&ectx);
900526
+	dns_name_destroy();
900526
+	if (verbose > 10)
900526
+		isc_mem_stats(mctx, stdout);
900526
+	isc_mem_destroy(&mctx);
900526
+
900526
+	fflush(stdout);
900526
+	if (ferror(stdout)) {
900526
+		fprintf(stderr, "write error\n");
900526
+		return (1);
900526
+	} else
900526
+		return (0);
900526
+}
900526
diff --git a/bin/dnssec/dnssec-importkey.docbook b/bin/dnssec/dnssec-importkey.docbook
900526
new file mode 100644
900526
index 0000000..b8d160c
900526
--- /dev/null
900526
+++ b/bin/dnssec/dnssec-importkey.docbook
900526
@@ -0,0 +1,185 @@
900526
+
900526
+               "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"
900526
+               []>
900526
+
900526
+ - Copyright (C) 2009-2011  Internet Systems Consortium, Inc. ("ISC")
900526
+ -
900526
+ - Permission to use, copy, modify, and/or distribute this software for any
900526
+ - purpose with or without fee is hereby granted, provided that the above
900526
+ - copyright notice and this permission notice appear in all copies.
900526
+ -
900526
+ - THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
900526
+ - REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
900526
+ - AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
900526
+ - INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
900526
+ - LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
900526
+ - OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
900526
+ - PERFORMANCE OF THIS SOFTWARE.
900526
+-->
900526
+
900526
+
900526
+<refentry id="man.dnssec-importkey">
900526
+  <refentryinfo>
900526
+    <date>August 30, 2013</date>
900526
+  </refentryinfo>
900526
+
900526
+  <refmeta>
900526
+    <refentrytitle><application>dnssec-importkey</application></refentrytitle>
900526
+    <manvolnum>8</manvolnum>
900526
+    <refmiscinfo>BIND9</refmiscinfo>
900526
+  </refmeta>
900526
+
900526
+  <refnamediv>
900526
+    <refname><application>dnssec-importkey</application></refname>
900526
+    <refpurpose>Import DNSKEY records from external systems so they can be managed.</refpurpose>
900526
+  </refnamediv>
900526
+
900526
+  <docinfo>
900526
+    <copyright>
900526
+      <year>2013</year>
900526
+      <holder>Internet Systems Consortium, Inc. ("ISC")</holder>
900526
+    </copyright>
900526
+  </docinfo>
900526
+
900526
+  <refsynopsisdiv>
900526
+    <cmdsynopsis>
900526
+      <command>dnssec-importkey</command>
900526
+      <arg><option>-f <replaceable class="parameter">filename</replaceable></option></arg>
900526
+      <arg><option>-K <replaceable class="parameter">directory</replaceable></option></arg>
900526
+      <arg><option>-P <replaceable class="parameter">date/offset</replaceable></option></arg>
900526
+      <arg><option>-D <replaceable class="parameter">date/offset</replaceable></option></arg>
900526
+      <arg><option>-h</option></arg>
900526
+      <arg><option>-v <replaceable class="parameter">level</replaceable></option></arg>
900526
+      <arg><option>keyname</option></arg>
900526
+    </cmdsynopsis>
900526
+  </refsynopsisdiv>
900526
+
900526
+  <refsect1>
900526
+    <title>DESCRIPTION</title>
900526
+    <para><command>dnssec-importkey</command>
900526
+      read a DNSKEY record and generated a .key/.private key pair.
900526
+      Publication (<option>-P</option>) and deletions (<option>-D</option>)
900526
+      times can be set for the key.
900526
+    </para>
900526
+  </refsect1>
900526
+
900526
+  <refsect1>
900526
+    <title>OPTIONS</title>
900526
+
900526
+    <variablelist>
900526
+      <varlistentry>
900526
+	<term>-f <replaceable class="parameter">filename</replaceable></term>
900526
+        <listitem>
900526
+	  <para>
900526
+	    Filename to read the key from.
900526
+	  </para>
900526
+        </listitem>
900526
+      </varlistentry>
900526
+  
900526
+      <varlistentry>
900526
+        <term>-K <replaceable class="parameter">directory</replaceable></term>
900526
+        <listitem>
900526
+          <para>
900526
+            Sets the directory in which the key files are to reside.
900526
+          </para>
900526
+        </listitem>
900526
+      </varlistentry>
900526
+
900526
+      <varlistentry>
900526
+        <term>-L <replaceable class="parameter">ttl</replaceable></term>
900526
+        <listitem>
900526
+          <para>
900526
+            Sets the default TTL to use for this key when it is converted
900526
+            into a DNSKEY RR.  If the key is imported into a zone,
900526
+            this is the TTL that will be used for it, unless there was
900526
+            already a DNSKEY RRset in place, in which case the existing TTL
900526
+            would take precedence.  importkey the default TTL to
900526
+            <literal>0</literal> or <literal>none</literal> removes it.
900526
+          </para>
900526
+        </listitem>
900526
+      </varlistentry>
900526
+
900526
+      <varlistentry>
900526
+	<term>-h</term>
900526
+        <listitem>
900526
+	  <para>
900526
+	    Emit usage message and exit.
900526
+	  </para>
900526
+        </listitem>
900526
+      </varlistentry>
900526
+  
900526
+      <varlistentry>
900526
+        <term>-v <replaceable class="parameter">level</replaceable></term>
900526
+        <listitem>
900526
+          <para>
900526
+            Sets the debugging level.
900526
+          </para>
900526
+        </listitem>
900526
+      </varlistentry>
900526
+
900526
+    </variablelist>
900526
+  </refsect1>
900526
+
900526
+  <refsect1>
900526
+    <title>TIMING OPTIONS</title>
900526
+    <para>
900526
+      Dates can be expressed in the format YYYYMMDD or YYYYMMDDHHMMSS.
900526
+      If the argument begins with a '+' or '-', it is interpreted as
900526
+      an offset from the present time.  For convenience, if such an offset
900526
+      is followed by one of the suffixes 'y', 'mo', 'w', 'd', 'h', or 'mi',
900526
+      then the offset is computed in years (defined as 365 24-hour days,
900526
+      ignoring leap years), months (defined as 30 24-hour days), weeks,
900526
+      days, hours, or minutes, respectively.  Without a suffix, the offset
900526
+      is computed in seconds.  To unset a date, use 'none'.
900526
+    </para>
900526
+
900526
+    <variablelist>
900526
+      <varlistentry>
900526
+        <term>-P <replaceable class="parameter">date/offset</replaceable></term>
900526
+        <listitem>
900526
+          <para>
900526
+            Sets the date on which a key is to be published to the zone.
900526
+            After that date, the key will be included in the zone but will
900526
+            not be used to sign it.
900526
+          </para>
900526
+        </listitem>
900526
+      </varlistentry>
900526
+
900526
+      <varlistentry>
900526
+        <term>-D <replaceable class="parameter">date/offset</replaceable></term>
900526
+        <listitem>
900526
+          <para>
900526
+            Sets the date on which the key is to be deleted.  After that
900526
+            date, the key will no longer be included in the zone.  (It
900526
+            may remain in the key repository, however.)
900526
+          </para>
900526
+        </listitem>
900526
+      </varlistentry>
900526
+
900526
+    </variablelist>
900526
+  </refsect1>
900526
+
900526
+  <refsect1>
900526
+    <title>SEE ALSO</title>
900526
+    <para><citerefentry>
900526
+        <refentrytitle>dnssec-keygen</refentrytitle><manvolnum>8</manvolnum>
900526
+      </citerefentry>,
900526
+      <citerefentry>
900526
+        <refentrytitle>dnssec-signzone</refentrytitle><manvolnum>8</manvolnum>
900526
+      </citerefentry>,
900526
+      <citetitle>BIND 9 Administrator Reference Manual</citetitle>,
900526
+      <citetitle>RFC 5011</citetitle>.
900526
+    </para>
900526
+  </refsect1>
900526
+
900526
+  <refsect1>
900526
+    <title>AUTHOR</title>
900526
+    <para><corpauthor>Internet Systems Consortium</corpauthor>
900526
+    </para>
900526
+  </refsect1>
900526
+
900526
+</refentry>
900526
+ - Local variables:
900526
+ - mode: sgml
900526
+ - End:
900526
+-->
900526
diff --git a/bin/dnssec/dnssec-importkey.html b/bin/dnssec/dnssec-importkey.html
900526
new file mode 100644
900526
index 0000000..f74be50
900526
--- /dev/null
900526
+++ b/bin/dnssec/dnssec-importkey.html
900526
@@ -0,0 +1,112 @@
900526
+
900526
+ - Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
900526
+ - 
900526
+ - Permission to use, copy, modify, and/or distribute this software for any
900526
+ - purpose with or without fee is hereby granted, provided that the above
900526
+ - copyright notice and this permission notice appear in all copies.
900526
+ - 
900526
+ - THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
900526
+ - REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
900526
+ - AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
900526
+ - INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
900526
+ - LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
900526
+ - OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
900526
+ - PERFORMANCE OF THIS SOFTWARE.
900526
+-->
900526
+
900526
+<html>
900526
+<head>
900526
+<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
900526
+<title>dnssec-importkey</title>
900526
+<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
900526
+</head>
900526
+<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
900526
+
900526
+
900526
+

Name

900526
+

dnssec-importkey — Import DNSKEY records from external systems so they can be managed.

900526
+
900526
+
900526
+

Synopsis

900526
+

dnssec-importkey [-f filename] [-K directory] [-P date/offset] [-D date/offset] [-h] [-v level] [keyname]

900526
+
900526
+
900526
+

DESCRIPTION

900526
+

dnssec-importkey

900526
+      read a DNSKEY record and generated a .key/.private key pair.
900526
+      Publication (-P) and deletions (-D)
900526
+      times can be set for the key.
900526
+    

900526
+
900526
+
900526
+

OPTIONS

900526
+
900526
+
-f filename
900526
+

900526
+	    Filename to read the key from.
900526
+	  

900526
+
-K directory
900526
+

900526
+            Sets the directory in which the key files are to reside.
900526
+          

900526
+
-L ttl
900526
+

900526
+            Sets the default TTL to use for this key when it is converted
900526
+            into a DNSKEY RR.  If the key is imported into a zone,
900526
+            this is the TTL that will be used for it, unless there was
900526
+            already a DNSKEY RRset in place, in which case the existing TTL
900526
+            would take precedence.  importkey the default TTL to
900526
+            0 or none removes it.
900526
+          

900526
+
-h
900526
+

900526
+	    Emit usage message and exit.
900526
+	  

900526
+
-v level
900526
+

900526
+            Sets the debugging level.
900526
+          

900526
+
900526
+
900526
+
900526
+

TIMING OPTIONS

900526
+

900526
+      Dates can be expressed in the format YYYYMMDD or YYYYMMDDHHMMSS.
900526
+      If the argument begins with a '+' or '-', it is interpreted as
900526
+      an offset from the present time.  For convenience, if such an offset
900526
+      is followed by one of the suffixes 'y', 'mo', 'w', 'd', 'h', or 'mi',
900526
+      then the offset is computed in years (defined as 365 24-hour days,
900526
+      ignoring leap years), months (defined as 30 24-hour days), weeks,
900526
+      days, hours, or minutes, respectively.  Without a suffix, the offset
900526
+      is computed in seconds.  To unset a date, use 'none'.
900526
+    

900526
+
900526
+
-P date/offset
900526
+

900526
+            Sets the date on which a key is to be published to the zone.
900526
+            After that date, the key will be included in the zone but will
900526
+            not be used to sign it.
900526
+          

900526
+
-D date/offset
900526
+

900526
+            Sets the date on which the key is to be deleted.  After that
900526
+            date, the key will no longer be included in the zone.  (It
900526
+            may remain in the key repository, however.)
900526
+          

900526
+
900526
+
900526
+
900526
+

SEE ALSO

900526
+

dnssec-keygen(8),

900526
+      dnssec-signzone(8),
900526
+      BIND 9 Administrator Reference Manual,
900526
+      RFC 5011.
900526
+    

900526
+
900526
+
900526
+

AUTHOR

900526
+

Internet Systems Consortium

900526
+    

900526
+
900526
+</body>
900526
+</html>
900526
diff --git a/bin/dnssec/dnssec-settime.c b/bin/dnssec/dnssec-settime.c
900526
index 4c88a07..108d803 100644
900526
--- a/bin/dnssec/dnssec-settime.c
900526
+++ b/bin/dnssec/dnssec-settime.c
900526
@@ -370,7 +370,7 @@ main(int argc, char **argv) {
900526
 		if (result != ISC_R_SUCCESS)
900526
 			fatal("Invalid keyfile %s: %s",
900526
 			      filename, isc_result_totext(result));
900526
-		if (!dst_key_isprivate(prevkey))
900526
+		if (!dst_key_isprivate(prevkey) && !dst_key_isexternal(prevkey))
900526
 			fatal("%s is not a private key", filename);
900526
 
900526
 		name = dst_key_name(prevkey);
900526
@@ -462,7 +462,7 @@ main(int argc, char **argv) {
900526
 		fatal("Invalid keyfile %s: %s",
900526
 		      filename, isc_result_totext(result));
900526
 
900526
-	if (!dst_key_isprivate(key))
900526
+	if (!dst_key_isprivate(key) && !dst_key_isexternal(key))
900526
 		fatal("%s is not a private key", filename);
900526
 
900526
 	dst_key_format(key, keystr, sizeof(keystr));
900526
diff --git a/bin/tests/system/conf.sh.in b/bin/tests/system/conf.sh.in
900526
index 60f22f8..b15853d 100644
900526
--- a/bin/tests/system/conf.sh.in
900526
+++ b/bin/tests/system/conf.sh.in
900526
@@ -43,6 +43,7 @@ SIGNER=$TOP/bin/dnssec/dnssec-signzone
900526
 REVOKE=$TOP/bin/dnssec/dnssec-revoke
900526
 SETTIME=$TOP/bin/dnssec/dnssec-settime
900526
 DSFROMKEY=$TOP/bin/dnssec/dnssec-dsfromkey
900526
+IMPORTKEY=$TOP/bin/dnssec/dnssec-importkey
900526
 CHECKDS=$TOP/bin/python/dnssec-checkds
900526
 COVERAGE=$TOP/bin/python/dnssec-coverage
900526
 CHECKZONE=$TOP/bin/check/named-checkzone
900526
diff --git a/bin/tests/system/inline/clean.sh b/bin/tests/system/inline/clean.sh
900526
index 8ef3e2c..412031a 100644
900526
--- a/bin/tests/system/inline/clean.sh
900526
+++ b/bin/tests/system/inline/clean.sh
900526
@@ -60,6 +60,9 @@ rm -f ns3/retransfer.bk
900526
 rm -f ns3/retransfer.bk.jnl
900526
 rm -f ns3/retransfer.bk.signed
900526
 rm -f ns3/retransfer.bk.signed.jnl
900526
+rm -f ns3/externalkey.db
900526
+rm -f ns3/externalkey.db.signed
900526
+rm -f ns3/externalkey.db.signed.jnl
900526
 rm -f ns4/K*
900526
 rm -f ns4/noixfr.db
900526
 rm -f ns4/noixfr.db.jnl
900526
diff --git a/bin/tests/system/inline/ns1/root.db.in b/bin/tests/system/inline/ns1/root.db.in
900526
index 8d3af51..a08db51 100644
900526
--- a/bin/tests/system/inline/ns1/root.db.in
900526
+++ b/bin/tests/system/inline/ns1/root.db.in
900526
@@ -50,3 +50,6 @@ ns3.retransfer.		A	10.53.0.3
900526
 
900526
 nsec3.			NS	ns3.nsec3.
900526
 ns3.nsec3.		A	10.53.0.3
900526
+
900526
+externalkey.		NS	ns3.externalkey.
900526
+ns3.externalkey.	A	10.53.0.3
900526
diff --git a/bin/tests/system/inline/ns3/named.conf b/bin/tests/system/inline/ns3/named.conf
900526
index a17384c..7c23edd 100644
900526
--- a/bin/tests/system/inline/ns3/named.conf
900526
+++ b/bin/tests/system/inline/ns3/named.conf
900526
@@ -103,3 +103,11 @@ zone "nsec3" {
900526
 	allow-update { any; };
900526
 	file "nsec3.db";
900526
 };
900526
+
900526
+zone "externalkey" {
900526
+	type master;
900526
+	inline-signing yes;
900526
+	auto-dnssec maintain;
900526
+	allow-update { any; };
900526
+	file "externalkey.db";
900526
+};
900526
diff --git a/bin/tests/system/inline/ns3/sign.sh b/bin/tests/system/inline/ns3/sign.sh
900526
index bbd11af..f695973 100644
900526
--- a/bin/tests/system/inline/ns3/sign.sh
900526
+++ b/bin/tests/system/inline/ns3/sign.sh
900526
@@ -92,3 +92,32 @@ do
900526
 	keyname=`$KEYGEN -q -r $RANDFILE -a RSASHA1 -b 768 -n zone $zone`
900526
 	keyname=`$KEYGEN -q -r $RANDFILE -a RSASHA1 -b 1024 -n zone -f KSK $zone`
900526
 done
900526
+
900526
+zone=externalkey
900526
+rm -f K${zone}.+*+*.key
900526
+rm -f K${zone}.+*+*.private
900526
+
900526
+for alg in ECDSAP256SHA256 NSEC3RSASHA1 DSA ECCGOST
900526
+do
900526
+
900526
+if test $alg = ECCGOST
900526
+then
900526
+	sh ../../gost/prereq.sh 2> /dev/null || continue
900526
+fi
900526
+
900526
+k1=`$KEYGEN -q -r $RANDFILE -a $alg -b 1024 -n zone -f KSK $zone`
900526
+k2=`$KEYGEN -q -r $RANDFILE -a $alg -b 1024 -n zone $zone`
900526
+k3=`$KEYGEN -q -r $RANDFILE -a $alg -b 1024 -n zone $zone`
900526
+k4=`$KEYGEN -q -r $RANDFILE -a $alg -b 1024 -n zone $zone`
900526
+keyname=`$KEYGEN -q -r $RANDFILE -a $alg -b 1024 -n zone $zone`
900526
+keyname=`$KEYGEN -q -r $RANDFILE -a $alg -b 1024 -n zone -f KSK $zone`
900526
+$DSFROMKEY -T 1200 $keyname >> ../ns1/root.db
900526
+rm -f ${k3}.* ${k4}.*
900526
+
900526
+#
900526
+# Convert k1 and k2 in to External Keys.
900526
+rm -f $k1.private 
900526
+$IMPORTKEY -P now -D now+3600 -f $k1.key $zone
900526
+rm -f $k2.private 
900526
+$IMPORTKEY -f $k2.key $zone
900526
+done
900526
diff --git a/bin/tests/system/inline/setup.sh b/bin/tests/system/inline/setup.sh
900526
index 9fa42ab..adee4ff 100644
900526
--- a/bin/tests/system/inline/setup.sh
900526
+++ b/bin/tests/system/inline/setup.sh
900526
@@ -29,6 +29,7 @@ cp ns3/master.db.in ns3/dynamic.db
900526
 cp ns3/master.db.in ns3/updated.db
900526
 cp ns3/master.db.in ns3/expired.db
900526
 cp ns3/master.db.in ns3/nsec3.db
900526
+cp ns3/master.db.in ns3/externalkey.db
900526
 
900526
 touch ns4/trusted.conf
900526
 cp ns4/noixfr.db.in ns4/noixfr.db
900526
diff --git a/bin/tests/system/inline/tests.sh b/bin/tests/system/inline/tests.sh
900526
index 7e2e3d2..8acdee2 100644
900526
--- a/bin/tests/system/inline/tests.sh
900526
+++ b/bin/tests/system/inline/tests.sh
900526
@@ -809,7 +809,22 @@ $RNDC -c ../common/rndc.conf -s 10.53.0.3 -p 9953 addzone test-$zone \
900526
 $RNDC -c ../common/rndc.conf -s 10.53.0.3 -p 9953 delzone test-$zone
900526
 done
900526
 
900526
-if [ $ret != 0 ]; then echo "I:failed"; fi
900526
+n=`expr $n + 1`
900526
+echo "I:testing adding external keys to a inline zone ($n)"
900526
+ret=0
900526
+$DIG $DIGOPTS @10.53.0.3 -p 5300 dnskey externalkey > dig.out.ns3.test$n
900526
+for alg in 3 7 12 13
900526
+do
900526
+if test $alg = 12 
900526
+then
900526
+	sh ../gost/prereq.sh 2>/dev/null || continue;
900526
+fi
900526
+
900526
+dnskeys=`grep "IN.DNSKEY.25[67] [0-9]* $alg " dig.out.ns3.test$n | wc -l`
900526
+rrsigs=`grep "RRSIG.DNSKEY $alg " dig.out.ns3.test$n | wc -l`
900526
+test ${dnskeys:-0} -eq 3 || { echo "I: failed $alg (dnskeys ${dnskeys:-0})"; ret=1; }
900526
+test ${rrsigs:-0} -eq 2 || { echo "I: failed $alg (rrsigs ${rrsigs:-0})"; ret=1; }
900526
+done
900526
 status=`expr $status + $ret`
900526
 
900526
 exit $status
900526
diff --git a/lib/dns/dnssec.c b/lib/dns/dnssec.c
900526
index a1c5c69..cf97404 100644
900526
--- a/lib/dns/dnssec.c
900526
+++ b/lib/dns/dnssec.c
900526
@@ -684,6 +684,7 @@ dns_dnssec_findzonekeys2(dns_db_t *db, dns_dbversion_t *ver,
900526
 	isc_stdtime_get(&now;;
900526
 
900526
 	*nkeys = 0;
900526
+	memset(keys, 0, sizeof(*keys) * maxkeys);
900526
 	dns_rdataset_init(&rdataset);
900526
 	RETERR(dns_db_findrdataset(db, node, ver, dns_rdatatype_dnskey, 0, 0,
900526
 				   &rdataset, NULL));
900526
@@ -1312,9 +1313,9 @@ dns_dnssec_findmatchingkeys(dns_name_t *origin, const char *directory,
900526
 	isc_dir_t dir;
900526
 	dns_dnsseckey_t *key = NULL;
900526
 	dst_key_t *dstkey = NULL;
900526
-	char namebuf[DNS_NAME_FORMATSIZE], *p;
900526
+	char namebuf[DNS_NAME_FORMATSIZE];
900526
 	isc_buffer_t b;
900526
-	unsigned int len;
900526
+	unsigned int len, i;
900526
 	isc_stdtime_t now;
900526
 
900526
 	REQUIRE(keylist != NULL);
900526
@@ -1334,49 +1335,62 @@ dns_dnssec_findmatchingkeys(dns_name_t *origin, const char *directory,
900526
 	isc_stdtime_get(&now;;
900526
 
900526
 	while (isc_dir_read(&dir) == ISC_R_SUCCESS) {
900526
-		if (dir.entry.name[0] == 'K' &&
900526
-		    dir.entry.length > len + 1 &&
900526
-		    dir.entry.name[len + 1] == '+' &&
900526
-		    strncasecmp(dir.entry.name + 1, namebuf, len) == 0) {
900526
-			p = strrchr(dir.entry.name, '.');
900526
-			if (p != NULL && strcmp(p, ".private") != 0)
900526
-				continue;
900526
+		if (dir.entry.name[0] != 'K' ||
900526
+		    dir.entry.length < len + 1 ||
900526
+		    dir.entry.name[len + 1] != '+' ||
900526
+		    strncasecmp(dir.entry.name + 1, namebuf, len) != 0)
900526
+			continue;
900526
+
900526
+		for (i = len + 1 + 1; i < dir.entry.length ; i++)
900526
+			if (dir.entry.name[i] < '0' || dir.entry.name[i] > '9')
900526
+				break;
900526
+
900526
+		if (i == len + 1 + 1 || i >= dir.entry.length ||
900526
+		    dir.entry.name[i] != '+')
900526
+			continue;
900526
+
900526
+		for (i++ ; i < dir.entry.length ; i++)
900526
+			if (dir.entry.name[i] < '0' || dir.entry.name[i] > '9')
900526
+				break;
900526
 
900526
-			dstkey = NULL;
900526
-			result = dst_key_fromnamedfile(dir.entry.name,
900526
-						       directory,
900526
-						       DST_TYPE_PUBLIC |
900526
-						       DST_TYPE_PRIVATE,
900526
-						       mctx, &dstkey);
900526
-
900526
-			if (result != ISC_R_SUCCESS) {
900526
-				isc_log_write(dns_lctx,
900526
-					      DNS_LOGCATEGORY_GENERAL,
900526
-					      DNS_LOGMODULE_DNSSEC,
900526
-					      ISC_LOG_WARNING,
900526
-					      "dns_dnssec_findmatchingkeys: "
900526
-					      "error reading key file %s: %s",
900526
-					      dir.entry.name,
900526
-					      isc_result_totext(result));
900526
+		if (strcmp(dir.entry.name + i, ".private") != 0)
900526
 				continue;
900526
-			}
900526
 
900526
-			RETERR(dns_dnsseckey_create(mctx, &dstkey, &key));
900526
-			key->source = dns_keysource_repository;
900526
-			get_hints(key, now);
900526
+		dstkey = NULL;
900526
+		result = dst_key_fromnamedfile(dir.entry.name,
900526
+					       directory,
900526
+					       DST_TYPE_PUBLIC |
900526
+					       DST_TYPE_PRIVATE,
900526
+					       mctx, &dstkey);
900526
 
900526
-			if (key->legacy) {
900526
-				dns_dnsseckey_destroy(mctx, &key);
900526
-			} else {
900526
-				ISC_LIST_APPEND(list, key, link);
900526
-				key = NULL;
900526
-			}
900526
+		if (result != ISC_R_SUCCESS) {
900526
+			isc_log_write(dns_lctx,
900526
+				      DNS_LOGCATEGORY_GENERAL,
900526
+				      DNS_LOGMODULE_DNSSEC,
900526
+				      ISC_LOG_WARNING,
900526
+				      "dns_dnssec_findmatchingkeys: "
900526
+				      "error reading key file %s: %s",
900526
+				      dir.entry.name,
900526
+				      isc_result_totext(result));
900526
+			continue;
900526
+		}
900526
+
900526
+		RETERR(dns_dnsseckey_create(mctx, &dstkey, &key));
900526
+		key->source = dns_keysource_repository;
900526
+		get_hints(key, now);
900526
+
900526
+		if (key->legacy) {
900526
+			dns_dnsseckey_destroy(mctx, &key);
900526
+		} else {
900526
+			ISC_LIST_APPEND(list, key, link);
900526
+			key = NULL;
900526
 		}
900526
 	}
900526
 
900526
-	if (!ISC_LIST_EMPTY(list))
900526
+	if (!ISC_LIST_EMPTY(list)) {
900526
+		result = ISC_R_SUCCESS;
900526
 		ISC_LIST_APPENDLIST(*keylist, list, link);
900526
-	else
900526
+	} else
900526
 		result = ISC_R_NOTFOUND;
900526
 
900526
  failure:
900526
@@ -1794,7 +1808,13 @@ dns_dnssec_updatekeys(dns_dnsseckeylist_t *keys, dns_dnsseckeylist_t *newkeys,
900526
 		for (key2 = ISC_LIST_HEAD(*keys);
900526
 		     key2 != NULL;
900526
 		     key2 = ISC_LIST_NEXT(key2, link)) {
900526
-			if (dst_key_pubcompare(key1->key, key2->key,
900526
+			int f1 = dst_key_flags(key1->key);
900526
+			int f2 = dst_key_flags(key2->key);
900526
+			int nr1 = f1 & ~DNS_KEYFLAG_REVOKE;
900526
+			int nr2 = f2 & ~DNS_KEYFLAG_REVOKE;
900526
+			if (nr1 == nr2 &&
900526
+			    dst_key_alg(key1->key) == dst_key_alg(key2->key) &&
900526
+			    dst_key_pubcompare(key1->key, key2->key,
900526
 					       ISC_TRUE)) {
900526
 				int r1, r2;
900526
 				r1 = dst_key_flags(key1->key) &
900526
diff --git a/lib/dns/dst_api.c b/lib/dns/dst_api.c
900526
index c310be5..4c174f3 100644
900526
--- a/lib/dns/dst_api.c
900526
+++ b/lib/dns/dst_api.c
900526
@@ -448,6 +448,16 @@ dst_key_tofile(const dst_key_t *key, int type, const char *directory) {
900526
 		return (ISC_R_SUCCESS);
900526
 }
900526
 
900526
+void
900526
+dst_key_setexternal(dst_key_t *key, isc_boolean_t value) {
900526
+	key->external = value;
900526
+}
900526
+
900526
+isc_boolean_t
900526
+dst_key_isexternal(dst_key_t *key) {
900526
+	return (key->external);
900526
+}
900526
+
900526
 isc_result_t
900526
 dst_key_fromfile(dns_name_t *name, dns_keytag_t id,
900526
 		 unsigned int alg, int type, const char *directory,
900526
diff --git a/lib/dns/dst_internal.h b/lib/dns/dst_internal.h
900526
index 48ce9b8..49ca424 100644
900526
--- a/lib/dns/dst_internal.h
900526
+++ b/lib/dns/dst_internal.h
900526
@@ -128,6 +128,7 @@ struct dst_key {
900526
 	isc_boolean_t	numset[DST_MAX_NUMERIC + 1]; /*%< data set? */
900526
 	isc_boolean_t 	inactive;      /*%< private key not present as it is
900526
 					    inactive */
900526
+	isc_boolean_t 	external;      /*%< external key */
900526
 
900526
 	int		fmt_major;     /*%< private key format, major version */
900526
 	int		fmt_minor;     /*%< private key format, minor version */
900526
diff --git a/lib/dns/dst_parse.c b/lib/dns/dst_parse.c
900526
index ca43cb3..6348cc1 100644
900526
--- a/lib/dns/dst_parse.c
900526
+++ b/lib/dns/dst_parse.c
900526
@@ -178,14 +178,18 @@ find_numericdata(const char *s) {
900526
 }
900526
 
900526
 static int
900526
-check_rsa(const dst_private_t *priv) {
900526
+check_rsa(const dst_private_t *priv, isc_boolean_t external) {
900526
 	int i, j;
900526
 	isc_boolean_t have[RSA_NTAGS];
900526
 	isc_boolean_t ok;
900526
 	unsigned int mask;
900526
 
900526
+	if (external)
900526
+		return ((priv->nelements == 0) ? 0 : -1);
900526
+
900526
 	for (i = 0; i < RSA_NTAGS; i++)
900526
 		have[i] = ISC_FALSE;
900526
+
900526
 	for (j = 0; j < priv->nelements; j++) {
900526
 		for (i = 0; i < RSA_NTAGS; i++)
900526
 			if (priv->elements[j].tag == TAG(DST_ALG_RSAMD5, i))
900526
@@ -231,10 +235,15 @@ check_dh(const dst_private_t *priv) {
900526
 }
900526
 
900526
 static int
900526
-check_dsa(const dst_private_t *priv) {
900526
+check_dsa(const dst_private_t *priv, isc_boolean_t external) {
900526
 	int i, j;
900526
+
900526
+	if (external)
900526
+		return ((priv->nelements == 0)? 0 : -1);
900526
+
900526
 	if (priv->nelements != DSA_NTAGS)
900526
 		return (-1);
900526
+
900526
 	for (i = 0; i < DSA_NTAGS; i++) {
900526
 		for (j = 0; j < priv->nelements; j++)
900526
 			if (priv->elements[j].tag == TAG(DST_ALG_DSA, i))
900526
@@ -246,7 +255,11 @@ check_dsa(const dst_private_t *priv) {
900526
 }
900526
 
900526
 static int
900526
-check_gost(const dst_private_t *priv) {
900526
+check_gost(const dst_private_t *priv, isc_boolean_t external) {
900526
+
900526
+	if (external)
900526
+		return ((priv->nelements == 0)? 0 : -1);
900526
+
900526
 	if (priv->nelements != GOST_NTAGS)
900526
 		return (-1);
900526
 	if (priv->elements[0].tag != TAG(DST_ALG_ECCGOST, 0))
900526
@@ -255,7 +268,11 @@ check_gost(const dst_private_t *priv) {
900526
 }
900526
 
900526
 static int
900526
-check_ecdsa(const dst_private_t *priv) {
900526
+check_ecdsa(const dst_private_t *priv, isc_boolean_t external) {
900526
+
900526
+	if (external)
900526
+		return ((priv->nelements == 0) ? 0 : -1);
900526
+
900526
 	if (priv->nelements != ECDSA_NTAGS)
900526
 		return (-1);
900526
 	if (priv->elements[0].tag != TAG(DST_ALG_ECDSA256, 0))
900526
@@ -309,7 +326,7 @@ check_hmac_sha(const dst_private_t *priv, unsigned int ntags,
900526
 
900526
 static int
900526
 check_data(const dst_private_t *priv, const unsigned int alg,
900526
-	   isc_boolean_t old)
900526
+	   isc_boolean_t old, isc_boolean_t external)
900526
 {
900526
 	/* XXXVIX this switch statement is too sparse to gen a jump table. */
900526
 	switch (alg) {
900526
@@ -318,17 +335,17 @@ check_data(const dst_private_t *priv, const unsigned int alg,
900526
 	case DST_ALG_NSEC3RSASHA1:
900526
 	case DST_ALG_RSASHA256:
900526
 	case DST_ALG_RSASHA512:
900526
-		return (check_rsa(priv));
900526
+		return (check_rsa(priv, external));
900526
 	case DST_ALG_DH:
900526
 		return (check_dh(priv));
900526
 	case DST_ALG_DSA:
900526
 	case DST_ALG_NSEC3DSA:
900526
-		return (check_dsa(priv));
900526
+		return (check_dsa(priv, external));
900526
 	case DST_ALG_ECCGOST:
900526
-		return (check_gost(priv));
900526
+		return (check_gost(priv, external));
900526
 	case DST_ALG_ECDSA256:
900526
 	case DST_ALG_ECDSA384:
900526
-		return (check_ecdsa(priv));
900526
+		return (check_ecdsa(priv, external));
900526
 	case DST_ALG_HMACMD5:
900526
 		return (check_hmac_md5(priv, old));
900526
 	case DST_ALG_HMACSHA1:
900526
@@ -372,6 +389,7 @@ dst__privstruct_parse(dst_key_t *key, unsigned int alg, isc_lex_t *lex,
900526
 	unsigned int opt = ISC_LEXOPT_EOL;
900526
 	isc_stdtime_t when;
900526
 	isc_result_t ret;
900526
+	isc_boolean_t external = ISC_FALSE;
900526
 
900526
 	REQUIRE(priv != NULL);
900526
 
900526
@@ -467,9 +485,15 @@ dst__privstruct_parse(dst_key_t *key, unsigned int alg, isc_lex_t *lex,
900526
 
900526
 		if (token.type != isc_tokentype_string) {
900526
 			ret = DST_R_INVALIDPRIVATEKEY;
900526
+			NEXTTOKEN(lex, opt, &token);
900526
 			goto fail;
900526
 		}
900526
 
900526
+		if (strcmp(DST_AS_STR(token), "External:") == 0) {
900526
+			external = ISC_TRUE;
900526
+			goto next;
900526
+		}
900526
+
900526
 		/* Numeric metadata */
900526
 		tag = find_numericdata(DST_AS_STR(token));
900526
 		if (tag >= 0) {
900526
@@ -534,8 +558,14 @@ dst__privstruct_parse(dst_key_t *key, unsigned int alg, isc_lex_t *lex,
900526
 		READLINE(lex, opt, &token);
900526
 		data = NULL;
900526
 	}
900526
+
900526
  done:
900526
-	check = check_data(priv, alg, ISC_TRUE);
900526
+	if (external && priv->nelements != 0) {
900526
+		ret = DST_R_INVALIDPRIVATEKEY;
900526
+		goto fail;
900526
+	}
900526
+
900526
+	check = check_data(priv, alg, ISC_TRUE, external);
900526
 	if (check < 0) {
900526
 		ret = DST_R_INVALIDPRIVATEKEY;
900526
 		goto fail;
900526
@@ -544,6 +574,8 @@ dst__privstruct_parse(dst_key_t *key, unsigned int alg, isc_lex_t *lex,
900526
 		goto fail;
900526
 	}
900526
 
900526
+	key->external = external;
900526
+
900526
 	return (ISC_R_SUCCESS);
900526
 
900526
 fail:
900526
@@ -573,7 +605,7 @@ dst__privstruct_writefile(const dst_key_t *key, const dst_private_t *priv,
900526
 
900526
 	REQUIRE(priv != NULL);
900526
 
900526
-	ret = check_data(priv, dst_key_alg(key), ISC_FALSE);
900526
+	ret = check_data(priv, dst_key_alg(key), ISC_FALSE, key->external);
900526
 	if (ret < 0)
900526
 		return (DST_R_INVALIDPRIVATEKEY);
900526
 	else if (ret != ISC_R_SUCCESS)
900526
@@ -691,6 +723,9 @@ dst__privstruct_writefile(const dst_key_t *key, const dst_private_t *priv,
900526
 	       fprintf(fp, "%s %.*s\n", s, (int)r.length, r.base);
900526
 	}
900526
 
900526
+	if (key->external)
900526
+	       fprintf(fp, "External:\n");
900526
+
900526
 	/* Add the metadata tags */
900526
 	if (major > 1 || (major == 1 && minor >= 3)) {
900526
 		for (i = 0; i < NUMERIC_NTAGS; i++) {
900526
@@ -706,14 +741,14 @@ dst__privstruct_writefile(const dst_key_t *key, const dst_private_t *priv,
900526
 
900526
 			isc_buffer_init(&b, buffer, sizeof(buffer));
900526
 			result = dns_time32_totext(when, &b);
900526
-		       if (result != ISC_R_SUCCESS) {
900526
+			if (result != ISC_R_SUCCESS) {
900526
 			       fclose(fp);
900526
 			       return (DST_R_INVALIDPRIVATEKEY);
900526
-		       }
900526
+			}
900526
 
900526
 			isc_buffer_usedregion(&b, &r);
900526
 
900526
-		       fprintf(fp, "%s %.*s\n", timetags[i], (int)r.length,
900526
+			fprintf(fp, "%s %.*s\n", timetags[i], (int)r.length,
900526
 				r.base);
900526
 		}
900526
 	}
900526
diff --git a/lib/dns/dst_result.c b/lib/dns/dst_result.c
900526
index 297e809..30aa1fa 100644
900526
--- a/lib/dns/dst_result.c
900526
+++ b/lib/dns/dst_result.c
900526
@@ -35,7 +35,7 @@ static const char *text[DST_R_NRESULTS] = {
900526
 	"illegal operation for a null key",	/*%< 3 */
900526
 	"public key is invalid",		/*%< 4 */
900526
 	"private key is invalid",		/*%< 5 */
900526
-	"UNUSED6",				/*%< 6 */
900526
+	"external key",				/*%< 6 */
900526
 	"error occurred writing key to disk",	/*%< 7 */
900526
 	"invalid algorithm specific parameter",	/*%< 8 */
900526
 	"UNUSED9",				/*%< 9 */
900526
diff --git a/lib/dns/include/dns/master.h b/lib/dns/include/dns/master.h
900526
index 6abcb7e..931e930 100644
900526
--- a/lib/dns/include/dns/master.h
900526
+++ b/lib/dns/include/dns/master.h
900526
@@ -57,6 +57,7 @@
900526
 
900526
 #define DNS_MASTER_RESIGN	0x00002000
900526
 #define DNS_MASTER_KEY	 	0x00004000	/*%< Loading a key zone master file. */
900526
+#define DNS_MASTER_NOTTL	0x00008000	/*%< Don't require ttl. */
900526
 
900526
 ISC_LANG_BEGINDECLS
900526
 
900526
diff --git a/lib/dns/include/dst/dst.h b/lib/dns/include/dst/dst.h
900526
index 6ce3a0c..8676d1a 100644
900526
--- a/lib/dns/include/dst/dst.h
900526
+++ b/lib/dns/include/dst/dst.h
900526
@@ -953,6 +953,12 @@ dst_key_setinactive(dst_key_t *key, isc_boolean_t inactive);
900526
  *	'key' to be valid.
900526
  */
900526
 
900526
+void
900526
+dst_key_setexternal(dst_key_t *key, isc_boolean_t value);
900526
+
900526
+isc_boolean_t
900526
+dst_key_isexternal(dst_key_t *key);
900526
+
900526
 ISC_LANG_ENDDECLS
900526
 
900526
 #endif /* DST_DST_H */
900526
diff --git a/lib/dns/master.c b/lib/dns/master.c
900526
index aa8f1ac..1a2c84a 100644
900526
--- a/lib/dns/master.c
900526
+++ b/lib/dns/master.c
900526
@@ -592,9 +592,9 @@ loadctx_create(dns_masterformat_t format, isc_mem_t *mctx,
900526
 		isc_lex_setcomments(lctx->lex, ISC_LEXCOMMENT_DNSMASTERFILE);
900526
 	}
900526
 
900526
-	lctx->ttl_known = ISC_FALSE;
900526
+	lctx->ttl_known = ISC_TF((options & DNS_MASTER_NOTTL) != 0);
900526
 	lctx->ttl = 0;
900526
-	lctx->default_ttl_known = ISC_FALSE;
900526
+	lctx->default_ttl_known = lctx->ttl_known;
900526
 	lctx->default_ttl = 0;
900526
 	lctx->warn_1035 = ISC_TRUE;	/* XXX Argument? */
900526
 	lctx->warn_tcr = ISC_TRUE;	/* XXX Argument? */
900526
diff --git a/lib/dns/openssldsa_link.c b/lib/dns/openssldsa_link.c
900526
index 8bea1c0..a24baae 100644
900526
--- a/lib/dns/openssldsa_link.c
900526
+++ b/lib/dns/openssldsa_link.c
900526
@@ -522,6 +522,11 @@ openssldsa_tofile(const dst_key_t *key, const char *directory) {
900526
 
900526
 	if (key->keydata.dsa == NULL)
900526
 		return (DST_R_NULLKEY);
900526
+	
900526
+	if (key->external) {
900526
+		priv.nelements = 0;
900526
+		return (dst__privstruct_writefile(key, &priv, directory));
900526
+	}
900526
 
900526
 	dsa = key->keydata.dsa;
900526
 
900526
@@ -569,6 +574,7 @@ openssldsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
900526
 #define DST_RET(a) {ret = a; goto err;}
900526
 
900526
 	UNUSED(pub);
900526
+
900526
 	/* read private key file */
900526
 	ret = dst__privstruct_parse(key, DST_ALG_DSA, lexer, mctx, &priv;;
900526
 	if (ret != ISC_R_SUCCESS)
900526
@@ -607,6 +613,19 @@ openssldsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
900526
 	}
900526
 	dst__privstruct_free(&priv, mctx);
900526
 
900526
+	if (key->external) {
900526
+		if (pub == NULL)
900526
+			DST_RET(DST_R_INVALIDPRIVATEKEY);
900526
+		dsa->q = pub->keydata.dsa->q;
900526
+		pub->keydata.dsa->q = NULL;
900526
+		dsa->p = pub->keydata.dsa->p;
900526
+		pub->keydata.dsa->p = NULL;
900526
+		dsa->g = pub->keydata.dsa->g;
900526
+		pub->keydata.dsa->g =  NULL;
900526
+		dsa->pub_key = pub->keydata.dsa->pub_key;
900526
+		pub->keydata.dsa->pub_key = NULL;
900526
+	}
900526
+
900526
 	key->key_size = BN_num_bits(dsa->p);
900526
 
900526
 	return (ISC_R_SUCCESS);
900526
diff --git a/lib/dns/opensslecdsa_link.c b/lib/dns/opensslecdsa_link.c
900526
index c3f5061..7eff9a0 100644
900526
--- a/lib/dns/opensslecdsa_link.c
900526
+++ b/lib/dns/opensslecdsa_link.c
900526
@@ -453,6 +453,11 @@ opensslecdsa_tofile(const dst_key_t *key, const char *directory) {
900526
 	if (key->keydata.pkey == NULL)
900526
 		return (DST_R_NULLKEY);
900526
 
900526
+	if (key->external) {
900526
+		priv.nelements = 0;
900526
+		return (dst__privstruct_writefile(key, &priv, directory));
900526
+	}
900526
+
900526
 	pkey = key->keydata.pkey;
900526
 	eckey = EVP_PKEY_get1_EC_KEY(pkey);
900526
 	if (eckey == NULL)
900526
@@ -514,8 +519,9 @@ static isc_result_t
900526
 opensslecdsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
900526
 	dst_private_t priv;
900526
 	isc_result_t ret;
900526
-	EVP_PKEY *pkey;
900526
-	EC_KEY *eckey = NULL;
900526
+	EVP_PKEY *pkey, *pubpkey;
900526
+	EC_KEY *eckey = NULL, *pubeckey = NULL;
900526
+	const EC_POINT *pubkey;
900526
 	BIGNUM *privkey;
900526
 	int group_nid;
900526
 	isc_mem_t *mctx = key->mctx;
900526
@@ -537,17 +543,36 @@ opensslecdsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
900526
 	if (ret != ISC_R_SUCCESS)
900526
 		goto err;
900526
 
900526
-	privkey = BN_bin2bn(priv.elements[0].data,
900526
-			    priv.elements[0].length, NULL);
900526
-	if (privkey == NULL)
900526
-		DST_RET(ISC_R_NOMEMORY);
900526
-	if (!EC_KEY_set_private_key(eckey, privkey))
900526
-		DST_RET(ISC_R_NOMEMORY);
900526
-	if (ecdsa_check(eckey, pub) != ISC_R_SUCCESS)
900526
-		DST_RET(DST_R_INVALIDPRIVATEKEY);
900526
-	dst__privstruct_free(&priv, mctx);
900526
-	memset(&priv, 0, sizeof(priv));
900526
-
900526
+	if (key->external) {
900526
+		/*
900526
+		 * Copy the public key to this new key.
900526
+		 */
900526
+		if (pub == NULL)
900526
+			DST_RET(DST_R_INVALIDPRIVATEKEY);
900526
+		pubpkey = pub->keydata.pkey;
900526
+		pubeckey = EVP_PKEY_get1_EC_KEY(pubpkey);
900526
+		if (pubeckey == NULL)
900526
+			DST_RET(DST_R_INVALIDPRIVATEKEY);
900526
+		pubkey = EC_KEY_get0_public_key(pubeckey);
900526
+		if (pubkey == NULL)
900526
+			DST_RET(DST_R_INVALIDPRIVATEKEY);
900526
+		if (EC_KEY_set_public_key(eckey, pubkey) != 1)
900526
+			DST_RET(DST_R_INVALIDPRIVATEKEY);
900526
+		if (EC_KEY_check_key(eckey) != 1)
900526
+			DST_RET(DST_R_INVALIDPRIVATEKEY);
900526
+	} else {
900526
+		privkey = BN_bin2bn(priv.elements[0].data,
900526
+				    priv.elements[0].length, NULL);
900526
+		if (privkey == NULL)
900526
+			DST_RET(ISC_R_NOMEMORY);
900526
+		if (!EC_KEY_set_private_key(eckey, privkey))
900526
+			DST_RET(ISC_R_NOMEMORY);
900526
+		if (ecdsa_check(eckey, pub) != ISC_R_SUCCESS)
900526
+			DST_RET(DST_R_INVALIDPRIVATEKEY);
900526
+		dst__privstruct_free(&priv, mctx);
900526
+		memset(&priv, 0, sizeof(priv));
900526
+	}
900526
+ 
900526
 	pkey = EVP_PKEY_new();
900526
 	if (pkey == NULL)
900526
 		DST_RET (ISC_R_NOMEMORY);
900526
@@ -561,6 +586,8 @@ opensslecdsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
900526
  err:
900526
 	if (eckey != NULL)
900526
 		EC_KEY_free(eckey);
900526
+	if (pubeckey != NULL)
900526
+		EC_KEY_free(pubeckey);
900526
 	dst__privstruct_free(&priv, mctx);
900526
 	memset(&priv, 0, sizeof(priv));
900526
 	return (ret);
900526
diff --git a/lib/dns/opensslgost_link.c b/lib/dns/opensslgost_link.c
900526
index 1ce4405..325a7c0 100644
900526
--- a/lib/dns/opensslgost_link.c
900526
+++ b/lib/dns/opensslgost_link.c
900526
@@ -296,6 +296,11 @@ opensslgost_tofile(const dst_key_t *key, const char *directory) {
900526
 	if (key->keydata.pkey == NULL)
900526
 		return (DST_R_NULLKEY);
900526
 
900526
+	if (key->external) {
900526
+		priv.nelements = 0;
900526
+		return (dst__privstruct_writefile(key, &priv, directory));
900526
+	}
900526
+
900526
 	pkey = key->keydata.pkey;
900526
 
900526
 	len = i2d_PrivateKey(pkey, NULL);
900526
@@ -337,13 +342,21 @@ opensslgost_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
900526
 	if (ret != ISC_R_SUCCESS)
900526
 		return (ret);
900526
 
900526
-	INSIST(priv.elements[0].tag == TAG_GOST_PRIVASN1);
900526
-	p = priv.elements[0].data;
900526
-	if (d2i_PrivateKey(NID_id_GostR3410_2001, &pkey, &p,
900526
-			   (long) priv.elements[0].length) == NULL)
900526
-		DST_RET(dst__openssl_toresult2("d2i_PrivateKey",
900526
-					       DST_R_INVALIDPRIVATEKEY));
900526
-	key->keydata.pkey = pkey;
900526
+	if (key->external) {
900526
+		INSIST(priv.nelements == 0);
900526
+		if (pub == NULL)
900526
+			DST_RET(DST_R_INVALIDPRIVATEKEY);
900526
+		key->keydata.pkey = pub->keydata.pkey;
900526
+		pub->keydata.pkey = NULL;
900526
+	} else {
900526
+		INSIST(priv.elements[0].tag == TAG_GOST_PRIVASN1);
900526
+		p = priv.elements[0].data;
900526
+		if (d2i_PrivateKey(NID_id_GostR3410_2001, &pkey, &p,
900526
+				   (long) priv.elements[0].length) == NULL)
900526
+			DST_RET(dst__openssl_toresult2("d2i_PrivateKey",
900526
+						     DST_R_INVALIDPRIVATEKEY));
900526
+		key->keydata.pkey = pkey;
900526
+	}
900526
 	key->key_size = EVP_PKEY_bits(pkey);
900526
 	dst__privstruct_free(&priv, mctx);
900526
 	memset(&priv, 0, sizeof(priv));
900526
diff --git a/lib/dns/opensslrsa_link.c b/lib/dns/opensslrsa_link.c
900526
index fa7412c..894c7ae 100644
900526
--- a/lib/dns/opensslrsa_link.c
900526
+++ b/lib/dns/opensslrsa_link.c
900526
@@ -1048,8 +1048,14 @@ opensslrsa_tofile(const dst_key_t *key, const char *directory) {
900526
 		return (DST_R_NULLKEY);
900526
 	rsa = key->keydata.rsa;
900526
 #endif
900526
-
900526
 	memset(bufs, 0, sizeof(bufs));
900526
+
900526
+	if (key->external) {
900526
+		priv.nelements = 0;
900526
+		result = dst__privstruct_writefile(key, &priv, directory);
900526
+		goto fail;
900526
+	}
900526
+
900526
 	for (i = 0; i < 8; i++) {
900526
 		bufs[i] = isc_mem_get(key->mctx, BN_num_bytes(rsa->n));
900526
 		if (bufs[i] == NULL) {
900526
@@ -1205,6 +1211,9 @@ opensslrsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
900526
 	if (ret != ISC_R_SUCCESS)
900526
 		goto err;
900526
 
900526
+	if (key->external && priv.nelements != 0)
900526
+		DST_RET(DST_R_INVALIDPRIVATEKEY);
900526
+
900526
 	for (i = 0; i < priv.nelements; i++) {
900526
 		switch (priv.elements[i].tag) {
900526
 		case TAG_RSA_ENGINE:
900526
@@ -1217,6 +1226,7 @@ opensslrsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
900526
 			break;
900526
 		}
900526
 	}
900526
+
900526
 	/*
900526
 	 * Is this key is stored in a HSM?
900526
 	 * See if we can fetch it.
900526
@@ -1328,8 +1338,10 @@ opensslrsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
900526
 
900526
 	if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
900526
 		DST_RET(DST_R_INVALIDPRIVATEKEY);
900526
-	if (BN_num_bits(rsa->e) > RSA_MAX_PUBEXP_BITS)
900526
-		DST_RET(ISC_R_RANGE);
900526
+	if (!key->external) {
900526
+		if (BN_num_bits(rsa->e) > RSA_MAX_PUBEXP_BITS)
900526
+			DST_RET(ISC_R_RANGE);
900526
+	}
900526
 	key->key_size = BN_num_bits(rsa->n);
900526
 	if (pubrsa != NULL)
900526
 		RSA_free(pubrsa);
900526
diff --git a/lib/dns/zone.c b/lib/dns/zone.c
900526
index daf495b..b82ad58 100644
900526
--- a/lib/dns/zone.c
900526
+++ b/lib/dns/zone.c
900526
@@ -5545,6 +5545,7 @@ find_zone_keys(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver,
900526
 	const char *directory = dns_zone_getkeydirectory(zone);
900526
 
900526
 	CHECK(dns_db_findnode(db, dns_db_origin(db), ISC_FALSE, &node));
900526
+	memset(keys, 0, sizeof(*keys) * maxkeys);
900526
 	result = dns_dnssec_findzonekeys2(db, ver, node, dns_db_origin(db),
900526
 					  directory, mctx, maxkeys, keys,
900526
 					  nkeys);
900526
@@ -13132,6 +13133,7 @@ sync_secure_db(dns_zone_t *seczone, dns_db_t *secdb,
900526
 
900526
 static void
900526
 receive_secure_serial(isc_task_t *task, isc_event_t *event) {
900526
+	static char me[] = "receive_secure_serial";
900526
 	isc_result_t result;
900526
 	dns_journal_t *rjournal = NULL;
900526
 	isc_uint32_t start, end;
900526
@@ -13147,6 +13149,8 @@ receive_secure_serial(isc_task_t *task, isc_event_t *event) {
900526
 	end = ((struct secure_event *)event)->serial;
900526
 	isc_event_free(&event);
900526
 
900526
+	ENTER;
900526
+
900526
 	LOCK_ZONE(zone);
900526
 
900526
 	dns_diff_init(zone->mctx, &diff);
900526
-- 
900526
1.9.3
900526