2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: tsigconf.c,v 1.21.208.4 2004/03/08 04:04:19 marka Exp $ */
22 #include <isc/base64.h>
23 #include <isc/buffer.h>
25 #include <isc/string.h>
27 #include <isccfg/cfg.h>
30 #include <dns/result.h>
32 #include <named/log.h>
34 #include <named/config.h>
35 #include <named/tsigconf.h>
38 add_initial_keys(cfg_obj_t *list, dns_tsig_keyring_t *ring, isc_mem_t *mctx) {
39 cfg_listelt_t *element;
40 cfg_obj_t *key = NULL;
42 unsigned char *secret = NULL;
48 for (element = cfg_list_first(list);
50 element = cfg_list_next(element))
52 cfg_obj_t *algobj = NULL;
53 cfg_obj_t *secretobj = NULL;
57 char keynamedata[1024];
58 isc_buffer_t keynamesrc, keynamebuf;
60 isc_buffer_t secretbuf;
62 key = cfg_listelt_value(element);
63 keyid = cfg_obj_asstring(cfg_map_getname(key));
67 (void)cfg_map_get(key, "algorithm", &algobj);
68 (void)cfg_map_get(key, "secret", &secretobj);
69 INSIST(algobj != NULL && secretobj != NULL);
72 * Create the key name.
74 dns_name_init(&keyname, NULL);
75 isc_buffer_init(&keynamesrc, keyid, strlen(keyid));
76 isc_buffer_add(&keynamesrc, strlen(keyid));
77 isc_buffer_init(&keynamebuf, keynamedata, sizeof(keynamedata));
78 ret = dns_name_fromtext(&keyname, &keynamesrc, dns_rootname,
79 ISC_TRUE, &keynamebuf);
80 if (ret != ISC_R_SUCCESS)
84 * Create the algorithm.
86 algstr = cfg_obj_asstring(algobj);
87 if (ns_config_getkeyalgorithm(algstr, &alg) != ISC_R_SUCCESS) {
88 cfg_obj_log(algobj, ns_g_lctx, ISC_LOG_ERROR,
89 "key '%s': the only supported algorithm "
90 "is hmac-md5", keyid);
95 secretstr = cfg_obj_asstring(secretobj);
96 secretalloc = secretlen = strlen(secretstr) * 3 / 4;
97 secret = isc_mem_get(mctx, secretlen);
102 isc_buffer_init(&secretbuf, secret, secretlen);
103 ret = isc_base64_decodestring(secretstr, &secretbuf);
104 if (ret != ISC_R_SUCCESS)
106 secretlen = isc_buffer_usedlength(&secretbuf);
108 isc_stdtime_get(&now);
109 ret = dns_tsigkey_create(&keyname, alg, secret, secretlen,
110 ISC_FALSE, NULL, now, now,
112 isc_mem_put(mctx, secret, secretalloc);
114 if (ret != ISC_R_SUCCESS)
118 return (ISC_R_SUCCESS);
121 cfg_obj_log(key, ns_g_lctx, ISC_LOG_ERROR,
122 "configuring key '%s': %s", keyid,
123 isc_result_totext(ret));
126 isc_mem_put(mctx, secret, secretalloc);
132 ns_tsigkeyring_fromconfig(cfg_obj_t *config, cfg_obj_t *vconfig,
133 isc_mem_t *mctx, dns_tsig_keyring_t **ringp)
137 dns_tsig_keyring_t *ring = NULL;
145 maps[i++] = cfg_tuple_get(vconfig, "options");
148 result = dns_tsigkeyring_create(mctx, &ring);
149 if (result != ISC_R_SUCCESS)
156 result = cfg_map_get(maps[i], "key", &keylist);
157 if (result != ISC_R_SUCCESS)
159 result = add_initial_keys(keylist, ring, mctx);
160 if (result != ISC_R_SUCCESS)
165 return (ISC_R_SUCCESS);
168 dns_tsigkeyring_destroy(&ring);