2 * Copyright (C) 2004-2011 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.31.10.1 2011-03-21 19:53:35 each 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_ECCGOST 12
62 #define DST_ALG_HMACMD5 157
63 #define DST_ALG_GSSAPI 160
64 #define DST_ALG_HMACSHA1 161 /* XXXMPA */
65 #define DST_ALG_HMACSHA224 162 /* XXXMPA */
66 #define DST_ALG_HMACSHA256 163 /* XXXMPA */
67 #define DST_ALG_HMACSHA384 164 /* XXXMPA */
68 #define DST_ALG_HMACSHA512 165 /* XXXMPA */
69 #define DST_ALG_PRIVATE 254
70 #define DST_ALG_EXPAND 255
71 #define DST_MAX_ALGS 255
73 /*% A buffer of this size is large enough to hold any key */
74 #define DST_KEY_MAXSIZE 1280
77 * A buffer of this size is large enough to hold the textual representation
80 #define DST_KEY_MAXTEXTSIZE 2048
82 /*% 'Type' for dst_read_key() */
83 #define DST_TYPE_KEY 0x1000000 /* KEY key */
84 #define DST_TYPE_PRIVATE 0x2000000
85 #define DST_TYPE_PUBLIC 0x4000000
87 /* Key timing metadata definitions */
88 #define DST_TIME_CREATED 0
89 #define DST_TIME_PUBLISH 1
90 #define DST_TIME_ACTIVATE 2
91 #define DST_TIME_REVOKE 3
92 #define DST_TIME_INACTIVE 4
93 #define DST_TIME_DELETE 5
94 #define DST_TIME_DSPUBLISH 6
95 #define DST_MAX_TIMES 6
97 /* Numeric metadata definitions */
98 #define DST_NUM_PREDECESSOR 0
99 #define DST_NUM_SUCCESSOR 1
100 #define DST_NUM_MAXTTL 2
101 #define DST_NUM_ROLLPERIOD 3
102 #define DST_MAX_NUMERIC 3
105 * Current format version number of the private key parser.
107 * When parsing a key file with the same major number but a higher minor
108 * number, the key parser will ignore any fields it does not recognize.
109 * Thus, DST_MINOR_VERSION should be incremented whenever new
110 * fields are added to the private key file (such as new metadata).
112 * When rewriting these keys, those fields will be dropped, and the
113 * format version set back to the current one..
115 * When a key is seen with a higher major number, the key parser will
116 * reject it as invalid. Thus, DST_MAJOR_VERSION should be incremented
117 * and DST_MINOR_VERSION set to zero whenever there is a format change
118 * which is not backward compatible to previous versions of the dst_key
119 * parser, such as change in the syntax of an existing field, the removal
120 * of a currently mandatory field, or a new field added which would
121 * alter the functioning of the key if it were absent.
123 #define DST_MAJOR_VERSION 1
124 #define DST_MINOR_VERSION 3
131 dst_lib_init(isc_mem_t *mctx, isc_entropy_t *ectx, unsigned int eflags);
134 dst_lib_init2(isc_mem_t *mctx, isc_entropy_t *ectx,
135 const char *engine, unsigned int eflags);
137 * Initializes the DST subsystem.
140 * \li "mctx" is a valid memory context
141 * \li "ectx" is a valid entropy context
149 * \li DST is properly initialized.
153 dst_lib_destroy(void);
155 * Releases all resources allocated by DST.
159 dst_algorithm_supported(unsigned int alg);
161 * Checks that a given algorithm is supported by DST.
169 dst_context_create(dst_key_t *key, isc_mem_t *mctx, dst_context_t **dctxp);
171 * Creates a context to be used for a sign or verify operation.
174 * \li "key" is a valid key.
175 * \li "mctx" is a valid memory context.
176 * \li dctxp != NULL && *dctxp == NULL
183 * \li *dctxp will contain a usable context.
187 dst_context_destroy(dst_context_t **dctxp);
189 * Destroys all memory associated with a context.
192 * \li *dctxp != NULL && *dctxp == NULL
199 dst_context_adddata(dst_context_t *dctx, const isc_region_t *data);
201 * Incrementally adds data to the context to be used in a sign or verify
205 * \li "dctx" is a valid context
206 * \li "data" is a valid region
210 * \li DST_R_SIGNFAILURE
211 * \li all other errors indicate failure
215 dst_context_sign(dst_context_t *dctx, isc_buffer_t *sig);
217 * Computes a signature using the data and key stored in the context.
220 * \li "dctx" is a valid context.
221 * \li "sig" is a valid buffer.
225 * \li DST_R_VERIFYFAILURE
226 * \li all other errors indicate failure
229 * \li "sig" will contain the signature
233 dst_context_verify(dst_context_t *dctx, isc_region_t *sig);
235 * Verifies the signature using the data and key stored in the context.
238 * \li "dctx" is a valid context.
239 * \li "sig" is a valid region.
243 * \li all other errors indicate failure
246 * \li "sig" will contain the signature
250 dst_key_computesecret(const dst_key_t *pub, const dst_key_t *priv,
251 isc_buffer_t *secret);
253 * Computes a shared secret from two (Diffie-Hellman) keys.
256 * \li "pub" is a valid key that can be used to derive a shared secret
257 * \li "priv" is a valid private key that can be used to derive a shared secret
258 * \li "secret" is a valid buffer
262 * \li any other result indicates failure
265 * \li If successful, secret will contain the derived shared secret.
269 dst_key_fromfile(dns_name_t *name, dns_keytag_t id, unsigned int alg, int type,
270 const char *directory, isc_mem_t *mctx, dst_key_t **keyp);
272 * Reads a key from permanent storage. The key can either be a public or
273 * private key, and is specified by name, algorithm, and id. If a private key
274 * is specified, the public key must also be present. If directory is NULL,
275 * the current directory is assumed.
278 * \li "name" is a valid absolute dns name.
279 * \li "id" is a valid key tag identifier.
280 * \li "alg" is a supported key algorithm.
281 * \li "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union.
282 * DST_TYPE_KEY look for a KEY record otherwise DNSKEY
283 * \li "mctx" is a valid memory context.
284 * \li "keyp" is not NULL and "*keyp" is NULL.
288 * \li any other result indicates failure
291 * \li If successful, *keyp will contain a valid key.
295 dst_key_fromnamedfile(const char *filename, const char *dirname,
296 int type, isc_mem_t *mctx, dst_key_t **keyp);
298 * Reads a key from permanent storage. The key can either be a public or
299 * key, and is specified by filename. If a private key is specified, the
300 * public key must also be present.
302 * If 'dirname' is not NULL, and 'filename' is a relative path,
303 * then the file is looked up relative to the given directory.
304 * If 'filename' is an absolute path, 'dirname' is ignored.
307 * \li "filename" is not NULL
308 * \li "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union
309 * DST_TYPE_KEY look for a KEY record otherwise DNSKEY
310 * \li "mctx" is a valid memory context
311 * \li "keyp" is not NULL and "*keyp" is NULL.
315 * \li any other result indicates failure
318 * \li If successful, *keyp will contain a valid key.
323 dst_key_read_public(const char *filename, int type,
324 isc_mem_t *mctx, dst_key_t **keyp);
326 * Reads a public key from permanent storage. The key must be a public key.
329 * \li "filename" is not NULL
330 * \li "type" is DST_TYPE_KEY look for a KEY record otherwise DNSKEY
331 * \li "mctx" is a valid memory context
332 * \li "keyp" is not NULL and "*keyp" is NULL.
336 * \li DST_R_BADKEYTYPE if the key type is not the expected one
337 * \li ISC_R_UNEXPECTEDTOKEN if the file can not be parsed as a public key
338 * \li any other result indicates failure
341 * \li If successful, *keyp will contain a valid key.
345 dst_key_tofile(const dst_key_t *key, int type, const char *directory);
347 * Writes a key to permanent storage. The key can either be a public or
348 * private key. Public keys are written in DNS format and private keys
349 * are written as a set of base64 encoded values. If directory is NULL,
350 * the current directory is assumed.
353 * \li "key" is a valid key.
354 * \li "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union
358 * \li any other result indicates failure
362 dst_key_fromdns(dns_name_t *name, dns_rdataclass_t rdclass,
363 isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp);
365 * Converts a DNS KEY record into a DST key.
368 * \li "name" is a valid absolute dns name.
369 * \li "source" is a valid buffer. There must be at least 4 bytes available.
370 * \li "mctx" is a valid memory context.
371 * \li "keyp" is not NULL and "*keyp" is NULL.
375 * \li any other result indicates failure
378 * \li If successful, *keyp will contain a valid key, and the consumed
379 * pointer in data will be advanced.
383 dst_key_todns(const dst_key_t *key, isc_buffer_t *target);
385 * Converts a DST key into a DNS KEY record.
388 * \li "key" is a valid key.
389 * \li "target" is a valid buffer. There must be at least 4 bytes unused.
393 * \li any other result indicates failure
396 * \li If successful, the used pointer in 'target' is advanced by at least 4.
400 dst_key_frombuffer(dns_name_t *name, unsigned int alg,
401 unsigned int flags, unsigned int protocol,
402 dns_rdataclass_t rdclass,
403 isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp);
405 * Converts a buffer containing DNS KEY RDATA into a DST key.
408 *\li "name" is a valid absolute dns name.
409 *\li "alg" is a supported key algorithm.
410 *\li "source" is a valid buffer.
411 *\li "mctx" is a valid memory context.
412 *\li "keyp" is not NULL and "*keyp" is NULL.
416 * \li any other result indicates failure
419 *\li If successful, *keyp will contain a valid key, and the consumed
420 * pointer in source will be advanced.
424 dst_key_tobuffer(const dst_key_t *key, isc_buffer_t *target);
426 * Converts a DST key into DNS KEY RDATA format.
429 *\li "key" is a valid key.
430 *\li "target" is a valid buffer.
434 * \li any other result indicates failure
437 *\li If successful, the used pointer in 'target' is advanced.
441 dst_key_privatefrombuffer(dst_key_t *key, isc_buffer_t *buffer);
443 * Converts a public key into a private key, reading the private key
444 * information from the buffer. The buffer should contain the same data
445 * as the .private key file would.
448 *\li "key" is a valid public key.
449 *\li "buffer" is not NULL.
453 * \li any other result indicates failure
456 *\li If successful, key will contain a valid private key.
460 dst_key_getgssctx(const dst_key_t *key);
462 * Returns the opaque key data.
463 * Be cautions when using this value unless you know what you are doing.
466 *\li "key" is not NULL.
469 *\li gssctx key data, possibly NULL.
473 dst_key_fromgssapi(dns_name_t *name, gss_ctx_id_t gssctx, isc_mem_t *mctx,
474 dst_key_t **keyp, isc_region_t *intoken);
476 * Converts a GSSAPI opaque context id into a DST key.
479 *\li "name" is a valid absolute dns name.
480 *\li "gssctx" is a GSSAPI context id.
481 *\li "mctx" is a valid memory context.
482 *\li "keyp" is not NULL and "*keyp" is NULL.
486 * \li any other result indicates failure
489 *\li If successful, *keyp will contain a valid key and be responsible for
494 dst_key_fromlabel(dns_name_t *name, int alg, unsigned int flags,
495 unsigned int protocol, dns_rdataclass_t rdclass,
496 const char *engine, const char *label, const char *pin,
497 isc_mem_t *mctx, dst_key_t **keyp);
500 dst_key_generate(dns_name_t *name, unsigned int alg,
501 unsigned int bits, unsigned int param,
502 unsigned int flags, unsigned int protocol,
503 dns_rdataclass_t rdclass,
504 isc_mem_t *mctx, dst_key_t **keyp);
507 dst_key_generate2(dns_name_t *name, unsigned int alg,
508 unsigned int bits, unsigned int param,
509 unsigned int flags, unsigned int protocol,
510 dns_rdataclass_t rdclass,
511 isc_mem_t *mctx, dst_key_t **keyp,
512 void (*callback)(int));
514 * Generate a DST key (or keypair) with the supplied parameters. The
515 * interpretation of the "param" field depends on the algorithm:
519 * !0 use Fermat4 (2^16 + 1)
521 * 0 default - use well known prime if bits == 768 or 1024,
522 * otherwise use 2 as the generator.
523 * !0 use this value as the generator.
526 * 0 default - require good entropy
527 * !0 lack of good entropy is ok
531 *\li "name" is a valid absolute dns name.
532 *\li "keyp" is not NULL and "*keyp" is NULL.
536 * \li any other result indicates failure
539 *\li If successful, *keyp will contain a valid key.
543 dst_key_compare(const dst_key_t *key1, const dst_key_t *key2);
545 * Compares two DST keys. Returns true if they match, false otherwise.
547 * Keys ARE NOT considered to match if one of them is the revoked version
551 *\li "key1" is a valid key.
552 *\li "key2" is a valid key.
560 dst_key_pubcompare(const dst_key_t *key1, const dst_key_t *key2,
561 isc_boolean_t match_revoked_key);
563 * Compares only the public portions of two DST keys. Returns true
564 * if they match, false otherwise. This allows us, for example, to
565 * determine whether a public key found in a zone matches up with a
566 * key pair found on disk.
568 * If match_revoked_key is TRUE, then keys ARE considered to match if one
569 * of them is the revoked version of the other. Otherwise, they are not.
572 *\li "key1" is a valid key.
573 *\li "key2" is a valid key.
581 dst_key_paramcompare(const dst_key_t *key1, const dst_key_t *key2);
583 * Compares the parameters of two DST keys. This is used to determine if
584 * two (Diffie-Hellman) keys can be used to derive a shared secret.
587 *\li "key1" is a valid key.
588 *\li "key2" is a valid key.
596 dst_key_attach(dst_key_t *source, dst_key_t **target);
598 * Attach to a existing key increasing the reference count.
601 *\li 'source' to be a valid key.
602 *\li 'target' to be non-NULL and '*target' to be NULL.
606 dst_key_free(dst_key_t **keyp);
608 * Decrement the key's reference counter and, when it reaches zero,
609 * release all memory associated with the key.
612 *\li "keyp" is not NULL and "*keyp" is a valid key.
613 *\li reference counter greater than zero.
616 *\li All memory associated with "*keyp" will be freed.
621 * Accessor functions to obtain key fields.
624 *\li "key" is a valid key.
627 dst_key_name(const dst_key_t *key);
630 dst_key_size(const dst_key_t *key);
633 dst_key_proto(const dst_key_t *key);
636 dst_key_alg(const dst_key_t *key);
639 dst_key_flags(const dst_key_t *key);
642 dst_key_id(const dst_key_t *key);
645 dst_key_class(const dst_key_t *key);
648 dst_key_isprivate(const dst_key_t *key);
651 dst_key_iszonekey(const dst_key_t *key);
654 dst_key_isnullkey(const dst_key_t *key);
657 dst_key_buildfilename(const dst_key_t *key, int type,
658 const char *directory, isc_buffer_t *out);
660 * Generates the filename used by dst to store the specified key.
661 * If directory is NULL, the current directory is assumed.
664 *\li "key" is a valid key
665 *\li "type" is either DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or 0 for no suffix.
666 *\li "out" is a valid buffer
669 *\li the file name will be written to "out", and the used pointer will
674 dst_key_sigsize(const dst_key_t *key, unsigned int *n);
676 * Computes the size of a signature generated by the given key.
679 *\li "key" is a valid key.
684 *\li DST_R_UNSUPPORTEDALG
687 *\li "n" stores the size of a generated signature
691 dst_key_secretsize(const dst_key_t *key, unsigned int *n);
693 * Computes the size of a shared secret generated by the given key.
696 *\li "key" is a valid key.
701 *\li DST_R_UNSUPPORTEDALG
704 *\li "n" stores the size of a generated shared secret
708 dst_region_computeid(const isc_region_t *source, unsigned int alg);
710 * Computes the key id of the key stored in the provided region with the
714 *\li "source" contains a valid, non-NULL region.
721 dst_key_getbits(const dst_key_t *key);
723 * Get the number of digest bits required (0 == MAX).
726 * "key" is a valid key.
730 dst_key_setbits(dst_key_t *key, isc_uint16_t bits);
732 * Set the number of digest bits required (0 == MAX).
735 * "key" is a valid key.
739 dst_key_setflags(dst_key_t *key, isc_uint32_t flags);
741 * Set the key flags, and recompute the key ID.
744 * "key" is a valid key.
748 dst_key_getnum(const dst_key_t *key, int type, isc_uint32_t *valuep);
750 * Get a member of the numeric metadata array and place it in '*valuep'.
753 * "key" is a valid key.
754 * "type" is no larger than DST_MAX_NUMERIC
755 * "timep" is not null.
759 dst_key_setnum(dst_key_t *key, int type, isc_uint32_t value);
761 * Set a member of the numeric metadata array.
764 * "key" is a valid key.
765 * "type" is no larger than DST_MAX_NUMERIC
769 dst_key_unsetnum(dst_key_t *key, int type);
771 * Flag a member of the numeric metadata array as "not set".
774 * "key" is a valid key.
775 * "type" is no larger than DST_MAX_NUMERIC
779 dst_key_gettime(const dst_key_t *key, int type, isc_stdtime_t *timep);
781 * Get a member of the timing metadata array and place it in '*timep'.
784 * "key" is a valid key.
785 * "type" is no larger than DST_MAX_TIMES
786 * "timep" is not null.
790 dst_key_settime(dst_key_t *key, int type, isc_stdtime_t when);
792 * Set a member of the timing metadata array.
795 * "key" is a valid key.
796 * "type" is no larger than DST_MAX_TIMES
800 dst_key_unsettime(dst_key_t *key, int type);
802 * Flag a member of the timing metadata array as "not set".
805 * "key" is a valid key.
806 * "type" is no larger than DST_MAX_TIMES
810 dst_key_getprivateformat(const dst_key_t *key, int *majorp, int *minorp);
812 * Get the private key format version number. (If the key does not have
813 * a private key associated with it, the version will be 0.0.) The major
814 * version number is placed in '*majorp', and the minor version number in
818 * "key" is a valid key.
819 * "majorp" is not NULL.
820 * "minorp" is not NULL.
824 dst_key_setprivateformat(dst_key_t *key, int major, int minor);
826 * Set the private key format version number.
829 * "key" is a valid key.
832 #define DST_KEY_FORMATSIZE (DNS_NAME_FORMATSIZE + DNS_SECALG_FORMATSIZE + 7)
835 dst_key_format(const dst_key_t *key, char *cp, unsigned int size);
837 * Write the uniquely identifying information about the key (name,
838 * algorithm, key ID) into a string 'cp' of size 'size'.
843 dst_key_tkeytoken(const dst_key_t *key);
845 * Return the token from the TKEY request, if any. If this key was
846 * not negotiated via TKEY, return NULL.
849 * "key" is a valid key.
854 dst_key_dump(dst_key_t *key, isc_mem_t *mctx, char **buffer, int *length);
856 * Allocate 'buffer' and dump the key into it in base64 format. The buffer
857 * is not NUL terminated. The length of the buffer is returned in *length.
859 * 'buffer' needs to be freed using isc_mem_put(mctx, buffer, length);
862 * 'buffer' to be non NULL and *buffer to be NULL.
863 * 'length' to be non NULL and *length to be zero.
868 * ISC_R_NOTIMPLEMENTED
873 dst_key_restore(dns_name_t *name, unsigned int alg, unsigned int flags,
874 unsigned int protocol, dns_rdataclass_t rdclass,
875 isc_mem_t *mctx, const char *keystr, dst_key_t **keyp);
880 #endif /* DST_DST_H */