File: g_imp_cred.c

package info (click to toggle)
krb5 1.12.1%2Bdfsg-19%2Bdeb8u4
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 67,680 kB
  • ctags: 34,155
  • sloc: ansic: 309,213; cpp: 15,753; exp: 13,257; makefile: 7,652; python: 7,228; sh: 6,457; perl: 3,514; asm: 1,544; yacc: 1,187; awk: 396; xml: 368; csh: 147; lisp: 104; sed: 43
file content (177 lines) | stat: -rw-r--r-- 6,324 bytes parent folder | download | duplicates (8)
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
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* lib/gssapi/mechglue/g_imp_cred.c - gss_import_cred definition */
/*
 * Copyright (C) 2012 by the Massachusetts Institute of Technology.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "mglueP.h"

static OM_uint32
val_imp_cred_args(OM_uint32 *minor_status, gss_buffer_t token,
                  gss_cred_id_t *cred_handle)
{
    /* Initialize outputs. */
    if (minor_status != NULL)
        *minor_status = 0;
    if (cred_handle != NULL)
        *cred_handle = GSS_C_NO_CREDENTIAL;

    /* Validate arguments. */
    if (minor_status == NULL)
        return GSS_S_CALL_INACCESSIBLE_WRITE;
    if (cred_handle == NULL)
        return GSS_S_CALL_INACCESSIBLE_WRITE;
    if (token == GSS_C_NO_BUFFER)
        return GSS_S_CALL_INACCESSIBLE_READ | GSS_S_DEFECTIVE_TOKEN;
    if (GSS_EMPTY_BUFFER(token))
        return GSS_S_DEFECTIVE_TOKEN;
    return GSS_S_COMPLETE;
}

/* Populate mech_oid and mech_token with the next entry in token, using aliased
 * memory from token.  Advance token by the amount consumed. */
static OM_uint32
get_entry(OM_uint32 *minor_status, gss_buffer_t token, gss_OID mech_oid,
          gss_buffer_t mech_token)
{
    OM_uint32 len;

    /* Get the mechanism OID. */
    if (token->length < 4)
        return GSS_S_DEFECTIVE_TOKEN;
    len = load_32_be(token->value);
    if (token->length - 4 < len)
        return GSS_S_DEFECTIVE_TOKEN;
    mech_oid->length = len;
    mech_oid->elements = (char *)token->value + 4;
    token->value = (char *)token->value + 4 + len;
    token->length -= 4 + len;

    /* Get the mechanism token. */
    if (token->length < 4)
        return GSS_S_DEFECTIVE_TOKEN;
    len = load_32_be(token->value);
    if (token->length - 4 < len)
        return GSS_S_DEFECTIVE_TOKEN;
    mech_token->length = len;
    mech_token->value = (char *)token->value + 4;
    token->value = (char *)token->value + 4 + len;
    token->length -= 4 + len;

    return GSS_S_COMPLETE;
}

OM_uint32 KRB5_CALLCONV
gss_import_cred(OM_uint32 *minor_status, gss_buffer_t token,
                gss_cred_id_t *cred_handle)
{
    OM_uint32 status, tmpmin, count;
    gss_union_cred_t cred = NULL;
    gss_mechanism mech;
    gss_buffer_desc tok, mech_token;
    gss_OID_desc mech_oid;
    gss_OID selected_mech;
    gss_cred_id_t mech_cred;
    void *elemcopy;

    status = val_imp_cred_args(minor_status, token, cred_handle);
    if (status != GSS_S_COMPLETE)
        return status;

    /* Count the entries in token. */
    for (tok = *token, count = 0; tok.length > 0; count++) {
        status = get_entry(minor_status, &tok, &mech_oid, &mech_token);
        if (status != GSS_S_COMPLETE)
            return status;
    }

    /* Allocate a union credential. */
    cred = calloc(1, sizeof(*cred));
    if (cred == NULL)
        goto oom;
    cred->loopback = cred;
    cred->count = 0;
    cred->mechs_array = calloc(count, sizeof(*cred->mechs_array));
    if (cred->mechs_array == NULL)
        goto oom;
    cred->cred_array = calloc(count, sizeof(*cred->cred_array));
    if (cred->cred_array == NULL)
        goto oom;

    tok = *token;
    while (tok.length > 0) {
        (void)get_entry(minor_status, &tok, &mech_oid, &mech_token);

        /* Import this entry's mechanism token. */
        status = gssint_select_mech_type(minor_status, &mech_oid,
                                         &selected_mech);
        if (status != GSS_S_COMPLETE)
            goto error;
        mech = gssint_get_mechanism(selected_mech);
        if (mech == NULL || (mech->gss_import_cred == NULL &&
                             mech->gssspi_import_cred_by_mech == NULL)) {
            status = GSS_S_DEFECTIVE_TOKEN;
            goto error;
        }
        if (mech->gssspi_import_cred_by_mech) {
            status = mech->gssspi_import_cred_by_mech(minor_status,
                                        gssint_get_public_oid(selected_mech),
                                        &mech_token, &mech_cred);
        } else {
            status = mech->gss_import_cred(minor_status, &mech_token,
                                           &mech_cred);
        }
        if (status != GSS_S_COMPLETE) {
            map_error(minor_status, mech);
            goto error;
        }

        /* Add the resulting mechanism cred to the union cred. */
        elemcopy = malloc(selected_mech->length);
        if (elemcopy == NULL) {
            if (mech->gss_release_cred != NULL)
                mech->gss_release_cred(&tmpmin, &mech_cred);
            goto oom;
        }
        memcpy(elemcopy, selected_mech->elements, selected_mech->length);
        cred->mechs_array[cred->count].length = selected_mech->length;
        cred->mechs_array[cred->count].elements = elemcopy;
        cred->cred_array[cred->count++] = mech_cred;
    }

    *cred_handle = cred;
    return GSS_S_COMPLETE;

oom:
    status = GSS_S_FAILURE;
    *minor_status = ENOMEM;
error:
    (void)gss_release_cred(&tmpmin, &cred);
    return status;
}