2 Copyright (c) 2004-2006 The Regents of the University of Michigan.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. Neither the name of the University nor the names of its
15 contributors may be used to endorse or promote products derived
16 from this software without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #ifndef HAVE_LUCID_CONTEXT_SUPPORT
40 #include <gssapi/gssapi.h>
44 # include "lgss_utils.h"
46 # include "gss_util.h"
47 # include "gss_oids.h"
48 # include "err_util.h"
54 #if (KRB5_VERSION > 131)
55 /* XXX argggg, there's gotta be a better way than just duplicating this
56 * whole struct. Unfortunately, this is in a "private" header file,
57 * so this is our best choice at this point :-/
60 typedef struct _krb5_gss_ctx_id_rec {
61 unsigned int initiate : 1; /* nonzero if initiating, zero if accepting */
62 unsigned int established : 1;
63 unsigned int big_endian : 1;
64 unsigned int have_acceptor_subkey : 1;
65 unsigned int seed_init : 1; /* XXX tested but never actually set */
67 unsigned int testing_unknown_tokid : 1; /* for testing only */
70 unsigned char seed[16];
73 krb5_keyblock *subkey;
79 krb5_timestamp endtime;
81 /* XXX these used to be signed. the old spec is inspecific, and
82 the new spec specifies unsigned. I don't believe that the change
83 affects the wire encoding. */
84 uint64_t seq_send; /* gssint_uint64 */
85 uint64_t seq_recv; /* gssint_uint64 */
87 krb5_auth_context auth_context;
88 gss_OID_desc *mech_used; /* gss_OID_desc */
89 /* Protocol spec revision
90 0 => RFC 1964 with 3DES and RC4 enhancements
91 1 => draft-ietf-krb-wg-gssapi-cfx-01
92 No others defined so far. */
94 krb5_cksumtype cksumtype; /* for "main" subkey */
95 krb5_keyblock *acceptor_subkey; /* CFX only */
96 krb5_cksumtype acceptor_subkey_cksumtype;
98 gss_buffer_desc init_token;
100 } krb5_gss_ctx_id_rec, *krb5_gss_ctx_id_t;
102 #else /* KRB5_VERSION > 131 */
104 typedef struct _krb5_gss_ctx_id_rec {
108 unsigned char seed[16];
110 krb5_principal there;
111 krb5_keyblock *subkey;
117 krb5_timestamp endtime;
118 krb5_flags krb_flags;
124 krb5_auth_context auth_context;
125 gss_OID_desc *mech_used;
127 krb5_cksumtype *ctypes;
128 } krb5_gss_ctx_id_rec, *krb5_gss_ctx_id_t;
130 #endif /* KRB5_VERSION */
134 write_keyblock(char **p, char *end, struct _krb5_keyblock *arg)
138 if (WRITE_BYTES(p, end, arg->enctype)) return -1;
139 tmp.length = arg->length;
140 tmp.value = arg->contents;
141 if (write_buffer(p, end, &tmp)) return -1;
146 * XXX Hack alert! XXX Do NOT submit upstream!
147 * XXX Hack alert! XXX Do NOT submit upstream!
149 * We shouldn't be using these definitions
151 * XXX Hack alert! XXX Do NOT submit upstream!
152 * XXX Hack alert! XXX Do NOT submit upstream!
155 #define KG_USAGE_SEAL 22
156 #define KG_USAGE_SIGN 23
157 #define KG_USAGE_SEQ 24
160 #define KG_USAGE_ACCEPTOR_SEAL 22
161 #define KG_USAGE_ACCEPTOR_SIGN 23
162 #define KG_USAGE_INITIATOR_SEAL 24
163 #define KG_USAGE_INITIATOR_SIGN 25
165 /* Lifted from mit src/lib/gssapi/krb5/gssapiP_krb5.h */
167 SEAL_ALG_NONE = 0xffff,
168 SEAL_ALG_DES = 0x0000,
169 SEAL_ALG_1 = 0x0001, /* not published */
170 SEAL_ALG_MICROSOFT_RC4 = 0x0010, /* microsoft w2k; */
171 SEAL_ALG_DES3KD = 0x0002
174 #define KEY_USAGE_SEED_ENCRYPTION 0xAA
175 #define KEY_USAGE_SEED_INTEGRITY 0x55
176 #define KEY_USAGE_SEED_CHECKSUM 0x99
179 extern void krb5_enc_des3;
180 extern void krb5int_enc_des3;
181 extern void krb5int_enc_arcfour;
182 extern void krb5int_enc_aes128;
183 extern void krb5int_enc_aes256;
184 extern int krb5_derive_key();
187 * XXX Hack alert! XXX Do NOT submit upstream!
188 * XXX Hack alert! XXX Do NOT submit upstream!
190 * We should be passing down a single key to the kernel
191 * and it should be deriving the other keys. We cannot
192 * depend on any of this stuff being accessible in the
195 * XXX Hack alert! XXX Do NOT submit upstream!
196 * XXX Hack alert! XXX Do NOT submit upstream!
199 * Function to derive a new key from a given key and given constant data.
201 static krb5_error_code
202 derive_key(const krb5_keyblock *in, krb5_keyblock *out, int usage, char extra)
204 krb5_error_code code;
205 unsigned char constant_data[K5CLENGTH];
210 switch (in->enctype) {
211 #ifdef ENCTYPE_DES3_CBC_RAW
212 case ENCTYPE_DES3_CBC_RAW:
214 /* Extra hack, the structure was renamed as rc4 was added... */
215 #if defined(ENCTYPE_ARCFOUR_HMAC)
216 enc = &krb5int_enc_des3;
218 enc = &krb5_enc_des3;
222 #ifdef ENCTYPE_ARCFOUR_HMAC
223 case ENCTYPE_ARCFOUR_HMAC:
225 enc = &krb5int_enc_arcfour;
229 code = KRB5_BAD_ENCTYPE;
233 /* allocate memory for output key */
234 if ((out->contents = malloc(keylength)) == NULL) {
238 out->length = keylength;
239 out->enctype = in->enctype;
241 datain.data = (char *) constant_data;
242 datain.length = K5CLENGTH;
244 datain.data[0] = (usage>>24)&0xff;
245 datain.data[1] = (usage>>16)&0xff;
246 datain.data[2] = (usage>>8)&0xff;
247 datain.data[3] = usage&0xff;
249 datain.data[4] = (char) extra;
251 if ((code = krb5_derive_key(enc, in, out, &datain))) {
253 out->contents = NULL;
258 printerr(0, "ERROR: derive_key returning error %d (%s)\n",
259 code, error_message(code));
264 * We really shouldn't know about glue-layer context structure, but
265 * we need to get at the real krb5 context pointer. This should be
266 * removed as soon as we say there is no support for MIT Kerberos
267 * prior to 1.4 -- which gives us "legal" access to the context info.
269 typedef struct gss_union_ctx_id_t {
271 gss_ctx_id_t internal_ctx_id;
272 } gss_union_ctx_id_desc, *gss_union_ctx_id_t;
275 serialize_krb5_ctx(gss_ctx_id_t ctx, gss_buffer_desc *buf)
277 krb5_gss_ctx_id_t kctx = ((gss_union_ctx_id_t)ctx)->internal_ctx_id;
279 static int constant_zero = 0;
280 static int constant_one = 1;
281 static int constant_two = 2;
282 uint32_t word_seq_send;
283 u_int64_t seq_send_64bit;
284 uint32_t v2_flags = 0;
285 krb5_keyblock derived_key;
288 if (!(buf->value = calloc(1, MAX_CTX_LEN)))
291 end = buf->value + MAX_CTX_LEN;
293 switch (kctx->sealalg) {
295 /* Old format of context to the kernel */
296 if (kctx->initiate) {
297 if (WRITE_BYTES(&p, end, constant_one)) goto out_err;
300 if (WRITE_BYTES(&p, end, constant_zero)) goto out_err;
302 if (kctx->seed_init) {
303 if (WRITE_BYTES(&p, end, constant_one)) goto out_err;
306 if (WRITE_BYTES(&p, end, constant_zero)) goto out_err;
308 if (write_bytes(&p, end, &kctx->seed, sizeof(kctx->seed)))
310 if (WRITE_BYTES(&p, end, kctx->signalg)) goto out_err;
311 if (WRITE_BYTES(&p, end, kctx->sealalg)) goto out_err;
312 if (WRITE_BYTES(&p, end, kctx->endtime)) goto out_err;
313 word_seq_send = kctx->seq_send;
314 if (WRITE_BYTES(&p, end, word_seq_send)) goto out_err;
315 if (write_oid(&p, end, kctx->mech_used)) goto out_err;
317 printerr(2, "serialize_krb5_ctx: serializing keys with "
318 "enctype %d and length %d\n",
319 kctx->enc->enctype, kctx->enc->length);
321 if (write_keyblock(&p, end, kctx->enc)) goto out_err;
322 if (write_keyblock(&p, end, kctx->seq)) goto out_err;
324 case SEAL_ALG_MICROSOFT_RC4:
325 case SEAL_ALG_DES3KD:
326 /* New format of context to the kernel */
329 * #define KRB5_CTX_FLAG_INITIATOR 0x00000001
330 * #define KRB5_CTX_FLAG_CFX 0x00000002
331 * #define KRB5_CTX_FLAG_ACCEPTOR_SUBKEY 0x00000004
334 * u32 size_of_each_key; ( size in bytes )
335 * u32 number_of_keys; ( N (assumed to be 3 for now) )
336 * keydata-1; ( Ke (Kenc for DES3) )
337 * keydata-2; ( Ki (Kseq for DES3) )
338 * keydata-3; ( Kc (derived checksum key) )
340 if (WRITE_BYTES(&p, end, constant_two)) goto out_err;
341 if (WRITE_BYTES(&p, end, kctx->endtime)) goto out_err;
343 /* Only applicable flag for this is initiator */
344 if (kctx->initiate) v2_flags |= KRB5_CTX_FLAG_INITIATOR;
345 if (WRITE_BYTES(&p, end, v2_flags)) goto out_err;
347 seq_send_64bit = kctx->seq_send;
348 if (WRITE_BYTES(&p, end, seq_send_64bit)) goto out_err;
350 if (WRITE_BYTES(&p, end, kctx->enc->enctype)) goto out_err;
351 if (WRITE_BYTES(&p, end, kctx->enc->length)) goto out_err;
353 if (WRITE_BYTES(&p, end, numkeys)) goto out_err;
354 printerr(2, "serialize_krb5_ctx: serializing %d keys with "
355 "enctype %d and size %d\n",
356 numkeys, kctx->enc->enctype, kctx->enc->length);
359 if (write_bytes(&p, end, kctx->enc->contents,
364 if (write_bytes(&p, end, kctx->enc->contents,
369 if (derive_key(kctx->seq, &derived_key,
370 KG_USAGE_SIGN, KEY_USAGE_SEED_CHECKSUM))
372 if (write_bytes(&p, end, derived_key.contents,
375 free(derived_key.contents);
378 printerr(0, "ERROR: serialize_krb5_ctx: unsupported seal "
379 "algorithm %d\n", kctx->sealalg);
383 buf->length = p - (char *)buf->value;
387 printerr(0, "ERROR: failed serializing krb5 context for kernel\n");
396 #endif /* HAVE_KRB5 */
397 #endif /* HAVE_LUCID_CONTEXT_SUPPORT */