functions.c 16.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*  Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

17
#include "utils/keymgr/functions.h"
18 19 20 21 22 23

#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <strings.h>
#include <time.h>
24
#include <fcntl.h>
25

Libor Peltan's avatar
Libor Peltan committed
26
#include "contrib/wire_ctx.h"
27
#include "dnssec/lib/dnssec/error.h"
28
#include "dnssec/shared/shared.h"
29
#include "knot/dnssec/kasp/policy.h"
30
#include "knot/dnssec/zone-keys.h"
31
#include "utils/keymgr/bind_privkey.h"
32
#include "zscanner/scanner.h"
33
#include "contrib/base64.h"
34

Libor Peltan's avatar
Libor Peltan committed
35 36
static bool genkeyargs(int argc, char *argv[], bool just_timing,
		       bool *isksk, dnssec_key_algorithm_t *algorithm,
37
		       uint16_t *keysize, knot_kasp_key_timing_t *timing)
38 39 40 41 42 43 44 45 46
{
	// generate algorithms field
	char *algnames[256] = { 0 };
	algnames[DNSSEC_KEY_ALGORITHM_RSA_SHA1] = "rsasha1";
	algnames[DNSSEC_KEY_ALGORITHM_RSA_SHA1_NSEC3] = "rsasha1nsec3sha1";
	algnames[DNSSEC_KEY_ALGORITHM_RSA_SHA256] = "rsasha256";
	algnames[DNSSEC_KEY_ALGORITHM_RSA_SHA512] = "rsasha512";
	algnames[DNSSEC_KEY_ALGORITHM_ECDSA_P256_SHA256] = "ecdsap256sha256";
	algnames[DNSSEC_KEY_ALGORITHM_ECDSA_P384_SHA384] = "ecdsap384sha384";
47
	algnames[DNSSEC_KEY_ALGORITHM_ED25519] = "ed25519";
48 49 50

	// parse args
	for (int i = 0; i < argc; i++) {
Libor Peltan's avatar
Libor Peltan committed
51
		if (!just_timing && strncasecmp(argv[i], "algorithm=", 10) == 0) {
52
			if (isdigit((int)argv[i][10]) && atol(argv[i] + 10) < 256) {
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
				*algorithm = atol(argv[i] + 10);
				continue;
			}
			int al;
			for (al = 0; al < 256; al++) {
				if (algnames[al] != NULL &&
				    strcasecmp(argv[i] + 10, algnames[al]) == 0) {
					*algorithm = al;
					break;
				}
			}
			if (al == 256) {
				printf("Unknown algorithm: %s\n", argv[i] + 10);
				return false;
			}
Libor Peltan's avatar
Libor Peltan committed
68
		} else if (!just_timing && strncasecmp(argv[i], "ksk=", 4) == 0) {
69
			switch (tolower((int)argv[i][4])) {
70 71 72 73 74 75 76 77
			case '1':
			case 'y':
			case 't':
				*isksk = true;
				break;
			default:
				*isksk = false;
			}
Libor Peltan's avatar
Libor Peltan committed
78
		} else if (!just_timing && strncasecmp(argv[i], "size=", 5) == 0) {
79 80 81
			*keysize = atol(argv[i] + 5);
		} else if (strncasecmp(argv[i], "created=", 8) == 0 ||
			   strncasecmp(argv[i], "publish=", 8) == 0 ||
82
			   strncasecmp(argv[i], "ready=", 6) == 0 ||
83 84 85
			   strncasecmp(argv[i], "active=", 7) == 0 ||
			   strncasecmp(argv[i], "retire=", 7) == 0 ||
			   strncasecmp(argv[i], "remove=", 7) == 0) {
86 87 88 89
			knot_time_t stamp;
			int ret = knot_time_parse("YMDhms|'now'+-#u|'t'+-#u|+-#u|#",
			                          strchr(argv[i], '=') + 1, &stamp);
			if (ret < 0) {
90 91 92
				printf("Invalid timestamp: %s\n", argv[i]);
				return false;
			}
93
			switch ((argv[i][0] == 'r') ? argv[i][3] : argv[i][0]) {
94
			case 'c':
95
				timing->created = stamp;
96 97
				break;
			case 'a':
98
				timing->active = stamp;
99
				break;
100
			case 'd':
101
				timing->ready = stamp;
102
				break;
103
			case 'p':
104
				timing->publish = stamp;
105
				break;
106
			case 'i':
107
				timing->retire = stamp;
108
				break;
109
			case 'o':
110
				timing->remove = stamp;
111 112 113 114 115 116 117 118 119 120 121
				break;
			}
		} else {
			printf("Invalid parameter: %s\n", argv[i]);
			return false;
		}
	}
	return true;
}

// modifies ctx->policy options, so don't do anything afterwards !
122
int keymgr_generate_key(kdnssec_ctx_t *ctx, int argc, char *argv[]) {
123
	knot_time_t now = knot_time(), infty = 0;
124
	knot_kasp_key_timing_t gen_timing = { now, now, now, now, infty, infty };
125 126
	bool isksk = false;
	uint16_t keysize = 0;
Libor Peltan's avatar
Libor Peltan committed
127
	if (!genkeyargs(argc, argv, false, &isksk, &ctx->policy->algorithm,
128 129 130 131 132 133 134 135 136 137 138 139
			&keysize, &gen_timing)) {
		return KNOT_EINVAL;
	}
	if (keysize > 0) {
		if (isksk) {
			ctx->policy->ksk_size = keysize;
		} else {
			ctx->policy->zsk_size = keysize;
		}
	}
	printf("alg %d\n", (int)ctx->policy->algorithm);

140
	knot_kasp_key_t *key = NULL;
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
	int ret = kdnssec_generate_key(ctx, isksk, &key);
	if (ret != KNOT_EOK) {
		return ret;
	}

	key->timing = gen_timing;

	ret = kdnssec_ctx_commit(ctx);

	if (ret == KNOT_EOK) {
		printf("%s\n", key->id);
	}

	return ret;
}

static void parse_record(zs_scanner_t *scanner)
{
	dnssec_key_t *key = scanner->process.data;

	if (dnssec_key_get_dname(key) != NULL ||
	    scanner->r_type != KNOT_RRTYPE_DNSKEY) {
		scanner->state = ZS_STATE_STOP;
		return;
	}

	dnssec_binary_t rdata = {
		.data = scanner->r_data,
		.size = scanner->r_data_length
	};
	dnssec_key_set_dname(key, scanner->dname);
	dnssec_key_set_rdata(key, &rdata);
}

int bind_pubkey_parse(const char *filename, dnssec_key_t **key_ptr)
{
	dnssec_key_t *key = NULL;
	int result = dnssec_key_new(&key);
	if (result != DNSSEC_EOK) {
		return KNOT_ENOMEM;
	}

	uint16_t cls = KNOT_CLASS_IN;
	uint32_t ttl = 0;
	zs_scanner_t *scanner = malloc(sizeof(zs_scanner_t));
	if (scanner == NULL) {
		dnssec_key_free(key);
		return KNOT_ENOMEM;
	}

	if (zs_init(scanner, ".", cls, ttl) != 0 ||
	    zs_set_input_file(scanner, filename) != 0 ||
	    zs_set_processing(scanner, parse_record, NULL, key) != 0 ||
	    zs_parse_all(scanner) != 0) {
		zs_deinit(scanner);
		free(scanner);
		dnssec_key_free(key);
		return KNOT_ENOENT;
	}
	zs_deinit(scanner);
	free(scanner);

	if (dnssec_key_get_dname(key) == NULL) {
		dnssec_key_free(key);
		return KNOT_INVALID_PUBLIC_KEY;
	}

	*key_ptr = key;
	return KNOT_EOK;
}

static char *genname(const char *orig, const char *wantsuff, const char *altsuff)
{
	char *res;
	if (orig == NULL || wantsuff == NULL || altsuff == NULL ||
	    (res = malloc(strlen(orig) + strlen(wantsuff) + 1)) == NULL) {
		return NULL;
	}
	strcpy(res, orig);
	char *dot = strrchr(res, '.');
	if (dot != NULL && strcmp(dot, wantsuff) == 0) {
		;
	} else if (dot != NULL && strcmp(dot, altsuff) == 0) {
		strcpy(dot, wantsuff);
	} else {
		strcat(res, wantsuff);
	}
	return res;
}

231
int keymgr_import_bind(kdnssec_ctx_t *ctx, const char *import_file)
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
{
	char *pubname = genname(import_file, ".key", ".private");
	char *privname = genname(import_file, ".private", ".key");
	if (ctx == NULL || import_file == NULL || pubname == NULL || privname == NULL) {
		free(pubname);
		free(privname);
		return KNOT_EINVAL;
	}

	char *keyid = NULL;
	dnssec_key_t *key = NULL;
	int ret = KNOT_EOK;
	printf("kkib %s %s\n", pubname, privname);

	ret = bind_pubkey_parse(pubname, &key);
	if (ret != KNOT_EOK) {
		goto fail;
	}

	bind_privkey_t bpriv = { 0 };
	ret = bind_privkey_parse(privname, &bpriv);
	if (ret != DNSSEC_EOK) {
		goto fail;
	}

	dnssec_binary_t pem = { 0 };
	ret = bind_privkey_to_pem(key, &bpriv, &pem);
	if (ret != DNSSEC_EOK) {
		bind_privkey_free(&bpriv);
		goto fail;
	}

264
	knot_kasp_key_timing_t timing = { 0 };
265 266 267 268 269
	bind_privkey_to_timing(&bpriv, &timing); // time created remains always zero

	bind_privkey_free(&bpriv);

	ret = dnssec_keystore_import(ctx->keystore, &pem, &keyid);
270
	dnssec_binary_free(&pem);
271 272 273 274
	if (ret != DNSSEC_EOK) {
		goto fail;
	}

275
	knot_kasp_key_t *kkey = calloc(1, sizeof(*kkey));
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	if (!kkey) {
		ret = KNOT_ENOMEM;
		goto fail;
	}

	kkey->id = keyid;
	kkey->key = key;
	kkey->timing = timing;

	ret = kasp_zone_append(ctx->zone, kkey);
	free(kkey);
	if (ret != KNOT_EOK) {
		goto fail;
	}

	ret = kdnssec_ctx_commit(ctx);
	// ret fallthrough

	if (ret == KNOT_EOK) {
		printf("%s\n", keyid);
	}

	goto cleanup;

fail:
	dnssec_key_free(key);
	free(keyid);
cleanup:
	free(pubname);
	free(privname);
	return knot_error_from_libdnssec(ret);
}

309 310 311
int keymgr_import_pem(kdnssec_ctx_t *ctx, const char *import_file, int argc, char *argv[])
{
	// parse params
312
	knot_time_t now = knot_time(), infty = 0;
313 314 315 316 317 318 319 320 321 322 323
	knot_kasp_key_timing_t gen_timing = { now, now, now, now, infty, infty };
	bool isksk = false;
	uint16_t keysize = 0;
	if (!genkeyargs(argc, argv, false, &isksk, &ctx->policy->algorithm,
			&keysize, &gen_timing)) {
		return KNOT_EINVAL;
	}

	// open file
	int fd = open(import_file, O_RDONLY, 0);
	if (fd == -1) {
324
		return knot_map_errno();
325 326 327 328 329 330
	}

	// determine size
	off_t fsize = lseek(fd, 0, SEEK_END);
	if (fsize == -1) {
		close(fd);
331
		return knot_map_errno();
332 333 334
	}
	if (lseek(fd, 0, SEEK_SET) == -1) {
		close(fd);
335
		return knot_map_errno();
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
	}

	// alloc memory
	dnssec_binary_t read_pem = { 0 };
	int ret = dnssec_binary_alloc(&read_pem, fsize);
	if (ret != DNSSEC_EOK) {
		close(fd);
		return knot_error_from_libdnssec(ret);
	}

	// read pem
	ssize_t read_count = read(fd, read_pem.data, read_pem.size);
	close(fd);
	if (read_count == -1) {
		dnssec_binary_free(&read_pem);
351
		return knot_map_errno();
352 353 354 355 356 357 358
	}

	// put pem to kesytore
	char *keyid = NULL;
	ret = dnssec_keystore_import(ctx->keystore, &read_pem, &keyid);
	dnssec_binary_free(&read_pem);
	if (ret != DNSSEC_EOK) {
359
		return knot_error_from_libdnssec(ret);
360 361 362 363 364
	}

	// create dnssec key
	dnssec_key_t *dnskey = NULL;
	ret = dnssec_key_new(&dnskey);
365
	if (ret != DNSSEC_EOK) {
366
		free(keyid);
367
		return knot_error_from_libdnssec(ret);
368 369
	}
	ret = dnssec_key_set_dname(dnskey, ctx->zone->dname);
370
	if (ret != DNSSEC_EOK) {
371 372
		dnssec_key_free(dnskey);
		free(keyid);
373
		return knot_error_from_libdnssec(ret);
374 375 376 377 378 379
	}
	dnssec_key_set_flags(dnskey, dnskey_flags(isksk));
	dnssec_key_set_algorithm(dnskey, ctx->policy->algorithm);

	// fill key structure from keystore (incl. pubkey from privkey computation)
	ret = dnssec_key_import_keystore(dnskey, ctx->keystore, keyid);
380
	if (ret != DNSSEC_EOK) {
381 382
		dnssec_key_free(dnskey);
		free(keyid);
383
		return knot_error_from_libdnssec(ret);
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
	}

	// allocate kasp key
	knot_kasp_key_t *kkey = calloc(1, sizeof(*kkey));
	if (!kkey) {
		dnssec_key_free(dnskey);
		free(keyid);
		return KNOT_ENOMEM;
	}
	kkey->id = keyid;
	kkey->key = dnskey;
	kkey->timing = gen_timing;

	// append to zone
	ret = kasp_zone_append(ctx->zone, kkey);
	free(kkey);
	if (ret != KNOT_EOK) {
		dnssec_key_free(dnskey);
		free(keyid);
		return ret;
	}
	ret = kdnssec_ctx_commit(ctx);

	return ret;
}

410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
static void print_tsig(dnssec_tsig_algorithm_t mac, const char *name,
		       const dnssec_binary_t *secret)
{
	assert(name);
	assert(secret);

	const char *mac_name = dnssec_tsig_algorithm_to_name(mac);
	assert(mac_name);

	// client format (as a comment)
	printf("# %s:%s:%.*s\n", mac_name, name, (int)secret->size, secret->data);

	// server format
	printf("key:\n");
	printf("  - id: %s\n", name);
	printf("    algorithm: %s\n", mac_name);
	printf("    secret: %.*s\n", (int)secret->size, secret->data);
}

429
int keymgr_generate_tsig(const char *tsig_name, const char *alg_name, int bits)
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
{
	dnssec_tsig_algorithm_t alg = dnssec_tsig_algorithm_from_name(alg_name);
	if (alg == DNSSEC_TSIG_UNKNOWN) {
		return KNOT_INVALID_KEY_ALGORITHM;
	}

	int optimal_bits = dnssec_tsig_optimal_key_size(alg);
	if (bits == 0) {
		bits = optimal_bits; // TODO review
	}

	// round up bits to bytes
	bits = (bits + CHAR_BIT - 1) / CHAR_BIT * CHAR_BIT;

	if (bits != optimal_bits) {
		printf("Notice: Optimal key size for %s is %d bits.",
		       dnssec_tsig_algorithm_to_name(alg), optimal_bits);
	}
	assert(bits % CHAR_BIT == 0);

	_cleanup_binary_ dnssec_binary_t key = { 0 };
	int r = dnssec_binary_alloc(&key, bits / CHAR_BIT);
	if (r != DNSSEC_EOK) {
		printf("Failed to allocate memory.");
		return knot_error_from_libdnssec(r);
	}

	r = gnutls_rnd(GNUTLS_RND_KEY, key.data, key.size);
	if (r != 0) {
		printf("Failed to generate secret the key.");
		return knot_error_from_libdnssec(r);
	}

	_cleanup_binary_ dnssec_binary_t key_b64 = { 0 };
	r = dnssec_binary_to_base64(&key, &key_b64);
	if (r != DNSSEC_EOK) {
		printf("Failed to convert the key to Base64.");
		return knot_error_from_libdnssec(r);
	}

	print_tsig(alg, tsig_name, &key_b64);

	return KNOT_EOK;
}
Libor Peltan's avatar
Libor Peltan committed
474 475 476 477 478 479 480

static long is_uint32(const char *string)
{
	if (*string == '\0') {
		return -1;
	}
	for (const char *p = string; *p != '\0'; p++) {
481
		if (!isdigit((int)*p)) {
Libor Peltan's avatar
Libor Peltan committed
482 483 484 485 486 487 488 489 490 491
			return -1;
		}
	}
	long res = atol(string);
	return (res <= UINT32_MAX ? res : -1);
}

static bool is_hex(const char *string)
{
	for (const char *p = string; *p != '\0'; p++) {
492
		if (!isxdigit((int)*p)) {
Libor Peltan's avatar
Libor Peltan committed
493 494 495 496 497 498
			return false;
		}
	}
	return (*string != '\0');
}

499
int keymgr_get_key(kdnssec_ctx_t *ctx, const char *key_spec, knot_kasp_key_t **key)
Libor Peltan's avatar
Libor Peltan committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
{
	long spec_tag = is_uint32(key_spec), spec_len = strlen(key_spec);
	if (spec_tag < 0 && !is_hex(key_spec)) {
		printf("Error in key specification.\n");
		return KNOT_EINVAL;
	}

	*key = NULL;
	for (size_t i = 0; i < ctx->zone->num_keys; i++) {
		knot_kasp_key_t *candidate = &ctx->zone->keys[i];
		if ((spec_tag >= 0 && dnssec_key_get_keytag(candidate->key) == spec_tag) ||
		    (spec_tag < 0 && strncmp(candidate->id, key_spec, spec_len) == 0)) {
			if (*key == NULL) {
				*key = candidate;
			}
			else {
				printf("Key is not specified uniquely.\n");
				return KNOT_ELIMIT;
			}
		}
	}
	if (*key == NULL) {
		printf("Key not found.\n");
		return KNOT_ENOENT;
	}
	return KNOT_EOK;
}

528
int keymgr_foreign_key_id(char *argv[], knot_dname_t **key_zone, char **key_id)
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
{
	*key_zone = knot_dname_from_str_alloc(argv[0]);
	if (*key_zone == NULL) {
		return KNOT_ENOMEM;
	}
	(void)knot_dname_to_lower(*key_zone);

	kdnssec_ctx_t kctx = { 0 };
	int ret = kdnssec_ctx_init(conf(), &kctx, *key_zone, NULL);
	if (ret != KNOT_EOK) {
		printf("Failed to initialize zone %s (%s)\n", argv[0], knot_strerror(ret));
		free(*key_zone);
		*key_zone = NULL;
		return KNOT_ENOZONE;
	}
	knot_kasp_key_t *key;
545
	ret = keymgr_get_key(&kctx, argv[2], &key);
546 547 548 549 550 551 552 553 554 555
	if (ret == KNOT_EOK) {
		*key_id = strdup(key->id);
		if (*key_id == NULL) {
			ret = KNOT_ENOMEM;
		}
	}
	kdnssec_ctx_deinit(&kctx);
	return ret;
}

556
int keymgr_set_timing(knot_kasp_key_t *key, int argc, char *argv[])
Libor Peltan's avatar
Libor Peltan committed
557 558 559 560 561 562 563 564 565 566
{
	knot_kasp_key_timing_t temp = key->timing;

	if (genkeyargs(argc, argv, true, NULL, NULL, NULL, &temp)) {
		key->timing = temp;
		return KNOT_EOK;
	}
	return KNOT_EINVAL;
}

567
int keymgr_list_keys(kdnssec_ctx_t *ctx)
Libor Peltan's avatar
Libor Peltan committed
568 569 570
{
	for (size_t i = 0; i < ctx->zone->num_keys; i++) {
		knot_kasp_key_t *key = &ctx->zone->keys[i];
571
		printf("%s ksk=%s tag=%05d algorithm=%d created=%lld publish=%lld ready=%lld"
Libor Peltan's avatar
Libor Peltan committed
572 573
		       " active=%lld retire=%lld remove=%lld\n", key->id,
		       ((dnssec_key_get_flags(key->key) == dnskey_flags(true)) ? "yes" : "no "),
574 575 576
		       dnssec_key_get_keytag(key->key), (int)dnssec_key_get_algorithm(key->key),
		       (long long)key->timing.created, (long long)key->timing.publish,
		       (long long)key->timing.ready, (long long)key->timing.active,
Libor Peltan's avatar
Libor Peltan committed
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
		       (long long)key->timing.retire, (long long)key->timing.remove);
	}
	return KNOT_EOK;
}

static int print_ds(const knot_dname_t *dname, const dnssec_binary_t *rdata)
{
	wire_ctx_t ctx = wire_ctx_init(rdata->data, rdata->size);
	if (wire_ctx_available(&ctx) < 4) {
		return KNOT_EMALF;
	}

	char *name = knot_dname_to_str_alloc(dname);
	if (!name) {
		return KNOT_ENOMEM;
	}

	uint16_t keytag   = wire_ctx_read_u16(&ctx);
	uint8_t algorithm = wire_ctx_read_u8(&ctx);
	uint8_t digest_type = wire_ctx_read_u8(&ctx);

	size_t digest_size = wire_ctx_available(&ctx);

	printf("%s DS %d %d %d ", name, keytag, algorithm, digest_type);
	for (size_t i = 0; i < digest_size; i++) {
		printf("%02x", ctx.position[i]);
	}
	printf("\n");

	free(name);
	return KNOT_EOK;
}

static int create_and_print_ds(const knot_dname_t *zone_name,
			       const dnssec_key_t *key, dnssec_key_digest_t digest)
{
	_cleanup_binary_ dnssec_binary_t rdata = { 0 };
	int r = dnssec_key_create_ds(key, digest, &rdata);
	if (r != DNSSEC_EOK) {
		return knot_error_from_libdnssec(r);
	}

	return print_ds(zone_name, &rdata);
}

622
int keymgr_generate_ds(const knot_dname_t *dname, const knot_kasp_key_t *key)
Libor Peltan's avatar
Libor Peltan committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
{
	static const dnssec_key_digest_t digests[] = {
		DNSSEC_KEY_DIGEST_SHA1,
		DNSSEC_KEY_DIGEST_SHA256,
		DNSSEC_KEY_DIGEST_SHA384,
		0
	};

	int ret = KNOT_EOK;
	for (int i = 0; digests[i] != 0 && ret == KNOT_EOK; i++) {
		ret = create_and_print_ds(dname, key->key, digests[i]);
	}

	return ret;
}
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653

int keymgr_generate_dnskey(const knot_dname_t *dname, const knot_kasp_key_t *key)
{
	const dnssec_key_t *dnskey = key->key;

	char *name = knot_dname_to_str_alloc(dname);
	if (!name) {
		return KNOT_ENOMEM;
	}

	uint16_t flags = dnssec_key_get_flags(dnskey);
	uint8_t algorithm = dnssec_key_get_algorithm(dnskey);

	dnssec_binary_t pubkey = { 0 };
	int ret = dnssec_key_get_pubkey(dnskey, &pubkey);
	if (ret != DNSSEC_EOK) {
654
		free(name);
655 656 657 658 659 660
		return knot_error_from_libdnssec(ret);
	}

	uint8_t *base64_output = NULL;
	int len = base64_encode_alloc(pubkey.data, pubkey.size, &base64_output);
	if (len < 0) {
661
		free(name);
662 663 664 665 666 667 668 669 670
		return len;
	}

	printf("%s DNSKEY %u 3 %u %.*s\n", name, flags, algorithm, len, base64_output);

	free(base64_output);
	free(name);
	return KNOT_EOK;
}