1 /* $NetBSD: arcfour.c,v 1.1.1.2 2014/04/24 12:45:29 pettai Exp $ */
4 * Copyright (c) 2003 - 2006 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "gsskrb5_locl.h"
39 * Implements draft-brezak-win2k-krb-rc4-hmac-04.txt
41 * The arcfour message have the following formats:
61 * WRAP in DCE-style have a fixed size header, the oid and length over
62 * the WRAP header is a total of
63 * GSS_ARCFOUR_WRAP_TOKEN_DCE_DER_HEADER_SIZE +
64 * GSS_ARCFOUR_WRAP_TOKEN_SIZE byte (ie total of 45 bytes overhead,
65 * remember the 2 bytes from APPL [0] SEQ).
68 #define GSS_ARCFOUR_WRAP_TOKEN_SIZE 32
69 #define GSS_ARCFOUR_WRAP_TOKEN_DCE_DER_HEADER_SIZE 13
72 static krb5_error_code
73 arcfour_mic_key(krb5_context context
, krb5_keyblock
*key
,
74 void *cksum_data
, size_t cksum_size
,
75 void *key6_data
, size_t key6_size
)
88 cksum_k5
.checksum
.data
= k5_data
;
89 cksum_k5
.checksum
.length
= sizeof(k5_data
);
91 if (key
->keytype
== ENCTYPE_ARCFOUR_HMAC_MD5_56
) {
92 char L40
[14] = "fortybits";
94 memcpy(L40
+ 10, T
, sizeof(T
));
95 ret
= krb5_hmac(context
, CKSUMTYPE_RSA_MD5
,
96 L40
, 14, 0, key
, &cksum_k5
);
97 memset(&k5_data
[7], 0xAB, 9);
99 ret
= krb5_hmac(context
, CKSUMTYPE_RSA_MD5
,
100 T
, 4, 0, key
, &cksum_k5
);
105 key5
.keytype
= ENCTYPE_ARCFOUR_HMAC_MD5
;
106 key5
.keyvalue
= cksum_k5
.checksum
;
108 cksum_k6
.checksum
.data
= key6_data
;
109 cksum_k6
.checksum
.length
= key6_size
;
111 return krb5_hmac(context
, CKSUMTYPE_RSA_MD5
,
112 cksum_data
, cksum_size
, 0, &key5
, &cksum_k6
);
116 static krb5_error_code
117 arcfour_mic_cksum(krb5_context context
,
118 krb5_keyblock
*key
, unsigned usage
,
119 u_char
*sgn_cksum
, size_t sgn_cksum_sz
,
120 const u_char
*v1
, size_t l1
,
121 const void *v2
, size_t l2
,
122 const void *v3
, size_t l3
)
130 assert(sgn_cksum_sz
== 8);
139 memcpy(ptr
+ l1
, v2
, l2
);
140 memcpy(ptr
+ l1
+ l2
, v3
, l3
);
142 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
148 ret
= krb5_create_checksum(context
,
156 memcpy(sgn_cksum
, CKSUM
.checksum
.data
, sgn_cksum_sz
);
157 free_Checksum(&CKSUM
);
159 krb5_crypto_destroy(context
, crypto
);
166 _gssapi_get_mic_arcfour(OM_uint32
* minor_status
,
167 const gsskrb5_ctx context_handle
,
168 krb5_context context
,
170 const gss_buffer_t message_buffer
,
171 gss_buffer_t message_token
,
176 size_t len
, total_len
;
177 u_char k6_data
[16], *p0
, *p
;
178 EVP_CIPHER_CTX rc4_key
;
180 _gsskrb5_encap_length (22, &len
, &total_len
, GSS_KRB5_MECHANISM
);
182 message_token
->length
= total_len
;
183 message_token
->value
= malloc (total_len
);
184 if (message_token
->value
== NULL
) {
185 *minor_status
= ENOMEM
;
186 return GSS_S_FAILURE
;
189 p0
= _gssapi_make_mech_header(message_token
->value
,
194 *p
++ = 0x01; /* TOK_ID */
196 *p
++ = 0x11; /* SGN_ALG */
198 *p
++ = 0xff; /* Filler */
205 ret
= arcfour_mic_cksum(context
,
206 key
, KRB5_KU_USAGE_SIGN
,
207 p0
+ 16, 8, /* SGN_CKSUM */
208 p0
, 8, /* TOK_ID, SGN_ALG, Filer */
209 message_buffer
->value
, message_buffer
->length
,
212 _gsskrb5_release_buffer(minor_status
, message_token
);
214 return GSS_S_FAILURE
;
217 ret
= arcfour_mic_key(context
, key
,
218 p0
+ 16, 8, /* SGN_CKSUM */
219 k6_data
, sizeof(k6_data
));
221 _gsskrb5_release_buffer(minor_status
, message_token
);
223 return GSS_S_FAILURE
;
226 HEIMDAL_MUTEX_lock(&context_handle
->ctx_id_mutex
);
227 krb5_auth_con_getlocalseqnumber (context
,
228 context_handle
->auth_context
,
230 p
= p0
+ 8; /* SND_SEQ */
231 _gsskrb5_encode_be_om_uint32(seq_number
, p
);
233 krb5_auth_con_setlocalseqnumber (context
,
234 context_handle
->auth_context
,
236 HEIMDAL_MUTEX_unlock(&context_handle
->ctx_id_mutex
);
238 memset (p
+ 4, (context_handle
->more_flags
& LOCAL
) ? 0 : 0xff, 4);
240 EVP_CIPHER_CTX_init(&rc4_key
);
241 EVP_CipherInit_ex(&rc4_key
, EVP_rc4(), NULL
, k6_data
, NULL
, 1);
242 EVP_Cipher(&rc4_key
, p
, p
, 8);
243 EVP_CIPHER_CTX_cleanup(&rc4_key
);
245 memset(k6_data
, 0, sizeof(k6_data
));
248 return GSS_S_COMPLETE
;
253 _gssapi_verify_mic_arcfour(OM_uint32
* minor_status
,
254 const gsskrb5_ctx context_handle
,
255 krb5_context context
,
256 const gss_buffer_t message_buffer
,
257 const gss_buffer_t token_buffer
,
258 gss_qop_t
* qop_state
,
265 u_char SND_SEQ
[8], cksum_data
[8], *p
;
272 p
= token_buffer
->value
;
273 omret
= _gsskrb5_verify_header (&p
,
274 token_buffer
->length
,
280 if (memcmp(p
, "\x11\x00", 2) != 0) /* SGN_ALG = HMAC MD5 ARCFOUR */
281 return GSS_S_BAD_SIG
;
283 if (memcmp (p
, "\xff\xff\xff\xff", 4) != 0)
284 return GSS_S_BAD_MIC
;
287 ret
= arcfour_mic_cksum(context
,
288 key
, KRB5_KU_USAGE_SIGN
,
289 cksum_data
, sizeof(cksum_data
),
291 message_buffer
->value
, message_buffer
->length
,
295 return GSS_S_FAILURE
;
298 ret
= arcfour_mic_key(context
, key
,
299 cksum_data
, sizeof(cksum_data
),
300 k6_data
, sizeof(k6_data
));
303 return GSS_S_FAILURE
;
306 cmp
= ct_memcmp(cksum_data
, p
+ 8, 8);
309 return GSS_S_BAD_MIC
;
313 EVP_CIPHER_CTX rc4_key
;
315 EVP_CIPHER_CTX_init(&rc4_key
);
316 EVP_CipherInit_ex(&rc4_key
, EVP_rc4(), NULL
, (void *)k6_data
, NULL
, 0);
317 EVP_Cipher(&rc4_key
, SND_SEQ
, p
, 8);
318 EVP_CIPHER_CTX_cleanup(&rc4_key
);
320 memset(k6_data
, 0, sizeof(k6_data
));
323 _gsskrb5_decode_be_om_uint32(SND_SEQ
, &seq_number
);
325 if (context_handle
->more_flags
& LOCAL
)
326 cmp
= memcmp(&SND_SEQ
[4], "\xff\xff\xff\xff", 4);
328 cmp
= memcmp(&SND_SEQ
[4], "\x00\x00\x00\x00", 4);
330 memset(SND_SEQ
, 0, sizeof(SND_SEQ
));
333 return GSS_S_BAD_MIC
;
336 HEIMDAL_MUTEX_lock(&context_handle
->ctx_id_mutex
);
337 omret
= _gssapi_msg_order_check(context_handle
->order
, seq_number
);
338 HEIMDAL_MUTEX_unlock(&context_handle
->ctx_id_mutex
);
343 return GSS_S_COMPLETE
;
347 _gssapi_wrap_arcfour(OM_uint32
* minor_status
,
348 const gsskrb5_ctx context_handle
,
349 krb5_context context
,
352 const gss_buffer_t input_message_buffer
,
354 gss_buffer_t output_message_buffer
,
357 u_char Klocaldata
[16], k6_data
[16], *p
, *p0
;
358 size_t len
, total_len
, datalen
;
359 krb5_keyblock Klocal
;
366 datalen
= input_message_buffer
->length
;
368 if (IS_DCE_STYLE(context_handle
)) {
369 len
= GSS_ARCFOUR_WRAP_TOKEN_SIZE
;
370 _gssapi_encap_length(len
, &len
, &total_len
, GSS_KRB5_MECHANISM
);
371 total_len
+= datalen
;
373 datalen
+= 1; /* padding */
374 len
= datalen
+ GSS_ARCFOUR_WRAP_TOKEN_SIZE
;
375 _gssapi_encap_length(len
, &len
, &total_len
, GSS_KRB5_MECHANISM
);
378 output_message_buffer
->length
= total_len
;
379 output_message_buffer
->value
= malloc (total_len
);
380 if (output_message_buffer
->value
== NULL
) {
381 *minor_status
= ENOMEM
;
382 return GSS_S_FAILURE
;
385 p0
= _gssapi_make_mech_header(output_message_buffer
->value
,
390 *p
++ = 0x02; /* TOK_ID */
392 *p
++ = 0x11; /* SGN_ALG */
395 *p
++ = 0x10; /* SEAL_ALG */
398 *p
++ = 0xff; /* SEAL_ALG */
401 *p
++ = 0xff; /* Filler */
406 HEIMDAL_MUTEX_lock(&context_handle
->ctx_id_mutex
);
407 krb5_auth_con_getlocalseqnumber (context
,
408 context_handle
->auth_context
,
411 _gsskrb5_encode_be_om_uint32(seq_number
, p0
+ 8);
413 krb5_auth_con_setlocalseqnumber (context
,
414 context_handle
->auth_context
,
416 HEIMDAL_MUTEX_unlock(&context_handle
->ctx_id_mutex
);
419 (context_handle
->more_flags
& LOCAL
) ? 0 : 0xff,
422 krb5_generate_random_block(p0
+ 24, 8); /* fill in Confounder */
424 /* p points to data */
425 p
= p0
+ GSS_ARCFOUR_WRAP_TOKEN_SIZE
;
426 memcpy(p
, input_message_buffer
->value
, input_message_buffer
->length
);
428 if (!IS_DCE_STYLE(context_handle
))
429 p
[input_message_buffer
->length
] = 1; /* padding */
431 ret
= arcfour_mic_cksum(context
,
432 key
, KRB5_KU_USAGE_SEAL
,
433 p0
+ 16, 8, /* SGN_CKSUM */
434 p0
, 8, /* TOK_ID, SGN_ALG, SEAL_ALG, Filler */
435 p0
+ 24, 8, /* Confounder */
436 p0
+ GSS_ARCFOUR_WRAP_TOKEN_SIZE
,
440 _gsskrb5_release_buffer(minor_status
, output_message_buffer
);
441 return GSS_S_FAILURE
;
447 Klocal
.keytype
= key
->keytype
;
448 Klocal
.keyvalue
.data
= Klocaldata
;
449 Klocal
.keyvalue
.length
= sizeof(Klocaldata
);
451 for (i
= 0; i
< 16; i
++)
452 Klocaldata
[i
] = ((u_char
*)key
->keyvalue
.data
)[i
] ^ 0xF0;
454 ret
= arcfour_mic_key(context
, &Klocal
,
455 p0
+ 8, 4, /* SND_SEQ */
456 k6_data
, sizeof(k6_data
));
457 memset(Klocaldata
, 0, sizeof(Klocaldata
));
459 _gsskrb5_release_buffer(minor_status
, output_message_buffer
);
461 return GSS_S_FAILURE
;
466 EVP_CIPHER_CTX rc4_key
;
468 EVP_CIPHER_CTX_init(&rc4_key
);
469 EVP_CipherInit_ex(&rc4_key
, EVP_rc4(), NULL
, k6_data
, NULL
, 1);
470 EVP_Cipher(&rc4_key
, p0
+ 24, p0
+ 24, 8 + datalen
);
471 EVP_CIPHER_CTX_cleanup(&rc4_key
);
473 memset(k6_data
, 0, sizeof(k6_data
));
475 ret
= arcfour_mic_key(context
, key
,
476 p0
+ 16, 8, /* SGN_CKSUM */
477 k6_data
, sizeof(k6_data
));
479 _gsskrb5_release_buffer(minor_status
, output_message_buffer
);
481 return GSS_S_FAILURE
;
485 EVP_CIPHER_CTX rc4_key
;
487 EVP_CIPHER_CTX_init(&rc4_key
);
488 EVP_CipherInit_ex(&rc4_key
, EVP_rc4(), NULL
, k6_data
, NULL
, 1);
489 EVP_Cipher(&rc4_key
, p0
+ 8, p0
+ 8 /* SND_SEQ */, 8);
490 EVP_CIPHER_CTX_cleanup(&rc4_key
);
491 memset(k6_data
, 0, sizeof(k6_data
));
495 *conf_state
= conf_req_flag
;
498 return GSS_S_COMPLETE
;
501 OM_uint32
_gssapi_unwrap_arcfour(OM_uint32
*minor_status
,
502 const gsskrb5_ctx context_handle
,
503 krb5_context context
,
504 const gss_buffer_t input_message_buffer
,
505 gss_buffer_t output_message_buffer
,
507 gss_qop_t
*qop_state
,
510 u_char Klocaldata
[16];
511 krb5_keyblock Klocal
;
516 u_char k6_data
[16], SND_SEQ
[8], Confounder
[8];
517 u_char cksum_data
[8];
521 size_t padlen
= 0, len
;
528 p0
= input_message_buffer
->value
;
530 if (IS_DCE_STYLE(context_handle
)) {
531 len
= GSS_ARCFOUR_WRAP_TOKEN_SIZE
+
532 GSS_ARCFOUR_WRAP_TOKEN_DCE_DER_HEADER_SIZE
;
533 if (input_message_buffer
->length
< len
)
534 return GSS_S_BAD_MECH
;
536 len
= input_message_buffer
->length
;
539 omret
= _gssapi_verify_mech_header(&p0
,
545 /* length of mech header */
546 len
= (p0
- (u_char
*)input_message_buffer
->value
) +
547 GSS_ARCFOUR_WRAP_TOKEN_SIZE
;
549 if (len
> input_message_buffer
->length
)
550 return GSS_S_BAD_MECH
;
553 datalen
= input_message_buffer
->length
- len
;
557 if (memcmp(p
, "\x02\x01", 2) != 0)
558 return GSS_S_BAD_SIG
;
560 if (memcmp(p
, "\x11\x00", 2) != 0) /* SGN_ALG = HMAC MD5 ARCFOUR */
561 return GSS_S_BAD_SIG
;
564 if (memcmp (p
, "\x10\x00", 2) == 0)
566 else if (memcmp (p
, "\xff\xff", 2) == 0)
569 return GSS_S_BAD_SIG
;
572 if (memcmp (p
, "\xff\xff", 2) != 0)
573 return GSS_S_BAD_MIC
;
576 ret
= arcfour_mic_key(context
, key
,
577 p0
+ 16, 8, /* SGN_CKSUM */
578 k6_data
, sizeof(k6_data
));
581 return GSS_S_FAILURE
;
585 EVP_CIPHER_CTX rc4_key
;
587 EVP_CIPHER_CTX_init(&rc4_key
);
588 EVP_CipherInit_ex(&rc4_key
, EVP_rc4(), NULL
, k6_data
, NULL
, 1);
589 EVP_Cipher(&rc4_key
, SND_SEQ
, p0
+ 8, 8);
590 EVP_CIPHER_CTX_cleanup(&rc4_key
);
591 memset(k6_data
, 0, sizeof(k6_data
));
594 _gsskrb5_decode_be_om_uint32(SND_SEQ
, &seq_number
);
596 if (context_handle
->more_flags
& LOCAL
)
597 cmp
= memcmp(&SND_SEQ
[4], "\xff\xff\xff\xff", 4);
599 cmp
= memcmp(&SND_SEQ
[4], "\x00\x00\x00\x00", 4);
603 return GSS_S_BAD_MIC
;
609 Klocal
.keytype
= key
->keytype
;
610 Klocal
.keyvalue
.data
= Klocaldata
;
611 Klocal
.keyvalue
.length
= sizeof(Klocaldata
);
613 for (i
= 0; i
< 16; i
++)
614 Klocaldata
[i
] = ((u_char
*)key
->keyvalue
.data
)[i
] ^ 0xF0;
616 ret
= arcfour_mic_key(context
, &Klocal
,
618 k6_data
, sizeof(k6_data
));
619 memset(Klocaldata
, 0, sizeof(Klocaldata
));
622 return GSS_S_FAILURE
;
625 output_message_buffer
->value
= malloc(datalen
);
626 if (output_message_buffer
->value
== NULL
) {
627 *minor_status
= ENOMEM
;
628 return GSS_S_FAILURE
;
630 output_message_buffer
->length
= datalen
;
633 EVP_CIPHER_CTX rc4_key
;
635 EVP_CIPHER_CTX_init(&rc4_key
);
636 EVP_CipherInit_ex(&rc4_key
, EVP_rc4(), NULL
, k6_data
, NULL
, 1);
637 EVP_Cipher(&rc4_key
, Confounder
, p0
+ 24, 8);
638 EVP_Cipher(&rc4_key
, output_message_buffer
->value
, p0
+ GSS_ARCFOUR_WRAP_TOKEN_SIZE
, datalen
);
639 EVP_CIPHER_CTX_cleanup(&rc4_key
);
641 memcpy(Confounder
, p0
+ 24, 8); /* Confounder */
642 memcpy(output_message_buffer
->value
,
643 p0
+ GSS_ARCFOUR_WRAP_TOKEN_SIZE
,
646 memset(k6_data
, 0, sizeof(k6_data
));
648 if (!IS_DCE_STYLE(context_handle
)) {
649 ret
= _gssapi_verify_pad(output_message_buffer
, datalen
, &padlen
);
651 _gsskrb5_release_buffer(minor_status
, output_message_buffer
);
655 output_message_buffer
->length
-= padlen
;
658 ret
= arcfour_mic_cksum(context
,
659 key
, KRB5_KU_USAGE_SEAL
,
660 cksum_data
, sizeof(cksum_data
),
662 Confounder
, sizeof(Confounder
),
663 output_message_buffer
->value
,
664 output_message_buffer
->length
+ padlen
);
666 _gsskrb5_release_buffer(minor_status
, output_message_buffer
);
668 return GSS_S_FAILURE
;
671 cmp
= ct_memcmp(cksum_data
, p0
+ 16, 8); /* SGN_CKSUM */
673 _gsskrb5_release_buffer(minor_status
, output_message_buffer
);
675 return GSS_S_BAD_MIC
;
678 HEIMDAL_MUTEX_lock(&context_handle
->ctx_id_mutex
);
679 omret
= _gssapi_msg_order_check(context_handle
->order
, seq_number
);
680 HEIMDAL_MUTEX_unlock(&context_handle
->ctx_id_mutex
);
685 *conf_state
= conf_flag
;
688 return GSS_S_COMPLETE
;
692 max_wrap_length_arcfour(const gsskrb5_ctx ctx
,
695 OM_uint32
*max_input_size
)
698 * if GSS_C_DCE_STYLE is in use:
699 * - we only need to encapsulate the WRAP token
700 * However, since this is a fixed since, we just
702 if (IS_DCE_STYLE(ctx
)) {
703 size_t len
, total_len
;
705 len
= GSS_ARCFOUR_WRAP_TOKEN_SIZE
;
706 _gssapi_encap_length(len
, &len
, &total_len
, GSS_KRB5_MECHANISM
);
708 if (input_length
< len
)
711 *max_input_size
= input_length
- len
;
714 size_t extrasize
= GSS_ARCFOUR_WRAP_TOKEN_SIZE
;
715 size_t blocksize
= 8;
716 size_t len
, total_len
;
718 len
= 8 + input_length
+ blocksize
+ extrasize
;
720 _gsskrb5_encap_length(len
, &len
, &total_len
, GSS_KRB5_MECHANISM
);
722 total_len
-= input_length
; /* token length */
723 if (total_len
< input_length
) {
724 *max_input_size
= (input_length
- total_len
);
725 (*max_input_size
) &= (~(OM_uint32
)(blocksize
- 1));
731 return GSS_S_COMPLETE
;
735 _gssapi_wrap_size_arcfour(OM_uint32
*minor_status
,
736 const gsskrb5_ctx ctx
,
737 krb5_context context
,
740 OM_uint32 req_output_size
,
741 OM_uint32
*max_input_size
,
747 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
750 return GSS_S_FAILURE
;
753 ret
= max_wrap_length_arcfour(ctx
, crypto
,
754 req_output_size
, max_input_size
);
757 krb5_crypto_destroy(context
, crypto
);
758 return GSS_S_FAILURE
;
761 krb5_crypto_destroy(context
, crypto
);
763 return GSS_S_COMPLETE
;