2 * Copyright (C) 2004-2006 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2002 Internet Software Consortium.
5 * Permission to use, copy, modify, and 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.1.6.5 2006/01/27 23:57:44 marka Exp $ */
27 #include <dns/types.h>
36 * The dst_key structure is opaque. Applications should use the accessor
37 * functions provided to retrieve key attributes. If an application needs
38 * to set attributes, new accessor functions will be written.
41 typedef struct dst_key dst_key_t;
42 typedef struct dst_context dst_context_t;
44 /* DST algorithm codes */
45 #define DST_ALG_UNKNOWN 0
46 #define DST_ALG_RSAMD5 1
47 #define DST_ALG_RSA DST_ALG_RSAMD5 /*%< backwards compatibility */
51 #define DST_ALG_RSASHA1 5
52 #define DST_ALG_HMACMD5 157
53 #define DST_ALG_GSSAPI 160
54 #define DST_ALG_HMACSHA1 161 /* XXXMPA */
55 #define DST_ALG_HMACSHA224 162 /* XXXMPA */
56 #define DST_ALG_HMACSHA256 163 /* XXXMPA */
57 #define DST_ALG_HMACSHA384 164 /* XXXMPA */
58 #define DST_ALG_HMACSHA512 165 /* XXXMPA */
59 #define DST_ALG_PRIVATE 254
60 #define DST_ALG_EXPAND 255
61 #define DST_MAX_ALGS 255
63 /*% A buffer of this size is large enough to hold any key */
64 #define DST_KEY_MAXSIZE 1280
67 * A buffer of this size is large enough to hold the textual representation
70 #define DST_KEY_MAXTEXTSIZE 2048
72 /*% 'Type' for dst_read_key() */
73 #define DST_TYPE_KEY 0x1000000 /* KEY key */
74 #define DST_TYPE_PRIVATE 0x2000000
75 #define DST_TYPE_PUBLIC 0x4000000
82 dst_lib_init(isc_mem_t *mctx, isc_entropy_t *ectx, unsigned int eflags);
84 * Initializes the DST subsystem.
87 * \li "mctx" is a valid memory context
88 * \li "ectx" is a valid entropy context
95 * \li DST is properly initialized.
99 dst_lib_destroy(void);
101 * Releases all resources allocated by DST.
105 dst_algorithm_supported(unsigned int alg);
107 * Checks that a given algorithm is supported by DST.
115 dst_context_create(dst_key_t *key, isc_mem_t *mctx, dst_context_t **dctxp);
117 * Creates a context to be used for a sign or verify operation.
120 * \li "key" is a valid key.
121 * \li "mctx" is a valid memory context.
122 * \li dctxp != NULL && *dctxp == NULL
129 * \li *dctxp will contain a usable context.
133 dst_context_destroy(dst_context_t **dctxp);
135 * Destroys all memory associated with a context.
138 * \li *dctxp != NULL && *dctxp == NULL
145 dst_context_adddata(dst_context_t *dctx, const isc_region_t *data);
147 * Incrementally adds data to the context to be used in a sign or verify
151 * \li "dctx" is a valid context
152 * \li "data" is a valid region
156 * \li DST_R_SIGNFAILURE
157 * \li all other errors indicate failure
161 dst_context_sign(dst_context_t *dctx, isc_buffer_t *sig);
163 * Computes a signature using the data and key stored in the context.
166 * \li "dctx" is a valid context.
167 * \li "sig" is a valid buffer.
171 * \li DST_R_VERIFYFAILURE
172 * \li all other errors indicate failure
175 * \li "sig" will contain the signature
179 dst_context_verify(dst_context_t *dctx, isc_region_t *sig);
181 * Verifies the signature using the data and key stored in the context.
184 * \li "dctx" is a valid context.
185 * \li "sig" is a valid region.
189 * \li all other errors indicate failure
192 * \li "sig" will contain the signature
196 dst_key_computesecret(const dst_key_t *pub, const dst_key_t *priv,
197 isc_buffer_t *secret);
199 * Computes a shared secret from two (Diffie-Hellman) keys.
202 * \li "pub" is a valid key that can be used to derive a shared secret
203 * \li "priv" is a valid private key that can be used to derive a shared secret
204 * \li "secret" is a valid buffer
208 * \li any other result indicates failure
211 * \li If successful, secret will contain the derived shared secret.
215 dst_key_fromfile(dns_name_t *name, dns_keytag_t id, unsigned int alg, int type,
216 const char *directory, isc_mem_t *mctx, dst_key_t **keyp);
218 * Reads a key from permanent storage. The key can either be a public or
219 * private key, and is specified by name, algorithm, and id. If a private key
220 * is specified, the public key must also be present. If directory is NULL,
221 * the current directory is assumed.
224 * \li "name" is a valid absolute dns name.
225 * \li "id" is a valid key tag identifier.
226 * \li "alg" is a supported key algorithm.
227 * \li "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union.
228 * DST_TYPE_KEY look for a KEY record otherwise DNSKEY
229 * \li "mctx" is a valid memory context.
230 * \li "keyp" is not NULL and "*keyp" is NULL.
234 * \li any other result indicates failure
237 * \li If successful, *keyp will contain a valid key.
241 dst_key_fromnamedfile(const char *filename, int type, isc_mem_t *mctx,
244 * Reads a key from permanent storage. The key can either be a public or
245 * key, and is specified by filename. If a private key is specified, the
246 * public key must also be present.
249 * \li "filename" is not NULL
250 * \li "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union
251 * DST_TYPE_KEY look for a KEY record otherwise DNSKEY
252 * \li "mctx" is a valid memory context
253 * \li "keyp" is not NULL and "*keyp" is NULL.
257 * \li any other result indicates failure
260 * \li If successful, *keyp will contain a valid key.
265 dst_key_read_public(const char *filename, int type,
266 isc_mem_t *mctx, dst_key_t **keyp);
268 * Reads a public key from permanent storage. The key must be a public key.
271 * \li "filename" is not NULL
272 * \li "type" is DST_TYPE_KEY look for a KEY record otherwise DNSKEY
273 * \li "mctx" is a valid memory context
274 * \li "keyp" is not NULL and "*keyp" is NULL.
278 * \li DST_R_BADKEYTYPE if the key type is not the expected one
279 * \li ISC_R_UNEXPECTEDTOKEN if the file can not be parsed as a public key
280 * \li any other result indicates failure
283 * \li If successful, *keyp will contain a valid key.
287 dst_key_tofile(const dst_key_t *key, int type, const char *directory);
289 * Writes a key to permanent storage. The key can either be a public or
290 * private key. Public keys are written in DNS format and private keys
291 * are written as a set of base64 encoded values. If directory is NULL,
292 * the current directory is assumed.
295 * \li "key" is a valid key.
296 * \li "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union
300 * \li any other result indicates failure
304 dst_key_fromdns(dns_name_t *name, dns_rdataclass_t rdclass,
305 isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp);
307 * Converts a DNS KEY record into a DST key.
310 * \li "name" is a valid absolute dns name.
311 * \li "source" is a valid buffer. There must be at least 4 bytes available.
312 * \li "mctx" is a valid memory context.
313 * \li "keyp" is not NULL and "*keyp" is NULL.
317 * \li any other result indicates failure
320 * \li If successful, *keyp will contain a valid key, and the consumed
321 * pointer in data will be advanced.
325 dst_key_todns(const dst_key_t *key, isc_buffer_t *target);
327 * Converts a DST key into a DNS KEY record.
330 * \li "key" is a valid key.
331 * \li "target" is a valid buffer. There must be at least 4 bytes unused.
335 * \li any other result indicates failure
338 * \li If successful, the used pointer in 'target' is advanced by at least 4.
342 dst_key_frombuffer(dns_name_t *name, unsigned int alg,
343 unsigned int flags, unsigned int protocol,
344 dns_rdataclass_t rdclass,
345 isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp);
347 * Converts a buffer containing DNS KEY RDATA into a DST key.
350 *\li "name" is a valid absolute dns name.
351 *\li "alg" is a supported key algorithm.
352 *\li "source" is a valid buffer.
353 *\li "mctx" is a valid memory context.
354 *\li "keyp" is not NULL and "*keyp" is NULL.
358 * \li any other result indicates failure
361 *\li If successful, *keyp will contain a valid key, and the consumed
362 * pointer in source will be advanced.
366 dst_key_tobuffer(const dst_key_t *key, isc_buffer_t *target);
368 * Converts a DST key into DNS KEY RDATA format.
371 *\li "key" is a valid key.
372 *\li "target" is a valid buffer.
376 * \li any other result indicates failure
379 *\li If successful, the used pointer in 'target' is advanced.
383 dst_key_privatefrombuffer(dst_key_t *key, isc_buffer_t *buffer);
385 * Converts a public key into a private key, reading the private key
386 * information from the buffer. The buffer should contain the same data
387 * as the .private key file would.
390 *\li "key" is a valid public key.
391 *\li "buffer" is not NULL.
395 * \li any other result indicates failure
398 *\li If successful, key will contain a valid private key.
403 dst_key_fromgssapi(dns_name_t *name, void *opaque, isc_mem_t *mctx,
406 * Converts a GSSAPI opaque context id into a DST key.
409 *\li "name" is a valid absolute dns name.
410 *\li "opaque" is a GSSAPI context id.
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 be responsible for
424 dst_key_generate(dns_name_t *name, unsigned int alg,
425 unsigned int bits, unsigned int param,
426 unsigned int flags, unsigned int protocol,
427 dns_rdataclass_t rdclass,
428 isc_mem_t *mctx, dst_key_t **keyp);
430 * Generate a DST key (or keypair) with the supplied parameters. The
431 * interpretation of the "param" field depends on the algorithm:
435 * !0 use Fermat4 (2^16 + 1)
437 * 0 default - use well known prime if bits == 768 or 1024,
438 * otherwise use 2 as the generator.
439 * !0 use this value as the generator.
442 * 0 default - require good entropy
443 * !0 lack of good entropy is ok
447 *\li "name" is a valid absolute dns name.
448 *\li "keyp" is not NULL and "*keyp" is NULL.
452 * \li any other result indicates failure
455 *\li If successful, *keyp will contain a valid key.
459 dst_key_compare(const dst_key_t *key1, const dst_key_t *key2);
461 * Compares two DST keys.
464 *\li "key1" is a valid key.
465 *\li "key2" is a valid key.
473 dst_key_paramcompare(const dst_key_t *key1, const dst_key_t *key2);
475 * Compares the parameters of two DST keys. This is used to determine if
476 * two (Diffie-Hellman) keys can be used to derive a shared secret.
479 *\li "key1" is a valid key.
480 *\li "key2" is a valid key.
488 dst_key_free(dst_key_t **keyp);
490 * Release all memory associated with the key.
493 *\li "keyp" is not NULL and "*keyp" is a valid key.
496 *\li All memory associated with "*keyp" will be freed.
501 * Accessor functions to obtain key fields.
504 *\li "key" is a valid key.
507 dst_key_name(const dst_key_t *key);
510 dst_key_size(const dst_key_t *key);
513 dst_key_proto(const dst_key_t *key);
516 dst_key_alg(const dst_key_t *key);
519 dst_key_flags(const dst_key_t *key);
522 dst_key_id(const dst_key_t *key);
525 dst_key_class(const dst_key_t *key);
528 dst_key_isprivate(const dst_key_t *key);
531 dst_key_iszonekey(const dst_key_t *key);
534 dst_key_isnullkey(const dst_key_t *key);
537 dst_key_buildfilename(const dst_key_t *key, int type,
538 const char *directory, isc_buffer_t *out);
540 * Generates the filename used by dst to store the specified key.
541 * If directory is NULL, the current directory is assumed.
544 *\li "key" is a valid key
545 *\li "type" is either DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or 0 for no suffix.
546 *\li "out" is a valid buffer
549 *\li the file name will be written to "out", and the used pointer will
554 dst_key_sigsize(const dst_key_t *key, unsigned int *n);
556 * Computes the size of a signature generated by the given key.
559 *\li "key" is a valid key.
564 *\li DST_R_UNSUPPORTEDALG
567 *\li "n" stores the size of a generated signature
571 dst_key_secretsize(const dst_key_t *key, unsigned int *n);
573 * Computes the size of a shared secret generated by the given key.
576 *\li "key" is a valid key.
581 *\li DST_R_UNSUPPORTEDALG
584 *\li "n" stores the size of a generated shared secret
588 dst_region_computeid(const isc_region_t *source, unsigned int alg);
590 * Computes the key id of the key stored in the provided region with the
594 *\li "source" contains a valid, non-NULL region.
601 dst_key_getbits(const dst_key_t *key);
603 * Get the number of digest bits required (0 == MAX).
606 * "key" is a valid key.
610 dst_key_setbits(dst_key_t *key, isc_uint16_t bits);
612 * Set the number of digest bits required (0 == MAX).
615 * "key" is a valid key.
620 #endif /* DST_DST_H */