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