2 * Copyright (c) 1997 - 2004 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. 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
34 #include "gsskrb5_locl.h"
36 #ifdef HEIM_WEAK_CRYPTO
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,
45 gss_qop_t * qop_state,
53 EVP_CIPHER_CTX des_ctx;
54 DES_key_schedule schedule;
65 if (IS_DCE_STYLE(context_handle)) {
66 token_len = 22 + 8 + 15; /* 45 */
68 token_len = input_message_buffer->length;
71 p = input_message_buffer->value;
72 ret = _gsskrb5_verify_header (&p,
79 if (memcmp (p, "\x00\x00", 2) != 0)
82 if (memcmp (p, "\x00\x00", 2) == 0) {
84 } else if (memcmp (p, "\xFF\xFF", 2) == 0) {
89 if(conf_state != NULL)
91 if (memcmp (p, "\xff\xff", 2) != 0)
92 return GSS_S_DEFECTIVE_TOKEN;
96 len = p - (u_char *)input_message_buffer->value;
100 memcpy (&deskey, key->keyvalue.data, sizeof(deskey));
102 for (i = 0; i < sizeof(deskey); ++i)
106 EVP_CIPHER_CTX_init(&des_ctx);
107 EVP_CipherInit_ex(&des_ctx, EVP_des_cbc(), NULL, deskey, zero, 0);
108 EVP_Cipher(&des_ctx, p, p, input_message_buffer->length - len);
109 EVP_CIPHER_CTX_cleanup(&des_ctx);
111 memset (&schedule, 0, sizeof(schedule));
114 if (IS_DCE_STYLE(context_handle)) {
118 ret = _gssapi_verify_pad(input_message_buffer,
119 input_message_buffer->length - len,
125 md5 = EVP_MD_CTX_create();
126 EVP_DigestInit_ex(md5, EVP_md5(), NULL);
127 EVP_DigestUpdate(md5, p - 24, 8);
128 EVP_DigestUpdate(md5, p, input_message_buffer->length - len);
129 EVP_DigestFinal_ex(md5, hash, NULL);
130 EVP_MD_CTX_destroy(md5);
132 memset (&zero, 0, sizeof(zero));
133 memcpy (&deskey, key->keyvalue.data, sizeof(deskey));
134 DES_set_key_unchecked (&deskey, &schedule);
135 DES_cbc_cksum ((void *)hash, (void *)hash, sizeof(hash),
137 if (ct_memcmp (p - 8, hash, 8) != 0)
138 return GSS_S_BAD_MIC;
140 /* verify sequence number */
142 HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex);
146 EVP_CIPHER_CTX_init(&des_ctx);
147 EVP_CipherInit_ex(&des_ctx, EVP_des_cbc(), NULL, key->keyvalue.data, hash, 0);
148 EVP_Cipher(&des_ctx, p, p, 8);
149 EVP_CIPHER_CTX_cleanup(&des_ctx);
151 memset (deskey, 0, sizeof(deskey));
152 memset (&schedule, 0, sizeof(schedule));
155 _gsskrb5_decode_om_uint32(seq, &seq_number);
157 if (context_handle->more_flags & LOCAL)
158 cmp = ct_memcmp(&seq[4], "\xff\xff\xff\xff", 4);
160 cmp = ct_memcmp(&seq[4], "\x00\x00\x00\x00", 4);
163 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
164 return GSS_S_BAD_MIC;
167 ret = _gssapi_msg_order_check(context_handle->order, seq_number);
169 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
173 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
177 output_message_buffer->length = input_message_buffer->length
178 - len - padlength - 8;
179 output_message_buffer->value = malloc(output_message_buffer->length);
180 if(output_message_buffer->length != 0 && output_message_buffer->value == NULL)
181 return GSS_S_FAILURE;
182 memcpy (output_message_buffer->value,
184 output_message_buffer->length);
185 return GSS_S_COMPLETE;
191 (OM_uint32 * minor_status,
192 const gsskrb5_ctx context_handle,
193 krb5_context context,
194 const gss_buffer_t input_message_buffer,
195 gss_buffer_t output_message_buffer,
197 gss_qop_t * qop_state,
215 if (IS_DCE_STYLE(context_handle)) {
216 token_len = 34 + 8 + 15; /* 57 */
218 token_len = input_message_buffer->length;
221 p = input_message_buffer->value;
222 ret = _gsskrb5_verify_header (&p,
229 if (memcmp (p, "\x04\x00", 2) != 0) /* HMAC SHA1 DES3_KD */
230 return GSS_S_BAD_SIG;
232 if (ct_memcmp (p, "\x02\x00", 2) == 0) {
234 } else if (ct_memcmp (p, "\xff\xff", 2) == 0) {
237 return GSS_S_BAD_MIC;
239 if(conf_state != NULL)
240 *conf_state = cstate;
241 if (ct_memcmp (p, "\xff\xff", 2) != 0)
242 return GSS_S_DEFECTIVE_TOKEN;
246 len = p - (u_char *)input_message_buffer->value;
252 ret = krb5_crypto_init(context, key,
253 ETYPE_DES3_CBC_NONE, &crypto);
256 return GSS_S_FAILURE;
258 ret = krb5_decrypt(context, crypto, KRB5_KU_USAGE_SEAL,
259 p, input_message_buffer->length - len, &tmp);
260 krb5_crypto_destroy(context, crypto);
263 return GSS_S_FAILURE;
265 assert (tmp.length == input_message_buffer->length - len);
267 memcpy (p, tmp.data, tmp.length);
268 krb5_data_free(&tmp);
271 if (IS_DCE_STYLE(context_handle)) {
275 ret = _gssapi_verify_pad(input_message_buffer,
276 input_message_buffer->length - len,
282 /* verify sequence number */
284 HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex);
288 ret = krb5_crypto_init(context, key,
289 ETYPE_DES3_CBC_NONE, &crypto);
292 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
293 return GSS_S_FAILURE;
298 memcpy(&ivec, p + 8, 8);
299 ret = krb5_decrypt_ivec (context,
305 krb5_crypto_destroy (context, crypto);
308 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
309 return GSS_S_FAILURE;
311 if (seq_data.length != 8) {
312 krb5_data_free (&seq_data);
314 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
315 return GSS_S_BAD_MIC;
319 _gsskrb5_decode_om_uint32(seq, &seq_number);
321 if (context_handle->more_flags & LOCAL)
322 cmp = ct_memcmp(&seq[4], "\xff\xff\xff\xff", 4);
324 cmp = ct_memcmp(&seq[4], "\x00\x00\x00\x00", 4);
326 krb5_data_free (&seq_data);
329 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
330 return GSS_S_BAD_MIC;
333 ret = _gssapi_msg_order_check(context_handle->order, seq_number);
336 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
340 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
342 /* verify checksum */
344 memcpy (cksum, p + 8, 20);
346 memcpy (p + 20, p - 8, 8);
348 csum.cksumtype = CKSUMTYPE_HMAC_SHA1_DES3;
349 csum.checksum.length = 20;
350 csum.checksum.data = cksum;
352 ret = krb5_crypto_init(context, key, 0, &crypto);
355 return GSS_S_FAILURE;
358 ret = krb5_verify_checksum (context, crypto,
361 input_message_buffer->length - len + 8,
363 krb5_crypto_destroy (context, crypto);
366 return GSS_S_FAILURE;
371 output_message_buffer->length = input_message_buffer->length
372 - len - padlength - 8;
373 output_message_buffer->value = malloc(output_message_buffer->length);
374 if(output_message_buffer->length != 0 && output_message_buffer->value == NULL)
375 return GSS_S_FAILURE;
376 memcpy (output_message_buffer->value,
378 output_message_buffer->length);
379 return GSS_S_COMPLETE;
382 OM_uint32 GSSAPI_CALLCONV _gsskrb5_unwrap
383 (OM_uint32 * minor_status,
384 const gss_ctx_id_t context_handle,
385 const gss_buffer_t input_message_buffer,
386 gss_buffer_t output_message_buffer,
388 gss_qop_t * qop_state
392 krb5_context context;
394 krb5_keytype keytype;
395 gsskrb5_ctx ctx = (gsskrb5_ctx) context_handle;
397 output_message_buffer->value = NULL;
398 output_message_buffer->length = 0;
399 if (qop_state != NULL)
400 *qop_state = GSS_C_QOP_DEFAULT;
402 GSSAPI_KRB5_INIT (&context);
404 if (ctx->more_flags & IS_CFX)
405 return _gssapi_unwrap_cfx (minor_status, ctx, context,
406 input_message_buffer, output_message_buffer,
407 conf_state, qop_state);
409 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
410 ret = _gsskrb5i_get_token_key(ctx, context, &key);
411 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
414 return GSS_S_FAILURE;
416 krb5_enctype_to_keytype (context, key->keytype, &keytype);
422 #ifdef HEIM_WEAK_CRYPTO
423 ret = unwrap_des (minor_status, ctx,
424 input_message_buffer, output_message_buffer,
425 conf_state, qop_state, key);
431 ret = unwrap_des3 (minor_status, ctx, context,
432 input_message_buffer, output_message_buffer,
433 conf_state, qop_state, key);
435 case KEYTYPE_ARCFOUR:
436 case KEYTYPE_ARCFOUR_56:
437 ret = _gssapi_unwrap_arcfour (minor_status, ctx, context,
438 input_message_buffer, output_message_buffer,
439 conf_state, qop_state, key);
445 krb5_free_keyblock (context, key);