Implement gss_wrap_iov, gss_unwrap_iov for CFX type encryption types.
[heimdal.git] / lib / gssapi / krb5 / unwrap.c
blob9de6b9593c5f9f71b60888344bcdb31c9f9144ae
1 /*
2 * Copyright (c) 1997 - 2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "gsskrb5_locl.h"
36 static OM_uint32
37 unwrap_des
38 (OM_uint32 * minor_status,
39 const gsskrb5_ctx context_handle,
40 const gss_buffer_t input_message_buffer,
41 gss_buffer_t output_message_buffer,
42 int * conf_state,
43 gss_qop_t * qop_state,
44 krb5_keyblock *key
47 u_char *p, *seq;
48 size_t len;
49 MD5_CTX md5;
50 u_char hash[16];
51 DES_key_schedule schedule;
52 DES_cblock deskey;
53 DES_cblock zero;
54 int i;
55 uint32_t seq_number;
56 size_t padlength;
57 OM_uint32 ret;
58 int cstate;
59 int cmp;
61 p = input_message_buffer->value;
62 ret = _gsskrb5_verify_header (&p,
63 input_message_buffer->length,
64 "\x02\x01",
65 GSS_KRB5_MECHANISM);
66 if (ret)
67 return ret;
69 if (memcmp (p, "\x00\x00", 2) != 0)
70 return GSS_S_BAD_SIG;
71 p += 2;
72 if (memcmp (p, "\x00\x00", 2) == 0) {
73 cstate = 1;
74 } else if (memcmp (p, "\xFF\xFF", 2) == 0) {
75 cstate = 0;
76 } else
77 return GSS_S_BAD_MIC;
78 p += 2;
79 if(conf_state != NULL)
80 *conf_state = cstate;
81 if (memcmp (p, "\xff\xff", 2) != 0)
82 return GSS_S_DEFECTIVE_TOKEN;
83 p += 2;
84 p += 16;
86 len = p - (u_char *)input_message_buffer->value;
88 if(cstate) {
89 /* decrypt data */
90 memcpy (&deskey, key->keyvalue.data, sizeof(deskey));
92 for (i = 0; i < sizeof(deskey); ++i)
93 deskey[i] ^= 0xf0;
94 DES_set_key_unchecked (&deskey, &schedule);
95 memset (&zero, 0, sizeof(zero));
96 DES_cbc_encrypt ((void *)p,
97 (void *)p,
98 input_message_buffer->length - len,
99 &schedule,
100 &zero,
101 DES_DECRYPT);
103 memset (deskey, 0, sizeof(deskey));
104 memset (&schedule, 0, sizeof(schedule));
106 /* check pad */
107 ret = _gssapi_verify_pad(input_message_buffer,
108 input_message_buffer->length - len,
109 &padlength);
110 if (ret)
111 return ret;
113 MD5_Init (&md5);
114 MD5_Update (&md5, p - 24, 8);
115 MD5_Update (&md5, p, input_message_buffer->length - len);
116 MD5_Final (hash, &md5);
118 memset (&zero, 0, sizeof(zero));
119 memcpy (&deskey, key->keyvalue.data, sizeof(deskey));
120 DES_set_key_unchecked (&deskey, &schedule);
121 DES_cbc_cksum ((void *)hash, (void *)hash, sizeof(hash),
122 &schedule, &zero);
123 if (memcmp (p - 8, hash, 8) != 0)
124 return GSS_S_BAD_MIC;
126 /* verify sequence number */
128 HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex);
130 p -= 16;
131 DES_set_key_unchecked (&deskey, &schedule);
132 DES_cbc_encrypt ((void *)p, (void *)p, 8,
133 &schedule, (DES_cblock *)hash, DES_DECRYPT);
135 memset (deskey, 0, sizeof(deskey));
136 memset (&schedule, 0, sizeof(schedule));
138 seq = p;
139 _gsskrb5_decode_om_uint32(seq, &seq_number);
141 if (context_handle->more_flags & LOCAL)
142 cmp = memcmp(&seq[4], "\xff\xff\xff\xff", 4);
143 else
144 cmp = memcmp(&seq[4], "\x00\x00\x00\x00", 4);
146 if (cmp != 0) {
147 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
148 return GSS_S_BAD_MIC;
151 ret = _gssapi_msg_order_check(context_handle->order, seq_number);
152 if (ret) {
153 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
154 return ret;
157 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
159 /* copy out data */
161 output_message_buffer->length = input_message_buffer->length
162 - len - padlength - 8;
163 output_message_buffer->value = malloc(output_message_buffer->length);
164 if(output_message_buffer->length != 0 && output_message_buffer->value == NULL)
165 return GSS_S_FAILURE;
166 memcpy (output_message_buffer->value,
167 p + 24,
168 output_message_buffer->length);
169 return GSS_S_COMPLETE;
172 static OM_uint32
173 unwrap_des3
174 (OM_uint32 * minor_status,
175 const gsskrb5_ctx context_handle,
176 krb5_context context,
177 const gss_buffer_t input_message_buffer,
178 gss_buffer_t output_message_buffer,
179 int * conf_state,
180 gss_qop_t * qop_state,
181 krb5_keyblock *key
184 u_char *p;
185 size_t len;
186 u_char *seq;
187 krb5_data seq_data;
188 u_char cksum[20];
189 uint32_t seq_number;
190 size_t padlength;
191 OM_uint32 ret;
192 int cstate;
193 krb5_crypto crypto;
194 Checksum csum;
195 int cmp;
197 p = input_message_buffer->value;
198 ret = _gsskrb5_verify_header (&p,
199 input_message_buffer->length,
200 "\x02\x01",
201 GSS_KRB5_MECHANISM);
202 if (ret)
203 return ret;
205 if (memcmp (p, "\x04\x00", 2) != 0) /* HMAC SHA1 DES3_KD */
206 return GSS_S_BAD_SIG;
207 p += 2;
208 if (memcmp (p, "\x02\x00", 2) == 0) {
209 cstate = 1;
210 } else if (memcmp (p, "\xff\xff", 2) == 0) {
211 cstate = 0;
212 } else
213 return GSS_S_BAD_MIC;
214 p += 2;
215 if(conf_state != NULL)
216 *conf_state = cstate;
217 if (memcmp (p, "\xff\xff", 2) != 0)
218 return GSS_S_DEFECTIVE_TOKEN;
219 p += 2;
220 p += 28;
222 len = p - (u_char *)input_message_buffer->value;
224 if(cstate) {
225 /* decrypt data */
226 krb5_data tmp;
228 ret = krb5_crypto_init(context, key,
229 ETYPE_DES3_CBC_NONE, &crypto);
230 if (ret) {
231 *minor_status = ret;
232 return GSS_S_FAILURE;
234 ret = krb5_decrypt(context, crypto, KRB5_KU_USAGE_SEAL,
235 p, input_message_buffer->length - len, &tmp);
236 krb5_crypto_destroy(context, crypto);
237 if (ret) {
238 *minor_status = ret;
239 return GSS_S_FAILURE;
241 assert (tmp.length == input_message_buffer->length - len);
243 memcpy (p, tmp.data, tmp.length);
244 krb5_data_free(&tmp);
246 /* check pad */
247 ret = _gssapi_verify_pad(input_message_buffer,
248 input_message_buffer->length - len,
249 &padlength);
250 if (ret)
251 return ret;
253 /* verify sequence number */
255 HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex);
257 p -= 28;
259 ret = krb5_crypto_init(context, key,
260 ETYPE_DES3_CBC_NONE, &crypto);
261 if (ret) {
262 *minor_status = ret;
263 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
264 return GSS_S_FAILURE;
267 DES_cblock ivec;
269 memcpy(&ivec, p + 8, 8);
270 ret = krb5_decrypt_ivec (context,
271 crypto,
272 KRB5_KU_USAGE_SEQ,
273 p, 8, &seq_data,
274 &ivec);
276 krb5_crypto_destroy (context, crypto);
277 if (ret) {
278 *minor_status = ret;
279 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
280 return GSS_S_FAILURE;
282 if (seq_data.length != 8) {
283 krb5_data_free (&seq_data);
284 *minor_status = 0;
285 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
286 return GSS_S_BAD_MIC;
289 seq = seq_data.data;
290 _gsskrb5_decode_om_uint32(seq, &seq_number);
292 if (context_handle->more_flags & LOCAL)
293 cmp = memcmp(&seq[4], "\xff\xff\xff\xff", 4);
294 else
295 cmp = memcmp(&seq[4], "\x00\x00\x00\x00", 4);
297 krb5_data_free (&seq_data);
298 if (cmp != 0) {
299 *minor_status = 0;
300 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
301 return GSS_S_BAD_MIC;
304 ret = _gssapi_msg_order_check(context_handle->order, seq_number);
305 if (ret) {
306 *minor_status = 0;
307 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
308 return ret;
311 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
313 /* verify checksum */
315 memcpy (cksum, p + 8, 20);
317 memcpy (p + 20, p - 8, 8);
319 csum.cksumtype = CKSUMTYPE_HMAC_SHA1_DES3;
320 csum.checksum.length = 20;
321 csum.checksum.data = cksum;
323 ret = krb5_crypto_init(context, key, 0, &crypto);
324 if (ret) {
325 *minor_status = ret;
326 return GSS_S_FAILURE;
329 ret = krb5_verify_checksum (context, crypto,
330 KRB5_KU_USAGE_SIGN,
331 p + 20,
332 input_message_buffer->length - len + 8,
333 &csum);
334 krb5_crypto_destroy (context, crypto);
335 if (ret) {
336 *minor_status = ret;
337 return GSS_S_FAILURE;
340 /* copy out data */
342 output_message_buffer->length = input_message_buffer->length
343 - len - padlength - 8;
344 output_message_buffer->value = malloc(output_message_buffer->length);
345 if(output_message_buffer->length != 0 && output_message_buffer->value == NULL)
346 return GSS_S_FAILURE;
347 memcpy (output_message_buffer->value,
348 p + 36,
349 output_message_buffer->length);
350 return GSS_S_COMPLETE;
353 OM_uint32 _gsskrb5_unwrap
354 (OM_uint32 * minor_status,
355 const gss_ctx_id_t context_handle,
356 const gss_buffer_t input_message_buffer,
357 gss_buffer_t output_message_buffer,
358 int * conf_state,
359 gss_qop_t * qop_state
362 krb5_keyblock *key;
363 krb5_context context;
364 OM_uint32 ret;
365 krb5_keytype keytype;
366 gsskrb5_ctx ctx = (gsskrb5_ctx) context_handle;
368 output_message_buffer->value = NULL;
369 output_message_buffer->length = 0;
370 if (qop_state != NULL)
371 *qop_state = GSS_C_QOP_DEFAULT;
373 GSSAPI_KRB5_INIT (&context);
375 if (ctx->more_flags & IS_CFX)
376 return _gssapi_unwrap_cfx (minor_status, ctx, context,
377 input_message_buffer, output_message_buffer,
378 conf_state, qop_state);
380 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
381 ret = _gsskrb5i_get_token_key(ctx, context, &key);
382 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
383 if (ret) {
384 *minor_status = ret;
385 return GSS_S_FAILURE;
387 krb5_enctype_to_keytype (context, key->keytype, &keytype);
389 *minor_status = 0;
391 switch (keytype) {
392 case KEYTYPE_DES :
393 ret = unwrap_des (minor_status, ctx,
394 input_message_buffer, output_message_buffer,
395 conf_state, qop_state, key);
396 break;
397 case KEYTYPE_DES3 :
398 ret = unwrap_des3 (minor_status, ctx, context,
399 input_message_buffer, output_message_buffer,
400 conf_state, qop_state, key);
401 break;
402 case KEYTYPE_ARCFOUR:
403 case KEYTYPE_ARCFOUR_56:
404 ret = _gssapi_unwrap_arcfour (minor_status, ctx, context,
405 input_message_buffer, output_message_buffer,
406 conf_state, qop_state, key);
407 break;
408 default :
409 abort();
410 break;
412 krb5_free_keyblock (context, key);
413 return ret;