Home | History | Annotate | Line # | Download | only in libresolv
dst_api.c revision 1.3.2.2
      1  1.3.2.2  christos /*	$NetBSD: dst_api.c,v 1.3.2.2 2012/11/16 02:16:39 christos Exp $	*/
      2  1.3.2.2  christos 
      3  1.3.2.2  christos /*
      4  1.3.2.2  christos  * Portions Copyright (c) 1995-1998 by Trusted Information Systems, Inc.
      5  1.3.2.2  christos  *
      6  1.3.2.2  christos  * Permission to use, copy modify, and distribute this software for any
      7  1.3.2.2  christos  * purpose with or without fee is hereby granted, provided that the above
      8  1.3.2.2  christos  * copyright notice and this permission notice appear in all copies.
      9  1.3.2.2  christos  *
     10  1.3.2.2  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND TRUSTED INFORMATION SYSTEMS
     11  1.3.2.2  christos  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
     12  1.3.2.2  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL
     13  1.3.2.2  christos  * TRUSTED INFORMATION SYSTEMS BE LIABLE FOR ANY SPECIAL, DIRECT,
     14  1.3.2.2  christos  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
     15  1.3.2.2  christos  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
     16  1.3.2.2  christos  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
     17  1.3.2.2  christos  * WITH THE USE OR PERFORMANCE OF THE SOFTWARE.
     18  1.3.2.2  christos  */
     19  1.3.2.2  christos /*
     20  1.3.2.2  christos  * This file contains the interface between the DST API and the crypto API.
     21  1.3.2.2  christos  * This is the only file that needs to be changed if the crypto system is
     22  1.3.2.2  christos  * changed.  Exported functions are:
     23  1.3.2.2  christos  * void dst_init()	 Initialize the toolkit
     24  1.3.2.2  christos  * int  dst_check_algorithm()   Function to determines if alg is suppored.
     25  1.3.2.2  christos  * int  dst_compare_keys()      Function to compare two keys for equality.
     26  1.3.2.2  christos  * int  dst_sign_data()         Incremental signing routine.
     27  1.3.2.2  christos  * int  dst_verify_data()       Incremental verify routine.
     28  1.3.2.2  christos  * int  dst_generate_key()      Function to generate new KEY
     29  1.3.2.2  christos  * DST_KEY *dst_read_key()      Function to retrieve private/public KEY.
     30  1.3.2.2  christos  * void dst_write_key()         Function to write out a key.
     31  1.3.2.2  christos  * DST_KEY *dst_dnskey_to_key() Function to convert DNS KEY RR to a DST
     32  1.3.2.2  christos  *				KEY structure.
     33  1.3.2.2  christos  * int dst_key_to_dnskey() 	Function to return a public key in DNS
     34  1.3.2.2  christos  *				format binary
     35  1.3.2.2  christos  * DST_KEY *dst_buffer_to_key() Converst a data in buffer to KEY
     36  1.3.2.2  christos  * int *dst_key_to_buffer()	Writes out DST_KEY key matterial in buffer
     37  1.3.2.2  christos  * void dst_free_key()       	Releases all memory referenced by key structure
     38  1.3.2.2  christos  */
     39  1.3.2.2  christos #include <sys/cdefs.h>
     40  1.3.2.2  christos #if 0
     41  1.3.2.2  christos static const char rcsid[] = "Header: /proj/cvs/prod/libbind/dst/dst_api.c,v 1.17 2007/09/24 17:18:25 each Exp ";
     42  1.3.2.2  christos #else
     43  1.3.2.2  christos __RCSID("$NetBSD: dst_api.c,v 1.3.2.2 2012/11/16 02:16:39 christos Exp $");
     44  1.3.2.2  christos #endif
     45  1.3.2.2  christos 
     46  1.3.2.2  christos 
     47  1.3.2.2  christos #include "port_before.h"
     48  1.3.2.2  christos #include <stdio.h>
     49  1.3.2.2  christos #include <errno.h>
     50  1.3.2.2  christos #include <fcntl.h>
     51  1.3.2.2  christos #include <stdlib.h>
     52  1.3.2.2  christos #include <unistd.h>
     53  1.3.2.2  christos #include <string.h>
     54  1.3.2.2  christos #include <memory.h>
     55  1.3.2.2  christos #include <ctype.h>
     56  1.3.2.2  christos #include <time.h>
     57  1.3.2.2  christos #include <sys/param.h>
     58  1.3.2.2  christos #include <sys/stat.h>
     59  1.3.2.2  christos #include <sys/socket.h>
     60  1.3.2.2  christos #include <netinet/in.h>
     61  1.3.2.2  christos #include <arpa/nameser.h>
     62  1.3.2.2  christos #include <resolv.h>
     63  1.3.2.2  christos 
     64  1.3.2.2  christos #include "dst_internal.h"
     65  1.3.2.2  christos #include "port_after.h"
     66  1.3.2.2  christos 
     67  1.3.2.2  christos /* static variables */
     68  1.3.2.2  christos static int done_init = 0;
     69  1.3.2.2  christos dst_func *dst_t_func[DST_MAX_ALGS];
     70  1.3.2.2  christos const char *dst_path = "";
     71  1.3.2.2  christos 
     72  1.3.2.2  christos /* internal I/O functions */
     73  1.3.2.2  christos static DST_KEY *dst_s_read_public_key(const char *in_name,
     74  1.3.2.2  christos 				      const u_int16_t in_id, int in_alg);
     75  1.3.2.2  christos static int dst_s_read_private_key_file(char *name, DST_KEY *pk_key,
     76  1.3.2.2  christos 				       u_int16_t in_id, int in_alg);
     77  1.3.2.2  christos static int dst_s_write_public_key(const DST_KEY *key);
     78  1.3.2.2  christos static int dst_s_write_private_key(const DST_KEY *key);
     79  1.3.2.2  christos 
     80  1.3.2.2  christos /* internal function to set up data structure */
     81  1.3.2.2  christos static DST_KEY *dst_s_get_key_struct(const char *name, const int alg,
     82  1.3.2.2  christos 				     const int flags, const int protocol,
     83  1.3.2.2  christos 				     const int bits);
     84  1.3.2.2  christos 
     85  1.3.2.2  christos /*%
     86  1.3.2.2  christos  *  dst_init
     87  1.3.2.2  christos  *	This function initializes the Digital Signature Toolkit.
     88  1.3.2.2  christos  *	Right now, it just checks the DSTKEYPATH environment variable.
     89  1.3.2.2  christos  *  Parameters
     90  1.3.2.2  christos  *	none
     91  1.3.2.2  christos  *  Returns
     92  1.3.2.2  christos  *	none
     93  1.3.2.2  christos  */
     94  1.3.2.2  christos void
     95  1.3.2.2  christos dst_init(void)
     96  1.3.2.2  christos {
     97  1.3.2.2  christos 	char *s;
     98  1.3.2.2  christos 	size_t len;
     99  1.3.2.2  christos 
    100  1.3.2.2  christos 	if (done_init != 0)
    101  1.3.2.2  christos 		return;
    102  1.3.2.2  christos 	done_init = 1;
    103  1.3.2.2  christos 
    104  1.3.2.2  christos 	s = getenv("DSTKEYPATH");
    105  1.3.2.2  christos 	len = 0;
    106  1.3.2.2  christos 	if (s) {
    107  1.3.2.2  christos 		struct stat statbuf;
    108  1.3.2.2  christos 
    109  1.3.2.2  christos 		len = strlen(s);
    110  1.3.2.2  christos 		if (len > PATH_MAX) {
    111  1.3.2.2  christos 			EREPORT(("%s: %s is longer than %d characters,"
    112  1.3.2.2  christos 			    " ignoring\n", __func__, s, PATH_MAX));
    113  1.3.2.2  christos 		} else if (stat(s, &statbuf) != 0 ||
    114  1.3.2.2  christos 		    !S_ISDIR(statbuf.st_mode)) {
    115  1.3.2.2  christos 			EREPORT(("%s: %s is not a valid directory\n",
    116  1.3.2.2  christos 			    __func__, s));
    117  1.3.2.2  christos 		} else {
    118  1.3.2.2  christos 			char *tmp;
    119  1.3.2.2  christos 			tmp = (char *) malloc(len + 2);
    120  1.3.2.2  christos 			memcpy(tmp, s, len + 1);
    121  1.3.2.2  christos 			if (tmp[strlen(tmp) - 1] != '/') {
    122  1.3.2.2  christos 				tmp[strlen(tmp) + 1] = 0;
    123  1.3.2.2  christos 				tmp[strlen(tmp)] = '/';
    124  1.3.2.2  christos 			}
    125  1.3.2.2  christos 			dst_path = tmp;
    126  1.3.2.2  christos 		}
    127  1.3.2.2  christos 	}
    128  1.3.2.2  christos 	memset(dst_t_func, 0, sizeof(dst_t_func));
    129  1.3.2.2  christos 	/* first one is selected */
    130  1.3.2.2  christos 	dst_hmac_md5_init();
    131  1.3.2.2  christos }
    132  1.3.2.2  christos 
    133  1.3.2.2  christos /*%
    134  1.3.2.2  christos  *  dst_check_algorithm
    135  1.3.2.2  christos  *	This function determines if the crypto system for the specified
    136  1.3.2.2  christos  *	algorithm is present.
    137  1.3.2.2  christos  *  Parameters
    138  1.3.2.2  christos  *	alg     1       KEY_RSA
    139  1.3.2.2  christos  *		3       KEY_DSA
    140  1.3.2.2  christos  *	      157     KEY_HMAC_MD5
    141  1.3.2.2  christos  *		      future algorithms TBD and registered with IANA.
    142  1.3.2.2  christos  *  Returns
    143  1.3.2.2  christos  *	1 - The algorithm is available.
    144  1.3.2.2  christos  *	0 - The algorithm is not available.
    145  1.3.2.2  christos  */
    146  1.3.2.2  christos int
    147  1.3.2.2  christos dst_check_algorithm(const int alg)
    148  1.3.2.2  christos {
    149  1.3.2.2  christos 	return (dst_t_func[alg] != NULL);
    150  1.3.2.2  christos }
    151  1.3.2.2  christos 
    152  1.3.2.2  christos /*%
    153  1.3.2.2  christos  * dst_s_get_key_struct
    154  1.3.2.2  christos  *	This function allocates key structure and fills in some of the
    155  1.3.2.2  christos  *	fields of the structure.
    156  1.3.2.2  christos  * Parameters:
    157  1.3.2.2  christos  *	name:     the name of the key
    158  1.3.2.2  christos  *	alg:      the algorithm number
    159  1.3.2.2  christos  *	flags:    the dns flags of the key
    160  1.3.2.2  christos  *	protocol: the dns protocol of the key
    161  1.3.2.2  christos  *	bits:     the size of the key
    162  1.3.2.2  christos  * Returns:
    163  1.3.2.2  christos  *       NULL if error
    164  1.3.2.2  christos  *       valid pointer otherwise
    165  1.3.2.2  christos  */
    166  1.3.2.2  christos static DST_KEY *
    167  1.3.2.2  christos dst_s_get_key_struct(const char *name, const int alg, const int flags,
    168  1.3.2.2  christos 		     const int protocol, const int bits)
    169  1.3.2.2  christos {
    170  1.3.2.2  christos 	DST_KEY *new_key = NULL;
    171  1.3.2.2  christos 
    172  1.3.2.2  christos 	if (dst_check_algorithm(alg)) /*%< make sure alg is available */
    173  1.3.2.2  christos 		new_key = (DST_KEY *) malloc(sizeof(*new_key));
    174  1.3.2.2  christos 	if (new_key == NULL)
    175  1.3.2.2  christos 		return (NULL);
    176  1.3.2.2  christos 
    177  1.3.2.2  christos 	memset(new_key, 0, sizeof(*new_key));
    178  1.3.2.2  christos 	new_key->dk_key_name = strdup(name);
    179  1.3.2.2  christos 	if (new_key->dk_key_name == NULL) {
    180  1.3.2.2  christos 		free(new_key);
    181  1.3.2.2  christos 		return (NULL);
    182  1.3.2.2  christos 	}
    183  1.3.2.2  christos 	new_key->dk_alg = alg;
    184  1.3.2.2  christos 	new_key->dk_flags = flags;
    185  1.3.2.2  christos 	new_key->dk_proto = protocol;
    186  1.3.2.2  christos 	new_key->dk_KEY_struct = NULL;
    187  1.3.2.2  christos 	new_key->dk_key_size = bits;
    188  1.3.2.2  christos 	new_key->dk_func = dst_t_func[alg];
    189  1.3.2.2  christos 	return (new_key);
    190  1.3.2.2  christos }
    191  1.3.2.2  christos 
    192  1.3.2.2  christos /*%
    193  1.3.2.2  christos  *  dst_compare_keys
    194  1.3.2.2  christos  *	Compares two keys for equality.
    195  1.3.2.2  christos  *  Parameters
    196  1.3.2.2  christos  *	key1, key2      Two keys to be compared.
    197  1.3.2.2  christos  *  Returns
    198  1.3.2.2  christos  *	0	       The keys are equal.
    199  1.3.2.2  christos  *	non-zero	The keys are not equal.
    200  1.3.2.2  christos  */
    201  1.3.2.2  christos 
    202  1.3.2.2  christos int
    203  1.3.2.2  christos dst_compare_keys(const DST_KEY *key1, const DST_KEY *key2)
    204  1.3.2.2  christos {
    205  1.3.2.2  christos 	if (key1 == key2)
    206  1.3.2.2  christos 		return (0);
    207  1.3.2.2  christos 	if (key1 == NULL || key2 == NULL)
    208  1.3.2.2  christos 		return (4);
    209  1.3.2.2  christos 	if (key1->dk_alg != key2->dk_alg)
    210  1.3.2.2  christos 		return (1);
    211  1.3.2.2  christos 	if (key1->dk_key_size != key2->dk_key_size)
    212  1.3.2.2  christos 		return (2);
    213  1.3.2.2  christos 	if (key1->dk_id != key2->dk_id)
    214  1.3.2.2  christos 		return (3);
    215  1.3.2.2  christos 	return (key1->dk_func->compare(key1, key2));
    216  1.3.2.2  christos }
    217  1.3.2.2  christos 
    218  1.3.2.2  christos /*%
    219  1.3.2.2  christos  * dst_sign_data
    220  1.3.2.2  christos  *	An incremental signing function.  Data is signed in steps.
    221  1.3.2.2  christos  *	First the context must be initialized (SIG_MODE_INIT).
    222  1.3.2.2  christos  *	Then data is hashed (SIG_MODE_UPDATE).  Finally the signature
    223  1.3.2.2  christos  *	itself is created (SIG_MODE_FINAL).  This function can be called
    224  1.3.2.2  christos  *	once with INIT, UPDATE and FINAL modes all set, or it can be
    225  1.3.2.2  christos  *	called separately with a different mode set for each step.  The
    226  1.3.2.2  christos  *	UPDATE step can be repeated.
    227  1.3.2.2  christos  * Parameters
    228  1.3.2.2  christos  *	mode    A bit mask used to specify operation(s) to be performed.
    229  1.3.2.2  christos  *		  SIG_MODE_INIT	   1   Initialize digest
    230  1.3.2.2  christos  *		  SIG_MODE_UPDATE	 2   Add data to digest
    231  1.3.2.2  christos  *		  SIG_MODE_FINAL	  4   Generate signature
    232  1.3.2.2  christos  *					      from signature
    233  1.3.2.2  christos  *		  SIG_MODE_ALL (SIG_MODE_INIT,SIG_MODE_UPDATE,SIG_MODE_FINAL
    234  1.3.2.2  christos  *	data    Data to be signed.
    235  1.3.2.2  christos  *	len     The length in bytes of data to be signed.
    236  1.3.2.2  christos  *	in_key  Contains a private key to sign with.
    237  1.3.2.2  christos  *		  KEY structures should be handled (created, converted,
    238  1.3.2.2  christos  *		  compared, stored, freed) by the DST.
    239  1.3.2.2  christos  *	signature
    240  1.3.2.2  christos  *	      The location to which the signature will be written.
    241  1.3.2.2  christos  *	sig_len Length of the signature field in bytes.
    242  1.3.2.2  christos  * Return
    243  1.3.2.2  christos  *	 0      Successfull INIT or Update operation
    244  1.3.2.2  christos  *	&gt;0      success FINAL (sign) operation
    245  1.3.2.2  christos  *	&lt;0      failure
    246  1.3.2.2  christos  */
    247  1.3.2.2  christos 
    248  1.3.2.2  christos int
    249  1.3.2.2  christos dst_sign_data(const int mode, DST_KEY *in_key, void **context,
    250  1.3.2.2  christos 	      const u_char *data, const int len,
    251  1.3.2.2  christos 	      u_char *signature, const int sig_len)
    252  1.3.2.2  christos {
    253  1.3.2.2  christos 	DUMP(data, mode, len, "dst_sign_data()");
    254  1.3.2.2  christos 
    255  1.3.2.2  christos 	if (mode & SIG_MODE_FINAL &&
    256  1.3.2.2  christos 	    (in_key->dk_KEY_struct == NULL || signature == NULL))
    257  1.3.2.2  christos 		return (MISSING_KEY_OR_SIGNATURE);
    258  1.3.2.2  christos 
    259  1.3.2.2  christos 	if (in_key->dk_func && in_key->dk_func->sign)
    260  1.3.2.2  christos 		return (in_key->dk_func->sign(mode, in_key, context, data, len,
    261  1.3.2.2  christos 					      signature, sig_len));
    262  1.3.2.2  christos 	return (UNKNOWN_KEYALG);
    263  1.3.2.2  christos }
    264  1.3.2.2  christos 
    265  1.3.2.2  christos /*%
    266  1.3.2.2  christos  *  dst_verify_data
    267  1.3.2.2  christos  *	An incremental verify function.  Data is verified in steps.
    268  1.3.2.2  christos  *	First the context must be initialized (SIG_MODE_INIT).
    269  1.3.2.2  christos  *	Then data is hashed (SIG_MODE_UPDATE).  Finally the signature
    270  1.3.2.2  christos  *	is verified (SIG_MODE_FINAL).  This function can be called
    271  1.3.2.2  christos  *	once with INIT, UPDATE and FINAL modes all set, or it can be
    272  1.3.2.2  christos  *	called separately with a different mode set for each step.  The
    273  1.3.2.2  christos  *	UPDATE step can be repeated.
    274  1.3.2.2  christos  *  Parameters
    275  1.3.2.2  christos  *	mode	Operations to perform this time.
    276  1.3.2.2  christos  *		      SIG_MODE_INIT       1   Initialize digest
    277  1.3.2.2  christos  *		      SIG_MODE_UPDATE     2   add data to digest
    278  1.3.2.2  christos  *		      SIG_MODE_FINAL      4   verify signature
    279  1.3.2.2  christos  *		      SIG_MODE_ALL
    280  1.3.2.2  christos  *			  (SIG_MODE_INIT,SIG_MODE_UPDATE,SIG_MODE_FINAL)
    281  1.3.2.2  christos  *	data	Data to pass through the hash function.
    282  1.3.2.2  christos  *	len	 Length of the data in bytes.
    283  1.3.2.2  christos  *	in_key      Key for verification.
    284  1.3.2.2  christos  *	signature   Location of signature.
    285  1.3.2.2  christos  *	sig_len     Length of the signature in bytes.
    286  1.3.2.2  christos  *  Returns
    287  1.3.2.2  christos  *	0	   Verify success
    288  1.3.2.2  christos  *	Non-Zero    Verify Failure
    289  1.3.2.2  christos  */
    290  1.3.2.2  christos 
    291  1.3.2.2  christos int
    292  1.3.2.2  christos dst_verify_data(const int mode, DST_KEY *in_key, void **context,
    293  1.3.2.2  christos 		const u_char *data, const int len,
    294  1.3.2.2  christos 		const u_char *signature, const int sig_len)
    295  1.3.2.2  christos {
    296  1.3.2.2  christos 	DUMP(data, mode, len, "dst_verify_data()");
    297  1.3.2.2  christos 	if (mode & SIG_MODE_FINAL &&
    298  1.3.2.2  christos 	    (in_key->dk_KEY_struct == NULL || signature == NULL))
    299  1.3.2.2  christos 		return (MISSING_KEY_OR_SIGNATURE);
    300  1.3.2.2  christos 
    301  1.3.2.2  christos 	if (in_key->dk_func == NULL || in_key->dk_func->verify == NULL)
    302  1.3.2.2  christos 		return (UNSUPPORTED_KEYALG);
    303  1.3.2.2  christos 	return (in_key->dk_func->verify(mode, in_key, context, data, len,
    304  1.3.2.2  christos 					signature, sig_len));
    305  1.3.2.2  christos }
    306  1.3.2.2  christos 
    307  1.3.2.2  christos /*%
    308  1.3.2.2  christos  *  dst_read_private_key
    309  1.3.2.2  christos  *	Access a private key.  First the list of private keys that have
    310  1.3.2.2  christos  *	already been read in is searched, then the key accessed on disk.
    311  1.3.2.2  christos  *	If the private key can be found, it is returned.  If the key cannot
    312  1.3.2.2  christos  *	be found, a null pointer is returned.  The options specify required
    313  1.3.2.2  christos  *	key characteristics.  If the private key requested does not have
    314  1.3.2.2  christos  *	these characteristics, it will not be read.
    315  1.3.2.2  christos  *  Parameters
    316  1.3.2.2  christos  *	in_keyname  The private key name.
    317  1.3.2.2  christos  *	in_id	    The id of the private key.
    318  1.3.2.2  christos  *	options     DST_FORCE_READ  Read from disk - don't use a previously
    319  1.3.2.2  christos  *				      read key.
    320  1.3.2.2  christos  *		  DST_CAN_SIGN    The key must be useable for signing.
    321  1.3.2.2  christos  *		  DST_NO_AUTHEN   The key must be useable for authentication.
    322  1.3.2.2  christos  *		  DST_STANDARD    Return any key
    323  1.3.2.2  christos  *  Returns
    324  1.3.2.2  christos  *	NULL	If there is no key found in the current directory or
    325  1.3.2.2  christos  *		      this key has not been loaded before.
    326  1.3.2.2  christos  *	!NULL       Success - KEY structure returned.
    327  1.3.2.2  christos  */
    328  1.3.2.2  christos 
    329  1.3.2.2  christos DST_KEY *
    330  1.3.2.2  christos dst_read_key(const char *in_keyname, const u_int16_t in_id,
    331  1.3.2.2  christos 	     const int in_alg, const int type)
    332  1.3.2.2  christos {
    333  1.3.2.2  christos 	char keyname[PATH_MAX];
    334  1.3.2.2  christos 	DST_KEY *dg_key = NULL, *pubkey = NULL;
    335  1.3.2.2  christos 
    336  1.3.2.2  christos 	if (!dst_check_algorithm(in_alg)) { /*%< make sure alg is available */
    337  1.3.2.2  christos 		EREPORT(("%s: Algorithm %d not suppored\n", __func__, in_alg));
    338  1.3.2.2  christos 		return (NULL);
    339  1.3.2.2  christos 	}
    340  1.3.2.2  christos 	if ((type & (DST_PUBLIC | DST_PRIVATE)) == 0)
    341  1.3.2.2  christos 		return (NULL);
    342  1.3.2.2  christos 	if (in_keyname == NULL) {
    343  1.3.2.2  christos 		EREPORT(("%s: Null key name passed in\n", __func__));
    344  1.3.2.2  christos 		return (NULL);
    345  1.3.2.2  christos 	} else if (strlen(in_keyname) >= sizeof(keyname)) {
    346  1.3.2.2  christos 		EREPORT(("%s: keyname too big\n", __func__));
    347  1.3.2.2  christos 		return (NULL);
    348  1.3.2.2  christos 	} else
    349  1.3.2.2  christos 		strcpy(keyname, in_keyname);
    350  1.3.2.2  christos 
    351  1.3.2.2  christos 	/* before I read in the public key, check if it is allowed to sign */
    352  1.3.2.2  christos 	if ((pubkey = dst_s_read_public_key(keyname, in_id, in_alg)) == NULL)
    353  1.3.2.2  christos 		return (NULL);
    354  1.3.2.2  christos 
    355  1.3.2.2  christos 	if (type == DST_PUBLIC)
    356  1.3.2.2  christos 		return pubkey;
    357  1.3.2.2  christos 
    358  1.3.2.2  christos 	if (!(dg_key = dst_s_get_key_struct(keyname, pubkey->dk_alg,
    359  1.3.2.2  christos 					    (int)pubkey->dk_flags,
    360  1.3.2.2  christos 					    pubkey->dk_proto, 0)))
    361  1.3.2.2  christos 		return (dg_key);
    362  1.3.2.2  christos 	/* Fill in private key and some fields in the general key structure */
    363  1.3.2.2  christos 	if (dst_s_read_private_key_file(keyname, dg_key, pubkey->dk_id,
    364  1.3.2.2  christos 					pubkey->dk_alg) == 0)
    365  1.3.2.2  christos 		dg_key = dst_free_key(dg_key);
    366  1.3.2.2  christos 
    367  1.3.2.2  christos 	(void)dst_free_key(pubkey);
    368  1.3.2.2  christos 	return (dg_key);
    369  1.3.2.2  christos }
    370  1.3.2.2  christos 
    371  1.3.2.2  christos int
    372  1.3.2.2  christos dst_write_key(const DST_KEY *key, const int type)
    373  1.3.2.2  christos {
    374  1.3.2.2  christos 	int pub = 0, priv = 0;
    375  1.3.2.2  christos 
    376  1.3.2.2  christos 	if (key == NULL)
    377  1.3.2.2  christos 		return (0);
    378  1.3.2.2  christos 	if (!dst_check_algorithm(key->dk_alg)) { /*%< make sure alg is available */
    379  1.3.2.2  christos 		EREPORT(("%s: Algorithm %d not suppored\n", __func__,
    380  1.3.2.2  christos 		    key->dk_alg));
    381  1.3.2.2  christos 		return (UNSUPPORTED_KEYALG);
    382  1.3.2.2  christos 	}
    383  1.3.2.2  christos 	if ((type & (DST_PRIVATE|DST_PUBLIC)) == 0)
    384  1.3.2.2  christos 		return (0);
    385  1.3.2.2  christos 
    386  1.3.2.2  christos 	if (type & DST_PUBLIC)
    387  1.3.2.2  christos 		if ((pub = dst_s_write_public_key(key)) < 0)
    388  1.3.2.2  christos 			return (pub);
    389  1.3.2.2  christos 	if (type & DST_PRIVATE)
    390  1.3.2.2  christos 		if ((priv = dst_s_write_private_key(key)) < 0)
    391  1.3.2.2  christos 			return (priv);
    392  1.3.2.2  christos 	return (priv+pub);
    393  1.3.2.2  christos }
    394  1.3.2.2  christos 
    395  1.3.2.2  christos /*%
    396  1.3.2.2  christos  *  dst_write_private_key
    397  1.3.2.2  christos  *	Write a private key to disk.  The filename will be of the form:
    398  1.3.2.2  christos  *	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;.
    399  1.3.2.2  christos  *	If there is already a file with this name, an error is returned.
    400  1.3.2.2  christos  *
    401  1.3.2.2  christos  *  Parameters
    402  1.3.2.2  christos  *	key     A DST managed key structure that contains
    403  1.3.2.2  christos  *	      all information needed about a key.
    404  1.3.2.2  christos  *  Return
    405  1.3.2.2  christos  *	&gt;= 0    Correct behavior.  Returns length of encoded key value
    406  1.3.2.2  christos  *		  written to disk.
    407  1.3.2.2  christos  *	&lt;  0    error.
    408  1.3.2.2  christos  */
    409  1.3.2.2  christos 
    410  1.3.2.2  christos static int
    411  1.3.2.2  christos dst_s_write_private_key(const DST_KEY *key)
    412  1.3.2.2  christos {
    413  1.3.2.2  christos 	u_char encoded_block[RAW_KEY_SIZE];
    414  1.3.2.2  christos 	char file[PATH_MAX];
    415  1.3.2.2  christos 	int len;
    416  1.3.2.2  christos 	FILE *fp;
    417  1.3.2.2  christos 
    418  1.3.2.2  christos 	/* First encode the key into the portable key format */
    419  1.3.2.2  christos 	if (key == NULL)
    420  1.3.2.2  christos 		return (-1);
    421  1.3.2.2  christos 	if (key->dk_KEY_struct == NULL)
    422  1.3.2.2  christos 		return (0);	/*%< null key has no private key */
    423  1.3.2.2  christos 	if (key->dk_func == NULL || key->dk_func->to_file_fmt == NULL) {
    424  1.3.2.2  christos 		EREPORT(("%s: Unsupported operation %d\n", __func__,
    425  1.3.2.2  christos 		    key->dk_alg));
    426  1.3.2.2  christos 		return (-5);
    427  1.3.2.2  christos 	} else if ((len = key->dk_func->to_file_fmt(key, (char *)encoded_block,
    428  1.3.2.2  christos 					 (int)sizeof(encoded_block))) <= 0) {
    429  1.3.2.2  christos 		EREPORT(("%s: Failed encoding private RSA bsafe key %d\n",
    430  1.3.2.2  christos 		    __func__, len));
    431  1.3.2.2  christos 		return (-8);
    432  1.3.2.2  christos 	}
    433  1.3.2.2  christos 	/* Now I can create the file I want to use */
    434  1.3.2.2  christos 	dst_s_build_filename(file, key->dk_key_name, key->dk_id, key->dk_alg,
    435  1.3.2.2  christos 			     PRIVATE_KEY, PATH_MAX);
    436  1.3.2.2  christos 
    437  1.3.2.2  christos 	/* Do not overwrite an existing file */
    438  1.3.2.2  christos 	if ((fp = dst_s_fopen(file, "w", 0600)) != NULL) {
    439  1.3.2.2  christos 		ssize_t nn;
    440  1.3.2.2  christos 		nn = fwrite(encoded_block, 1, len, fp);
    441  1.3.2.2  christos 		if (nn != len) {
    442  1.3.2.2  christos 			EREPORT(("%s: Write failure on %s %d != %zd"
    443  1.3.2.2  christos 			    " errno=%d\n", __func__, file, len, nn, errno));
    444  1.3.2.2  christos 
    445  1.3.2.2  christos 			fclose(fp);
    446  1.3.2.2  christos 			return (-5);
    447  1.3.2.2  christos 		}
    448  1.3.2.2  christos 		fclose(fp);
    449  1.3.2.2  christos 	} else {
    450  1.3.2.2  christos 		EREPORT(("%s: Can not create file %s\n", __func__,
    451  1.3.2.2  christos 		    file));
    452  1.3.2.2  christos 		return (-6);
    453  1.3.2.2  christos 	}
    454  1.3.2.2  christos 	memset(encoded_block, 0, len);
    455  1.3.2.2  christos 	return (len);
    456  1.3.2.2  christos }
    457  1.3.2.2  christos 
    458  1.3.2.2  christos /*%
    459  1.3.2.2  christos *
    460  1.3.2.2  christos  *  dst_read_public_key
    461  1.3.2.2  christos  *	Read a public key from disk and store in a DST key structure.
    462  1.3.2.2  christos  *  Parameters
    463  1.3.2.2  christos  *	in_name	 K&lt;in_name&gt;&lt;in_id&gt;.&lt;public key suffix&gt; is the
    464  1.3.2.2  christos  *		      filename of the key file to be read.
    465  1.3.2.2  christos  *  Returns
    466  1.3.2.2  christos  *	NULL	    If the key does not exist or no name is supplied.
    467  1.3.2.2  christos  *	NON-NULL	Initialized key structure if the key exists.
    468  1.3.2.2  christos  */
    469  1.3.2.2  christos 
    470  1.3.2.2  christos static DST_KEY *
    471  1.3.2.2  christos dst_s_read_public_key(const char *in_name, const u_int16_t in_id, int in_alg)
    472  1.3.2.2  christos {
    473  1.3.2.2  christos 	int flags, proto, alg, dlen;
    474  1.3.2.2  christos 	size_t len;
    475  1.3.2.2  christos 	int c;
    476  1.3.2.2  christos 	char name[PATH_MAX], enckey[RAW_KEY_SIZE], *notspace;
    477  1.3.2.2  christos 	u_char deckey[RAW_KEY_SIZE];
    478  1.3.2.2  christos 	FILE *fp;
    479  1.3.2.2  christos 
    480  1.3.2.2  christos 	if (in_name == NULL) {
    481  1.3.2.2  christos 		EREPORT(("%s: No key name given\n", __func__));
    482  1.3.2.2  christos 		return (NULL);
    483  1.3.2.2  christos 	}
    484  1.3.2.2  christos 	if (dst_s_build_filename(name, in_name, in_id, in_alg, PUBLIC_KEY,
    485  1.3.2.2  christos 				 PATH_MAX) == -1) {
    486  1.3.2.2  christos 		EREPORT(("%s: Cannot make filename from %s, %d, and %s\n",
    487  1.3.2.2  christos 		    __func__, in_name, in_id, PUBLIC_KEY));
    488  1.3.2.2  christos 		return (NULL);
    489  1.3.2.2  christos 	}
    490  1.3.2.2  christos 	/*
    491  1.3.2.2  christos 	 * Open the file and read it's formatted contents up to key
    492  1.3.2.2  christos 	 * File format:
    493  1.3.2.2  christos 	 *    domain.name [ttl] [IN] KEY  &lt;flags&gt; &lt;protocol&gt; &lt;algorithm&gt; &lt;key&gt;
    494  1.3.2.2  christos 	 * flags, proto, alg stored as decimal (or hex numbers FIXME).
    495  1.3.2.2  christos 	 * (FIXME: handle parentheses for line continuation.)
    496  1.3.2.2  christos 	 */
    497  1.3.2.2  christos 	if ((fp = dst_s_fopen(name, "r", 0)) == NULL) {
    498  1.3.2.2  christos 		EREPORT(("%s: Public Key not found %s\n", __func__, name));
    499  1.3.2.2  christos 		return (NULL);
    500  1.3.2.2  christos 	}
    501  1.3.2.2  christos 	/* Skip domain name, which ends at first blank */
    502  1.3.2.2  christos 	while ((c = getc(fp)) != EOF)
    503  1.3.2.2  christos 		if (isspace(c))
    504  1.3.2.2  christos 			break;
    505  1.3.2.2  christos 	/* Skip blank to get to next field */
    506  1.3.2.2  christos 	while ((c = getc(fp)) != EOF)
    507  1.3.2.2  christos 		if (!isspace(c))
    508  1.3.2.2  christos 			break;
    509  1.3.2.2  christos 
    510  1.3.2.2  christos 	/* Skip optional TTL -- if initial digit, skip whole word. */
    511  1.3.2.2  christos 	if (isdigit(c)) {
    512  1.3.2.2  christos 		while ((c = getc(fp)) != EOF)
    513  1.3.2.2  christos 			if (isspace(c))
    514  1.3.2.2  christos 				break;
    515  1.3.2.2  christos 		while ((c = getc(fp)) != EOF)
    516  1.3.2.2  christos 			if (!isspace(c))
    517  1.3.2.2  christos 				break;
    518  1.3.2.2  christos 	}
    519  1.3.2.2  christos 	/* Skip optional "IN" */
    520  1.3.2.2  christos 	if (c == 'I' || c == 'i') {
    521  1.3.2.2  christos 		while ((c = getc(fp)) != EOF)
    522  1.3.2.2  christos 			if (isspace(c))
    523  1.3.2.2  christos 				break;
    524  1.3.2.2  christos 		while ((c = getc(fp)) != EOF)
    525  1.3.2.2  christos 			if (!isspace(c))
    526  1.3.2.2  christos 				break;
    527  1.3.2.2  christos 	}
    528  1.3.2.2  christos 	/* Locate and skip "KEY" */
    529  1.3.2.2  christos 	if (c != 'K' && c != 'k') {
    530  1.3.2.2  christos 		EREPORT(("%s: \"KEY\" doesn't appear in file: %s", __func__,
    531  1.3.2.2  christos 		    name));
    532  1.3.2.2  christos 		return NULL;
    533  1.3.2.2  christos 	}
    534  1.3.2.2  christos 	while ((c = getc(fp)) != EOF)
    535  1.3.2.2  christos 		if (isspace(c))
    536  1.3.2.2  christos 			break;
    537  1.3.2.2  christos 	while ((c = getc(fp)) != EOF)
    538  1.3.2.2  christos 		if (!isspace(c))
    539  1.3.2.2  christos 			break;
    540  1.3.2.2  christos 	ungetc(c, fp);		/*%< return the charcter to the input field */
    541  1.3.2.2  christos 	/* Handle hex!! FIXME.  */
    542  1.3.2.2  christos 
    543  1.3.2.2  christos 	if (fscanf(fp, "%d %d %d", &flags, &proto, &alg) != 3) {
    544  1.3.2.2  christos 		EREPORT(("%s: Can not read flag/proto/alg field from %s\n",
    545  1.3.2.2  christos 		    __func__, name));
    546  1.3.2.2  christos 		return (NULL);
    547  1.3.2.2  christos 	}
    548  1.3.2.2  christos 	/* read in the key string */
    549  1.3.2.2  christos 	fgets(enckey, (int)sizeof(enckey), fp);
    550  1.3.2.2  christos 
    551  1.3.2.2  christos 	/* If we aren't at end-of-file, something is wrong.  */
    552  1.3.2.2  christos 	while ((c = getc(fp)) != EOF)
    553  1.3.2.2  christos 		if (!isspace(c))
    554  1.3.2.2  christos 			break;
    555  1.3.2.2  christos 	if (!feof(fp)) {
    556  1.3.2.2  christos 		EREPORT(("%s: Key too long in file: %s", __func__, name));
    557  1.3.2.2  christos 		return NULL;
    558  1.3.2.2  christos 	}
    559  1.3.2.2  christos 	fclose(fp);
    560  1.3.2.2  christos 
    561  1.3.2.2  christos 	if ((len = strlen(enckey)) == 0)
    562  1.3.2.2  christos 		return (NULL);
    563  1.3.2.2  christos 
    564  1.3.2.2  christos 	/* discard \n */
    565  1.3.2.2  christos 	enckey[--len] = '\0';
    566  1.3.2.2  christos 
    567  1.3.2.2  christos 	/* remove leading spaces */
    568  1.3.2.2  christos 	for (notspace = (char *) enckey; isspace((*notspace)&0xff); len--)
    569  1.3.2.2  christos 		notspace++;
    570  1.3.2.2  christos 
    571  1.3.2.2  christos 	dlen = b64_pton(notspace, deckey, sizeof(deckey));
    572  1.3.2.2  christos 	if (dlen < 0) {
    573  1.3.2.2  christos 		EREPORT(("%s: bad return from b64_pton = %d", __func__, dlen));
    574  1.3.2.2  christos 		return (NULL);
    575  1.3.2.2  christos 	}
    576  1.3.2.2  christos 	/* store key and info in a key structure that is returned */
    577  1.3.2.2  christos /*	return dst_store_public_key(in_name, alg, proto, 666, flags, deckey,
    578  1.3.2.2  christos 				    dlen);*/
    579  1.3.2.2  christos 	return dst_buffer_to_key(in_name, alg, flags, proto, deckey, dlen);
    580  1.3.2.2  christos }
    581  1.3.2.2  christos 
    582  1.3.2.2  christos /*%
    583  1.3.2.2  christos  *  dst_write_public_key
    584  1.3.2.2  christos  *	Write a key to disk in DNS format.
    585  1.3.2.2  christos  *  Parameters
    586  1.3.2.2  christos  *	key     Pointer to a DST key structure.
    587  1.3.2.2  christos  *  Returns
    588  1.3.2.2  christos  *	0       Failure
    589  1.3.2.2  christos  *	1       Success
    590  1.3.2.2  christos  */
    591  1.3.2.2  christos 
    592  1.3.2.2  christos static int
    593  1.3.2.2  christos dst_s_write_public_key(const DST_KEY *key)
    594  1.3.2.2  christos {
    595  1.3.2.2  christos 	FILE *fp;
    596  1.3.2.2  christos 	char filename[PATH_MAX];
    597  1.3.2.2  christos 	u_char out_key[RAW_KEY_SIZE];
    598  1.3.2.2  christos 	char enc_key[RAW_KEY_SIZE];
    599  1.3.2.2  christos 	int len = 0;
    600  1.3.2.2  christos 	int mode;
    601  1.3.2.2  christos 
    602  1.3.2.2  christos 	memset(out_key, 0, sizeof(out_key));
    603  1.3.2.2  christos 	if (key == NULL) {
    604  1.3.2.2  christos 		EREPORT(("%s: No key specified \n", __func__));
    605  1.3.2.2  christos 		return (0);
    606  1.3.2.2  christos 	} else if ((len = dst_key_to_dnskey(key, out_key,
    607  1.3.2.2  christos 	    (int)sizeof(out_key)))< 0)
    608  1.3.2.2  christos 		return (0);
    609  1.3.2.2  christos 
    610  1.3.2.2  christos 	/* Make the filename */
    611  1.3.2.2  christos 	if (dst_s_build_filename(filename, key->dk_key_name, key->dk_id,
    612  1.3.2.2  christos 				 key->dk_alg, PUBLIC_KEY, PATH_MAX) == -1) {
    613  1.3.2.2  christos 		EREPORT(("%s: Cannot make filename from %s, %d, and %s\n",
    614  1.3.2.2  christos 		    __func__, key->dk_key_name, key->dk_id, PUBLIC_KEY));
    615  1.3.2.2  christos 		return (0);
    616  1.3.2.2  christos 	}
    617  1.3.2.2  christos 	/* XXX in general this should be a check for symmetric keys */
    618  1.3.2.2  christos 	mode = (key->dk_alg == KEY_HMAC_MD5) ? 0600 : 0644;
    619  1.3.2.2  christos 	/* create public key file */
    620  1.3.2.2  christos 	if ((fp = dst_s_fopen(filename, "w+", mode)) == NULL) {
    621  1.3.2.2  christos 		EREPORT(("%s: open of file:%s failed (errno=%d)\n",
    622  1.3.2.2  christos 		    __func__, filename, errno));
    623  1.3.2.2  christos 		return (0);
    624  1.3.2.2  christos 	}
    625  1.3.2.2  christos 	/*write out key first base64 the key data */
    626  1.3.2.2  christos 	if (key->dk_flags & DST_EXTEND_FLAG)
    627  1.3.2.2  christos 		b64_ntop(&out_key[6], len - 6, enc_key, sizeof(enc_key));
    628  1.3.2.2  christos 	else
    629  1.3.2.2  christos 		b64_ntop(&out_key[4], len - 4, enc_key, sizeof(enc_key));
    630  1.3.2.2  christos 	fprintf(fp, "%s IN KEY %d %d %d %s\n",
    631  1.3.2.2  christos 		key->dk_key_name,
    632  1.3.2.2  christos 		key->dk_flags, key->dk_proto, key->dk_alg, enc_key);
    633  1.3.2.2  christos 	fclose(fp);
    634  1.3.2.2  christos 	return (1);
    635  1.3.2.2  christos }
    636  1.3.2.2  christos 
    637  1.3.2.2  christos /*%
    638  1.3.2.2  christos  *  dst_dnskey_to_public_key
    639  1.3.2.2  christos  *	This function converts the contents of a DNS KEY RR into a DST
    640  1.3.2.2  christos  *	key structure.
    641  1.3.2.2  christos  *  Paramters
    642  1.3.2.2  christos  *	len	 Length of the RDATA of the KEY RR RDATA
    643  1.3.2.2  christos  *	rdata	 A pointer to the the KEY RR RDATA.
    644  1.3.2.2  christos  *	in_name     Key name to be stored in key structure.
    645  1.3.2.2  christos  *  Returns
    646  1.3.2.2  christos  *	NULL	    Failure
    647  1.3.2.2  christos  *	NON-NULL	Success.  Pointer to key structure.
    648  1.3.2.2  christos  *			Caller's responsibility to free() it.
    649  1.3.2.2  christos  */
    650  1.3.2.2  christos 
    651  1.3.2.2  christos DST_KEY *
    652  1.3.2.2  christos dst_dnskey_to_key(const char *in_name, const u_char *rdata, const int len)
    653  1.3.2.2  christos {
    654  1.3.2.2  christos 	DST_KEY *key_st;
    655  1.3.2.2  christos 	int alg ;
    656  1.3.2.2  christos 	int start = DST_KEY_START;
    657  1.3.2.2  christos 
    658  1.3.2.2  christos 	if (rdata == NULL || len <= DST_KEY_ALG) /*%< no data */
    659  1.3.2.2  christos 		return (NULL);
    660  1.3.2.2  christos 	alg = (u_int8_t) rdata[DST_KEY_ALG];
    661  1.3.2.2  christos 	if (!dst_check_algorithm(alg)) { /*%< make sure alg is available */
    662  1.3.2.2  christos 		EREPORT(("%s: Algorithm %d not suppored\n", __func__,
    663  1.3.2.2  christos 		    alg));
    664  1.3.2.2  christos 		return (NULL);
    665  1.3.2.2  christos 	}
    666  1.3.2.2  christos 
    667  1.3.2.2  christos 	if (in_name == NULL)
    668  1.3.2.2  christos 		return (NULL);
    669  1.3.2.2  christos 
    670  1.3.2.2  christos 	if ((key_st = dst_s_get_key_struct(in_name, alg, 0, 0, 0)) == NULL)
    671  1.3.2.2  christos 		return (NULL);
    672  1.3.2.2  christos 
    673  1.3.2.2  christos 	key_st->dk_id = dst_s_dns_key_id(rdata, len);
    674  1.3.2.2  christos 	key_st->dk_flags = dst_s_get_int16(rdata);
    675  1.3.2.2  christos 	key_st->dk_proto = (u_int16_t) rdata[DST_KEY_PROT];
    676  1.3.2.2  christos 	if (key_st->dk_flags & DST_EXTEND_FLAG) {
    677  1.3.2.2  christos 		u_int32_t ext_flags;
    678  1.3.2.2  christos 		ext_flags = (u_int32_t) dst_s_get_int16(&rdata[DST_EXT_FLAG]);
    679  1.3.2.2  christos 		key_st->dk_flags = key_st->dk_flags | (ext_flags << 16);
    680  1.3.2.2  christos 		start += 2;
    681  1.3.2.2  christos 	}
    682  1.3.2.2  christos 	/*
    683  1.3.2.2  christos 	 * now point to the begining of the data representing the encoding
    684  1.3.2.2  christos 	 * of the key
    685  1.3.2.2  christos 	 */
    686  1.3.2.2  christos 	if (key_st->dk_func && key_st->dk_func->from_dns_key) {
    687  1.3.2.2  christos 		if (key_st->dk_func->from_dns_key(key_st, &rdata[start],
    688  1.3.2.2  christos 						  len - start) > 0)
    689  1.3.2.2  christos 			return (key_st);
    690  1.3.2.2  christos 	} else
    691  1.3.2.2  christos 		EREPORT(("%s: unsuppored alg %d\n", __func__,
    692  1.3.2.2  christos 			 alg));
    693  1.3.2.2  christos 
    694  1.3.2.2  christos 	SAFE_FREE(key_st);
    695  1.3.2.2  christos 	return (NULL);
    696  1.3.2.2  christos }
    697  1.3.2.2  christos 
    698  1.3.2.2  christos /*%
    699  1.3.2.2  christos  *  dst_public_key_to_dnskey
    700  1.3.2.2  christos  *	Function to encode a public key into DNS KEY wire format
    701  1.3.2.2  christos  *  Parameters
    702  1.3.2.2  christos  *	key	     Key structure to encode.
    703  1.3.2.2  christos  *	out_storage     Location to write the encoded key to.
    704  1.3.2.2  christos  *	out_len	 Size of the output array.
    705  1.3.2.2  christos  *  Returns
    706  1.3.2.2  christos  *	<0      Failure
    707  1.3.2.2  christos  *	>=0     Number of bytes written to out_storage
    708  1.3.2.2  christos  */
    709  1.3.2.2  christos 
    710  1.3.2.2  christos int
    711  1.3.2.2  christos dst_key_to_dnskey(const DST_KEY *key, u_char *out_storage,
    712  1.3.2.2  christos 			 const int out_len)
    713  1.3.2.2  christos {
    714  1.3.2.2  christos 	u_int16_t val;
    715  1.3.2.2  christos 	int loc = 0;
    716  1.3.2.2  christos 	int enc_len = 0;
    717  1.3.2.2  christos 	if (key == NULL)
    718  1.3.2.2  christos 		return (-1);
    719  1.3.2.2  christos 
    720  1.3.2.2  christos 	if (!dst_check_algorithm(key->dk_alg)) { /*%< make sure alg is available */
    721  1.3.2.2  christos 		EREPORT(("%s: Algorithm %d not suppored\n", __func__,
    722  1.3.2.2  christos 		    key->dk_alg));
    723  1.3.2.2  christos 		return (UNSUPPORTED_KEYALG);
    724  1.3.2.2  christos 	}
    725  1.3.2.2  christos 	memset(out_storage, 0, out_len);
    726  1.3.2.2  christos 	val = (u_int16_t)(key->dk_flags & 0xffff);
    727  1.3.2.2  christos 	dst_s_put_int16(out_storage, val);
    728  1.3.2.2  christos 	loc += 2;
    729  1.3.2.2  christos 
    730  1.3.2.2  christos 	out_storage[loc++] = (u_char) key->dk_proto;
    731  1.3.2.2  christos 	out_storage[loc++] = (u_char) key->dk_alg;
    732  1.3.2.2  christos 
    733  1.3.2.2  christos 	if (key->dk_flags > 0xffff) {	/*%< Extended flags */
    734  1.3.2.2  christos 		val = (u_int16_t)((key->dk_flags >> 16) & 0xffff);
    735  1.3.2.2  christos 		dst_s_put_int16(&out_storage[loc], val);
    736  1.3.2.2  christos 		loc += 2;
    737  1.3.2.2  christos 	}
    738  1.3.2.2  christos 	if (key->dk_KEY_struct == NULL)
    739  1.3.2.2  christos 		return (loc);
    740  1.3.2.2  christos 	if (key->dk_func && key->dk_func->to_dns_key) {
    741  1.3.2.2  christos 		enc_len = key->dk_func->to_dns_key(key,
    742  1.3.2.2  christos 						 (u_char *) &out_storage[loc],
    743  1.3.2.2  christos 						   out_len - loc);
    744  1.3.2.2  christos 		if (enc_len > 0)
    745  1.3.2.2  christos 			return (enc_len + loc);
    746  1.3.2.2  christos 		else
    747  1.3.2.2  christos 			return (-1);
    748  1.3.2.2  christos 	} else
    749  1.3.2.2  christos 		EREPORT(("%s: Unsupported ALG %d\n", __func__, key->dk_alg));
    750  1.3.2.2  christos 	return (-1);
    751  1.3.2.2  christos }
    752  1.3.2.2  christos 
    753  1.3.2.2  christos /*%
    754  1.3.2.2  christos  *  dst_buffer_to_key
    755  1.3.2.2  christos  *	Function to encode a string of raw data into a DST key
    756  1.3.2.2  christos  *  Parameters
    757  1.3.2.2  christos  *	alg		The algorithm (HMAC only)
    758  1.3.2.2  christos  *	key		A pointer to the data
    759  1.3.2.2  christos  *	keylen		The length of the data
    760  1.3.2.2  christos  *  Returns
    761  1.3.2.2  christos  *	NULL	    an error occurred
    762  1.3.2.2  christos  *	NON-NULL	the DST key
    763  1.3.2.2  christos  */
    764  1.3.2.2  christos DST_KEY *
    765  1.3.2.2  christos dst_buffer_to_key(const char *key_name,		/*!< name of the key  */
    766  1.3.2.2  christos 		  const int alg,		/*!< algorithm  */
    767  1.3.2.2  christos 		  const int flags,		/*!< dns flags  */
    768  1.3.2.2  christos 		  const int protocol,		/*!< dns protocol  */
    769  1.3.2.2  christos 		  const u_char *key_buf,	/*!< key in dns wire fmt  */
    770  1.3.2.2  christos 		  const int key_len)		/*!< size of key  */
    771  1.3.2.2  christos {
    772  1.3.2.2  christos 
    773  1.3.2.2  christos 	DST_KEY *dkey = NULL;
    774  1.3.2.2  christos 	int dnslen;
    775  1.3.2.2  christos 	u_char dns[2048];
    776  1.3.2.2  christos 
    777  1.3.2.2  christos 	if (!dst_check_algorithm(alg)) { /*%< make sure alg is available */
    778  1.3.2.2  christos 		EREPORT(("%s: Algorithm %d not suppored\n", __func__, alg));
    779  1.3.2.2  christos 		return (NULL);
    780  1.3.2.2  christos 	}
    781  1.3.2.2  christos 
    782  1.3.2.2  christos 	dkey = dst_s_get_key_struct(key_name, alg, flags, protocol, -1);
    783  1.3.2.2  christos 
    784  1.3.2.2  christos 	if (dkey == NULL || dkey->dk_func == NULL ||
    785  1.3.2.2  christos 	    dkey->dk_func->from_dns_key == NULL)
    786  1.3.2.2  christos 		return (dst_free_key(dkey));
    787  1.3.2.2  christos 
    788  1.3.2.2  christos 	if (dkey->dk_func->from_dns_key(dkey, key_buf, key_len) < 0) {
    789  1.3.2.2  christos 		EREPORT(("%s: dst_buffer_to_hmac failed\n", __func__));
    790  1.3.2.2  christos 		return (dst_free_key(dkey));
    791  1.3.2.2  christos 	}
    792  1.3.2.2  christos 
    793  1.3.2.2  christos 	dnslen = dst_key_to_dnskey(dkey, dns, (int)sizeof(dns));
    794  1.3.2.2  christos 	dkey->dk_id = dst_s_dns_key_id(dns, dnslen);
    795  1.3.2.2  christos 	return (dkey);
    796  1.3.2.2  christos }
    797  1.3.2.2  christos 
    798  1.3.2.2  christos int
    799  1.3.2.2  christos dst_key_to_buffer(DST_KEY *key, u_char *out_buff, int buf_len)
    800  1.3.2.2  christos {
    801  1.3.2.2  christos 	int len;
    802  1.3.2.2  christos   /* this function will extrac the secret of HMAC into a buffer */
    803  1.3.2.2  christos 	if (key == NULL)
    804  1.3.2.2  christos 		return (0);
    805  1.3.2.2  christos 	if (key->dk_func != NULL && key->dk_func->to_dns_key != NULL) {
    806  1.3.2.2  christos 		len = key->dk_func->to_dns_key(key, out_buff, buf_len);
    807  1.3.2.2  christos 		if (len < 0)
    808  1.3.2.2  christos 			return (0);
    809  1.3.2.2  christos 		return (len);
    810  1.3.2.2  christos 	}
    811  1.3.2.2  christos 	return (0);
    812  1.3.2.2  christos }
    813  1.3.2.2  christos 
    814  1.3.2.2  christos /*%
    815  1.3.2.2  christos  * dst_s_read_private_key_file
    816  1.3.2.2  christos  *     Function reads in private key from a file.
    817  1.3.2.2  christos  *     Fills out the KEY structure.
    818  1.3.2.2  christos  * Parameters
    819  1.3.2.2  christos  *     name    Name of the key to be read.
    820  1.3.2.2  christos  *     pk_key  Structure that the key is returned in.
    821  1.3.2.2  christos  *     in_id   Key identifier (tag)
    822  1.3.2.2  christos  * Return
    823  1.3.2.2  christos  *     1 if everthing works
    824  1.3.2.2  christos  *     0 if there is any problem
    825  1.3.2.2  christos  */
    826  1.3.2.2  christos 
    827  1.3.2.2  christos static int
    828  1.3.2.2  christos dst_s_read_private_key_file(char *name, DST_KEY *pk_key, u_int16_t in_id,
    829  1.3.2.2  christos 			    int in_alg)
    830  1.3.2.2  christos {
    831  1.3.2.2  christos 	int alg, major, minor, file_major, file_minor;
    832  1.3.2.2  christos 	ssize_t cnt;
    833  1.3.2.2  christos 	size_t len;
    834  1.3.2.2  christos 	int ret, id;
    835  1.3.2.2  christos 	char filename[PATH_MAX];
    836  1.3.2.2  christos 	u_char in_buff[RAW_KEY_SIZE], *p;
    837  1.3.2.2  christos 	FILE *fp;
    838  1.3.2.2  christos 	int dnslen;
    839  1.3.2.2  christos 	u_char dns[2048];
    840  1.3.2.2  christos 
    841  1.3.2.2  christos 	if (name == NULL || pk_key == NULL) {
    842  1.3.2.2  christos 		EREPORT(("%s: No key name given\n", __func__));
    843  1.3.2.2  christos 		return (0);
    844  1.3.2.2  christos 	}
    845  1.3.2.2  christos 	/* Make the filename */
    846  1.3.2.2  christos 	if (dst_s_build_filename(filename, name, in_id, in_alg, PRIVATE_KEY,
    847  1.3.2.2  christos 				 PATH_MAX) == -1) {
    848  1.3.2.2  christos 		EREPORT(("%s: Cannot make filename from %s, %d, and %s\n",
    849  1.3.2.2  christos 		    __func__, name, in_id, PRIVATE_KEY));
    850  1.3.2.2  christos 		return (0);
    851  1.3.2.2  christos 	}
    852  1.3.2.2  christos 	/* first check if we can find the key file */
    853  1.3.2.2  christos 	if ((fp = dst_s_fopen(filename, "r", 0)) == NULL) {
    854  1.3.2.2  christos 		EREPORT(("%s: Could not open file %s in directory %s\n",
    855  1.3.2.2  christos 		    __func__, filename, dst_path[0] ? dst_path :
    856  1.3.2.2  christos 		    getcwd(NULL, PATH_MAX - 1)));
    857  1.3.2.2  christos 		return (0);
    858  1.3.2.2  christos 	}
    859  1.3.2.2  christos 	/* now read the header info from the file */
    860  1.3.2.2  christos 	if ((cnt = fread(in_buff, 1, sizeof(in_buff), fp)) < 5) {
    861  1.3.2.2  christos 		fclose(fp);
    862  1.3.2.2  christos 		EREPORT(("%s: error reading file %s (empty file)\n",
    863  1.3.2.2  christos 		    __func__, filename));
    864  1.3.2.2  christos 		return (0);
    865  1.3.2.2  christos 	}
    866  1.3.2.2  christos 	len = cnt;
    867  1.3.2.2  christos 	/* decrypt key */
    868  1.3.2.2  christos 	fclose(fp);
    869  1.3.2.2  christos 	if (memcmp(in_buff, "Private-key-format: v", 20) != 0)
    870  1.3.2.2  christos 		goto fail;
    871  1.3.2.2  christos 	p = in_buff;
    872  1.3.2.2  christos 
    873  1.3.2.2  christos 	if (!dst_s_verify_str((const char **) (void *)&p,
    874  1.3.2.2  christos 			       "Private-key-format: v")) {
    875  1.3.2.2  christos 		EREPORT(("%s: Not a Key file/Decrypt failed %s\n", __func__,
    876  1.3.2.2  christos 		    name));
    877  1.3.2.2  christos 		goto fail;
    878  1.3.2.2  christos 	}
    879  1.3.2.2  christos 	/* read in file format */
    880  1.3.2.2  christos 	sscanf((char *)p, "%d.%d", &file_major, &file_minor);
    881  1.3.2.2  christos 	sscanf(KEY_FILE_FORMAT, "%d.%d", &major, &minor);
    882  1.3.2.2  christos 	if (file_major < 1) {
    883  1.3.2.2  christos 		EREPORT(("%s: Unknown keyfile %d.%d version for %s\n",
    884  1.3.2.2  christos 		    __func__, file_major, file_minor, name));
    885  1.3.2.2  christos 		goto fail;
    886  1.3.2.2  christos 	} else if (file_major > major || file_minor > minor)
    887  1.3.2.2  christos 		EREPORT(("%s: Keyfile %s version higher than mine %d.%d MAY"
    888  1.3.2.2  christos 		    " FAIL\n", __func__, name, file_major, file_minor));
    889  1.3.2.2  christos 
    890  1.3.2.2  christos 	while (*p++ != '\n') ;	/*%< skip to end of line */
    891  1.3.2.2  christos 
    892  1.3.2.2  christos 	if (!dst_s_verify_str((const char **) (void *)&p, "Algorithm: "))
    893  1.3.2.2  christos 		goto fail;
    894  1.3.2.2  christos 
    895  1.3.2.2  christos 	if (sscanf((char *)p, "%d", &alg) != 1)
    896  1.3.2.2  christos 		goto fail;
    897  1.3.2.2  christos 	while (*p++ != '\n') ;	/*%< skip to end of line */
    898  1.3.2.2  christos 
    899  1.3.2.2  christos 	if (pk_key->dk_key_name && !strcmp(pk_key->dk_key_name, name))
    900  1.3.2.2  christos 		SAFE_FREE2(pk_key->dk_key_name, strlen(pk_key->dk_key_name));
    901  1.3.2.2  christos 	pk_key->dk_key_name = strdup(name);
    902  1.3.2.2  christos 
    903  1.3.2.2  christos 	/* allocate and fill in key structure */
    904  1.3.2.2  christos 	if (pk_key->dk_func == NULL || pk_key->dk_func->from_file_fmt == NULL)
    905  1.3.2.2  christos 		goto fail;
    906  1.3.2.2  christos 
    907  1.3.2.2  christos 	ret = pk_key->dk_func->from_file_fmt(pk_key, (char *)p,
    908  1.3.2.2  christos 	    (int)(&in_buff[len] - p));
    909  1.3.2.2  christos 	if (ret < 0)
    910  1.3.2.2  christos 		goto fail;
    911  1.3.2.2  christos 
    912  1.3.2.2  christos 	dnslen = dst_key_to_dnskey(pk_key, dns, (int)sizeof(dns));
    913  1.3.2.2  christos 	id = dst_s_dns_key_id(dns, dnslen);
    914  1.3.2.2  christos 
    915  1.3.2.2  christos 	/* Make sure the actual key tag matches the input tag used in the
    916  1.3.2.2  christos 	 * filename */
    917  1.3.2.2  christos 	if (id != in_id) {
    918  1.3.2.2  christos 		EREPORT(("%s: actual tag of key read %d != input tag used to"
    919  1.3.2.2  christos 		    "build filename %d.\n", __func__, id, in_id));
    920  1.3.2.2  christos 		goto fail;
    921  1.3.2.2  christos 	}
    922  1.3.2.2  christos 	pk_key->dk_id = (u_int16_t) id;
    923  1.3.2.2  christos 	pk_key->dk_alg = alg;
    924  1.3.2.2  christos 	memset(in_buff, 0, len);
    925  1.3.2.2  christos 	return (1);
    926  1.3.2.2  christos 
    927  1.3.2.2  christos  fail:
    928  1.3.2.2  christos 	memset(in_buff, 0, len);
    929  1.3.2.2  christos 	return (0);
    930  1.3.2.2  christos }
    931  1.3.2.2  christos 
    932  1.3.2.2  christos /*%
    933  1.3.2.2  christos  *	Generate and store a public/private keypair.
    934  1.3.2.2  christos  *	Keys will be stored in formatted files.
    935  1.3.2.2  christos  *
    936  1.3.2.2  christos  *  Parameters
    937  1.3.2.2  christos  &
    938  1.3.2.2  christos  *\par	name    Name of the new key.  Used to create key files
    939  1.3.2.2  christos  *\li		  K&lt;name&gt;+&lt;alg&gt;+&lt;id&gt;.public and K&lt;name&gt;+&lt;alg&gt;+&lt;id&gt;.private.
    940  1.3.2.2  christos  *\par	bits    Size of the new key in bits.
    941  1.3.2.2  christos  *\par	exp     What exponent to use:
    942  1.3.2.2  christos  *\li		  0	   use exponent 3
    943  1.3.2.2  christos  *\li		  non-zero    use Fermant4
    944  1.3.2.2  christos  *\par	flags   The default value of the DNS Key flags.
    945  1.3.2.2  christos  *\li		  The DNS Key RR Flag field is defined in RFC2065,
    946  1.3.2.2  christos  *		  section 3.3.  The field has 16 bits.
    947  1.3.2.2  christos  *\par	protocol
    948  1.3.2.2  christos  *\li	      Default value of the DNS Key protocol field.
    949  1.3.2.2  christos  *\li		  The DNS Key protocol field is defined in RFC2065,
    950  1.3.2.2  christos  *		  section 3.4.  The field has 8 bits.
    951  1.3.2.2  christos  *\par	alg     What algorithm to use.  Currently defined:
    952  1.3.2.2  christos  *\li		  KEY_RSA       1
    953  1.3.2.2  christos  *\li		  KEY_DSA       3
    954  1.3.2.2  christos  *\li		  KEY_HMAC    157
    955  1.3.2.2  christos  *\par	out_id The key tag is returned.
    956  1.3.2.2  christos  *
    957  1.3.2.2  christos  *  Return
    958  1.3.2.2  christos  *\li	NULL		Failure
    959  1.3.2.2  christos  *\li	non-NULL 	the generated key pair
    960  1.3.2.2  christos  *			Caller frees the result, and its dk_name pointer.
    961  1.3.2.2  christos  */
    962  1.3.2.2  christos DST_KEY *
    963  1.3.2.2  christos dst_generate_key(const char *name, const int bits, const int exp,
    964  1.3.2.2  christos 		 const int flags, const int protocol, const int alg)
    965  1.3.2.2  christos {
    966  1.3.2.2  christos 	DST_KEY *new_key = NULL;
    967  1.3.2.2  christos 	int dnslen;
    968  1.3.2.2  christos 	u_char dns[2048];
    969  1.3.2.2  christos 
    970  1.3.2.2  christos 	if (name == NULL)
    971  1.3.2.2  christos 		return (NULL);
    972  1.3.2.2  christos 
    973  1.3.2.2  christos 	if (!dst_check_algorithm(alg)) { /*%< make sure alg is available */
    974  1.3.2.2  christos 		EREPORT(("%s: Algorithm %d not suppored\n", __func__, alg));
    975  1.3.2.2  christos 		return (NULL);
    976  1.3.2.2  christos 	}
    977  1.3.2.2  christos 
    978  1.3.2.2  christos 	new_key = dst_s_get_key_struct(name, alg, flags, protocol, bits);
    979  1.3.2.2  christos 	if (new_key == NULL)
    980  1.3.2.2  christos 		return (NULL);
    981  1.3.2.2  christos 	if (bits == 0) /*%< null key we are done */
    982  1.3.2.2  christos 		return (new_key);
    983  1.3.2.2  christos 	if (new_key->dk_func == NULL || new_key->dk_func->generate == NULL) {
    984  1.3.2.2  christos 		EREPORT(("%s: Unsupported algorithm %d\n", __func__, alg));
    985  1.3.2.2  christos 		return (dst_free_key(new_key));
    986  1.3.2.2  christos 	}
    987  1.3.2.2  christos 	if (new_key->dk_func->generate(new_key, exp) <= 0) {
    988  1.3.2.2  christos 		EREPORT(("%s: Key generation failure %s %d %d %d\n", __func__,
    989  1.3.2.2  christos 		    new_key->dk_key_name, new_key->dk_alg,
    990  1.3.2.2  christos 		    new_key->dk_key_size, exp));
    991  1.3.2.2  christos 		return (dst_free_key(new_key));
    992  1.3.2.2  christos 	}
    993  1.3.2.2  christos 
    994  1.3.2.2  christos 	dnslen = dst_key_to_dnskey(new_key, dns, (int)sizeof(dns));
    995  1.3.2.2  christos 	if (dnslen != UNSUPPORTED_KEYALG)
    996  1.3.2.2  christos 		new_key->dk_id = dst_s_dns_key_id(dns, dnslen);
    997  1.3.2.2  christos 	else
    998  1.3.2.2  christos 		new_key->dk_id = 0;
    999  1.3.2.2  christos 
   1000  1.3.2.2  christos 	return (new_key);
   1001  1.3.2.2  christos }
   1002  1.3.2.2  christos 
   1003  1.3.2.2  christos /*%
   1004  1.3.2.2  christos  *	Release all data structures pointed to by a key structure.
   1005  1.3.2.2  christos  *
   1006  1.3.2.2  christos  *  Parameters
   1007  1.3.2.2  christos  *\li	f_key   Key structure to be freed.
   1008  1.3.2.2  christos  */
   1009  1.3.2.2  christos 
   1010  1.3.2.2  christos DST_KEY *
   1011  1.3.2.2  christos dst_free_key(DST_KEY *f_key)
   1012  1.3.2.2  christos {
   1013  1.3.2.2  christos 
   1014  1.3.2.2  christos 	if (f_key == NULL)
   1015  1.3.2.2  christos 		return (f_key);
   1016  1.3.2.2  christos 	if (f_key->dk_func && f_key->dk_func->destroy)
   1017  1.3.2.2  christos 		f_key->dk_KEY_struct =
   1018  1.3.2.2  christos 			f_key->dk_func->destroy(f_key->dk_KEY_struct);
   1019  1.3.2.2  christos 	else {
   1020  1.3.2.2  christos 		EREPORT(("%s: Unknown key alg %d\n", __func__, f_key->dk_alg));
   1021  1.3.2.2  christos 	}
   1022  1.3.2.2  christos 	if (f_key->dk_KEY_struct) {
   1023  1.3.2.2  christos 		free(f_key->dk_KEY_struct);
   1024  1.3.2.2  christos 		f_key->dk_KEY_struct = NULL;
   1025  1.3.2.2  christos 	}
   1026  1.3.2.2  christos 	if (f_key->dk_key_name)
   1027  1.3.2.2  christos 		SAFE_FREE(f_key->dk_key_name);
   1028  1.3.2.2  christos 	SAFE_FREE(f_key);
   1029  1.3.2.2  christos 	return (NULL);
   1030  1.3.2.2  christos }
   1031  1.3.2.2  christos 
   1032  1.3.2.2  christos /*%
   1033  1.3.2.2  christos  *	Return the maximim size of signature from the key specified in bytes
   1034  1.3.2.2  christos  *
   1035  1.3.2.2  christos  * Parameters
   1036  1.3.2.2  christos  *\li      key
   1037  1.3.2.2  christos  *
   1038  1.3.2.2  christos  * Returns
   1039  1.3.2.2  christos  *  \li   bytes
   1040  1.3.2.2  christos  */
   1041  1.3.2.2  christos int
   1042  1.3.2.2  christos dst_sig_size(DST_KEY *key) {
   1043  1.3.2.2  christos 	switch (key->dk_alg) {
   1044  1.3.2.2  christos 	    case KEY_HMAC_MD5:
   1045  1.3.2.2  christos 		return (16);
   1046  1.3.2.2  christos 	    case KEY_HMAC_SHA1:
   1047  1.3.2.2  christos 		return (20);
   1048  1.3.2.2  christos 	    case KEY_RSA:
   1049  1.3.2.2  christos 		return (key->dk_key_size + 7) / 8;
   1050  1.3.2.2  christos 	    case KEY_DSA:
   1051  1.3.2.2  christos 		return (40);
   1052  1.3.2.2  christos 	    default:
   1053  1.3.2.2  christos 		EREPORT(("%s: Unknown key alg %d\n", __func__, key->dk_alg));
   1054  1.3.2.2  christos 		return -1;
   1055  1.3.2.2  christos 	}
   1056  1.3.2.2  christos }
   1057  1.3.2.2  christos 
   1058  1.3.2.2  christos /*! \file */
   1059