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 #endif /* HAVE_CONFIG_H */
35 #ifdef HAVE_LUCID_CONTEXT_SUPPORT
38 * Newer versions of MIT and Heimdal have lucid context support.
39 * We can use common code if it is supported.
47 #include <gssapi/gssapi_krb5.h>
55 typedef uint64_t OM_uint64;
59 write_lucid_keyblock(char **p, char *end, gss_krb5_lucid_key_t *key)
63 if (WRITE_BYTES(p, end, key->type)) return -1;
64 tmp.length = key->length;
65 tmp.value = key->data;
66 if (write_buffer(p, end, &tmp)) return -1;
71 prepare_krb5_rfc1964_buffer(gss_krb5_lucid_context_v1_t *lctx,
72 gss_buffer_desc *buf, int32_t *endtime)
74 #define FAKESEED_SIZE 16
76 static int constant_zero = 0;
77 unsigned char fakeseed[FAKESEED_SIZE];
78 uint32_t word_send_seq;
79 gss_krb5_lucid_key_t enc_key;
82 gss_buffer_desc fakeoid;
85 * The new Kerberos interface to get the gss context
86 * does not include the seed or seed_init fields
87 * because we never really use them. But for now,
88 * send down a fake buffer so we can use the same
89 * interface to the kernel.
91 memset(&enc_key, 0, sizeof(enc_key));
92 memset(&fakeoid, 0, sizeof(fakeoid));
93 memset(fakeseed, 0, FAKESEED_SIZE);
95 if (!(buf->value = calloc(1, MAX_CTX_LEN)))
98 end = buf->value + MAX_CTX_LEN;
100 if (WRITE_BYTES(&p, end, lctx->initiate)) goto out_err;
102 /* seed_init and seed not used by kernel anyway */
103 if (WRITE_BYTES(&p, end, constant_zero)) goto out_err;
104 if (write_bytes(&p, end, &fakeseed, FAKESEED_SIZE)) goto out_err;
106 if (WRITE_BYTES(&p, end, lctx->rfc1964_kd.sign_alg)) goto out_err;
107 if (WRITE_BYTES(&p, end, lctx->rfc1964_kd.seal_alg)) goto out_err;
108 if (WRITE_BYTES(&p, end, lctx->endtime)) goto out_err;
110 *endtime = lctx->endtime;
111 word_send_seq = lctx->send_seq; /* XXX send_seq is 64-bit */
112 if (WRITE_BYTES(&p, end, word_send_seq)) goto out_err;
113 if (write_oid(&p, end, &krb5oid)) goto out_err;
117 * The kernel gss code expects des-cbc-raw for all flavors of des.
118 * The keytype from MIT has this type, but Heimdal does not.
119 * Force the Heimdal keytype to 4 (des-cbc-raw).
120 * Note that the rfc1964 version only supports DES enctypes.
122 if (lctx->rfc1964_kd.ctx_key.type != 4) {
123 printerr(2, "%s: overriding heimdal keytype (%d => %d)\n",
124 __FUNCTION__, lctx->rfc1964_kd.ctx_key.type, 4);
125 lctx->rfc1964_kd.ctx_key.type = 4;
128 printerr(2, "%s: serializing keys with enctype %d and length %d\n",
129 __FUNCTION__, lctx->rfc1964_kd.ctx_key.type,
130 lctx->rfc1964_kd.ctx_key.length);
132 /* derive the encryption key and copy it into buffer */
133 enc_key.type = lctx->rfc1964_kd.ctx_key.type;
134 enc_key.length = lctx->rfc1964_kd.ctx_key.length;
135 if ((enc_key.data = calloc(1, enc_key.length)) == NULL)
137 skd = (char *) lctx->rfc1964_kd.ctx_key.data;
138 dkd = (char *) enc_key.data;
139 for (i = 0; i < enc_key.length; i++)
140 dkd[i] = skd[i] ^ 0xf0;
141 if (write_lucid_keyblock(&p, end, &enc_key)) {
147 if (write_lucid_keyblock(&p, end, &lctx->rfc1964_kd.ctx_key))
150 buf->length = p - (char *)buf->value;
153 printerr(0, "ERROR: failed serializing krb5 context for kernel\n");
154 if (buf->value) free(buf->value);
156 if (enc_key.data) free(enc_key.data);
160 /* Flags for version 2 context flags */
161 #define KRB5_CTX_FLAG_INITIATOR 0x00000001
162 #define KRB5_CTX_FLAG_CFX 0x00000002
163 #define KRB5_CTX_FLAG_ACCEPTOR_SUBKEY 0x00000004
166 * Prepare a new-style buffer, as defined in rfc4121 (a.k.a. cfx),
167 * to send to the kernel for newer encryption types -- or for DES3.
172 * #define KRB5_CTX_FLAG_INITIATOR 0x00000001
173 * #define KRB5_CTX_FLAG_CFX 0x00000002
174 * #define KRB5_CTX_FLAG_ACCEPTOR_SUBKEY 0x00000004
177 * u32 enctype; ( encrption type of key )
178 * raw key; ( raw key bytes (kernel will derive))
182 prepare_krb5_rfc4121_buffer(gss_krb5_lucid_context_v1_t *lctx,
183 gss_buffer_desc *buf, int32_t *endtime)
186 uint32_t v2_flags = 0;
190 if (!(buf->value = calloc(1, MAX_CTX_LEN)))
193 end = buf->value + MAX_CTX_LEN;
197 v2_flags |= KRB5_CTX_FLAG_INITIATOR;
198 if (lctx->protocol != 0)
199 v2_flags |= KRB5_CTX_FLAG_CFX;
200 if (lctx->protocol != 0 && lctx->cfx_kd.have_acceptor_subkey == 1)
201 v2_flags |= KRB5_CTX_FLAG_ACCEPTOR_SUBKEY;
203 if (WRITE_BYTES(&p, end, v2_flags)) goto out_err;
204 if (WRITE_BYTES(&p, end, lctx->endtime)) goto out_err;
206 *endtime = lctx->endtime;
207 if (WRITE_BYTES(&p, end, lctx->send_seq)) goto out_err;
209 /* Protocol 0 here implies DES3 or RC4 */
210 printerr(2, "%s: protocol %d\n", __FUNCTION__, lctx->protocol);
211 if (lctx->protocol == 0) {
212 enctype = lctx->rfc1964_kd.ctx_key.type;
213 keysize = lctx->rfc1964_kd.ctx_key.length;
215 if (lctx->cfx_kd.have_acceptor_subkey) {
216 enctype = lctx->cfx_kd.acceptor_subkey.type;
217 keysize = lctx->cfx_kd.acceptor_subkey.length;
219 enctype = lctx->cfx_kd.ctx_key.type;
220 keysize = lctx->cfx_kd.ctx_key.length;
223 printerr(2, "%s: serializing key with enctype %d and size %d\n",
224 __FUNCTION__, enctype, keysize);
226 if (WRITE_BYTES(&p, end, enctype)) goto out_err;
228 if (lctx->protocol == 0) {
229 if (write_bytes(&p, end, lctx->rfc1964_kd.ctx_key.data,
230 lctx->rfc1964_kd.ctx_key.length))
233 if (lctx->cfx_kd.have_acceptor_subkey) {
234 if (write_bytes(&p, end,
235 lctx->cfx_kd.acceptor_subkey.data,
236 lctx->cfx_kd.acceptor_subkey.length))
239 if (write_bytes(&p, end, lctx->cfx_kd.ctx_key.data,
240 lctx->cfx_kd.ctx_key.length))
245 buf->length = p - (char *)buf->value;
249 printerr(0, "ERROR: %s: failed serializing krb5 context for kernel\n",
261 serialize_krb5_ctx(gss_ctx_id_t ctx, gss_buffer_desc *buf, int32_t *endtime)
263 OM_uint32 maj_stat, min_stat;
264 void *return_ctx = 0;
266 gss_krb5_lucid_context_v1_t *lctx = 0;
269 printerr(2, "DEBUG: %s: lucid version!\n", __FUNCTION__);
270 maj_stat = gss_export_lucid_sec_context(&min_stat, &ctx,
272 if (maj_stat != GSS_S_COMPLETE) {
273 pgsserr("gss_export_lucid_sec_context",
274 maj_stat, min_stat, &krb5oid);
278 /* Check the version returned, we only support v1 right now */
279 vers = ((gss_krb5_lucid_context_version_t *)return_ctx)->version;
282 lctx = (gss_krb5_lucid_context_v1_t *) return_ctx;
285 printerr(0, "ERROR: unsupported lucid sec context version %d\n",
292 * Now lctx points to a lucid context that we can send down to kernel
294 * Note: we send down different information to the kernel depending
295 * on the protocol version and the enctyption type.
296 * For protocol version 0 with all enctypes besides DES3, we use
297 * the original format. For protocol version != 0 or DES3, we
298 * send down the new style information.
301 if (lctx->protocol == 0 && lctx->rfc1964_kd.ctx_key.type <= 4)
302 retcode = prepare_krb5_rfc1964_buffer(lctx, buf, endtime);
304 retcode = prepare_krb5_rfc4121_buffer(lctx, buf, endtime);
306 maj_stat = gss_free_lucid_sec_context(&min_stat, ctx, return_ctx);
307 if (maj_stat != GSS_S_COMPLETE) {
308 pgsserr("gss_free_lucid_sec_context",
309 maj_stat, min_stat, &krb5oid);
310 printerr(0, "WARN: failed to free lucid sec context\n");
314 printerr(1, "%s: prepare_krb5_*_buffer failed (retcode = %d)\n",
315 __FUNCTION__, retcode);
322 printerr(0, "ERROR: failed serializing krb5 context for kernel\n");
328 #endif /* HAVE_LUCID_CONTEXT_SUPPORT */