xref: /onnv-gate/usr/src/lib/libresolv2/common/dst/dst_api.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate #ifndef LINT
2*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Header: /proj/cvs/prod/libbind/dst/dst_api.c,v 1.17 2007/09/24 17:18:25 each Exp $";
30Sstevel@tonic-gate #endif
40Sstevel@tonic-gate 
50Sstevel@tonic-gate /*
60Sstevel@tonic-gate  * Portions Copyright (c) 1995-1998 by Trusted Information Systems, Inc.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * Permission to use, copy modify, and distribute this software for any
90Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
100Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND TRUSTED INFORMATION SYSTEMS
130Sstevel@tonic-gate  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
140Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL
150Sstevel@tonic-gate  * TRUSTED INFORMATION SYSTEMS BE LIABLE FOR ANY SPECIAL, DIRECT,
160Sstevel@tonic-gate  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
170Sstevel@tonic-gate  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
180Sstevel@tonic-gate  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
190Sstevel@tonic-gate  * WITH THE USE OR PERFORMANCE OF THE SOFTWARE.
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
220Sstevel@tonic-gate  * This file contains the interface between the DST API and the crypto API.
230Sstevel@tonic-gate  * This is the only file that needs to be changed if the crypto system is
240Sstevel@tonic-gate  * changed.  Exported functions are:
250Sstevel@tonic-gate  * void dst_init()	 Initialize the toolkit
260Sstevel@tonic-gate  * int  dst_check_algorithm()   Function to determines if alg is suppored.
270Sstevel@tonic-gate  * int  dst_compare_keys()      Function to compare two keys for equality.
280Sstevel@tonic-gate  * int  dst_sign_data()         Incremental signing routine.
290Sstevel@tonic-gate  * int  dst_verify_data()       Incremental verify routine.
300Sstevel@tonic-gate  * int  dst_generate_key()      Function to generate new KEY
310Sstevel@tonic-gate  * DST_KEY *dst_read_key()      Function to retrieve private/public KEY.
320Sstevel@tonic-gate  * void dst_write_key()         Function to write out a key.
330Sstevel@tonic-gate  * DST_KEY *dst_dnskey_to_key() Function to convert DNS KEY RR to a DST
340Sstevel@tonic-gate  *				KEY structure.
350Sstevel@tonic-gate  * int dst_key_to_dnskey() 	Function to return a public key in DNS
360Sstevel@tonic-gate  *				format binary
370Sstevel@tonic-gate  * DST_KEY *dst_buffer_to_key() Converst a data in buffer to KEY
380Sstevel@tonic-gate  * int *dst_key_to_buffer()	Writes out DST_KEY key matterial in buffer
390Sstevel@tonic-gate  * void dst_free_key()       	Releases all memory referenced by key structure
400Sstevel@tonic-gate  */
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include "port_before.h"
430Sstevel@tonic-gate #include <stdio.h>
440Sstevel@tonic-gate #include <errno.h>
450Sstevel@tonic-gate #include <fcntl.h>
460Sstevel@tonic-gate #include <stdlib.h>
470Sstevel@tonic-gate #include <unistd.h>
480Sstevel@tonic-gate #include <string.h>
490Sstevel@tonic-gate #include <memory.h>
500Sstevel@tonic-gate #include <ctype.h>
510Sstevel@tonic-gate #include <time.h>
520Sstevel@tonic-gate #include <sys/param.h>
530Sstevel@tonic-gate #include <sys/stat.h>
540Sstevel@tonic-gate #include <sys/socket.h>
550Sstevel@tonic-gate #include <netinet/in.h>
560Sstevel@tonic-gate #include <arpa/nameser.h>
570Sstevel@tonic-gate #include <resolv.h>
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include "dst_internal.h"
600Sstevel@tonic-gate #include "port_after.h"
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /* static variables */
630Sstevel@tonic-gate static int done_init = 0;
640Sstevel@tonic-gate dst_func *dst_t_func[DST_MAX_ALGS];
650Sstevel@tonic-gate const char *key_file_fmt_str = "Private-key-format: v%s\nAlgorithm: %d (%s)\n";
660Sstevel@tonic-gate const char *dst_path = "";
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /* internal I/O functions */
690Sstevel@tonic-gate static DST_KEY *dst_s_read_public_key(const char *in_name,
700Sstevel@tonic-gate 				      const u_int16_t in_id, int in_alg);
710Sstevel@tonic-gate static int dst_s_read_private_key_file(char *name, DST_KEY *pk_key,
720Sstevel@tonic-gate 				       u_int16_t in_id, int in_alg);
730Sstevel@tonic-gate static int dst_s_write_public_key(const DST_KEY *key);
740Sstevel@tonic-gate static int dst_s_write_private_key(const DST_KEY *key);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /* internal function to set up data structure */
770Sstevel@tonic-gate static DST_KEY *dst_s_get_key_struct(const char *name, const int alg,
780Sstevel@tonic-gate 				     const int flags, const int protocol,
790Sstevel@tonic-gate 				     const int bits);
800Sstevel@tonic-gate 
81*11038SRao.Shoaib@Sun.COM /*%
820Sstevel@tonic-gate  *  dst_init
830Sstevel@tonic-gate  *	This function initializes the Digital Signature Toolkit.
840Sstevel@tonic-gate  *	Right now, it just checks the DSTKEYPATH environment variable.
850Sstevel@tonic-gate  *  Parameters
860Sstevel@tonic-gate  *	none
870Sstevel@tonic-gate  *  Returns
880Sstevel@tonic-gate  *	none
890Sstevel@tonic-gate  */
900Sstevel@tonic-gate void
dst_init()910Sstevel@tonic-gate dst_init()
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	char *s;
940Sstevel@tonic-gate 	int len;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	if (done_init != 0)
970Sstevel@tonic-gate 		return;
980Sstevel@tonic-gate 	done_init = 1;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	s = getenv("DSTKEYPATH");
1010Sstevel@tonic-gate 	len = 0;
1020Sstevel@tonic-gate 	if (s) {
1030Sstevel@tonic-gate 		struct stat statbuf;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 		len = strlen(s);
1060Sstevel@tonic-gate 		if (len > PATH_MAX) {
1070Sstevel@tonic-gate 			EREPORT(("%s is longer than %d characters, ignoring\n",
1080Sstevel@tonic-gate 				 s, PATH_MAX));
1090Sstevel@tonic-gate 		} else if (stat(s, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
1100Sstevel@tonic-gate 			EREPORT(("%s is not a valid directory\n", s));
1110Sstevel@tonic-gate 		} else {
1120Sstevel@tonic-gate 			char *tmp;
1130Sstevel@tonic-gate 			tmp = (char *) malloc(len + 2);
1140Sstevel@tonic-gate 			memcpy(tmp, s, len + 1);
1150Sstevel@tonic-gate 			if (tmp[strlen(tmp) - 1] != '/') {
1160Sstevel@tonic-gate 				tmp[strlen(tmp) + 1] = 0;
1170Sstevel@tonic-gate 				tmp[strlen(tmp)] = '/';
1180Sstevel@tonic-gate 			}
1190Sstevel@tonic-gate 			dst_path = tmp;
1200Sstevel@tonic-gate 		}
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 	memset(dst_t_func, 0, sizeof(dst_t_func));
1230Sstevel@tonic-gate 	/* first one is selected */
1240Sstevel@tonic-gate 	dst_hmac_md5_init();
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
127*11038SRao.Shoaib@Sun.COM /*%
1280Sstevel@tonic-gate  *  dst_check_algorithm
1290Sstevel@tonic-gate  *	This function determines if the crypto system for the specified
1300Sstevel@tonic-gate  *	algorithm is present.
1310Sstevel@tonic-gate  *  Parameters
1320Sstevel@tonic-gate  *	alg     1       KEY_RSA
1330Sstevel@tonic-gate  *		3       KEY_DSA
1340Sstevel@tonic-gate  *	      157     KEY_HMAC_MD5
1350Sstevel@tonic-gate  *		      future algorithms TBD and registered with IANA.
1360Sstevel@tonic-gate  *  Returns
1370Sstevel@tonic-gate  *	1 - The algorithm is available.
1380Sstevel@tonic-gate  *	0 - The algorithm is not available.
1390Sstevel@tonic-gate  */
1400Sstevel@tonic-gate int
dst_check_algorithm(const int alg)1410Sstevel@tonic-gate dst_check_algorithm(const int alg)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	return (dst_t_func[alg] != NULL);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate 
146*11038SRao.Shoaib@Sun.COM /*%
1470Sstevel@tonic-gate  * dst_s_get_key_struct
1480Sstevel@tonic-gate  *	This function allocates key structure and fills in some of the
1490Sstevel@tonic-gate  *	fields of the structure.
1500Sstevel@tonic-gate  * Parameters:
1510Sstevel@tonic-gate  *	name:     the name of the key
1520Sstevel@tonic-gate  *	alg:      the algorithm number
1530Sstevel@tonic-gate  *	flags:    the dns flags of the key
1540Sstevel@tonic-gate  *	protocol: the dns protocol of the key
1550Sstevel@tonic-gate  *	bits:     the size of the key
1560Sstevel@tonic-gate  * Returns:
1570Sstevel@tonic-gate  *       NULL if error
1580Sstevel@tonic-gate  *       valid pointer otherwise
1590Sstevel@tonic-gate  */
1600Sstevel@tonic-gate static DST_KEY *
dst_s_get_key_struct(const char * name,const int alg,const int flags,const int protocol,const int bits)1610Sstevel@tonic-gate dst_s_get_key_struct(const char *name, const int alg, const int flags,
1620Sstevel@tonic-gate 		     const int protocol, const int bits)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	DST_KEY *new_key = NULL;
1650Sstevel@tonic-gate 
166*11038SRao.Shoaib@Sun.COM 	if (dst_check_algorithm(alg)) /*%< make sure alg is available */
1670Sstevel@tonic-gate 		new_key = (DST_KEY *) malloc(sizeof(*new_key));
1680Sstevel@tonic-gate 	if (new_key == NULL)
1690Sstevel@tonic-gate 		return (NULL);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	memset(new_key, 0, sizeof(*new_key));
1720Sstevel@tonic-gate 	new_key->dk_key_name = strdup(name);
173*11038SRao.Shoaib@Sun.COM 	if (new_key->dk_key_name == NULL) {
174*11038SRao.Shoaib@Sun.COM 		free(new_key);
175*11038SRao.Shoaib@Sun.COM 		return (NULL);
176*11038SRao.Shoaib@Sun.COM 	}
1770Sstevel@tonic-gate 	new_key->dk_alg = alg;
1780Sstevel@tonic-gate 	new_key->dk_flags = flags;
1790Sstevel@tonic-gate 	new_key->dk_proto = protocol;
1800Sstevel@tonic-gate 	new_key->dk_KEY_struct = NULL;
1810Sstevel@tonic-gate 	new_key->dk_key_size = bits;
1820Sstevel@tonic-gate 	new_key->dk_func = dst_t_func[alg];
1830Sstevel@tonic-gate 	return (new_key);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate 
186*11038SRao.Shoaib@Sun.COM /*%
1870Sstevel@tonic-gate  *  dst_compare_keys
1880Sstevel@tonic-gate  *	Compares two keys for equality.
1890Sstevel@tonic-gate  *  Parameters
1900Sstevel@tonic-gate  *	key1, key2      Two keys to be compared.
1910Sstevel@tonic-gate  *  Returns
1920Sstevel@tonic-gate  *	0	       The keys are equal.
1930Sstevel@tonic-gate  *	non-zero	The keys are not equal.
1940Sstevel@tonic-gate  */
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate int
dst_compare_keys(const DST_KEY * key1,const DST_KEY * key2)1970Sstevel@tonic-gate dst_compare_keys(const DST_KEY *key1, const DST_KEY *key2)
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate 	if (key1 == key2)
2000Sstevel@tonic-gate 		return (0);
2010Sstevel@tonic-gate 	if (key1 == NULL || key2 == NULL)
2020Sstevel@tonic-gate 		return (4);
2030Sstevel@tonic-gate 	if (key1->dk_alg != key2->dk_alg)
2040Sstevel@tonic-gate 		return (1);
2050Sstevel@tonic-gate 	if (key1->dk_key_size != key2->dk_key_size)
2060Sstevel@tonic-gate 		return (2);
2070Sstevel@tonic-gate 	if (key1->dk_id != key2->dk_id)
2080Sstevel@tonic-gate 		return (3);
2090Sstevel@tonic-gate 	return (key1->dk_func->compare(key1, key2));
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate 
212*11038SRao.Shoaib@Sun.COM /*%
2130Sstevel@tonic-gate  * dst_sign_data
2140Sstevel@tonic-gate  *	An incremental signing function.  Data is signed in steps.
2150Sstevel@tonic-gate  *	First the context must be initialized (SIG_MODE_INIT).
2160Sstevel@tonic-gate  *	Then data is hashed (SIG_MODE_UPDATE).  Finally the signature
2170Sstevel@tonic-gate  *	itself is created (SIG_MODE_FINAL).  This function can be called
2180Sstevel@tonic-gate  *	once with INIT, UPDATE and FINAL modes all set, or it can be
2190Sstevel@tonic-gate  *	called separately with a different mode set for each step.  The
2200Sstevel@tonic-gate  *	UPDATE step can be repeated.
2210Sstevel@tonic-gate  * Parameters
2220Sstevel@tonic-gate  *	mode    A bit mask used to specify operation(s) to be performed.
2230Sstevel@tonic-gate  *		  SIG_MODE_INIT	   1   Initialize digest
2240Sstevel@tonic-gate  *		  SIG_MODE_UPDATE	 2   Add data to digest
2250Sstevel@tonic-gate  *		  SIG_MODE_FINAL	  4   Generate signature
2260Sstevel@tonic-gate  *					      from signature
2270Sstevel@tonic-gate  *		  SIG_MODE_ALL (SIG_MODE_INIT,SIG_MODE_UPDATE,SIG_MODE_FINAL
2280Sstevel@tonic-gate  *	data    Data to be signed.
2290Sstevel@tonic-gate  *	len     The length in bytes of data to be signed.
2300Sstevel@tonic-gate  *	in_key  Contains a private key to sign with.
2310Sstevel@tonic-gate  *		  KEY structures should be handled (created, converted,
2320Sstevel@tonic-gate  *		  compared, stored, freed) by the DST.
2330Sstevel@tonic-gate  *	signature
2340Sstevel@tonic-gate  *	      The location to which the signature will be written.
2350Sstevel@tonic-gate  *	sig_len Length of the signature field in bytes.
2360Sstevel@tonic-gate  * Return
2370Sstevel@tonic-gate  *	 0      Successfull INIT or Update operation
238*11038SRao.Shoaib@Sun.COM  *	&gt;0      success FINAL (sign) operation
239*11038SRao.Shoaib@Sun.COM  *	&lt;0      failure
2400Sstevel@tonic-gate  */
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate int
dst_sign_data(const int mode,DST_KEY * in_key,void ** context,const u_char * data,const int len,u_char * signature,const int sig_len)2430Sstevel@tonic-gate dst_sign_data(const int mode, DST_KEY *in_key, void **context,
2440Sstevel@tonic-gate 	      const u_char *data, const int len,
2450Sstevel@tonic-gate 	      u_char *signature, const int sig_len)
2460Sstevel@tonic-gate {
2470Sstevel@tonic-gate 	DUMP(data, mode, len, "dst_sign_data()");
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	if (mode & SIG_MODE_FINAL &&
2500Sstevel@tonic-gate 	    (in_key->dk_KEY_struct == NULL || signature == NULL))
2510Sstevel@tonic-gate 		return (MISSING_KEY_OR_SIGNATURE);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	if (in_key->dk_func && in_key->dk_func->sign)
2540Sstevel@tonic-gate 		return (in_key->dk_func->sign(mode, in_key, context, data, len,
2550Sstevel@tonic-gate 					      signature, sig_len));
2560Sstevel@tonic-gate 	return (UNKNOWN_KEYALG);
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate 
259*11038SRao.Shoaib@Sun.COM /*%
2600Sstevel@tonic-gate  *  dst_verify_data
2610Sstevel@tonic-gate  *	An incremental verify function.  Data is verified in steps.
2620Sstevel@tonic-gate  *	First the context must be initialized (SIG_MODE_INIT).
2630Sstevel@tonic-gate  *	Then data is hashed (SIG_MODE_UPDATE).  Finally the signature
2640Sstevel@tonic-gate  *	is verified (SIG_MODE_FINAL).  This function can be called
2650Sstevel@tonic-gate  *	once with INIT, UPDATE and FINAL modes all set, or it can be
2660Sstevel@tonic-gate  *	called separately with a different mode set for each step.  The
2670Sstevel@tonic-gate  *	UPDATE step can be repeated.
2680Sstevel@tonic-gate  *  Parameters
2690Sstevel@tonic-gate  *	mode	Operations to perform this time.
2700Sstevel@tonic-gate  *		      SIG_MODE_INIT       1   Initialize digest
2710Sstevel@tonic-gate  *		      SIG_MODE_UPDATE     2   add data to digest
2720Sstevel@tonic-gate  *		      SIG_MODE_FINAL      4   verify signature
2730Sstevel@tonic-gate  *		      SIG_MODE_ALL
2740Sstevel@tonic-gate  *			  (SIG_MODE_INIT,SIG_MODE_UPDATE,SIG_MODE_FINAL)
2750Sstevel@tonic-gate  *	data	Data to pass through the hash function.
2760Sstevel@tonic-gate  *	len	 Length of the data in bytes.
2770Sstevel@tonic-gate  *	in_key      Key for verification.
2780Sstevel@tonic-gate  *	signature   Location of signature.
2790Sstevel@tonic-gate  *	sig_len     Length of the signature in bytes.
2800Sstevel@tonic-gate  *  Returns
2810Sstevel@tonic-gate  *	0	   Verify success
2820Sstevel@tonic-gate  *	Non-Zero    Verify Failure
2830Sstevel@tonic-gate  */
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate int
dst_verify_data(const int mode,DST_KEY * in_key,void ** context,const u_char * data,const int len,const u_char * signature,const int sig_len)2860Sstevel@tonic-gate dst_verify_data(const int mode, DST_KEY *in_key, void **context,
2870Sstevel@tonic-gate 		const u_char *data, const int len,
2880Sstevel@tonic-gate 		const u_char *signature, const int sig_len)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate 	DUMP(data, mode, len, "dst_verify_data()");
2910Sstevel@tonic-gate 	if (mode & SIG_MODE_FINAL &&
2920Sstevel@tonic-gate 	    (in_key->dk_KEY_struct == NULL || signature == NULL))
2930Sstevel@tonic-gate 		return (MISSING_KEY_OR_SIGNATURE);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	if (in_key->dk_func == NULL || in_key->dk_func->verify == NULL)
2960Sstevel@tonic-gate 		return (UNSUPPORTED_KEYALG);
2970Sstevel@tonic-gate 	return (in_key->dk_func->verify(mode, in_key, context, data, len,
2980Sstevel@tonic-gate 					signature, sig_len));
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate 
301*11038SRao.Shoaib@Sun.COM /*%
3020Sstevel@tonic-gate  *  dst_read_private_key
3030Sstevel@tonic-gate  *	Access a private key.  First the list of private keys that have
3040Sstevel@tonic-gate  *	already been read in is searched, then the key accessed on disk.
3050Sstevel@tonic-gate  *	If the private key can be found, it is returned.  If the key cannot
3060Sstevel@tonic-gate  *	be found, a null pointer is returned.  The options specify required
3070Sstevel@tonic-gate  *	key characteristics.  If the private key requested does not have
3080Sstevel@tonic-gate  *	these characteristics, it will not be read.
3090Sstevel@tonic-gate  *  Parameters
3100Sstevel@tonic-gate  *	in_keyname  The private key name.
3110Sstevel@tonic-gate  *	in_id	    The id of the private key.
3120Sstevel@tonic-gate  *	options     DST_FORCE_READ  Read from disk - don't use a previously
3130Sstevel@tonic-gate  *				      read key.
3140Sstevel@tonic-gate  *		  DST_CAN_SIGN    The key must be useable for signing.
3150Sstevel@tonic-gate  *		  DST_NO_AUTHEN   The key must be useable for authentication.
3160Sstevel@tonic-gate  *		  DST_STANDARD    Return any key
3170Sstevel@tonic-gate  *  Returns
3180Sstevel@tonic-gate  *	NULL	If there is no key found in the current directory or
3190Sstevel@tonic-gate  *		      this key has not been loaded before.
3200Sstevel@tonic-gate  *	!NULL       Success - KEY structure returned.
3210Sstevel@tonic-gate  */
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate DST_KEY *
dst_read_key(const char * in_keyname,const u_int16_t in_id,const int in_alg,const int type)3240Sstevel@tonic-gate dst_read_key(const char *in_keyname, const u_int16_t in_id,
3250Sstevel@tonic-gate 	     const int in_alg, const int type)
3260Sstevel@tonic-gate {
3270Sstevel@tonic-gate 	char keyname[PATH_MAX];
3280Sstevel@tonic-gate 	DST_KEY *dg_key = NULL, *pubkey = NULL;
3290Sstevel@tonic-gate 
330*11038SRao.Shoaib@Sun.COM 	if (!dst_check_algorithm(in_alg)) { /*%< make sure alg is available */
3310Sstevel@tonic-gate 		EREPORT(("dst_read_private_key(): Algorithm %d not suppored\n",
3320Sstevel@tonic-gate 			 in_alg));
3330Sstevel@tonic-gate 		return (NULL);
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 	if ((type & (DST_PUBLIC | DST_PRIVATE)) == 0)
3360Sstevel@tonic-gate 		return (NULL);
3370Sstevel@tonic-gate 	if (in_keyname == NULL) {
3380Sstevel@tonic-gate 		EREPORT(("dst_read_private_key(): Null key name passed in\n"));
3390Sstevel@tonic-gate 		return (NULL);
340*11038SRao.Shoaib@Sun.COM 	} else if (strlen(in_keyname) >= sizeof(keyname)) {
341*11038SRao.Shoaib@Sun.COM 		EREPORT(("dst_read_private_key(): keyname too big\n"));
342*11038SRao.Shoaib@Sun.COM 		return (NULL);
343*11038SRao.Shoaib@Sun.COM 	} else
3440Sstevel@tonic-gate 		strcpy(keyname, in_keyname);
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	/* before I read in the public key, check if it is allowed to sign */
3470Sstevel@tonic-gate 	if ((pubkey = dst_s_read_public_key(keyname, in_id, in_alg)) == NULL)
3480Sstevel@tonic-gate 		return (NULL);
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	if (type == DST_PUBLIC)
3510Sstevel@tonic-gate 		return pubkey;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	if (!(dg_key = dst_s_get_key_struct(keyname, pubkey->dk_alg,
354*11038SRao.Shoaib@Sun.COM 					    pubkey->dk_flags, pubkey->dk_proto,
3550Sstevel@tonic-gate 					    0)))
3560Sstevel@tonic-gate 		return (dg_key);
3570Sstevel@tonic-gate 	/* Fill in private key and some fields in the general key structure */
3580Sstevel@tonic-gate 	if (dst_s_read_private_key_file(keyname, dg_key, pubkey->dk_id,
3590Sstevel@tonic-gate 					pubkey->dk_alg) == 0)
3600Sstevel@tonic-gate 		dg_key = dst_free_key(dg_key);
3610Sstevel@tonic-gate 
362*11038SRao.Shoaib@Sun.COM 	(void)dst_free_key(pubkey);
3630Sstevel@tonic-gate 	return (dg_key);
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate int
dst_write_key(const DST_KEY * key,const int type)3670Sstevel@tonic-gate dst_write_key(const DST_KEY *key, const int type)
3680Sstevel@tonic-gate {
3690Sstevel@tonic-gate 	int pub = 0, priv = 0;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	if (key == NULL)
3720Sstevel@tonic-gate 		return (0);
373*11038SRao.Shoaib@Sun.COM 	if (!dst_check_algorithm(key->dk_alg)) { /*%< make sure alg is available */
3740Sstevel@tonic-gate 		EREPORT(("dst_write_key(): Algorithm %d not suppored\n",
3750Sstevel@tonic-gate 			 key->dk_alg));
3760Sstevel@tonic-gate 		return (UNSUPPORTED_KEYALG);
3770Sstevel@tonic-gate 	}
3780Sstevel@tonic-gate 	if ((type & (DST_PRIVATE|DST_PUBLIC)) == 0)
3790Sstevel@tonic-gate 		return (0);
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	if (type & DST_PUBLIC)
3820Sstevel@tonic-gate 		if ((pub = dst_s_write_public_key(key)) < 0)
3830Sstevel@tonic-gate 			return (pub);
3840Sstevel@tonic-gate 	if (type & DST_PRIVATE)
3850Sstevel@tonic-gate 		if ((priv = dst_s_write_private_key(key)) < 0)
3860Sstevel@tonic-gate 			return (priv);
3870Sstevel@tonic-gate 	return (priv+pub);
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate 
390*11038SRao.Shoaib@Sun.COM /*%
3910Sstevel@tonic-gate  *  dst_write_private_key
3920Sstevel@tonic-gate  *	Write a private key to disk.  The filename will be of the form:
393*11038SRao.Shoaib@Sun.COM  *	K&lt;key-&gt;dk_name&gt;+&lt;key-&gt;dk_alg+&gt;&lt;key-d&gt;k_id.&gt;&lt;private key suffix&gt;.
3940Sstevel@tonic-gate  *	If there is already a file with this name, an error is returned.
3950Sstevel@tonic-gate  *
3960Sstevel@tonic-gate  *  Parameters
3970Sstevel@tonic-gate  *	key     A DST managed key structure that contains
3980Sstevel@tonic-gate  *	      all information needed about a key.
3990Sstevel@tonic-gate  *  Return
400*11038SRao.Shoaib@Sun.COM  *	&gt;= 0    Correct behavior.  Returns length of encoded key value
4010Sstevel@tonic-gate  *		  written to disk.
402*11038SRao.Shoaib@Sun.COM  *	&lt;  0    error.
4030Sstevel@tonic-gate  */
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate static int
dst_s_write_private_key(const DST_KEY * key)4060Sstevel@tonic-gate dst_s_write_private_key(const DST_KEY *key)
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate 	u_char encoded_block[RAW_KEY_SIZE];
4090Sstevel@tonic-gate 	char file[PATH_MAX];
4100Sstevel@tonic-gate 	int len;
4110Sstevel@tonic-gate 	FILE *fp;
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	/* First encode the key into the portable key format */
4140Sstevel@tonic-gate 	if (key == NULL)
4150Sstevel@tonic-gate 		return (-1);
4160Sstevel@tonic-gate 	if (key->dk_KEY_struct == NULL)
417*11038SRao.Shoaib@Sun.COM 		return (0);	/*%< null key has no private key */
4180Sstevel@tonic-gate 	if (key->dk_func == NULL || key->dk_func->to_file_fmt == NULL) {
4190Sstevel@tonic-gate 		EREPORT(("dst_write_private_key(): Unsupported operation %d\n",
4200Sstevel@tonic-gate 			 key->dk_alg));
4210Sstevel@tonic-gate 		return (-5);
4220Sstevel@tonic-gate 	} else if ((len = key->dk_func->to_file_fmt(key, (char *)encoded_block,
4230Sstevel@tonic-gate 					     sizeof(encoded_block))) <= 0) {
4240Sstevel@tonic-gate 		EREPORT(("dst_write_private_key(): Failed encoding private RSA bsafe key %d\n", len));
4250Sstevel@tonic-gate 		return (-8);
4260Sstevel@tonic-gate 	}
4270Sstevel@tonic-gate 	/* Now I can create the file I want to use */
4280Sstevel@tonic-gate 	dst_s_build_filename(file, key->dk_key_name, key->dk_id, key->dk_alg,
4290Sstevel@tonic-gate 			     PRIVATE_KEY, PATH_MAX);
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	/* Do not overwrite an existing file */
4320Sstevel@tonic-gate 	if ((fp = dst_s_fopen(file, "w", 0600)) != NULL) {
4330Sstevel@tonic-gate 		int nn;
4340Sstevel@tonic-gate 		if ((nn = fwrite(encoded_block, 1, len, fp)) != len) {
4350Sstevel@tonic-gate 			EREPORT(("dst_write_private_key(): Write failure on %s %d != %d errno=%d\n",
4360Sstevel@tonic-gate 				 file, len, nn, errno));
437*11038SRao.Shoaib@Sun.COM 			fclose(fp);
4380Sstevel@tonic-gate 			return (-5);
4390Sstevel@tonic-gate 		}
4400Sstevel@tonic-gate 		fclose(fp);
4410Sstevel@tonic-gate 	} else {
4420Sstevel@tonic-gate 		EREPORT(("dst_write_private_key(): Can not create file %s\n"
4430Sstevel@tonic-gate 			 ,file));
4440Sstevel@tonic-gate 		return (-6);
4450Sstevel@tonic-gate 	}
4460Sstevel@tonic-gate 	memset(encoded_block, 0, len);
4470Sstevel@tonic-gate 	return (len);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate 
450*11038SRao.Shoaib@Sun.COM /*%
4510Sstevel@tonic-gate *
4520Sstevel@tonic-gate  *  dst_read_public_key
4530Sstevel@tonic-gate  *	Read a public key from disk and store in a DST key structure.
4540Sstevel@tonic-gate  *  Parameters
455*11038SRao.Shoaib@Sun.COM  *	in_name	 K&lt;in_name&gt;&lt;in_id&gt;.&lt;public key suffix&gt; is the
4560Sstevel@tonic-gate  *		      filename of the key file to be read.
4570Sstevel@tonic-gate  *  Returns
4580Sstevel@tonic-gate  *	NULL	    If the key does not exist or no name is supplied.
4590Sstevel@tonic-gate  *	NON-NULL	Initialized key structure if the key exists.
4600Sstevel@tonic-gate  */
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate static DST_KEY *
dst_s_read_public_key(const char * in_name,const u_int16_t in_id,int in_alg)4630Sstevel@tonic-gate dst_s_read_public_key(const char *in_name, const u_int16_t in_id, int in_alg)
4640Sstevel@tonic-gate {
4650Sstevel@tonic-gate 	int flags, proto, alg, len, dlen;
4660Sstevel@tonic-gate 	int c;
4670Sstevel@tonic-gate 	char name[PATH_MAX], enckey[RAW_KEY_SIZE], *notspace;
4680Sstevel@tonic-gate 	u_char deckey[RAW_KEY_SIZE];
4690Sstevel@tonic-gate 	FILE *fp;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	if (in_name == NULL) {
4720Sstevel@tonic-gate 		EREPORT(("dst_read_public_key(): No key name given\n"));
4730Sstevel@tonic-gate 		return (NULL);
4740Sstevel@tonic-gate 	}
4750Sstevel@tonic-gate 	if (dst_s_build_filename(name, in_name, in_id, in_alg, PUBLIC_KEY,
4760Sstevel@tonic-gate 				 PATH_MAX) == -1) {
4770Sstevel@tonic-gate 		EREPORT(("dst_read_public_key(): Cannot make filename from %s, %d, and %s\n",
4780Sstevel@tonic-gate 			 in_name, in_id, PUBLIC_KEY));
4790Sstevel@tonic-gate 		return (NULL);
4800Sstevel@tonic-gate 	}
4810Sstevel@tonic-gate 	/*
4820Sstevel@tonic-gate 	 * Open the file and read it's formatted contents up to key
4830Sstevel@tonic-gate 	 * File format:
484*11038SRao.Shoaib@Sun.COM 	 *    domain.name [ttl] [IN] KEY  &lt;flags&gt; &lt;protocol&gt; &lt;algorithm&gt; &lt;key&gt;
4850Sstevel@tonic-gate 	 * flags, proto, alg stored as decimal (or hex numbers FIXME).
4860Sstevel@tonic-gate 	 * (FIXME: handle parentheses for line continuation.)
4870Sstevel@tonic-gate 	 */
4880Sstevel@tonic-gate 	if ((fp = dst_s_fopen(name, "r", 0)) == NULL) {
4890Sstevel@tonic-gate 		EREPORT(("dst_read_public_key(): Public Key not found %s\n",
4900Sstevel@tonic-gate 			 name));
4910Sstevel@tonic-gate 		return (NULL);
4920Sstevel@tonic-gate 	}
4930Sstevel@tonic-gate 	/* Skip domain name, which ends at first blank */
4940Sstevel@tonic-gate 	while ((c = getc(fp)) != EOF)
4950Sstevel@tonic-gate 		if (isspace(c))
4960Sstevel@tonic-gate 			break;
4970Sstevel@tonic-gate 	/* Skip blank to get to next field */
4980Sstevel@tonic-gate 	while ((c = getc(fp)) != EOF)
4990Sstevel@tonic-gate 		if (!isspace(c))
5000Sstevel@tonic-gate 			break;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	/* Skip optional TTL -- if initial digit, skip whole word. */
5030Sstevel@tonic-gate 	if (isdigit(c)) {
5040Sstevel@tonic-gate 		while ((c = getc(fp)) != EOF)
5050Sstevel@tonic-gate 			if (isspace(c))
5060Sstevel@tonic-gate 				break;
5070Sstevel@tonic-gate 		while ((c = getc(fp)) != EOF)
5080Sstevel@tonic-gate 			if (!isspace(c))
5090Sstevel@tonic-gate 				break;
5100Sstevel@tonic-gate 	}
5110Sstevel@tonic-gate 	/* Skip optional "IN" */
5120Sstevel@tonic-gate 	if (c == 'I' || c == 'i') {
5130Sstevel@tonic-gate 		while ((c = getc(fp)) != EOF)
5140Sstevel@tonic-gate 			if (isspace(c))
5150Sstevel@tonic-gate 				break;
5160Sstevel@tonic-gate 		while ((c = getc(fp)) != EOF)
5170Sstevel@tonic-gate 			if (!isspace(c))
5180Sstevel@tonic-gate 				break;
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 	/* Locate and skip "KEY" */
5210Sstevel@tonic-gate 	if (c != 'K' && c != 'k') {
5220Sstevel@tonic-gate 		EREPORT(("\"KEY\" doesn't appear in file: %s", name));
5230Sstevel@tonic-gate 		return NULL;
5240Sstevel@tonic-gate 	}
5250Sstevel@tonic-gate 	while ((c = getc(fp)) != EOF)
5260Sstevel@tonic-gate 		if (isspace(c))
5270Sstevel@tonic-gate 			break;
5280Sstevel@tonic-gate 	while ((c = getc(fp)) != EOF)
5290Sstevel@tonic-gate 		if (!isspace(c))
5300Sstevel@tonic-gate 			break;
531*11038SRao.Shoaib@Sun.COM 	ungetc(c, fp);		/*%< return the charcter to the input field */
5320Sstevel@tonic-gate 	/* Handle hex!! FIXME.  */
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	if (fscanf(fp, "%d %d %d", &flags, &proto, &alg) != 3) {
5350Sstevel@tonic-gate 		EREPORT(("dst_read_public_key(): Can not read flag/proto/alg field from %s\n"
5360Sstevel@tonic-gate 			 ,name));
5370Sstevel@tonic-gate 		return (NULL);
5380Sstevel@tonic-gate 	}
5390Sstevel@tonic-gate 	/* read in the key string */
5400Sstevel@tonic-gate 	fgets(enckey, sizeof(enckey), fp);
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	/* If we aren't at end-of-file, something is wrong.  */
5430Sstevel@tonic-gate 	while ((c = getc(fp)) != EOF)
5440Sstevel@tonic-gate 		if (!isspace(c))
5450Sstevel@tonic-gate 			break;
5460Sstevel@tonic-gate 	if (!feof(fp)) {
5470Sstevel@tonic-gate 		EREPORT(("Key too long in file: %s", name));
5480Sstevel@tonic-gate 		return NULL;
5490Sstevel@tonic-gate 	}
5500Sstevel@tonic-gate 	fclose(fp);
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	if ((len = strlen(enckey)) <= 0)
5530Sstevel@tonic-gate 		return (NULL);
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	/* discard \n */
5560Sstevel@tonic-gate 	enckey[--len] = '\0';
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	/* remove leading spaces */
5590Sstevel@tonic-gate 	for (notspace = (char *) enckey; isspace((*notspace)&0xff); len--)
5600Sstevel@tonic-gate 		notspace++;
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	dlen = b64_pton(notspace, deckey, sizeof(deckey));
5630Sstevel@tonic-gate 	if (dlen < 0) {
5640Sstevel@tonic-gate 		EREPORT(("dst_read_public_key: bad return from b64_pton = %d",
5650Sstevel@tonic-gate 			 dlen));
5660Sstevel@tonic-gate 		return (NULL);
5670Sstevel@tonic-gate 	}
5680Sstevel@tonic-gate 	/* store key and info in a key structure that is returned */
5690Sstevel@tonic-gate /*	return dst_store_public_key(in_name, alg, proto, 666, flags, deckey,
5700Sstevel@tonic-gate 				    dlen);*/
5710Sstevel@tonic-gate 	return dst_buffer_to_key(in_name, alg, flags, proto, deckey, dlen);
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate 
574*11038SRao.Shoaib@Sun.COM /*%
5750Sstevel@tonic-gate  *  dst_write_public_key
5760Sstevel@tonic-gate  *	Write a key to disk in DNS format.
5770Sstevel@tonic-gate  *  Parameters
5780Sstevel@tonic-gate  *	key     Pointer to a DST key structure.
5790Sstevel@tonic-gate  *  Returns
5800Sstevel@tonic-gate  *	0       Failure
5810Sstevel@tonic-gate  *	1       Success
5820Sstevel@tonic-gate  */
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate static int
dst_s_write_public_key(const DST_KEY * key)5850Sstevel@tonic-gate dst_s_write_public_key(const DST_KEY *key)
5860Sstevel@tonic-gate {
5870Sstevel@tonic-gate 	FILE *fp;
5880Sstevel@tonic-gate 	char filename[PATH_MAX];
5890Sstevel@tonic-gate 	u_char out_key[RAW_KEY_SIZE];
5900Sstevel@tonic-gate 	char enc_key[RAW_KEY_SIZE];
5910Sstevel@tonic-gate 	int len = 0;
5920Sstevel@tonic-gate 	int mode;
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	memset(out_key, 0, sizeof(out_key));
5950Sstevel@tonic-gate 	if (key == NULL) {
5960Sstevel@tonic-gate 		EREPORT(("dst_write_public_key(): No key specified \n"));
5970Sstevel@tonic-gate 		return (0);
5980Sstevel@tonic-gate 	} else if ((len = dst_key_to_dnskey(key, out_key, sizeof(out_key)))< 0)
5990Sstevel@tonic-gate 		return (0);
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	/* Make the filename */
6020Sstevel@tonic-gate 	if (dst_s_build_filename(filename, key->dk_key_name, key->dk_id,
6030Sstevel@tonic-gate 				 key->dk_alg, PUBLIC_KEY, PATH_MAX) == -1) {
6040Sstevel@tonic-gate 		EREPORT(("dst_write_public_key(): Cannot make filename from %s, %d, and %s\n",
6050Sstevel@tonic-gate 			 key->dk_key_name, key->dk_id, PUBLIC_KEY));
6060Sstevel@tonic-gate 		return (0);
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 	/* XXX in general this should be a check for symmetric keys */
6090Sstevel@tonic-gate 	mode = (key->dk_alg == KEY_HMAC_MD5) ? 0600 : 0644;
6100Sstevel@tonic-gate 	/* create public key file */
6110Sstevel@tonic-gate 	if ((fp = dst_s_fopen(filename, "w+", mode)) == NULL) {
6120Sstevel@tonic-gate 		EREPORT(("DST_write_public_key: open of file:%s failed (errno=%d)\n",
6130Sstevel@tonic-gate 			 filename, errno));
6140Sstevel@tonic-gate 		return (0);
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate 	/*write out key first base64 the key data */
6170Sstevel@tonic-gate 	if (key->dk_flags & DST_EXTEND_FLAG)
6180Sstevel@tonic-gate 		b64_ntop(&out_key[6], len - 6, enc_key, sizeof(enc_key));
6190Sstevel@tonic-gate 	else
6200Sstevel@tonic-gate 		b64_ntop(&out_key[4], len - 4, enc_key, sizeof(enc_key));
6210Sstevel@tonic-gate 	fprintf(fp, "%s IN KEY %d %d %d %s\n",
6220Sstevel@tonic-gate 		key->dk_key_name,
6230Sstevel@tonic-gate 		key->dk_flags, key->dk_proto, key->dk_alg, enc_key);
6240Sstevel@tonic-gate 	fclose(fp);
6250Sstevel@tonic-gate 	return (1);
6260Sstevel@tonic-gate }
6270Sstevel@tonic-gate 
628*11038SRao.Shoaib@Sun.COM /*%
6290Sstevel@tonic-gate  *  dst_dnskey_to_public_key
6300Sstevel@tonic-gate  *	This function converts the contents of a DNS KEY RR into a DST
6310Sstevel@tonic-gate  *	key structure.
6320Sstevel@tonic-gate  *  Paramters
6330Sstevel@tonic-gate  *	len	 Length of the RDATA of the KEY RR RDATA
6340Sstevel@tonic-gate  *	rdata	 A pointer to the the KEY RR RDATA.
6350Sstevel@tonic-gate  *	in_name     Key name to be stored in key structure.
6360Sstevel@tonic-gate  *  Returns
6370Sstevel@tonic-gate  *	NULL	    Failure
6380Sstevel@tonic-gate  *	NON-NULL	Success.  Pointer to key structure.
6390Sstevel@tonic-gate  *			Caller's responsibility to free() it.
6400Sstevel@tonic-gate  */
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate DST_KEY *
dst_dnskey_to_key(const char * in_name,const u_char * rdata,const int len)6430Sstevel@tonic-gate dst_dnskey_to_key(const char *in_name, const u_char *rdata, const int len)
6440Sstevel@tonic-gate {
6450Sstevel@tonic-gate 	DST_KEY *key_st;
6460Sstevel@tonic-gate 	int alg ;
6470Sstevel@tonic-gate 	int start = DST_KEY_START;
6480Sstevel@tonic-gate 
649*11038SRao.Shoaib@Sun.COM 	if (rdata == NULL || len <= DST_KEY_ALG) /*%< no data */
6500Sstevel@tonic-gate 		return (NULL);
6510Sstevel@tonic-gate 	alg = (u_int8_t) rdata[DST_KEY_ALG];
652*11038SRao.Shoaib@Sun.COM 	if (!dst_check_algorithm(alg)) { /*%< make sure alg is available */
6530Sstevel@tonic-gate 		EREPORT(("dst_dnskey_to_key(): Algorithm %d not suppored\n",
6540Sstevel@tonic-gate 			 alg));
6550Sstevel@tonic-gate 		return (NULL);
6560Sstevel@tonic-gate 	}
657*11038SRao.Shoaib@Sun.COM 
658*11038SRao.Shoaib@Sun.COM 	if (in_name == NULL)
659*11038SRao.Shoaib@Sun.COM 		return (NULL);
660*11038SRao.Shoaib@Sun.COM 
6610Sstevel@tonic-gate 	if ((key_st = dst_s_get_key_struct(in_name, alg, 0, 0, 0)) == NULL)
6620Sstevel@tonic-gate 		return (NULL);
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	key_st->dk_id = dst_s_dns_key_id(rdata, len);
6650Sstevel@tonic-gate 	key_st->dk_flags = dst_s_get_int16(rdata);
6660Sstevel@tonic-gate 	key_st->dk_proto = (u_int16_t) rdata[DST_KEY_PROT];
6670Sstevel@tonic-gate 	if (key_st->dk_flags & DST_EXTEND_FLAG) {
6680Sstevel@tonic-gate 		u_int32_t ext_flags;
6690Sstevel@tonic-gate 		ext_flags = (u_int32_t) dst_s_get_int16(&rdata[DST_EXT_FLAG]);
6700Sstevel@tonic-gate 		key_st->dk_flags = key_st->dk_flags | (ext_flags << 16);
6710Sstevel@tonic-gate 		start += 2;
6720Sstevel@tonic-gate 	}
6730Sstevel@tonic-gate 	/*
6740Sstevel@tonic-gate 	 * now point to the begining of the data representing the encoding
6750Sstevel@tonic-gate 	 * of the key
6760Sstevel@tonic-gate 	 */
6770Sstevel@tonic-gate 	if (key_st->dk_func && key_st->dk_func->from_dns_key) {
6780Sstevel@tonic-gate 		if (key_st->dk_func->from_dns_key(key_st, &rdata[start],
6790Sstevel@tonic-gate 						  len - start) > 0)
6800Sstevel@tonic-gate 			return (key_st);
6810Sstevel@tonic-gate 	} else
6820Sstevel@tonic-gate 		EREPORT(("dst_dnskey_to_public_key(): unsuppored alg %d\n",
6830Sstevel@tonic-gate 			 alg));
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	SAFE_FREE(key_st);
6860Sstevel@tonic-gate 	return (key_st);
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate 
689*11038SRao.Shoaib@Sun.COM /*%
6900Sstevel@tonic-gate  *  dst_public_key_to_dnskey
6910Sstevel@tonic-gate  *	Function to encode a public key into DNS KEY wire format
6920Sstevel@tonic-gate  *  Parameters
6930Sstevel@tonic-gate  *	key	     Key structure to encode.
6940Sstevel@tonic-gate  *	out_storage     Location to write the encoded key to.
6950Sstevel@tonic-gate  *	out_len	 Size of the output array.
6960Sstevel@tonic-gate  *  Returns
6970Sstevel@tonic-gate  *	<0      Failure
6980Sstevel@tonic-gate  *	>=0     Number of bytes written to out_storage
6990Sstevel@tonic-gate  */
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate int
dst_key_to_dnskey(const DST_KEY * key,u_char * out_storage,const int out_len)7020Sstevel@tonic-gate dst_key_to_dnskey(const DST_KEY *key, u_char *out_storage,
7030Sstevel@tonic-gate 			 const int out_len)
7040Sstevel@tonic-gate {
7050Sstevel@tonic-gate 	u_int16_t val;
7060Sstevel@tonic-gate 	int loc = 0;
7070Sstevel@tonic-gate 	int enc_len = 0;
7080Sstevel@tonic-gate 	if (key == NULL)
7090Sstevel@tonic-gate 		return (-1);
7100Sstevel@tonic-gate 
711*11038SRao.Shoaib@Sun.COM 	if (!dst_check_algorithm(key->dk_alg)) { /*%< make sure alg is available */
7120Sstevel@tonic-gate 		EREPORT(("dst_key_to_dnskey(): Algorithm %d not suppored\n",
7130Sstevel@tonic-gate 			 key->dk_alg));
7140Sstevel@tonic-gate 		return (UNSUPPORTED_KEYALG);
7150Sstevel@tonic-gate 	}
7160Sstevel@tonic-gate 	memset(out_storage, 0, out_len);
7170Sstevel@tonic-gate 	val = (u_int16_t)(key->dk_flags & 0xffff);
7180Sstevel@tonic-gate 	dst_s_put_int16(out_storage, val);
7190Sstevel@tonic-gate 	loc += 2;
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	out_storage[loc++] = (u_char) key->dk_proto;
7220Sstevel@tonic-gate 	out_storage[loc++] = (u_char) key->dk_alg;
7230Sstevel@tonic-gate 
724*11038SRao.Shoaib@Sun.COM 	if (key->dk_flags > 0xffff) {	/*%< Extended flags */
7250Sstevel@tonic-gate 		val = (u_int16_t)((key->dk_flags >> 16) & 0xffff);
7260Sstevel@tonic-gate 		dst_s_put_int16(&out_storage[loc], val);
7270Sstevel@tonic-gate 		loc += 2;
7280Sstevel@tonic-gate 	}
7290Sstevel@tonic-gate 	if (key->dk_KEY_struct == NULL)
7300Sstevel@tonic-gate 		return (loc);
7310Sstevel@tonic-gate 	if (key->dk_func && key->dk_func->to_dns_key) {
7320Sstevel@tonic-gate 		enc_len = key->dk_func->to_dns_key(key,
7330Sstevel@tonic-gate 						 (u_char *) &out_storage[loc],
7340Sstevel@tonic-gate 						   out_len - loc);
7350Sstevel@tonic-gate 		if (enc_len > 0)
7360Sstevel@tonic-gate 			return (enc_len + loc);
7370Sstevel@tonic-gate 		else
7380Sstevel@tonic-gate 			return (-1);
7390Sstevel@tonic-gate 	} else
7400Sstevel@tonic-gate 		EREPORT(("dst_key_to_dnskey(): Unsupported ALG %d\n",
7410Sstevel@tonic-gate 			 key->dk_alg));
7420Sstevel@tonic-gate 	return (-1);
7430Sstevel@tonic-gate }
7440Sstevel@tonic-gate 
745*11038SRao.Shoaib@Sun.COM /*%
7460Sstevel@tonic-gate  *  dst_buffer_to_key
7470Sstevel@tonic-gate  *	Function to encode a string of raw data into a DST key
7480Sstevel@tonic-gate  *  Parameters
7490Sstevel@tonic-gate  *	alg		The algorithm (HMAC only)
7500Sstevel@tonic-gate  *	key		A pointer to the data
7510Sstevel@tonic-gate  *	keylen		The length of the data
7520Sstevel@tonic-gate  *  Returns
7530Sstevel@tonic-gate  *	NULL	    an error occurred
7540Sstevel@tonic-gate  *	NON-NULL	the DST key
7550Sstevel@tonic-gate  */
7560Sstevel@tonic-gate DST_KEY *
dst_buffer_to_key(const char * key_name,const int alg,const int flags,const int protocol,const u_char * key_buf,const int key_len)757*11038SRao.Shoaib@Sun.COM dst_buffer_to_key(const char *key_name,		/*!< name of the key  */
758*11038SRao.Shoaib@Sun.COM 		  const int alg,		/*!< algorithm  */
759*11038SRao.Shoaib@Sun.COM 		  const int flags,		/*!< dns flags  */
760*11038SRao.Shoaib@Sun.COM 		  const int protocol,		/*!< dns protocol  */
761*11038SRao.Shoaib@Sun.COM 		  const u_char *key_buf,	/*!< key in dns wire fmt  */
762*11038SRao.Shoaib@Sun.COM 		  const int key_len)		/*!< size of key  */
7630Sstevel@tonic-gate {
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	DST_KEY *dkey = NULL;
7660Sstevel@tonic-gate 	int dnslen;
7670Sstevel@tonic-gate 	u_char dns[2048];
7680Sstevel@tonic-gate 
769*11038SRao.Shoaib@Sun.COM 	if (!dst_check_algorithm(alg)) { /*%< make sure alg is available */
7700Sstevel@tonic-gate 		EREPORT(("dst_buffer_to_key(): Algorithm %d not suppored\n", alg));
7710Sstevel@tonic-gate 		return (NULL);
7720Sstevel@tonic-gate 	}
7730Sstevel@tonic-gate 
774*11038SRao.Shoaib@Sun.COM 	dkey = dst_s_get_key_struct(key_name, alg, flags, protocol, -1);
7750Sstevel@tonic-gate 
776*11038SRao.Shoaib@Sun.COM 	if (dkey == NULL || dkey->dk_func == NULL ||
777*11038SRao.Shoaib@Sun.COM 	    dkey->dk_func->from_dns_key == NULL)
778*11038SRao.Shoaib@Sun.COM 		return (dst_free_key(dkey));
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	if (dkey->dk_func->from_dns_key(dkey, key_buf, key_len) < 0) {
7810Sstevel@tonic-gate 		EREPORT(("dst_buffer_to_key(): dst_buffer_to_hmac failed\n"));
7820Sstevel@tonic-gate 		return (dst_free_key(dkey));
7830Sstevel@tonic-gate 	}
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	dnslen = dst_key_to_dnskey(dkey, dns, sizeof(dns));
7860Sstevel@tonic-gate 	dkey->dk_id = dst_s_dns_key_id(dns, dnslen);
7870Sstevel@tonic-gate 	return (dkey);
7880Sstevel@tonic-gate }
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate int
dst_key_to_buffer(DST_KEY * key,u_char * out_buff,int buf_len)7910Sstevel@tonic-gate dst_key_to_buffer(DST_KEY *key, u_char *out_buff, int buf_len)
7920Sstevel@tonic-gate {
7930Sstevel@tonic-gate 	int len;
7940Sstevel@tonic-gate   /* this function will extrac the secret of HMAC into a buffer */
7950Sstevel@tonic-gate 	if (key == NULL)
7960Sstevel@tonic-gate 		return (0);
7970Sstevel@tonic-gate 	if (key->dk_func != NULL && key->dk_func->to_dns_key != NULL) {
7980Sstevel@tonic-gate 		len = key->dk_func->to_dns_key(key, out_buff, buf_len);
7990Sstevel@tonic-gate 		if (len < 0)
8000Sstevel@tonic-gate 			return (0);
8010Sstevel@tonic-gate 		return (len);
8020Sstevel@tonic-gate 	}
8030Sstevel@tonic-gate 	return (0);
8040Sstevel@tonic-gate }
8050Sstevel@tonic-gate 
806*11038SRao.Shoaib@Sun.COM /*%
8070Sstevel@tonic-gate  * dst_s_read_private_key_file
8080Sstevel@tonic-gate  *     Function reads in private key from a file.
8090Sstevel@tonic-gate  *     Fills out the KEY structure.
8100Sstevel@tonic-gate  * Parameters
8110Sstevel@tonic-gate  *     name    Name of the key to be read.
8120Sstevel@tonic-gate  *     pk_key  Structure that the key is returned in.
8130Sstevel@tonic-gate  *     in_id   Key identifier (tag)
8140Sstevel@tonic-gate  * Return
8150Sstevel@tonic-gate  *     1 if everthing works
8160Sstevel@tonic-gate  *     0 if there is any problem
8170Sstevel@tonic-gate  */
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate static int
dst_s_read_private_key_file(char * name,DST_KEY * pk_key,u_int16_t in_id,int in_alg)8200Sstevel@tonic-gate dst_s_read_private_key_file(char *name, DST_KEY *pk_key, u_int16_t in_id,
8210Sstevel@tonic-gate 			    int in_alg)
8220Sstevel@tonic-gate {
8230Sstevel@tonic-gate 	int cnt, alg, len, major, minor, file_major, file_minor;
8240Sstevel@tonic-gate 	int ret, id;
8250Sstevel@tonic-gate 	char filename[PATH_MAX];
8260Sstevel@tonic-gate 	u_char in_buff[RAW_KEY_SIZE], *p;
8270Sstevel@tonic-gate 	FILE *fp;
8280Sstevel@tonic-gate 	int dnslen;
8290Sstevel@tonic-gate 	u_char dns[2048];
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	if (name == NULL || pk_key == NULL) {
8320Sstevel@tonic-gate 		EREPORT(("dst_read_private_key_file(): No key name given\n"));
8330Sstevel@tonic-gate 		return (0);
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate 	/* Make the filename */
8360Sstevel@tonic-gate 	if (dst_s_build_filename(filename, name, in_id, in_alg, PRIVATE_KEY,
8370Sstevel@tonic-gate 				 PATH_MAX) == -1) {
8380Sstevel@tonic-gate 		EREPORT(("dst_read_private_key(): Cannot make filename from %s, %d, and %s\n",
8390Sstevel@tonic-gate 			 name, in_id, PRIVATE_KEY));
8400Sstevel@tonic-gate 		return (0);
8410Sstevel@tonic-gate 	}
8420Sstevel@tonic-gate 	/* first check if we can find the key file */
8430Sstevel@tonic-gate 	if ((fp = dst_s_fopen(filename, "r", 0)) == NULL) {
8440Sstevel@tonic-gate 		EREPORT(("dst_s_read_private_key_file: Could not open file %s in directory %s\n",
8450Sstevel@tonic-gate 			 filename, dst_path[0] ? dst_path :
8460Sstevel@tonic-gate 			 (char *) getcwd(NULL, PATH_MAX - 1)));
8470Sstevel@tonic-gate 		return (0);
8480Sstevel@tonic-gate 	}
8490Sstevel@tonic-gate 	/* now read the header info from the file */
8500Sstevel@tonic-gate 	if ((cnt = fread(in_buff, 1, sizeof(in_buff), fp)) < 5) {
8510Sstevel@tonic-gate 		fclose(fp);
8520Sstevel@tonic-gate 		EREPORT(("dst_s_read_private_key_file: error reading file %s (empty file)\n",
8530Sstevel@tonic-gate 			 filename));
8540Sstevel@tonic-gate 		return (0);
8550Sstevel@tonic-gate 	}
8560Sstevel@tonic-gate 	/* decrypt key */
8570Sstevel@tonic-gate 	fclose(fp);
8580Sstevel@tonic-gate 	if (memcmp(in_buff, "Private-key-format: v", 20) != 0)
8590Sstevel@tonic-gate 		goto fail;
8600Sstevel@tonic-gate 	len = cnt;
8610Sstevel@tonic-gate 	p = in_buff;
8620Sstevel@tonic-gate 
863*11038SRao.Shoaib@Sun.COM 	if (!dst_s_verify_str((const char **) (void *)&p,
864*11038SRao.Shoaib@Sun.COM 			       "Private-key-format: v")) {
8650Sstevel@tonic-gate 		EREPORT(("dst_s_read_private_key_file(): Not a Key file/Decrypt failed %s\n", name));
8660Sstevel@tonic-gate 		goto fail;
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate 	/* read in file format */
8690Sstevel@tonic-gate 	sscanf((char *)p, "%d.%d", &file_major, &file_minor);
8700Sstevel@tonic-gate 	sscanf(KEY_FILE_FORMAT, "%d.%d", &major, &minor);
8710Sstevel@tonic-gate 	if (file_major < 1) {
8720Sstevel@tonic-gate 		EREPORT(("dst_s_read_private_key_file(): Unknown keyfile %d.%d version for %s\n",
8730Sstevel@tonic-gate 			 file_major, file_minor, name));
8740Sstevel@tonic-gate 		goto fail;
8750Sstevel@tonic-gate 	} else if (file_major > major || file_minor > minor)
8760Sstevel@tonic-gate 		EREPORT((
8770Sstevel@tonic-gate 				"dst_s_read_private_key_file(): Keyfile %s version higher than mine %d.%d MAY FAIL\n",
8780Sstevel@tonic-gate 				name, file_major, file_minor));
8790Sstevel@tonic-gate 
880*11038SRao.Shoaib@Sun.COM 	while (*p++ != '\n') ;	/*%< skip to end of line */
8810Sstevel@tonic-gate 
882*11038SRao.Shoaib@Sun.COM 	if (!dst_s_verify_str((const char **) (void *)&p, "Algorithm: "))
8830Sstevel@tonic-gate 		goto fail;
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	if (sscanf((char *)p, "%d", &alg) != 1)
8860Sstevel@tonic-gate 		goto fail;
887*11038SRao.Shoaib@Sun.COM 	while (*p++ != '\n') ;	/*%< skip to end of line */
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	if (pk_key->dk_key_name && !strcmp(pk_key->dk_key_name, name))
8900Sstevel@tonic-gate 		SAFE_FREE2(pk_key->dk_key_name, strlen(pk_key->dk_key_name));
8910Sstevel@tonic-gate 	pk_key->dk_key_name = (char *) strdup(name);
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	/* allocate and fill in key structure */
8940Sstevel@tonic-gate 	if (pk_key->dk_func == NULL || pk_key->dk_func->from_file_fmt == NULL)
8950Sstevel@tonic-gate 		goto fail;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	ret = pk_key->dk_func->from_file_fmt(pk_key, (char *)p, &in_buff[len] - p);
8980Sstevel@tonic-gate 	if (ret < 0)
8990Sstevel@tonic-gate 		goto fail;
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	dnslen = dst_key_to_dnskey(pk_key, dns, sizeof(dns));
9020Sstevel@tonic-gate 	id = dst_s_dns_key_id(dns, dnslen);
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	/* Make sure the actual key tag matches the input tag used in the filename
9050Sstevel@tonic-gate 	 */
9060Sstevel@tonic-gate 	if (id != in_id) {
9070Sstevel@tonic-gate 		EREPORT(("dst_s_read_private_key_file(): actual tag of key read %d != input tag used to build filename %d.\n", id, in_id));
9080Sstevel@tonic-gate 		goto fail;
9090Sstevel@tonic-gate 	}
9100Sstevel@tonic-gate 	pk_key->dk_id = (u_int16_t) id;
9110Sstevel@tonic-gate 	pk_key->dk_alg = alg;
9120Sstevel@tonic-gate 	memset(in_buff, 0, cnt);
9130Sstevel@tonic-gate 	return (1);
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate  fail:
9160Sstevel@tonic-gate 	memset(in_buff, 0, cnt);
9170Sstevel@tonic-gate 	return (0);
9180Sstevel@tonic-gate }
9190Sstevel@tonic-gate 
920*11038SRao.Shoaib@Sun.COM /*%
9210Sstevel@tonic-gate  *	Generate and store a public/private keypair.
9220Sstevel@tonic-gate  *	Keys will be stored in formatted files.
923*11038SRao.Shoaib@Sun.COM  *
9240Sstevel@tonic-gate  *  Parameters
925*11038SRao.Shoaib@Sun.COM  &
926*11038SRao.Shoaib@Sun.COM  *\par	name    Name of the new key.  Used to create key files
927*11038SRao.Shoaib@Sun.COM  *\li		  K&lt;name&gt;+&lt;alg&gt;+&lt;id&gt;.public and K&lt;name&gt;+&lt;alg&gt;+&lt;id&gt;.private.
928*11038SRao.Shoaib@Sun.COM  *\par	bits    Size of the new key in bits.
929*11038SRao.Shoaib@Sun.COM  *\par	exp     What exponent to use:
930*11038SRao.Shoaib@Sun.COM  *\li		  0	   use exponent 3
931*11038SRao.Shoaib@Sun.COM  *\li		  non-zero    use Fermant4
932*11038SRao.Shoaib@Sun.COM  *\par	flags   The default value of the DNS Key flags.
933*11038SRao.Shoaib@Sun.COM  *\li		  The DNS Key RR Flag field is defined in RFC2065,
9340Sstevel@tonic-gate  *		  section 3.3.  The field has 16 bits.
935*11038SRao.Shoaib@Sun.COM  *\par	protocol
936*11038SRao.Shoaib@Sun.COM  *\li	      Default value of the DNS Key protocol field.
937*11038SRao.Shoaib@Sun.COM  *\li		  The DNS Key protocol field is defined in RFC2065,
9380Sstevel@tonic-gate  *		  section 3.4.  The field has 8 bits.
939*11038SRao.Shoaib@Sun.COM  *\par	alg     What algorithm to use.  Currently defined:
940*11038SRao.Shoaib@Sun.COM  *\li		  KEY_RSA       1
941*11038SRao.Shoaib@Sun.COM  *\li		  KEY_DSA       3
942*11038SRao.Shoaib@Sun.COM  *\li		  KEY_HMAC    157
943*11038SRao.Shoaib@Sun.COM  *\par	out_id The key tag is returned.
9440Sstevel@tonic-gate  *
9450Sstevel@tonic-gate  *  Return
946*11038SRao.Shoaib@Sun.COM  *\li	NULL		Failure
947*11038SRao.Shoaib@Sun.COM  *\li	non-NULL 	the generated key pair
9480Sstevel@tonic-gate  *			Caller frees the result, and its dk_name pointer.
9490Sstevel@tonic-gate  */
9500Sstevel@tonic-gate DST_KEY *
dst_generate_key(const char * name,const int bits,const int exp,const int flags,const int protocol,const int alg)9510Sstevel@tonic-gate dst_generate_key(const char *name, const int bits, const int exp,
9520Sstevel@tonic-gate 		 const int flags, const int protocol, const int alg)
9530Sstevel@tonic-gate {
9540Sstevel@tonic-gate 	DST_KEY *new_key = NULL;
9550Sstevel@tonic-gate 	int dnslen;
9560Sstevel@tonic-gate 	u_char dns[2048];
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	if (name == NULL)
9590Sstevel@tonic-gate 		return (NULL);
9600Sstevel@tonic-gate 
961*11038SRao.Shoaib@Sun.COM 	if (!dst_check_algorithm(alg)) { /*%< make sure alg is available */
9620Sstevel@tonic-gate 		EREPORT(("dst_generate_key(): Algorithm %d not suppored\n", alg));
9630Sstevel@tonic-gate 		return (NULL);
9640Sstevel@tonic-gate 	}
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	new_key = dst_s_get_key_struct(name, alg, flags, protocol, bits);
9670Sstevel@tonic-gate 	if (new_key == NULL)
9680Sstevel@tonic-gate 		return (NULL);
969*11038SRao.Shoaib@Sun.COM 	if (bits == 0) /*%< null key we are done */
9700Sstevel@tonic-gate 		return (new_key);
9710Sstevel@tonic-gate 	if (new_key->dk_func == NULL || new_key->dk_func->generate == NULL) {
9720Sstevel@tonic-gate 		EREPORT(("dst_generate_key_pair():Unsupported algorithm %d\n",
9730Sstevel@tonic-gate 			 alg));
9740Sstevel@tonic-gate 		return (dst_free_key(new_key));
9750Sstevel@tonic-gate 	}
976*11038SRao.Shoaib@Sun.COM 	if (new_key->dk_func->generate(new_key, exp) <= 0) {
9770Sstevel@tonic-gate 		EREPORT(("dst_generate_key_pair(): Key generation failure %s %d %d %d\n",
9780Sstevel@tonic-gate 			 new_key->dk_key_name, new_key->dk_alg,
9790Sstevel@tonic-gate 			 new_key->dk_key_size, exp));
9800Sstevel@tonic-gate 		return (dst_free_key(new_key));
9810Sstevel@tonic-gate 	}
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	dnslen = dst_key_to_dnskey(new_key, dns, sizeof(dns));
9840Sstevel@tonic-gate 	if (dnslen != UNSUPPORTED_KEYALG)
9850Sstevel@tonic-gate 		new_key->dk_id = dst_s_dns_key_id(dns, dnslen);
9860Sstevel@tonic-gate 	else
9870Sstevel@tonic-gate 		new_key->dk_id = 0;
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	return (new_key);
9900Sstevel@tonic-gate }
9910Sstevel@tonic-gate 
992*11038SRao.Shoaib@Sun.COM /*%
9930Sstevel@tonic-gate  *	Release all data structures pointed to by a key structure.
994*11038SRao.Shoaib@Sun.COM  *
9950Sstevel@tonic-gate  *  Parameters
996*11038SRao.Shoaib@Sun.COM  *\li	f_key   Key structure to be freed.
9970Sstevel@tonic-gate  */
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate DST_KEY *
dst_free_key(DST_KEY * f_key)10000Sstevel@tonic-gate dst_free_key(DST_KEY *f_key)
10010Sstevel@tonic-gate {
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	if (f_key == NULL)
10040Sstevel@tonic-gate 		return (f_key);
10050Sstevel@tonic-gate 	if (f_key->dk_func && f_key->dk_func->destroy)
10060Sstevel@tonic-gate 		f_key->dk_KEY_struct =
10070Sstevel@tonic-gate 			f_key->dk_func->destroy(f_key->dk_KEY_struct);
10080Sstevel@tonic-gate 	else {
10090Sstevel@tonic-gate 		EREPORT(("dst_free_key(): Unknown key alg %d\n",
10100Sstevel@tonic-gate 			 f_key->dk_alg));
10110Sstevel@tonic-gate 	}
10120Sstevel@tonic-gate 	if (f_key->dk_KEY_struct) {
10130Sstevel@tonic-gate 		free(f_key->dk_KEY_struct);
10140Sstevel@tonic-gate 		f_key->dk_KEY_struct = NULL;
10150Sstevel@tonic-gate 	}
10160Sstevel@tonic-gate 	if (f_key->dk_key_name)
10170Sstevel@tonic-gate 		SAFE_FREE(f_key->dk_key_name);
10180Sstevel@tonic-gate 	SAFE_FREE(f_key);
10190Sstevel@tonic-gate 	return (NULL);
10200Sstevel@tonic-gate }
10210Sstevel@tonic-gate 
1022*11038SRao.Shoaib@Sun.COM /*%
10230Sstevel@tonic-gate  *	Return the maximim size of signature from the key specified in bytes
1024*11038SRao.Shoaib@Sun.COM  *
10250Sstevel@tonic-gate  * Parameters
1026*11038SRao.Shoaib@Sun.COM  *\li      key
1027*11038SRao.Shoaib@Sun.COM  *
10280Sstevel@tonic-gate  * Returns
1029*11038SRao.Shoaib@Sun.COM  *  \li   bytes
10300Sstevel@tonic-gate  */
10310Sstevel@tonic-gate int
dst_sig_size(DST_KEY * key)10320Sstevel@tonic-gate dst_sig_size(DST_KEY *key) {
10330Sstevel@tonic-gate 	switch (key->dk_alg) {
10340Sstevel@tonic-gate 	    case KEY_HMAC_MD5:
10350Sstevel@tonic-gate 		return (16);
10360Sstevel@tonic-gate 	    case KEY_HMAC_SHA1:
10370Sstevel@tonic-gate 		return (20);
10380Sstevel@tonic-gate 	    case KEY_RSA:
10390Sstevel@tonic-gate 		return (key->dk_key_size + 7) / 8;
10400Sstevel@tonic-gate 	    case KEY_DSA:
10410Sstevel@tonic-gate 		return (40);
10420Sstevel@tonic-gate 	    default:
10430Sstevel@tonic-gate 		EREPORT(("dst_sig_size(): Unknown key alg %d\n", key->dk_alg));
10440Sstevel@tonic-gate 		return -1;
10450Sstevel@tonic-gate 	}
10460Sstevel@tonic-gate }
10470Sstevel@tonic-gate 
1048*11038SRao.Shoaib@Sun.COM /*! \file */
1049