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#ifdef HEIM_KRB5_ARCFOUR
37
38int krb5_heim_use_broken_arcfour_string2key = 0;
39
40static krb5_error_code
41ARCFOUR_string_to_key_broken(krb5_context context,
42                             krb5_enctype enctype,
43                             krb5_data password,
44                             krb5_salt salt,
45                             krb5_data opaque,
46                             krb5_keyblock *key)
47{
48    krb5_error_code ret;
49    size_t i;
50    CCDigestRef m;
51
52    m = CCDigestCreate(kCCDigestMD4);
53    if (m == NULL) {
54        ret = ENOMEM;
55        krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
56        goto out;
57    }
58
59    /* LE encoding */
60    for (i = 0; i < password.length; i++) {
61        unsigned char p;
62
63        p = ((const uint8_t *)password.data)[i] & 0xff;
64        CCDigestUpdate(m, &p, 1);
65        p = 0;
66        CCDigestUpdate(m, &p, 1);
67    }
68
69    key->keytype = enctype;
70    ret = krb5_data_alloc (&key->keyvalue, 16);
71    if (ret) {
72        krb5_set_error_message (context, ENOMEM, N_("malloc: out of memory", ""));
73        goto out;
74    }
75    CCDigestFinal(m, key->keyvalue.data);
76
77 out:
78    CCDigestDestroy(m);
79    return ret;
80}
81
82static krb5_error_code
83ARCFOUR_string_to_key(krb5_context context,
84		      krb5_enctype enctype,
85		      krb5_data password,
86		      krb5_salt salt,
87		      krb5_data opaque,
88		      krb5_keyblock *key)
89{
90    krb5_error_code ret;
91    uint16_t *s = NULL;
92    char *str = NULL;
93    size_t len = 0, i;
94    CCDigestRef m;
95
96    if (getenv("KRB5_USE_BROKEN_ARCFOUR_STRING2KEY") || krb5_heim_use_broken_arcfour_string2key)
97	return ARCFOUR_string_to_key_broken(context, enctype, password,
98					    salt, opaque, key);
99
100    str = malloc(password.length + 1);
101    if (str == NULL) {
102	ret = ENOMEM;
103	krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
104	return ret;
105    }
106    memcpy(str, password.data, password.length);
107    str[password.length] = '\0';
108
109    m = CCDigestCreate(kCCDigestMD4);
110    if (m == NULL) {
111	ret = ENOMEM;
112	krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
113	goto out;
114    }
115
116    ret = wind_utf8ucs2_length(str, &len);
117    if (ret) {
118	krb5_set_error_message (context, ret,
119				N_("Password not an UCS2 string", ""));
120	goto out;
121    }
122
123    s = malloc (len * sizeof(s[0]));
124    if (len != 0 && s == NULL) {
125	krb5_set_error_message (context, ENOMEM,
126				N_("malloc: out of memory", ""));
127	ret = ENOMEM;
128	goto out;
129    }
130
131    ret = wind_utf8ucs2(str, s, &len);
132    if (ret) {
133	krb5_set_error_message (context, ret,
134				N_("Password not an UCS2 string", ""));
135	goto out;
136    }
137
138    /* LE encoding */
139    for (i = 0; i < len; i++) {
140	unsigned char p;
141	p = (s[i] & 0xff);
142	CCDigestUpdate(m, &p, 1);
143	p = (s[i] >> 8) & 0xff;
144	CCDigestUpdate(m, &p, 1);
145    }
146
147    key->keytype = enctype;
148    ret = krb5_data_alloc (&key->keyvalue, 16);
149    if (ret) {
150	krb5_set_error_message (context, ENOMEM, N_("malloc: out of memory", ""));
151	goto out;
152    }
153    CCDigestFinal(m, key->keyvalue.data);
154
155 out:
156    CCDigestDestroy(m);
157    if (s) {
158	memset(s, 0, len);
159	free(s);
160    }
161    if (str)
162	free(str);
163    return ret;
164}
165
166struct salt_type _krb5_arcfour_salt[] = {
167    {
168	KRB5_PW_SALT,
169	"pw-salt",
170	ARCFOUR_string_to_key
171    },
172    { 0 }
173};
174
175#endif /* HEIM_KRB5_ARCFOUR */
176