2 * Copyright (c) 2003 - 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 #include "krb5/gsskrb5_locl.h"
36 RCSID("$Id: sequence.c,v 1.8 2006/10/07 22:15:32 lha Exp $");
38 #define DEFAULT_JITTER_WINDOW 20
40 struct gss_msg_order
{
44 OM_uint32 jitter_window
;
55 msg_order_alloc(OM_uint32
*minor_status
,
56 struct gss_msg_order
**o
,
57 OM_uint32 jitter_window
)
61 len
= jitter_window
* sizeof((*o
)->elem
[0]);
63 len
-= sizeof((*o
)->elem
[0]);
67 *minor_status
= ENOMEM
;
72 return GSS_S_COMPLETE
;
80 _gssapi_msg_order_create(OM_uint32
*minor_status
,
81 struct gss_msg_order
**o
,
84 OM_uint32 jitter_window
,
89 if (jitter_window
== 0)
90 jitter_window
= DEFAULT_JITTER_WINDOW
;
92 ret
= msg_order_alloc(minor_status
, o
, jitter_window
);
93 if(ret
!= GSS_S_COMPLETE
)
98 (*o
)->first_seq
= seq_num
;
99 (*o
)->jitter_window
= jitter_window
;
100 (*o
)->elem
[0] = seq_num
- 1;
103 return GSS_S_COMPLETE
;
107 _gssapi_msg_order_destroy(struct gss_msg_order
**m
)
111 return GSS_S_COMPLETE
;
115 elem_set(struct gss_msg_order
*o
, unsigned int slot
, OM_uint32 val
)
117 o
->elem
[slot
% o
->jitter_window
] = val
;
121 elem_insert(struct gss_msg_order
*o
,
122 unsigned int after_slot
,
125 assert(o
->jitter_window
> after_slot
);
127 if (o
->length
> after_slot
)
128 memmove(&o
->elem
[after_slot
+ 1], &o
->elem
[after_slot
],
129 (o
->length
- after_slot
- 1) * sizeof(o
->elem
[0]));
131 elem_set(o
, after_slot
, seq_num
);
133 if (o
->length
< o
->jitter_window
)
137 /* rule 1: expected sequence number */
138 /* rule 2: > expected sequence number */
139 /* rule 3: seqnum < seqnum(first) */
140 /* rule 4+5: seqnum in [seqnum(first),seqnum(last)] */
143 _gssapi_msg_order_check(struct gss_msg_order
*o
, OM_uint32 seq_num
)
149 return GSS_S_COMPLETE
;
151 if ((o
->flags
& (GSS_C_REPLAY_FLAG
|GSS_C_SEQUENCE_FLAG
)) == 0)
152 return GSS_S_COMPLETE
;
154 /* check if the packet is the next in order */
155 if (o
->elem
[0] == seq_num
- 1) {
156 elem_insert(o
, 0, seq_num
);
157 return GSS_S_COMPLETE
;
160 r
= (o
->flags
& (GSS_C_REPLAY_FLAG
|GSS_C_SEQUENCE_FLAG
))==GSS_C_REPLAY_FLAG
;
162 /* sequence number larger then largest sequence number
163 * or smaller then the first sequence number */
164 if (seq_num
> o
->elem
[0]
165 || seq_num
< o
->first_seq
168 elem_insert(o
, 0, seq_num
);
170 return GSS_S_COMPLETE
;
172 return GSS_S_GAP_TOKEN
;
176 assert(o
->length
> 0);
178 /* sequence number smaller the first sequence number */
179 if (seq_num
< o
->elem
[o
->length
- 1]) {
181 return(GSS_S_OLD_TOKEN
);
183 return(GSS_S_UNSEQ_TOKEN
);
186 if (seq_num
== o
->elem
[o
->length
- 1]) {
187 return GSS_S_DUPLICATE_TOKEN
;
190 for (i
= 0; i
< o
->length
- 1; i
++) {
191 if (o
->elem
[i
] == seq_num
)
192 return GSS_S_DUPLICATE_TOKEN
;
193 if (o
->elem
[i
+ 1] < seq_num
&& o
->elem
[i
] < seq_num
) {
194 elem_insert(o
, i
, seq_num
);
196 return GSS_S_COMPLETE
;
198 return GSS_S_UNSEQ_TOKEN
;
202 return GSS_S_FAILURE
;
206 _gssapi_msg_order_f(OM_uint32 flags
)
208 return flags
& (GSS_C_SEQUENCE_FLAG
|GSS_C_REPLAY_FLAG
);
212 * Translate `o` into inter-process format and export in to `sp'.
216 _gssapi_msg_order_export(krb5_storage
*sp
, struct gss_msg_order
*o
)
218 krb5_error_code kret
;
221 kret
= krb5_store_int32(sp
, o
->flags
);
224 kret
= krb5_store_int32(sp
, o
->start
);
227 kret
= krb5_store_int32(sp
, o
->length
);
230 kret
= krb5_store_int32(sp
, o
->jitter_window
);
233 kret
= krb5_store_int32(sp
, o
->first_seq
);
237 for (i
= 0; i
< o
->jitter_window
; i
++) {
238 kret
= krb5_store_int32(sp
, o
->elem
[i
]);
247 _gssapi_msg_order_import(OM_uint32
*minor_status
,
249 struct gss_msg_order
**o
)
252 krb5_error_code kret
;
253 int32_t i
, flags
, start
, length
, jitter_window
, first_seq
;
255 kret
= krb5_ret_int32(sp
, &flags
);
258 ret
= krb5_ret_int32(sp
, &start
);
261 ret
= krb5_ret_int32(sp
, &length
);
264 ret
= krb5_ret_int32(sp
, &jitter_window
);
267 ret
= krb5_ret_int32(sp
, &first_seq
);
271 ret
= msg_order_alloc(minor_status
, o
, jitter_window
);
272 if (ret
!= GSS_S_COMPLETE
)
277 (*o
)->length
= length
;
278 (*o
)->jitter_window
= jitter_window
;
279 (*o
)->first_seq
= first_seq
;
281 for( i
= 0; i
< jitter_window
; i
++ ) {
282 kret
= krb5_ret_int32(sp
, (int32_t*)&((*o
)->elem
[i
]));
288 return GSS_S_COMPLETE
;
291 _gssapi_msg_order_destroy(o
);
292 *minor_status
= kret
;
293 return GSS_S_FAILURE
;