2 * Copyright (C) 2004-2008 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.12 2008/09/24 02:46:23 marka 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_NSEC3DSA 6
55 #define DST_ALG_NSEC3RSASHA1 7
56 #define DST_ALG_HMACMD5 157
57 #define DST_ALG_GSSAPI 160
58 #define DST_ALG_HMACSHA1 161 /* XXXMPA */
59 #define DST_ALG_HMACSHA224 162 /* XXXMPA */
60 #define DST_ALG_HMACSHA256 163 /* XXXMPA */
61 #define DST_ALG_HMACSHA384 164 /* XXXMPA */
62 #define DST_ALG_HMACSHA512 165 /* XXXMPA */
63 #define DST_ALG_PRIVATE 254
64 #define DST_ALG_EXPAND 255
65 #define DST_MAX_ALGS 255
67 /*% A buffer of this size is large enough to hold any key */
68 #define DST_KEY_MAXSIZE 1280
71 * A buffer of this size is large enough to hold the textual representation
74 #define DST_KEY_MAXTEXTSIZE 2048
76 /*% 'Type' for dst_read_key() */
77 #define DST_TYPE_KEY 0x1000000 /* KEY key */
78 #define DST_TYPE_PRIVATE 0x2000000
79 #define DST_TYPE_PUBLIC 0x4000000
86 dst_lib_init(isc_mem_t *mctx, isc_entropy_t *ectx, unsigned int eflags);
88 * Initializes the DST subsystem.
91 * \li "mctx" is a valid memory context
92 * \li "ectx" is a valid entropy context
99 * \li DST is properly initialized.
103 dst_lib_destroy(void);
105 * Releases all resources allocated by DST.
109 dst_algorithm_supported(unsigned int alg);
111 * Checks that a given algorithm is supported by DST.
119 dst_context_create(dst_key_t *key, isc_mem_t *mctx, dst_context_t **dctxp);
121 * Creates a context to be used for a sign or verify operation.
124 * \li "key" is a valid key.
125 * \li "mctx" is a valid memory context.
126 * \li dctxp != NULL && *dctxp == NULL
133 * \li *dctxp will contain a usable context.
137 dst_context_destroy(dst_context_t **dctxp);
139 * Destroys all memory associated with a context.
142 * \li *dctxp != NULL && *dctxp == NULL
149 dst_context_adddata(dst_context_t *dctx, const isc_region_t *data);
151 * Incrementally adds data to the context to be used in a sign or verify
155 * \li "dctx" is a valid context
156 * \li "data" is a valid region
160 * \li DST_R_SIGNFAILURE
161 * \li all other errors indicate failure
165 dst_context_sign(dst_context_t *dctx, isc_buffer_t *sig);
167 * Computes a signature using the data and key stored in the context.
170 * \li "dctx" is a valid context.
171 * \li "sig" is a valid buffer.
175 * \li DST_R_VERIFYFAILURE
176 * \li all other errors indicate failure
179 * \li "sig" will contain the signature
183 dst_context_verify(dst_context_t *dctx, isc_region_t *sig);
185 * Verifies the signature using the data and key stored in the context.
188 * \li "dctx" is a valid context.
189 * \li "sig" is a valid region.
193 * \li all other errors indicate failure
196 * \li "sig" will contain the signature
200 dst_key_computesecret(const dst_key_t *pub, const dst_key_t *priv,
201 isc_buffer_t *secret);
203 * Computes a shared secret from two (Diffie-Hellman) keys.
206 * \li "pub" is a valid key that can be used to derive a shared secret
207 * \li "priv" is a valid private key that can be used to derive a shared secret
208 * \li "secret" is a valid buffer
212 * \li any other result indicates failure
215 * \li If successful, secret will contain the derived shared secret.
219 dst_key_fromfile(dns_name_t *name, dns_keytag_t id, unsigned int alg, int type,
220 const char *directory, isc_mem_t *mctx, dst_key_t **keyp);
222 * Reads a key from permanent storage. The key can either be a public or
223 * private key, and is specified by name, algorithm, and id. If a private key
224 * is specified, the public key must also be present. If directory is NULL,
225 * the current directory is assumed.
228 * \li "name" is a valid absolute dns name.
229 * \li "id" is a valid key tag identifier.
230 * \li "alg" is a supported key algorithm.
231 * \li "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union.
232 * DST_TYPE_KEY look for a KEY record otherwise DNSKEY
233 * \li "mctx" is a valid memory context.
234 * \li "keyp" is not NULL and "*keyp" is NULL.
238 * \li any other result indicates failure
241 * \li If successful, *keyp will contain a valid key.
245 dst_key_fromnamedfile(const char *filename, int type, isc_mem_t *mctx,
248 * Reads a key from permanent storage. The key can either be a public or
249 * key, and is specified by filename. If a private key is specified, the
250 * public key must also be present.
253 * \li "filename" is not NULL
254 * \li "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union
255 * DST_TYPE_KEY look for a KEY record otherwise DNSKEY
256 * \li "mctx" is a valid memory context
257 * \li "keyp" is not NULL and "*keyp" is NULL.
261 * \li any other result indicates failure
264 * \li If successful, *keyp will contain a valid key.
269 dst_key_read_public(const char *filename, int type,
270 isc_mem_t *mctx, dst_key_t **keyp);
272 * Reads a public key from permanent storage. The key must be a public key.
275 * \li "filename" is not NULL
276 * \li "type" is DST_TYPE_KEY look for a KEY record otherwise DNSKEY
277 * \li "mctx" is a valid memory context
278 * \li "keyp" is not NULL and "*keyp" is NULL.
282 * \li DST_R_BADKEYTYPE if the key type is not the expected one
283 * \li ISC_R_UNEXPECTEDTOKEN if the file can not be parsed as a public key
284 * \li any other result indicates failure
287 * \li If successful, *keyp will contain a valid key.
291 dst_key_tofile(const dst_key_t *key, int type, const char *directory);
293 * Writes a key to permanent storage. The key can either be a public or
294 * private key. Public keys are written in DNS format and private keys
295 * are written as a set of base64 encoded values. If directory is NULL,
296 * the current directory is assumed.
299 * \li "key" is a valid key.
300 * \li "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union
304 * \li any other result indicates failure
308 dst_key_fromdns(dns_name_t *name, dns_rdataclass_t rdclass,
309 isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp);
311 * Converts a DNS KEY record into a DST key.
314 * \li "name" is a valid absolute dns name.
315 * \li "source" is a valid buffer. There must be at least 4 bytes available.
316 * \li "mctx" is a valid memory context.
317 * \li "keyp" is not NULL and "*keyp" is NULL.
321 * \li any other result indicates failure
324 * \li If successful, *keyp will contain a valid key, and the consumed
325 * pointer in data will be advanced.
329 dst_key_todns(const dst_key_t *key, isc_buffer_t *target);
331 * Converts a DST key into a DNS KEY record.
334 * \li "key" is a valid key.
335 * \li "target" is a valid buffer. There must be at least 4 bytes unused.
339 * \li any other result indicates failure
342 * \li If successful, the used pointer in 'target' is advanced by at least 4.
346 dst_key_frombuffer(dns_name_t *name, unsigned int alg,
347 unsigned int flags, unsigned int protocol,
348 dns_rdataclass_t rdclass,
349 isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp);
351 * Converts a buffer containing DNS KEY RDATA into a DST key.
354 *\li "name" is a valid absolute dns name.
355 *\li "alg" is a supported key algorithm.
356 *\li "source" is a valid buffer.
357 *\li "mctx" is a valid memory context.
358 *\li "keyp" is not NULL and "*keyp" is NULL.
362 * \li any other result indicates failure
365 *\li If successful, *keyp will contain a valid key, and the consumed
366 * pointer in source will be advanced.
370 dst_key_tobuffer(const dst_key_t *key, isc_buffer_t *target);
372 * Converts a DST key into DNS KEY RDATA format.
375 *\li "key" is a valid key.
376 *\li "target" is a valid buffer.
380 * \li any other result indicates failure
383 *\li If successful, the used pointer in 'target' is advanced.
387 dst_key_privatefrombuffer(dst_key_t *key, isc_buffer_t *buffer);
389 * Converts a public key into a private key, reading the private key
390 * information from the buffer. The buffer should contain the same data
391 * as the .private key file would.
394 *\li "key" is a valid public key.
395 *\li "buffer" is not NULL.
399 * \li any other result indicates failure
402 *\li If successful, key will contain a valid private key.
406 dst_key_getgssctx(const dst_key_t *key);
408 * Returns the opaque key data.
409 * Be cautions when using this value unless you know what you are doing.
412 *\li "key" is not NULL.
415 *\li gssctx key data, possibly NULL.
419 dst_key_fromgssapi(dns_name_t *name, gss_ctx_id_t gssctx, isc_mem_t *mctx,
422 * Converts a GSSAPI opaque context id into a DST key.
425 *\li "name" is a valid absolute dns name.
426 *\li "gssctx" is a GSSAPI context id.
427 *\li "mctx" is a valid memory context.
428 *\li "keyp" is not NULL and "*keyp" is NULL.
432 * \li any other result indicates failure
435 *\li If successful, *keyp will contain a valid key and be responsible for
440 dst_key_fromlabel(dns_name_t *name, int alg, unsigned int flags,
441 unsigned int protocol, dns_rdataclass_t rdclass,
442 const char *engine, const char *label, const char *pin,
443 isc_mem_t *mctx, dst_key_t **keyp);
446 dst_key_generate(dns_name_t *name, unsigned int alg,
447 unsigned int bits, unsigned int param,
448 unsigned int flags, unsigned int protocol,
449 dns_rdataclass_t rdclass,
450 isc_mem_t *mctx, dst_key_t **keyp);
452 * Generate a DST key (or keypair) with the supplied parameters. The
453 * interpretation of the "param" field depends on the algorithm:
457 * !0 use Fermat4 (2^16 + 1)
459 * 0 default - use well known prime if bits == 768 or 1024,
460 * otherwise use 2 as the generator.
461 * !0 use this value as the generator.
464 * 0 default - require good entropy
465 * !0 lack of good entropy is ok
469 *\li "name" is a valid absolute dns name.
470 *\li "keyp" is not NULL and "*keyp" is NULL.
474 * \li any other result indicates failure
477 *\li If successful, *keyp will contain a valid key.
481 dst_key_compare(const dst_key_t *key1, const dst_key_t *key2);
483 * Compares two DST keys.
486 *\li "key1" is a valid key.
487 *\li "key2" is a valid key.
495 dst_key_paramcompare(const dst_key_t *key1, const dst_key_t *key2);
497 * Compares the parameters of two DST keys. This is used to determine if
498 * two (Diffie-Hellman) keys can be used to derive a shared secret.
501 *\li "key1" is a valid key.
502 *\li "key2" is a valid key.
510 dst_key_free(dst_key_t **keyp);
512 * Release all memory associated with the key.
515 *\li "keyp" is not NULL and "*keyp" is a valid key.
518 *\li All memory associated with "*keyp" will be freed.
523 * Accessor functions to obtain key fields.
526 *\li "key" is a valid key.
529 dst_key_name(const dst_key_t *key);
532 dst_key_size(const dst_key_t *key);
535 dst_key_proto(const dst_key_t *key);
538 dst_key_alg(const dst_key_t *key);
541 dst_key_flags(const dst_key_t *key);
544 dst_key_id(const dst_key_t *key);
547 dst_key_class(const dst_key_t *key);
550 dst_key_isprivate(const dst_key_t *key);
553 dst_key_iszonekey(const dst_key_t *key);
556 dst_key_isnullkey(const dst_key_t *key);
559 dst_key_buildfilename(const dst_key_t *key, int type,
560 const char *directory, isc_buffer_t *out);
562 * Generates the filename used by dst to store the specified key.
563 * If directory is NULL, the current directory is assumed.
566 *\li "key" is a valid key
567 *\li "type" is either DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or 0 for no suffix.
568 *\li "out" is a valid buffer
571 *\li the file name will be written to "out", and the used pointer will
576 dst_key_sigsize(const dst_key_t *key, unsigned int *n);
578 * Computes the size of a signature generated by the given key.
581 *\li "key" is a valid key.
586 *\li DST_R_UNSUPPORTEDALG
589 *\li "n" stores the size of a generated signature
593 dst_key_secretsize(const dst_key_t *key, unsigned int *n);
595 * Computes the size of a shared secret generated by the given key.
598 *\li "key" is a valid key.
603 *\li DST_R_UNSUPPORTEDALG
606 *\li "n" stores the size of a generated shared secret
610 dst_region_computeid(const isc_region_t *source, unsigned int alg);
612 * Computes the key id of the key stored in the provided region with the
616 *\li "source" contains a valid, non-NULL region.
623 dst_key_getbits(const dst_key_t *key);
625 * Get the number of digest bits required (0 == MAX).
628 * "key" is a valid key.
632 dst_key_setbits(dst_key_t *key, isc_uint16_t bits);
634 * Set the number of digest bits required (0 == MAX).
637 * "key" is a valid key.
642 #endif /* DST_DST_H */