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