dnssec.h revision 269257
1/* 2 * dnssec.h -- defines for the Domain Name System (SEC) (DNSSEC) 3 * 4 * Copyright (c) 2005-2008, NLnet Labs. All rights reserved. 5 * 6 * See LICENSE for the license. 7 * 8 * A bunch of defines that are used in the DNS 9 */ 10 11/** 12 * \file dnssec.h 13 * 14 * This module contains base functions for DNSSEC operations 15 * (RFC4033 t/m RFC4035). 16 * 17 * Since those functions heavily rely op cryptographic operations, 18 * this module is dependent on openssl. 19 * 20 */ 21 22 23#ifndef LDNS_DNSSEC_H 24#define LDNS_DNSSEC_H 25 26#include <ldns/common.h> 27#if LDNS_BUILD_CONFIG_HAVE_SSL 28#include <openssl/ssl.h> 29#include <openssl/evp.h> 30#endif /* LDNS_BUILD_CONFIG_HAVE_SSL */ 31#include <ldns/packet.h> 32#include <ldns/keys.h> 33#include <ldns/zone.h> 34#include <ldns/resolver.h> 35#include <ldns/dnssec_zone.h> 36 37#ifdef __cplusplus 38extern "C" { 39#endif 40 41#define LDNS_MAX_KEYLEN 2048 42#define LDNS_DNSSEC_KEYPROTO 3 43/* default time before sigs expire */ 44#define LDNS_DEFAULT_EXP_TIME 2419200 /* 4 weeks */ 45 46/** return values for the old-signature callback */ 47#define LDNS_SIGNATURE_LEAVE_ADD_NEW 0 48#define LDNS_SIGNATURE_LEAVE_NO_ADD 1 49#define LDNS_SIGNATURE_REMOVE_ADD_NEW 2 50#define LDNS_SIGNATURE_REMOVE_NO_ADD 3 51 52/** 53 * Returns the first RRSIG rr that corresponds to the rrset 54 * with the given name and type 55 * 56 * \param[in] name The dname of the RRset covered by the RRSIG to find 57 * \param[in] type The type of the RRset covered by the RRSIG to find 58 * \param[in] rrs List of rrs to search in 59 * \returns Pointer to the first RRsig ldns_rr found, or NULL if it is 60 * not present 61 */ 62ldns_rr *ldns_dnssec_get_rrsig_for_name_and_type(const ldns_rdf *name, 63 const ldns_rr_type type, 64 const ldns_rr_list *rrs); 65 66/** 67 * Returns the DNSKEY that corresponds to the given RRSIG rr from the list, if 68 * any 69 * 70 * \param[in] rrsig The rrsig to find the DNSKEY for 71 * \param[in] rrs The rr list to find the key in 72 * \return The DNSKEY that corresponds to the given RRSIG, or NULL if it was 73 * not found. 74 */ 75ldns_rr *ldns_dnssec_get_dnskey_for_rrsig(const ldns_rr *rrsig, const ldns_rr_list *rrs); 76 77/** 78 * Returns the rdata field that contains the bitmap of the covered types of 79 * the given NSEC record 80 * 81 * \param[in] nsec The nsec to get the covered type bitmap of 82 * \return An ldns_rdf containing the bitmap, or NULL on error 83 */ 84ldns_rdf *ldns_nsec_get_bitmap(ldns_rr *nsec); 85 86 87#define LDNS_NSEC3_MAX_ITERATIONS 65535 88 89/** 90 * Returns the dname of the closest (provable) encloser 91 */ 92ldns_rdf * 93ldns_dnssec_nsec3_closest_encloser(ldns_rdf *qname, 94 ldns_rr_type qtype, 95 ldns_rr_list *nsec3s); 96 97/** 98 * Checks whether the packet contains rrsigs 99 */ 100bool 101ldns_dnssec_pkt_has_rrsigs(const ldns_pkt *pkt); 102 103/** 104 * Returns a ldns_rr_list containing the signatures covering the given name 105 * and type 106 */ 107ldns_rr_list *ldns_dnssec_pkt_get_rrsigs_for_name_and_type(const ldns_pkt *pkt, ldns_rdf *name, ldns_rr_type type); 108 109/** 110 * Returns a ldns_rr_list containing the signatures covering the given type 111 */ 112ldns_rr_list *ldns_dnssec_pkt_get_rrsigs_for_type(const ldns_pkt *pkt, ldns_rr_type type); 113 114/** 115 * calculates a keytag of a key for use in DNSSEC. 116 * 117 * \param[in] key the key as an RR to use for the calc. 118 * \return the keytag 119 */ 120uint16_t ldns_calc_keytag(const ldns_rr *key); 121 122/** 123 * Calculates keytag of DNSSEC key, operates on wireformat rdata. 124 * \param[in] key the key as uncompressed wireformat rdata. 125 * \param[in] keysize length of key data. 126 * \return the keytag 127 */ 128uint16_t ldns_calc_keytag_raw(uint8_t* key, size_t keysize); 129 130#if LDNS_BUILD_CONFIG_HAVE_SSL 131/** 132 * converts a buffer holding key material to a DSA key in openssl. 133 * 134 * \param[in] key the key to convert 135 * \return a DSA * structure with the key material 136 */ 137DSA *ldns_key_buf2dsa(ldns_buffer *key); 138/** 139 * Like ldns_key_buf2dsa, but uses raw buffer. 140 * \param[in] key the uncompressed wireformat of the key. 141 * \param[in] len length of key data 142 * \return a DSA * structure with the key material 143 */ 144DSA *ldns_key_buf2dsa_raw(unsigned char* key, size_t len); 145 146/** 147 * Utility function to calculate hash using generic EVP_MD pointer. 148 * \param[in] data the data to hash. 149 * \param[in] len length of data. 150 * \param[out] dest the destination of the hash, must be large enough. 151 * \param[in] md the message digest to use. 152 * \return true if worked, false on failure. 153 */ 154int ldns_digest_evp(unsigned char* data, unsigned int len, 155 unsigned char* dest, const EVP_MD* md); 156 157/** 158 * Converts a holding buffer with key material to EVP PKEY in openssl. 159 * Only available if ldns was compiled with GOST. 160 * \param[in] key data to convert 161 * \param[in] keylen length of the key data 162 * \return the key or NULL on error. 163 */ 164EVP_PKEY* ldns_gost2pkey_raw(unsigned char* key, size_t keylen); 165 166/** 167 * Converts a holding buffer with key material to EVP PKEY in openssl. 168 * Only available if ldns was compiled with ECDSA. 169 * \param[in] key data to convert 170 * \param[in] keylen length of the key data 171 * \param[in] algo precise algorithm to initialize ECC group values. 172 * \return the key or NULL on error. 173 */ 174EVP_PKEY* ldns_ecdsa2pkey_raw(unsigned char* key, size_t keylen, uint8_t algo); 175 176#endif /* LDNS_BUILD_CONFIG_HAVE_SSL */ 177 178#if LDNS_BUILD_CONFIG_HAVE_SSL 179/** 180 * converts a buffer holding key material to a RSA key in openssl. 181 * 182 * \param[in] key the key to convert 183 * \return a RSA * structure with the key material 184 */ 185RSA *ldns_key_buf2rsa(ldns_buffer *key); 186 187/** 188 * Like ldns_key_buf2rsa, but uses raw buffer. 189 * \param[in] key the uncompressed wireformat of the key. 190 * \param[in] len length of key data 191 * \return a RSA * structure with the key material 192 */ 193RSA *ldns_key_buf2rsa_raw(unsigned char* key, size_t len); 194#endif /* LDNS_BUILD_CONFIG_HAVE_SSL */ 195 196/** 197 * returns a new DS rr that represents the given key rr. 198 * 199 * \param[in] *key the key to convert 200 * \param[in] h the hash to use LDNS_SHA1/LDNS_SHA256 201 * 202 * \return ldns_rr* a new rr pointer to a DS 203 */ 204ldns_rr *ldns_key_rr2ds(const ldns_rr *key, ldns_hash h); 205 206/** 207 * Create the type bitmap for an NSEC(3) record 208 */ 209ldns_rdf * 210ldns_dnssec_create_nsec_bitmap(ldns_rr_type rr_type_list[], 211 size_t size, 212 ldns_rr_type nsec_type); 213 214/** 215 * returns whether a rrset of the given type is found in the rrsets. 216 * 217 * \param[in] rrsets the rrsets to be tested 218 * \param[in] type the type to test for 219 * \return int 1 if the type was found, 0 otherwise. 220 */ 221int 222ldns_dnssec_rrsets_contains_type (ldns_dnssec_rrsets *rrsets, ldns_rr_type type); 223 224/** 225 * Creates NSEC 226 */ 227ldns_rr * 228ldns_dnssec_create_nsec(ldns_dnssec_name *from, 229 ldns_dnssec_name *to, 230 ldns_rr_type nsec_type); 231 232 233/** 234 * Creates NSEC3 235 */ 236ldns_rr * 237ldns_dnssec_create_nsec3(ldns_dnssec_name *from, 238 ldns_dnssec_name *to, 239 ldns_rdf *zone_name, 240 uint8_t algorithm, 241 uint8_t flags, 242 uint16_t iterations, 243 uint8_t salt_length, 244 uint8_t *salt); 245 246/** 247 * Create a NSEC record 248 * \param[in] cur_owner the current owner which should be taken as the starting point 249 * \param[in] next_owner the rrlist which the nsec rr should point to 250 * \param[in] rrs all rrs from the zone, to find all RR types of cur_owner in 251 * \return a ldns_rr with the nsec record in it 252 */ 253ldns_rr * ldns_create_nsec(ldns_rdf *cur_owner, ldns_rdf *next_owner, ldns_rr_list *rrs); 254 255/** 256 * Calculates the hashed name using the given parameters 257 * \param[in] *name The owner name to calculate the hash for 258 * \param[in] algorithm The hash algorithm to use 259 * \param[in] iterations The number of hash iterations to use 260 * \param[in] salt_length The length of the salt in bytes 261 * \param[in] salt The salt to use 262 * \return The hashed owner name rdf, without the domain name 263 */ 264ldns_rdf *ldns_nsec3_hash_name(ldns_rdf *name, uint8_t algorithm, uint16_t iterations, uint8_t salt_length, uint8_t *salt); 265 266/** 267 * Sets all the NSEC3 options. The rr to set them in must be initialized with _new() and 268 * type LDNS_RR_TYPE_NSEC3 269 * \param[in] *rr The RR to set the values in 270 * \param[in] algorithm The NSEC3 hash algorithm 271 * \param[in] flags The flags field 272 * \param[in] iterations The number of hash iterations 273 * \param[in] salt_length The length of the salt in bytes 274 * \param[in] salt The salt bytes 275 */ 276void ldns_nsec3_add_param_rdfs(ldns_rr *rr, 277 uint8_t algorithm, 278 uint8_t flags, 279 uint16_t iterations, 280 uint8_t salt_length, 281 uint8_t *salt); 282 283/* this will NOT return the NSEC3 completed, you will have to run the 284 finalize function on the rrlist later! */ 285ldns_rr * 286ldns_create_nsec3(ldns_rdf *cur_owner, 287 ldns_rdf *cur_zone, 288 ldns_rr_list *rrs, 289 uint8_t algorithm, 290 uint8_t flags, 291 uint16_t iterations, 292 uint8_t salt_length, 293 uint8_t *salt, 294 bool emptynonterminal); 295 296/** 297 * Returns the hash algorithm used in the given NSEC3 RR 298 * \param[in] *nsec3_rr The RR to read from 299 * \return The algorithm identifier, or 0 on error 300 */ 301uint8_t ldns_nsec3_algorithm(const ldns_rr *nsec3_rr); 302 303/** 304 * Returns flags field 305 */ 306uint8_t 307ldns_nsec3_flags(const ldns_rr *nsec3_rr); 308 309/** 310 * Returns true if the opt-out flag has been set in the given NSEC3 RR 311 * \param[in] *nsec3_rr The RR to read from 312 * \return true if the RR has type NSEC3 and the opt-out bit has been set, false otherwise 313 */ 314bool ldns_nsec3_optout(const ldns_rr *nsec3_rr); 315 316/** 317 * Returns the number of hash iterations used in the given NSEC3 RR 318 * \param[in] *nsec3_rr The RR to read from 319 * \return The number of iterations 320 */ 321uint16_t ldns_nsec3_iterations(const ldns_rr *nsec3_rr); 322 323/** 324 * Returns the salt used in the given NSEC3 RR 325 * \param[in] *nsec3_rr The RR to read from 326 * \return The salt rdf, or NULL on error 327 */ 328ldns_rdf *ldns_nsec3_salt(const ldns_rr *nsec3_rr); 329 330/** 331 * Returns the length of the salt used in the given NSEC3 RR 332 * \param[in] *nsec3_rr The RR to read from 333 * \return The length of the salt in bytes 334 */ 335uint8_t ldns_nsec3_salt_length(const ldns_rr *nsec3_rr); 336 337/** 338 * Returns the salt bytes used in the given NSEC3 RR 339 * \param[in] *nsec3_rr The RR to read from 340 * \return The salt in bytes, this is alloced, so you need to free it 341 */ 342uint8_t *ldns_nsec3_salt_data(const ldns_rr *nsec3_rr); 343 344/** 345 * Returns the first label of the next ownername in the NSEC3 chain (ie. without the domain) 346 * \param[in] nsec3_rr The RR to read from 347 * \return The first label of the next owner name in the NSEC3 chain, or NULL on error 348 */ 349ldns_rdf *ldns_nsec3_next_owner(const ldns_rr *nsec3_rr); 350 351/** 352 * Returns the bitmap specifying the covered types of the given NSEC3 RR 353 * \param[in] *nsec3_rr The RR to read from 354 * \return The covered type bitmap rdf 355 */ 356ldns_rdf *ldns_nsec3_bitmap(const ldns_rr *nsec3_rr); 357 358/** 359 * Calculates the hashed name using the parameters of the given NSEC3 RR 360 * \param[in] *nsec The RR to use the parameters from 361 * \param[in] *name The owner name to calculate the hash for 362 * \return The hashed owner name rdf, without the domain name 363 */ 364ldns_rdf *ldns_nsec3_hash_name_frm_nsec3(const ldns_rr *nsec, ldns_rdf *name); 365 366/** 367 * Check if RR type t is enumerated and set in the RR type bitmap rdf. 368 * \param[in] bitmap the RR type bitmap rdf to look in 369 * \param[in] type the type to check for 370 * \return true when t is found and set, otherwise return false 371 */ 372bool ldns_nsec_bitmap_covers_type(const ldns_rdf* bitmap, ldns_rr_type type); 373 374/** 375 * Checks if RR type t is enumerated in the type bitmap rdf and sets the bit. 376 * \param[in] bitmap the RR type bitmap rdf to look in 377 * \param[in] type the type to for which the bit to set 378 * \return LDNS_STATUS_OK on success. LDNS_STATUS_TYPE_NOT_IN_BITMAP is 379 * returned when the bitmap does not contain the bit to set. 380 */ 381ldns_status ldns_nsec_bitmap_set_type(ldns_rdf* bitmap, ldns_rr_type type); 382 383/** 384 * Checks if RR type t is enumerated in the type bitmap rdf and clears the bit. 385 * \param[in] bitmap the RR type bitmap rdf to look in 386 * \param[in] type the type to for which the bit to clear 387 * \return LDNS_STATUS_OK on success. LDNS_STATUS_TYPE_NOT_IN_BITMAP is 388 * returned when the bitmap does not contain the bit to clear. 389 */ 390ldns_status ldns_nsec_bitmap_clear_type(ldns_rdf* bitmap, ldns_rr_type type); 391 392/** 393 * Checks coverage of NSEC(3) RR name span 394 * Remember that nsec and name must both be in canonical form (ie use 395 * \ref ldns_rr2canonical and \ref ldns_dname2canonical prior to calling this 396 * function) 397 * 398 * \param[in] nsec The NSEC RR to check 399 * \param[in] name The owner dname to check, if the nsec record is a NSEC3 record, this should be the hashed name 400 * \return true if the NSEC RR covers the owner name 401 */ 402bool ldns_nsec_covers_name(const ldns_rr *nsec, const ldns_rdf *name); 403 404#if LDNS_BUILD_CONFIG_HAVE_SSL 405/** 406 * verify a packet 407 * \param[in] p the packet 408 * \param[in] t the rr set type to check 409 * \param[in] o the rr set name to check 410 * \param[in] k list of keys 411 * \param[in] s list of sigs (may be null) 412 * \param[out] good_keys keys which validated the packet 413 * \return status 414 * 415 */ 416ldns_status ldns_pkt_verify(ldns_pkt *p, ldns_rr_type t, ldns_rdf *o, ldns_rr_list *k, ldns_rr_list *s, ldns_rr_list *good_keys); 417 418/** 419 * verify a packet 420 * \param[in] p the packet 421 * \param[in] t the rr set type to check 422 * \param[in] o the rr set name to check 423 * \param[in] k list of keys 424 * \param[in] s list of sigs (may be null) 425 * \param[in] check_time the time for which the validation is performed 426 * \param[out] good_keys keys which validated the packet 427 * \return status 428 * 429 */ 430ldns_status ldns_pkt_verify_time(ldns_pkt *p, ldns_rr_type t, ldns_rdf *o, ldns_rr_list *k, ldns_rr_list *s, time_t check_time, ldns_rr_list *good_keys); 431 432#endif 433 434/** 435 * chains nsec3 list 436 */ 437ldns_status 438ldns_dnssec_chain_nsec3_list(ldns_rr_list *nsec3_rrs); 439 440/** 441 * compare for nsec3 sort 442 */ 443int 444qsort_rr_compare_nsec3(const void *a, const void *b); 445 446/** 447 * sort nsec3 list 448 */ 449void 450ldns_rr_list_sort_nsec3(ldns_rr_list *unsorted); 451 452/** 453 * Default callback function to always leave present signatures, and 454 * add new ones 455 * \param[in] sig The signature to check for removal (unused) 456 * \param[in] n Optional argument (unused) 457 * \return LDNS_SIGNATURE_LEAVE_ADD_NEW 458 */ 459int ldns_dnssec_default_add_to_signatures(ldns_rr *sig, void *n); 460/** 461 * Default callback function to always leave present signatures, and 462 * add no new ones for the keys of these signatures 463 * \param[in] sig The signature to check for removal (unused) 464 * \param[in] n Optional argument (unused) 465 * \return LDNS_SIGNATURE_LEAVE_NO_ADD 466 */ 467int ldns_dnssec_default_leave_signatures(ldns_rr *sig, void *n); 468/** 469 * Default callback function to always remove present signatures, but 470 * add no new ones 471 * \param[in] sig The signature to check for removal (unused) 472 * \param[in] n Optional argument (unused) 473 * \return LDNS_SIGNATURE_REMOVE_NO_ADD 474 */ 475int ldns_dnssec_default_delete_signatures(ldns_rr *sig, void *n); 476/** 477 * Default callback function to always leave present signatures, and 478 * add new ones 479 * \param[in] sig The signature to check for removal (unused) 480 * \param[in] n Optional argument (unused) 481 * \return LDNS_SIGNATURE_REMOVE_ADD_NEW 482 */ 483int ldns_dnssec_default_replace_signatures(ldns_rr *sig, void *n); 484 485#if LDNS_BUILD_CONFIG_HAVE_SSL 486/** 487 * Converts the DSA signature from ASN1 representation (RFC2459, as 488 * used by OpenSSL) to raw signature data as used in DNS (rfc2536) 489 * 490 * \param[in] sig The signature in RFC2459 format 491 * \param[in] sig_len The length of the signature 492 * \return a new rdf with the signature 493 */ 494ldns_rdf * 495ldns_convert_dsa_rrsig_asn12rdf(const ldns_buffer *sig, 496 const long sig_len); 497 498/** 499 * Converts the RRSIG signature RDF (in rfc2536 format) to a buffer 500 * with the signature in rfc2459 format 501 * 502 * \param[out] target_buffer buffer to place the signature data 503 * \param[in] sig_rdf The signature rdf to convert 504 * \return LDNS_STATUS_OK on success, error code otherwise 505 */ 506ldns_status 507ldns_convert_dsa_rrsig_rdf2asn1(ldns_buffer *target_buffer, 508 const ldns_rdf *sig_rdf); 509 510/** 511 * Converts the ECDSA signature from ASN1 representation (as 512 * used by OpenSSL) to raw signature data as used in DNS 513 * This routine is only present if ldns is compiled with ecdsa support. 514 * 515 * \param[in] sig The signature in ASN1 format 516 * \param[in] sig_len The length of the signature 517 * \return a new rdf with the signature 518 */ 519ldns_rdf * 520ldns_convert_ecdsa_rrsig_asn12rdf(const ldns_buffer *sig, const long sig_len); 521 522/** 523 * Converts the RRSIG signature RDF (from DNS) to a buffer with the 524 * signature in ASN1 format as openssl uses it. 525 * This routine is only present if ldns is compiled with ecdsa support. 526 * 527 * \param[out] target_buffer buffer to place the signature data in ASN1. 528 * \param[in] sig_rdf The signature rdf to convert 529 * \return LDNS_STATUS_OK on success, error code otherwise 530 */ 531ldns_status 532ldns_convert_ecdsa_rrsig_rdf2asn1(ldns_buffer *target_buffer, 533 const ldns_rdf *sig_rdf); 534 535#endif /* LDNS_BUILD_CONFIG_HAVE_SSL */ 536 537#ifdef __cplusplus 538} 539#endif 540 541#endif /* LDNS_DNSSEC_H */ 542