1 /* 2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 3 * Use is subject to license terms. 4 */ 5 6 #pragma ident "%Z%%M% %I% %E% SMI" 7 8 /* 9 * Copyright (C) 1998 by the FundsXpress, INC. 10 * 11 * All rights reserved. 12 * 13 * Export of this software from the United States of America may require 14 * a specific license from the United States Government. It is the 15 * responsibility of any person or organization contemplating export to 16 * obtain such a license before exporting. 17 * 18 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and 19 * distribute this software and its documentation for any purpose and 20 * without fee is hereby granted, provided that the above copyright 21 * notice appear in all copies and that both that copyright notice and 22 * this permission notice appear in supporting documentation, and that 23 * the name of FundsXpress. not be used in advertising or publicity pertaining 24 * to distribution of the software without specific, written prior 25 * permission. FundsXpress makes no representations about the suitability of 26 * this software for any purpose. It is provided "as is" without express 27 * or implied warranty. 28 * 29 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 30 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 31 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 32 */ 33 34 #include <k5-int.h> 35 #include <etypes.h> 36 37 krb5_error_code KRB5_CALLCONV 38 krb5_c_make_random_key(krb5_context context, krb5_enctype enctype, 39 krb5_keyblock *random_key) 40 { 41 int i; 42 krb5_error_code ret; 43 const struct krb5_enc_provider *enc; 44 size_t keybytes, keylength; 45 krb5_data random_data; 46 unsigned char *bytes; 47 48 for (i=0; i<krb5_enctypes_length; i++) { 49 if (krb5_enctypes_list[i].etype == enctype) 50 break; 51 } 52 53 /* Solaris Kerberos: Better error message */ 54 if (i == krb5_enctypes_length) { 55 krb5_set_error_message(context, KRB5_BAD_ENCTYPE, 56 "Unknown encryption type: %d", enctype); 57 return(KRB5_BAD_ENCTYPE); 58 } 59 60 enc = krb5_enctypes_list[i].enc; 61 62 keybytes = enc->keybytes; 63 keylength = enc->keylength; 64 65 if ((bytes = (unsigned char *) malloc(keybytes)) == NULL) 66 return(ENOMEM); 67 if ((random_key->contents = (krb5_octet *) malloc(keylength)) == NULL) { 68 free(bytes); 69 return(ENOMEM); 70 } 71 72 random_data.data = (char *) bytes; 73 random_data.length = keybytes; 74 75 if ((ret = krb5_c_random_make_octets(context, &random_data))) 76 goto cleanup; 77 78 random_key->magic = KV5M_KEYBLOCK; 79 random_key->enctype = enctype; 80 random_key->length = keylength; 81 random_key->dk_list = NULL; 82 #ifdef _KERNEL 83 random_key->kef_key = NULL; 84 #else 85 random_key->hKey = CK_INVALID_HANDLE; 86 #endif 87 88 ret = ((*(enc->make_key))(context, &random_data, random_key)); 89 90 cleanup: 91 memset(bytes, 0, keybytes); 92 free(bytes); 93 94 if (ret) { 95 memset(random_key->contents, 0, keylength); 96 free(random_key->contents); 97 random_key->contents = NULL; 98 } 99 100 return(ret); 101 } 102