r8302: import mini HEIMDAL into the tree
[samba.git] / source4 / heimdal / lib / gssapi / sequence.c
1 /*
2  * Copyright (c) 2003 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: sequence.c,v 1.5 2005/04/27 17:49:43 lha Exp $");
37
38 #define DEFAULT_JITTER_WINDOW 20
39
40 struct gss_msg_order {
41     OM_uint32 flags;
42     OM_uint32 start;
43     OM_uint32 length;
44     OM_uint32 jitter_window;
45     OM_uint32 first_seq;
46     OM_uint32 elem[1];
47 };
48
49 /*
50  *
51  */
52
53 OM_uint32
54 _gssapi_msg_order_create(OM_uint32 *minor_status,
55                          struct gss_msg_order **o, 
56                          OM_uint32 flags, 
57                          OM_uint32 seq_num, 
58                          OM_uint32 jitter_window,
59                          int use_64)
60 {
61     size_t len;
62
63     if (jitter_window == 0)
64         jitter_window = DEFAULT_JITTER_WINDOW;
65
66     len = jitter_window * sizeof((*o)->elem[0]);
67     len += sizeof(**o);
68     len -= sizeof((*o)->elem[0]);
69     
70     *o = malloc(len);
71     if (*o == NULL) {
72         *minor_status = ENOMEM;
73         return GSS_S_FAILURE;
74     }   
75     memset(*o, 0, len);
76     (*o)->flags = flags;
77     (*o)->length = 0;
78     (*o)->first_seq = seq_num;
79     (*o)->jitter_window = jitter_window;
80     (*o)->elem[0] = seq_num - 1;
81
82     *minor_status = 0;
83     return GSS_S_COMPLETE;
84 }
85
86 OM_uint32
87 _gssapi_msg_order_destroy(struct gss_msg_order **m)
88 {
89     free(*m);
90     *m = NULL;
91     return GSS_S_COMPLETE;
92 }
93
94 static void
95 elem_set(struct gss_msg_order *o, unsigned int slot, OM_uint32 val)
96 {
97     o->elem[slot % o->jitter_window] = val;
98 }
99
100 static void
101 elem_insert(struct gss_msg_order *o, 
102             unsigned int after_slot,
103             OM_uint32 seq_num)
104 {
105     assert(o->jitter_window > after_slot);
106
107     if (o->length > after_slot)
108         memmove(&o->elem[after_slot + 1], &o->elem[after_slot],
109                 (o->length - after_slot - 1) * sizeof(o->elem[0]));
110
111     elem_set(o, after_slot, seq_num);
112
113     if (o->length < o->jitter_window)
114         o->length++;
115 }
116
117 /* rule 1: expected sequence number */
118 /* rule 2: > expected sequence number */
119 /* rule 3: seqnum < seqnum(first) */
120 /* rule 4+5: seqnum in [seqnum(first),seqnum(last)]  */
121
122 OM_uint32
123 _gssapi_msg_order_check(struct gss_msg_order *o, OM_uint32 seq_num)
124 {
125     OM_uint32 r;
126     int i;
127
128     if (o == NULL)
129         return GSS_S_COMPLETE;
130
131     if ((o->flags & (GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) == 0)
132         return GSS_S_COMPLETE;
133
134     /* check if the packet is the next in order */
135     if (o->elem[0] == seq_num - 1) {
136         elem_insert(o, 0, seq_num);
137         return GSS_S_COMPLETE;
138     }
139
140     r = (o->flags & (GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG))==GSS_C_REPLAY_FLAG;
141
142     /* sequence number larger then largest sequence number 
143      * or smaller then the first sequence number */
144     if (seq_num > o->elem[0]
145         || seq_num < o->first_seq
146         || o->length == 0) 
147     {
148         elem_insert(o, 0, seq_num);
149         if (r) {
150             return GSS_S_COMPLETE;
151         } else {
152             return GSS_S_GAP_TOKEN;
153         }
154     }
155
156     assert(o->length > 0);
157
158     /* sequence number smaller the first sequence number */
159     if (seq_num < o->elem[o->length - 1]) {
160         if (r)
161             return(GSS_S_OLD_TOKEN);
162         else
163             return(GSS_S_UNSEQ_TOKEN);
164     }
165
166     if (seq_num == o->elem[o->length - 1]) {
167         return GSS_S_DUPLICATE_TOKEN;
168     }
169
170     for (i = 0; i < o->length - 1; i++) {
171         if (o->elem[i] == seq_num)
172             return GSS_S_DUPLICATE_TOKEN;
173         if (o->elem[i + 1] < seq_num && o->elem[i] < seq_num) {
174             elem_insert(o, i, seq_num);
175             if (r)
176                 return GSS_S_COMPLETE;
177             else
178                 return GSS_S_UNSEQ_TOKEN;
179         }
180     }
181
182     return GSS_S_FAILURE;
183 }
184
185 OM_uint32
186 _gssapi_msg_order_f(OM_uint32 flags)
187 {
188     return flags & (GSS_C_SEQUENCE_FLAG|GSS_C_REPLAY_FLAG);
189 }