1 /* $NetBSD: set_keys.c,v 1.1.1.2 2014/04/24 12:45:49 pettai Exp $ */
4 * Copyright (c) 1997 - 2001, 2003 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "kadm5_locl.h"
41 * Set the keys of `ent' to the string-to-key of `password'
45 _kadm5_set_keys(kadm5_server_context
*context
,
53 ret
= hdb_generate_key_set_password(context
->context
,
55 password
, &keys
, &num_keys
);
59 _kadm5_free_keys (context
->context
, ent
->keys
.len
, ent
->keys
.val
);
61 ent
->keys
.len
= num_keys
;
63 hdb_entry_set_pw_change_time(context
->context
, ent
, 0);
65 if (krb5_config_get_bool_default(context
->context
, NULL
, FALSE
,
66 "kadmin", "save-password", NULL
))
68 ret
= hdb_entry_set_password(context
->context
, context
->db
,
78 * Set the keys of `ent' to (`n_key_data', `key_data')
82 _kadm5_set_keys2(kadm5_server_context
*context
,
85 krb5_key_data
*key_data
)
93 keys
= malloc (len
* sizeof(*keys
));
94 if (keys
== NULL
&& len
!= 0)
97 _kadm5_init_keys (keys
, len
);
99 for(i
= 0; i
< n_key_data
; i
++) {
100 keys
[i
].mkvno
= NULL
;
101 keys
[i
].key
.keytype
= key_data
[i
].key_data_type
[0];
102 ret
= krb5_data_copy(&keys
[i
].key
.keyvalue
,
103 key_data
[i
].key_data_contents
[0],
104 key_data
[i
].key_data_length
[0]);
107 if(key_data
[i
].key_data_ver
== 2) {
110 salt
= calloc(1, sizeof(*salt
));
116 salt
->type
= key_data
[i
].key_data_type
[1];
117 krb5_data_copy(&salt
->salt
,
118 key_data
[i
].key_data_contents
[1],
119 key_data
[i
].key_data_length
[1]);
123 _kadm5_free_keys (context
->context
, ent
->keys
.len
, ent
->keys
.val
);
125 ent
->keys
.val
= keys
;
127 hdb_entry_set_pw_change_time(context
->context
, ent
, 0);
128 hdb_entry_clear_password(context
->context
, ent
);
132 _kadm5_free_keys (context
->context
, len
, keys
);
137 * Set the keys of `ent' to `n_keys, keys'
141 _kadm5_set_keys3(kadm5_server_context
*context
,
144 krb5_keyblock
*keyblocks
)
152 keys
= malloc (len
* sizeof(*keys
));
153 if (keys
== NULL
&& len
!= 0)
156 _kadm5_init_keys (keys
, len
);
158 for(i
= 0; i
< n_keys
; i
++) {
159 keys
[i
].mkvno
= NULL
;
160 ret
= krb5_copy_keyblock_contents (context
->context
,
167 _kadm5_free_keys (context
->context
, ent
->keys
.len
, ent
->keys
.val
);
169 ent
->keys
.val
= keys
;
171 hdb_entry_set_pw_change_time(context
->context
, ent
, 0);
172 hdb_entry_clear_password(context
->context
, ent
);
176 _kadm5_free_keys (context
->context
, len
, keys
);
185 is_des_key_p(int keytype
)
187 return keytype
== ETYPE_DES_CBC_CRC
||
188 keytype
== ETYPE_DES_CBC_MD4
||
189 keytype
== ETYPE_DES_CBC_MD5
;
194 * Set the keys of `ent' to random keys and return them in `n_keys'
199 _kadm5_set_keys_randomly (kadm5_server_context
*context
,
201 krb5_keyblock
**new_keys
,
204 krb5_keyblock
*kblock
= NULL
;
210 ret
= hdb_generate_key_set(context
->context
, ent
->principal
,
211 &keys
, &num_keys
, 1);
215 kblock
= malloc(num_keys
* sizeof(kblock
[0]));
216 if (kblock
== NULL
) {
218 _kadm5_free_keys (context
->context
, num_keys
, keys
);
221 memset(kblock
, 0, num_keys
* sizeof(kblock
[0]));
224 for (i
= 0; i
< num_keys
; i
++) {
227 * To make sure all des keys are the the same we generate only
228 * the first one and then copy key to all other des keys.
231 if (des_keyblock
!= -1 && is_des_key_p(keys
[i
].key
.keytype
)) {
232 ret
= krb5_copy_keyblock_contents (context
->context
,
233 &kblock
[des_keyblock
],
237 kblock
[i
].keytype
= keys
[i
].key
.keytype
;
239 ret
= krb5_generate_random_keyblock (context
->context
,
245 if (is_des_key_p(keys
[i
].key
.keytype
))
249 ret
= krb5_copy_keyblock_contents (context
->context
,
258 for (i
= 0; i
< num_keys
; ++i
)
259 krb5_free_keyblock_contents (context
->context
, &kblock
[i
]);
261 _kadm5_free_keys (context
->context
, num_keys
, keys
);
265 _kadm5_free_keys (context
->context
, ent
->keys
.len
, ent
->keys
.val
);
266 ent
->keys
.val
= keys
;
267 ent
->keys
.len
= num_keys
;
271 hdb_entry_set_pw_change_time(context
->context
, ent
, 0);
272 hdb_entry_clear_password(context
->context
, ent
);