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
== KRB5_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
= KRB5_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
,
155 memcpy(sgn_cksum
, CKSUM
.checksum
.data
, sgn_cksum_sz
);
156 free_Checksum(&CKSUM
);
158 krb5_crypto_destroy(context
, crypto
);
165 _gssapi_get_mic_arcfour(OM_uint32
* minor_status
,
166 const gsskrb5_ctx context_handle
,
167 krb5_context context
,
169 const gss_buffer_t message_buffer
,
170 gss_buffer_t message_token
,
175 size_t len
, total_len
;
176 u_char k6_data
[16], *p0
, *p
;
177 EVP_CIPHER_CTX rc4_key
;
179 _gsskrb5_encap_length (22, &len
, &total_len
, GSS_KRB5_MECHANISM
);
181 message_token
->length
= total_len
;
182 message_token
->value
= malloc (total_len
);
183 if (message_token
->value
== NULL
) {
184 *minor_status
= ENOMEM
;
185 return GSS_S_FAILURE
;
188 p0
= _gssapi_make_mech_header(message_token
->value
,
193 *p
++ = 0x01; /* TOK_ID */
195 *p
++ = 0x11; /* SGN_ALG */
197 *p
++ = 0xff; /* Filler */
204 ret
= arcfour_mic_cksum(context
,
205 key
, KRB5_KU_USAGE_SIGN
,
206 p0
+ 16, 8, /* SGN_CKSUM */
207 p0
, 8, /* TOK_ID, SGN_ALG, Filer */
208 message_buffer
->value
, message_buffer
->length
,
211 _gsskrb5_release_buffer(minor_status
, message_token
);
213 return GSS_S_FAILURE
;
216 ret
= arcfour_mic_key(context
, key
,
217 p0
+ 16, 8, /* SGN_CKSUM */
218 k6_data
, sizeof(k6_data
));
220 _gsskrb5_release_buffer(minor_status
, message_token
);
222 return GSS_S_FAILURE
;
225 HEIMDAL_MUTEX_lock(&context_handle
->ctx_id_mutex
);
226 krb5_auth_con_getlocalseqnumber (context
,
227 context_handle
->auth_context
,
229 p
= p0
+ 8; /* SND_SEQ */
230 _gsskrb5_encode_be_om_uint32(seq_number
, p
);
232 krb5_auth_con_setlocalseqnumber (context
,
233 context_handle
->auth_context
,
235 HEIMDAL_MUTEX_unlock(&context_handle
->ctx_id_mutex
);
237 memset (p
+ 4, (context_handle
->more_flags
& LOCAL
) ? 0 : 0xff, 4);
239 EVP_CIPHER_CTX_init(&rc4_key
);
240 EVP_CipherInit_ex(&rc4_key
, EVP_rc4(), NULL
, k6_data
, NULL
, 1);
241 EVP_Cipher(&rc4_key
, p
, p
, 8);
242 EVP_CIPHER_CTX_cleanup(&rc4_key
);
244 memset(k6_data
, 0, sizeof(k6_data
));
247 return GSS_S_COMPLETE
;
252 _gssapi_verify_mic_arcfour(OM_uint32
* minor_status
,
253 const gsskrb5_ctx context_handle
,
254 krb5_context context
,
255 const gss_buffer_t message_buffer
,
256 const gss_buffer_t token_buffer
,
257 gss_qop_t
* qop_state
,
264 u_char SND_SEQ
[8], cksum_data
[8], *p
;
271 p
= token_buffer
->value
;
272 omret
= _gsskrb5_verify_header (&p
,
273 token_buffer
->length
,
279 if (memcmp(p
, "\x11\x00", 2) != 0) /* SGN_ALG = HMAC MD5 ARCFOUR */
280 return GSS_S_BAD_SIG
;
282 if (memcmp (p
, "\xff\xff\xff\xff", 4) != 0)
283 return GSS_S_BAD_MIC
;
286 ret
= arcfour_mic_cksum(context
,
287 key
, KRB5_KU_USAGE_SIGN
,
288 cksum_data
, sizeof(cksum_data
),
290 message_buffer
->value
, message_buffer
->length
,
294 return GSS_S_FAILURE
;
297 ret
= arcfour_mic_key(context
, key
,
298 cksum_data
, sizeof(cksum_data
),
299 k6_data
, sizeof(k6_data
));
302 return GSS_S_FAILURE
;
305 cmp
= ct_memcmp(cksum_data
, p
+ 8, 8);
308 return GSS_S_BAD_MIC
;
312 EVP_CIPHER_CTX rc4_key
;
314 EVP_CIPHER_CTX_init(&rc4_key
);
315 EVP_CipherInit_ex(&rc4_key
, EVP_rc4(), NULL
, (void *)k6_data
, NULL
, 0);
316 EVP_Cipher(&rc4_key
, SND_SEQ
, p
, 8);
317 EVP_CIPHER_CTX_cleanup(&rc4_key
);
319 memset(k6_data
, 0, sizeof(k6_data
));
322 _gsskrb5_decode_be_om_uint32(SND_SEQ
, &seq_number
);
324 if (context_handle
->more_flags
& LOCAL
)
325 cmp
= memcmp(&SND_SEQ
[4], "\xff\xff\xff\xff", 4);
327 cmp
= memcmp(&SND_SEQ
[4], "\x00\x00\x00\x00", 4);
329 memset(SND_SEQ
, 0, sizeof(SND_SEQ
));
332 return GSS_S_BAD_MIC
;
335 HEIMDAL_MUTEX_lock(&context_handle
->ctx_id_mutex
);
336 omret
= _gssapi_msg_order_check(context_handle
->order
, seq_number
);
337 HEIMDAL_MUTEX_unlock(&context_handle
->ctx_id_mutex
);
342 return GSS_S_COMPLETE
;
346 _gssapi_wrap_arcfour(OM_uint32
* minor_status
,
347 const gsskrb5_ctx context_handle
,
348 krb5_context context
,
351 const gss_buffer_t input_message_buffer
,
353 gss_buffer_t output_message_buffer
,
356 u_char Klocaldata
[16], k6_data
[16], *p
, *p0
;
357 size_t len
, total_len
, datalen
;
358 krb5_keyblock Klocal
;
365 datalen
= input_message_buffer
->length
;
367 if (IS_DCE_STYLE(context_handle
)) {
368 len
= GSS_ARCFOUR_WRAP_TOKEN_SIZE
;
369 _gssapi_encap_length(len
, &len
, &total_len
, GSS_KRB5_MECHANISM
);
370 total_len
+= datalen
;
372 datalen
+= 1; /* padding */
373 len
= datalen
+ GSS_ARCFOUR_WRAP_TOKEN_SIZE
;
374 _gssapi_encap_length(len
, &len
, &total_len
, GSS_KRB5_MECHANISM
);
377 output_message_buffer
->length
= total_len
;
378 output_message_buffer
->value
= malloc (total_len
);
379 if (output_message_buffer
->value
== NULL
) {
380 *minor_status
= ENOMEM
;
381 return GSS_S_FAILURE
;
384 p0
= _gssapi_make_mech_header(output_message_buffer
->value
,
389 *p
++ = 0x02; /* TOK_ID */
391 *p
++ = 0x11; /* SGN_ALG */
394 *p
++ = 0x10; /* SEAL_ALG */
397 *p
++ = 0xff; /* SEAL_ALG */
400 *p
++ = 0xff; /* Filler */
405 HEIMDAL_MUTEX_lock(&context_handle
->ctx_id_mutex
);
406 krb5_auth_con_getlocalseqnumber (context
,
407 context_handle
->auth_context
,
410 _gsskrb5_encode_be_om_uint32(seq_number
, p0
+ 8);
412 krb5_auth_con_setlocalseqnumber (context
,
413 context_handle
->auth_context
,
415 HEIMDAL_MUTEX_unlock(&context_handle
->ctx_id_mutex
);
418 (context_handle
->more_flags
& LOCAL
) ? 0 : 0xff,
421 krb5_generate_random_block(p0
+ 24, 8); /* fill in Confounder */
423 /* p points to data */
424 p
= p0
+ GSS_ARCFOUR_WRAP_TOKEN_SIZE
;
425 memcpy(p
, input_message_buffer
->value
, input_message_buffer
->length
);
427 if (!IS_DCE_STYLE(context_handle
))
428 p
[input_message_buffer
->length
] = 1; /* padding */
430 ret
= arcfour_mic_cksum(context
,
431 key
, KRB5_KU_USAGE_SEAL
,
432 p0
+ 16, 8, /* SGN_CKSUM */
433 p0
, 8, /* TOK_ID, SGN_ALG, SEAL_ALG, Filler */
434 p0
+ 24, 8, /* Confounder */
435 p0
+ GSS_ARCFOUR_WRAP_TOKEN_SIZE
,
439 _gsskrb5_release_buffer(minor_status
, output_message_buffer
);
440 return GSS_S_FAILURE
;
446 Klocal
.keytype
= key
->keytype
;
447 Klocal
.keyvalue
.data
= Klocaldata
;
448 Klocal
.keyvalue
.length
= sizeof(Klocaldata
);
450 for (i
= 0; i
< 16; i
++)
451 Klocaldata
[i
] = ((u_char
*)key
->keyvalue
.data
)[i
] ^ 0xF0;
453 ret
= arcfour_mic_key(context
, &Klocal
,
454 p0
+ 8, 4, /* SND_SEQ */
455 k6_data
, sizeof(k6_data
));
456 memset(Klocaldata
, 0, sizeof(Klocaldata
));
458 _gsskrb5_release_buffer(minor_status
, output_message_buffer
);
460 return GSS_S_FAILURE
;
465 EVP_CIPHER_CTX rc4_key
;
467 EVP_CIPHER_CTX_init(&rc4_key
);
468 EVP_CipherInit_ex(&rc4_key
, EVP_rc4(), NULL
, k6_data
, NULL
, 1);
469 EVP_Cipher(&rc4_key
, p0
+ 24, p0
+ 24, 8 + datalen
);
470 EVP_CIPHER_CTX_cleanup(&rc4_key
);
472 memset(k6_data
, 0, sizeof(k6_data
));
474 ret
= arcfour_mic_key(context
, key
,
475 p0
+ 16, 8, /* SGN_CKSUM */
476 k6_data
, sizeof(k6_data
));
478 _gsskrb5_release_buffer(minor_status
, output_message_buffer
);
480 return GSS_S_FAILURE
;
484 EVP_CIPHER_CTX rc4_key
;
486 EVP_CIPHER_CTX_init(&rc4_key
);
487 EVP_CipherInit_ex(&rc4_key
, EVP_rc4(), NULL
, k6_data
, NULL
, 1);
488 EVP_Cipher(&rc4_key
, p0
+ 8, p0
+ 8 /* SND_SEQ */, 8);
489 EVP_CIPHER_CTX_cleanup(&rc4_key
);
490 memset(k6_data
, 0, sizeof(k6_data
));
494 *conf_state
= conf_req_flag
;
497 return GSS_S_COMPLETE
;
500 OM_uint32
_gssapi_unwrap_arcfour(OM_uint32
*minor_status
,
501 const gsskrb5_ctx context_handle
,
502 krb5_context context
,
503 const gss_buffer_t input_message_buffer
,
504 gss_buffer_t output_message_buffer
,
506 gss_qop_t
*qop_state
,
509 u_char Klocaldata
[16];
510 krb5_keyblock Klocal
;
515 u_char k6_data
[16], SND_SEQ
[8], Confounder
[8];
516 u_char cksum_data
[8];
520 size_t padlen
= 0, len
;
527 p0
= input_message_buffer
->value
;
529 if (IS_DCE_STYLE(context_handle
)) {
530 len
= GSS_ARCFOUR_WRAP_TOKEN_SIZE
+
531 GSS_ARCFOUR_WRAP_TOKEN_DCE_DER_HEADER_SIZE
;
532 if (input_message_buffer
->length
< len
)
533 return GSS_S_BAD_MECH
;
535 len
= input_message_buffer
->length
;
538 omret
= _gssapi_verify_mech_header(&p0
,
544 /* length of mech header */
545 len
= (p0
- (u_char
*)input_message_buffer
->value
) +
546 GSS_ARCFOUR_WRAP_TOKEN_SIZE
;
548 if (len
> input_message_buffer
->length
)
549 return GSS_S_BAD_MECH
;
552 datalen
= input_message_buffer
->length
- len
;
556 if (memcmp(p
, "\x02\x01", 2) != 0)
557 return GSS_S_BAD_SIG
;
559 if (memcmp(p
, "\x11\x00", 2) != 0) /* SGN_ALG = HMAC MD5 ARCFOUR */
560 return GSS_S_BAD_SIG
;
563 if (memcmp (p
, "\x10\x00", 2) == 0)
565 else if (memcmp (p
, "\xff\xff", 2) == 0)
568 return GSS_S_BAD_SIG
;
571 if (memcmp (p
, "\xff\xff", 2) != 0)
572 return GSS_S_BAD_MIC
;
575 ret
= arcfour_mic_key(context
, key
,
576 p0
+ 16, 8, /* SGN_CKSUM */
577 k6_data
, sizeof(k6_data
));
580 return GSS_S_FAILURE
;
584 EVP_CIPHER_CTX rc4_key
;
586 EVP_CIPHER_CTX_init(&rc4_key
);
587 EVP_CipherInit_ex(&rc4_key
, EVP_rc4(), NULL
, k6_data
, NULL
, 1);
588 EVP_Cipher(&rc4_key
, SND_SEQ
, p0
+ 8, 8);
589 EVP_CIPHER_CTX_cleanup(&rc4_key
);
590 memset(k6_data
, 0, sizeof(k6_data
));
593 _gsskrb5_decode_be_om_uint32(SND_SEQ
, &seq_number
);
595 if (context_handle
->more_flags
& LOCAL
)
596 cmp
= memcmp(&SND_SEQ
[4], "\xff\xff\xff\xff", 4);
598 cmp
= memcmp(&SND_SEQ
[4], "\x00\x00\x00\x00", 4);
602 return GSS_S_BAD_MIC
;
608 Klocal
.keytype
= key
->keytype
;
609 Klocal
.keyvalue
.data
= Klocaldata
;
610 Klocal
.keyvalue
.length
= sizeof(Klocaldata
);
612 for (i
= 0; i
< 16; i
++)
613 Klocaldata
[i
] = ((u_char
*)key
->keyvalue
.data
)[i
] ^ 0xF0;
615 ret
= arcfour_mic_key(context
, &Klocal
,
617 k6_data
, sizeof(k6_data
));
618 memset(Klocaldata
, 0, sizeof(Klocaldata
));
621 return GSS_S_FAILURE
;
624 output_message_buffer
->value
= malloc(datalen
);
625 if (output_message_buffer
->value
== NULL
) {
626 *minor_status
= ENOMEM
;
627 return GSS_S_FAILURE
;
629 output_message_buffer
->length
= datalen
;
632 EVP_CIPHER_CTX rc4_key
;
634 EVP_CIPHER_CTX_init(&rc4_key
);
635 EVP_CipherInit_ex(&rc4_key
, EVP_rc4(), NULL
, k6_data
, NULL
, 1);
636 EVP_Cipher(&rc4_key
, Confounder
, p0
+ 24, 8);
637 EVP_Cipher(&rc4_key
, output_message_buffer
->value
, p0
+ GSS_ARCFOUR_WRAP_TOKEN_SIZE
, datalen
);
638 EVP_CIPHER_CTX_cleanup(&rc4_key
);
640 memcpy(Confounder
, p0
+ 24, 8); /* Confounder */
641 memcpy(output_message_buffer
->value
,
642 p0
+ GSS_ARCFOUR_WRAP_TOKEN_SIZE
,
645 memset(k6_data
, 0, sizeof(k6_data
));
647 if (!IS_DCE_STYLE(context_handle
)) {
648 ret
= _gssapi_verify_pad(output_message_buffer
, datalen
, &padlen
);
650 _gsskrb5_release_buffer(minor_status
, output_message_buffer
);
654 output_message_buffer
->length
-= padlen
;
657 ret
= arcfour_mic_cksum(context
,
658 key
, KRB5_KU_USAGE_SEAL
,
659 cksum_data
, sizeof(cksum_data
),
661 Confounder
, sizeof(Confounder
),
662 output_message_buffer
->value
,
663 output_message_buffer
->length
+ padlen
);
665 _gsskrb5_release_buffer(minor_status
, output_message_buffer
);
667 return GSS_S_FAILURE
;
670 cmp
= ct_memcmp(cksum_data
, p0
+ 16, 8); /* SGN_CKSUM */
672 _gsskrb5_release_buffer(minor_status
, output_message_buffer
);
674 return GSS_S_BAD_MIC
;
677 HEIMDAL_MUTEX_lock(&context_handle
->ctx_id_mutex
);
678 omret
= _gssapi_msg_order_check(context_handle
->order
, seq_number
);
679 HEIMDAL_MUTEX_unlock(&context_handle
->ctx_id_mutex
);
684 *conf_state
= conf_flag
;
687 return GSS_S_COMPLETE
;
691 max_wrap_length_arcfour(const gsskrb5_ctx ctx
,
694 OM_uint32
*max_input_size
)
697 * if GSS_C_DCE_STYLE is in use:
698 * - we only need to encapsulate the WRAP token
699 * However, since this is a fixed since, we just
701 if (IS_DCE_STYLE(ctx
)) {
702 size_t len
, total_len
;
704 len
= GSS_ARCFOUR_WRAP_TOKEN_SIZE
;
705 _gssapi_encap_length(len
, &len
, &total_len
, GSS_KRB5_MECHANISM
);
707 if (input_length
< len
)
710 *max_input_size
= input_length
- len
;
713 size_t extrasize
= GSS_ARCFOUR_WRAP_TOKEN_SIZE
;
714 size_t blocksize
= 8;
715 size_t len
, total_len
;
717 len
= 8 + input_length
+ blocksize
+ extrasize
;
719 _gsskrb5_encap_length(len
, &len
, &total_len
, GSS_KRB5_MECHANISM
);
721 total_len
-= input_length
; /* token length */
722 if (total_len
< input_length
) {
723 *max_input_size
= (input_length
- total_len
);
724 (*max_input_size
) &= (~(OM_uint32
)(blocksize
- 1));
730 return GSS_S_COMPLETE
;
734 _gssapi_wrap_size_arcfour(OM_uint32
*minor_status
,
735 const gsskrb5_ctx ctx
,
736 krb5_context context
,
739 OM_uint32 req_output_size
,
740 OM_uint32
*max_input_size
,
746 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
749 return GSS_S_FAILURE
;
752 ret
= max_wrap_length_arcfour(ctx
, crypto
,
753 req_output_size
, max_input_size
);
756 krb5_crypto_destroy(context
, crypto
);
757 return GSS_S_FAILURE
;
760 krb5_crypto_destroy(context
, crypto
);
762 return GSS_S_COMPLETE
;