/*
 * Part of DNS zone file validator `validns`.
 *
 * Copyright 2025-2026 OARC, Inc.
 * Copyright 2011-2025 Anton Berezin <tobez@tobez.org>
 * Modified BSD license.
 * (See LICENSE file in the distribution.)
 *
 */
#include <sys/types.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "common.h"
#include "textparse.h"
#include "mempool.h"
#include "carp.h"
#include "rr.h"

static struct rr* ds_cds_parse(char* name, long ttl, int type, char* s)
{
    struct rr_ds* rr = getmem(sizeof(*rr));
    int           key_tag, algorithm, digest_type;

    key_tag = extract_integer(&s, "key tag", NULL);
    if (key_tag < 0)
        return NULL;
    rr->key_tag = key_tag;

    int alg_flags;
    algorithm = extract_algorithm(&s, "algorithm", &alg_flags);
    if (algorithm < 0)
        return NULL;
    rr->algorithm = algorithm;
    if (alg_flags & ALG_FLAG_UNKNOWN)
        bitch("%s %s record refers to a DNSKEY with an unsupported algorithm %d",
            name, type == T_CDS ? "CDS" : "DS", algorithm);
    /* ALG_FLAG_POLICY_OFF: silent here; the global one-shot warning has
     * already fired from check_algorithm_available. Record is kept. */

    digest_type = extract_integer(&s, "digest type", NULL);
    if (digest_type < 0)
        return NULL;
    rr->digest_type = digest_type;

    rr->digest = extract_hex_binary_data(&s, "digest", EXTRACT_EAT_WHITESPACE);
    if (rr->digest.length < 0)
        return NULL;

    /* See http://www.iana.org/assignments/ds-rr-types/ds-rr-types.xml
     * for valid digest types. */
    switch (digest_type) {
    case 1:
        if (rr->digest.length != SHA1_BYTES) {
            return bitch("wrong SHA-1 digest length: %d bytes found, %d bytes expected", rr->digest.length, SHA1_BYTES);
        }
        break;
    case 2:
        if (rr->digest.length != SHA256_BYTES) {
            return bitch("wrong SHA-256 digest length: %d bytes found, %d bytes expected", rr->digest.length, SHA256_BYTES);
        }
        break;
    case 3:
        if (rr->digest.length != GOST_BYTES) {
            return bitch("wrong GOST R 34.11-94 digest length: %d bytes found, %d bytes expected", rr->digest.length, GOST_BYTES);
        }
        break;
    case 4:
        if (rr->digest.length != SHA384_BYTES) {
            return bitch("wrong SHA-384 digest length: %d bytes found, %d bytes expected", rr->digest.length, SHA384_BYTES);
        }
        break;
    default:
        return bitch("bad or unsupported digest type %d", digest_type);
    }

    if (*s) {
        return bitch("garbage after valid %s data", type == T_CDS ? "CDS" : "DS");
    }
    return store_record(type, name, ttl, rr);
}

static char* ds_cds_human(struct rr* rrv)
{
    RRCAST(ds);
    char  ss[4096];
    char* s = ss;
    int   l;
    int   i;

    l = snprintf(s, 4096, "%u %u %u ", rr->key_tag, rr->algorithm, rr->digest_type);
    s += l;
    for (i = 0; i < rr->digest.length; i++) {
        l = snprintf(s, 4096 - (s - ss), "%02X", (unsigned char)rr->digest.data[i]);
        s += l;
    }
    return quickstrdup_temp(ss);
}

static struct binary_data ds_cds_wirerdata(struct rr* rrv)
{
    RRCAST(ds);

    return compose_binary_data("211d", 1,
        rr->key_tag, rr->algorithm, rr->digest_type,
        rr->digest);
}

struct rr_methods ds_methods  = { ds_cds_parse, ds_cds_human, ds_cds_wirerdata, NULL, NULL };
struct rr_methods cds_methods = { ds_cds_parse, ds_cds_human, ds_cds_wirerdata, NULL, NULL };
