2 * Copyright (C) 2004-2007 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.9 2007/06/19 23:47:17 tbox Exp $ */
23 /*! \file dst/dst.h */
27 #include <dns/types.h>
29 #include <dst/gssapi.h>
38 * The dst_key structure is opaque. Applications should use the accessor
39 * functions provided to retrieve key attributes. If an application needs
40 * to set attributes, new accessor functions will be written.
43 typedef struct dst_key dst_key_t;
44 typedef struct dst_context dst_context_t;
46 /* DST algorithm codes */
47 #define DST_ALG_UNKNOWN 0
48 #define DST_ALG_RSAMD5 1
49 #define DST_ALG_RSA DST_ALG_RSAMD5 /*%< backwards compatibility */
53 #define DST_ALG_RSASHA1 5
54 #define DST_ALG_HMACMD5 157
55 #define DST_ALG_GSSAPI 160
56 #define DST_ALG_HMACSHA1 161 /* XXXMPA */
57 #define DST_ALG_HMACSHA224 162 /* XXXMPA */
58 #define DST_ALG_HMACSHA256 163 /* XXXMPA */
59 #define DST_ALG_HMACSHA384 164 /* XXXMPA */
60 #define DST_ALG_HMACSHA512 165 /* XXXMPA */
61 #define DST_ALG_PRIVATE 254
62 #define DST_ALG_EXPAND 255
63 #define DST_MAX_ALGS 255
65 /*% A buffer of this size is large enough to hold any key */
66 #define DST_KEY_MAXSIZE 1280
69 * A buffer of this size is large enough to hold the textual representation
72 #define DST_KEY_MAXTEXTSIZE 2048
74 /*% 'Type' for dst_read_key() */
75 #define DST_TYPE_KEY 0x1000000 /* KEY key */
76 #define DST_TYPE_PRIVATE 0x2000000
77 #define DST_TYPE_PUBLIC 0x4000000
84 dst_lib_init(isc_mem_t *mctx, isc_entropy_t *ectx, unsigned int eflags);
86 * Initializes the DST subsystem.
89 * \li "mctx" is a valid memory context
90 * \li "ectx" is a valid entropy context
97 * \li DST is properly initialized.
101 dst_lib_destroy(void);
103 * Releases all resources allocated by DST.
107 dst_algorithm_supported(unsigned int alg);
109 * Checks that a given algorithm is supported by DST.
117 dst_context_create(dst_key_t *key, isc_mem_t *mctx, dst_context_t **dctxp);
119 * Creates a context to be used for a sign or verify operation.
122 * \li "key" is a valid key.
123 * \li "mctx" is a valid memory context.
124 * \li dctxp != NULL && *dctxp == NULL
131 * \li *dctxp will contain a usable context.
135 dst_context_destroy(dst_context_t **dctxp);
137 * Destroys all memory associated with a context.
140 * \li *dctxp != NULL && *dctxp == NULL
147 dst_context_adddata(dst_context_t *dctx, const isc_region_t *data);
149 * Incrementally adds data to the context to be used in a sign or verify
153 * \li "dctx" is a valid context
154 * \li "data" is a valid region
158 * \li DST_R_SIGNFAILURE
159 * \li all other errors indicate failure
163 dst_context_sign(dst_context_t *dctx, isc_buffer_t *sig);
165 * Computes a signature using the data and key stored in the context.
168 * \li "dctx" is a valid context.
169 * \li "sig" is a valid buffer.
173 * \li DST_R_VERIFYFAILURE
174 * \li all other errors indicate failure
177 * \li "sig" will contain the signature
181 dst_context_verify(dst_context_t *dctx, isc_region_t *sig);
183 * Verifies the signature using the data and key stored in the context.
186 * \li "dctx" is a valid context.
187 * \li "sig" is a valid region.
191 * \li all other errors indicate failure
194 * \li "sig" will contain the signature
198 dst_key_computesecret(const dst_key_t *pub, const dst_key_t *priv,
199 isc_buffer_t *secret);
201 * Computes a shared secret from two (Diffie-Hellman) keys.
204 * \li "pub" is a valid key that can be used to derive a shared secret
205 * \li "priv" is a valid private key that can be used to derive a shared secret
206 * \li "secret" is a valid buffer
210 * \li any other result indicates failure
213 * \li If successful, secret will contain the derived shared secret.
217 dst_key_fromfile(dns_name_t *name, dns_keytag_t id, unsigned int alg, int type,
218 const char *directory, isc_mem_t *mctx, dst_key_t **keyp);
220 * Reads a key from permanent storage. The key can either be a public or
221 * private key, and is specified by name, algorithm, and id. If a private key
222 * is specified, the public key must also be present. If directory is NULL,
223 * the current directory is assumed.
226 * \li "name" is a valid absolute dns name.
227 * \li "id" is a valid key tag identifier.
228 * \li "alg" is a supported key algorithm.
229 * \li "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union.
230 * DST_TYPE_KEY look for a KEY record otherwise DNSKEY
231 * \li "mctx" is a valid memory context.
232 * \li "keyp" is not NULL and "*keyp" is NULL.
236 * \li any other result indicates failure
239 * \li If successful, *keyp will contain a valid key.
243 dst_key_fromnamedfile(const char *filename, int type, isc_mem_t *mctx,
246 * Reads a key from permanent storage. The key can either be a public or
247 * key, and is specified by filename. If a private key is specified, the
248 * public key must also be present.
251 * \li "filename" is not NULL
252 * \li "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union
253 * DST_TYPE_KEY look for a KEY record otherwise DNSKEY
254 * \li "mctx" is a valid memory context
255 * \li "keyp" is not NULL and "*keyp" is NULL.
259 * \li any other result indicates failure
262 * \li If successful, *keyp will contain a valid key.
267 dst_key_read_public(const char *filename, int type,
268 isc_mem_t *mctx, dst_key_t **keyp);
270 * Reads a public key from permanent storage. The key must be a public key.
273 * \li "filename" is not NULL
274 * \li "type" is DST_TYPE_KEY look for a KEY record otherwise DNSKEY
275 * \li "mctx" is a valid memory context
276 * \li "keyp" is not NULL and "*keyp" is NULL.
280 * \li DST_R_BADKEYTYPE if the key type is not the expected one
281 * \li ISC_R_UNEXPECTEDTOKEN if the file can not be parsed as a public key
282 * \li any other result indicates failure
285 * \li If successful, *keyp will contain a valid key.
289 dst_key_tofile(const dst_key_t *key, int type, const char *directory);
291 * Writes a key to permanent storage. The key can either be a public or
292 * private key. Public keys are written in DNS format and private keys
293 * are written as a set of base64 encoded values. If directory is NULL,
294 * the current directory is assumed.
297 * \li "key" is a valid key.
298 * \li "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union
302 * \li any other result indicates failure
306 dst_key_fromdns(dns_name_t *name, dns_rdataclass_t rdclass,
307 isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp);
309 * Converts a DNS KEY record into a DST key.
312 * \li "name" is a valid absolute dns name.
313 * \li "source" is a valid buffer. There must be at least 4 bytes available.
314 * \li "mctx" is a valid memory context.
315 * \li "keyp" is not NULL and "*keyp" is NULL.
319 * \li any other result indicates failure
322 * \li If successful, *keyp will contain a valid key, and the consumed
323 * pointer in data will be advanced.
327 dst_key_todns(const dst_key_t *key, isc_buffer_t *target);
329 * Converts a DST key into a DNS KEY record.
332 * \li "key" is a valid key.
333 * \li "target" is a valid buffer. There must be at least 4 bytes unused.
337 * \li any other result indicates failure
340 * \li If successful, the used pointer in 'target' is advanced by at least 4.
344 dst_key_frombuffer(dns_name_t *name, unsigned int alg,
345 unsigned int flags, unsigned int protocol,
346 dns_rdataclass_t rdclass,
347 isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp);
349 * Converts a buffer containing DNS KEY RDATA into a DST key.
352 *\li "name" is a valid absolute dns name.
353 *\li "alg" is a supported key algorithm.
354 *\li "source" is a valid buffer.
355 *\li "mctx" is a valid memory context.
356 *\li "keyp" is not NULL and "*keyp" is NULL.
360 * \li any other result indicates failure
363 *\li If successful, *keyp will contain a valid key, and the consumed
364 * pointer in source will be advanced.
368 dst_key_tobuffer(const dst_key_t *key, isc_buffer_t *target);
370 * Converts a DST key into DNS KEY RDATA format.
373 *\li "key" is a valid key.
374 *\li "target" is a valid buffer.
378 * \li any other result indicates failure
381 *\li If successful, the used pointer in 'target' is advanced.
385 dst_key_privatefrombuffer(dst_key_t *key, isc_buffer_t *buffer);
387 * Converts a public key into a private key, reading the private key
388 * information from the buffer. The buffer should contain the same data
389 * as the .private key file would.
392 *\li "key" is a valid public key.
393 *\li "buffer" is not NULL.
397 * \li any other result indicates failure
400 *\li If successful, key will contain a valid private key.
404 dst_key_getgssctx(const dst_key_t *key);
406 * Returns the opaque key data.
407 * Be cautions when using this value unless you know what you are doing.
410 *\li "key" is not NULL.
413 *\li gssctx key data, possibly NULL.
417 dst_key_fromgssapi(dns_name_t *name, gss_ctx_id_t gssctx, isc_mem_t *mctx,
420 * Converts a GSSAPI opaque context id into a DST key.
423 *\li "name" is a valid absolute dns name.
424 *\li "gssctx" is a GSSAPI context id.
425 *\li "mctx" is a valid memory context.
426 *\li "keyp" is not NULL and "*keyp" is NULL.
430 * \li any other result indicates failure
433 *\li If successful, *keyp will contain a valid key and be responsible for
438 dst_key_generate(dns_name_t *name, unsigned int alg,
439 unsigned int bits, unsigned int param,
440 unsigned int flags, unsigned int protocol,
441 dns_rdataclass_t rdclass,
442 isc_mem_t *mctx, dst_key_t **keyp);
444 * Generate a DST key (or keypair) with the supplied parameters. The
445 * interpretation of the "param" field depends on the algorithm:
449 * !0 use Fermat4 (2^16 + 1)
451 * 0 default - use well known prime if bits == 768 or 1024,
452 * otherwise use 2 as the generator.
453 * !0 use this value as the generator.
456 * 0 default - require good entropy
457 * !0 lack of good entropy is ok
461 *\li "name" is a valid absolute dns name.
462 *\li "keyp" is not NULL and "*keyp" is NULL.
466 * \li any other result indicates failure
469 *\li If successful, *keyp will contain a valid key.
473 dst_key_compare(const dst_key_t *key1, const dst_key_t *key2);
475 * Compares two DST keys.
478 *\li "key1" is a valid key.
479 *\li "key2" is a valid key.
487 dst_key_paramcompare(const dst_key_t *key1, const dst_key_t *key2);
489 * Compares the parameters of two DST keys. This is used to determine if
490 * two (Diffie-Hellman) keys can be used to derive a shared secret.
493 *\li "key1" is a valid key.
494 *\li "key2" is a valid key.
502 dst_key_free(dst_key_t **keyp);
504 * Release all memory associated with the key.
507 *\li "keyp" is not NULL and "*keyp" is a valid key.
510 *\li All memory associated with "*keyp" will be freed.
515 * Accessor functions to obtain key fields.
518 *\li "key" is a valid key.
521 dst_key_name(const dst_key_t *key);
524 dst_key_size(const dst_key_t *key);
527 dst_key_proto(const dst_key_t *key);
530 dst_key_alg(const dst_key_t *key);
533 dst_key_flags(const dst_key_t *key);
536 dst_key_id(const dst_key_t *key);
539 dst_key_class(const dst_key_t *key);
542 dst_key_isprivate(const dst_key_t *key);
545 dst_key_iszonekey(const dst_key_t *key);
548 dst_key_isnullkey(const dst_key_t *key);
551 dst_key_buildfilename(const dst_key_t *key, int type,
552 const char *directory, isc_buffer_t *out);
554 * Generates the filename used by dst to store the specified key.
555 * If directory is NULL, the current directory is assumed.
558 *\li "key" is a valid key
559 *\li "type" is either DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or 0 for no suffix.
560 *\li "out" is a valid buffer
563 *\li the file name will be written to "out", and the used pointer will
568 dst_key_sigsize(const dst_key_t *key, unsigned int *n);
570 * Computes the size of a signature generated by the given key.
573 *\li "key" is a valid key.
578 *\li DST_R_UNSUPPORTEDALG
581 *\li "n" stores the size of a generated signature
585 dst_key_secretsize(const dst_key_t *key, unsigned int *n);
587 * Computes the size of a shared secret generated by the given key.
590 *\li "key" is a valid key.
595 *\li DST_R_UNSUPPORTEDALG
598 *\li "n" stores the size of a generated shared secret
602 dst_region_computeid(const isc_region_t *source, unsigned int alg);
604 * Computes the key id of the key stored in the provided region with the
608 *\li "source" contains a valid, non-NULL region.
615 dst_key_getbits(const dst_key_t *key);
617 * Get the number of digest bits required (0 == MAX).
620 * "key" is a valid key.
624 dst_key_setbits(dst_key_t *key, isc_uint16_t bits);
626 * Set the number of digest bits required (0 == MAX).
629 * "key" is a valid key.
634 #endif /* DST_DST_H */