Skip to content
Snippets Groups Projects
Commit 7af5d6ec authored by Daniel Salzman's avatar Daniel Salzman
Browse files

Merge branch 'dnssec-ds' into 'master'

DS support in DNSSEC

See merge request !409
parents e98ad9b1 46681d33
No related branches found
No related tags found
No related merge requests found
......@@ -70,6 +70,7 @@ enum dnssec_error {
DNSSEC_INVALID_KEY_ALGORITHM,
DNSSEC_INVALID_KEY_SIZE,
DNSSEC_INVALID_KEY_ID,
DNSSEC_INVALID_KEY_NAME,
DNSSEC_NO_PUBLIC_KEY,
DNSSEC_NO_PRIVATE_KEY,
......@@ -78,9 +79,13 @@ enum dnssec_error {
DNSSEC_SIGN_INIT_ERROR,
DNSSEC_SIGN_ERROR,
DNSSEC_INVALID_SIGNATURE,
DNSSEC_INVALID_NSEC3_ALGORITHM,
DNSSEC_NSEC3_HASHING_ERROR,
DNSSEC_INVALID_DS_ALGORITHM,
DNSSEC_DS_HASHING_ERROR,
DNSSEC_CONFIG_MALFORMED,
DNSSEC_CONFIG_INVALID_KEY_ID,
......
......@@ -47,6 +47,7 @@ static const error_message_t ERROR_MESSAGES[] = {
{ DNSSEC_INVALID_KEY_ALGORITHM, "invalid key algorithm" },
{ DNSSEC_INVALID_KEY_SIZE, "invalid key size" },
{ DNSSEC_INVALID_KEY_ID, "invalid key ID" },
{ DNSSEC_INVALID_KEY_NAME, "invalid key name" },
{ DNSSEC_NO_PUBLIC_KEY, "no public key" },
{ DNSSEC_NO_PRIVATE_KEY, "no private key" },
......@@ -55,9 +56,13 @@ static const error_message_t ERROR_MESSAGES[] = {
{ DNSSEC_SIGN_INIT_ERROR, "signing initialization error" },
{ DNSSEC_SIGN_ERROR, "signing error" },
{ DNSSEC_INVALID_SIGNATURE, "invalid signature" },
{ DNSSEC_INVALID_NSEC3_ALGORITHM, "invalid NSEC3 algorithm" },
{ DNSSEC_NSEC3_HASHING_ERROR, "NSEC3 hashing error" },
{ DNSSEC_INVALID_DS_ALGORITHM, "invalid DS algorithm" },
{ DNSSEC_DS_HASHING_ERROR, "DS hashing error" },
{ DNSSEC_CONFIG_MALFORMED, "malformed config value" },
{ DNSSEC_CONFIG_INVALID_KEY_ID, "invalid key ID in config" },
......
......@@ -19,9 +19,92 @@
#include "key.h"
#include "shared.h"
#include "key/internal.h"
#include "dname.h"
#include "wire.h"
#include <gnutls/gnutls.h>
#include <gnutls/crypto.h>
/*!
* Convert DNSSEC DS digest algorithm to GnuTLS digest algorithm.
*/
static gnutls_digest_algorithm_t lookup_algorithm(dnssec_key_digest_t algorithm)
{
switch (algorithm) {
case DNSSEC_KEY_DIGEST_SHA1: return GNUTLS_DIG_SHA1;
case DNSSEC_KEY_DIGEST_SHA256: return GNUTLS_DIG_SHA256;
case DNSSEC_KEY_DIGEST_SHA384: return GNUTLS_DIG_SHA384;
default:
return GNUTLS_DIG_UNKNOWN;
};
}
static void wire_write_digest(wire_ctx_t *wire,
gnutls_hash_hd_t digest, int digest_size)
{
assert(wire_available(wire) >= digest_size);
gnutls_hash_output(digest, wire->position);
wire->position += digest_size;
}
_public_
int dnssec_key_create_ds(const dnssec_key_t *key, dnssec_key_digest_t digest,
dnssec_binary_t *rdata)
int dnssec_key_create_ds(const dnssec_key_t *key,
dnssec_key_digest_t ds_algorithm,
dnssec_binary_t *rdata_ptr)
{
return DNSSEC_NOT_IMPLEMENTED_ERROR;
if (!key || !rdata_ptr) {
return DNSSEC_EINVAL;
}
if (!key->dname) {
return DNSSEC_INVALID_KEY_NAME;
}
if (!key->public_key){
return DNSSEC_INVALID_PUBLIC_KEY;
}
gnutls_digest_algorithm_t algorithm = lookup_algorithm(ds_algorithm);
if (algorithm == GNUTLS_DIG_UNKNOWN) {
return DNSSEC_INVALID_DS_ALGORITHM;
}
// compute DS hash
_cleanup_hash_ gnutls_hash_hd_t digest = NULL;
int r = gnutls_hash_init(&digest, algorithm);
if (r < 0) {
return DNSSEC_DS_HASHING_ERROR;
}
if (gnutls_hash(digest, key->dname, dname_length(key->dname)) != 0 ||
gnutls_hash(digest, key->rdata.data, key->rdata.size) != 0
) {
return DNSSEC_DS_HASHING_ERROR;
}
// build DS RDATA
int digest_size = gnutls_hash_get_len(algorithm);
if (digest_size == 0) {
return DNSSEC_DS_HASHING_ERROR;
}
dnssec_binary_t rdata = { 0 };
r = dnssec_binary_alloc(&rdata, 4 + digest_size);
if (r != DNSSEC_EOK) {
return r;
}
wire_ctx_t wire = wire_init_binary(&rdata);
wire_write_u16(&wire, key->keytag);
wire_write_u8(&wire, dnssec_key_get_algorithm(key));
wire_write_u8(&wire, ds_algorithm);
wire_write_digest(&wire, digest, digest_size);
assert(wire_tell(&wire) == wire.size);
*rdata_ptr = rdata;
return DNSSEC_EOK;
}
......@@ -11,6 +11,7 @@
/kasp_store
/key
/key_algorithm
/key_ds
/keyid
/keystore_pkcs8
/keystore_pkcs8_dir
......
......@@ -28,6 +28,7 @@ check_PROGRAMS = \
kasp_store \
key \
key_algorithm \
key_ds \
keyid \
keystore_pkcs8 \
keystore_pkcs8_dir \
......
/* Copyright (C) 2015 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/>.
*/
#include <tap/basic.h>
#include <stddef.h>
#include <string.h>
#include "dnssec/crypto.h"
#include "dnssec/error.h"
#include "dnssec/key.h"
#include "sample_keys.h"
static void test_key(const char *name, const struct key_parameters *params)
{
dnssec_key_t *key = NULL;
dnssec_key_new(&key);
dnssec_key_set_dname(key, params->name);
dnssec_key_set_rdata(key, &params->rdata);
struct ds_type {
const char *name;
dnssec_key_digest_t digest;
size_t params_offset;
};
static const struct ds_type DS_TYPES[] = {
{ "SHA-1", DNSSEC_KEY_DIGEST_SHA1, offsetof(typeof(*params), ds_sha1) },
{ "SHA-256", DNSSEC_KEY_DIGEST_SHA256, offsetof(typeof(*params), ds_sha256) },
{ "SHA-384", DNSSEC_KEY_DIGEST_SHA384, offsetof(typeof(*params), ds_sha384) },
{ NULL }
};
for (const struct ds_type *dt = DS_TYPES; dt->name != NULL; dt++) {
dnssec_binary_t ds = { 0 };
int r = dnssec_key_create_ds(key, dt->digest, &ds);
const dnssec_binary_t *expect = (void *)params + dt->params_offset;
ok(r == DNSSEC_EOK &&
ds.size == expect->size &&
memcmp(ds.data, expect->data, ds.size) == 0,
"dnssec_key_create_ds() for %s/%s", name, dt->name);
dnssec_binary_free(&ds);
}
dnssec_key_free(key);
}
static void test_errors(const struct key_parameters *params)
{
dnssec_key_t *key = NULL;
dnssec_binary_t ds = { 0 };
int r = dnssec_key_create_ds(key, DNSSEC_KEY_DIGEST_SHA1, &ds);
is_int(DNSSEC_EINVAL, r, "dnssec_key_create_ds() no key");
dnssec_binary_free(&ds);
dnssec_key_new(&key);
r = dnssec_key_create_ds(key, DNSSEC_KEY_DIGEST_SHA1, &ds);
is_int(DNSSEC_INVALID_KEY_NAME, r, "dnssec_key_create_ds() no key name");
dnssec_key_set_dname(key, params->name);
r = dnssec_key_create_ds(key, DNSSEC_KEY_DIGEST_SHA1, &ds);
is_int(DNSSEC_INVALID_PUBLIC_KEY, r, "dnssec_key_create_ds() no public key");
dnssec_key_set_rdata(key, &params->rdata);
r = dnssec_key_create_ds(key, DNSSEC_KEY_DIGEST_SHA1, NULL);
is_int(DNSSEC_EINVAL, r, "dnssec_key_create_ds() no RDATA buffer");
r = dnssec_key_create_ds(key, 3, &ds);
is_int(DNSSEC_INVALID_DS_ALGORITHM, r, "dnssec_key_create_ds() unsupported algorithm");
r = dnssec_key_create_ds(key, DNSSEC_KEY_DIGEST_SHA1, &ds);
is_int(DNSSEC_EOK, r, "dnssec_key_create_ds() valid parameters");
dnssec_binary_free(&ds);
dnssec_key_free(key);
}
int main(int argc, char *argv[])
{
plan_lazy();
dnssec_crypto_init();
test_key("RSA", &SAMPLE_RSA_KEY);
test_key("DSA", &SAMPLE_DSA_KEY);
test_key("ECDSA", &SAMPLE_ECDSA_KEY);
test_errors(&SAMPLE_ECDSA_KEY);
dnssec_crypto_cleanup();
return 0;
}
......@@ -20,8 +20,6 @@
#include "error.h"
#include "key.h"
#include "sample_keys.h"
static void test_keyid_is_valid_run(const char *param, bool should_ok)
{
ok(dnssec_keyid_is_valid(param) == should_ok,
......
......@@ -20,6 +20,7 @@
typedef struct key_parameters {
// DNSSEC fields
uint8_t *name;
uint16_t flags;
uint8_t protocol;
uint8_t algorithm;
......@@ -31,6 +32,9 @@ typedef struct key_parameters {
// Hashes
const char *key_id;
uint16_t keytag;
dnssec_binary_t ds_sha1;
dnssec_binary_t ds_sha256;
dnssec_binary_t ds_sha384;
// Key information
unsigned bit_size;
......@@ -44,7 +48,9 @@ typedef struct key_parameters {
RSA-SHA-256
rsa. IN DNSKEY 256 3 8 AwEAAarbp0oh52KuF0SwXoSgMNRpcW/uPKCKQAu8NyYaY+e9G29rh7eqK1hqp7skbSvKKlItgAaFdDxZvPiD4AzBHQk= ;{id = 1506 (zsk), size = 512b}
rsa. IN DS 1506 8 1 172a500b374158d1a64ba3073cdbbc319b2fdf2c
rsa. IN DS 1506 8 2 253b099ff47b02c6ffa52695a30a94c6681c56befe0e71a5077d6f79514972f9
rsa. IN DS 1506 8 4 22ea940600dc2d9a98b1126c26ac0dc5c91b31eb50fe784b36ad675e9eecfe6573c1f85c53b6bc94580f3ac443d13c4c
Modulus: qtunSiHnYq4XRLBehKAw1Glxb+48oIpAC7w3Jhpj570bb2uHt6orWGqnuyRtK8oqUi2ABoV0PFm8+IPgDMEdCQ==
PublicExponent: AQAB
......@@ -69,6 +75,7 @@ Y5lIjDCa4+M=
*/
static const key_parameters_t SAMPLE_RSA_KEY = {
.name = (uint8_t *)"\x03""rsa",
.flags = 256,
.protocol = 3,
.algorithm = 8,
......@@ -93,6 +100,26 @@ static const key_parameters_t SAMPLE_RSA_KEY = {
}},
.key_id = "6d2b811564be0200132ef67d281de19e65ab3d1d",
.keytag = 1506,
.ds_sha1 = { .size = 24, .data = (uint8_t []) {
0x05, 0xe2, 0x08, 0x01,
0x17, 0x2a, 0x50, 0x0b, 0x37, 0x41, 0x58, 0xd1, 0xa6, 0x4b,
0xa3, 0x07, 0x3c, 0xdb, 0xbc, 0x31, 0x9b, 0x2f, 0xdf, 0x2c,
}},
.ds_sha256 = { .size = 36, .data = (uint8_t []) {
0x05, 0xe2, 0x08, 0x02,
0x25, 0x3b, 0x09, 0x9f, 0xf4, 0x7b, 0x02, 0xc6, 0xff, 0xa5,
0x26, 0x95, 0xa3, 0x0a, 0x94, 0xc6, 0x68, 0x1c, 0x56, 0xbe,
0xfe, 0x0e, 0x71, 0xa5, 0x07, 0x7d, 0x6f, 0x79, 0x51, 0x49,
0x72, 0xf9,
}},
.ds_sha384 = { .size = 52, .data = (uint8_t []) {
0x05, 0xe2, 0x08, 0x04,
0x22, 0xea, 0x94, 0x06, 0x00, 0xdc, 0x2d, 0x9a, 0x98, 0xb1,
0x12, 0x6c, 0x26, 0xac, 0x0d, 0xc5, 0xc9, 0x1b, 0x31, 0xeb,
0x50, 0xfe, 0x78, 0x4b, 0x36, 0xad, 0x67, 0x5e, 0x9e, 0xec,
0xfe, 0x65, 0x73, 0xc1, 0xf8, 0x5c, 0x53, 0xb6, 0xbc, 0x94,
0x58, 0x0f, 0x3a, 0xc4, 0x43, 0xd1, 0x3c, 0x4c,
}},
.bit_size = 512,
.pem = { .size = 522, .data = (uint8_t []) {
0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e,
......@@ -157,6 +184,8 @@ DSA-NSEC3
dsa. IN DNSKEY 257 3 6 ALRB7JW8C3f9YYqtgW088YMR28u/tUD9ON0VGCNjvi6OFbTvuIP+Jaen8uFCpXy7KPPUXZQiDZrCvX5Vt0nvdAuHBk2Lqi9pn5nq3aWpOeer+L9o83KQ+jmCk1v1m+ryiNOCR1g14hmD6zF3x3Vncg0hrYRplMBB8UlgQKuCoJ2ktA9pTQYai0/TvXKVlqpXi41enyKld/E1Y7yDNjxDgGPROctlj8cYbs+6yjT4BbiQWG8qPCRsbfgqiLzjplQbGGkrSD08ozntKLD7XDCrq9RJP8gm ;{id = 1203 (ksk), size = 512b}
dsa. IN DS 1203 6 1 27a6bdcd62d80264ad71126adf9f2200e3c87f2f
dsa. IN DS 1203 6 2 6eeb47942d85578e9c611936e6ac7a3b969f543fc2f7b69790c256766c5df3e5
dsa. IN DS 1203 6 4 ab11a09b0dcea468a7d899e8f0be8791aa0fd911e97ead24cd428277f1762e40a266735533043ef1a692261647ff0e06
Prime(p): tUD9ON0VGCNjvi6OFbTvuIP+Jaen8uFCpXy7KPPUXZQiDZrCvX5Vt0nvdAuHBk2Lqi9pn5nq3aWpOeer+L9o8w==
Subprime(q): tEHslbwLd/1hiq2BbTzxgxHby78=
......@@ -175,6 +204,7 @@ ZjTfYqKkNYYh
*/
static const key_parameters_t SAMPLE_DSA_KEY = {
.name = (uint8_t *)"\x03""dsa",
.flags = 257,
.protocol = 3,
.algorithm = 6,
......@@ -229,6 +259,26 @@ static const key_parameters_t SAMPLE_DSA_KEY = {
}},
.keytag = 1203,
.key_id = "141b2d54837494735b53795bf0c1579bd9e12754",
.ds_sha1 = { .size = 24, .data = (uint8_t []) {
0x04, 0xb3, 0x06, 0x01,
0x27, 0xa6, 0xbd, 0xcd, 0x62, 0xd8, 0x02, 0x64, 0xad, 0x71,
0x12, 0x6a, 0xdf, 0x9f, 0x22, 0x00, 0xe3, 0xc8, 0x7f, 0x2f,
}},
.ds_sha256 = { .size = 36, .data = (uint8_t []) {
0x04, 0xb3, 0x06, 0x02,
0x6e, 0xeb, 0x47, 0x94, 0x2d, 0x85, 0x57, 0x8e, 0x9c, 0x61,
0x19, 0x36, 0xe6, 0xac, 0x7a, 0x3b, 0x96, 0x9f, 0x54, 0x3f,
0xc2, 0xf7, 0xb6, 0x97, 0x90, 0xc2, 0x56, 0x76, 0x6c, 0x5d,
0xf3, 0xe5,
}},
.ds_sha384 = { .size = 52, .data = (uint8_t []) {
0x04, 0xb3, 0x06, 0x04,
0xab, 0x11, 0xa0, 0x9b, 0x0d, 0xce, 0xa4, 0x68, 0xa7, 0xd8,
0x99, 0xe8, 0xf0, 0xbe, 0x87, 0x91, 0xaa, 0x0f, 0xd9, 0x11,
0xe9, 0x7e, 0xad, 0x24, 0xcd, 0x42, 0x82, 0x77, 0xf1, 0x76,
0x2e, 0x40, 0xa2, 0x66, 0x73, 0x55, 0x33, 0x04, 0x3e, 0xf1,
0xa6, 0x92, 0x26, 0x16, 0x47, 0xff, 0x0e, 0x06,
}},
.bit_size = 512,
.pem = { .size = 327, .data = (uint8_t []) {
0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e,
......@@ -271,9 +321,10 @@ static const key_parameters_t SAMPLE_DSA_KEY = {
ECDSA-P256-SHA256
ecdsa. IN DNSKEY 256 3 13 8uD7C4THTM/w7uhryRSToeE/jKT78/p853RX0L5EwrZrSLBubLPiBw7g bvUP6SsIga5ZQ4CSAxNmYA/gZsuXzA==
ecdsa. IN DS 5345 13 1 954103AC7C43810CE9F414E80F30AB1CBE49B236
ecdsa. IN DS 5345 13 2 BAC2107036E735B50F85006CE409A19A3438CAB272E70769EBDA032239A3D0CA
ecdsa. IN DNSKEY 256 3 13 8uD7C4THTM/w7uhryRSToeE/jKT78/p853RX0L5EwrZrSLBubLPiBw7g bvUP6SsIga5ZQ4CSAxNmYA/gZsuXzA==
ecdsa. IN DS 5345 13 1 954103ac7c43810ce9f414e80f30ab1cbe49b236
ecdsa. IN DS 5345 13 2 bac2107036e735b50f85006ce409a19a3438cab272e70769ebda032239a3d0ca
ecdsa. IN DS 5345 13 4 a0ac6790483872be72a258314200a88ab75cdd70f66a18a09f0f414c074df0989fdb1df0e67d82d4312cda67b93a76c1
PrivateKey: iyLIPdk3DOIxVmmSYlmTstbtUPiVlEyDX46psyCwNVQ=
......@@ -287,6 +338,7 @@ YA/gZsuXzA==
*/
static const key_parameters_t SAMPLE_ECDSA_KEY = {
.name = (uint8_t *)"\x05""ecdsa",
.flags = 256,
.protocol = 3,
.algorithm = 13,
......@@ -311,6 +363,26 @@ static const key_parameters_t SAMPLE_ECDSA_KEY = {
}},
.keytag = 5345,
.key_id = "47fd10011e76cc6741af586041eae5519465fc8d",
.ds_sha1 = { .size = 24, .data = (uint8_t []) {
0x14, 0xe1, 0x0d, 0x01,
0x95, 0x41, 0x03, 0xac, 0x7c, 0x43, 0x81, 0x0c, 0xe9, 0xf4,
0x14, 0xe8, 0x0f, 0x30, 0xab, 0x1c, 0xbe, 0x49, 0xb2, 0x36,
}},
.ds_sha256 = { .size = 36, .data = (uint8_t []) {
0x14, 0xe1, 0x0d, 0x02,
0xba, 0xc2, 0x10, 0x70, 0x36, 0xe7, 0x35, 0xb5, 0x0f, 0x85,
0x00, 0x6c, 0xe4, 0x09, 0xa1, 0x9a, 0x34, 0x38, 0xca, 0xb2,
0x72, 0xe7, 0x07, 0x69, 0xeb, 0xda, 0x03, 0x22, 0x39, 0xa3,
0xd0, 0xca,
}},
.ds_sha384 = { .size = 52, .data = (uint8_t []) {
0x14, 0xe1, 0x0d, 0x04,
0xa0, 0xac, 0x67, 0x90, 0x48, 0x38, 0x72, 0xbe, 0x72, 0xa2,
0x58, 0x31, 0x42, 0x00, 0xa8, 0x8a, 0xb7, 0x5c, 0xdd, 0x70,
0xf6, 0x6a, 0x18, 0xa0, 0x9f, 0x0f, 0x41, 0x4c, 0x07, 0x4d,
0xf0, 0x98, 0x9f, 0xdb, 0x1d, 0xf0, 0xe6, 0x7d, 0x82, 0xd4,
0x31, 0x2c, 0xda, 0x67, 0xb9, 0x3a, 0x76, 0xc1,
}},
.bit_size = 256,
.pem = { .size = 262, .data = (uint8_t []) {
0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment