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  * Copyright (C) 1998 by the FundsXpress, INC.
9  *
10  * All rights reserved.
11  *
12  * Export of this software from the United States of America may require
13  * a specific license from the United States Government.  It is the
14  * responsibility of any person or organization contemplating export to
15  * 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 FundsXpress. not be used in advertising or publicity pertaining
23  * to distribution of the software without specific, written prior
24  * permission.  FundsXpress makes no representations about the suitability of
25  * this software for any purpose.  It is provided "as is" without express
26  * or implied warranty.
27  *
28  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
29  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
30  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
31  */
32 
33 #include <k5-int.h>
34 #include <etypes.h>
35 
36 krb5_error_code KRB5_CALLCONV
37 krb5_c_string_to_key_with_params(krb5_context context,
38                                  krb5_enctype enctype,
39                                  const krb5_data *string,
40                                  const krb5_data *salt,
41                                  const krb5_data *params,
42                                  krb5_keyblock *key);
43 
44 /*ARGSUSED*/
45 krb5_error_code KRB5_CALLCONV
46 krb5_c_string_to_key(krb5_context context, krb5_enctype enctype,
47                      const krb5_data *string, const krb5_data *salt,
48                      krb5_keyblock *key)
49 {
50     return krb5_c_string_to_key_with_params(context, enctype, string, salt,
51 					    NULL, key);
52 }
53 
54 krb5_error_code KRB5_CALLCONV
55 krb5_c_string_to_key_with_params(krb5_context context, krb5_enctype enctype,
56                                  const krb5_data *string,
57 				 const krb5_data *salt,
58 				 const krb5_data *params, krb5_keyblock *key)
59 {
60     int i;
61     krb5_error_code ret;
62     const struct krb5_enc_provider *enc;
63     size_t keybytes, keylength;
64 
65     for (i=0; i<krb5_enctypes_length; i++) {
66 	if (krb5_enctypes_list[i].etype == enctype)
67             break;
68     }
69 
70     if (i == krb5_enctypes_length)
71 	return(KRB5_BAD_ENCTYPE);
72 
73     enc = krb5_enctypes_list[i].enc;
74 /* xxx AFS string2key function is indicated by a special length  in
75 * the salt in much of the code.  However only the DES enctypes can
76 * deal with this.  Using s2kparams would be a much better solution.*/
77     if (salt && salt->length == SALT_TYPE_AFS_LENGTH) {
78         switch (enctype) {
79         case ENCTYPE_DES_CBC_CRC:
80         case ENCTYPE_DES_CBC_MD4:
81         case ENCTYPE_DES_CBC_MD5:
82             break;
83         default:
84             return (KRB5_CRYPTO_INTERNAL);
85         }
86     }
87 
88     keybytes = enc->keybytes;
89     keylength = enc->keylength;
90 
91     if ((key->contents = (krb5_octet *) malloc(keylength)) == NULL)
92 	return(ENOMEM);
93 
94     key->magic = KV5M_KEYBLOCK;
95     key->enctype = enctype;
96     key->length = keylength;
97     key->dk_list = NULL;
98     key->hKey = CK_INVALID_HANDLE;
99 
100     ret = (*krb5_enctypes_list[i].str2key)(context, enc, string, salt,
101 			params, key);
102     if (ret) {
103 	memset(key->contents, 0, keylength);
104 	free(key->contents);
105 	key->contents = NULL;
106     }
107 
108     return(ret);
109 }
110