7 * a Net::DNS like library for C
9 * (c) NLnet Labs, 2005-2006
11 * See the file LICENSE for the license
17 * Addendum to \ref dnssec.h, this module contains key and algorithm definitions and functions.
24 #include <ldns/common.h>
25 #if LDNS_BUILD_CONFIG_HAVE_SSL
26 #include <openssl/ssl.h>
27 #endif /* LDNS_BUILD_CONFIG_HAVE_SSL */
28 #include <ldns/util.h>
35 extern ldns_lookup_table ldns_signing_algorithms[];
37 #define LDNS_KEY_ZONE_KEY 0x0100 /* rfc 4034 */
38 #define LDNS_KEY_SEP_KEY 0x0001 /* rfc 4034 */
39 #define LDNS_KEY_REVOKE_KEY 0x0080 /* rfc 5011 */
42 * Algorithms used in dns
44 enum ldns_enum_algorithm
46 LDNS_RSAMD5 = 1, /* RFC 4034,4035 */
52 LDNS_RSASHA1_NSEC3 = 7,
53 LDNS_RSASHA256 = 8, /* RFC 5702 */
54 LDNS_RSASHA512 = 10, /* RFC 5702 */
55 LDNS_ECC_GOST = 12, /* RFC 5933 */
56 LDNS_ECDSAP256SHA256 = 13, /* RFC 6605 */
57 LDNS_ECDSAP384SHA384 = 14, /* RFC 6605 */
59 LDNS_PRIVATEDNS = 253,
62 typedef enum ldns_enum_algorithm ldns_algorithm;
65 * Hashing algorithms used in the DS record
69 LDNS_SHA1 = 1, /* RFC 4034 */
70 LDNS_SHA256 = 2, /* RFC 4509 */
71 LDNS_HASH_GOST = 3, /* RFC 5933 */
72 LDNS_SHA384 = 4 /* RFC 6605 */
74 typedef enum ldns_enum_hash ldns_hash;
77 * Algorithms used in dns for signing
79 enum ldns_enum_signing_algorithm
81 LDNS_SIGN_RSAMD5 = LDNS_RSAMD5,
82 LDNS_SIGN_RSASHA1 = LDNS_RSASHA1,
83 LDNS_SIGN_DSA = LDNS_DSA,
84 LDNS_SIGN_RSASHA1_NSEC3 = LDNS_RSASHA1_NSEC3,
85 LDNS_SIGN_RSASHA256 = LDNS_RSASHA256,
86 LDNS_SIGN_RSASHA512 = LDNS_RSASHA512,
87 LDNS_SIGN_DSA_NSEC3 = LDNS_DSA_NSEC3,
88 LDNS_SIGN_ECC_GOST = LDNS_ECC_GOST,
89 LDNS_SIGN_ECDSAP256SHA256 = LDNS_ECDSAP256SHA256,
90 LDNS_SIGN_ECDSAP384SHA384 = LDNS_ECDSAP384SHA384,
91 LDNS_SIGN_HMACMD5 = 157, /* not official! This type is for TSIG, not DNSSEC */
92 LDNS_SIGN_HMACSHA1 = 158, /* not official! This type is for TSIG, not DNSSEC */
93 LDNS_SIGN_HMACSHA256 = 159 /* ditto */
95 typedef enum ldns_enum_signing_algorithm ldns_signing_algorithm;
98 * General key structure, can contain all types of keys that
99 * are used in DNSSEC. Mostly used to store private keys, since
100 * public keys can also be stored in a \ref ldns_rr with type
101 * \ref LDNS_RR_TYPE_DNSKEY.
103 * This structure can also store some variables that influence the
104 * signatures generated by signing with this key, for instance the
107 struct ldns_struct_key {
108 ldns_signing_algorithm _alg;
109 /** Whether to use this key when signing */
111 /** Storage pointers for the types of keys supported */
112 /* TODO remove unions? */
114 #if LDNS_BUILD_CONFIG_HAVE_SSL
116 /* The key can be an OpenSSL EVP Key
120 #endif /* LDNS_BUILD_CONFIG_HAVE_SSL */
122 * The key can be an HMAC key
128 /** the key structure can also just point to some external
133 /** Depending on the key we can have extra data */
135 /** Some values that influence generated signatures */
137 /** The TTL of the rrset that is currently signed */
139 /** The inception date of signatures made with this key. */
141 /** The expiration date of signatures made with this key. */
143 /** The keytag of this key. */
145 /** The dnssec key flags as specified in RFC4035, like ZSK and KSK */
149 /** Owner name of the key */
150 ldns_rdf *_pubkey_owner;
152 typedef struct ldns_struct_key ldns_key;
155 * Same as rr_list, but now for keys
157 struct ldns_struct_key_list
162 typedef struct ldns_struct_key_list ldns_key_list;
166 * Creates a new empty key list
167 * \return a new ldns_key_list structure pointer
169 ldns_key_list *ldns_key_list_new(void);
172 * Creates a new empty key structure
173 * \return a new ldns_key * structure
175 ldns_key *ldns_key_new(void);
178 * Creates a new key based on the algorithm
180 * \param[in] a The algorithm to use
181 * \param[in] size the number of bytes for the keysize
182 * \return a new ldns_key structure with the key
184 ldns_key *ldns_key_new_frm_algorithm(ldns_signing_algorithm a, uint16_t size);
187 * Creates a new priv key based on the
188 * contents of the file pointed by fp.
190 * The file should be in Private-key-format v1.x.
192 * \param[out] k the new ldns_key structure
193 * \param[in] fp the file pointer to use
194 * \return an error or LDNS_STATUS_OK
196 ldns_status ldns_key_new_frm_fp(ldns_key **k, FILE *fp);
199 * Creates a new private key based on the
200 * contents of the file pointed by fp
202 * The file should be in Private-key-format v1.x.
204 * \param[out] k the new ldns_key structure
205 * \param[in] fp the file pointer to use
206 * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
207 * \return an error or LDNS_STATUS_OK
209 ldns_status ldns_key_new_frm_fp_l(ldns_key **k, FILE *fp, int *line_nr);
211 #if LDNS_BUILD_CONFIG_HAVE_SSL
213 * Read the key with the given id from the given engine and store it
214 * in the given ldns_key structure. The algorithm type is set
216 ldns_status ldns_key_new_frm_engine(ldns_key **key, ENGINE *e, char *key_id, ldns_algorithm);
220 * frm_fp helper function. This function parses the
221 * remainder of the (RSA) priv. key file generated from bind9
222 * \param[in] fp the file to parse
223 * \return NULL on failure otherwise a RSA structure
225 RSA *ldns_key_new_frm_fp_rsa(FILE *fp);
226 #endif /* LDNS_BUILD_CONFIG_HAVE_SSL */
228 #if LDNS_BUILD_CONFIG_HAVE_SSL
230 * frm_fp helper function. This function parses the
231 * remainder of the (RSA) priv. key file generated from bind9
232 * \param[in] fp the file to parse
233 * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
234 * \return NULL on failure otherwise a RSA structure
236 RSA *ldns_key_new_frm_fp_rsa_l(FILE *fp, int *line_nr);
237 #endif /* LDNS_BUILD_CONFIG_HAVE_SSL */
239 #if LDNS_BUILD_CONFIG_HAVE_SSL
241 * frm_fp helper function. This function parses the
242 * remainder of the (DSA) priv. key file
243 * \param[in] fp the file to parse
244 * \return NULL on failure otherwise a RSA structure
246 DSA *ldns_key_new_frm_fp_dsa(FILE *fp);
247 #endif /* LDNS_BUILD_CONFIG_HAVE_SSL */
249 #if LDNS_BUILD_CONFIG_HAVE_SSL
251 * frm_fp helper function. This function parses the
252 * remainder of the (DSA) priv. key file
253 * \param[in] fp the file to parse
254 * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
255 * \return NULL on failure otherwise a RSA structure
257 DSA *ldns_key_new_frm_fp_dsa_l(FILE *fp, int *line_nr);
258 #endif /* LDNS_BUILD_CONFIG_HAVE_SSL */
260 #if LDNS_BUILD_CONFIG_HAVE_SSL
262 * frm_fp helper function. This function parses the
263 * remainder of the (HMAC-MD5) key file
264 * This function allocated a buffer that needs to be freed
265 * \param[in] fp the file to parse
266 * \param[out] hmac_size the number of bits in the resulting buffer
267 * \return NULL on failure otherwise a newly allocated char buffer
269 unsigned char *ldns_key_new_frm_fp_hmac(FILE *fp, size_t *hmac_size);
272 #if LDNS_BUILD_CONFIG_HAVE_SSL
274 * frm_fp helper function. This function parses the
275 * remainder of the (HMAC-MD5) key file
276 * This function allocated a buffer that needs to be freed
277 * \param[in] fp the file to parse
278 * \param[in] line_nr pointer to an integer containing the current line number (for error reporting purposes)
279 * \param[out] hmac_size the number of bits in the resulting buffer
280 * \return NULL on failure otherwise a newly allocated char buffer
282 unsigned char *ldns_key_new_frm_fp_hmac_l(FILE *fp, int *line_nr, size_t *hmac_size);
283 #endif /* LDNS_BUILD_CONFIG_HAVE_SSL */
285 /* acces write functions */
287 * Set the key's algorithm
288 * \param[in] k the key
289 * \param[in] l the algorithm
291 void ldns_key_set_algorithm(ldns_key *k, ldns_signing_algorithm l);
292 #if LDNS_BUILD_CONFIG_HAVE_SSL
294 * Set the key's evp key
295 * \param[in] k the key
296 * \param[in] e the evp key
298 void ldns_key_set_evp_key(ldns_key *k, EVP_PKEY *e);
301 * Set the key's rsa data.
302 * The rsa data should be freed by the user.
303 * \param[in] k the key
304 * \param[in] r the rsa data
306 void ldns_key_set_rsa_key(ldns_key *k, RSA *r);
309 * Set the key's dsa data
310 * The dsa data should be freed by the user.
311 * \param[in] k the key
312 * \param[in] d the dsa data
314 void ldns_key_set_dsa_key(ldns_key *k, DSA *d);
317 * Assign the key's rsa data
318 * The rsa data will be freed automatically when the key is freed.
319 * \param[in] k the key
320 * \param[in] r the rsa data
322 void ldns_key_assign_rsa_key(ldns_key *k, RSA *r);
325 * Assign the key's dsa data
326 * The dsa data will be freed automatically when the key is freed.
327 * \param[in] k the key
328 * \param[in] d the dsa data
330 void ldns_key_assign_dsa_key(ldns_key *k, DSA *d);
333 * Get the PKEY id for GOST, loads GOST into openssl as a side effect.
334 * Only available if GOST is compiled into the library and openssl.
335 * \return the gost id for EVP_CTX creation.
337 int ldns_key_EVP_load_gost_id(void);
339 /** Release the engine reference held for the GOST engine. */
340 void ldns_key_EVP_unload_gost(void);
341 #endif /* LDNS_BUILD_CONFIG_HAVE_SSL */
344 * Set the key's hmac data
345 * \param[in] k the key
346 * \param[in] hmac the raw key data
348 void ldns_key_set_hmac_key(ldns_key *k, unsigned char *hmac);
351 * Set the key id data. This is used if the key points to
352 * some externally stored key data
354 * Only the pointer is set, the data there is not copied,
355 * and must be freed manually; ldns_key_deep_free() does
356 * *not* free this data
357 * \param[in] key the key
358 * \param[in] external_key key id data
360 void ldns_key_set_external_key(ldns_key *key, void *external_key);
363 * Set the key's hmac size
364 * \param[in] k the key
365 * \param[in] hmac_size the size of the hmac data
367 void ldns_key_set_hmac_size(ldns_key *k, size_t hmac_size);
369 * Set the key's original ttl
370 * \param[in] k the key
371 * \param[in] t the ttl
373 void ldns_key_set_origttl(ldns_key *k, uint32_t t);
375 * Set the key's inception date (seconds after epoch)
376 * \param[in] k the key
377 * \param[in] i the inception
379 void ldns_key_set_inception(ldns_key *k, uint32_t i);
381 * Set the key's expiration date (seconds after epoch)
382 * \param[in] k the key
383 * \param[in] e the expiration
385 void ldns_key_set_expiration(ldns_key *k, uint32_t e);
387 * Set the key's pubkey owner
388 * \param[in] k the key
389 * \param[in] r the owner
391 void ldns_key_set_pubkey_owner(ldns_key *k, ldns_rdf *r);
393 * Set the key's key tag
394 * \param[in] k the key
395 * \param[in] tag the keytag
397 void ldns_key_set_keytag(ldns_key *k, uint16_t tag);
399 * Set the key's flags
400 * \param[in] k the key
401 * \param[in] flags the flags
403 void ldns_key_set_flags(ldns_key *k, uint16_t flags);
405 * Set the keylist's key count to count
406 * \param[in] key the key
407 * \param[in] count the cuont
409 void ldns_key_list_set_key_count(ldns_key_list *key, size_t count);
412 * pushes a key to a keylist
413 * \param[in] key_list the key_list to push to
414 * \param[in] key the key to push
415 * \return false on error, otherwise true
417 bool ldns_key_list_push_key(ldns_key_list *key_list, ldns_key *key);
420 * returns the number of keys in the key list
421 * \param[in] key_list the key_list
422 * \return the numbers of keys in the list
424 size_t ldns_key_list_key_count(const ldns_key_list *key_list);
427 * returns a pointer to the key in the list at the given position
428 * \param[in] key the key
429 * \param[in] nr the position in the list
432 ldns_key *ldns_key_list_key(const ldns_key_list *key, size_t nr);
434 #if LDNS_BUILD_CONFIG_HAVE_SSL
436 * returns the (openssl) RSA struct contained in the key
437 * \param[in] k the key to look in
438 * \return the RSA * structure in the key
440 RSA *ldns_key_rsa_key(const ldns_key *k);
442 * returns the (openssl) EVP struct contained in the key
443 * \param[in] k the key to look in
444 * \return the RSA * structure in the key
446 EVP_PKEY *ldns_key_evp_key(const ldns_key *k);
447 #endif /* LDNS_BUILD_CONFIG_HAVE_SSL */
450 * returns the (openssl) DSA struct contained in the key
452 #if LDNS_BUILD_CONFIG_HAVE_SSL
453 DSA *ldns_key_dsa_key(const ldns_key *k);
454 #endif /* LDNS_BUILD_CONFIG_HAVE_SSL */
457 * return the signing alg of the key
458 * \param[in] k the key
459 * \return the algorithm
461 ldns_signing_algorithm ldns_key_algorithm(const ldns_key *k);
464 * \param[in] k the key
465 * \param[in] v the boolean value to set the _use field to
467 void ldns_key_set_use(ldns_key *k, bool v);
469 * return the use flag
470 * \param[in] k the key
471 * \return the boolean value of the _use field
473 bool ldns_key_use(const ldns_key *k);
475 * return the hmac key data
476 * \param[in] k the key
477 * \return the hmac key data
479 unsigned char *ldns_key_hmac_key(const ldns_key *k);
481 * return the key id key data
482 * \param[in] k the key
483 * \return the key id data
485 void *ldns_key_external_key(const ldns_key *k);
487 * return the hmac key size
488 * \param[in] k the key
489 * \return the hmac key size
491 size_t ldns_key_hmac_size(const ldns_key *k);
493 * return the original ttl of the key
494 * \param[in] k the key
495 * \return the original ttl
497 uint32_t ldns_key_origttl(const ldns_key *k);
499 * return the key's inception date
500 * \param[in] k the key
501 * \return the inception date
503 uint32_t ldns_key_inception(const ldns_key *k);
505 * return the key's expiration date
506 * \param[in] k the key
507 * \return the experiration date
509 uint32_t ldns_key_expiration(const ldns_key *k);
512 * \param[in] k the key
515 uint16_t ldns_key_keytag(const ldns_key *k);
517 * return the public key's owner
518 * \param[in] k the key
521 ldns_rdf *ldns_key_pubkey_owner(const ldns_key *k);
523 * Set the 'use' flag for all keys in the list
524 * \param[in] keys The key_list
525 * \param[in] v The value to set the use flags to
528 ldns_key_list_set_use(ldns_key_list *keys, bool v);
531 * return the flag of the key
532 * \param[in] k the key
535 uint16_t ldns_key_flags(const ldns_key *k);
538 * pops the last rr from a keylist
539 * \param[in] key_list the rr_list to pop from
540 * \return NULL if nothing to pop. Otherwise the popped RR
542 ldns_key *ldns_key_list_pop_key(ldns_key_list *key_list);
545 * converts a ldns_key to a public key rr
546 * If the key data exists at an external point, the corresponding
547 * rdata field must still be added with ldns_rr_rdf_push() to the
548 * result rr of this function
550 * \param[in] k the ldns_key to convert
551 * \return ldns_rr representation of the key
553 ldns_rr *ldns_key2rr(const ldns_key *k);
556 * print a private key to the file ouput
558 * \param[in] output the FILE descriptor where to print to
559 * \param[in] k the ldns_key to print
561 void ldns_key_print(FILE *output, const ldns_key *k);
564 * frees a key structure, but not its internal data structures
566 * \param[in] key the key object to free
568 void ldns_key_free(ldns_key *key);
571 * frees a key structure and all its internal data structures, except
572 * the data set by ldns_key_set_external_key()
574 * \param[in] key the key object to free
576 void ldns_key_deep_free(ldns_key *key);
579 * Frees a key list structure
580 * \param[in] key_list the key list object to free
582 void ldns_key_list_free(ldns_key_list *key_list);
585 * Instantiates a DNSKEY or DS RR from file.
586 * \param[in] filename the file to read the record from
587 * \return the corresponding RR, or NULL if the parsing failed
589 ldns_rr * ldns_read_anchor_file(const char *filename);
592 * Returns the 'default base name' for key files;
593 * IE. K\<zone\>+\<alg\>+\<keytag\>
594 * (without the .key or .private)
595 * The memory for this is allocated by this function,
596 * and should be freed by the caller
598 * \param[in] key the key to get the file name from
599 * \returns A string containing the file base name
601 char *ldns_key_get_file_base_name(ldns_key *key);
604 * See if a key algorithm is supported
605 * \param[in] algo the signing algorithm number.
606 * \returns true if supported.
608 int ldns_key_algo_supported(int algo);
611 * Get signing algorithm by name. Comparison is case insensitive.
612 * \param[in] name string with the name.
613 * \returns 0 on parse failure or the algorithm number.
615 ldns_signing_algorithm ldns_get_signing_algorithm_by_name(const char* name);
621 #endif /* LDNS_KEYS_H */