- Complete re-write of sasc.
[dragonfly.git] / crypto / heimdal / lib / gssapi / 8003.c
1 /*
2  * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
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. 
16  *
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. 
20  *
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. 
32  */
33
34 #include "gssapi_locl.h"
35
36 RCSID("$Id: 8003.c,v 1.11 2002/03/10 23:47:39 assar Exp $");
37
38 static krb5_error_code
39 encode_om_uint32(OM_uint32 n, u_char *p)
40 {
41   p[0] = (n >> 0)  & 0xFF;
42   p[1] = (n >> 8)  & 0xFF;
43   p[2] = (n >> 16) & 0xFF;
44   p[3] = (n >> 24) & 0xFF;
45   return 0;
46 }
47
48 static krb5_error_code
49 decode_om_uint32(u_char *p, OM_uint32 *n)
50 {
51     *n = (p[0] << 0) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
52     return 0;
53 }
54
55 static krb5_error_code
56 hash_input_chan_bindings (const gss_channel_bindings_t b,
57                           u_char *p)
58 {
59   u_char num[4];
60   MD5_CTX md5;
61
62   MD5_Init(&md5);
63   encode_om_uint32 (b->initiator_addrtype, num);
64   MD5_Update (&md5, num, sizeof(num));
65   encode_om_uint32 (b->initiator_address.length, num);
66   MD5_Update (&md5, num, sizeof(num));
67   if (b->initiator_address.length)
68     MD5_Update (&md5,
69                 b->initiator_address.value,
70                 b->initiator_address.length);
71   encode_om_uint32 (b->acceptor_addrtype, num);
72   MD5_Update (&md5, num, sizeof(num));
73   encode_om_uint32 (b->acceptor_address.length, num);
74   MD5_Update (&md5, num, sizeof(num));
75   if (b->acceptor_address.length)
76     MD5_Update (&md5,
77                 b->acceptor_address.value,
78                 b->acceptor_address.length);
79   encode_om_uint32 (b->application_data.length, num);
80   MD5_Update (&md5, num, sizeof(num));
81   if (b->application_data.length)
82     MD5_Update (&md5,
83                 b->application_data.value,
84                 b->application_data.length);
85   MD5_Final (p, &md5);
86   return 0;
87 }
88
89 /*
90  * create a checksum over the chanel bindings in
91  * `input_chan_bindings', `flags' and `fwd_data' and return it in
92  * `result'
93  */
94
95 OM_uint32
96 gssapi_krb5_create_8003_checksum (
97                       OM_uint32 *minor_status,    
98                       const gss_channel_bindings_t input_chan_bindings,
99                       OM_uint32 flags,
100                       const krb5_data *fwd_data,
101                       Checksum *result)
102 {
103   u_char *p;
104
105   /* 
106    * see rfc1964 (section 1.1.1 (Initial Token), and the checksum value 
107    * field's format) */
108   result->cksumtype = 0x8003;
109   if (fwd_data->length > 0 && (flags & GSS_C_DELEG_FLAG))
110     result->checksum.length = 24 + 4 + fwd_data->length;
111   else 
112     result->checksum.length = 24;
113   result->checksum.data   = malloc (result->checksum.length);
114   if (result->checksum.data == NULL) {
115     *minor_status = ENOMEM;
116     return GSS_S_FAILURE;
117   }
118   
119   p = result->checksum.data;
120   encode_om_uint32 (16, p);
121   p += 4;
122   if (input_chan_bindings == GSS_C_NO_CHANNEL_BINDINGS) {
123     memset (p, 0, 16);
124   } else {
125     hash_input_chan_bindings (input_chan_bindings, p);
126   }
127   p += 16;
128   encode_om_uint32 (flags, p);
129   p += 4;
130
131   if (fwd_data->length > 0 && (flags & GSS_C_DELEG_FLAG)) {
132 #if 0
133      u_char *tmp;
134
135      result->checksum.length = 28 + fwd_data->length;
136      tmp = realloc(result->checksum.data, result->checksum.length);
137      if (tmp == NULL)
138         return ENOMEM;
139      result->checksum.data = tmp;
140
141      p = (u_char*)result->checksum.data + 24;  
142 #endif
143      *p++ = (1 >> 0) & 0xFF;                   /* DlgOpt */ /* == 1 */
144      *p++ = (1 >> 8) & 0xFF;                   /* DlgOpt */ /* == 0 */
145      *p++ = (fwd_data->length >> 0) & 0xFF;    /* Dlgth  */
146      *p++ = (fwd_data->length >> 8) & 0xFF;    /* Dlgth  */
147      memcpy(p, (unsigned char *) fwd_data->data, fwd_data->length);
148
149      p += fwd_data->length;
150   }
151      
152   return GSS_S_COMPLETE;
153 }
154
155 /*
156  * verify the checksum in `cksum' over `input_chan_bindings'
157  * returning  `flags' and `fwd_data'
158  */
159
160 OM_uint32
161 gssapi_krb5_verify_8003_checksum(
162                       OM_uint32 *minor_status,    
163                       const gss_channel_bindings_t input_chan_bindings,
164                       const Checksum *cksum,
165                       OM_uint32 *flags,
166                       krb5_data *fwd_data)
167 {
168     unsigned char hash[16];
169     unsigned char *p;
170     OM_uint32 length;
171     int DlgOpt;
172     static unsigned char zeros[16];
173
174     /* XXX should handle checksums > 24 bytes */
175     if(cksum->cksumtype != 0x8003) {
176         *minor_status = 0;
177         return GSS_S_BAD_BINDINGS;
178     }
179     
180     p = cksum->checksum.data;
181     decode_om_uint32(p, &length);
182     if(length != sizeof(hash)) {
183         *minor_status = 0;
184         return GSS_S_BAD_BINDINGS;
185     }
186     
187     p += 4;
188     
189     if (input_chan_bindings != GSS_C_NO_CHANNEL_BINDINGS
190         && memcmp(p, zeros, sizeof(zeros)) != 0) {
191         if(hash_input_chan_bindings(input_chan_bindings, hash) != 0) {
192             *minor_status = 0;
193             return GSS_S_BAD_BINDINGS;
194         }
195         if(memcmp(hash, p, sizeof(hash)) != 0) {
196             *minor_status = 0;
197             return GSS_S_BAD_BINDINGS;
198         }
199     }
200     
201     p += sizeof(hash);
202     
203     decode_om_uint32(p, flags);
204
205     if (cksum->checksum.length > 24 && (*flags & GSS_C_DELEG_FLAG)) {
206     
207       p += 4;
208     
209       DlgOpt = (p[0] << 0) | (p[1] << 8 );
210       if (DlgOpt != 1) {
211           *minor_status = 0;
212           return GSS_S_BAD_BINDINGS;
213       }
214       
215       p += 2;
216       fwd_data->length = (p[0] << 0) | (p[1] << 8);
217       fwd_data->data = malloc(fwd_data->length);
218       if (fwd_data->data == NULL) {
219           *minor_status = ENOMEM;
220           return GSS_S_FAILURE;
221       }
222
223       p += 2;
224       memcpy(fwd_data->data, p, fwd_data->length);
225     }
226     
227     return GSS_S_COMPLETE;
228 }