Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_krb5_mech.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Modifications for Lustre
5  * Copyright 2004 - 2006, Cluster File Systems, Inc.
6  * All rights reserved
7  * Author: Eric Mei <ericm@clusterfs.com>
8  */
9
10 /*
11  *  linux/net/sunrpc/gss_krb5_mech.c
12  *  linux/net/sunrpc/gss_krb5_crypto.c
13  *  linux/net/sunrpc/gss_krb5_seal.c
14  *  linux/net/sunrpc/gss_krb5_seqnum.c
15  *  linux/net/sunrpc/gss_krb5_unseal.c
16  *
17  *  Copyright (c) 2001 The Regents of the University of Michigan.
18  *  All rights reserved.
19  *
20  *  Andy Adamson <andros@umich.edu>
21  *  J. Bruce Fields <bfields@umich.edu>
22  *
23  *  Redistribution and use in source and binary forms, with or without
24  *  modification, are permitted provided that the following conditions
25  *  are met:
26  *
27  *  1. Redistributions of source code must retain the above copyright
28  *     notice, this list of conditions and the following disclaimer.
29  *  2. Redistributions in binary form must reproduce the above copyright
30  *     notice, this list of conditions and the following disclaimer in the
31  *     documentation and/or other materials provided with the distribution.
32  *  3. Neither the name of the University nor the names of its
33  *     contributors may be used to endorse or promote products derived
34  *     from this software without specific prior written permission.
35  *
36  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
37  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
38  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
44  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
45  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
46  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  *
48  */
49
50 #ifndef EXPORT_SYMTAB
51 # define EXPORT_SYMTAB
52 #endif
53 #define DEBUG_SUBSYSTEM S_SEC
54 #ifdef __KERNEL__
55 #include <linux/init.h>
56 #include <linux/module.h>
57 #include <linux/slab.h>
58 #include <linux/crypto.h>
59 #include <linux/random.h>
60 #include <linux/mutex.h>
61 #else
62 #include <liblustre.h>
63 #endif
64
65 #include <obd.h>
66 #include <obd_class.h>
67 #include <obd_support.h>
68 #include <lustre/lustre_idl.h>
69 #include <lustre_net.h>
70 #include <lustre_import.h>
71 #include <lustre_sec.h>
72
73 #include "gss_err.h"
74 #include "gss_internal.h"
75 #include "gss_api.h"
76 #include "gss_asn1.h"
77 #include "gss_krb5.h"
78
79 spinlock_t krb5_seq_lock = SPIN_LOCK_UNLOCKED;
80
81 struct krb5_enctype {
82         char           *ke_dispname;
83         char           *ke_enc_name;            /* linux tfm name */
84         char           *ke_hash_name;           /* linux tfm name */
85         int             ke_enc_mode;            /* linux tfm mode */
86         int             ke_hash_size;           /* checksum size */
87         int             ke_conf_size;           /* confounder size */
88         unsigned int    ke_hash_hmac:1;         /* is hmac? */
89 };
90
91 /*
92  * NOTE: for aes128-cts and aes256-cts, MIT implementation use CTS encryption.
93  * but currently we simply CBC with padding, because linux doesn't support CTS
94  * yet. this need to be fixed in the future.
95  */
96 static struct krb5_enctype enctypes[] = {
97         [ENCTYPE_DES_CBC_RAW] = {               /* des-cbc-md5 */
98                 "des-cbc-md5",
99                 "cbc(des)",
100                 "md5",
101                 0,
102                 16,
103                 8,
104                 0,
105         },
106         [ENCTYPE_DES3_CBC_RAW] = {              /* des3-hmac-sha1 */
107                 "des3-hmac-sha1",
108                 "cbc(des3_ede)",
109                 "hmac(sha1)",
110                 0,
111                 20,
112                 8,
113                 1,
114         },
115         [ENCTYPE_AES128_CTS_HMAC_SHA1_96] = {   /* aes128-cts */
116                 "aes128-cts-hmac-sha1-96",
117                 "cbc(aes)",
118                 "hmac(sha1)",
119                 0,
120                 12,
121                 16,
122                 1,
123         },
124         [ENCTYPE_AES256_CTS_HMAC_SHA1_96] = {   /* aes256-cts */
125                 "aes256-cts-hmac-sha1-96",
126                 "cbc(aes)",
127                 "hmac(sha1)",
128                 0,
129                 12,
130                 16,
131                 1,
132         },
133         [ENCTYPE_ARCFOUR_HMAC] = {              /* arcfour-hmac-md5 */
134                 "arcfour-hmac-md5",
135                 "ecb(arc4)",
136                 "hmac(md5)",
137                 0,
138                 16,
139                 8,
140                 1,
141         },
142 };
143
144 #define MAX_ENCTYPES    sizeof(enctypes)/sizeof(struct krb5_enctype)
145
146 static const char * enctype2str(__u32 enctype)
147 {
148         if (enctype < MAX_ENCTYPES && enctypes[enctype].ke_dispname)
149                 return enctypes[enctype].ke_dispname;
150
151         return "unknown";
152 }
153
154 static
155 int keyblock_init(struct krb5_keyblock *kb, char *alg_name, int alg_mode)
156 {
157         kb->kb_tfm = ll_crypto_alloc_blkcipher(alg_name, alg_mode, 0);
158         if (kb->kb_tfm == NULL) {
159                 CERROR("failed to alloc tfm: %s, mode %d\n",
160                        alg_name, alg_mode);
161                 return -1;
162         }
163
164         if (ll_crypto_blkcipher_setkey(kb->kb_tfm, kb->kb_key.data, kb->kb_key.len)) {
165                 CERROR("failed to set %s key, len %d\n",
166                        alg_name, kb->kb_key.len);
167                 return -1;
168         }
169
170         return 0;
171 }
172
173 static
174 int krb5_init_keys(struct krb5_ctx *kctx)
175 {
176         struct krb5_enctype *ke;
177
178         if (kctx->kc_enctype >= MAX_ENCTYPES ||
179             enctypes[kctx->kc_enctype].ke_hash_size == 0) {
180                 CERROR("unsupported enctype %x\n", kctx->kc_enctype);
181                 return -1;
182         }
183
184         ke = &enctypes[kctx->kc_enctype];
185
186         /* tfm arc4 is stateful, user should alloc-use-free by his own */
187         if (kctx->kc_enctype != ENCTYPE_ARCFOUR_HMAC &&
188             keyblock_init(&kctx->kc_keye, ke->ke_enc_name, ke->ke_enc_mode))
189                 return -1;
190
191         /* tfm hmac is stateful, user should alloc-use-free by his own */
192         if (ke->ke_hash_hmac == 0 &&
193             keyblock_init(&kctx->kc_keyi, ke->ke_enc_name, ke->ke_enc_mode))
194                 return -1;
195         if (ke->ke_hash_hmac == 0 &&
196             keyblock_init(&kctx->kc_keyc, ke->ke_enc_name, ke->ke_enc_mode))
197                 return -1;
198
199         return 0;
200 }
201
202 static
203 void keyblock_free(struct krb5_keyblock *kb)
204 {
205         rawobj_free(&kb->kb_key);
206         if (kb->kb_tfm)
207                 ll_crypto_free_blkcipher(kb->kb_tfm);
208 }
209
210 static
211 int keyblock_dup(struct krb5_keyblock *new, struct krb5_keyblock *kb)
212 {
213         return rawobj_dup(&new->kb_key, &kb->kb_key);
214 }
215
216 static
217 int get_bytes(char **ptr, const char *end, void *res, int len)
218 {
219         char *p, *q;
220         p = *ptr;
221         q = p + len;
222         if (q > end || q < p)
223                 return -1;
224         memcpy(res, p, len);
225         *ptr = q;
226         return 0;
227 }
228
229 static
230 int get_rawobj(char **ptr, const char *end, rawobj_t *res)
231 {
232         char   *p, *q;
233         __u32   len;
234
235         p = *ptr;
236         if (get_bytes(&p, end, &len, sizeof(len)))
237                 return -1;
238
239         q = p + len;
240         if (q > end || q < p)
241                 return -1;
242
243         OBD_ALLOC(res->data, len);
244         if (!res->data)
245                 return -1;
246
247         res->len = len;
248         memcpy(res->data, p, len);
249         *ptr = q;
250         return 0;
251 }
252
253 static
254 int get_keyblock(char **ptr, const char *end,
255                  struct krb5_keyblock *kb, __u32 keysize)
256 {
257         char *buf;
258
259         OBD_ALLOC(buf, keysize);
260         if (buf == NULL)
261                 return -1;
262
263         if (get_bytes(ptr, end, buf, keysize)) {
264                 OBD_FREE(buf, keysize);
265                 return -1;
266         }
267
268         kb->kb_key.len = keysize;
269         kb->kb_key.data = buf;
270         return 0;
271 }
272
273 static
274 void delete_context_kerberos(struct krb5_ctx *kctx)
275 {
276         rawobj_free(&kctx->kc_mech_used);
277
278         keyblock_free(&kctx->kc_keye);
279         keyblock_free(&kctx->kc_keyi);
280         keyblock_free(&kctx->kc_keyc);
281 }
282
283 static
284 __u32 import_context_rfc1964(struct krb5_ctx *kctx, char *p, char *end)
285 {
286         unsigned int    tmp_uint, keysize;
287
288         /* seed_init flag */
289         if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
290                 goto out_err;
291         kctx->kc_seed_init = (tmp_uint != 0);
292
293         /* seed */
294         if (get_bytes(&p, end, kctx->kc_seed, sizeof(kctx->kc_seed)))
295                 goto out_err;
296
297         /* sign/seal algorithm, not really used now */
298         if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)) ||
299             get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
300                 goto out_err;
301
302         /* end time */
303         if (get_bytes(&p, end, &kctx->kc_endtime, sizeof(kctx->kc_endtime)))
304                 goto out_err;
305
306         /* seq send */
307         if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
308                 goto out_err;
309         kctx->kc_seq_send = tmp_uint;
310
311         /* mech oid */
312         if (get_rawobj(&p, end, &kctx->kc_mech_used))
313                 goto out_err;
314
315         /* old style enc/seq keys in format:
316          *   - enctype (u32)
317          *   - keysize (u32)
318          *   - keydata
319          * we decompose them to fit into the new context
320          */
321
322         /* enc key */
323         if (get_bytes(&p, end, &kctx->kc_enctype, sizeof(kctx->kc_enctype)))
324                 goto out_err;
325
326         if (get_bytes(&p, end, &keysize, sizeof(keysize)))
327                 goto out_err;
328
329         if (get_keyblock(&p, end, &kctx->kc_keye, keysize))
330                 goto out_err;
331
332         /* seq key */
333         if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)) ||
334             tmp_uint != kctx->kc_enctype)
335                 goto out_err;
336
337         if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)) ||
338             tmp_uint != keysize)
339                 goto out_err;
340
341         if (get_keyblock(&p, end, &kctx->kc_keyc, keysize))
342                 goto out_err;
343
344         /* old style fallback */
345         if (keyblock_dup(&kctx->kc_keyi, &kctx->kc_keyc))
346                 goto out_err;
347
348         if (p != end)
349                 goto out_err;
350
351         CDEBUG(D_SEC, "succesfully imported rfc1964 context\n");
352         return 0;
353 out_err:
354         return GSS_S_FAILURE;
355 }
356
357 /* Flags for version 2 context flags */
358 #define KRB5_CTX_FLAG_INITIATOR         0x00000001
359 #define KRB5_CTX_FLAG_CFX               0x00000002
360 #define KRB5_CTX_FLAG_ACCEPTOR_SUBKEY   0x00000004
361
362 static
363 __u32 import_context_rfc4121(struct krb5_ctx *kctx, char *p, char *end)
364 {
365         unsigned int    tmp_uint, keysize;
366
367         /* end time */
368         if (get_bytes(&p, end, &kctx->kc_endtime, sizeof(kctx->kc_endtime)))
369                 goto out_err;
370
371         /* flags */
372         if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
373                 goto out_err;
374
375         if (tmp_uint & KRB5_CTX_FLAG_INITIATOR)
376                 kctx->kc_initiate = 1;
377         if (tmp_uint & KRB5_CTX_FLAG_CFX)
378                 kctx->kc_cfx = 1;
379         if (tmp_uint & KRB5_CTX_FLAG_ACCEPTOR_SUBKEY)
380                 kctx->kc_have_acceptor_subkey = 1;
381
382         /* seq send */
383         if (get_bytes(&p, end, &kctx->kc_seq_send, sizeof(kctx->kc_seq_send)))
384                 goto out_err;
385
386         /* enctype */
387         if (get_bytes(&p, end, &kctx->kc_enctype, sizeof(kctx->kc_enctype)))
388                 goto out_err;
389
390         /* size of each key */
391         if (get_bytes(&p, end, &keysize, sizeof(keysize)))
392                 goto out_err;
393
394         /* number of keys - should always be 3 */
395         if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
396                 goto out_err;
397
398         if (tmp_uint != 3) {
399                 CERROR("Invalid number of keys: %u\n", tmp_uint);
400                 goto out_err;
401         }
402
403         /* ke */
404         if (get_keyblock(&p, end, &kctx->kc_keye, keysize))
405                 goto out_err;
406         /* ki */
407         if (get_keyblock(&p, end, &kctx->kc_keyi, keysize))
408                 goto out_err;
409         /* ki */
410         if (get_keyblock(&p, end, &kctx->kc_keyc, keysize))
411                 goto out_err;
412
413         CDEBUG(D_SEC, "succesfully imported v2 context\n");
414         return 0;
415 out_err:
416         return GSS_S_FAILURE;
417 }
418
419 /*
420  * The whole purpose here is trying to keep user level gss context parsing
421  * from nfs-utils unchanged as possible as we can, they are not quite mature
422  * yet, and many stuff still not clear, like heimdal etc.
423  */
424 static
425 __u32 gss_import_sec_context_kerberos(rawobj_t *inbuf,
426                                       struct gss_ctx *gctx)
427 {
428         struct krb5_ctx *kctx;
429         char            *p = (char *) inbuf->data;
430         char            *end = (char *) (inbuf->data + inbuf->len);
431         unsigned int     tmp_uint, rc;
432
433         if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint))) {
434                 CERROR("Fail to read version\n");
435                 return GSS_S_FAILURE;
436         }
437
438         /* only support 0, 1 for the moment */
439         if (tmp_uint > 2) {
440                 CERROR("Invalid version %u\n", tmp_uint);
441                 return GSS_S_FAILURE;
442         }
443
444         OBD_ALLOC_PTR(kctx);
445         if (!kctx)
446                 return GSS_S_FAILURE;
447
448         if (tmp_uint == 0 || tmp_uint == 1) {
449                 kctx->kc_initiate = tmp_uint;
450                 rc = import_context_rfc1964(kctx, p, end);
451         } else {
452                 rc = import_context_rfc4121(kctx, p, end);
453         }
454
455         if (rc == 0)
456                 rc = krb5_init_keys(kctx);
457
458         if (rc) {
459                 delete_context_kerberos(kctx);
460                 OBD_FREE_PTR(kctx);
461
462                 return GSS_S_FAILURE;
463         }
464
465         gctx->internal_ctx_id = kctx;
466         return GSS_S_COMPLETE;
467 }
468
469 static
470 __u32 gss_copy_reverse_context_kerberos(struct gss_ctx *gctx,
471                                         struct gss_ctx *gctx_new)
472 {
473         struct krb5_ctx *kctx = gctx->internal_ctx_id;
474         struct krb5_ctx *knew;
475
476         OBD_ALLOC_PTR(knew);
477         if (!knew)
478                 return GSS_S_FAILURE;
479
480         knew->kc_initiate = kctx->kc_initiate ? 0 : 1;
481         knew->kc_cfx = kctx->kc_cfx;
482         knew->kc_seed_init = kctx->kc_seed_init;
483         knew->kc_have_acceptor_subkey = kctx->kc_have_acceptor_subkey;
484         knew->kc_endtime = kctx->kc_endtime;
485
486         memcpy(knew->kc_seed, kctx->kc_seed, sizeof(kctx->kc_seed));
487         knew->kc_seq_send = kctx->kc_seq_recv;
488         knew->kc_seq_recv = kctx->kc_seq_send;
489         knew->kc_enctype = kctx->kc_enctype;
490
491         if (rawobj_dup(&knew->kc_mech_used, &kctx->kc_mech_used))
492                 goto out_err;
493
494         if (keyblock_dup(&knew->kc_keye, &kctx->kc_keye))
495                 goto out_err;
496         if (keyblock_dup(&knew->kc_keyi, &kctx->kc_keyi))
497                 goto out_err;
498         if (keyblock_dup(&knew->kc_keyc, &kctx->kc_keyc))
499                 goto out_err;
500         if (krb5_init_keys(knew))
501                 goto out_err;
502
503         gctx_new->internal_ctx_id = knew;
504         CDEBUG(D_SEC, "succesfully copied reverse context\n");
505         return GSS_S_COMPLETE;
506
507 out_err:
508         delete_context_kerberos(knew);
509         OBD_FREE_PTR(knew);
510         return GSS_S_FAILURE;
511 }
512
513 static
514 __u32 gss_inquire_context_kerberos(struct gss_ctx *gctx,
515                                    unsigned long  *endtime)
516 {
517         struct krb5_ctx *kctx = gctx->internal_ctx_id;
518
519         *endtime = (unsigned long) ((__u32) kctx->kc_endtime);
520         return GSS_S_COMPLETE;
521 }
522
523 static
524 void gss_delete_sec_context_kerberos(void *internal_ctx)
525 {
526         struct krb5_ctx *kctx = internal_ctx;
527
528         delete_context_kerberos(kctx);
529         OBD_FREE_PTR(kctx);
530 }
531
532 static
533 void buf_to_sg(struct scatterlist *sg, char *ptr, int len)
534 {
535         sg->page = virt_to_page(ptr);
536         sg->offset = offset_in_page(ptr);
537         sg->length = len;
538 }
539
540 static
541 __u32 krb5_encrypt(struct ll_crypto_cipher *tfm,
542                    int decrypt,
543                    void * iv,
544                    void * in,
545                    void * out,
546                    int length)
547 {
548         struct blkcipher_desc desc;
549         struct scatterlist    sg;
550         __u8 local_iv[16] = {0};
551         __u32 ret = -EINVAL;
552
553         LASSERT(tfm);
554         desc.tfm  = tfm;
555         desc.info = local_iv;
556         desc.flags= 0;
557
558         if (length % ll_crypto_blkcipher_blocksize(tfm) != 0) {
559                 CERROR("output length %d mismatch blocksize %d\n",
560                        length, ll_crypto_blkcipher_blocksize(tfm));
561                 goto out;
562         }
563
564         if (ll_crypto_blkcipher_ivsize(tfm) > 16) {
565                 CERROR("iv size too large %d\n", ll_crypto_blkcipher_ivsize(tfm));
566                 goto out;
567         }
568
569         if (iv)
570                 memcpy(local_iv, iv, ll_crypto_blkcipher_ivsize(tfm));
571
572         memcpy(out, in, length);
573         buf_to_sg(&sg, out, length);
574
575         if (decrypt)
576                 ret = ll_crypto_blkcipher_decrypt_iv(&desc, &sg, &sg, length);
577         else
578                 ret = ll_crypto_blkcipher_encrypt_iv(&desc, &sg, &sg, length);
579
580 out:
581         return(ret);
582 }
583
584 static inline
585 int krb5_digest_hmac(struct ll_crypto_hash *tfm,
586                      rawobj_t *key,
587                      struct krb5_header *khdr,
588                      int msgcnt, rawobj_t *msgs,
589                      rawobj_t *cksum)
590 #ifdef HAVE_ASYNC_BLOCK_CIPHER
591 {
592         struct hash_desc   desc;
593         struct scatterlist sg[1];
594         int                i;
595
596         ll_crypto_hash_setkey(tfm, key->data, key->len);
597         desc.tfm  = tfm;
598         desc.flags= 0;
599
600         ll_crypto_hash_init(&desc);
601
602         for (i = 0; i < msgcnt; i++) {
603                 if (msgs[i].len == 0)
604                         continue;
605                 buf_to_sg(sg, (char *) msgs[i].data, msgs[i].len);
606                 ll_crypto_hash_update(&desc, sg, msgs[i].len);
607         }
608
609         if (khdr) {
610                 buf_to_sg(sg, (char *) khdr, sizeof(*khdr));
611                 ll_crypto_hash_update(&desc, sg, sizeof(*khdr));
612         }
613
614         return ll_crypto_hash_final(&desc, cksum->data);
615 }
616 #else /* HAVE_ASYNC_BLOCK_CIPHER */
617 {
618         struct scatterlist sg[1];
619         __u32              keylen = key->len, i;
620
621         crypto_hmac_init(tfm, key->data, &keylen);
622
623         for (i = 0; i < msgcnt; i++) {
624                 if (msgs[i].len == 0)
625                         continue;
626                 buf_to_sg(sg, (char *) msgs[i].data, msgs[i].len);
627                 crypto_hmac_update(tfm, sg, 1);
628         }
629
630         if (khdr) {
631                 buf_to_sg(sg, (char *) khdr, sizeof(*khdr));
632                 crypto_hmac_update(tfm, sg, 1);
633         }
634
635         crypto_hmac_final(tfm, key->data, &keylen, cksum->data);
636         return 0;
637 }
638 #endif /* HAVE_ASYNC_BLOCK_CIPHER */
639
640 static inline
641 int krb5_digest_norm(struct ll_crypto_hash *tfm,
642                      struct krb5_keyblock *kb,
643                      struct krb5_header *khdr,
644                      int msgcnt, rawobj_t *msgs,
645                      rawobj_t *cksum)
646 {
647         struct hash_desc   desc;
648         struct scatterlist sg[1];
649         int                i;
650
651         LASSERT(kb->kb_tfm);
652         desc.tfm  = tfm;
653         desc.flags= 0;
654
655         ll_crypto_hash_init(&desc);
656
657         for (i = 0; i < msgcnt; i++) {
658                 if (msgs[i].len == 0)
659                         continue;
660                 buf_to_sg(sg, (char *) msgs[i].data, msgs[i].len);
661                 ll_crypto_hash_update(&desc, sg, msgs[i].len);
662         }
663
664         if (khdr) {
665                 buf_to_sg(sg, (char *) khdr, sizeof(*khdr));
666                 ll_crypto_hash_update(&desc, sg, sizeof(*khdr));
667         }
668
669         ll_crypto_hash_final(&desc, cksum->data);
670
671         return krb5_encrypt(kb->kb_tfm, 0, NULL, cksum->data,
672                             cksum->data, cksum->len);
673 }
674
675 /*
676  * compute (keyed/keyless) checksum against the plain text which appended
677  * with krb5 wire token header.
678  */
679 static
680 __s32 krb5_make_checksum(__u32 enctype,
681                          struct krb5_keyblock *kb,
682                          struct krb5_header *khdr,
683                          int msgcnt, rawobj_t *msgs,
684                          rawobj_t *cksum)
685 {
686         struct krb5_enctype   *ke = &enctypes[enctype];
687         struct ll_crypto_hash *tfm;
688         __u32                  code = GSS_S_FAILURE;
689         int                    rc;
690
691         if (!(tfm = ll_crypto_alloc_hash(ke->ke_hash_name, 0, 0))) {
692                 CERROR("failed to alloc TFM: %s\n", ke->ke_hash_name);
693                 return GSS_S_FAILURE;
694         }
695
696         cksum->len = ll_crypto_hash_digestsize(tfm);
697         OBD_ALLOC(cksum->data, cksum->len);
698         if (!cksum->data) {
699                 cksum->len = 0;
700                 goto out_tfm;
701         }
702
703         if (ke->ke_hash_hmac)
704                 rc = krb5_digest_hmac(tfm, &kb->kb_key,
705                                       khdr, msgcnt, msgs, cksum);
706         else
707                 rc = krb5_digest_norm(tfm, kb,
708                                       khdr, msgcnt, msgs, cksum);
709
710         if (rc == 0)
711                 code = GSS_S_COMPLETE;
712 out_tfm:
713         ll_crypto_free_hash(tfm);
714         return code;
715 }
716
717 static
718 __u32 gss_get_mic_kerberos(struct gss_ctx *gctx,
719                            int msgcnt,
720                            rawobj_t *msgs,
721                            rawobj_t *token)
722 {
723         struct krb5_ctx     *kctx = gctx->internal_ctx_id;
724         struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
725         struct krb5_header  *khdr;
726         unsigned char        acceptor_flag;
727         rawobj_t             cksum = RAWOBJ_EMPTY;
728         __u32                rc = GSS_S_FAILURE;
729
730         acceptor_flag = kctx->kc_initiate ? 0 : FLAG_SENDER_IS_ACCEPTOR;
731
732         /* fill krb5 header */
733         LASSERT(token->len >= sizeof(*khdr));
734         khdr = (struct krb5_header *) token->data;
735
736         khdr->kh_tok_id = cpu_to_be16(KG_TOK_MIC_MSG);
737         khdr->kh_flags = acceptor_flag;
738         khdr->kh_filler = 0xff;
739         khdr->kh_ec = cpu_to_be16(0xffff);
740         khdr->kh_rrc = cpu_to_be16(0xffff);
741         spin_lock(&krb5_seq_lock);
742         khdr->kh_seq = cpu_to_be64(kctx->kc_seq_send++);
743         spin_unlock(&krb5_seq_lock);
744
745         /* checksum */
746         if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyc,
747                                khdr, msgcnt, msgs, &cksum))
748                 goto out_err;
749
750         LASSERT(cksum.len >= ke->ke_hash_size);
751         LASSERT(token->len >= sizeof(*khdr) + ke->ke_hash_size);
752         memcpy(khdr + 1, cksum.data + cksum.len - ke->ke_hash_size,
753                ke->ke_hash_size);
754
755         token->len = sizeof(*khdr) + ke->ke_hash_size;
756         rc = GSS_S_COMPLETE;
757 out_err:
758         rawobj_free(&cksum);
759         return rc;
760 }
761
762 static
763 __u32 gss_verify_mic_kerberos(struct gss_ctx *gctx,
764                               int msgcnt,
765                               rawobj_t *msgs,
766                               rawobj_t *token)
767 {
768         struct krb5_ctx     *kctx = gctx->internal_ctx_id;
769         struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
770         struct krb5_header  *khdr;
771         unsigned char        acceptor_flag;
772         rawobj_t             cksum = RAWOBJ_EMPTY;
773         __u32                rc = GSS_S_FAILURE;
774
775         acceptor_flag = kctx->kc_initiate ? FLAG_SENDER_IS_ACCEPTOR : 0;
776
777         if (token->len < sizeof(*khdr)) {
778                 CERROR("short signature: %u\n", token->len);
779                 return GSS_S_DEFECTIVE_TOKEN;
780         }
781
782         khdr = (struct krb5_header *) token->data;
783
784         /* sanity checks */
785         if (be16_to_cpu(khdr->kh_tok_id) != KG_TOK_MIC_MSG) {
786                 CERROR("bad token id\n");
787                 return GSS_S_DEFECTIVE_TOKEN;
788         }
789         if ((khdr->kh_flags & FLAG_SENDER_IS_ACCEPTOR) != acceptor_flag) {
790                 CERROR("bad direction flag\n");
791                 return GSS_S_BAD_SIG;
792         }
793         if (khdr->kh_filler != 0xff) {
794                 CERROR("bad filler\n");
795                 return GSS_S_DEFECTIVE_TOKEN;
796         }
797         if (be16_to_cpu(khdr->kh_ec) != 0xffff ||
798             be16_to_cpu(khdr->kh_rrc) != 0xffff) {
799                 CERROR("bad EC or RRC\n");
800                 return GSS_S_DEFECTIVE_TOKEN;
801         }
802
803         if (token->len < sizeof(*khdr) + ke->ke_hash_size) {
804                 CERROR("short signature: %u, require %d\n",
805                        token->len, (int) sizeof(*khdr) + ke->ke_hash_size);
806                 goto out;
807         }
808
809         if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyc,
810                                khdr, msgcnt, msgs, &cksum))
811                 return GSS_S_FAILURE;
812
813         LASSERT(cksum.len >= ke->ke_hash_size);
814         if (memcmp(khdr + 1, cksum.data + cksum.len - ke->ke_hash_size,
815                    ke->ke_hash_size)) {
816                 CERROR("checksum mismatch\n");
817                 rc = GSS_S_BAD_SIG;
818                 goto out;
819         }
820
821         rc = GSS_S_COMPLETE;
822 out:
823         rawobj_free(&cksum);
824         return rc;
825 }
826
827 static
828 int add_padding(rawobj_t *msg, int msg_buflen, int blocksize)
829 {
830         int padding;
831
832         padding = (blocksize - (msg->len & (blocksize - 1))) &
833                   (blocksize - 1);
834         if (!padding)
835                 return 0;
836
837         if (msg->len + padding > msg_buflen) {
838                 CERROR("bufsize %u too small: datalen %u, padding %u\n",
839                         msg_buflen, msg->len, padding);
840                 return -EINVAL;
841         }
842
843         memset(msg->data + msg->len, padding, padding);
844         msg->len += padding;
845         return 0;
846 }
847
848 static
849 int krb5_encrypt_rawobjs(struct ll_crypto_cipher *tfm,
850                          int mode_ecb,
851                          int inobj_cnt,
852                          rawobj_t *inobjs,
853                          rawobj_t *outobj,
854                          int enc)
855 {
856         struct blkcipher_desc desc;
857         struct scatterlist    src, dst;
858         __u8                  local_iv[16] = {0}, *buf;
859         __u32                 datalen = 0;
860         int                   i, rc;
861         ENTRY;
862
863         buf = outobj->data;
864         desc.tfm  = tfm;
865         desc.info = local_iv;
866         desc.flags = 0;
867
868         for (i = 0; i < inobj_cnt; i++) {
869                 LASSERT(buf + inobjs[i].len <= outobj->data + outobj->len);
870
871                 buf_to_sg(&src, inobjs[i].data, inobjs[i].len);
872                 buf_to_sg(&dst, buf, outobj->len - datalen);
873
874                 if (mode_ecb) {
875                         if (enc)
876                                 rc = ll_crypto_blkcipher_encrypt(
877                                         &desc, &dst, &src, src.length);
878                         else
879                                 rc = ll_crypto_blkcipher_decrypt(
880                                         &desc, &dst, &src, src.length);
881                 } else {
882                         if (enc)
883                                 rc = ll_crypto_blkcipher_encrypt_iv(
884                                         &desc, &dst, &src, src.length);
885                         else
886                                 rc = ll_crypto_blkcipher_decrypt_iv(
887                                         &desc, &dst, &src, src.length);
888                 }
889
890                 if (rc) {
891                         CERROR("encrypt error %d\n", rc);
892                         RETURN(rc);
893                 }
894
895                 datalen += inobjs[i].len;
896                 buf += inobjs[i].len;
897         }
898
899         outobj->len = datalen;
900         RETURN(0);
901 }
902
903 static
904 __u32 gss_wrap_kerberos(struct gss_ctx *gctx,
905                         rawobj_t *msg,
906                         int msg_buflen,
907                         rawobj_t *token)
908 {
909         struct krb5_ctx     *kctx = gctx->internal_ctx_id;
910         struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
911         struct krb5_header  *khdr;
912         unsigned char        acceptor_flag;
913         int                  blocksize;
914         rawobj_t             cksum = RAWOBJ_EMPTY;
915         rawobj_t             data_desc[3], cipher;
916         __u8                 conf[GSS_MAX_CIPHER_BLOCK];
917         int                  enc_rc = 0;
918
919         LASSERT(ke);
920         LASSERT(ke->ke_conf_size <= GSS_MAX_CIPHER_BLOCK);
921         LASSERT(kctx->kc_keye.kb_tfm == NULL ||
922                 ke->ke_conf_size >=
923                 ll_crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm));
924
925         acceptor_flag = kctx->kc_initiate ? 0 : FLAG_SENDER_IS_ACCEPTOR;
926
927         /* fill krb5 header */
928         LASSERT(token->len >= sizeof(*khdr));
929         khdr = (struct krb5_header *) token->data;
930
931         khdr->kh_tok_id = cpu_to_be16(KG_TOK_WRAP_MSG);
932         khdr->kh_flags = acceptor_flag | FLAG_WRAP_CONFIDENTIAL;
933         khdr->kh_filler = 0xff;
934         khdr->kh_ec = cpu_to_be16(0);
935         khdr->kh_rrc = cpu_to_be16(0);
936         spin_lock(&krb5_seq_lock);
937         khdr->kh_seq = cpu_to_be64(kctx->kc_seq_send++);
938         spin_unlock(&krb5_seq_lock);
939
940         /* generate confounder */
941         get_random_bytes(conf, ke->ke_conf_size);
942
943         /* get encryption blocksize. note kc_keye might not associated with
944          * a tfm, currently only for arcfour-hmac */
945         if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
946                 LASSERT(kctx->kc_keye.kb_tfm == NULL);
947                 blocksize = 1;
948         } else {
949                 LASSERT(kctx->kc_keye.kb_tfm);
950                 blocksize = ll_crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
951         }
952         LASSERT(blocksize <= ke->ke_conf_size);
953
954         /* padding the message */
955         if (add_padding(msg, msg_buflen, blocksize))
956                 return GSS_S_FAILURE;
957
958         /*
959          * clear text layout, same for both checksum & encryption:
960          * -----------------------------------------
961          * | confounder | clear msgs | krb5 header |
962          * -----------------------------------------
963          */
964         data_desc[0].data = conf;
965         data_desc[0].len = ke->ke_conf_size;
966         data_desc[1].data = msg->data;
967         data_desc[1].len = msg->len;
968         data_desc[2].data = (__u8 *) khdr;
969         data_desc[2].len = sizeof(*khdr);
970
971         /* compute checksum */
972         if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyi,
973                                khdr, 3, data_desc, &cksum))
974                 return GSS_S_FAILURE;
975         LASSERT(cksum.len >= ke->ke_hash_size);
976
977         /* encrypting, cipher text will be directly inplace */
978         cipher.data = (__u8 *) (khdr + 1);
979         cipher.len = token->len - sizeof(*khdr);
980         LASSERT(cipher.len >= ke->ke_conf_size + msg->len + sizeof(*khdr));
981
982         if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
983                 rawobj_t                 arc4_keye;
984                 struct ll_crypto_cipher *arc4_tfm;
985
986                 if (krb5_make_checksum(ENCTYPE_ARCFOUR_HMAC, &kctx->kc_keyi,
987                                        NULL, 1, &cksum, &arc4_keye)) {
988                         CERROR("failed to obtain arc4 enc key\n");
989                         GOTO(arc4_out, enc_rc = -EACCES);
990                 }
991
992                 arc4_tfm = ll_crypto_alloc_blkcipher("ecb(arc4)", 0, 0);
993                 if (arc4_tfm == NULL) {
994                         CERROR("failed to alloc tfm arc4 in ECB mode\n");
995                         GOTO(arc4_out_key, enc_rc = -EACCES);
996                 }
997
998                 if (ll_crypto_blkcipher_setkey(arc4_tfm, arc4_keye.data,
999                                                arc4_keye.len)) {
1000                         CERROR("failed to set arc4 key, len %d\n",
1001                                arc4_keye.len);
1002                         GOTO(arc4_out_tfm, enc_rc = -EACCES);
1003                 }
1004
1005                 enc_rc = krb5_encrypt_rawobjs(arc4_tfm, 1,
1006                                               3, data_desc, &cipher, 1);
1007 arc4_out_tfm:
1008                 ll_crypto_free_blkcipher(arc4_tfm);
1009 arc4_out_key:
1010                 rawobj_free(&arc4_keye);
1011 arc4_out:
1012                 do {} while(0); /* just to avoid compile warning */
1013         } else {
1014                 enc_rc = krb5_encrypt_rawobjs(kctx->kc_keye.kb_tfm, 0,
1015                                               3, data_desc, &cipher, 1);
1016         }
1017
1018         if (enc_rc != 0) {
1019                 rawobj_free(&cksum);
1020                 return GSS_S_FAILURE;
1021         }
1022
1023         /* fill in checksum */
1024         LASSERT(token->len >= sizeof(*khdr) + cipher.len + ke->ke_hash_size);
1025         memcpy((char *)(khdr + 1) + cipher.len,
1026                cksum.data + cksum.len - ke->ke_hash_size,
1027                ke->ke_hash_size);
1028         rawobj_free(&cksum);
1029
1030         /* final token length */
1031         token->len = sizeof(*khdr) + cipher.len + ke->ke_hash_size;
1032         return GSS_S_COMPLETE;
1033 }
1034
1035 static
1036 __u32 gss_unwrap_kerberos(struct gss_ctx  *gctx,
1037                           rawobj_t        *token,
1038                           rawobj_t        *msg)
1039 {
1040         struct krb5_ctx     *kctx = gctx->internal_ctx_id;
1041         struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
1042         struct krb5_header  *khdr;
1043         unsigned char        acceptor_flag;
1044         unsigned char       *tmpbuf;
1045         int                  blocksize, bodysize;
1046         rawobj_t             cksum = RAWOBJ_EMPTY;
1047         rawobj_t             cipher_in, plain_out;
1048         __u32                rc = GSS_S_FAILURE, enc_rc = 0;
1049
1050         LASSERT(ke);
1051
1052         acceptor_flag = kctx->kc_initiate ? FLAG_SENDER_IS_ACCEPTOR : 0;
1053
1054         if (token->len < sizeof(*khdr)) {
1055                 CERROR("short signature: %u\n", token->len);
1056                 return GSS_S_DEFECTIVE_TOKEN;
1057         }
1058
1059         khdr = (struct krb5_header *) token->data;
1060
1061         /* sanity check header */
1062         if (be16_to_cpu(khdr->kh_tok_id) != KG_TOK_WRAP_MSG) {
1063                 CERROR("bad token id\n");
1064                 return GSS_S_DEFECTIVE_TOKEN;
1065         }
1066         if ((khdr->kh_flags & FLAG_SENDER_IS_ACCEPTOR) != acceptor_flag) {
1067                 CERROR("bad direction flag\n");
1068                 return GSS_S_BAD_SIG;
1069         }
1070         if ((khdr->kh_flags & FLAG_WRAP_CONFIDENTIAL) == 0) {
1071                 CERROR("missing confidential flag\n");
1072                 return GSS_S_BAD_SIG;
1073         }
1074         if (khdr->kh_filler != 0xff) {
1075                 CERROR("bad filler\n");
1076                 return GSS_S_DEFECTIVE_TOKEN;
1077         }
1078         if (be16_to_cpu(khdr->kh_ec) != 0x0 ||
1079             be16_to_cpu(khdr->kh_rrc) != 0x0) {
1080                 CERROR("bad EC or RRC\n");
1081                 return GSS_S_DEFECTIVE_TOKEN;
1082         }
1083
1084         /* block size */
1085         if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1086                 LASSERT(kctx->kc_keye.kb_tfm == NULL);
1087                 blocksize = 1;
1088         } else {
1089                 LASSERT(kctx->kc_keye.kb_tfm);
1090                 blocksize = ll_crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
1091         }
1092
1093         /* expected token layout:
1094          * ----------------------------------------
1095          * | krb5 header | cipher text | checksum |
1096          * ----------------------------------------
1097          */
1098         bodysize = token->len - sizeof(*khdr) - ke->ke_hash_size;
1099
1100         if (bodysize % blocksize) {
1101                 CERROR("odd bodysize %d\n", bodysize);
1102                 return GSS_S_DEFECTIVE_TOKEN;
1103         }
1104
1105         if (bodysize <= ke->ke_conf_size + sizeof(*khdr)) {
1106                 CERROR("incomplete token: bodysize %d\n", bodysize);
1107                 return GSS_S_DEFECTIVE_TOKEN;
1108         }
1109
1110         if (msg->len < bodysize - ke->ke_conf_size - sizeof(*khdr)) {
1111                 CERROR("buffer too small: %u, require %d\n",
1112                        msg->len, bodysize - ke->ke_conf_size);
1113                 return GSS_S_FAILURE;
1114         }
1115
1116         /* decrypting */
1117         OBD_ALLOC(tmpbuf, bodysize);
1118         if (!tmpbuf)
1119                 return GSS_S_FAILURE;
1120
1121         cipher_in.data = (__u8 *) (khdr + 1);
1122         cipher_in.len = bodysize;
1123         plain_out.data = tmpbuf;
1124         plain_out.len = bodysize;
1125
1126         if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1127                 rawobj_t                 arc4_keye;
1128                 struct ll_crypto_cipher *arc4_tfm;
1129
1130                 cksum.data = token->data + token->len - ke->ke_hash_size;
1131                 cksum.len = ke->ke_hash_size;
1132
1133                 if (krb5_make_checksum(ENCTYPE_ARCFOUR_HMAC, &kctx->kc_keyi,
1134                                        NULL, 1, &cksum, &arc4_keye)) {
1135                         CERROR("failed to obtain arc4 enc key\n");
1136                         GOTO(arc4_out, enc_rc = -EACCES);
1137                 }
1138
1139                 arc4_tfm = ll_crypto_alloc_blkcipher("ecb(arc4)", 0, 0);
1140                 if (arc4_tfm == NULL) {
1141                         CERROR("failed to alloc tfm arc4 in ECB mode\n");
1142                         GOTO(arc4_out_key, enc_rc = -EACCES);
1143                 }
1144
1145                 if (ll_crypto_blkcipher_setkey(arc4_tfm,
1146                                          arc4_keye.data, arc4_keye.len)) {
1147                         CERROR("failed to set arc4 key, len %d\n",
1148                                arc4_keye.len);
1149                         GOTO(arc4_out_tfm, enc_rc = -EACCES);
1150                 }
1151
1152                 enc_rc = krb5_encrypt_rawobjs(arc4_tfm, 1,
1153                                               1, &cipher_in, &plain_out, 0);
1154 arc4_out_tfm:
1155                 ll_crypto_free_blkcipher(arc4_tfm);
1156 arc4_out_key:
1157                 rawobj_free(&arc4_keye);
1158 arc4_out:
1159                 cksum = RAWOBJ_EMPTY;
1160         } else {
1161                 enc_rc = krb5_encrypt_rawobjs(kctx->kc_keye.kb_tfm, 0,
1162                                               1, &cipher_in, &plain_out, 0);
1163         }
1164
1165         if (enc_rc != 0) {
1166                 CERROR("error decrypt\n");
1167                 goto out_free;
1168         }
1169         LASSERT(plain_out.len == bodysize);
1170
1171         /* expected clear text layout:
1172          * -----------------------------------------
1173          * | confounder | clear msgs | krb5 header |
1174          * -----------------------------------------
1175          */
1176
1177         /* last part must be identical to the krb5 header */
1178         if (memcmp(khdr, plain_out.data + plain_out.len - sizeof(*khdr),
1179                    sizeof(*khdr))) {
1180                 CERROR("decrypted header mismatch\n");
1181                 goto out_free;
1182         }
1183
1184         /* verify checksum */
1185         if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyi,
1186                                khdr, 1, &plain_out, &cksum))
1187                 goto out_free;
1188
1189         LASSERT(cksum.len >= ke->ke_hash_size);
1190         if (memcmp((char *)(khdr + 1) + bodysize,
1191                    cksum.data + cksum.len - ke->ke_hash_size,
1192                    ke->ke_hash_size)) {
1193                 CERROR("cksum mismatch\n");
1194                 goto out_free;
1195         }
1196
1197         msg->len =  bodysize - ke->ke_conf_size - sizeof(*khdr);
1198         memcpy(msg->data, tmpbuf + ke->ke_conf_size, msg->len);
1199
1200         rc = GSS_S_COMPLETE;
1201 out_free:
1202         OBD_FREE(tmpbuf, bodysize);
1203         rawobj_free(&cksum);
1204         return rc;
1205 }
1206
1207 static
1208 __u32 gss_plain_encrypt_kerberos(struct gss_ctx  *ctx,
1209                                  int              decrypt,
1210                                  int              length,
1211                                  void            *in_buf,
1212                                  void            *out_buf)
1213 {
1214         struct krb5_ctx        *kctx = ctx->internal_ctx_id;
1215         __u32                   rc;
1216
1217         rc = krb5_encrypt(kctx->kc_keye.kb_tfm, decrypt,
1218                           NULL, in_buf, out_buf, length);
1219         if (rc)
1220                 CERROR("plain encrypt error: %d\n", rc);
1221
1222         return rc;
1223 }
1224
1225 int gss_display_kerberos(struct gss_ctx        *ctx,
1226                          char                  *buf,
1227                          int                    bufsize)
1228 {
1229         struct krb5_ctx    *kctx = ctx->internal_ctx_id;
1230         int                 written;
1231
1232         written = snprintf(buf, bufsize, "krb5 (%s)",
1233                            enctype2str(kctx->kc_enctype));
1234         return written;
1235 }
1236
1237 static struct gss_api_ops gss_kerberos_ops = {
1238         .gss_import_sec_context     = gss_import_sec_context_kerberos,
1239         .gss_copy_reverse_context   = gss_copy_reverse_context_kerberos,
1240         .gss_inquire_context        = gss_inquire_context_kerberos,
1241         .gss_get_mic                = gss_get_mic_kerberos,
1242         .gss_verify_mic             = gss_verify_mic_kerberos,
1243         .gss_wrap                   = gss_wrap_kerberos,
1244         .gss_unwrap                 = gss_unwrap_kerberos,
1245         .gss_plain_encrypt          = gss_plain_encrypt_kerberos,
1246         .gss_delete_sec_context     = gss_delete_sec_context_kerberos,
1247         .gss_display                = gss_display_kerberos,
1248 };
1249
1250 static struct subflavor_desc gss_kerberos_sfs[] = {
1251         {
1252                 .sf_subflavor   = SPTLRPC_SUBFLVR_KRB5N,
1253                 .sf_qop         = 0,
1254                 .sf_service     = SPTLRPC_SVC_NULL,
1255                 .sf_name        = "krb5n"
1256         },
1257         {
1258                 .sf_subflavor   = SPTLRPC_SUBFLVR_KRB5A,
1259                 .sf_qop         = 0,
1260                 .sf_service     = SPTLRPC_SVC_AUTH,
1261                 .sf_name        = "krb5a"
1262         },
1263         {
1264                 .sf_subflavor   = SPTLRPC_SUBFLVR_KRB5I,
1265                 .sf_qop         = 0,
1266                 .sf_service     = SPTLRPC_SVC_INTG,
1267                 .sf_name        = "krb5i"
1268         },
1269         {
1270                 .sf_subflavor   = SPTLRPC_SUBFLVR_KRB5P,
1271                 .sf_qop         = 0,
1272                 .sf_service     = SPTLRPC_SVC_PRIV,
1273                 .sf_name        = "krb5p"
1274         },
1275 };
1276
1277 /*
1278  * currently we leave module owner NULL
1279  */
1280 static struct gss_api_mech gss_kerberos_mech = {
1281         .gm_owner       = NULL, /*THIS_MODULE, */
1282         .gm_name        = "krb5",
1283         .gm_oid         = (rawobj_t)
1284                                 {9, "\052\206\110\206\367\022\001\002\002"},
1285         .gm_ops         = &gss_kerberos_ops,
1286         .gm_sf_num      = 4,
1287         .gm_sfs         = gss_kerberos_sfs,
1288 };
1289
1290 int __init init_kerberos_module(void)
1291 {
1292         int status;
1293
1294         status = lgss_mech_register(&gss_kerberos_mech);
1295         if (status)
1296                 CERROR("Failed to register kerberos gss mechanism!\n");
1297         return status;
1298 }
1299
1300 void __exit cleanup_kerberos_module(void)
1301 {
1302         lgss_mech_unregister(&gss_kerberos_mech);
1303 }