<commit_before>/*
 * Copyright (C) 1999  Internet Software Consortium.
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

/*
 * $Id: tsig.c,v 1.3 1999/08/26 20:41:53 bwelling Exp $
 * Principal Author: <NAME>
 */


#include <config.h>

#include <stdlib.h>

#include <isc/assertions.h>
#include <isc/buffer.h>
#include <isc/error.h>
#include <isc/list.h>
#include <isc/net.h>
#include <isc/result.h>
#include <isc/rwlock.h>
#include <isc/time.h>
#include <isc/types.h>

#include <dns/keyvalues.h>
#include <dns/name.h>
#include <dns/message.h>
#include <dns/rdata.h>
#include <dns/rdatalist.h>
#include <dns/rdataset.h>
#include <dns/rdatastruct.h>
#include <dns/tsig.h>

#include <dst/dst.h>
#include <dst/result.h>

#define TSIG_MAGIC		0x54534947	/* TSIG */
#define VALID_TSIG_KEY(x)	((x) != NULL && (x)->magic == TSIG_MAGIC)

/* XXXBEW If an unsorted list isn't good enough, this can be updated */
static ISC_LIST(dns_tsig_key_t) tsigkeys;
static isc_rwlock_t tsiglock;
static isc_mem_t *tsig_mctx = NULL;

dns_name_t *dns_tsig_hmacmd5_name = NULL;

#define is_response(msg) (msg->flags & DNS_MESSAGEFLAG_QR)

isc_result_t
dns_tsig_key_create(dns_name_t *name, dns_name_t *algorithm,
		    unsigned char *secret, int length,
		    isc_mem_t *mctx, dns_tsig_key_t **key)
{
	isc_buffer_t b, nameb;
	char namestr[1024];
	isc_uint16_t alg;
	dns_tsig_key_t *tkey;
	isc_result_t ret;

	REQUIRE(key != NULL);
	REQUIRE(*key == NULL);
	REQUIRE(name != NULL);
	REQUIRE(algorithm != NULL);
	REQUIRE(length >= 0);
	if (length > 0)
		REQUIRE(secret != NULL);
	REQUIRE(mctx != NULL);

	if (!dns_name_equal(algorithm, DNS_TSIG_HMACMD5_NAME))
		return (ISC_R_NOTFOUND);
	else
		alg = DST_ALG_HMAC_MD5;

	*key = (dns_tsig_key_t *) isc_mem_get(mctx, sizeof(dns_tsig_key_t));
	if (*key == NULL)
		return (ISC_R_NOMEMORY);
	tkey = *key;

	dns_name_init(&tkey->name, NULL);
	ret = dns_name_dup(name, mctx, &tkey->name);
	if (ret != ISC_R_SUCCESS)
		goto cleanup_key;
	dns_name_downcase(&tkey->name);

	dns_name_init(&tkey->algorithm, NULL);
	ret = dns_name_dup(algorithm, mctx, &tkey->algorithm);
	if (ret != ISC_R_SUCCESS)
		goto cleanup_name;
	dns_name_downcase(&tkey->algorithm);

	isc_buffer_init(&nameb, namestr, sizeof(namestr), ISC_BUFFERTYPE_TEXT);
	ret = dns_name_totext(name, ISC_FALSE, &nameb);
	if (ret != ISC_R_SUCCESS)
		goto cleanup_algorithm;

	if (length > 0) {
		isc_buffer_init(&b, secret, length, ISC_BUFFERTYPE_BINARY);
		isc_buffer_add(&b, length);
		ret = dst_key_frombuffer(namestr, alg,
					 NS_KEY_NAME_ENTITY,
					 NS_KEY_PROT_DNSSEC,
					 &b, mctx, &tkey->key);
		if (ret != ISC_R_SUCCESS)
			goto cleanup_algorithm;

		ISC_LINK_INIT(tkey, link);
		isc_rwlock_lock(&tsiglock, isc_rwlocktype_write);
		ISC_LIST_APPEND(tsigkeys, tkey, link);
		isc_rwlock_unlock(&tsiglock, isc_rwlocktype_write);
	}
	else
		tkey->key = NULL;
<commit_msg>dns_name_downcase() API change<commit_after>/*
 * Copyright (C) 1999  Internet Software Consortium.
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

/*
 * $Id: tsig.c,v 1.4 1999/08/26 21:07:56 halley Exp $
 * Principal Author: <NAME>
 */


#include <config.h>

#include <stdlib.h>

#include <isc/assertions.h>
#include <isc/buffer.h>
#include <isc/error.h>
#include <isc/list.h>
#include <isc/net.h>
#include <isc/result.h>
#include <isc/rwlock.h>
#include <isc/time.h>
#include <isc/types.h>

#include <dns/keyvalues.h>
#include <dns/name.h>
#include <dns/message.h>
#include <dns/rdata.h>
#include <dns/rdatalist.h>
#include <dns/rdataset.h>
#include <dns/rdatastruct.h>
#include <dns/tsig.h>

#include <dst/dst.h>
#include <dst/result.h>

#define TSIG_MAGIC		0x54534947	/* TSIG */
#define VALID_TSIG_KEY(x)	((x) != NULL && (x)->magic == TSIG_MAGIC)

/* XXXBEW If an unsorted list isn't good enough, this can be updated */
static ISC_LIST(dns_tsig_key_t) tsigkeys;
static isc_rwlock_t tsiglock;
static isc_mem_t *tsig_mctx = NULL;

dns_name_t *dns_tsig_hmacmd5_name = NULL;

#define is_response(msg) (msg->flags & DNS_MESSAGEFLAG_QR)

isc_result_t
dns_tsig_key_create(dns_name_t *name, dns_name_t *algorithm,
		    unsigned char *secret, int length,
		    isc_mem_t *mctx, dns_tsig_key_t **key)
{
	isc_buffer_t b, nameb;
	char namestr[1024];
	isc_uint16_t alg;
	dns_tsig_key_t *tkey;
	isc_result_t ret;

	REQUIRE(key != NULL);
	REQUIRE(*key == NULL);
	REQUIRE(name != NULL);
	REQUIRE(algorithm != NULL);
	REQUIRE(length >= 0);
	if (length > 0)
		REQUIRE(secret != NULL);
	REQUIRE(mctx != NULL);

	if (!dns_name_equal(algorithm, DNS_TSIG_HMACMD5_NAME))
		return (ISC_R_NOTFOUND);
	else
		alg = DST_ALG_HMAC_MD5;

	*key = (dns_tsig_key_t *) isc_mem_get(mctx, sizeof(dns_tsig_key_t));
	if (*key == NULL)
		return (ISC_R_NOMEMORY);
	tkey = *key;

	dns_name_init(&tkey->name, NULL);
	ret = dns_name_dup(name, mctx, &tkey->name);
	if (ret != ISC_R_SUCCESS)
		goto cleanup_key;
	dns_name_downcase(&tkey->name, &tkey->name, NULL);

	dns_name_init(&tkey->algorithm, NULL);
	ret = dns_name_dup(algorithm, mctx, &tkey->algorithm);
	if (ret != ISC_R_SUCCESS)
		goto cleanup_name;
	dns_name_downcase(&tkey->algorithm, &tkey->algorithm, NULL);

	isc_buffer_init(&nameb, namestr, sizeof(namestr), ISC_BUFFERTYPE_TEXT);
	ret = dns_name_totext(name, ISC_FALSE, &nameb);
	if (ret != ISC_R_SUCCESS)
		goto cleanup_algorithm;

	if (length > 0) {
		isc_buffer_init(&b, secret, length, ISC_BUFFERTYPE_BINARY);
		isc_buffer_add(&b, length);
		ret = dst_key_frombuffer(namestr, alg,
					 NS_KEY_NAME_ENTITY,
					 NS_KEY_PROT_DNSSEC,
					 &b, mctx, &tkey->key);
		if (ret != ISC_R_SUCCESS)
			goto cleanup_algorithm;

		ISC_LINK_INIT(tkey, link);
		isc_rwlock_lock(&tsiglock, isc_rwlocktype_write);
		ISC_LIST_APPEND(tsigkeys, tkey, link);
		isc_rwlock_unlock(&tsiglock, isc_rwlocktype_write);
	}
	else
		tkey->key = NULL;

{'fraction_non_alphanumeric': 0.12305555555555556, 'fraction_numerical': 0.01138888888888889, 'mean_word_length': 4.926748971193415, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2431271', 'n_tokens_mistral': 3013, 'n_tokens_neox': 2818, 'n_words': 692}