1 /* 2 * Copyright 2005 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 * lib/krb5/krb/copy_key.c 9 * 10 * Copyright 1990,1991 by the Massachusetts Institute of Technology. 11 * All Rights Reserved. 12 * 13 * Export of this software from the United States of America may 14 * require a specific license from the United States Government. 15 * It is the responsibility of any person or organization contemplating 16 * export to 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 M.I.T. not be used in advertising or publicity pertaining 24 * to distribution of the software without specific, written prior 25 * permission. Furthermore if you modify this software you must label 26 * your software as modified software and not distribute it in such a 27 * fashion that it might be confused with the original M.I.T. software. 28 * M.I.T. makes no representations about the suitability of 29 * this software for any purpose. It is provided "as is" without express 30 * or implied warranty. 31 * 32 * 33 * krb5_copy_keyblock() 34 */ 35 36 #include <k5-int.h> 37 38 /* 39 * krb5_copy_keyblock_data 40 * 41 * Utility for copying keyblock data structures safely. 42 * This assumes that the necessary storage areas are 43 * already allocated. 44 */ 45 krb5_error_code 46 krb5_copy_keyblock_data(krb5_context context, 47 const krb5_keyblock *from, krb5_keyblock *to) 48 { 49 krb5_error_code ret = 0; 50 51 /* If nothing to copy, return no error */ 52 if (from == NULL || to == NULL) 53 return (0); 54 55 if ((to->contents == NULL || from->contents == NULL) && 56 from->length > 0) 57 return (ENOMEM); 58 59 to->magic = from->magic; 60 to->enctype = from->enctype; 61 to->length = from->length; 62 to->dk_list = NULL; 63 64 if (from->length > 0) 65 (void) memcpy(to->contents, from->contents, from->length); 66 67 #ifdef _KERNEL 68 to->kef_mt = from->kef_mt; 69 to->kef_key.ck_data = NULL; 70 to->key_tmpl = NULL; 71 if ((ret = init_key_kef(context->kef_cipher_mt, to))) { 72 return (ret); 73 } 74 #else 75 /* 76 * Don't copy or try to initialize crypto framework 77 * data. This data gets initialized the first time it is 78 * used. 79 */ 80 to->hKey = CK_INVALID_HANDLE; 81 #endif /* _KERNEL */ 82 return (ret); 83 } 84 85 86 /* 87 * Copy a keyblock, including alloc'ed storage. 88 */ 89 /*ARGSUSED*/ 90 krb5_error_code KRB5_CALLCONV 91 krb5_copy_keyblock(context, from, to) 92 krb5_context context; 93 const krb5_keyblock *from; 94 krb5_keyblock **to; 95 { 96 krb5_keyblock *new_key; 97 krb5_error_code ret = 0; 98 if (!(new_key = (krb5_keyblock *) MALLOC(sizeof(krb5_keyblock)))) 99 return (ENOMEM); 100 101 if (!(new_key->contents = (krb5_octet *)MALLOC(from->length))) { 102 FREE(new_key, sizeof(krb5_keyblock)); 103 return (ENOMEM); 104 } 105 106 ret = krb5_copy_keyblock_data(context, from, new_key); 107 if (ret) { 108 krb5_free_keyblock(context, new_key); 109 return (ret); 110 } 111 112 *to = new_key; 113 return (ret); 114 } 115 116