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