Debian Codegraph @

lib/krb5/salt.c

raw
1 /*
2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include "krb5_locl.h"
35
36 /* coverity[+alloc : arg-*3] */
37 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
38 krb5_salttype_to_string (krb5_context context,
39 krb5_enctype etype,
40 krb5_salttype stype,
41 char **string)
42 {
43 struct _krb5_encryption_type *e;
44 struct salt_type *st;
45
46 *string = NULL;
47 e = _krb5_find_enctype (etype);
48 if (e == NULL) {
49 krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
50 "encryption type %d not supported",
51 etype);
52 return KRB5_PROG_ETYPE_NOSUPP;
53 }
54 for (st = e->keytype->string_to_key; st && st->type; st++) {
55 if (st->type == stype) {
56 *string = strdup (st->name);
57 if (*string == NULL)
58 return krb5_enomem(context);
59 return 0;
60 }
61 }
62 krb5_set_error_message (context, HEIM_ERR_SALTTYPE_NOSUPP,
63 "salttype %d not supported", stype);
64 return HEIM_ERR_SALTTYPE_NOSUPP;
65 }
66
67 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
68 krb5_string_to_salttype (krb5_context context,
69 krb5_enctype etype,
70 const char *string,
71 krb5_salttype *salttype)
72 {
73 struct _krb5_encryption_type *e;
74 struct salt_type *st;
75
76 e = _krb5_find_enctype (etype);
77 if (e == NULL) {
78 krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
79 N_("encryption type %d not supported", ""),
80 etype);
81 return KRB5_PROG_ETYPE_NOSUPP;
82 }
83 for (st = e->keytype->string_to_key; st && st->type; st++) {
84 if (strcasecmp (st->name, string) == 0) {
85 *salttype = st->type;
86 return 0;
87 }
88 }
89 krb5_set_error_message(context, HEIM_ERR_SALTTYPE_NOSUPP,
90 N_("salttype %s not supported", ""), string);
91 return HEIM_ERR_SALTTYPE_NOSUPP;
92 }
93
94 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
95 krb5_get_pw_salt(krb5_context context,
96 krb5_const_principal principal,
97 krb5_salt *salt)
98 {
99 size_t len;
100 size_t i;
101 krb5_error_code ret;
102 char *p;
103
104 salt->salttype = KRB5_PW_SALT;
105 len = strlen(principal->realm);
106 for (i = 0; i < principal->name.name_string.len; ++i)
107 len += strlen(principal->name.name_string.val[i]);
108 ret = krb5_data_alloc (&salt->saltvalue, len);
109 if (ret)
110 return ret;
111 p = salt->saltvalue.data;
112 memcpy (p, principal->realm, strlen(principal->realm));
113 p += strlen(principal->realm);
114 for (i = 0; i < principal->name.name_string.len; ++i) {
115 memcpy (p,
116 principal->name.name_string.val[i],
117 strlen(principal->name.name_string.val[i]));
118 p += strlen(principal->name.name_string.val[i]);
119 }
120 return 0;
121 }
122
123 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
124 krb5_free_salt(krb5_context context,
125 krb5_salt salt)
126 {
127 krb5_data_free(&salt.saltvalue);
128 return 0;
129 }
130
131 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
132 krb5_string_to_key_data (krb5_context context,
133 krb5_enctype enctype,
134 krb5_data password,
135 krb5_principal principal,
136 krb5_keyblock *key)
137 {
138 krb5_error_code ret;
139 krb5_salt salt;
140
141 ret = krb5_get_pw_salt(context, principal, &salt);
142 if(ret)
143 return ret;
144 ret = krb5_string_to_key_data_salt(context, enctype, password, salt, key);
145 krb5_free_salt(context, salt);
146 return ret;
147 }
148
149 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
150 krb5_string_to_key (krb5_context context,
151 krb5_enctype enctype,
152 const char *password,
153 krb5_principal principal,
154 krb5_keyblock *key)
155 {
156 krb5_data pw;
157 pw.data = rk_UNCONST(password);
158 pw.length = strlen(password);
159 return krb5_string_to_key_data(context, enctype, pw, principal, key);
160 }
161
162 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
163 krb5_string_to_key_data_salt (krb5_context context,
164 krb5_enctype enctype,
165 krb5_data password,
166 krb5_salt salt,
167 krb5_keyblock *key)
168 {
169 krb5_data opaque;
170 krb5_data_zero(&opaque);
171 return krb5_string_to_key_data_salt_opaque(context, enctype, password,
172 salt, opaque, key);
173 }
174
175 /*
176 * Do a string -> key for encryption type `enctype' operation on
177 * `password' (with salt `salt' and the enctype specific data string
178 * `opaque'), returning the resulting key in `key'
179 */
180
181 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
182 krb5_string_to_key_data_salt_opaque (krb5_context context,
183 krb5_enctype enctype,
184 krb5_data password,
185 krb5_salt salt,
186 krb5_data opaque,
187 krb5_keyblock *key)
188 {
189 struct _krb5_encryption_type *et =_krb5_find_enctype(enctype);
190 struct salt_type *st;
191 if(et == NULL) {
192 krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
193 N_("encryption type %d not supported", ""),
194 enctype);
195 return KRB5_PROG_ETYPE_NOSUPP;
196 }
197 for(st = et->keytype->string_to_key; st && st->type; st++)
198 if(st->type == salt.salttype)
199 return (*st->string_to_key)(context, enctype, password,
200 salt, opaque, key);
201 krb5_set_error_message(context, HEIM_ERR_SALTTYPE_NOSUPP,
202 N_("salt type %d not supported", ""),
203 salt.salttype);
204 return HEIM_ERR_SALTTYPE_NOSUPP;
205 }
206
207 /*
208 * Do a string -> key for encryption type `enctype' operation on the
209 * string `password' (with salt `salt'), returning the resulting key
210 * in `key'
211 */
212
213 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
214 krb5_string_to_key_salt (krb5_context context,
215 krb5_enctype enctype,
216 const char *password,
217 krb5_salt salt,
218 krb5_keyblock *key)
219 {
220 krb5_data pw;
221 pw.data = rk_UNCONST(password);
222 pw.length = strlen(password);
223 return krb5_string_to_key_data_salt(context, enctype, pw, salt, key);
224 }
225
226 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
227 krb5_string_to_key_salt_opaque (krb5_context context,
228 krb5_enctype enctype,
229 const char *password,
230 krb5_salt salt,
231 krb5_data opaque,
232 krb5_keyblock *key)
233 {
234 krb5_data pw;
235 pw.data = rk_UNCONST(password);
236 pw.length = strlen(password);
237 return krb5_string_to_key_data_salt_opaque(context, enctype,
238 pw, salt, opaque, key);
239 }
240
241
242 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
243 krb5_string_to_key_derived(krb5_context context,
244 const void *str,
245 size_t len,
246 krb5_enctype etype,
247 krb5_keyblock *key)
248 {
249 struct _krb5_encryption_type *et = _krb5_find_enctype(etype);
250 krb5_error_code ret;
251 struct _krb5_key_data kd;
252 size_t keylen;
253 u_char *tmp;
254
255 if(et == NULL) {
256 krb5_set_error_message (context, KRB5_PROG_ETYPE_NOSUPP,
257 N_("encryption type %d not supported", ""),
258 etype);
259 return KRB5_PROG_ETYPE_NOSUPP;
260 }
261 keylen = et->keytype->bits / 8;
262
263 ALLOC(kd.key, 1);
264 if (kd.key == NULL)
265 return krb5_enomem(context);
266 ret = krb5_data_alloc(&kd.key->keyvalue, et->keytype->size);
267 if(ret) {
268 free(kd.key);
269 return ret;
270 }
271 kd.key->keytype = etype;
272 tmp = malloc (keylen);
273 if(tmp == NULL) {
274 krb5_free_keyblock(context, kd.key);
275 return krb5_enomem(context);
276 }
277 ret = _krb5_n_fold(str, len, tmp, keylen);
278 if (ret) {
279 free(tmp);
280 krb5_enomem(context);
281 return ret;
282 }
283 kd.schedule = NULL;
284 _krb5_DES3_random_to_key(context, kd.key, tmp, keylen);
285 memset(tmp, 0, keylen);
286 free(tmp);
287 ret = _krb5_derive_key(context,
288 et,
289 &kd,
290 "kerberos", /* XXX well known constant */
291 strlen("kerberos"));
292 if (ret) {
293 _krb5_free_key_data(context, &kd, et);
294 return ret;
295 }
296 ret = krb5_copy_keyblock_contents(context, kd.key, key);
297 _krb5_free_key_data(context, &kd, et);
298 return ret;
299 }