1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*7c478bd9Sstevel@tonic-gate 
8*7c478bd9Sstevel@tonic-gate /*
9*7c478bd9Sstevel@tonic-gate  * Copyright 2000 by the Massachusetts Institute of Technology.
10*7c478bd9Sstevel@tonic-gate  * All Rights Reserved.
11*7c478bd9Sstevel@tonic-gate  *
12*7c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
13*7c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
14*7c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
15*7c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
16*7c478bd9Sstevel@tonic-gate  *
17*7c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
18*7c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
19*7c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
20*7c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
21*7c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
22*7c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
23*7c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
24*7c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
25*7c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
26*7c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
27*7c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
28*7c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
29*7c478bd9Sstevel@tonic-gate  * or implied warranty.
30*7c478bd9Sstevel@tonic-gate  *
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate /*
33*7c478bd9Sstevel@tonic-gate  * Copyright 1993 by OpenVision Technologies, Inc.
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, distribute, and sell this software
36*7c478bd9Sstevel@tonic-gate  * and its documentation for any purpose is hereby granted without fee,
37*7c478bd9Sstevel@tonic-gate  * provided that the above copyright notice appears in all copies and
38*7c478bd9Sstevel@tonic-gate  * that both that copyright notice and this permission notice appear in
39*7c478bd9Sstevel@tonic-gate  * supporting documentation, and that the name of OpenVision not be used
40*7c478bd9Sstevel@tonic-gate  * in advertising or publicity pertaining to distribution of the software
41*7c478bd9Sstevel@tonic-gate  * without specific, written prior permission. OpenVision makes no
42*7c478bd9Sstevel@tonic-gate  * representations about the suitability of this software for any
43*7c478bd9Sstevel@tonic-gate  * purpose.  It is provided "as is" without express or implied warranty.
44*7c478bd9Sstevel@tonic-gate  *
45*7c478bd9Sstevel@tonic-gate  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
46*7c478bd9Sstevel@tonic-gate  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
47*7c478bd9Sstevel@tonic-gate  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
48*7c478bd9Sstevel@tonic-gate  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
49*7c478bd9Sstevel@tonic-gate  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
50*7c478bd9Sstevel@tonic-gate  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
51*7c478bd9Sstevel@tonic-gate  * PERFORMANCE OF THIS SOFTWARE.
52*7c478bd9Sstevel@tonic-gate  */
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate /*
55*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1998 by the FundsXpress, INC.
56*7c478bd9Sstevel@tonic-gate  *
57*7c478bd9Sstevel@tonic-gate  * All rights reserved.
58*7c478bd9Sstevel@tonic-gate  *
59*7c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may require
60*7c478bd9Sstevel@tonic-gate  * a specific license from the United States Government.  It is the
61*7c478bd9Sstevel@tonic-gate  * responsibility of any person or organization contemplating export to
62*7c478bd9Sstevel@tonic-gate  * obtain such a license before exporting.
63*7c478bd9Sstevel@tonic-gate  *
64*7c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
65*7c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
66*7c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
67*7c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
68*7c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
69*7c478bd9Sstevel@tonic-gate  * the name of FundsXpress. not be used in advertising or publicity pertaining
70*7c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
71*7c478bd9Sstevel@tonic-gate  * permission.  FundsXpress makes no representations about the suitability of
72*7c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
73*7c478bd9Sstevel@tonic-gate  * or implied warranty.
74*7c478bd9Sstevel@tonic-gate  *
75*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
76*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
77*7c478bd9Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
78*7c478bd9Sstevel@tonic-gate  */
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate #include <gssapiP_krb5.h>
81*7c478bd9Sstevel@tonic-gate #include <k5-int.h>
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate /*
84*7c478bd9Sstevel@tonic-gate  * $Id: wrap_size_limit.c,v 1.7.6.2 2000/04/19 00:33:42 raeburn Exp $
85*7c478bd9Sstevel@tonic-gate  */
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate /* V2 interface */
88*7c478bd9Sstevel@tonic-gate  /*ARGSUSED*/
89*7c478bd9Sstevel@tonic-gate OM_uint32
90*7c478bd9Sstevel@tonic-gate krb5_gss_wrap_size_limit(ct, minor_status, context_handle, conf_req_flag,
91*7c478bd9Sstevel@tonic-gate 			 qop_req, req_output_size, max_input_size)
92*7c478bd9Sstevel@tonic-gate     void		*ct;
93*7c478bd9Sstevel@tonic-gate     OM_uint32		*minor_status;
94*7c478bd9Sstevel@tonic-gate     gss_ctx_id_t	context_handle;
95*7c478bd9Sstevel@tonic-gate     int			conf_req_flag;
96*7c478bd9Sstevel@tonic-gate     gss_qop_t		qop_req;
97*7c478bd9Sstevel@tonic-gate     OM_uint32		req_output_size;
98*7c478bd9Sstevel@tonic-gate     OM_uint32		*max_input_size;
99*7c478bd9Sstevel@tonic-gate {
100*7c478bd9Sstevel@tonic-gate     krb5_context	context;
101*7c478bd9Sstevel@tonic-gate     krb5_gss_ctx_id_rec	*ctx;
102*7c478bd9Sstevel@tonic-gate     OM_uint32		conflen;
103*7c478bd9Sstevel@tonic-gate     OM_uint32		ohlen;
104*7c478bd9Sstevel@tonic-gate     OM_uint32		data_size;
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate    /* Solaris Kerberos:  for MT safety, we avoid the use of a default
107*7c478bd9Sstevel@tonic-gate     * context via kg_get_context() */
108*7c478bd9Sstevel@tonic-gate #if 0
109*7c478bd9Sstevel@tonic-gate     if (GSS_ERROR(kg_get_context(minor_status, &context)))
110*7c478bd9Sstevel@tonic-gate        return(GSS_S_FAILURE);
111*7c478bd9Sstevel@tonic-gate #endif
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate     KRB5_LOG0(KRB5_INFO, "krb5_gss_wrap_size_limit() start\n");
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate     /* check to make sure we aren't writing to a NULL pointer */
116*7c478bd9Sstevel@tonic-gate     if (!max_input_size)
117*7c478bd9Sstevel@tonic-gate 	return(GSS_S_CALL_INACCESSIBLE_WRITE);
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate     mutex_lock(&krb5_mutex);
120*7c478bd9Sstevel@tonic-gate     context = ct;
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate     /* only default qop is allowed */
123*7c478bd9Sstevel@tonic-gate     if ((qop_req & GSS_KRB5_CONF_C_QOP_MASK) != GSS_C_QOP_DEFAULT) {
124*7c478bd9Sstevel@tonic-gate 	*minor_status = (OM_uint32) G_UNKNOWN_QOP;
125*7c478bd9Sstevel@tonic-gate 	mutex_unlock(&krb5_mutex);
126*7c478bd9Sstevel@tonic-gate 	return(GSS_S_BAD_QOP);
127*7c478bd9Sstevel@tonic-gate     }
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate     /* validate the context handle */
130*7c478bd9Sstevel@tonic-gate     if (! kg_validate_ctx_id(context_handle)) {
131*7c478bd9Sstevel@tonic-gate 	*minor_status = (OM_uint32) G_VALIDATE_FAILED;
132*7c478bd9Sstevel@tonic-gate 	mutex_unlock(&krb5_mutex);
133*7c478bd9Sstevel@tonic-gate 	return(GSS_S_NO_CONTEXT);
134*7c478bd9Sstevel@tonic-gate     }
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate     ctx = (krb5_gss_ctx_id_rec *) context_handle;
137*7c478bd9Sstevel@tonic-gate     if (! ctx->established) {
138*7c478bd9Sstevel@tonic-gate 	*minor_status = KG_CTX_INCOMPLETE;
139*7c478bd9Sstevel@tonic-gate 	mutex_unlock(&krb5_mutex);
140*7c478bd9Sstevel@tonic-gate 	return(GSS_S_NO_CONTEXT);
141*7c478bd9Sstevel@tonic-gate     }
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate     if (ctx->proto == 1) {
144*7c478bd9Sstevel@tonic-gate         /* No pseudo-ASN.1 wrapper overhead, so no sequence length and
145*7c478bd9Sstevel@tonic-gate            OID.  */
146*7c478bd9Sstevel@tonic-gate         OM_uint32 sz = req_output_size;
147*7c478bd9Sstevel@tonic-gate         if (conf_req_flag) {
148*7c478bd9Sstevel@tonic-gate 	    size_t enclen;
149*7c478bd9Sstevel@tonic-gate 	    if ( (*minor_status = krb5_c_encrypt_length(context,
150*7c478bd9Sstevel@tonic-gate 				ctx->enc->enctype,
151*7c478bd9Sstevel@tonic-gate 				sz, &enclen))) {
152*7c478bd9Sstevel@tonic-gate 		mutex_unlock(&krb5_mutex);
153*7c478bd9Sstevel@tonic-gate 		return (GSS_S_FAILURE);
154*7c478bd9Sstevel@tonic-gate 	    }
155*7c478bd9Sstevel@tonic-gate 	    /*
156*7c478bd9Sstevel@tonic-gate 	     * The 16 byte token header is included 2 times,
157*7c478bd9Sstevel@tonic-gate 	     * once at the beginning of the token and once
158*7c478bd9Sstevel@tonic-gate 	     * encrypted with the plaintext data.
159*7c478bd9Sstevel@tonic-gate 	     */
160*7c478bd9Sstevel@tonic-gate             while (sz > 0 && enclen + 32 > req_output_size) {
161*7c478bd9Sstevel@tonic-gate                 sz--;
162*7c478bd9Sstevel@tonic-gate 	        if ((*minor_status = krb5_c_encrypt_length(context,
163*7c478bd9Sstevel@tonic-gate 			ctx->enc->enctype, sz, &enclen))) {
164*7c478bd9Sstevel@tonic-gate 			mutex_unlock(&krb5_mutex);
165*7c478bd9Sstevel@tonic-gate 			return (GSS_S_FAILURE);
166*7c478bd9Sstevel@tonic-gate 		}
167*7c478bd9Sstevel@tonic-gate 	    }
168*7c478bd9Sstevel@tonic-gate         } else {
169*7c478bd9Sstevel@tonic-gate             if (sz < 16 + ctx->cksum_size)
170*7c478bd9Sstevel@tonic-gate                 sz = 0;
171*7c478bd9Sstevel@tonic-gate             else
172*7c478bd9Sstevel@tonic-gate                 sz -= (16 + ctx->cksum_size);
173*7c478bd9Sstevel@tonic-gate         }
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate         *max_input_size = sz;
176*7c478bd9Sstevel@tonic-gate         *minor_status = 0;
177*7c478bd9Sstevel@tonic-gate 	goto end;
178*7c478bd9Sstevel@tonic-gate     }
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate     data_size = req_output_size;
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate     /* The confounder is always used */
183*7c478bd9Sstevel@tonic-gate     conflen = kg_confounder_size(context, ctx->enc);
184*7c478bd9Sstevel@tonic-gate     data_size = (conflen + data_size + 8) & (~7);
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate     /*
187*7c478bd9Sstevel@tonic-gate      * If we are encrypting, check the size, it may be larger than
188*7c478bd9Sstevel@tonic-gate      * the input in some cases due to padding and byte-boundaries.
189*7c478bd9Sstevel@tonic-gate      */
190*7c478bd9Sstevel@tonic-gate     if (conf_req_flag) {
191*7c478bd9Sstevel@tonic-gate 	    data_size = kg_encrypt_size(context, ctx->enc, data_size);
192*7c478bd9Sstevel@tonic-gate     }
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate     /*
195*7c478bd9Sstevel@tonic-gate      * Calculate the token size for a buffer that is 'req_output_size'
196*7c478bd9Sstevel@tonic-gate      * long.
197*7c478bd9Sstevel@tonic-gate      */
198*7c478bd9Sstevel@tonic-gate     ohlen = g_token_size(&(ctx->mech_used),
199*7c478bd9Sstevel@tonic-gate 			(unsigned int)(data_size + ctx->cksum_size + 14)) -
200*7c478bd9Sstevel@tonic-gate 	    req_output_size;
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate     KRB5_LOG1(KRB5_INFO, "ohlen = %u, req_output_size = %u.\n",
203*7c478bd9Sstevel@tonic-gate 	ohlen, req_output_size);
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate     *max_input_size = (req_output_size > ohlen) ?
206*7c478bd9Sstevel@tonic-gate 	    ((req_output_size - ohlen) & (~7)) : 0;
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate     *minor_status = 0;
209*7c478bd9Sstevel@tonic-gate end:
210*7c478bd9Sstevel@tonic-gate     mutex_unlock(&krb5_mutex);
211*7c478bd9Sstevel@tonic-gate     KRB5_LOG(KRB5_INFO, "krb5_gss_wrap_size_limit() end, "
212*7c478bd9Sstevel@tonic-gate 	"max_input_size = %u.\n", *max_input_size);
213*7c478bd9Sstevel@tonic-gate     return(GSS_S_COMPLETE);
214*7c478bd9Sstevel@tonic-gate }
215