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 "gsskrb5_locl.h"
36 #ifdef HEIM_WEAK_CRYPTO
40 (OM_uint32
* minor_status
,
41 const gsskrb5_ctx context_handle
,
43 const gss_buffer_t message_buffer
,
44 const gss_buffer_t token_buffer
,
45 gss_qop_t
* qop_state
,
52 u_char hash
[16], *seq
;
53 DES_key_schedule schedule
;
54 EVP_CIPHER_CTX des_ctx
;
61 p
= token_buffer
->value
;
62 ret
= _gsskrb5_verify_header (&p
,
69 if (memcmp(p
, "\x00\x00", 2) != 0)
72 if (memcmp (p
, "\xff\xff\xff\xff", 4) != 0)
78 md5
= EVP_MD_CTX_create();
79 EVP_DigestInit_ex(md5
, EVP_md5(), NULL
);
80 EVP_DigestUpdate(md5
, p
- 24, 8);
81 EVP_DigestUpdate(md5
, message_buffer
->value
, message_buffer
->length
);
82 EVP_DigestFinal_ex(md5
, hash
, NULL
);
83 EVP_MD_CTX_destroy(md5
);
85 memset (&zero
, 0, sizeof(zero
));
86 memcpy (&deskey
, key
->keyvalue
.data
, sizeof(deskey
));
88 DES_set_key_unchecked (&deskey
, &schedule
);
89 DES_cbc_cksum ((void *)hash
, (void *)hash
, sizeof(hash
),
91 if (ct_memcmp (p
- 8, hash
, 8) != 0) {
92 memset (deskey
, 0, sizeof(deskey
));
93 memset (&schedule
, 0, sizeof(schedule
));
97 /* verify sequence number */
99 HEIMDAL_MUTEX_lock(&context_handle
->ctx_id_mutex
);
103 EVP_CIPHER_CTX_init(&des_ctx
);
104 EVP_CipherInit_ex(&des_ctx
, EVP_des_cbc(), NULL
, key
->keyvalue
.data
, hash
, 0);
105 EVP_Cipher(&des_ctx
, p
, p
, 8);
106 EVP_CIPHER_CTX_cleanup(&des_ctx
);
108 memset (deskey
, 0, sizeof(deskey
));
109 memset (&schedule
, 0, sizeof(schedule
));
112 _gsskrb5_decode_om_uint32(seq
, &seq_number
);
114 if (context_handle
->more_flags
& LOCAL
)
115 cmp
= ct_memcmp(&seq
[4], "\xff\xff\xff\xff", 4);
117 cmp
= ct_memcmp(&seq
[4], "\x00\x00\x00\x00", 4);
120 HEIMDAL_MUTEX_unlock(&context_handle
->ctx_id_mutex
);
121 return GSS_S_BAD_MIC
;
124 ret
= _gssapi_msg_order_check(context_handle
->order
, seq_number
);
126 HEIMDAL_MUTEX_unlock(&context_handle
->ctx_id_mutex
);
130 HEIMDAL_MUTEX_unlock(&context_handle
->ctx_id_mutex
);
132 return GSS_S_COMPLETE
;
138 (OM_uint32
* minor_status
,
139 const gsskrb5_ctx context_handle
,
140 krb5_context context
,
141 const gss_buffer_t message_buffer
,
142 const gss_buffer_t token_buffer
,
143 gss_qop_t
* qop_state
,
159 p
= token_buffer
->value
;
160 ret
= _gsskrb5_verify_header (&p
,
161 token_buffer
->length
,
167 if (memcmp(p
, "\x04\x00", 2) != 0) /* SGN_ALG = HMAC SHA1 DES3-KD */
168 return GSS_S_BAD_SIG
;
170 if (memcmp (p
, "\xff\xff\xff\xff", 4) != 0)
171 return GSS_S_BAD_MIC
;
174 ret
= krb5_crypto_init(context
, key
,
175 ETYPE_DES3_CBC_NONE
, &crypto
);
178 return GSS_S_FAILURE
;
181 /* verify sequence number */
187 memcpy(ivec
, p
+ 8, 8);
189 ret
= krb5_decrypt_ivec (context
,
192 p
, 8, &seq_data
, ivec
);
195 krb5_crypto_destroy (context
, crypto
);
197 return GSS_S_FAILURE
;
202 if (seq_data
.length
!= 8) {
203 krb5_data_free (&seq_data
);
205 krb5_crypto_destroy (context
, crypto
);
206 return GSS_S_BAD_MIC
;
211 HEIMDAL_MUTEX_lock(&context_handle
->ctx_id_mutex
);
214 _gsskrb5_decode_om_uint32(seq
, &seq_number
);
216 if (context_handle
->more_flags
& LOCAL
)
217 cmp
= ct_memcmp(&seq
[4], "\xff\xff\xff\xff", 4);
219 cmp
= ct_memcmp(&seq
[4], "\x00\x00\x00\x00", 4);
221 krb5_data_free (&seq_data
);
223 krb5_crypto_destroy (context
, crypto
);
225 HEIMDAL_MUTEX_unlock(&context_handle
->ctx_id_mutex
);
226 return GSS_S_BAD_MIC
;
229 ret
= _gssapi_msg_order_check(context_handle
->order
, seq_number
);
231 krb5_crypto_destroy (context
, crypto
);
233 HEIMDAL_MUTEX_unlock(&context_handle
->ctx_id_mutex
);
237 /* verify checksum */
239 tmp
= malloc (message_buffer
->length
+ 8);
241 krb5_crypto_destroy (context
, crypto
);
242 HEIMDAL_MUTEX_unlock(&context_handle
->ctx_id_mutex
);
243 *minor_status
= ENOMEM
;
244 return GSS_S_FAILURE
;
247 memcpy (tmp
, p
- 8, 8);
248 memcpy (tmp
+ 8, message_buffer
->value
, message_buffer
->length
);
250 csum
.cksumtype
= CKSUMTYPE_HMAC_SHA1_DES3
;
251 csum
.checksum
.length
= 20;
252 csum
.checksum
.data
= p
+ 8;
254 krb5_crypto_destroy (context
, crypto
);
255 ret
= krb5_crypto_init(context
, key
,
256 ETYPE_DES3_CBC_SHA1
, &crypto
);
259 return GSS_S_FAILURE
;
262 ret
= krb5_verify_checksum (context
, crypto
,
264 tmp
, message_buffer
->length
+ 8,
268 krb5_crypto_destroy (context
, crypto
);
270 HEIMDAL_MUTEX_unlock(&context_handle
->ctx_id_mutex
);
271 return GSS_S_BAD_MIC
;
273 HEIMDAL_MUTEX_unlock(&context_handle
->ctx_id_mutex
);
275 krb5_crypto_destroy (context
, crypto
);
276 return GSS_S_COMPLETE
;
280 _gsskrb5_verify_mic_internal
281 (OM_uint32
* minor_status
,
282 const gsskrb5_ctx ctx
,
283 krb5_context context
,
284 const gss_buffer_t message_buffer
,
285 const gss_buffer_t token_buffer
,
286 gss_qop_t
* qop_state
,
293 if (ctx
->more_flags
& IS_CFX
)
294 return _gssapi_verify_mic_cfx (minor_status
, ctx
,
295 context
, message_buffer
, token_buffer
,
298 HEIMDAL_MUTEX_lock(&ctx
->ctx_id_mutex
);
299 ret
= _gsskrb5i_get_token_key(ctx
, context
, &key
);
300 HEIMDAL_MUTEX_unlock(&ctx
->ctx_id_mutex
);
303 return GSS_S_FAILURE
;
307 switch (key
->keytype
) {
308 case KRB5_ENCTYPE_DES_CBC_CRC
:
309 case KRB5_ENCTYPE_DES_CBC_MD4
:
310 case KRB5_ENCTYPE_DES_CBC_MD5
:
311 #ifdef HEIM_WEAK_CRYPTO
312 ret
= verify_mic_des (minor_status
, ctx
, context
,
313 message_buffer
, token_buffer
, qop_state
, key
,
319 case KRB5_ENCTYPE_DES3_CBC_MD5
:
320 case KRB5_ENCTYPE_DES3_CBC_SHA1
:
321 ret
= verify_mic_des3 (minor_status
, ctx
, context
,
322 message_buffer
, token_buffer
, qop_state
, key
,
325 case KRB5_ENCTYPE_ARCFOUR_HMAC_MD5
:
326 case KRB5_ENCTYPE_ARCFOUR_HMAC_MD5_56
:
327 ret
= _gssapi_verify_mic_arcfour (minor_status
, ctx
,
329 message_buffer
, token_buffer
,
330 qop_state
, key
, type
);
335 krb5_free_keyblock (context
, key
);
340 OM_uint32 GSSAPI_CALLCONV
342 (OM_uint32
* minor_status
,
343 gss_const_ctx_id_t context_handle
,
344 const gss_buffer_t message_buffer
,
345 const gss_buffer_t token_buffer
,
346 gss_qop_t
* qop_state
349 krb5_context context
;
352 GSSAPI_KRB5_INIT (&context
);
354 if (qop_state
!= NULL
)
355 *qop_state
= GSS_C_QOP_DEFAULT
;
357 ret
= _gsskrb5_verify_mic_internal(minor_status
,
358 (gsskrb5_ctx
)context_handle
,
360 message_buffer
, token_buffer
,
361 qop_state
, (void *)(intptr_t)"\x01\x01");