xref: /freebsd/crypto/krb5/src/lib/gssapi/mechglue/g_seal.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /*
3*7f2fe78bSCy Schubert  * Copyright 1996 by Sun Microsystems, Inc.
4*7f2fe78bSCy Schubert  *
5*7f2fe78bSCy Schubert  * Permission to use, copy, modify, distribute, and sell this software
6*7f2fe78bSCy Schubert  * and its documentation for any purpose is hereby granted without fee,
7*7f2fe78bSCy Schubert  * provided that the above copyright notice appears in all copies and
8*7f2fe78bSCy Schubert  * that both that copyright notice and this permission notice appear in
9*7f2fe78bSCy Schubert  * supporting documentation, and that the name of Sun Microsystems not be used
10*7f2fe78bSCy Schubert  * in advertising or publicity pertaining to distribution of the software
11*7f2fe78bSCy Schubert  * without specific, written prior permission. Sun Microsystems makes no
12*7f2fe78bSCy Schubert  * representations about the suitability of this software for any
13*7f2fe78bSCy Schubert  * purpose.  It is provided "as is" without express or implied warranty.
14*7f2fe78bSCy Schubert  *
15*7f2fe78bSCy Schubert  * SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16*7f2fe78bSCy Schubert  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17*7f2fe78bSCy Schubert  * EVENT SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18*7f2fe78bSCy Schubert  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19*7f2fe78bSCy Schubert  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
20*7f2fe78bSCy Schubert  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21*7f2fe78bSCy Schubert  * PERFORMANCE OF THIS SOFTWARE.
22*7f2fe78bSCy Schubert  */
23*7f2fe78bSCy Schubert 
24*7f2fe78bSCy Schubert /*
25*7f2fe78bSCy Schubert  *  glue routine for gss_wrap
26*7f2fe78bSCy Schubert  */
27*7f2fe78bSCy Schubert 
28*7f2fe78bSCy Schubert #include "mglueP.h"
29*7f2fe78bSCy Schubert 
30*7f2fe78bSCy Schubert static OM_uint32
val_wrap_args(OM_uint32 * minor_status,gss_ctx_id_t context_handle,int conf_req_flag,gss_qop_t qop_req,gss_buffer_t input_message_buffer,int * conf_state,gss_buffer_t output_message_buffer)31*7f2fe78bSCy Schubert val_wrap_args(OM_uint32 *minor_status,
32*7f2fe78bSCy Schubert               gss_ctx_id_t context_handle,
33*7f2fe78bSCy Schubert               int conf_req_flag,
34*7f2fe78bSCy Schubert               gss_qop_t qop_req,
35*7f2fe78bSCy Schubert               gss_buffer_t input_message_buffer,
36*7f2fe78bSCy Schubert               int *conf_state,
37*7f2fe78bSCy Schubert               gss_buffer_t output_message_buffer)
38*7f2fe78bSCy Schubert {
39*7f2fe78bSCy Schubert     /* Initialize outputs. */
40*7f2fe78bSCy Schubert 
41*7f2fe78bSCy Schubert     if (minor_status != NULL)
42*7f2fe78bSCy Schubert         *minor_status = 0;
43*7f2fe78bSCy Schubert 
44*7f2fe78bSCy Schubert     if (output_message_buffer != GSS_C_NO_BUFFER) {
45*7f2fe78bSCy Schubert         output_message_buffer->length = 0;
46*7f2fe78bSCy Schubert         output_message_buffer->value = NULL;
47*7f2fe78bSCy Schubert     }
48*7f2fe78bSCy Schubert 
49*7f2fe78bSCy Schubert     /* Validate arguments. */
50*7f2fe78bSCy Schubert 
51*7f2fe78bSCy Schubert     if (minor_status == NULL)
52*7f2fe78bSCy Schubert         return (GSS_S_CALL_INACCESSIBLE_WRITE);
53*7f2fe78bSCy Schubert 
54*7f2fe78bSCy Schubert     if (context_handle == GSS_C_NO_CONTEXT)
55*7f2fe78bSCy Schubert         return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_NO_CONTEXT);
56*7f2fe78bSCy Schubert 
57*7f2fe78bSCy Schubert     if (input_message_buffer == GSS_C_NO_BUFFER)
58*7f2fe78bSCy Schubert         return (GSS_S_CALL_INACCESSIBLE_READ);
59*7f2fe78bSCy Schubert 
60*7f2fe78bSCy Schubert     if (output_message_buffer == GSS_C_NO_BUFFER)
61*7f2fe78bSCy Schubert         return (GSS_S_CALL_INACCESSIBLE_WRITE);
62*7f2fe78bSCy Schubert 
63*7f2fe78bSCy Schubert     return (GSS_S_COMPLETE);
64*7f2fe78bSCy Schubert }
65*7f2fe78bSCy Schubert 
66*7f2fe78bSCy Schubert OM_uint32 KRB5_CALLCONV
gss_wrap(OM_uint32 * minor_status,gss_ctx_id_t context_handle,int conf_req_flag,gss_qop_t qop_req,gss_buffer_t input_message_buffer,int * conf_state,gss_buffer_t output_message_buffer)67*7f2fe78bSCy Schubert gss_wrap( OM_uint32 *minor_status,
68*7f2fe78bSCy Schubert           gss_ctx_id_t context_handle,
69*7f2fe78bSCy Schubert           int conf_req_flag,
70*7f2fe78bSCy Schubert           gss_qop_t qop_req,
71*7f2fe78bSCy Schubert           gss_buffer_t input_message_buffer,
72*7f2fe78bSCy Schubert           int *conf_state,
73*7f2fe78bSCy Schubert           gss_buffer_t output_message_buffer)
74*7f2fe78bSCy Schubert {
75*7f2fe78bSCy Schubert 
76*7f2fe78bSCy Schubert     /* EXPORT DELETE START */
77*7f2fe78bSCy Schubert 
78*7f2fe78bSCy Schubert     OM_uint32           status;
79*7f2fe78bSCy Schubert     gss_union_ctx_id_t  ctx;
80*7f2fe78bSCy Schubert     gss_mechanism       mech;
81*7f2fe78bSCy Schubert 
82*7f2fe78bSCy Schubert     status = val_wrap_args(minor_status, context_handle,
83*7f2fe78bSCy Schubert                            conf_req_flag, qop_req,
84*7f2fe78bSCy Schubert                            input_message_buffer, conf_state,
85*7f2fe78bSCy Schubert                            output_message_buffer);
86*7f2fe78bSCy Schubert     if (status != GSS_S_COMPLETE)
87*7f2fe78bSCy Schubert         return (status);
88*7f2fe78bSCy Schubert 
89*7f2fe78bSCy Schubert     /*
90*7f2fe78bSCy Schubert      * select the approprate underlying mechanism routine and
91*7f2fe78bSCy Schubert      * call it.
92*7f2fe78bSCy Schubert      */
93*7f2fe78bSCy Schubert 
94*7f2fe78bSCy Schubert     ctx = (gss_union_ctx_id_t) context_handle;
95*7f2fe78bSCy Schubert     if (ctx->internal_ctx_id == GSS_C_NO_CONTEXT)
96*7f2fe78bSCy Schubert         return (GSS_S_NO_CONTEXT);
97*7f2fe78bSCy Schubert     mech = gssint_get_mechanism (ctx->mech_type);
98*7f2fe78bSCy Schubert 
99*7f2fe78bSCy Schubert     if (mech) {
100*7f2fe78bSCy Schubert         if (mech->gss_wrap) {
101*7f2fe78bSCy Schubert             status = mech->gss_wrap(minor_status,
102*7f2fe78bSCy Schubert                                     ctx->internal_ctx_id,
103*7f2fe78bSCy Schubert                                     conf_req_flag,
104*7f2fe78bSCy Schubert                                     qop_req,
105*7f2fe78bSCy Schubert                                     input_message_buffer,
106*7f2fe78bSCy Schubert                                     conf_state,
107*7f2fe78bSCy Schubert                                     output_message_buffer);
108*7f2fe78bSCy Schubert             if (status != GSS_S_COMPLETE)
109*7f2fe78bSCy Schubert                 map_error(minor_status, mech);
110*7f2fe78bSCy Schubert         } else if (mech->gss_wrap_aead ||
111*7f2fe78bSCy Schubert                    (mech->gss_wrap_iov && mech->gss_wrap_iov_length)) {
112*7f2fe78bSCy Schubert             status = gssint_wrap_aead(mech,
113*7f2fe78bSCy Schubert                                       minor_status,
114*7f2fe78bSCy Schubert                                       ctx,
115*7f2fe78bSCy Schubert                                       conf_req_flag,
116*7f2fe78bSCy Schubert                                       (gss_qop_t)qop_req,
117*7f2fe78bSCy Schubert                                       GSS_C_NO_BUFFER,
118*7f2fe78bSCy Schubert                                       input_message_buffer,
119*7f2fe78bSCy Schubert                                       conf_state,
120*7f2fe78bSCy Schubert                                       output_message_buffer);
121*7f2fe78bSCy Schubert         } else
122*7f2fe78bSCy Schubert             status = GSS_S_UNAVAILABLE;
123*7f2fe78bSCy Schubert 
124*7f2fe78bSCy Schubert         return(status);
125*7f2fe78bSCy Schubert     }
126*7f2fe78bSCy Schubert     /* EXPORT DELETE END */
127*7f2fe78bSCy Schubert 
128*7f2fe78bSCy Schubert     return (GSS_S_BAD_MECH);
129*7f2fe78bSCy Schubert }
130*7f2fe78bSCy Schubert 
131*7f2fe78bSCy Schubert OM_uint32 KRB5_CALLCONV
gss_seal(OM_uint32 * minor_status,gss_ctx_id_t context_handle,int conf_req_flag,int qop_req,gss_buffer_t input_message_buffer,int * conf_state,gss_buffer_t output_message_buffer)132*7f2fe78bSCy Schubert gss_seal(OM_uint32 *minor_status,
133*7f2fe78bSCy Schubert          gss_ctx_id_t context_handle,
134*7f2fe78bSCy Schubert          int conf_req_flag,
135*7f2fe78bSCy Schubert          int qop_req,
136*7f2fe78bSCy Schubert          gss_buffer_t input_message_buffer,
137*7f2fe78bSCy Schubert          int *conf_state,
138*7f2fe78bSCy Schubert          gss_buffer_t output_message_buffer)
139*7f2fe78bSCy Schubert {
140*7f2fe78bSCy Schubert 
141*7f2fe78bSCy Schubert     return gss_wrap(minor_status, context_handle,
142*7f2fe78bSCy Schubert                     conf_req_flag, (gss_qop_t) qop_req,
143*7f2fe78bSCy Schubert                     input_message_buffer, conf_state,
144*7f2fe78bSCy Schubert                     output_message_buffer);
145*7f2fe78bSCy Schubert }
146*7f2fe78bSCy Schubert 
147*7f2fe78bSCy Schubert /*
148*7f2fe78bSCy Schubert  * It is only possible to implement gss_wrap_size_limit() on top
149*7f2fe78bSCy Schubert  * of gss_wrap_iov_length() for mechanisms that do not use any
150*7f2fe78bSCy Schubert  * padding and have fixed length headers/trailers.
151*7f2fe78bSCy Schubert  */
152*7f2fe78bSCy Schubert static OM_uint32
gssint_wrap_size_limit_iov_shim(gss_mechanism mech,OM_uint32 * minor_status,gss_ctx_id_t context_handle,int conf_req_flag,gss_qop_t qop_req,OM_uint32 req_output_size,OM_uint32 * max_input_size)153*7f2fe78bSCy Schubert gssint_wrap_size_limit_iov_shim(gss_mechanism mech,
154*7f2fe78bSCy Schubert                                 OM_uint32 *minor_status,
155*7f2fe78bSCy Schubert                                 gss_ctx_id_t context_handle,
156*7f2fe78bSCy Schubert                                 int conf_req_flag,
157*7f2fe78bSCy Schubert                                 gss_qop_t qop_req,
158*7f2fe78bSCy Schubert                                 OM_uint32 req_output_size,
159*7f2fe78bSCy Schubert                                 OM_uint32 *max_input_size)
160*7f2fe78bSCy Schubert {
161*7f2fe78bSCy Schubert     gss_iov_buffer_desc iov[4];
162*7f2fe78bSCy Schubert     OM_uint32           status;
163*7f2fe78bSCy Schubert     OM_uint32           ohlen;
164*7f2fe78bSCy Schubert 
165*7f2fe78bSCy Schubert     iov[0].type = GSS_IOV_BUFFER_TYPE_HEADER;
166*7f2fe78bSCy Schubert     iov[0].buffer.value = NULL;
167*7f2fe78bSCy Schubert     iov[0].buffer.length = 0;
168*7f2fe78bSCy Schubert 
169*7f2fe78bSCy Schubert     iov[1].type = GSS_IOV_BUFFER_TYPE_DATA;
170*7f2fe78bSCy Schubert     iov[1].buffer.length = req_output_size;
171*7f2fe78bSCy Schubert     iov[1].buffer.value = NULL;
172*7f2fe78bSCy Schubert 
173*7f2fe78bSCy Schubert     iov[2].type = GSS_IOV_BUFFER_TYPE_PADDING;
174*7f2fe78bSCy Schubert     iov[2].buffer.value = NULL;
175*7f2fe78bSCy Schubert     iov[2].buffer.length = 0;
176*7f2fe78bSCy Schubert 
177*7f2fe78bSCy Schubert     iov[3].type = GSS_IOV_BUFFER_TYPE_TRAILER;
178*7f2fe78bSCy Schubert     iov[3].buffer.value = NULL;
179*7f2fe78bSCy Schubert     iov[3].buffer.length = 0;
180*7f2fe78bSCy Schubert 
181*7f2fe78bSCy Schubert     assert(mech->gss_wrap_iov_length);
182*7f2fe78bSCy Schubert 
183*7f2fe78bSCy Schubert     status = mech->gss_wrap_iov_length(minor_status, context_handle,
184*7f2fe78bSCy Schubert                                        conf_req_flag, qop_req,
185*7f2fe78bSCy Schubert                                        NULL, iov,
186*7f2fe78bSCy Schubert                                        sizeof(iov)/sizeof(iov[0]));
187*7f2fe78bSCy Schubert     if (status != GSS_S_COMPLETE) {
188*7f2fe78bSCy Schubert         map_error(minor_status, mech);
189*7f2fe78bSCy Schubert         return status;
190*7f2fe78bSCy Schubert     }
191*7f2fe78bSCy Schubert 
192*7f2fe78bSCy Schubert     ohlen = iov[0].buffer.length + iov[3].buffer.length;
193*7f2fe78bSCy Schubert 
194*7f2fe78bSCy Schubert     if (iov[2].buffer.length == 0 && ohlen < req_output_size)
195*7f2fe78bSCy Schubert         *max_input_size = req_output_size - ohlen;
196*7f2fe78bSCy Schubert     else
197*7f2fe78bSCy Schubert         *max_input_size = 0;
198*7f2fe78bSCy Schubert 
199*7f2fe78bSCy Schubert     return GSS_S_COMPLETE;
200*7f2fe78bSCy Schubert }
201*7f2fe78bSCy Schubert 
202*7f2fe78bSCy Schubert /*
203*7f2fe78bSCy Schubert  * New for V2
204*7f2fe78bSCy Schubert  */
205*7f2fe78bSCy Schubert OM_uint32 KRB5_CALLCONV
gss_wrap_size_limit(OM_uint32 * minor_status,gss_ctx_id_t context_handle,int conf_req_flag,gss_qop_t qop_req,OM_uint32 req_output_size,OM_uint32 * max_input_size)206*7f2fe78bSCy Schubert gss_wrap_size_limit(OM_uint32  *minor_status,
207*7f2fe78bSCy Schubert                     gss_ctx_id_t context_handle,
208*7f2fe78bSCy Schubert                     int conf_req_flag,
209*7f2fe78bSCy Schubert                     gss_qop_t qop_req, OM_uint32 req_output_size, OM_uint32 *max_input_size)
210*7f2fe78bSCy Schubert {
211*7f2fe78bSCy Schubert     gss_union_ctx_id_t  ctx;
212*7f2fe78bSCy Schubert     gss_mechanism       mech;
213*7f2fe78bSCy Schubert     OM_uint32           major_status;
214*7f2fe78bSCy Schubert 
215*7f2fe78bSCy Schubert     if (minor_status == NULL)
216*7f2fe78bSCy Schubert         return (GSS_S_CALL_INACCESSIBLE_WRITE);
217*7f2fe78bSCy Schubert     *minor_status = 0;
218*7f2fe78bSCy Schubert 
219*7f2fe78bSCy Schubert     if (context_handle == GSS_C_NO_CONTEXT)
220*7f2fe78bSCy Schubert         return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_NO_CONTEXT);
221*7f2fe78bSCy Schubert 
222*7f2fe78bSCy Schubert     if (max_input_size == NULL)
223*7f2fe78bSCy Schubert         return (GSS_S_CALL_INACCESSIBLE_WRITE);
224*7f2fe78bSCy Schubert 
225*7f2fe78bSCy Schubert     /*
226*7f2fe78bSCy Schubert      * select the approprate underlying mechanism routine and
227*7f2fe78bSCy Schubert      * call it.
228*7f2fe78bSCy Schubert      */
229*7f2fe78bSCy Schubert 
230*7f2fe78bSCy Schubert     ctx = (gss_union_ctx_id_t) context_handle;
231*7f2fe78bSCy Schubert     if (ctx->internal_ctx_id == GSS_C_NO_CONTEXT)
232*7f2fe78bSCy Schubert         return (GSS_S_NO_CONTEXT);
233*7f2fe78bSCy Schubert     mech = gssint_get_mechanism (ctx->mech_type);
234*7f2fe78bSCy Schubert 
235*7f2fe78bSCy Schubert     if (!mech)
236*7f2fe78bSCy Schubert         return (GSS_S_BAD_MECH);
237*7f2fe78bSCy Schubert 
238*7f2fe78bSCy Schubert     if (mech->gss_wrap_size_limit)
239*7f2fe78bSCy Schubert         major_status = mech->gss_wrap_size_limit(minor_status,
240*7f2fe78bSCy Schubert                                                  ctx->internal_ctx_id,
241*7f2fe78bSCy Schubert                                                  conf_req_flag, qop_req,
242*7f2fe78bSCy Schubert                                                  req_output_size, max_input_size);
243*7f2fe78bSCy Schubert     else if (mech->gss_wrap_iov_length)
244*7f2fe78bSCy Schubert         major_status = gssint_wrap_size_limit_iov_shim(mech, minor_status,
245*7f2fe78bSCy Schubert                                                        ctx->internal_ctx_id,
246*7f2fe78bSCy Schubert                                                        conf_req_flag, qop_req,
247*7f2fe78bSCy Schubert                                                        req_output_size, max_input_size);
248*7f2fe78bSCy Schubert     else
249*7f2fe78bSCy Schubert         major_status = GSS_S_UNAVAILABLE;
250*7f2fe78bSCy Schubert     if (major_status != GSS_S_COMPLETE)
251*7f2fe78bSCy Schubert         map_error(minor_status, mech);
252*7f2fe78bSCy Schubert     return major_status;
253*7f2fe78bSCy Schubert }
254