x
[heimdal.git] / lib / gssapi / krb5 / verify_mic.c
blobc7e4f773290a0325585dd003667a3b9d26645bb4
1 /*
2 * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "gssapi_locl.h"
36 RCSID("$Id$");
38 static OM_uint32
39 verify_mic_des
40 (OM_uint32 * minor_status,
41 const gss_ctx_id_t context_handle,
42 const gss_buffer_t message_buffer,
43 const gss_buffer_t token_buffer,
44 gss_qop_t * qop_state,
45 krb5_keyblock *key
48 u_char *p;
49 MD5_CTX md5;
50 u_char hash[16], seq_data[8];
51 des_key_schedule schedule;
52 des_cblock zero;
53 des_cblock deskey;
54 int32_t seq_number;
55 OM_uint32 ret;
57 p = token_buffer->value;
58 ret = gssapi_krb5_verify_header (&p,
59 token_buffer->length,
60 "\x01\x01");
61 if (ret) {
62 *minor_status = 0;
63 return ret;
66 if (memcmp(p, "\x00\x00", 2) != 0)
67 return GSS_S_BAD_SIG;
68 p += 2;
69 if (memcmp (p, "\xff\xff\xff\xff", 4) != 0)
70 return GSS_S_BAD_MIC;
71 p += 4;
72 p += 16;
74 /* verify checksum */
75 MD5_Init (&md5);
76 MD5_Update (&md5, p - 24, 8);
77 MD5_Update (&md5, message_buffer->value,
78 message_buffer->length);
79 MD5_Final (hash, &md5);
81 memset (&zero, 0, sizeof(zero));
82 memcpy (&deskey, key->keyvalue.data, sizeof(deskey));
84 des_set_key (&deskey, schedule);
85 des_cbc_cksum ((void *)hash, (void *)hash, sizeof(hash),
86 schedule, &zero);
87 if (memcmp (p - 8, hash, 8) != 0) {
88 memset (deskey, 0, sizeof(deskey));
89 memset (schedule, 0, sizeof(schedule));
90 return GSS_S_BAD_MIC;
93 /* verify sequence number */
95 krb5_auth_getremoteseqnumber (gssapi_krb5_context,
96 context_handle->auth_context,
97 &seq_number);
98 seq_data[0] = (seq_number >> 0) & 0xFF;
99 seq_data[1] = (seq_number >> 8) & 0xFF;
100 seq_data[2] = (seq_number >> 16) & 0xFF;
101 seq_data[3] = (seq_number >> 24) & 0xFF;
102 memset (seq_data + 4,
103 (context_handle->more_flags & LOCAL) ? 0xFF : 0,
106 p -= 16;
107 des_set_key (&deskey, schedule);
108 des_cbc_encrypt ((void *)p, (void *)p, 8,
109 schedule, (des_cblock *)hash, DES_DECRYPT);
111 memset (deskey, 0, sizeof(deskey));
112 memset (schedule, 0, sizeof(schedule));
114 if (memcmp (p, seq_data, 8) != 0) {
115 return GSS_S_BAD_MIC;
118 krb5_auth_con_setremoteseqnumber (gssapi_krb5_context,
119 context_handle->auth_context,
120 ++seq_number);
122 return GSS_S_COMPLETE;
125 static OM_uint32
126 verify_mic_des3
127 (OM_uint32 * minor_status,
128 const gss_ctx_id_t context_handle,
129 const gss_buffer_t message_buffer,
130 const gss_buffer_t token_buffer,
131 gss_qop_t * qop_state,
132 krb5_keyblock *key
135 u_char *p;
136 u_char seq[8];
137 int32_t seq_number;
138 OM_uint32 ret;
139 krb5_crypto crypto;
140 krb5_data seq_data;
141 int cmp;
142 Checksum csum;
143 char *tmp;
145 p = token_buffer->value;
146 ret = gssapi_krb5_verify_header (&p,
147 token_buffer->length,
148 "\x01\x01");
149 if (ret) {
150 *minor_status = 0;
151 return ret;
154 if (memcmp(p, "\x04\x00", 2) != 0) /* SGN_ALG = HMAC SHA1 DES3-KD */
155 return GSS_S_BAD_SIG;
156 p += 2;
157 if (memcmp (p, "\xff\xff\xff\xff", 4) != 0)
158 return GSS_S_BAD_MIC;
159 p += 4;
161 ret = krb5_crypto_init(gssapi_krb5_context, key,
162 ETYPE_DES3_CBC_NONE, &crypto);
163 if (ret){
164 gssapi_krb5_set_error_string ();
165 *minor_status = ret;
166 return GSS_S_FAILURE;
169 /* verify sequence number */
171 ret = krb5_decrypt (gssapi_krb5_context,
172 crypto,
173 KRB5_KU_USAGE_SEQ,
174 p, 8, &seq_data);
175 if (ret) {
176 gssapi_krb5_set_error_string ();
177 krb5_crypto_destroy (gssapi_krb5_context, crypto);
178 *minor_status = ret;
179 return GSS_S_FAILURE;
182 if (seq_data.length != 8) {
183 krb5_crypto_destroy (gssapi_krb5_context, crypto);
184 krb5_data_free (&seq_data);
185 return GSS_S_BAD_MIC;
188 krb5_auth_getremoteseqnumber (gssapi_krb5_context,
189 context_handle->auth_context,
190 &seq_number);
191 seq[0] = (seq_number >> 0) & 0xFF;
192 seq[1] = (seq_number >> 8) & 0xFF;
193 seq[2] = (seq_number >> 16) & 0xFF;
194 seq[3] = (seq_number >> 24) & 0xFF;
195 memset (seq + 4,
196 (context_handle->more_flags & LOCAL) ? 0xFF : 0,
198 cmp = memcmp (seq, seq_data.data, seq_data.length);
199 krb5_data_free (&seq_data);
200 if (cmp != 0) {
201 krb5_crypto_destroy (gssapi_krb5_context, crypto);
202 return GSS_S_BAD_MIC;
205 /* verify checksum */
207 tmp = malloc (message_buffer->length + 8);
208 if (tmp == NULL) {
209 krb5_crypto_destroy (gssapi_krb5_context, crypto);
210 *minor_status = ENOMEM;
211 return GSS_S_FAILURE;
214 memcpy (tmp, p - 8, 8);
215 memcpy (tmp + 8, message_buffer->value, message_buffer->length);
217 csum.cksumtype = CKSUMTYPE_HMAC_SHA1_DES3;
218 csum.checksum.length = 20;
219 csum.checksum.data = p + 8;
221 ret = krb5_verify_checksum (gssapi_krb5_context, crypto,
222 KRB5_KU_USAGE_SIGN,
223 tmp, message_buffer->length + 8,
224 &csum);
225 free (tmp);
226 if (ret) {
227 gssapi_krb5_set_error_string ();
228 krb5_crypto_destroy (gssapi_krb5_context, crypto);
229 *minor_status = ret;
230 return GSS_S_BAD_MIC;
233 krb5_auth_con_setremoteseqnumber (gssapi_krb5_context,
234 context_handle->auth_context,
235 ++seq_number);
237 krb5_crypto_destroy (gssapi_krb5_context, crypto);
238 return GSS_S_COMPLETE;
241 OM_uint32
242 gss_verify_mic
243 (OM_uint32 * minor_status,
244 const gss_ctx_id_t context_handle,
245 const gss_buffer_t message_buffer,
246 const gss_buffer_t token_buffer,
247 gss_qop_t * qop_state
250 krb5_keyblock *key;
251 OM_uint32 ret;
252 krb5_keytype keytype;
254 if (qop_state != NULL)
255 *qop_state = GSS_C_QOP_DEFAULT;
256 ret = gss_krb5_get_remotekey(context_handle, &key);
257 if (ret) {
258 gssapi_krb5_set_error_string ();
259 *minor_status = ret;
260 return GSS_S_FAILURE;
262 krb5_enctype_to_keytype (gssapi_krb5_context, key->keytype, &keytype);
263 switch (keytype) {
264 case KEYTYPE_DES :
265 ret = verify_mic_des (minor_status, context_handle,
266 message_buffer, token_buffer, qop_state, key);
267 break;
268 case KEYTYPE_DES3 :
269 ret = verify_mic_des3 (minor_status, context_handle,
270 message_buffer, token_buffer, qop_state, key);
271 break;
272 default :
273 *minor_status = KRB5_PROG_ETYPE_NOSUPP;
274 ret = GSS_S_FAILURE;
275 break;
277 krb5_free_keyblock (gssapi_krb5_context, key);
278 return ret;