~ubuntu-branches/ubuntu/trusty/globus-gss-assist/trusty

« back to all changes in this revision

Viewing changes to .pc/globus-gss-assist-format.patch/tokens_f.c

  • Committer: Package Import Robot
  • Author(s): Mattias Ellert
  • Date: 2012-04-29 07:03:12 UTC
  • mfrom: (1.2.5)
  • Revision ID: package-import@ubuntu.com-20120429070312-vxpkcewt8lat0mwu
Tags: 8.5-1
* Update to Globus Toolkit 5.2.1
* Drop patches globus-gss-assist-doxygen.patch, globus-gss-assist-deps.patch
  and globus-gss-assist-format.patch (fixed upstream)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 1999-2006 University of Chicago
3
 
 * 
4
 
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 
 * you may not use this file except in compliance with the License.
6
 
 * You may obtain a copy of the License at
7
 
 * 
8
 
 * http://www.apache.org/licenses/LICENSE-2.0
9
 
 * 
10
 
 * Unless required by applicable law or agreed to in writing, software
11
 
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 
 * See the License for the specific language governing permissions and
14
 
 * limitations under the License.
15
 
 */
16
 
 
17
 
#ifndef GLOBUS_DONT_DOCUMENT_INTERNAL
18
 
/**
19
 
 * @file tokens_f.c
20
 
 * @author Sam Lang, Sam Meder
21
 
 * 
22
 
 * $RCSfile: tokens_f.c,v $
23
 
 * $Revision: 1.9 $
24
 
 * $Date: 2007/08/09 14:24:23 $
25
 
 */
26
 
#endif
27
 
 
28
 
/**
29
 
 * @defgroup globus_gsi_gss_assist_tokens Security Token Transport
30
 
 * Token routines using fread and fwrite
31
 
 *
32
 
 * Additional code has been added to detect tokens which 
33
 
 * are sent without a length field. These can currently be
34
 
 * only SSL tokens. This does require some knowledge of the 
35
 
 * underlying GSSAPI, by the application, but is within the 
36
 
 * guidelines of the GSSAPI specifications. 
37
 
 *
38
 
 * The get routine will automaticly attempt this test,
39
 
 * while a new send routine will check a flag. The old send
40
 
 * routine will work as before, sending a 4-byte length.
41
 
 */
42
 
 
43
 
#include "globus_i_gss_assist.h"
44
 
#include <stdio.h>
45
 
#include <errno.h>
46
 
#include <stdlib.h>
47
 
 
48
 
/**
49
 
 * @name Token Get File Descriptor
50
 
 */
51
 
/* @{ */
52
 
/**
53
 
 * @ingroup globus_gsi_gss_assist_tokens
54
 
 * Use a open file discriptor to get a token.  This function
55
 
 * provides parameter types that allow it to be passed to
56
 
 * @ref globus_gss_assist_init_sec_context and 
57
 
 * @ref globus_gss_assist_accept_sec_context
58
 
 *
59
 
 * @param arg
60
 
 *        the FILE * stream cast to a void pointer
61
 
 * @param bufp
62
 
 *        the resulting token
63
 
 * @param sizep
64
 
 *        the size (number of bytes) read into bufp
65
 
 * @return      
66
 
 *        0 on success
67
 
 *        > 0 is internal return
68
 
 *        < 0 is the -errno 
69
 
 */
70
 
int
71
 
globus_gss_assist_token_get_fd(
72
 
    void *                              arg, 
73
 
    void **                             bufp, 
74
 
    size_t *                            sizep)
75
 
{
76
 
    unsigned char                       int_buf[5];
77
 
    unsigned char *                     pp;
78
 
    unsigned char *                     bp = NULL;
79
 
    int                                 bsize;
80
 
    int                                 dsize;
81
 
    int                                 size;
82
 
    void *                              cp;
83
 
    FILE *                              fd;
84
 
    int                                 bytesread;
85
 
    int                                 return_value = 0;
86
 
    static char *                       _function_name_ =
87
 
        "globus_gss_assist_token_get_fd";
88
 
    GLOBUS_I_GSI_GSS_ASSIST_DEBUG_ENTER;
89
 
 
90
 
    fd = (FILE *) arg;
91
 
    if ((bytesread = fread(int_buf, 1, 4, fd)) != 4)
92
 
    {
93
 
        fprintf(stderr,_GASL("Failed reading length %d\n"),bytesread);
94
 
        return_value = GLOBUS_GSS_ASSIST_TOKEN_EOF;
95
 
        goto exit;
96
 
    }
97
 
    
98
 
    GLOBUS_I_GSI_GSS_ASSIST_DEBUG_FPRINTF(
99
 
        4, (globus_i_gsi_gss_assist_debug_fstream,
100
 
            _GASL("token read:%2.2x%2.2x%2.2x%2.2x\n"),
101
 
            int_buf[0],int_buf[1],int_buf[2],int_buf[3]));
102
 
 
103
 
    /*
104
 
     * check if the length is missing, and we are receiving 
105
 
     * a SSL token directly. 
106
 
     * SSLv3 will start with a flag byte in the twenties
107
 
     * followed by major version 3 minor version 0  
108
 
     * Will also accept a SSLv2 hello 2 0 
109
 
     * or a TLS  3 1
110
 
     */
111
 
         
112
 
    if (((int_buf[0]  >= 20) && (int_buf[0] <= 26) 
113
 
        && (((int_buf[1] == 3 && ((int_buf[2] == 0) || int_buf[2] == 1))
114
 
             || (int_buf[1] == 2 && int_buf[2] == 0))))
115
 
        || ((int_buf[0] & 0x80) && int_buf[2] == 1))
116
 
    {
117
 
        /* looks like a SSL token read rest of length */
118
 
        
119
 
        if (fread(&int_buf[4], 1, 1, fd) != 1)
120
 
        {
121
 
            GLOBUS_I_GSI_GSS_ASSIST_DEBUG_FPRINTF(
122
 
                3, (globus_i_gsi_gss_assist_debug_fstream,
123
 
                    _GASL("FAILED READING EXTRA BYTE\n")));
124
 
            return_value =  GLOBUS_GSS_ASSIST_TOKEN_EOF;
125
 
            goto exit;
126
 
        }
127
 
        
128
 
        GLOBUS_I_GSI_GSS_ASSIST_DEBUG_FPRINTF(
129
 
            4, (globus_i_gsi_gss_assist_debug_fstream,
130
 
                _GASL("reading SSL token %.2x%.2x%.2x%.2x%.2x\n"),
131
 
                int_buf[0], int_buf[1], int_buf[2], int_buf[3], int_buf[4]));
132
 
        
133
 
        if ((int_buf[0] & 0x80)) {
134
 
            /* looks like a sslv2 hello 
135
 
             * length is of following bytes in header. 
136
 
             * we read in 5, 2 length and 3 extra, 
137
 
             * so only need next dsize -3
138
 
             */
139
 
            dsize = ( ((unsigned int) int_buf[0] & 0x7f)<<8 
140
 
                      | (unsigned int) int_buf[1]) - 3;
141
 
        } else {
142
 
            dsize = (  ( ((unsigned int) int_buf[3]) << 8)
143
 
                       |   ((unsigned int) int_buf[4]) );
144
 
        }
145
 
        
146
 
        /* If we are using the globus_ssleay, with 
147
 
         * international version, we may be using the 
148
 
         * "26" type, where the length is really the hash 
149
 
         * length, and there is a hash, 8 byte seq andi
150
 
         * 4 byte data length following. We need to get
151
 
         * these as well. 
152
 
         */
153
 
        
154
 
        if (int_buf[0] == 26 ) 
155
 
        {
156
 
            bsize = dsize + 12;  /* MD, seq, data-length */
157
 
            bp = (void *)malloc(bsize);
158
 
            if (!bp)
159
 
            {
160
 
                return_value = GLOBUS_GSS_ASSIST_TOKEN_ERR_MALLOC;
161
 
                goto exit;
162
 
            }
163
 
            if (fread(bp, 1, bsize, fd) != bsize)
164
 
            {
165
 
                return_value = GLOBUS_GSS_ASSIST_TOKEN_EOF;
166
 
                goto exit;
167
 
            }
168
 
            dsize = (  ( ((unsigned int) bp[bsize-4]) << 24)
169
 
                       | ( ((unsigned int) bp[bsize-3]) << 16)
170
 
                       | ( ((unsigned int) bp[bsize-2]) << 8)
171
 
                       |   ((unsigned int) bp[bsize-1]) );
172
 
            
173
 
            size = bsize + dsize + 5;
174
 
        }
175
 
        else
176
 
        {
177
 
            size = dsize + 5; 
178
 
        }
179
 
        cp = (void *)malloc(size);                              
180
 
        if (!cp)
181
 
        {
182
 
            return_value = GLOBUS_GSS_ASSIST_TOKEN_ERR_MALLOC;
183
 
            goto exit;
184
 
        }
185
 
        
186
 
        /* reassemble token header from in_buf and bp */
187
 
 
188
 
        pp = cp;
189
 
        memcpy(pp,int_buf,5);
190
 
        pp += 5;
191
 
        if (bp)
192
 
        {
193
 
            memcpy(pp,bp,bsize);
194
 
            pp += bsize;
195
 
            free(bp);
196
 
            bp = NULL;
197
 
        }
198
 
        if ((bytesread=fread(pp, 1, dsize, fd)) != dsize)
199
 
        {
200
 
            GLOBUS_I_GSI_GSS_ASSIST_DEBUG_FPRINTF(
201
 
                3, (globus_i_gsi_gss_assist_debug_fstream,
202
 
                    _GASL("READ SHORT: %d, %d\n"), dsize, bytesread));
203
 
            return_value = GLOBUS_GSS_ASSIST_TOKEN_EOF;
204
 
            goto exit;
205
 
        }
206
 
    }
207
 
    else
208
 
    {
209
 
        size = (  ( ((unsigned int) int_buf[0]) << 24)
210
 
                  | ( ((unsigned int) int_buf[1]) << 16)
211
 
                  | ( ((unsigned int) int_buf[2]) << 8)
212
 
                  |   ((unsigned int) int_buf[3]) );
213
 
        
214
 
        if (size > 1<<24 || size < 0)  /* size may be garbage */
215
 
        {
216
 
            return_value = GLOBUS_GSS_ASSIST_TOKEN_ERR_BAD_SIZE; 
217
 
            goto exit;
218
 
        }
219
 
        
220
 
        cp = (void *)malloc(size);
221
 
        if (!cp)
222
 
        {
223
 
            return_value = GLOBUS_GSS_ASSIST_TOKEN_ERR_MALLOC;
224
 
        }
225
 
        if ((bytesread=fread(cp, 1, size, fd)) != size)
226
 
        {
227
 
            GLOBUS_I_GSI_GSS_ASSIST_DEBUG_FPRINTF(
228
 
                3, (globus_i_gsi_gss_assist_debug_fstream,
229
 
                    _GASL("read short: %d, %d\n"), size, bytesread));
230
 
            return_value = GLOBUS_GSS_ASSIST_TOKEN_EOF;
231
 
            goto exit;
232
 
        }
233
 
    }
234
 
    
235
 
    *bufp = cp;
236
 
    *sizep = size;
237
 
 
238
 
 exit:
239
 
 
240
 
    GLOBUS_I_GSI_GSS_ASSIST_DEBUG_EXIT;
241
 
    return return_value;
242
 
}
243
 
/* @} */
244
 
 
245
 
/**
246
 
 * @name Token Send File Descriptor
247
 
 */
248
 
/* @{ */
249
 
/**
250
 
 * @ingroup globus_gsi_gss_assist_tokens
251
 
 * Write a token to the open file descriptor.  WIll write it with a 4 byte
252
 
 * length.  This function provides parameter types that allow 
253
 
 * it to be passed to
254
 
 * @ref globus_gss_assist_init_sec_context and 
255
 
 * @ref globus_gss_assist_accept_sec_context
256
 
 *
257
 
 * @param arg
258
 
 *        the FILE * stream to send the token on
259
 
 * @param buf
260
 
 *        the token
261
 
 * @param size
262
 
 *        the size of the token in bytes
263
 
 *
264
 
 * @return
265
 
 *        0 on success
266
 
 *        >0 on error
267
 
 *        <0 on errno error
268
 
 */
269
 
int
270
 
globus_gss_assist_token_send_fd(
271
 
    void *                              arg,  
272
 
    void *                              buf, 
273
 
    size_t                              size)
274
 
{
275
 
    int                                 return_value = 0;
276
 
    globus_gss_assist_ex                ex; 
277
 
    static char *                       _function_name_ =
278
 
        "globus_gss_assist_token_send_fd";
279
 
    GLOBUS_I_GSI_GSS_ASSIST_DEBUG_ENTER;
280
 
 
281
 
    ex.arg = arg;
282
 
    ex.flags = 0;
283
 
    
284
 
    return_value = globus_gss_assist_token_send_fd_ex((void *)&ex, buf, size);
285
 
    
286
 
    GLOBUS_I_GSI_GSS_ASSIST_DEBUG_EXIT;
287
 
    return return_value;
288
 
}
289
 
/* @} */
290
 
 
291
 
/**
292
 
 * @name Token Send File Descriptor Without Length
293
 
 */
294
 
/* @{ */
295
 
/**
296
 
 * @ingroup globus_gsi_gss_assist_tokens
297
 
 * Write a token to the open file descripter. 
298
 
 * Will write it without a length. so as to 
299
 
 */
300
 
int
301
 
globus_gss_assist_token_send_fd_without_length(
302
 
    void *                              arg,  
303
 
    void *                              buf, 
304
 
    size_t                              size)
305
 
{
306
 
    int                                 return_value = 0;
307
 
    globus_gss_assist_ex                ex;
308
 
    static char *                       _function_name_ =
309
 
        "globus_gss_assist_token_send_fd_without_length";
310
 
    GLOBUS_I_GSI_GSS_ASSIST_DEBUG_ENTER;
311
 
    
312
 
    ex.arg  = arg;
313
 
    ex.flags = GLOBUS_GSS_ASSIST_EX_SEND_WITHOUT_LENGTH;
314
 
    
315
 
    return_value = globus_gss_assist_token_send_fd_ex((void *)&ex, buf, size);
316
 
 
317
 
    GLOBUS_I_GSI_GSS_ASSIST_DEBUG_EXIT;
318
 
    return return_value;
319
 
}
320
 
/* @} */
321
 
 
322
 
/**
323
 
 * @name Token Send File Descriptor Flag EX
324
 
 */
325
 
/* @{ */
326
 
/**
327
 
 * @ingroup globus_gsi_gss_assist_tokens
328
 
 * Write a token to the open file descripter. 
329
 
 * will look at the flag to determine if the length field need
330
 
 * to be written.
331
 
 *
332
 
 * @param exp
333
 
 *        the globus_gss_assist_ex variable that holds
334
 
 *        the FILE * stream and flags to bet set
335
 
 * @param buf
336
 
 *        the token buffer to send
337
 
 * @param size
338
 
 *        size of the token buffer
339
 
 *
340
 
 * @return
341
 
 *        0 on success
342
 
 *        >0 on error
343
 
 *        <0 on errno error (-errno)
344
 
 */
345
 
int
346
 
globus_gss_assist_token_send_fd_ex(
347
 
    void *                              exp,  
348
 
    void *                              buf, 
349
 
    size_t                              size)
350
 
{
351
 
    int                                 return_value = 0;
352
 
    unsigned char                       int_buf[4];
353
 
    char *                              header = (char *)buf;
354
 
    FILE *                              fd;
355
 
    globus_gss_assist_ex *              ex;
356
 
 
357
 
    static char *                       _function_name_ =
358
 
        "globus_gss_assist_token_send_fd_ex";
359
 
    GLOBUS_I_GSI_GSS_ASSIST_DEBUG_ENTER;
360
 
        
361
 
    ex = (globus_gss_assist_ex *) exp;
362
 
    fd = (FILE *) ex->arg;
363
 
 
364
 
    /*
365
 
     * Will always send SSL type tokens without a length
366
 
     */
367
 
 
368
 
    GLOBUS_I_GSI_GSS_ASSIST_DEBUG_FPRINTF(
369
 
        3, (globus_i_gsi_gss_assist_debug_fstream,
370
 
            _GASL("send_token: flags: %d length: %u\n"),
371
 
            ex->flags, size));
372
 
 
373
 
    if (!(size > 5 && header[0] <= 26 && header[0] >= 20
374
 
          && ((header[1] == 3 && (header[2] == 0 || header[2] == 1))
375
 
              || (header[1] == 2 && header[2] == 0))))
376
 
    {
377
 
        
378
 
        if (!(ex->flags & GLOBUS_GSS_ASSIST_EX_SEND_WITHOUT_LENGTH)) 
379
 
        {
380
 
            int_buf[0] =  size >> 24;
381
 
            int_buf[1] =  size >> 16;
382
 
            int_buf[2] =  size >>  8;
383
 
            int_buf[3] =  size;
384
 
            
385
 
            GLOBUS_I_GSI_GSS_ASSIST_DEBUG_FPRINTF(
386
 
                3, (globus_i_gsi_gss_assist_debug_fstream,
387
 
                    _GASL("with 4 byte length")));
388
 
            
389
 
            if (fwrite(int_buf ,1 ,4 , fd) != 4)
390
 
            {
391
 
                return_value = GLOBUS_GSS_ASSIST_TOKEN_EOF;
392
 
                goto exit;
393
 
            }
394
 
        }
395
 
    }
396
 
 
397
 
    GLOBUS_I_GSI_GSS_ASSIST_DEBUG_PRINT(3, "\n");
398
 
 
399
 
    if (fwrite(buf, 1, size, fd) != size)
400
 
    {
401
 
        return_value = GLOBUS_GSS_ASSIST_TOKEN_EOF;
402
 
        goto exit;
403
 
    }
404
 
 
405
 
 exit:
406
 
    GLOBUS_I_GSI_GSS_ASSIST_DEBUG_EXIT;
407
 
    return return_value;
408
 
}
409
 
/* @} */