tsigconf.c revision 1a69a1a78cfaa86f3b68bbc965232b7876d4da2a
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister/*
04b5785fde2948599bf259d2ca3235a3d9f55172Tinderbox User * Copyright (C) 1999, 2000 Internet Software Consortium.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * copyright notice and this permission notice appear in all copies.
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * SOFTWARE.
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
d7201de09b85929a86b157f4b2d91667c68c6b52Automatic Updater#include <config.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/base64.h>
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence#include <isc/buffer.h>
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#include <isc/lex.h>
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#include <isc/mem.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/string.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
364a82f7c25b62967678027043425201a5e5171aBob Halley#include <dns/tsig.h>
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉#include <dns/tsigconf.h>
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
b3e77535185043f089b346166440402d092030c3David Lawrencestatic isc_result_t
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellingtonadd_initial_keys(dns_c_kdeflist_t *list, dns_tsig_keyring_t *ring,
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_mem_t *mctx)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister{
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_lex_t *lex = NULL;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein dns_c_kdef_t *key;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister unsigned char *secret = NULL;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister int secretalloc = 0;
a920fb9dc2ff16f32dd73e53469d0febcdcc6c11Mark Andrews int secretlen = 0;
a920fb9dc2ff16f32dd73e53469d0febcdcc6c11Mark Andrews isc_result_t ret;
a920fb9dc2ff16f32dd73e53469d0febcdcc6c11Mark Andrews isc_stdtime_t now;
a920fb9dc2ff16f32dd73e53469d0febcdcc6c11Mark Andrews
a920fb9dc2ff16f32dd73e53469d0febcdcc6c11Mark Andrews key = ISC_LIST_HEAD(list->keydefs);
a920fb9dc2ff16f32dd73e53469d0febcdcc6c11Mark Andrews while (key != NULL) {
a920fb9dc2ff16f32dd73e53469d0febcdcc6c11Mark Andrews dns_name_t keyname;
a920fb9dc2ff16f32dd73e53469d0febcdcc6c11Mark Andrews dns_name_t alg;
a920fb9dc2ff16f32dd73e53469d0febcdcc6c11Mark Andrews char keynamedata[1024], algdata[1024];
a920fb9dc2ff16f32dd73e53469d0febcdcc6c11Mark Andrews isc_buffer_t keynamesrc, keynamebuf, algsrc, algbuf;
a920fb9dc2ff16f32dd73e53469d0febcdcc6c11Mark Andrews isc_buffer_t secretsrc, secretbuf;
a920fb9dc2ff16f32dd73e53469d0febcdcc6c11Mark Andrews
a920fb9dc2ff16f32dd73e53469d0febcdcc6c11Mark Andrews dns_name_init(&keyname, NULL);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister dns_name_init(&alg, NULL);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson /*
b3e77535185043f089b346166440402d092030c3David Lawrence * Create the key name.
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson */
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson isc_buffer_init(&keynamesrc, key->keyid, strlen(key->keyid));
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_buffer_add(&keynamesrc, strlen(key->keyid));
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_buffer_init(&keynamebuf, keynamedata, sizeof(keynamedata));
b3e77535185043f089b346166440402d092030c3David Lawrence ret = dns_name_fromtext(&keyname, &keynamesrc, dns_rootname,
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister ISC_TRUE, &keynamebuf);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (ret != ISC_R_SUCCESS)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister goto failure;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson /*
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson * Create the algorithm.
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister */
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (strcasecmp(key->algorithm, "hmac-md5") == 0)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister alg = *dns_tsig_hmacmd5_name;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister else {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_buffer_init(&algsrc, key->algorithm,
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister strlen(key->algorithm));
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence isc_buffer_add(&algsrc, strlen(key->algorithm));
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_buffer_init(&algbuf, algdata, sizeof(algdata));
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence ret = dns_name_fromtext(&alg, &algsrc, dns_rootname,
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister ISC_TRUE, &algbuf);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister if (ret != ISC_R_SUCCESS)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister goto failure;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister }
b3e77535185043f089b346166440402d092030c3David Lawrence
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister if (strlen(key->secret) % 4 != 0) {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister ret = ISC_R_BADBASE64;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister goto failure;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister }
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister secretalloc = secretlen = strlen(key->secret) * 3 / 4;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence secret = isc_mem_get(mctx, secretlen);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister if (secret == NULL) {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister ret = ISC_R_NOMEMORY;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister goto failure;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister }
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_buffer_init(&secretsrc, key->secret, strlen(key->secret));
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_buffer_add(&secretsrc, strlen(key->secret));
b3e77535185043f089b346166440402d092030c3David Lawrence isc_buffer_init(&secretbuf, secret, secretlen);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister ret = isc_lex_create(mctx, strlen(key->secret), &lex);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (ret != ISC_R_SUCCESS)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister goto failure;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister ret = isc_lex_openbuffer(lex, &secretsrc);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister if (ret != ISC_R_SUCCESS)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister goto failure;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister ret = isc_base64_tobuffer(lex, &secretbuf, -1);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister if (ret != ISC_R_SUCCESS)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister goto failure;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister secretlen = isc_buffer_usedlength(&secretbuf);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_lex_close(lex);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_lex_destroy(&lex);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson isc_stdtime_get(&now);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson ret = dns_tsigkey_create(&keyname, &alg, secret, secretlen,
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister ISC_FALSE, NULL, now, now,
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister mctx, ring, NULL);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson isc_mem_put(mctx, secret, secretalloc);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson secret = NULL;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister if (ret != ISC_R_SUCCESS)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister goto failure;
b3e77535185043f089b346166440402d092030c3David Lawrence key = ISC_LIST_NEXT(key, next);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister }
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister return (ISC_R_SUCCESS);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister failure:
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister if (lex != NULL)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_lex_destroy(&lex);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (secret != NULL)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_mem_put(mctx, secret, secretlen);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister return (ret);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister}
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerisc_result_t
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerdns_tsigkeyring_fromconfig(dns_c_ctx_t *confctx, isc_mem_t *mctx,
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_tsig_keyring_t **ringp)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister{
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson dns_c_kdeflist_t *keylist = NULL;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_tsig_keyring_t *ring = NULL;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_result_t result;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister result = dns_tsigkeyring_create(mctx, &ring);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister if (result != ISC_R_SUCCESS)
b3e77535185043f089b346166440402d092030c3David Lawrence return (result);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister result = dns_c_ctx_getkdeflist(confctx, &keylist);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister if (result == ISC_R_SUCCESS)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence result = add_initial_keys(keylist, ring, mctx);
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews else if (result == ISC_R_NOTFOUND)
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews result = ISC_R_SUCCESS;
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews if (result != ISC_R_SUCCESS)
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews goto failure;
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews *ringp = ring;
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews return (ISC_R_SUCCESS);
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews failure:
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews dns_tsigkeyring_destroy(&ring);
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews return (result);
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews}
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews