Import bind 9.5.2 vendor sources.
[dragonfly.git] / contrib / bind-9.5.2 / lib / dns / include / dst / dst.h
blob2473bf6deef7cec7cb1671224ae4789a72879841
1 /*
2 * Copyright (C) 2004-2007 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.9 2007/06/19 23:47:17 tbox Exp $ */
20 #ifndef DST_DST_H
21 #define DST_DST_H 1
23 /*! \file dst/dst.h */
25 #include <isc/lang.h>
27 #include <dns/types.h>
29 #include <dst/gssapi.h>
31 ISC_LANG_BEGINDECLS
33 /***
34 *** Types
35 ***/
37 /*%
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 */
50 #define DST_ALG_DH 2
51 #define DST_ALG_DSA 3
52 #define DST_ALG_ECC 4
53 #define DST_ALG_RSASHA1 5
54 #define DST_ALG_HMACMD5 157
55 #define DST_ALG_GSSAPI 160
56 #define DST_ALG_HMACSHA1 161 /* XXXMPA */
57 #define DST_ALG_HMACSHA224 162 /* XXXMPA */
58 #define DST_ALG_HMACSHA256 163 /* XXXMPA */
59 #define DST_ALG_HMACSHA384 164 /* XXXMPA */
60 #define DST_ALG_HMACSHA512 165 /* XXXMPA */
61 #define DST_ALG_PRIVATE 254
62 #define DST_ALG_EXPAND 255
63 #define DST_MAX_ALGS 255
65 /*% A buffer of this size is large enough to hold any key */
66 #define DST_KEY_MAXSIZE 1280
68 /*%
69 * A buffer of this size is large enough to hold the textual representation
70 * of any key
72 #define DST_KEY_MAXTEXTSIZE 2048
74 /*% 'Type' for dst_read_key() */
75 #define DST_TYPE_KEY 0x1000000 /* KEY key */
76 #define DST_TYPE_PRIVATE 0x2000000
77 #define DST_TYPE_PUBLIC 0x4000000
79 /***
80 *** Functions
81 ***/
83 isc_result_t
84 dst_lib_init(isc_mem_t *mctx, isc_entropy_t *ectx, unsigned int eflags);
85 /*%<
86 * Initializes the DST subsystem.
88 * Requires:
89 * \li "mctx" is a valid memory context
90 * \li "ectx" is a valid entropy context
92 * Returns:
93 * \li ISC_R_SUCCESS
94 * \li ISC_R_NOMEMORY
96 * Ensures:
97 * \li DST is properly initialized.
100 void
101 dst_lib_destroy(void);
102 /*%<
103 * Releases all resources allocated by DST.
106 isc_boolean_t
107 dst_algorithm_supported(unsigned int alg);
108 /*%<
109 * Checks that a given algorithm is supported by DST.
111 * Returns:
112 * \li ISC_TRUE
113 * \li ISC_FALSE
116 isc_result_t
117 dst_context_create(dst_key_t *key, isc_mem_t *mctx, dst_context_t **dctxp);
118 /*%<
119 * Creates a context to be used for a sign or verify operation.
121 * Requires:
122 * \li "key" is a valid key.
123 * \li "mctx" is a valid memory context.
124 * \li dctxp != NULL && *dctxp == NULL
126 * Returns:
127 * \li ISC_R_SUCCESS
128 * \li ISC_R_NOMEMORY
130 * Ensures:
131 * \li *dctxp will contain a usable context.
134 void
135 dst_context_destroy(dst_context_t **dctxp);
136 /*%<
137 * Destroys all memory associated with a context.
139 * Requires:
140 * \li *dctxp != NULL && *dctxp == NULL
142 * Ensures:
143 * \li *dctxp == NULL
146 isc_result_t
147 dst_context_adddata(dst_context_t *dctx, const isc_region_t *data);
148 /*%<
149 * Incrementally adds data to the context to be used in a sign or verify
150 * operation.
152 * Requires:
153 * \li "dctx" is a valid context
154 * \li "data" is a valid region
156 * Returns:
157 * \li ISC_R_SUCCESS
158 * \li DST_R_SIGNFAILURE
159 * \li all other errors indicate failure
162 isc_result_t
163 dst_context_sign(dst_context_t *dctx, isc_buffer_t *sig);
164 /*%<
165 * Computes a signature using the data and key stored in the context.
167 * Requires:
168 * \li "dctx" is a valid context.
169 * \li "sig" is a valid buffer.
171 * Returns:
172 * \li ISC_R_SUCCESS
173 * \li DST_R_VERIFYFAILURE
174 * \li all other errors indicate failure
176 * Ensures:
177 * \li "sig" will contain the signature
180 isc_result_t
181 dst_context_verify(dst_context_t *dctx, isc_region_t *sig);
182 /*%<
183 * Verifies the signature using the data and key stored in the context.
185 * Requires:
186 * \li "dctx" is a valid context.
187 * \li "sig" is a valid region.
189 * Returns:
190 * \li ISC_R_SUCCESS
191 * \li all other errors indicate failure
193 * Ensures:
194 * \li "sig" will contain the signature
197 isc_result_t
198 dst_key_computesecret(const dst_key_t *pub, const dst_key_t *priv,
199 isc_buffer_t *secret);
200 /*%<
201 * Computes a shared secret from two (Diffie-Hellman) keys.
203 * Requires:
204 * \li "pub" is a valid key that can be used to derive a shared secret
205 * \li "priv" is a valid private key that can be used to derive a shared secret
206 * \li "secret" is a valid buffer
208 * Returns:
209 * \li ISC_R_SUCCESS
210 * \li any other result indicates failure
212 * Ensures:
213 * \li If successful, secret will contain the derived shared secret.
216 isc_result_t
217 dst_key_fromfile(dns_name_t *name, dns_keytag_t id, unsigned int alg, int type,
218 const char *directory, isc_mem_t *mctx, dst_key_t **keyp);
219 /*%<
220 * Reads a key from permanent storage. The key can either be a public or
221 * private key, and is specified by name, algorithm, and id. If a private key
222 * is specified, the public key must also be present. If directory is NULL,
223 * the current directory is assumed.
225 * Requires:
226 * \li "name" is a valid absolute dns name.
227 * \li "id" is a valid key tag identifier.
228 * \li "alg" is a supported key algorithm.
229 * \li "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union.
230 * DST_TYPE_KEY look for a KEY record otherwise DNSKEY
231 * \li "mctx" is a valid memory context.
232 * \li "keyp" is not NULL and "*keyp" is NULL.
234 * Returns:
235 * \li ISC_R_SUCCESS
236 * \li any other result indicates failure
238 * Ensures:
239 * \li If successful, *keyp will contain a valid key.
242 isc_result_t
243 dst_key_fromnamedfile(const char *filename, int type, isc_mem_t *mctx,
244 dst_key_t **keyp);
245 /*%<
246 * Reads a key from permanent storage. The key can either be a public or
247 * key, and is specified by filename. If a private key is specified, the
248 * public key must also be present.
250 * Requires:
251 * \li "filename" is not NULL
252 * \li "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union
253 * DST_TYPE_KEY look for a KEY record otherwise DNSKEY
254 * \li "mctx" is a valid memory context
255 * \li "keyp" is not NULL and "*keyp" is NULL.
257 * Returns:
258 * \li ISC_R_SUCCESS
259 * \li any other result indicates failure
261 * Ensures:
262 * \li If successful, *keyp will contain a valid key.
266 isc_result_t
267 dst_key_read_public(const char *filename, int type,
268 isc_mem_t *mctx, dst_key_t **keyp);
269 /*%<
270 * Reads a public key from permanent storage. The key must be a public key.
272 * Requires:
273 * \li "filename" is not NULL
274 * \li "type" is DST_TYPE_KEY look for a KEY record otherwise DNSKEY
275 * \li "mctx" is a valid memory context
276 * \li "keyp" is not NULL and "*keyp" is NULL.
278 * Returns:
279 * \li ISC_R_SUCCESS
280 * \li DST_R_BADKEYTYPE if the key type is not the expected one
281 * \li ISC_R_UNEXPECTEDTOKEN if the file can not be parsed as a public key
282 * \li any other result indicates failure
284 * Ensures:
285 * \li If successful, *keyp will contain a valid key.
288 isc_result_t
289 dst_key_tofile(const dst_key_t *key, int type, const char *directory);
290 /*%<
291 * Writes a key to permanent storage. The key can either be a public or
292 * private key. Public keys are written in DNS format and private keys
293 * are written as a set of base64 encoded values. If directory is NULL,
294 * the current directory is assumed.
296 * Requires:
297 * \li "key" is a valid key.
298 * \li "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union
300 * Returns:
301 * \li ISC_R_SUCCESS
302 * \li any other result indicates failure
305 isc_result_t
306 dst_key_fromdns(dns_name_t *name, dns_rdataclass_t rdclass,
307 isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp);
308 /*%<
309 * Converts a DNS KEY record into a DST key.
311 * Requires:
312 * \li "name" is a valid absolute dns name.
313 * \li "source" is a valid buffer. There must be at least 4 bytes available.
314 * \li "mctx" is a valid memory context.
315 * \li "keyp" is not NULL and "*keyp" is NULL.
317 * Returns:
318 * \li ISC_R_SUCCESS
319 * \li any other result indicates failure
321 * Ensures:
322 * \li If successful, *keyp will contain a valid key, and the consumed
323 * pointer in data will be advanced.
326 isc_result_t
327 dst_key_todns(const dst_key_t *key, isc_buffer_t *target);
328 /*%<
329 * Converts a DST key into a DNS KEY record.
331 * Requires:
332 * \li "key" is a valid key.
333 * \li "target" is a valid buffer. There must be at least 4 bytes unused.
335 * Returns:
336 * \li ISC_R_SUCCESS
337 * \li any other result indicates failure
339 * Ensures:
340 * \li If successful, the used pointer in 'target' is advanced by at least 4.
343 isc_result_t
344 dst_key_frombuffer(dns_name_t *name, unsigned int alg,
345 unsigned int flags, unsigned int protocol,
346 dns_rdataclass_t rdclass,
347 isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp);
348 /*%<
349 * Converts a buffer containing DNS KEY RDATA into a DST key.
351 * Requires:
352 *\li "name" is a valid absolute dns name.
353 *\li "alg" is a supported key algorithm.
354 *\li "source" is a valid buffer.
355 *\li "mctx" is a valid memory context.
356 *\li "keyp" is not NULL and "*keyp" is NULL.
358 * Returns:
359 *\li ISC_R_SUCCESS
360 * \li any other result indicates failure
362 * Ensures:
363 *\li If successful, *keyp will contain a valid key, and the consumed
364 * pointer in source will be advanced.
367 isc_result_t
368 dst_key_tobuffer(const dst_key_t *key, isc_buffer_t *target);
369 /*%<
370 * Converts a DST key into DNS KEY RDATA format.
372 * Requires:
373 *\li "key" is a valid key.
374 *\li "target" is a valid buffer.
376 * Returns:
377 *\li ISC_R_SUCCESS
378 * \li any other result indicates failure
380 * Ensures:
381 *\li If successful, the used pointer in 'target' is advanced.
384 isc_result_t
385 dst_key_privatefrombuffer(dst_key_t *key, isc_buffer_t *buffer);
386 /*%<
387 * Converts a public key into a private key, reading the private key
388 * information from the buffer. The buffer should contain the same data
389 * as the .private key file would.
391 * Requires:
392 *\li "key" is a valid public key.
393 *\li "buffer" is not NULL.
395 * Returns:
396 *\li ISC_R_SUCCESS
397 * \li any other result indicates failure
399 * Ensures:
400 *\li If successful, key will contain a valid private key.
403 gss_ctx_id_t
404 dst_key_getgssctx(const dst_key_t *key);
405 /*%<
406 * Returns the opaque key data.
407 * Be cautions when using this value unless you know what you are doing.
409 * Requires:
410 *\li "key" is not NULL.
412 * Returns:
413 *\li gssctx key data, possibly NULL.
416 isc_result_t
417 dst_key_fromgssapi(dns_name_t *name, gss_ctx_id_t gssctx, isc_mem_t *mctx,
418 dst_key_t **keyp);
419 /*%<
420 * Converts a GSSAPI opaque context id into a DST key.
422 * Requires:
423 *\li "name" is a valid absolute dns name.
424 *\li "gssctx" is a GSSAPI context id.
425 *\li "mctx" is a valid memory context.
426 *\li "keyp" is not NULL and "*keyp" is NULL.
428 * Returns:
429 *\li ISC_R_SUCCESS
430 * \li any other result indicates failure
432 * Ensures:
433 *\li If successful, *keyp will contain a valid key and be responsible for
434 * the context id.
437 isc_result_t
438 dst_key_generate(dns_name_t *name, unsigned int alg,
439 unsigned int bits, unsigned int param,
440 unsigned int flags, unsigned int protocol,
441 dns_rdataclass_t rdclass,
442 isc_mem_t *mctx, dst_key_t **keyp);
443 /*%<
444 * Generate a DST key (or keypair) with the supplied parameters. The
445 * interpretation of the "param" field depends on the algorithm:
446 * \code
447 * RSA: exponent
448 * 0 use exponent 3
449 * !0 use Fermat4 (2^16 + 1)
450 * DH: generator
451 * 0 default - use well known prime if bits == 768 or 1024,
452 * otherwise use 2 as the generator.
453 * !0 use this value as the generator.
454 * DSA: unused
455 * HMACMD5: entropy
456 * 0 default - require good entropy
457 * !0 lack of good entropy is ok
458 *\endcode
460 * Requires:
461 *\li "name" is a valid absolute dns name.
462 *\li "keyp" is not NULL and "*keyp" is NULL.
464 * Returns:
465 *\li ISC_R_SUCCESS
466 * \li any other result indicates failure
468 * Ensures:
469 *\li If successful, *keyp will contain a valid key.
472 isc_boolean_t
473 dst_key_compare(const dst_key_t *key1, const dst_key_t *key2);
474 /*%<
475 * Compares two DST keys.
477 * Requires:
478 *\li "key1" is a valid key.
479 *\li "key2" is a valid key.
481 * Returns:
482 *\li ISC_TRUE
483 * \li ISC_FALSE
486 isc_boolean_t
487 dst_key_paramcompare(const dst_key_t *key1, const dst_key_t *key2);
488 /*%<
489 * Compares the parameters of two DST keys. This is used to determine if
490 * two (Diffie-Hellman) keys can be used to derive a shared secret.
492 * Requires:
493 *\li "key1" is a valid key.
494 *\li "key2" is a valid key.
496 * Returns:
497 *\li ISC_TRUE
498 * \li ISC_FALSE
501 void
502 dst_key_free(dst_key_t **keyp);
503 /*%<
504 * Release all memory associated with the key.
506 * Requires:
507 *\li "keyp" is not NULL and "*keyp" is a valid key.
509 * Ensures:
510 *\li All memory associated with "*keyp" will be freed.
511 *\li *keyp == NULL
514 /*%<
515 * Accessor functions to obtain key fields.
517 * Require:
518 *\li "key" is a valid key.
520 dns_name_t *
521 dst_key_name(const dst_key_t *key);
523 unsigned int
524 dst_key_size(const dst_key_t *key);
526 unsigned int
527 dst_key_proto(const dst_key_t *key);
529 unsigned int
530 dst_key_alg(const dst_key_t *key);
532 isc_uint32_t
533 dst_key_flags(const dst_key_t *key);
535 dns_keytag_t
536 dst_key_id(const dst_key_t *key);
538 dns_rdataclass_t
539 dst_key_class(const dst_key_t *key);
541 isc_boolean_t
542 dst_key_isprivate(const dst_key_t *key);
544 isc_boolean_t
545 dst_key_iszonekey(const dst_key_t *key);
547 isc_boolean_t
548 dst_key_isnullkey(const dst_key_t *key);
550 isc_result_t
551 dst_key_buildfilename(const dst_key_t *key, int type,
552 const char *directory, isc_buffer_t *out);
553 /*%<
554 * Generates the filename used by dst to store the specified key.
555 * If directory is NULL, the current directory is assumed.
557 * Requires:
558 *\li "key" is a valid key
559 *\li "type" is either DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or 0 for no suffix.
560 *\li "out" is a valid buffer
562 * Ensures:
563 *\li the file name will be written to "out", and the used pointer will
564 * be advanced.
567 isc_result_t
568 dst_key_sigsize(const dst_key_t *key, unsigned int *n);
569 /*%<
570 * Computes the size of a signature generated by the given key.
572 * Requires:
573 *\li "key" is a valid key.
574 *\li "n" is not NULL
576 * Returns:
577 *\li #ISC_R_SUCCESS
578 *\li DST_R_UNSUPPORTEDALG
580 * Ensures:
581 *\li "n" stores the size of a generated signature
584 isc_result_t
585 dst_key_secretsize(const dst_key_t *key, unsigned int *n);
586 /*%<
587 * Computes the size of a shared secret generated by the given key.
589 * Requires:
590 *\li "key" is a valid key.
591 *\li "n" is not NULL
593 * Returns:
594 *\li #ISC_R_SUCCESS
595 *\li DST_R_UNSUPPORTEDALG
597 * Ensures:
598 *\li "n" stores the size of a generated shared secret
601 isc_uint16_t
602 dst_region_computeid(const isc_region_t *source, unsigned int alg);
603 /*%<
604 * Computes the key id of the key stored in the provided region with the
605 * given algorithm.
607 * Requires:
608 *\li "source" contains a valid, non-NULL region.
610 * Returns:
611 *\li the key id
614 isc_uint16_t
615 dst_key_getbits(const dst_key_t *key);
617 * Get the number of digest bits required (0 == MAX).
619 * Requires:
620 * "key" is a valid key.
623 void
624 dst_key_setbits(dst_key_t *key, isc_uint16_t bits);
626 * Set the number of digest bits required (0 == MAX).
628 * Requires:
629 * "key" is a valid key.
632 ISC_LANG_ENDDECLS
634 #endif /* DST_DST_H */