3 * The Regents of the University of Michigan
6 * Permission is granted to use, copy, create derivative works
7 * and redistribute this software and such derivative works
8 * for any purpose, so long as the name of The University of
9 * Michigan is not used in any advertising or publicity
10 * pertaining to the use of distribution of this software
11 * without specific, written prior authorization. If the
12 * above copyright notice or any other identification of the
13 * University of Michigan is included in any copy of any
14 * portion of this software, then the disclaimer below must
17 * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION
18 * FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY
19 * PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY OF
20 * MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
21 * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
23 * REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE
24 * FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
25 * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
26 * OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
27 * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
33 #ifdef HAVE_LUCID_CONTEXT_SUPPORT
36 * Newer versions of MIT and Heimdal have lucid context support.
37 * We can use common code if it is supported.
46 #include <gssapi/gssapi.h>
48 typedef uint64_t OM_uint64;
50 #include <gssapi/gssapi_krb5.h>
53 # include "lgss_utils.h"
55 # include "gss_util.h"
56 # include "gss_oids.h"
57 # include "err_util.h"
59 #include "write_bytes.h"
62 extern OM_uint32 gss_export_lucid_sec_context(OM_uint32 *min_stat,
66 extern OM_uint32 gss_free_lucid_sec_context(OM_uint32 *min_stat,
71 write_lucid_keyblock(char **p, char *end, gss_krb5_lucid_key_t *key)
75 if (WRITE_BYTES(p, end, key->type)) return -1;
76 tmp.length = key->length;
77 tmp.value = key->data;
78 if (write_buffer(p, end, &tmp)) return -1;
83 prepare_krb5_rfc1964_buffer(gss_krb5_lucid_context_v1_t *lctx,
87 static int constant_zero = 0;
88 unsigned char fakeseed[16] = { 0 };
89 uint32_t word_send_seq;
90 gss_krb5_lucid_key_t enc_key;
93 gss_buffer_desc fakeoid;
96 * The new Kerberos interface to get the gss context
97 * does not include the seed or seed_init fields
98 * because we never really use them. But for now,
99 * send down a fake buffer so we can use the same
100 * interface to the kernel.
102 memset(&enc_key, 0, sizeof(enc_key));
103 memset(&fakeoid, 0, sizeof(fakeoid));
105 if (!(buf->value = calloc(1, MAX_CTX_LEN)))
108 end = buf->value + MAX_CTX_LEN;
110 if (WRITE_BYTES(&p, end, lctx->initiate)) goto out_err;
112 /* seed_init and seed not used by kernel anyway */
113 if (WRITE_BYTES(&p, end, constant_zero)) goto out_err;
114 if (write_bytes(&p, end, &fakeseed, 16)) goto out_err;
116 if (WRITE_BYTES(&p, end, lctx->rfc1964_kd.sign_alg)) goto out_err;
117 if (WRITE_BYTES(&p, end, lctx->rfc1964_kd.seal_alg)) goto out_err;
118 if (WRITE_BYTES(&p, end, lctx->endtime)) goto out_err;
119 word_send_seq = lctx->send_seq; /* XXX send_seq is 64-bit */
120 if (WRITE_BYTES(&p, end, word_send_seq)) goto out_err;
121 if (write_oid(&p, end, &krb5oid)) goto out_err;
125 * The kernel gss code expects des-cbc-raw for all flavors of des.
126 * The keytype from MIT has this type, but Heimdal does not.
127 * Force the Heimdal keytype to 4 (des-cbc-raw).
128 * Note that the rfc1964 version only supports DES enctypes.
130 if (lctx->rfc1964_kd.ctx_key.type != 4) {
131 printerr(2, "%s: overriding heimdal keytype (%d => %d)\n",
132 __FUNCTION__, lctx->rfc1964_kd.ctx_key.type, 4);
133 lctx->rfc1964_kd.ctx_key.type = 4;
136 printerr(2, "%s: serializing keys with enctype %d and length %d\n",
137 __FUNCTION__, lctx->rfc1964_kd.ctx_key.type,
138 lctx->rfc1964_kd.ctx_key.length);
140 /* derive the encryption key and copy it into buffer */
141 enc_key.type = lctx->rfc1964_kd.ctx_key.type;
142 enc_key.length = lctx->rfc1964_kd.ctx_key.length;
143 if ((enc_key.data = calloc(1, enc_key.length)) == NULL)
145 skd = (char *) lctx->rfc1964_kd.ctx_key.data;
146 dkd = (char *) enc_key.data;
147 for (i = 0; i < enc_key.length; i++)
148 dkd[i] = skd[i] ^ 0xf0;
149 if (write_lucid_keyblock(&p, end, &enc_key)) {
155 if (write_lucid_keyblock(&p, end, &lctx->rfc1964_kd.ctx_key))
158 buf->length = p - (char *)buf->value;
161 printerr(0, "ERROR: failed serializing krb5 context for kernel\n");
162 if (buf->value) free(buf->value);
164 if (enc_key.data) free(enc_key.data);
168 /* XXX Hack alert! XXX Do NOT submit upstream! XXX */
169 /* XXX Hack alert! XXX Do NOT submit upstream! XXX */
172 #define KG_USAGE_SEAL 22
173 #define KG_USAGE_SIGN 23
174 #define KG_USAGE_SEQ 24
177 #define KG_USAGE_ACCEPTOR_SEAL 22
178 #define KG_USAGE_ACCEPTOR_SIGN 23
179 #define KG_USAGE_INITIATOR_SEAL 24
180 #define KG_USAGE_INITIATOR_SIGN 25
182 /* Lifted from mit src/lib/gssapi/krb5/gssapiP_krb5.h */
184 SEAL_ALG_NONE = 0xffff,
185 SEAL_ALG_DES = 0x0000,
186 SEAL_ALG_1 = 0x0001, /* not published */
187 SEAL_ALG_MICROSOFT_RC4 = 0x0010, /* microsoft w2k; */
188 SEAL_ALG_DES3KD = 0x0002
191 #define KEY_USAGE_SEED_ENCRYPTION 0xAA
192 #define KEY_USAGE_SEED_INTEGRITY 0x55
193 #define KEY_USAGE_SEED_CHECKSUM 0x99
196 /* Flags for version 2 context flags */
197 #define KRB5_CTX_FLAG_INITIATOR 0x00000001
198 #define KRB5_CTX_FLAG_CFX 0x00000002
199 #define KRB5_CTX_FLAG_ACCEPTOR_SUBKEY 0x00000004
201 /* XXX Hack alert! XXX Do NOT submit upstream! XXX */
202 /* XXX Hack alert! XXX Do NOT submit upstream! XXX */
204 * We don't have "legal" access to these MIT-only
205 * structures located in libk5crypto
207 extern void *krb5int_enc_arcfour;
208 extern void *krb5int_enc_des3;
209 extern void *krb5int_enc_aes128;
210 extern void *krb5int_enc_aes256;
213 key_lucid_to_krb5(const gss_krb5_lucid_key_t *lin, krb5_keyblock *kout)
215 memset(kout, '\0', sizeof(kout));
217 kout->enctype = lin->type;
218 kout->length = lin->length;
219 kout->contents = lin->data;
221 kout->keytype = lin->type;
222 kout->keyvalue.length = lin->length;
223 kout->keyvalue.data = lin->data;
228 key_krb5_to_lucid(const krb5_keyblock *kin, gss_krb5_lucid_key_t *lout)
230 memset(lout, '\0', sizeof(lout));
232 lout->type = kin->enctype;
233 lout->length = kin->length;
234 lout->data = kin->contents;
236 lout->type = kin->keytype;
237 lout->length = kin->keyvalue.length;
238 memcpy(lout->data, kin->keyvalue.data, kin->keyvalue.length);
242 /* XXX Hack alert! XXX Do NOT submit upstream! XXX */
243 /* XXX Hack alert! XXX Do NOT submit upstream! XXX */
244 /* XXX Hack alert! XXX Do NOT submit upstream! XXX */
245 /* XXX Hack alert! XXX Do NOT submit upstream! XXX */
247 * Function to derive a new key from a given key and given constant data.
249 static krb5_error_code
250 derive_key_lucid(const gss_krb5_lucid_key_t *in, gss_krb5_lucid_key_t *out,
251 int usage, char extra)
253 krb5_error_code code;
254 unsigned char constant_data[K5CLENGTH];
258 krb5_keyblock kin, kout; /* must send krb5_keyblock, not lucid! */
259 #if defined(HAVE_HEIMDAL) || HAVE_KRB5INT_DERIVE_KEY
260 krb5_context kcontext;
262 #if HAVE_KRB5INT_DERIVE_KEY
263 krb5_key key_in, key_out;
266 krb5_keyblock *outkey;
270 * XXX Hack alert. We don't have "legal" access to these
271 * values and structures located in libk5crypto
274 case ENCTYPE_DES3_CBC_SHA1:
276 case ENCTYPE_DES3_CBC_RAW:
280 enc = &krb5int_enc_des3;
283 case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
286 enc = &krb5int_enc_aes128;
289 case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
292 enc = &krb5int_enc_aes256;
296 code = KRB5_BAD_ENCTYPE;
300 /* allocate memory for output key */
301 if ((out->data = malloc(keylength)) == NULL) {
305 out->length = keylength;
306 out->type = in->type;
308 /* Convert to correct format for call to krb5_derive_key */
309 key_lucid_to_krb5(in, &kin);
310 key_lucid_to_krb5(out, &kout);
312 datain.data = (char *) constant_data;
313 datain.length = K5CLENGTH;
315 ((char *)(datain.data))[0] = (usage>>24)&0xff;
316 ((char *)(datain.data))[1] = (usage>>16)&0xff;
317 ((char *)(datain.data))[2] = (usage>>8)&0xff;
318 ((char *)(datain.data))[3] = usage&0xff;
320 ((char *)(datain.data))[4] = (char) extra;
323 #if HAVE_KRB5INT_DERIVE_KEY
324 code = krb5_init_context(&kcontext);
330 code = krb5_k_create_key(kcontext, &kin, &key_in);
336 code = krb5_k_create_key(kcontext, &kout, &key_out);
342 code = krb5int_derive_key(enc, key_in, &key_out, &datain,
344 #else /* !HAVE_KRB5INT_DERIVE_KEY */
345 code = krb5_derive_key(enc, &kin, &kout, &datain);
346 #endif /* HAVE_KRB5INT_DERIVE_KEY */
347 #else /* !defined(HAVE_KRB5) */
348 if ((code = krb5_init_context(&kcontext))) {
350 code = krb5_derive_key(kcontext, &kin, in->type, constant_data, K5CLENGTH, &outkey);
351 #endif /* defined(HAVE_KRB5) */
358 key_krb5_to_lucid(&kout, out);
359 #if HAVE_KRB5INT_DERIVE_KEY
360 krb5_free_context(kcontext);
361 #endif /* HAVE_KRB5INT_DERIVE_KEY */
362 #else /* !defined(HAVE_KRB5) */
363 key_krb5_to_lucid(outkey, out);
364 krb5_free_keyblock(kcontext, outkey);
365 krb5_free_context(kcontext);
366 #endif /* defined(HAVE_KRB5) */
370 printerr(0, "ERROR: %s: returning error %d (%s)\n",
371 __FUNCTION__, code, error_message(code));
377 * Prepare a new-style buffer, as defined in rfc4121 (a.k.a. cfx),
378 * to send to the kernel for newer encryption types -- or for DES3.
382 * u32 initiate; ( whether we are the initiator or not )
385 * #define KRB5_CTX_FLAG_INITIATOR 0x00000001
386 * #define KRB5_CTX_FLAG_CFX 0x00000002
387 * #define KRB5_CTX_FLAG_ACCEPTOR_SUBKEY 0x00000004
389 * u32 enctype; ( encrption type of keys )
390 * u32 size_of_each_key; ( size of each key in bytes )
391 * u32 number_of_keys; ( N -- should always be 3 for now )
398 prepare_krb5_rfc4121_buffer(gss_krb5_lucid_context_v1_t *lctx,
399 gss_buffer_desc *buf)
401 static int constant_two = 2;
403 uint32_t v2_flags = 0;
404 gss_krb5_lucid_key_t enc_key;
405 gss_krb5_lucid_key_t derived_key;
406 gss_buffer_desc fakeoid;
411 memset(&enc_key, 0, sizeof(enc_key));
412 memset(&fakeoid, 0, sizeof(fakeoid));
414 if (!(buf->value = calloc(1, MAX_CTX_LEN)))
417 end = buf->value + MAX_CTX_LEN;
420 if (WRITE_BYTES(&p, end, constant_two)) goto out_err;
421 if (WRITE_BYTES(&p, end, lctx->endtime)) goto out_err;
424 v2_flags |= KRB5_CTX_FLAG_INITIATOR;
425 if (lctx->protocol != 0)
426 v2_flags |= KRB5_CTX_FLAG_CFX;
427 if (lctx->protocol != 0 && lctx->cfx_kd.have_acceptor_subkey == 1)
428 v2_flags |= KRB5_CTX_FLAG_ACCEPTOR_SUBKEY;
430 if (WRITE_BYTES(&p, end, v2_flags)) goto out_err;
432 if (WRITE_BYTES(&p, end, lctx->send_seq)) goto out_err;
434 /* Protocol 0 here implies DES3 or RC4 */
435 printerr(3, "protocol %d\n", lctx->protocol);
436 if (lctx->protocol == 0) {
437 enctype = lctx->rfc1964_kd.ctx_key.type;
440 * The kernel gss code expects ENCTYPE_DES3_CBC_RAW (6) for
441 * 3des keys, but Heimdal key has ENCTYPE_DES3_CBC_SHA1 (16).
442 * Force the Heimdal enctype to 6.
444 if (enctype == ENCTYPE_DES3_CBC_SHA1) {
445 printerr(2, "%s: overriding heimdal keytype (%d => %d)\n",
446 __FUNCTION__, enctype, 6);
451 keysize = lctx->rfc1964_kd.ctx_key.length;
452 numkeys = 3; /* XXX is always gonna be three? */
454 if (lctx->cfx_kd.have_acceptor_subkey) {
455 enctype = lctx->cfx_kd.acceptor_subkey.type;
456 keysize = lctx->cfx_kd.acceptor_subkey.length;
458 enctype = lctx->cfx_kd.ctx_key.type;
459 keysize = lctx->cfx_kd.ctx_key.length;
463 printerr(3, "serializing %d keys with enctype %d and size %d\n",
464 numkeys, enctype, keysize);
465 if (WRITE_BYTES(&p, end, enctype)) goto out_err;
466 if (WRITE_BYTES(&p, end, keysize)) goto out_err;
467 if (WRITE_BYTES(&p, end, numkeys)) goto out_err;
469 if (lctx->protocol == 0) {
470 /* derive and send down: Ke, Ki, and Kc */
472 if (write_bytes(&p, end, lctx->rfc1964_kd.ctx_key.data,
473 lctx->rfc1964_kd.ctx_key.length))
477 if (write_bytes(&p, end, lctx->rfc1964_kd.ctx_key.data,
478 lctx->rfc1964_kd.ctx_key.length))
483 * RC4 is special, it dosen't need key derivation. Actually
484 * the Ke is based on plain text. Here we just let all three
485 * key identical, kernel will handle everything. --ericm
487 if (lctx->rfc1964_kd.ctx_key.type == ENCTYPE_ARCFOUR_HMAC) {
488 if (write_bytes(&p, end, lctx->rfc1964_kd.ctx_key.data,
489 lctx->rfc1964_kd.ctx_key.length))
492 if (derive_key_lucid(&lctx->rfc1964_kd.ctx_key,
494 KG_USAGE_SIGN, KEY_USAGE_SEED_CHECKSUM))
496 if (write_bytes(&p, end, derived_key.data,
499 free(derived_key.data);
502 gss_krb5_lucid_key_t *keyptr;
503 uint32_t sign_usage, seal_usage;
505 if (lctx->cfx_kd.have_acceptor_subkey)
506 keyptr = &lctx->cfx_kd.acceptor_subkey;
508 keyptr = &lctx->cfx_kd.ctx_key;
511 if (lctx->initiate == 1) {
512 sign_usage = KG_USAGE_INITIATOR_SIGN;
513 seal_usage = KG_USAGE_INITIATOR_SEAL;
515 sign_usage = KG_USAGE_ACCEPTOR_SIGN;
516 seal_usage = KG_USAGE_ACCEPTOR_SEAL;
520 * These are from rfc4142, but I don't understand: if we supply
521 * different 'usage' value for client & server, then the peers
522 * will have different derived keys. How could this work?
524 * Here we simply use old SIGN/SEAL values until we find the
528 sign_usage = KG_USAGE_SIGN;
529 seal_usage = KG_USAGE_SEAL;
532 /* derive and send down: Ke, Ki, and Kc */
535 if (derive_key_lucid(keyptr, &derived_key,
536 seal_usage, KEY_USAGE_SEED_ENCRYPTION))
538 if (write_bytes(&p, end, derived_key.data,
541 free(derived_key.data);
544 if (derive_key_lucid(keyptr, &derived_key,
545 seal_usage, KEY_USAGE_SEED_INTEGRITY))
547 if (write_bytes(&p, end, derived_key.data,
550 free(derived_key.data);
553 if (derive_key_lucid(keyptr, &derived_key,
554 sign_usage, KEY_USAGE_SEED_CHECKSUM))
556 if (write_bytes(&p, end, derived_key.data,
559 free(derived_key.data);
562 buf->length = p - (char *)buf->value;
566 printerr(0, "ERROR: %s: failed serializing krb5 context for kernel\n",
580 serialize_krb5_ctx(gss_ctx_id_t ctx, gss_buffer_desc *buf)
582 OM_uint32 maj_stat, min_stat;
583 void *return_ctx = 0;
585 gss_krb5_lucid_context_v1_t *lctx = 0;
588 printerr(3, "lucid version!\n");
589 maj_stat = gss_export_lucid_sec_context(&min_stat, &ctx,
591 if (maj_stat != GSS_S_COMPLETE) {
592 pgsserr("gss_export_lucid_sec_context",
593 maj_stat, min_stat, &krb5oid);
597 /* Check the version returned, we only support v1 right now */
598 vers = ((gss_krb5_lucid_context_version_t *)return_ctx)->version;
601 lctx = (gss_krb5_lucid_context_v1_t *) return_ctx;
604 printerr(0, "ERROR: unsupported lucid sec context version %d\n",
611 * Now lctx points to a lucid context that we can send down to kernel
613 * Note: we send down different information to the kernel depending
614 * on the protocol version and the enctyption type.
615 * For protocol version 0 with all enctypes besides DES3, we use
616 * the original format. For protocol version != 0 or DES3, we
617 * send down the new style information.
620 if (lctx->protocol == 0 && lctx->rfc1964_kd.ctx_key.type <= 4)
621 retcode = prepare_krb5_rfc1964_buffer(lctx, buf);
623 retcode = prepare_krb5_rfc4121_buffer(lctx, buf);
625 maj_stat = gss_free_lucid_sec_context(&min_stat, ctx, return_ctx);
626 if (maj_stat != GSS_S_COMPLETE) {
627 pgsserr("gss_export_lucid_sec_context",
628 maj_stat, min_stat, &krb5oid);
629 printerr(0, "WARN: failed to free lucid sec context\n");
633 printerr(1, "%s: prepare_krb5_*_buffer failed (retcode = %d)\n",
634 __FUNCTION__, retcode);
641 printerr(0, "ERROR: failed serializing krb5 context for kernel\n");
647 #endif /* HAVE_LUCID_CONTEXT_SUPPORT */