2 * Copyright (c) 1997 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. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Kungliga Tekniska
20 * Högskolan and its contributors.
22 * 4. Neither the name of the Institute nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 hdb_next_keytype2key(krb5_context context
,
51 i
= *key
- e
->keys
.val
+ 1;
54 for(; i
< e
->keys
.len
; i
++)
55 if(e
->keys
.val
[i
].key
.keytype
== keytype
){
56 *key
= &e
->keys
.val
[i
];
59 return KRB5_PROG_ETYPE_NOSUPP
; /* XXX */
63 hdb_keytype2key(krb5_context context
,
69 return hdb_next_keytype2key(context
,e
, keytype
, key
);
73 hdb_next_etype2key(krb5_context context
,
80 ret
= krb5_etype_to_keytype(context
, etype
, &keytype
);
83 return hdb_next_keytype2key(context
, e
, keytype
, key
);
87 hdb_etype2key(krb5_context context
,
93 return hdb_next_etype2key(context
,e
, etype
, key
);
96 /* this is a bit ugly, but will get better when the crypto framework
100 hdb_process_master_key(krb5_context context
, EncryptionKey key
,
103 if(key
.keytype
!= KEYTYPE_DES
)
104 return KRB5_PROG_KEYTYPE_NOSUPP
;
105 schedule
->length
= sizeof(des_key_schedule
);
106 schedule
->data
= malloc(schedule
->length
);
108 des_set_key((des_cblock
*)key
.keyvalue
.data
, schedule
->data
);
113 hdb_read_master_key(krb5_context context
, const char *filename
,
117 unsigned char buf
[256];
121 filename
= HDB_DB_DIR
"/m-key";
122 f
= fopen(filename
, "r");
125 len
= fread(buf
, 1, sizeof(buf
), f
);
129 ret
= decode_EncryptionKey(buf
, len
, key
, &len
);
131 memset(buf
, 0, sizeof(buf
));
136 hdb_unseal_key(Key
*key
, krb5_data schedule
)
142 new_key
= malloc(sizeof(*new_key
));
143 copy_Key(key
, new_key
);
144 memset(&iv
, 0, sizeof(iv
));
145 des_cfb64_encrypt(key
->key
.keyvalue
.data
,
146 new_key
->key
.keyvalue
.data
,
147 key
->key
.keyvalue
.length
,
148 schedule
.data
, &iv
, &num
, 0);
153 hdb_seal_key(Key
*key
, krb5_data schedule
)
158 memset(&iv
, 0, sizeof(iv
));
159 des_cfb64_encrypt(key
->key
.keyvalue
.data
,
160 key
->key
.keyvalue
.data
,
161 key
->key
.keyvalue
.length
,
162 schedule
.data
, &iv
, &num
, 1);
166 hdb_unseal_keys(hdb_entry
*ent
, krb5_data schedule
)
169 for(i
= 0; i
< ent
->keys
.len
; i
++){
172 memset(&iv
, 0, sizeof(iv
));
173 des_cfb64_encrypt(ent
->keys
.val
[i
].key
.keyvalue
.data
,
174 ent
->keys
.val
[i
].key
.keyvalue
.data
,
175 ent
->keys
.val
[i
].key
.keyvalue
.length
,
176 schedule
.data
, &iv
, &num
, 0);
181 hdb_seal_keys(hdb_entry
*ent
, krb5_data schedule
)
184 for(i
= 0; i
< ent
->keys
.len
; i
++)
185 hdb_seal_key(&ent
->keys
.val
[i
], schedule
);
189 hdb_free_key(Key
*key
)
191 memset(key
->key
.keyvalue
.data
,
193 key
->key
.keyvalue
.length
);
200 hdb_lock(int fd
, int operation
)
203 for(i
= 0; i
< 3; i
++){
204 code
= flock(fd
, (operation
== HDB_RLOCK
? LOCK_SH
: LOCK_EX
) | LOCK_NB
);
205 if(code
== 0 || errno
!= EWOULDBLOCK
)
211 if(errno
== EWOULDBLOCK
)
212 return HDB_ERR_DB_INUSE
;
213 return HDB_ERR_CANT_LOCK_DB
;
220 code
= flock(fd
, LOCK_UN
);
222 return 4711 /* XXX */;
227 hdb_free_entry(krb5_context context
, hdb_entry
*ent
)
231 for(i
= 0; i
< ent
->keys
.len
; ++i
) {
232 Key
*k
= &ent
->keys
.val
[i
];
234 memset (k
->key
.keyvalue
.data
, 0, k
->key
.keyvalue
.length
);
240 hdb_foreach(krb5_context context
,
242 hdb_foreach_func_t func
,
247 ret
= db
->firstkey(context
, db
, &entry
);
249 ret
= (*func
)(context
, db
, &entry
, data
);
250 hdb_free_entry(context
, &entry
);
252 ret
= db
->nextkey(context
, db
, &entry
);
254 if(ret
== HDB_ERR_NOENTRY
)
260 hdb_check_db_format(krb5_context context
, HDB
*db
)
268 tag
.data
= HDB_DB_FORMAT_ENTRY
;
269 tag
.length
= strlen(tag
.data
);
270 ret
= (*db
->_get
)(context
, db
, tag
, &version
);
273 foo
= sscanf(version
.data
, "%u", &ver
);
274 krb5_data_free (&version
);
276 return HDB_ERR_BADVERSION
;
277 if(ver
!= HDB_DB_FORMAT
)
278 return HDB_ERR_BADVERSION
;
283 hdb_init_db(krb5_context context
, HDB
*db
)
290 ret
= hdb_check_db_format(context
, db
);
291 if(ret
!= HDB_ERR_NOENTRY
)
294 tag
.data
= HDB_DB_FORMAT_ENTRY
;
295 tag
.length
= strlen(tag
.data
);
296 snprintf(ver
, sizeof(ver
), "%u", HDB_DB_FORMAT
);
298 version
.length
= strlen(version
.data
) + 1; /* zero terminated */
299 ret
= (*db
->_put
)(context
, db
, 0, tag
, version
);
304 hdb_create(krb5_context context
, HDB
**db
, const char *filename
)
306 krb5_error_code ret
= 0;
308 filename
= HDB_DEFAULT_DB
;
309 initialize_hdb_error_table(&context
->et_list
);
311 ret
= hdb_db_create(context
, db
, filename
);
313 ret
= hdb_ndbm_create(context
, db
, filename
);
315 krb5_errx(context
, 1, "No database support! (hdb_create)");
321 hdb_set_master_key (krb5_context context
,
328 ret
= hdb_read_master_key(context
, keyfile
, &key
);
333 ret
= hdb_process_master_key(context
, key
, &db
->master_key
);
336 des_set_random_generator_seed(key
.keyvalue
.data
);
337 db
->master_key_set
= 1;
338 memset(key
.keyvalue
.data
, 0, key
.keyvalue
.length
);
339 free_EncryptionKey(&key
);
345 hdb_clear_master_key (krb5_context context
,
348 if (db
->master_key_set
) {
349 memset(db
->master_key
.data
, 0, db
->master_key
.length
);
350 krb5_data_free(&db
->master_key
);
351 db
->master_key_set
= 0;