]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - contrib/bind9/lib/dns/include/dst/dst.h
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / contrib / bind9 / lib / dns / include / dst / dst.h
1 /*
2  * Copyright (C) 2004-2008  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2000-2002  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id: dst.h,v 1.12 2008/09/24 02:46:23 marka Exp $ */
19
20 #ifndef DST_DST_H
21 #define DST_DST_H 1
22
23 /*! \file dst/dst.h */
24
25 #include <isc/lang.h>
26
27 #include <dns/types.h>
28
29 #include <dst/gssapi.h>
30
31 ISC_LANG_BEGINDECLS
32
33 /***
34  *** Types
35  ***/
36
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.
41  */
42
43 typedef struct dst_key          dst_key_t;
44 typedef struct dst_context      dst_context_t;
45
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_NSEC3DSA        6
55 #define DST_ALG_NSEC3RSASHA1    7
56 #define DST_ALG_HMACMD5         157
57 #define DST_ALG_GSSAPI          160
58 #define DST_ALG_HMACSHA1        161     /* XXXMPA */
59 #define DST_ALG_HMACSHA224      162     /* XXXMPA */
60 #define DST_ALG_HMACSHA256      163     /* XXXMPA */
61 #define DST_ALG_HMACSHA384      164     /* XXXMPA */
62 #define DST_ALG_HMACSHA512      165     /* XXXMPA */
63 #define DST_ALG_PRIVATE         254
64 #define DST_ALG_EXPAND          255
65 #define DST_MAX_ALGS            255
66
67 /*% A buffer of this size is large enough to hold any key */
68 #define DST_KEY_MAXSIZE         1280
69
70 /*%
71  * A buffer of this size is large enough to hold the textual representation
72  * of any key
73  */
74 #define DST_KEY_MAXTEXTSIZE     2048
75
76 /*% 'Type' for dst_read_key() */
77 #define DST_TYPE_KEY            0x1000000       /* KEY key */
78 #define DST_TYPE_PRIVATE        0x2000000
79 #define DST_TYPE_PUBLIC         0x4000000
80
81 /***
82  *** Functions
83  ***/
84
85 isc_result_t
86 dst_lib_init(isc_mem_t *mctx, isc_entropy_t *ectx, unsigned int eflags);
87 /*%<
88  * Initializes the DST subsystem.
89  *
90  * Requires:
91  * \li  "mctx" is a valid memory context
92  * \li  "ectx" is a valid entropy context
93  *
94  * Returns:
95  * \li  ISC_R_SUCCESS
96  * \li  ISC_R_NOMEMORY
97  *
98  * Ensures:
99  * \li  DST is properly initialized.
100  */
101
102 void
103 dst_lib_destroy(void);
104 /*%<
105  * Releases all resources allocated by DST.
106  */
107
108 isc_boolean_t
109 dst_algorithm_supported(unsigned int alg);
110 /*%<
111  * Checks that a given algorithm is supported by DST.
112  *
113  * Returns:
114  * \li  ISC_TRUE
115  * \li  ISC_FALSE
116  */
117
118 isc_result_t
119 dst_context_create(dst_key_t *key, isc_mem_t *mctx, dst_context_t **dctxp);
120 /*%<
121  * Creates a context to be used for a sign or verify operation.
122  *
123  * Requires:
124  * \li  "key" is a valid key.
125  * \li  "mctx" is a valid memory context.
126  * \li  dctxp != NULL && *dctxp == NULL
127  *
128  * Returns:
129  * \li  ISC_R_SUCCESS
130  * \li  ISC_R_NOMEMORY
131  *
132  * Ensures:
133  * \li  *dctxp will contain a usable context.
134  */
135
136 void
137 dst_context_destroy(dst_context_t **dctxp);
138 /*%<
139  * Destroys all memory associated with a context.
140  *
141  * Requires:
142  * \li  *dctxp != NULL && *dctxp == NULL
143  *
144  * Ensures:
145  * \li  *dctxp == NULL
146  */
147
148 isc_result_t
149 dst_context_adddata(dst_context_t *dctx, const isc_region_t *data);
150 /*%<
151  * Incrementally adds data to the context to be used in a sign or verify
152  * operation.
153  *
154  * Requires:
155  * \li  "dctx" is a valid context
156  * \li  "data" is a valid region
157  *
158  * Returns:
159  * \li  ISC_R_SUCCESS
160  * \li  DST_R_SIGNFAILURE
161  * \li  all other errors indicate failure
162  */
163
164 isc_result_t
165 dst_context_sign(dst_context_t *dctx, isc_buffer_t *sig);
166 /*%<
167  * Computes a signature using the data and key stored in the context.
168  *
169  * Requires:
170  * \li  "dctx" is a valid context.
171  * \li  "sig" is a valid buffer.
172  *
173  * Returns:
174  * \li  ISC_R_SUCCESS
175  * \li  DST_R_VERIFYFAILURE
176  * \li  all other errors indicate failure
177  *
178  * Ensures:
179  * \li  "sig" will contain the signature
180  */
181
182 isc_result_t
183 dst_context_verify(dst_context_t *dctx, isc_region_t *sig);
184 /*%<
185  * Verifies the signature using the data and key stored in the context.
186  *
187  * Requires:
188  * \li  "dctx" is a valid context.
189  * \li  "sig" is a valid region.
190  *
191  * Returns:
192  * \li  ISC_R_SUCCESS
193  * \li  all other errors indicate failure
194  *
195  * Ensures:
196  * \li  "sig" will contain the signature
197  */
198
199 isc_result_t
200 dst_key_computesecret(const dst_key_t *pub, const dst_key_t *priv,
201                       isc_buffer_t *secret);
202 /*%<
203  * Computes a shared secret from two (Diffie-Hellman) keys.
204  *
205  * Requires:
206  * \li  "pub" is a valid key that can be used to derive a shared secret
207  * \li  "priv" is a valid private key that can be used to derive a shared secret
208  * \li  "secret" is a valid buffer
209  *
210  * Returns:
211  * \li  ISC_R_SUCCESS
212  * \li  any other result indicates failure
213  *
214  * Ensures:
215  * \li  If successful, secret will contain the derived shared secret.
216  */
217
218 isc_result_t
219 dst_key_fromfile(dns_name_t *name, dns_keytag_t id, unsigned int alg, int type,
220                  const char *directory, isc_mem_t *mctx, dst_key_t **keyp);
221 /*%<
222  * Reads a key from permanent storage.  The key can either be a public or
223  * private key, and is specified by name, algorithm, and id.  If a private key
224  * is specified, the public key must also be present.  If directory is NULL,
225  * the current directory is assumed.
226  *
227  * Requires:
228  * \li  "name" is a valid absolute dns name.
229  * \li  "id" is a valid key tag identifier.
230  * \li  "alg" is a supported key algorithm.
231  * \li  "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union.
232  *                DST_TYPE_KEY look for a KEY record otherwise DNSKEY
233  * \li  "mctx" is a valid memory context.
234  * \li  "keyp" is not NULL and "*keyp" is NULL.
235  *
236  * Returns:
237  * \li  ISC_R_SUCCESS
238  * \li  any other result indicates failure
239  *
240  * Ensures:
241  * \li  If successful, *keyp will contain a valid key.
242  */
243
244 isc_result_t
245 dst_key_fromnamedfile(const char *filename, int type, isc_mem_t *mctx,
246                       dst_key_t **keyp);
247 /*%<
248  * Reads a key from permanent storage.  The key can either be a public or
249  * key, and is specified by filename.  If a private key is specified, the
250  * public key must also be present.
251  *
252  * Requires:
253  * \li  "filename" is not NULL
254  * \li  "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union
255  *                DST_TYPE_KEY look for a KEY record otherwise DNSKEY
256  * \li  "mctx" is a valid memory context
257  * \li  "keyp" is not NULL and "*keyp" is NULL.
258  *
259  * Returns:
260  * \li  ISC_R_SUCCESS
261  * \li  any other result indicates failure
262  *
263  * Ensures:
264  * \li  If successful, *keyp will contain a valid key.
265  */
266
267
268 isc_result_t
269 dst_key_read_public(const char *filename, int type,
270                     isc_mem_t *mctx, dst_key_t **keyp);
271 /*%<
272  * Reads a public key from permanent storage.  The key must be a public key.
273  *
274  * Requires:
275  * \li  "filename" is not NULL
276  * \li  "type" is DST_TYPE_KEY look for a KEY record otherwise DNSKEY
277  * \li  "mctx" is a valid memory context
278  * \li  "keyp" is not NULL and "*keyp" is NULL.
279  *
280  * Returns:
281  * \li  ISC_R_SUCCESS
282  * \li  DST_R_BADKEYTYPE if the key type is not the expected one
283  * \li  ISC_R_UNEXPECTEDTOKEN if the file can not be parsed as a public key
284  * \li  any other result indicates failure
285  *
286  * Ensures:
287  * \li  If successful, *keyp will contain a valid key.
288  */
289
290 isc_result_t
291 dst_key_tofile(const dst_key_t *key, int type, const char *directory);
292 /*%<
293  * Writes a key to permanent storage.  The key can either be a public or
294  * private key.  Public keys are written in DNS format and private keys
295  * are written as a set of base64 encoded values.  If directory is NULL,
296  * the current directory is assumed.
297  *
298  * Requires:
299  * \li  "key" is a valid key.
300  * \li  "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union
301  *
302  * Returns:
303  * \li  ISC_R_SUCCESS
304  * \li  any other result indicates failure
305  */
306
307 isc_result_t
308 dst_key_fromdns(dns_name_t *name, dns_rdataclass_t rdclass,
309                 isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp);
310 /*%<
311  * Converts a DNS KEY record into a DST key.
312  *
313  * Requires:
314  * \li  "name" is a valid absolute dns name.
315  * \li  "source" is a valid buffer.  There must be at least 4 bytes available.
316  * \li  "mctx" is a valid memory context.
317  * \li  "keyp" is not NULL and "*keyp" is NULL.
318  *
319  * Returns:
320  * \li  ISC_R_SUCCESS
321  * \li  any other result indicates failure
322  *
323  * Ensures:
324  * \li  If successful, *keyp will contain a valid key, and the consumed
325  *      pointer in data will be advanced.
326  */
327
328 isc_result_t
329 dst_key_todns(const dst_key_t *key, isc_buffer_t *target);
330 /*%<
331  * Converts a DST key into a DNS KEY record.
332  *
333  * Requires:
334  * \li  "key" is a valid key.
335  * \li  "target" is a valid buffer.  There must be at least 4 bytes unused.
336  *
337  * Returns:
338  * \li  ISC_R_SUCCESS
339  * \li  any other result indicates failure
340  *
341  * Ensures:
342  * \li  If successful, the used pointer in 'target' is advanced by at least 4.
343  */
344
345 isc_result_t
346 dst_key_frombuffer(dns_name_t *name, unsigned int alg,
347                    unsigned int flags, unsigned int protocol,
348                    dns_rdataclass_t rdclass,
349                    isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp);
350 /*%<
351  * Converts a buffer containing DNS KEY RDATA into a DST key.
352  *
353  * Requires:
354  *\li   "name" is a valid absolute dns name.
355  *\li   "alg" is a supported key algorithm.
356  *\li   "source" is a valid buffer.
357  *\li   "mctx" is a valid memory context.
358  *\li   "keyp" is not NULL and "*keyp" is NULL.
359  *
360  * Returns:
361  *\li   ISC_R_SUCCESS
362  * \li  any other result indicates failure
363  *
364  * Ensures:
365  *\li   If successful, *keyp will contain a valid key, and the consumed
366  *      pointer in source will be advanced.
367  */
368
369 isc_result_t
370 dst_key_tobuffer(const dst_key_t *key, isc_buffer_t *target);
371 /*%<
372  * Converts a DST key into DNS KEY RDATA format.
373  *
374  * Requires:
375  *\li   "key" is a valid key.
376  *\li   "target" is a valid buffer.
377  *
378  * Returns:
379  *\li   ISC_R_SUCCESS
380  * \li  any other result indicates failure
381  *
382  * Ensures:
383  *\li   If successful, the used pointer in 'target' is advanced.
384  */
385
386 isc_result_t
387 dst_key_privatefrombuffer(dst_key_t *key, isc_buffer_t *buffer);
388 /*%<
389  * Converts a public key into a private key, reading the private key
390  * information from the buffer.  The buffer should contain the same data
391  * as the .private key file would.
392  *
393  * Requires:
394  *\li   "key" is a valid public key.
395  *\li   "buffer" is not NULL.
396  *
397  * Returns:
398  *\li   ISC_R_SUCCESS
399  * \li  any other result indicates failure
400  *
401  * Ensures:
402  *\li   If successful, key will contain a valid private key.
403  */
404
405 gss_ctx_id_t
406 dst_key_getgssctx(const dst_key_t *key);
407 /*%<
408  * Returns the opaque key data.
409  * Be cautions when using this value unless you know what you are doing.
410  *
411  * Requires:
412  *\li   "key" is not NULL.
413  *
414  * Returns:
415  *\li   gssctx key data, possibly NULL.
416  */
417
418 isc_result_t
419 dst_key_fromgssapi(dns_name_t *name, gss_ctx_id_t gssctx, isc_mem_t *mctx,
420                    dst_key_t **keyp);
421 /*%<
422  * Converts a GSSAPI opaque context id into a DST key.
423  *
424  * Requires:
425  *\li   "name" is a valid absolute dns name.
426  *\li   "gssctx" is a GSSAPI context id.
427  *\li   "mctx" is a valid memory context.
428  *\li   "keyp" is not NULL and "*keyp" is NULL.
429  *
430  * Returns:
431  *\li   ISC_R_SUCCESS
432  * \li  any other result indicates failure
433  *
434  * Ensures:
435  *\li   If successful, *keyp will contain a valid key and be responsible for
436  *      the context id.
437  */
438
439 isc_result_t
440 dst_key_fromlabel(dns_name_t *name, int alg, unsigned int flags,
441                   unsigned int protocol, dns_rdataclass_t rdclass,
442                   const char *engine, const char *label, const char *pin,
443                   isc_mem_t *mctx, dst_key_t **keyp);
444
445 isc_result_t
446 dst_key_generate(dns_name_t *name, unsigned int alg,
447                  unsigned int bits, unsigned int param,
448                  unsigned int flags, unsigned int protocol,
449                  dns_rdataclass_t rdclass,
450                  isc_mem_t *mctx, dst_key_t **keyp);
451 /*%<
452  * Generate a DST key (or keypair) with the supplied parameters.  The
453  * interpretation of the "param" field depends on the algorithm:
454  * \code
455  *      RSA:    exponent
456  *              0       use exponent 3
457  *              !0      use Fermat4 (2^16 + 1)
458  *      DH:     generator
459  *              0       default - use well known prime if bits == 768 or 1024,
460  *                      otherwise use 2 as the generator.
461  *              !0      use this value as the generator.
462  *      DSA:    unused
463  *      HMACMD5: entropy
464  *              0       default - require good entropy
465  *              !0      lack of good entropy is ok
466  *\endcode
467  *
468  * Requires:
469  *\li   "name" is a valid absolute dns name.
470  *\li   "keyp" is not NULL and "*keyp" is NULL.
471  *
472  * Returns:
473  *\li   ISC_R_SUCCESS
474  * \li  any other result indicates failure
475  *
476  * Ensures:
477  *\li   If successful, *keyp will contain a valid key.
478  */
479
480 isc_boolean_t
481 dst_key_compare(const dst_key_t *key1, const dst_key_t *key2);
482 /*%<
483  * Compares two DST keys.
484  *
485  * Requires:
486  *\li   "key1" is a valid key.
487  *\li   "key2" is a valid key.
488  *
489  * Returns:
490  *\li   ISC_TRUE
491  * \li  ISC_FALSE
492  */
493
494 isc_boolean_t
495 dst_key_paramcompare(const dst_key_t *key1, const dst_key_t *key2);
496 /*%<
497  * Compares the parameters of two DST keys.  This is used to determine if
498  * two (Diffie-Hellman) keys can be used to derive a shared secret.
499  *
500  * Requires:
501  *\li   "key1" is a valid key.
502  *\li   "key2" is a valid key.
503  *
504  * Returns:
505  *\li   ISC_TRUE
506  * \li  ISC_FALSE
507  */
508
509 void
510 dst_key_free(dst_key_t **keyp);
511 /*%<
512  * Release all memory associated with the key.
513  *
514  * Requires:
515  *\li   "keyp" is not NULL and "*keyp" is a valid key.
516  *
517  * Ensures:
518  *\li   All memory associated with "*keyp" will be freed.
519  *\li   *keyp == NULL
520  */
521
522 /*%<
523  * Accessor functions to obtain key fields.
524  *
525  * Require:
526  *\li   "key" is a valid key.
527  */
528 dns_name_t *
529 dst_key_name(const dst_key_t *key);
530
531 unsigned int
532 dst_key_size(const dst_key_t *key);
533
534 unsigned int
535 dst_key_proto(const dst_key_t *key);
536
537 unsigned int
538 dst_key_alg(const dst_key_t *key);
539
540 isc_uint32_t
541 dst_key_flags(const dst_key_t *key);
542
543 dns_keytag_t
544 dst_key_id(const dst_key_t *key);
545
546 dns_rdataclass_t
547 dst_key_class(const dst_key_t *key);
548
549 isc_boolean_t
550 dst_key_isprivate(const dst_key_t *key);
551
552 isc_boolean_t
553 dst_key_iszonekey(const dst_key_t *key);
554
555 isc_boolean_t
556 dst_key_isnullkey(const dst_key_t *key);
557
558 isc_result_t
559 dst_key_buildfilename(const dst_key_t *key, int type,
560                       const char *directory, isc_buffer_t *out);
561 /*%<
562  * Generates the filename used by dst to store the specified key.
563  * If directory is NULL, the current directory is assumed.
564  *
565  * Requires:
566  *\li   "key" is a valid key
567  *\li   "type" is either DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or 0 for no suffix.
568  *\li   "out" is a valid buffer
569  *
570  * Ensures:
571  *\li   the file name will be written to "out", and the used pointer will
572  *              be advanced.
573  */
574
575 isc_result_t
576 dst_key_sigsize(const dst_key_t *key, unsigned int *n);
577 /*%<
578  * Computes the size of a signature generated by the given key.
579  *
580  * Requires:
581  *\li   "key" is a valid key.
582  *\li   "n" is not NULL
583  *
584  * Returns:
585  *\li   #ISC_R_SUCCESS
586  *\li   DST_R_UNSUPPORTEDALG
587  *
588  * Ensures:
589  *\li   "n" stores the size of a generated signature
590  */
591
592 isc_result_t
593 dst_key_secretsize(const dst_key_t *key, unsigned int *n);
594 /*%<
595  * Computes the size of a shared secret generated by the given key.
596  *
597  * Requires:
598  *\li   "key" is a valid key.
599  *\li   "n" is not NULL
600  *
601  * Returns:
602  *\li   #ISC_R_SUCCESS
603  *\li   DST_R_UNSUPPORTEDALG
604  *
605  * Ensures:
606  *\li   "n" stores the size of a generated shared secret
607  */
608
609 isc_uint16_t
610 dst_region_computeid(const isc_region_t *source, unsigned int alg);
611 /*%<
612  * Computes the key id of the key stored in the provided region with the
613  * given algorithm.
614  *
615  * Requires:
616  *\li   "source" contains a valid, non-NULL region.
617  *
618  * Returns:
619  *\li   the key id
620  */
621
622 isc_uint16_t
623 dst_key_getbits(const dst_key_t *key);
624 /*
625  * Get the number of digest bits required (0 == MAX).
626  *
627  * Requires:
628  *      "key" is a valid key.
629  */
630
631 void
632 dst_key_setbits(dst_key_t *key, isc_uint16_t bits);
633 /*
634  * Set the number of digest bits required (0 == MAX).
635  *
636  * Requires:
637  *      "key" is a valid key.
638  */
639
640 ISC_LANG_ENDDECLS
641
642 #endif /* DST_DST_H */