Line data Source code
1 : /*
2 : * Copyright (c) 2003 - 2006 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 "gsskrb5_locl.h"
35 :
36 : #define DEFAULT_JITTER_WINDOW 20
37 :
38 : struct gss_msg_order {
39 : OM_uint32 flags;
40 : OM_uint32 start;
41 : OM_uint32 length;
42 : OM_uint32 jitter_window;
43 : OM_uint32 first_seq;
44 : OM_uint32 elem[1];
45 : };
46 :
47 :
48 : /*
49 : *
50 : */
51 :
52 : static OM_uint32
53 75841 : msg_order_alloc(OM_uint32 *minor_status,
54 : struct gss_msg_order **o,
55 : OM_uint32 jitter_window)
56 : {
57 1761 : size_t len;
58 :
59 75841 : len = jitter_window * sizeof((*o)->elem[0]);
60 75841 : len += sizeof(**o);
61 75841 : len -= sizeof((*o)->elem[0]);
62 :
63 75841 : *o = calloc(1, len);
64 75841 : if (*o == NULL) {
65 0 : *minor_status = ENOMEM;
66 0 : return GSS_S_FAILURE;
67 : }
68 :
69 75841 : *minor_status = 0;
70 75841 : return GSS_S_COMPLETE;
71 : }
72 :
73 : /*
74 : *
75 : */
76 :
77 : OM_uint32
78 75841 : _gssapi_msg_order_create(OM_uint32 *minor_status,
79 : struct gss_msg_order **o,
80 : OM_uint32 flags,
81 : OM_uint32 seq_num,
82 : OM_uint32 jitter_window,
83 : int use_64)
84 : {
85 1761 : OM_uint32 ret;
86 :
87 75841 : if (jitter_window == 0)
88 75841 : jitter_window = DEFAULT_JITTER_WINDOW;
89 :
90 75841 : ret = msg_order_alloc(minor_status, o, jitter_window);
91 75841 : if(ret != GSS_S_COMPLETE)
92 0 : return ret;
93 :
94 75841 : (*o)->flags = flags;
95 75841 : (*o)->length = 0;
96 75841 : (*o)->first_seq = seq_num;
97 75841 : (*o)->jitter_window = jitter_window;
98 75841 : (*o)->elem[0] = seq_num - 1;
99 :
100 75841 : *minor_status = 0;
101 75841 : return GSS_S_COMPLETE;
102 : }
103 :
104 : OM_uint32
105 75792 : _gssapi_msg_order_destroy(struct gss_msg_order **m)
106 : {
107 75792 : free(*m);
108 75792 : *m = NULL;
109 75792 : return GSS_S_COMPLETE;
110 : }
111 :
112 : static void
113 1433812 : elem_set(struct gss_msg_order *o, unsigned int slot, OM_uint32 val)
114 : {
115 1433812 : o->elem[slot % o->jitter_window] = val;
116 1431253 : }
117 :
118 : static void
119 1433812 : elem_insert(struct gss_msg_order *o,
120 : unsigned int after_slot,
121 : OM_uint32 seq_num)
122 : {
123 1433812 : assert(o->jitter_window > after_slot);
124 :
125 1433812 : if (o->length > after_slot)
126 1375024 : memmove(&o->elem[after_slot + 1], &o->elem[after_slot],
127 1372465 : (o->length - after_slot - 1) * sizeof(o->elem[0]));
128 :
129 1433812 : elem_set(o, after_slot, seq_num);
130 :
131 1433812 : if (o->length < o->jitter_window)
132 394246 : o->length++;
133 1433812 : }
134 :
135 : /* rule 1: expected sequence number */
136 : /* rule 2: > expected sequence number */
137 : /* rule 3: seqnum < seqnum(first) */
138 : /* rule 4+5: seqnum in [seqnum(first),seqnum(last)] */
139 :
140 : OM_uint32
141 1433818 : _gssapi_msg_order_check(struct gss_msg_order *o, OM_uint32 seq_num)
142 : {
143 2565 : OM_uint32 r;
144 2565 : size_t i;
145 :
146 1433818 : if (o == NULL)
147 0 : return GSS_S_COMPLETE;
148 :
149 1433812 : if ((o->flags & (GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) == 0)
150 0 : return GSS_S_COMPLETE;
151 :
152 : /* check if the packet is the next in order */
153 1433812 : if (o->elem[0] == seq_num - 1) {
154 1433812 : elem_insert(o, 0, seq_num);
155 1433812 : return GSS_S_COMPLETE;
156 : }
157 :
158 0 : r = (o->flags & (GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG))==GSS_C_REPLAY_FLAG;
159 :
160 : /* sequence number larger then largest sequence number
161 : * or smaller then the first sequence number */
162 0 : if (seq_num > o->elem[0]
163 0 : || seq_num < o->first_seq
164 0 : || o->length == 0)
165 : {
166 0 : elem_insert(o, 0, seq_num);
167 0 : if (r) {
168 0 : return GSS_S_COMPLETE;
169 : } else {
170 0 : return GSS_S_GAP_TOKEN;
171 : }
172 : }
173 :
174 0 : assert(o->length > 0);
175 :
176 : /* sequence number smaller the first sequence number */
177 0 : if (seq_num < o->elem[o->length - 1]) {
178 0 : if (r)
179 0 : return(GSS_S_OLD_TOKEN);
180 : else
181 0 : return(GSS_S_UNSEQ_TOKEN);
182 : }
183 :
184 0 : if (seq_num == o->elem[o->length - 1]) {
185 0 : return GSS_S_DUPLICATE_TOKEN;
186 : }
187 :
188 0 : for (i = 0; i < o->length - 1; i++) {
189 0 : if (o->elem[i] == seq_num)
190 0 : return GSS_S_DUPLICATE_TOKEN;
191 0 : if (o->elem[i + 1] < seq_num && o->elem[i] < seq_num) {
192 0 : elem_insert(o, i, seq_num);
193 0 : if (r)
194 0 : return GSS_S_COMPLETE;
195 : else
196 0 : return GSS_S_UNSEQ_TOKEN;
197 : }
198 : }
199 :
200 0 : return GSS_S_FAILURE;
201 : }
202 :
203 : OM_uint32
204 75939 : _gssapi_msg_order_f(OM_uint32 flags)
205 : {
206 75939 : return flags & (GSS_C_SEQUENCE_FLAG|GSS_C_REPLAY_FLAG);
207 : }
208 :
209 : /*
210 : * Translate `o` into inter-process format and export in to `sp'.
211 : */
212 :
213 : krb5_error_code
214 0 : _gssapi_msg_order_export(krb5_storage *sp, struct gss_msg_order *o)
215 : {
216 0 : krb5_error_code kret;
217 0 : OM_uint32 i;
218 :
219 0 : kret = krb5_store_int32(sp, o->flags);
220 0 : if (kret)
221 0 : return kret;
222 0 : kret = krb5_store_int32(sp, o->start);
223 0 : if (kret)
224 0 : return kret;
225 0 : kret = krb5_store_int32(sp, o->length);
226 0 : if (kret)
227 0 : return kret;
228 0 : kret = krb5_store_int32(sp, o->jitter_window);
229 0 : if (kret)
230 0 : return kret;
231 0 : kret = krb5_store_int32(sp, o->first_seq);
232 0 : if (kret)
233 0 : return kret;
234 :
235 0 : for (i = 0; i < o->jitter_window; i++) {
236 0 : kret = krb5_store_int32(sp, o->elem[i]);
237 0 : if (kret)
238 0 : return kret;
239 : }
240 :
241 0 : return 0;
242 : }
243 :
244 : OM_uint32
245 0 : _gssapi_msg_order_import(OM_uint32 *minor_status,
246 : krb5_storage *sp,
247 : struct gss_msg_order **o)
248 : {
249 0 : OM_uint32 ret;
250 0 : krb5_error_code kret;
251 0 : int32_t i, flags, start, length, jitter_window, first_seq;
252 :
253 0 : kret = krb5_ret_int32(sp, &flags);
254 0 : if (kret)
255 0 : goto failed;
256 0 : kret = krb5_ret_int32(sp, &start);
257 0 : if (kret)
258 0 : goto failed;
259 0 : kret = krb5_ret_int32(sp, &length);
260 0 : if (kret)
261 0 : goto failed;
262 0 : kret = krb5_ret_int32(sp, &jitter_window);
263 0 : if (kret)
264 0 : goto failed;
265 0 : kret = krb5_ret_int32(sp, &first_seq);
266 0 : if (kret)
267 0 : goto failed;
268 :
269 0 : ret = msg_order_alloc(minor_status, o, jitter_window);
270 0 : if (ret != GSS_S_COMPLETE)
271 0 : return ret;
272 :
273 0 : (*o)->flags = flags;
274 0 : (*o)->start = start;
275 0 : (*o)->length = length;
276 0 : (*o)->jitter_window = jitter_window;
277 0 : (*o)->first_seq = first_seq;
278 :
279 0 : for( i = 0; i < jitter_window; i++ ) {
280 0 : kret = krb5_ret_int32(sp, (int32_t*)&((*o)->elem[i]));
281 0 : if (kret)
282 0 : goto failed;
283 : }
284 :
285 0 : *minor_status = 0;
286 0 : return GSS_S_COMPLETE;
287 :
288 0 : failed:
289 0 : _gssapi_msg_order_destroy(o);
290 0 : *minor_status = kret;
291 0 : return GSS_S_FAILURE;
292 : }
|