]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bind9/lib/dns/tkey.c
MFaltix:
[FreeBSD/FreeBSD.git] / contrib / bind9 / lib / dns / tkey.c
1 /*
2  * Copyright (C) 2004-2008, 2010  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2001, 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 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 /*
19  * $Id: tkey.c,v 1.90.118.4 2010-12-09 01:12:55 marka Exp $
20  */
21 /*! \file */
22 #include <config.h>
23
24 #include <isc/buffer.h>
25 #include <isc/entropy.h>
26 #include <isc/md5.h>
27 #include <isc/mem.h>
28 #include <isc/string.h>
29 #include <isc/util.h>
30
31 #include <dns/dnssec.h>
32 #include <dns/fixedname.h>
33 #include <dns/keyvalues.h>
34 #include <dns/log.h>
35 #include <dns/message.h>
36 #include <dns/name.h>
37 #include <dns/rdata.h>
38 #include <dns/rdatalist.h>
39 #include <dns/rdataset.h>
40 #include <dns/rdatastruct.h>
41 #include <dns/result.h>
42 #include <dns/tkey.h>
43 #include <dns/tsig.h>
44
45 #include <dst/dst.h>
46 #include <dst/gssapi.h>
47
48 #define TKEY_RANDOM_AMOUNT 16
49
50 #define RETERR(x) do { \
51         result = (x); \
52         if (result != ISC_R_SUCCESS) \
53                 goto failure; \
54         } while (0)
55
56 static void
57 tkey_log(const char *fmt, ...) ISC_FORMAT_PRINTF(1, 2);
58
59 static void
60 tkey_log(const char *fmt, ...) {
61         va_list ap;
62
63         va_start(ap, fmt);
64         isc_log_vwrite(dns_lctx, DNS_LOGCATEGORY_GENERAL,
65                        DNS_LOGMODULE_REQUEST, ISC_LOG_DEBUG(4), fmt, ap);
66         va_end(ap);
67 }
68
69 static void
70 _dns_tkey_dumpmessage(dns_message_t *msg) {
71         isc_buffer_t outbuf;
72         unsigned char output[4096];
73         isc_result_t result;
74
75         isc_buffer_init(&outbuf, output, sizeof(output));
76         result = dns_message_totext(msg, &dns_master_style_debug, 0,
77                                     &outbuf);
78         /* XXXMLG ignore result */
79         fprintf(stderr, "%.*s\n", (int)isc_buffer_usedlength(&outbuf),
80                 (char *)isc_buffer_base(&outbuf));
81 }
82
83 isc_result_t
84 dns_tkeyctx_create(isc_mem_t *mctx, isc_entropy_t *ectx, dns_tkeyctx_t **tctxp)
85 {
86         dns_tkeyctx_t *tctx;
87
88         REQUIRE(mctx != NULL);
89         REQUIRE(ectx != NULL);
90         REQUIRE(tctxp != NULL && *tctxp == NULL);
91
92         tctx = isc_mem_get(mctx, sizeof(dns_tkeyctx_t));
93         if (tctx == NULL)
94                 return (ISC_R_NOMEMORY);
95         tctx->mctx = NULL;
96         isc_mem_attach(mctx, &tctx->mctx);
97         tctx->ectx = NULL;
98         isc_entropy_attach(ectx, &tctx->ectx);
99         tctx->dhkey = NULL;
100         tctx->domain = NULL;
101         tctx->gsscred = NULL;
102
103         *tctxp = tctx;
104         return (ISC_R_SUCCESS);
105 }
106
107 void
108 dns_tkeyctx_destroy(dns_tkeyctx_t **tctxp) {
109         isc_mem_t *mctx;
110         dns_tkeyctx_t *tctx;
111
112         REQUIRE(tctxp != NULL && *tctxp != NULL);
113
114         tctx = *tctxp;
115         mctx = tctx->mctx;
116
117         if (tctx->dhkey != NULL)
118                 dst_key_free(&tctx->dhkey);
119         if (tctx->domain != NULL) {
120                 if (dns_name_dynamic(tctx->domain))
121                         dns_name_free(tctx->domain, mctx);
122                 isc_mem_put(mctx, tctx->domain, sizeof(dns_name_t));
123         }
124         if (tctx->gsscred != NULL)
125                 dst_gssapi_releasecred(&tctx->gsscred);
126         isc_entropy_detach(&tctx->ectx);
127         isc_mem_put(mctx, tctx, sizeof(dns_tkeyctx_t));
128         isc_mem_detach(&mctx);
129         *tctxp = NULL;
130 }
131
132 static isc_result_t
133 add_rdata_to_list(dns_message_t *msg, dns_name_t *name, dns_rdata_t *rdata,
134                 isc_uint32_t ttl, dns_namelist_t *namelist)
135 {
136         isc_result_t result;
137         isc_region_t r, newr;
138         dns_rdata_t *newrdata = NULL;
139         dns_name_t *newname = NULL;
140         dns_rdatalist_t *newlist = NULL;
141         dns_rdataset_t *newset = NULL;
142         isc_buffer_t *tmprdatabuf = NULL;
143
144         RETERR(dns_message_gettemprdata(msg, &newrdata));
145
146         dns_rdata_toregion(rdata, &r);
147         RETERR(isc_buffer_allocate(msg->mctx, &tmprdatabuf, r.length));
148         isc_buffer_availableregion(tmprdatabuf, &newr);
149         memcpy(newr.base, r.base, r.length);
150         dns_rdata_fromregion(newrdata, rdata->rdclass, rdata->type, &newr);
151         dns_message_takebuffer(msg, &tmprdatabuf);
152
153         RETERR(dns_message_gettempname(msg, &newname));
154         dns_name_init(newname, NULL);
155         RETERR(dns_name_dup(name, msg->mctx, newname));
156
157         RETERR(dns_message_gettemprdatalist(msg, &newlist));
158         newlist->rdclass = newrdata->rdclass;
159         newlist->type = newrdata->type;
160         newlist->covers = 0;
161         newlist->ttl = ttl;
162         ISC_LIST_INIT(newlist->rdata);
163         ISC_LIST_APPEND(newlist->rdata, newrdata, link);
164
165         RETERR(dns_message_gettemprdataset(msg, &newset));
166         dns_rdataset_init(newset);
167         RETERR(dns_rdatalist_tordataset(newlist, newset));
168
169         ISC_LIST_INIT(newname->list);
170         ISC_LIST_APPEND(newname->list, newset, link);
171
172         ISC_LIST_APPEND(*namelist, newname, link);
173
174         return (ISC_R_SUCCESS);
175
176  failure:
177         if (newrdata != NULL) {
178                 if (ISC_LINK_LINKED(newrdata, link))
179                         ISC_LIST_UNLINK(newlist->rdata, newrdata, link);
180                 dns_message_puttemprdata(msg, &newrdata);
181         }
182         if (newname != NULL)
183                 dns_message_puttempname(msg, &newname);
184         if (newset != NULL) {
185                 dns_rdataset_disassociate(newset);
186                 dns_message_puttemprdataset(msg, &newset);
187         }
188         if (newlist != NULL)
189                 dns_message_puttemprdatalist(msg, &newlist);
190         return (result);
191 }
192
193 static void
194 free_namelist(dns_message_t *msg, dns_namelist_t *namelist) {
195         dns_name_t *name;
196         dns_rdataset_t *set;
197
198         while (!ISC_LIST_EMPTY(*namelist)) {
199                 name = ISC_LIST_HEAD(*namelist);
200                 ISC_LIST_UNLINK(*namelist, name, link);
201                 while (!ISC_LIST_EMPTY(name->list)) {
202                         set = ISC_LIST_HEAD(name->list);
203                         ISC_LIST_UNLINK(name->list, set, link);
204                         dns_message_puttemprdataset(msg, &set);
205                 }
206                 dns_message_puttempname(msg, &name);
207         }
208 }
209
210 static isc_result_t
211 compute_secret(isc_buffer_t *shared, isc_region_t *queryrandomness,
212                isc_region_t *serverrandomness, isc_buffer_t *secret)
213 {
214         isc_md5_t md5ctx;
215         isc_region_t r, r2;
216         unsigned char digests[32];
217         unsigned int i;
218
219         isc_buffer_usedregion(shared, &r);
220
221         /*
222          * MD5 ( query data | DH value ).
223          */
224         isc_md5_init(&md5ctx);
225         isc_md5_update(&md5ctx, queryrandomness->base,
226                        queryrandomness->length);
227         isc_md5_update(&md5ctx, r.base, r.length);
228         isc_md5_final(&md5ctx, digests);
229
230         /*
231          * MD5 ( server data | DH value ).
232          */
233         isc_md5_init(&md5ctx);
234         isc_md5_update(&md5ctx, serverrandomness->base,
235                        serverrandomness->length);
236         isc_md5_update(&md5ctx, r.base, r.length);
237         isc_md5_final(&md5ctx, &digests[ISC_MD5_DIGESTLENGTH]);
238
239         /*
240          * XOR ( DH value, MD5-1 | MD5-2).
241          */
242         isc_buffer_availableregion(secret, &r);
243         isc_buffer_usedregion(shared, &r2);
244         if (r.length < sizeof(digests) || r.length < r2.length)
245                 return (ISC_R_NOSPACE);
246         if (r2.length > sizeof(digests)) {
247                 memcpy(r.base, r2.base, r2.length);
248                 for (i = 0; i < sizeof(digests); i++)
249                         r.base[i] ^= digests[i];
250                 isc_buffer_add(secret, r2.length);
251         } else {
252                 memcpy(r.base, digests, sizeof(digests));
253                 for (i = 0; i < r2.length; i++)
254                         r.base[i] ^= r2.base[i];
255                 isc_buffer_add(secret, sizeof(digests));
256         }
257         return (ISC_R_SUCCESS);
258
259 }
260
261 static isc_result_t
262 process_dhtkey(dns_message_t *msg, dns_name_t *signer, dns_name_t *name,
263                dns_rdata_tkey_t *tkeyin, dns_tkeyctx_t *tctx,
264                dns_rdata_tkey_t *tkeyout,
265                dns_tsig_keyring_t *ring, dns_namelist_t *namelist)
266 {
267         isc_result_t result = ISC_R_SUCCESS;
268         dns_name_t *keyname, ourname;
269         dns_rdataset_t *keyset = NULL;
270         dns_rdata_t keyrdata = DNS_RDATA_INIT, ourkeyrdata = DNS_RDATA_INIT;
271         isc_boolean_t found_key = ISC_FALSE, found_incompatible = ISC_FALSE;
272         dst_key_t *pubkey = NULL;
273         isc_buffer_t ourkeybuf, *shared = NULL;
274         isc_region_t r, r2, ourkeyr;
275         unsigned char keydata[DST_KEY_MAXSIZE];
276         unsigned int sharedsize;
277         isc_buffer_t secret;
278         unsigned char *randomdata = NULL, secretdata[256];
279         dns_ttl_t ttl = 0;
280
281         if (tctx->dhkey == NULL) {
282                 tkey_log("process_dhtkey: tkey-dhkey not defined");
283                 tkeyout->error = dns_tsigerror_badalg;
284                 return (DNS_R_REFUSED);
285         }
286
287         if (!dns_name_equal(&tkeyin->algorithm, DNS_TSIG_HMACMD5_NAME)) {
288                 tkey_log("process_dhtkey: algorithms other than "
289                          "hmac-md5 are not supported");
290                 tkeyout->error = dns_tsigerror_badalg;
291                 return (ISC_R_SUCCESS);
292         }
293
294         /*
295          * Look for a DH KEY record that will work with ours.
296          */
297         for (result = dns_message_firstname(msg, DNS_SECTION_ADDITIONAL);
298              result == ISC_R_SUCCESS && !found_key;
299              result = dns_message_nextname(msg, DNS_SECTION_ADDITIONAL)) {
300                 keyname = NULL;
301                 dns_message_currentname(msg, DNS_SECTION_ADDITIONAL, &keyname);
302                 keyset = NULL;
303                 result = dns_message_findtype(keyname, dns_rdatatype_key, 0,
304                                               &keyset);
305                 if (result != ISC_R_SUCCESS)
306                         continue;
307
308                 for (result = dns_rdataset_first(keyset);
309                      result == ISC_R_SUCCESS && !found_key;
310                      result = dns_rdataset_next(keyset)) {
311                         dns_rdataset_current(keyset, &keyrdata);
312                         pubkey = NULL;
313                         result = dns_dnssec_keyfromrdata(keyname, &keyrdata,
314                                                          msg->mctx, &pubkey);
315                         if (result != ISC_R_SUCCESS) {
316                                 dns_rdata_reset(&keyrdata);
317                                 continue;
318                         }
319                         if (dst_key_alg(pubkey) == DNS_KEYALG_DH) {
320                                 if (dst_key_paramcompare(pubkey, tctx->dhkey))
321                                 {
322                                         found_key = ISC_TRUE;
323                                         ttl = keyset->ttl;
324                                         break;
325                                 } else
326                                         found_incompatible = ISC_TRUE;
327                         }
328                         dst_key_free(&pubkey);
329                         dns_rdata_reset(&keyrdata);
330                 }
331         }
332
333         if (!found_key) {
334                 if (found_incompatible) {
335                         tkey_log("process_dhtkey: found an incompatible key");
336                         tkeyout->error = dns_tsigerror_badkey;
337                         return (ISC_R_SUCCESS);
338                 } else {
339                         tkey_log("process_dhtkey: failed to find a key");
340                         return (DNS_R_FORMERR);
341                 }
342         }
343
344         RETERR(add_rdata_to_list(msg, keyname, &keyrdata, ttl, namelist));
345
346         isc_buffer_init(&ourkeybuf, keydata, sizeof(keydata));
347         RETERR(dst_key_todns(tctx->dhkey, &ourkeybuf));
348         isc_buffer_usedregion(&ourkeybuf, &ourkeyr);
349         dns_rdata_fromregion(&ourkeyrdata, dns_rdataclass_any,
350                              dns_rdatatype_key, &ourkeyr);
351
352         dns_name_init(&ourname, NULL);
353         dns_name_clone(dst_key_name(tctx->dhkey), &ourname);
354
355         /*
356          * XXXBEW The TTL should be obtained from the database, if it exists.
357          */
358         RETERR(add_rdata_to_list(msg, &ourname, &ourkeyrdata, 0, namelist));
359
360         RETERR(dst_key_secretsize(tctx->dhkey, &sharedsize));
361         RETERR(isc_buffer_allocate(msg->mctx, &shared, sharedsize));
362
363         result = dst_key_computesecret(pubkey, tctx->dhkey, shared);
364         if (result != ISC_R_SUCCESS) {
365                 tkey_log("process_dhtkey: failed to compute shared secret: %s",
366                          isc_result_totext(result));
367                 goto failure;
368         }
369         dst_key_free(&pubkey);
370
371         isc_buffer_init(&secret, secretdata, sizeof(secretdata));
372
373         randomdata = isc_mem_get(tkeyout->mctx, TKEY_RANDOM_AMOUNT);
374         if (randomdata == NULL)
375                 goto failure;
376
377         result = isc_entropy_getdata(tctx->ectx, randomdata,
378                                      TKEY_RANDOM_AMOUNT, NULL, 0);
379         if (result != ISC_R_SUCCESS) {
380                 tkey_log("process_dhtkey: failed to obtain entropy: %s",
381                          isc_result_totext(result));
382                 goto failure;
383         }
384
385         r.base = randomdata;
386         r.length = TKEY_RANDOM_AMOUNT;
387         r2.base = tkeyin->key;
388         r2.length = tkeyin->keylen;
389         RETERR(compute_secret(shared, &r2, &r, &secret));
390         isc_buffer_free(&shared);
391
392         RETERR(dns_tsigkey_create(name, &tkeyin->algorithm,
393                                   isc_buffer_base(&secret),
394                                   isc_buffer_usedlength(&secret),
395                                   ISC_TRUE, signer, tkeyin->inception,
396                                   tkeyin->expire, ring->mctx, ring, NULL));
397
398         /* This key is good for a long time */
399         tkeyout->inception = tkeyin->inception;
400         tkeyout->expire = tkeyin->expire;
401
402         tkeyout->key = randomdata;
403         tkeyout->keylen = TKEY_RANDOM_AMOUNT;
404
405         return (ISC_R_SUCCESS);
406
407  failure:
408         if (!ISC_LIST_EMPTY(*namelist))
409                 free_namelist(msg, namelist);
410         if (shared != NULL)
411                 isc_buffer_free(&shared);
412         if (pubkey != NULL)
413                 dst_key_free(&pubkey);
414         if (randomdata != NULL)
415                 isc_mem_put(tkeyout->mctx, randomdata, TKEY_RANDOM_AMOUNT);
416         return (result);
417 }
418
419 static isc_result_t
420 process_gsstkey(dns_name_t *name, dns_rdata_tkey_t *tkeyin,
421                 dns_tkeyctx_t *tctx, dns_rdata_tkey_t *tkeyout,
422                 dns_tsig_keyring_t *ring)
423 {
424         isc_result_t result = ISC_R_SUCCESS;
425         dst_key_t *dstkey = NULL;
426         dns_tsigkey_t *tsigkey = NULL;
427         dns_fixedname_t principal;
428         isc_stdtime_t now;
429         isc_region_t intoken;
430         isc_buffer_t *outtoken = NULL;
431         gss_ctx_id_t gss_ctx = NULL;
432
433         if (tctx->gsscred == NULL)
434                 return (ISC_R_NOPERM);
435
436         if (!dns_name_equal(&tkeyin->algorithm, DNS_TSIG_GSSAPI_NAME) &&
437             !dns_name_equal(&tkeyin->algorithm, DNS_TSIG_GSSAPIMS_NAME)) {
438                 tkeyout->error = dns_tsigerror_badalg;
439                 tkey_log("process_gsstkey(): dns_tsigerror_badalg");    /* XXXSRA */
440                 return (ISC_R_SUCCESS);
441         }
442
443         /*
444          * XXXDCL need to check for key expiry per 4.1.1
445          * XXXDCL need a way to check fully established, perhaps w/key_flags
446          */
447
448         intoken.base = tkeyin->key;
449         intoken.length = tkeyin->keylen;
450
451         result = dns_tsigkey_find(&tsigkey, name, &tkeyin->algorithm, ring);
452         if (result == ISC_R_SUCCESS)
453                 gss_ctx = dst_key_getgssctx(tsigkey->key);
454
455         dns_fixedname_init(&principal);
456
457         result = dst_gssapi_acceptctx(tctx->gsscred, &intoken,
458                                       &outtoken, &gss_ctx,
459                                       dns_fixedname_name(&principal),
460                                       tctx->mctx);
461         if (result == DNS_R_INVALIDTKEY) {
462                 if (tsigkey != NULL)
463                         dns_tsigkey_detach(&tsigkey);
464                 tkeyout->error = dns_tsigerror_badkey;
465                 tkey_log("process_gsstkey(): dns_tsigerror_badkey");    /* XXXSRA */
466                 return (ISC_R_SUCCESS);
467         } else if (result == ISC_R_FAILURE)
468                 goto failure;
469         ENSURE(result == DNS_R_CONTINUE || result == ISC_R_SUCCESS);
470         /*
471          * XXXDCL Section 4.1.3: Limit GSS_S_CONTINUE_NEEDED to 10 times.
472          */
473
474         isc_stdtime_get(&now);
475
476         if (tsigkey == NULL) {
477 #ifdef GSSAPI
478                 OM_uint32 gret, minor, lifetime;
479 #endif
480                 isc_uint32_t expire;
481
482                 RETERR(dst_key_fromgssapi(name, gss_ctx, ring->mctx, &dstkey));
483                 /*
484                  * Limit keys to 1 hour or the context's lifetime whichever
485                  * is smaller.
486                  */
487                 expire = now + 3600;
488 #ifdef GSSAPI
489                 gret = gss_context_time(&minor, gss_ctx, &lifetime);
490                 if (gret == GSS_S_COMPLETE && now + lifetime < expire)
491                         expire = now + lifetime;
492 #endif
493                 RETERR(dns_tsigkey_createfromkey(name, &tkeyin->algorithm,
494                                                  dstkey, ISC_TRUE,
495                                                  dns_fixedname_name(&principal),
496                                                  now, expire, ring->mctx, ring,
497                                                  NULL));
498                 dst_key_free(&dstkey);
499                 tkeyout->inception = now;
500                 tkeyout->expire = expire;
501         } else {
502                 tkeyout->inception = tsigkey->inception;
503                 tkeyout->expire = tkeyout->expire;
504                 dns_tsigkey_detach(&tsigkey);
505         }
506
507         if (outtoken) {
508                 tkeyout->key = isc_mem_get(tkeyout->mctx,
509                                            isc_buffer_usedlength(outtoken));
510                 if (tkeyout->key == NULL) {
511                         result = ISC_R_NOMEMORY;
512                         goto failure;
513                 }
514                 tkeyout->keylen = isc_buffer_usedlength(outtoken);
515                 memcpy(tkeyout->key, isc_buffer_base(outtoken),
516                        isc_buffer_usedlength(outtoken));
517                 isc_buffer_free(&outtoken);
518         } else {
519                 tkeyout->key = isc_mem_get(tkeyout->mctx, tkeyin->keylen);
520                 if (tkeyout->key == NULL) {
521                         result = ISC_R_NOMEMORY;
522                         goto failure;
523                 }
524                 tkeyout->keylen = tkeyin->keylen;
525                 memcpy(tkeyout->key, tkeyin->key, tkeyin->keylen);
526         }
527
528         tkeyout->error = dns_rcode_noerror;
529
530         tkey_log("process_gsstkey(): dns_tsigerror_noerror");   /* XXXSRA */
531
532         return (ISC_R_SUCCESS);
533
534 failure:
535         if (tsigkey != NULL)
536                 dns_tsigkey_detach(&tsigkey);
537
538         if (dstkey != NULL)
539                 dst_key_free(&dstkey);
540
541         if (outtoken != NULL)
542                 isc_buffer_free(&outtoken);
543
544         tkey_log("process_gsstkey(): %s",
545                 isc_result_totext(result));     /* XXXSRA */
546
547         return (result);
548 }
549
550 static isc_result_t
551 process_deletetkey(dns_name_t *signer, dns_name_t *name,
552                    dns_rdata_tkey_t *tkeyin, dns_rdata_tkey_t *tkeyout,
553                    dns_tsig_keyring_t *ring)
554 {
555         isc_result_t result;
556         dns_tsigkey_t *tsigkey = NULL;
557         dns_name_t *identity;
558
559         result = dns_tsigkey_find(&tsigkey, name, &tkeyin->algorithm, ring);
560         if (result != ISC_R_SUCCESS) {
561                 tkeyout->error = dns_tsigerror_badname;
562                 return (ISC_R_SUCCESS);
563         }
564
565         /*
566          * Only allow a delete if the identity that created the key is the
567          * same as the identity that signed the message.
568          */
569         identity = dns_tsigkey_identity(tsigkey);
570         if (identity == NULL || !dns_name_equal(identity, signer)) {
571                 dns_tsigkey_detach(&tsigkey);
572                 return (DNS_R_REFUSED);
573         }
574
575         /*
576          * Set the key to be deleted when no references are left.  If the key
577          * was not generated with TKEY and is in the config file, it may be
578          * reloaded later.
579          */
580         dns_tsigkey_setdeleted(tsigkey);
581
582         /* Release the reference */
583         dns_tsigkey_detach(&tsigkey);
584
585         return (ISC_R_SUCCESS);
586 }
587
588 isc_result_t
589 dns_tkey_processquery(dns_message_t *msg, dns_tkeyctx_t *tctx,
590                       dns_tsig_keyring_t *ring)
591 {
592         isc_result_t result = ISC_R_SUCCESS;
593         dns_rdata_tkey_t tkeyin, tkeyout;
594         isc_boolean_t freetkeyin = ISC_FALSE;
595         dns_name_t *qname, *name, *keyname, *signer, tsigner;
596         dns_fixedname_t fkeyname;
597         dns_rdataset_t *tkeyset;
598         dns_rdata_t rdata;
599         dns_namelist_t namelist;
600         char tkeyoutdata[512];
601         isc_buffer_t tkeyoutbuf;
602
603         REQUIRE(msg != NULL);
604         REQUIRE(tctx != NULL);
605         REQUIRE(ring != NULL);
606
607         ISC_LIST_INIT(namelist);
608
609         /*
610          * Interpret the question section.
611          */
612         result = dns_message_firstname(msg, DNS_SECTION_QUESTION);
613         if (result != ISC_R_SUCCESS)
614                 return (DNS_R_FORMERR);
615
616         qname = NULL;
617         dns_message_currentname(msg, DNS_SECTION_QUESTION, &qname);
618
619         /*
620          * Look for a TKEY record that matches the question.
621          */
622         tkeyset = NULL;
623         name = NULL;
624         result = dns_message_findname(msg, DNS_SECTION_ADDITIONAL, qname,
625                                       dns_rdatatype_tkey, 0, &name, &tkeyset);
626         if (result != ISC_R_SUCCESS) {
627                 /*
628                  * Try the answer section, since that's where Win2000
629                  * puts it.
630                  */
631                 if (dns_message_findname(msg, DNS_SECTION_ANSWER, qname,
632                                          dns_rdatatype_tkey, 0, &name,
633                                          &tkeyset) != ISC_R_SUCCESS) {
634                         result = DNS_R_FORMERR;
635                         tkey_log("dns_tkey_processquery: couldn't find a TKEY "
636                                  "matching the question");
637                         goto failure;
638                 }
639         }
640         result = dns_rdataset_first(tkeyset);
641         if (result != ISC_R_SUCCESS) {
642                 result = DNS_R_FORMERR;
643                 goto failure;
644         }
645         dns_rdata_init(&rdata);
646         dns_rdataset_current(tkeyset, &rdata);
647
648         RETERR(dns_rdata_tostruct(&rdata, &tkeyin, NULL));
649         freetkeyin = ISC_TRUE;
650
651         if (tkeyin.error != dns_rcode_noerror) {
652                 result = DNS_R_FORMERR;
653                 goto failure;
654         }
655
656         /*
657          * Before we go any farther, verify that the message was signed.
658          * GSSAPI TKEY doesn't require a signature, the rest do.
659          */
660         dns_name_init(&tsigner, NULL);
661         result = dns_message_signer(msg, &tsigner);
662         if (result != ISC_R_SUCCESS) {
663                 if (tkeyin.mode == DNS_TKEYMODE_GSSAPI &&
664                     result == ISC_R_NOTFOUND)
665                        signer = NULL;
666                 else {
667                         tkey_log("dns_tkey_processquery: query was not "
668                                  "properly signed - rejecting");
669                         result = DNS_R_FORMERR;
670                         goto failure;
671                 }
672         } else
673                 signer = &tsigner;
674
675         tkeyout.common.rdclass = tkeyin.common.rdclass;
676         tkeyout.common.rdtype = tkeyin.common.rdtype;
677         ISC_LINK_INIT(&tkeyout.common, link);
678         tkeyout.mctx = msg->mctx;
679
680         dns_name_init(&tkeyout.algorithm, NULL);
681         dns_name_clone(&tkeyin.algorithm, &tkeyout.algorithm);
682
683         tkeyout.inception = tkeyout.expire = 0;
684         tkeyout.mode = tkeyin.mode;
685         tkeyout.error = 0;
686         tkeyout.keylen = tkeyout.otherlen = 0;
687         tkeyout.key = tkeyout.other = NULL;
688
689         /*
690          * A delete operation must have a fully specified key name.  If this
691          * is not a delete, we do the following:
692          * if (qname != ".")
693          *      keyname = qname + defaultdomain
694          * else
695          *      keyname = <random hex> + defaultdomain
696          */
697         if (tkeyin.mode != DNS_TKEYMODE_DELETE) {
698                 dns_tsigkey_t *tsigkey = NULL;
699
700                 if (tctx->domain == NULL && tkeyin.mode != DNS_TKEYMODE_GSSAPI) {
701                         tkey_log("dns_tkey_processquery: tkey-domain not set");
702                         result = DNS_R_REFUSED;
703                         goto failure;
704                 }
705
706                 dns_fixedname_init(&fkeyname);
707                 keyname = dns_fixedname_name(&fkeyname);
708
709                 if (!dns_name_equal(qname, dns_rootname)) {
710                         unsigned int n = dns_name_countlabels(qname);
711                         RUNTIME_CHECK(dns_name_copy(qname, keyname, NULL)
712                                       == ISC_R_SUCCESS);
713                         dns_name_getlabelsequence(keyname, 0, n - 1, keyname);
714                 } else {
715                         static char hexdigits[16] = {
716                                 '0', '1', '2', '3', '4', '5', '6', '7',
717                                 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
718                         unsigned char randomdata[16];
719                         char randomtext[32];
720                         isc_buffer_t b;
721                         unsigned int i, j;
722
723                         result = isc_entropy_getdata(tctx->ectx,
724                                                      randomdata,
725                                                      sizeof(randomdata),
726                                                      NULL, 0);
727                         if (result != ISC_R_SUCCESS)
728                                 goto failure;
729
730                         for (i = 0, j = 0; i < sizeof(randomdata); i++) {
731                                 unsigned char val = randomdata[i];
732                                 randomtext[j++] = hexdigits[val >> 4];
733                                 randomtext[j++] = hexdigits[val & 0xF];
734                         }
735                         isc_buffer_init(&b, randomtext, sizeof(randomtext));
736                         isc_buffer_add(&b, sizeof(randomtext));
737                         result = dns_name_fromtext(keyname, &b, NULL,
738                                                    ISC_FALSE, NULL);
739                         if (result != ISC_R_SUCCESS)
740                                 goto failure;
741                 }
742
743                 if (tkeyin.mode == DNS_TKEYMODE_GSSAPI) {
744                         /* Yup.  This is a hack */
745                         result = dns_name_concatenate(keyname, dns_rootname,
746                                                       keyname, NULL);
747                         if (result != ISC_R_SUCCESS)
748                                 goto failure;
749                 } else {
750                         result = dns_name_concatenate(keyname, tctx->domain,
751                                                       keyname, NULL);
752                         if (result != ISC_R_SUCCESS)
753                                 goto failure;
754                 }
755
756                 result = dns_tsigkey_find(&tsigkey, keyname, NULL, ring);
757
758                 if (result == ISC_R_SUCCESS) {
759                         tkeyout.error = dns_tsigerror_badname;
760                         dns_tsigkey_detach(&tsigkey);
761                         goto failure_with_tkey;
762                 } else if (result != ISC_R_NOTFOUND)
763                         goto failure;
764         } else
765                 keyname = qname;
766
767         switch (tkeyin.mode) {
768                 case DNS_TKEYMODE_DIFFIEHELLMAN:
769                         tkeyout.error = dns_rcode_noerror;
770                         RETERR(process_dhtkey(msg, signer, keyname, &tkeyin,
771                                               tctx, &tkeyout, ring,
772                                               &namelist));
773                         break;
774                 case DNS_TKEYMODE_GSSAPI:
775                         tkeyout.error = dns_rcode_noerror;
776                         RETERR(process_gsstkey(keyname, &tkeyin, tctx,
777                                                &tkeyout, ring));
778                         break;
779                 case DNS_TKEYMODE_DELETE:
780                         tkeyout.error = dns_rcode_noerror;
781                         RETERR(process_deletetkey(signer, keyname, &tkeyin,
782                                                   &tkeyout, ring));
783                         break;
784                 case DNS_TKEYMODE_SERVERASSIGNED:
785                 case DNS_TKEYMODE_RESOLVERASSIGNED:
786                         result = DNS_R_NOTIMP;
787                         goto failure;
788                 default:
789                         tkeyout.error = dns_tsigerror_badmode;
790         }
791
792  failure_with_tkey:
793         dns_rdata_init(&rdata);
794         isc_buffer_init(&tkeyoutbuf, tkeyoutdata, sizeof(tkeyoutdata));
795         result = dns_rdata_fromstruct(&rdata, tkeyout.common.rdclass,
796                                       tkeyout.common.rdtype, &tkeyout,
797                                       &tkeyoutbuf);
798
799         if (freetkeyin) {
800                 dns_rdata_freestruct(&tkeyin);
801                 freetkeyin = ISC_FALSE;
802         }
803
804         if (tkeyout.key != NULL)
805                 isc_mem_put(tkeyout.mctx, tkeyout.key, tkeyout.keylen);
806         if (tkeyout.other != NULL)
807                 isc_mem_put(tkeyout.mctx, tkeyout.other, tkeyout.otherlen);
808         if (result != ISC_R_SUCCESS)
809                 goto failure;
810
811         RETERR(add_rdata_to_list(msg, keyname, &rdata, 0, &namelist));
812
813         RETERR(dns_message_reply(msg, ISC_TRUE));
814
815         name = ISC_LIST_HEAD(namelist);
816         while (name != NULL) {
817                 dns_name_t *next = ISC_LIST_NEXT(name, link);
818                 ISC_LIST_UNLINK(namelist, name, link);
819                 dns_message_addname(msg, name, DNS_SECTION_ANSWER);
820                 name = next;
821         }
822
823         return (ISC_R_SUCCESS);
824
825  failure:
826         if (freetkeyin)
827                 dns_rdata_freestruct(&tkeyin);
828         if (!ISC_LIST_EMPTY(namelist))
829                 free_namelist(msg, &namelist);
830         return (result);
831 }
832
833 static isc_result_t
834 buildquery(dns_message_t *msg, dns_name_t *name,
835            dns_rdata_tkey_t *tkey, isc_boolean_t win2k)
836 {
837         dns_name_t *qname = NULL, *aname = NULL;
838         dns_rdataset_t *question = NULL, *tkeyset = NULL;
839         dns_rdatalist_t *tkeylist = NULL;
840         dns_rdata_t *rdata = NULL;
841         isc_buffer_t *dynbuf = NULL;
842         isc_result_t result;
843
844         REQUIRE(msg != NULL);
845         REQUIRE(name != NULL);
846         REQUIRE(tkey != NULL);
847
848         RETERR(dns_message_gettempname(msg, &qname));
849         RETERR(dns_message_gettempname(msg, &aname));
850
851         RETERR(dns_message_gettemprdataset(msg, &question));
852         dns_rdataset_init(question);
853         dns_rdataset_makequestion(question, dns_rdataclass_any,
854                                   dns_rdatatype_tkey);
855
856         RETERR(isc_buffer_allocate(msg->mctx, &dynbuf, 4096));
857         RETERR(dns_message_gettemprdata(msg, &rdata));
858
859         RETERR(dns_rdata_fromstruct(rdata, dns_rdataclass_any,
860                                     dns_rdatatype_tkey, tkey, dynbuf));
861         dns_message_takebuffer(msg, &dynbuf);
862
863         RETERR(dns_message_gettemprdatalist(msg, &tkeylist));
864         tkeylist->rdclass = dns_rdataclass_any;
865         tkeylist->type = dns_rdatatype_tkey;
866         tkeylist->covers = 0;
867         tkeylist->ttl = 0;
868         ISC_LIST_INIT(tkeylist->rdata);
869         ISC_LIST_APPEND(tkeylist->rdata, rdata, link);
870
871         RETERR(dns_message_gettemprdataset(msg, &tkeyset));
872         dns_rdataset_init(tkeyset);
873         RETERR(dns_rdatalist_tordataset(tkeylist, tkeyset));
874
875         dns_name_init(qname, NULL);
876         dns_name_clone(name, qname);
877
878         dns_name_init(aname, NULL);
879         dns_name_clone(name, aname);
880
881         ISC_LIST_APPEND(qname->list, question, link);
882         ISC_LIST_APPEND(aname->list, tkeyset, link);
883
884         dns_message_addname(msg, qname, DNS_SECTION_QUESTION);
885
886         /*
887          * Windows 2000 needs this in the answer section, not the additional
888          * section where the RFC specifies.
889          */
890         if (win2k)
891                 dns_message_addname(msg, aname, DNS_SECTION_ANSWER);
892         else
893                 dns_message_addname(msg, aname, DNS_SECTION_ADDITIONAL);
894
895         return (ISC_R_SUCCESS);
896
897  failure:
898         if (qname != NULL)
899                 dns_message_puttempname(msg, &qname);
900         if (aname != NULL)
901                 dns_message_puttempname(msg, &aname);
902         if (question != NULL) {
903                 dns_rdataset_disassociate(question);
904                 dns_message_puttemprdataset(msg, &question);
905         }
906         if (dynbuf != NULL)
907                 isc_buffer_free(&dynbuf);
908         printf("buildquery error\n");
909         return (result);
910 }
911
912 isc_result_t
913 dns_tkey_builddhquery(dns_message_t *msg, dst_key_t *key, dns_name_t *name,
914                       dns_name_t *algorithm, isc_buffer_t *nonce,
915                       isc_uint32_t lifetime)
916 {
917         dns_rdata_tkey_t tkey;
918         dns_rdata_t *rdata = NULL;
919         isc_buffer_t *dynbuf = NULL;
920         isc_region_t r;
921         dns_name_t keyname;
922         dns_namelist_t namelist;
923         isc_result_t result;
924         isc_stdtime_t now;
925
926         REQUIRE(msg != NULL);
927         REQUIRE(key != NULL);
928         REQUIRE(dst_key_alg(key) == DNS_KEYALG_DH);
929         REQUIRE(dst_key_isprivate(key));
930         REQUIRE(name != NULL);
931         REQUIRE(algorithm != NULL);
932
933         tkey.common.rdclass = dns_rdataclass_any;
934         tkey.common.rdtype = dns_rdatatype_tkey;
935         ISC_LINK_INIT(&tkey.common, link);
936         tkey.mctx = msg->mctx;
937         dns_name_init(&tkey.algorithm, NULL);
938         dns_name_clone(algorithm, &tkey.algorithm);
939         isc_stdtime_get(&now);
940         tkey.inception = now;
941         tkey.expire = now + lifetime;
942         tkey.mode = DNS_TKEYMODE_DIFFIEHELLMAN;
943         if (nonce != NULL)
944                 isc_buffer_usedregion(nonce, &r);
945         else {
946                 r.base = isc_mem_get(msg->mctx, 0);
947                 r.length = 0;
948         }
949         tkey.error = 0;
950         tkey.key = r.base;
951         tkey.keylen =  r.length;
952         tkey.other = NULL;
953         tkey.otherlen = 0;
954
955         RETERR(buildquery(msg, name, &tkey, ISC_FALSE));
956
957         if (nonce == NULL)
958                 isc_mem_put(msg->mctx, r.base, 0);
959
960         RETERR(dns_message_gettemprdata(msg, &rdata));
961         RETERR(isc_buffer_allocate(msg->mctx, &dynbuf, 1024));
962         RETERR(dst_key_todns(key, dynbuf));
963         isc_buffer_usedregion(dynbuf, &r);
964         dns_rdata_fromregion(rdata, dns_rdataclass_any,
965                              dns_rdatatype_key, &r);
966         dns_message_takebuffer(msg, &dynbuf);
967
968         dns_name_init(&keyname, NULL);
969         dns_name_clone(dst_key_name(key), &keyname);
970
971         ISC_LIST_INIT(namelist);
972         RETERR(add_rdata_to_list(msg, &keyname, rdata, 0, &namelist));
973         dns_message_addname(msg, ISC_LIST_HEAD(namelist),
974                             DNS_SECTION_ADDITIONAL);
975
976         return (ISC_R_SUCCESS);
977
978  failure:
979
980         if (dynbuf != NULL)
981                 isc_buffer_free(&dynbuf);
982         return (result);
983 }
984
985 isc_result_t
986 dns_tkey_buildgssquery(dns_message_t *msg, dns_name_t *name, dns_name_t *gname,
987                        isc_buffer_t *intoken, isc_uint32_t lifetime,
988                        gss_ctx_id_t *context, isc_boolean_t win2k)
989 {
990         dns_rdata_tkey_t tkey;
991         isc_result_t result;
992         isc_stdtime_t now;
993         isc_buffer_t token;
994         unsigned char array[4096];
995
996         UNUSED(intoken);
997
998         REQUIRE(msg != NULL);
999         REQUIRE(name != NULL);
1000         REQUIRE(gname != NULL);
1001         REQUIRE(context != NULL);
1002
1003         isc_buffer_init(&token, array, sizeof(array));
1004         result = dst_gssapi_initctx(gname, NULL, &token, context);
1005         if (result != DNS_R_CONTINUE && result != ISC_R_SUCCESS)
1006                 return (result);
1007
1008         tkey.common.rdclass = dns_rdataclass_any;
1009         tkey.common.rdtype = dns_rdatatype_tkey;
1010         ISC_LINK_INIT(&tkey.common, link);
1011         tkey.mctx = NULL;
1012         dns_name_init(&tkey.algorithm, NULL);
1013
1014         if (win2k)
1015                 dns_name_clone(DNS_TSIG_GSSAPIMS_NAME, &tkey.algorithm);
1016         else
1017                 dns_name_clone(DNS_TSIG_GSSAPI_NAME, &tkey.algorithm);
1018
1019         isc_stdtime_get(&now);
1020         tkey.inception = now;
1021         tkey.expire = now + lifetime;
1022         tkey.mode = DNS_TKEYMODE_GSSAPI;
1023         tkey.error = 0;
1024         tkey.key = isc_buffer_base(&token);
1025         tkey.keylen = isc_buffer_usedlength(&token);
1026         tkey.other = NULL;
1027         tkey.otherlen = 0;
1028
1029         RETERR(buildquery(msg, name, &tkey, win2k));
1030
1031         return (ISC_R_SUCCESS);
1032
1033  failure:
1034         return (result);
1035 }
1036
1037 isc_result_t
1038 dns_tkey_builddeletequery(dns_message_t *msg, dns_tsigkey_t *key) {
1039         dns_rdata_tkey_t tkey;
1040
1041         REQUIRE(msg != NULL);
1042         REQUIRE(key != NULL);
1043
1044         tkey.common.rdclass = dns_rdataclass_any;
1045         tkey.common.rdtype = dns_rdatatype_tkey;
1046         ISC_LINK_INIT(&tkey.common, link);
1047         tkey.mctx = msg->mctx;
1048         dns_name_init(&tkey.algorithm, NULL);
1049         dns_name_clone(key->algorithm, &tkey.algorithm);
1050         tkey.inception = tkey.expire = 0;
1051         tkey.mode = DNS_TKEYMODE_DELETE;
1052         tkey.error = 0;
1053         tkey.keylen = tkey.otherlen = 0;
1054         tkey.key = tkey.other = NULL;
1055
1056         return (buildquery(msg, &key->name, &tkey, ISC_FALSE));
1057 }
1058
1059 static isc_result_t
1060 find_tkey(dns_message_t *msg, dns_name_t **name, dns_rdata_t *rdata,
1061           int section)
1062 {
1063         dns_rdataset_t *tkeyset;
1064         isc_result_t result;
1065
1066         result = dns_message_firstname(msg, section);
1067         while (result == ISC_R_SUCCESS) {
1068                 *name = NULL;
1069                 dns_message_currentname(msg, section, name);
1070                 tkeyset = NULL;
1071                 result = dns_message_findtype(*name, dns_rdatatype_tkey, 0,
1072                                               &tkeyset);
1073                 if (result == ISC_R_SUCCESS) {
1074                         result = dns_rdataset_first(tkeyset);
1075                         if (result != ISC_R_SUCCESS)
1076                                 return (result);
1077                         dns_rdataset_current(tkeyset, rdata);
1078                         return (ISC_R_SUCCESS);
1079                 }
1080                 result = dns_message_nextname(msg, section);
1081         }
1082         if (result == ISC_R_NOMORE)
1083                 return (ISC_R_NOTFOUND);
1084         return (result);
1085 }
1086
1087 isc_result_t
1088 dns_tkey_processdhresponse(dns_message_t *qmsg, dns_message_t *rmsg,
1089                            dst_key_t *key, isc_buffer_t *nonce,
1090                            dns_tsigkey_t **outkey, dns_tsig_keyring_t *ring)
1091 {
1092         dns_rdata_t qtkeyrdata = DNS_RDATA_INIT, rtkeyrdata = DNS_RDATA_INIT;
1093         dns_name_t keyname, *tkeyname, *theirkeyname, *ourkeyname, *tempname;
1094         dns_rdataset_t *theirkeyset = NULL, *ourkeyset = NULL;
1095         dns_rdata_t theirkeyrdata = DNS_RDATA_INIT;
1096         dst_key_t *theirkey = NULL;
1097         dns_rdata_tkey_t qtkey, rtkey;
1098         unsigned char secretdata[256];
1099         unsigned int sharedsize;
1100         isc_buffer_t *shared = NULL, secret;
1101         isc_region_t r, r2;
1102         isc_result_t result;
1103         isc_boolean_t freertkey = ISC_FALSE;
1104
1105         REQUIRE(qmsg != NULL);
1106         REQUIRE(rmsg != NULL);
1107         REQUIRE(key != NULL);
1108         REQUIRE(dst_key_alg(key) == DNS_KEYALG_DH);
1109         REQUIRE(dst_key_isprivate(key));
1110         if (outkey != NULL)
1111                 REQUIRE(*outkey == NULL);
1112
1113         if (rmsg->rcode != dns_rcode_noerror)
1114                 return (ISC_RESULTCLASS_DNSRCODE + rmsg->rcode);
1115         RETERR(find_tkey(rmsg, &tkeyname, &rtkeyrdata, DNS_SECTION_ANSWER));
1116         RETERR(dns_rdata_tostruct(&rtkeyrdata, &rtkey, NULL));
1117         freertkey = ISC_TRUE;
1118
1119         RETERR(find_tkey(qmsg, &tempname, &qtkeyrdata,
1120                          DNS_SECTION_ADDITIONAL));
1121         RETERR(dns_rdata_tostruct(&qtkeyrdata, &qtkey, NULL));
1122
1123         if (rtkey.error != dns_rcode_noerror ||
1124             rtkey.mode != DNS_TKEYMODE_DIFFIEHELLMAN ||
1125             rtkey.mode != qtkey.mode ||
1126             !dns_name_equal(&rtkey.algorithm, &qtkey.algorithm) ||
1127             rmsg->rcode != dns_rcode_noerror) {
1128                 tkey_log("dns_tkey_processdhresponse: tkey mode invalid "
1129                          "or error set(1)");
1130                 result = DNS_R_INVALIDTKEY;
1131                 dns_rdata_freestruct(&qtkey);
1132                 goto failure;
1133         }
1134
1135         dns_rdata_freestruct(&qtkey);
1136
1137         dns_name_init(&keyname, NULL);
1138         dns_name_clone(dst_key_name(key), &keyname);
1139
1140         ourkeyname = NULL;
1141         ourkeyset = NULL;
1142         RETERR(dns_message_findname(rmsg, DNS_SECTION_ANSWER, &keyname,
1143                                     dns_rdatatype_key, 0, &ourkeyname,
1144                                     &ourkeyset));
1145
1146         result = dns_message_firstname(rmsg, DNS_SECTION_ANSWER);
1147         while (result == ISC_R_SUCCESS) {
1148                 theirkeyname = NULL;
1149                 dns_message_currentname(rmsg, DNS_SECTION_ANSWER,
1150                                         &theirkeyname);
1151                 if (dns_name_equal(theirkeyname, ourkeyname))
1152                         goto next;
1153                 theirkeyset = NULL;
1154                 result = dns_message_findtype(theirkeyname, dns_rdatatype_key,
1155                                               0, &theirkeyset);
1156                 if (result == ISC_R_SUCCESS) {
1157                         RETERR(dns_rdataset_first(theirkeyset));
1158                         break;
1159                 }
1160  next:
1161                 result = dns_message_nextname(rmsg, DNS_SECTION_ANSWER);
1162         }
1163
1164         if (theirkeyset == NULL) {
1165                 tkey_log("dns_tkey_processdhresponse: failed to find server "
1166                          "key");
1167                 result = ISC_R_NOTFOUND;
1168                 goto failure;
1169         }
1170
1171         dns_rdataset_current(theirkeyset, &theirkeyrdata);
1172         RETERR(dns_dnssec_keyfromrdata(theirkeyname, &theirkeyrdata,
1173                                        rmsg->mctx, &theirkey));
1174
1175         RETERR(dst_key_secretsize(key, &sharedsize));
1176         RETERR(isc_buffer_allocate(rmsg->mctx, &shared, sharedsize));
1177
1178         RETERR(dst_key_computesecret(theirkey, key, shared));
1179
1180         isc_buffer_init(&secret, secretdata, sizeof(secretdata));
1181
1182         r.base = rtkey.key;
1183         r.length = rtkey.keylen;
1184         if (nonce != NULL)
1185                 isc_buffer_usedregion(nonce, &r2);
1186         else {
1187                 r2.base = isc_mem_get(rmsg->mctx, 0);
1188                 r2.length = 0;
1189         }
1190         RETERR(compute_secret(shared, &r2, &r, &secret));
1191         if (nonce == NULL)
1192                 isc_mem_put(rmsg->mctx, r2.base, 0);
1193
1194         isc_buffer_usedregion(&secret, &r);
1195         result = dns_tsigkey_create(tkeyname, &rtkey.algorithm,
1196                                     r.base, r.length, ISC_TRUE,
1197                                     NULL, rtkey.inception, rtkey.expire,
1198                                     rmsg->mctx, ring, outkey);
1199         isc_buffer_free(&shared);
1200         dns_rdata_freestruct(&rtkey);
1201         dst_key_free(&theirkey);
1202         return (result);
1203
1204  failure:
1205         if (shared != NULL)
1206                 isc_buffer_free(&shared);
1207
1208         if (theirkey != NULL)
1209                 dst_key_free(&theirkey);
1210
1211         if (freertkey)
1212                 dns_rdata_freestruct(&rtkey);
1213
1214         return (result);
1215 }
1216
1217 isc_result_t
1218 dns_tkey_processgssresponse(dns_message_t *qmsg, dns_message_t *rmsg,
1219                             dns_name_t *gname, gss_ctx_id_t *context,
1220                             isc_buffer_t *outtoken, dns_tsigkey_t **outkey,
1221                             dns_tsig_keyring_t *ring)
1222 {
1223         dns_rdata_t rtkeyrdata = DNS_RDATA_INIT, qtkeyrdata = DNS_RDATA_INIT;
1224         dns_name_t *tkeyname;
1225         dns_rdata_tkey_t rtkey, qtkey;
1226         dst_key_t *dstkey = NULL;
1227         isc_buffer_t intoken;
1228         isc_result_t result;
1229         unsigned char array[1024];
1230
1231         REQUIRE(outtoken != NULL);
1232         REQUIRE(qmsg != NULL);
1233         REQUIRE(rmsg != NULL);
1234         REQUIRE(gname != NULL);
1235         if (outkey != NULL)
1236                 REQUIRE(*outkey == NULL);
1237
1238         if (rmsg->rcode != dns_rcode_noerror)
1239                 return (ISC_RESULTCLASS_DNSRCODE + rmsg->rcode);
1240         RETERR(find_tkey(rmsg, &tkeyname, &rtkeyrdata, DNS_SECTION_ANSWER));
1241         RETERR(dns_rdata_tostruct(&rtkeyrdata, &rtkey, NULL));
1242
1243         /*
1244          * Win2k puts the item in the ANSWER section, while the RFC
1245          * specifies it should be in the ADDITIONAL section.  Check first
1246          * where it should be, and then where it may be.
1247          */
1248         result = find_tkey(qmsg, &tkeyname, &qtkeyrdata,
1249                            DNS_SECTION_ADDITIONAL);
1250         if (result == ISC_R_NOTFOUND)
1251                 result = find_tkey(qmsg, &tkeyname, &qtkeyrdata,
1252                                    DNS_SECTION_ANSWER);
1253         if (result != ISC_R_SUCCESS)
1254                 goto failure;
1255
1256         RETERR(dns_rdata_tostruct(&qtkeyrdata, &qtkey, NULL));
1257
1258         if (rtkey.error != dns_rcode_noerror ||
1259             rtkey.mode != DNS_TKEYMODE_GSSAPI ||
1260             !dns_name_equal(&rtkey.algorithm, &qtkey.algorithm)) {
1261                 tkey_log("dns_tkey_processgssresponse: tkey mode invalid "
1262                          "or error set(2) %d", rtkey.error);
1263                 _dns_tkey_dumpmessage(qmsg);
1264                 _dns_tkey_dumpmessage(rmsg);
1265                 result = DNS_R_INVALIDTKEY;
1266                 goto failure;
1267         }
1268
1269         isc_buffer_init(outtoken, array, sizeof(array));
1270         isc_buffer_init(&intoken, rtkey.key, rtkey.keylen);
1271         RETERR(dst_gssapi_initctx(gname, &intoken, outtoken, context));
1272
1273         RETERR(dst_key_fromgssapi(dns_rootname, *context, rmsg->mctx,
1274                                   &dstkey));
1275
1276         RETERR(dns_tsigkey_createfromkey(tkeyname, DNS_TSIG_GSSAPI_NAME,
1277                                          dstkey, ISC_FALSE, NULL,
1278                                          rtkey.inception, rtkey.expire,
1279                                          ring->mctx, ring, outkey));
1280         dst_key_free(&dstkey);
1281         dns_rdata_freestruct(&rtkey);
1282         return (result);
1283
1284  failure:
1285         /*
1286          * XXXSRA This probably leaks memory from rtkey and qtkey.
1287          */
1288         if (dstkey != NULL)
1289                 dst_key_free(&dstkey);
1290         return (result);
1291 }
1292
1293 isc_result_t
1294 dns_tkey_processdeleteresponse(dns_message_t *qmsg, dns_message_t *rmsg,
1295                                dns_tsig_keyring_t *ring)
1296 {
1297         dns_rdata_t qtkeyrdata = DNS_RDATA_INIT, rtkeyrdata = DNS_RDATA_INIT;
1298         dns_name_t *tkeyname, *tempname;
1299         dns_rdata_tkey_t qtkey, rtkey;
1300         dns_tsigkey_t *tsigkey = NULL;
1301         isc_result_t result;
1302
1303         REQUIRE(qmsg != NULL);
1304         REQUIRE(rmsg != NULL);
1305
1306         if (rmsg->rcode != dns_rcode_noerror)
1307                 return(ISC_RESULTCLASS_DNSRCODE + rmsg->rcode);
1308
1309         RETERR(find_tkey(rmsg, &tkeyname, &rtkeyrdata, DNS_SECTION_ANSWER));
1310         RETERR(dns_rdata_tostruct(&rtkeyrdata, &rtkey, NULL));
1311
1312         RETERR(find_tkey(qmsg, &tempname, &qtkeyrdata,
1313                          DNS_SECTION_ADDITIONAL));
1314         RETERR(dns_rdata_tostruct(&qtkeyrdata, &qtkey, NULL));
1315
1316         if (rtkey.error != dns_rcode_noerror ||
1317             rtkey.mode != DNS_TKEYMODE_DELETE ||
1318             rtkey.mode != qtkey.mode ||
1319             !dns_name_equal(&rtkey.algorithm, &qtkey.algorithm) ||
1320             rmsg->rcode != dns_rcode_noerror) {
1321                 tkey_log("dns_tkey_processdeleteresponse: tkey mode invalid "
1322                          "or error set(3)");
1323                 result = DNS_R_INVALIDTKEY;
1324                 dns_rdata_freestruct(&qtkey);
1325                 dns_rdata_freestruct(&rtkey);
1326                 goto failure;
1327         }
1328
1329         dns_rdata_freestruct(&qtkey);
1330
1331         RETERR(dns_tsigkey_find(&tsigkey, tkeyname, &rtkey.algorithm, ring));
1332
1333         dns_rdata_freestruct(&rtkey);
1334
1335         /*
1336          * Mark the key as deleted.
1337          */
1338         dns_tsigkey_setdeleted(tsigkey);
1339         /*
1340          * Release the reference.
1341          */
1342         dns_tsigkey_detach(&tsigkey);
1343
1344  failure:
1345         return (result);
1346 }
1347
1348 isc_result_t
1349 dns_tkey_gssnegotiate(dns_message_t *qmsg, dns_message_t *rmsg,
1350                       dns_name_t *server, gss_ctx_id_t *context,
1351                       dns_tsigkey_t **outkey, dns_tsig_keyring_t *ring,
1352                       isc_boolean_t win2k)
1353 {
1354         dns_rdata_t rtkeyrdata = DNS_RDATA_INIT, qtkeyrdata = DNS_RDATA_INIT;
1355         dns_name_t *tkeyname;
1356         dns_rdata_tkey_t rtkey, qtkey;
1357         isc_buffer_t intoken, outtoken;
1358         dst_key_t *dstkey = NULL;
1359         isc_result_t result;
1360         unsigned char array[1024];
1361
1362         REQUIRE(qmsg != NULL);
1363         REQUIRE(rmsg != NULL);
1364         REQUIRE(server != NULL);
1365         if (outkey != NULL)
1366                 REQUIRE(*outkey == NULL);
1367
1368         if (rmsg->rcode != dns_rcode_noerror)
1369                 return (ISC_RESULTCLASS_DNSRCODE + rmsg->rcode);
1370
1371         RETERR(find_tkey(rmsg, &tkeyname, &rtkeyrdata, DNS_SECTION_ANSWER));
1372         RETERR(dns_rdata_tostruct(&rtkeyrdata, &rtkey, NULL));
1373
1374         if (win2k == ISC_TRUE)
1375                 RETERR(find_tkey(qmsg, &tkeyname, &qtkeyrdata,
1376                                  DNS_SECTION_ANSWER));
1377         else
1378                 RETERR(find_tkey(qmsg, &tkeyname, &qtkeyrdata,
1379                                  DNS_SECTION_ADDITIONAL));
1380
1381         RETERR(dns_rdata_tostruct(&qtkeyrdata, &qtkey, NULL));
1382
1383         if (rtkey.error != dns_rcode_noerror ||
1384             rtkey.mode != DNS_TKEYMODE_GSSAPI ||
1385             !dns_name_equal(&rtkey.algorithm, &qtkey.algorithm))
1386         {
1387                 tkey_log("dns_tkey_processdhresponse: tkey mode invalid "
1388                          "or error set(4)");
1389                 result = DNS_R_INVALIDTKEY;
1390                 goto failure;
1391         }
1392
1393         isc_buffer_init(&intoken, rtkey.key, rtkey.keylen);
1394         isc_buffer_init(&outtoken, array, sizeof(array));
1395
1396         result = dst_gssapi_initctx(server, &intoken, &outtoken, context);
1397         if (result != DNS_R_CONTINUE && result != ISC_R_SUCCESS)
1398                 return (result);
1399
1400         RETERR(dst_key_fromgssapi(dns_rootname, *context, rmsg->mctx,
1401                                   &dstkey));
1402
1403         /*
1404          * XXXSRA This seems confused.  If we got CONTINUE from initctx,
1405          * the GSS negotiation hasn't completed yet, so we can't sign
1406          * anything yet.
1407          */
1408
1409         RETERR(dns_tsigkey_createfromkey(tkeyname,
1410                                          (win2k
1411                                           ? DNS_TSIG_GSSAPIMS_NAME
1412                                           : DNS_TSIG_GSSAPI_NAME),
1413                                          dstkey, ISC_TRUE, NULL,
1414                                          rtkey.inception, rtkey.expire,
1415                                          ring->mctx, ring, outkey));
1416         dst_key_free(&dstkey);
1417         dns_rdata_freestruct(&rtkey);
1418         return (result);
1419
1420  failure:
1421         /*
1422          * XXXSRA This probably leaks memory from qtkey.
1423          */
1424         dns_rdata_freestruct(&rtkey);
1425         if (dstkey != NULL)
1426                 dst_key_free(&dstkey);
1427         return (result);
1428 }