hdb: read_master_key use free_master_key on error
[heimdal.git] / lib / krb5 / mit_glue.c
blobdeee242c1e8cae958c1a0ddc34db0c48a3f10b77
1 /*
2 * Copyright (c) 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "krb5_locl.h"
36 #ifndef HEIMDAL_SMALLER
39 * Glue for MIT API
42 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
43 krb5_c_make_checksum(krb5_context context,
44 krb5_cksumtype cksumtype,
45 const krb5_keyblock *key,
46 krb5_keyusage usage,
47 const krb5_data *input,
48 krb5_checksum *cksum)
50 krb5_error_code ret;
51 krb5_crypto crypto;
53 ret = krb5_crypto_init(context, key, 0, &crypto);
54 if (ret)
55 return ret;
57 ret = krb5_create_checksum(context, crypto, usage, cksumtype,
58 input->data, input->length, cksum);
59 krb5_crypto_destroy(context, crypto);
61 return ret ;
64 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
65 krb5_c_verify_checksum(krb5_context context, const krb5_keyblock *key,
66 krb5_keyusage usage, const krb5_data *data,
67 const krb5_checksum *cksum, krb5_boolean *valid)
69 krb5_error_code ret;
70 krb5_checksum data_cksum;
72 *valid = 0;
74 ret = krb5_c_make_checksum(context, cksum->cksumtype,
75 key, usage, data, &data_cksum);
76 if (ret)
77 return ret;
79 if (data_cksum.cksumtype == cksum->cksumtype
80 && krb5_data_ct_cmp(&data_cksum.checksum, &cksum->checksum) == 0)
81 *valid = 1;
83 krb5_free_checksum_contents(context, &data_cksum);
85 return 0;
88 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
89 krb5_c_get_checksum(krb5_context context, const krb5_checksum *cksum,
90 krb5_cksumtype *type, krb5_data **data)
92 krb5_error_code ret;
94 if (type)
95 *type = cksum->cksumtype;
96 if (data) {
97 *data = malloc(sizeof(**data));
98 if (*data == NULL)
99 return krb5_enomem(context);
101 ret = der_copy_octet_string(&cksum->checksum, *data);
102 if (ret) {
103 free(*data);
104 *data = NULL;
105 return ret;
108 return 0;
111 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
112 krb5_c_set_checksum(krb5_context context, krb5_checksum *cksum,
113 krb5_cksumtype type, const krb5_data *data)
115 cksum->cksumtype = type;
116 return der_copy_octet_string(data, &cksum->checksum);
119 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
120 krb5_free_checksum (krb5_context context, krb5_checksum *cksum)
122 krb5_checksum_free(context, cksum);
123 free(cksum);
126 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
127 krb5_free_checksum_contents(krb5_context context, krb5_checksum *cksum)
129 krb5_checksum_free(context, cksum);
130 memset(cksum, 0, sizeof(*cksum));
133 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
134 krb5_checksum_free(krb5_context context, krb5_checksum *cksum)
136 free_Checksum(cksum);
139 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
140 krb5_c_valid_enctype (krb5_enctype etype)
142 return !krb5_enctype_valid(NULL, etype);
145 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
146 krb5_c_valid_cksumtype(krb5_cksumtype ctype)
148 return krb5_cksumtype_valid(NULL, ctype);
151 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
152 krb5_c_is_coll_proof_cksum(krb5_cksumtype ctype)
154 return krb5_checksum_is_collision_proof(NULL, ctype);
157 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
158 krb5_c_is_keyed_cksum(krb5_cksumtype ctype)
160 return krb5_checksum_is_keyed(NULL, ctype);
163 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
164 krb5_copy_checksum (krb5_context context,
165 const krb5_checksum *old,
166 krb5_checksum **new)
168 *new = malloc(sizeof(**new));
169 if (*new == NULL)
170 return krb5_enomem(context);
171 return copy_Checksum(old, *new);
174 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
175 krb5_c_checksum_length (krb5_context context, krb5_cksumtype cksumtype,
176 size_t *length)
178 return krb5_checksumsize(context, cksumtype, length);
181 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
182 krb5_c_block_size(krb5_context context,
183 krb5_enctype enctype,
184 size_t *blocksize)
186 krb5_error_code ret;
187 krb5_crypto crypto;
188 krb5_keyblock key;
190 ret = krb5_generate_random_keyblock(context, enctype, &key);
191 if (ret)
192 return ret;
194 ret = krb5_crypto_init(context, &key, 0, &crypto);
195 krb5_free_keyblock_contents(context, &key);
196 if (ret)
197 return ret;
198 ret = krb5_crypto_getblocksize(context, crypto, blocksize);
199 krb5_crypto_destroy(context, crypto);
201 return ret;
204 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
205 krb5_c_decrypt(krb5_context context,
206 const krb5_keyblock key,
207 krb5_keyusage usage,
208 const krb5_data *ivec,
209 krb5_enc_data *input,
210 krb5_data *output)
212 krb5_error_code ret;
213 krb5_crypto crypto;
215 ret = krb5_crypto_init(context, &key, input->enctype, &crypto);
216 if (ret)
217 return ret;
219 if (ivec) {
220 size_t blocksize;
222 ret = krb5_crypto_getblocksize(context, crypto, &blocksize);
223 if (ret) {
224 krb5_crypto_destroy(context, crypto);
225 return ret;
228 if (blocksize > ivec->length) {
229 krb5_crypto_destroy(context, crypto);
230 return KRB5_BAD_MSIZE;
234 ret = krb5_decrypt_ivec(context, crypto, usage,
235 input->ciphertext.data, input->ciphertext.length,
236 output,
237 ivec ? ivec->data : NULL);
239 krb5_crypto_destroy(context, crypto);
241 return ret ;
244 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
245 krb5_c_encrypt(krb5_context context,
246 const krb5_keyblock *key,
247 krb5_keyusage usage,
248 const krb5_data *ivec,
249 const krb5_data *input,
250 krb5_enc_data *output)
252 krb5_error_code ret;
253 krb5_crypto crypto;
255 ret = krb5_crypto_init(context, key, 0, &crypto);
256 if (ret)
257 return ret;
259 if (ivec) {
260 size_t blocksize;
262 ret = krb5_crypto_getblocksize(context, crypto, &blocksize);
263 if (ret) {
264 krb5_crypto_destroy(context, crypto);
265 return ret;
268 if (blocksize > ivec->length) {
269 krb5_crypto_destroy(context, crypto);
270 return KRB5_BAD_MSIZE;
274 ret = krb5_encrypt_ivec(context, crypto, usage,
275 input->data, input->length,
276 &output->ciphertext,
277 ivec ? ivec->data : NULL);
278 output->kvno = 0;
279 krb5_crypto_getenctype(context, crypto, &output->enctype);
281 krb5_crypto_destroy(context, crypto);
283 return ret ;
286 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
287 krb5_c_encrypt_length(krb5_context context,
288 krb5_enctype enctype,
289 size_t inputlen,
290 size_t *length)
292 krb5_error_code ret;
293 krb5_crypto crypto;
294 krb5_keyblock key;
296 ret = krb5_generate_random_keyblock(context, enctype, &key);
297 if (ret)
298 return ret;
300 ret = krb5_crypto_init(context, &key, 0, &crypto);
301 krb5_free_keyblock_contents(context, &key);
302 if (ret)
303 return ret;
305 *length = krb5_get_wrapped_length(context, crypto, inputlen);
306 krb5_crypto_destroy(context, crypto);
308 return 0;
312 * Deprecated: keytypes doesn't exists, they are really enctypes.
314 * @ingroup krb5_deprecated
317 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
318 krb5_c_enctype_compare(krb5_context context,
319 krb5_enctype e1,
320 krb5_enctype e2,
321 krb5_boolean *similar)
322 KRB5_DEPRECATED_FUNCTION("Use X instead")
324 *similar = (e1 == e2);
325 return 0;
328 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
329 krb5_c_make_random_key(krb5_context context,
330 krb5_enctype enctype,
331 krb5_keyblock *random_key)
333 return krb5_generate_random_keyblock(context, enctype, random_key);
336 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
337 krb5_c_keylengths(krb5_context context,
338 krb5_enctype enctype,
339 size_t *ilen,
340 size_t *keylen)
342 krb5_error_code ret;
344 ret = krb5_enctype_keybits(context, enctype, ilen);
345 if (ret)
346 return ret;
347 *ilen = (*ilen + 7) / 8;
348 return krb5_enctype_keysize(context, enctype, keylen);
351 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
352 krb5_c_prf_length(krb5_context context,
353 krb5_enctype type,
354 size_t *length)
356 return krb5_crypto_prf_length(context, type, length);
359 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
360 krb5_c_prf(krb5_context context,
361 const krb5_keyblock *key,
362 const krb5_data *input,
363 krb5_data *output)
365 krb5_crypto crypto;
366 krb5_error_code ret;
368 ret = krb5_crypto_init(context, key, 0, &crypto);
369 if (ret)
370 return ret;
372 ret = krb5_crypto_prf(context, crypto, input, output);
373 krb5_crypto_destroy(context, crypto);
375 return ret;
378 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
379 krb5_c_random_make_octets(krb5_context context, krb5_data * data)
381 krb5_generate_random_block(data->data, data->length);
382 return 0;
386 * MIT compat glue
388 * @ingroup krb5_ccache
391 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
392 krb5_cc_copy_creds(krb5_context context,
393 const krb5_ccache from,
394 krb5_ccache to)
396 return krb5_cc_copy_cache(context, from, to);
399 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
400 krb5_auth_con_getsendsubkey(krb5_context context, krb5_auth_context auth_context,
401 krb5_keyblock **keyblock)
403 return krb5_auth_con_getlocalsubkey(context, auth_context, keyblock);
406 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
407 krb5_auth_con_getrecvsubkey(krb5_context context, krb5_auth_context auth_context,
408 krb5_keyblock **keyblock)
410 return krb5_auth_con_getremotesubkey(context, auth_context, keyblock);
413 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
414 krb5_auth_con_setsendsubkey(krb5_context context, krb5_auth_context auth_context,
415 krb5_keyblock *keyblock)
417 return krb5_auth_con_setlocalsubkey(context, auth_context, keyblock);
420 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
421 krb5_auth_con_setrecvsubkey(krb5_context context, krb5_auth_context auth_context,
422 krb5_keyblock *keyblock)
424 return krb5_auth_con_setremotesubkey(context, auth_context, keyblock);
427 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
428 krb5_free_default_realm(krb5_context context, krb5_realm realm)
430 return krb5_xfree(realm);
433 #endif /* HEIMDAL_SMALLER */