xref: /minix3/crypto/external/bsd/heimdal/dist/lib/gssapi/krb5/encapsulate.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: encapsulate.c,v 1.1.1.2 2014/04/24 12:45:29 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 1997 - 2003 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #include "gsskrb5_locl.h"
37ebfedea0SLionel Sambuc 
38ebfedea0SLionel Sambuc void
_gssapi_encap_length(size_t data_len,size_t * len,size_t * total_len,const gss_OID mech)39ebfedea0SLionel Sambuc _gssapi_encap_length (size_t data_len,
40ebfedea0SLionel Sambuc 		      size_t *len,
41ebfedea0SLionel Sambuc 		      size_t *total_len,
42ebfedea0SLionel Sambuc 		      const gss_OID mech)
43ebfedea0SLionel Sambuc {
44ebfedea0SLionel Sambuc     size_t len_len;
45ebfedea0SLionel Sambuc 
46ebfedea0SLionel Sambuc     *len = 1 + 1 + mech->length + data_len;
47ebfedea0SLionel Sambuc 
48ebfedea0SLionel Sambuc     len_len = der_length_len(*len);
49ebfedea0SLionel Sambuc 
50ebfedea0SLionel Sambuc     *total_len = 1 + len_len + *len;
51ebfedea0SLionel Sambuc }
52ebfedea0SLionel Sambuc 
53ebfedea0SLionel Sambuc void
_gsskrb5_encap_length(size_t data_len,size_t * len,size_t * total_len,const gss_OID mech)54ebfedea0SLionel Sambuc _gsskrb5_encap_length (size_t data_len,
55ebfedea0SLionel Sambuc 			  size_t *len,
56ebfedea0SLionel Sambuc 			  size_t *total_len,
57ebfedea0SLionel Sambuc 			  const gss_OID mech)
58ebfedea0SLionel Sambuc {
59ebfedea0SLionel Sambuc     _gssapi_encap_length(data_len + 2, len, total_len, mech);
60ebfedea0SLionel Sambuc }
61ebfedea0SLionel Sambuc 
62ebfedea0SLionel Sambuc void *
_gsskrb5_make_header(void * ptr,size_t len,const void * type,const gss_OID mech)63ebfedea0SLionel Sambuc _gsskrb5_make_header (void *ptr,
64ebfedea0SLionel Sambuc 			 size_t len,
65ebfedea0SLionel Sambuc 			 const void *type,
66ebfedea0SLionel Sambuc 			 const gss_OID mech)
67ebfedea0SLionel Sambuc {
68ebfedea0SLionel Sambuc     u_char *p = ptr;
69ebfedea0SLionel Sambuc     p = _gssapi_make_mech_header(p, len, mech);
70ebfedea0SLionel Sambuc     memcpy (p, type, 2);
71ebfedea0SLionel Sambuc     p += 2;
72ebfedea0SLionel Sambuc     return p;
73ebfedea0SLionel Sambuc }
74ebfedea0SLionel Sambuc 
75ebfedea0SLionel Sambuc void *
_gssapi_make_mech_header(void * ptr,size_t len,const gss_OID mech)76ebfedea0SLionel Sambuc _gssapi_make_mech_header(void *ptr,
77ebfedea0SLionel Sambuc 			 size_t len,
78ebfedea0SLionel Sambuc 			 const gss_OID mech)
79ebfedea0SLionel Sambuc {
80ebfedea0SLionel Sambuc     u_char *p = ptr;
81ebfedea0SLionel Sambuc     int e;
82ebfedea0SLionel Sambuc     size_t len_len, foo;
83ebfedea0SLionel Sambuc 
84ebfedea0SLionel Sambuc     *p++ = 0x60;
85ebfedea0SLionel Sambuc     len_len = der_length_len(len);
86ebfedea0SLionel Sambuc     e = der_put_length (p + len_len - 1, len_len, len, &foo);
87ebfedea0SLionel Sambuc     if(e || foo != len_len)
88ebfedea0SLionel Sambuc 	abort ();
89ebfedea0SLionel Sambuc     p += len_len;
90ebfedea0SLionel Sambuc     *p++ = 0x06;
91ebfedea0SLionel Sambuc     *p++ = mech->length;
92ebfedea0SLionel Sambuc     memcpy (p, mech->elements, mech->length);
93ebfedea0SLionel Sambuc     p += mech->length;
94ebfedea0SLionel Sambuc     return p;
95ebfedea0SLionel Sambuc }
96ebfedea0SLionel Sambuc 
97ebfedea0SLionel Sambuc /*
98ebfedea0SLionel Sambuc  * Give it a krb5_data and it will encapsulate with extra GSS-API wrappings.
99ebfedea0SLionel Sambuc  */
100ebfedea0SLionel Sambuc 
101ebfedea0SLionel Sambuc OM_uint32
_gssapi_encapsulate(OM_uint32 * minor_status,const krb5_data * in_data,gss_buffer_t output_token,const gss_OID mech)102ebfedea0SLionel Sambuc _gssapi_encapsulate(
103ebfedea0SLionel Sambuc     OM_uint32 *minor_status,
104ebfedea0SLionel Sambuc     const krb5_data *in_data,
105ebfedea0SLionel Sambuc     gss_buffer_t output_token,
106ebfedea0SLionel Sambuc     const gss_OID mech
107ebfedea0SLionel Sambuc )
108ebfedea0SLionel Sambuc {
109ebfedea0SLionel Sambuc     size_t len, outer_len;
110ebfedea0SLionel Sambuc     void *p;
111ebfedea0SLionel Sambuc 
112ebfedea0SLionel Sambuc     _gssapi_encap_length (in_data->length, &len, &outer_len, mech);
113ebfedea0SLionel Sambuc 
114ebfedea0SLionel Sambuc     output_token->length = outer_len;
115ebfedea0SLionel Sambuc     output_token->value  = malloc (outer_len);
116ebfedea0SLionel Sambuc     if (output_token->value == NULL) {
117ebfedea0SLionel Sambuc 	*minor_status = ENOMEM;
118ebfedea0SLionel Sambuc 	return GSS_S_FAILURE;
119ebfedea0SLionel Sambuc     }
120ebfedea0SLionel Sambuc 
121ebfedea0SLionel Sambuc     p = _gssapi_make_mech_header (output_token->value, len, mech);
122ebfedea0SLionel Sambuc     memcpy (p, in_data->data, in_data->length);
123ebfedea0SLionel Sambuc     return GSS_S_COMPLETE;
124ebfedea0SLionel Sambuc }
125ebfedea0SLionel Sambuc 
126ebfedea0SLionel Sambuc /*
127ebfedea0SLionel Sambuc  * Give it a krb5_data and it will encapsulate with extra GSS-API krb5
128ebfedea0SLionel Sambuc  * wrappings.
129ebfedea0SLionel Sambuc  */
130ebfedea0SLionel Sambuc 
131ebfedea0SLionel Sambuc OM_uint32
_gsskrb5_encapsulate(OM_uint32 * minor_status,const krb5_data * in_data,gss_buffer_t output_token,const void * type,const gss_OID mech)132ebfedea0SLionel Sambuc _gsskrb5_encapsulate(
133ebfedea0SLionel Sambuc 			OM_uint32 *minor_status,
134ebfedea0SLionel Sambuc 			const krb5_data *in_data,
135ebfedea0SLionel Sambuc 			gss_buffer_t output_token,
136ebfedea0SLionel Sambuc 			const void *type,
137ebfedea0SLionel Sambuc 			const gss_OID mech
138ebfedea0SLionel Sambuc )
139ebfedea0SLionel Sambuc {
140ebfedea0SLionel Sambuc     size_t len, outer_len;
141ebfedea0SLionel Sambuc     u_char *p;
142ebfedea0SLionel Sambuc 
143ebfedea0SLionel Sambuc     _gsskrb5_encap_length (in_data->length, &len, &outer_len, mech);
144ebfedea0SLionel Sambuc 
145ebfedea0SLionel Sambuc     output_token->length = outer_len;
146ebfedea0SLionel Sambuc     output_token->value  = malloc (outer_len);
147ebfedea0SLionel Sambuc     if (output_token->value == NULL) {
148ebfedea0SLionel Sambuc 	*minor_status = ENOMEM;
149ebfedea0SLionel Sambuc 	return GSS_S_FAILURE;
150ebfedea0SLionel Sambuc     }
151ebfedea0SLionel Sambuc 
152ebfedea0SLionel Sambuc     p = _gsskrb5_make_header (output_token->value, len, type, mech);
153ebfedea0SLionel Sambuc     memcpy (p, in_data->data, in_data->length);
154ebfedea0SLionel Sambuc     return GSS_S_COMPLETE;
155ebfedea0SLionel Sambuc }
156