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

📄 dst_api.c

📁 bind 9.3结合mysql数据库
💻 C
📖 第 1 页 / 共 2 页
字号:
			break;	ungetc(c, fp);		/* return the charcter to the input field */	/* Handle hex!! FIXME.  */	if (fscanf(fp, "%d %d %d", &flags, &proto, &alg) != 3) {		EREPORT(("dst_read_public_key(): Can not read flag/proto/alg field from %s\n"			 ,name));		return (NULL);	}	/* read in the key string */	fgets(enckey, sizeof(enckey), fp);	/* If we aren't at end-of-file, something is wrong.  */	while ((c = getc(fp)) != EOF)		if (!isspace(c))			break;	if (!feof(fp)) {		EREPORT(("Key too long in file: %s", name));		return NULL;	}	fclose(fp);	if ((len = strlen(enckey)) <= 0)		return (NULL);	/* discard \n */	enckey[--len] = '\0';	/* remove leading spaces */	for (notspace = (char *) enckey; isspace((*notspace)&0xff); len--)		notspace++;	dlen = b64_pton(notspace, deckey, sizeof(deckey));	if (dlen < 0) {		EREPORT(("dst_read_public_key: bad return from b64_pton = %d",			 dlen));		return (NULL);	}	/* store key and info in a key structure that is returned *//*	return dst_store_public_key(in_name, alg, proto, 666, flags, deckey,				    dlen);*/	return dst_buffer_to_key(in_name, alg, flags, proto, deckey, dlen);}/* *  dst_write_public_key *	Write a key to disk in DNS format. *  Parameters *	key     Pointer to a DST key structure. *  Returns *	0       Failure *	1       Success */static intdst_s_write_public_key(const DST_KEY *key){	FILE *fp;	char filename[PATH_MAX];	u_char out_key[RAW_KEY_SIZE];	char enc_key[RAW_KEY_SIZE];	int len = 0;	int mode;	memset(out_key, 0, sizeof(out_key));	if (key == NULL) {		EREPORT(("dst_write_public_key(): No key specified \n"));		return (0);	} else if ((len = dst_key_to_dnskey(key, out_key, sizeof(out_key)))< 0)		return (0);	/* Make the filename */	if (dst_s_build_filename(filename, key->dk_key_name, key->dk_id,				 key->dk_alg, PUBLIC_KEY, PATH_MAX) == -1) {		EREPORT(("dst_write_public_key(): Cannot make filename from %s, %d, and %s\n",			 key->dk_key_name, key->dk_id, PUBLIC_KEY));		return (0);	}	/* XXX in general this should be a check for symmetric keys */	mode = (key->dk_alg == KEY_HMAC_MD5) ? 0600 : 0644;	/* create public key file */	if ((fp = dst_s_fopen(filename, "w+", mode)) == NULL) {		EREPORT(("DST_write_public_key: open of file:%s failed (errno=%d)\n",			 filename, errno));		return (0);	}	/*write out key first base64 the key data */	if (key->dk_flags & DST_EXTEND_FLAG)		b64_ntop(&out_key[6], len - 6, enc_key, sizeof(enc_key));	else		b64_ntop(&out_key[4], len - 4, enc_key, sizeof(enc_key));	fprintf(fp, "%s IN KEY %d %d %d %s\n",		key->dk_key_name,		key->dk_flags, key->dk_proto, key->dk_alg, enc_key);	fclose(fp);	return (1);}/* *  dst_dnskey_to_public_key *	This function converts the contents of a DNS KEY RR into a DST *	key structure. *  Paramters *	len	 Length of the RDATA of the KEY RR RDATA *	rdata	 A pointer to the the KEY RR RDATA. *	in_name     Key name to be stored in key structure. *  Returns *	NULL	    Failure *	NON-NULL	Success.  Pointer to key structure. *			Caller's responsibility to free() it. */DST_KEY *dst_dnskey_to_key(const char *in_name, const u_char *rdata, const int len){	DST_KEY *key_st;	int alg ;	int start = DST_KEY_START;	if (rdata == NULL || len <= DST_KEY_ALG) /* no data */		return (NULL);	alg = (u_int8_t) rdata[DST_KEY_ALG];	if (!dst_check_algorithm(alg)) { /* make sure alg is available */		EREPORT(("dst_dnskey_to_key(): Algorithm %d not suppored\n",			 alg));		return (NULL);	}	if ((key_st = dst_s_get_key_struct(in_name, alg, 0, 0, 0)) == NULL)		return (NULL);	if (in_name == NULL)		return (NULL);	key_st->dk_id = dst_s_dns_key_id(rdata, len);	key_st->dk_flags = dst_s_get_int16(rdata);	key_st->dk_proto = (u_int16_t) rdata[DST_KEY_PROT];	if (key_st->dk_flags & DST_EXTEND_FLAG) {		u_int32_t ext_flags;		ext_flags = (u_int32_t) dst_s_get_int16(&rdata[DST_EXT_FLAG]);		key_st->dk_flags = key_st->dk_flags | (ext_flags << 16);		start += 2;	}	/*	 * now point to the begining of the data representing the encoding	 * of the key	 */	if (key_st->dk_func && key_st->dk_func->from_dns_key) {		if (key_st->dk_func->from_dns_key(key_st, &rdata[start],						  len - start) > 0)			return (key_st);	} else		EREPORT(("dst_dnskey_to_public_key(): unsuppored alg %d\n",			 alg));	SAFE_FREE(key_st);	return (key_st);}/* *  dst_public_key_to_dnskey *	Function to encode a public key into DNS KEY wire format  *  Parameters *	key	     Key structure to encode. *	out_storage     Location to write the encoded key to. *	out_len	 Size of the output array. *  Returns *	<0      Failure *	>=0     Number of bytes written to out_storage */intdst_key_to_dnskey(const DST_KEY *key, u_char *out_storage,			 const int out_len){	u_int16_t val;	int loc = 0;	int enc_len = 0;	if (key == NULL)		return (-1);	if (!dst_check_algorithm(key->dk_alg)) { /* make sure alg is available */		EREPORT(("dst_key_to_dnskey(): Algorithm %d not suppored\n",			 key->dk_alg));		return (UNSUPPORTED_KEYALG);	}	memset(out_storage, 0, out_len);	val = (u_int16_t)(key->dk_flags & 0xffff);	dst_s_put_int16(out_storage, val);	loc += 2;	out_storage[loc++] = (u_char) key->dk_proto;	out_storage[loc++] = (u_char) key->dk_alg;	if (key->dk_flags > 0xffff) {	/* Extended flags */		val = (u_int16_t)((key->dk_flags >> 16) & 0xffff);		dst_s_put_int16(&out_storage[loc], val);		loc += 2;	}	if (key->dk_KEY_struct == NULL)		return (loc);	if (key->dk_func && key->dk_func->to_dns_key) {		enc_len = key->dk_func->to_dns_key(key,						 (u_char *) &out_storage[loc],						   out_len - loc);		if (enc_len > 0)			return (enc_len + loc);		else			return (-1);	} else		EREPORT(("dst_key_to_dnskey(): Unsupported ALG %d\n",			 key->dk_alg));	return (-1);}/* *  dst_buffer_to_key *	Function to encode a string of raw data into a DST key *  Parameters *	alg		The algorithm (HMAC only) *	key		A pointer to the data *	keylen		The length of the data *  Returns *	NULL	    an error occurred *	NON-NULL	the DST key */DST_KEY *dst_buffer_to_key(const char *key_name,		/* name of the key */		  const int alg,		/* algorithm */		  const int flags,		/* dns flags */		  const int protocol,		/* dns protocol */		  const u_char *key_buf,	/* key in dns wire fmt */		  const int key_len)		/* size of key */{		DST_KEY *dkey = NULL; 	int dnslen;	u_char dns[2048];	if (!dst_check_algorithm(alg)) { /* make sure alg is available */		EREPORT(("dst_buffer_to_key(): Algorithm %d not suppored\n", alg));		return (NULL);	}	dkey = dst_s_get_key_struct(key_name, alg, flags, 					     protocol, -1);	if (dkey == NULL)		return (NULL);	if (dkey->dk_func == NULL || dkey->dk_func->from_dns_key == NULL)		return NULL;	if (dkey->dk_func->from_dns_key(dkey, key_buf, key_len) < 0) {		EREPORT(("dst_buffer_to_key(): dst_buffer_to_hmac failed\n"));		return (dst_free_key(dkey));	}	dnslen = dst_key_to_dnskey(dkey, dns, sizeof(dns));	dkey->dk_id = dst_s_dns_key_id(dns, dnslen);	return (dkey);}int dst_key_to_buffer(DST_KEY *key, u_char *out_buff, int buf_len){	int len;  /* this function will extrac the secret of HMAC into a buffer */	if (key == NULL) 		return (0);	if (key->dk_func != NULL && key->dk_func->to_dns_key != NULL) {		len = key->dk_func->to_dns_key(key, out_buff, buf_len);		if (len < 0)			return (0);		return (len);	}	return (0);}/* * dst_s_read_private_key_file *     Function reads in private key from a file. *     Fills out the KEY structure. * Parameters *     name    Name of the key to be read. *     pk_key  Structure that the key is returned in. *     in_id   Key identifier (tag) * Return *     1 if everthing works *     0 if there is any problem */static intdst_s_read_private_key_file(char *name, DST_KEY *pk_key, u_int16_t in_id,			    int in_alg){	int cnt, alg, len, major, minor, file_major, file_minor;	int ret, id;	char filename[PATH_MAX];	u_char in_buff[RAW_KEY_SIZE], *p;	FILE *fp;	int dnslen;	u_char dns[2048];	if (name == NULL || pk_key == NULL) {		EREPORT(("dst_read_private_key_file(): No key name given\n"));		return (0);	}	/* Make the filename */	if (dst_s_build_filename(filename, name, in_id, in_alg, PRIVATE_KEY,				 PATH_MAX) == -1) {		EREPORT(("dst_read_private_key(): Cannot make filename from %s, %d, and %s\n",			 name, in_id, PRIVATE_KEY));		return (0);	}	/* first check if we can find the key file */	if ((fp = dst_s_fopen(filename, "r", 0)) == NULL) {		EREPORT(("dst_s_read_private_key_file: Could not open file %s in directory %s\n",			 filename, dst_path[0] ? dst_path :			 (char *) getcwd(NULL, PATH_MAX - 1)));		return (0);	}	/* now read the header info from the file */	if ((cnt = fread(in_buff, 1, sizeof(in_buff), fp)) < 5) {		fclose(fp);		EREPORT(("dst_s_read_private_key_file: error reading file %s (empty file)\n",			 filename));		return (0);	}	/* decrypt key */	fclose(fp);	if (memcmp(in_buff, "Private-key-format: v", 20) != 0)		goto fail;	len = cnt;	p = in_buff;	if (!dst_s_verify_str((const char **) &p, "Private-key-format: v")) {		EREPORT(("dst_s_read_private_key_file(): Not a Key file/Decrypt failed %s\n", name));		goto fail;	}	/* read in file format */	sscanf((char *)p, "%d.%d", &file_major, &file_minor);	sscanf(KEY_FILE_FORMAT, "%d.%d", &major, &minor);	if (file_major < 1) {		EREPORT(("dst_s_read_private_key_file(): Unknown keyfile %d.%d version for %s\n",			 file_major, file_minor, name));		goto fail;	} else if (file_major > major || file_minor > minor)		EREPORT((				"dst_s_read_private_key_file(): Keyfile %s version higher than mine %d.%d MAY FAIL\n",				name, file_major, file_minor));	while (*p++ != '\n') ;	/* skip to end of line */	if (!dst_s_verify_str((const char **) &p, "Algorithm: "))		goto fail;	if (sscanf((char *)p, "%d", &alg) != 1)		goto fail;	while (*p++ != '\n') ;	/* skip to end of line */	if (pk_key->dk_key_name && !strcmp(pk_key->dk_key_name, name))		SAFE_FREE2(pk_key->dk_key_name, strlen(pk_key->dk_key_name));	pk_key->dk_key_name = (char *) strdup(name);	/* allocate and fill in key structure */	if (pk_key->dk_func == NULL || pk_key->dk_func->from_file_fmt == NULL)		goto fail;	ret = pk_key->dk_func->from_file_fmt(pk_key, (char *)p, &in_buff[len] - p);	if (ret < 0)		goto fail;	dnslen = dst_key_to_dnskey(pk_key, dns, sizeof(dns));	id = dst_s_dns_key_id(dns, dnslen);	/* Make sure the actual key tag matches the input tag used in the filename	 */	if (id != in_id) {		EREPORT(("dst_s_read_private_key_file(): actual tag of key read %d != input tag used to build filename %d.\n", id, in_id));		goto fail;	}	pk_key->dk_id = (u_int16_t) id;	pk_key->dk_alg = alg;	memset(in_buff, 0, cnt);	return (1); fail:	memset(in_buff, 0, cnt);	return (0);}/* *  dst_generate_key *	Generate and store a public/private keypair. *	Keys will be stored in formatted files. *  Parameters *	name    Name of the new key.  Used to create key files *		  K<name>+<alg>+<id>.public and K<name>+<alg>+<id>.private. *	bits    Size of the new key in bits. *	exp     What exponent to use: *		  0	   use exponent 3 *		  non-zero    use Fermant4 *	flags   The default value of the DNS Key flags. *		  The DNS Key RR Flag field is defined in RFC 2065, *		  section 3.3.  The field has 16 bits. *	protocol *	      Default value of the DNS Key protocol field. *		  The DNS Key protocol field is defined in RFC 2065, *		  section 3.4.  The field has 8 bits. *	alg     What algorithm to use.  Currently defined: *		  KEY_RSA       1 *		  KEY_DSA       3 *		  KEY_HMAC    157 *	out_id The key tag is returned. * *  Return *	NULL		Failure *	non-NULL 	the generated key pair *			Caller frees the result, and its dk_name pointer. */DST_KEY *dst_generate_key(const char *name, const int bits, const int exp,		 const int flags, const int protocol, const int alg){	DST_KEY *new_key = NULL;	int res;	int dnslen;	u_char dns[2048];	if (name == NULL)		return (NULL);	if (!dst_check_algorithm(alg)) { /* make sure alg is available */		EREPORT(("dst_generate_key(): Algorithm %d not suppored\n", alg));		return (NULL);	}	new_key = dst_s_get_key_struct(name, alg, flags, protocol, bits);	if (new_key == NULL)		return (NULL);	if (bits == 0) /* null key we are done */		return (new_key);	if (new_key->dk_func == NULL || new_key->dk_func->generate == NULL) {		EREPORT(("dst_generate_key_pair():Unsupported algorithm %d\n",			 alg));		return (dst_free_key(new_key));	}	if ((res = new_key->dk_func->generate(new_key, exp)) <= 0) {		EREPORT(("dst_generate_key_pair(): Key generation failure %s %d %d %d\n",			 new_key->dk_key_name, new_key->dk_alg,			 new_key->dk_key_size, exp));		return (dst_free_key(new_key));	}	dnslen = dst_key_to_dnskey(new_key, dns, sizeof(dns));	if (dnslen != UNSUPPORTED_KEYALG)		new_key->dk_id = dst_s_dns_key_id(dns, dnslen);	else		new_key->dk_id = 0;	return (new_key);}/* *  dst_free_key *	Release all data structures pointed to by a key structure. *  Parameters *	f_key   Key structure to be freed. */DST_KEY *dst_free_key(DST_KEY *f_key){	if (f_key == NULL)		return (f_key);	if (f_key->dk_func && f_key->dk_func->destroy)		f_key->dk_KEY_struct =			f_key->dk_func->destroy(f_key->dk_KEY_struct);	else {		EREPORT(("dst_free_key(): Unknown key alg %d\n",			 f_key->dk_alg));		free(f_key->dk_KEY_struct);	/* SHOULD NOT happen */	}	if (f_key->dk_KEY_struct) {		free(f_key->dk_KEY_struct);		f_key->dk_KEY_struct = NULL;	}	if (f_key->dk_key_name)		SAFE_FREE(f_key->dk_key_name);	SAFE_FREE(f_key);	return (NULL);}/* * dst_sig_size *	Return the maximim size of signature from the key specified in bytes * Parameters *      key  * Returns *     bytes */intdst_sig_size(DST_KEY *key) {	switch (key->dk_alg) {	    case KEY_HMAC_MD5:		return (16);	    case KEY_HMAC_SHA1:		return (20);	    case KEY_RSA:		return (key->dk_key_size + 7) / 8;	    case KEY_DSA:		return (40);	    default:		EREPORT(("dst_sig_size(): Unknown key alg %d\n", key->dk_alg));		return -1;	}}

⌨️ 快捷键说明

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