File: import_name.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 (562 lines) | stat: -rw-r--r-- 18,010 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
 * 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 import_name.c
 * @author Sam Lang, Sam Meder
 * 
 * $RCSfile: import_name.c,v $
 * $Revision: 1.20 $
 * $Date: 2008/11/21 15:04:49 $
 */
#endif

static char *rcsid = "$Id: import_name.c,v 1.20 2008/11/21 15:04:49 bester Exp $";

#include "gssapi_openssl.h"
#include "globus_i_gsi_gss_utils.h"
#include "globus_gsi_gss_constants.h"
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

static
OM_uint32
gss_l_resolve_ip(
    OM_uint32 *                         minor_status,
    gss_name_desc *                     name);


/**
 * Import a name into a gss_name_t
 * @ingroup globus_gsi_gssapi
 *
 * Creates a new gss_name_t which contains a mechanism-specific representation
 * of the input name. GSSAPI OpenSSL implements the following name types, based
 * on the input_name_type OID: 
 *
 * - GSS_C_NT_ANONYMOUS (input_name_buffer is ignored)
 * - GSS_C_NT_HOSTBASED_SERVICE (input_name_buffer contains a string
 * "service@FQN" which will match /CN=service/FQDN)
 * - GSS_C_NT_EXPORT_NAME (input_name_buffer contains a string with the
 * X509_oneline representation of a name)
 * like "/X=Y/Z=A...")
 * - GSS_C_NO_OID or GSS_C_NT_USER_NAME (input_name_buffer contains an X.500
 *   name formatted
 * like "/X=Y/Z=A...")
 * - GLOBUS_GSS_C_NT_HOST_IP (input_name_buffer contains a string
 * "FQDN/ip-address" which will match names with the FQDN or the IP address)
 * - GLOBUS_SSS_C_NT_X509 (input buffer is an X509 struct from OpenSSL)
 *
 * @param minor_status
 *     Minor status
 * @param input_name_buffer
 *     Input name buffer which is interpreted based on the @a input_name_type
 * @param input_name_type
 *     OID of the name
 * @param output_name_P
 *     New gss_name_t value containing the name
 *
 * @retval GSS_S_COMPLETE
 *     indicates that a valid name representation is
 *     output in output_name and described by the type value in
 *     output_name_type.
 * @retval GSS_S_BAD_NAMETYPE
 *     indicates that the input_name_type is unsupported
 *     by the applicable underlying GSS-API mechanism(s), so the import
 *     operation could not be completed.
 * @retval GSS_S_BAD_NAME
 *     indicates that the provided input_name_string is ill-formed in terms of
 *     the input_name_type, so the import operation could not be completed.
 * @retval GSS_S_BAD_MECH
 *     indicates that the input presented for import was an exported name
 *     object and that its enclosed mechanism type was not recognized or was
 *     unsupported by the GSS-API implementation.
 * @retval GSS_S_FAILURE
 *     indicates that the requested operation could not be performed for
 *     reasons unspecified at the GSS-API level.
 */
OM_uint32 
GSS_CALLCONV gss_import_name(
    OM_uint32 *                         minor_status,
    const gss_buffer_t                  input_name_buffer,
    const gss_OID                       input_name_type,
    gss_name_t *                        output_name_P)
{
    OM_uint32                           major_status = GSS_S_COMPLETE;
    globus_result_t                     local_result = GLOBUS_SUCCESS;
    gss_name_desc *                     output_name = NULL;
    int                                 length, i;
    char *                              name_buffer = NULL;
    char *                              index;
    static char *                       _function_name_ = "gss_import_name";

    GLOBUS_I_GSI_GSSAPI_DEBUG_ENTER;

    if (minor_status == NULL || input_name_buffer == NULL ||
        output_name_P == NULL)
    {
        major_status = GSS_S_FAILURE;

        if (minor_status != NULL)
        {
            GLOBUS_GSI_GSSAPI_OPENSSL_ERROR_RESULT(
                    minor_status,
                    GLOBUS_GSI_GSSAPI_ERROR_BAD_ARGUMENT,
                    (_GGSL("Invalid parameter")));
        }

        goto out;
    }

    *minor_status = (OM_uint32) GLOBUS_SUCCESS;

    output_name = calloc(1, sizeof(gss_name_desc));
    
    if (output_name == NULL)
    {
        GLOBUS_GSI_GSSAPI_MALLOC_ERROR(minor_status);
        major_status = GSS_S_FAILURE;
        goto out;
    } 
    
    output_name->name_oid = input_name_type;
    output_name->x509n = X509_NAME_new();
    if (output_name->x509n == NULL)
    {
        GLOBUS_GSI_GSSAPI_MALLOC_ERROR(minor_status);
        major_status = GSS_S_FAILURE;
        goto release_name_out;
    }

    if(g_OID_equal(input_name_type, GSS_C_NT_ANONYMOUS))
    {
        ;
    }
    else if (g_OID_equal(GSS_C_NT_HOSTBASED_SERVICE, input_name_type))
    {
        name_buffer = globus_libc_strdup(input_name_buffer->value);
        if (name_buffer == NULL)
        {
            GLOBUS_GSI_GSSAPI_MALLOC_ERROR(minor_status);
            major_status = GSS_S_FAILURE;
            goto release_name_out;
        }

        index = strchr(name_buffer, '@');
        if (index)
        {
            *index = '\0';
            output_name->service_name = name_buffer;
            name_buffer = NULL;
            output_name->host_name = globus_libc_strdup(index+1);
            if (output_name->host_name == NULL)
            {
                GLOBUS_GSI_GSSAPI_MALLOC_ERROR(minor_status);
                major_status = GSS_S_FAILURE;

                goto release_name_out;
            }
        }
        else
        {
            output_name->host_name = name_buffer;
            name_buffer = NULL;
            output_name->service_name = globus_libc_strdup("host");
        }
        name_buffer = globus_common_create_string(
                "/CN=%s%s%s",
                output_name->service_name ? output_name->service_name : "",
                output_name->service_name ? "/" : "",
                output_name->host_name);
        if (name_buffer == NULL)
        {
            GLOBUS_GSI_GSSAPI_MALLOC_ERROR(minor_status);
            major_status = GSS_S_FAILURE;

            goto release_name_out;
        }

        local_result = globus_gsi_cert_utils_get_x509_name(
                name_buffer,
                strlen(name_buffer),
                output_name->x509n);

        free(name_buffer);
        name_buffer = NULL;

        if (local_result != GLOBUS_SUCCESS)
        {
            GLOBUS_GSI_GSSAPI_ERROR_CHAIN_RESULT(
                minor_status, local_result,
                GLOBUS_GSI_GSSAPI_ERROR_BAD_NAME);
            major_status = GSS_S_FAILURE;

            goto release_name_out;
        }
        output_name->x509n_oneline =
                X509_NAME_oneline(output_name->x509n, NULL, 0);
        if (output_name->x509n_oneline == NULL)
        {
            GLOBUS_GSI_GSSAPI_MALLOC_ERROR(minor_status);
            major_status = GSS_S_FAILURE;

            goto release_name_out;
        }
    }
    else if (g_OID_equal(GSS_C_NT_EXPORT_NAME, input_name_type))
    {
        /* Note: Does not support all name types */
        output_name->name_oid = GSS_C_NO_OID;
        name_buffer = input_name_buffer->value;

        i = 0;
        if (name_buffer[i++] != 0x04 || name_buffer[i++] != 0x01 ||
            name_buffer[i++] !=
                ((gss_mech_globus_gssapi_openssl->length+2) >> 8) ||
            name_buffer[i++] !=
                ((gss_mech_globus_gssapi_openssl->length+2) & 0xff) ||
            name_buffer[i++] != 0x06 ||
            name_buffer[i++] !=
                (gss_mech_globus_gssapi_openssl->length & 0xff) ||
            (memcmp(&(name_buffer[i]), gss_mech_globus_gssapi_openssl->elements,
                    gss_mech_globus_gssapi_openssl->length) != 0))
        {
            GLOBUS_GSI_GSSAPI_ERROR_CHAIN_RESULT(
                minor_status, local_result,
                GLOBUS_GSI_GSSAPI_ERROR_BAD_NAME);
            major_status = GSS_S_BAD_NAME;
            goto release_name_out;
        }

        i += gss_mech_globus_gssapi_openssl->length;
        length = name_buffer[i++] << 24;
        length += name_buffer[i++] << 16;
        length += name_buffer[i++] << 8;
        length += name_buffer[i++] & 0xff;

        local_result = globus_gsi_cert_utils_get_x509_name(
            &(name_buffer[i]),
            length,
            output_name->x509n);
        if(local_result != GLOBUS_SUCCESS)
        {
            GLOBUS_GSI_GSSAPI_ERROR_CHAIN_RESULT(
                minor_status, local_result,
                GLOBUS_GSI_GSSAPI_ERROR_BAD_NAME);
            major_status = GSS_S_BAD_NAME;
            goto release_name_out;
        }
        output_name->x509n_oneline =
                X509_NAME_oneline(output_name->x509n, NULL, 0);

        major_status = globus_i_gsi_gssapi_get_hostname(
                minor_status,
                output_name);
        if (GSS_ERROR(major_status))
        {
            goto release_name_out;
        }
    }
    else if (g_OID_equal(GSS_C_NO_OID, input_name_type) ||
             g_OID_equal(GSS_C_NT_USER_NAME, input_name_type))
    {
        output_name->user_name = globus_libc_strdup(input_name_buffer->value);
        if (output_name->user_name == NULL)
        {
            GLOBUS_GSI_GSSAPI_MALLOC_ERROR(minor_status);
            major_status = GSS_S_FAILURE;

            goto release_name_out;
        }
        local_result = globus_gsi_cert_utils_get_x509_name(
                output_name->user_name,
                strlen(output_name->user_name),
                output_name->x509n);
        if(local_result != GLOBUS_SUCCESS)
        {
            GLOBUS_GSI_GSSAPI_ERROR_CHAIN_RESULT(
                minor_status, local_result,
                GLOBUS_GSI_GSSAPI_ERROR_BAD_NAME);
            major_status = GSS_S_BAD_NAME;

            goto release_name_out;
        }
        output_name->x509n_oneline =
                X509_NAME_oneline(output_name->x509n, NULL, 0);
        if (output_name->x509n_oneline == NULL)
        {
            GLOBUS_GSI_GSSAPI_MALLOC_ERROR(minor_status);
            major_status = GSS_S_FAILURE;

            goto release_name_out;
        }
        major_status = globus_i_gsi_gssapi_get_hostname(
                minor_status, output_name);
        if (GSS_ERROR(major_status))
        {
            goto release_name_out;
        }
    }
    else if (g_OID_equal(GLOBUS_GSS_C_NT_HOST_IP, input_name_type))
    {
        name_buffer = globus_libc_strdup(input_name_buffer->value);
        if (name_buffer == NULL)
        {
            GLOBUS_GSI_GSSAPI_MALLOC_ERROR(minor_status);
            major_status = GSS_S_FAILURE;

            goto release_name_out;
        }

        index = strchr(name_buffer, '/');
        if (index)
        {
            *index = '\0';
            output_name->host_name = name_buffer;
            name_buffer = NULL;
            output_name->ip_address = globus_libc_strdup(index+1);
        }
        else
        {
            free(name_buffer);
            name_buffer = NULL;

            major_status = GSS_S_BAD_NAME;

            GLOBUS_GSI_GSSAPI_OPENSSL_ERROR_RESULT(
                    minor_status,
                    GLOBUS_GSI_GSSAPI_ERROR_BAD_NAME,
                    (_GGSL("Bad name")));
        }
        name_buffer = globus_common_create_string(
                "/CN=%s",
                output_name->host_name);
        if (name_buffer == NULL)
        {
            GLOBUS_GSI_GSSAPI_MALLOC_ERROR(minor_status);
            major_status = GSS_S_FAILURE;

            goto release_name_out;
        }

        local_result = globus_gsi_cert_utils_get_x509_name(
                name_buffer,
                strlen(name_buffer),
                output_name->x509n);
        free(name_buffer);
        name_buffer = NULL;
        if(local_result != GLOBUS_SUCCESS)
        {
            GLOBUS_GSI_GSSAPI_ERROR_CHAIN_RESULT(
                minor_status, local_result,
                GLOBUS_GSI_GSSAPI_ERROR_BAD_NAME);
            major_status = GSS_S_BAD_NAME;

            goto release_name_out;
        }
        output_name->x509n_oneline =
                X509_NAME_oneline(output_name->x509n, NULL, 0);
        if (output_name->x509n_oneline == NULL)
        {
            GLOBUS_GSI_GSSAPI_MALLOC_ERROR(minor_status);
            major_status = GSS_S_FAILURE;

            goto release_name_out;
        }
        major_status = gss_l_resolve_ip(minor_status, output_name);
    }
    else if (g_OID_equal(GLOBUS_GSS_C_NT_X509, input_name_type))
    {
        X509_NAME * n;
        X509 * x509_input = input_name_buffer->value;
        GENERAL_NAMES * subject_alt_name;
        int idx;

        /* Extract SubjectName if present */
        if ((n = X509_get_subject_name(x509_input)) != NULL)
        {
            X509_NAME_free(output_name->x509n);

            output_name->x509n = X509_NAME_dup(n);
            if (output_name->x509n == NULL)
            {
                GLOBUS_GSI_GSSAPI_MALLOC_ERROR(minor_status);
                major_status = GSS_S_FAILURE;

                goto release_name_out;
            }
            output_name->x509n_oneline =
                    X509_NAME_oneline(output_name->x509n, NULL, 0);
            if (output_name->x509n_oneline == NULL)
            {
                GLOBUS_GSI_GSSAPI_MALLOC_ERROR(minor_status);
                major_status = GSS_S_FAILURE;

                goto release_name_out;
            }
            major_status = globus_i_gsi_gssapi_get_hostname(
                    minor_status, output_name);
            if (GSS_ERROR(major_status))
            {
                goto release_name_out;
            }
        }

        /* Extract subjectAltName if present */
        idx = -1;
        for (idx = X509_get_ext_by_NID(x509_input, NID_subject_alt_name, idx);
             idx != -1;
             idx = X509_get_ext_by_NID(x509_input, NID_subject_alt_name, idx))
        {
            X509_EXTENSION * ext_value;

            ext_value = X509_get_ext(x509_input, idx);
            if (!ext_value)
            {
                continue;
            }
            subject_alt_name = X509V3_EXT_d2i(ext_value);
            if (!subject_alt_name)
            {
                continue;
            }

            output_name->subjectAltNames =
                    sk_GENERAL_NAME_dup(subject_alt_name);
            sk_GENERAL_NAME_free(subject_alt_name);
            if (output_name->subjectAltNames == NULL)
            {
                GLOBUS_GSI_GSSAPI_MALLOC_ERROR(minor_status);
                major_status = GSS_S_FAILURE;

                goto release_name_out;
            }
        }
    }
    else
    {
        GLOBUS_GSI_GSSAPI_ERROR_RESULT(
                minor_status, 
                GLOBUS_GSI_GSSAPI_ERROR_BAD_NAME,
                (_GGSL("Bad name type")));

        major_status = GSS_S_BAD_NAMETYPE;

        goto release_name_out;
    }

    if (major_status != GSS_S_COMPLETE)
    {
        OM_uint32 dummy;
release_name_out:
        gss_release_name(&dummy, &output_name);
    }

    *output_name_P = output_name;
    
 out:

    GLOBUS_I_GSI_GSSAPI_DEBUG_EXIT;
    return major_status;
} 
/* gss_import_name */


static
OM_uint32
gss_l_resolve_ip(
    OM_uint32 *                         minor_status,
    gss_name_desc *                     name)
{
    char                                realhostname[NI_MAXHOST + 1];
    OM_uint32                           major_status;
    globus_result_t                     result = GLOBUS_SUCCESS;
    globus_addrinfo_t                   hints;
    globus_addrinfo_t *                 addrinfo = NULL;
    static char *                       _function_name_ = "gss_l_resolve_ip";

    major_status = GSS_S_COMPLETE;

    memset(&hints, 0, sizeof(globus_addrinfo_t));
    hints.ai_family = PF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;

    /*
     * Hostname is an ip address: do a non-canonname getaddrinfo to get
     * the sockaddr, then getnameinfo to get the canonical hostname from that
     * address
     */
    hints.ai_flags = GLOBUS_AI_NUMERICHOST;
    result = globus_libc_getaddrinfo(name->ip_address, NULL, &hints, &addrinfo);

    if (result == GLOBUS_SUCCESS)
    {
        if (addrinfo == NULL || addrinfo->ai_addr == NULL)
        {
            goto error_exit;
        }

        /*
         * For connections to localhost, check for certificate
         * matching our real hostname, not "localhost"
         */
        if (globus_libc_addr_is_loopback(
            (const globus_sockaddr_t *) addrinfo->ai_addr) == GLOBUS_TRUE)
        {
            globus_libc_gethostname(
                realhostname, sizeof(realhostname) - 1);
        }
        else
        {
            /* use GLOBUS_NI_NAMEREQD to fail if address can't be looked up?
             * if not, realhostname will just be the same ip address
             * we pass in */
            result = globus_libc_getnameinfo(
                (const globus_sockaddr_t *) addrinfo->ai_addr,
                realhostname,
                sizeof(realhostname) - 1,
                NULL,
                0,
                0);
            if(result != GLOBUS_SUCCESS)
            {
                goto error_exit;
            }
        }
    }
    else
    {
        goto error_exit;
    }

    name->ip_name = globus_libc_strdup(realhostname);
    if (name->ip_name == NULL)
    {
        GLOBUS_GSI_GSSAPI_MALLOC_ERROR(minor_status);
        major_status = GSS_S_FAILURE;
    }

 error_exit:
    if (addrinfo != NULL)
    {
        globus_libc_freeaddrinfo(addrinfo);
    }
    return major_status;
}
/* gss_l_resolve_ip() */