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 */
67 if (input_message_buffer->length < token_len)
68 return GSS_S_BAD_MECH;
70 token_len = input_message_buffer->length;
73 p = input_message_buffer->value;
74 ret = _gsskrb5_verify_header (&p,
81 len = (p - (u_char *)input_message_buffer->value)
83 if (input_message_buffer->length < len)
84 return GSS_S_BAD_MECH;
86 if (memcmp (p, "\x00\x00", 2) != 0)
89 if (memcmp (p, "\x00\x00", 2) == 0) {
91 } else if (memcmp (p, "\xFF\xFF", 2) == 0) {
96 if(conf_state != NULL)
98 if (memcmp (p, "\xff\xff", 2) != 0)
99 return GSS_S_DEFECTIVE_TOKEN;
103 len = p - (u_char *)input_message_buffer->value;
107 memcpy (&deskey, key->keyvalue.data, sizeof(deskey));
108 memset (&zero, 0, sizeof(zero));
110 for (i = 0; i < sizeof(deskey); ++i)
114 des_ctx = EVP_CIPHER_CTX_new();
115 if (des_ctx == NULL) {
116 memset (deskey, 0, sizeof(deskey));
117 *minor_status = ENOMEM;
118 return GSS_S_FAILURE;
120 EVP_CipherInit_ex(des_ctx, EVP_des_cbc(), NULL, deskey, zero, 0);
121 EVP_Cipher(des_ctx, p, p, input_message_buffer->length - len);
122 EVP_CIPHER_CTX_free(des_ctx);
124 memset (deskey, 0, sizeof(deskey));
127 if (IS_DCE_STYLE(context_handle)) {
131 ret = _gssapi_verify_pad(input_message_buffer,
132 input_message_buffer->length - len - 8,
138 md5 = EVP_MD_CTX_create();
139 EVP_DigestInit_ex(md5, EVP_md5(), NULL);
140 EVP_DigestUpdate(md5, p - 24, 8);
141 EVP_DigestUpdate(md5, p, input_message_buffer->length - len);
142 EVP_DigestFinal_ex(md5, hash, NULL);
143 EVP_MD_CTX_destroy(md5);
145 memset (&zero, 0, sizeof(zero));
146 memcpy (&deskey, key->keyvalue.data, sizeof(deskey));
147 DES_set_key_unchecked (&deskey, &schedule);
148 DES_cbc_cksum ((void *)hash, (void *)hash, sizeof(hash),
150 if (ct_memcmp (p - 8, hash, 8) != 0) {
151 memset (deskey, 0, sizeof(deskey));
152 memset (&schedule, 0, sizeof(schedule));
153 return GSS_S_BAD_MIC;
156 /* verify sequence number */
158 des_ctx = EVP_CIPHER_CTX_new();
159 if (des_ctx == NULL) {
160 memset (deskey, 0, sizeof(deskey));
161 memset (&schedule, 0, sizeof(schedule));
162 *minor_status = ENOMEM;
163 return GSS_S_FAILURE;
166 HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex);
170 EVP_CipherInit_ex(des_ctx, EVP_des_cbc(), NULL, key->keyvalue.data, hash, 0);
171 EVP_Cipher(des_ctx, p, p, 8);
172 EVP_CIPHER_CTX_free(des_ctx);
174 memset (deskey, 0, sizeof(deskey));
175 memset (&schedule, 0, sizeof(schedule));
178 _gsskrb5_decode_om_uint32(seq, &seq_number);
180 if (context_handle->more_flags & LOCAL)
181 cmp = ct_memcmp(&seq[4], "\xff\xff\xff\xff", 4);
183 cmp = ct_memcmp(&seq[4], "\x00\x00\x00\x00", 4);
186 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
187 return GSS_S_BAD_MIC;
190 ret = _gssapi_msg_order_check(context_handle->order, seq_number);
192 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
196 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
200 output_message_buffer->length = input_message_buffer->length
201 - len - padlength - 8;
202 output_message_buffer->value = malloc(output_message_buffer->length);
203 if(output_message_buffer->length != 0 && output_message_buffer->value == NULL)
204 return GSS_S_FAILURE;
205 if (output_message_buffer->value != NULL)
206 memcpy (output_message_buffer->value,
208 output_message_buffer->length);
209 return GSS_S_COMPLETE;
215 (OM_uint32 * minor_status,
216 const gsskrb5_ctx context_handle,
217 krb5_context context,
218 const gss_buffer_t input_message_buffer,
219 gss_buffer_t output_message_buffer,
221 gss_qop_t * qop_state,
239 if (IS_DCE_STYLE(context_handle)) {
240 token_len = 34 + 8 + 15; /* 57 */
241 if (input_message_buffer->length < token_len)
242 return GSS_S_BAD_MECH;
244 token_len = input_message_buffer->length;
247 p = input_message_buffer->value;
248 ret = _gsskrb5_verify_header (&p,
255 len = (p - (u_char *)input_message_buffer->value)
257 if (input_message_buffer->length < len)
258 return GSS_S_BAD_MECH;
260 if (ct_memcmp (p, "\x04\x00", 2) != 0) /* HMAC SHA1 DES3_KD */
261 return GSS_S_BAD_SIG;
263 if (ct_memcmp (p, "\x02\x00", 2) == 0) {
265 } else if (ct_memcmp (p, "\xff\xff", 2) == 0) {
268 return GSS_S_BAD_MIC;
270 if(conf_state != NULL)
271 *conf_state = cstate;
272 if (ct_memcmp (p, "\xff\xff", 2) != 0)
273 return GSS_S_DEFECTIVE_TOKEN;
277 len = p - (u_char *)input_message_buffer->value;
283 ret = krb5_crypto_init(context, key,
284 ETYPE_DES3_CBC_NONE, &crypto);
287 return GSS_S_FAILURE;
289 ret = krb5_decrypt(context, crypto, KRB5_KU_USAGE_SEAL,
290 p, input_message_buffer->length - len, &tmp);
291 krb5_crypto_destroy(context, crypto);
294 return GSS_S_FAILURE;
296 assert (tmp.length == input_message_buffer->length - len);
298 memcpy (p, tmp.data, tmp.length);
299 krb5_data_free(&tmp);
302 if (IS_DCE_STYLE(context_handle)) {
306 ret = _gssapi_verify_pad(input_message_buffer,
307 input_message_buffer->length - len - 8,
313 /* verify sequence number */
315 HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex);
319 ret = krb5_crypto_init(context, key,
320 ETYPE_DES3_CBC_NONE, &crypto);
323 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
324 return GSS_S_FAILURE;
329 memcpy(&ivec, p + 8, 8);
330 ret = krb5_decrypt_ivec (context,
336 krb5_crypto_destroy (context, crypto);
339 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
340 return GSS_S_FAILURE;
342 if (seq_data.length != 8) {
343 krb5_data_free (&seq_data);
345 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
346 return GSS_S_BAD_MIC;
350 _gsskrb5_decode_om_uint32(seq, &seq_number);
352 if (context_handle->more_flags & LOCAL)
353 cmp = ct_memcmp(&seq[4], "\xff\xff\xff\xff", 4);
355 cmp = ct_memcmp(&seq[4], "\x00\x00\x00\x00", 4);
357 krb5_data_free (&seq_data);
360 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
361 return GSS_S_BAD_MIC;
364 ret = _gssapi_msg_order_check(context_handle->order, seq_number);
367 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
371 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
373 /* verify checksum */
375 memcpy (cksum, p + 8, 20);
377 memcpy (p + 20, p - 8, 8);
379 csum.cksumtype = CKSUMTYPE_HMAC_SHA1_DES3;
380 csum.checksum.length = 20;
381 csum.checksum.data = cksum;
383 ret = krb5_crypto_init(context, key, 0, &crypto);
386 return GSS_S_FAILURE;
389 ret = krb5_verify_checksum (context, crypto,
392 input_message_buffer->length - len + 8,
394 krb5_crypto_destroy (context, crypto);
397 return GSS_S_FAILURE;
402 output_message_buffer->length = input_message_buffer->length
403 - len - padlength - 8;
404 output_message_buffer->value = malloc(output_message_buffer->length);
405 if(output_message_buffer->length != 0 && output_message_buffer->value == NULL)
406 return GSS_S_FAILURE;
407 if (output_message_buffer->value != NULL)
408 memcpy (output_message_buffer->value,
410 output_message_buffer->length);
411 return GSS_S_COMPLETE;
414 OM_uint32 GSSAPI_CALLCONV _gsskrb5_unwrap
415 (OM_uint32 * minor_status,
416 const gss_ctx_id_t context_handle,
417 const gss_buffer_t input_message_buffer,
418 gss_buffer_t output_message_buffer,
420 gss_qop_t * qop_state
424 krb5_context context;
426 krb5_keytype keytype;
427 gsskrb5_ctx ctx = (gsskrb5_ctx) context_handle;
429 output_message_buffer->value = NULL;
430 output_message_buffer->length = 0;
431 if (qop_state != NULL)
432 *qop_state = GSS_C_QOP_DEFAULT;
434 GSSAPI_KRB5_INIT (&context);
436 if (ctx->more_flags & IS_CFX)
437 return _gssapi_unwrap_cfx (minor_status, ctx, context,
438 input_message_buffer, output_message_buffer,
439 conf_state, qop_state);
441 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
442 ret = _gsskrb5i_get_token_key(ctx, context, &key);
443 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
446 return GSS_S_FAILURE;
448 krb5_enctype_to_keytype (context, key->keytype, &keytype);
454 #ifdef HEIM_WEAK_CRYPTO
455 ret = unwrap_des (minor_status, ctx,
456 input_message_buffer, output_message_buffer,
457 conf_state, qop_state, key);
463 ret = unwrap_des3 (minor_status, ctx, context,
464 input_message_buffer, output_message_buffer,
465 conf_state, qop_state, key);
467 case KEYTYPE_ARCFOUR:
468 case KEYTYPE_ARCFOUR_56:
469 ret = _gssapi_unwrap_arcfour (minor_status, ctx, context,
470 input_message_buffer, output_message_buffer,
471 conf_state, qop_state, key);
477 krb5_free_keyblock (context, key);