xref: /freebsd/crypto/krb5/src/lib/gssapi/mechglue/g_imp_cred.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* lib/gssapi/mechglue/g_imp_cred.c - gss_import_cred definition */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert  * Copyright (C) 2012 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert  * All rights reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert  * modification, are permitted provided that the following conditions
9*7f2fe78bSCy Schubert  * are met:
10*7f2fe78bSCy Schubert  *
11*7f2fe78bSCy Schubert  * * Redistributions of source code must retain the above copyright
12*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer.
13*7f2fe78bSCy Schubert  *
14*7f2fe78bSCy Schubert  * * Redistributions in binary form must reproduce the above copyright
15*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer in
16*7f2fe78bSCy Schubert  *   the documentation and/or other materials provided with the
17*7f2fe78bSCy Schubert  *   distribution.
18*7f2fe78bSCy Schubert  *
19*7f2fe78bSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*7f2fe78bSCy Schubert  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*7f2fe78bSCy Schubert  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22*7f2fe78bSCy Schubert  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23*7f2fe78bSCy Schubert  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24*7f2fe78bSCy Schubert  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25*7f2fe78bSCy Schubert  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26*7f2fe78bSCy Schubert  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*7f2fe78bSCy Schubert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28*7f2fe78bSCy Schubert  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*7f2fe78bSCy Schubert  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30*7f2fe78bSCy Schubert  * OF THE POSSIBILITY OF SUCH DAMAGE.
31*7f2fe78bSCy Schubert  */
32*7f2fe78bSCy Schubert 
33*7f2fe78bSCy Schubert #include "mglueP.h"
34*7f2fe78bSCy Schubert 
35*7f2fe78bSCy Schubert static OM_uint32
val_imp_cred_args(OM_uint32 * minor_status,gss_buffer_t token,gss_cred_id_t * cred_handle)36*7f2fe78bSCy Schubert val_imp_cred_args(OM_uint32 *minor_status, gss_buffer_t token,
37*7f2fe78bSCy Schubert                   gss_cred_id_t *cred_handle)
38*7f2fe78bSCy Schubert {
39*7f2fe78bSCy Schubert     /* Initialize outputs. */
40*7f2fe78bSCy Schubert     if (minor_status != NULL)
41*7f2fe78bSCy Schubert         *minor_status = 0;
42*7f2fe78bSCy Schubert     if (cred_handle != NULL)
43*7f2fe78bSCy Schubert         *cred_handle = GSS_C_NO_CREDENTIAL;
44*7f2fe78bSCy Schubert 
45*7f2fe78bSCy Schubert     /* Validate arguments. */
46*7f2fe78bSCy Schubert     if (minor_status == NULL)
47*7f2fe78bSCy Schubert         return GSS_S_CALL_INACCESSIBLE_WRITE;
48*7f2fe78bSCy Schubert     if (cred_handle == NULL)
49*7f2fe78bSCy Schubert         return GSS_S_CALL_INACCESSIBLE_WRITE;
50*7f2fe78bSCy Schubert     if (token == GSS_C_NO_BUFFER)
51*7f2fe78bSCy Schubert         return GSS_S_CALL_INACCESSIBLE_READ | GSS_S_DEFECTIVE_TOKEN;
52*7f2fe78bSCy Schubert     if (GSS_EMPTY_BUFFER(token))
53*7f2fe78bSCy Schubert         return GSS_S_DEFECTIVE_TOKEN;
54*7f2fe78bSCy Schubert     return GSS_S_COMPLETE;
55*7f2fe78bSCy Schubert }
56*7f2fe78bSCy Schubert 
57*7f2fe78bSCy Schubert /* Populate mech_oid and mech_token with the next entry in token, using aliased
58*7f2fe78bSCy Schubert  * memory from token.  Advance token by the amount consumed. */
59*7f2fe78bSCy Schubert static OM_uint32
get_entry(OM_uint32 * minor_status,gss_buffer_t token,gss_OID mech_oid,gss_buffer_t mech_token)60*7f2fe78bSCy Schubert get_entry(OM_uint32 *minor_status, gss_buffer_t token, gss_OID mech_oid,
61*7f2fe78bSCy Schubert           gss_buffer_t mech_token)
62*7f2fe78bSCy Schubert {
63*7f2fe78bSCy Schubert     OM_uint32 len;
64*7f2fe78bSCy Schubert 
65*7f2fe78bSCy Schubert     /* Get the mechanism OID. */
66*7f2fe78bSCy Schubert     if (token->length < 4)
67*7f2fe78bSCy Schubert         return GSS_S_DEFECTIVE_TOKEN;
68*7f2fe78bSCy Schubert     len = load_32_be(token->value);
69*7f2fe78bSCy Schubert     if (token->length - 4 < len)
70*7f2fe78bSCy Schubert         return GSS_S_DEFECTIVE_TOKEN;
71*7f2fe78bSCy Schubert     mech_oid->length = len;
72*7f2fe78bSCy Schubert     mech_oid->elements = (char *)token->value + 4;
73*7f2fe78bSCy Schubert     token->value = (char *)token->value + 4 + len;
74*7f2fe78bSCy Schubert     token->length -= 4 + len;
75*7f2fe78bSCy Schubert 
76*7f2fe78bSCy Schubert     /* Get the mechanism token. */
77*7f2fe78bSCy Schubert     if (token->length < 4)
78*7f2fe78bSCy Schubert         return GSS_S_DEFECTIVE_TOKEN;
79*7f2fe78bSCy Schubert     len = load_32_be(token->value);
80*7f2fe78bSCy Schubert     if (token->length - 4 < len)
81*7f2fe78bSCy Schubert         return GSS_S_DEFECTIVE_TOKEN;
82*7f2fe78bSCy Schubert     mech_token->length = len;
83*7f2fe78bSCy Schubert     mech_token->value = (char *)token->value + 4;
84*7f2fe78bSCy Schubert     token->value = (char *)token->value + 4 + len;
85*7f2fe78bSCy Schubert     token->length -= 4 + len;
86*7f2fe78bSCy Schubert 
87*7f2fe78bSCy Schubert     return GSS_S_COMPLETE;
88*7f2fe78bSCy Schubert }
89*7f2fe78bSCy Schubert 
90*7f2fe78bSCy Schubert OM_uint32 KRB5_CALLCONV
gss_import_cred(OM_uint32 * minor_status,gss_buffer_t token,gss_cred_id_t * cred_handle)91*7f2fe78bSCy Schubert gss_import_cred(OM_uint32 *minor_status, gss_buffer_t token,
92*7f2fe78bSCy Schubert                 gss_cred_id_t *cred_handle)
93*7f2fe78bSCy Schubert {
94*7f2fe78bSCy Schubert     OM_uint32 status, tmpmin, count;
95*7f2fe78bSCy Schubert     gss_union_cred_t cred = NULL;
96*7f2fe78bSCy Schubert     gss_mechanism mech;
97*7f2fe78bSCy Schubert     gss_buffer_desc tok, mech_token;
98*7f2fe78bSCy Schubert     gss_OID_desc mech_oid;
99*7f2fe78bSCy Schubert     gss_OID selected_mech;
100*7f2fe78bSCy Schubert     gss_cred_id_t mech_cred;
101*7f2fe78bSCy Schubert     void *elemcopy;
102*7f2fe78bSCy Schubert 
103*7f2fe78bSCy Schubert     status = val_imp_cred_args(minor_status, token, cred_handle);
104*7f2fe78bSCy Schubert     if (status != GSS_S_COMPLETE)
105*7f2fe78bSCy Schubert         return status;
106*7f2fe78bSCy Schubert 
107*7f2fe78bSCy Schubert     /* Count the entries in token. */
108*7f2fe78bSCy Schubert     for (tok = *token, count = 0; tok.length > 0; count++) {
109*7f2fe78bSCy Schubert         status = get_entry(minor_status, &tok, &mech_oid, &mech_token);
110*7f2fe78bSCy Schubert         if (status != GSS_S_COMPLETE)
111*7f2fe78bSCy Schubert             return status;
112*7f2fe78bSCy Schubert     }
113*7f2fe78bSCy Schubert 
114*7f2fe78bSCy Schubert     /* Allocate a union credential. */
115*7f2fe78bSCy Schubert     cred = calloc(1, sizeof(*cred));
116*7f2fe78bSCy Schubert     if (cred == NULL)
117*7f2fe78bSCy Schubert         goto oom;
118*7f2fe78bSCy Schubert     cred->loopback = cred;
119*7f2fe78bSCy Schubert     cred->count = 0;
120*7f2fe78bSCy Schubert     cred->mechs_array = calloc(count, sizeof(*cred->mechs_array));
121*7f2fe78bSCy Schubert     if (cred->mechs_array == NULL)
122*7f2fe78bSCy Schubert         goto oom;
123*7f2fe78bSCy Schubert     cred->cred_array = calloc(count, sizeof(*cred->cred_array));
124*7f2fe78bSCy Schubert     if (cred->cred_array == NULL)
125*7f2fe78bSCy Schubert         goto oom;
126*7f2fe78bSCy Schubert 
127*7f2fe78bSCy Schubert     tok = *token;
128*7f2fe78bSCy Schubert     while (tok.length > 0) {
129*7f2fe78bSCy Schubert         (void)get_entry(minor_status, &tok, &mech_oid, &mech_token);
130*7f2fe78bSCy Schubert 
131*7f2fe78bSCy Schubert         /* Import this entry's mechanism token. */
132*7f2fe78bSCy Schubert         status = gssint_select_mech_type(minor_status, &mech_oid,
133*7f2fe78bSCy Schubert                                          &selected_mech);
134*7f2fe78bSCy Schubert         if (status != GSS_S_COMPLETE)
135*7f2fe78bSCy Schubert             goto error;
136*7f2fe78bSCy Schubert         mech = gssint_get_mechanism(selected_mech);
137*7f2fe78bSCy Schubert         if (mech == NULL || (mech->gss_import_cred == NULL &&
138*7f2fe78bSCy Schubert                              mech->gssspi_import_cred_by_mech == NULL)) {
139*7f2fe78bSCy Schubert             status = GSS_S_DEFECTIVE_TOKEN;
140*7f2fe78bSCy Schubert             goto error;
141*7f2fe78bSCy Schubert         }
142*7f2fe78bSCy Schubert         if (mech->gssspi_import_cred_by_mech) {
143*7f2fe78bSCy Schubert             status = mech->gssspi_import_cred_by_mech(minor_status,
144*7f2fe78bSCy Schubert                                         gssint_get_public_oid(selected_mech),
145*7f2fe78bSCy Schubert                                         &mech_token, &mech_cred);
146*7f2fe78bSCy Schubert         } else {
147*7f2fe78bSCy Schubert             status = mech->gss_import_cred(minor_status, &mech_token,
148*7f2fe78bSCy Schubert                                            &mech_cred);
149*7f2fe78bSCy Schubert         }
150*7f2fe78bSCy Schubert         if (status != GSS_S_COMPLETE) {
151*7f2fe78bSCy Schubert             map_error(minor_status, mech);
152*7f2fe78bSCy Schubert             goto error;
153*7f2fe78bSCy Schubert         }
154*7f2fe78bSCy Schubert 
155*7f2fe78bSCy Schubert         /* Add the resulting mechanism cred to the union cred. */
156*7f2fe78bSCy Schubert         elemcopy = malloc(selected_mech->length);
157*7f2fe78bSCy Schubert         if (elemcopy == NULL) {
158*7f2fe78bSCy Schubert             if (mech->gss_release_cred != NULL)
159*7f2fe78bSCy Schubert                 mech->gss_release_cred(&tmpmin, &mech_cred);
160*7f2fe78bSCy Schubert             goto oom;
161*7f2fe78bSCy Schubert         }
162*7f2fe78bSCy Schubert         memcpy(elemcopy, selected_mech->elements, selected_mech->length);
163*7f2fe78bSCy Schubert         cred->mechs_array[cred->count].length = selected_mech->length;
164*7f2fe78bSCy Schubert         cred->mechs_array[cred->count].elements = elemcopy;
165*7f2fe78bSCy Schubert         cred->cred_array[cred->count++] = mech_cred;
166*7f2fe78bSCy Schubert     }
167*7f2fe78bSCy Schubert 
168*7f2fe78bSCy Schubert     *cred_handle = cred;
169*7f2fe78bSCy Schubert     return GSS_S_COMPLETE;
170*7f2fe78bSCy Schubert 
171*7f2fe78bSCy Schubert oom:
172*7f2fe78bSCy Schubert     status = GSS_S_FAILURE;
173*7f2fe78bSCy Schubert     *minor_status = ENOMEM;
174*7f2fe78bSCy Schubert error:
175*7f2fe78bSCy Schubert     (void)gss_release_cred(&tmpmin, &cred);
176*7f2fe78bSCy Schubert     return status;
177*7f2fe78bSCy Schubert }
178