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: get_mic.c,v 1.34 2006/10/18 15:59:23 lha Exp $");
40 (OM_uint32 * minor_status,
41 const gsskrb5_ctx ctx,
43 const gss_buffer_t message_buffer,
44 gss_buffer_t message_token,
51 DES_key_schedule schedule;
55 size_t len, total_len;
57 _gsskrb5_encap_length (22, &len, &total_len, GSS_KRB5_MECHANISM);
59 message_token->length = total_len;
60 message_token->value = malloc (total_len);
61 if (message_token->value == NULL) {
62 message_token->length = 0;
63 *minor_status = ENOMEM;
67 p = _gsskrb5_make_header(message_token->value,
69 "\x01\x01", /* TOK_ID */
72 memcpy (p, "\x00\x00", 2); /* SGN_ALG = DES MAC MD5 */
75 memcpy (p, "\xff\xff\xff\xff", 4); /* Filler */
78 /* Fill in later (SND-SEQ) */
84 MD5_Update (&md5, p - 24, 8);
85 MD5_Update (&md5, message_buffer->value, message_buffer->length);
86 MD5_Final (hash, &md5);
88 memset (&zero, 0, sizeof(zero));
89 memcpy (&deskey, key->keyvalue.data, sizeof(deskey));
90 DES_set_key (&deskey, &schedule);
91 DES_cbc_cksum ((void *)hash, (void *)hash, sizeof(hash),
93 memcpy (p - 8, hash, 8); /* SGN_CKSUM */
95 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
97 krb5_auth_con_getlocalseqnumber (_gsskrb5_context,
101 p -= 16; /* SND_SEQ */
102 p[0] = (seq_number >> 0) & 0xFF;
103 p[1] = (seq_number >> 8) & 0xFF;
104 p[2] = (seq_number >> 16) & 0xFF;
105 p[3] = (seq_number >> 24) & 0xFF;
107 (ctx->more_flags & LOCAL) ? 0 : 0xFF,
110 DES_set_key (&deskey, &schedule);
111 DES_cbc_encrypt ((void *)p, (void *)p, 8,
112 &schedule, (DES_cblock *)(p + 8), DES_ENCRYPT);
114 krb5_auth_con_setlocalseqnumber (_gsskrb5_context,
117 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
119 memset (deskey, 0, sizeof(deskey));
120 memset (&schedule, 0, sizeof(schedule));
123 return GSS_S_COMPLETE;
128 (OM_uint32 * minor_status,
129 const gsskrb5_ctx ctx,
131 const gss_buffer_t message_buffer,
132 gss_buffer_t message_token,
141 size_t len, total_len;
144 krb5_error_code kret;
149 _gsskrb5_encap_length (36, &len, &total_len, GSS_KRB5_MECHANISM);
151 message_token->length = total_len;
152 message_token->value = malloc (total_len);
153 if (message_token->value == NULL) {
154 message_token->length = 0;
155 *minor_status = ENOMEM;
156 return GSS_S_FAILURE;
159 p = _gsskrb5_make_header(message_token->value,
161 "\x01\x01", /* TOK-ID */
164 memcpy (p, "\x04\x00", 2); /* SGN_ALG = HMAC SHA1 DES3-KD */
167 memcpy (p, "\xff\xff\xff\xff", 4); /* filler */
170 /* this should be done in parts */
172 tmp = malloc (message_buffer->length + 8);
174 free (message_token->value);
175 message_token->value = NULL;
176 message_token->length = 0;
177 *minor_status = ENOMEM;
178 return GSS_S_FAILURE;
180 memcpy (tmp, p - 8, 8);
181 memcpy (tmp + 8, message_buffer->value, message_buffer->length);
183 kret = krb5_crypto_init(_gsskrb5_context, key, 0, &crypto);
185 free (message_token->value);
186 message_token->value = NULL;
187 message_token->length = 0;
189 _gsskrb5_set_error_string ();
190 *minor_status = kret;
191 return GSS_S_FAILURE;
194 kret = krb5_create_checksum (_gsskrb5_context,
199 message_buffer->length + 8,
202 krb5_crypto_destroy (_gsskrb5_context, crypto);
204 free (message_token->value);
205 message_token->value = NULL;
206 message_token->length = 0;
207 _gsskrb5_set_error_string ();
208 *minor_status = kret;
209 return GSS_S_FAILURE;
212 memcpy (p + 8, cksum.checksum.data, cksum.checksum.length);
214 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
215 /* sequence number */
216 krb5_auth_con_getlocalseqnumber (_gsskrb5_context,
220 seq[0] = (seq_number >> 0) & 0xFF;
221 seq[1] = (seq_number >> 8) & 0xFF;
222 seq[2] = (seq_number >> 16) & 0xFF;
223 seq[3] = (seq_number >> 24) & 0xFF;
225 (ctx->more_flags & LOCAL) ? 0 : 0xFF,
228 kret = krb5_crypto_init(_gsskrb5_context, key,
229 ETYPE_DES3_CBC_NONE, &crypto);
231 free (message_token->value);
232 message_token->value = NULL;
233 message_token->length = 0;
234 _gsskrb5_set_error_string ();
235 *minor_status = kret;
236 return GSS_S_FAILURE;
239 if (ctx->more_flags & COMPAT_OLD_DES3)
242 memcpy(ivec, p + 8, 8);
244 kret = krb5_encrypt_ivec (_gsskrb5_context,
247 seq, 8, &encdata, ivec);
248 krb5_crypto_destroy (_gsskrb5_context, crypto);
250 free (message_token->value);
251 message_token->value = NULL;
252 message_token->length = 0;
253 _gsskrb5_set_error_string ();
254 *minor_status = kret;
255 return GSS_S_FAILURE;
258 assert (encdata.length == 8);
260 memcpy (p, encdata.data, encdata.length);
261 krb5_data_free (&encdata);
263 krb5_auth_con_setlocalseqnumber (_gsskrb5_context,
266 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
268 free_Checksum (&cksum);
270 return GSS_S_COMPLETE;
273 OM_uint32 _gsskrb5_get_mic
274 (OM_uint32 * minor_status,
275 const gss_ctx_id_t context_handle,
277 const gss_buffer_t message_buffer,
278 gss_buffer_t message_token
281 const gsskrb5_ctx ctx = (const gsskrb5_ctx) context_handle;
284 krb5_keytype keytype;
286 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
287 ret = _gsskrb5i_get_token_key(ctx, &key);
288 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
290 _gsskrb5_set_error_string ();
292 return GSS_S_FAILURE;
294 krb5_enctype_to_keytype (_gsskrb5_context, key->keytype, &keytype);
298 ret = mic_des (minor_status, ctx, qop_req,
299 message_buffer, message_token, key);
302 ret = mic_des3 (minor_status, ctx, qop_req,
303 message_buffer, message_token, key);
305 case KEYTYPE_ARCFOUR:
306 case KEYTYPE_ARCFOUR_56:
307 ret = _gssapi_get_mic_arcfour (minor_status, ctx, qop_req,
308 message_buffer, message_token, key);
311 ret = _gssapi_mic_cfx (minor_status, ctx, qop_req,
312 message_buffer, message_token, key);
315 krb5_free_keyblock (_gsskrb5_context, key);