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
== KEYTYPE_ARCFOUR_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
= KEYTYPE_ARCFOUR
;
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
;
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_set_key (&rc4_key
, sizeof(k6_data
), k6_data
);
239 RC4 (&rc4_key
, 8, p
, p
);
241 memset(&rc4_key
, 0, sizeof(rc4_key
));
242 memset(k6_data
, 0, sizeof(k6_data
));
245 return GSS_S_COMPLETE
;
250 _gssapi_verify_mic_arcfour(OM_uint32
* minor_status
,
251 const gsskrb5_ctx context_handle
,
252 krb5_context context
,
253 const gss_buffer_t message_buffer
,
254 const gss_buffer_t token_buffer
,
255 gss_qop_t
* qop_state
,
262 u_char SND_SEQ
[8], cksum_data
[8], *p
;
269 p
= token_buffer
->value
;
270 omret
= _gsskrb5_verify_header (&p
,
271 token_buffer
->length
,
277 if (memcmp(p
, "\x11\x00", 2) != 0) /* SGN_ALG = HMAC MD5 ARCFOUR */
278 return GSS_S_BAD_SIG
;
280 if (memcmp (p
, "\xff\xff\xff\xff", 4) != 0)
281 return GSS_S_BAD_MIC
;
284 ret
= arcfour_mic_cksum(context
,
285 key
, KRB5_KU_USAGE_SIGN
,
286 cksum_data
, sizeof(cksum_data
),
288 message_buffer
->value
, message_buffer
->length
,
292 return GSS_S_FAILURE
;
295 ret
= arcfour_mic_key(context
, key
,
296 cksum_data
, sizeof(cksum_data
),
297 k6_data
, sizeof(k6_data
));
300 return GSS_S_FAILURE
;
303 cmp
= memcmp(cksum_data
, p
+ 8, 8);
306 return GSS_S_BAD_MIC
;
312 RC4_set_key (&rc4_key
, sizeof(k6_data
), (void*)k6_data
);
313 RC4 (&rc4_key
, 8, p
, SND_SEQ
);
315 memset(&rc4_key
, 0, sizeof(rc4_key
));
316 memset(k6_data
, 0, sizeof(k6_data
));
319 _gsskrb5_decode_be_om_uint32(SND_SEQ
, &seq_number
);
321 if (context_handle
->more_flags
& LOCAL
)
322 cmp
= memcmp(&SND_SEQ
[4], "\xff\xff\xff\xff", 4);
324 cmp
= memcmp(&SND_SEQ
[4], "\x00\x00\x00\x00", 4);
326 memset(SND_SEQ
, 0, sizeof(SND_SEQ
));
329 return GSS_S_BAD_MIC
;
332 HEIMDAL_MUTEX_lock(&context_handle
->ctx_id_mutex
);
333 omret
= _gssapi_msg_order_check(context_handle
->order
, seq_number
);
334 HEIMDAL_MUTEX_unlock(&context_handle
->ctx_id_mutex
);
339 return GSS_S_COMPLETE
;
343 _gssapi_wrap_arcfour(OM_uint32
* minor_status
,
344 const gsskrb5_ctx context_handle
,
345 krb5_context context
,
348 const gss_buffer_t input_message_buffer
,
350 gss_buffer_t output_message_buffer
,
353 u_char Klocaldata
[16], k6_data
[16], *p
, *p0
;
354 size_t len
, total_len
, datalen
;
355 krb5_keyblock Klocal
;
362 datalen
= input_message_buffer
->length
;
364 if (IS_DCE_STYLE(context_handle
)) {
365 len
= GSS_ARCFOUR_WRAP_TOKEN_SIZE
;
366 _gssapi_encap_length(len
, &len
, &total_len
, GSS_KRB5_MECHANISM
);
367 total_len
+= datalen
;
369 datalen
+= 1; /* padding */
370 len
= datalen
+ GSS_ARCFOUR_WRAP_TOKEN_SIZE
;
371 _gssapi_encap_length(len
, &len
, &total_len
, GSS_KRB5_MECHANISM
);
374 output_message_buffer
->length
= total_len
;
375 output_message_buffer
->value
= malloc (total_len
);
376 if (output_message_buffer
->value
== NULL
) {
377 *minor_status
= ENOMEM
;
378 return GSS_S_FAILURE
;
381 p0
= _gssapi_make_mech_header(output_message_buffer
->value
,
386 *p
++ = 0x02; /* TOK_ID */
388 *p
++ = 0x11; /* SGN_ALG */
391 *p
++ = 0x10; /* SEAL_ALG */
394 *p
++ = 0xff; /* SEAL_ALG */
397 *p
++ = 0xff; /* Filler */
402 HEIMDAL_MUTEX_lock(&context_handle
->ctx_id_mutex
);
403 krb5_auth_con_getlocalseqnumber (context
,
404 context_handle
->auth_context
,
407 _gsskrb5_encode_be_om_uint32(seq_number
, p0
+ 8);
409 krb5_auth_con_setlocalseqnumber (context
,
410 context_handle
->auth_context
,
412 HEIMDAL_MUTEX_unlock(&context_handle
->ctx_id_mutex
);
415 (context_handle
->more_flags
& LOCAL
) ? 0 : 0xff,
418 krb5_generate_random_block(p0
+ 24, 8); /* fill in Confounder */
420 /* p points to data */
421 p
= p0
+ GSS_ARCFOUR_WRAP_TOKEN_SIZE
;
422 memcpy(p
, input_message_buffer
->value
, input_message_buffer
->length
);
424 if (!IS_DCE_STYLE(context_handle
))
425 p
[input_message_buffer
->length
] = 1; /* padding */
427 ret
= arcfour_mic_cksum(context
,
428 key
, KRB5_KU_USAGE_SEAL
,
429 p0
+ 16, 8, /* SGN_CKSUM */
430 p0
, 8, /* TOK_ID, SGN_ALG, SEAL_ALG, Filler */
431 p0
+ 24, 8, /* Confounder */
432 p0
+ GSS_ARCFOUR_WRAP_TOKEN_SIZE
,
436 _gsskrb5_release_buffer(minor_status
, output_message_buffer
);
437 return GSS_S_FAILURE
;
443 Klocal
.keytype
= key
->keytype
;
444 Klocal
.keyvalue
.data
= Klocaldata
;
445 Klocal
.keyvalue
.length
= sizeof(Klocaldata
);
447 for (i
= 0; i
< 16; i
++)
448 Klocaldata
[i
] = ((u_char
*)key
->keyvalue
.data
)[i
] ^ 0xF0;
450 ret
= arcfour_mic_key(context
, &Klocal
,
451 p0
+ 8, 4, /* SND_SEQ */
452 k6_data
, sizeof(k6_data
));
453 memset(Klocaldata
, 0, sizeof(Klocaldata
));
455 _gsskrb5_release_buffer(minor_status
, output_message_buffer
);
457 return GSS_S_FAILURE
;
464 RC4_set_key (&rc4_key
, sizeof(k6_data
), (void *)k6_data
);
466 RC4 (&rc4_key
, 8 + datalen
, p0
+ 24, p0
+ 24); /* Confounder + data */
467 memset(&rc4_key
, 0, sizeof(rc4_key
));
469 memset(k6_data
, 0, sizeof(k6_data
));
471 ret
= arcfour_mic_key(context
, key
,
472 p0
+ 16, 8, /* SGN_CKSUM */
473 k6_data
, sizeof(k6_data
));
475 _gsskrb5_release_buffer(minor_status
, output_message_buffer
);
477 return GSS_S_FAILURE
;
483 RC4_set_key (&rc4_key
, sizeof(k6_data
), k6_data
);
484 RC4 (&rc4_key
, 8, p0
+ 8, p0
+ 8); /* SND_SEQ */
485 memset(&rc4_key
, 0, sizeof(rc4_key
));
486 memset(k6_data
, 0, sizeof(k6_data
));
490 *conf_state
= conf_req_flag
;
493 return GSS_S_COMPLETE
;
496 OM_uint32
_gssapi_unwrap_arcfour(OM_uint32
*minor_status
,
497 const gsskrb5_ctx context_handle
,
498 krb5_context context
,
499 const gss_buffer_t input_message_buffer
,
500 gss_buffer_t output_message_buffer
,
502 gss_qop_t
*qop_state
,
505 u_char Klocaldata
[16];
506 krb5_keyblock Klocal
;
511 u_char k6_data
[16], SND_SEQ
[8], Confounder
[8];
512 u_char cksum_data
[8];
516 size_t padlen
= 0, len
;
523 p0
= input_message_buffer
->value
;
525 if (IS_DCE_STYLE(context_handle
)) {
526 len
= GSS_ARCFOUR_WRAP_TOKEN_SIZE
+
527 GSS_ARCFOUR_WRAP_TOKEN_DCE_DER_HEADER_SIZE
;
528 if (input_message_buffer
->length
< len
)
529 return GSS_S_BAD_MECH
;
531 len
= input_message_buffer
->length
;
534 omret
= _gssapi_verify_mech_header(&p0
,
540 /* length of mech header */
541 len
= (p0
- (u_char
*)input_message_buffer
->value
) +
542 GSS_ARCFOUR_WRAP_TOKEN_SIZE
;
544 if (len
> input_message_buffer
->length
)
545 return GSS_S_BAD_MECH
;
548 datalen
= input_message_buffer
->length
- len
;
552 if (memcmp(p
, "\x02\x01", 2) != 0)
553 return GSS_S_BAD_SIG
;
555 if (memcmp(p
, "\x11\x00", 2) != 0) /* SGN_ALG = HMAC MD5 ARCFOUR */
556 return GSS_S_BAD_SIG
;
559 if (memcmp (p
, "\x10\x00", 2) == 0)
561 else if (memcmp (p
, "\xff\xff", 2) == 0)
564 return GSS_S_BAD_SIG
;
567 if (memcmp (p
, "\xff\xff", 2) != 0)
568 return GSS_S_BAD_MIC
;
571 ret
= arcfour_mic_key(context
, key
,
572 p0
+ 16, 8, /* SGN_CKSUM */
573 k6_data
, sizeof(k6_data
));
576 return GSS_S_FAILURE
;
582 RC4_set_key (&rc4_key
, sizeof(k6_data
), k6_data
);
583 RC4 (&rc4_key
, 8, p0
+ 8, SND_SEQ
); /* SND_SEQ */
584 memset(&rc4_key
, 0, sizeof(rc4_key
));
585 memset(k6_data
, 0, sizeof(k6_data
));
588 _gsskrb5_decode_be_om_uint32(SND_SEQ
, &seq_number
);
590 if (context_handle
->more_flags
& LOCAL
)
591 cmp
= memcmp(&SND_SEQ
[4], "\xff\xff\xff\xff", 4);
593 cmp
= memcmp(&SND_SEQ
[4], "\x00\x00\x00\x00", 4);
597 return GSS_S_BAD_MIC
;
603 Klocal
.keytype
= key
->keytype
;
604 Klocal
.keyvalue
.data
= Klocaldata
;
605 Klocal
.keyvalue
.length
= sizeof(Klocaldata
);
607 for (i
= 0; i
< 16; i
++)
608 Klocaldata
[i
] = ((u_char
*)key
->keyvalue
.data
)[i
] ^ 0xF0;
610 ret
= arcfour_mic_key(context
, &Klocal
,
612 k6_data
, sizeof(k6_data
));
613 memset(Klocaldata
, 0, sizeof(Klocaldata
));
616 return GSS_S_FAILURE
;
619 output_message_buffer
->value
= malloc(datalen
);
620 if (output_message_buffer
->value
== NULL
) {
621 *minor_status
= ENOMEM
;
622 return GSS_S_FAILURE
;
624 output_message_buffer
->length
= datalen
;
629 RC4_set_key (&rc4_key
, sizeof(k6_data
), k6_data
);
630 RC4 (&rc4_key
, 8, p0
+ 24, Confounder
); /* Confounder */
631 RC4 (&rc4_key
, datalen
, p0
+ GSS_ARCFOUR_WRAP_TOKEN_SIZE
,
632 output_message_buffer
->value
);
633 memset(&rc4_key
, 0, sizeof(rc4_key
));
635 memcpy(Confounder
, p0
+ 24, 8); /* Confounder */
636 memcpy(output_message_buffer
->value
,
637 p0
+ GSS_ARCFOUR_WRAP_TOKEN_SIZE
,
640 memset(k6_data
, 0, sizeof(k6_data
));
642 if (!IS_DCE_STYLE(context_handle
)) {
643 ret
= _gssapi_verify_pad(output_message_buffer
, datalen
, &padlen
);
645 _gsskrb5_release_buffer(minor_status
, output_message_buffer
);
649 output_message_buffer
->length
-= padlen
;
652 ret
= arcfour_mic_cksum(context
,
653 key
, KRB5_KU_USAGE_SEAL
,
654 cksum_data
, sizeof(cksum_data
),
656 Confounder
, sizeof(Confounder
),
657 output_message_buffer
->value
,
658 output_message_buffer
->length
+ padlen
);
660 _gsskrb5_release_buffer(minor_status
, output_message_buffer
);
662 return GSS_S_FAILURE
;
665 cmp
= memcmp(cksum_data
, p0
+ 16, 8); /* SGN_CKSUM */
667 _gsskrb5_release_buffer(minor_status
, output_message_buffer
);
669 return GSS_S_BAD_MIC
;
672 HEIMDAL_MUTEX_lock(&context_handle
->ctx_id_mutex
);
673 omret
= _gssapi_msg_order_check(context_handle
->order
, seq_number
);
674 HEIMDAL_MUTEX_unlock(&context_handle
->ctx_id_mutex
);
679 *conf_state
= conf_flag
;
682 return GSS_S_COMPLETE
;
686 max_wrap_length_arcfour(const gsskrb5_ctx ctx
,
689 OM_uint32
*max_input_size
)
692 * if GSS_C_DCE_STYLE is in use:
693 * - we only need to encapsulate the WRAP token
694 * However, since this is a fixed since, we just
696 if (IS_DCE_STYLE(ctx
)) {
697 size_t len
, total_len
;
699 len
= GSS_ARCFOUR_WRAP_TOKEN_SIZE
;
700 _gssapi_encap_length(len
, &len
, &total_len
, GSS_KRB5_MECHANISM
);
702 if (input_length
< len
)
705 *max_input_size
= input_length
- len
;
708 size_t extrasize
= GSS_ARCFOUR_WRAP_TOKEN_SIZE
;
709 size_t blocksize
= 8;
710 size_t len
, total_len
;
712 len
= 8 + input_length
+ blocksize
+ extrasize
;
714 _gsskrb5_encap_length(len
, &len
, &total_len
, GSS_KRB5_MECHANISM
);
716 total_len
-= input_length
; /* token length */
717 if (total_len
< input_length
) {
718 *max_input_size
= (input_length
- total_len
);
719 (*max_input_size
) &= (~(OM_uint32
)(blocksize
- 1));
725 return GSS_S_COMPLETE
;
729 _gssapi_wrap_size_arcfour(OM_uint32
*minor_status
,
730 const gsskrb5_ctx ctx
,
731 krb5_context context
,
734 OM_uint32 req_output_size
,
735 OM_uint32
*max_input_size
,
741 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
744 return GSS_S_FAILURE
;
747 ret
= max_wrap_length_arcfour(ctx
, crypto
,
748 req_output_size
, max_input_size
);
751 krb5_crypto_destroy(context
, crypto
);
752 return GSS_S_FAILURE
;
755 krb5_crypto_destroy(context
, crypto
);
757 return GSS_S_COMPLETE
;