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 rc4_key = EVP_CIPHER_CTX_new();
239 if (rc4_key == NULL) {
240 _gsskrb5_release_buffer(minor_status, message_token);
241 *minor_status = ENOMEM;
242 return GSS_S_FAILURE;
245 EVP_CipherInit_ex(rc4_key, EVP_rc4(), NULL, k6_data, NULL, 1);
246 EVP_Cipher(rc4_key, p, p, 8);
247 EVP_CIPHER_CTX_free(rc4_key);
249 memset(k6_data, 0, sizeof(k6_data));
252 return GSS_S_COMPLETE;
257 _gssapi_verify_mic_arcfour(OM_uint32 * minor_status,
258 const gsskrb5_ctx context_handle,
259 krb5_context context,
260 const gss_buffer_t message_buffer,
261 const gss_buffer_t token_buffer,
262 gss_qop_t * qop_state,
269 u_char SND_SEQ[8], cksum_data[8], *p;
276 p = token_buffer->value;
277 omret = _gsskrb5_verify_header (&p,
278 token_buffer->length,
284 if (memcmp(p, "\x11\x00", 2) != 0) /* SGN_ALG = HMAC MD5 ARCFOUR */
285 return GSS_S_BAD_SIG;
287 if (memcmp (p, "\xff\xff\xff\xff", 4) != 0)
288 return GSS_S_BAD_MIC;
291 ret = arcfour_mic_cksum(context,
292 key, KRB5_KU_USAGE_SIGN,
293 cksum_data, sizeof(cksum_data),
295 message_buffer->value, message_buffer->length,
299 return GSS_S_FAILURE;
302 ret = arcfour_mic_key(context, key,
303 cksum_data, sizeof(cksum_data),
304 k6_data, sizeof(k6_data));
307 return GSS_S_FAILURE;
310 cmp = (ct_memcmp(cksum_data, p + 8, 8) == 0);
313 return GSS_S_BAD_MIC;
317 EVP_CIPHER_CTX *rc4_key;
319 rc4_key = EVP_CIPHER_CTX_new();
320 if (rc4_key == NULL) {
321 *minor_status = ENOMEM;
322 return GSS_S_FAILURE;
324 EVP_CipherInit_ex(rc4_key, EVP_rc4(), NULL, (void *)k6_data, NULL, 0);
325 EVP_Cipher(rc4_key, SND_SEQ, p, 8);
326 EVP_CIPHER_CTX_free(rc4_key);
328 memset(k6_data, 0, sizeof(k6_data));
331 _gsskrb5_decode_be_om_uint32(SND_SEQ, &seq_number);
333 if (context_handle->more_flags & LOCAL)
334 cmp = (ct_memcmp(&SND_SEQ[4], "\xff\xff\xff\xff", 4) != 0);
336 cmp = (ct_memcmp(&SND_SEQ[4], "\x00\x00\x00\x00", 4) != 0);
338 memset(SND_SEQ, 0, sizeof(SND_SEQ));
341 return GSS_S_BAD_MIC;
344 HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex);
345 omret = _gssapi_msg_order_check(context_handle->order, seq_number);
346 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
351 return GSS_S_COMPLETE;
355 _gssapi_wrap_arcfour(OM_uint32 * minor_status,
356 const gsskrb5_ctx context_handle,
357 krb5_context context,
360 const gss_buffer_t input_message_buffer,
362 gss_buffer_t output_message_buffer,
365 u_char Klocaldata[16], k6_data[16], *p, *p0;
366 size_t len, total_len, datalen;
367 krb5_keyblock Klocal;
374 datalen = input_message_buffer->length;
376 if (IS_DCE_STYLE(context_handle)) {
377 len = GSS_ARCFOUR_WRAP_TOKEN_SIZE;
378 _gssapi_encap_length(len, &len, &total_len, GSS_KRB5_MECHANISM);
379 total_len += datalen;
381 datalen += 1; /* padding */
382 len = datalen + GSS_ARCFOUR_WRAP_TOKEN_SIZE;
383 _gssapi_encap_length(len, &len, &total_len, GSS_KRB5_MECHANISM);
386 output_message_buffer->length = total_len;
387 output_message_buffer->value = malloc (total_len);
388 if (output_message_buffer->value == NULL) {
389 *minor_status = ENOMEM;
390 return GSS_S_FAILURE;
393 p0 = _gssapi_make_mech_header(output_message_buffer->value,
398 *p++ = 0x02; /* TOK_ID */
400 *p++ = 0x11; /* SGN_ALG */
403 *p++ = 0x10; /* SEAL_ALG */
406 *p++ = 0xff; /* SEAL_ALG */
409 *p++ = 0xff; /* Filler */
414 HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex);
415 krb5_auth_con_getlocalseqnumber (context,
416 context_handle->auth_context,
419 _gsskrb5_encode_be_om_uint32(seq_number, p0 + 8);
421 krb5_auth_con_setlocalseqnumber (context,
422 context_handle->auth_context,
424 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
427 (context_handle->more_flags & LOCAL) ? 0 : 0xff,
430 krb5_generate_random_block(p0 + 24, 8); /* fill in Confounder */
432 /* p points to data */
433 p = p0 + GSS_ARCFOUR_WRAP_TOKEN_SIZE;
434 memcpy(p, input_message_buffer->value, input_message_buffer->length);
436 if (!IS_DCE_STYLE(context_handle))
437 p[input_message_buffer->length] = 1; /* padding */
439 ret = arcfour_mic_cksum(context,
440 key, KRB5_KU_USAGE_SEAL,
441 p0 + 16, 8, /* SGN_CKSUM */
442 p0, 8, /* TOK_ID, SGN_ALG, SEAL_ALG, Filler */
443 p0 + 24, 8, /* Confounder */
444 p0 + GSS_ARCFOUR_WRAP_TOKEN_SIZE,
448 _gsskrb5_release_buffer(minor_status, output_message_buffer);
449 return GSS_S_FAILURE;
455 Klocal.keytype = key->keytype;
456 Klocal.keyvalue.data = Klocaldata;
457 Klocal.keyvalue.length = sizeof(Klocaldata);
459 for (i = 0; i < 16; i++)
460 Klocaldata[i] = ((u_char *)key->keyvalue.data)[i] ^ 0xF0;
462 ret = arcfour_mic_key(context, &Klocal,
463 p0 + 8, 4, /* SND_SEQ */
464 k6_data, sizeof(k6_data));
465 memset(Klocaldata, 0, sizeof(Klocaldata));
467 _gsskrb5_release_buffer(minor_status, output_message_buffer);
469 return GSS_S_FAILURE;
474 EVP_CIPHER_CTX *rc4_key;
476 rc4_key = EVP_CIPHER_CTX_new();
477 if (rc4_key == NULL) {
478 _gsskrb5_release_buffer(minor_status, output_message_buffer);
479 *minor_status = ENOMEM;
480 return GSS_S_FAILURE;
482 EVP_CipherInit_ex(rc4_key, EVP_rc4(), NULL, k6_data, NULL, 1);
483 EVP_Cipher(rc4_key, p0 + 24, p0 + 24, 8 + datalen);
484 EVP_CIPHER_CTX_free(rc4_key);
486 memset(k6_data, 0, sizeof(k6_data));
488 ret = arcfour_mic_key(context, key,
489 p0 + 16, 8, /* SGN_CKSUM */
490 k6_data, sizeof(k6_data));
492 _gsskrb5_release_buffer(minor_status, output_message_buffer);
494 return GSS_S_FAILURE;
498 EVP_CIPHER_CTX *rc4_key;
500 rc4_key = EVP_CIPHER_CTX_new();
501 if (rc4_key == NULL) {
502 _gsskrb5_release_buffer(minor_status, output_message_buffer);
503 *minor_status = ENOMEM;
504 return GSS_S_FAILURE;
506 EVP_CipherInit_ex(rc4_key, EVP_rc4(), NULL, k6_data, NULL, 1);
507 EVP_Cipher(rc4_key, p0 + 8, p0 + 8 /* SND_SEQ */, 8);
508 EVP_CIPHER_CTX_free(rc4_key);
509 memset(k6_data, 0, sizeof(k6_data));
513 *conf_state = conf_req_flag;
516 return GSS_S_COMPLETE;
519 OM_uint32 _gssapi_unwrap_arcfour(OM_uint32 *minor_status,
520 const gsskrb5_ctx context_handle,
521 krb5_context context,
522 const gss_buffer_t input_message_buffer,
523 gss_buffer_t output_message_buffer,
525 gss_qop_t *qop_state,
528 u_char Klocaldata[16];
529 krb5_keyblock Klocal;
534 u_char k6_data[16], SND_SEQ[8], Confounder[8];
535 u_char cksum_data[8];
539 size_t padlen = 0, len;
546 p0 = input_message_buffer->value;
548 if (IS_DCE_STYLE(context_handle)) {
549 len = GSS_ARCFOUR_WRAP_TOKEN_SIZE +
550 GSS_ARCFOUR_WRAP_TOKEN_DCE_DER_HEADER_SIZE;
551 if (input_message_buffer->length < len)
552 return GSS_S_BAD_MECH;
554 len = input_message_buffer->length;
557 omret = _gssapi_verify_mech_header(&p0,
563 /* length of mech header */
564 len = (p0 - (u_char *)input_message_buffer->value) +
565 GSS_ARCFOUR_WRAP_TOKEN_SIZE;
567 if (len > input_message_buffer->length)
568 return GSS_S_BAD_MECH;
571 datalen = input_message_buffer->length - len;
575 if (memcmp(p, "\x02\x01", 2) != 0)
576 return GSS_S_BAD_SIG;
578 if (memcmp(p, "\x11\x00", 2) != 0) /* SGN_ALG = HMAC MD5 ARCFOUR */
579 return GSS_S_BAD_SIG;
582 if (memcmp (p, "\x10\x00", 2) == 0)
584 else if (memcmp (p, "\xff\xff", 2) == 0)
587 return GSS_S_BAD_SIG;
590 if (memcmp (p, "\xff\xff", 2) != 0)
591 return GSS_S_BAD_MIC;
594 ret = arcfour_mic_key(context, key,
595 p0 + 16, 8, /* SGN_CKSUM */
596 k6_data, sizeof(k6_data));
599 return GSS_S_FAILURE;
603 EVP_CIPHER_CTX *rc4_key;
605 rc4_key = EVP_CIPHER_CTX_new();
606 if (rc4_key == NULL) {
607 *minor_status = ENOMEM;
608 return GSS_S_FAILURE;
610 EVP_CipherInit_ex(rc4_key, EVP_rc4(), NULL, k6_data, NULL, 1);
611 EVP_Cipher(rc4_key, SND_SEQ, p0 + 8, 8);
612 EVP_CIPHER_CTX_free(rc4_key);
613 memset(k6_data, 0, sizeof(k6_data));
616 _gsskrb5_decode_be_om_uint32(SND_SEQ, &seq_number);
618 if (context_handle->more_flags & LOCAL)
619 cmp = (ct_memcmp(&SND_SEQ[4], "\xff\xff\xff\xff", 4) != 0);
621 cmp = (ct_memcmp(&SND_SEQ[4], "\x00\x00\x00\x00", 4) != 0);
625 return GSS_S_BAD_MIC;
631 Klocal.keytype = key->keytype;
632 Klocal.keyvalue.data = Klocaldata;
633 Klocal.keyvalue.length = sizeof(Klocaldata);
635 for (i = 0; i < 16; i++)
636 Klocaldata[i] = ((u_char *)key->keyvalue.data)[i] ^ 0xF0;
638 ret = arcfour_mic_key(context, &Klocal,
640 k6_data, sizeof(k6_data));
641 memset(Klocaldata, 0, sizeof(Klocaldata));
644 return GSS_S_FAILURE;
647 output_message_buffer->value = malloc(datalen);
648 if (output_message_buffer->value == NULL) {
649 *minor_status = ENOMEM;
650 return GSS_S_FAILURE;
652 output_message_buffer->length = datalen;
655 EVP_CIPHER_CTX *rc4_key;
657 rc4_key = EVP_CIPHER_CTX_new();
658 if (rc4_key == NULL) {
659 _gsskrb5_release_buffer(minor_status, output_message_buffer);
660 *minor_status = ENOMEM;
661 return GSS_S_FAILURE;
663 EVP_CipherInit_ex(rc4_key, EVP_rc4(), NULL, k6_data, NULL, 1);
664 EVP_Cipher(rc4_key, Confounder, p0 + 24, 8);
665 EVP_Cipher(rc4_key, output_message_buffer->value, p0 + GSS_ARCFOUR_WRAP_TOKEN_SIZE, datalen);
666 EVP_CIPHER_CTX_free(rc4_key);
668 memcpy(Confounder, p0 + 24, 8); /* Confounder */
669 memcpy(output_message_buffer->value,
670 p0 + GSS_ARCFOUR_WRAP_TOKEN_SIZE,
673 memset(k6_data, 0, sizeof(k6_data));
675 if (!IS_DCE_STYLE(context_handle)) {
676 ret = _gssapi_verify_pad(output_message_buffer, datalen, &padlen);
678 _gsskrb5_release_buffer(minor_status, output_message_buffer);
682 output_message_buffer->length -= padlen;
685 ret = arcfour_mic_cksum(context,
686 key, KRB5_KU_USAGE_SEAL,
687 cksum_data, sizeof(cksum_data),
689 Confounder, sizeof(Confounder),
690 output_message_buffer->value,
691 output_message_buffer->length + padlen);
693 _gsskrb5_release_buffer(minor_status, output_message_buffer);
695 return GSS_S_FAILURE;
698 cmp = (ct_memcmp(cksum_data, p0 + 16, 8) == 0); /* SGN_CKSUM */
700 _gsskrb5_release_buffer(minor_status, output_message_buffer);
702 return GSS_S_BAD_MIC;
705 HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex);
706 omret = _gssapi_msg_order_check(context_handle->order, seq_number);
707 HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);
712 *conf_state = conf_flag;
715 return GSS_S_COMPLETE;
719 max_wrap_length_arcfour(const gsskrb5_ctx ctx,
722 OM_uint32 *max_input_size)
725 * if GSS_C_DCE_STYLE is in use:
726 * - we only need to encapsulate the WRAP token
727 * However, since this is a fixed since, we just
729 if (IS_DCE_STYLE(ctx)) {
730 size_t len, total_len;
732 len = GSS_ARCFOUR_WRAP_TOKEN_SIZE;
733 _gssapi_encap_length(len, &len, &total_len, GSS_KRB5_MECHANISM);
735 if (input_length < len)
738 *max_input_size = input_length - len;
741 size_t extrasize = GSS_ARCFOUR_WRAP_TOKEN_SIZE;
742 size_t blocksize = 8;
743 size_t len, total_len;
745 len = 8 + input_length + blocksize + extrasize;
747 _gsskrb5_encap_length(len, &len, &total_len, GSS_KRB5_MECHANISM);
749 total_len -= input_length; /* token length */
750 if (total_len < input_length) {
751 *max_input_size = (input_length - total_len);
752 (*max_input_size) &= (~(OM_uint32)(blocksize - 1));
758 return GSS_S_COMPLETE;
762 _gssapi_wrap_size_arcfour(OM_uint32 *minor_status,
763 const gsskrb5_ctx ctx,
764 krb5_context context,
767 OM_uint32 req_output_size,
768 OM_uint32 *max_input_size,
774 ret = krb5_crypto_init(context, key, 0, &crypto);
777 return GSS_S_FAILURE;
780 ret = max_wrap_length_arcfour(ctx, crypto,
781 req_output_size, max_input_size);
784 krb5_crypto_destroy(context, crypto);
785 return GSS_S_FAILURE;
788 krb5_crypto_destroy(context, crypto);
790 return GSS_S_COMPLETE;