1 /* $NetBSD: keys.c,v 1.3 2014/11/26 10:12:27 pettai Exp $ */
5 * Copyright (c) 1997 - 2001, 2003 - 2004 Kungliga Tekniska Högskolan
6 * (Royal Institute of Technology, Stockholm, Sweden).
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the Institute nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * free all the memory used by (len, keys)
44 hdb_free_keys (krb5_context context
, int len
, Key
*keys
)
48 for (i
= 0; i
< len
; i
++) {
51 if (keys
[i
].salt
!= NULL
) {
52 free_Salt(keys
[i
].salt
);
56 krb5_free_keyblock_contents(context
, &keys
[i
].key
);
62 * for each entry in `default_keys' try to parse it as a sequence
63 * of etype:salttype:salt, syntax of this if something like:
64 * [(des|des3|etype):](pw-salt|afs3)[:string], if etype is omitted it
65 * means all etypes, and if string is omitted is means the default
66 * string (for that principal). Additional special values:
69 * afs or afs3 == des:afs3-salt
72 static const krb5_enctype des_etypes
[] = {
78 static const krb5_enctype all_etypes
[] = {
79 ETYPE_AES256_CTS_HMAC_SHA1_96
,
81 ETYPE_ARCFOUR_HMAC_MD5
84 static krb5_error_code
85 parse_key_set(krb5_context context
, const char *key
,
86 krb5_enctype
**ret_enctypes
, size_t *ret_num_enctypes
,
87 krb5_salt
*salt
, krb5_principal principal
)
92 int i
, num_enctypes
= 0;
94 const krb5_enctype
*enctypes
= NULL
;
100 *ret_num_enctypes
= 0;
102 /* split p in a list of :-separated strings */
103 for(num_buf
= 0; num_buf
< 3; num_buf
++)
104 if(strsep_copy(&p
, ":", buf
[num_buf
], sizeof(buf
[num_buf
])) == -1)
107 salt
->saltvalue
.data
= NULL
;
108 salt
->saltvalue
.length
= 0;
110 for(i
= 0; i
< num_buf
; i
++) {
111 if(enctypes
== NULL
&& num_buf
> 1) {
112 /* this might be a etype specifier */
113 /* XXX there should be a string_to_etypes handling
114 special cases like `des' and `all' */
115 if(strcmp(buf
[i
], "des") == 0) {
116 enctypes
= des_etypes
;
117 num_enctypes
= sizeof(des_etypes
)/sizeof(des_etypes
[0]);
118 } else if(strcmp(buf
[i
], "des3") == 0) {
119 e
= ETYPE_DES3_CBC_SHA1
;
123 ret
= krb5_string_to_enctype(context
, buf
[i
], &e
);
132 if(salt
->salttype
== 0) {
133 /* interpret string as a salt specifier, if no etype
134 is set, this sets default values */
135 /* XXX should perhaps use string_to_salttype, but that
137 if(strcmp(buf
[i
], "pw-salt") == 0) {
138 if(enctypes
== NULL
) {
139 enctypes
= all_etypes
;
140 num_enctypes
= sizeof(all_etypes
)/sizeof(all_etypes
[0]);
142 salt
->salttype
= KRB5_PW_SALT
;
143 } else if(strcmp(buf
[i
], "afs3-salt") == 0) {
144 if(enctypes
== NULL
) {
145 enctypes
= des_etypes
;
146 num_enctypes
= sizeof(des_etypes
)/sizeof(des_etypes
[0]);
148 salt
->salttype
= KRB5_AFS3_SALT
;
154 /* if there is a final string, use it as the string to
155 salt with, this is mostly useful with null salt for
156 v4 compat, and a cell name for afs compat */
157 salt
->saltvalue
.data
= strdup(buf
[i
]);
158 if (salt
->saltvalue
.data
== NULL
) {
159 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
162 salt
->saltvalue
.length
= strlen(buf
[i
]);
166 if(enctypes
== NULL
|| salt
->salttype
== 0) {
167 krb5_set_error_message(context
, EINVAL
, "bad value for default_keys `%s'", key
);
171 /* if no salt was specified make up default salt */
172 if(salt
->saltvalue
.data
== NULL
) {
173 if(salt
->salttype
== KRB5_PW_SALT
)
174 ret
= krb5_get_pw_salt(context
, principal
, salt
);
175 else if(salt
->salttype
== KRB5_AFS3_SALT
) {
176 krb5_const_realm realm
= krb5_principal_get_realm(context
, principal
);
177 salt
->saltvalue
.data
= strdup(realm
);
178 if(salt
->saltvalue
.data
== NULL
) {
179 krb5_set_error_message(context
, ENOMEM
,
180 "out of memory while "
181 "parsing salt specifiers");
184 strlwr(salt
->saltvalue
.data
);
185 salt
->saltvalue
.length
= strlen(realm
);
189 *ret_enctypes
= malloc(sizeof(enctypes
[0]) * num_enctypes
);
190 if (*ret_enctypes
== NULL
) {
191 krb5_free_salt(context
, *salt
);
192 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
195 memcpy(*ret_enctypes
, enctypes
, sizeof(enctypes
[0]) * num_enctypes
);
196 *ret_num_enctypes
= num_enctypes
;
201 static krb5_error_code
202 add_enctype_to_key_set(Key
**key_set
, size_t *nkeyset
,
203 krb5_enctype enctype
, krb5_salt
*salt
)
208 memset(&key
, 0, sizeof(key
));
210 tmp
= realloc(*key_set
, (*nkeyset
+ 1) * sizeof((*key_set
)[0]));
216 key
.key
.keytype
= enctype
;
217 key
.key
.keyvalue
.length
= 0;
218 key
.key
.keyvalue
.data
= NULL
;
221 key
.salt
= calloc(1, sizeof(*key
.salt
));
222 if (key
.salt
== NULL
) {
227 key
.salt
->type
= salt
->salttype
;
228 krb5_data_zero (&key
.salt
->salt
);
230 ret
= krb5_data_copy(&key
.salt
->salt
,
231 salt
->saltvalue
.data
,
232 salt
->saltvalue
.length
);
240 (*key_set
)[*nkeyset
] = key
;
249 * Generate the `key_set' from the [kadmin]default_keys statement. If
250 * `no_salt' is set, salt is not important (and will not be set) since
251 * it's random keys that is going to be created.
255 hdb_generate_key_set(krb5_context context
, krb5_principal principal
,
256 Key
**ret_key_set
, size_t *nkeyset
, int no_salt
)
262 static const char *default_keytypes
[] = {
263 "aes256-cts-hmac-sha1-96:pw-salt",
264 "des3-cbc-sha1:pw-salt",
265 "arcfour-hmac-md5:pw-salt",
269 ktypes
= krb5_config_get_strings(context
, NULL
, "kadmin",
270 "default_keys", NULL
);
272 ktypes
= (char **)(intptr_t)default_keytypes
;
274 *ret_key_set
= key_set
= NULL
;
279 for(kp
= ktypes
; kp
&& *kp
; kp
++) {
282 krb5_enctype
*enctypes
;
287 if(strcmp(p
, "v5") == 0)
289 else if(strcmp(p
, "v4") == 0)
291 else if(strcmp(p
, "afs") == 0 || strcmp(p
, "afs3") == 0)
293 else if (strcmp(p
, "arcfour-hmac-md5") == 0)
294 p
= "arcfour-hmac-md5:pw-salt";
296 memset(&salt
, 0, sizeof(salt
));
298 ret
= parse_key_set(context
, p
,
299 &enctypes
, &num_enctypes
, &salt
, principal
);
301 krb5_warn(context
, ret
, "bad value for default_keys `%s'", *kp
);
306 for (i
= 0; i
< num_enctypes
; i
++) {
307 /* find duplicates */
308 for (j
= 0; j
< *nkeyset
; j
++) {
312 if (k
->key
.keytype
== enctypes
[i
]) {
315 if (k
->salt
== NULL
&& salt
.salttype
== KRB5_PW_SALT
)
317 if (k
->salt
->type
== salt
.salttype
&&
318 k
->salt
->salt
.length
== salt
.saltvalue
.length
&&
319 memcmp(k
->salt
->salt
.data
, salt
.saltvalue
.data
,
320 salt
.saltvalue
.length
) == 0)
324 /* not a duplicate, lets add it */
326 ret
= add_enctype_to_key_set(&key_set
, nkeyset
, enctypes
[i
],
327 no_salt
? NULL
: &salt
);
330 krb5_free_salt(context
, salt
);
336 krb5_free_salt(context
, salt
);
339 *ret_key_set
= key_set
;
342 if (ktypes
!= (char **)(intptr_t)default_keytypes
)
343 krb5_config_free_strings(ktypes
);
346 krb5_warn(context
, ret
,
347 "failed to parse the [kadmin]default_keys values");
349 for (i
= 0; i
< *nkeyset
; i
++)
350 free_Key(&key_set
[i
]);
352 } else if (*nkeyset
== 0) {
354 "failed to parse any of the [kadmin]default_keys values");
355 ret
= EINVAL
; /* XXX */
363 hdb_generate_key_set_password(krb5_context context
,
364 krb5_principal principal
,
365 const char *password
,
366 Key
**keys
, size_t *num_keys
)
371 ret
= hdb_generate_key_set(context
, principal
,
376 for (i
= 0; i
< (*num_keys
); i
++) {
379 salt
.salttype
= (*keys
)[i
].salt
->type
;
380 salt
.saltvalue
.length
= (*keys
)[i
].salt
->salt
.length
;
381 salt
.saltvalue
.data
= (*keys
)[i
].salt
->salt
.data
;
383 ret
= krb5_string_to_key_salt (context
,
384 (*keys
)[i
].key
.keytype
,
394 hdb_free_keys (context
, *num_keys
, *keys
);