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 "krb5/gsskrb5_locl.h"
36 RCSID("$Id: unwrap.c 19031 2006-11-13 18:02:57Z lha $");
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 DES_key_schedule schedule;
63 p = input_message_buffer->value;
64 ret = _gsskrb5_verify_header (&p,
65 input_message_buffer->length,
71 if (memcmp (p, "\x00\x00", 2) != 0)
74 if (memcmp (p, "\x00\x00", 2) == 0) {
76 } else if (memcmp (p, "\xFF\xFF", 2) == 0) {
81 if(conf_state != NULL)
83 if (memcmp (p, "\xff\xff", 2) != 0)
84 return GSS_S_DEFECTIVE_TOKEN;
88 len = p - (u_char *)input_message_buffer->value;
92 memcpy (&deskey, key->keyvalue.data, sizeof(deskey));
94 for (i = 0; i < sizeof(deskey); ++i)
96 DES_set_key (&deskey, &schedule);
97 memset (&zero, 0, sizeof(zero));
98 DES_cbc_encrypt ((void *)p,
100 input_message_buffer->length - len,
105 memset (deskey, 0, sizeof(deskey));
106 memset (&schedule, 0, sizeof(schedule));
109 ret = _gssapi_verify_pad(input_message_buffer,
110 input_message_buffer->length - len,
116 MD5_Update (&md5, p - 24, 8);
117 MD5_Update (&md5, p, input_message_buffer->length - len);
118 MD5_Final (hash, &md5);
120 memset (&zero, 0, sizeof(zero));
121 memcpy (&deskey, key->keyvalue.data, sizeof(deskey));
122 DES_set_key (&deskey, &schedule);
123 DES_cbc_cksum ((void *)hash, (void *)hash, sizeof(hash),
125 if (memcmp (p - 8, hash, 8) != 0)
126 return GSS_S_BAD_MIC;
128 /* verify sequence number */
130 HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex);
133 DES_set_key (&deskey, &schedule);
134 DES_cbc_encrypt ((void *)p, (void *)p, 8,
135 &schedule, (DES_cblock *)hash, DES_DECRYPT);
137 memset (deskey, 0, sizeof(deskey));
138 memset (&schedule, 0, sizeof(schedule));
141 _gsskrb5_decode_om_uint32(seq, &seq_number);
143 if (context_handle->more_flags & LOCAL)
144 cmp = memcmp(&seq[4], "\xff\xff\xff\xff", 4);
146 cmp = memcmp(&seq[4], "\x00\x00\x00\x00", 4);
149 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
150 return GSS_S_BAD_MIC;
153 ret = _gssapi_msg_order_check(context_handle->order, seq_number);
155 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
159 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
163 output_message_buffer->length = input_message_buffer->length
164 - len - padlength - 8;
165 output_message_buffer->value = malloc(output_message_buffer->length);
166 if(output_message_buffer->length != 0 && output_message_buffer->value == NULL)
167 return GSS_S_FAILURE;
168 memcpy (output_message_buffer->value,
170 output_message_buffer->length);
171 return GSS_S_COMPLETE;
176 (OM_uint32 * minor_status,
177 const gsskrb5_ctx context_handle,
178 krb5_context context,
179 const gss_buffer_t input_message_buffer,
180 gss_buffer_t output_message_buffer,
182 gss_qop_t * qop_state,
199 p = input_message_buffer->value;
200 ret = _gsskrb5_verify_header (&p,
201 input_message_buffer->length,
207 if (memcmp (p, "\x04\x00", 2) != 0) /* HMAC SHA1 DES3_KD */
208 return GSS_S_BAD_SIG;
210 if (memcmp (p, "\x02\x00", 2) == 0) {
212 } else if (memcmp (p, "\xff\xff", 2) == 0) {
215 return GSS_S_BAD_MIC;
217 if(conf_state != NULL)
218 *conf_state = cstate;
219 if (memcmp (p, "\xff\xff", 2) != 0)
220 return GSS_S_DEFECTIVE_TOKEN;
224 len = p - (u_char *)input_message_buffer->value;
230 ret = krb5_crypto_init(context, key,
231 ETYPE_DES3_CBC_NONE, &crypto);
234 return GSS_S_FAILURE;
236 ret = krb5_decrypt(context, crypto, KRB5_KU_USAGE_SEAL,
237 p, input_message_buffer->length - len, &tmp);
238 krb5_crypto_destroy(context, crypto);
241 return GSS_S_FAILURE;
243 assert (tmp.length == input_message_buffer->length - len);
245 memcpy (p, tmp.data, tmp.length);
246 krb5_data_free(&tmp);
249 ret = _gssapi_verify_pad(input_message_buffer,
250 input_message_buffer->length - len,
255 /* verify sequence number */
257 HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex);
261 ret = krb5_crypto_init(context, key,
262 ETYPE_DES3_CBC_NONE, &crypto);
265 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
266 return GSS_S_FAILURE;
271 memcpy(&ivec, p + 8, 8);
272 ret = krb5_decrypt_ivec (context,
278 krb5_crypto_destroy (context, crypto);
281 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
282 return GSS_S_FAILURE;
284 if (seq_data.length != 8) {
285 krb5_data_free (&seq_data);
287 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
288 return GSS_S_BAD_MIC;
292 _gsskrb5_decode_om_uint32(seq, &seq_number);
294 if (context_handle->more_flags & LOCAL)
295 cmp = memcmp(&seq[4], "\xff\xff\xff\xff", 4);
297 cmp = memcmp(&seq[4], "\x00\x00\x00\x00", 4);
299 krb5_data_free (&seq_data);
302 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
303 return GSS_S_BAD_MIC;
306 ret = _gssapi_msg_order_check(context_handle->order, seq_number);
309 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
313 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
315 /* verify checksum */
317 memcpy (cksum, p + 8, 20);
319 memcpy (p + 20, p - 8, 8);
321 csum.cksumtype = CKSUMTYPE_HMAC_SHA1_DES3;
322 csum.checksum.length = 20;
323 csum.checksum.data = cksum;
325 ret = krb5_crypto_init(context, key, 0, &crypto);
328 return GSS_S_FAILURE;
331 ret = krb5_verify_checksum (context, crypto,
334 input_message_buffer->length - len + 8,
336 krb5_crypto_destroy (context, crypto);
339 return GSS_S_FAILURE;
344 output_message_buffer->length = input_message_buffer->length
345 - len - padlength - 8;
346 output_message_buffer->value = malloc(output_message_buffer->length);
347 if(output_message_buffer->length != 0 && output_message_buffer->value == NULL)
348 return GSS_S_FAILURE;
349 memcpy (output_message_buffer->value,
351 output_message_buffer->length);
352 return GSS_S_COMPLETE;
355 OM_uint32 _gsskrb5_unwrap
356 (OM_uint32 * minor_status,
357 const gss_ctx_id_t context_handle,
358 const gss_buffer_t input_message_buffer,
359 gss_buffer_t output_message_buffer,
361 gss_qop_t * qop_state
365 krb5_context context;
367 krb5_keytype keytype;
368 gsskrb5_ctx ctx = (gsskrb5_ctx) context_handle;
370 output_message_buffer->value = NULL;
371 output_message_buffer->length = 0;
373 GSSAPI_KRB5_INIT (&context);
375 if (qop_state != NULL)
376 *qop_state = GSS_C_QOP_DEFAULT;
377 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
378 ret = _gsskrb5i_get_token_key(ctx, context, &key);
379 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
382 return GSS_S_FAILURE;
384 krb5_enctype_to_keytype (context, key->keytype, &keytype);
390 ret = unwrap_des (minor_status, ctx,
391 input_message_buffer, output_message_buffer,
392 conf_state, qop_state, key);
395 ret = unwrap_des3 (minor_status, ctx, context,
396 input_message_buffer, output_message_buffer,
397 conf_state, qop_state, key);
399 case KEYTYPE_ARCFOUR:
400 case KEYTYPE_ARCFOUR_56:
401 ret = _gssapi_unwrap_arcfour (minor_status, ctx, context,
402 input_message_buffer, output_message_buffer,
403 conf_state, qop_state, key);
406 ret = _gssapi_unwrap_cfx (minor_status, ctx, context,
407 input_message_buffer, output_message_buffer,
408 conf_state, qop_state, key);
411 krb5_free_keyblock (context, key);