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 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 krb5_error_code ret; 51 krb5_crypto crypto; 52 53 ret = krb5_crypto_init(context, key, 0, &crypto); 54 if (ret) 55 return ret; 56 57 ret = krb5_create_checksum(context, crypto, usage, cksumtype, 58 input->data, input->length, cksum); 59 krb5_crypto_destroy(context, crypto); 60 61 return ret ; 62 } 63 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) 68 { 69 krb5_error_code ret; 70 krb5_checksum data_cksum; 71 72 *valid = 0; 73 74 ret = krb5_c_make_checksum(context, cksum->cksumtype, 75 key, usage, data, &data_cksum); 76 if (ret) 77 return ret; 78 79 if (data_cksum.cksumtype == cksum->cksumtype 80 && krb5_data_ct_cmp(&data_cksum.checksum, &cksum->checksum) == 0) 81 *valid = 1; 82 83 krb5_free_checksum_contents(context, &data_cksum); 84 85 return 0; 86 } 87 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) 91 { 92 krb5_error_code ret; 93 94 if (type) 95 *type = cksum->cksumtype; 96 if (data) { 97 *data = malloc(sizeof(**data)); 98 if (*data == NULL) 99 return ENOMEM; 100 101 ret = der_copy_octet_string(&cksum->checksum, *data); 102 if (ret) { 103 free(*data); 104 *data = NULL; 105 return ret; 106 } 107 } 108 return 0; 109 } 110 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) 114 { 115 cksum->cksumtype = type; 116 return der_copy_octet_string(data, &cksum->checksum); 117 } 118 119 KRB5_LIB_FUNCTION void KRB5_LIB_CALL 120 krb5_free_checksum (krb5_context context, krb5_checksum *cksum) 121 { 122 krb5_checksum_free(context, cksum); 123 free(cksum); 124 } 125 126 KRB5_LIB_FUNCTION void KRB5_LIB_CALL 127 krb5_free_checksum_contents(krb5_context context, krb5_checksum *cksum) 128 { 129 krb5_checksum_free(context, cksum); 130 memset(cksum, 0, sizeof(*cksum)); 131 } 132 133 KRB5_LIB_FUNCTION void KRB5_LIB_CALL 134 krb5_checksum_free(krb5_context context, krb5_checksum *cksum) 135 { 136 free_Checksum(cksum); 137 } 138 139 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL 140 krb5_c_valid_enctype (krb5_enctype etype) 141 { 142 return !krb5_enctype_valid(NULL, etype); 143 } 144 145 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL 146 krb5_c_valid_cksumtype(krb5_cksumtype ctype) 147 { 148 return krb5_cksumtype_valid(NULL, ctype); 149 } 150 151 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL 152 krb5_c_is_coll_proof_cksum(krb5_cksumtype ctype) 153 { 154 return krb5_checksum_is_collision_proof(NULL, ctype); 155 } 156 157 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL 158 krb5_c_is_keyed_cksum(krb5_cksumtype ctype) 159 { 160 return krb5_checksum_is_keyed(NULL, ctype); 161 } 162 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) 167 { 168 *new = malloc(sizeof(**new)); 169 if (*new == NULL) 170 return ENOMEM; 171 return copy_Checksum(old, *new); 172 } 173 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) 177 { 178 return krb5_checksumsize(context, cksumtype, length); 179 } 180 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) 185 { 186 krb5_error_code ret; 187 krb5_crypto crypto; 188 krb5_keyblock key; 189 190 ret = krb5_generate_random_keyblock(context, enctype, &key); 191 if (ret) 192 return ret; 193 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); 200 201 return ret; 202 } 203 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) 211 { 212 krb5_error_code ret; 213 krb5_crypto crypto; 214 215 ret = krb5_crypto_init(context, &key, input->enctype, &crypto); 216 if (ret) 217 return ret; 218 219 if (ivec) { 220 size_t blocksize; 221 222 ret = krb5_crypto_getblocksize(context, crypto, &blocksize); 223 if (ret) { 224 krb5_crypto_destroy(context, crypto); 225 return ret; 226 } 227 228 if (blocksize > ivec->length) { 229 krb5_crypto_destroy(context, crypto); 230 return KRB5_BAD_MSIZE; 231 } 232 } 233 234 ret = krb5_decrypt_ivec(context, crypto, usage, 235 input->ciphertext.data, input->ciphertext.length, 236 output, 237 ivec ? ivec->data : NULL); 238 239 krb5_crypto_destroy(context, crypto); 240 241 return ret ; 242 } 243 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) 251 { 252 krb5_error_code ret; 253 krb5_crypto crypto; 254 255 ret = krb5_crypto_init(context, key, 0, &crypto); 256 if (ret) 257 return ret; 258 259 if (ivec) { 260 size_t blocksize; 261 262 ret = krb5_crypto_getblocksize(context, crypto, &blocksize); 263 if (ret) { 264 krb5_crypto_destroy(context, crypto); 265 return ret; 266 } 267 268 if (blocksize > ivec->length) { 269 krb5_crypto_destroy(context, crypto); 270 return KRB5_BAD_MSIZE; 271 } 272 } 273 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); 280 281 krb5_crypto_destroy(context, crypto); 282 283 return ret ; 284 } 285 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) 291 { 292 krb5_error_code ret; 293 krb5_crypto crypto; 294 krb5_keyblock key; 295 296 ret = krb5_generate_random_keyblock(context, enctype, &key); 297 if (ret) 298 return ret; 299 300 ret = krb5_crypto_init(context, &key, 0, &crypto); 301 krb5_free_keyblock_contents(context, &key); 302 if (ret) 303 return ret; 304 305 *length = krb5_get_wrapped_length(context, crypto, inputlen); 306 krb5_crypto_destroy(context, crypto); 307 308 return 0; 309 } 310 311 /** 312 * Deprecated: keytypes doesn't exists, they are really enctypes. 313 * 314 * @ingroup krb5_deprecated 315 */ 316 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") 323 { 324 *similar = (e1 == e2); 325 return 0; 326 } 327 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) 332 { 333 return krb5_generate_random_keyblock(context, enctype, random_key); 334 } 335 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) 341 { 342 krb5_error_code ret; 343 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); 349 } 350 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) 355 { 356 return krb5_crypto_prf_length(context, type, length); 357 } 358 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) 364 { 365 krb5_crypto crypto; 366 krb5_error_code ret; 367 368 ret = krb5_crypto_init(context, key, 0, &crypto); 369 if (ret) 370 return ret; 371 372 ret = krb5_crypto_prf(context, crypto, input, output); 373 krb5_crypto_destroy(context, crypto); 374 375 return ret; 376 } 377 378 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 379 krb5_c_random_make_octets(krb5_context context, krb5_data * data) 380 { 381 return krb5_generate_random_keyblock(context, data->length, data->data); 382 } 383 384 /** 385 * MIT compat glue 386 * 387 * @ingroup krb5_ccache 388 */ 389 390 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 391 krb5_cc_copy_creds(krb5_context context, 392 const krb5_ccache from, 393 krb5_ccache to) 394 { 395 return krb5_cc_copy_cache(context, from, to); 396 } 397 398 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 399 krb5_auth_con_getsendsubkey(krb5_context context, krb5_auth_context auth_context, 400 krb5_keyblock **keyblock) 401 { 402 return krb5_auth_con_getlocalsubkey(context, auth_context, keyblock); 403 } 404 405 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 406 krb5_auth_con_getrecvsubkey(krb5_context context, krb5_auth_context auth_context, 407 krb5_keyblock **keyblock) 408 { 409 return krb5_auth_con_getremotesubkey(context, auth_context, keyblock); 410 } 411 412 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 413 krb5_auth_con_setsendsubkey(krb5_context context, krb5_auth_context auth_context, 414 krb5_keyblock *keyblock) 415 { 416 return krb5_auth_con_setlocalsubkey(context, auth_context, keyblock); 417 } 418 419 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 420 krb5_auth_con_setrecvsubkey(krb5_context context, krb5_auth_context auth_context, 421 krb5_keyblock *keyblock) 422 { 423 return krb5_auth_con_setremotesubkey(context, auth_context, keyblock); 424 } 425 426 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 427 krb5_free_default_realm(krb5_context context, krb5_realm realm) 428 { 429 return krb5_xfree(realm); 430 } 431 432 #endif /* HEIMDAL_SMALLER */ 433