2 * Copyright (c) 1997 - 2003 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 "gssapi_locl.h"
39 gss_krb5_get_remotekey(const gss_ctx_id_t context_handle
,
44 krb5_auth_con_getremotesubkey(gssapi_krb5_context
,
45 context_handle
->auth_context
,
48 krb5_auth_con_getlocalsubkey(gssapi_krb5_context
,
49 context_handle
->auth_context
,
52 krb5_auth_con_getkey(gssapi_krb5_context
,
53 context_handle
->auth_context
,
56 return GSS_KRB5_S_KG_NO_SUBKEY
; /* XXX */
63 (OM_uint32
* minor_status
,
64 const gss_ctx_id_t context_handle
,
65 const gss_buffer_t input_message_buffer
,
66 gss_buffer_t output_message_buffer
,
68 gss_qop_t
* qop_state
,
75 u_char hash
[16], seq_data
[8];
76 des_key_schedule schedule
;
85 p
= input_message_buffer
->value
;
86 ret
= gssapi_krb5_verify_header (&p
,
87 input_message_buffer
->length
,
92 if (memcmp (p
, "\x00\x00", 2) != 0)
95 if (memcmp (p
, "\x00\x00", 2) == 0) {
97 } else if (memcmp (p
, "\xFF\xFF", 2) == 0) {
100 return GSS_S_BAD_MIC
;
102 if(conf_state
!= NULL
)
103 *conf_state
= cstate
;
104 if (memcmp (p
, "\xff\xff", 2) != 0)
105 return GSS_S_DEFECTIVE_TOKEN
;
109 len
= p
- (u_char
*)input_message_buffer
->value
;
113 memcpy (&deskey
, key
->keyvalue
.data
, sizeof(deskey
));
115 for (i
= 0; i
< sizeof(deskey
); ++i
)
117 des_set_key (&deskey
, schedule
);
118 memset (&zero
, 0, sizeof(zero
));
119 des_cbc_encrypt ((void *)p
,
121 input_message_buffer
->length
- len
,
126 memset (deskey
, 0, sizeof(deskey
));
127 memset (schedule
, 0, sizeof(schedule
));
131 pad
= (u_char
*)input_message_buffer
->value
+ input_message_buffer
->length
- 1;
134 for (i
= padlength
; i
> 0 && *pad
== padlength
; i
--, pad
--)
137 return GSS_S_BAD_MIC
;
140 MD5_Update (&md5
, p
- 24, 8);
141 MD5_Update (&md5
, p
, input_message_buffer
->length
- len
);
142 MD5_Final (hash
, &md5
);
144 memset (&zero
, 0, sizeof(zero
));
145 memcpy (&deskey
, key
->keyvalue
.data
, sizeof(deskey
));
146 des_set_key (&deskey
, schedule
);
147 des_cbc_cksum ((void *)hash
, (void *)hash
, sizeof(hash
),
149 if (memcmp (p
- 8, hash
, 8) != 0)
150 return GSS_S_BAD_MIC
;
152 /* verify sequence number */
154 krb5_auth_getremoteseqnumber (gssapi_krb5_context
,
155 context_handle
->auth_context
,
157 seq_data
[0] = (seq_number
>> 0) & 0xFF;
158 seq_data
[1] = (seq_number
>> 8) & 0xFF;
159 seq_data
[2] = (seq_number
>> 16) & 0xFF;
160 seq_data
[3] = (seq_number
>> 24) & 0xFF;
161 memset (seq_data
+ 4,
162 (context_handle
->more_flags
& LOCAL
) ? 0xFF : 0,
166 des_set_key (&deskey
, schedule
);
167 des_cbc_encrypt ((void *)p
, (void *)p
, 8,
168 schedule
, (des_cblock
*)hash
, DES_DECRYPT
);
170 memset (deskey
, 0, sizeof(deskey
));
171 memset (schedule
, 0, sizeof(schedule
));
173 if (memcmp (p
, seq_data
, 8) != 0) {
174 return GSS_S_BAD_MIC
;
177 krb5_auth_con_setremoteseqnumber (gssapi_krb5_context
,
178 context_handle
->auth_context
,
183 output_message_buffer
->length
= input_message_buffer
->length
184 - len
- padlength
- 8;
185 output_message_buffer
->value
= malloc(output_message_buffer
->length
);
186 if(output_message_buffer
->length
!= 0 && output_message_buffer
->value
== NULL
)
187 return GSS_S_FAILURE
;
188 memcpy (output_message_buffer
->value
,
190 output_message_buffer
->length
);
191 return GSS_S_COMPLETE
;
196 (OM_uint32
* minor_status
,
197 const gss_ctx_id_t context_handle
,
198 const gss_buffer_t input_message_buffer
,
199 gss_buffer_t output_message_buffer
,
201 gss_qop_t
* qop_state
,
219 p
= input_message_buffer
->value
;
220 ret
= gssapi_krb5_verify_header (&p
,
221 input_message_buffer
->length
,
226 if (memcmp (p
, "\x04\x00", 2) != 0) /* HMAC SHA1 DES3_KD */
227 return GSS_S_BAD_SIG
;
229 if (memcmp (p
, "\x02\x00", 2) == 0) {
231 } else if (memcmp (p
, "\xff\xff", 2) == 0) {
234 return GSS_S_BAD_MIC
;
236 if(conf_state
!= NULL
)
237 *conf_state
= cstate
;
238 if (memcmp (p
, "\xff\xff", 2) != 0)
239 return GSS_S_DEFECTIVE_TOKEN
;
243 len
= p
- (u_char
*)input_message_buffer
->value
;
249 ret
= krb5_crypto_init(gssapi_krb5_context
, key
,
250 ETYPE_DES3_CBC_NONE
, &crypto
);
252 gssapi_krb5_set_error_string ();
254 return GSS_S_FAILURE
;
256 ret
= krb5_decrypt(gssapi_krb5_context
, crypto
, KRB5_KU_USAGE_SEAL
,
257 p
, input_message_buffer
->length
- len
, &tmp
);
258 krb5_crypto_destroy(gssapi_krb5_context
, crypto
);
260 gssapi_krb5_set_error_string ();
262 return GSS_S_FAILURE
;
264 assert (tmp
.length
== input_message_buffer
->length
- len
);
266 memcpy (p
, tmp
.data
, tmp
.length
);
267 krb5_data_free(&tmp
);
271 pad
= (u_char
*)input_message_buffer
->value
+ input_message_buffer
->length
- 1;
274 for (i
= padlength
; i
> 0 && *pad
== padlength
; i
--, pad
--)
277 return GSS_S_BAD_MIC
;
279 /* verify sequence number */
281 krb5_auth_getremoteseqnumber (gssapi_krb5_context
,
282 context_handle
->auth_context
,
284 seq
[0] = (seq_number
>> 0) & 0xFF;
285 seq
[1] = (seq_number
>> 8) & 0xFF;
286 seq
[2] = (seq_number
>> 16) & 0xFF;
287 seq
[3] = (seq_number
>> 24) & 0xFF;
289 (context_handle
->more_flags
& LOCAL
) ? 0xFF : 0,
294 ret
= krb5_crypto_init(gssapi_krb5_context
, key
,
295 ETYPE_DES3_CBC_NONE
, &crypto
);
297 gssapi_krb5_set_error_string ();
299 return GSS_S_FAILURE
;
304 memcpy(&ivec
, p
+ 8, 8);
305 ret
= krb5_decrypt_ivec (gssapi_krb5_context
,
311 krb5_crypto_destroy (gssapi_krb5_context
, crypto
);
313 gssapi_krb5_set_error_string ();
315 return GSS_S_FAILURE
;
317 if (seq_data
.length
!= 8) {
318 krb5_data_free (&seq_data
);
319 return GSS_S_BAD_MIC
;
322 cmp
= memcmp (seq
, seq_data
.data
, seq_data
.length
);
323 krb5_data_free (&seq_data
);
325 return GSS_S_BAD_MIC
;
328 krb5_auth_con_setremoteseqnumber (gssapi_krb5_context
,
329 context_handle
->auth_context
,
332 /* verify checksum */
334 memcpy (cksum
, p
+ 8, 20);
336 memcpy (p
+ 20, p
- 8, 8);
338 csum
.cksumtype
= CKSUMTYPE_HMAC_SHA1_DES3
;
339 csum
.checksum
.length
= 20;
340 csum
.checksum
.data
= cksum
;
342 ret
= krb5_crypto_init(gssapi_krb5_context
, key
, 0, &crypto
);
344 gssapi_krb5_set_error_string ();
346 return GSS_S_FAILURE
;
349 ret
= krb5_verify_checksum (gssapi_krb5_context
, crypto
,
352 input_message_buffer
->length
- len
+ 8,
354 krb5_crypto_destroy (gssapi_krb5_context
, crypto
);
356 gssapi_krb5_set_error_string ();
358 return GSS_S_FAILURE
;
363 output_message_buffer
->length
= input_message_buffer
->length
364 - len
- padlength
- 8;
365 output_message_buffer
->value
= malloc(output_message_buffer
->length
);
366 if(output_message_buffer
->length
!= 0 && output_message_buffer
->value
== NULL
)
367 return GSS_S_FAILURE
;
368 memcpy (output_message_buffer
->value
,
370 output_message_buffer
->length
);
371 return GSS_S_COMPLETE
;
375 (OM_uint32
* minor_status
,
376 const gss_ctx_id_t context_handle
,
377 const gss_buffer_t input_message_buffer
,
378 gss_buffer_t output_message_buffer
,
380 gss_qop_t
* qop_state
385 krb5_keytype keytype
;
387 if (qop_state
!= NULL
)
388 *qop_state
= GSS_C_QOP_DEFAULT
;
389 ret
= gss_krb5_get_remotekey(context_handle
, &key
);
391 gssapi_krb5_set_error_string ();
393 return GSS_S_FAILURE
;
395 krb5_enctype_to_keytype (gssapi_krb5_context
, key
->keytype
, &keytype
);
401 ret
= unwrap_des (minor_status
, context_handle
,
402 input_message_buffer
, output_message_buffer
,
403 conf_state
, qop_state
, key
);
406 ret
= unwrap_des3 (minor_status
, context_handle
,
407 input_message_buffer
, output_message_buffer
,
408 conf_state
, qop_state
, key
);
410 case KEYTYPE_ARCFOUR
:
411 ret
= _gssapi_unwrap_arcfour (minor_status
, context_handle
,
412 input_message_buffer
, output_message_buffer
,
413 conf_state
, qop_state
, key
);
416 *minor_status
= KRB5_PROG_ETYPE_NOSUPP
;
420 krb5_free_keyblock (gssapi_krb5_context
, key
);