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