libknot: replace dnssec/shared/dname with libknot/dname

This commit is contained in:
Daniel Salzman 2025-10-19 13:43:27 +02:00 committed by Libor Peltan
parent 22d4d0a5d6
commit 07751bc184
12 changed files with 9 additions and 284 deletions

View file

@ -481,8 +481,6 @@ src/libknot/dnssec/sample_keys.h
src/libknot/dnssec/shared/bignum.c
src/libknot/dnssec/shared/bignum.h
src/libknot/dnssec/shared/binary_wire.h
src/libknot/dnssec/shared/dname.c
src/libknot/dnssec/shared/dname.h
src/libknot/dnssec/shared/keyid_gnutls.c
src/libknot/dnssec/shared/keyid_gnutls.h
src/libknot/dnssec/shared/shared.h
@ -736,7 +734,6 @@ tests/libknot/test_dnssec_nsec_bitmap.c
tests/libknot/test_dnssec_nsec_hash.c
tests/libknot/test_dnssec_random.c
tests/libknot/test_dnssec_shared_bignum.c
tests/libknot/test_dnssec_shared_dname.c
tests/libknot/test_dnssec_sign.c
tests/libknot/test_dnssec_sign_der.c
tests/libknot/test_dnssec_tsig.c

View file

@ -129,8 +129,6 @@ libknot_la_SOURCES = \
libknot/dnssec/shared/bignum.c \
libknot/dnssec/shared/bignum.h \
libknot/dnssec/shared/binary_wire.h \
libknot/dnssec/shared/dname.c \
libknot/dnssec/shared/dname.h \
libknot/dnssec/shared/keyid_gnutls.c \
libknot/dnssec/shared/keyid_gnutls.h \
libknot/dnssec/shared/shared.h \

View file

@ -3,11 +3,11 @@
* For more information, see <https://www.knot-dns.cz/>
*/
#include "libknot/dname.h"
#include "libknot/dnssec/binary.h"
#include "libknot/dnssec/error.h"
#include "libknot/dnssec/key.h"
#include "libknot/dnssec/key/internal.h"
#include "libknot/dnssec/shared/dname.h"
#include "libknot/dnssec/shared/shared.h"
#include "libknot/dnssec/shared/binary_wire.h"
@ -85,7 +85,7 @@ int dnssec_key_create_ds(const dnssec_key_t *key,
return DNSSEC_DS_HASHING_ERROR;
}
if (gnutls_hash(digest, key->dname, dname_length(key->dname)) != 0 ||
if (gnutls_hash(digest, key->dname, knot_dname_size(key->dname)) != 0 ||
gnutls_hash(digest, key->rdata.data, key->rdata.size) != 0
) {
return DNSSEC_DS_HASHING_ERROR;

View file

@ -9,7 +9,6 @@
#include <stdint.h>
#include "libknot/dnssec/key.h"
#include "libknot/dnssec/shared/dname.h"
/*!
* DNSSEC key.

View file

@ -9,6 +9,7 @@
#include <stdlib.h>
#include <string.h>
#include "libknot/dname.h"
#include "libknot/dnssec/binary.h"
#include "libknot/dnssec/error.h"
#include "libknot/dnssec/key.h"
@ -176,12 +177,12 @@ int dnssec_key_set_dname(dnssec_key_t *key, const uint8_t *dname)
uint8_t *copy = NULL;
if (dname) {
copy = dname_copy(dname);
copy = knot_dname_copy(dname, NULL);
if (!copy) {
return DNSSEC_ENOMEM;
}
dname_normalize(copy);
knot_dname_to_lower(copy);
}
free(key->dname);

View file

@ -1,154 +0,0 @@
/* Copyright (C) CZ.NIC, z.s.p.o. and contributors
* SPDX-License-Identifier: GPL-2.0-or-later
* For more information, see <https://www.knot-dns.cz/>
*/
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "libknot/dnssec/shared/dname.h"
#include "libknot/dnssec/shared/shared.h"
#include "contrib/tolower.h"
/*!
* Get length of a domain name in wire format.
*/
size_t dname_length(const uint8_t *dname)
{
if (!dname) {
return 0;
}
const uint8_t *scan = dname;
uint8_t label_len;
do {
label_len = *scan;
scan += 1 + label_len;
} while (label_len > 0);
assert(scan > dname);
size_t length = scan - dname;
if (length > DNAME_MAX_LENGTH) {
return 0;
}
return length;
}
/*!
* Copy domain name in wire format.
*/
uint8_t *dname_copy(const uint8_t *dname)
{
if (!dname) {
return NULL;
}
size_t length = dname_length(dname);
if (length == 0) {
return NULL;
}
uint8_t *copy = malloc(length);
if (!copy) {
return NULL;
}
memmove(copy, dname, length);
return copy;
}
/*!
* Normalize dname label in-place.
*
* \return Number of processed bytes, 0 if we encounter the last label.
*/
static uint8_t normalize_label(uint8_t *label)
{
assert(label);
uint8_t len = *label;
if (len == 0 || len > DNAME_MAX_LABEL_LENGTH) {
return 0;
}
for (uint8_t *scan = label + 1, *end = scan + len; scan < end; scan++) {
*scan = knot_tolower(*scan);
}
return len + 1;
}
/*!
* Normalize domain name in wire format.
*/
void dname_normalize(uint8_t *dname)
{
if (!dname) {
return;
}
uint8_t read, *scan = dname;
do {
read = normalize_label(scan);
scan += read;
} while (read > 0);
}
/*!
* Compare dname labels case insensitively.
*/
static int label_casecmp(const uint8_t *a, const uint8_t *b, uint8_t len)
{
assert(a);
assert(b);
for (const uint8_t *a_end = a + len; a < a_end; a++, b++) {
if (knot_tolower(*a) != knot_tolower(*b)) {
return false;
}
}
return true;
}
/*!
* Check if two dnames are equal.
*/
bool dname_equal(const uint8_t *one, const uint8_t *two)
{
if (!one || !two) {
return false;
}
const uint8_t *scan_one = one;
const uint8_t *scan_two = two;
for (;;) {
if (*scan_one != *scan_two) {
return false;
}
uint8_t len = *scan_one;
if (len == 0) {
return true;
} else if (len > DNAME_MAX_LABEL_LENGTH) {
return false;
}
scan_one += 1;
scan_two += 1;
if (!label_casecmp(scan_one, scan_two, len)) {
return false;
}
scan_one += len;
scan_two += len;
}
return true;
}

View file

@ -1,46 +0,0 @@
/* Copyright (C) CZ.NIC, z.s.p.o. and contributors
* SPDX-License-Identifier: GPL-2.0-or-later
* For more information, see <https://www.knot-dns.cz/>
*/
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
/*!
* Maximal length of domain name including labels and length bytes.
* \see RFC 1035
*/
#define DNAME_MAX_LENGTH 255
/*!
* Maximal length of the domain name label, excluding the label size.
* \see RFC 1035
*/
#define DNAME_MAX_LABEL_LENGTH 63
/*!
* Get length of a domain name in wire format.
*/
size_t dname_length(const uint8_t *dname);
/*!
* Copy domain name in wire format.
*/
uint8_t *dname_copy(const uint8_t *dname);
/*!
* Normalize domain name in wire format.
*
* Currently converts all letters to lowercase.
*/
void dname_normalize(uint8_t *dname);
/*!
* Check if two dnames are equal.
*
* Case insensitive.
*/
bool dname_equal(const uint8_t *one, const uint8_t *two);

View file

@ -11,7 +11,7 @@
#include <stdlib.h>
#include <string.h>
#include "libknot/dnssec/shared/dname.h"
#include "libknot/dname.h"
#include "libknot/dnssec/error.h"
#include "libknot/dnssec/shared/shared.h"
#include "libknot/dnssec/tsig.h"
@ -71,7 +71,7 @@ static const algorithm_id_t *lookup_algorithm(algorithm_match_cb match,
static bool match_dname(const algorithm_id_t *algorithm, const void *data)
{
const uint8_t *search = data;
return dname_equal(search, (uint8_t *)algorithm->dname);
return knot_dname_is_case_equal(search, (uint8_t *)algorithm->dname);
}
static bool match_name(const algorithm_id_t *algorithm, const void *data)

1
tests/.gitignore vendored
View file

@ -66,7 +66,6 @@
/libknot/test_dnssec_nsec_hash
/libknot/test_dnssec_random
/libknot/test_dnssec_shared_bignum
/libknot/test_dnssec_shared_dname
/libknot/test_dnssec_sign
/libknot/test_dnssec_sign_der
/libknot/test_dnssec_tsig

View file

@ -131,7 +131,6 @@ check_PROGRAMS += \
libknot/test_dnssec_sign \
libknot/test_dnssec_sign_der \
libknot/test_dnssec_shared_bignum \
libknot/test_dnssec_shared_dname \
libknot/test_dnssec_tsig \
libknot/test_control \
libknot/test_cookies \

View file

@ -1,68 +0,0 @@
/* Copyright (C) CZ.NIC, z.s.p.o. and contributors
* SPDX-License-Identifier: GPL-2.0-or-later
* For more information, see <https://www.knot-dns.cz/>
*/
#include <stdbool.h>
#include <string.h>
#include <tap/basic.h>
#include "libknot/dnssec/shared/dname.c"
static void ok_length(const char *dname, size_t length, const char *info)
{
ok(dname_length((uint8_t *)dname) == length,
"dname_length() for %s", info);
}
static void test_length(void)
{
ok_length(NULL, 0, "NULL");
ok_length("", 1, ".");
ok_length("\x2""cz", 4, "cz.");
ok_length("\x7""example""\x3""com", 13, "example.com.");
}
static bool dname_binary_equal(const uint8_t *one, const uint8_t *two)
{
return one && two && strcmp((char *)one, (char *)two) == 0;
}
static void test_copy(void)
{
const uint8_t *dname = (uint8_t *)"\x3""www""\x8""KNOT-DNS""\x2""cz";
uint8_t *copy = dname_copy(dname);
ok(dname_binary_equal(dname, copy), "dname_copy()");
free(copy);
}
static void test_equal(void)
{
#define eq(a, b) dname_equal((uint8_t *)a, (uint8_t *)b)
ok(eq("\x4""kiwi""\x4""limo", "\x4""kiwi""\x4""limo") == true,
"dname_equal() same");
ok(eq("\x6""orange", "\x6""ORANGE") == true,
"dname_equal() case single label");
ok(eq("\x6""Banana""\03""Tea", "\x6""bANAna""\x3""tea") == true,
"dname_equal() case two labels");
ok(eq("\x4""Coco""\x4""MILK", "\x3""cow""\x4""milk") == false,
"dname_equal() different first");
ok(eq("\x4""LIME""\x5""syrup", "\x4""LIme""\x4""beer") == false,
"dname_equal() different last");
ok(eq("\x5""apple", "\x5""apple""\x5""shake") == false,
"dname_equal() a prefix of b");
ok(eq("\x5""apple""\x5""juice", "\x5""apple") == false,
"dname_equal() b prefix of a");
}
int main(void)
{
plan_lazy();
test_length();
test_copy();
test_equal();
return 0;
}

View file

@ -6,8 +6,8 @@
#include <tap/basic.h>
#include <string.h>
#include "libknot/dname.h"
#include "libknot/dnssec/binary.h"
#include "libknot/dnssec/shared/dname.c"
#include "libknot/dnssec/tsig.h"
static const dnssec_binary_t payload = {
@ -82,7 +82,7 @@ static void test_lookup_dname(const uint8_t *dname, int algorithm)
const uint8_t *reverse = dnssec_tsig_algorithm_to_dname(algorithm);
ok((algorithm == DNSSEC_TSIG_UNKNOWN && reverse == NULL) ||
(algorithm != DNSSEC_TSIG_UNKNOWN && dname_equal(reverse, dname)),
(algorithm != DNSSEC_TSIG_UNKNOWN && knot_dname_is_case_equal(reverse, dname)),
"dnssec_tsig_algorithm_to_dname(%d)", algorithm);
}