⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tsig.c

📁 非常好的dns解析软件
💻 C
📖 第 1 页 / 共 3 页
字号:
	REQUIRE(targetp != NULL && *targetp == NULL);	isc_refcount_increment(&source->refs, NULL);	*targetp = source;}static voidtsigkey_free(dns_tsigkey_t *key) {	REQUIRE(VALID_TSIG_KEY(key));	key->magic = 0;	dns_name_free(&key->name, key->mctx);	if (algname_is_allocated(key->algorithm)) {		dns_name_free(key->algorithm, key->mctx);		isc_mem_put(key->mctx, key->algorithm, sizeof(dns_name_t));	}	if (key->key != NULL)		dst_key_free(&key->key);	if (key->creator != NULL) {		dns_name_free(key->creator, key->mctx);		isc_mem_put(key->mctx, key->creator, sizeof(dns_name_t));	}	isc_refcount_destroy(&key->refs);	isc_mem_put(key->mctx, key, sizeof(dns_tsigkey_t));}voiddns_tsigkey_detach(dns_tsigkey_t **keyp) {	dns_tsigkey_t *key;	unsigned int refs;	REQUIRE(keyp != NULL);	REQUIRE(VALID_TSIG_KEY(*keyp));	key = *keyp;	isc_refcount_decrement(&key->refs, &refs);	if (refs == 0)		tsigkey_free(key);	*keyp = NULL;}voiddns_tsigkey_setdeleted(dns_tsigkey_t *key) {	REQUIRE(VALID_TSIG_KEY(key));	REQUIRE(key->ring != NULL);	RWLOCK(&key->ring->lock, isc_rwlocktype_write);	(void)dns_rbt_deletename(key->ring->keys, &key->name, ISC_FALSE);	RWUNLOCK(&key->ring->lock, isc_rwlocktype_write);}static voidbuffer_putuint48(isc_buffer_t *b, isc_uint64_t val) {	isc_uint16_t valhi;	isc_uint32_t vallo;	valhi = (isc_uint16_t)(val >> 32);	vallo = (isc_uint32_t)(val & 0xFFFFFFFF);	isc_buffer_putuint16(b, valhi);	isc_buffer_putuint32(b, vallo);}isc_result_tdns_tsig_sign(dns_message_t *msg) {	dns_tsigkey_t *key;	dns_rdata_any_tsig_t tsig, querytsig;	unsigned char data[128];	isc_buffer_t databuf, sigbuf;	isc_buffer_t *dynbuf;	dns_name_t *owner;	dns_rdata_t *rdata = NULL;	dns_rdatalist_t *datalist;	dns_rdataset_t *dataset;	isc_region_t r;	isc_stdtime_t now;	isc_mem_t *mctx;	dst_context_t *ctx = NULL;	isc_result_t ret;	unsigned char badtimedata[BADTIMELEN];	unsigned int sigsize = 0;	REQUIRE(msg != NULL);	REQUIRE(VALID_TSIG_KEY(dns_message_gettsigkey(msg)));	/*	 * If this is a response, there should be a query tsig.	 */	if (is_response(msg) && msg->querytsig == NULL)		return (DNS_R_EXPECTEDTSIG);	dynbuf = NULL;	mctx = msg->mctx;	key = dns_message_gettsigkey(msg);	tsig.mctx = mctx;	tsig.common.rdclass = dns_rdataclass_any;	tsig.common.rdtype = dns_rdatatype_tsig;	ISC_LINK_INIT(&tsig.common, link);	dns_name_init(&tsig.algorithm, NULL);	dns_name_clone(key->algorithm, &tsig.algorithm);	isc_stdtime_get(&now);	tsig.timesigned = now + msg->timeadjust;	tsig.fudge = DNS_TSIG_FUDGE;	tsig.originalid = msg->id;	isc_buffer_init(&databuf, data, sizeof(data));	if (is_response(msg))		tsig.error = msg->querytsigstatus;	else		tsig.error = dns_rcode_noerror;	if (tsig.error != dns_tsigerror_badtime) {		tsig.otherlen = 0;		tsig.other = NULL;	} else {		isc_buffer_t otherbuf;		tsig.otherlen = BADTIMELEN;		tsig.other = badtimedata;		isc_buffer_init(&otherbuf, tsig.other, tsig.otherlen);		buffer_putuint48(&otherbuf, tsig.timesigned);	}	if (key->key != NULL && tsig.error != dns_tsigerror_badsig) {		unsigned char header[DNS_MESSAGE_HEADERLEN];		isc_buffer_t headerbuf;		isc_uint16_t digestbits;		ret = dst_context_create(key->key, mctx, &ctx);		if (ret != ISC_R_SUCCESS)			return (ret);		/*		 * If this is a response, digest the query signature.		 */		if (is_response(msg)) {			dns_rdata_t querytsigrdata = DNS_RDATA_INIT;			ret = dns_rdataset_first(msg->querytsig);			if (ret != ISC_R_SUCCESS)				goto cleanup_context;			dns_rdataset_current(msg->querytsig, &querytsigrdata);			ret = dns_rdata_tostruct(&querytsigrdata, &querytsig,						 NULL);			if (ret != ISC_R_SUCCESS)				goto cleanup_context;			isc_buffer_putuint16(&databuf, querytsig.siglen);			if (isc_buffer_availablelength(&databuf) <			    querytsig.siglen)			{				ret = ISC_R_NOSPACE;				goto cleanup_context;			}			isc_buffer_putmem(&databuf, querytsig.signature,					  querytsig.siglen);			isc_buffer_usedregion(&databuf, &r);			ret = dst_context_adddata(ctx, &r);			if (ret != ISC_R_SUCCESS)				goto cleanup_context;		}		/*		 * Digest the header.		 */		isc_buffer_init(&headerbuf, header, sizeof(header));		dns_message_renderheader(msg, &headerbuf);		isc_buffer_usedregion(&headerbuf, &r);		ret = dst_context_adddata(ctx, &r);		if (ret != ISC_R_SUCCESS)			goto cleanup_context;		/*		 * Digest the remainder of the message.		 */		isc_buffer_usedregion(msg->buffer, &r);		isc_region_consume(&r, DNS_MESSAGE_HEADERLEN);		ret = dst_context_adddata(ctx, &r);		if (ret != ISC_R_SUCCESS)			goto cleanup_context;		if (msg->tcp_continuation == 0) {			/*			 * Digest the name, class, ttl, alg.			 */			dns_name_toregion(&key->name, &r);			ret = dst_context_adddata(ctx, &r);			if (ret != ISC_R_SUCCESS)				goto cleanup_context;			isc_buffer_clear(&databuf);			isc_buffer_putuint16(&databuf, dns_rdataclass_any);			isc_buffer_putuint32(&databuf, 0); /* ttl */			isc_buffer_usedregion(&databuf, &r);			ret = dst_context_adddata(ctx, &r);			if (ret != ISC_R_SUCCESS)				goto cleanup_context;			dns_name_toregion(&tsig.algorithm, &r);			ret = dst_context_adddata(ctx, &r);			if (ret != ISC_R_SUCCESS)				goto cleanup_context;		}		/* Digest the timesigned and fudge */		isc_buffer_clear(&databuf);		if (tsig.error == dns_tsigerror_badtime)			tsig.timesigned = querytsig.timesigned;		buffer_putuint48(&databuf, tsig.timesigned);		isc_buffer_putuint16(&databuf, tsig.fudge);		isc_buffer_usedregion(&databuf, &r);		ret = dst_context_adddata(ctx, &r);		if (ret != ISC_R_SUCCESS)			goto cleanup_context;		if (msg->tcp_continuation == 0) {			/*			 * Digest the error and other data length.			 */			isc_buffer_clear(&databuf);			isc_buffer_putuint16(&databuf, tsig.error);			isc_buffer_putuint16(&databuf, tsig.otherlen);			isc_buffer_usedregion(&databuf, &r);			ret = dst_context_adddata(ctx, &r);			if (ret != ISC_R_SUCCESS)				goto cleanup_context;			/*			 * Digest the error and other data.			 */			if (tsig.otherlen > 0) {				r.length = tsig.otherlen;				r.base = tsig.other;				ret = dst_context_adddata(ctx, &r);				if (ret != ISC_R_SUCCESS)					goto cleanup_context;			}		}		ret = dst_key_sigsize(key->key, &sigsize);		if (ret != ISC_R_SUCCESS)			goto cleanup_context;		tsig.signature = (unsigned char *) isc_mem_get(mctx, sigsize);		if (tsig.signature == NULL) {			ret = ISC_R_NOMEMORY;			goto cleanup_context;		}		isc_buffer_init(&sigbuf, tsig.signature, sigsize);		ret = dst_context_sign(ctx, &sigbuf);		if (ret != ISC_R_SUCCESS)			goto cleanup_signature;		dst_context_destroy(&ctx);		digestbits = dst_key_getbits(key->key);		if (digestbits != 0) {			unsigned int bytes = (digestbits + 1) / 8;			if (is_response(msg) && bytes < querytsig.siglen)				bytes = querytsig.siglen;			if (bytes > isc_buffer_usedlength(&sigbuf))				bytes = isc_buffer_usedlength(&sigbuf);			tsig.siglen = bytes;		} else			tsig.siglen = isc_buffer_usedlength(&sigbuf);	} else {		tsig.siglen = 0;		tsig.signature = NULL;	}	ret = dns_message_gettemprdata(msg, &rdata);	if (ret != ISC_R_SUCCESS)		goto cleanup_signature;	ret = isc_buffer_allocate(msg->mctx, &dynbuf, 512);	if (ret != ISC_R_SUCCESS)		goto cleanup_rdata;	ret = dns_rdata_fromstruct(rdata, dns_rdataclass_any,				   dns_rdatatype_tsig, &tsig, dynbuf);	if (ret != ISC_R_SUCCESS)		goto cleanup_dynbuf;	dns_message_takebuffer(msg, &dynbuf);	if (tsig.signature != NULL) {		isc_mem_put(mctx, tsig.signature, sigsize);		tsig.signature = NULL;	}	owner = NULL;	ret = dns_message_gettempname(msg, &owner);	if (ret != ISC_R_SUCCESS)		goto cleanup_rdata;	dns_name_init(owner, NULL);	ret = dns_name_dup(&key->name, msg->mctx, owner);	if (ret != ISC_R_SUCCESS)		goto cleanup_owner;	datalist = NULL;	ret = dns_message_gettemprdatalist(msg, &datalist);	if (ret != ISC_R_SUCCESS)		goto cleanup_owner;	dataset = NULL;	ret = dns_message_gettemprdataset(msg, &dataset);	if (ret != ISC_R_SUCCESS)		goto cleanup_rdatalist;	datalist->rdclass = dns_rdataclass_any;	datalist->type = dns_rdatatype_tsig;	datalist->covers = 0;	datalist->ttl = 0;	ISC_LIST_INIT(datalist->rdata);	ISC_LIST_APPEND(datalist->rdata, rdata, link);	dns_rdataset_init(dataset);	RUNTIME_CHECK(dns_rdatalist_tordataset(datalist, dataset)		      == ISC_R_SUCCESS);	msg->tsig = dataset;	msg->tsigname = owner;	return (ISC_R_SUCCESS); cleanup_rdatalist:	dns_message_puttemprdatalist(msg, &datalist); cleanup_owner:	dns_message_puttempname(msg, &owner);	goto cleanup_rdata; cleanup_dynbuf:	isc_buffer_free(&dynbuf); cleanup_rdata:	dns_message_puttemprdata(msg, &rdata); cleanup_signature:	if (tsig.signature != NULL)		isc_mem_put(mctx, tsig.signature, sigsize); cleanup_context:	if (ctx != NULL)		dst_context_destroy(&ctx);	return (ret);}isc_result_tdns_tsig_verify(isc_buffer_t *source, dns_message_t *msg,		dns_tsig_keyring_t *ring1, dns_tsig_keyring_t *ring2){	dns_rdata_any_tsig_t tsig, querytsig;	isc_region_t r, source_r, header_r, sig_r;	isc_buffer_t databuf;	unsigned char data[32];	dns_name_t *keyname;	dns_rdata_t rdata = DNS_RDATA_INIT;	isc_stdtime_t now;	isc_result_t ret;	dns_tsigkey_t *tsigkey;	dst_key_t *key = NULL;	unsigned char header[DNS_MESSAGE_HEADERLEN];	dst_context_t *ctx = NULL;	isc_mem_t *mctx;	isc_uint16_t addcount, id;	unsigned int siglen;	unsigned int alg;	REQUIRE(source != NULL);	REQUIRE(DNS_MESSAGE_VALID(msg));	tsigkey = dns_message_gettsigkey(msg);	REQUIRE(tsigkey == NULL || VALID_TSIG_KEY(tsigkey));	msg->verify_attempted = 1;	if (msg->tcp_continuation) {		if (tsigkey == NULL || msg->querytsig == NULL)			return (DNS_R_UNEXPECTEDTSIG);		return (tsig_verify_tcp(source, msg));	}	/*	 * There should be a TSIG record...	 */	if (msg->tsig == NULL)		return (DNS_R_EXPECTEDTSIG);	/*	 * If this is a response and there's no key or query TSIG, there	 * shouldn't be one on the response.	 */	if (is_response(msg) &&	    (tsigkey == NULL || msg->querytsig == NULL))		return (DNS_R_UNEXPECTEDTSIG);	mctx = msg->mctx;	/*	 * If we're here, we know the message is well formed and contains a	 * TSIG record.	 */	keyname = msg->tsigname;	ret = dns_rdataset_first(msg->tsig);	if (ret != ISC_R_SUCCESS)		return (ret);	dns_rdataset_current(msg->tsig, &rdata);	ret = dns_rdata_tostruct(&rdata, &tsig, NULL);	if (ret != ISC_R_SUCCESS)		return (ret);	dns_rdata_reset(&rdata);	if (is_response(msg)) {		ret = dns_rdataset_first(msg->querytsig);		if (ret != ISC_R_SUCCESS)			return (ret);		dns_rdataset_current(msg->querytsig, &rdata);		ret = dns_rdata_tostruct(&rdata, &querytsig, NULL);		if (ret != ISC_R_SUCCESS)			return (ret);	}	/*	 * Do the key name and algorithm match that of the query?	 */	if (is_response(msg) &&	    (!dns_name_equal(keyname, &tsigkey->name) ||	     !dns_name_equal(&tsig.algorithm, &querytsig.algorithm)))	{		msg->tsigstatus = dns_tsigerror_badkey;		tsig_log(msg->tsigkey, 2,			 "key name and algorithm do not match");		return (DNS_R_TSIGVERIFYFAILURE);	}	/*	 * Get the current time.	 */	isc_stdtime_get(&now);	/*	 * Find dns_tsigkey_t based on keyname.	 */	if (tsigkey == NULL) {		ret = ISC_R_NOTFOUND;		if (ring1 != NULL)			ret = dns_tsigkey_find(&tsigkey, keyname,					       &tsig.algorithm, ring1);		if (ret == ISC_R_NOTFOUND && ring2 != NULL)			ret = dns_tsigkey_find(&tsigkey, keyname,					       &tsig.algorithm, ring2);		if (ret != ISC_R_SUCCESS) {			msg->tsigstatus = dns_tsigerror_badkey;			ret = dns_tsigkey_create(keyname, &tsig.algorithm,						 NULL, 0, ISC_FALSE, NULL,						 now, now,						 mctx, NULL, &msg->tsigkey);			if (ret != ISC_R_SUCCESS)				return (ret);			tsig_log(msg->tsigkey, 2, "unknown key");			return (DNS_R_TSIGVERIFYFAILURE);		}		msg->tsigkey = tsigkey;	}	key = tsigkey->key;	/*	 * Is the time ok?	 */	if (now + msg->timeadjust > tsig.timesigned + tsig.fudge) {		msg->tsigstatus = dns_tsigerror_badtime;		tsig_log(msg->tsigkey, 2, "signature has expired");		return (DNS_R_CLOCKSKEW);	} else if (now + msg->timeadjust < tsig.timesigned - tsig.fudge) {		msg->tsigstatus = dns_tsigerror_badtime;		tsig_log(msg->tsigkey, 2, "signature is in the future");		return (DNS_R_CLOCKSKEW);	}	/*	 * Check digest length.	 */	alg = dst_key_alg(key);	ret = dst_key_sigsize(key, &siglen);	if (ret != ISC_R_SUCCESS)		return (ret);	if (alg == DST_ALG_HMACMD5 || alg == DST_ALG_HMACSHA1 ||	    alg == DST_ALG_HMACSHA224 || alg == DST_ALG_HMACSHA256 ||	    alg == DST_ALG_HMACSHA384 || alg == DST_ALG_HMACSHA512) {		isc_uint16_t digestbits = dst_key_getbits(key);		if (tsig.siglen > siglen) {			tsig_log(msg->tsigkey, 2, "signature length to big");			return (DNS_R_FORMERR);		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -