]> CyberLeo.Net >> Repos - FreeBSD/releng/9.3.git/blob - contrib/bind9/lib/dns/dst_api.c
Fix resource exhaustion in TCP reassembly. [SA-15:15]
[FreeBSD/releng/9.3.git] / contrib / bind9 / lib / dns / dst_api.c
1 /*
2  * Portions Copyright (C) 2004-2013  Internet Systems Consortium, Inc. ("ISC")
3  * Portions Copyright (C) 1999-2003  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 AND NETWORK ASSOCIATES DISCLAIMS
10  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE
12  * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  *
17  * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
18  *
19  * Permission to use, copy, modify, and/or distribute this software for any
20  * purpose with or without fee is hereby granted, provided that the above
21  * copyright notice and this permission notice appear in all copies.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
24  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE
26  * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
27  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
28  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
29  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30  */
31
32 /*
33  * Principal Author: Brian Wellington
34  * $Id: dst_api.c,v 1.65 2011/10/20 21:20:02 marka Exp $
35  */
36
37 /*! \file */
38
39 #include <config.h>
40
41 #include <stdlib.h>
42 #include <time.h>
43
44 #include <isc/buffer.h>
45 #include <isc/dir.h>
46 #include <isc/entropy.h>
47 #include <isc/fsaccess.h>
48 #include <isc/hmacsha.h>
49 #include <isc/lex.h>
50 #include <isc/mem.h>
51 #include <isc/once.h>
52 #include <isc/platform.h>
53 #include <isc/print.h>
54 #include <isc/refcount.h>
55 #include <isc/random.h>
56 #include <isc/string.h>
57 #include <isc/time.h>
58 #include <isc/util.h>
59 #include <isc/file.h>
60
61 #define DST_KEY_INTERNAL
62
63 #include <dns/fixedname.h>
64 #include <dns/keyvalues.h>
65 #include <dns/name.h>
66 #include <dns/rdata.h>
67 #include <dns/rdataclass.h>
68 #include <dns/ttl.h>
69 #include <dns/types.h>
70
71 #include <dst/result.h>
72
73 #include "dst_internal.h"
74
75 #define DST_AS_STR(t) ((t).value.as_textregion.base)
76
77 static dst_func_t *dst_t_func[DST_MAX_ALGS];
78 #ifdef BIND9
79 static isc_entropy_t *dst_entropy_pool = NULL;
80 #endif
81 static unsigned int dst_entropy_flags = 0;
82 static isc_boolean_t dst_initialized = ISC_FALSE;
83
84 void gss_log(int level, const char *fmt, ...) ISC_FORMAT_PRINTF(2, 3);
85
86 isc_mem_t *dst__memory_pool = NULL;
87
88 /*
89  * Static functions.
90  */
91 static dst_key_t *      get_key_struct(dns_name_t *name,
92                                        unsigned int alg,
93                                        unsigned int flags,
94                                        unsigned int protocol,
95                                        unsigned int bits,
96                                        dns_rdataclass_t rdclass,
97                                        dns_ttl_t ttl,
98                                        isc_mem_t *mctx);
99 static isc_result_t     write_public_key(const dst_key_t *key, int type,
100                                          const char *directory);
101 static isc_result_t     buildfilename(dns_name_t *name,
102                                       dns_keytag_t id,
103                                       unsigned int alg,
104                                       unsigned int type,
105                                       const char *directory,
106                                       isc_buffer_t *out);
107 static isc_result_t     computeid(dst_key_t *key);
108 static isc_result_t     frombuffer(dns_name_t *name,
109                                    unsigned int alg,
110                                    unsigned int flags,
111                                    unsigned int protocol,
112                                    dns_rdataclass_t rdclass,
113                                    isc_buffer_t *source,
114                                    isc_mem_t *mctx,
115                                    dst_key_t **keyp);
116
117 static isc_result_t     algorithm_status(unsigned int alg);
118
119 static isc_result_t     addsuffix(char *filename, int len,
120                                   const char *dirname, const char *ofilename,
121                                   const char *suffix);
122
123 #define RETERR(x)                               \
124         do {                                    \
125                 result = (x);                   \
126                 if (result != ISC_R_SUCCESS)    \
127                         goto out;               \
128         } while (0)
129
130 #define CHECKALG(alg)                           \
131         do {                                    \
132                 isc_result_t _r;                \
133                 _r = algorithm_status(alg);     \
134                 if (_r != ISC_R_SUCCESS)        \
135                         return (_r);            \
136         } while (0);                            \
137
138 #if defined(OPENSSL) && defined(BIND9)
139 static void *
140 default_memalloc(void *arg, size_t size) {
141         UNUSED(arg);
142         if (size == 0U)
143                 size = 1;
144         return (malloc(size));
145 }
146
147 static void
148 default_memfree(void *arg, void *ptr) {
149         UNUSED(arg);
150         free(ptr);
151 }
152 #endif
153
154 isc_result_t
155 dst_lib_init(isc_mem_t *mctx, isc_entropy_t *ectx, unsigned int eflags) {
156         return (dst_lib_init2(mctx, ectx, NULL, eflags));
157 }
158
159 isc_result_t
160 dst_lib_init2(isc_mem_t *mctx, isc_entropy_t *ectx,
161               const char *engine, unsigned int eflags) {
162         isc_result_t result;
163
164         REQUIRE(mctx != NULL);
165 #ifdef BIND9
166         REQUIRE(ectx != NULL);
167 #else
168         UNUSED(ectx);
169 #endif
170         REQUIRE(dst_initialized == ISC_FALSE);
171
172 #ifndef OPENSSL
173         UNUSED(engine);
174 #endif
175
176         dst__memory_pool = NULL;
177
178 #if defined(OPENSSL) && defined(BIND9)
179         UNUSED(mctx);
180         /*
181          * When using --with-openssl, there seems to be no good way of not
182          * leaking memory due to the openssl error handling mechanism.
183          * Avoid assertions by using a local memory context and not checking
184          * for leaks on exit.  Note: as there are leaks we cannot use
185          * ISC_MEMFLAG_INTERNAL as it will free up memory still being used
186          * by libcrypto.
187          */
188         result = isc_mem_createx2(0, 0, default_memalloc, default_memfree,
189                                   NULL, &dst__memory_pool, 0);
190         if (result != ISC_R_SUCCESS)
191                 return (result);
192         isc_mem_setname(dst__memory_pool, "dst", NULL);
193 #ifndef OPENSSL_LEAKS
194         isc_mem_setdestroycheck(dst__memory_pool, ISC_FALSE);
195 #endif
196 #else
197         isc_mem_attach(mctx, &dst__memory_pool);
198 #endif
199 #ifdef BIND9
200         isc_entropy_attach(ectx, &dst_entropy_pool);
201 #endif
202         dst_entropy_flags = eflags;
203
204         dst_result_register();
205
206         memset(dst_t_func, 0, sizeof(dst_t_func));
207         RETERR(dst__hmacmd5_init(&dst_t_func[DST_ALG_HMACMD5]));
208         RETERR(dst__hmacsha1_init(&dst_t_func[DST_ALG_HMACSHA1]));
209         RETERR(dst__hmacsha224_init(&dst_t_func[DST_ALG_HMACSHA224]));
210         RETERR(dst__hmacsha256_init(&dst_t_func[DST_ALG_HMACSHA256]));
211         RETERR(dst__hmacsha384_init(&dst_t_func[DST_ALG_HMACSHA384]));
212         RETERR(dst__hmacsha512_init(&dst_t_func[DST_ALG_HMACSHA512]));
213 #ifdef OPENSSL
214         RETERR(dst__openssl_init(engine));
215         RETERR(dst__opensslrsa_init(&dst_t_func[DST_ALG_RSAMD5],
216                                     DST_ALG_RSAMD5));
217         RETERR(dst__opensslrsa_init(&dst_t_func[DST_ALG_RSASHA1],
218                                     DST_ALG_RSASHA1));
219         RETERR(dst__opensslrsa_init(&dst_t_func[DST_ALG_NSEC3RSASHA1],
220                                     DST_ALG_NSEC3RSASHA1));
221         RETERR(dst__opensslrsa_init(&dst_t_func[DST_ALG_RSASHA256],
222                                     DST_ALG_RSASHA256));
223         RETERR(dst__opensslrsa_init(&dst_t_func[DST_ALG_RSASHA512],
224                                     DST_ALG_RSASHA512));
225 #ifdef HAVE_OPENSSL_DSA
226         RETERR(dst__openssldsa_init(&dst_t_func[DST_ALG_DSA]));
227         RETERR(dst__openssldsa_init(&dst_t_func[DST_ALG_NSEC3DSA]));
228 #endif
229         RETERR(dst__openssldh_init(&dst_t_func[DST_ALG_DH]));
230 #ifdef HAVE_OPENSSL_GOST
231         RETERR(dst__opensslgost_init(&dst_t_func[DST_ALG_ECCGOST]));
232 #endif
233 #ifdef HAVE_OPENSSL_ECDSA
234         RETERR(dst__opensslecdsa_init(&dst_t_func[DST_ALG_ECDSA256]));
235         RETERR(dst__opensslecdsa_init(&dst_t_func[DST_ALG_ECDSA384]));
236 #endif
237 #endif /* OPENSSL */
238 #ifdef GSSAPI
239         RETERR(dst__gssapi_init(&dst_t_func[DST_ALG_GSSAPI]));
240 #endif
241         dst_initialized = ISC_TRUE;
242         return (ISC_R_SUCCESS);
243
244  out:
245         /* avoid immediate crash! */
246         dst_initialized = ISC_TRUE;
247         dst_lib_destroy();
248         return (result);
249 }
250
251 void
252 dst_lib_destroy(void) {
253         int i;
254         RUNTIME_CHECK(dst_initialized == ISC_TRUE);
255         dst_initialized = ISC_FALSE;
256
257         for (i = 0; i < DST_MAX_ALGS; i++)
258                 if (dst_t_func[i] != NULL && dst_t_func[i]->cleanup != NULL)
259                         dst_t_func[i]->cleanup();
260 #ifdef OPENSSL
261         dst__openssl_destroy();
262 #endif
263         if (dst__memory_pool != NULL)
264                 isc_mem_detach(&dst__memory_pool);
265 #ifdef BIND9
266         if (dst_entropy_pool != NULL)
267                 isc_entropy_detach(&dst_entropy_pool);
268 #endif
269 }
270
271 isc_boolean_t
272 dst_algorithm_supported(unsigned int alg) {
273         REQUIRE(dst_initialized == ISC_TRUE);
274
275         if (alg >= DST_MAX_ALGS || dst_t_func[alg] == NULL)
276                 return (ISC_FALSE);
277         return (ISC_TRUE);
278 }
279
280 isc_result_t
281 dst_context_create(dst_key_t *key, isc_mem_t *mctx, dst_context_t **dctxp) {
282         return (dst_context_create2(key, mctx,
283                                     DNS_LOGCATEGORY_GENERAL, dctxp));
284 }
285
286 isc_result_t
287 dst_context_create2(dst_key_t *key, isc_mem_t *mctx,
288                     isc_logcategory_t *category, dst_context_t **dctxp) {
289         dst_context_t *dctx;
290         isc_result_t result;
291
292         REQUIRE(dst_initialized == ISC_TRUE);
293         REQUIRE(VALID_KEY(key));
294         REQUIRE(mctx != NULL);
295         REQUIRE(dctxp != NULL && *dctxp == NULL);
296
297         if (key->func->createctx == NULL)
298                 return (DST_R_UNSUPPORTEDALG);
299         if (key->keydata.generic == NULL)
300                 return (DST_R_NULLKEY);
301
302         dctx = isc_mem_get(mctx, sizeof(dst_context_t));
303         if (dctx == NULL)
304                 return (ISC_R_NOMEMORY);
305         dctx->key = key;
306         dctx->mctx = mctx;
307         dctx->category = category;
308         result = key->func->createctx(key, dctx);
309         if (result != ISC_R_SUCCESS) {
310                 isc_mem_put(mctx, dctx, sizeof(dst_context_t));
311                 return (result);
312         }
313         dctx->magic = CTX_MAGIC;
314         *dctxp = dctx;
315         return (ISC_R_SUCCESS);
316 }
317
318 void
319 dst_context_destroy(dst_context_t **dctxp) {
320         dst_context_t *dctx;
321
322         REQUIRE(dctxp != NULL && VALID_CTX(*dctxp));
323
324         dctx = *dctxp;
325         INSIST(dctx->key->func->destroyctx != NULL);
326         dctx->key->func->destroyctx(dctx);
327         dctx->magic = 0;
328         isc_mem_put(dctx->mctx, dctx, sizeof(dst_context_t));
329         *dctxp = NULL;
330 }
331
332 isc_result_t
333 dst_context_adddata(dst_context_t *dctx, const isc_region_t *data) {
334         REQUIRE(VALID_CTX(dctx));
335         REQUIRE(data != NULL);
336         INSIST(dctx->key->func->adddata != NULL);
337
338         return (dctx->key->func->adddata(dctx, data));
339 }
340
341 isc_result_t
342 dst_context_sign(dst_context_t *dctx, isc_buffer_t *sig) {
343         dst_key_t *key;
344
345         REQUIRE(VALID_CTX(dctx));
346         REQUIRE(sig != NULL);
347
348         key = dctx->key;
349         CHECKALG(key->key_alg);
350         if (key->keydata.generic == NULL)
351                 return (DST_R_NULLKEY);
352
353         if (key->func->sign == NULL)
354                 return (DST_R_NOTPRIVATEKEY);
355         if (key->func->isprivate == NULL ||
356             key->func->isprivate(key) == ISC_FALSE)
357                 return (DST_R_NOTPRIVATEKEY);
358
359         return (key->func->sign(dctx, sig));
360 }
361
362 isc_result_t
363 dst_context_verify(dst_context_t *dctx, isc_region_t *sig) {
364         REQUIRE(VALID_CTX(dctx));
365         REQUIRE(sig != NULL);
366
367         CHECKALG(dctx->key->key_alg);
368         if (dctx->key->keydata.generic == NULL)
369                 return (DST_R_NULLKEY);
370         if (dctx->key->func->verify == NULL)
371                 return (DST_R_NOTPUBLICKEY);
372
373         return (dctx->key->func->verify(dctx, sig));
374 }
375
376 isc_result_t
377 dst_context_verify2(dst_context_t *dctx, unsigned int maxbits,
378                     isc_region_t *sig)
379 {
380         REQUIRE(VALID_CTX(dctx));
381         REQUIRE(sig != NULL);
382
383         CHECKALG(dctx->key->key_alg);
384         if (dctx->key->keydata.generic == NULL)
385                 return (DST_R_NULLKEY);
386         if (dctx->key->func->verify == NULL &&
387             dctx->key->func->verify2 == NULL)
388                 return (DST_R_NOTPUBLICKEY);
389
390         return (dctx->key->func->verify2 != NULL ?
391                 dctx->key->func->verify2(dctx, maxbits, sig) :
392                 dctx->key->func->verify(dctx, sig));
393 }
394
395 isc_result_t
396 dst_key_computesecret(const dst_key_t *pub, const dst_key_t *priv,
397                       isc_buffer_t *secret)
398 {
399         REQUIRE(dst_initialized == ISC_TRUE);
400         REQUIRE(VALID_KEY(pub) && VALID_KEY(priv));
401         REQUIRE(secret != NULL);
402
403         CHECKALG(pub->key_alg);
404         CHECKALG(priv->key_alg);
405
406         if (pub->keydata.generic == NULL || priv->keydata.generic == NULL)
407                 return (DST_R_NULLKEY);
408
409         if (pub->key_alg != priv->key_alg ||
410             pub->func->computesecret == NULL ||
411             priv->func->computesecret == NULL)
412                 return (DST_R_KEYCANNOTCOMPUTESECRET);
413
414         if (dst_key_isprivate(priv) == ISC_FALSE)
415                 return (DST_R_NOTPRIVATEKEY);
416
417         return (pub->func->computesecret(pub, priv, secret));
418 }
419
420 isc_result_t
421 dst_key_tofile(const dst_key_t *key, int type, const char *directory) {
422         isc_result_t ret = ISC_R_SUCCESS;
423
424         REQUIRE(dst_initialized == ISC_TRUE);
425         REQUIRE(VALID_KEY(key));
426         REQUIRE((type & (DST_TYPE_PRIVATE | DST_TYPE_PUBLIC)) != 0);
427
428         CHECKALG(key->key_alg);
429
430         if (key->func->tofile == NULL)
431                 return (DST_R_UNSUPPORTEDALG);
432
433         if (type & DST_TYPE_PUBLIC) {
434                 ret = write_public_key(key, type, directory);
435                 if (ret != ISC_R_SUCCESS)
436                         return (ret);
437         }
438
439         if ((type & DST_TYPE_PRIVATE) &&
440             (key->key_flags & DNS_KEYFLAG_TYPEMASK) != DNS_KEYTYPE_NOKEY)
441                 return (key->func->tofile(key, directory));
442         else
443                 return (ISC_R_SUCCESS);
444 }
445
446 void
447 dst_key_setexternal(dst_key_t *key, isc_boolean_t value) {
448         key->external = value;
449 }
450
451 isc_boolean_t
452 dst_key_isexternal(dst_key_t *key) {
453         return (key->external);
454 }
455
456 isc_result_t
457 dst_key_fromfile(dns_name_t *name, dns_keytag_t id,
458                  unsigned int alg, int type, const char *directory,
459                  isc_mem_t *mctx, dst_key_t **keyp)
460 {
461         char filename[ISC_DIR_NAMEMAX];
462         isc_buffer_t b;
463         dst_key_t *key;
464         isc_result_t result;
465
466         REQUIRE(dst_initialized == ISC_TRUE);
467         REQUIRE(dns_name_isabsolute(name));
468         REQUIRE((type & (DST_TYPE_PRIVATE | DST_TYPE_PUBLIC)) != 0);
469         REQUIRE(mctx != NULL);
470         REQUIRE(keyp != NULL && *keyp == NULL);
471
472         CHECKALG(alg);
473
474         isc_buffer_init(&b, filename, sizeof(filename));
475         result = buildfilename(name, id, alg, type, directory, &b);
476         if (result != ISC_R_SUCCESS)
477                 return (result);
478
479         key = NULL;
480         result = dst_key_fromnamedfile(filename, NULL, type, mctx, &key);
481         if (result != ISC_R_SUCCESS)
482                 return (result);
483
484         result = computeid(key);
485         if (result != ISC_R_SUCCESS) {
486                 dst_key_free(&key);
487                 return (result);
488         }
489
490         if (!dns_name_equal(name, key->key_name) || id != key->key_id ||
491             alg != key->key_alg) {
492                 dst_key_free(&key);
493                 return (DST_R_INVALIDPRIVATEKEY);
494         }
495
496         *keyp = key;
497         return (ISC_R_SUCCESS);
498 }
499
500 isc_result_t
501 dst_key_fromnamedfile(const char *filename, const char *dirname,
502                       int type, isc_mem_t *mctx, dst_key_t **keyp)
503 {
504         isc_result_t result;
505         dst_key_t *pubkey = NULL, *key = NULL;
506         char *newfilename = NULL;
507         int newfilenamelen = 0;
508         isc_lex_t *lex = NULL;
509
510         REQUIRE(dst_initialized == ISC_TRUE);
511         REQUIRE(filename != NULL);
512         REQUIRE((type & (DST_TYPE_PRIVATE | DST_TYPE_PUBLIC)) != 0);
513         REQUIRE(mctx != NULL);
514         REQUIRE(keyp != NULL && *keyp == NULL);
515
516         /* If an absolute path is specified, don't use the key directory */
517 #ifndef WIN32
518         if (filename[0] == '/')
519                 dirname = NULL;
520 #else /* WIN32 */
521         if (filename[0] == '/' || filename[0] == '\\')
522                 dirname = NULL;
523 #endif
524
525         newfilenamelen = strlen(filename) + 5;
526         if (dirname != NULL)
527                 newfilenamelen += strlen(dirname) + 1;
528         newfilename = isc_mem_get(mctx, newfilenamelen);
529         if (newfilename == NULL)
530                 return (ISC_R_NOMEMORY);
531         result = addsuffix(newfilename, newfilenamelen,
532                            dirname, filename, ".key");
533         INSIST(result == ISC_R_SUCCESS);
534
535         result = dst_key_read_public(newfilename, type, mctx, &pubkey);
536         isc_mem_put(mctx, newfilename, newfilenamelen);
537         newfilename = NULL;
538         RETERR(result);
539
540         if ((type & (DST_TYPE_PRIVATE | DST_TYPE_PUBLIC)) == DST_TYPE_PUBLIC ||
541             (pubkey->key_flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
542                 result = computeid(pubkey);
543                 if (result != ISC_R_SUCCESS) {
544                         dst_key_free(&pubkey);
545                         return (result);
546                 }
547
548                 *keyp = pubkey;
549                 return (ISC_R_SUCCESS);
550         }
551
552         result = algorithm_status(pubkey->key_alg);
553         if (result != ISC_R_SUCCESS) {
554                 dst_key_free(&pubkey);
555                 return (result);
556         }
557
558         key = get_key_struct(pubkey->key_name, pubkey->key_alg,
559                              pubkey->key_flags, pubkey->key_proto, 0,
560                              pubkey->key_class, pubkey->key_ttl, mctx);
561         if (key == NULL) {
562                 dst_key_free(&pubkey);
563                 return (ISC_R_NOMEMORY);
564         }
565
566         if (key->func->parse == NULL)
567                 RETERR(DST_R_UNSUPPORTEDALG);
568
569         newfilenamelen = strlen(filename) + 9;
570         if (dirname != NULL)
571                 newfilenamelen += strlen(dirname) + 1;
572         newfilename = isc_mem_get(mctx, newfilenamelen);
573         if (newfilename == NULL)
574                 RETERR(ISC_R_NOMEMORY);
575         result = addsuffix(newfilename, newfilenamelen,
576                            dirname, filename, ".private");
577         INSIST(result == ISC_R_SUCCESS);
578
579         RETERR(isc_lex_create(mctx, 1500, &lex));
580         RETERR(isc_lex_openfile(lex, newfilename));
581         isc_mem_put(mctx, newfilename, newfilenamelen);
582
583         RETERR(key->func->parse(key, lex, pubkey));
584         isc_lex_destroy(&lex);
585
586         RETERR(computeid(key));
587
588         if (pubkey->key_id != key->key_id)
589                 RETERR(DST_R_INVALIDPRIVATEKEY);
590         dst_key_free(&pubkey);
591
592         *keyp = key;
593         return (ISC_R_SUCCESS);
594
595  out:
596         if (pubkey != NULL)
597                 dst_key_free(&pubkey);
598         if (newfilename != NULL)
599                 isc_mem_put(mctx, newfilename, newfilenamelen);
600         if (lex != NULL)
601                 isc_lex_destroy(&lex);
602         if (key != NULL)
603                 dst_key_free(&key);
604         return (result);
605 }
606
607 isc_result_t
608 dst_key_todns(const dst_key_t *key, isc_buffer_t *target) {
609         REQUIRE(dst_initialized == ISC_TRUE);
610         REQUIRE(VALID_KEY(key));
611         REQUIRE(target != NULL);
612
613         CHECKALG(key->key_alg);
614
615         if (key->func->todns == NULL)
616                 return (DST_R_UNSUPPORTEDALG);
617
618         if (isc_buffer_availablelength(target) < 4)
619                 return (ISC_R_NOSPACE);
620         isc_buffer_putuint16(target, (isc_uint16_t)(key->key_flags & 0xffff));
621         isc_buffer_putuint8(target, (isc_uint8_t)key->key_proto);
622         isc_buffer_putuint8(target, (isc_uint8_t)key->key_alg);
623
624         if (key->key_flags & DNS_KEYFLAG_EXTENDED) {
625                 if (isc_buffer_availablelength(target) < 2)
626                         return (ISC_R_NOSPACE);
627                 isc_buffer_putuint16(target,
628                                      (isc_uint16_t)((key->key_flags >> 16)
629                                                     & 0xffff));
630         }
631
632         if (key->keydata.generic == NULL) /*%< NULL KEY */
633                 return (ISC_R_SUCCESS);
634
635         return (key->func->todns(key, target));
636 }
637
638 isc_result_t
639 dst_key_fromdns(dns_name_t *name, dns_rdataclass_t rdclass,
640                 isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp)
641 {
642         isc_uint8_t alg, proto;
643         isc_uint32_t flags, extflags;
644         dst_key_t *key = NULL;
645         dns_keytag_t id, rid;
646         isc_region_t r;
647         isc_result_t result;
648
649         REQUIRE(dst_initialized);
650
651         isc_buffer_remainingregion(source, &r);
652
653         if (isc_buffer_remaininglength(source) < 4)
654                 return (DST_R_INVALIDPUBLICKEY);
655         flags = isc_buffer_getuint16(source);
656         proto = isc_buffer_getuint8(source);
657         alg = isc_buffer_getuint8(source);
658
659         id = dst_region_computeid(&r, alg);
660         rid = dst_region_computerid(&r, alg);
661
662         if (flags & DNS_KEYFLAG_EXTENDED) {
663                 if (isc_buffer_remaininglength(source) < 2)
664                         return (DST_R_INVALIDPUBLICKEY);
665                 extflags = isc_buffer_getuint16(source);
666                 flags |= (extflags << 16);
667         }
668
669         result = frombuffer(name, alg, flags, proto, rdclass, source,
670                             mctx, &key);
671         if (result != ISC_R_SUCCESS)
672                 return (result);
673         key->key_id = id;
674         key->key_rid = rid;
675
676         *keyp = key;
677         return (ISC_R_SUCCESS);
678 }
679
680 isc_result_t
681 dst_key_frombuffer(dns_name_t *name, unsigned int alg,
682                    unsigned int flags, unsigned int protocol,
683                    dns_rdataclass_t rdclass,
684                    isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp)
685 {
686         dst_key_t *key = NULL;
687         isc_result_t result;
688
689         REQUIRE(dst_initialized);
690
691         result = frombuffer(name, alg, flags, protocol, rdclass, source,
692                             mctx, &key);
693         if (result != ISC_R_SUCCESS)
694                 return (result);
695
696         result = computeid(key);
697         if (result != ISC_R_SUCCESS) {
698                 dst_key_free(&key);
699                 return (result);
700         }
701
702         *keyp = key;
703         return (ISC_R_SUCCESS);
704 }
705
706 isc_result_t
707 dst_key_tobuffer(const dst_key_t *key, isc_buffer_t *target) {
708         REQUIRE(dst_initialized == ISC_TRUE);
709         REQUIRE(VALID_KEY(key));
710         REQUIRE(target != NULL);
711
712         CHECKALG(key->key_alg);
713
714         if (key->func->todns == NULL)
715                 return (DST_R_UNSUPPORTEDALG);
716
717         return (key->func->todns(key, target));
718 }
719
720 isc_result_t
721 dst_key_privatefrombuffer(dst_key_t *key, isc_buffer_t *buffer) {
722         isc_lex_t *lex = NULL;
723         isc_result_t result = ISC_R_SUCCESS;
724
725         REQUIRE(dst_initialized == ISC_TRUE);
726         REQUIRE(VALID_KEY(key));
727         REQUIRE(!dst_key_isprivate(key));
728         REQUIRE(buffer != NULL);
729
730         if (key->func->parse == NULL)
731                 RETERR(DST_R_UNSUPPORTEDALG);
732
733         RETERR(isc_lex_create(key->mctx, 1500, &lex));
734         RETERR(isc_lex_openbuffer(lex, buffer));
735         RETERR(key->func->parse(key, lex, NULL));
736  out:
737         if (lex != NULL)
738                 isc_lex_destroy(&lex);
739         return (result);
740 }
741
742 gss_ctx_id_t
743 dst_key_getgssctx(const dst_key_t *key)
744 {
745         REQUIRE(key != NULL);
746
747         return (key->keydata.gssctx);
748 }
749
750 isc_result_t
751 dst_key_fromgssapi(dns_name_t *name, gss_ctx_id_t gssctx, isc_mem_t *mctx,
752                    dst_key_t **keyp, isc_region_t *intoken)
753 {
754         dst_key_t *key;
755         isc_result_t result;
756
757         REQUIRE(gssctx != NULL);
758         REQUIRE(keyp != NULL && *keyp == NULL);
759
760         key = get_key_struct(name, DST_ALG_GSSAPI, 0, DNS_KEYPROTO_DNSSEC,
761                              0, dns_rdataclass_in, 0, mctx);
762         if (key == NULL)
763                 return (ISC_R_NOMEMORY);
764
765         if (intoken != NULL) {
766                 /*
767                  * Keep the token for use by external ssu rules. They may need
768                  * to examine the PAC in the kerberos ticket.
769                  */
770                 RETERR(isc_buffer_allocate(key->mctx, &key->key_tkeytoken,
771                        intoken->length));
772                 RETERR(isc_buffer_copyregion(key->key_tkeytoken, intoken));
773         }
774
775         key->keydata.gssctx = gssctx;
776         *keyp = key;
777         result = ISC_R_SUCCESS;
778 out:
779         return result;
780 }
781
782 isc_result_t
783 dst_key_buildinternal(dns_name_t *name, unsigned int alg,
784                       unsigned int bits, unsigned int flags,
785                       unsigned int protocol, dns_rdataclass_t rdclass,
786                       void *data, isc_mem_t *mctx, dst_key_t **keyp)
787 {
788         dst_key_t *key;
789         isc_result_t result;
790
791         REQUIRE(dst_initialized == ISC_TRUE);
792         REQUIRE(dns_name_isabsolute(name));
793         REQUIRE(mctx != NULL);
794         REQUIRE(keyp != NULL && *keyp == NULL);
795         REQUIRE(data != NULL);
796
797         CHECKALG(alg);
798
799         key = get_key_struct(name, alg, flags, protocol, bits, rdclass,
800                              0, mctx);
801         if (key == NULL)
802                 return (ISC_R_NOMEMORY);
803
804         key->keydata.generic = data;
805
806         result = computeid(key);
807         if (result != ISC_R_SUCCESS) {
808                 dst_key_free(&key);
809                 return (result);
810         }
811
812         *keyp = key;
813         return (ISC_R_SUCCESS);
814 }
815
816 isc_result_t
817 dst_key_fromlabel(dns_name_t *name, int alg, unsigned int flags,
818                   unsigned int protocol, dns_rdataclass_t rdclass,
819                   const char *engine, const char *label, const char *pin,
820                   isc_mem_t *mctx, dst_key_t **keyp)
821 {
822         dst_key_t *key;
823         isc_result_t result;
824
825         REQUIRE(dst_initialized == ISC_TRUE);
826         REQUIRE(dns_name_isabsolute(name));
827         REQUIRE(mctx != NULL);
828         REQUIRE(keyp != NULL && *keyp == NULL);
829         REQUIRE(label != NULL);
830
831         CHECKALG(alg);
832
833         key = get_key_struct(name, alg, flags, protocol, 0, rdclass, 0, mctx);
834         if (key == NULL)
835                 return (ISC_R_NOMEMORY);
836
837         if (key->func->fromlabel == NULL) {
838                 dst_key_free(&key);
839                 return (DST_R_UNSUPPORTEDALG);
840         }
841
842         result = key->func->fromlabel(key, engine, label, pin);
843         if (result != ISC_R_SUCCESS) {
844                 dst_key_free(&key);
845                 return (result);
846         }
847
848         result = computeid(key);
849         if (result != ISC_R_SUCCESS) {
850                 dst_key_free(&key);
851                 return (result);
852         }
853
854         *keyp = key;
855         return (ISC_R_SUCCESS);
856 }
857
858 isc_result_t
859 dst_key_generate(dns_name_t *name, unsigned int alg,
860                  unsigned int bits, unsigned int param,
861                  unsigned int flags, unsigned int protocol,
862                  dns_rdataclass_t rdclass,
863                  isc_mem_t *mctx, dst_key_t **keyp)
864 {
865         return (dst_key_generate2(name, alg, bits, param, flags, protocol,
866                                   rdclass, mctx, keyp, NULL));
867 }
868
869 isc_result_t
870 dst_key_generate2(dns_name_t *name, unsigned int alg,
871                   unsigned int bits, unsigned int param,
872                   unsigned int flags, unsigned int protocol,
873                   dns_rdataclass_t rdclass,
874                   isc_mem_t *mctx, dst_key_t **keyp,
875                   void (*callback)(int))
876 {
877         dst_key_t *key;
878         isc_result_t ret;
879
880         REQUIRE(dst_initialized == ISC_TRUE);
881         REQUIRE(dns_name_isabsolute(name));
882         REQUIRE(mctx != NULL);
883         REQUIRE(keyp != NULL && *keyp == NULL);
884
885         CHECKALG(alg);
886
887         key = get_key_struct(name, alg, flags, protocol, bits,
888                              rdclass, 0, mctx);
889         if (key == NULL)
890                 return (ISC_R_NOMEMORY);
891
892         if (bits == 0) { /*%< NULL KEY */
893                 key->key_flags |= DNS_KEYTYPE_NOKEY;
894                 *keyp = key;
895                 return (ISC_R_SUCCESS);
896         }
897
898         if (key->func->generate == NULL) {
899                 dst_key_free(&key);
900                 return (DST_R_UNSUPPORTEDALG);
901         }
902
903         ret = key->func->generate(key, param, callback);
904         if (ret != ISC_R_SUCCESS) {
905                 dst_key_free(&key);
906                 return (ret);
907         }
908
909         ret = computeid(key);
910         if (ret != ISC_R_SUCCESS) {
911                 dst_key_free(&key);
912                 return (ret);
913         }
914
915         *keyp = key;
916         return (ISC_R_SUCCESS);
917 }
918
919 isc_result_t
920 dst_key_getnum(const dst_key_t *key, int type, isc_uint32_t *valuep)
921 {
922         REQUIRE(VALID_KEY(key));
923         REQUIRE(valuep != NULL);
924         REQUIRE(type <= DST_MAX_NUMERIC);
925         if (!key->numset[type])
926                 return (ISC_R_NOTFOUND);
927         *valuep = key->nums[type];
928         return (ISC_R_SUCCESS);
929 }
930
931 void
932 dst_key_setnum(dst_key_t *key, int type, isc_uint32_t value)
933 {
934         REQUIRE(VALID_KEY(key));
935         REQUIRE(type <= DST_MAX_NUMERIC);
936         key->nums[type] = value;
937         key->numset[type] = ISC_TRUE;
938 }
939
940 void
941 dst_key_unsetnum(dst_key_t *key, int type)
942 {
943         REQUIRE(VALID_KEY(key));
944         REQUIRE(type <= DST_MAX_NUMERIC);
945         key->numset[type] = ISC_FALSE;
946 }
947
948 isc_result_t
949 dst_key_gettime(const dst_key_t *key, int type, isc_stdtime_t *timep) {
950         REQUIRE(VALID_KEY(key));
951         REQUIRE(timep != NULL);
952         REQUIRE(type <= DST_MAX_TIMES);
953         if (!key->timeset[type])
954                 return (ISC_R_NOTFOUND);
955         *timep = key->times[type];
956         return (ISC_R_SUCCESS);
957 }
958
959 void
960 dst_key_settime(dst_key_t *key, int type, isc_stdtime_t when) {
961         REQUIRE(VALID_KEY(key));
962         REQUIRE(type <= DST_MAX_TIMES);
963         key->times[type] = when;
964         key->timeset[type] = ISC_TRUE;
965 }
966
967 void
968 dst_key_unsettime(dst_key_t *key, int type) {
969         REQUIRE(VALID_KEY(key));
970         REQUIRE(type <= DST_MAX_TIMES);
971         key->timeset[type] = ISC_FALSE;
972 }
973
974 isc_result_t
975 dst_key_getprivateformat(const dst_key_t *key, int *majorp, int *minorp) {
976         REQUIRE(VALID_KEY(key));
977         REQUIRE(majorp != NULL);
978         REQUIRE(minorp != NULL);
979         *majorp = key->fmt_major;
980         *minorp = key->fmt_minor;
981         return (ISC_R_SUCCESS);
982 }
983
984 void
985 dst_key_setprivateformat(dst_key_t *key, int major, int minor) {
986         REQUIRE(VALID_KEY(key));
987         key->fmt_major = major;
988         key->fmt_minor = minor;
989 }
990
991 static isc_boolean_t
992 comparekeys(const dst_key_t *key1, const dst_key_t *key2,
993             isc_boolean_t match_revoked_key,
994             isc_boolean_t (*compare)(const dst_key_t *key1,
995                                      const dst_key_t *key2))
996 {
997         REQUIRE(dst_initialized == ISC_TRUE);
998         REQUIRE(VALID_KEY(key1));
999         REQUIRE(VALID_KEY(key2));
1000
1001         if (key1 == key2)
1002                 return (ISC_TRUE);
1003
1004         if (key1 == NULL || key2 == NULL)
1005                 return (ISC_FALSE);
1006
1007         if (key1->key_alg != key2->key_alg)
1008                 return (ISC_FALSE);
1009
1010         if (key1->key_id != key2->key_id) {
1011                 if (!match_revoked_key)
1012                         return (ISC_FALSE);
1013                 if (key1->key_alg == DST_ALG_RSAMD5)
1014                         return (ISC_FALSE);
1015                 if ((key1->key_flags & DNS_KEYFLAG_REVOKE) ==
1016                     (key2->key_flags & DNS_KEYFLAG_REVOKE))
1017                         return (ISC_FALSE);
1018                 if (key1->key_id != key2->key_rid &&
1019                     key1->key_rid != key2->key_id)
1020                         return (ISC_FALSE);
1021         }
1022
1023         if (compare != NULL)
1024                 return (compare(key1, key2));
1025         else
1026                 return (ISC_FALSE);
1027 }
1028
1029
1030 /*
1031  * Compares only the public portion of two keys, by converting them
1032  * both to wire format and comparing the results.
1033  */
1034 static isc_boolean_t
1035 pub_compare(const dst_key_t *key1, const dst_key_t *key2) {
1036         isc_result_t result;
1037         unsigned char buf1[DST_KEY_MAXSIZE], buf2[DST_KEY_MAXSIZE];
1038         isc_buffer_t b1, b2;
1039         isc_region_t r1, r2;
1040
1041         isc_buffer_init(&b1, buf1, sizeof(buf1));
1042         result = dst_key_todns(key1, &b1);
1043         if (result != ISC_R_SUCCESS)
1044                 return (ISC_FALSE);
1045         /* Zero out flags. */
1046         buf1[0] = buf1[1] = 0;
1047         if ((key1->key_flags & DNS_KEYFLAG_EXTENDED) != 0)
1048                 isc_buffer_subtract(&b1, 2);
1049
1050         isc_buffer_init(&b2, buf2, sizeof(buf2));
1051         result = dst_key_todns(key2, &b2);
1052         if (result != ISC_R_SUCCESS)
1053                 return (ISC_FALSE);
1054         /* Zero out flags. */
1055         buf2[0] = buf2[1] = 0;
1056         if ((key2->key_flags & DNS_KEYFLAG_EXTENDED) != 0)
1057                 isc_buffer_subtract(&b2, 2);
1058
1059         isc_buffer_usedregion(&b1, &r1);
1060         /* Remove extended flags. */
1061         if ((key1->key_flags & DNS_KEYFLAG_EXTENDED) != 0) {
1062                 memmove(&buf1[4], &buf1[6], r1.length - 6);
1063                 r1.length -= 2;
1064         }
1065
1066         isc_buffer_usedregion(&b2, &r2);
1067         /* Remove extended flags. */
1068         if ((key2->key_flags & DNS_KEYFLAG_EXTENDED) != 0) {
1069                 memmove(&buf2[4], &buf2[6], r2.length - 6);
1070                 r2.length -= 2;
1071         }
1072         return (ISC_TF(isc_region_compare(&r1, &r2) == 0));
1073 }
1074
1075 isc_boolean_t
1076 dst_key_compare(const dst_key_t *key1, const dst_key_t *key2) {
1077         return (comparekeys(key1, key2, ISC_FALSE, key1->func->compare));
1078 }
1079
1080 isc_boolean_t
1081 dst_key_pubcompare(const dst_key_t *key1, const dst_key_t *key2,
1082                    isc_boolean_t match_revoked_key)
1083 {
1084         return (comparekeys(key1, key2, match_revoked_key, pub_compare));
1085 }
1086
1087
1088 isc_boolean_t
1089 dst_key_paramcompare(const dst_key_t *key1, const dst_key_t *key2) {
1090         REQUIRE(dst_initialized == ISC_TRUE);
1091         REQUIRE(VALID_KEY(key1));
1092         REQUIRE(VALID_KEY(key2));
1093
1094         if (key1 == key2)
1095                 return (ISC_TRUE);
1096         if (key1 == NULL || key2 == NULL)
1097                 return (ISC_FALSE);
1098         if (key1->key_alg == key2->key_alg &&
1099             key1->func->paramcompare != NULL &&
1100             key1->func->paramcompare(key1, key2) == ISC_TRUE)
1101                 return (ISC_TRUE);
1102         else
1103                 return (ISC_FALSE);
1104 }
1105
1106 void
1107 dst_key_attach(dst_key_t *source, dst_key_t **target) {
1108
1109         REQUIRE(dst_initialized == ISC_TRUE);
1110         REQUIRE(target != NULL && *target == NULL);
1111         REQUIRE(VALID_KEY(source));
1112
1113         isc_refcount_increment(&source->refs, NULL);
1114         *target = source;
1115 }
1116
1117 void
1118 dst_key_free(dst_key_t **keyp) {
1119         isc_mem_t *mctx;
1120         dst_key_t *key;
1121         unsigned int refs;
1122
1123         REQUIRE(dst_initialized == ISC_TRUE);
1124         REQUIRE(keyp != NULL && VALID_KEY(*keyp));
1125
1126         key = *keyp;
1127         mctx = key->mctx;
1128
1129         isc_refcount_decrement(&key->refs, &refs);
1130         if (refs != 0)
1131                 return;
1132
1133         isc_refcount_destroy(&key->refs);
1134         if (key->keydata.generic != NULL) {
1135                 INSIST(key->func->destroy != NULL);
1136                 key->func->destroy(key);
1137         }
1138         if (key->engine != NULL)
1139                 isc_mem_free(mctx, key->engine);
1140         if (key->label != NULL)
1141                 isc_mem_free(mctx, key->label);
1142         dns_name_free(key->key_name, mctx);
1143         isc_mem_put(mctx, key->key_name, sizeof(dns_name_t));
1144         if (key->key_tkeytoken) {
1145                 isc_buffer_free(&key->key_tkeytoken);
1146         }
1147         memset(key, 0, sizeof(dst_key_t));
1148         isc_mem_putanddetach(&mctx, key, sizeof(dst_key_t));
1149         *keyp = NULL;
1150 }
1151
1152 isc_boolean_t
1153 dst_key_isprivate(const dst_key_t *key) {
1154         REQUIRE(VALID_KEY(key));
1155         INSIST(key->func->isprivate != NULL);
1156         return (key->func->isprivate(key));
1157 }
1158
1159 isc_result_t
1160 dst_key_buildfilename(const dst_key_t *key, int type,
1161                       const char *directory, isc_buffer_t *out) {
1162
1163         REQUIRE(VALID_KEY(key));
1164         REQUIRE(type == DST_TYPE_PRIVATE || type == DST_TYPE_PUBLIC ||
1165                 type == 0);
1166
1167         return (buildfilename(key->key_name, key->key_id, key->key_alg,
1168                               type, directory, out));
1169 }
1170
1171 isc_result_t
1172 dst_key_sigsize(const dst_key_t *key, unsigned int *n) {
1173         REQUIRE(dst_initialized == ISC_TRUE);
1174         REQUIRE(VALID_KEY(key));
1175         REQUIRE(n != NULL);
1176
1177         /* XXXVIX this switch statement is too sparse to gen a jump table. */
1178         switch (key->key_alg) {
1179         case DST_ALG_RSAMD5:
1180         case DST_ALG_RSASHA1:
1181         case DST_ALG_NSEC3RSASHA1:
1182         case DST_ALG_RSASHA256:
1183         case DST_ALG_RSASHA512:
1184                 *n = (key->key_size + 7) / 8;
1185                 break;
1186         case DST_ALG_DSA:
1187         case DST_ALG_NSEC3DSA:
1188                 *n = DNS_SIG_DSASIGSIZE;
1189                 break;
1190         case DST_ALG_ECCGOST:
1191                 *n = DNS_SIG_GOSTSIGSIZE;
1192                 break;
1193         case DST_ALG_ECDSA256:
1194                 *n = DNS_SIG_ECDSA256SIZE;
1195                 break;
1196         case DST_ALG_ECDSA384:
1197                 *n = DNS_SIG_ECDSA384SIZE;
1198                 break;
1199         case DST_ALG_HMACMD5:
1200                 *n = 16;
1201                 break;
1202         case DST_ALG_HMACSHA1:
1203                 *n = ISC_SHA1_DIGESTLENGTH;
1204                 break;
1205         case DST_ALG_HMACSHA224:
1206                 *n = ISC_SHA224_DIGESTLENGTH;
1207                 break;
1208         case DST_ALG_HMACSHA256:
1209                 *n = ISC_SHA256_DIGESTLENGTH;
1210                 break;
1211         case DST_ALG_HMACSHA384:
1212                 *n = ISC_SHA384_DIGESTLENGTH;
1213                 break;
1214         case DST_ALG_HMACSHA512:
1215                 *n = ISC_SHA512_DIGESTLENGTH;
1216                 break;
1217         case DST_ALG_GSSAPI:
1218                 *n = 128; /*%< XXX */
1219                 break;
1220         case DST_ALG_DH:
1221         default:
1222                 return (DST_R_UNSUPPORTEDALG);
1223         }
1224         return (ISC_R_SUCCESS);
1225 }
1226
1227 isc_result_t
1228 dst_key_secretsize(const dst_key_t *key, unsigned int *n) {
1229         REQUIRE(dst_initialized == ISC_TRUE);
1230         REQUIRE(VALID_KEY(key));
1231         REQUIRE(n != NULL);
1232
1233         if (key->key_alg == DST_ALG_DH)
1234                 *n = (key->key_size + 7) / 8;
1235         else
1236                 return (DST_R_UNSUPPORTEDALG);
1237         return (ISC_R_SUCCESS);
1238 }
1239
1240 /*%
1241  * Set the flags on a key, then recompute the key ID
1242  */
1243 isc_result_t
1244 dst_key_setflags(dst_key_t *key, isc_uint32_t flags) {
1245         REQUIRE(VALID_KEY(key));
1246         key->key_flags = flags;
1247         return (computeid(key));
1248 }
1249
1250 void
1251 dst_key_format(const dst_key_t *key, char *cp, unsigned int size) {
1252         char namestr[DNS_NAME_FORMATSIZE];
1253         char algstr[DNS_NAME_FORMATSIZE];
1254
1255         dns_name_format(dst_key_name(key), namestr, sizeof(namestr));
1256         dns_secalg_format((dns_secalg_t) dst_key_alg(key), algstr,
1257                           sizeof(algstr));
1258         snprintf(cp, size, "%s/%s/%d", namestr, algstr, dst_key_id(key));
1259 }
1260
1261 isc_result_t
1262 dst_key_dump(dst_key_t *key, isc_mem_t *mctx, char **buffer, int *length) {
1263
1264         REQUIRE(buffer != NULL && *buffer == NULL);
1265         REQUIRE(length != NULL && *length == 0);
1266         REQUIRE(VALID_KEY(key));
1267
1268         if (key->func->dump == NULL)
1269                 return (ISC_R_NOTIMPLEMENTED);
1270         return (key->func->dump(key, mctx, buffer, length));
1271 }
1272
1273 isc_result_t
1274 dst_key_restore(dns_name_t *name, unsigned int alg, unsigned int flags,
1275                 unsigned int protocol, dns_rdataclass_t rdclass,
1276                 isc_mem_t *mctx, const char *keystr, dst_key_t **keyp)
1277 {
1278         isc_result_t result;
1279         dst_key_t *key;
1280
1281         REQUIRE(dst_initialized == ISC_TRUE);
1282         REQUIRE(keyp != NULL && *keyp == NULL);
1283
1284         if (alg >= DST_MAX_ALGS || dst_t_func[alg] == NULL)
1285                 return (DST_R_UNSUPPORTEDALG);
1286
1287         if (dst_t_func[alg]->restore == NULL)
1288                 return (ISC_R_NOTIMPLEMENTED);
1289
1290         key = get_key_struct(name, alg, flags, protocol, 0, rdclass, 0, mctx);
1291         if (key == NULL)
1292                 return (ISC_R_NOMEMORY);
1293
1294         result = (dst_t_func[alg]->restore)(key, keystr);
1295         if (result == ISC_R_SUCCESS)
1296                 *keyp = key;
1297         else
1298                 dst_key_free(&key);
1299
1300         return (result);
1301 }
1302
1303 /***
1304  *** Static methods
1305  ***/
1306
1307 /*%
1308  * Allocates a key structure and fills in some of the fields.
1309  */
1310 static dst_key_t *
1311 get_key_struct(dns_name_t *name, unsigned int alg,
1312                unsigned int flags, unsigned int protocol,
1313                unsigned int bits, dns_rdataclass_t rdclass,
1314                dns_ttl_t ttl, isc_mem_t *mctx)
1315 {
1316         dst_key_t *key;
1317         isc_result_t result;
1318         int i;
1319
1320         key = (dst_key_t *) isc_mem_get(mctx, sizeof(dst_key_t));
1321         if (key == NULL)
1322                 return (NULL);
1323
1324         memset(key, 0, sizeof(dst_key_t));
1325
1326         key->key_name = isc_mem_get(mctx, sizeof(dns_name_t));
1327         if (key->key_name == NULL) {
1328                 isc_mem_put(mctx, key, sizeof(dst_key_t));
1329                 return (NULL);
1330         }
1331
1332         dns_name_init(key->key_name, NULL);
1333         result = dns_name_dup(name, mctx, key->key_name);
1334         if (result != ISC_R_SUCCESS) {
1335                 isc_mem_put(mctx, key->key_name, sizeof(dns_name_t));
1336                 isc_mem_put(mctx, key, sizeof(dst_key_t));
1337                 return (NULL);
1338         }
1339
1340         result = isc_refcount_init(&key->refs, 1);
1341         if (result != ISC_R_SUCCESS) {
1342                 dns_name_free(key->key_name, mctx);
1343                 isc_mem_put(mctx, key->key_name, sizeof(dns_name_t));
1344                 isc_mem_put(mctx, key, sizeof(dst_key_t));
1345                 return (NULL);
1346         }
1347         isc_mem_attach(mctx, &key->mctx);
1348         key->key_alg = alg;
1349         key->key_flags = flags;
1350         key->key_proto = protocol;
1351         key->keydata.generic = NULL;
1352         key->key_size = bits;
1353         key->key_class = rdclass;
1354         key->key_ttl = ttl;
1355         key->func = dst_t_func[alg];
1356         key->fmt_major = 0;
1357         key->fmt_minor = 0;
1358         for (i = 0; i < (DST_MAX_TIMES + 1); i++) {
1359                 key->times[i] = 0;
1360                 key->timeset[i] = ISC_FALSE;
1361         }
1362         key->inactive = ISC_FALSE;
1363         key->magic = KEY_MAGIC;
1364         return (key);
1365 }
1366
1367 isc_boolean_t
1368 dst_key_inactive(const dst_key_t *key) {
1369
1370         REQUIRE(VALID_KEY(key));
1371
1372         return (key->inactive);
1373 }
1374
1375 void
1376 dst_key_setinactive(dst_key_t *key, isc_boolean_t inactive) {
1377
1378         REQUIRE(VALID_KEY(key));
1379
1380         key->inactive = inactive;
1381 }
1382
1383 /*%
1384  * Reads a public key from disk
1385  */
1386 isc_result_t
1387 dst_key_read_public(const char *filename, int type,
1388                     isc_mem_t *mctx, dst_key_t **keyp)
1389 {
1390         u_char rdatabuf[DST_KEY_MAXSIZE];
1391         isc_buffer_t b;
1392         dns_fixedname_t name;
1393         isc_lex_t *lex = NULL;
1394         isc_token_t token;
1395         isc_result_t ret;
1396         dns_rdata_t rdata = DNS_RDATA_INIT;
1397         unsigned int opt = ISC_LEXOPT_DNSMULTILINE;
1398         dns_rdataclass_t rdclass = dns_rdataclass_in;
1399         isc_lexspecials_t specials;
1400         isc_uint32_t ttl = 0;
1401         isc_result_t result;
1402         dns_rdatatype_t keytype;
1403
1404         /*
1405          * Open the file and read its formatted contents
1406          * File format:
1407          *    domain.name [ttl] [class] [KEY|DNSKEY] <flags> <protocol> <algorithm> <key>
1408          */
1409
1410         /* 1500 should be large enough for any key */
1411         ret = isc_lex_create(mctx, 1500, &lex);
1412         if (ret != ISC_R_SUCCESS)
1413                 goto cleanup;
1414
1415         memset(specials, 0, sizeof(specials));
1416         specials['('] = 1;
1417         specials[')'] = 1;
1418         specials['"'] = 1;
1419         isc_lex_setspecials(lex, specials);
1420         isc_lex_setcomments(lex, ISC_LEXCOMMENT_DNSMASTERFILE);
1421
1422         ret = isc_lex_openfile(lex, filename);
1423         if (ret != ISC_R_SUCCESS)
1424                 goto cleanup;
1425
1426 #define NEXTTOKEN(lex, opt, token) { \
1427         ret = isc_lex_gettoken(lex, opt, token); \
1428         if (ret != ISC_R_SUCCESS) \
1429                 goto cleanup; \
1430         }
1431
1432 #define BADTOKEN() { \
1433         ret = ISC_R_UNEXPECTEDTOKEN; \
1434         goto cleanup; \
1435         }
1436
1437         /* Read the domain name */
1438         NEXTTOKEN(lex, opt, &token);
1439         if (token.type != isc_tokentype_string)
1440                 BADTOKEN();
1441
1442         /*
1443          * We don't support "@" in .key files.
1444          */
1445         if (!strcmp(DST_AS_STR(token), "@"))
1446                 BADTOKEN();
1447
1448         dns_fixedname_init(&name);
1449         isc_buffer_init(&b, DST_AS_STR(token), strlen(DST_AS_STR(token)));
1450         isc_buffer_add(&b, strlen(DST_AS_STR(token)));
1451         ret = dns_name_fromtext(dns_fixedname_name(&name), &b, dns_rootname,
1452                                 0, NULL);
1453         if (ret != ISC_R_SUCCESS)
1454                 goto cleanup;
1455
1456         /* Read the next word: either TTL, class, or 'KEY' */
1457         NEXTTOKEN(lex, opt, &token);
1458
1459         if (token.type != isc_tokentype_string)
1460                 BADTOKEN();
1461
1462         /* If it's a TTL, read the next one */
1463         result = dns_ttl_fromtext(&token.value.as_textregion, &ttl);
1464         if (result == ISC_R_SUCCESS)
1465                 NEXTTOKEN(lex, opt, &token);
1466
1467         if (token.type != isc_tokentype_string)
1468                 BADTOKEN();
1469
1470         ret = dns_rdataclass_fromtext(&rdclass, &token.value.as_textregion);
1471         if (ret == ISC_R_SUCCESS)
1472                 NEXTTOKEN(lex, opt, &token);
1473
1474         if (token.type != isc_tokentype_string)
1475                 BADTOKEN();
1476
1477         if (strcasecmp(DST_AS_STR(token), "DNSKEY") == 0)
1478                 keytype = dns_rdatatype_dnskey;
1479         else if (strcasecmp(DST_AS_STR(token), "KEY") == 0)
1480                 keytype = dns_rdatatype_key; /*%< SIG(0), TKEY */
1481         else
1482                 BADTOKEN();
1483
1484         if (((type & DST_TYPE_KEY) != 0 && keytype != dns_rdatatype_key) ||
1485             ((type & DST_TYPE_KEY) == 0 && keytype != dns_rdatatype_dnskey)) {
1486                 ret = DST_R_BADKEYTYPE;
1487                 goto cleanup;
1488         }
1489
1490         isc_buffer_init(&b, rdatabuf, sizeof(rdatabuf));
1491         ret = dns_rdata_fromtext(&rdata, rdclass, keytype, lex, NULL,
1492                                  ISC_FALSE, mctx, &b, NULL);
1493         if (ret != ISC_R_SUCCESS)
1494                 goto cleanup;
1495
1496         ret = dst_key_fromdns(dns_fixedname_name(&name), rdclass, &b, mctx,
1497                               keyp);
1498         if (ret != ISC_R_SUCCESS)
1499                 goto cleanup;
1500
1501         dst_key_setttl(*keyp, ttl);
1502
1503  cleanup:
1504         if (lex != NULL)
1505                 isc_lex_destroy(&lex);
1506         return (ret);
1507 }
1508
1509 static isc_boolean_t
1510 issymmetric(const dst_key_t *key) {
1511         REQUIRE(dst_initialized == ISC_TRUE);
1512         REQUIRE(VALID_KEY(key));
1513
1514         /* XXXVIX this switch statement is too sparse to gen a jump table. */
1515         switch (key->key_alg) {
1516         case DST_ALG_RSAMD5:
1517         case DST_ALG_RSASHA1:
1518         case DST_ALG_NSEC3RSASHA1:
1519         case DST_ALG_RSASHA256:
1520         case DST_ALG_RSASHA512:
1521         case DST_ALG_DSA:
1522         case DST_ALG_NSEC3DSA:
1523         case DST_ALG_DH:
1524         case DST_ALG_ECCGOST:
1525         case DST_ALG_ECDSA256:
1526         case DST_ALG_ECDSA384:
1527                 return (ISC_FALSE);
1528         case DST_ALG_HMACMD5:
1529         case DST_ALG_GSSAPI:
1530                 return (ISC_TRUE);
1531         default:
1532                 return (ISC_FALSE);
1533         }
1534 }
1535
1536 /*%
1537  * Write key timing metadata to a file pointer, preceded by 'tag'
1538  */
1539 static void
1540 printtime(const dst_key_t *key, int type, const char *tag, FILE *stream) {
1541         isc_result_t result;
1542 #ifdef ISC_PLATFORM_USETHREADS
1543         char output[26]; /* Minimum buffer as per ctime_r() specification. */
1544 #else
1545         const char *output;
1546 #endif
1547         isc_stdtime_t when;
1548         time_t t;
1549         char utc[sizeof("YYYYMMDDHHSSMM")];
1550         isc_buffer_t b;
1551         isc_region_t r;
1552
1553         result = dst_key_gettime(key, type, &when);
1554         if (result == ISC_R_NOTFOUND)
1555                 return;
1556
1557         /* time_t and isc_stdtime_t might be different sizes */
1558         t = when;
1559 #ifdef ISC_PLATFORM_USETHREADS
1560 #ifdef WIN32
1561         if (ctime_s(output, sizeof(output), &t) != 0)
1562                 goto error;
1563 #else
1564         if (ctime_r(&t, output) == NULL)
1565                 goto error;
1566 #endif
1567 #else
1568         output = ctime(&t);
1569 #endif
1570
1571         isc_buffer_init(&b, utc, sizeof(utc));
1572         result = dns_time32_totext(when, &b);
1573         if (result != ISC_R_SUCCESS)
1574                 goto error;
1575
1576         isc_buffer_usedregion(&b, &r);
1577         fprintf(stream, "%s: %.*s (%.*s)\n", tag, (int)r.length, r.base,
1578                  (int)strlen(output) - 1, output);
1579         return;
1580
1581  error:
1582         fprintf(stream, "%s: (set, unable to display)\n", tag);
1583 }
1584
1585 /*%
1586  * Writes a public key to disk in DNS format.
1587  */
1588 static isc_result_t
1589 write_public_key(const dst_key_t *key, int type, const char *directory) {
1590         FILE *fp;
1591         isc_buffer_t keyb, textb, fileb, classb;
1592         isc_region_t r;
1593         char filename[ISC_DIR_NAMEMAX];
1594         unsigned char key_array[DST_KEY_MAXSIZE];
1595         char text_array[DST_KEY_MAXTEXTSIZE];
1596         char class_array[10];
1597         isc_result_t ret;
1598         dns_rdata_t rdata = DNS_RDATA_INIT;
1599         isc_fsaccess_t access;
1600
1601         REQUIRE(VALID_KEY(key));
1602
1603         isc_buffer_init(&keyb, key_array, sizeof(key_array));
1604         isc_buffer_init(&textb, text_array, sizeof(text_array));
1605         isc_buffer_init(&classb, class_array, sizeof(class_array));
1606
1607         ret = dst_key_todns(key, &keyb);
1608         if (ret != ISC_R_SUCCESS)
1609                 return (ret);
1610
1611         isc_buffer_usedregion(&keyb, &r);
1612         dns_rdata_fromregion(&rdata, key->key_class, dns_rdatatype_dnskey, &r);
1613
1614         ret = dns_rdata_totext(&rdata, (dns_name_t *) NULL, &textb);
1615         if (ret != ISC_R_SUCCESS)
1616                 return (DST_R_INVALIDPUBLICKEY);
1617
1618         ret = dns_rdataclass_totext(key->key_class, &classb);
1619         if (ret != ISC_R_SUCCESS)
1620                 return (DST_R_INVALIDPUBLICKEY);
1621
1622         /*
1623          * Make the filename.
1624          */
1625         isc_buffer_init(&fileb, filename, sizeof(filename));
1626         ret = dst_key_buildfilename(key, DST_TYPE_PUBLIC, directory, &fileb);
1627         if (ret != ISC_R_SUCCESS)
1628                 return (ret);
1629
1630         /*
1631          * Create public key file.
1632          */
1633         if ((fp = fopen(filename, "w")) == NULL)
1634                 return (DST_R_WRITEERROR);
1635
1636         if (issymmetric(key)) {
1637                 access = 0;
1638                 isc_fsaccess_add(ISC_FSACCESS_OWNER,
1639                                  ISC_FSACCESS_READ | ISC_FSACCESS_WRITE,
1640                                  &access);
1641                 (void)isc_fsaccess_set(filename, access);
1642         }
1643
1644         /* Write key information in comments */
1645         if ((type & DST_TYPE_KEY) == 0) {
1646                 fprintf(fp, "; This is a %s%s-signing key, keyid %d, for ",
1647                         (key->key_flags & DNS_KEYFLAG_REVOKE) != 0 ?
1648                                 "revoked " :
1649                                 "",
1650                         (key->key_flags & DNS_KEYFLAG_KSK) != 0 ?
1651                                 "key" :
1652                                 "zone",
1653                         key->key_id);
1654                 ret = dns_name_print(key->key_name, fp);
1655                 if (ret != ISC_R_SUCCESS) {
1656                         fclose(fp);
1657                         return (ret);
1658                 }
1659                 fputc('\n', fp);
1660
1661                 printtime(key, DST_TIME_CREATED, "; Created", fp);
1662                 printtime(key, DST_TIME_PUBLISH, "; Publish", fp);
1663                 printtime(key, DST_TIME_ACTIVATE, "; Activate", fp);
1664                 printtime(key, DST_TIME_REVOKE, "; Revoke", fp);
1665                 printtime(key, DST_TIME_INACTIVE, "; Inactive", fp);
1666                 printtime(key, DST_TIME_DELETE, "; Delete", fp);
1667         }
1668
1669         /* Now print the actual key */
1670         ret = dns_name_print(key->key_name, fp);
1671         fprintf(fp, " ");
1672
1673         if (key->key_ttl != 0)
1674                 fprintf(fp, "%d ", key->key_ttl);
1675
1676         isc_buffer_usedregion(&classb, &r);
1677         if ((unsigned) fwrite(r.base, 1, r.length, fp) != r.length)
1678                ret = DST_R_WRITEERROR;
1679
1680         if ((type & DST_TYPE_KEY) != 0)
1681                 fprintf(fp, " KEY ");
1682         else
1683                 fprintf(fp, " DNSKEY ");
1684
1685         isc_buffer_usedregion(&textb, &r);
1686         if ((unsigned) fwrite(r.base, 1, r.length, fp) != r.length)
1687                ret = DST_R_WRITEERROR;
1688
1689         fputc('\n', fp);
1690         fflush(fp);
1691         if (ferror(fp))
1692                 ret = DST_R_WRITEERROR;
1693         fclose(fp);
1694
1695         return (ret);
1696 }
1697
1698 static isc_result_t
1699 buildfilename(dns_name_t *name, dns_keytag_t id,
1700               unsigned int alg, unsigned int type,
1701               const char *directory, isc_buffer_t *out)
1702 {
1703         const char *suffix = "";
1704         unsigned int len;
1705         isc_result_t result;
1706
1707         REQUIRE(out != NULL);
1708         if ((type & DST_TYPE_PRIVATE) != 0)
1709                 suffix = ".private";
1710         else if (type == DST_TYPE_PUBLIC)
1711                 suffix = ".key";
1712         if (directory != NULL) {
1713                 if (isc_buffer_availablelength(out) < strlen(directory))
1714                         return (ISC_R_NOSPACE);
1715                 isc_buffer_putstr(out, directory);
1716                 if (strlen(directory) > 0U &&
1717                     directory[strlen(directory) - 1] != '/')
1718                         isc_buffer_putstr(out, "/");
1719         }
1720         if (isc_buffer_availablelength(out) < 1)
1721                 return (ISC_R_NOSPACE);
1722         isc_buffer_putstr(out, "K");
1723         result = dns_name_tofilenametext(name, ISC_FALSE, out);
1724         if (result != ISC_R_SUCCESS)
1725                 return (result);
1726         len = 1 + 3 + 1 + 5 + strlen(suffix) + 1;
1727         if (isc_buffer_availablelength(out) < len)
1728                 return (ISC_R_NOSPACE);
1729         sprintf((char *) isc_buffer_used(out), "+%03d+%05d%s", alg, id,
1730                 suffix);
1731         isc_buffer_add(out, len);
1732
1733         return (ISC_R_SUCCESS);
1734 }
1735
1736 static isc_result_t
1737 computeid(dst_key_t *key) {
1738         isc_buffer_t dnsbuf;
1739         unsigned char dns_array[DST_KEY_MAXSIZE];
1740         isc_region_t r;
1741         isc_result_t ret;
1742
1743         isc_buffer_init(&dnsbuf, dns_array, sizeof(dns_array));
1744         ret = dst_key_todns(key, &dnsbuf);
1745         if (ret != ISC_R_SUCCESS)
1746                 return (ret);
1747
1748         isc_buffer_usedregion(&dnsbuf, &r);
1749         key->key_id = dst_region_computeid(&r, key->key_alg);
1750         key->key_rid = dst_region_computerid(&r, key->key_alg);
1751         return (ISC_R_SUCCESS);
1752 }
1753
1754 static isc_result_t
1755 frombuffer(dns_name_t *name, unsigned int alg, unsigned int flags,
1756            unsigned int protocol, dns_rdataclass_t rdclass,
1757            isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp)
1758 {
1759         dst_key_t *key;
1760         isc_result_t ret;
1761
1762         REQUIRE(dns_name_isabsolute(name));
1763         REQUIRE(source != NULL);
1764         REQUIRE(mctx != NULL);
1765         REQUIRE(keyp != NULL && *keyp == NULL);
1766
1767         key = get_key_struct(name, alg, flags, protocol, 0, rdclass, 0, mctx);
1768         if (key == NULL)
1769                 return (ISC_R_NOMEMORY);
1770
1771         if (isc_buffer_remaininglength(source) > 0) {
1772                 ret = algorithm_status(alg);
1773                 if (ret != ISC_R_SUCCESS) {
1774                         dst_key_free(&key);
1775                         return (ret);
1776                 }
1777                 if (key->func->fromdns == NULL) {
1778                         dst_key_free(&key);
1779                         return (DST_R_UNSUPPORTEDALG);
1780                 }
1781
1782                 ret = key->func->fromdns(key, source);
1783                 if (ret != ISC_R_SUCCESS) {
1784                         dst_key_free(&key);
1785                         return (ret);
1786                 }
1787         }
1788
1789         *keyp = key;
1790         return (ISC_R_SUCCESS);
1791 }
1792
1793 static isc_result_t
1794 algorithm_status(unsigned int alg) {
1795         REQUIRE(dst_initialized == ISC_TRUE);
1796
1797         if (dst_algorithm_supported(alg))
1798                 return (ISC_R_SUCCESS);
1799 #ifndef OPENSSL
1800         if (alg == DST_ALG_RSAMD5 || alg == DST_ALG_RSASHA1 ||
1801             alg == DST_ALG_DSA || alg == DST_ALG_DH ||
1802             alg == DST_ALG_HMACMD5 || alg == DST_ALG_NSEC3DSA ||
1803             alg == DST_ALG_NSEC3RSASHA1 ||
1804             alg == DST_ALG_RSASHA256 || alg == DST_ALG_RSASHA512 ||
1805             alg == DST_ALG_ECCGOST ||
1806             alg == DST_ALG_ECDSA256 || alg == DST_ALG_ECDSA384)
1807                 return (DST_R_NOCRYPTO);
1808 #endif
1809         return (DST_R_UNSUPPORTEDALG);
1810 }
1811
1812 static isc_result_t
1813 addsuffix(char *filename, int len, const char *odirname,
1814           const char *ofilename, const char *suffix)
1815 {
1816         int olen = strlen(ofilename);
1817         int n;
1818
1819         if (olen > 1 && ofilename[olen - 1] == '.')
1820                 olen -= 1;
1821         else if (olen > 8 && strcmp(ofilename + olen - 8, ".private") == 0)
1822                 olen -= 8;
1823         else if (olen > 4 && strcmp(ofilename + olen - 4, ".key") == 0)
1824                 olen -= 4;
1825
1826         if (odirname == NULL)
1827                 n = snprintf(filename, len, "%.*s%s", olen, ofilename, suffix);
1828         else
1829                 n = snprintf(filename, len, "%s/%.*s%s",
1830                              odirname, olen, ofilename, suffix);
1831         if (n < 0)
1832                 return (ISC_R_FAILURE);
1833         if (n >= len)
1834                 return (ISC_R_NOSPACE);
1835         return (ISC_R_SUCCESS);
1836 }
1837
1838 isc_result_t
1839 dst__entropy_getdata(void *buf, unsigned int len, isc_boolean_t pseudo) {
1840 #ifdef BIND9
1841         unsigned int flags = dst_entropy_flags;
1842
1843         if (len == 0)
1844                 return (ISC_R_SUCCESS);
1845         if (pseudo)
1846                 flags &= ~ISC_ENTROPY_GOODONLY;
1847         else
1848                 flags |= ISC_ENTROPY_BLOCKING;
1849         return (isc_entropy_getdata(dst_entropy_pool, buf, len, NULL, flags));
1850 #else
1851         UNUSED(buf);
1852         UNUSED(len);
1853         UNUSED(pseudo);
1854
1855         return (ISC_R_NOTIMPLEMENTED);
1856 #endif
1857 }
1858
1859 unsigned int
1860 dst__entropy_status(void) {
1861 #ifdef BIND9
1862 #ifdef GSSAPI
1863         unsigned int flags = dst_entropy_flags;
1864         isc_result_t ret;
1865         unsigned char buf[32];
1866         static isc_boolean_t first = ISC_TRUE;
1867
1868         if (first) {
1869                 /* Someone believes RAND_status() initializes the PRNG */
1870                 flags &= ~ISC_ENTROPY_GOODONLY;
1871                 ret = isc_entropy_getdata(dst_entropy_pool, buf,
1872                                           sizeof(buf), NULL, flags);
1873                 INSIST(ret == ISC_R_SUCCESS);
1874                 isc_entropy_putdata(dst_entropy_pool, buf,
1875                                     sizeof(buf), 2 * sizeof(buf));
1876                 first = ISC_FALSE;
1877         }
1878 #endif
1879         return (isc_entropy_status(dst_entropy_pool));
1880 #else
1881         return (0);
1882 #endif
1883 }
1884
1885 isc_buffer_t *
1886 dst_key_tkeytoken(const dst_key_t *key) {
1887         REQUIRE(VALID_KEY(key));
1888         return (key->key_tkeytoken);
1889 }