xref: /freebsd/crypto/krb5/src/lib/gssapi/mechglue/g_negoex.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /*
3*7f2fe78bSCy Schubert  * Copyright (C) 2011 by the Massachusetts Institute of Technology.
4*7f2fe78bSCy Schubert  * All rights reserved.
5*7f2fe78bSCy Schubert  *
6*7f2fe78bSCy Schubert  * Redistribution and use in source and binary forms, with or without
7*7f2fe78bSCy Schubert  * modification, are permitted provided that the following conditions
8*7f2fe78bSCy Schubert  * are met:
9*7f2fe78bSCy Schubert  *
10*7f2fe78bSCy Schubert  * * Redistributions of source code must retain the above copyright
11*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer.
12*7f2fe78bSCy Schubert  *
13*7f2fe78bSCy Schubert  * * Redistributions in binary form must reproduce the above copyright
14*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer in
15*7f2fe78bSCy Schubert  *   the documentation and/or other materials provided with the
16*7f2fe78bSCy Schubert  *   distribution.
17*7f2fe78bSCy Schubert  *
18*7f2fe78bSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*7f2fe78bSCy Schubert  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*7f2fe78bSCy Schubert  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21*7f2fe78bSCy Schubert  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22*7f2fe78bSCy Schubert  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23*7f2fe78bSCy Schubert  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24*7f2fe78bSCy Schubert  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25*7f2fe78bSCy Schubert  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*7f2fe78bSCy Schubert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27*7f2fe78bSCy Schubert  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*7f2fe78bSCy Schubert  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29*7f2fe78bSCy Schubert  * OF THE POSSIBILITY OF SUCH DAMAGE.
30*7f2fe78bSCy Schubert  */
31*7f2fe78bSCy Schubert 
32*7f2fe78bSCy Schubert /*
33*7f2fe78bSCy Schubert  * This file contains dispatch functions for the three GSSAPI extensions
34*7f2fe78bSCy Schubert  * described in draft-zhu-negoex-04, renamed to use the gssspi_ prefix.  Since
35*7f2fe78bSCy Schubert  * the only caller of these functions is SPNEGO, argument validation is
36*7f2fe78bSCy Schubert  * omitted.
37*7f2fe78bSCy Schubert  */
38*7f2fe78bSCy Schubert 
39*7f2fe78bSCy Schubert #include "mglueP.h"
40*7f2fe78bSCy Schubert 
41*7f2fe78bSCy Schubert OM_uint32 KRB5_CALLCONV
gssspi_query_meta_data(OM_uint32 * minor_status,gss_const_OID mech_oid,gss_cred_id_t cred_handle,gss_ctx_id_t * context_handle,const gss_name_t targ_name,OM_uint32 req_flags,gss_buffer_t meta_data)42*7f2fe78bSCy Schubert gssspi_query_meta_data(OM_uint32 *minor_status, gss_const_OID mech_oid,
43*7f2fe78bSCy Schubert                        gss_cred_id_t cred_handle, gss_ctx_id_t *context_handle,
44*7f2fe78bSCy Schubert                        const gss_name_t targ_name, OM_uint32 req_flags,
45*7f2fe78bSCy Schubert                        gss_buffer_t meta_data)
46*7f2fe78bSCy Schubert {
47*7f2fe78bSCy Schubert     OM_uint32 status, minor;
48*7f2fe78bSCy Schubert     gss_union_ctx_id_t ctx = (gss_union_ctx_id_t)*context_handle;
49*7f2fe78bSCy Schubert     gss_union_cred_t cred = (gss_union_cred_t)cred_handle;
50*7f2fe78bSCy Schubert     gss_union_name_t union_name = (gss_union_name_t)targ_name;
51*7f2fe78bSCy Schubert     gss_mechanism mech;
52*7f2fe78bSCy Schubert     gss_OID selected_mech, public_mech;
53*7f2fe78bSCy Schubert     gss_cred_id_t internal_cred = GSS_C_NO_CREDENTIAL;
54*7f2fe78bSCy Schubert     gss_name_t internal_name = GSS_C_NO_NAME, imported_name = GSS_C_NO_NAME;
55*7f2fe78bSCy Schubert     gss_ctx_id_t new_ctx = GSS_C_NO_CONTEXT, *internal_ctx;
56*7f2fe78bSCy Schubert 
57*7f2fe78bSCy Schubert     *minor_status = 0;
58*7f2fe78bSCy Schubert     meta_data->length = 0;
59*7f2fe78bSCy Schubert     meta_data->value = NULL;
60*7f2fe78bSCy Schubert 
61*7f2fe78bSCy Schubert     status = gssint_select_mech_type(minor_status, mech_oid, &selected_mech);
62*7f2fe78bSCy Schubert     if (status != GSS_S_COMPLETE)
63*7f2fe78bSCy Schubert         return status;
64*7f2fe78bSCy Schubert     public_mech = gssint_get_public_oid(selected_mech);
65*7f2fe78bSCy Schubert 
66*7f2fe78bSCy Schubert     mech = gssint_get_mechanism(selected_mech);
67*7f2fe78bSCy Schubert     if (mech == NULL)
68*7f2fe78bSCy Schubert         return GSS_S_BAD_MECH;
69*7f2fe78bSCy Schubert     if (mech->gssspi_query_meta_data == NULL)
70*7f2fe78bSCy Schubert         return GSS_S_UNAVAILABLE;
71*7f2fe78bSCy Schubert 
72*7f2fe78bSCy Schubert     if (cred != NULL) {
73*7f2fe78bSCy Schubert         internal_cred = gssint_get_mechanism_cred(cred, selected_mech);
74*7f2fe78bSCy Schubert         if (internal_cred == GSS_C_NO_CREDENTIAL)
75*7f2fe78bSCy Schubert             return GSS_S_NO_CRED;
76*7f2fe78bSCy Schubert     }
77*7f2fe78bSCy Schubert 
78*7f2fe78bSCy Schubert     if (union_name != NULL) {
79*7f2fe78bSCy Schubert         if (union_name->mech_type != GSS_C_NO_OID &&
80*7f2fe78bSCy Schubert             g_OID_equal(union_name->mech_type, selected_mech)) {
81*7f2fe78bSCy Schubert             internal_name = union_name->mech_name;
82*7f2fe78bSCy Schubert         } else {
83*7f2fe78bSCy Schubert             status = gssint_import_internal_name(minor_status, selected_mech,
84*7f2fe78bSCy Schubert                                                  union_name, &imported_name);
85*7f2fe78bSCy Schubert             if (status != GSS_S_COMPLETE)
86*7f2fe78bSCy Schubert                 goto cleanup;
87*7f2fe78bSCy Schubert             internal_name = imported_name;
88*7f2fe78bSCy Schubert         }
89*7f2fe78bSCy Schubert     }
90*7f2fe78bSCy Schubert 
91*7f2fe78bSCy Schubert     internal_ctx = (ctx != NULL) ? &ctx->internal_ctx_id : &new_ctx;
92*7f2fe78bSCy Schubert     status = mech->gssspi_query_meta_data(minor_status, public_mech,
93*7f2fe78bSCy Schubert                                           internal_cred, internal_ctx,
94*7f2fe78bSCy Schubert                                           internal_name, req_flags, meta_data);
95*7f2fe78bSCy Schubert     if (status != GSS_S_COMPLETE) {
96*7f2fe78bSCy Schubert         map_error(minor_status, mech);
97*7f2fe78bSCy Schubert         goto cleanup;
98*7f2fe78bSCy Schubert     }
99*7f2fe78bSCy Schubert 
100*7f2fe78bSCy Schubert     /* If the mech created a context, wrap it in a union context. */
101*7f2fe78bSCy Schubert     if (new_ctx != GSS_C_NO_CONTEXT) {
102*7f2fe78bSCy Schubert         assert(ctx == NULL);
103*7f2fe78bSCy Schubert         status = gssint_create_union_context(minor_status, selected_mech,
104*7f2fe78bSCy Schubert                                              &ctx);
105*7f2fe78bSCy Schubert         if (status != GSS_S_COMPLETE)
106*7f2fe78bSCy Schubert             goto cleanup;
107*7f2fe78bSCy Schubert 
108*7f2fe78bSCy Schubert         ctx->internal_ctx_id = new_ctx;
109*7f2fe78bSCy Schubert         new_ctx = GSS_C_NO_CONTEXT;
110*7f2fe78bSCy Schubert         *context_handle = (gss_ctx_id_t)ctx;
111*7f2fe78bSCy Schubert     }
112*7f2fe78bSCy Schubert 
113*7f2fe78bSCy Schubert cleanup:
114*7f2fe78bSCy Schubert     if (imported_name != GSS_C_NO_NAME) {
115*7f2fe78bSCy Schubert         (void)gssint_release_internal_name(&minor, selected_mech,
116*7f2fe78bSCy Schubert                                            &imported_name);
117*7f2fe78bSCy Schubert     }
118*7f2fe78bSCy Schubert     if (new_ctx != GSS_C_NO_CONTEXT) {
119*7f2fe78bSCy Schubert         (void)gssint_delete_internal_sec_context(&minor, &mech->mech_type,
120*7f2fe78bSCy Schubert                                                  &new_ctx, GSS_C_NO_BUFFER);
121*7f2fe78bSCy Schubert     }
122*7f2fe78bSCy Schubert     return status;
123*7f2fe78bSCy Schubert }
124*7f2fe78bSCy Schubert 
125*7f2fe78bSCy Schubert OM_uint32 KRB5_CALLCONV
gssspi_exchange_meta_data(OM_uint32 * minor_status,gss_const_OID mech_oid,gss_cred_id_t cred_handle,gss_ctx_id_t * context_handle,const gss_name_t targ_name,OM_uint32 req_flags,gss_const_buffer_t meta_data)126*7f2fe78bSCy Schubert gssspi_exchange_meta_data(OM_uint32 *minor_status, gss_const_OID mech_oid,
127*7f2fe78bSCy Schubert                           gss_cred_id_t cred_handle,
128*7f2fe78bSCy Schubert                           gss_ctx_id_t *context_handle,
129*7f2fe78bSCy Schubert                           const gss_name_t targ_name, OM_uint32 req_flags,
130*7f2fe78bSCy Schubert                           gss_const_buffer_t meta_data)
131*7f2fe78bSCy Schubert {
132*7f2fe78bSCy Schubert     OM_uint32 status, minor;
133*7f2fe78bSCy Schubert     gss_union_ctx_id_t ctx = (gss_union_ctx_id_t)*context_handle;
134*7f2fe78bSCy Schubert     gss_union_cred_t cred = (gss_union_cred_t)cred_handle;
135*7f2fe78bSCy Schubert     gss_union_name_t union_name = (gss_union_name_t)targ_name;
136*7f2fe78bSCy Schubert     gss_mechanism mech;
137*7f2fe78bSCy Schubert     gss_OID selected_mech, public_mech;
138*7f2fe78bSCy Schubert     gss_cred_id_t internal_cred = GSS_C_NO_CREDENTIAL;
139*7f2fe78bSCy Schubert     gss_name_t internal_name = GSS_C_NO_NAME, imported_name = GSS_C_NO_NAME;
140*7f2fe78bSCy Schubert     gss_ctx_id_t new_ctx = GSS_C_NO_CONTEXT, *internal_ctx;
141*7f2fe78bSCy Schubert 
142*7f2fe78bSCy Schubert     *minor_status = 0;
143*7f2fe78bSCy Schubert 
144*7f2fe78bSCy Schubert     status = gssint_select_mech_type(minor_status, mech_oid, &selected_mech);
145*7f2fe78bSCy Schubert     if (status != GSS_S_COMPLETE)
146*7f2fe78bSCy Schubert         return status;
147*7f2fe78bSCy Schubert     public_mech = gssint_get_public_oid(selected_mech);
148*7f2fe78bSCy Schubert 
149*7f2fe78bSCy Schubert     mech = gssint_get_mechanism(selected_mech);
150*7f2fe78bSCy Schubert     if (mech == NULL)
151*7f2fe78bSCy Schubert         return GSS_S_BAD_MECH;
152*7f2fe78bSCy Schubert     if (mech->gssspi_exchange_meta_data == NULL)
153*7f2fe78bSCy Schubert         return GSS_S_UNAVAILABLE;
154*7f2fe78bSCy Schubert 
155*7f2fe78bSCy Schubert     if (cred != NULL) {
156*7f2fe78bSCy Schubert         internal_cred = gssint_get_mechanism_cred(cred, selected_mech);
157*7f2fe78bSCy Schubert         if (internal_cred == GSS_C_NO_CREDENTIAL)
158*7f2fe78bSCy Schubert             return GSS_S_NO_CRED;
159*7f2fe78bSCy Schubert     }
160*7f2fe78bSCy Schubert 
161*7f2fe78bSCy Schubert     if (union_name != NULL) {
162*7f2fe78bSCy Schubert         if (union_name->mech_type != GSS_C_NO_OID &&
163*7f2fe78bSCy Schubert             g_OID_equal(union_name->mech_type, selected_mech)) {
164*7f2fe78bSCy Schubert             internal_name = union_name->mech_name;
165*7f2fe78bSCy Schubert         } else {
166*7f2fe78bSCy Schubert             status = gssint_import_internal_name(minor_status, selected_mech,
167*7f2fe78bSCy Schubert                                                  union_name, &imported_name);
168*7f2fe78bSCy Schubert             if (GSS_ERROR(status))
169*7f2fe78bSCy Schubert                 return status;
170*7f2fe78bSCy Schubert             internal_name = imported_name;
171*7f2fe78bSCy Schubert         }
172*7f2fe78bSCy Schubert     }
173*7f2fe78bSCy Schubert 
174*7f2fe78bSCy Schubert     internal_ctx = (ctx != NULL) ? &ctx->internal_ctx_id : &new_ctx;
175*7f2fe78bSCy Schubert     status = mech->gssspi_exchange_meta_data(minor_status, public_mech,
176*7f2fe78bSCy Schubert                                              internal_cred, internal_ctx,
177*7f2fe78bSCy Schubert                                              internal_name, req_flags,
178*7f2fe78bSCy Schubert                                              meta_data);
179*7f2fe78bSCy Schubert     if (status != GSS_S_COMPLETE) {
180*7f2fe78bSCy Schubert         map_error(minor_status, mech);
181*7f2fe78bSCy Schubert         goto cleanup;
182*7f2fe78bSCy Schubert     }
183*7f2fe78bSCy Schubert 
184*7f2fe78bSCy Schubert     /* If the mech created a context, wrap it in a union context. */
185*7f2fe78bSCy Schubert     if (new_ctx != GSS_C_NO_CONTEXT) {
186*7f2fe78bSCy Schubert         assert(ctx == NULL);
187*7f2fe78bSCy Schubert         status = gssint_create_union_context(minor_status, selected_mech,
188*7f2fe78bSCy Schubert                                              &ctx);
189*7f2fe78bSCy Schubert         if (status != GSS_S_COMPLETE)
190*7f2fe78bSCy Schubert             goto cleanup;
191*7f2fe78bSCy Schubert 
192*7f2fe78bSCy Schubert         ctx->internal_ctx_id = new_ctx;
193*7f2fe78bSCy Schubert         new_ctx = GSS_C_NO_CONTEXT;
194*7f2fe78bSCy Schubert         *context_handle = (gss_ctx_id_t)ctx;
195*7f2fe78bSCy Schubert     }
196*7f2fe78bSCy Schubert 
197*7f2fe78bSCy Schubert cleanup:
198*7f2fe78bSCy Schubert     if (imported_name != GSS_C_NO_NAME) {
199*7f2fe78bSCy Schubert         (void)gssint_release_internal_name(&minor, selected_mech,
200*7f2fe78bSCy Schubert                                            &imported_name);
201*7f2fe78bSCy Schubert     }
202*7f2fe78bSCy Schubert     if (new_ctx != GSS_C_NO_CONTEXT) {
203*7f2fe78bSCy Schubert         (void)gssint_delete_internal_sec_context(&minor, &mech->mech_type,
204*7f2fe78bSCy Schubert                                                  &new_ctx, GSS_C_NO_BUFFER);
205*7f2fe78bSCy Schubert     }
206*7f2fe78bSCy Schubert     return status;
207*7f2fe78bSCy Schubert }
208*7f2fe78bSCy Schubert 
209*7f2fe78bSCy Schubert OM_uint32 KRB5_CALLCONV
gssspi_query_mechanism_info(OM_uint32 * minor_status,gss_const_OID mech_oid,unsigned char auth_scheme[16])210*7f2fe78bSCy Schubert gssspi_query_mechanism_info(OM_uint32 *minor_status, gss_const_OID mech_oid,
211*7f2fe78bSCy Schubert                             unsigned char auth_scheme[16])
212*7f2fe78bSCy Schubert {
213*7f2fe78bSCy Schubert     OM_uint32 status;
214*7f2fe78bSCy Schubert     gss_OID selected_mech, public_mech;
215*7f2fe78bSCy Schubert     gss_mechanism mech;
216*7f2fe78bSCy Schubert 
217*7f2fe78bSCy Schubert     *minor_status = 0;
218*7f2fe78bSCy Schubert     memset(auth_scheme, 0, 16);
219*7f2fe78bSCy Schubert 
220*7f2fe78bSCy Schubert     status = gssint_select_mech_type(minor_status, mech_oid, &selected_mech);
221*7f2fe78bSCy Schubert     if (status != GSS_S_COMPLETE)
222*7f2fe78bSCy Schubert         return status;
223*7f2fe78bSCy Schubert     public_mech = gssint_get_public_oid(selected_mech);
224*7f2fe78bSCy Schubert 
225*7f2fe78bSCy Schubert     mech = gssint_get_mechanism(selected_mech);
226*7f2fe78bSCy Schubert     if (mech == NULL)
227*7f2fe78bSCy Schubert         return GSS_S_BAD_MECH;
228*7f2fe78bSCy Schubert     if (mech->gssspi_query_mechanism_info == NULL)
229*7f2fe78bSCy Schubert         return GSS_S_UNAVAILABLE;
230*7f2fe78bSCy Schubert 
231*7f2fe78bSCy Schubert     status = mech->gssspi_query_mechanism_info(minor_status, public_mech,
232*7f2fe78bSCy Schubert                                                auth_scheme);
233*7f2fe78bSCy Schubert     if (GSS_ERROR(status))
234*7f2fe78bSCy Schubert         map_error(minor_status, mech);
235*7f2fe78bSCy Schubert 
236*7f2fe78bSCy Schubert     return status;
237*7f2fe78bSCy Schubert }
238