2 * Copyright (c) 1997 - 2001, 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
34 #include "kadm5_locl.h"
36 __RCSID("$Heimdal: set_keys.c 15888 2005-08-11 13:40:35Z lha $"
40 * Set the keys of `ent' to the string-to-key of `password'
44 _kadm5_set_keys(kadm5_server_context
*context
,
52 ret
= hdb_generate_key_set_password(context
->context
,
54 password
, &keys
, &num_keys
);
58 _kadm5_free_keys (context
->context
, ent
->keys
.len
, ent
->keys
.val
);
60 ent
->keys
.len
= num_keys
;
62 hdb_entry_set_pw_change_time(context
->context
, ent
, 0);
64 if (krb5_config_get_bool_default(context
->context
, NULL
, FALSE
,
65 "kadmin", "save-password", NULL
))
67 ret
= hdb_entry_set_password(context
->context
, context
->db
,
77 * Set the keys of `ent' to (`n_key_data', `key_data')
81 _kadm5_set_keys2(kadm5_server_context
*context
,
84 krb5_key_data
*key_data
)
92 keys
= malloc (len
* sizeof(*keys
));
96 _kadm5_init_keys (keys
, len
);
98 for(i
= 0; i
< n_key_data
; i
++) {
100 keys
[i
].key
.keytype
= key_data
[i
].key_data_type
[0];
101 ret
= krb5_data_copy(&keys
[i
].key
.keyvalue
,
102 key_data
[i
].key_data_contents
[0],
103 key_data
[i
].key_data_length
[0]);
106 if(key_data
[i
].key_data_ver
== 2) {
109 salt
= malloc(sizeof(*salt
));
115 salt
->type
= key_data
[i
].key_data_type
[1];
116 krb5_data_copy(&salt
->salt
,
117 key_data
[i
].key_data_contents
[1],
118 key_data
[i
].key_data_length
[1]);
122 _kadm5_free_keys (context
->context
, ent
->keys
.len
, ent
->keys
.val
);
124 ent
->keys
.val
= keys
;
126 hdb_entry_set_pw_change_time(context
->context
, ent
, 0);
127 hdb_entry_clear_password(context
->context
, ent
);
131 _kadm5_free_keys (context
->context
, len
, keys
);
136 * Set the keys of `ent' to `n_keys, keys'
140 _kadm5_set_keys3(kadm5_server_context
*context
,
143 krb5_keyblock
*keyblocks
)
151 keys
= malloc (len
* sizeof(*keys
));
155 _kadm5_init_keys (keys
, len
);
157 for(i
= 0; i
< n_keys
; i
++) {
158 keys
[i
].mkvno
= NULL
;
159 ret
= krb5_copy_keyblock_contents (context
->context
,
166 _kadm5_free_keys (context
->context
, ent
->keys
.len
, ent
->keys
.val
);
168 ent
->keys
.val
= keys
;
170 hdb_entry_set_pw_change_time(context
->context
, ent
, 0);
171 hdb_entry_clear_password(context
->context
, ent
);
175 _kadm5_free_keys (context
->context
, len
, keys
);
184 is_des_key_p(int keytype
)
186 return keytype
== ETYPE_DES_CBC_CRC
||
187 keytype
== ETYPE_DES_CBC_MD4
||
188 keytype
== ETYPE_DES_CBC_MD5
;
193 * Set the keys of `ent' to random keys and return them in `n_keys'
198 _kadm5_set_keys_randomly (kadm5_server_context
*context
,
200 krb5_keyblock
**new_keys
,
203 krb5_keyblock
*kblock
= NULL
;
209 ret
= hdb_generate_key_set(context
->context
, ent
->principal
,
210 &keys
, &num_keys
, 1);
214 kblock
= malloc(num_keys
* sizeof(kblock
[0]));
215 if (kblock
== NULL
) {
217 _kadm5_free_keys (context
->context
, num_keys
, keys
);
220 memset(kblock
, 0, num_keys
* sizeof(kblock
[0]));
223 for (i
= 0; i
< num_keys
; i
++) {
226 * To make sure all des keys are the the same we generate only
227 * the first one and then copy key to all other des keys.
230 if (des_keyblock
!= -1 && is_des_key_p(keys
[i
].key
.keytype
)) {
231 ret
= krb5_copy_keyblock_contents (context
->context
,
232 &kblock
[des_keyblock
],
236 kblock
[i
].keytype
= keys
[i
].key
.keytype
;
238 ret
= krb5_generate_random_keyblock (context
->context
,
244 if (is_des_key_p(keys
[i
].key
.keytype
))
248 ret
= krb5_copy_keyblock_contents (context
->context
,
257 for (i
= 0; i
< num_keys
; ++i
)
258 krb5_free_keyblock_contents (context
->context
, &kblock
[i
]);
260 _kadm5_free_keys (context
->context
, num_keys
, keys
);
264 _kadm5_free_keys (context
->context
, ent
->keys
.len
, ent
->keys
.val
);
265 ent
->keys
.val
= keys
;
266 ent
->keys
.len
= num_keys
;
270 hdb_entry_set_pw_change_time(context
->context
, ent
, 0);
271 hdb_entry_clear_password(context
->context
, ent
);