Whamcloud - gitweb
LU-2384 kerberos: Support for MIT-kerberos >=1.8.X is broken
[fs/lustre-release.git] / lustre / utils / gss / context_lucid.c
1 /*
2  * COPYRIGHT (c) 2006
3  * The Regents of the University of Michigan
4  * ALL RIGHTS RESERVED
5  *
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
15  * also be included.
16  *
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
28  * SUCH DAMAGES.
29  */
30
31 #include "config.h"
32
33 #ifdef HAVE_LUCID_CONTEXT_SUPPORT
34
35 /*
36  * Newer versions of MIT and Heimdal have lucid context support.
37  * We can use common code if it is supported.
38  */
39
40 #include <stdio.h>
41 #include <syslog.h>
42 #include <string.h>
43 #include <errno.h>
44 #include <stdint.h>
45 #include <krb5.h>
46 #include <gssapi/gssapi.h>
47 #ifndef OM_uint64
48 typedef uint64_t OM_uint64;
49 #endif
50 #include <gssapi/gssapi_krb5.h>
51
52 #ifdef _NEW_BUILD_
53 # include "lgss_utils.h"
54 #else
55 # include "gss_util.h"
56 # include "gss_oids.h"
57 # include "err_util.h"
58 #endif
59 #include "write_bytes.h"
60 #include "context.h"
61
62 extern OM_uint32 gss_export_lucid_sec_context(OM_uint32 *min_stat,
63                                               gss_ctx_id_t *ctx,
64                                               OM_uint32 version,
65                                               void **kctx);
66 extern OM_uint32 gss_free_lucid_sec_context(OM_uint32 *min_stat,
67                                             gss_ctx_id_t ctx,
68                                             void *kctx);
69
70 static int
71 write_lucid_keyblock(char **p, char *end, gss_krb5_lucid_key_t *key)
72 {
73         gss_buffer_desc tmp;
74
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;
79         return 0;
80 }
81
82 static int
83 prepare_krb5_rfc1964_buffer(gss_krb5_lucid_context_v1_t *lctx,
84         gss_buffer_desc *buf)
85 {
86         char *p, *end;
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;
91         int i;
92         char *skd, *dkd;
93         gss_buffer_desc fakeoid;
94
95         /*
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.
101          */
102         memset(&enc_key, 0, sizeof(enc_key));
103         memset(&fakeoid, 0, sizeof(fakeoid));
104
105         if (!(buf->value = calloc(1, MAX_CTX_LEN)))
106                 goto out_err;
107         p = buf->value;
108         end = buf->value + MAX_CTX_LEN;
109
110         if (WRITE_BYTES(&p, end, lctx->initiate)) goto out_err;
111
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;
115
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;
122
123 #ifdef HAVE_HEIMDAL
124         /*
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.
129          */
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;
134         }
135 #endif
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);
139
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)
144                 goto out_err;
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)) {
150                 free(enc_key.data);
151                 goto out_err;
152         }
153         free(enc_key.data);
154
155         if (write_lucid_keyblock(&p, end, &lctx->rfc1964_kd.ctx_key))
156                 goto out_err;
157
158         buf->length = p - (char *)buf->value;
159         return 0;
160 out_err:
161         printerr(0, "ERROR: failed serializing krb5 context for kernel\n");
162         if (buf->value) free(buf->value);
163         buf->length = 0;
164         if (enc_key.data) free(enc_key.data);
165         return -1;
166 }
167
168 /* XXX Hack alert! XXX  Do NOT submit upstream! XXX */
169 /* XXX Hack alert! XXX  Do NOT submit upstream! XXX */
170
171 /* for 3DES */
172 #define KG_USAGE_SEAL 22
173 #define KG_USAGE_SIGN 23
174 #define KG_USAGE_SEQ  24
175
176 /* for rfc???? */
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
181
182 /* Lifted from mit src/lib/gssapi/krb5/gssapiP_krb5.h */
183 enum seal_alg {
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
189 };
190
191 #define KEY_USAGE_SEED_ENCRYPTION       0xAA
192 #define KEY_USAGE_SEED_INTEGRITY        0x55
193 #define KEY_USAGE_SEED_CHECKSUM         0x99
194 #define K5CLENGTH 5
195
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
200
201 /* XXX Hack alert! XXX  Do NOT submit upstream! XXX */
202 /* XXX Hack alert! XXX  Do NOT submit upstream! XXX */
203 /*
204  * We don't have "legal" access to these MIT-only
205  * structures located in libk5crypto
206  */
207 extern void krb5int_enc_arcfour;
208 extern void krb5int_enc_des3;
209 extern void krb5int_enc_aes128;
210 extern void krb5int_enc_aes256;
211 #if HAVE_KRB5INT_DERIVE_KEY
212 /* Taken from crypto_int.h */
213 enum deriv_alg {
214         DERIVE_RFC3961,         /* RFC 3961 section 5.1 */
215 #ifdef CAMELLIA
216         DERIVE_SP800_108_CMAC,  /* NIST SP 800-108 with CMAC as PRF */
217 #endif
218 };
219 #endif  /* HAVE_KRB5INT_DERIVE_KEY */
220
221 static void
222 key_lucid_to_krb5(const gss_krb5_lucid_key_t *lin, krb5_keyblock *kout)
223 {
224         memset(kout, '\0', sizeof(kout));
225 #ifdef HAVE_KRB5
226         kout->enctype = lin->type;
227         kout->length = lin->length;
228         kout->contents = lin->data;
229 #else
230         kout->keytype = lin->type;
231         kout->keyvalue.length = lin->length;
232         kout->keyvalue.data = lin->data;
233 #endif
234 }
235
236 static void
237 key_krb5_to_lucid(const krb5_keyblock *kin, gss_krb5_lucid_key_t *lout)
238 {
239         memset(lout, '\0', sizeof(lout));
240 #ifdef HAVE_KRB5
241         lout->type = kin->enctype;
242         lout->length = kin->length;
243         lout->data = kin->contents;
244 #else
245         lout->type = kin->keytype;
246         lout->length = kin->keyvalue.length;
247         memcpy(lout->data, kin->keyvalue.data, kin->keyvalue.length);
248 #endif
249 }
250
251 /* XXX Hack alert! XXX  Do NOT submit upstream! XXX */
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 /*
256  * Function to derive a new key from a given key and given constant data.
257  */
258 static krb5_error_code
259 derive_key_lucid(const gss_krb5_lucid_key_t *in, gss_krb5_lucid_key_t *out,
260                  int usage, char extra)
261 {
262         krb5_error_code code;
263         unsigned char constant_data[K5CLENGTH];
264         krb5_data datain;
265         int keylength;
266         void *enc;
267         krb5_keyblock kin, kout;  /* must send krb5_keyblock, not lucid! */
268 #if defined(HAVE_HEIMDAL) || HAVE_KRB5INT_DERIVE_KEY
269         krb5_context kcontext;
270 #endif
271 #if HAVE_KRB5INT_DERIVE_KEY
272         krb5_key key_in, key_out;
273 #endif
274 #ifdef HAVE_HEIMDAL
275         krb5_keyblock *outkey;
276 #endif
277
278         /*
279          * XXX Hack alert.  We don't have "legal" access to these
280          * values and structures located in libk5crypto
281          */
282         switch (in->type) {
283         case ENCTYPE_DES3_CBC_SHA1:
284 #ifdef HAVE_KRB5
285         case ENCTYPE_DES3_CBC_RAW:
286 #endif
287                 keylength = 24;
288 #ifdef HAVE_KRB5
289                 enc = &krb5int_enc_des3;
290 #endif
291                 break;
292         case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
293                 keylength = 16;
294 #ifdef HAVE_KRB5
295                 enc = &krb5int_enc_aes128;
296 #endif
297                 break;
298         case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
299                 keylength = 32;
300 #ifdef HAVE_KRB5
301                 enc = &krb5int_enc_aes256;
302 #endif
303                 break;
304         default:
305                 code = KRB5_BAD_ENCTYPE;
306                 goto out;
307         }
308
309         /* allocate memory for output key */
310         if ((out->data = malloc(keylength)) == NULL) {
311                 code = ENOMEM;
312                 goto out;
313         }
314         out->length = keylength;
315         out->type = in->type;
316
317         /* Convert to correct format for call to krb5_derive_key */
318         key_lucid_to_krb5(in, &kin);
319         key_lucid_to_krb5(out, &kout);
320
321         datain.data = (char *) constant_data;
322         datain.length = K5CLENGTH;
323
324         ((char *)(datain.data))[0] = (usage>>24)&0xff;
325         ((char *)(datain.data))[1] = (usage>>16)&0xff;
326         ((char *)(datain.data))[2] = (usage>>8)&0xff;
327         ((char *)(datain.data))[3] = usage&0xff;
328
329         ((char *)(datain.data))[4] = (char) extra;
330
331 #ifdef HAVE_KRB5
332 #if HAVE_KRB5INT_DERIVE_KEY
333         code = krb5_init_context(&kcontext);
334         if (code) {
335                 free(out->data);
336                 out->data = NULL;
337                 goto out;
338         }
339         code = krb5_k_create_key(kcontext, &kin, &key_in);
340         if (code) {
341                 free(out->data);
342                 out->data = NULL;
343                 goto out;
344         }
345         code = krb5_k_create_key(kcontext, &kout, &key_out);
346         if (code) {
347                 free(out->data);
348                 out->data = NULL;
349                 goto out;
350         }
351         code = krb5int_derive_key(enc, key_in, &key_out, &datain,
352                                   DERIVE_RFC3961);
353 #else  /* !HAVE_KRB5INT_DERIVE_KEY */
354         code = krb5_derive_key(enc, &kin, &kout, &datain);
355 #endif  /* HAVE_KRB5INT_DERIVE_KEY */
356 #else   /* !defined(HAVE_KRB5) */
357         if ((code = krb5_init_context(&kcontext))) {
358         }
359         code = krb5_derive_key(kcontext, &kin, in->type, constant_data, K5CLENGTH, &outkey);
360 #endif  /* defined(HAVE_KRB5) */
361         if (code) {
362                 free(out->data);
363                 out->data = NULL;
364                 goto out;
365         }
366 #ifdef HAVE_KRB5
367         key_krb5_to_lucid(&kout, out);
368 #if HAVE_KRB5INT_DERIVE_KEY
369         krb5_free_context(kcontext);
370 #endif  /* HAVE_KRB5INT_DERIVE_KEY */
371 #else   /* !defined(HAVE_KRB5) */
372         key_krb5_to_lucid(outkey, out);
373         krb5_free_keyblock(kcontext, outkey);
374         krb5_free_context(kcontext);
375 #endif  /* defined(HAVE_KRB5) */
376
377   out:
378         if (code)
379                 printerr(0, "ERROR: %s: returning error %d (%s)\n",
380                          __FUNCTION__, code, error_message(code));
381         return (code);
382 }
383
384
385 /*
386  * Prepare a new-style buffer, as defined in rfc4121 (a.k.a. cfx),
387  * to send to the kernel for newer encryption types -- or for DES3.
388  *
389  * The new format is:
390  *
391  *      u32 initiate;                   ( whether we are the initiator or not )
392  *      s32 endtime;
393  *      u32 flags;
394  *      #define KRB5_CTX_FLAG_INITIATOR         0x00000001
395  *      #define KRB5_CTX_FLAG_CFX               0x00000002
396  *      #define KRB5_CTX_FLAG_ACCEPTOR_SUBKEY   0x00000004
397  *      u64 seq_send;
398  *      u32  enctype;                   ( encrption type of keys )
399  *      u32  size_of_each_key;          ( size of each key in bytes )
400  *      u32  number_of_keys;            ( N -- should always be 3 for now )
401  *      keydata-1;                      ( Ke )
402  *      keydata-2;                      ( Ki )
403  *      keydata-3;                      ( Kc )
404  *
405  */
406 static int
407 prepare_krb5_rfc4121_buffer(gss_krb5_lucid_context_v1_t *lctx,
408                             gss_buffer_desc *buf)
409 {
410         static int constant_two = 2;
411         char *p, *end;
412         uint32_t v2_flags = 0;
413         gss_krb5_lucid_key_t enc_key;
414         gss_krb5_lucid_key_t derived_key;
415         gss_buffer_desc fakeoid;
416         uint32_t enctype;
417         uint32_t keysize;
418         uint32_t numkeys;
419
420         memset(&enc_key, 0, sizeof(enc_key));
421         memset(&fakeoid, 0, sizeof(fakeoid));
422
423         if (!(buf->value = calloc(1, MAX_CTX_LEN)))
424                 goto out_err;
425         p = buf->value;
426         end = buf->value + MAX_CTX_LEN;
427
428         /* Version 2 */
429         if (WRITE_BYTES(&p, end, constant_two)) goto out_err;
430         if (WRITE_BYTES(&p, end, lctx->endtime)) goto out_err;
431
432         if (lctx->initiate)
433                 v2_flags |= KRB5_CTX_FLAG_INITIATOR;
434         if (lctx->protocol != 0)
435                 v2_flags |= KRB5_CTX_FLAG_CFX;
436         if (lctx->protocol != 0 && lctx->cfx_kd.have_acceptor_subkey == 1)
437                 v2_flags |= KRB5_CTX_FLAG_ACCEPTOR_SUBKEY;
438
439         if (WRITE_BYTES(&p, end, v2_flags)) goto out_err;
440
441         if (WRITE_BYTES(&p, end, lctx->send_seq)) goto out_err;
442
443         /* Protocol 0 here implies DES3 or RC4 */
444         printerr(3, "protocol %d\n", lctx->protocol);
445         if (lctx->protocol == 0) {
446                 enctype = lctx->rfc1964_kd.ctx_key.type;
447 #ifdef HAVE_HEIMDAL
448                 /*
449                  * The kernel gss code expects ENCTYPE_DES3_CBC_RAW (6) for
450                  * 3des keys, but Heimdal key has ENCTYPE_DES3_CBC_SHA1 (16).
451                  * Force the Heimdal enctype to 6.
452                  */
453                 if (enctype == ENCTYPE_DES3_CBC_SHA1) {
454                         printerr(2, "%s: overriding heimdal keytype (%d => %d)\n",
455                                  __FUNCTION__, enctype, 6);
456
457                         enctype = 6;
458                 }
459 #endif
460                 keysize = lctx->rfc1964_kd.ctx_key.length;
461                 numkeys = 3;    /* XXX is always gonna be three? */
462         } else {
463                 if (lctx->cfx_kd.have_acceptor_subkey) {
464                         enctype = lctx->cfx_kd.acceptor_subkey.type;
465                         keysize = lctx->cfx_kd.acceptor_subkey.length;
466                 } else {
467                         enctype = lctx->cfx_kd.ctx_key.type;
468                         keysize = lctx->cfx_kd.ctx_key.length;
469                 }
470                 numkeys = 3;
471         }
472         printerr(3, "serializing %d keys with enctype %d and size %d\n",
473                  numkeys, enctype, keysize);
474         if (WRITE_BYTES(&p, end, enctype)) goto out_err;
475         if (WRITE_BYTES(&p, end, keysize)) goto out_err;
476         if (WRITE_BYTES(&p, end, numkeys)) goto out_err;
477
478         if (lctx->protocol == 0) {
479                 /* derive and send down: Ke, Ki, and Kc */
480                 /* Ke */
481                 if (write_bytes(&p, end, lctx->rfc1964_kd.ctx_key.data,
482                                 lctx->rfc1964_kd.ctx_key.length))
483                         goto out_err;
484
485                 /* Ki */
486                 if (write_bytes(&p, end, lctx->rfc1964_kd.ctx_key.data,
487                                 lctx->rfc1964_kd.ctx_key.length))
488                         goto out_err;
489
490                 /* Kc */
491                 /*
492                  * RC4 is special, it dosen't need key derivation. Actually
493                  * the Ke is based on plain text. Here we just let all three
494                  * key identical, kernel will handle everything. --ericm
495                  */
496                 if (lctx->rfc1964_kd.ctx_key.type == ENCTYPE_ARCFOUR_HMAC) {
497                         if (write_bytes(&p, end, lctx->rfc1964_kd.ctx_key.data,
498                                         lctx->rfc1964_kd.ctx_key.length))
499                                 goto out_err;
500                 } else {
501                         if (derive_key_lucid(&lctx->rfc1964_kd.ctx_key,
502                                         &derived_key,
503                                         KG_USAGE_SIGN, KEY_USAGE_SEED_CHECKSUM))
504                                 goto out_err;
505                         if (write_bytes(&p, end, derived_key.data,
506                                         derived_key.length))
507                                 goto out_err;
508                         free(derived_key.data);
509                 }
510         } else {
511                 gss_krb5_lucid_key_t *keyptr;
512                 uint32_t sign_usage, seal_usage;
513
514                 if (lctx->cfx_kd.have_acceptor_subkey)
515                         keyptr = &lctx->cfx_kd.acceptor_subkey;
516                 else
517                         keyptr = &lctx->cfx_kd.ctx_key;
518
519 #if 0
520                 if (lctx->initiate == 1) {
521                         sign_usage = KG_USAGE_INITIATOR_SIGN;
522                         seal_usage = KG_USAGE_INITIATOR_SEAL;
523                 } else {
524                         sign_usage = KG_USAGE_ACCEPTOR_SIGN;
525                         seal_usage = KG_USAGE_ACCEPTOR_SEAL;
526                 }
527 #else
528                 /* FIXME
529                  * These are from rfc4142, but I don't understand: if we supply
530                  * different 'usage' value for client & server, then the peers
531                  * will have different derived keys. How could this work?
532                  *
533                  * Here we simply use old SIGN/SEAL values until we find the
534                  * answer.  --ericm
535                  * FIXME
536                  */
537                 sign_usage = KG_USAGE_SIGN;
538                 seal_usage = KG_USAGE_SEAL;
539 #endif
540
541                 /* derive and send down: Ke, Ki, and Kc */
542
543                 /* Ke */
544                 if (derive_key_lucid(keyptr, &derived_key,
545                                seal_usage, KEY_USAGE_SEED_ENCRYPTION))
546                         goto out_err;
547                 if (write_bytes(&p, end, derived_key.data,
548                                 derived_key.length))
549                         goto out_err;
550                 free(derived_key.data);
551
552                 /* Ki */
553                 if (derive_key_lucid(keyptr, &derived_key,
554                                seal_usage, KEY_USAGE_SEED_INTEGRITY))
555                         goto out_err;
556                 if (write_bytes(&p, end, derived_key.data,
557                                 derived_key.length))
558                         goto out_err;
559                 free(derived_key.data);
560
561                 /* Kc */
562                 if (derive_key_lucid(keyptr, &derived_key,
563                                sign_usage, KEY_USAGE_SEED_CHECKSUM))
564                         goto out_err;
565                 if (write_bytes(&p, end, derived_key.data,
566                                 derived_key.length))
567                         goto out_err;
568                 free(derived_key.data);
569         }
570
571         buf->length = p - (char *)buf->value;
572         return 0;
573
574 out_err:
575         printerr(0, "ERROR: %s: failed serializing krb5 context for kernel\n",
576                  __FUNCTION__);
577         if (buf->value) {
578                 free(buf->value);
579                 buf->value = NULL;
580         }
581         buf->length = 0;
582         if (enc_key.data) {
583                 free(enc_key.data);
584                 enc_key.data = NULL;
585         }
586         return -1;
587 }
588 int
589 serialize_krb5_ctx(gss_ctx_id_t ctx, gss_buffer_desc *buf)
590 {
591         OM_uint32 maj_stat, min_stat;
592         void *return_ctx = 0;
593         OM_uint32 vers;
594         gss_krb5_lucid_context_v1_t *lctx = 0;
595         int retcode = 0;
596
597         printerr(3, "lucid version!\n");
598         maj_stat = gss_export_lucid_sec_context(&min_stat, &ctx,
599                                                 1, &return_ctx);
600         if (maj_stat != GSS_S_COMPLETE) {
601                 pgsserr("gss_export_lucid_sec_context",
602                         maj_stat, min_stat, &krb5oid);
603                 goto out_err;
604         }
605
606         /* Check the version returned, we only support v1 right now */
607         vers = ((gss_krb5_lucid_context_version_t *)return_ctx)->version;
608         switch (vers) {
609         case 1:
610                 lctx = (gss_krb5_lucid_context_v1_t *) return_ctx;
611                 break;
612         default:
613                 printerr(0, "ERROR: unsupported lucid sec context version %d\n",
614                         vers);
615                 goto out_err;
616                 break;
617         }
618
619         /*
620          * Now lctx points to a lucid context that we can send down to kernel
621          *
622          * Note: we send down different information to the kernel depending
623          * on the protocol version and the enctyption type.
624          * For protocol version 0 with all enctypes besides DES3, we use
625          * the original format.  For protocol version != 0 or DES3, we
626          * send down the new style information.
627          */
628
629         if (lctx->protocol == 0 && lctx->rfc1964_kd.ctx_key.type <= 4)
630                 retcode = prepare_krb5_rfc1964_buffer(lctx, buf);
631         else
632                 retcode = prepare_krb5_rfc4121_buffer(lctx, buf);
633
634         maj_stat = gss_free_lucid_sec_context(&min_stat, ctx, return_ctx);
635         if (maj_stat != GSS_S_COMPLETE) {
636                 pgsserr("gss_export_lucid_sec_context",
637                         maj_stat, min_stat, &krb5oid);
638                 printerr(0, "WARN: failed to free lucid sec context\n");
639         }
640
641         if (retcode) {
642                 printerr(1, "%s: prepare_krb5_*_buffer failed (retcode = %d)\n",
643                          __FUNCTION__, retcode);
644                 goto out_err;
645         }
646
647         return 0;
648
649 out_err:
650         printerr(0, "ERROR: failed serializing krb5 context for kernel\n");
651         return -1;
652 }
653
654
655
656 #endif /* HAVE_LUCID_CONTEXT_SUPPORT */