2 * COPYRIGHT (c) 2006 The Regents of the University of Michigan
5 * Copyright (c) 2014, Intel Corporation.
7 * Permission is granted to use, copy, create derivative works
8 * and redistribute this software and such derivative works
9 * for any purpose, so long as the name of The University of
10 * Michigan is not used in any advertising or publicity
11 * pertaining to the use of distribution of this software
12 * without specific, written prior authorization. If the
13 * above copyright notice or any other identification of the
14 * University of Michigan is included in any copy of any
15 * portion of this software, then the disclaimer below must
18 * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION
19 * FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY
20 * PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY OF
21 * MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
22 * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
24 * REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE
25 * FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
26 * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
27 * OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
28 * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
34 #ifdef HAVE_LUCID_CONTEXT_SUPPORT
37 * Newer versions of MIT and Heimdal have lucid context support.
38 * We can use common code if it is supported.
47 #include <gssapi/gssapi.h>
49 typedef uint64_t OM_uint64;
51 #include <gssapi/gssapi_krb5.h>
54 # include "lgss_utils.h"
56 # include "gss_util.h"
57 # include "gss_oids.h"
58 # include "err_util.h"
60 #include "write_bytes.h"
63 extern OM_uint32 gss_export_lucid_sec_context(OM_uint32 *min_stat,
67 extern OM_uint32 gss_free_lucid_sec_context(OM_uint32 *min_stat,
72 write_lucid_keyblock(char **p, char *end, gss_krb5_lucid_key_t *key)
76 if (WRITE_BYTES(p, end, key->type)) return -1;
77 tmp.length = key->length;
78 tmp.value = key->data;
79 if (write_buffer(p, end, &tmp)) return -1;
84 prepare_krb5_rfc1964_buffer(gss_krb5_lucid_context_v1_t *lctx,
88 static int constant_zero = 0;
89 unsigned char fakeseed[16] = { 0 };
90 uint32_t word_send_seq;
91 gss_krb5_lucid_key_t enc_key;
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));
104 if (!(buf->value = calloc(1, MAX_CTX_LEN)))
107 end = buf->value + MAX_CTX_LEN;
109 if (WRITE_BYTES(&p, end, lctx->initiate)) goto out_err;
111 /* seed_init and seed not used by kernel anyway */
112 if (WRITE_BYTES(&p, end, constant_zero)) goto out_err;
113 if (write_bytes(&p, end, &fakeseed, 16)) goto out_err;
115 if (WRITE_BYTES(&p, end, lctx->rfc1964_kd.sign_alg)) goto out_err;
116 if (WRITE_BYTES(&p, end, lctx->rfc1964_kd.seal_alg)) goto out_err;
117 if (WRITE_BYTES(&p, end, lctx->endtime)) goto out_err;
118 word_send_seq = lctx->send_seq; /* XXX send_seq is 64-bit */
119 if (WRITE_BYTES(&p, end, word_send_seq)) goto out_err;
120 if (write_oid(&p, end, &krb5oid)) goto out_err;
124 * The kernel gss code expects des-cbc-raw for all flavors of des.
125 * The keytype from MIT has this type, but Heimdal does not.
126 * Force the Heimdal keytype to 4 (des-cbc-raw).
127 * Note that the rfc1964 version only supports DES enctypes.
129 if (lctx->rfc1964_kd.ctx_key.type != 4) {
130 printerr(2, "%s: overriding heimdal keytype (%d => %d)\n",
131 __FUNCTION__, lctx->rfc1964_kd.ctx_key.type, 4);
132 lctx->rfc1964_kd.ctx_key.type = 4;
135 printerr(2, "%s: serializing keys with enctype %d and length %d\n",
136 __FUNCTION__, lctx->rfc1964_kd.ctx_key.type,
137 lctx->rfc1964_kd.ctx_key.length);
139 /* derive the encryption key and copy it into buffer */
140 enc_key.type = lctx->rfc1964_kd.ctx_key.type;
141 enc_key.length = lctx->rfc1964_kd.ctx_key.length;
142 if ((enc_key.data = calloc(1, enc_key.length)) == NULL)
144 skd = (char *) lctx->rfc1964_kd.ctx_key.data;
145 dkd = (char *) enc_key.data;
146 for (i = 0; i < enc_key.length; i++)
147 dkd[i] = skd[i] ^ 0xf0;
148 if (write_lucid_keyblock(&p, end, &enc_key))
150 if (write_lucid_keyblock(&p, end, &lctx->rfc1964_kd.ctx_key))
154 buf->length = p - (char *)buf->value;
157 printerr(0, "ERROR: failed serializing krb5 context for kernel\n");
163 if (enc_key.data) free(enc_key.data);
167 /* XXX Hack alert! XXX Do NOT submit upstream! XXX */
168 /* XXX Hack alert! XXX Do NOT submit upstream! XXX */
171 #define KG_USAGE_SEAL 22
172 #define KG_USAGE_SIGN 23
173 #define KG_USAGE_SEQ 24
176 #define KG_USAGE_ACCEPTOR_SEAL 22
177 #define KG_USAGE_ACCEPTOR_SIGN 23
178 #define KG_USAGE_INITIATOR_SEAL 24
179 #define KG_USAGE_INITIATOR_SIGN 25
181 /* Lifted from mit src/lib/gssapi/krb5/gssapiP_krb5.h */
183 SEAL_ALG_NONE = 0xffff,
184 SEAL_ALG_DES = 0x0000,
185 SEAL_ALG_1 = 0x0001, /* not published */
186 SEAL_ALG_MICROSOFT_RC4 = 0x0010, /* microsoft w2k; */
187 SEAL_ALG_DES3KD = 0x0002
190 #define KEY_USAGE_SEED_ENCRYPTION 0xAA
191 #define KEY_USAGE_SEED_INTEGRITY 0x55
192 #define KEY_USAGE_SEED_CHECKSUM 0x99
195 /* Flags for version 2 context flags */
196 #define KRB5_CTX_FLAG_INITIATOR 0x00000001
197 #define KRB5_CTX_FLAG_CFX 0x00000002
198 #define KRB5_CTX_FLAG_ACCEPTOR_SUBKEY 0x00000004
200 /* XXX Hack alert! XXX Do NOT submit upstream! XXX */
201 /* XXX Hack alert! XXX Do NOT submit upstream! XXX */
203 * We don't have "legal" access to these MIT-only
204 * structures located in libk5crypto
206 extern void *krb5int_enc_arcfour;
207 extern void *krb5int_enc_des3;
208 extern void *krb5int_enc_aes128;
209 extern void *krb5int_enc_aes256;
212 key_lucid_to_krb5(const gss_krb5_lucid_key_t *lin, krb5_keyblock *kout)
214 memset(kout, 0, sizeof(*kout));
216 kout->enctype = lin->type;
217 kout->length = lin->length;
218 kout->contents = lin->data;
220 kout->keytype = lin->type;
221 kout->keyvalue.length = lin->length;
222 kout->keyvalue.data = lin->data;
227 key_krb5_to_lucid(const krb5_keyblock *kin, gss_krb5_lucid_key_t *lout)
229 memset(lout, 0, sizeof(*lout));
232 lout->data = malloc(kin->length);
233 if (lout->data == NULL)
234 return KRB5_CC_NOMEM;
236 lout->type = kin->enctype;
237 lout->length = kin->length;
238 memcpy(lout->data, kin->contents, kin->length);
240 lout->data = malloc(kin->keyvalue.length);
241 if (lout->data == NULL)
242 return KRB5_CC_NOMEM;
244 lout->type = kin->keytype;
245 lout->length = kin->keyvalue.length;
246 memcpy(lout->data, kin->keyvalue.data, kin->keyvalue.length);
252 /* XXX Hack alert! XXX Do NOT submit upstream! XXX */
253 /* XXX Hack alert! XXX Do NOT submit upstream! XXX */
254 /* XXX Hack alert! XXX Do NOT submit upstream! XXX */
255 /* XXX Hack alert! XXX Do NOT submit upstream! XXX */
257 * Function to derive a new key from a given key and given constant data.
259 static krb5_error_code
260 derive_key_lucid(const gss_krb5_lucid_key_t *in, gss_krb5_lucid_key_t *out,
261 int usage, char extra)
263 krb5_error_code code;
264 unsigned char constant_data[K5CLENGTH];
270 krb5_keyblock kin; /* must send krb5_keyblock, not lucid! */
271 #if defined(HAVE_HEIMDAL) || HAVE_KRB5INT_DERIVE_KEY
272 krb5_context kcontext;
273 krb5_keyblock *outkey;
277 #if HAVE_KRB5INT_DERIVE_KEY
278 krb5_key key_in, key_out;
282 * XXX Hack alert. We don't have "legal" access to these
283 * values and structures located in libk5crypto
286 case ENCTYPE_DES3_CBC_SHA1:
288 case ENCTYPE_DES3_CBC_RAW:
292 enc = &krb5int_enc_des3;
295 case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
298 enc = &krb5int_enc_aes128;
301 case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
304 enc = &krb5int_enc_aes256;
308 code = KRB5_BAD_ENCTYPE;
312 /* Convert to correct format for call to krb5_derive_key */
313 key_lucid_to_krb5(in, &kin);
315 datain.data = (char *) constant_data;
316 datain.length = K5CLENGTH;
318 ((char *)(datain.data))[0] = (usage>>24)&0xff;
319 ((char *)(datain.data))[1] = (usage>>16)&0xff;
320 ((char *)(datain.data))[2] = (usage>>8)&0xff;
321 ((char *)(datain.data))[3] = usage&0xff;
323 ((char *)(datain.data))[4] = (char) extra;
325 /* Step 1: Init context */
326 /* Heimdal and newer MIT Kerberos require kcontext */
327 #if defined(HAVE_KRB5INT_DERIVE_KEY) || defined(HAVE_HEIMDAL)
328 code = krb5_init_context(&kcontext);
333 /* Step 2: Get the derived key */
335 #if HAVE_KRB5INT_DERIVE_KEY
336 code = krb5_k_create_key(kcontext, &kin, &key_in);
340 code = krb5int_derive_key(enc, key_in, &key_out, &datain,
343 krb5_k_free_key(kcontext, key_in);
345 krb5_k_key_keyblock(kcontext, key_out, &outkey);
346 krb5_k_free_key(kcontext, key_out);
348 #else /* !HAVE_KRB5INT_DERIVE_KEY */
349 out->length = keylength;
350 out->type = in->type;
352 key_lucid_to_krb5(out, &kout);
354 code = krb5_derive_key(enc, &kin, &kout, &datain);
355 #endif /* HAVE_KRB5INT_DERIVE_KEY */
356 #else /* !defined(HAVE_KRB5) */
357 code = krb5_derive_key(kcontext, &kin, in->type, constant_data, K5CLENGTH, &outkey);
358 #endif /* defined(HAVE_KRB5) */
363 /* Step 3: Copy the key to out */
364 #if defined(HAVE_KRB5INT_DERIVE_KEY) || defined(HAVE_HEIMDAL)
365 code = key_krb5_to_lucid(outkey, out);
366 krb5_free_keyblock(kcontext, outkey);
367 #else /* !defined(HAVE_KRB5) */
368 code = key_krb5_to_lucid(&kout, out);
369 #endif /* defined(HAVE_KRB5) */
371 /* Step 4: Free the context */
372 #if defined(HAVE_KRB5INT_DERIVE_KEY) || defined(HAVE_HEIMDAL)
373 krb5_free_context(kcontext);
378 printerr(0, "ERROR: %s: returning error %d (%s)\n",
379 __FUNCTION__, code, error_message(code));
385 * Prepare a new-style buffer, as defined in rfc4121 (a.k.a. cfx),
386 * to send to the kernel for newer encryption types -- or for DES3.
390 * u32 initiate; ( whether we are the initiator or not )
393 * #define KRB5_CTX_FLAG_INITIATOR 0x00000001
394 * #define KRB5_CTX_FLAG_CFX 0x00000002
395 * #define KRB5_CTX_FLAG_ACCEPTOR_SUBKEY 0x00000004
397 * u32 enctype; ( encrption type of keys )
398 * u32 size_of_each_key; ( size of each key in bytes )
399 * u32 number_of_keys; ( N -- should always be 3 for now )
406 prepare_krb5_rfc4121_buffer(gss_krb5_lucid_context_v1_t *lctx,
407 gss_buffer_desc *buf)
409 static int constant_two = 2;
411 uint32_t v2_flags = 0;
412 gss_krb5_lucid_key_t enc_key;
413 gss_krb5_lucid_key_t derived_key;
414 gss_buffer_desc fakeoid;
419 memset(&enc_key, 0, sizeof(enc_key));
420 memset(&fakeoid, 0, sizeof(fakeoid));
422 if (!(buf->value = calloc(1, MAX_CTX_LEN)))
425 end = buf->value + MAX_CTX_LEN;
428 if (WRITE_BYTES(&p, end, constant_two)) goto out_err;
429 if (WRITE_BYTES(&p, end, lctx->endtime)) goto out_err;
432 v2_flags |= KRB5_CTX_FLAG_INITIATOR;
433 if (lctx->protocol != 0)
434 v2_flags |= KRB5_CTX_FLAG_CFX;
435 if (lctx->protocol != 0 && lctx->cfx_kd.have_acceptor_subkey == 1)
436 v2_flags |= KRB5_CTX_FLAG_ACCEPTOR_SUBKEY;
438 if (WRITE_BYTES(&p, end, v2_flags)) goto out_err;
440 if (WRITE_BYTES(&p, end, lctx->send_seq)) goto out_err;
442 /* Protocol 0 here implies DES3 or RC4 */
443 printerr(3, "protocol %d\n", lctx->protocol);
444 if (lctx->protocol == 0) {
445 enctype = lctx->rfc1964_kd.ctx_key.type;
448 * The kernel gss code expects ENCTYPE_DES3_CBC_RAW (6) for
449 * 3des keys, but Heimdal key has ENCTYPE_DES3_CBC_SHA1 (16).
450 * Force the Heimdal enctype to 6.
452 if (enctype == ENCTYPE_DES3_CBC_SHA1) {
453 printerr(2, "%s: overriding heimdal keytype (%d => %d)\n",
454 __FUNCTION__, enctype, 6);
459 keysize = lctx->rfc1964_kd.ctx_key.length;
460 numkeys = 3; /* XXX is always gonna be three? */
462 if (lctx->cfx_kd.have_acceptor_subkey) {
463 enctype = lctx->cfx_kd.acceptor_subkey.type;
464 keysize = lctx->cfx_kd.acceptor_subkey.length;
466 enctype = lctx->cfx_kd.ctx_key.type;
467 keysize = lctx->cfx_kd.ctx_key.length;
471 printerr(3, "serializing %d keys with enctype %d and size %d\n",
472 numkeys, enctype, keysize);
473 if (WRITE_BYTES(&p, end, enctype)) goto out_err;
474 if (WRITE_BYTES(&p, end, keysize)) goto out_err;
475 if (WRITE_BYTES(&p, end, numkeys)) goto out_err;
477 if (lctx->protocol == 0) {
478 /* derive and send down: Ke, Ki, and Kc */
480 if (write_bytes(&p, end, lctx->rfc1964_kd.ctx_key.data,
481 lctx->rfc1964_kd.ctx_key.length))
485 if (write_bytes(&p, end, lctx->rfc1964_kd.ctx_key.data,
486 lctx->rfc1964_kd.ctx_key.length))
491 * RC4 is special, it dosen't need key derivation. Actually
492 * the Ke is based on plain text. Here we just let all three
493 * key identical, kernel will handle everything. --ericm
495 if (lctx->rfc1964_kd.ctx_key.type == ENCTYPE_ARCFOUR_HMAC) {
496 if (write_bytes(&p, end, lctx->rfc1964_kd.ctx_key.data,
497 lctx->rfc1964_kd.ctx_key.length))
500 if (derive_key_lucid(&lctx->rfc1964_kd.ctx_key,
502 KG_USAGE_SIGN, KEY_USAGE_SEED_CHECKSUM))
504 if (write_bytes(&p, end, derived_key.data,
507 free(derived_key.data);
510 gss_krb5_lucid_key_t *keyptr;
511 uint32_t sign_usage, seal_usage;
513 if (lctx->cfx_kd.have_acceptor_subkey)
514 keyptr = &lctx->cfx_kd.acceptor_subkey;
516 keyptr = &lctx->cfx_kd.ctx_key;
519 if (lctx->initiate == 1) {
520 sign_usage = KG_USAGE_INITIATOR_SIGN;
521 seal_usage = KG_USAGE_INITIATOR_SEAL;
523 sign_usage = KG_USAGE_ACCEPTOR_SIGN;
524 seal_usage = KG_USAGE_ACCEPTOR_SEAL;
528 * These are from rfc4142, but I don't understand: if we supply
529 * different 'usage' value for client & server, then the peers
530 * will have different derived keys. How could this work?
532 * Here we simply use old SIGN/SEAL values until we find the
536 sign_usage = KG_USAGE_SIGN;
537 seal_usage = KG_USAGE_SEAL;
540 /* derive and send down: Ke, Ki, and Kc */
543 if (derive_key_lucid(keyptr, &derived_key,
544 seal_usage, KEY_USAGE_SEED_ENCRYPTION))
546 if (write_bytes(&p, end, derived_key.data,
549 free(derived_key.data);
552 if (derive_key_lucid(keyptr, &derived_key,
553 seal_usage, KEY_USAGE_SEED_INTEGRITY))
555 if (write_bytes(&p, end, derived_key.data,
558 free(derived_key.data);
561 if (derive_key_lucid(keyptr, &derived_key,
562 sign_usage, KEY_USAGE_SEED_CHECKSUM))
564 if (write_bytes(&p, end, derived_key.data,
567 free(derived_key.data);
570 buf->length = p - (char *)buf->value;
574 printerr(0, "ERROR: %s: failed serializing krb5 context for kernel\n",
588 serialize_krb5_ctx(gss_ctx_id_t ctx, gss_buffer_desc *buf)
590 OM_uint32 maj_stat, min_stat;
591 void *return_ctx = 0;
593 gss_krb5_lucid_context_v1_t *lctx = 0;
596 printerr(3, "lucid version!\n");
597 maj_stat = gss_export_lucid_sec_context(&min_stat, &ctx,
599 if (maj_stat != GSS_S_COMPLETE) {
600 pgsserr("gss_export_lucid_sec_context",
601 maj_stat, min_stat, &krb5oid);
605 /* Check the version returned, we only support v1 right now */
606 vers = ((gss_krb5_lucid_context_version_t *)return_ctx)->version;
609 lctx = (gss_krb5_lucid_context_v1_t *) return_ctx;
612 printerr(0, "ERROR: unsupported lucid sec context version %d\n",
619 * Now lctx points to a lucid context that we can send down to kernel
621 * Note: we send down different information to the kernel depending
622 * on the protocol version and the enctyption type.
623 * For protocol version 0 with all enctypes besides DES3, we use
624 * the original format. For protocol version != 0 or DES3, we
625 * send down the new style information.
628 if (lctx->protocol == 0 && lctx->rfc1964_kd.ctx_key.type <= 4)
629 retcode = prepare_krb5_rfc1964_buffer(lctx, buf);
631 retcode = prepare_krb5_rfc4121_buffer(lctx, buf);
633 maj_stat = gss_free_lucid_sec_context(&min_stat, ctx, return_ctx);
634 if (maj_stat != GSS_S_COMPLETE) {
635 pgsserr("gss_export_lucid_sec_context",
636 maj_stat, min_stat, &krb5oid);
637 printerr(0, "WARN: failed to free lucid sec context\n");
641 printerr(1, "%s: prepare_krb5_*_buffer failed (retcode = %d)\n",
642 __FUNCTION__, retcode);
649 printerr(0, "ERROR: failed serializing krb5 context for kernel\n");
655 #endif /* HAVE_LUCID_CONTEXT_SUPPORT */