2 * Copyright (c) 2003 - 2006 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"
37 * Implements draft-brezak-win2k-krb-rc4-hmac-04.txt
39 * The arcfour message have the following formats:
59 * WRAP in DCE-style have a fixed size header, the oid and length over
60 * the WRAP header is a total of
61 * GSS_ARCFOUR_WRAP_TOKEN_DCE_DER_HEADER_SIZE +
62 * GSS_ARCFOUR_WRAP_TOKEN_SIZE byte (ie total of 45 bytes overhead,
63 * remember the 2 bytes from APPL [0] SEQ).
66 #define GSS_ARCFOUR_WRAP_TOKEN_SIZE 32
67 #define GSS_ARCFOUR_WRAP_TOKEN_DCE_DER_HEADER_SIZE 13
70 static krb5_error_code
71 arcfour_mic_key(krb5_context context, krb5_keyblock *key,
72 void *cksum_data, size_t cksum_size,
73 void *key6_data, size_t key6_size)
86 cksum_k5.checksum.data = k5_data;
87 cksum_k5.checksum.length = sizeof(k5_data);
89 if (key->keytype == ENCTYPE_ARCFOUR_HMAC_MD5_56) {
90 char L40[14] = "fortybits";
92 memcpy(L40 + 10, T, sizeof(T));
93 ret = krb5_hmac(context, CKSUMTYPE_RSA_MD5,
94 L40, 14, 0, key, &cksum_k5);
95 memset(&k5_data[7], 0xAB, 9);
97 ret = krb5_hmac(context, CKSUMTYPE_RSA_MD5,
98 T, 4, 0, key, &cksum_k5);
103 key5.keytype = ENCTYPE_ARCFOUR_HMAC_MD5;
104 key5.keyvalue = cksum_k5.checksum;
106 cksum_k6.checksum.data = key6_data;
107 cksum_k6.checksum.length = key6_size;
109 return krb5_hmac(context, CKSUMTYPE_RSA_MD5,
110 cksum_data, cksum_size, 0, &key5, &cksum_k6);
114 static krb5_error_code
115 arcfour_mic_cksum(krb5_context context,
116 krb5_keyblock *key, unsigned usage,
117 u_char *sgn_cksum, size_t sgn_cksum_sz,
118 const u_char *v1, size_t l1,
119 const void *v2, size_t l2,
120 const void *v3, size_t l3)
128 assert(sgn_cksum_sz == 8);
137 memcpy(ptr + l1, v2, l2);
138 memcpy(ptr + l1 + l2, v3, l3);
140 ret = krb5_crypto_init(context, key, 0, &crypto);
146 ret = krb5_create_checksum(context,
154 memcpy(sgn_cksum, CKSUM.checksum.data, sgn_cksum_sz);
155 free_Checksum(&CKSUM);
157 krb5_crypto_destroy(context, crypto);
164 _gssapi_get_mic_arcfour(OM_uint32 * minor_status,
165 const gsskrb5_ctx context_handle,
166 krb5_context context,
168 const gss_buffer_t message_buffer,
169 gss_buffer_t message_token,
174 size_t len, total_len;
175 u_char k6_data[16], *p0, *p;
176 EVP_CIPHER_CTX rc4_key;
178 _gsskrb5_encap_length (22, &len, &total_len, GSS_KRB5_MECHANISM);
180 message_token->length = total_len;
181 message_token->value = malloc (total_len);
182 if (message_token->value == NULL) {
183 *minor_status = ENOMEM;
184 return GSS_S_FAILURE;
187 p0 = _gssapi_make_mech_header(message_token->value,
192 *p++ = 0x01; /* TOK_ID */
194 *p++ = 0x11; /* SGN_ALG */
196 *p++ = 0xff; /* Filler */
203 ret = arcfour_mic_cksum(context,
204 key, KRB5_KU_USAGE_SIGN,
205 p0 + 16, 8, /* SGN_CKSUM */
206 p0, 8, /* TOK_ID, SGN_ALG, Filer */
207 message_buffer->value, message_buffer->length,
210 _gsskrb5_release_buffer(minor_status, message_token);
212 return GSS_S_FAILURE;
215 ret = arcfour_mic_key(context, key,
216 p0 + 16, 8, /* SGN_CKSUM */
217 k6_data, sizeof(k6_data));
219 _gsskrb5_release_buffer(minor_status, message_token);
221 return GSS_S_FAILURE;
224 HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex);
225 krb5_auth_con_getlocalseqnumber (context,
226 context_handle->auth_context,
228 p = p0 + 8; /* SND_SEQ */
229 _gsskrb5_encode_be_om_uint32(seq_number, p);
231 krb5_auth_con_setlocalseqnumber (context,
232 context_handle->auth_context,
234 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
236 memset (p + 4, (context_handle->more_flags & LOCAL) ? 0 : 0xff, 4);
238 EVP_CIPHER_CTX_init(&rc4_key);
239 EVP_CipherInit_ex(&rc4_key, EVP_rc4(), NULL, k6_data, NULL, 1);
240 EVP_Cipher(&rc4_key, p, p, 8);
241 EVP_CIPHER_CTX_cleanup(&rc4_key);
243 memset(k6_data, 0, sizeof(k6_data));
246 return GSS_S_COMPLETE;
251 _gssapi_verify_mic_arcfour(OM_uint32 * minor_status,
252 const gsskrb5_ctx context_handle,
253 krb5_context context,
254 const gss_buffer_t message_buffer,
255 const gss_buffer_t token_buffer,
256 gss_qop_t * qop_state,
263 u_char SND_SEQ[8], cksum_data[8], *p;
270 p = token_buffer->value;
271 omret = _gsskrb5_verify_header (&p,
272 token_buffer->length,
278 if (memcmp(p, "\x11\x00", 2) != 0) /* SGN_ALG = HMAC MD5 ARCFOUR */
279 return GSS_S_BAD_SIG;
281 if (memcmp (p, "\xff\xff\xff\xff", 4) != 0)
282 return GSS_S_BAD_MIC;
285 ret = arcfour_mic_cksum(context,
286 key, KRB5_KU_USAGE_SIGN,
287 cksum_data, sizeof(cksum_data),
289 message_buffer->value, message_buffer->length,
293 return GSS_S_FAILURE;
296 ret = arcfour_mic_key(context, key,
297 cksum_data, sizeof(cksum_data),
298 k6_data, sizeof(k6_data));
301 return GSS_S_FAILURE;
304 cmp = ct_memcmp(cksum_data, p + 8, 8);
307 return GSS_S_BAD_MIC;
311 EVP_CIPHER_CTX rc4_key;
313 EVP_CIPHER_CTX_init(&rc4_key);
314 EVP_CipherInit_ex(&rc4_key, EVP_rc4(), NULL, (void *)k6_data, NULL, 0);
315 EVP_Cipher(&rc4_key, SND_SEQ, p, 8);
316 EVP_CIPHER_CTX_cleanup(&rc4_key);
318 memset(k6_data, 0, sizeof(k6_data));
321 _gsskrb5_decode_be_om_uint32(SND_SEQ, &seq_number);
323 if (context_handle->more_flags & LOCAL)
324 cmp = memcmp(&SND_SEQ[4], "\xff\xff\xff\xff", 4);
326 cmp = memcmp(&SND_SEQ[4], "\x00\x00\x00\x00", 4);
328 memset(SND_SEQ, 0, sizeof(SND_SEQ));
331 return GSS_S_BAD_MIC;
334 HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex);
335 omret = _gssapi_msg_order_check(context_handle->order, seq_number);
336 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
341 return GSS_S_COMPLETE;
345 _gssapi_wrap_arcfour(OM_uint32 * minor_status,
346 const gsskrb5_ctx context_handle,
347 krb5_context context,
350 const gss_buffer_t input_message_buffer,
352 gss_buffer_t output_message_buffer,
355 u_char Klocaldata[16], k6_data[16], *p, *p0;
356 size_t len, total_len, datalen;
357 krb5_keyblock Klocal;
364 datalen = input_message_buffer->length;
366 if (IS_DCE_STYLE(context_handle)) {
367 len = GSS_ARCFOUR_WRAP_TOKEN_SIZE;
368 _gssapi_encap_length(len, &len, &total_len, GSS_KRB5_MECHANISM);
369 total_len += datalen;
371 datalen += 1; /* padding */
372 len = datalen + GSS_ARCFOUR_WRAP_TOKEN_SIZE;
373 _gssapi_encap_length(len, &len, &total_len, GSS_KRB5_MECHANISM);
376 output_message_buffer->length = total_len;
377 output_message_buffer->value = malloc (total_len);
378 if (output_message_buffer->value == NULL) {
379 *minor_status = ENOMEM;
380 return GSS_S_FAILURE;
383 p0 = _gssapi_make_mech_header(output_message_buffer->value,
388 *p++ = 0x02; /* TOK_ID */
390 *p++ = 0x11; /* SGN_ALG */
393 *p++ = 0x10; /* SEAL_ALG */
396 *p++ = 0xff; /* SEAL_ALG */
399 *p++ = 0xff; /* Filler */
404 HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex);
405 krb5_auth_con_getlocalseqnumber (context,
406 context_handle->auth_context,
409 _gsskrb5_encode_be_om_uint32(seq_number, p0 + 8);
411 krb5_auth_con_setlocalseqnumber (context,
412 context_handle->auth_context,
414 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
417 (context_handle->more_flags & LOCAL) ? 0 : 0xff,
420 krb5_generate_random_block(p0 + 24, 8); /* fill in Confounder */
422 /* p points to data */
423 p = p0 + GSS_ARCFOUR_WRAP_TOKEN_SIZE;
424 memcpy(p, input_message_buffer->value, input_message_buffer->length);
426 if (!IS_DCE_STYLE(context_handle))
427 p[input_message_buffer->length] = 1; /* padding */
429 ret = arcfour_mic_cksum(context,
430 key, KRB5_KU_USAGE_SEAL,
431 p0 + 16, 8, /* SGN_CKSUM */
432 p0, 8, /* TOK_ID, SGN_ALG, SEAL_ALG, Filler */
433 p0 + 24, 8, /* Confounder */
434 p0 + GSS_ARCFOUR_WRAP_TOKEN_SIZE,
438 _gsskrb5_release_buffer(minor_status, output_message_buffer);
439 return GSS_S_FAILURE;
445 Klocal.keytype = key->keytype;
446 Klocal.keyvalue.data = Klocaldata;
447 Klocal.keyvalue.length = sizeof(Klocaldata);
449 for (i = 0; i < 16; i++)
450 Klocaldata[i] = ((u_char *)key->keyvalue.data)[i] ^ 0xF0;
452 ret = arcfour_mic_key(context, &Klocal,
453 p0 + 8, 4, /* SND_SEQ */
454 k6_data, sizeof(k6_data));
455 memset(Klocaldata, 0, sizeof(Klocaldata));
457 _gsskrb5_release_buffer(minor_status, output_message_buffer);
459 return GSS_S_FAILURE;
464 EVP_CIPHER_CTX rc4_key;
466 EVP_CIPHER_CTX_init(&rc4_key);
467 EVP_CipherInit_ex(&rc4_key, EVP_rc4(), NULL, k6_data, NULL, 1);
468 EVP_Cipher(&rc4_key, p0 + 24, p0 + 24, 8 + datalen);
469 EVP_CIPHER_CTX_cleanup(&rc4_key);
471 memset(k6_data, 0, sizeof(k6_data));
473 ret = arcfour_mic_key(context, key,
474 p0 + 16, 8, /* SGN_CKSUM */
475 k6_data, sizeof(k6_data));
477 _gsskrb5_release_buffer(minor_status, output_message_buffer);
479 return GSS_S_FAILURE;
483 EVP_CIPHER_CTX rc4_key;
485 EVP_CIPHER_CTX_init(&rc4_key);
486 EVP_CipherInit_ex(&rc4_key, EVP_rc4(), NULL, k6_data, NULL, 1);
487 EVP_Cipher(&rc4_key, p0 + 8, p0 + 8 /* SND_SEQ */, 8);
488 EVP_CIPHER_CTX_cleanup(&rc4_key);
489 memset(k6_data, 0, sizeof(k6_data));
493 *conf_state = conf_req_flag;
496 return GSS_S_COMPLETE;
499 OM_uint32 _gssapi_unwrap_arcfour(OM_uint32 *minor_status,
500 const gsskrb5_ctx context_handle,
501 krb5_context context,
502 const gss_buffer_t input_message_buffer,
503 gss_buffer_t output_message_buffer,
505 gss_qop_t *qop_state,
508 u_char Klocaldata[16];
509 krb5_keyblock Klocal;
514 u_char k6_data[16], SND_SEQ[8], Confounder[8];
515 u_char cksum_data[8];
519 size_t padlen = 0, len;
526 p0 = input_message_buffer->value;
528 if (IS_DCE_STYLE(context_handle)) {
529 len = GSS_ARCFOUR_WRAP_TOKEN_SIZE +
530 GSS_ARCFOUR_WRAP_TOKEN_DCE_DER_HEADER_SIZE;
531 if (input_message_buffer->length < len)
532 return GSS_S_BAD_MECH;
534 len = input_message_buffer->length;
537 omret = _gssapi_verify_mech_header(&p0,
543 /* length of mech header */
544 len = (p0 - (u_char *)input_message_buffer->value) +
545 GSS_ARCFOUR_WRAP_TOKEN_SIZE;
547 if (len > input_message_buffer->length)
548 return GSS_S_BAD_MECH;
551 datalen = input_message_buffer->length - len;
555 if (memcmp(p, "\x02\x01", 2) != 0)
556 return GSS_S_BAD_SIG;
558 if (memcmp(p, "\x11\x00", 2) != 0) /* SGN_ALG = HMAC MD5 ARCFOUR */
559 return GSS_S_BAD_SIG;
562 if (memcmp (p, "\x10\x00", 2) == 0)
564 else if (memcmp (p, "\xff\xff", 2) == 0)
567 return GSS_S_BAD_SIG;
570 if (memcmp (p, "\xff\xff", 2) != 0)
571 return GSS_S_BAD_MIC;
574 ret = arcfour_mic_key(context, key,
575 p0 + 16, 8, /* SGN_CKSUM */
576 k6_data, sizeof(k6_data));
579 return GSS_S_FAILURE;
583 EVP_CIPHER_CTX rc4_key;
585 EVP_CIPHER_CTX_init(&rc4_key);
586 EVP_CipherInit_ex(&rc4_key, EVP_rc4(), NULL, k6_data, NULL, 1);
587 EVP_Cipher(&rc4_key, SND_SEQ, p0 + 8, 8);
588 EVP_CIPHER_CTX_cleanup(&rc4_key);
589 memset(k6_data, 0, sizeof(k6_data));
592 _gsskrb5_decode_be_om_uint32(SND_SEQ, &seq_number);
594 if (context_handle->more_flags & LOCAL)
595 cmp = memcmp(&SND_SEQ[4], "\xff\xff\xff\xff", 4);
597 cmp = memcmp(&SND_SEQ[4], "\x00\x00\x00\x00", 4);
601 return GSS_S_BAD_MIC;
607 Klocal.keytype = key->keytype;
608 Klocal.keyvalue.data = Klocaldata;
609 Klocal.keyvalue.length = sizeof(Klocaldata);
611 for (i = 0; i < 16; i++)
612 Klocaldata[i] = ((u_char *)key->keyvalue.data)[i] ^ 0xF0;
614 ret = arcfour_mic_key(context, &Klocal,
616 k6_data, sizeof(k6_data));
617 memset(Klocaldata, 0, sizeof(Klocaldata));
620 return GSS_S_FAILURE;
623 output_message_buffer->value = malloc(datalen);
624 if (output_message_buffer->value == NULL) {
625 *minor_status = ENOMEM;
626 return GSS_S_FAILURE;
628 output_message_buffer->length = datalen;
631 EVP_CIPHER_CTX rc4_key;
633 EVP_CIPHER_CTX_init(&rc4_key);
634 EVP_CipherInit_ex(&rc4_key, EVP_rc4(), NULL, k6_data, NULL, 1);
635 EVP_Cipher(&rc4_key, Confounder, p0 + 24, 8);
636 EVP_Cipher(&rc4_key, output_message_buffer->value, p0 + GSS_ARCFOUR_WRAP_TOKEN_SIZE, datalen);
637 EVP_CIPHER_CTX_cleanup(&rc4_key);
639 memcpy(Confounder, p0 + 24, 8); /* Confounder */
640 memcpy(output_message_buffer->value,
641 p0 + GSS_ARCFOUR_WRAP_TOKEN_SIZE,
644 memset(k6_data, 0, sizeof(k6_data));
646 if (!IS_DCE_STYLE(context_handle)) {
647 ret = _gssapi_verify_pad(output_message_buffer, datalen, &padlen);
649 _gsskrb5_release_buffer(minor_status, output_message_buffer);
653 output_message_buffer->length -= padlen;
656 ret = arcfour_mic_cksum(context,
657 key, KRB5_KU_USAGE_SEAL,
658 cksum_data, sizeof(cksum_data),
660 Confounder, sizeof(Confounder),
661 output_message_buffer->value,
662 output_message_buffer->length + padlen);
664 _gsskrb5_release_buffer(minor_status, output_message_buffer);
666 return GSS_S_FAILURE;
669 cmp = ct_memcmp(cksum_data, p0 + 16, 8); /* SGN_CKSUM */
671 _gsskrb5_release_buffer(minor_status, output_message_buffer);
673 return GSS_S_BAD_MIC;
676 HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex);
677 omret = _gssapi_msg_order_check(context_handle->order, seq_number);
678 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
683 *conf_state = conf_flag;
686 return GSS_S_COMPLETE;
690 max_wrap_length_arcfour(const gsskrb5_ctx ctx,
693 OM_uint32 *max_input_size)
696 * if GSS_C_DCE_STYLE is in use:
697 * - we only need to encapsulate the WRAP token
698 * However, since this is a fixed since, we just
700 if (IS_DCE_STYLE(ctx)) {
701 size_t len, total_len;
703 len = GSS_ARCFOUR_WRAP_TOKEN_SIZE;
704 _gssapi_encap_length(len, &len, &total_len, GSS_KRB5_MECHANISM);
706 if (input_length < len)
709 *max_input_size = input_length - len;
712 size_t extrasize = GSS_ARCFOUR_WRAP_TOKEN_SIZE;
713 size_t blocksize = 8;
714 size_t len, total_len;
716 len = 8 + input_length + blocksize + extrasize;
718 _gsskrb5_encap_length(len, &len, &total_len, GSS_KRB5_MECHANISM);
720 total_len -= input_length; /* token length */
721 if (total_len < input_length) {
722 *max_input_size = (input_length - total_len);
723 (*max_input_size) &= (~(OM_uint32)(blocksize - 1));
729 return GSS_S_COMPLETE;
733 _gssapi_wrap_size_arcfour(OM_uint32 *minor_status,
734 const gsskrb5_ctx ctx,
735 krb5_context context,
738 OM_uint32 req_output_size,
739 OM_uint32 *max_input_size,
745 ret = krb5_crypto_init(context, key, 0, &crypto);
748 return GSS_S_FAILURE;
751 ret = max_wrap_length_arcfour(ctx, crypto,
752 req_output_size, max_input_size);
755 krb5_crypto_destroy(context, crypto);
756 return GSS_S_FAILURE;
759 krb5_crypto_destroy(context, crypto);
761 return GSS_S_COMPLETE;