1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <sys/note.h>
28*7c478bd9Sstevel@tonic-gate #include "dh_gssapi.h"
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * This module implements the interfaces for replay and out-of-sequence
32*7c478bd9Sstevel@tonic-gate  * detection.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #define	WBITS_DEF 8 * sizeof (seq_word_t) /*  Bits in a seq_word_t */
36*7c478bd9Sstevel@tonic-gate static const int WBITS = WBITS_DEF; /* Stored in a static int for debuging */
37*7c478bd9Sstevel@tonic-gate static const int NBITS =  SSIZE * WBITS_DEF; /* Total bits in the sequence */
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate /*
40*7c478bd9Sstevel@tonic-gate  * The following routines are for debuging:
41*7c478bd9Sstevel@tonic-gate  *	__context_debug_set_next_seqno
42*7c478bd9Sstevel@tonic-gate  *	__context_debug_get_next_seqno
43*7c478bd9Sstevel@tonic-gate  *	__context_debug_set_last_seqno
44*7c478bd9Sstevel@tonic-gate  *	__context_debug_get_last_seqno
45*7c478bd9Sstevel@tonic-gate  *	__context_debug_print_seq_hist
46*7c478bd9Sstevel@tonic-gate  *      __context_debug_get_hist_size
47*7c478bd9Sstevel@tonic-gate  *	__context_debug
48*7c478bd9Sstevel@tonic-gate  *
49*7c478bd9Sstevel@tonic-gate  * These routines are declared static and there addresses placed into a table.
50*7c478bd9Sstevel@tonic-gate  * There is one publicly declare routine __context_debug_entry that is used
51*7c478bd9Sstevel@tonic-gate  * to fetch these entries. This way other routines can be added with out
52*7c478bd9Sstevel@tonic-gate  * changing the map-version file. This is being done for use with a libgss
53*7c478bd9Sstevel@tonic-gate  * test driver. In particular this technique is being used to implement
54*7c478bd9Sstevel@tonic-gate  * a pseudo libgss entry point gss_context_cntrl. Its declaration is
55*7c478bd9Sstevel@tonic-gate  * OM_uint32
56*7c478bd9Sstevel@tonic-gate  * gss_context_cntl(OM_uint32 *minor, gss_ctx_id_t ctx, int cmd, void *argp);
57*7c478bd9Sstevel@tonic-gate  *
58*7c478bd9Sstevel@tonic-gate  * Hence the declaratin of the debug routines below.
59*7c478bd9Sstevel@tonic-gate  */
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate /* Set the next sequence number to be sent */
62*7c478bd9Sstevel@tonic-gate static OM_uint32
__context_debug_set_next_seqno(OM_uint32 * minor,gss_ctx_id_t cntx,void * argp)63*7c478bd9Sstevel@tonic-gate __context_debug_set_next_seqno(OM_uint32 *minor, gss_ctx_id_t cntx, void *argp)
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate 	dh_gss_context_t ctx = (dh_gss_context_t)cntx;
66*7c478bd9Sstevel@tonic-gate 	OM_uint32 seqno = (OM_uint32)(intptr_t)argp;
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	if (minor == 0)
69*7c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	*minor = DH_SUCCESS;
72*7c478bd9Sstevel@tonic-gate 	/*
73*7c478bd9Sstevel@tonic-gate 	 * If context, set the sequence number.
74*7c478bd9Sstevel@tonic-gate 	 * Locking should not be necessary since OM_uint32 should be atomic
75*7c478bd9Sstevel@tonic-gate 	 * size.
76*7c478bd9Sstevel@tonic-gate 	 */
77*7c478bd9Sstevel@tonic-gate 	if (ctx) {
78*7c478bd9Sstevel@tonic-gate 		mutex_lock(&ctx->seqno_lock);
79*7c478bd9Sstevel@tonic-gate 		ctx->next_seqno = seqno;
80*7c478bd9Sstevel@tonic-gate 		mutex_unlock(&ctx->seqno_lock);
81*7c478bd9Sstevel@tonic-gate 	}
82*7c478bd9Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
83*7c478bd9Sstevel@tonic-gate }
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate /* Get the next sequence number to be sent */
86*7c478bd9Sstevel@tonic-gate static OM_uint32
__context_debug_get_next_seqno(OM_uint32 * minor,gss_ctx_id_t cntx,void * argp)87*7c478bd9Sstevel@tonic-gate __context_debug_get_next_seqno(OM_uint32 *minor, gss_ctx_id_t cntx, void *argp)
88*7c478bd9Sstevel@tonic-gate {
89*7c478bd9Sstevel@tonic-gate 	dh_gss_context_t ctx = (dh_gss_context_t)cntx;
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	if (minor == 0)
92*7c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	if (argp == 0)
95*7c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	*minor = DH_SUCCESS;
98*7c478bd9Sstevel@tonic-gate 	/* Grap the next sequence number */
99*7c478bd9Sstevel@tonic-gate 	*(OM_uint32 *)argp = ctx->next_seqno;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate /* Set the last sequence number to was seen */
105*7c478bd9Sstevel@tonic-gate static OM_uint32
__context_debug_set_last_seqno(OM_uint32 * minor,gss_ctx_id_t cntx,void * argp)106*7c478bd9Sstevel@tonic-gate __context_debug_set_last_seqno(OM_uint32 *minor, gss_ctx_id_t cntx, void *argp)
107*7c478bd9Sstevel@tonic-gate {
108*7c478bd9Sstevel@tonic-gate 	dh_gss_context_t ctx = (dh_gss_context_t)cntx;
109*7c478bd9Sstevel@tonic-gate 	OM_uint32 seqno = (OM_uint32)(intptr_t)argp;
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	if (minor == 0)
112*7c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	*minor = DH_SUCCESS;
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	/*
117*7c478bd9Sstevel@tonic-gate 	 * If context, set the sequence number.
118*7c478bd9Sstevel@tonic-gate 	 * Locking should not be necessary since OM_uint32 should be atomic
119*7c478bd9Sstevel@tonic-gate 	 * size.
120*7c478bd9Sstevel@tonic-gate 	 */
121*7c478bd9Sstevel@tonic-gate 	if (ctx) {
122*7c478bd9Sstevel@tonic-gate 		mutex_lock(&ctx->hist.seq_arr_lock);
123*7c478bd9Sstevel@tonic-gate 		ctx->hist.seqno = seqno;
124*7c478bd9Sstevel@tonic-gate 		mutex_unlock(&ctx->hist.seq_arr_lock);
125*7c478bd9Sstevel@tonic-gate 	}
126*7c478bd9Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
127*7c478bd9Sstevel@tonic-gate }
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate /* Get the last sequence number seen */
130*7c478bd9Sstevel@tonic-gate static OM_uint32
__context_debug_get_last_seqno(OM_uint32 * minor,gss_ctx_id_t cntx,void * argp)131*7c478bd9Sstevel@tonic-gate __context_debug_get_last_seqno(OM_uint32 *minor, gss_ctx_id_t cntx, void *argp)
132*7c478bd9Sstevel@tonic-gate {
133*7c478bd9Sstevel@tonic-gate 	dh_gss_context_t ctx = (dh_gss_context_t)cntx;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	if (minor == 0)
136*7c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	if (argp == 0)
139*7c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	*minor = DH_SUCCESS;
142*7c478bd9Sstevel@tonic-gate 	/* Grap the next sequence number */
143*7c478bd9Sstevel@tonic-gate 	*(OM_uint32 *)argp = ctx->hist.seqno;
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
146*7c478bd9Sstevel@tonic-gate }
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate static seq_word_t
rev(seq_word_t r)149*7c478bd9Sstevel@tonic-gate rev(seq_word_t r)
150*7c478bd9Sstevel@tonic-gate {
151*7c478bd9Sstevel@tonic-gate 	int i;
152*7c478bd9Sstevel@tonic-gate 	seq_word_t t = 0;
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < WBITS; i++)
155*7c478bd9Sstevel@tonic-gate 		if (r & ((seq_word_t)1 << i))
156*7c478bd9Sstevel@tonic-gate 			t |= ((seq_word_t)1 << (WBITS - 1 - i));
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	return (t);
159*7c478bd9Sstevel@tonic-gate }
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate /* Print out the sequence history to stderr */
162*7c478bd9Sstevel@tonic-gate static OM_uint32
__context_debug_print_seq_hist(OM_uint32 * minor,gss_ctx_id_t cntx,void * argp)163*7c478bd9Sstevel@tonic-gate __context_debug_print_seq_hist(OM_uint32 *minor, gss_ctx_id_t cntx, void *argp)
164*7c478bd9Sstevel@tonic-gate {
165*7c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(argp))
166*7c478bd9Sstevel@tonic-gate 	dh_gss_context_t ctx = (dh_gss_context_t)cntx;
167*7c478bd9Sstevel@tonic-gate 	int i;
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	if (minor == 0)
170*7c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	*minor = DH_SUCCESS;
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	/* Print out the sequence history */
175*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%u: ", ctx->hist.seqno);
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < SSIZE; i++)
178*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%016.16llx", rev(ctx->hist.arr[i]));
179*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\n");
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
182*7c478bd9Sstevel@tonic-gate }
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate /* Fetch the size of the history */
185*7c478bd9Sstevel@tonic-gate static OM_uint32
__context_debug_get_hist_size(OM_uint32 * minor,gss_ctx_id_t cntx,void * argp)186*7c478bd9Sstevel@tonic-gate __context_debug_get_hist_size(OM_uint32 *minor, gss_ctx_id_t cntx, void *argp)
187*7c478bd9Sstevel@tonic-gate {
188*7c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(cntx))
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	if (minor == 0)
191*7c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
192*7c478bd9Sstevel@tonic-gate 	if (argp == 0)
193*7c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 	*minor = DH_SUCCESS;
196*7c478bd9Sstevel@tonic-gate 	*(OM_uint32 *)argp = NBITS;
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
199*7c478bd9Sstevel@tonic-gate }
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate /* Set the debug flag on the context */
202*7c478bd9Sstevel@tonic-gate static OM_uint32
__context_debug(OM_uint32 * minor,gss_ctx_id_t cntx,void * argp)203*7c478bd9Sstevel@tonic-gate __context_debug(OM_uint32 *minor, gss_ctx_id_t cntx, void *argp)
204*7c478bd9Sstevel@tonic-gate {
205*7c478bd9Sstevel@tonic-gate 	dh_gss_context_t ctx = (dh_gss_context_t)cntx;
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	if (minor == 0)
208*7c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 	*minor = DH_SUCCESS;
211*7c478bd9Sstevel@tonic-gate 	ctx->debug = (OM_uint32)(intptr_t)argp;
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
214*7c478bd9Sstevel@tonic-gate }
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate /* Type to descript debug routines */
217*7c478bd9Sstevel@tonic-gate typedef OM_uint32 (*fptr)(OM_uint32 *, gss_ctx_id_t, void *);
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate /* Array of debug entries defined above */
220*7c478bd9Sstevel@tonic-gate static fptr __context_debug_entry_array[] = {
221*7c478bd9Sstevel@tonic-gate 	__context_debug,
222*7c478bd9Sstevel@tonic-gate 	__context_debug_set_next_seqno,
223*7c478bd9Sstevel@tonic-gate 	__context_debug_get_next_seqno,
224*7c478bd9Sstevel@tonic-gate 	__context_debug_print_seq_hist,
225*7c478bd9Sstevel@tonic-gate 	__context_debug_get_hist_size,
226*7c478bd9Sstevel@tonic-gate 	__context_debug_set_last_seqno,
227*7c478bd9Sstevel@tonic-gate 	__context_debug_get_last_seqno
228*7c478bd9Sstevel@tonic-gate };
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate /* Structure to hold the debug entries */
231*7c478bd9Sstevel@tonic-gate static struct {
232*7c478bd9Sstevel@tonic-gate 	int no_entries;
233*7c478bd9Sstevel@tonic-gate 	fptr  *entrys;
234*7c478bd9Sstevel@tonic-gate } __context_debug_entry_points = {
235*7c478bd9Sstevel@tonic-gate 	sizeof (__context_debug_entry_array)/sizeof (fptr),
236*7c478bd9Sstevel@tonic-gate 	__context_debug_entry_array
237*7c478bd9Sstevel@tonic-gate };
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate /*
240*7c478bd9Sstevel@tonic-gate  * Exported entry point for debug routines. A call to this routine will
241*7c478bd9Sstevel@tonic-gate  * return a pointer to the above structure.
242*7c478bd9Sstevel@tonic-gate  */
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate void*
__context_debug_entry()245*7c478bd9Sstevel@tonic-gate __context_debug_entry()
246*7c478bd9Sstevel@tonic-gate {
247*7c478bd9Sstevel@tonic-gate 	return (&__context_debug_entry_points);
248*7c478bd9Sstevel@tonic-gate }
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate /* *************** End of Debug Section ***************** */
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate /* Clear all the bits in a sequence array */
253*7c478bd9Sstevel@tonic-gate static void
clear_all_bits(seq_array_t sa)254*7c478bd9Sstevel@tonic-gate clear_all_bits(seq_array_t sa)
255*7c478bd9Sstevel@tonic-gate {
256*7c478bd9Sstevel@tonic-gate 	unsigned int i;
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < SSIZE; i++)
259*7c478bd9Sstevel@tonic-gate 		sa->arr[i] = (seq_word_t)0;
260*7c478bd9Sstevel@tonic-gate }
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate /* Check that a bit is set in a sequence array */
263*7c478bd9Sstevel@tonic-gate static unsigned int
check_bit(seq_array_t sa,unsigned int bit)264*7c478bd9Sstevel@tonic-gate check_bit(seq_array_t sa, unsigned int bit)
265*7c478bd9Sstevel@tonic-gate {
266*7c478bd9Sstevel@tonic-gate 	if (bit >=  NBITS)
267*7c478bd9Sstevel@tonic-gate 		return (0);
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	return (sa->arr[bit/WBITS] & ((seq_word_t)1 << (bit % WBITS)) ? 1 : 0);
270*7c478bd9Sstevel@tonic-gate }
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate /* Set a bit in a sequence array */
273*7c478bd9Sstevel@tonic-gate void
set_bit(seq_array_t sa,unsigned int bit)274*7c478bd9Sstevel@tonic-gate set_bit(seq_array_t sa, unsigned int bit)
275*7c478bd9Sstevel@tonic-gate {
276*7c478bd9Sstevel@tonic-gate 	if (bit < NBITS)
277*7c478bd9Sstevel@tonic-gate 		sa->arr[bit/WBITS] |= ((seq_word_t)1 << (bit % WBITS));
278*7c478bd9Sstevel@tonic-gate }
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate /* Clear a bit in a sequence array */
281*7c478bd9Sstevel@tonic-gate /*
282*7c478bd9Sstevel@tonic-gate  * This function is not used, but is here as a comment for completeness.
283*7c478bd9Sstevel@tonic-gate  * Lint will complain if it is not commented out.
284*7c478bd9Sstevel@tonic-gate  * static void
285*7c478bd9Sstevel@tonic-gate  * clear_bit(seq_array_t sa, unsigned int bit)
286*7c478bd9Sstevel@tonic-gate  * {
287*7c478bd9Sstevel@tonic-gate  *	if (bit < NBITS)
288*7c478bd9Sstevel@tonic-gate  *		sa->arr[bit/WBITS] &= ~((seq_word_t)1 << (bit % WBITS));
289*7c478bd9Sstevel@tonic-gate  * }
290*7c478bd9Sstevel@tonic-gate  */
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate /*
293*7c478bd9Sstevel@tonic-gate  * Sift the bits in a sequence array by n
294*7c478bd9Sstevel@tonic-gate  *
295*7c478bd9Sstevel@tonic-gate  * The seqeunece arrays are logically arranged least significant bit to
296*7c478bd9Sstevel@tonic-gate  * most significant bit, where the LSB represents that last sequence
297*7c478bd9Sstevel@tonic-gate  * number seen. Thus this routine shifts the entire array to the left by
298*7c478bd9Sstevel@tonic-gate  * n.
299*7c478bd9Sstevel@tonic-gate  *
300*7c478bd9Sstevel@tonic-gate  *  0                                                             NBITS-1
301*7c478bd9Sstevel@tonic-gate  * +---------------------------------------------------------------+
302*7c478bd9Sstevel@tonic-gate  * |                                                               |
303*7c478bd9Sstevel@tonic-gate  * +---------------------------------------------------------------+
304*7c478bd9Sstevel@tonic-gate  *  ^
305*7c478bd9Sstevel@tonic-gate  *  This bit corresponds to the last sequence number seen sa->seqno.
306*7c478bd9Sstevel@tonic-gate  */
307*7c478bd9Sstevel@tonic-gate static void
shift_bits(seq_array_t sa,unsigned int n)308*7c478bd9Sstevel@tonic-gate shift_bits(seq_array_t sa, unsigned int n)
309*7c478bd9Sstevel@tonic-gate {
310*7c478bd9Sstevel@tonic-gate 	int i, m;
311*7c478bd9Sstevel@tonic-gate 	seq_word_t in = 0, out;
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate 	/* How many words to shift */
314*7c478bd9Sstevel@tonic-gate 	m = n / WBITS;
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 	/* Do we need to shift by words */
317*7c478bd9Sstevel@tonic-gate 	if (m) {
318*7c478bd9Sstevel@tonic-gate 		for (i = SSIZE - 1; i >= m; i--)
319*7c478bd9Sstevel@tonic-gate 			sa->arr[i] = sa->arr[i - m];
320*7c478bd9Sstevel@tonic-gate 		for (; i >= 0; i--)
321*7c478bd9Sstevel@tonic-gate 			sa->arr[i] = (seq_word_t)0;
322*7c478bd9Sstevel@tonic-gate 	}
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 	if (m >= SSIZE)
325*7c478bd9Sstevel@tonic-gate 		return;
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 	/* The bits we need to shift */
328*7c478bd9Sstevel@tonic-gate 	n %= WBITS;
329*7c478bd9Sstevel@tonic-gate 	if (n == 0)
330*7c478bd9Sstevel@tonic-gate 		return;
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate 	for (i = m; i < SSIZE; i++) {
334*7c478bd9Sstevel@tonic-gate 		/* The out going bits */
335*7c478bd9Sstevel@tonic-gate 		out = (sa->arr[i] >> (WBITS - n));
336*7c478bd9Sstevel@tonic-gate 		/*
337*7c478bd9Sstevel@tonic-gate 		 * shift this part of the bit array and "add in"
338*7c478bd9Sstevel@tonic-gate 		 * the most significant bits shifted out of the previous
339*7c478bd9Sstevel@tonic-gate 		 * previous word.
340*7c478bd9Sstevel@tonic-gate 		 */
341*7c478bd9Sstevel@tonic-gate 		sa->arr[i] = (sa->arr[i] << n) |  in;
342*7c478bd9Sstevel@tonic-gate 		/* The output of this word is the input to the next */
343*7c478bd9Sstevel@tonic-gate 		in = out;
344*7c478bd9Sstevel@tonic-gate 	}
345*7c478bd9Sstevel@tonic-gate }
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate /*
349*7c478bd9Sstevel@tonic-gate  * See if the given sequence number is out of sequence or is a replay
350*7c478bd9Sstevel@tonic-gate  * on the given context. If the context is not interested in either
351*7c478bd9Sstevel@tonic-gate  * just return GSS_S_COMPLETE
352*7c478bd9Sstevel@tonic-gate  */
353*7c478bd9Sstevel@tonic-gate OM_uint32
__dh_seq_detection(dh_gss_context_t ctx,OM_uint32 seqno)354*7c478bd9Sstevel@tonic-gate __dh_seq_detection(dh_gss_context_t ctx, OM_uint32 seqno)
355*7c478bd9Sstevel@tonic-gate {
356*7c478bd9Sstevel@tonic-gate 	OM_uint32 n;
357*7c478bd9Sstevel@tonic-gate 	OM_uint32 stat = GSS_S_COMPLETE;
358*7c478bd9Sstevel@tonic-gate 	OM_uint32 minor;
359*7c478bd9Sstevel@tonic-gate 
360*7c478bd9Sstevel@tonic-gate 	/*
361*7c478bd9Sstevel@tonic-gate 	 * See if there is anything to do. If not return with no bits set.
362*7c478bd9Sstevel@tonic-gate 	 */
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate 	if (((ctx->flags & GSS_C_REPLAY_FLAG) == 0) &&
365*7c478bd9Sstevel@tonic-gate 	    ((ctx->flags & GSS_C_SEQUENCE_FLAG) == 0))
366*7c478bd9Sstevel@tonic-gate 		return (stat);
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate 	/* lock the history why we check */
369*7c478bd9Sstevel@tonic-gate 	mutex_lock(&ctx->hist.seq_arr_lock);
370*7c478bd9Sstevel@tonic-gate 
371*7c478bd9Sstevel@tonic-gate 	/* If debugging print out the current history */
372*7c478bd9Sstevel@tonic-gate 	if (ctx->debug)
373*7c478bd9Sstevel@tonic-gate 		__context_debug_print_seq_hist(&minor, (gss_ctx_id_t)ctx, 0);
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate 	n = seqno - ctx->hist.seqno;
376*7c478bd9Sstevel@tonic-gate 	/* See if n is zero or that the high order bit is set or n = 0 */
377*7c478bd9Sstevel@tonic-gate 	if ((n & ~((~((OM_uint32)0)) >> 1)) || n == 0) {
378*7c478bd9Sstevel@tonic-gate 		/* sequence number is in the past */
379*7c478bd9Sstevel@tonic-gate 
380*7c478bd9Sstevel@tonic-gate 		/*
381*7c478bd9Sstevel@tonic-gate 		 * We want the small piece of the pie, so take the
382*7c478bd9Sstevel@tonic-gate 		 * 2s complement (-n).
383*7c478bd9Sstevel@tonic-gate 		 */
384*7c478bd9Sstevel@tonic-gate 		n =  ~n + 1;
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate 		/* the sequence number is ancient history */
387*7c478bd9Sstevel@tonic-gate 		if (n > NBITS - 1)
388*7c478bd9Sstevel@tonic-gate 			stat = GSS_S_OLD_TOKEN;
389*7c478bd9Sstevel@tonic-gate 		/* See if it has been seen before */
390*7c478bd9Sstevel@tonic-gate 		else if (check_bit(&ctx->hist, n))
391*7c478bd9Sstevel@tonic-gate 			stat = GSS_S_DUPLICATE_TOKEN;
392*7c478bd9Sstevel@tonic-gate 		else {
393*7c478bd9Sstevel@tonic-gate 			/* Otherwise we've seen it now, so recored the fact */
394*7c478bd9Sstevel@tonic-gate 			set_bit(&ctx->hist, n);
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate 			/* If we care, report that we're out of sequence */
397*7c478bd9Sstevel@tonic-gate 			if (ctx->flags & GSS_C_SEQUENCE_FLAG)
398*7c478bd9Sstevel@tonic-gate 				stat = GSS_S_UNSEQ_TOKEN;
399*7c478bd9Sstevel@tonic-gate 		}
400*7c478bd9Sstevel@tonic-gate 	} else {
401*7c478bd9Sstevel@tonic-gate 		/* sequence number is in the future so shift */
402*7c478bd9Sstevel@tonic-gate 		shift_bits(&ctx->hist, n);
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate 		/* The sequence number is the most recent now */
405*7c478bd9Sstevel@tonic-gate 		ctx->hist.seqno = seqno;
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate 		/* So set the most recent bit */
408*7c478bd9Sstevel@tonic-gate 		set_bit(&ctx->hist, 0);
409*7c478bd9Sstevel@tonic-gate 
410*7c478bd9Sstevel@tonic-gate 		/* if n > 1 and we care report a gap in the sequence */
411*7c478bd9Sstevel@tonic-gate 		if (n > 1 && (ctx->flags & GSS_C_SEQUENCE_FLAG))
412*7c478bd9Sstevel@tonic-gate 			stat = GSS_S_GAP_TOKEN;
413*7c478bd9Sstevel@tonic-gate 	}
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 	/* If we're debugging print out the new state */
416*7c478bd9Sstevel@tonic-gate 	if (ctx->debug)
417*7c478bd9Sstevel@tonic-gate 		__context_debug_print_seq_hist(&minor, (gss_ctx_id_t)ctx, 0);
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate 	/* Let other threads in */
420*7c478bd9Sstevel@tonic-gate 	mutex_unlock(&ctx->hist.seq_arr_lock);
421*7c478bd9Sstevel@tonic-gate 
422*7c478bd9Sstevel@tonic-gate 	/* return the status */
423*7c478bd9Sstevel@tonic-gate 	return (stat);
424*7c478bd9Sstevel@tonic-gate }
425*7c478bd9Sstevel@tonic-gate 
426*7c478bd9Sstevel@tonic-gate /*
427*7c478bd9Sstevel@tonic-gate  * Set the next sequence number to use on this context.
428*7c478bd9Sstevel@tonic-gate  * Return that sequence number.
429*7c478bd9Sstevel@tonic-gate  */
430*7c478bd9Sstevel@tonic-gate OM_uint32
__dh_next_seqno(dh_gss_context_t ctx)431*7c478bd9Sstevel@tonic-gate __dh_next_seqno(dh_gss_context_t ctx)
432*7c478bd9Sstevel@tonic-gate {
433*7c478bd9Sstevel@tonic-gate 	OM_uint32 t;
434*7c478bd9Sstevel@tonic-gate 
435*7c478bd9Sstevel@tonic-gate 	mutex_lock(&ctx->seqno_lock);
436*7c478bd9Sstevel@tonic-gate 	t = ctx->next_seqno++;
437*7c478bd9Sstevel@tonic-gate 	mutex_unlock(&ctx->seqno_lock);
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 	return (t);
440*7c478bd9Sstevel@tonic-gate }
441*7c478bd9Sstevel@tonic-gate 
442*7c478bd9Sstevel@tonic-gate 
443*7c478bd9Sstevel@tonic-gate /*
444*7c478bd9Sstevel@tonic-gate  * Initialize sequence history on a new context
445*7c478bd9Sstevel@tonic-gate  */
446*7c478bd9Sstevel@tonic-gate void
__dh_init_seq_hist(dh_gss_context_t ctx)447*7c478bd9Sstevel@tonic-gate __dh_init_seq_hist(dh_gss_context_t ctx)
448*7c478bd9Sstevel@tonic-gate {
449*7c478bd9Sstevel@tonic-gate 	mutex_init(&ctx->seqno_lock, USYNC_THREAD, 0);
450*7c478bd9Sstevel@tonic-gate 	ctx->next_seqno = 1;
451*7c478bd9Sstevel@tonic-gate 	mutex_init(&ctx->hist.seq_arr_lock, USYNC_THREAD, 0);
452*7c478bd9Sstevel@tonic-gate 	ctx->hist.seqno = 0;
453*7c478bd9Sstevel@tonic-gate 	clear_all_bits(&ctx->hist);
454*7c478bd9Sstevel@tonic-gate }
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate /*
457*7c478bd9Sstevel@tonic-gate  * Destroy sequence history on a context.
458*7c478bd9Sstevel@tonic-gate  */
459*7c478bd9Sstevel@tonic-gate void
__dh_destroy_seq_hist(dh_gss_context_t ctx)460*7c478bd9Sstevel@tonic-gate __dh_destroy_seq_hist(dh_gss_context_t ctx)
461*7c478bd9Sstevel@tonic-gate {
462*7c478bd9Sstevel@tonic-gate 	if (ctx) {
463*7c478bd9Sstevel@tonic-gate 		mutex_destroy(&ctx->seqno_lock);
464*7c478bd9Sstevel@tonic-gate 		mutex_destroy(&ctx->hist.seq_arr_lock);
465*7c478bd9Sstevel@tonic-gate 	}
466*7c478bd9Sstevel@tonic-gate }
467