1*0a6a1f1dSLionel Sambuc /* $NetBSD: 8003.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 krb5_error_code
_gsskrb5_encode_om_uint32(OM_uint32 n,u_char * p)39ebfedea0SLionel Sambuc _gsskrb5_encode_om_uint32(OM_uint32 n, u_char *p)
40ebfedea0SLionel Sambuc {
41ebfedea0SLionel Sambuc p[0] = (n >> 0) & 0xFF;
42ebfedea0SLionel Sambuc p[1] = (n >> 8) & 0xFF;
43ebfedea0SLionel Sambuc p[2] = (n >> 16) & 0xFF;
44ebfedea0SLionel Sambuc p[3] = (n >> 24) & 0xFF;
45ebfedea0SLionel Sambuc return 0;
46ebfedea0SLionel Sambuc }
47ebfedea0SLionel Sambuc
48ebfedea0SLionel Sambuc krb5_error_code
_gsskrb5_encode_be_om_uint32(OM_uint32 n,u_char * p)49ebfedea0SLionel Sambuc _gsskrb5_encode_be_om_uint32(OM_uint32 n, u_char *p)
50ebfedea0SLionel Sambuc {
51ebfedea0SLionel Sambuc p[0] = (n >> 24) & 0xFF;
52ebfedea0SLionel Sambuc p[1] = (n >> 16) & 0xFF;
53ebfedea0SLionel Sambuc p[2] = (n >> 8) & 0xFF;
54ebfedea0SLionel Sambuc p[3] = (n >> 0) & 0xFF;
55ebfedea0SLionel Sambuc return 0;
56ebfedea0SLionel Sambuc }
57ebfedea0SLionel Sambuc
58ebfedea0SLionel Sambuc krb5_error_code
_gsskrb5_decode_om_uint32(const void * ptr,OM_uint32 * n)59ebfedea0SLionel Sambuc _gsskrb5_decode_om_uint32(const void *ptr, OM_uint32 *n)
60ebfedea0SLionel Sambuc {
61ebfedea0SLionel Sambuc const u_char *p = ptr;
62ebfedea0SLionel Sambuc *n = (p[0] << 0) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
63ebfedea0SLionel Sambuc return 0;
64ebfedea0SLionel Sambuc }
65ebfedea0SLionel Sambuc
66ebfedea0SLionel Sambuc krb5_error_code
_gsskrb5_decode_be_om_uint32(const void * ptr,OM_uint32 * n)67ebfedea0SLionel Sambuc _gsskrb5_decode_be_om_uint32(const void *ptr, OM_uint32 *n)
68ebfedea0SLionel Sambuc {
69ebfedea0SLionel Sambuc const u_char *p = ptr;
70ebfedea0SLionel Sambuc *n = (p[0] <<24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0);
71ebfedea0SLionel Sambuc return 0;
72ebfedea0SLionel Sambuc }
73ebfedea0SLionel Sambuc
74ebfedea0SLionel Sambuc static krb5_error_code
hash_input_chan_bindings(const gss_channel_bindings_t b,u_char * p)75ebfedea0SLionel Sambuc hash_input_chan_bindings (const gss_channel_bindings_t b,
76ebfedea0SLionel Sambuc u_char *p)
77ebfedea0SLionel Sambuc {
78ebfedea0SLionel Sambuc u_char num[4];
79ebfedea0SLionel Sambuc EVP_MD_CTX *ctx;
80ebfedea0SLionel Sambuc
81ebfedea0SLionel Sambuc ctx = EVP_MD_CTX_create();
82ebfedea0SLionel Sambuc EVP_DigestInit_ex(ctx, EVP_md5(), NULL);
83ebfedea0SLionel Sambuc
84ebfedea0SLionel Sambuc _gsskrb5_encode_om_uint32 (b->initiator_addrtype, num);
85ebfedea0SLionel Sambuc EVP_DigestUpdate(ctx, num, sizeof(num));
86ebfedea0SLionel Sambuc _gsskrb5_encode_om_uint32 (b->initiator_address.length, num);
87ebfedea0SLionel Sambuc EVP_DigestUpdate(ctx, num, sizeof(num));
88ebfedea0SLionel Sambuc if (b->initiator_address.length)
89ebfedea0SLionel Sambuc EVP_DigestUpdate(ctx,
90ebfedea0SLionel Sambuc b->initiator_address.value,
91ebfedea0SLionel Sambuc b->initiator_address.length);
92ebfedea0SLionel Sambuc _gsskrb5_encode_om_uint32 (b->acceptor_addrtype, num);
93ebfedea0SLionel Sambuc EVP_DigestUpdate(ctx, num, sizeof(num));
94ebfedea0SLionel Sambuc _gsskrb5_encode_om_uint32 (b->acceptor_address.length, num);
95ebfedea0SLionel Sambuc EVP_DigestUpdate(ctx, num, sizeof(num));
96ebfedea0SLionel Sambuc if (b->acceptor_address.length)
97ebfedea0SLionel Sambuc EVP_DigestUpdate(ctx,
98ebfedea0SLionel Sambuc b->acceptor_address.value,
99ebfedea0SLionel Sambuc b->acceptor_address.length);
100ebfedea0SLionel Sambuc _gsskrb5_encode_om_uint32 (b->application_data.length, num);
101ebfedea0SLionel Sambuc EVP_DigestUpdate(ctx, num, sizeof(num));
102ebfedea0SLionel Sambuc if (b->application_data.length)
103ebfedea0SLionel Sambuc EVP_DigestUpdate(ctx,
104ebfedea0SLionel Sambuc b->application_data.value,
105ebfedea0SLionel Sambuc b->application_data.length);
106ebfedea0SLionel Sambuc EVP_DigestFinal_ex(ctx, p, NULL);
107ebfedea0SLionel Sambuc EVP_MD_CTX_destroy(ctx);
108ebfedea0SLionel Sambuc
109ebfedea0SLionel Sambuc return 0;
110ebfedea0SLionel Sambuc }
111ebfedea0SLionel Sambuc
112ebfedea0SLionel Sambuc /*
113ebfedea0SLionel Sambuc * create a checksum over the chanel bindings in
114ebfedea0SLionel Sambuc * `input_chan_bindings', `flags' and `fwd_data' and return it in
115ebfedea0SLionel Sambuc * `result'
116ebfedea0SLionel Sambuc */
117ebfedea0SLionel Sambuc
118ebfedea0SLionel Sambuc OM_uint32
_gsskrb5_create_8003_checksum(OM_uint32 * minor_status,const gss_channel_bindings_t input_chan_bindings,OM_uint32 flags,const krb5_data * fwd_data,Checksum * result)119ebfedea0SLionel Sambuc _gsskrb5_create_8003_checksum (
120ebfedea0SLionel Sambuc OM_uint32 *minor_status,
121ebfedea0SLionel Sambuc const gss_channel_bindings_t input_chan_bindings,
122ebfedea0SLionel Sambuc OM_uint32 flags,
123ebfedea0SLionel Sambuc const krb5_data *fwd_data,
124ebfedea0SLionel Sambuc Checksum *result)
125ebfedea0SLionel Sambuc {
126ebfedea0SLionel Sambuc u_char *p;
127ebfedea0SLionel Sambuc
128ebfedea0SLionel Sambuc /*
129ebfedea0SLionel Sambuc * see rfc1964 (section 1.1.1 (Initial Token), and the checksum value
130ebfedea0SLionel Sambuc * field's format) */
131ebfedea0SLionel Sambuc result->cksumtype = CKSUMTYPE_GSSAPI;
132ebfedea0SLionel Sambuc if (fwd_data->length > 0 && (flags & GSS_C_DELEG_FLAG))
133ebfedea0SLionel Sambuc result->checksum.length = 24 + 4 + fwd_data->length;
134ebfedea0SLionel Sambuc else
135ebfedea0SLionel Sambuc result->checksum.length = 24;
136ebfedea0SLionel Sambuc result->checksum.data = malloc (result->checksum.length);
137ebfedea0SLionel Sambuc if (result->checksum.data == NULL) {
138ebfedea0SLionel Sambuc *minor_status = ENOMEM;
139ebfedea0SLionel Sambuc return GSS_S_FAILURE;
140ebfedea0SLionel Sambuc }
141ebfedea0SLionel Sambuc
142ebfedea0SLionel Sambuc p = result->checksum.data;
143ebfedea0SLionel Sambuc _gsskrb5_encode_om_uint32 (16, p);
144ebfedea0SLionel Sambuc p += 4;
145ebfedea0SLionel Sambuc if (input_chan_bindings == GSS_C_NO_CHANNEL_BINDINGS) {
146ebfedea0SLionel Sambuc memset (p, 0, 16);
147ebfedea0SLionel Sambuc } else {
148ebfedea0SLionel Sambuc hash_input_chan_bindings (input_chan_bindings, p);
149ebfedea0SLionel Sambuc }
150ebfedea0SLionel Sambuc p += 16;
151ebfedea0SLionel Sambuc _gsskrb5_encode_om_uint32 (flags, p);
152ebfedea0SLionel Sambuc p += 4;
153ebfedea0SLionel Sambuc
154ebfedea0SLionel Sambuc if (fwd_data->length > 0 && (flags & GSS_C_DELEG_FLAG)) {
155ebfedea0SLionel Sambuc
156ebfedea0SLionel Sambuc *p++ = (1 >> 0) & 0xFF; /* DlgOpt */ /* == 1 */
157ebfedea0SLionel Sambuc *p++ = (1 >> 8) & 0xFF; /* DlgOpt */ /* == 0 */
158ebfedea0SLionel Sambuc *p++ = (fwd_data->length >> 0) & 0xFF; /* Dlgth */
159ebfedea0SLionel Sambuc *p++ = (fwd_data->length >> 8) & 0xFF; /* Dlgth */
160ebfedea0SLionel Sambuc memcpy(p, (unsigned char *) fwd_data->data, fwd_data->length);
161ebfedea0SLionel Sambuc
162ebfedea0SLionel Sambuc p += fwd_data->length;
163ebfedea0SLionel Sambuc }
164ebfedea0SLionel Sambuc
165ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
166ebfedea0SLionel Sambuc }
167ebfedea0SLionel Sambuc
168ebfedea0SLionel Sambuc /*
169ebfedea0SLionel Sambuc * verify the checksum in `cksum' over `input_chan_bindings'
170ebfedea0SLionel Sambuc * returning `flags' and `fwd_data'
171ebfedea0SLionel Sambuc */
172ebfedea0SLionel Sambuc
173ebfedea0SLionel Sambuc OM_uint32
_gsskrb5_verify_8003_checksum(OM_uint32 * minor_status,const gss_channel_bindings_t input_chan_bindings,const Checksum * cksum,OM_uint32 * flags,krb5_data * fwd_data)174ebfedea0SLionel Sambuc _gsskrb5_verify_8003_checksum(
175ebfedea0SLionel Sambuc OM_uint32 *minor_status,
176ebfedea0SLionel Sambuc const gss_channel_bindings_t input_chan_bindings,
177ebfedea0SLionel Sambuc const Checksum *cksum,
178ebfedea0SLionel Sambuc OM_uint32 *flags,
179ebfedea0SLionel Sambuc krb5_data *fwd_data)
180ebfedea0SLionel Sambuc {
181ebfedea0SLionel Sambuc unsigned char hash[16];
182ebfedea0SLionel Sambuc unsigned char *p;
183ebfedea0SLionel Sambuc OM_uint32 length;
184ebfedea0SLionel Sambuc int DlgOpt;
185ebfedea0SLionel Sambuc static unsigned char zeros[16];
186ebfedea0SLionel Sambuc
187ebfedea0SLionel Sambuc /* XXX should handle checksums > 24 bytes */
188ebfedea0SLionel Sambuc if(cksum->cksumtype != CKSUMTYPE_GSSAPI || cksum->checksum.length < 24) {
189ebfedea0SLionel Sambuc *minor_status = 0;
190ebfedea0SLionel Sambuc return GSS_S_BAD_BINDINGS;
191ebfedea0SLionel Sambuc }
192ebfedea0SLionel Sambuc
193ebfedea0SLionel Sambuc p = cksum->checksum.data;
194ebfedea0SLionel Sambuc _gsskrb5_decode_om_uint32(p, &length);
195ebfedea0SLionel Sambuc if(length != sizeof(hash)) {
196ebfedea0SLionel Sambuc *minor_status = 0;
197ebfedea0SLionel Sambuc return GSS_S_BAD_BINDINGS;
198ebfedea0SLionel Sambuc }
199ebfedea0SLionel Sambuc
200ebfedea0SLionel Sambuc p += 4;
201ebfedea0SLionel Sambuc
202ebfedea0SLionel Sambuc if (input_chan_bindings != GSS_C_NO_CHANNEL_BINDINGS
203ebfedea0SLionel Sambuc && memcmp(p, zeros, sizeof(zeros)) != 0) {
204ebfedea0SLionel Sambuc if(hash_input_chan_bindings(input_chan_bindings, hash) != 0) {
205ebfedea0SLionel Sambuc *minor_status = 0;
206ebfedea0SLionel Sambuc return GSS_S_BAD_BINDINGS;
207ebfedea0SLionel Sambuc }
208ebfedea0SLionel Sambuc if(ct_memcmp(hash, p, sizeof(hash)) != 0) {
209ebfedea0SLionel Sambuc *minor_status = 0;
210ebfedea0SLionel Sambuc return GSS_S_BAD_BINDINGS;
211ebfedea0SLionel Sambuc }
212ebfedea0SLionel Sambuc }
213ebfedea0SLionel Sambuc
214ebfedea0SLionel Sambuc p += sizeof(hash);
215ebfedea0SLionel Sambuc
216ebfedea0SLionel Sambuc _gsskrb5_decode_om_uint32(p, flags);
217ebfedea0SLionel Sambuc p += 4;
218ebfedea0SLionel Sambuc
219ebfedea0SLionel Sambuc if (cksum->checksum.length > 24 && (*flags & GSS_C_DELEG_FLAG)) {
220ebfedea0SLionel Sambuc if(cksum->checksum.length < 28) {
221ebfedea0SLionel Sambuc *minor_status = 0;
222ebfedea0SLionel Sambuc return GSS_S_BAD_BINDINGS;
223ebfedea0SLionel Sambuc }
224ebfedea0SLionel Sambuc
225ebfedea0SLionel Sambuc DlgOpt = (p[0] << 0) | (p[1] << 8);
226ebfedea0SLionel Sambuc p += 2;
227ebfedea0SLionel Sambuc if (DlgOpt != 1) {
228ebfedea0SLionel Sambuc *minor_status = 0;
229ebfedea0SLionel Sambuc return GSS_S_BAD_BINDINGS;
230ebfedea0SLionel Sambuc }
231ebfedea0SLionel Sambuc
232ebfedea0SLionel Sambuc fwd_data->length = (p[0] << 0) | (p[1] << 8);
233ebfedea0SLionel Sambuc p += 2;
234ebfedea0SLionel Sambuc if(cksum->checksum.length < 28 + fwd_data->length) {
235ebfedea0SLionel Sambuc *minor_status = 0;
236ebfedea0SLionel Sambuc return GSS_S_BAD_BINDINGS;
237ebfedea0SLionel Sambuc }
238ebfedea0SLionel Sambuc fwd_data->data = malloc(fwd_data->length);
239ebfedea0SLionel Sambuc if (fwd_data->data == NULL) {
240ebfedea0SLionel Sambuc *minor_status = ENOMEM;
241ebfedea0SLionel Sambuc return GSS_S_FAILURE;
242ebfedea0SLionel Sambuc }
243ebfedea0SLionel Sambuc memcpy(fwd_data->data, p, fwd_data->length);
244ebfedea0SLionel Sambuc }
245ebfedea0SLionel Sambuc
246ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
247ebfedea0SLionel Sambuc }
248