2 * Copyright (C) 2004-2010 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2002 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: dst.h,v 1.27 2010/12/09 04:31:57 tbox Exp $ */
23 /*! \file dst/dst.h */
26 #include <isc/stdtime.h>
28 #include <dns/types.h>
30 #include <dns/secalg.h>
32 #include <dst/gssapi.h>
41 * The dst_key structure is opaque. Applications should use the accessor
42 * functions provided to retrieve key attributes. If an application needs
43 * to set attributes, new accessor functions will be written.
46 typedef struct dst_key dst_key_t;
47 typedef struct dst_context dst_context_t;
49 /* DST algorithm codes */
50 #define DST_ALG_UNKNOWN 0
51 #define DST_ALG_RSAMD5 1
52 #define DST_ALG_RSA DST_ALG_RSAMD5 /*%< backwards compatibility */
56 #define DST_ALG_RSASHA1 5
57 #define DST_ALG_NSEC3DSA 6
58 #define DST_ALG_NSEC3RSASHA1 7
59 #define DST_ALG_RSASHA256 8
60 #define DST_ALG_RSASHA512 10
61 #define DST_ALG_HMACMD5 157
62 #define DST_ALG_GSSAPI 160
63 #define DST_ALG_HMACSHA1 161 /* XXXMPA */
64 #define DST_ALG_HMACSHA224 162 /* XXXMPA */
65 #define DST_ALG_HMACSHA256 163 /* XXXMPA */
66 #define DST_ALG_HMACSHA384 164 /* XXXMPA */
67 #define DST_ALG_HMACSHA512 165 /* XXXMPA */
68 #define DST_ALG_PRIVATE 254
69 #define DST_ALG_EXPAND 255
70 #define DST_MAX_ALGS 255
72 /*% A buffer of this size is large enough to hold any key */
73 #define DST_KEY_MAXSIZE 1280
76 * A buffer of this size is large enough to hold the textual representation
79 #define DST_KEY_MAXTEXTSIZE 2048
81 /*% 'Type' for dst_read_key() */
82 #define DST_TYPE_KEY 0x1000000 /* KEY key */
83 #define DST_TYPE_PRIVATE 0x2000000
84 #define DST_TYPE_PUBLIC 0x4000000
86 /* Key timing metadata definitions */
87 #define DST_TIME_CREATED 0
88 #define DST_TIME_PUBLISH 1
89 #define DST_TIME_ACTIVATE 2
90 #define DST_TIME_REVOKE 3
91 #define DST_TIME_INACTIVE 4
92 #define DST_TIME_DELETE 5
93 #define DST_TIME_DSPUBLISH 6
94 #define DST_MAX_TIMES 6
96 /* Numeric metadata definitions */
97 #define DST_NUM_PREDECESSOR 0
98 #define DST_NUM_SUCCESSOR 1
99 #define DST_NUM_MAXTTL 2
100 #define DST_NUM_ROLLPERIOD 3
101 #define DST_MAX_NUMERIC 3
104 * Current format version number of the private key parser.
106 * When parsing a key file with the same major number but a higher minor
107 * number, the key parser will ignore any fields it does not recognize.
108 * Thus, DST_MINOR_VERSION should be incremented whenever new
109 * fields are added to the private key file (such as new metadata).
111 * When rewriting these keys, those fields will be dropped, and the
112 * format version set back to the current one..
114 * When a key is seen with a higher major number, the key parser will
115 * reject it as invalid. Thus, DST_MAJOR_VERSION should be incremented
116 * and DST_MINOR_VERSION set to zero whenever there is a format change
117 * which is not backward compatible to previous versions of the dst_key
118 * parser, such as change in the syntax of an existing field, the removal
119 * of a currently mandatory field, or a new field added which would
120 * alter the functioning of the key if it were absent.
122 #define DST_MAJOR_VERSION 1
123 #define DST_MINOR_VERSION 3
130 dst_lib_init(isc_mem_t *mctx, isc_entropy_t *ectx, unsigned int eflags);
133 dst_lib_init2(isc_mem_t *mctx, isc_entropy_t *ectx,
134 const char *engine, unsigned int eflags);
136 * Initializes the DST subsystem.
139 * \li "mctx" is a valid memory context
140 * \li "ectx" is a valid entropy context
148 * \li DST is properly initialized.
152 dst_lib_destroy(void);
154 * Releases all resources allocated by DST.
158 dst_algorithm_supported(unsigned int alg);
160 * Checks that a given algorithm is supported by DST.
168 dst_context_create(dst_key_t *key, isc_mem_t *mctx, dst_context_t **dctxp);
170 * Creates a context to be used for a sign or verify operation.
173 * \li "key" is a valid key.
174 * \li "mctx" is a valid memory context.
175 * \li dctxp != NULL && *dctxp == NULL
182 * \li *dctxp will contain a usable context.
186 dst_context_destroy(dst_context_t **dctxp);
188 * Destroys all memory associated with a context.
191 * \li *dctxp != NULL && *dctxp == NULL
198 dst_context_adddata(dst_context_t *dctx, const isc_region_t *data);
200 * Incrementally adds data to the context to be used in a sign or verify
204 * \li "dctx" is a valid context
205 * \li "data" is a valid region
209 * \li DST_R_SIGNFAILURE
210 * \li all other errors indicate failure
214 dst_context_sign(dst_context_t *dctx, isc_buffer_t *sig);
216 * Computes a signature using the data and key stored in the context.
219 * \li "dctx" is a valid context.
220 * \li "sig" is a valid buffer.
224 * \li DST_R_VERIFYFAILURE
225 * \li all other errors indicate failure
228 * \li "sig" will contain the signature
232 dst_context_verify(dst_context_t *dctx, isc_region_t *sig);
234 * Verifies the signature using the data and key stored in the context.
237 * \li "dctx" is a valid context.
238 * \li "sig" is a valid region.
242 * \li all other errors indicate failure
245 * \li "sig" will contain the signature
249 dst_key_computesecret(const dst_key_t *pub, const dst_key_t *priv,
250 isc_buffer_t *secret);
252 * Computes a shared secret from two (Diffie-Hellman) keys.
255 * \li "pub" is a valid key that can be used to derive a shared secret
256 * \li "priv" is a valid private key that can be used to derive a shared secret
257 * \li "secret" is a valid buffer
261 * \li any other result indicates failure
264 * \li If successful, secret will contain the derived shared secret.
268 dst_key_fromfile(dns_name_t *name, dns_keytag_t id, unsigned int alg, int type,
269 const char *directory, isc_mem_t *mctx, dst_key_t **keyp);
271 * Reads a key from permanent storage. The key can either be a public or
272 * private key, and is specified by name, algorithm, and id. If a private key
273 * is specified, the public key must also be present. If directory is NULL,
274 * the current directory is assumed.
277 * \li "name" is a valid absolute dns name.
278 * \li "id" is a valid key tag identifier.
279 * \li "alg" is a supported key algorithm.
280 * \li "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union.
281 * DST_TYPE_KEY look for a KEY record otherwise DNSKEY
282 * \li "mctx" is a valid memory context.
283 * \li "keyp" is not NULL and "*keyp" is NULL.
287 * \li any other result indicates failure
290 * \li If successful, *keyp will contain a valid key.
294 dst_key_fromnamedfile(const char *filename, const char *dirname,
295 int type, isc_mem_t *mctx, dst_key_t **keyp);
297 * Reads a key from permanent storage. The key can either be a public or
298 * key, and is specified by filename. If a private key is specified, the
299 * public key must also be present.
301 * If 'dirname' is not NULL, and 'filename' is a relative path,
302 * then the file is looked up relative to the given directory.
303 * If 'filename' is an absolute path, 'dirname' is ignored.
306 * \li "filename" is not NULL
307 * \li "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union
308 * DST_TYPE_KEY look for a KEY record otherwise DNSKEY
309 * \li "mctx" is a valid memory context
310 * \li "keyp" is not NULL and "*keyp" is NULL.
314 * \li any other result indicates failure
317 * \li If successful, *keyp will contain a valid key.
322 dst_key_read_public(const char *filename, int type,
323 isc_mem_t *mctx, dst_key_t **keyp);
325 * Reads a public key from permanent storage. The key must be a public key.
328 * \li "filename" is not NULL
329 * \li "type" is DST_TYPE_KEY look for a KEY record otherwise DNSKEY
330 * \li "mctx" is a valid memory context
331 * \li "keyp" is not NULL and "*keyp" is NULL.
335 * \li DST_R_BADKEYTYPE if the key type is not the expected one
336 * \li ISC_R_UNEXPECTEDTOKEN if the file can not be parsed as a public key
337 * \li any other result indicates failure
340 * \li If successful, *keyp will contain a valid key.
344 dst_key_tofile(const dst_key_t *key, int type, const char *directory);
346 * Writes a key to permanent storage. The key can either be a public or
347 * private key. Public keys are written in DNS format and private keys
348 * are written as a set of base64 encoded values. If directory is NULL,
349 * the current directory is assumed.
352 * \li "key" is a valid key.
353 * \li "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union
357 * \li any other result indicates failure
361 dst_key_fromdns(dns_name_t *name, dns_rdataclass_t rdclass,
362 isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp);
364 * Converts a DNS KEY record into a DST key.
367 * \li "name" is a valid absolute dns name.
368 * \li "source" is a valid buffer. There must be at least 4 bytes available.
369 * \li "mctx" is a valid memory context.
370 * \li "keyp" is not NULL and "*keyp" is NULL.
374 * \li any other result indicates failure
377 * \li If successful, *keyp will contain a valid key, and the consumed
378 * pointer in data will be advanced.
382 dst_key_todns(const dst_key_t *key, isc_buffer_t *target);
384 * Converts a DST key into a DNS KEY record.
387 * \li "key" is a valid key.
388 * \li "target" is a valid buffer. There must be at least 4 bytes unused.
392 * \li any other result indicates failure
395 * \li If successful, the used pointer in 'target' is advanced by at least 4.
399 dst_key_frombuffer(dns_name_t *name, unsigned int alg,
400 unsigned int flags, unsigned int protocol,
401 dns_rdataclass_t rdclass,
402 isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp);
404 * Converts a buffer containing DNS KEY RDATA into a DST key.
407 *\li "name" is a valid absolute dns name.
408 *\li "alg" is a supported key algorithm.
409 *\li "source" is a valid buffer.
410 *\li "mctx" is a valid memory context.
411 *\li "keyp" is not NULL and "*keyp" is NULL.
415 * \li any other result indicates failure
418 *\li If successful, *keyp will contain a valid key, and the consumed
419 * pointer in source will be advanced.
423 dst_key_tobuffer(const dst_key_t *key, isc_buffer_t *target);
425 * Converts a DST key into DNS KEY RDATA format.
428 *\li "key" is a valid key.
429 *\li "target" is a valid buffer.
433 * \li any other result indicates failure
436 *\li If successful, the used pointer in 'target' is advanced.
440 dst_key_privatefrombuffer(dst_key_t *key, isc_buffer_t *buffer);
442 * Converts a public key into a private key, reading the private key
443 * information from the buffer. The buffer should contain the same data
444 * as the .private key file would.
447 *\li "key" is a valid public key.
448 *\li "buffer" is not NULL.
452 * \li any other result indicates failure
455 *\li If successful, key will contain a valid private key.
459 dst_key_getgssctx(const dst_key_t *key);
461 * Returns the opaque key data.
462 * Be cautions when using this value unless you know what you are doing.
465 *\li "key" is not NULL.
468 *\li gssctx key data, possibly NULL.
472 dst_key_fromgssapi(dns_name_t *name, gss_ctx_id_t gssctx, isc_mem_t *mctx,
475 * Converts a GSSAPI opaque context id into a DST key.
478 *\li "name" is a valid absolute dns name.
479 *\li "gssctx" is a GSSAPI context id.
480 *\li "mctx" is a valid memory context.
481 *\li "keyp" is not NULL and "*keyp" is NULL.
485 * \li any other result indicates failure
488 *\li If successful, *keyp will contain a valid key and be responsible for
493 dst_key_fromlabel(dns_name_t *name, int alg, unsigned int flags,
494 unsigned int protocol, dns_rdataclass_t rdclass,
495 const char *engine, const char *label, const char *pin,
496 isc_mem_t *mctx, dst_key_t **keyp);
499 dst_key_generate(dns_name_t *name, unsigned int alg,
500 unsigned int bits, unsigned int param,
501 unsigned int flags, unsigned int protocol,
502 dns_rdataclass_t rdclass,
503 isc_mem_t *mctx, dst_key_t **keyp);
506 dst_key_generate2(dns_name_t *name, unsigned int alg,
507 unsigned int bits, unsigned int param,
508 unsigned int flags, unsigned int protocol,
509 dns_rdataclass_t rdclass,
510 isc_mem_t *mctx, dst_key_t **keyp,
511 void (*callback)(int));
513 * Generate a DST key (or keypair) with the supplied parameters. The
514 * interpretation of the "param" field depends on the algorithm:
518 * !0 use Fermat4 (2^16 + 1)
520 * 0 default - use well known prime if bits == 768 or 1024,
521 * otherwise use 2 as the generator.
522 * !0 use this value as the generator.
525 * 0 default - require good entropy
526 * !0 lack of good entropy is ok
530 *\li "name" is a valid absolute dns name.
531 *\li "keyp" is not NULL and "*keyp" is NULL.
535 * \li any other result indicates failure
538 *\li If successful, *keyp will contain a valid key.
542 dst_key_compare(const dst_key_t *key1, const dst_key_t *key2);
544 * Compares two DST keys. Returns true if they match, false otherwise.
546 * Keys ARE NOT considered to match if one of them is the revoked version
550 *\li "key1" is a valid key.
551 *\li "key2" is a valid key.
559 dst_key_pubcompare(const dst_key_t *key1, const dst_key_t *key2,
560 isc_boolean_t match_revoked_key);
562 * Compares only the public portions of two DST keys. Returns true
563 * if they match, false otherwise. This allows us, for example, to
564 * determine whether a public key found in a zone matches up with a
565 * key pair found on disk.
567 * If match_revoked_key is TRUE, then keys ARE considered to match if one
568 * of them is the revoked version of the other. Otherwise, they are not.
571 *\li "key1" is a valid key.
572 *\li "key2" is a valid key.
580 dst_key_paramcompare(const dst_key_t *key1, const dst_key_t *key2);
582 * Compares the parameters of two DST keys. This is used to determine if
583 * two (Diffie-Hellman) keys can be used to derive a shared secret.
586 *\li "key1" is a valid key.
587 *\li "key2" is a valid key.
595 dst_key_attach(dst_key_t *source, dst_key_t **target);
597 * Attach to a existing key increasing the reference count.
600 *\li 'source' to be a valid key.
601 *\li 'target' to be non-NULL and '*target' to be NULL.
605 dst_key_free(dst_key_t **keyp);
607 * Decrement the key's reference counter and, when it reaches zero,
608 * release all memory associated with the key.
611 *\li "keyp" is not NULL and "*keyp" is a valid key.
612 *\li reference counter greater than zero.
615 *\li All memory associated with "*keyp" will be freed.
620 * Accessor functions to obtain key fields.
623 *\li "key" is a valid key.
626 dst_key_name(const dst_key_t *key);
629 dst_key_size(const dst_key_t *key);
632 dst_key_proto(const dst_key_t *key);
635 dst_key_alg(const dst_key_t *key);
638 dst_key_flags(const dst_key_t *key);
641 dst_key_id(const dst_key_t *key);
644 dst_key_class(const dst_key_t *key);
647 dst_key_isprivate(const dst_key_t *key);
650 dst_key_iszonekey(const dst_key_t *key);
653 dst_key_isnullkey(const dst_key_t *key);
656 dst_key_buildfilename(const dst_key_t *key, int type,
657 const char *directory, isc_buffer_t *out);
659 * Generates the filename used by dst to store the specified key.
660 * If directory is NULL, the current directory is assumed.
663 *\li "key" is a valid key
664 *\li "type" is either DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or 0 for no suffix.
665 *\li "out" is a valid buffer
668 *\li the file name will be written to "out", and the used pointer will
673 dst_key_sigsize(const dst_key_t *key, unsigned int *n);
675 * Computes the size of a signature generated by the given key.
678 *\li "key" is a valid key.
683 *\li DST_R_UNSUPPORTEDALG
686 *\li "n" stores the size of a generated signature
690 dst_key_secretsize(const dst_key_t *key, unsigned int *n);
692 * Computes the size of a shared secret generated by the given key.
695 *\li "key" is a valid key.
700 *\li DST_R_UNSUPPORTEDALG
703 *\li "n" stores the size of a generated shared secret
707 dst_region_computeid(const isc_region_t *source, unsigned int alg);
709 * Computes the key id of the key stored in the provided region with the
713 *\li "source" contains a valid, non-NULL region.
720 dst_key_getbits(const dst_key_t *key);
722 * Get the number of digest bits required (0 == MAX).
725 * "key" is a valid key.
729 dst_key_setbits(dst_key_t *key, isc_uint16_t bits);
731 * Set the number of digest bits required (0 == MAX).
734 * "key" is a valid key.
738 dst_key_setflags(dst_key_t *key, isc_uint32_t flags);
740 * Set the key flags, and recompute the key ID.
743 * "key" is a valid key.
747 dst_key_getnum(const dst_key_t *key, int type, isc_uint32_t *valuep);
749 * Get a member of the numeric metadata array and place it in '*valuep'.
752 * "key" is a valid key.
753 * "type" is no larger than DST_MAX_NUMERIC
754 * "timep" is not null.
758 dst_key_setnum(dst_key_t *key, int type, isc_uint32_t value);
760 * Set a member of the numeric metadata array.
763 * "key" is a valid key.
764 * "type" is no larger than DST_MAX_NUMERIC
768 dst_key_unsetnum(dst_key_t *key, int type);
770 * Flag a member of the numeric metadata array as "not set".
773 * "key" is a valid key.
774 * "type" is no larger than DST_MAX_NUMERIC
778 dst_key_gettime(const dst_key_t *key, int type, isc_stdtime_t *timep);
780 * Get a member of the timing metadata array and place it in '*timep'.
783 * "key" is a valid key.
784 * "type" is no larger than DST_MAX_TIMES
785 * "timep" is not null.
789 dst_key_settime(dst_key_t *key, int type, isc_stdtime_t when);
791 * Set a member of the timing metadata array.
794 * "key" is a valid key.
795 * "type" is no larger than DST_MAX_TIMES
799 dst_key_unsettime(dst_key_t *key, int type);
801 * Flag a member of the timing metadata array as "not set".
804 * "key" is a valid key.
805 * "type" is no larger than DST_MAX_TIMES
809 dst_key_getprivateformat(const dst_key_t *key, int *majorp, int *minorp);
811 * Get the private key format version number. (If the key does not have
812 * a private key associated with it, the version will be 0.0.) The major
813 * version number is placed in '*majorp', and the minor version number in
817 * "key" is a valid key.
818 * "majorp" is not NULL.
819 * "minorp" is not NULL.
823 dst_key_setprivateformat(dst_key_t *key, int major, int minor);
825 * Set the private key format version number.
828 * "key" is a valid key.
831 #define DST_KEY_FORMATSIZE (DNS_NAME_FORMATSIZE + DNS_SECALG_FORMATSIZE + 7)
834 dst_key_format(dst_key_t *key, char *cp, unsigned int size);
836 * Write the uniquely identifying information about the key (name,
837 * algorithm, key ID) into a string 'cp' of size 'size'.
842 #endif /* DST_DST_H */