key_25.c revision caa55a655742e4e600bdb05a38e07f6896f32081
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley/*
7d98a1783f222964bcde7d56dab77b822706204dBob Halley * Copyright (C) 1999-2001 Internet Software Consortium.
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley *
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * Permission to use, copy, modify, and distribute this software for any
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * purpose with or without fee is hereby granted, provided that the above
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * copyright notice and this permission notice appear in all copies.
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley *
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley */
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence/* $Id: key_25.c,v 1.39 2001/03/28 02:57:26 bwelling Exp $ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley/*
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * Reviewed: Wed Mar 15 16:47:10 PST 2000 by halley.
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley */
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley/* RFC 2535 */
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley#ifndef RDATA_GENERIC_KEY_25_C
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley#define RDATA_GENERIC_KEY_25_C
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley
5fe5a0c02634eaadfcbc3528bf2c184557110a3bAndreas Gustafsson#include <dst/dst.h>
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley#define RRTYPE_KEY_ATTRIBUTES (DNS_RDATATYPEATTR_DNSSEC)
8327c62a49a2487d29a37acbed6b602e629fc0eeAndreas Gustafsson
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halleystatic inline isc_result_t
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halleyfromtext_key(ARGS_FROMTEXT) {
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley isc_token_t token;
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley dns_secalg_t alg;
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley dns_secproto_t proto;
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley dns_keyflags_t flags;
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley REQUIRE(type == 25);
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley UNUSED(type);
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley UNUSED(rdclass);
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley UNUSED(origin);
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley UNUSED(downcase);
8327c62a49a2487d29a37acbed6b602e629fc0eeAndreas Gustafsson
8327c62a49a2487d29a37acbed6b602e629fc0eeAndreas Gustafsson /* flags */
8327c62a49a2487d29a37acbed6b602e629fc0eeAndreas Gustafsson RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
608f870f4821972313eadc5388a42fa55b6279d1Mark Andrews ISC_FALSE));
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington RETTOK(dns_keyflags_fromtext(&flags, &token.value.as_textregion));
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington RETERR(uint16_tobuffer(flags, target));
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley /* protocol */
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley ISC_FALSE));
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson RETTOK(dns_secproto_fromtext(&proto, &token.value.as_textregion));
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley RETERR(mem_tobuffer(target, &proto, 1));
edcd1247ad7e81bb8b430e610d9718f64c70f05dDavid Lawrence
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley /* algorithm */
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley ISC_FALSE));
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley RETTOK(dns_secalg_fromtext(&alg, &token.value.as_textregion));
8582a1e113c13886ccbd1b534d6c240315767be6Bob Halley RETERR(mem_tobuffer(target, &alg, 1));
8582a1e113c13886ccbd1b534d6c240315767be6Bob Halley
8582a1e113c13886ccbd1b534d6c240315767be6Bob Halley /* No Key? */
edcd1247ad7e81bb8b430e610d9718f64c70f05dDavid Lawrence if ((flags & 0xc000) == 0xc000)
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley return (ISC_R_SUCCESS);
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley
edcd1247ad7e81bb8b430e610d9718f64c70f05dDavid Lawrence return (isc_base64_tobuffer(lexer, target, -1));
edcd1247ad7e81bb8b430e610d9718f64c70f05dDavid Lawrence}
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley
c90f5e8d1edbd5c277f2ee320167a12a30ba7c7bMichael Graffstatic inline isc_result_t
edcd1247ad7e81bb8b430e610d9718f64c70f05dDavid Lawrencetotext_key(ARGS_TOTEXT) {
c90f5e8d1edbd5c277f2ee320167a12a30ba7c7bMichael Graff isc_region_t sr;
8582a1e113c13886ccbd1b534d6c240315767be6Bob Halley char buf[sizeof "64000"];
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson unsigned int flags;
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson unsigned char algorithm;
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson REQUIRE(rdata->type == 25);
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson REQUIRE(rdata->length != 0);
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson dns_rdata_toregion(rdata, &sr);
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson /* flags */
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson flags = uint16_fromregion(&sr);
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson isc_region_consume(&sr, 2);
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson sprintf(buf, "%u", flags);
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson RETERR(str_totext(buf, target));
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson RETERR(str_totext(" ", target));
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson /* protocol */
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson sprintf(buf, "%u", sr.base[0]);
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson isc_region_consume(&sr, 1);
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson RETERR(str_totext(buf, target));
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson RETERR(str_totext(" ", target));
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson /* algorithm */
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson algorithm = sr.base[0];
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson sprintf(buf, "%u", algorithm);
8582a1e113c13886ccbd1b534d6c240315767be6Bob Halley isc_region_consume(&sr, 1);
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson RETERR(str_totext(buf, target));
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson /* No Key? */
8582a1e113c13886ccbd1b534d6c240315767be6Bob Halley if ((flags & 0xc000) == 0xc000)
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson return (ISC_R_SUCCESS);
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson /* key */
44fee668021c7ceef4ee1c848031d883a508b359James Brister if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
44fee668021c7ceef4ee1c848031d883a508b359James Brister RETERR(str_totext(" (", target));
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson RETERR(str_totext(tctx->linebreak, target));
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson RETERR(isc_base64_totext(&sr, tctx->width - 2,
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson tctx->linebreak, target));
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson if ((tctx->flags & DNS_STYLEFLAG_COMMENT) != 0)
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson RETERR(str_totext(tctx->linebreak, target));
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson else if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson RETERR(str_totext(" ", target));
44fee668021c7ceef4ee1c848031d883a508b359James Brister
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson RETERR(str_totext(")", target));
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson if ((tctx->flags & DNS_STYLEFLAG_COMMENT) != 0) {
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson isc_region_t tmpr;
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson RETERR(str_totext(" ; key id = ", target));
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson dns_rdata_toregion(rdata, &tmpr);
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson sprintf(buf, "%u", dst_region_computeid(&tmpr, algorithm));
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson RETERR(str_totext(buf, target));
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson }
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson return (ISC_R_SUCCESS);
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson}
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafssonstatic inline isc_result_t
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafssonfromwire_key(ARGS_FROMWIRE) {
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson isc_region_t sr;
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson REQUIRE(type == 25);
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson UNUSED(type);
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson UNUSED(rdclass);
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson UNUSED(dctx);
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson UNUSED(downcase);
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson isc_buffer_activeregion(source, &sr);
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson if (sr.length < 4)
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson return (ISC_R_UNEXPECTEDEND);
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson isc_buffer_forward(source, sr.length);
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson return (mem_tobuffer(target, sr.base, sr.length));
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson}
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafssonstatic inline isc_result_t
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafssontowire_key(ARGS_TOWIRE) {
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson isc_region_t sr;
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson REQUIRE(rdata->type == 25);
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson REQUIRE(rdata->length != 0);
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson UNUSED(cctx);
5542df09597c479be604da0ece8271cbc6fd9c4aDavid Lawrence
5542df09597c479be604da0ece8271cbc6fd9c4aDavid Lawrence dns_rdata_toregion(rdata, &sr);
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley return (mem_tobuffer(target, sr.base, sr.length));
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley}
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson
5542df09597c479be604da0ece8271cbc6fd9c4aDavid Lawrencestatic inline int
5542df09597c479be604da0ece8271cbc6fd9c4aDavid Lawrencecompare_key(ARGS_COMPARE) {
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley isc_region_t r1;
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley isc_region_t r2;
8582a1e113c13886ccbd1b534d6c240315767be6Bob Halley
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson REQUIRE(rdata1->type == rdata2->type);
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley REQUIRE(rdata1->rdclass == rdata2->rdclass);
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley REQUIRE(rdata1->type == 25);
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley REQUIRE(rdata1->length != 0);
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley REQUIRE(rdata2->length != 0);
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley dns_rdata_toregion(rdata1, &r1);
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley dns_rdata_toregion(rdata2, &r2);
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley return (compare_region(&r1, &r2));
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley}
static inline isc_result_t
fromstruct_key(ARGS_FROMSTRUCT) {
dns_rdata_key_t *key = source;
REQUIRE(type == 25);
REQUIRE(source != NULL);
REQUIRE(key->common.rdtype == type);
REQUIRE(key->common.rdclass == rdclass);
UNUSED(type);
UNUSED(rdclass);
/* Flags */
RETERR(uint16_tobuffer(key->flags, target));
/* Protocol */
RETERR(uint8_tobuffer(key->protocol, target));
/* Algorithm */
RETERR(uint8_tobuffer(key->algorithm, target));
/* Data */
return (mem_tobuffer(target, key->data, key->datalen));
}
static inline isc_result_t
tostruct_key(ARGS_TOSTRUCT) {
dns_rdata_key_t *key = target;
isc_region_t sr;
REQUIRE(rdata->type == 25);
REQUIRE(target != NULL);
REQUIRE(rdata->length != 0);
key->common.rdclass = rdata->rdclass;
key->common.rdtype = rdata->type;
ISC_LINK_INIT(&key->common, link);
dns_rdata_toregion(rdata, &sr);
/* Flags */
if (sr.length < 2)
return (ISC_R_UNEXPECTEDEND);
key->flags = uint16_fromregion(&sr);
isc_region_consume(&sr, 2);
/* Protocol */
if (sr.length < 1)
return (ISC_R_UNEXPECTEDEND);
key->protocol = uint8_fromregion(&sr);
isc_region_consume(&sr, 1);
/* Algorithm */
if (sr.length < 1)
return (ISC_R_UNEXPECTEDEND);
key->algorithm = uint8_fromregion(&sr);
isc_region_consume(&sr, 1);
/* Data */
key->datalen = sr.length;
if (key->datalen > 0) {
key->data = mem_maybedup(mctx, sr.base, key->datalen);
if (key->data == NULL)
return (ISC_R_NOMEMORY);
} else
key->data = NULL;
key->mctx = mctx;
return (ISC_R_SUCCESS);
}
static inline void
freestruct_key(ARGS_FREESTRUCT) {
dns_rdata_key_t *key = (dns_rdata_key_t *) source;
REQUIRE(source != NULL);
REQUIRE(key->common.rdtype == 25);
if (key->mctx == NULL)
return;
if (key->data != NULL)
isc_mem_free(key->mctx, key->data);
key->mctx = NULL;
}
static inline isc_result_t
additionaldata_key(ARGS_ADDLDATA) {
REQUIRE(rdata->type == 25);
UNUSED(rdata);
UNUSED(add);
UNUSED(arg);
return (ISC_R_SUCCESS);
}
static inline isc_result_t
digest_key(ARGS_DIGEST) {
isc_region_t r;
REQUIRE(rdata->type == 25);
dns_rdata_toregion(rdata, &r);
return ((digest)(arg, &r));
}
#endif /* RDATA_GENERIC_KEY_25_C */