| 1 |
/* |
| 2 |
* Copyright (c) 1997 - 2004 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 "gsskrb5_locl.h" |
| 35 |
|
| 36 |
#ifdef HEIM_WEAK_CRYPTO |
| 37 |
|
| 38 |
static OM_uint32 |
| 39 |
unwrap_des |
| 40 |
(OM_uint32 * minor_status, |
| 41 |
const gsskrb5_ctx context_handle, |
| 42 |
const gss_buffer_t input_message_buffer, |
| 43 |
gss_buffer_t output_message_buffer, |
| 44 |
int * conf_state, |
| 45 |
gss_qop_t * qop_state, |
| 46 |
krb5_keyblock *key |
| 47 |
) |
| 48 |
{ |
| 49 |
u_char *p, *seq; |
| 50 |
size_t len; |
| 51 |
EVP_MD_CTX *md5; |
| 52 |
u_char hash[16]; |
| 53 |
EVP_CIPHER_CTX des_ctx; |
| 54 |
DES_key_schedule schedule; |
| 55 |
DES_cblock deskey; |
| 56 |
DES_cblock zero; |
| 57 |
size_t i; |
| 58 |
uint32_t seq_number; |
| 59 |
size_t padlength; |
| 60 |
OM_uint32 ret; |
| 61 |
int cstate; |
| 62 |
int cmp; |
| 63 |
int token_len; |
| 64 |
|
| 65 |
if (IS_DCE_STYLE(context_handle)) { |
| 66 |
token_len = 22 + 8 + 15; /* 45 */ |
| 67 |
if (input_message_buffer->length < token_len) |
| 68 |
return GSS_S_BAD_MECH; |
| 69 |
} else { |
| 70 |
token_len = input_message_buffer->length; |
| 71 |
} |
| 72 |
|
| 73 |
p = input_message_buffer->value; |
| 74 |
ret = _gsskrb5_verify_header (&p, |
| 75 |
token_len, |
| 76 |
"\x02\x01", |
| 77 |
GSS_KRB5_MECHANISM); |
| 78 |
if (ret) |
| 79 |
return ret; |
| 80 |
|
| 81 |
len = (p - (u_char *)input_message_buffer->value) |
| 82 |
+ 22 + 8; |
| 83 |
if (input_message_buffer->length < len) |
| 84 |
return GSS_S_BAD_MECH; |
| 85 |
|
| 86 |
if (memcmp (p, "\x00\x00", 2) != 0) |
| 87 |
return GSS_S_BAD_SIG; |
| 88 |
p += 2; |
| 89 |
if (memcmp (p, "\x00\x00", 2) == 0) { |
| 90 |
cstate = 1; |
| 91 |
} else if (memcmp (p, "\xFF\xFF", 2) == 0) { |
| 92 |
cstate = 0; |
| 93 |
} else |
| 94 |
return GSS_S_BAD_MIC; |
| 95 |
p += 2; |
| 96 |
if(conf_state != NULL) |
| 97 |
*conf_state = cstate; |
| 98 |
if (memcmp (p, "\xff\xff", 2) != 0) |
| 99 |
return GSS_S_DEFECTIVE_TOKEN; |
| 100 |
p += 2; |
| 101 |
p += 16; |
| 102 |
|
| 103 |
len = p - (u_char *)input_message_buffer->value; |
| 104 |
|
| 105 |
if(cstate) { |
| 106 |
/* decrypt data */ |
| 107 |
memcpy (&deskey, key->keyvalue.data, sizeof(deskey)); |
| 108 |
memset (&zero, 0, sizeof(zero)); |
| 109 |
|
| 110 |
for (i = 0; i < sizeof(deskey); ++i) |
| 111 |
deskey[i] ^= 0xf0; |
| 112 |
|
| 113 |
|
| 114 |
EVP_CIPHER_CTX_init(&des_ctx); |
| 115 |
EVP_CipherInit_ex(&des_ctx, EVP_des_cbc(), NULL, deskey, zero, 0); |
| 116 |
EVP_Cipher(&des_ctx, p, p, input_message_buffer->length - len); |
| 117 |
EVP_CIPHER_CTX_cleanup(&des_ctx); |
| 118 |
|
| 119 |
memset (&schedule, 0, sizeof(schedule)); |
| 120 |
} |
| 121 |
|
| 122 |
if (IS_DCE_STYLE(context_handle)) { |
| 123 |
padlength = 0; |
| 124 |
} else { |
| 125 |
/* check pad */ |
| 126 |
ret = _gssapi_verify_pad(input_message_buffer, |
| 127 |
input_message_buffer->length - len - 8, |
| 128 |
&padlength); |
| 129 |
if (ret) |
| 130 |
return ret; |
| 131 |
} |
| 132 |
|
| 133 |
md5 = EVP_MD_CTX_create(); |
| 134 |
EVP_DigestInit_ex(md5, EVP_md5(), NULL); |
| 135 |
EVP_DigestUpdate(md5, p - 24, 8); |
| 136 |
EVP_DigestUpdate(md5, p, input_message_buffer->length - len); |
| 137 |
EVP_DigestFinal_ex(md5, hash, NULL); |
| 138 |
EVP_MD_CTX_destroy(md5); |
| 139 |
|
| 140 |
memset (&zero, 0, sizeof(zero)); |
| 141 |
memcpy (&deskey, key->keyvalue.data, sizeof(deskey)); |
| 142 |
DES_set_key_unchecked (&deskey, &schedule); |
| 143 |
DES_cbc_cksum ((void *)hash, (void *)hash, sizeof(hash), |
| 144 |
&schedule, &zero); |
| 145 |
if (ct_memcmp (p - 8, hash, 8) != 0) |
| 146 |
return GSS_S_BAD_MIC; |
| 147 |
|
| 148 |
/* verify sequence number */ |
| 149 |
|
| 150 |
HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex); |
| 151 |
|
| 152 |
p -= 16; |
| 153 |
|
| 154 |
EVP_CIPHER_CTX_init(&des_ctx); |
| 155 |
EVP_CipherInit_ex(&des_ctx, EVP_des_cbc(), NULL, key->keyvalue.data, hash, 0); |
| 156 |
EVP_Cipher(&des_ctx, p, p, 8); |
| 157 |
EVP_CIPHER_CTX_cleanup(&des_ctx); |
| 158 |
|
| 159 |
memset (deskey, 0, sizeof(deskey)); |
| 160 |
memset (&schedule, 0, sizeof(schedule)); |
| 161 |
|
| 162 |
seq = p; |
| 163 |
_gsskrb5_decode_om_uint32(seq, &seq_number); |
| 164 |
|
| 165 |
if (context_handle->more_flags & LOCAL) |
| 166 |
cmp = ct_memcmp(&seq[4], "\xff\xff\xff\xff", 4); |
| 167 |
else |
| 168 |
cmp = ct_memcmp(&seq[4], "\x00\x00\x00\x00", 4); |
| 169 |
|
| 170 |
if (cmp != 0) { |
| 171 |
HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex); |
| 172 |
return GSS_S_BAD_MIC; |
| 173 |
} |
| 174 |
|
| 175 |
ret = _gssapi_msg_order_check(context_handle->order, seq_number); |
| 176 |
if (ret) { |
| 177 |
HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex); |
| 178 |
return ret; |
| 179 |
} |
| 180 |
|
| 181 |
HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex); |
| 182 |
|
| 183 |
/* copy out data */ |
| 184 |
|
| 185 |
output_message_buffer->length = input_message_buffer->length |
| 186 |
- len - padlength - 8; |
| 187 |
output_message_buffer->value = malloc(output_message_buffer->length); |
| 188 |
if(output_message_buffer->length != 0 && output_message_buffer->value == NULL) |
| 189 |
return GSS_S_FAILURE; |
| 190 |
if (output_message_buffer->value != NULL) |
| 191 |
memcpy (output_message_buffer->value, |
| 192 |
p + 24, |
| 193 |
output_message_buffer->length); |
| 194 |
return GSS_S_COMPLETE; |
| 195 |
} |
| 196 |
#endif |
| 197 |
|
| 198 |
static OM_uint32 |
| 199 |
unwrap_des3 |
| 200 |
(OM_uint32 * minor_status, |
| 201 |
const gsskrb5_ctx context_handle, |
| 202 |
krb5_context context, |
| 203 |
const gss_buffer_t input_message_buffer, |
| 204 |
gss_buffer_t output_message_buffer, |
| 205 |
int * conf_state, |
| 206 |
gss_qop_t * qop_state, |
| 207 |
krb5_keyblock *key |
| 208 |
) |
| 209 |
{ |
| 210 |
u_char *p; |
| 211 |
size_t len; |
| 212 |
u_char *seq; |
| 213 |
krb5_data seq_data; |
| 214 |
u_char cksum[20]; |
| 215 |
uint32_t seq_number; |
| 216 |
size_t padlength; |
| 217 |
OM_uint32 ret; |
| 218 |
int cstate; |
| 219 |
krb5_crypto crypto; |
| 220 |
Checksum csum; |
| 221 |
int cmp; |
| 222 |
int token_len; |
| 223 |
|
| 224 |
if (IS_DCE_STYLE(context_handle)) { |
| 225 |
token_len = 34 + 8 + 15; /* 57 */ |
| 226 |
if (input_message_buffer->length < token_len) |
| 227 |
return GSS_S_BAD_MECH; |
| 228 |
} else { |
| 229 |
token_len = input_message_buffer->length; |
| 230 |
} |
| 231 |
|
| 232 |
p = input_message_buffer->value; |
| 233 |
ret = _gsskrb5_verify_header (&p, |
| 234 |
token_len, |
| 235 |
"\x02\x01", |
| 236 |
GSS_KRB5_MECHANISM); |
| 237 |
if (ret) |
| 238 |
return ret; |
| 239 |
|
| 240 |
len = (p - (u_char *)input_message_buffer->value) |
| 241 |
+ 34 + 8; |
| 242 |
if (input_message_buffer->length < len) |
| 243 |
return GSS_S_BAD_MECH; |
| 244 |
|
| 245 |
if (ct_memcmp (p, "\x04\x00", 2) != 0) /* HMAC SHA1 DES3_KD */ |
| 246 |
return GSS_S_BAD_SIG; |
| 247 |
p += 2; |
| 248 |
if (ct_memcmp (p, "\x02\x00", 2) == 0) { |
| 249 |
cstate = 1; |
| 250 |
} else if (ct_memcmp (p, "\xff\xff", 2) == 0) { |
| 251 |
cstate = 0; |
| 252 |
} else |
| 253 |
return GSS_S_BAD_MIC; |
| 254 |
p += 2; |
| 255 |
if(conf_state != NULL) |
| 256 |
*conf_state = cstate; |
| 257 |
if (ct_memcmp (p, "\xff\xff", 2) != 0) |
| 258 |
return GSS_S_DEFECTIVE_TOKEN; |
| 259 |
p += 2; |
| 260 |
p += 28; |
| 261 |
|
| 262 |
len = p - (u_char *)input_message_buffer->value; |
| 263 |
|
| 264 |
if(cstate) { |
| 265 |
/* decrypt data */ |
| 266 |
krb5_data tmp; |
| 267 |
|
| 268 |
ret = krb5_crypto_init(context, key, |
| 269 |
ETYPE_DES3_CBC_NONE, &crypto); |
| 270 |
if (ret) { |
| 271 |
*minor_status = ret; |
| 272 |
return GSS_S_FAILURE; |
| 273 |
} |
| 274 |
ret = krb5_decrypt(context, crypto, KRB5_KU_USAGE_SEAL, |
| 275 |
p, input_message_buffer->length - len, &tmp); |
| 276 |
krb5_crypto_destroy(context, crypto); |
| 277 |
if (ret) { |
| 278 |
*minor_status = ret; |
| 279 |
return GSS_S_FAILURE; |
| 280 |
} |
| 281 |
assert (tmp.length == input_message_buffer->length - len); |
| 282 |
|
| 283 |
memcpy (p, tmp.data, tmp.length); |
| 284 |
krb5_data_free(&tmp); |
| 285 |
} |
| 286 |
|
| 287 |
if (IS_DCE_STYLE(context_handle)) { |
| 288 |
padlength = 0; |
| 289 |
} else { |
| 290 |
/* check pad */ |
| 291 |
ret = _gssapi_verify_pad(input_message_buffer, |
| 292 |
input_message_buffer->length - len - 8, |
| 293 |
&padlength); |
| 294 |
if (ret) |
| 295 |
return ret; |
| 296 |
} |
| 297 |
|
| 298 |
/* verify sequence number */ |
| 299 |
|
| 300 |
HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex); |
| 301 |
|
| 302 |
p -= 28; |
| 303 |
|
| 304 |
ret = krb5_crypto_init(context, key, |
| 305 |
ETYPE_DES3_CBC_NONE, &crypto); |
| 306 |
if (ret) { |
| 307 |
*minor_status = ret; |
| 308 |
HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex); |
| 309 |
return GSS_S_FAILURE; |
| 310 |
} |
| 311 |
{ |
| 312 |
DES_cblock ivec; |
| 313 |
|
| 314 |
memcpy(&ivec, p + 8, 8); |
| 315 |
ret = krb5_decrypt_ivec (context, |
| 316 |
crypto, |
| 317 |
KRB5_KU_USAGE_SEQ, |
| 318 |
p, 8, &seq_data, |
| 319 |
&ivec); |
| 320 |
} |
| 321 |
krb5_crypto_destroy (context, crypto); |
| 322 |
if (ret) { |
| 323 |
*minor_status = ret; |
| 324 |
HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex); |
| 325 |
return GSS_S_FAILURE; |
| 326 |
} |
| 327 |
if (seq_data.length != 8) { |
| 328 |
krb5_data_free (&seq_data); |
| 329 |
*minor_status = 0; |
| 330 |
HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex); |
| 331 |
return GSS_S_BAD_MIC; |
| 332 |
} |
| 333 |
|
| 334 |
seq = seq_data.data; |
| 335 |
_gsskrb5_decode_om_uint32(seq, &seq_number); |
| 336 |
|
| 337 |
if (context_handle->more_flags & LOCAL) |
| 338 |
cmp = ct_memcmp(&seq[4], "\xff\xff\xff\xff", 4); |
| 339 |
else |
| 340 |
cmp = ct_memcmp(&seq[4], "\x00\x00\x00\x00", 4); |
| 341 |
|
| 342 |
krb5_data_free (&seq_data); |
| 343 |
if (cmp != 0) { |
| 344 |
*minor_status = 0; |
| 345 |
HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex); |
| 346 |
return GSS_S_BAD_MIC; |
| 347 |
} |
| 348 |
|
| 349 |
ret = _gssapi_msg_order_check(context_handle->order, seq_number); |
| 350 |
if (ret) { |
| 351 |
*minor_status = 0; |
| 352 |
HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex); |
| 353 |
return ret; |
| 354 |
} |
| 355 |
|
| 356 |
HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex); |
| 357 |
|
| 358 |
/* verify checksum */ |
| 359 |
|
| 360 |
memcpy (cksum, p + 8, 20); |
| 361 |
|
| 362 |
memcpy (p + 20, p - 8, 8); |
| 363 |
|
| 364 |
csum.cksumtype = CKSUMTYPE_HMAC_SHA1_DES3; |
| 365 |
csum.checksum.length = 20; |
| 366 |
csum.checksum.data = cksum; |
| 367 |
|
| 368 |
ret = krb5_crypto_init(context, key, 0, &crypto); |
| 369 |
if (ret) { |
| 370 |
*minor_status = ret; |
| 371 |
return GSS_S_FAILURE; |
| 372 |
} |
| 373 |
|
| 374 |
ret = krb5_verify_checksum (context, crypto, |
| 375 |
KRB5_KU_USAGE_SIGN, |
| 376 |
p + 20, |
| 377 |
input_message_buffer->length - len + 8, |
| 378 |
&csum); |
| 379 |
krb5_crypto_destroy (context, crypto); |
| 380 |
if (ret) { |
| 381 |
*minor_status = ret; |
| 382 |
return GSS_S_FAILURE; |
| 383 |
} |
| 384 |
|
| 385 |
/* copy out data */ |
| 386 |
|
| 387 |
output_message_buffer->length = input_message_buffer->length |
| 388 |
- len - padlength - 8; |
| 389 |
output_message_buffer->value = malloc(output_message_buffer->length); |
| 390 |
if(output_message_buffer->length != 0 && output_message_buffer->value == NULL) |
| 391 |
return GSS_S_FAILURE; |
| 392 |
if (output_message_buffer->value != NULL) |
| 393 |
memcpy (output_message_buffer->value, |
| 394 |
p + 36, |
| 395 |
output_message_buffer->length); |
| 396 |
return GSS_S_COMPLETE; |
| 397 |
} |
| 398 |
|
| 399 |
OM_uint32 GSSAPI_CALLCONV _gsskrb5_unwrap |
| 400 |
(OM_uint32 * minor_status, |
| 401 |
gss_const_ctx_id_t context_handle, |
| 402 |
const gss_buffer_t input_message_buffer, |
| 403 |
gss_buffer_t output_message_buffer, |
| 404 |
int * conf_state, |
| 405 |
gss_qop_t * qop_state |
| 406 |
) |
| 407 |
{ |
| 408 |
krb5_keyblock *key; |
| 409 |
krb5_context context; |
| 410 |
OM_uint32 ret; |
| 411 |
gsskrb5_ctx ctx = (gsskrb5_ctx) context_handle; |
| 412 |
|
| 413 |
output_message_buffer->value = NULL; |
| 414 |
output_message_buffer->length = 0; |
| 415 |
if (qop_state != NULL) |
| 416 |
*qop_state = GSS_C_QOP_DEFAULT; |
| 417 |
|
| 418 |
GSSAPI_KRB5_INIT (&context); |
| 419 |
|
| 420 |
if (ctx->more_flags & IS_CFX) |
| 421 |
return _gssapi_unwrap_cfx (minor_status, ctx, context, |
| 422 |
input_message_buffer, output_message_buffer, |
| 423 |
conf_state, qop_state); |
| 424 |
|
| 425 |
HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex); |
| 426 |
ret = _gsskrb5i_get_token_key(ctx, context, &key); |
| 427 |
HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex); |
| 428 |
if (ret) { |
| 429 |
*minor_status = ret; |
| 430 |
return GSS_S_FAILURE; |
| 431 |
} |
| 432 |
|
| 433 |
*minor_status = 0; |
| 434 |
|
| 435 |
switch (key->keytype) { |
| 436 |
case KRB5_ENCTYPE_DES_CBC_CRC : |
| 437 |
case KRB5_ENCTYPE_DES_CBC_MD4 : |
| 438 |
case KRB5_ENCTYPE_DES_CBC_MD5 : |
| 439 |
#ifdef HEIM_WEAK_CRYPTO |
| 440 |
ret = unwrap_des (minor_status, ctx, |
| 441 |
input_message_buffer, output_message_buffer, |
| 442 |
conf_state, qop_state, key); |
| 443 |
#else |
| 444 |
ret = GSS_S_FAILURE; |
| 445 |
#endif |
| 446 |
break; |
| 447 |
case KRB5_ENCTYPE_DES3_CBC_MD5 : |
| 448 |
case KRB5_ENCTYPE_DES3_CBC_SHA1 : |
| 449 |
ret = unwrap_des3 (minor_status, ctx, context, |
| 450 |
input_message_buffer, output_message_buffer, |
| 451 |
conf_state, qop_state, key); |
| 452 |
break; |
| 453 |
case KRB5_ENCTYPE_ARCFOUR_HMAC_MD5: |
| 454 |
case KRB5_ENCTYPE_ARCFOUR_HMAC_MD5_56: |
| 455 |
ret = _gssapi_unwrap_arcfour (minor_status, ctx, context, |
| 456 |
input_message_buffer, output_message_buffer, |
| 457 |
conf_state, qop_state, key); |
| 458 |
break; |
| 459 |
default : |
| 460 |
abort(); |
| 461 |
break; |
| 462 |
} |
| 463 |
krb5_free_keyblock (context, key); |
| 464 |
return ret; |
| 465 |
} |