2 * Copyright (c) 2000 - 2004 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
41 struct hdb_master_key_data
{
42 krb5_keytab_entry keytab
;
44 struct hdb_master_key_data
*next
;
48 hdb_free_master_key(krb5_context context
, hdb_master_key mkey
)
50 struct hdb_master_key_data
*ptr
;
52 krb5_kt_free_entry(context
, &mkey
->keytab
);
54 krb5_crypto_destroy(context
, mkey
->crypto
);
62 hdb_process_master_key(krb5_context context
,
63 int kvno
, krb5_keyblock
*key
, krb5_enctype etype
,
68 *mkey
= calloc(1, sizeof(**mkey
));
70 krb5_set_error_string(context
, "malloc: out of memory");
73 (*mkey
)->keytab
.vno
= kvno
;
74 ret
= krb5_parse_name(context
, "K/M", &(*mkey
)->keytab
.principal
);
77 ret
= krb5_copy_keyblock_contents(context
, key
, &(*mkey
)->keytab
.keyblock
);
81 (*mkey
)->keytab
.keyblock
.keytype
= etype
;
82 (*mkey
)->keytab
.timestamp
= time(NULL
);
83 ret
= krb5_crypto_init(context
, key
, etype
, &(*mkey
)->crypto
);
88 hdb_free_master_key(context
, *mkey
);
94 hdb_add_master_key(krb5_context context
, krb5_keyblock
*key
,
95 hdb_master_key
*inout
)
101 for(p
= *inout
; p
; p
= p
->next
)
102 vno
= max(vno
, p
->keytab
.vno
);
104 ret
= hdb_process_master_key(context
, vno
, key
, 0, &p
);
112 static krb5_error_code
113 read_master_keytab(krb5_context context
, const char *filename
,
114 hdb_master_key
*mkey
)
118 krb5_kt_cursor cursor
;
119 krb5_keytab_entry entry
;
122 ret
= krb5_kt_resolve(context
, filename
, &id
);
126 ret
= krb5_kt_start_seq_get(context
, id
, &cursor
);
130 while(krb5_kt_next_entry(context
, id
, &entry
, &cursor
) == 0) {
131 p
= calloc(1, sizeof(*p
));
133 krb5_kt_end_seq_get(context
, id
, &cursor
);
138 ret
= krb5_crypto_init(context
, &p
->keytab
.keyblock
, 0, &p
->crypto
);
142 krb5_kt_end_seq_get(context
, id
, &cursor
);
144 krb5_kt_close(context
, id
);
148 /* read a MIT master keyfile */
149 static krb5_error_code
150 read_master_mit(krb5_context context
, const char *filename
,
151 hdb_master_key
*mkey
)
159 fd
= open(filename
, O_RDONLY
| O_BINARY
);
161 int save_errno
= errno
;
162 krb5_set_error_string(context
, "failed to open %s: %s", filename
,
163 strerror(save_errno
));
166 sp
= krb5_storage_from_fd(fd
);
171 krb5_storage_set_flags(sp
, KRB5_STORAGE_HOST_BYTEORDER
);
173 /* could possibly use ret_keyblock here, but do it with more
175 ret
= krb5_ret_keyblock(sp
, &key
);
177 ret
= krb5_ret_int16(sp
, &enctype
);
178 if((htons(enctype
) & 0xff00) == 0x3000) {
179 krb5_set_error_string(context
, "unknown keytype in %s: %#x, expected %#x",
180 filename
, htons(enctype
), 0x3000);
181 ret
= HEIM_ERR_BAD_MKEY
;
184 key
.keytype
= enctype
;
185 ret
= krb5_ret_data(sp
, &key
.keyvalue
);
189 ret
= hdb_process_master_key(context
, 0, &key
, 0, mkey
);
190 krb5_free_keyblock_contents(context
, &key
);
192 krb5_storage_free(sp
);
197 /* read an old master key file */
198 static krb5_error_code
199 read_master_encryptionkey(krb5_context context
, const char *filename
,
200 hdb_master_key
*mkey
)
205 unsigned char buf
[256];
209 fd
= open(filename
, O_RDONLY
| O_BINARY
);
211 int save_errno
= errno
;
212 krb5_set_error_string(context
, "failed to open %s: %s",
213 filename
, strerror(save_errno
));
217 len
= read(fd
, buf
, sizeof(buf
));
220 int save_errno
= errno
;
221 krb5_set_error_string(context
, "error reading %s: %s",
222 filename
, strerror(save_errno
));
226 ret
= decode_EncryptionKey(buf
, len
, &key
, &ret_len
);
227 memset(buf
, 0, sizeof(buf
));
231 /* Originally, the keytype was just that, and later it got changed
232 to des-cbc-md5, but we always used des in cfb64 mode. This
233 should cover all cases, but will break if someone has hacked
234 this code to really use des-cbc-md5 -- but then that's not my
236 if(key
.keytype
== KEYTYPE_DES
|| key
.keytype
== ETYPE_DES_CBC_MD5
)
237 key
.keytype
= ETYPE_DES_CFB64_NONE
;
239 ret
= hdb_process_master_key(context
, 0, &key
, 0, mkey
);
240 krb5_free_keyblock_contents(context
, &key
);
244 /* read a krb4 /.k style file */
245 static krb5_error_code
246 read_master_krb4(krb5_context context
, const char *filename
,
247 hdb_master_key
*mkey
)
252 unsigned char buf
[256];
255 fd
= open(filename
, O_RDONLY
| O_BINARY
);
257 int save_errno
= errno
;
258 krb5_set_error_string(context
, "failed to open %s: %s",
259 filename
, strerror(save_errno
));
263 len
= read(fd
, buf
, sizeof(buf
));
266 int save_errno
= errno
;
267 krb5_set_error_string(context
, "error reading %s: %s",
268 filename
, strerror(save_errno
));
272 krb5_set_error_string(context
, "bad contents of %s", filename
);
273 return HEIM_ERR_EOF
; /* XXX file might be too large */
276 memset(&key
, 0, sizeof(key
));
277 key
.keytype
= ETYPE_DES_PCBC_NONE
;
278 ret
= krb5_data_copy(&key
.keyvalue
, buf
, len
);
279 memset(buf
, 0, sizeof(buf
));
283 ret
= hdb_process_master_key(context
, 0, &key
, 0, mkey
);
284 krb5_free_keyblock_contents(context
, &key
);
289 hdb_read_master_key(krb5_context context
, const char *filename
,
290 hdb_master_key
*mkey
)
293 unsigned char buf
[16];
301 filename
= HDB_DB_DIR
"/m-key";
303 f
= fopen(filename
, "r");
305 int save_errno
= errno
;
306 krb5_set_error_string(context
, "failed to open %s: %s",
307 filename
, strerror(save_errno
));
311 if(fread(buf
, 1, 2, f
) != 2) {
312 krb5_set_error_string(context
, "end of file reading %s", filename
);
317 fseek(f
, 0, SEEK_END
);
327 ret
= read_master_krb4(context
, filename
, mkey
);
328 } else if(buf
[0] == 0x30 && len
<= 127 && buf
[1] == len
- 2) {
329 ret
= read_master_encryptionkey(context
, filename
, mkey
);
330 } else if(buf
[0] == 5 && buf
[1] >= 1 && buf
[1] <= 2) {
331 ret
= read_master_keytab(context
, filename
, mkey
);
333 ret
= read_master_mit(context
, filename
, mkey
);
339 hdb_write_master_key(krb5_context context
, const char *filename
,
347 filename
= HDB_DB_DIR
"/m-key";
349 ret
= krb5_kt_resolve(context
, filename
, &kt
);
353 for(p
= mkey
; p
; p
= p
->next
) {
354 ret
= krb5_kt_add_entry(context
, kt
, &p
->keytab
);
357 krb5_kt_close(context
, kt
);
363 _hdb_find_master_key(uint32_t *mkvno
, hdb_master_key mkey
)
365 hdb_master_key ret
= NULL
;
367 if(ret
== NULL
&& mkey
->keytab
.vno
== 0)
370 if(ret
== NULL
|| mkey
->keytab
.vno
> ret
->keytab
.vno
)
372 } else if(mkey
->keytab
.vno
== *mkvno
)
380 _hdb_mkey_version(hdb_master_key mkey
)
382 return mkey
->keytab
.vno
;
386 _hdb_mkey_decrypt(krb5_context context
, hdb_master_key key
,
387 krb5_key_usage usage
,
388 void *ptr
, size_t size
, krb5_data
*res
)
390 return krb5_decrypt(context
, key
->crypto
, usage
,
395 _hdb_mkey_encrypt(krb5_context context
, hdb_master_key key
,
396 krb5_key_usage usage
,
397 const void *ptr
, size_t size
, krb5_data
*res
)
399 return krb5_encrypt(context
, key
->crypto
, usage
,
404 hdb_unseal_key_mkey(krb5_context context
, Key
*k
, hdb_master_key mkey
)
416 key
= _hdb_find_master_key(k
->mkvno
, mkey
);
419 return HDB_ERR_NO_MKEY
;
421 ret
= _hdb_mkey_decrypt(context
, key
, HDB_KU_MKEY
,
422 k
->key
.keyvalue
.data
,
423 k
->key
.keyvalue
.length
,
425 if(ret
== KRB5KRB_AP_ERR_BAD_INTEGRITY
) {
426 /* try to decrypt with MIT key usage */
427 ret
= _hdb_mkey_decrypt(context
, key
, 0,
428 k
->key
.keyvalue
.data
,
429 k
->key
.keyvalue
.length
,
435 /* fixup keylength if the key got padded when encrypting it */
436 ret
= krb5_enctype_keysize(context
, k
->key
.keytype
, &keysize
);
438 krb5_data_free(&res
);
441 if (keysize
> res
.length
) {
442 krb5_data_free(&res
);
443 return KRB5_BAD_KEYSIZE
;
446 memset(k
->key
.keyvalue
.data
, 0, k
->key
.keyvalue
.length
);
447 free(k
->key
.keyvalue
.data
);
448 k
->key
.keyvalue
= res
;
449 k
->key
.keyvalue
.length
= keysize
;
457 hdb_unseal_keys_mkey(krb5_context context
, hdb_entry
*ent
, hdb_master_key mkey
)
461 for(i
= 0; i
< ent
->keys
.len
; i
++){
464 ret
= hdb_unseal_key_mkey(context
, &ent
->keys
.val
[i
], mkey
);
472 hdb_unseal_keys(krb5_context context
, HDB
*db
, hdb_entry
*ent
)
474 if (db
->hdb_master_key_set
== 0)
476 return hdb_unseal_keys_mkey(context
, ent
, db
->hdb_master_key
);
480 hdb_unseal_key(krb5_context context
, HDB
*db
, Key
*k
)
482 if (db
->hdb_master_key_set
== 0)
484 return hdb_unseal_key_mkey(context
, k
, db
->hdb_master_key
);
488 hdb_seal_key_mkey(krb5_context context
, Key
*k
, hdb_master_key mkey
)
497 key
= _hdb_find_master_key(k
->mkvno
, mkey
);
500 return HDB_ERR_NO_MKEY
;
502 ret
= _hdb_mkey_encrypt(context
, key
, HDB_KU_MKEY
,
503 k
->key
.keyvalue
.data
,
504 k
->key
.keyvalue
.length
,
509 memset(k
->key
.keyvalue
.data
, 0, k
->key
.keyvalue
.length
);
510 free(k
->key
.keyvalue
.data
);
511 k
->key
.keyvalue
= res
;
513 if (k
->mkvno
== NULL
) {
514 k
->mkvno
= malloc(sizeof(*k
->mkvno
));
515 if (k
->mkvno
== NULL
)
518 *k
->mkvno
= key
->keytab
.vno
;
524 hdb_seal_keys_mkey(krb5_context context
, hdb_entry
*ent
, hdb_master_key mkey
)
527 for(i
= 0; i
< ent
->keys
.len
; i
++){
530 ret
= hdb_seal_key_mkey(context
, &ent
->keys
.val
[i
], mkey
);
538 hdb_seal_keys(krb5_context context
, HDB
*db
, hdb_entry
*ent
)
540 if (db
->hdb_master_key_set
== 0)
543 return hdb_seal_keys_mkey(context
, ent
, db
->hdb_master_key
);
547 hdb_seal_key(krb5_context context
, HDB
*db
, Key
*k
)
549 if (db
->hdb_master_key_set
== 0)
552 return hdb_seal_key_mkey(context
, k
, db
->hdb_master_key
);
556 hdb_set_master_key (krb5_context context
,
563 ret
= hdb_process_master_key(context
, 0, key
, 0, &mkey
);
566 db
->hdb_master_key
= mkey
;
567 #if 0 /* XXX - why? */
568 des_set_random_generator_seed(key
.keyvalue
.data
);
570 db
->hdb_master_key_set
= 1;
575 hdb_set_master_keyfile (krb5_context context
,
582 ret
= hdb_read_master_key(context
, keyfile
, &key
);
586 krb5_clear_error_string(context
);
589 db
->hdb_master_key
= key
;
590 db
->hdb_master_key_set
= 1;
595 hdb_clear_master_key (krb5_context context
,
598 if (db
->hdb_master_key_set
) {
599 hdb_free_master_key(context
, db
->hdb_master_key
);
600 db
->hdb_master_key_set
= 0;