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    size_t len, i;
93    CCDigestRef m;
94
95    if (getenv("KRB5_USE_BROKEN_ARCFOUR_STRING2KEY") || krb5_heim_use_broken_arcfour_string2key)
96	return ARCFOUR_string_to_key_broken(context, enctype, password,
97					    salt, opaque, key);
98
99    m = CCDigestCreate(kCCDigestMD4);
100    if (m == NULL) {
101	ret = ENOMEM;
102	krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
103	goto out;
104    }
105
106    ret = wind_utf8ucs2_length(password.data, &len);
107    if (ret) {
108	krb5_set_error_message (context, ret,
109				N_("Password not an UCS2 string", ""));
110	goto out;
111    }
112
113    s = malloc (len * sizeof(s[0]));
114    if (len != 0 && s == NULL) {
115	krb5_set_error_message (context, ENOMEM,
116				N_("malloc: out of memory", ""));
117	ret = ENOMEM;
118	goto out;
119    }
120
121    ret = wind_utf8ucs2(password.data, s, &len);
122    if (ret) {
123	krb5_set_error_message (context, ret,
124				N_("Password not an UCS2 string", ""));
125	goto out;
126    }
127
128    /* LE encoding */
129    for (i = 0; i < len; i++) {
130	unsigned char p;
131	p = (s[i] & 0xff);
132	CCDigestUpdate(m, &p, 1);
133	p = (s[i] >> 8) & 0xff;
134	CCDigestUpdate(m, &p, 1);
135    }
136
137    key->keytype = enctype;
138    ret = krb5_data_alloc (&key->keyvalue, 16);
139    if (ret) {
140	krb5_set_error_message (context, ENOMEM, N_("malloc: out of memory", ""));
141	goto out;
142    }
143    CCDigestFinal(m, key->keyvalue.data);
144
145 out:
146    CCDigestDestroy(m);
147    if (s)
148	memset (s, 0, len);
149    free (s);
150    return ret;
151}
152
153struct salt_type _krb5_arcfour_salt[] = {
154    {
155	KRB5_PW_SALT,
156	"pw-salt",
157	ARCFOUR_string_to_key
158    },
159    { 0 }
160};
161
162#endif /* HEIM_KRB5_ARCFOUR */
163