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