Line data Source code
1 : /*
2 : * Copyright (c) 2003 Kungliga Tekniska Högskolan
3 : * (Royal Institute of Technology, Stockholm, Sweden).
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions
8 : * are met:
9 : *
10 : * 1. Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : *
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.
16 : *
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.
20 : *
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.
32 : */
33 :
34 : #include "krb5_locl.h"
35 :
36 : #ifndef HEIMDAL_SMALLER
37 :
38 : /*
39 : * Glue for MIT API
40 : */
41 :
42 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
43 0 : 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)
49 : {
50 0 : krb5_error_code ret;
51 0 : krb5_crypto crypto;
52 :
53 0 : ret = krb5_crypto_init(context, key, 0, &crypto);
54 0 : if (ret)
55 0 : return ret;
56 :
57 0 : ret = krb5_create_checksum(context, crypto, usage, cksumtype,
58 0 : input->data, input->length, cksum);
59 0 : krb5_crypto_destroy(context, crypto);
60 :
61 0 : return ret ;
62 : }
63 :
64 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
65 1815 : 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)
68 : {
69 14 : krb5_error_code ret;
70 14 : krb5_crypto crypto;
71 :
72 1815 : *valid = 0;
73 :
74 1815 : ret = krb5_crypto_init(context, key, 0, &crypto);
75 1815 : if (ret)
76 0 : return ret;
77 :
78 1829 : ret = krb5_verify_checksum(context, crypto, usage,
79 1815 : data->data, data->length, rk_UNCONST(cksum));
80 1815 : krb5_crypto_destroy(context, crypto);
81 :
82 1815 : if (ret == 0) {
83 1671 : *valid = 1;
84 : }
85 :
86 1801 : return ret;
87 : }
88 :
89 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
90 0 : krb5_c_get_checksum(krb5_context context, const krb5_checksum *cksum,
91 : krb5_cksumtype *type, krb5_data **data)
92 : {
93 0 : krb5_error_code ret;
94 :
95 0 : if (type)
96 0 : *type = cksum->cksumtype;
97 0 : if (data) {
98 0 : *data = malloc(sizeof(**data));
99 0 : if (*data == NULL)
100 0 : return krb5_enomem(context);
101 :
102 0 : ret = der_copy_octet_string(&cksum->checksum, *data);
103 0 : if (ret) {
104 0 : free(*data);
105 0 : *data = NULL;
106 0 : return ret;
107 : }
108 : }
109 0 : return 0;
110 : }
111 :
112 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
113 0 : krb5_c_set_checksum(krb5_context context, krb5_checksum *cksum,
114 : krb5_cksumtype type, const krb5_data *data)
115 : {
116 0 : cksum->cksumtype = type;
117 0 : return der_copy_octet_string(data, &cksum->checksum);
118 : }
119 :
120 : KRB5_LIB_FUNCTION void KRB5_LIB_CALL
121 0 : krb5_free_checksum (krb5_context context, krb5_checksum *cksum)
122 : {
123 0 : krb5_checksum_free(context, cksum);
124 0 : free(cksum);
125 0 : }
126 :
127 : KRB5_LIB_FUNCTION void KRB5_LIB_CALL
128 0 : krb5_free_checksum_contents(krb5_context context, krb5_checksum *cksum)
129 : {
130 0 : krb5_checksum_free(context, cksum);
131 0 : memset(cksum, 0, sizeof(*cksum));
132 0 : }
133 :
134 : KRB5_LIB_FUNCTION void KRB5_LIB_CALL
135 0 : krb5_checksum_free(krb5_context context, krb5_checksum *cksum)
136 : {
137 0 : free_Checksum(cksum);
138 0 : }
139 :
140 : KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
141 0 : krb5_c_valid_enctype (krb5_enctype etype)
142 : {
143 0 : return !krb5_enctype_valid(NULL, etype);
144 : }
145 :
146 : KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
147 0 : krb5_c_valid_cksumtype(krb5_cksumtype ctype)
148 : {
149 0 : return krb5_cksumtype_valid(NULL, ctype);
150 : }
151 :
152 : KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
153 0 : krb5_c_is_coll_proof_cksum(krb5_cksumtype ctype)
154 : {
155 0 : return krb5_checksum_is_collision_proof(NULL, ctype);
156 : }
157 :
158 : KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
159 0 : krb5_c_is_keyed_cksum(krb5_cksumtype ctype)
160 : {
161 0 : return krb5_checksum_is_keyed(NULL, ctype);
162 : }
163 :
164 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
165 0 : krb5_copy_checksum (krb5_context context,
166 : const krb5_checksum *old,
167 : krb5_checksum **new)
168 : {
169 0 : *new = malloc(sizeof(**new));
170 0 : if (*new == NULL)
171 0 : return krb5_enomem(context);
172 0 : return copy_Checksum(old, *new);
173 : }
174 :
175 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
176 0 : krb5_c_checksum_length (krb5_context context, krb5_cksumtype cksumtype,
177 : size_t *length)
178 : {
179 0 : return krb5_checksumsize(context, cksumtype, length);
180 : }
181 :
182 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
183 0 : krb5_c_block_size(krb5_context context,
184 : krb5_enctype enctype,
185 : size_t *blocksize)
186 : {
187 0 : krb5_error_code ret;
188 0 : krb5_crypto crypto;
189 0 : krb5_keyblock key;
190 :
191 0 : ret = krb5_generate_random_keyblock(context, enctype, &key);
192 0 : if (ret)
193 0 : return ret;
194 :
195 0 : ret = krb5_crypto_init(context, &key, 0, &crypto);
196 0 : krb5_free_keyblock_contents(context, &key);
197 0 : if (ret)
198 0 : return ret;
199 0 : ret = krb5_crypto_getblocksize(context, crypto, blocksize);
200 0 : krb5_crypto_destroy(context, crypto);
201 :
202 0 : return ret;
203 : }
204 :
205 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
206 4 : krb5_c_decrypt(krb5_context context,
207 : const krb5_keyblock key,
208 : krb5_keyusage usage,
209 : const krb5_data *ivec,
210 : krb5_enc_data *input,
211 : krb5_data *output)
212 : {
213 4 : krb5_error_code ret;
214 4 : krb5_crypto crypto;
215 :
216 4 : ret = krb5_crypto_init(context, &key, input->enctype, &crypto);
217 4 : if (ret)
218 0 : return ret;
219 :
220 4 : if (ivec) {
221 0 : size_t blocksize;
222 :
223 0 : ret = krb5_crypto_getblocksize(context, crypto, &blocksize);
224 0 : if (ret) {
225 0 : krb5_crypto_destroy(context, crypto);
226 0 : return ret;
227 : }
228 :
229 0 : if (blocksize > ivec->length) {
230 0 : krb5_crypto_destroy(context, crypto);
231 0 : return KRB5_BAD_MSIZE;
232 : }
233 : }
234 :
235 4 : ret = krb5_decrypt_ivec(context, crypto, usage,
236 : input->ciphertext.data, input->ciphertext.length,
237 : output,
238 : ivec ? ivec->data : NULL);
239 :
240 4 : krb5_crypto_destroy(context, crypto);
241 :
242 4 : return ret ;
243 : }
244 :
245 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
246 0 : krb5_c_encrypt(krb5_context context,
247 : const krb5_keyblock *key,
248 : krb5_keyusage usage,
249 : const krb5_data *ivec,
250 : const krb5_data *input,
251 : krb5_enc_data *output)
252 : {
253 0 : krb5_error_code ret;
254 0 : krb5_crypto crypto;
255 :
256 0 : ret = krb5_crypto_init(context, key, 0, &crypto);
257 0 : if (ret)
258 0 : return ret;
259 :
260 0 : if (ivec) {
261 0 : size_t blocksize;
262 :
263 0 : ret = krb5_crypto_getblocksize(context, crypto, &blocksize);
264 0 : if (ret) {
265 0 : krb5_crypto_destroy(context, crypto);
266 0 : return ret;
267 : }
268 :
269 0 : if (blocksize > ivec->length) {
270 0 : krb5_crypto_destroy(context, crypto);
271 0 : return KRB5_BAD_MSIZE;
272 : }
273 : }
274 :
275 0 : ret = krb5_encrypt_ivec(context, crypto, usage,
276 0 : input->data, input->length,
277 : &output->ciphertext,
278 : ivec ? ivec->data : NULL);
279 0 : output->kvno = 0;
280 0 : krb5_crypto_getenctype(context, crypto, &output->enctype);
281 :
282 0 : krb5_crypto_destroy(context, crypto);
283 :
284 0 : return ret ;
285 : }
286 :
287 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
288 0 : krb5_c_encrypt_length(krb5_context context,
289 : krb5_enctype enctype,
290 : size_t inputlen,
291 : size_t *length)
292 : {
293 0 : krb5_error_code ret;
294 0 : krb5_crypto crypto;
295 0 : krb5_keyblock key;
296 :
297 0 : ret = krb5_generate_random_keyblock(context, enctype, &key);
298 0 : if (ret)
299 0 : return ret;
300 :
301 0 : ret = krb5_crypto_init(context, &key, 0, &crypto);
302 0 : krb5_free_keyblock_contents(context, &key);
303 0 : if (ret)
304 0 : return ret;
305 :
306 0 : *length = krb5_get_wrapped_length(context, crypto, inputlen);
307 0 : krb5_crypto_destroy(context, crypto);
308 :
309 0 : return 0;
310 : }
311 :
312 : /**
313 : * Deprecated: keytypes doesn't exists, they are really enctypes.
314 : *
315 : * @ingroup krb5_deprecated
316 : */
317 :
318 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
319 0 : krb5_c_enctype_compare(krb5_context context,
320 : krb5_enctype e1,
321 : krb5_enctype e2,
322 : krb5_boolean *similar)
323 : KRB5_DEPRECATED_FUNCTION("Use X instead")
324 : {
325 0 : *similar = (e1 == e2);
326 0 : return 0;
327 : }
328 :
329 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
330 0 : krb5_c_make_random_key(krb5_context context,
331 : krb5_enctype enctype,
332 : krb5_keyblock *random_key)
333 : {
334 0 : return krb5_generate_random_keyblock(context, enctype, random_key);
335 : }
336 :
337 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
338 0 : krb5_c_keylengths(krb5_context context,
339 : krb5_enctype enctype,
340 : size_t *ilen,
341 : size_t *keylen)
342 : {
343 0 : krb5_error_code ret;
344 :
345 0 : ret = krb5_enctype_keybits(context, enctype, ilen);
346 0 : if (ret)
347 0 : return ret;
348 0 : *ilen = (*ilen + 7) / 8;
349 0 : return krb5_enctype_keysize(context, enctype, keylen);
350 : }
351 :
352 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
353 0 : krb5_c_prf_length(krb5_context context,
354 : krb5_enctype type,
355 : size_t *length)
356 : {
357 0 : return krb5_crypto_prf_length(context, type, length);
358 : }
359 :
360 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
361 0 : krb5_c_prf(krb5_context context,
362 : const krb5_keyblock *key,
363 : const krb5_data *input,
364 : krb5_data *output)
365 : {
366 0 : krb5_crypto crypto;
367 0 : krb5_error_code ret;
368 :
369 0 : ret = krb5_crypto_init(context, key, 0, &crypto);
370 0 : if (ret)
371 0 : return ret;
372 :
373 0 : ret = krb5_crypto_prf(context, crypto, input, output);
374 0 : krb5_crypto_destroy(context, crypto);
375 :
376 0 : return ret;
377 : }
378 :
379 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
380 0 : krb5_c_random_make_octets(krb5_context context, krb5_data * data)
381 : {
382 0 : krb5_generate_random_block(data->data, data->length);
383 0 : return 0;
384 : }
385 :
386 : /**
387 : * MIT compat glue
388 : *
389 : * @ingroup krb5_ccache
390 : */
391 :
392 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
393 0 : krb5_cc_copy_creds(krb5_context context,
394 : const krb5_ccache from,
395 : krb5_ccache to)
396 : {
397 0 : return krb5_cc_copy_cache(context, from, to);
398 : }
399 :
400 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
401 0 : krb5_auth_con_getsendsubkey(krb5_context context, krb5_auth_context auth_context,
402 : krb5_keyblock **keyblock)
403 : {
404 0 : return krb5_auth_con_getlocalsubkey(context, auth_context, keyblock);
405 : }
406 :
407 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
408 0 : krb5_auth_con_getrecvsubkey(krb5_context context, krb5_auth_context auth_context,
409 : krb5_keyblock **keyblock)
410 : {
411 0 : return krb5_auth_con_getremotesubkey(context, auth_context, keyblock);
412 : }
413 :
414 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
415 0 : krb5_auth_con_setsendsubkey(krb5_context context, krb5_auth_context auth_context,
416 : krb5_keyblock *keyblock)
417 : {
418 0 : return krb5_auth_con_setlocalsubkey(context, auth_context, keyblock);
419 : }
420 :
421 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
422 0 : krb5_auth_con_setrecvsubkey(krb5_context context, krb5_auth_context auth_context,
423 : krb5_keyblock *keyblock)
424 : {
425 0 : return krb5_auth_con_setremotesubkey(context, auth_context, keyblock);
426 : }
427 :
428 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
429 2698 : krb5_free_default_realm(krb5_context context, krb5_realm realm)
430 : {
431 2698 : return krb5_xfree(realm);
432 : }
433 :
434 : #endif /* HEIMDAL_SMALLER */
|