1 /* 2 * Copyright 2006 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 * lib/kdb/encrypt_key.c 10 * 11 * Copyright 1990,1991 by the Massachusetts Institute of Technology. 12 * All Rights Reserved. 13 * 14 * Export of this software from the United States of America may 15 * require a specific license from the United States Government. 16 * It is the responsibility of any person or organization contemplating 17 * export to obtain such a license before exporting. 18 * 19 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and 20 * distribute this software and its documentation for any purpose and 21 * without fee is hereby granted, provided that the above copyright 22 * notice appear in all copies and that both that copyright notice and 23 * this permission notice appear in supporting documentation, and that 24 * the name of M.I.T. not be used in advertising or publicity pertaining 25 * to distribution of the software without specific, written prior 26 * permission. Furthermore if you modify this software you must label 27 * your software as modified software and not distribute it in such a 28 * fashion that it might be confused with the original M.I.T. software. 29 * M.I.T. makes no representations about the suitability of 30 * this software for any purpose. It is provided "as is" without express 31 * or implied warranty. 32 * 33 * 34 * krb5_kdb_encrypt_key(), krb5_kdb_decrypt_key functions 35 */ 36 37 /* 38 * Copyright (C) 1998 by the FundsXpress, INC. 39 * 40 * All rights reserved. 41 * 42 * Export of this software from the United States of America may require 43 * a specific license from the United States Government. It is the 44 * responsibility of any person or organization contemplating export to 45 * obtain such a license before exporting. 46 * 47 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and 48 * distribute this software and its documentation for any purpose and 49 * without fee is hereby granted, provided that the above copyright 50 * notice appear in all copies and that both that copyright notice and 51 * this permission notice appear in supporting documentation, and that 52 * the name of FundsXpress. not be used in advertising or publicity pertaining 53 * to distribution of the software without specific, written prior 54 * permission. FundsXpress makes no representations about the suitability of 55 * this software for any purpose. It is provided "as is" without express 56 * or implied warranty. 57 * 58 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 59 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 60 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 61 */ 62 63 #include "k5-int.h" 64 65 /* 66 * Encrypt a key for storage in the database. "eblock" is used 67 * to encrypt the key in "in" into "out"; the storage pointed to by "out" 68 * is allocated before use. 69 */ 70 71 krb5_error_code 72 krb5_dbekd_encrypt_key_data(context, mkey, dbkey, keysalt, keyver, key_data) 73 krb5_context context; 74 const krb5_keyblock * mkey; 75 const krb5_keyblock * dbkey; 76 const krb5_keysalt * keysalt; 77 int keyver; 78 krb5_key_data * key_data; 79 { 80 krb5_error_code retval; 81 krb5_octet * ptr; 82 size_t len; 83 int i; 84 krb5_data plain; 85 krb5_enc_data cipher; 86 87 for (i = 0; i < key_data->key_data_ver; i++) 88 if (key_data->key_data_contents[i]) 89 krb5_xfree(key_data->key_data_contents[i]); 90 91 key_data->key_data_ver = 1; 92 key_data->key_data_kvno = keyver; 93 94 /* 95 * The First element of the type/length/contents 96 * fields is the key type/length/contents 97 */ 98 if ((retval = krb5_c_encrypt_length(context, mkey->enctype, dbkey->length, 99 &len))) 100 return(retval); 101 102 if ((ptr = (krb5_octet *) malloc(2 + len)) == NULL) 103 return(ENOMEM); 104 105 (void) memset(ptr, 0, 2 + len); 106 107 key_data->key_data_type[0] = dbkey->enctype; 108 key_data->key_data_length[0] = 2 + len; 109 key_data->key_data_contents[0] = ptr; 110 111 krb5_kdb_encode_int16(dbkey->length, ptr); 112 ptr += 2; 113 114 plain.length = dbkey->length; 115 plain.data = (char *)dbkey->contents; /* SUNWresync121 XXX */ 116 117 cipher.ciphertext.length = len; 118 cipher.ciphertext.data = (char *)ptr; /* SUNWresync121 XXX */ 119 120 if ((retval = krb5_c_encrypt(context, mkey, /* XXX */ 0, 0, 121 &plain, &cipher))) { 122 krb5_xfree(key_data->key_data_contents[0]); 123 return retval; 124 } 125 126 /* After key comes the salt in necessary */ 127 if (keysalt) { 128 if (keysalt->type > 0) { 129 key_data->key_data_ver++; 130 key_data->key_data_type[1] = keysalt->type; 131 if ((key_data->key_data_length[1] = keysalt->data.length) != 0) { 132 key_data->key_data_contents[1] = 133 (krb5_octet *)malloc(keysalt->data.length); 134 if (key_data->key_data_contents[1] == NULL) { 135 krb5_xfree(key_data->key_data_contents[0]); 136 return ENOMEM; 137 } 138 memcpy(key_data->key_data_contents[1], keysalt->data.data, 139 (size_t) keysalt->data.length); 140 } 141 } 142 } 143 144 return retval; 145 } 146