Merge branch 'vendor/OPENSSH'
[dragonfly.git] / contrib / bind / lib / dns / include / dst / dst.h
1 /*
2  * Copyright (C) 2004-2007  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.9 2007/06/19 23:47:17 tbox 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_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
64
65 /*% A buffer of this size is large enough to hold any key */
66 #define DST_KEY_MAXSIZE         1280
67
68 /*%
69  * A buffer of this size is large enough to hold the textual representation
70  * of any key
71  */
72 #define DST_KEY_MAXTEXTSIZE     2048
73
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
78
79 /***
80  *** Functions
81  ***/
82
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.
87  *
88  * Requires:
89  * \li  "mctx" is a valid memory context
90  * \li  "ectx" is a valid entropy context
91  *
92  * Returns:
93  * \li  ISC_R_SUCCESS
94  * \li  ISC_R_NOMEMORY
95  *
96  * Ensures:
97  * \li  DST is properly initialized.
98  */
99
100 void
101 dst_lib_destroy(void);
102 /*%<
103  * Releases all resources allocated by DST.
104  */
105
106 isc_boolean_t
107 dst_algorithm_supported(unsigned int alg);
108 /*%<
109  * Checks that a given algorithm is supported by DST.
110  *
111  * Returns:
112  * \li  ISC_TRUE
113  * \li  ISC_FALSE
114  */
115
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.
120  *
121  * Requires:
122  * \li  "key" is a valid key.
123  * \li  "mctx" is a valid memory context.
124  * \li  dctxp != NULL && *dctxp == NULL
125  *
126  * Returns:
127  * \li  ISC_R_SUCCESS
128  * \li  ISC_R_NOMEMORY
129  *
130  * Ensures:
131  * \li  *dctxp will contain a usable context.
132  */
133
134 void
135 dst_context_destroy(dst_context_t **dctxp);
136 /*%<
137  * Destroys all memory associated with a context.
138  *
139  * Requires:
140  * \li  *dctxp != NULL && *dctxp == NULL
141  *
142  * Ensures:
143  * \li  *dctxp == NULL
144  */
145
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.
151  *
152  * Requires:
153  * \li  "dctx" is a valid context
154  * \li  "data" is a valid region
155  *
156  * Returns:
157  * \li  ISC_R_SUCCESS
158  * \li  DST_R_SIGNFAILURE
159  * \li  all other errors indicate failure
160  */
161
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.
166  *
167  * Requires:
168  * \li  "dctx" is a valid context.
169  * \li  "sig" is a valid buffer.
170  *
171  * Returns:
172  * \li  ISC_R_SUCCESS
173  * \li  DST_R_VERIFYFAILURE
174  * \li  all other errors indicate failure
175  *
176  * Ensures:
177  * \li  "sig" will contain the signature
178  */
179
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.
184  *
185  * Requires:
186  * \li  "dctx" is a valid context.
187  * \li  "sig" is a valid region.
188  *
189  * Returns:
190  * \li  ISC_R_SUCCESS
191  * \li  all other errors indicate failure
192  *
193  * Ensures:
194  * \li  "sig" will contain the signature
195  */
196
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.
202  *
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
207  *
208  * Returns:
209  * \li  ISC_R_SUCCESS
210  * \li  any other result indicates failure
211  *
212  * Ensures:
213  * \li  If successful, secret will contain the derived shared secret.
214  */
215
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.
224  *
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.
233  *
234  * Returns:
235  * \li  ISC_R_SUCCESS
236  * \li  any other result indicates failure
237  *
238  * Ensures:
239  * \li  If successful, *keyp will contain a valid key.
240  */
241
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.
249  *
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.
256  *
257  * Returns:
258  * \li  ISC_R_SUCCESS
259  * \li  any other result indicates failure
260  *
261  * Ensures:
262  * \li  If successful, *keyp will contain a valid key.
263  */
264
265
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.
271  *
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.
277  *
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
283  *
284  * Ensures:
285  * \li  If successful, *keyp will contain a valid key.
286  */
287
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.
295  *
296  * Requires:
297  * \li  "key" is a valid key.
298  * \li  "type" is DST_TYPE_PUBLIC, DST_TYPE_PRIVATE, or the bitwise union
299  *
300  * Returns:
301  * \li  ISC_R_SUCCESS
302  * \li  any other result indicates failure
303  */
304
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.
310  *
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.
316  *
317  * Returns:
318  * \li  ISC_R_SUCCESS
319  * \li  any other result indicates failure
320  *
321  * Ensures:
322  * \li  If successful, *keyp will contain a valid key, and the consumed
323  *      pointer in data will be advanced.
324  */
325
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.
330  *
331  * Requires:
332  * \li  "key" is a valid key.
333  * \li  "target" is a valid buffer.  There must be at least 4 bytes unused.
334  *
335  * Returns:
336  * \li  ISC_R_SUCCESS
337  * \li  any other result indicates failure
338  *
339  * Ensures:
340  * \li  If successful, the used pointer in 'target' is advanced by at least 4.
341  */
342
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.
350  *
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.
357  *
358  * Returns:
359  *\li   ISC_R_SUCCESS
360  * \li  any other result indicates failure
361  *
362  * Ensures:
363  *\li   If successful, *keyp will contain a valid key, and the consumed
364  *      pointer in source will be advanced.
365  */
366
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.
371  *
372  * Requires:
373  *\li   "key" is a valid key.
374  *\li   "target" is a valid buffer.
375  *
376  * Returns:
377  *\li   ISC_R_SUCCESS
378  * \li  any other result indicates failure
379  *
380  * Ensures:
381  *\li   If successful, the used pointer in 'target' is advanced.
382  */
383
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.
390  *
391  * Requires:
392  *\li   "key" is a valid public key.
393  *\li   "buffer" is not NULL.
394  *
395  * Returns:
396  *\li   ISC_R_SUCCESS
397  * \li  any other result indicates failure
398  *
399  * Ensures:
400  *\li   If successful, key will contain a valid private key.
401  */
402
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.
408  *
409  * Requires:
410  *\li   "key" is not NULL.
411  *
412  * Returns:
413  *\li   gssctx key data, possibly NULL.
414  */
415
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.
421  *
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.
427  *
428  * Returns:
429  *\li   ISC_R_SUCCESS
430  * \li  any other result indicates failure
431  *
432  * Ensures:
433  *\li   If successful, *keyp will contain a valid key and be responsible for
434  *      the context id.
435  */
436
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
459  *
460  * Requires:
461  *\li   "name" is a valid absolute dns name.
462  *\li   "keyp" is not NULL and "*keyp" is NULL.
463  *
464  * Returns:
465  *\li   ISC_R_SUCCESS
466  * \li  any other result indicates failure
467  *
468  * Ensures:
469  *\li   If successful, *keyp will contain a valid key.
470  */
471
472 isc_boolean_t
473 dst_key_compare(const dst_key_t *key1, const dst_key_t *key2);
474 /*%<
475  * Compares two DST keys.
476  *
477  * Requires:
478  *\li   "key1" is a valid key.
479  *\li   "key2" is a valid key.
480  *
481  * Returns:
482  *\li   ISC_TRUE
483  * \li  ISC_FALSE
484  */
485
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.
491  *
492  * Requires:
493  *\li   "key1" is a valid key.
494  *\li   "key2" is a valid key.
495  *
496  * Returns:
497  *\li   ISC_TRUE
498  * \li  ISC_FALSE
499  */
500
501 void
502 dst_key_free(dst_key_t **keyp);
503 /*%<
504  * Release all memory associated with the key.
505  *
506  * Requires:
507  *\li   "keyp" is not NULL and "*keyp" is a valid key.
508  *
509  * Ensures:
510  *\li   All memory associated with "*keyp" will be freed.
511  *\li   *keyp == NULL
512  */
513
514 /*%<
515  * Accessor functions to obtain key fields.
516  *
517  * Require:
518  *\li   "key" is a valid key.
519  */
520 dns_name_t *
521 dst_key_name(const dst_key_t *key);
522
523 unsigned int
524 dst_key_size(const dst_key_t *key);
525
526 unsigned int
527 dst_key_proto(const dst_key_t *key);
528
529 unsigned int
530 dst_key_alg(const dst_key_t *key);
531
532 isc_uint32_t
533 dst_key_flags(const dst_key_t *key);
534
535 dns_keytag_t
536 dst_key_id(const dst_key_t *key);
537
538 dns_rdataclass_t
539 dst_key_class(const dst_key_t *key);
540
541 isc_boolean_t
542 dst_key_isprivate(const dst_key_t *key);
543
544 isc_boolean_t
545 dst_key_iszonekey(const dst_key_t *key);
546
547 isc_boolean_t
548 dst_key_isnullkey(const dst_key_t *key);
549
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.
556  *
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
561  *
562  * Ensures:
563  *\li   the file name will be written to "out", and the used pointer will
564  *              be advanced.
565  */
566
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.
571  *
572  * Requires:
573  *\li   "key" is a valid key.
574  *\li   "n" is not NULL
575  *
576  * Returns:
577  *\li   #ISC_R_SUCCESS
578  *\li   DST_R_UNSUPPORTEDALG
579  *
580  * Ensures:
581  *\li   "n" stores the size of a generated signature
582  */
583
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.
588  *
589  * Requires:
590  *\li   "key" is a valid key.
591  *\li   "n" is not NULL
592  *
593  * Returns:
594  *\li   #ISC_R_SUCCESS
595  *\li   DST_R_UNSUPPORTEDALG
596  *
597  * Ensures:
598  *\li   "n" stores the size of a generated shared secret
599  */
600
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.
606  *
607  * Requires:
608  *\li   "source" contains a valid, non-NULL region.
609  *
610  * Returns:
611  *\li   the key id
612  */
613
614 isc_uint16_t
615 dst_key_getbits(const dst_key_t *key);
616 /*
617  * Get the number of digest bits required (0 == MAX).
618  *
619  * Requires:
620  *      "key" is a valid key.
621  */
622
623 void
624 dst_key_setbits(dst_key_t *key, isc_uint16_t bits);
625 /*
626  * Set the number of digest bits required (0 == MAX).
627  *
628  * Requires:
629  *      "key" is a valid key.
630  */
631
632 ISC_LANG_ENDDECLS
633
634 #endif /* DST_DST_H */