1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  *	dh_gssapi.h
24  *
25  *	Copyright (c) 1997, by Sun Microsystems, Inc.
26  *	All rights reserved.
27  *
28  */
29 
30 #ifndef _DH_GSSAPI_H_
31 #define	_DH_GSSAPI_H_
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <gssapi/gssapi.h>
41 #include <mechglueP.h>
42 #include <rpc/rpc.h>
43 #include <time.h>
44 #include <thread.h>
45 #include <synch.h>
46 #include "error.h"
47 #include "token.h"
48 #include "oid.h"
49 #include "crypto.h"
50 
51 #define	New(T, n) ((T *)calloc(n, sizeof (T)))
52 #define	Free(p) free(p)
53 
54 #define	DH_NO_SECRETKEY 1
55 #define	DH_NO_NETNAME 2
56 #define	DH_VALIDATE_FAILURE 3
57 
58 #define	DH_MECH_QOP 0
59 
60 /*
61  * This structure defines the necessary operations that a mechanism
62  * must provide for key management.
63  */
64 typedef struct keyopts_desc {
65 	/*
66 	 * This function pointer will encrypt the set of supplied session keys
67 	 * with this principal and a remote principal. For algorithm 0
68 	 * A common key is used, that is calculated using the classic
69 	 * Diffie-Hellman key exchange. An RSA style algorithm would encrypt
70 	 * the session key with the public key of the remote.
71 	 */
72 	int (*key_encryptsessions)(const char *remotename,
73 	    des_block deskeys[], int no_keys);
74 	/*
75 	 * This function decrypts the set of session keys from remote. It
76 	 * is the inverse of the above entry point. The last parameter
77 	 * is an in/out parameter. If it is non-zero going in, it allows
78 	 * the underlying mechanism to get the public key for the remote
79 	 * out of a cache. If it is zero, it indicates that the mechanism
80 	 * should get a definitive copy of the public key because it may
81 	 * have changed. When returning from the entry point *key_cached
82 	 * will be set to non zero if the session keys were decrypted using
83 	 * a cached public key, otherwise zero will be return. Most mechanism
84 	 * will not need/want this and will always return *key_cached as zero.
85 	 */
86 	int (*key_decryptsessions)(const char *remotename,
87 	    des_block deskeys[], int no_keys, int *key_cached);
88 	/*
89 	 * This entry point is used to generate a block of session keys
90 	 */
91 	int (*key_gendeskeys)(des_block *deskeys, int no_keys);
92 	/*
93 	 * This entry point is used to see if the principal's credentials
94 	 * are available.
95 	 */
96 	int (*key_secretkey_is_set)(void);
97 	/*
98 	 * This entry point will return the netname of the calling principal.
99 	 */
100 	char *(*get_principal)(void);
101 } dh_keyopts_desc, *dh_keyopts_t;
102 
103 /*
104  * Diffie-Hellman principal names are just null terminated charater strings
105  * that are ONC RPC netnames.
106  */
107 typedef char *dh_principal;
108 
109 /* Diffie-Hellman credentials */
110 typedef struct dh_cred_id_desc {
111 	uid_t uid;		/* The uid of this principal */
112 	gss_cred_usage_t usage; /* How this cred can be used */
113 	dh_principal  principal;    /* RPC netname */
114 	time_t expire;		/* When this cred expires */
115 } dh_cred_id_desc, *dh_cred_id_t;
116 
117 
118 /*
119  * This is the structure that defines the mechanism specific context.
120  * This allows a common backend to support a faimily of mechanism that
121  * use different key lengths and algorithms. We know the particular mechanism
122  * by that mechanism on initialization filling in the OID for that mechanaism
123  * and suppling a set of keyopts that correspond to the key length and
124  * algorithm used.
125  */
126 typedef struct dh_context_desc {
127 	gss_OID mech;
128 	dh_keyopts_t keyopts;
129 } dh_context_desc, *dh_context_t;
130 
131 
132 /* This defines the size of the history for replay and out-of-seq detection */
133 #define	SSIZE 4
134 typedef unsigned long long seq_word_t;
135 
136 /*
137  * This structure holds the state for replay and detection. It contains the
138  * bit array of the last seqence numbers that have been seen and the last
139  * sequence number. The 0th bit represents the last sequence number receive.
140  * The state contained in this structure in protected by a mutext so that
141  * multiple threads can manipulate the history.
142  */
143 typedef struct {
144 	mutex_t seq_arr_lock;   /* lock on this structure */
145 	seq_word_t arr[SSIZE];	/* Bit array of sequence history */
146 	OM_uint32 seqno;	/* Last seqno seen */
147 } seq_array, *seq_array_t;
148 
149 
150 typedef enum { INCOMPLETE, ESTABLISHED, BAD } DHState;
151 
152 /*
153  * The Diffie-Hellman context that corresponds to the gss_ctx_id_t.
154  */
155 typedef struct dh_gss_context_desc {
156 	DHState state;		/* Context state */
157 	int initiate;		/* 1 intiates, 0 accepts */
158 	int proto_version;	/* DH protocol version */
159 	dh_principal remote;	/* Netname of remote */
160 	dh_principal local;	/* Netname of local */
161 	int no_keys;		/* Number of session keys (currently 3) */
162 	des_block *keys;	/* The session keys */
163 	OM_uint32 flags;	/* GSS context flags */
164 	seq_array hist;		/* Out-of-sequence, replay history */
165 	mutex_t seqno_lock;	/* Lock to protect next_seqno */
166 	OM_uint32 next_seqno;	/* Next seqno to send */
167 	time_t expire;		/* When this context expires */
168 	int debug;		/* Turn on debuging if non zero */
169 } dh_gss_context_desc, *dh_gss_context_t;
170 
171 
172 /* declarations of internal name mechanism functions */
173 
174 gss_mechanism
175 __dh_generic_initialize(gss_mechanism, gss_OID_desc, dh_keyopts_t);
176 
177 /*
178  * The following routines are the entry points that libgss uses.
179  * The have the same signature as the corresponding libgss functions
180  * except they are passed an additinal first parameter that is a pointer
181  * to the mechanaism specific context. In our case that void pointer is
182  * actually pointing to a dh_context. See <gssapi/gssapi.h> or the
183  * draft-ietf_cat_gssv2-cbind document for an explanation of the parameters.
184  */
185 OM_uint32
186 __dh_gss_acquire_cred(void *, OM_uint32*, gss_name_t, OM_uint32, gss_OID_set,
187     gss_cred_usage_t, gss_cred_id_t *, gss_OID_set *, OM_uint32 *);
188 
189 OM_uint32
190 __dh_gss_release_cred(void *, OM_uint32 *, gss_cred_id_t *);
191 
192 OM_uint32
193 __dh_gss_init_sec_context(void *, OM_uint32 *, gss_cred_id_t, gss_ctx_id_t *,
194     gss_name_t, gss_OID, OM_uint32, OM_uint32, gss_channel_bindings_t,
195     gss_buffer_t, gss_OID *, gss_buffer_t, OM_uint32 *, OM_uint32 *);
196 
197 OM_uint32
198 __dh_gss_accept_sec_context(void *, OM_uint32 *, gss_ctx_id_t *, gss_cred_id_t,
199     gss_buffer_t, gss_channel_bindings_t, gss_name_t *, gss_OID *,
200     gss_buffer_t, OM_uint32 *, OM_uint32 *, gss_cred_id_t *);
201 
202 OM_uint32
203 __dh_gss_process_context_token(void *, OM_uint32 *,
204     gss_ctx_id_t, gss_buffer_t);
205 
206 OM_uint32
207 __dh_gss_delete_sec_context(void *, OM_uint32 *, gss_ctx_id_t *, gss_buffer_t);
208 
209 OM_uint32
210 __dh_gss_context_time(void *, OM_uint32 *, gss_ctx_id_t, OM_uint32 *);
211 
212 OM_uint32
213 __dh_gss_sign(void *, OM_uint32 *, gss_ctx_id_t,
214     int, gss_buffer_t, gss_buffer_t);
215 
216 OM_uint32
217 __dh_gss_verify(void *, OM_uint32 *, gss_ctx_id_t,
218     gss_buffer_t, gss_buffer_t, int *);
219 
220 OM_uint32
221 __dh_gss_seal(void *, OM_uint32 *, gss_ctx_id_t,
222     int, int, gss_buffer_t, int *, gss_buffer_t);
223 
224 OM_uint32
225 __dh_gss_unseal(void *, OM_uint32 *, gss_ctx_id_t,
226     gss_buffer_t, gss_buffer_t, int *, int *);
227 
228 OM_uint32
229 __dh_gss_display_status(void *, OM_uint32 *, OM_uint32,
230     int, gss_OID, OM_uint32 *, gss_buffer_t);
231 
232 OM_uint32
233 __dh_gss_indicate_mechs(void *, OM_uint32 *, gss_OID_set *);
234 
235 OM_uint32
236 __dh_gss_compare_name(void *, OM_uint32 *, gss_name_t, gss_name_t, int *);
237 
238 OM_uint32
239 __dh_gss_display_name(void *, OM_uint32 *,
240     gss_name_t, gss_buffer_t, gss_OID *);
241 
242 OM_uint32
243 __dh_gss_import_name(void *, OM_uint32 *, gss_buffer_t, gss_OID, gss_name_t *);
244 
245 OM_uint32
246 __dh_gss_release_name(void *, OM_uint32 *, gss_name_t *);
247 
248 OM_uint32
249 __dh_gss_inquire_cred(void *, OM_uint32 *, gss_cred_id_t, gss_name_t *,
250     OM_uint32 *, gss_cred_usage_t *, gss_OID_set *);
251 
252 OM_uint32
253 __dh_gss_inquire_context(void *, OM_uint32 *, gss_ctx_id_t, gss_name_t *,
254     gss_name_t *, OM_uint32 *, gss_OID *, OM_uint32 *, int *, int *);
255 
256 /* New V2 entry points */
257 OM_uint32
258 __dh_gss_get_mic(void *, OM_uint32 *, gss_ctx_id_t,
259     gss_qop_t, gss_buffer_t, gss_buffer_t);
260 
261 OM_uint32
262 __dh_gss_verify_mic(void *, OM_uint32 *, gss_ctx_id_t, gss_buffer_t,
263     gss_buffer_t, gss_qop_t *);
264 
265 OM_uint32
266 __dh_gss_wrap(void *, OM_uint32 *, gss_ctx_id_t, int, gss_qop_t,
267     gss_buffer_t, int *, gss_buffer_t);
268 
269 OM_uint32
270 __dh_gss_unwrap(void *, OM_uint32 *, gss_ctx_id_t, gss_buffer_t,
271     gss_buffer_t, int *, gss_qop_t *);
272 
273 OM_uint32
274 __dh_gss_wrap_size_limit(void *, OM_uint32 *, gss_ctx_id_t, int,
275     gss_qop_t, OM_uint32, OM_uint32 *);
276 
277 OM_uint32
278 __dh_gss_import_name_object(void *, OM_uint32 *,
279     void *, gss_OID, gss_name_t *);
280 
281 OM_uint32
282 __dh_gss_export_name_object(void *, OM_uint32 *, gss_name_t, gss_OID, void **);
283 
284 OM_uint32
285 __dh_gss_add_cred(void *, OM_uint32 *, gss_cred_id_t, gss_name_t, gss_OID,
286     gss_cred_usage_t, OM_uint32, OM_uint32, gss_cred_id_t *, gss_OID_set *,
287     OM_uint32 *, OM_uint32 *);
288 
289 OM_uint32
290 __dh_gss_inquire_cred_by_mech(void *, OM_uint32  *, gss_cred_id_t, gss_OID,
291     gss_name_t *, OM_uint32 *, OM_uint32 *, gss_cred_usage_t *);
292 
293 OM_uint32
294 __dh_gss_export_sec_context(void *, OM_uint32 *, gss_ctx_id_t *, gss_buffer_t);
295 
296 OM_uint32
297 __dh_gss_import_sec_context(void *, OM_uint32 *, gss_buffer_t, gss_ctx_id_t *);
298 
299 OM_uint32
300 __dh_gss_internal_release_oid(void *, OM_uint32 *, gss_OID *);
301 
302 OM_uint32
303 __dh_gss_inquire_names_for_mech(void *, OM_uint32 *, gss_OID, gss_OID_set *);
304 
305 /* Principal to uid mapping */
306 OM_uint32
307 __dh_pname_to_uid(void *ctx, OM_uint32 *minor,
308     const gss_name_t pname, uid_t *uid);
309 
310 OM_uint32
311 __dh_gss_export_name(void *ctx, OM_uint32 *minor,
312     const gss_name_t input_name, gss_buffer_t exported_name);
313 
314 /* ====================== End of libgss entry points ======================= */
315 
316 /* Routines to validate, install and remove contexts and credentials */
317 OM_uint32
318 __dh_validate_context(dh_gss_context_t);
319 
320 OM_uint32
321 __dh_install_context(dh_gss_context_t);
322 
323 OM_uint32
324 __dh_remove_context(dh_gss_context_t);
325 
326 OM_uint32
327 __dh_validate_cred(dh_cred_id_t);
328 
329 OM_uint32
330 __dh_install_cred(dh_cred_id_t);
331 
332 OM_uint32
333 __dh_remove_cred(dh_cred_id_t);
334 
335 OM_uint32
336 __dh_validate_principal(dh_principal);
337 
338 /* Routines for out-of-sequence and replay detection */
339 OM_uint32 __dh_seq_detection(dh_gss_context_t, OM_uint32);
340 
341 OM_uint32 __dh_next_seqno(dh_gss_context_t ctx);
342 
343 void __dh_init_seq_hist(dh_gss_context_t);
344 
345 void __dh_destroy_seq_hist(dh_gss_context_t ctx);
346 
347 #ifdef __cplusplus
348 }
349 #endif
350 
351 #endif /* _DH_GSSAPI_H_ */
352