2 * Copyright (C) 2004-2008, 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.12.50.2 2010/01/15 23:47:34 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_NSEC3DSA 6
55 #define DST_ALG_NSEC3RSASHA1 7
56 #define DST_ALG_RSASHA256 8
57 #define DST_ALG_RSASHA512 10
58 #define DST_ALG_HMACMD5 157
59 #define DST_ALG_GSSAPI 160
60 #define DST_ALG_HMACSHA1 161 /* XXXMPA */
61 #define DST_ALG_HMACSHA224 162 /* XXXMPA */
62 #define DST_ALG_HMACSHA256 163 /* XXXMPA */
63 #define DST_ALG_HMACSHA384 164 /* XXXMPA */
64 #define DST_ALG_HMACSHA512 165 /* XXXMPA */
65 #define DST_ALG_PRIVATE 254
66 #define DST_ALG_EXPAND 255
67 #define DST_MAX_ALGS 255
69 /*% A buffer of this size is large enough to hold any key */
70 #define DST_KEY_MAXSIZE 1280
73 * A buffer of this size is large enough to hold the textual representation
76 #define DST_KEY_MAXTEXTSIZE 2048
78 /*% 'Type' for dst_read_key() */
79 #define DST_TYPE_KEY 0x1000000 /* KEY key */
80 #define DST_TYPE_PRIVATE 0x2000000
81 #define DST_TYPE_PUBLIC 0x4000000
88 dst_lib_init(isc_mem_t *mctx, isc_entropy_t *ectx, unsigned int eflags);
90 * Initializes the DST subsystem.
93 * \li "mctx" is a valid memory context
94 * \li "ectx" is a valid entropy context
101 * \li DST is properly initialized.
105 dst_lib_destroy(void);
107 * Releases all resources allocated by DST.
111 dst_algorithm_supported(unsigned int alg);
113 * Checks that a given algorithm is supported by DST.
121 dst_context_create(dst_key_t *key, isc_mem_t *mctx, dst_context_t **dctxp);
123 * Creates a context to be used for a sign or verify operation.
126 * \li "key" is a valid key.
127 * \li "mctx" is a valid memory context.
128 * \li dctxp != NULL && *dctxp == NULL
135 * \li *dctxp will contain a usable context.
139 dst_context_destroy(dst_context_t **dctxp);
141 * Destroys all memory associated with a context.
144 * \li *dctxp != NULL && *dctxp == NULL
151 dst_context_adddata(dst_context_t *dctx, const isc_region_t *data);
153 * Incrementally adds data to the context to be used in a sign or verify
157 * \li "dctx" is a valid context
158 * \li "data" is a valid region
162 * \li DST_R_SIGNFAILURE
163 * \li all other errors indicate failure
167 dst_context_sign(dst_context_t *dctx, isc_buffer_t *sig);
169 * Computes a signature using the data and key stored in the context.
172 * \li "dctx" is a valid context.
173 * \li "sig" is a valid buffer.
177 * \li DST_R_VERIFYFAILURE
178 * \li all other errors indicate failure
181 * \li "sig" will contain the signature
185 dst_context_verify(dst_context_t *dctx, isc_region_t *sig);
187 * Verifies the signature using the data and key stored in the context.
190 * \li "dctx" is a valid context.
191 * \li "sig" is a valid region.
195 * \li all other errors indicate failure
198 * \li "sig" will contain the signature
202 dst_key_computesecret(const dst_key_t *pub, const dst_key_t *priv,
203 isc_buffer_t *secret);
205 * Computes a shared secret from two (Diffie-Hellman) keys.
208 * \li "pub" is a valid key that can be used to derive a shared secret
209 * \li "priv" is a valid private key that can be used to derive a shared secret
210 * \li "secret" is a valid buffer
214 * \li any other result indicates failure
217 * \li If successful, secret will contain the derived shared secret.
221 dst_key_fromfile(dns_name_t *name, dns_keytag_t id, unsigned int alg, int type,
222 const char *directory, isc_mem_t *mctx, dst_key_t **keyp);
224 * Reads a key from permanent storage. The key can either be a public or
225 * private key, and is specified by name, algorithm, and id. If a private key
226 * is specified, the public key must also be present. If directory is NULL,
227 * the current directory is assumed.
230 * \li "name" is a valid absolute dns name.
231 * \li "id" is a valid key tag identifier.
232 * \li "alg" is a supported key algorithm.
233 * \li "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union.
234 * DST_TYPE_KEY look for a KEY record otherwise DNSKEY
235 * \li "mctx" is a valid memory context.
236 * \li "keyp" is not NULL and "*keyp" is NULL.
240 * \li any other result indicates failure
243 * \li If successful, *keyp will contain a valid key.
247 dst_key_fromnamedfile(const char *filename, int type, isc_mem_t *mctx,
250 * Reads a key from permanent storage. The key can either be a public or
251 * key, and is specified by filename. If a private key is specified, the
252 * public key must also be present.
255 * \li "filename" is not NULL
256 * \li "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union
257 * DST_TYPE_KEY look for a KEY record otherwise DNSKEY
258 * \li "mctx" is a valid memory context
259 * \li "keyp" is not NULL and "*keyp" is NULL.
263 * \li any other result indicates failure
266 * \li If successful, *keyp will contain a valid key.
271 dst_key_read_public(const char *filename, int type,
272 isc_mem_t *mctx, dst_key_t **keyp);
274 * Reads a public key from permanent storage. The key must be a public key.
277 * \li "filename" is not NULL
278 * \li "type" is DST_TYPE_KEY look for a KEY record otherwise DNSKEY
279 * \li "mctx" is a valid memory context
280 * \li "keyp" is not NULL and "*keyp" is NULL.
284 * \li DST_R_BADKEYTYPE if the key type is not the expected one
285 * \li ISC_R_UNEXPECTEDTOKEN if the file can not be parsed as a public key
286 * \li any other result indicates failure
289 * \li If successful, *keyp will contain a valid key.
293 dst_key_tofile(const dst_key_t *key, int type, const char *directory);
295 * Writes a key to permanent storage. The key can either be a public or
296 * private key. Public keys are written in DNS format and private keys
297 * are written as a set of base64 encoded values. If directory is NULL,
298 * the current directory is assumed.
301 * \li "key" is a valid key.
302 * \li "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union
306 * \li any other result indicates failure
310 dst_key_fromdns(dns_name_t *name, dns_rdataclass_t rdclass,
311 isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp);
313 * Converts a DNS KEY record into a DST key.
316 * \li "name" is a valid absolute dns name.
317 * \li "source" is a valid buffer. There must be at least 4 bytes available.
318 * \li "mctx" is a valid memory context.
319 * \li "keyp" is not NULL and "*keyp" is NULL.
323 * \li any other result indicates failure
326 * \li If successful, *keyp will contain a valid key, and the consumed
327 * pointer in data will be advanced.
331 dst_key_todns(const dst_key_t *key, isc_buffer_t *target);
333 * Converts a DST key into a DNS KEY record.
336 * \li "key" is a valid key.
337 * \li "target" is a valid buffer. There must be at least 4 bytes unused.
341 * \li any other result indicates failure
344 * \li If successful, the used pointer in 'target' is advanced by at least 4.
348 dst_key_frombuffer(dns_name_t *name, unsigned int alg,
349 unsigned int flags, unsigned int protocol,
350 dns_rdataclass_t rdclass,
351 isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp);
353 * Converts a buffer containing DNS KEY RDATA into a DST key.
356 *\li "name" is a valid absolute dns name.
357 *\li "alg" is a supported key algorithm.
358 *\li "source" is a valid buffer.
359 *\li "mctx" is a valid memory context.
360 *\li "keyp" is not NULL and "*keyp" is NULL.
364 * \li any other result indicates failure
367 *\li If successful, *keyp will contain a valid key, and the consumed
368 * pointer in source will be advanced.
372 dst_key_tobuffer(const dst_key_t *key, isc_buffer_t *target);
374 * Converts a DST key into DNS KEY RDATA format.
377 *\li "key" is a valid key.
378 *\li "target" is a valid buffer.
382 * \li any other result indicates failure
385 *\li If successful, the used pointer in 'target' is advanced.
389 dst_key_privatefrombuffer(dst_key_t *key, isc_buffer_t *buffer);
391 * Converts a public key into a private key, reading the private key
392 * information from the buffer. The buffer should contain the same data
393 * as the .private key file would.
396 *\li "key" is a valid public key.
397 *\li "buffer" is not NULL.
401 * \li any other result indicates failure
404 *\li If successful, key will contain a valid private key.
408 dst_key_getgssctx(const dst_key_t *key);
410 * Returns the opaque key data.
411 * Be cautions when using this value unless you know what you are doing.
414 *\li "key" is not NULL.
417 *\li gssctx key data, possibly NULL.
421 dst_key_fromgssapi(dns_name_t *name, gss_ctx_id_t gssctx, isc_mem_t *mctx,
424 * Converts a GSSAPI opaque context id into a DST key.
427 *\li "name" is a valid absolute dns name.
428 *\li "gssctx" is a GSSAPI context id.
429 *\li "mctx" is a valid memory context.
430 *\li "keyp" is not NULL and "*keyp" is NULL.
434 * \li any other result indicates failure
437 *\li If successful, *keyp will contain a valid key and be responsible for
442 dst_key_fromlabel(dns_name_t *name, int alg, unsigned int flags,
443 unsigned int protocol, dns_rdataclass_t rdclass,
444 const char *engine, const char *label, const char *pin,
445 isc_mem_t *mctx, dst_key_t **keyp);
448 dst_key_generate(dns_name_t *name, unsigned int alg,
449 unsigned int bits, unsigned int param,
450 unsigned int flags, unsigned int protocol,
451 dns_rdataclass_t rdclass,
452 isc_mem_t *mctx, dst_key_t **keyp);
454 * Generate a DST key (or keypair) with the supplied parameters. The
455 * interpretation of the "param" field depends on the algorithm:
459 * !0 use Fermat4 (2^16 + 1)
461 * 0 default - use well known prime if bits == 768 or 1024,
462 * otherwise use 2 as the generator.
463 * !0 use this value as the generator.
466 * 0 default - require good entropy
467 * !0 lack of good entropy is ok
471 *\li "name" is a valid absolute dns name.
472 *\li "keyp" is not NULL and "*keyp" is NULL.
476 * \li any other result indicates failure
479 *\li If successful, *keyp will contain a valid key.
483 dst_key_compare(const dst_key_t *key1, const dst_key_t *key2);
485 * Compares two DST keys.
488 *\li "key1" is a valid key.
489 *\li "key2" is a valid key.
497 dst_key_paramcompare(const dst_key_t *key1, const dst_key_t *key2);
499 * Compares the parameters of two DST keys. This is used to determine if
500 * two (Diffie-Hellman) keys can be used to derive a shared secret.
503 *\li "key1" is a valid key.
504 *\li "key2" is a valid key.
512 dst_key_free(dst_key_t **keyp);
514 * Release all memory associated with the key.
517 *\li "keyp" is not NULL and "*keyp" is a valid key.
520 *\li All memory associated with "*keyp" will be freed.
525 * Accessor functions to obtain key fields.
528 *\li "key" is a valid key.
531 dst_key_name(const dst_key_t *key);
534 dst_key_size(const dst_key_t *key);
537 dst_key_proto(const dst_key_t *key);
540 dst_key_alg(const dst_key_t *key);
543 dst_key_flags(const dst_key_t *key);
546 dst_key_id(const dst_key_t *key);
549 dst_key_class(const dst_key_t *key);
552 dst_key_isprivate(const dst_key_t *key);
555 dst_key_iszonekey(const dst_key_t *key);
558 dst_key_isnullkey(const dst_key_t *key);
561 dst_key_buildfilename(const dst_key_t *key, int type,
562 const char *directory, isc_buffer_t *out);
564 * Generates the filename used by dst to store the specified key.
565 * If directory is NULL, the current directory is assumed.
568 *\li "key" is a valid key
569 *\li "type" is either DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or 0 for no suffix.
570 *\li "out" is a valid buffer
573 *\li the file name will be written to "out", and the used pointer will
578 dst_key_sigsize(const dst_key_t *key, unsigned int *n);
580 * Computes the size of a signature generated by the given key.
583 *\li "key" is a valid key.
588 *\li DST_R_UNSUPPORTEDALG
591 *\li "n" stores the size of a generated signature
595 dst_key_secretsize(const dst_key_t *key, unsigned int *n);
597 * Computes the size of a shared secret generated by the given key.
600 *\li "key" is a valid key.
605 *\li DST_R_UNSUPPORTEDALG
608 *\li "n" stores the size of a generated shared secret
612 dst_region_computeid(const isc_region_t *source, unsigned int alg);
614 * Computes the key id of the key stored in the provided region with the
618 *\li "source" contains a valid, non-NULL region.
625 dst_key_getbits(const dst_key_t *key);
627 * Get the number of digest bits required (0 == MAX).
630 * "key" is a valid key.
634 dst_key_setbits(dst_key_t *key, isc_uint16_t bits);
636 * Set the number of digest bits required (0 == MAX).
639 * "key" is a valid key.
644 #endif /* DST_DST_H */