2 * Copyright (c) 1997, 1998 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_getsomekey(const gss_ctx_id_t context_handle
,
42 /* XXX this is ugly, and probably incorrect... */
44 krb5_auth_con_getlocalsubkey(gssapi_krb5_context
,
45 context_handle
->auth_context
,
48 krb5_auth_con_getremotesubkey(gssapi_krb5_context
,
49 context_handle
->auth_context
,
52 krb5_auth_con_getkey(gssapi_krb5_context
,
53 context_handle
->auth_context
,
57 memcpy(key
, skey
->keyvalue
.data
, sizeof(*key
));
58 krb5_free_keyblock(gssapi_krb5_context
, skey
);
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
,
74 u_char hash
[16], seq_data
[8];
75 des_key_schedule schedule
;
84 p
= input_message_buffer
->value
;
85 ret
= gssapi_krb5_verify_header (&p
,
86 input_message_buffer
->length
,
91 if (memcmp (p
, "\x00\x00", 2) != 0)
94 if (memcmp (p
, "\x00\x00", 2) == 0) {
96 } else if (memcmp (p
, "\xFF\xFF", 2) == 0) {
101 if(conf_state
!= NULL
)
102 *conf_state
= cstate
;
103 if (memcmp (p
, "\xff\xff", 2) != 0)
104 return GSS_S_DEFECTIVE_TOKEN
;
108 len
= p
- (u_char
*)input_message_buffer
->value
;
112 gss_krb5_getsomekey(context_handle
, &key
);
113 for (i
= 0; i
< sizeof(key
); ++i
)
115 des_set_key (&key
, schedule
);
116 memset (&zero
, 0, sizeof(zero
));
117 des_cbc_encrypt ((des_cblock
*)p
,
119 input_message_buffer
->length
- len
,
124 memset (key
, 0, sizeof(key
));
125 memset (schedule
, 0, sizeof(schedule
));
129 pad
= (u_char
*)input_message_buffer
->value
+ input_message_buffer
->length
- 1;
132 for (i
= padlength
; i
> 0 && *pad
== padlength
; i
--, pad
--)
135 return GSS_S_BAD_MIC
;
138 md5_update (&md5
, p
- 24, 8);
139 md5_update (&md5
, p
, input_message_buffer
->length
- len
);
140 md5_finito (&md5
, hash
);
142 memset (&zero
, 0, sizeof(zero
));
143 gss_krb5_getsomekey(context_handle
, &key
);
144 des_set_key (&key
, schedule
);
145 des_cbc_cksum ((des_cblock
*)hash
,
146 (des_cblock
*)hash
, sizeof(hash
), schedule
, &zero
);
147 if (memcmp (p
- 8, hash
, 8) != 0)
148 return GSS_S_BAD_MIC
;
150 /* verify sequence number */
152 krb5_auth_getremoteseqnumber (gssapi_krb5_context
,
153 context_handle
->auth_context
,
155 seq_data
[0] = (seq_number
>> 0) & 0xFF;
156 seq_data
[1] = (seq_number
>> 8) & 0xFF;
157 seq_data
[2] = (seq_number
>> 16) & 0xFF;
158 seq_data
[3] = (seq_number
>> 24) & 0xFF;
159 memset (seq_data
+ 4,
160 (context_handle
->more_flags
& LOCAL
) ? 0xFF : 0,
164 des_set_key (&key
, schedule
);
165 des_cbc_encrypt ((des_cblock
*)p
, (des_cblock
*)p
, 8,
166 schedule
, (des_cblock
*)hash
, DES_DECRYPT
);
168 memset (key
, 0, sizeof(key
));
169 memset (schedule
, 0, sizeof(schedule
));
171 if (memcmp (p
, seq_data
, 8) != 0) {
172 return GSS_S_BAD_MIC
;
175 krb5_auth_setremoteseqnumber (gssapi_krb5_context
,
176 context_handle
->auth_context
,
181 output_message_buffer
->length
= input_message_buffer
->length
182 - len
- 8 - padlength
;
183 output_message_buffer
->value
= malloc(output_message_buffer
->length
);
184 if(output_message_buffer
->length
!= 0 && output_message_buffer
->value
== NULL
)
185 return GSS_S_FAILURE
;
186 memcpy (output_message_buffer
->value
,
188 output_message_buffer
->length
);
189 return GSS_S_COMPLETE
;