Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / dns / sec / dst / include / dst / dst.h
blobd7b59165ff6e9bfa7e38cf9b1281bcf1ea5459b6
1 /*
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 $ */
20 #ifndef DST_DST_H
21 #define DST_DST_H 1
23 #include <isc/lang.h>
25 #include <dns/types.h>
27 ISC_LANG_BEGINDECLS
29 /***
30 *** Types
31 ***/
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 */
46 #define DST_ALG_DH 2
47 #define DST_ALG_DSA 3
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
59 * of any key
61 #define DST_KEY_MAXTEXTSIZE 2048
63 /* 'Type' for dst_read_key() */
64 #define DST_TYPE_PRIVATE 0x2000000
65 #define DST_TYPE_PUBLIC 0x4000000
67 /***
68 *** Functions
69 ***/
71 isc_result_t
72 dst_lib_init(isc_mem_t *mctx, isc_entropy_t *ectx, unsigned int eflags);
74 * Initializes the DST subsystem.
76 * Requires:
77 * "mctx" is a valid memory context
78 * "ectx" is a valid entropy context
80 * Returns:
81 * ISC_R_SUCCESS
82 * ISC_R_NOMEMORY
84 * Ensures:
85 * DST is properly initialized.
88 void
89 dst_lib_destroy(void);
91 * Releases all resources allocated by DST.
94 isc_boolean_t
95 dst_algorithm_supported(unsigned int alg);
97 * Checks that a given algorithm is supported by DST.
99 * Returns:
100 * ISC_TRUE
101 * ISC_FALSE
104 isc_result_t
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.
109 * Requires:
110 * "key" is a valid key.
111 * "mctx" is a valid memory context.
112 * dctxp != NULL && *dctxp == NULL
114 * Returns:
115 * ISC_R_SUCCESS
116 * ISC_R_NOMEMORY
118 * Ensures:
119 * *dctxp will contain a usable context.
122 void
123 dst_context_destroy(dst_context_t **dctxp);
125 * Destroys all memory associated with a context.
127 * Requires:
128 * *dctxp != NULL && *dctxp == NULL
130 * Ensures:
131 * *dctxp == NULL
134 isc_result_t
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
138 * operation.
140 * Requires:
141 * "dctx" is a valid context
142 * "data" is a valid region
144 * Returns:
145 * ISC_R_SUCCESS
146 * DST_R_SIGNFAILURE
147 * all other errors indicate failure
150 isc_result_t
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.
155 * Requires:
156 * "dctx" is a valid context.
157 * "sig" is a valid buffer.
159 * Returns:
160 * ISC_R_SUCCESS
161 * DST_R_VERIFYFAILURE
162 * all other errors indicate failure
164 * Ensures:
165 * "sig" will contain the signature
168 isc_result_t
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.
173 * Requires:
174 * "dctx" is a valid context.
175 * "sig" is a valid region.
177 * Returns:
178 * ISC_R_SUCCESS
179 * all other errors indicate failure
181 * Ensures:
182 * "sig" will contain the signature
185 isc_result_t
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.
191 * Requires:
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
196 * Returns:
197 * ISC_R_SUCCESS
198 * any other result indicates failure
200 * Ensures:
201 * If successful, secret will contain the derived shared secret.
204 isc_result_t
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.
213 * Requires:
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.
221 * Returns:
222 * ISC_R_SUCCESS
223 * any other result indicates failure
225 * Ensures:
226 * If successful, *keyp will contain a valid key.
229 isc_result_t
230 dst_key_fromnamedfile(const char *filename, int type, isc_mem_t *mctx,
231 dst_key_t **keyp);
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.
237 * Requires:
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.
243 * Returns:
244 * ISC_R_SUCCESS
245 * any other result indicates failure
247 * Ensures:
248 * If successful, *keyp will contain a valid key.
251 isc_result_t
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.
259 * Requires:
260 * "key" is a valid key.
261 * "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union
263 * Returns:
264 * ISC_R_SUCCESS
265 * any other result indicates failure
268 isc_result_t
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.
274 * Requires:
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.
280 * Returns:
281 * ISC_R_SUCCESS
282 * any other result indicates failure
284 * Ensures:
285 * If successful, *keyp will contain a valid key, and the consumed
286 * pointer in data will be advanced.
289 isc_result_t
290 dst_key_todns(const dst_key_t *key, isc_buffer_t *target);
292 * Converts a DST key into a DNS KEY record.
294 * Requires:
295 * "key" is a valid key.
296 * "target" is a valid buffer. There must be at least 4 bytes unused.
298 * Returns:
299 * ISC_R_SUCCESS
300 * any other result indicates failure
302 * Ensures:
303 * If successful, the used pointer in 'target' is advanced by at least 4.
306 isc_result_t
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.
314 * Requires:
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.
321 * Returns:
322 * ISC_R_SUCCESS
323 * any other result indicates failure
325 * Ensures:
326 * If successful, *keyp will contain a valid key, and the consumed
327 * pointer in source will be advanced.
330 isc_result_t
331 dst_key_tobuffer(const dst_key_t *key, isc_buffer_t *target);
333 * Converts a DST key into DNS KEY RDATA format.
335 * Requires:
336 * "key" is a valid key.
337 * "target" is a valid buffer.
339 * Returns:
340 * ISC_R_SUCCESS
341 * any other result indicates failure
343 * Ensures:
344 * If successful, the used pointer in 'target' is advanced.
347 isc_result_t
348 dst_key_fromgssapi(dns_name_t *name, void *opaque, isc_mem_t *mctx,
349 dst_key_t **keyp);
351 * Converts a GSSAPI opaque context id into a DST key.
353 * Requires:
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.
359 * Returns:
360 * ISC_R_SUCCESS
361 * any other result indicates failure
363 * Ensures:
364 * If successful, *keyp will contain a valid key and be responsible for
365 * the context id.
368 isc_result_t
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:
377 * RSA: exponent
378 * 0 use exponent 3
379 * !0 use Fermat4 (2^16 + 1)
380 * DH: generator
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.
384 * DSA: unused
385 * HMACMD5: entropy
386 * 0 default - require good entropy
387 * !0 lack of good entropy is ok
389 * Requires:
390 * "name" is a valid absolute dns name.
391 * "keyp" is not NULL and "*keyp" is NULL.
393 * Returns:
394 * ISC_R_SUCCESS
395 * any other result indicates failure
397 * Ensures:
398 * If successful, *keyp will contain a valid key.
401 isc_boolean_t
402 dst_key_compare(const dst_key_t *key1, const dst_key_t *key2);
404 * Compares two DST keys.
406 * Requires:
407 * "key1" is a valid key.
408 * "key2" is a valid key.
410 * Returns:
411 * ISC_TRUE
412 * ISC_FALSE
415 isc_boolean_t
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.
421 * Requires:
422 * "key1" is a valid key.
423 * "key2" is a valid key.
425 * Returns:
426 * ISC_TRUE
427 * ISC_FALSE
430 void
431 dst_key_free(dst_key_t **keyp);
433 * Release all memory associated with the key.
435 * Requires:
436 * "keyp" is not NULL and "*keyp" is a valid key.
438 * Ensures:
439 * All memory associated with "*keyp" will be freed.
440 * *keyp == NULL
444 * Accessor functions to obtain key fields.
446 * Require:
447 * "key" is a valid key.
449 dns_name_t *
450 dst_key_name(const dst_key_t *key);
452 unsigned int
453 dst_key_size(const dst_key_t *key);
455 unsigned int
456 dst_key_proto(const dst_key_t *key);
458 unsigned int
459 dst_key_alg(const dst_key_t *key);
461 isc_uint32_t
462 dst_key_flags(const dst_key_t *key);
464 dns_keytag_t
465 dst_key_id(const dst_key_t *key);
467 dns_rdataclass_t
468 dst_key_class(const dst_key_t *key);
470 isc_boolean_t
471 dst_key_isprivate(const dst_key_t *key);
473 isc_boolean_t
474 dst_key_iszonekey(const dst_key_t *key);
476 isc_boolean_t
477 dst_key_isnullkey(const dst_key_t *key);
479 isc_result_t
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.
486 * Requires:
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
491 * Ensures:
492 * the file name will be written to "out", and the used pointer will
493 * be advanced.
496 isc_result_t
497 dst_key_sigsize(const dst_key_t *key, unsigned int *n);
499 * Computes the size of a signature generated by the given key.
501 * Requires:
502 * "key" is a valid key.
503 * "n" is not NULL
505 * Returns:
506 * ISC_R_SUCCESS
507 * DST_R_UNSUPPORTEDALG
509 * Ensures:
510 * "n" stores the size of a generated signature
513 isc_result_t
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.
518 * Requires:
519 * "key" is a valid key.
520 * "n" is not NULL
522 * Returns:
523 * ISC_R_SUCCESS
524 * DST_R_UNSUPPORTEDALG
526 * Ensures:
527 * "n" stores the size of a generated shared secret
530 isc_uint16_t
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
534 * given algorithm.
536 * Requires:
537 * "source" contains a valid, non-NULL region.
539 * Returns:
540 * the key id
543 ISC_LANG_ENDDECLS
545 #endif /* DST_DST_H */