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