17c478bd9Sstevel@tonic-gate /*
2159d09a2SMark Phalan * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
37c478bd9Sstevel@tonic-gate * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate */
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate * lib/krb5/krb/copy_key.c
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * Copyright 1990,1991 by the Massachusetts Institute of Technology.
107c478bd9Sstevel@tonic-gate * All Rights Reserved.
117c478bd9Sstevel@tonic-gate *
127c478bd9Sstevel@tonic-gate * Export of this software from the United States of America may
137c478bd9Sstevel@tonic-gate * require a specific license from the United States Government.
147c478bd9Sstevel@tonic-gate * It is the responsibility of any person or organization contemplating
157c478bd9Sstevel@tonic-gate * export to obtain such a license before exporting.
16*55fea89dSDan Cross *
177c478bd9Sstevel@tonic-gate * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
187c478bd9Sstevel@tonic-gate * distribute this software and its documentation for any purpose and
197c478bd9Sstevel@tonic-gate * without fee is hereby granted, provided that the above copyright
207c478bd9Sstevel@tonic-gate * notice appear in all copies and that both that copyright notice and
217c478bd9Sstevel@tonic-gate * this permission notice appear in supporting documentation, and that
227c478bd9Sstevel@tonic-gate * the name of M.I.T. not be used in advertising or publicity pertaining
237c478bd9Sstevel@tonic-gate * to distribution of the software without specific, written prior
247c478bd9Sstevel@tonic-gate * permission. Furthermore if you modify this software you must label
257c478bd9Sstevel@tonic-gate * your software as modified software and not distribute it in such a
267c478bd9Sstevel@tonic-gate * fashion that it might be confused with the original M.I.T. software.
277c478bd9Sstevel@tonic-gate * M.I.T. makes no representations about the suitability of
287c478bd9Sstevel@tonic-gate * this software for any purpose. It is provided "as is" without express
297c478bd9Sstevel@tonic-gate * or implied warranty.
30*55fea89dSDan Cross *
317c478bd9Sstevel@tonic-gate *
327c478bd9Sstevel@tonic-gate * krb5_copy_keyblock()
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate
35159d09a2SMark Phalan #include "k5-int.h"
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate * krb5_copy_keyblock_data
397c478bd9Sstevel@tonic-gate *
407c478bd9Sstevel@tonic-gate * Utility for copying keyblock data structures safely.
417c478bd9Sstevel@tonic-gate * This assumes that the necessary storage areas are
427c478bd9Sstevel@tonic-gate * already allocated.
437c478bd9Sstevel@tonic-gate */
447c478bd9Sstevel@tonic-gate krb5_error_code
krb5_copy_keyblock_data(krb5_context context,const krb5_keyblock * from,krb5_keyblock * to)457c478bd9Sstevel@tonic-gate krb5_copy_keyblock_data(krb5_context context,
46505d05c7Sgtb const krb5_keyblock *from, krb5_keyblock *to)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate krb5_error_code ret = 0;
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate /* If nothing to copy, return no error */
517c478bd9Sstevel@tonic-gate if (from == NULL || to == NULL)
527c478bd9Sstevel@tonic-gate return (0);
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate if ((to->contents == NULL || from->contents == NULL) &&
557c478bd9Sstevel@tonic-gate from->length > 0)
567c478bd9Sstevel@tonic-gate return (ENOMEM);
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate to->magic = from->magic;
597c478bd9Sstevel@tonic-gate to->enctype = from->enctype;
607c478bd9Sstevel@tonic-gate to->length = from->length;
617c478bd9Sstevel@tonic-gate to->dk_list = NULL;
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate if (from->length > 0)
647c478bd9Sstevel@tonic-gate (void) memcpy(to->contents, from->contents, from->length);
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate #ifdef _KERNEL
677c478bd9Sstevel@tonic-gate to->kef_mt = from->kef_mt;
687c478bd9Sstevel@tonic-gate to->kef_key.ck_data = NULL;
697c478bd9Sstevel@tonic-gate to->key_tmpl = NULL;
707c478bd9Sstevel@tonic-gate if ((ret = init_key_kef(context->kef_cipher_mt, to))) {
717c478bd9Sstevel@tonic-gate return (ret);
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate #else
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate * Don't copy or try to initialize crypto framework
767c478bd9Sstevel@tonic-gate * data. This data gets initialized the first time it is
777c478bd9Sstevel@tonic-gate * used.
787c478bd9Sstevel@tonic-gate */
797c478bd9Sstevel@tonic-gate to->hKey = CK_INVALID_HANDLE;
807c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
817c478bd9Sstevel@tonic-gate return (ret);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate * Copy a keyblock, including alloc'ed storage.
877c478bd9Sstevel@tonic-gate */
887c478bd9Sstevel@tonic-gate /*ARGSUSED*/
89505d05c7Sgtb krb5_error_code KRB5_CALLCONV
krb5_copy_keyblock(context,from,to)907c478bd9Sstevel@tonic-gate krb5_copy_keyblock(context, from, to)
917c478bd9Sstevel@tonic-gate krb5_context context;
927c478bd9Sstevel@tonic-gate const krb5_keyblock *from;
937c478bd9Sstevel@tonic-gate krb5_keyblock **to;
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate krb5_keyblock *new_key;
967c478bd9Sstevel@tonic-gate krb5_error_code ret = 0;
977c478bd9Sstevel@tonic-gate if (!(new_key = (krb5_keyblock *) MALLOC(sizeof(krb5_keyblock))))
987c478bd9Sstevel@tonic-gate return (ENOMEM);
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate if (!(new_key->contents = (krb5_octet *)MALLOC(from->length))) {
1017c478bd9Sstevel@tonic-gate FREE(new_key, sizeof(krb5_keyblock));
1027c478bd9Sstevel@tonic-gate return (ENOMEM);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate ret = krb5_copy_keyblock_data(context, from, new_key);
1067c478bd9Sstevel@tonic-gate if (ret) {
1077c478bd9Sstevel@tonic-gate krb5_free_keyblock(context, new_key);
1087c478bd9Sstevel@tonic-gate return (ret);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate *to = new_key;
1127c478bd9Sstevel@tonic-gate return (ret);
1137c478bd9Sstevel@tonic-gate }
114