File: verify_mic.c

package info (click to toggle)
globus-gssapi-gsi 7.5-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,100 kB
  • ctags: 888
  • sloc: ansic: 13,335; sh: 8,562; makefile: 358
file content (353 lines) | stat: -rw-r--r-- 10,710 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 * Copyright 1999-2006 University of Chicago
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef GLOBUS_DONT_DOCUMENT_INTERNAL
/**
 * @file gss_verify_mic
 * @author Sam Lang, Sam Meder
 * 
 * $RCSfile: verify_mic.c,v $
 * $Revision: 1.13.16.1 $
 * $Date: 2010/02/19 21:18:50 $
 */
#endif

static char *rcsid = "$Id: verify_mic.c,v 1.13.16.1 2010/02/19 21:18:50 bester Exp $";

#ifndef GLOBUS_DONT_DOCUMENT_INTERNAL

/* borrowed from OpenSSL's s3_enc.c
 */
static unsigned char ssl3_pad_1[48]={
	0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
	0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
	0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
	0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
	0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
	0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36 };

#endif /* GLOBUS_DONT_DOCUMENT_INTERNAL */

#include "gssapi.h"
#include "globus_i_gsi_gss_utils.h"
#include "gssapi_openssl.h"
#include <string.h>

#include <time.h>

/**
 * @name Verify MIC
 * @ingroup globus_gsi_gssapi
 */
/* @{ */
/**
 * Check a MIC of the data
 *
 * @param minor_status
 * @param context_handle
 * @param message_buffer
 * @param token_buffer
 * @param qop_state
 * 
 * @return
 */
OM_uint32 
GSS_CALLCONV gss_verify_mic(
    OM_uint32 *                         minor_status,
    const gss_ctx_id_t                  context_handle,
    const gss_buffer_t                  message_buffer,
    const gss_buffer_t                  token_buffer,
    gss_qop_t *                         qop_state)
{
    gss_ctx_id_desc *                   context = context_handle;
    unsigned char *                     mac_sec;
    unsigned char *                     seq;
    unsigned char *                     token_value;
    EVP_MD_CTX                          md_ctx;
    const EVP_MD *                      hash;
    unsigned int                        md_size;
    int                                 npad;
    int                                 index;
    int                                 buffer_len;
    int                                 seqtest;
    time_t                              context_goodtill;
    unsigned char                       md[EVP_MAX_MD_SIZE];
    OM_uint32                           major_status = GSS_S_COMPLETE;
    OM_uint32                           local_minor_status;

    static char *                       _function_name_ = 
        "gss_verify_mic";
    GLOBUS_I_GSI_GSSAPI_DEBUG_ENTER;

    *minor_status = (OM_uint32) GLOBUS_SUCCESS;

    if (context_handle == GSS_C_NO_CONTEXT)
    {
        major_status =  GSS_S_NO_CONTEXT;
        GLOBUS_GSI_GSSAPI_ERROR_RESULT(
            minor_status,
            GLOBUS_GSI_GSSAPI_ERROR_BAD_ARGUMENT,
            (_GGSL("Invalid context handle (GSS_C_NO_CONTEXT) passed to function")));
        goto exit;
    }

    if (token_buffer == NULL)
    {
        major_status = GSS_S_DEFECTIVE_TOKEN;
        GLOBUS_GSI_GSSAPI_ERROR_RESULT(
            minor_status,
            GLOBUS_GSI_GSSAPI_ERROR_BAD_ARGUMENT,
            (_GGSL("Invalid token_buffer (NULL) passed to function")));
        goto exit;
    }

    if (token_buffer->value == NULL)
    {
        major_status = GSS_S_DEFECTIVE_TOKEN;
        GLOBUS_GSI_GSSAPI_ERROR_RESULT(
            minor_status,
            GLOBUS_GSI_GSSAPI_ERROR_BAD_ARGUMENT,
            (_GGSL("Invalid token_buffer (value param is NULL) passed to function")));
        goto exit;
    }

    /* lock the context mutex */    
    globus_mutex_lock(&context->mutex);
    
    if(context->ctx_flags & GSS_I_PROTECTION_FAIL_ON_CONTEXT_EXPIRATION)
    {
        time_t                          current_time;

        current_time = time(NULL);
        
        major_status = globus_i_gsi_gss_get_context_goodtill(
            &local_minor_status,
            context,
            &context_goodtill);

        if(GSS_ERROR(major_status))
        {
            GLOBUS_GSI_GSSAPI_ERROR_CHAIN_RESULT(
                minor_status, local_minor_status,
                GLOBUS_GSI_GSSAPI_ERROR_WITH_GSS_CONTEXT);
            goto exit;
        }

        if(current_time > context_goodtill)
        {
            major_status = GSS_S_CONTEXT_EXPIRED;
            GLOBUS_GSI_GSSAPI_ERROR_RESULT(
                minor_status,
                GLOBUS_GSI_GSSAPI_ERROR_EXPIRED_CREDENTIAL,
                (_GGSL("Credential expired: %s < %s"),
                 ctime(&context_goodtill), ctime(&current_time)));
            goto exit;
        }
    }

    /* DEBUG BLOCK */
    {
        int                             debug_index;
        unsigned char *                 debug_token_value;

        GLOBUS_I_GSI_GSSAPI_DEBUG_FPRINTF(
            2, (globus_i_gsi_gssapi_debug_fstream,
                "verify_mic: len=%u mic:",
                token_buffer->length));
        debug_token_value = token_buffer->value;

        for (debug_index = 0; 
             debug_index < token_buffer->length; 
             debug_index++)
        {
            GLOBUS_I_GSI_GSSAPI_DEBUG_FPRINTF(
                2, (globus_i_gsi_gssapi_debug_fstream,
                    "%2.2X", (*(debug_token_value++) & 0xff)));
        }

        GLOBUS_I_GSI_GSSAPI_DEBUG_PRINT(2, "\n");
    }

    mac_sec = context->gss_ssl->s3->read_mac_secret;
    seq = context->gss_ssl->s3->read_sequence;
    #if OPENSSL_VERSION_NUMBER < 0x10000000L
    hash = context->gss_ssl->read_hash;
    #else
    hash = context->gss_ssl->read_hash->digest;
    #endif

    md_size = EVP_MD_size(hash);
    if (token_buffer->length != (GSS_SSL_MESSAGE_DIGEST_PADDING + md_size))
    {
        major_status = GSS_S_DEFECTIVE_TOKEN;
        GLOBUS_GSI_GSSAPI_ERROR_RESULT(
            minor_status,
            GLOBUS_GSI_GSSAPI_ERROR_TOKEN_FAIL,
            (_GGSL("Token length of %d does not match "
             "size of message digest %d"),
             token_buffer->length, 
             (GSS_SSL_MESSAGE_DIGEST_PADDING + md_size)));
        goto exit;
    }
    
    token_value = ((unsigned char *) token_buffer->value) + 
                  GSS_SSL3_WRITE_SEQUENCE_SIZE;
    
    N2L(token_value, buffer_len);
    token_value += 4;

    if (message_buffer->length != buffer_len)
    {
        major_status = GSS_S_DEFECTIVE_TOKEN;
        GLOBUS_GSI_GSSAPI_ERROR_RESULT(
            minor_status,
            GLOBUS_GSI_GSSAPI_ERROR_TOKEN_FAIL,
            (_GGSL("Message buffer length of %d does not match "
             "expected length of %d in token"),
             message_buffer->length,
             buffer_len));
        goto exit;
    }

    npad = (48 / md_size) * md_size;
    
    EVP_DigestInit(&md_ctx, (EVP_MD *) hash);
    EVP_DigestUpdate(&md_ctx, mac_sec, md_size);
    EVP_DigestUpdate(&md_ctx, ssl3_pad_1, npad);
    EVP_DigestUpdate(&md_ctx, token_buffer->value, 
                     GSS_SSL_MESSAGE_DIGEST_PADDING);
    EVP_DigestUpdate(&md_ctx, message_buffer->value, 
                     message_buffer->length);
    EVP_DigestFinal(&md_ctx, md, NULL);
    
    if (memcmp(md, ((unsigned char *) token_buffer->value) + 
               GSS_SSL_MESSAGE_DIGEST_PADDING, md_size))
    {
        major_status = GSS_S_BAD_SIG;
        GLOBUS_GSI_GSSAPI_ERROR_RESULT(
            minor_status,
            GLOBUS_GSI_GSSAPI_ERROR_WITH_MIC,
            (_GGSL("Message digest and token's contents are not equal")));
        goto exit;
    }

    /*
     * Now test for consistance with the MIC
     */    
    token_value = token_buffer->value;
    
    seqtest = 0;
    for (index = 0; index < GSS_SSL3_WRITE_SEQUENCE_SIZE; index++)
    {   
        if ((seqtest = *token_value++ - seq[index]))
        {
            break;      
        }
    }
    
    if (seqtest > 0)
    {
        /* missed a token, reset the sequence number */
        token_value = token_buffer->value;
        for (index = 0; index < GSS_SSL3_WRITE_SEQUENCE_SIZE; index++)
        {
            seq[index] = *token_value++;
        }
        major_status = GSS_S_GAP_TOKEN;
        GLOBUS_GSI_GSSAPI_ERROR_RESULT(
            minor_status,
            GLOBUS_GSI_GSSAPI_ERROR_TOKEN_FAIL,
            (_GGSL("Missing write sequence at index: %d in the token"),
             index));
        goto exit;
    }
    
    if (seqtest < 0)
    {
        /* old token, may be replay too. */
        major_status = GSS_S_OLD_TOKEN;
        GLOBUS_GSI_GSSAPI_ERROR_RESULT(
            minor_status,
            GLOBUS_GSI_GSSAPI_ERROR_TOKEN_FAIL,
            (_GGSL("Token is too old")));
        goto exit;
    }

    /* got the correct seq number, increment the sequence */
    for (index = (GSS_SSL3_WRITE_SEQUENCE_SIZE - 1); index >= 0; index--)
    {
        if (++seq[index]) break;
    }

exit:

    /* unlock the context mutex */
    globus_mutex_unlock(&context->mutex);

    GLOBUS_I_GSI_GSSAPI_DEBUG_EXIT;
    return major_status;
} 
/* @} */

/**
 * @name Verify
 * @ingroup globus_gsi_gssapi
 */
/* @{ */
/**
 * Obsolete variant of gss_verify for V1 compatability 
 * Check a MIC of the date
 *
 * @param minor_status
 * @param context_handle
 * @param massage_buffer
 * @param token_buffer
 * @param qop_state
 * 
 * @return
 */
OM_uint32 
GSS_CALLCONV gss_verify(
    OM_uint32 *                         minor_status,
    gss_ctx_id_t                        context_handle,
    gss_buffer_t                        message_buffer,
    gss_buffer_t                        token_buffer,
    int *                               qop_state)
{
    OM_uint32                           major_status = GSS_S_COMPLETE;
    OM_uint32                           local_minor_status;
    static char *                       _function_name_ =
        "gss_verify";
    GLOBUS_I_GSI_GSSAPI_DEBUG_ENTER;

    *minor_status = (OM_uint32) GLOBUS_SUCCESS;

    major_status = gss_verify_mic(&local_minor_status,
                                  context_handle,
                                  message_buffer,
                                  token_buffer,
                                  (gss_qop_t *) qop_state);
    if(GSS_ERROR(major_status))
    {
        GLOBUS_GSI_GSSAPI_ERROR_CHAIN_RESULT(
            minor_status, local_minor_status,
            GLOBUS_GSI_GSSAPI_ERROR_WITH_MIC);
    }

    GLOBUS_I_GSI_GSSAPI_DEBUG_EXIT;
    return major_status;
}
/* @} */