1/*
2 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
3 * Use is subject to license terms.
4 */
5
6#pragma ident	"%Z%%M%	%I%	%E% SMI"
7
8/*
9 * Copyright 2000 by the Massachusetts Institute of Technology.
10 * All Rights Reserved.
11 *
12 * Export of this software from the United States of America may
13 *   require a specific license from the United States Government.
14 *   It is the responsibility of any person or organization contemplating
15 *   export to obtain such a license before exporting.
16 *
17 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
18 * distribute this software and its documentation for any purpose and
19 * without fee is hereby granted, provided that the above copyright
20 * notice appear in all copies and that both that copyright notice and
21 * this permission notice appear in supporting documentation, and that
22 * the name of M.I.T. not be used in advertising or publicity pertaining
23 * to distribution of the software without specific, written prior
24 * permission.  Furthermore if you modify this software you must label
25 * your software as modified software and not distribute it in such a
26 * fashion that it might be confused with the original M.I.T. software.
27 * M.I.T. makes no representations about the suitability of
28 * this software for any purpose.  It is provided "as is" without express
29 * or implied warranty.
30 *
31 */
32/*
33 * Copyright 1993 by OpenVision Technologies, Inc.
34 *
35 * Permission to use, copy, modify, distribute, and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appears in all copies and
38 * that both that copyright notice and this permission notice appear in
39 * supporting documentation, and that the name of OpenVision not be used
40 * in advertising or publicity pertaining to distribution of the software
41 * without specific, written prior permission. OpenVision makes no
42 * representations about the suitability of this software for any
43 * purpose.  It is provided "as is" without express or implied warranty.
44 *
45 * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
46 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
47 * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
48 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
49 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
50 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
51 * PERFORMANCE OF THIS SOFTWARE.
52 */
53
54/*
55 * Copyright (C) 1998 by the FundsXpress, INC.
56 *
57 * All rights reserved.
58 *
59 * Export of this software from the United States of America may require
60 * a specific license from the United States Government.  It is the
61 * responsibility of any person or organization contemplating export to
62 * obtain such a license before exporting.
63 *
64 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
65 * distribute this software and its documentation for any purpose and
66 * without fee is hereby granted, provided that the above copyright
67 * notice appear in all copies and that both that copyright notice and
68 * this permission notice appear in supporting documentation, and that
69 * the name of FundsXpress. not be used in advertising or publicity pertaining
70 * to distribution of the software without specific, written prior
71 * permission.  FundsXpress makes no representations about the suitability of
72 * this software for any purpose.  It is provided "as is" without express
73 * or implied warranty.
74 *
75 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
76 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
77 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
78 */
79
80#include "gssapiP_krb5.h"
81
82size_t KRB5_CALLCONV krb5_encrypt_size(size_t, krb5_enctype);
83
84 /* SUNW15resync - XXX find new home for this func */
85#ifdef _KERNEL
86size_t KRB5_CALLCONV
87krb5_encrypt_size(size_t length, krb5_enctype crypto)
88{
89    size_t ret;
90
91    if (krb5_c_encrypt_length(/* XXX */ 0, crypto, length, &ret))
92	/*LINTED*/
93        return(-1); /* XXX */
94
95    return(ret);
96}
97#endif
98
99/* V2 interface */
100OM_uint32
101krb5_gss_wrap_size_limit(minor_status, context_handle, conf_req_flag,
102			 qop_req, req_output_size, max_input_size)
103    OM_uint32		*minor_status;
104    gss_ctx_id_t	context_handle;
105    int			conf_req_flag;
106    gss_qop_t		qop_req;
107    OM_uint32		req_output_size;
108    OM_uint32		*max_input_size;
109{
110    krb5_gss_ctx_id_rec	*ctx;
111    OM_uint32		data_size, conflen;
112    OM_uint32		ohlen;
113    int			overhead;
114
115    /* Solaris Kerb - check to make sure we aren't writing to a NULL pointer */
116    if (!max_input_size)
117 	return(GSS_S_CALL_INACCESSIBLE_WRITE);
118
119    /* only default qop is allowed */
120    /*
121     * SUNW15resync
122     * mit 1.2-6: if (qop_req != GSS_C_QOP_DEFAULT) {
123     * Go with Solaris version here, though not sure which is
124     * correct and RFC 2743 does not make it clear.
125     */
126    if ((qop_req & GSS_KRB5_CONF_C_QOP_MASK) != GSS_C_QOP_DEFAULT) {
127	*minor_status = (OM_uint32) G_UNKNOWN_QOP;
128	/* SUNW15resync - RFC 2743 is clear here but
129          this is still GSS_S_FAILURE in MIT */
130	return(GSS_S_BAD_QOP);
131    }
132
133    /* validate the context handle */
134    if (! kg_validate_ctx_id(context_handle)) {
135	*minor_status = (OM_uint32) G_VALIDATE_FAILED;
136	return(GSS_S_NO_CONTEXT);
137    }
138
139    ctx = (krb5_gss_ctx_id_rec *) context_handle;
140    if (! ctx->established) {
141	*minor_status = KG_CTX_INCOMPLETE;
142	return(GSS_S_NO_CONTEXT);
143    }
144
145    if (ctx->proto == 1) {
146	/* No pseudo-ASN.1 wrapper overhead, so no sequence length and
147	   OID.  */
148	OM_uint32 sz = req_output_size;
149	/* Token header: 16 octets.  */
150	if (conf_req_flag) {
151	    while (sz > 0 && krb5_encrypt_size(sz, ctx->enc->enctype) + 16 > req_output_size)
152		sz--;
153	    /* Allow for encrypted copy of header.  */
154	    if (sz > 16)
155		sz -= 16;
156	    else
157		sz = 0;
158#ifdef CFX_EXERCISE
159	    /* Allow for EC padding.  In the MIT implementation, only
160	       added while testing.  */
161	    if (sz > 65535)
162		sz -= 65535;
163	    else
164		sz = 0;
165#endif
166	} else {
167	    /* Allow for token header and checksum.  */
168	    if (sz < 16 + ctx->cksum_size)
169		sz = 0;
170	    else
171		sz -= (16 + ctx->cksum_size);
172	}
173
174	*max_input_size = sz;
175	*minor_status = 0;
176	return GSS_S_COMPLETE;
177    }
178
179    /* Calculate the token size and subtract that from the output size */
180    overhead = 7 + ctx->mech_used->length;
181    data_size = req_output_size;
182    conflen = kg_confounder_size(ctx->k5_context, ctx->enc);
183    data_size = (conflen + data_size + 8) & (~(OM_uint32)7);
184    ohlen = g_token_size(ctx->mech_used,
185			 (unsigned int) (data_size + ctx->cksum_size + 14))
186      - req_output_size;
187
188    if (ohlen+overhead < req_output_size)
189      /*
190       * Cannot have trailer length that will cause us to pad over our
191       * length.
192       */
193      *max_input_size = (req_output_size - ohlen - overhead) & (~(OM_uint32)7);
194    else
195      *max_input_size = 0;
196
197    *minor_status = 0;
198    return(GSS_S_COMPLETE);
199}
200