xref: /illumos-gate/usr/src/lib/gss_mechs/mech_krb5/crypto/make_random_key.c (revision 55fea89dcaa64928bed4327112404dcb3e07b79f)
17c478bd9Sstevel@tonic-gate /*
2*5e01956fSGlenn Barry  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
37c478bd9Sstevel@tonic-gate  */
47c478bd9Sstevel@tonic-gate 
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright (C) 1998 by the FundsXpress, INC.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * All rights reserved.
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may require
127c478bd9Sstevel@tonic-gate  * a specific license from the United States Government.  It is the
137c478bd9Sstevel@tonic-gate  * responsibility of any person or organization contemplating export to
147c478bd9Sstevel@tonic-gate  * obtain such a license before exporting.
157c478bd9Sstevel@tonic-gate  *
167c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
177c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
187c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
197c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
207c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
217c478bd9Sstevel@tonic-gate  * the name of FundsXpress. not be used in advertising or publicity pertaining
227c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
237c478bd9Sstevel@tonic-gate  * permission.  FundsXpress makes no representations about the suitability of
247c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
257c478bd9Sstevel@tonic-gate  * or implied warranty.
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
287c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
297c478bd9Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
32159d09a2SMark Phalan #include "k5-int.h"
33159d09a2SMark Phalan #include "etypes.h"
34*5e01956fSGlenn Barry #include <locale.h>
357c478bd9Sstevel@tonic-gate 
36505d05c7Sgtb krb5_error_code KRB5_CALLCONV
krb5_c_make_random_key(krb5_context context,krb5_enctype enctype,krb5_keyblock * random_key)37505d05c7Sgtb krb5_c_make_random_key(krb5_context context, krb5_enctype enctype,
38505d05c7Sgtb 		       krb5_keyblock *random_key)
397c478bd9Sstevel@tonic-gate {
407c478bd9Sstevel@tonic-gate     int i;
417c478bd9Sstevel@tonic-gate     krb5_error_code ret;
427c478bd9Sstevel@tonic-gate     const struct krb5_enc_provider *enc;
437c478bd9Sstevel@tonic-gate     size_t keybytes, keylength;
44505d05c7Sgtb     krb5_data random_data;
457c478bd9Sstevel@tonic-gate     unsigned char *bytes;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate     for (i=0; i<krb5_enctypes_length; i++) {
487c478bd9Sstevel@tonic-gate 	if (krb5_enctypes_list[i].etype == enctype)
497c478bd9Sstevel@tonic-gate 	    break;
507c478bd9Sstevel@tonic-gate     }
517c478bd9Sstevel@tonic-gate 
527c64d375Smp153739     /* Solaris Kerberos: Better error message */
537c64d375Smp153739     if (i == krb5_enctypes_length) {
547c64d375Smp153739 	krb5_set_error_message(context, KRB5_BAD_ENCTYPE,
55*5e01956fSGlenn Barry 			    dgettext(TEXT_DOMAIN,
56*5e01956fSGlenn Barry 				    "Unknown encryption type: %d"),
57*5e01956fSGlenn Barry 			    enctype);
587c478bd9Sstevel@tonic-gate 	return(KRB5_BAD_ENCTYPE);
597c64d375Smp153739     }
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate     enc = krb5_enctypes_list[i].enc;
627c478bd9Sstevel@tonic-gate 
63505d05c7Sgtb     keybytes = enc->keybytes;
64505d05c7Sgtb     keylength = enc->keylength;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate     if ((bytes = (unsigned char *) malloc(keybytes)) == NULL)
677c478bd9Sstevel@tonic-gate 	return(ENOMEM);
687c478bd9Sstevel@tonic-gate     if ((random_key->contents = (krb5_octet *) malloc(keylength)) == NULL) {
697c478bd9Sstevel@tonic-gate 	free(bytes);
707c478bd9Sstevel@tonic-gate 	return(ENOMEM);
717c478bd9Sstevel@tonic-gate     }
727c478bd9Sstevel@tonic-gate 
73505d05c7Sgtb     random_data.data = (char *) bytes;
74505d05c7Sgtb     random_data.length = keybytes;
757c478bd9Sstevel@tonic-gate 
76505d05c7Sgtb     if ((ret = krb5_c_random_make_octets(context, &random_data)))
777c478bd9Sstevel@tonic-gate 	goto cleanup;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate     random_key->magic = KV5M_KEYBLOCK;
807c478bd9Sstevel@tonic-gate     random_key->enctype = enctype;
817c478bd9Sstevel@tonic-gate     random_key->length = keylength;
82159d09a2SMark Phalan 
83159d09a2SMark Phalan     /* Solaris Kerberos */
847c478bd9Sstevel@tonic-gate     random_key->dk_list = NULL;
857c478bd9Sstevel@tonic-gate #ifdef _KERNEL
867c478bd9Sstevel@tonic-gate     random_key->kef_key = NULL;
877c478bd9Sstevel@tonic-gate #else
887c478bd9Sstevel@tonic-gate     random_key->hKey = CK_INVALID_HANDLE;
897c478bd9Sstevel@tonic-gate #endif
907c478bd9Sstevel@tonic-gate 
91159d09a2SMark Phalan     /* Solaris Kerberos */
92505d05c7Sgtb     ret = ((*(enc->make_key))(context, &random_data, random_key));
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate cleanup:
957c478bd9Sstevel@tonic-gate     memset(bytes, 0, keybytes);
967c478bd9Sstevel@tonic-gate     free(bytes);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate     if (ret) {
997c478bd9Sstevel@tonic-gate 	memset(random_key->contents, 0, keylength);
1007c478bd9Sstevel@tonic-gate 	free(random_key->contents);
101159d09a2SMark Phalan 	/* Solaris Kerberos */
1027c478bd9Sstevel@tonic-gate 	random_key->contents = NULL;
1037c478bd9Sstevel@tonic-gate     }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate     return(ret);
1067c478bd9Sstevel@tonic-gate }
107