1271294Sngie/* 2271294Sngie * Copyright (c) 2003 - 2006 Kungliga Tekniska H��gskolan 3271294Sngie * (Royal Institute of Technology, Stockholm, Sweden). 4271294Sngie * All rights reserved. 5271294Sngie * 6271294Sngie * Redistribution and use in source and binary forms, with or without 7271294Sngie * modification, are permitted provided that the following conditions 8271294Sngie * are met: 9271294Sngie * 10271294Sngie * 1. Redistributions of source code must retain the above copyright 11271294Sngie * notice, this list of conditions and the following disclaimer. 12271294Sngie * 13271294Sngie * 2. Redistributions in binary form must reproduce the above copyright 14271294Sngie * notice, this list of conditions and the following disclaimer in the 15271294Sngie * documentation and/or other materials provided with the distribution. 16271294Sngie * 17271294Sngie * 3. Neither the name of the Institute nor the names of its contributors 18271294Sngie * may be used to endorse or promote products derived from this software 19271294Sngie * without specific prior written permission. 20271294Sngie * 21271294Sngie * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22271294Sngie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23271294Sngie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24271294Sngie * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25271294Sngie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26271294Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27271294Sngie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28271294Sngie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29271294Sngie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30271294Sngie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31271294Sngie * SUCH DAMAGE. 32271294Sngie */ 33271294Sngie 34271294Sngie#include "gsskrb5_locl.h" 35271294Sngie 36271294Sngie#define DEFAULT_JITTER_WINDOW 20 37271294Sngie 38271294Sngiestruct gss_msg_order { 39271294Sngie OM_uint32 flags; 40271294Sngie OM_uint32 start; 41271294Sngie OM_uint32 length; 42271294Sngie OM_uint32 jitter_window; 43271294Sngie OM_uint32 first_seq; 44271294Sngie OM_uint32 elem[1]; 45271294Sngie}; 46271294Sngie 47271294Sngie 48271294Sngie/* 49271294Sngie * 50271294Sngie */ 51271294Sngie 52271294Sngiestatic OM_uint32 53271294Sngiemsg_order_alloc(OM_uint32 *minor_status, 54271294Sngie struct gss_msg_order **o, 55271294Sngie OM_uint32 jitter_window) 56271294Sngie{ 57271294Sngie size_t len; 58271294Sngie 59271294Sngie len = jitter_window * sizeof((*o)->elem[0]); 60271294Sngie len += sizeof(**o); 61271294Sngie len -= sizeof((*o)->elem[0]); 62271294Sngie 63271294Sngie *o = calloc(1, len); 64271294Sngie if (*o == NULL) { 65271294Sngie *minor_status = ENOMEM; 66271294Sngie return GSS_S_FAILURE; 67271294Sngie } 68271294Sngie 69271294Sngie *minor_status = 0; 70271294Sngie return GSS_S_COMPLETE; 71271294Sngie} 72271294Sngie 73271294Sngie/* 74271294Sngie * 75271294Sngie */ 76271294Sngie 77271294SngieOM_uint32 78271294Sngie_gssapi_msg_order_create(OM_uint32 *minor_status, 79271294Sngie struct gss_msg_order **o, 80271294Sngie OM_uint32 flags, 81271294Sngie OM_uint32 seq_num, 82271294Sngie OM_uint32 jitter_window, 83271294Sngie int use_64) 84271294Sngie{ 85271294Sngie OM_uint32 ret; 86271294Sngie 87271294Sngie if (jitter_window == 0) 88271294Sngie jitter_window = DEFAULT_JITTER_WINDOW; 89271294Sngie 90271294Sngie ret = msg_order_alloc(minor_status, o, jitter_window); 91271294Sngie if(ret != GSS_S_COMPLETE) 92271294Sngie return ret; 93271294Sngie 94271294Sngie (*o)->flags = flags; 95271294Sngie (*o)->length = 0; 96271294Sngie (*o)->first_seq = seq_num; 97271294Sngie (*o)->jitter_window = jitter_window; 98271294Sngie (*o)->elem[0] = seq_num - 1; 99271294Sngie 100271294Sngie *minor_status = 0; 101271294Sngie return GSS_S_COMPLETE; 102271294Sngie} 103271294Sngie 104271294SngieOM_uint32 105271294Sngie_gssapi_msg_order_destroy(struct gss_msg_order **m) 106271294Sngie{ 107271294Sngie free(*m); 108271294Sngie *m = NULL; 109271294Sngie return GSS_S_COMPLETE; 110271294Sngie} 111271294Sngie 112271294Sngiestatic void 113271294Sngieelem_set(struct gss_msg_order *o, unsigned int slot, OM_uint32 val) 114271294Sngie{ 115271294Sngie o->elem[slot % o->jitter_window] = val; 116271294Sngie} 117271294Sngie 118271294Sngiestatic void 119elem_insert(struct gss_msg_order *o, 120 unsigned int after_slot, 121 OM_uint32 seq_num) 122{ 123 assert(o->jitter_window > after_slot); 124 125 if (o->length > after_slot) 126 memmove(&o->elem[after_slot + 1], &o->elem[after_slot], 127 (o->length - after_slot - 1) * sizeof(o->elem[0])); 128 129 elem_set(o, after_slot, seq_num); 130 131 if (o->length < o->jitter_window) 132 o->length++; 133} 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 140OM_uint32 141_gssapi_msg_order_check(struct gss_msg_order *o, OM_uint32 seq_num) 142{ 143 OM_uint32 r; 144 size_t i; 145 146 if (o == NULL) 147 return GSS_S_COMPLETE; 148 149 if ((o->flags & (GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) == 0) 150 return GSS_S_COMPLETE; 151 152 /* check if the packet is the next in order */ 153 if (o->elem[0] == seq_num - 1) { 154 elem_insert(o, 0, seq_num); 155 return GSS_S_COMPLETE; 156 } 157 158 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 if (seq_num > o->elem[0] 163 || seq_num < o->first_seq 164 || o->length == 0) 165 { 166 elem_insert(o, 0, seq_num); 167 if (r) { 168 return GSS_S_COMPLETE; 169 } else { 170 return GSS_S_GAP_TOKEN; 171 } 172 } 173 174 assert(o->length > 0); 175 176 /* sequence number smaller the first sequence number */ 177 if (seq_num < o->elem[o->length - 1]) { 178 if (r) 179 return(GSS_S_OLD_TOKEN); 180 else 181 return(GSS_S_UNSEQ_TOKEN); 182 } 183 184 if (seq_num == o->elem[o->length - 1]) { 185 return GSS_S_DUPLICATE_TOKEN; 186 } 187 188 for (i = 0; i < o->length - 1; i++) { 189 if (o->elem[i] == seq_num) 190 return GSS_S_DUPLICATE_TOKEN; 191 if (o->elem[i + 1] < seq_num && o->elem[i] < seq_num) { 192 elem_insert(o, i, seq_num); 193 if (r) 194 return GSS_S_COMPLETE; 195 else 196 return GSS_S_UNSEQ_TOKEN; 197 } 198 } 199 200 return GSS_S_FAILURE; 201} 202 203OM_uint32 204_gssapi_msg_order_f(OM_uint32 flags) 205{ 206 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 213krb5_error_code 214_gssapi_msg_order_export(krb5_storage *sp, struct gss_msg_order *o) 215{ 216 krb5_error_code kret; 217 OM_uint32 i; 218 219 kret = krb5_store_int32(sp, o->flags); 220 if (kret) 221 return kret; 222 kret = krb5_store_int32(sp, o->start); 223 if (kret) 224 return kret; 225 kret = krb5_store_int32(sp, o->length); 226 if (kret) 227 return kret; 228 kret = krb5_store_int32(sp, o->jitter_window); 229 if (kret) 230 return kret; 231 kret = krb5_store_int32(sp, o->first_seq); 232 if (kret) 233 return kret; 234 235 for (i = 0; i < o->jitter_window; i++) { 236 kret = krb5_store_int32(sp, o->elem[i]); 237 if (kret) 238 return kret; 239 } 240 241 return 0; 242} 243 244OM_uint32 245_gssapi_msg_order_import(OM_uint32 *minor_status, 246 krb5_storage *sp, 247 struct gss_msg_order **o) 248{ 249 OM_uint32 ret; 250 krb5_error_code kret; 251 int32_t i, flags, start, length, jitter_window, first_seq; 252 253 kret = krb5_ret_int32(sp, &flags); 254 if (kret) 255 goto failed; 256 kret = krb5_ret_int32(sp, &start); 257 if (kret) 258 goto failed; 259 kret = krb5_ret_int32(sp, &length); 260 if (kret) 261 goto failed; 262 kret = krb5_ret_int32(sp, &jitter_window); 263 if (kret) 264 goto failed; 265 kret = krb5_ret_int32(sp, &first_seq); 266 if (kret) 267 goto failed; 268 269 ret = msg_order_alloc(minor_status, o, jitter_window); 270 if (ret != GSS_S_COMPLETE) 271 return ret; 272 273 (*o)->flags = flags; 274 (*o)->start = start; 275 (*o)->length = length; 276 (*o)->jitter_window = jitter_window; 277 (*o)->first_seq = first_seq; 278 279 for( i = 0; i < jitter_window; i++ ) { 280 kret = krb5_ret_int32(sp, (int32_t*)&((*o)->elem[i])); 281 if (kret) 282 goto failed; 283 } 284 285 *minor_status = 0; 286 return GSS_S_COMPLETE; 287 288failed: 289 _gssapi_msg_order_destroy(o); 290 *minor_status = kret; 291 return GSS_S_FAILURE; 292} 293