2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001 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.42.2.2 2004/03/09 06:11:43 marka Exp $ */
25 #include <dns/types.h>
34 * The dst_key structure is opaque. Applications should use the accessor
35 * functions provided to retrieve key attributes. If an application needs
36 * to set attributes, new accessor functions will be written.
39 typedef struct dst_key dst_key_t;
40 typedef struct dst_context dst_context_t;
42 /* DST algorithm codes */
43 #define DST_ALG_UNKNOWN 0
44 #define DST_ALG_RSAMD5 1
45 #define DST_ALG_RSA DST_ALG_RSAMD5 /* backwards compatibility */
48 #define DST_ALG_HMACMD5 157
49 #define DST_ALG_GSSAPI 160
50 #define DST_ALG_PRIVATE 254
51 #define DST_ALG_EXPAND 255
52 #define DST_MAX_ALGS 255
54 /* A buffer of this size is large enough to hold any key */
55 #define DST_KEY_MAXSIZE 1280
58 * A buffer of this size is large enough to hold the textual representation
61 #define DST_KEY_MAXTEXTSIZE 2048
63 /* 'Type' for dst_read_key() */
64 #define DST_TYPE_PRIVATE 0x2000000
65 #define DST_TYPE_PUBLIC 0x4000000
72 dst_lib_init(isc_mem_t *mctx, isc_entropy_t *ectx, unsigned int eflags);
74 * Initializes the DST subsystem.
77 * "mctx" is a valid memory context
78 * "ectx" is a valid entropy context
85 * DST is properly initialized.
89 dst_lib_destroy(void);
91 * Releases all resources allocated by DST.
95 dst_algorithm_supported(unsigned int alg);
97 * Checks that a given algorithm is supported by DST.
105 dst_context_create(dst_key_t *key, isc_mem_t *mctx, dst_context_t **dctxp);
107 * Creates a context to be used for a sign or verify operation.
110 * "key" is a valid key.
111 * "mctx" is a valid memory context.
112 * dctxp != NULL && *dctxp == NULL
119 * *dctxp will contain a usable context.
123 dst_context_destroy(dst_context_t **dctxp);
125 * Destroys all memory associated with a context.
128 * *dctxp != NULL && *dctxp == NULL
135 dst_context_adddata(dst_context_t *dctx, const isc_region_t *data);
137 * Incrementally adds data to the context to be used in a sign or verify
141 * "dctx" is a valid context
142 * "data" is a valid region
147 * all other errors indicate failure
151 dst_context_sign(dst_context_t *dctx, isc_buffer_t *sig);
153 * Computes a signature using the data and key stored in the context.
156 * "dctx" is a valid context.
157 * "sig" is a valid buffer.
161 * DST_R_VERIFYFAILURE
162 * all other errors indicate failure
165 * "sig" will contain the signature
169 dst_context_verify(dst_context_t *dctx, isc_region_t *sig);
171 * Verifies the signature using the data and key stored in the context.
174 * "dctx" is a valid context.
175 * "sig" is a valid region.
179 * all other errors indicate failure
182 * "sig" will contain the signature
186 dst_key_computesecret(const dst_key_t *pub, const dst_key_t *priv,
187 isc_buffer_t *secret);
189 * Computes a shared secret from two (Diffie-Hellman) keys.
192 * "pub" is a valid key that can be used to derive a shared secret
193 * "priv" is a valid private key that can be used to derive a shared secret
194 * "secret" is a valid buffer
198 * any other result indicates failure
201 * If successful, secret will contain the derived shared secret.
205 dst_key_fromfile(dns_name_t *name, dns_keytag_t id, unsigned int alg, int type,
206 const char *directory, isc_mem_t *mctx, dst_key_t **keyp);
208 * Reads a key from permanent storage. The key can either be a public or
209 * private key, and is specified by name, algorithm, and id. If a private key
210 * is specified, the public key must also be present. If directory is NULL,
211 * the current directory is assumed.
214 * "name" is a valid absolute dns name.
215 * "id" is a valid key tag identifier.
216 * "alg" is a supported key algorithm.
217 * "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union.
218 * "mctx" is a valid memory context.
219 * "keyp" is not NULL and "*keyp" is NULL.
223 * any other result indicates failure
226 * If successful, *keyp will contain a valid key.
230 dst_key_fromnamedfile(const char *filename, int type, isc_mem_t *mctx,
233 * Reads a key from permanent storage. The key can either be a public or
234 * key, and is specified by filename. If a private key is specified, the
235 * public key must also be present.
238 * "filename" is not NULL
239 * "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union
240 * "mctx" is a valid memory context
241 * "keyp" is not NULL and "*keyp" is NULL.
245 * any other result indicates failure
248 * If successful, *keyp will contain a valid key.
252 dst_key_tofile(const dst_key_t *key, int type, const char *directory);
254 * Writes a key to permanent storage. The key can either be a public or
255 * private key. Public keys are written in DNS format and private keys
256 * are written as a set of base64 encoded values. If directory is NULL,
257 * the current directory is assumed.
260 * "key" is a valid key.
261 * "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union
265 * any other result indicates failure
269 dst_key_fromdns(dns_name_t *name, dns_rdataclass_t rdclass,
270 isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp);
272 * Converts a DNS KEY record into a DST key.
275 * "name" is a valid absolute dns name.
276 * "source" is a valid buffer. There must be at least 4 bytes available.
277 * "mctx" is a valid memory context.
278 * "keyp" is not NULL and "*keyp" is NULL.
282 * any other result indicates failure
285 * If successful, *keyp will contain a valid key, and the consumed
286 * pointer in data will be advanced.
290 dst_key_todns(const dst_key_t *key, isc_buffer_t *target);
292 * Converts a DST key into a DNS KEY record.
295 * "key" is a valid key.
296 * "target" is a valid buffer. There must be at least 4 bytes unused.
300 * any other result indicates failure
303 * If successful, the used pointer in 'target' is advanced by at least 4.
307 dst_key_frombuffer(dns_name_t *name, unsigned int alg,
308 unsigned int flags, unsigned int protocol,
309 dns_rdataclass_t rdclass,
310 isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp);
312 * Converts a buffer containing DNS KEY RDATA into a DST key.
315 * "name" is a valid absolute dns name.
316 * "alg" is a supported key algorithm.
317 * "source" is a valid buffer.
318 * "mctx" is a valid memory context.
319 * "keyp" is not NULL and "*keyp" is NULL.
323 * any other result indicates failure
326 * If successful, *keyp will contain a valid key, and the consumed
327 * pointer in source will be advanced.
331 dst_key_tobuffer(const dst_key_t *key, isc_buffer_t *target);
333 * Converts a DST key into DNS KEY RDATA format.
336 * "key" is a valid key.
337 * "target" is a valid buffer.
341 * any other result indicates failure
344 * If successful, the used pointer in 'target' is advanced.
348 dst_key_fromgssapi(dns_name_t *name, void *opaque, isc_mem_t *mctx,
351 * Converts a GSSAPI opaque context id into a DST key.
354 * "name" is a valid absolute dns name.
355 * "opaque" is a GSSAPI context id.
356 * "mctx" is a valid memory context.
357 * "keyp" is not NULL and "*keyp" is NULL.
361 * any other result indicates failure
364 * If successful, *keyp will contain a valid key and be responsible for
369 dst_key_generate(dns_name_t *name, unsigned int alg,
370 unsigned int bits, unsigned int param,
371 unsigned int flags, unsigned int protocol,
372 dns_rdataclass_t rdclass,
373 isc_mem_t *mctx, dst_key_t **keyp);
375 * Generate a DST key (or keypair) with the supplied parameters. The
376 * interpretation of the "param" field depends on the algorithm:
379 * !0 use Fermat4 (2^16 + 1)
381 * 0 default - use well known prime if bits == 768 or 1024,
382 * otherwise use 2 as the generator.
383 * !0 use this value as the generator.
386 * 0 default - require good entropy
387 * !0 lack of good entropy is ok
390 * "name" is a valid absolute dns name.
391 * "keyp" is not NULL and "*keyp" is NULL.
395 * any other result indicates failure
398 * If successful, *keyp will contain a valid key.
402 dst_key_compare(const dst_key_t *key1, const dst_key_t *key2);
404 * Compares two DST keys.
407 * "key1" is a valid key.
408 * "key2" is a valid key.
416 dst_key_paramcompare(const dst_key_t *key1, const dst_key_t *key2);
418 * Compares the parameters of two DST keys. This is used to determine if
419 * two (Diffie-Hellman) keys can be used to derive a shared secret.
422 * "key1" is a valid key.
423 * "key2" is a valid key.
431 dst_key_free(dst_key_t **keyp);
433 * Release all memory associated with the key.
436 * "keyp" is not NULL and "*keyp" is a valid key.
439 * All memory associated with "*keyp" will be freed.
444 * Accessor functions to obtain key fields.
447 * "key" is a valid key.
450 dst_key_name(const dst_key_t *key);
453 dst_key_size(const dst_key_t *key);
456 dst_key_proto(const dst_key_t *key);
459 dst_key_alg(const dst_key_t *key);
462 dst_key_flags(const dst_key_t *key);
465 dst_key_id(const dst_key_t *key);
468 dst_key_class(const dst_key_t *key);
471 dst_key_isprivate(const dst_key_t *key);
474 dst_key_iszonekey(const dst_key_t *key);
477 dst_key_isnullkey(const dst_key_t *key);
480 dst_key_buildfilename(const dst_key_t *key, int type,
481 const char *directory, isc_buffer_t *out);
483 * Generates the filename used by dst to store the specified key.
484 * If directory is NULL, the current directory is assumed.
487 * "key" is a valid key
488 * "type" is either DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or 0 for no suffix.
489 * "out" is a valid buffer
492 * the file name will be written to "out", and the used pointer will
497 dst_key_sigsize(const dst_key_t *key, unsigned int *n);
499 * Computes the size of a signature generated by the given key.
502 * "key" is a valid key.
507 * DST_R_UNSUPPORTEDALG
510 * "n" stores the size of a generated signature
514 dst_key_secretsize(const dst_key_t *key, unsigned int *n);
516 * Computes the size of a shared secret generated by the given key.
519 * "key" is a valid key.
524 * DST_R_UNSUPPORTEDALG
527 * "n" stores the size of a generated shared secret
531 dst_region_computeid(const isc_region_t *source, unsigned int alg);
533 * Computes the key id of the key stored in the provided region with the
537 * "source" contains a valid, non-NULL region.
545 #endif /* DST_DST_H */