Implement gss_wrap_iov, gss_unwrap_iov for CFX type encryption types.
[heimdal.git] / lib / gssapi / krb5 / 8003.c
bloba6f0165e7247c3863dd3934dfef247a75b938b10
1 /*
2 * Copyright (c) 1997 - 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "gsskrb5_locl.h"
36 krb5_error_code
37 _gsskrb5_encode_om_uint32(OM_uint32 n, u_char *p)
39 p[0] = (n >> 0) & 0xFF;
40 p[1] = (n >> 8) & 0xFF;
41 p[2] = (n >> 16) & 0xFF;
42 p[3] = (n >> 24) & 0xFF;
43 return 0;
46 krb5_error_code
47 _gsskrb5_encode_be_om_uint32(OM_uint32 n, u_char *p)
49 p[0] = (n >> 24) & 0xFF;
50 p[1] = (n >> 16) & 0xFF;
51 p[2] = (n >> 8) & 0xFF;
52 p[3] = (n >> 0) & 0xFF;
53 return 0;
56 krb5_error_code
57 _gsskrb5_decode_om_uint32(const void *ptr, OM_uint32 *n)
59 const u_char *p = ptr;
60 *n = (p[0] << 0) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
61 return 0;
64 krb5_error_code
65 _gsskrb5_decode_be_om_uint32(const void *ptr, OM_uint32 *n)
67 const u_char *p = ptr;
68 *n = (p[0] <<24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0);
69 return 0;
72 static krb5_error_code
73 hash_input_chan_bindings (const gss_channel_bindings_t b,
74 u_char *p)
76 u_char num[4];
77 MD5_CTX md5;
79 MD5_Init(&md5);
80 _gsskrb5_encode_om_uint32 (b->initiator_addrtype, num);
81 MD5_Update (&md5, num, sizeof(num));
82 _gsskrb5_encode_om_uint32 (b->initiator_address.length, num);
83 MD5_Update (&md5, num, sizeof(num));
84 if (b->initiator_address.length)
85 MD5_Update (&md5,
86 b->initiator_address.value,
87 b->initiator_address.length);
88 _gsskrb5_encode_om_uint32 (b->acceptor_addrtype, num);
89 MD5_Update (&md5, num, sizeof(num));
90 _gsskrb5_encode_om_uint32 (b->acceptor_address.length, num);
91 MD5_Update (&md5, num, sizeof(num));
92 if (b->acceptor_address.length)
93 MD5_Update (&md5,
94 b->acceptor_address.value,
95 b->acceptor_address.length);
96 _gsskrb5_encode_om_uint32 (b->application_data.length, num);
97 MD5_Update (&md5, num, sizeof(num));
98 if (b->application_data.length)
99 MD5_Update (&md5,
100 b->application_data.value,
101 b->application_data.length);
102 MD5_Final (p, &md5);
103 return 0;
107 * create a checksum over the chanel bindings in
108 * `input_chan_bindings', `flags' and `fwd_data' and return it in
109 * `result'
112 OM_uint32
113 _gsskrb5_create_8003_checksum (
114 OM_uint32 *minor_status,
115 const gss_channel_bindings_t input_chan_bindings,
116 OM_uint32 flags,
117 const krb5_data *fwd_data,
118 Checksum *result)
120 u_char *p;
123 * see rfc1964 (section 1.1.1 (Initial Token), and the checksum value
124 * field's format) */
125 result->cksumtype = CKSUMTYPE_GSSAPI;
126 if (fwd_data->length > 0 && (flags & GSS_C_DELEG_FLAG))
127 result->checksum.length = 24 + 4 + fwd_data->length;
128 else
129 result->checksum.length = 24;
130 result->checksum.data = malloc (result->checksum.length);
131 if (result->checksum.data == NULL) {
132 *minor_status = ENOMEM;
133 return GSS_S_FAILURE;
136 p = result->checksum.data;
137 _gsskrb5_encode_om_uint32 (16, p);
138 p += 4;
139 if (input_chan_bindings == GSS_C_NO_CHANNEL_BINDINGS) {
140 memset (p, 0, 16);
141 } else {
142 hash_input_chan_bindings (input_chan_bindings, p);
144 p += 16;
145 _gsskrb5_encode_om_uint32 (flags, p);
146 p += 4;
148 if (fwd_data->length > 0 && (flags & GSS_C_DELEG_FLAG)) {
150 *p++ = (1 >> 0) & 0xFF; /* DlgOpt */ /* == 1 */
151 *p++ = (1 >> 8) & 0xFF; /* DlgOpt */ /* == 0 */
152 *p++ = (fwd_data->length >> 0) & 0xFF; /* Dlgth */
153 *p++ = (fwd_data->length >> 8) & 0xFF; /* Dlgth */
154 memcpy(p, (unsigned char *) fwd_data->data, fwd_data->length);
156 p += fwd_data->length;
159 return GSS_S_COMPLETE;
163 * verify the checksum in `cksum' over `input_chan_bindings'
164 * returning `flags' and `fwd_data'
167 OM_uint32
168 _gsskrb5_verify_8003_checksum(
169 OM_uint32 *minor_status,
170 const gss_channel_bindings_t input_chan_bindings,
171 const Checksum *cksum,
172 OM_uint32 *flags,
173 krb5_data *fwd_data)
175 unsigned char hash[16];
176 unsigned char *p;
177 OM_uint32 length;
178 int DlgOpt;
179 static unsigned char zeros[16];
181 if (cksum == NULL) {
182 *minor_status = 0;
183 return GSS_S_BAD_BINDINGS;
186 /* XXX should handle checksums > 24 bytes */
187 if(cksum->cksumtype != CKSUMTYPE_GSSAPI || cksum->checksum.length < 24) {
188 *minor_status = 0;
189 return GSS_S_BAD_BINDINGS;
192 p = cksum->checksum.data;
193 _gsskrb5_decode_om_uint32(p, &length);
194 if(length != sizeof(hash)) {
195 *minor_status = 0;
196 return GSS_S_BAD_BINDINGS;
199 p += 4;
201 if (input_chan_bindings != GSS_C_NO_CHANNEL_BINDINGS
202 && memcmp(p, zeros, sizeof(zeros)) != 0) {
203 if(hash_input_chan_bindings(input_chan_bindings, hash) != 0) {
204 *minor_status = 0;
205 return GSS_S_BAD_BINDINGS;
207 if(memcmp(hash, p, sizeof(hash)) != 0) {
208 *minor_status = 0;
209 return GSS_S_BAD_BINDINGS;
213 p += sizeof(hash);
215 _gsskrb5_decode_om_uint32(p, flags);
216 p += 4;
218 if (cksum->checksum.length > 24 && (*flags & GSS_C_DELEG_FLAG)) {
219 if(cksum->checksum.length < 28) {
220 *minor_status = 0;
221 return GSS_S_BAD_BINDINGS;
224 DlgOpt = (p[0] << 0) | (p[1] << 8);
225 p += 2;
226 if (DlgOpt != 1) {
227 *minor_status = 0;
228 return GSS_S_BAD_BINDINGS;
231 fwd_data->length = (p[0] << 0) | (p[1] << 8);
232 p += 2;
233 if(cksum->checksum.length < 28 + fwd_data->length) {
234 *minor_status = 0;
235 return GSS_S_BAD_BINDINGS;
237 fwd_data->data = malloc(fwd_data->length);
238 if (fwd_data->data == NULL) {
239 *minor_status = ENOMEM;
240 return GSS_S_FAILURE;
242 memcpy(fwd_data->data, p, fwd_data->length);
245 return GSS_S_COMPLETE;