2 * Copyright (c) 1997 - 2003 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: verify_mic.c 19031 2006-11-13 18:02:57Z lha $");
40 (OM_uint32 * minor_status,
41 const gsskrb5_ctx context_handle,
43 const gss_buffer_t message_buffer,
44 const gss_buffer_t token_buffer,
45 gss_qop_t * qop_state,
52 u_char hash[16], *seq;
53 DES_key_schedule schedule;
60 p = token_buffer->value;
61 ret = _gsskrb5_verify_header (&p,
68 if (memcmp(p, "\x00\x00", 2) != 0)
71 if (memcmp (p, "\xff\xff\xff\xff", 4) != 0)
78 MD5_Update (&md5, p - 24, 8);
79 MD5_Update (&md5, message_buffer->value,
80 message_buffer->length);
81 MD5_Final (hash, &md5);
83 memset (&zero, 0, sizeof(zero));
84 memcpy (&deskey, key->keyvalue.data, sizeof(deskey));
86 DES_set_key (&deskey, &schedule);
87 DES_cbc_cksum ((void *)hash, (void *)hash, sizeof(hash),
89 if (memcmp (p - 8, hash, 8) != 0) {
90 memset (deskey, 0, sizeof(deskey));
91 memset (&schedule, 0, sizeof(schedule));
95 /* verify sequence number */
97 HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex);
100 DES_set_key (&deskey, &schedule);
101 DES_cbc_encrypt ((void *)p, (void *)p, 8,
102 &schedule, (DES_cblock *)hash, DES_DECRYPT);
104 memset (deskey, 0, sizeof(deskey));
105 memset (&schedule, 0, sizeof(schedule));
108 _gsskrb5_decode_om_uint32(seq, &seq_number);
110 if (context_handle->more_flags & LOCAL)
111 cmp = memcmp(&seq[4], "\xff\xff\xff\xff", 4);
113 cmp = memcmp(&seq[4], "\x00\x00\x00\x00", 4);
116 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
117 return GSS_S_BAD_MIC;
120 ret = _gssapi_msg_order_check(context_handle->order, seq_number);
122 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
126 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
128 return GSS_S_COMPLETE;
133 (OM_uint32 * minor_status,
134 const gsskrb5_ctx context_handle,
135 krb5_context context,
136 const gss_buffer_t message_buffer,
137 const gss_buffer_t token_buffer,
138 gss_qop_t * qop_state,
154 p = token_buffer->value;
155 ret = _gsskrb5_verify_header (&p,
156 token_buffer->length,
162 if (memcmp(p, "\x04\x00", 2) != 0) /* SGN_ALG = HMAC SHA1 DES3-KD */
163 return GSS_S_BAD_SIG;
165 if (memcmp (p, "\xff\xff\xff\xff", 4) != 0)
166 return GSS_S_BAD_MIC;
169 ret = krb5_crypto_init(context, key,
170 ETYPE_DES3_CBC_NONE, &crypto);
173 return GSS_S_FAILURE;
176 /* verify sequence number */
182 memcpy(ivec, p + 8, 8);
184 ret = krb5_decrypt_ivec (context,
187 p, 8, &seq_data, ivec);
190 krb5_crypto_destroy (context, crypto);
192 return GSS_S_FAILURE;
197 if (seq_data.length != 8) {
198 krb5_data_free (&seq_data);
200 krb5_crypto_destroy (context, crypto);
201 return GSS_S_BAD_MIC;
206 HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex);
209 _gsskrb5_decode_om_uint32(seq, &seq_number);
211 if (context_handle->more_flags & LOCAL)
212 cmp = memcmp(&seq[4], "\xff\xff\xff\xff", 4);
214 cmp = memcmp(&seq[4], "\x00\x00\x00\x00", 4);
216 krb5_data_free (&seq_data);
218 krb5_crypto_destroy (context, crypto);
220 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
221 return GSS_S_BAD_MIC;
224 ret = _gssapi_msg_order_check(context_handle->order, seq_number);
226 krb5_crypto_destroy (context, crypto);
228 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
232 /* verify checksum */
234 tmp = malloc (message_buffer->length + 8);
236 krb5_crypto_destroy (context, crypto);
237 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
238 *minor_status = ENOMEM;
239 return GSS_S_FAILURE;
242 memcpy (tmp, p - 8, 8);
243 memcpy (tmp + 8, message_buffer->value, message_buffer->length);
245 csum.cksumtype = CKSUMTYPE_HMAC_SHA1_DES3;
246 csum.checksum.length = 20;
247 csum.checksum.data = p + 8;
249 ret = krb5_verify_checksum (context, crypto,
251 tmp, message_buffer->length + 8,
255 krb5_crypto_destroy (context, crypto);
257 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
258 return GSS_S_BAD_MIC;
260 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
262 krb5_crypto_destroy (context, crypto);
263 return GSS_S_COMPLETE;
267 _gsskrb5_verify_mic_internal
268 (OM_uint32 * minor_status,
269 const gsskrb5_ctx context_handle,
270 krb5_context context,
271 const gss_buffer_t message_buffer,
272 const gss_buffer_t token_buffer,
273 gss_qop_t * qop_state,
279 krb5_keytype keytype;
281 HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex);
282 ret = _gsskrb5i_get_token_key(context_handle, context, &key);
283 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
286 return GSS_S_FAILURE;
289 krb5_enctype_to_keytype (context, key->keytype, &keytype);
292 ret = verify_mic_des (minor_status, context_handle, context,
293 message_buffer, token_buffer, qop_state, key,
297 ret = verify_mic_des3 (minor_status, context_handle, context,
298 message_buffer, token_buffer, qop_state, key,
301 case KEYTYPE_ARCFOUR :
302 case KEYTYPE_ARCFOUR_56 :
303 ret = _gssapi_verify_mic_arcfour (minor_status, context_handle,
305 message_buffer, token_buffer,
306 qop_state, key, type);
309 ret = _gssapi_verify_mic_cfx (minor_status, context_handle,
311 message_buffer, token_buffer, qop_state,
315 krb5_free_keyblock (context, key);
322 (OM_uint32 * minor_status,
323 const gss_ctx_id_t context_handle,
324 const gss_buffer_t message_buffer,
325 const gss_buffer_t token_buffer,
326 gss_qop_t * qop_state
329 krb5_context context;
332 GSSAPI_KRB5_INIT (&context);
334 if (qop_state != NULL)
335 *qop_state = GSS_C_QOP_DEFAULT;
337 ret = _gsskrb5_verify_mic_internal(minor_status,
338 (gsskrb5_ctx)context_handle,
340 message_buffer, token_buffer,
341 qop_state, "\x01\x01");