Whamcloud - gitweb
LU-5710 all: second batch of corrected typos and grammar errors
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_krb5_mech.c
1 /*
2  * Modifications for Lustre
3  *
4  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
5  *
6  * Copyright (c) 2011, 2014, Intel Corporation.
7  *
8  * Author: Eric Mei <ericm@clusterfs.com>
9  */
10
11 /*
12  *  linux/net/sunrpc/gss_krb5_mech.c
13  *  linux/net/sunrpc/gss_krb5_crypto.c
14  *  linux/net/sunrpc/gss_krb5_seal.c
15  *  linux/net/sunrpc/gss_krb5_seqnum.c
16  *  linux/net/sunrpc/gss_krb5_unseal.c
17  *
18  *  Copyright (c) 2001 The Regents of the University of Michigan.
19  *  All rights reserved.
20  *
21  *  Andy Adamson <andros@umich.edu>
22  *  J. Bruce Fields <bfields@umich.edu>
23  *
24  *  Redistribution and use in source and binary forms, with or without
25  *  modification, are permitted provided that the following conditions
26  *  are met:
27  *
28  *  1. Redistributions of source code must retain the above copyright
29  *     notice, this list of conditions and the following disclaimer.
30  *  2. Redistributions in binary form must reproduce the above copyright
31  *     notice, this list of conditions and the following disclaimer in the
32  *     documentation and/or other materials provided with the distribution.
33  *  3. Neither the name of the University nor the names of its
34  *     contributors may be used to endorse or promote products derived
35  *     from this software without specific prior written permission.
36  *
37  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
38  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
39  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
41  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
44  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
45  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
46  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
47  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  *
49  */
50
51 #define DEBUG_SUBSYSTEM S_SEC
52 #include <linux/init.h>
53 #include <linux/module.h>
54 #include <linux/slab.h>
55 #include <linux/crypto.h>
56 #include <linux/mutex.h>
57
58 #include <obd.h>
59 #include <obd_class.h>
60 #include <obd_support.h>
61 #include <lustre/lustre_idl.h>
62 #include <lustre_net.h>
63 #include <lustre_import.h>
64 #include <lustre_sec.h>
65
66 #include "gss_err.h"
67 #include "gss_internal.h"
68 #include "gss_api.h"
69 #include "gss_asn1.h"
70 #include "gss_krb5.h"
71
72 static spinlock_t krb5_seq_lock;
73
74 struct krb5_enctype {
75         char           *ke_dispname;
76         char           *ke_enc_name;            /* linux tfm name */
77         char           *ke_hash_name;           /* linux tfm name */
78         int             ke_enc_mode;            /* linux tfm mode */
79         int             ke_hash_size;           /* checksum size */
80         int             ke_conf_size;           /* confounder size */
81         unsigned int    ke_hash_hmac:1;         /* is hmac? */
82 };
83
84 /*
85  * NOTE: for aes128-cts and aes256-cts, MIT implementation use CTS encryption.
86  * but currently we simply CBC with padding, because linux doesn't support CTS
87  * yet. this need to be fixed in the future.
88  */
89 static struct krb5_enctype enctypes[] = {
90         [ENCTYPE_DES_CBC_RAW] = {               /* des-cbc-md5 */
91                 "des-cbc-md5",
92                 "cbc(des)",
93                 "md5",
94                 0,
95                 16,
96                 8,
97                 0,
98         },
99         [ENCTYPE_DES3_CBC_RAW] = {              /* des3-hmac-sha1 */
100                 "des3-hmac-sha1",
101                 "cbc(des3_ede)",
102                 "hmac(sha1)",
103                 0,
104                 20,
105                 8,
106                 1,
107         },
108         [ENCTYPE_AES128_CTS_HMAC_SHA1_96] = {   /* aes128-cts */
109                 "aes128-cts-hmac-sha1-96",
110                 "cbc(aes)",
111                 "hmac(sha1)",
112                 0,
113                 12,
114                 16,
115                 1,
116         },
117         [ENCTYPE_AES256_CTS_HMAC_SHA1_96] = {   /* aes256-cts */
118                 "aes256-cts-hmac-sha1-96",
119                 "cbc(aes)",
120                 "hmac(sha1)",
121                 0,
122                 12,
123                 16,
124                 1,
125         },
126         [ENCTYPE_ARCFOUR_HMAC] = {              /* arcfour-hmac-md5 */
127                 "arcfour-hmac-md5",
128                 "ecb(arc4)",
129                 "hmac(md5)",
130                 0,
131                 16,
132                 8,
133                 1,
134         },
135 };
136
137 #define MAX_ENCTYPES    sizeof(enctypes)/sizeof(struct krb5_enctype)
138
139 static const char * enctype2str(__u32 enctype)
140 {
141         if (enctype < MAX_ENCTYPES && enctypes[enctype].ke_dispname)
142                 return enctypes[enctype].ke_dispname;
143
144         return "unknown";
145 }
146
147 static
148 int keyblock_init(struct krb5_keyblock *kb, char *alg_name, int alg_mode)
149 {
150         kb->kb_tfm = crypto_alloc_blkcipher(alg_name, alg_mode, 0);
151         if (IS_ERR(kb->kb_tfm)) {
152                 CERROR("failed to alloc tfm: %s, mode %d\n",
153                        alg_name, alg_mode);
154                 return -1;
155         }
156
157         if (crypto_blkcipher_setkey(kb->kb_tfm, kb->kb_key.data, kb->kb_key.len)) {
158                 CERROR("failed to set %s key, len %d\n",
159                        alg_name, kb->kb_key.len);
160                 return -1;
161         }
162
163         return 0;
164 }
165
166 static
167 int krb5_init_keys(struct krb5_ctx *kctx)
168 {
169         struct krb5_enctype *ke;
170
171         if (kctx->kc_enctype >= MAX_ENCTYPES ||
172             enctypes[kctx->kc_enctype].ke_hash_size == 0) {
173                 CERROR("unsupported enctype %x\n", kctx->kc_enctype);
174                 return -1;
175         }
176
177         ke = &enctypes[kctx->kc_enctype];
178
179         /* tfm arc4 is stateful, user should alloc-use-free by his own */
180         if (kctx->kc_enctype != ENCTYPE_ARCFOUR_HMAC &&
181             keyblock_init(&kctx->kc_keye, ke->ke_enc_name, ke->ke_enc_mode))
182                 return -1;
183
184         /* tfm hmac is stateful, user should alloc-use-free by his own */
185         if (ke->ke_hash_hmac == 0 &&
186             keyblock_init(&kctx->kc_keyi, ke->ke_enc_name, ke->ke_enc_mode))
187                 return -1;
188         if (ke->ke_hash_hmac == 0 &&
189             keyblock_init(&kctx->kc_keyc, ke->ke_enc_name, ke->ke_enc_mode))
190                 return -1;
191
192         return 0;
193 }
194
195 static
196 void keyblock_free(struct krb5_keyblock *kb)
197 {
198         rawobj_free(&kb->kb_key);
199         if (kb->kb_tfm)
200                 crypto_free_blkcipher(kb->kb_tfm);
201 }
202
203 static
204 int keyblock_dup(struct krb5_keyblock *new, struct krb5_keyblock *kb)
205 {
206         return rawobj_dup(&new->kb_key, &kb->kb_key);
207 }
208
209 static
210 int get_bytes(char **ptr, const char *end, void *res, int len)
211 {
212         char *p, *q;
213         p = *ptr;
214         q = p + len;
215         if (q > end || q < p)
216                 return -1;
217         memcpy(res, p, len);
218         *ptr = q;
219         return 0;
220 }
221
222 static
223 int get_rawobj(char **ptr, const char *end, rawobj_t *res)
224 {
225         char   *p, *q;
226         __u32   len;
227
228         p = *ptr;
229         if (get_bytes(&p, end, &len, sizeof(len)))
230                 return -1;
231
232         q = p + len;
233         if (q > end || q < p)
234                 return -1;
235
236         OBD_ALLOC_LARGE(res->data, len);
237         if (!res->data)
238                 return -1;
239
240         res->len = len;
241         memcpy(res->data, p, len);
242         *ptr = q;
243         return 0;
244 }
245
246 static
247 int get_keyblock(char **ptr, const char *end,
248                  struct krb5_keyblock *kb, __u32 keysize)
249 {
250         char *buf;
251
252         OBD_ALLOC_LARGE(buf, keysize);
253         if (buf == NULL)
254                 return -1;
255
256         if (get_bytes(ptr, end, buf, keysize)) {
257                 OBD_FREE_LARGE(buf, keysize);
258                 return -1;
259         }
260
261         kb->kb_key.len = keysize;
262         kb->kb_key.data = buf;
263         return 0;
264 }
265
266 static
267 void delete_context_kerberos(struct krb5_ctx *kctx)
268 {
269         rawobj_free(&kctx->kc_mech_used);
270
271         keyblock_free(&kctx->kc_keye);
272         keyblock_free(&kctx->kc_keyi);
273         keyblock_free(&kctx->kc_keyc);
274 }
275
276 static
277 __u32 import_context_rfc1964(struct krb5_ctx *kctx, char *p, char *end)
278 {
279         unsigned int    tmp_uint, keysize;
280
281         /* seed_init flag */
282         if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
283                 goto out_err;
284         kctx->kc_seed_init = (tmp_uint != 0);
285
286         /* seed */
287         if (get_bytes(&p, end, kctx->kc_seed, sizeof(kctx->kc_seed)))
288                 goto out_err;
289
290         /* sign/seal algorithm, not really used now */
291         if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)) ||
292             get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
293                 goto out_err;
294
295         /* end time */
296         if (get_bytes(&p, end, &kctx->kc_endtime, sizeof(kctx->kc_endtime)))
297                 goto out_err;
298
299         /* seq send */
300         if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
301                 goto out_err;
302         kctx->kc_seq_send = tmp_uint;
303
304         /* mech oid */
305         if (get_rawobj(&p, end, &kctx->kc_mech_used))
306                 goto out_err;
307
308         /* old style enc/seq keys in format:
309          *   - enctype (u32)
310          *   - keysize (u32)
311          *   - keydata
312          * we decompose them to fit into the new context
313          */
314
315         /* enc key */
316         if (get_bytes(&p, end, &kctx->kc_enctype, sizeof(kctx->kc_enctype)))
317                 goto out_err;
318
319         if (get_bytes(&p, end, &keysize, sizeof(keysize)))
320                 goto out_err;
321
322         if (get_keyblock(&p, end, &kctx->kc_keye, keysize))
323                 goto out_err;
324
325         /* seq key */
326         if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)) ||
327             tmp_uint != kctx->kc_enctype)
328                 goto out_err;
329
330         if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)) ||
331             tmp_uint != keysize)
332                 goto out_err;
333
334         if (get_keyblock(&p, end, &kctx->kc_keyc, keysize))
335                 goto out_err;
336
337         /* old style fallback */
338         if (keyblock_dup(&kctx->kc_keyi, &kctx->kc_keyc))
339                 goto out_err;
340
341         if (p != end)
342                 goto out_err;
343
344         CDEBUG(D_SEC, "successfully imported rfc1964 context\n");
345         return 0;
346 out_err:
347         return GSS_S_FAILURE;
348 }
349
350 /* Flags for version 2 context flags */
351 #define KRB5_CTX_FLAG_INITIATOR         0x00000001
352 #define KRB5_CTX_FLAG_CFX               0x00000002
353 #define KRB5_CTX_FLAG_ACCEPTOR_SUBKEY   0x00000004
354
355 static
356 __u32 import_context_rfc4121(struct krb5_ctx *kctx, char *p, char *end)
357 {
358         unsigned int    tmp_uint, keysize;
359
360         /* end time */
361         if (get_bytes(&p, end, &kctx->kc_endtime, sizeof(kctx->kc_endtime)))
362                 goto out_err;
363
364         /* flags */
365         if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
366                 goto out_err;
367
368         if (tmp_uint & KRB5_CTX_FLAG_INITIATOR)
369                 kctx->kc_initiate = 1;
370         if (tmp_uint & KRB5_CTX_FLAG_CFX)
371                 kctx->kc_cfx = 1;
372         if (tmp_uint & KRB5_CTX_FLAG_ACCEPTOR_SUBKEY)
373                 kctx->kc_have_acceptor_subkey = 1;
374
375         /* seq send */
376         if (get_bytes(&p, end, &kctx->kc_seq_send, sizeof(kctx->kc_seq_send)))
377                 goto out_err;
378
379         /* enctype */
380         if (get_bytes(&p, end, &kctx->kc_enctype, sizeof(kctx->kc_enctype)))
381                 goto out_err;
382
383         /* size of each key */
384         if (get_bytes(&p, end, &keysize, sizeof(keysize)))
385                 goto out_err;
386
387         /* number of keys - should always be 3 */
388         if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
389                 goto out_err;
390
391         if (tmp_uint != 3) {
392                 CERROR("Invalid number of keys: %u\n", tmp_uint);
393                 goto out_err;
394         }
395
396         /* ke */
397         if (get_keyblock(&p, end, &kctx->kc_keye, keysize))
398                 goto out_err;
399         /* ki */
400         if (get_keyblock(&p, end, &kctx->kc_keyi, keysize))
401                 goto out_err;
402         /* ki */
403         if (get_keyblock(&p, end, &kctx->kc_keyc, keysize))
404                 goto out_err;
405
406         CDEBUG(D_SEC, "successfully imported v2 context\n");
407         return 0;
408 out_err:
409         return GSS_S_FAILURE;
410 }
411
412 /*
413  * The whole purpose here is trying to keep user level gss context parsing
414  * from nfs-utils unchanged as possible as we can, they are not quite mature
415  * yet, and many stuff still not clear, like heimdal etc.
416  */
417 static
418 __u32 gss_import_sec_context_kerberos(rawobj_t *inbuf,
419                                       struct gss_ctx *gctx)
420 {
421         struct krb5_ctx *kctx;
422         char            *p = (char *) inbuf->data;
423         char            *end = (char *) (inbuf->data + inbuf->len);
424         unsigned int     tmp_uint, rc;
425
426         if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint))) {
427                 CERROR("Fail to read version\n");
428                 return GSS_S_FAILURE;
429         }
430
431         /* only support 0, 1 for the moment */
432         if (tmp_uint > 2) {
433                 CERROR("Invalid version %u\n", tmp_uint);
434                 return GSS_S_FAILURE;
435         }
436
437         OBD_ALLOC_PTR(kctx);
438         if (!kctx)
439                 return GSS_S_FAILURE;
440
441         if (tmp_uint == 0 || tmp_uint == 1) {
442                 kctx->kc_initiate = tmp_uint;
443                 rc = import_context_rfc1964(kctx, p, end);
444         } else {
445                 rc = import_context_rfc4121(kctx, p, end);
446         }
447
448         if (rc == 0)
449                 rc = krb5_init_keys(kctx);
450
451         if (rc) {
452                 delete_context_kerberos(kctx);
453                 OBD_FREE_PTR(kctx);
454
455                 return GSS_S_FAILURE;
456         }
457
458         gctx->internal_ctx_id = kctx;
459         return GSS_S_COMPLETE;
460 }
461
462 static
463 __u32 gss_copy_reverse_context_kerberos(struct gss_ctx *gctx,
464                                         struct gss_ctx *gctx_new)
465 {
466         struct krb5_ctx *kctx = gctx->internal_ctx_id;
467         struct krb5_ctx *knew;
468
469         OBD_ALLOC_PTR(knew);
470         if (!knew)
471                 return GSS_S_FAILURE;
472
473         knew->kc_initiate = kctx->kc_initiate ? 0 : 1;
474         knew->kc_cfx = kctx->kc_cfx;
475         knew->kc_seed_init = kctx->kc_seed_init;
476         knew->kc_have_acceptor_subkey = kctx->kc_have_acceptor_subkey;
477         knew->kc_endtime = kctx->kc_endtime;
478
479         memcpy(knew->kc_seed, kctx->kc_seed, sizeof(kctx->kc_seed));
480         knew->kc_seq_send = kctx->kc_seq_recv;
481         knew->kc_seq_recv = kctx->kc_seq_send;
482         knew->kc_enctype = kctx->kc_enctype;
483
484         if (rawobj_dup(&knew->kc_mech_used, &kctx->kc_mech_used))
485                 goto out_err;
486
487         if (keyblock_dup(&knew->kc_keye, &kctx->kc_keye))
488                 goto out_err;
489         if (keyblock_dup(&knew->kc_keyi, &kctx->kc_keyi))
490                 goto out_err;
491         if (keyblock_dup(&knew->kc_keyc, &kctx->kc_keyc))
492                 goto out_err;
493         if (krb5_init_keys(knew))
494                 goto out_err;
495
496         gctx_new->internal_ctx_id = knew;
497         CDEBUG(D_SEC, "successfully copied reverse context\n");
498         return GSS_S_COMPLETE;
499
500 out_err:
501         delete_context_kerberos(knew);
502         OBD_FREE_PTR(knew);
503         return GSS_S_FAILURE;
504 }
505
506 static
507 __u32 gss_inquire_context_kerberos(struct gss_ctx *gctx,
508                                    unsigned long  *endtime)
509 {
510         struct krb5_ctx *kctx = gctx->internal_ctx_id;
511
512         *endtime = (unsigned long) ((__u32) kctx->kc_endtime);
513         return GSS_S_COMPLETE;
514 }
515
516 static
517 void gss_delete_sec_context_kerberos(void *internal_ctx)
518 {
519         struct krb5_ctx *kctx = internal_ctx;
520
521         delete_context_kerberos(kctx);
522         OBD_FREE_PTR(kctx);
523 }
524
525 static
526 void buf_to_sg(struct scatterlist *sg, void *ptr, int len)
527 {
528         sg_init_table(sg, 1);
529         sg_set_buf(sg, ptr, len);
530 }
531
532 static
533 __u32 krb5_encrypt(struct crypto_blkcipher *tfm,
534                    int decrypt,
535                    void * iv,
536                    void * in,
537                    void * out,
538                    int length)
539 {
540         struct blkcipher_desc desc;
541         struct scatterlist    sg;
542         __u8 local_iv[16] = {0};
543         __u32 ret = -EINVAL;
544
545         LASSERT(tfm);
546         desc.tfm  = tfm;
547         desc.info = local_iv;
548         desc.flags= 0;
549
550         if (length % crypto_blkcipher_blocksize(tfm) != 0) {
551                 CERROR("output length %d mismatch blocksize %d\n",
552                        length, crypto_blkcipher_blocksize(tfm));
553                 goto out;
554         }
555
556         if (crypto_blkcipher_ivsize(tfm) > 16) {
557                 CERROR("iv size too large %d\n", crypto_blkcipher_ivsize(tfm));
558                 goto out;
559         }
560
561         if (iv)
562                 memcpy(local_iv, iv, crypto_blkcipher_ivsize(tfm));
563
564         memcpy(out, in, length);
565         buf_to_sg(&sg, out, length);
566
567         if (decrypt)
568                 ret = crypto_blkcipher_decrypt_iv(&desc, &sg, &sg, length);
569         else
570                 ret = crypto_blkcipher_encrypt_iv(&desc, &sg, &sg, length);
571
572 out:
573         return(ret);
574 }
575
576 static inline
577 int krb5_digest_hmac(struct crypto_hash *tfm,
578                      rawobj_t *key,
579                      struct krb5_header *khdr,
580                      int msgcnt, rawobj_t *msgs,
581                      int iovcnt, lnet_kiov_t *iovs,
582                      rawobj_t *cksum)
583 {
584         struct hash_desc   desc;
585         struct scatterlist sg[1];
586         int                i;
587
588         crypto_hash_setkey(tfm, key->data, key->len);
589         desc.tfm  = tfm;
590         desc.flags= 0;
591
592         crypto_hash_init(&desc);
593
594         for (i = 0; i < msgcnt; i++) {
595                 if (msgs[i].len == 0)
596                         continue;
597                 buf_to_sg(sg, (char *) msgs[i].data, msgs[i].len);
598                 crypto_hash_update(&desc, sg, msgs[i].len);
599         }
600
601         for (i = 0; i < iovcnt; i++) {
602                 if (iovs[i].kiov_len == 0)
603                         continue;
604
605                 sg_init_table(sg, 1);
606                 sg_set_page(&sg[0], iovs[i].kiov_page, iovs[i].kiov_len,
607                             iovs[i].kiov_offset);
608                 crypto_hash_update(&desc, sg, iovs[i].kiov_len);
609         }
610
611         if (khdr) {
612                 buf_to_sg(sg, (char *) khdr, sizeof(*khdr));
613                 crypto_hash_update(&desc, sg, sizeof(*khdr));
614         }
615
616         return crypto_hash_final(&desc, cksum->data);
617 }
618
619 static inline
620 int krb5_digest_norm(struct crypto_hash *tfm,
621                      struct krb5_keyblock *kb,
622                      struct krb5_header *khdr,
623                      int msgcnt, rawobj_t *msgs,
624                      int iovcnt, lnet_kiov_t *iovs,
625                      rawobj_t *cksum)
626 {
627         struct hash_desc   desc;
628         struct scatterlist sg[1];
629         int                i;
630
631         LASSERT(kb->kb_tfm);
632         desc.tfm  = tfm;
633         desc.flags= 0;
634
635         crypto_hash_init(&desc);
636
637         for (i = 0; i < msgcnt; i++) {
638                 if (msgs[i].len == 0)
639                         continue;
640                 buf_to_sg(sg, (char *) msgs[i].data, msgs[i].len);
641                 crypto_hash_update(&desc, sg, msgs[i].len);
642         }
643
644         for (i = 0; i < iovcnt; i++) {
645                 if (iovs[i].kiov_len == 0)
646                         continue;
647
648                 sg_init_table(sg, 1);
649                 sg_set_page(&sg[0], iovs[i].kiov_page, iovs[i].kiov_len,
650                             iovs[i].kiov_offset);
651                 crypto_hash_update(&desc, sg, iovs[i].kiov_len);
652         }
653
654         if (khdr) {
655                 buf_to_sg(sg, (char *) khdr, sizeof(*khdr));
656                 crypto_hash_update(&desc, sg, sizeof(*khdr));
657         }
658
659         crypto_hash_final(&desc, cksum->data);
660
661         return krb5_encrypt(kb->kb_tfm, 0, NULL, cksum->data,
662                             cksum->data, cksum->len);
663 }
664
665 /*
666  * compute (keyed/keyless) checksum against the plain text which appended
667  * with krb5 wire token header.
668  */
669 static
670 __s32 krb5_make_checksum(__u32 enctype,
671                          struct krb5_keyblock *kb,
672                          struct krb5_header *khdr,
673                          int msgcnt, rawobj_t *msgs,
674                          int iovcnt, lnet_kiov_t *iovs,
675                          rawobj_t *cksum)
676 {
677         struct krb5_enctype   *ke = &enctypes[enctype];
678         struct crypto_hash    *tfm;
679         __u32                  code = GSS_S_FAILURE;
680         int                    rc;
681
682         if (!(tfm = crypto_alloc_hash(ke->ke_hash_name, 0, 0))) {
683                 CERROR("failed to alloc TFM: %s\n", ke->ke_hash_name);
684                 return GSS_S_FAILURE;
685         }
686
687         cksum->len = crypto_hash_digestsize(tfm);
688         OBD_ALLOC_LARGE(cksum->data, cksum->len);
689         if (!cksum->data) {
690                 cksum->len = 0;
691                 goto out_tfm;
692         }
693
694         if (ke->ke_hash_hmac)
695                 rc = krb5_digest_hmac(tfm, &kb->kb_key,
696                                       khdr, msgcnt, msgs, iovcnt, iovs, cksum);
697         else
698                 rc = krb5_digest_norm(tfm, kb,
699                                       khdr, msgcnt, msgs, iovcnt, iovs, cksum);
700
701         if (rc == 0)
702                 code = GSS_S_COMPLETE;
703 out_tfm:
704         crypto_free_hash(tfm);
705         return code;
706 }
707
708 static void fill_krb5_header(struct krb5_ctx *kctx,
709                              struct krb5_header *khdr,
710                              int privacy)
711 {
712         unsigned char acceptor_flag;
713
714         acceptor_flag = kctx->kc_initiate ? 0 : FLAG_SENDER_IS_ACCEPTOR;
715
716         if (privacy) {
717                 khdr->kh_tok_id = cpu_to_be16(KG_TOK_WRAP_MSG);
718                 khdr->kh_flags = acceptor_flag | FLAG_WRAP_CONFIDENTIAL;
719                 khdr->kh_ec = cpu_to_be16(0);
720                 khdr->kh_rrc = cpu_to_be16(0);
721         } else {
722                 khdr->kh_tok_id = cpu_to_be16(KG_TOK_MIC_MSG);
723                 khdr->kh_flags = acceptor_flag;
724                 khdr->kh_ec = cpu_to_be16(0xffff);
725                 khdr->kh_rrc = cpu_to_be16(0xffff);
726         }
727
728         khdr->kh_filler = 0xff;
729         spin_lock(&krb5_seq_lock);
730         khdr->kh_seq = cpu_to_be64(kctx->kc_seq_send++);
731         spin_unlock(&krb5_seq_lock);
732 }
733
734 static __u32 verify_krb5_header(struct krb5_ctx *kctx,
735                                 struct krb5_header *khdr,
736                                 int privacy)
737 {
738         unsigned char acceptor_flag;
739         __u16         tok_id, ec_rrc;
740
741         acceptor_flag = kctx->kc_initiate ? FLAG_SENDER_IS_ACCEPTOR : 0;
742
743         if (privacy) {
744                 tok_id = KG_TOK_WRAP_MSG;
745                 ec_rrc = 0x0;
746         } else {
747                 tok_id = KG_TOK_MIC_MSG;
748                 ec_rrc = 0xffff;
749         }
750
751         /* sanity checks */
752         if (be16_to_cpu(khdr->kh_tok_id) != tok_id) {
753                 CERROR("bad token id\n");
754                 return GSS_S_DEFECTIVE_TOKEN;
755         }
756         if ((khdr->kh_flags & FLAG_SENDER_IS_ACCEPTOR) != acceptor_flag) {
757                 CERROR("bad direction flag\n");
758                 return GSS_S_BAD_SIG;
759         }
760         if (privacy && (khdr->kh_flags & FLAG_WRAP_CONFIDENTIAL) == 0) {
761                 CERROR("missing confidential flag\n");
762                 return GSS_S_BAD_SIG;
763         }
764         if (khdr->kh_filler != 0xff) {
765                 CERROR("bad filler\n");
766                 return GSS_S_DEFECTIVE_TOKEN;
767         }
768         if (be16_to_cpu(khdr->kh_ec) != ec_rrc ||
769             be16_to_cpu(khdr->kh_rrc) != ec_rrc) {
770                 CERROR("bad EC or RRC\n");
771                 return GSS_S_DEFECTIVE_TOKEN;
772         }
773         return GSS_S_COMPLETE;
774 }
775
776 static
777 __u32 gss_get_mic_kerberos(struct gss_ctx *gctx,
778                            int msgcnt,
779                            rawobj_t *msgs,
780                            int iovcnt,
781                            lnet_kiov_t *iovs,
782                            rawobj_t *token)
783 {
784         struct krb5_ctx     *kctx = gctx->internal_ctx_id;
785         struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
786         struct krb5_header  *khdr;
787         rawobj_t             cksum = RAWOBJ_EMPTY;
788
789         /* fill krb5 header */
790         LASSERT(token->len >= sizeof(*khdr));
791         khdr = (struct krb5_header *) token->data;
792         fill_krb5_header(kctx, khdr, 0);
793
794         /* checksum */
795         if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyc,
796                                khdr, msgcnt, msgs, iovcnt, iovs, &cksum))
797                 return GSS_S_FAILURE;
798
799         LASSERT(cksum.len >= ke->ke_hash_size);
800         LASSERT(token->len >= sizeof(*khdr) + ke->ke_hash_size);
801         memcpy(khdr + 1, cksum.data + cksum.len - ke->ke_hash_size,
802                ke->ke_hash_size);
803
804         token->len = sizeof(*khdr) + ke->ke_hash_size;
805         rawobj_free(&cksum);
806         return GSS_S_COMPLETE;
807 }
808
809 static
810 __u32 gss_verify_mic_kerberos(struct gss_ctx *gctx,
811                               int msgcnt,
812                               rawobj_t *msgs,
813                               int iovcnt,
814                               lnet_kiov_t *iovs,
815                               rawobj_t *token)
816 {
817         struct krb5_ctx     *kctx = gctx->internal_ctx_id;
818         struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
819         struct krb5_header  *khdr;
820         rawobj_t             cksum = RAWOBJ_EMPTY;
821         __u32                major;
822
823         if (token->len < sizeof(*khdr)) {
824                 CERROR("short signature: %u\n", token->len);
825                 return GSS_S_DEFECTIVE_TOKEN;
826         }
827
828         khdr = (struct krb5_header *) token->data;
829
830         major = verify_krb5_header(kctx, khdr, 0);
831         if (major != GSS_S_COMPLETE) {
832                 CERROR("bad krb5 header\n");
833                 return major;
834         }
835
836         if (token->len < sizeof(*khdr) + ke->ke_hash_size) {
837                 CERROR("short signature: %u, require %d\n",
838                        token->len, (int) sizeof(*khdr) + ke->ke_hash_size);
839                 return GSS_S_FAILURE;
840         }
841
842         if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyc,
843                                khdr, msgcnt, msgs, iovcnt, iovs, &cksum)) {
844                 CERROR("failed to make checksum\n");
845                 return GSS_S_FAILURE;
846         }
847
848         LASSERT(cksum.len >= ke->ke_hash_size);
849         if (memcmp(khdr + 1, cksum.data + cksum.len - ke->ke_hash_size,
850                    ke->ke_hash_size)) {
851                 CERROR("checksum mismatch\n");
852                 rawobj_free(&cksum);
853                 return GSS_S_BAD_SIG;
854         }
855
856         rawobj_free(&cksum);
857         return GSS_S_COMPLETE;
858 }
859
860 static
861 int add_padding(rawobj_t *msg, int msg_buflen, int blocksize)
862 {
863         int padding;
864
865         padding = (blocksize - (msg->len & (blocksize - 1))) &
866                   (blocksize - 1);
867         if (!padding)
868                 return 0;
869
870         if (msg->len + padding > msg_buflen) {
871                 CERROR("bufsize %u too small: datalen %u, padding %u\n",
872                         msg_buflen, msg->len, padding);
873                 return -EINVAL;
874         }
875
876         memset(msg->data + msg->len, padding, padding);
877         msg->len += padding;
878         return 0;
879 }
880
881 static
882 int krb5_encrypt_rawobjs(struct crypto_blkcipher *tfm,
883                          int mode_ecb,
884                          int inobj_cnt,
885                          rawobj_t *inobjs,
886                          rawobj_t *outobj,
887                          int enc)
888 {
889         struct blkcipher_desc desc;
890         struct scatterlist    src, dst;
891         __u8                  local_iv[16] = {0}, *buf;
892         __u32                 datalen = 0;
893         int                   i, rc;
894         ENTRY;
895
896         buf = outobj->data;
897         desc.tfm  = tfm;
898         desc.info = local_iv;
899         desc.flags = 0;
900
901         for (i = 0; i < inobj_cnt; i++) {
902                 LASSERT(buf + inobjs[i].len <= outobj->data + outobj->len);
903
904                 buf_to_sg(&src, inobjs[i].data, inobjs[i].len);
905                 buf_to_sg(&dst, buf, outobj->len - datalen);
906
907                 if (mode_ecb) {
908                         if (enc)
909                                 rc = crypto_blkcipher_encrypt(
910                                         &desc, &dst, &src, src.length);
911                         else
912                                 rc = crypto_blkcipher_decrypt(
913                                         &desc, &dst, &src, src.length);
914                 } else {
915                         if (enc)
916                                 rc = crypto_blkcipher_encrypt_iv(
917                                         &desc, &dst, &src, src.length);
918                         else
919                                 rc = crypto_blkcipher_decrypt_iv(
920                                         &desc, &dst, &src, src.length);
921                 }
922
923                 if (rc) {
924                         CERROR("encrypt error %d\n", rc);
925                         RETURN(rc);
926                 }
927
928                 datalen += inobjs[i].len;
929                 buf += inobjs[i].len;
930         }
931
932         outobj->len = datalen;
933         RETURN(0);
934 }
935
936 /*
937  * if adj_nob != 0, we adjust desc->bd_nob to the actual cipher text size.
938  */
939 static
940 int krb5_encrypt_bulk(struct crypto_blkcipher *tfm,
941                       struct krb5_header *khdr,
942                       char *confounder,
943                       struct ptlrpc_bulk_desc *desc,
944                       rawobj_t *cipher,
945                       int adj_nob)
946 {
947         struct blkcipher_desc   ciph_desc;
948         __u8                    local_iv[16] = {0};
949         struct scatterlist      src, dst;
950         int                     blocksize, i, rc, nob = 0;
951
952         LASSERT(desc->bd_iov_count);
953         LASSERT(desc->bd_enc_iov);
954
955         blocksize = crypto_blkcipher_blocksize(tfm);
956         LASSERT(blocksize > 1);
957         LASSERT(cipher->len == blocksize + sizeof(*khdr));
958
959         ciph_desc.tfm  = tfm;
960         ciph_desc.info = local_iv;
961         ciph_desc.flags = 0;
962
963         /* encrypt confounder */
964         buf_to_sg(&src, confounder, blocksize);
965         buf_to_sg(&dst, cipher->data, blocksize);
966
967         rc = crypto_blkcipher_encrypt_iv(&ciph_desc, &dst, &src, blocksize);
968         if (rc) {
969                 CERROR("error to encrypt confounder: %d\n", rc);
970                 return rc;
971         }
972
973         /* encrypt clear pages */
974         for (i = 0; i < desc->bd_iov_count; i++) {
975                 sg_init_table(&src, 1);
976                 sg_set_page(&src, desc->bd_iov[i].kiov_page,
977                             (desc->bd_iov[i].kiov_len + blocksize - 1) &
978                             (~(blocksize - 1)),
979                             desc->bd_iov[i].kiov_offset);
980                 if (adj_nob)
981                         nob += src.length;
982                 sg_init_table(&dst, 1);
983                 sg_set_page(&dst, desc->bd_enc_iov[i].kiov_page, src.length,
984                             src.offset);
985
986                 desc->bd_enc_iov[i].kiov_offset = dst.offset;
987                 desc->bd_enc_iov[i].kiov_len = dst.length;
988
989                 rc = crypto_blkcipher_encrypt_iv(&ciph_desc, &dst, &src,
990                                                     src.length);
991                 if (rc) {
992                         CERROR("error to encrypt page: %d\n", rc);
993                         return rc;
994                 }
995         }
996
997         /* encrypt krb5 header */
998         buf_to_sg(&src, khdr, sizeof(*khdr));
999         buf_to_sg(&dst, cipher->data + blocksize, sizeof(*khdr));
1000
1001         rc = crypto_blkcipher_encrypt_iv(&ciph_desc, &dst, &src,
1002                                          sizeof(*khdr));
1003         if (rc) {
1004                 CERROR("error to encrypt krb5 header: %d\n", rc);
1005                 return rc;
1006         }
1007
1008         if (adj_nob)
1009                 desc->bd_nob = nob;
1010
1011         return 0;
1012 }
1013
1014 /*
1015  * desc->bd_nob_transferred is the size of cipher text received.
1016  * desc->bd_nob is the target size of plain text supposed to be.
1017  *
1018  * if adj_nob != 0, we adjust each page's kiov_len to the actual
1019  * plain text size.
1020  * - for client read: we don't know data size for each page, so
1021  *   bd_iov[]->kiov_len is set to PAGE_SIZE, but actual data received might
1022  *   be smaller, so we need to adjust it according to bd_enc_iov[]->kiov_len.
1023  *   this means we DO NOT support the situation that server send an odd size
1024  *   data in a page which is not the last one.
1025  * - for server write: we knows exactly data size for each page being expected,
1026  *   thus kiov_len is accurate already, so we should not adjust it at all.
1027  *   and bd_enc_iov[]->kiov_len should be round_up(bd_iov[]->kiov_len) which
1028  *   should have been done by prep_bulk().
1029  */
1030 static
1031 int krb5_decrypt_bulk(struct crypto_blkcipher *tfm,
1032                       struct krb5_header *khdr,
1033                       struct ptlrpc_bulk_desc *desc,
1034                       rawobj_t *cipher,
1035                       rawobj_t *plain,
1036                       int adj_nob)
1037 {
1038         struct blkcipher_desc   ciph_desc;
1039         __u8                    local_iv[16] = {0};
1040         struct scatterlist      src, dst;
1041         int                     ct_nob = 0, pt_nob = 0;
1042         int                     blocksize, i, rc;
1043
1044         LASSERT(desc->bd_iov_count);
1045         LASSERT(desc->bd_enc_iov);
1046         LASSERT(desc->bd_nob_transferred);
1047
1048         blocksize = crypto_blkcipher_blocksize(tfm);
1049         LASSERT(blocksize > 1);
1050         LASSERT(cipher->len == blocksize + sizeof(*khdr));
1051
1052         ciph_desc.tfm  = tfm;
1053         ciph_desc.info = local_iv;
1054         ciph_desc.flags = 0;
1055
1056         if (desc->bd_nob_transferred % blocksize) {
1057                 CERROR("odd transferred nob: %d\n", desc->bd_nob_transferred);
1058                 return -EPROTO;
1059         }
1060
1061         /* decrypt head (confounder) */
1062         buf_to_sg(&src, cipher->data, blocksize);
1063         buf_to_sg(&dst, plain->data, blocksize);
1064
1065         rc = crypto_blkcipher_decrypt_iv(&ciph_desc, &dst, &src, blocksize);
1066         if (rc) {
1067                 CERROR("error to decrypt confounder: %d\n", rc);
1068                 return rc;
1069         }
1070
1071         for (i = 0; i < desc->bd_iov_count && ct_nob < desc->bd_nob_transferred;
1072              i++) {
1073                 if (desc->bd_enc_iov[i].kiov_offset % blocksize != 0 ||
1074                     desc->bd_enc_iov[i].kiov_len % blocksize != 0) {
1075                         CERROR("page %d: odd offset %u len %u, blocksize %d\n",
1076                                i, desc->bd_enc_iov[i].kiov_offset,
1077                                desc->bd_enc_iov[i].kiov_len, blocksize);
1078                         return -EFAULT;
1079                 }
1080
1081                 if (adj_nob) {
1082                         if (ct_nob + desc->bd_enc_iov[i].kiov_len >
1083                             desc->bd_nob_transferred)
1084                                 desc->bd_enc_iov[i].kiov_len =
1085                                         desc->bd_nob_transferred - ct_nob;
1086
1087                         desc->bd_iov[i].kiov_len = desc->bd_enc_iov[i].kiov_len;
1088                         if (pt_nob + desc->bd_enc_iov[i].kiov_len >desc->bd_nob)
1089                                 desc->bd_iov[i].kiov_len = desc->bd_nob -pt_nob;
1090                 } else {
1091                         /* this should be guaranteed by LNET */
1092                         LASSERT(ct_nob + desc->bd_enc_iov[i].kiov_len <=
1093                                 desc->bd_nob_transferred);
1094                         LASSERT(desc->bd_iov[i].kiov_len <=
1095                                 desc->bd_enc_iov[i].kiov_len);
1096                 }
1097
1098                 if (desc->bd_enc_iov[i].kiov_len == 0)
1099                         continue;
1100
1101                 sg_init_table(&src, 1);
1102                 sg_set_page(&src, desc->bd_enc_iov[i].kiov_page,
1103                             desc->bd_enc_iov[i].kiov_len,
1104                             desc->bd_enc_iov[i].kiov_offset);
1105                 dst = src;
1106                 if (desc->bd_iov[i].kiov_len % blocksize == 0)
1107                         sg_assign_page(&dst, desc->bd_iov[i].kiov_page);
1108
1109                 rc = crypto_blkcipher_decrypt_iv(&ciph_desc, &dst, &src,
1110                                                  src.length);
1111                 if (rc) {
1112                         CERROR("error to decrypt page: %d\n", rc);
1113                         return rc;
1114                 }
1115
1116                 if (desc->bd_iov[i].kiov_len % blocksize != 0) {
1117                         memcpy(page_address(desc->bd_iov[i].kiov_page) +
1118                                desc->bd_iov[i].kiov_offset,
1119                                page_address(desc->bd_enc_iov[i].kiov_page) +
1120                                desc->bd_iov[i].kiov_offset,
1121                                desc->bd_iov[i].kiov_len);
1122                 }
1123
1124                 ct_nob += desc->bd_enc_iov[i].kiov_len;
1125                 pt_nob += desc->bd_iov[i].kiov_len;
1126         }
1127
1128         if (unlikely(ct_nob != desc->bd_nob_transferred)) {
1129                 CERROR("%d cipher text transferred but only %d decrypted\n",
1130                        desc->bd_nob_transferred, ct_nob);
1131                 return -EFAULT;
1132         }
1133
1134         if (unlikely(!adj_nob && pt_nob != desc->bd_nob)) {
1135                 CERROR("%d plain text expected but only %d received\n",
1136                        desc->bd_nob, pt_nob);
1137                 return -EFAULT;
1138         }
1139
1140         /* if needed, clear up the rest unused iovs */
1141         if (adj_nob)
1142                 while (i < desc->bd_iov_count)
1143                         desc->bd_iov[i++].kiov_len = 0;
1144
1145         /* decrypt tail (krb5 header) */
1146         buf_to_sg(&src, cipher->data + blocksize, sizeof(*khdr));
1147         buf_to_sg(&dst, cipher->data + blocksize, sizeof(*khdr));
1148
1149         rc = crypto_blkcipher_decrypt_iv(&ciph_desc, &dst, &src,
1150                                          sizeof(*khdr));
1151         if (rc) {
1152                 CERROR("error to decrypt tail: %d\n", rc);
1153                 return rc;
1154         }
1155
1156         if (memcmp(cipher->data + blocksize, khdr, sizeof(*khdr))) {
1157                 CERROR("krb5 header doesn't match\n");
1158                 return -EACCES;
1159         }
1160
1161         return 0;
1162 }
1163
1164 static
1165 __u32 gss_wrap_kerberos(struct gss_ctx *gctx,
1166                         rawobj_t *gsshdr,
1167                         rawobj_t *msg,
1168                         int msg_buflen,
1169                         rawobj_t *token)
1170 {
1171         struct krb5_ctx     *kctx = gctx->internal_ctx_id;
1172         struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
1173         struct krb5_header  *khdr;
1174         int                  blocksize;
1175         rawobj_t             cksum = RAWOBJ_EMPTY;
1176         rawobj_t             data_desc[3], cipher;
1177         __u8                 conf[GSS_MAX_CIPHER_BLOCK];
1178         int                  rc = 0;
1179
1180         LASSERT(ke);
1181         LASSERT(ke->ke_conf_size <= GSS_MAX_CIPHER_BLOCK);
1182         LASSERT(kctx->kc_keye.kb_tfm == NULL ||
1183                 ke->ke_conf_size >=
1184                 crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm));
1185
1186         /*
1187          * final token format:
1188          * ---------------------------------------------------
1189          * | krb5 header | cipher text | checksum (16 bytes) |
1190          * ---------------------------------------------------
1191          */
1192
1193         /* fill krb5 header */
1194         LASSERT(token->len >= sizeof(*khdr));
1195         khdr = (struct krb5_header *) token->data;
1196         fill_krb5_header(kctx, khdr, 1);
1197
1198         /* generate confounder */
1199         cfs_get_random_bytes(conf, ke->ke_conf_size);
1200
1201         /* get encryption blocksize. note kc_keye might not associated with
1202          * a tfm, currently only for arcfour-hmac */
1203         if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1204                 LASSERT(kctx->kc_keye.kb_tfm == NULL);
1205                 blocksize = 1;
1206         } else {
1207                 LASSERT(kctx->kc_keye.kb_tfm);
1208                 blocksize = crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
1209         }
1210         LASSERT(blocksize <= ke->ke_conf_size);
1211
1212         /* padding the message */
1213         if (add_padding(msg, msg_buflen, blocksize))
1214                 return GSS_S_FAILURE;
1215
1216         /*
1217          * clear text layout for checksum:
1218          * ------------------------------------------------------
1219          * | confounder | gss header | clear msgs | krb5 header |
1220          * ------------------------------------------------------
1221          */
1222         data_desc[0].data = conf;
1223         data_desc[0].len = ke->ke_conf_size;
1224         data_desc[1].data = gsshdr->data;
1225         data_desc[1].len = gsshdr->len;
1226         data_desc[2].data = msg->data;
1227         data_desc[2].len = msg->len;
1228
1229         /* compute checksum */
1230         if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyi,
1231                                khdr, 3, data_desc, 0, NULL, &cksum))
1232                 return GSS_S_FAILURE;
1233         LASSERT(cksum.len >= ke->ke_hash_size);
1234
1235         /*
1236          * clear text layout for encryption:
1237          * -----------------------------------------
1238          * | confounder | clear msgs | krb5 header |
1239          * -----------------------------------------
1240          */
1241         data_desc[0].data = conf;
1242         data_desc[0].len = ke->ke_conf_size;
1243         data_desc[1].data = msg->data;
1244         data_desc[1].len = msg->len;
1245         data_desc[2].data = (__u8 *) khdr;
1246         data_desc[2].len = sizeof(*khdr);
1247
1248         /* cipher text will be directly inplace */
1249         cipher.data = (__u8 *) (khdr + 1);
1250         cipher.len = token->len - sizeof(*khdr);
1251         LASSERT(cipher.len >= ke->ke_conf_size + msg->len + sizeof(*khdr));
1252
1253         if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1254                 rawobj_t                 arc4_keye;
1255                 struct crypto_blkcipher *arc4_tfm;
1256
1257                 if (krb5_make_checksum(ENCTYPE_ARCFOUR_HMAC, &kctx->kc_keyi,
1258                                        NULL, 1, &cksum, 0, NULL, &arc4_keye)) {
1259                         CERROR("failed to obtain arc4 enc key\n");
1260                         GOTO(arc4_out, rc = -EACCES);
1261                 }
1262
1263                 arc4_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0, 0);
1264                 if (IS_ERR(arc4_tfm)) {
1265                         CERROR("failed to alloc tfm arc4 in ECB mode\n");
1266                         GOTO(arc4_out_key, rc = -EACCES);
1267                 }
1268
1269                 if (crypto_blkcipher_setkey(arc4_tfm, arc4_keye.data,
1270                                                arc4_keye.len)) {
1271                         CERROR("failed to set arc4 key, len %d\n",
1272                                arc4_keye.len);
1273                         GOTO(arc4_out_tfm, rc = -EACCES);
1274                 }
1275
1276                 rc = krb5_encrypt_rawobjs(arc4_tfm, 1,
1277                                           3, data_desc, &cipher, 1);
1278 arc4_out_tfm:
1279                 crypto_free_blkcipher(arc4_tfm);
1280 arc4_out_key:
1281                 rawobj_free(&arc4_keye);
1282 arc4_out:
1283                 do {} while(0); /* just to avoid compile warning */
1284         } else {
1285                 rc = krb5_encrypt_rawobjs(kctx->kc_keye.kb_tfm, 0,
1286                                           3, data_desc, &cipher, 1);
1287         }
1288
1289         if (rc != 0) {
1290                 rawobj_free(&cksum);
1291                 return GSS_S_FAILURE;
1292         }
1293
1294         /* fill in checksum */
1295         LASSERT(token->len >= sizeof(*khdr) + cipher.len + ke->ke_hash_size);
1296         memcpy((char *)(khdr + 1) + cipher.len,
1297                cksum.data + cksum.len - ke->ke_hash_size,
1298                ke->ke_hash_size);
1299         rawobj_free(&cksum);
1300
1301         /* final token length */
1302         token->len = sizeof(*khdr) + cipher.len + ke->ke_hash_size;
1303         return GSS_S_COMPLETE;
1304 }
1305
1306 static
1307 __u32 gss_prep_bulk_kerberos(struct gss_ctx *gctx,
1308                              struct ptlrpc_bulk_desc *desc)
1309 {
1310         struct krb5_ctx     *kctx = gctx->internal_ctx_id;
1311         int                  blocksize, i;
1312
1313         LASSERT(desc->bd_iov_count);
1314         LASSERT(desc->bd_enc_iov);
1315         LASSERT(kctx->kc_keye.kb_tfm);
1316
1317         blocksize = crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
1318
1319         for (i = 0; i < desc->bd_iov_count; i++) {
1320                 LASSERT(desc->bd_enc_iov[i].kiov_page);
1321                 /*
1322                  * offset should always start at page boundary of either
1323                  * client or server side.
1324                  */
1325                 if (desc->bd_iov[i].kiov_offset & blocksize) {
1326                         CERROR("odd offset %d in page %d\n",
1327                                desc->bd_iov[i].kiov_offset, i);
1328                         return GSS_S_FAILURE;
1329                 }
1330
1331                 desc->bd_enc_iov[i].kiov_offset = desc->bd_iov[i].kiov_offset;
1332                 desc->bd_enc_iov[i].kiov_len = (desc->bd_iov[i].kiov_len +
1333                                                 blocksize - 1) & (~(blocksize - 1));
1334         }
1335
1336         return GSS_S_COMPLETE;
1337 }
1338
1339 static
1340 __u32 gss_wrap_bulk_kerberos(struct gss_ctx *gctx,
1341                              struct ptlrpc_bulk_desc *desc,
1342                              rawobj_t *token, int adj_nob)
1343 {
1344         struct krb5_ctx     *kctx = gctx->internal_ctx_id;
1345         struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
1346         struct krb5_header  *khdr;
1347         int                  blocksize;
1348         rawobj_t             cksum = RAWOBJ_EMPTY;
1349         rawobj_t             data_desc[1], cipher;
1350         __u8                 conf[GSS_MAX_CIPHER_BLOCK];
1351         int                  rc = 0;
1352
1353         LASSERT(ke);
1354         LASSERT(ke->ke_conf_size <= GSS_MAX_CIPHER_BLOCK);
1355
1356         /*
1357          * final token format:
1358          * --------------------------------------------------
1359          * | krb5 header | head/tail cipher text | checksum |
1360          * --------------------------------------------------
1361          */
1362
1363         /* fill krb5 header */
1364         LASSERT(token->len >= sizeof(*khdr));
1365         khdr = (struct krb5_header *) token->data;
1366         fill_krb5_header(kctx, khdr, 1);
1367
1368         /* generate confounder */
1369         cfs_get_random_bytes(conf, ke->ke_conf_size);
1370
1371         /* get encryption blocksize. note kc_keye might not associated with
1372          * a tfm, currently only for arcfour-hmac */
1373         if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1374                 LASSERT(kctx->kc_keye.kb_tfm == NULL);
1375                 blocksize = 1;
1376         } else {
1377                 LASSERT(kctx->kc_keye.kb_tfm);
1378                 blocksize = crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
1379         }
1380
1381         /*
1382          * we assume the size of krb5_header (16 bytes) must be n * blocksize.
1383          * the bulk token size would be exactly (sizeof(krb5_header) +
1384          * blocksize + sizeof(krb5_header) + hashsize)
1385          */
1386         LASSERT(blocksize <= ke->ke_conf_size);
1387         LASSERT(sizeof(*khdr) >= blocksize && sizeof(*khdr) % blocksize == 0);
1388         LASSERT(token->len >= sizeof(*khdr) + blocksize + sizeof(*khdr) + 16);
1389
1390         /*
1391          * clear text layout for checksum:
1392          * ------------------------------------------
1393          * | confounder | clear pages | krb5 header |
1394          * ------------------------------------------
1395          */
1396         data_desc[0].data = conf;
1397         data_desc[0].len = ke->ke_conf_size;
1398
1399         /* compute checksum */
1400         if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyi,
1401                                khdr, 1, data_desc,
1402                                desc->bd_iov_count, desc->bd_iov,
1403                                &cksum))
1404                 return GSS_S_FAILURE;
1405         LASSERT(cksum.len >= ke->ke_hash_size);
1406
1407         /*
1408          * clear text layout for encryption:
1409          * ------------------------------------------
1410          * | confounder | clear pages | krb5 header |
1411          * ------------------------------------------
1412          *        |              |             |
1413          *        ----------  (cipher pages)   |
1414          * result token:   |                   |
1415          * -------------------------------------------
1416          * | krb5 header | cipher text | cipher text |
1417          * -------------------------------------------
1418          */
1419         data_desc[0].data = conf;
1420         data_desc[0].len = ke->ke_conf_size;
1421
1422         cipher.data = (__u8 *) (khdr + 1);
1423         cipher.len = blocksize + sizeof(*khdr);
1424
1425         if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1426                 LBUG();
1427                 rc = 0;
1428         } else {
1429                 rc = krb5_encrypt_bulk(kctx->kc_keye.kb_tfm, khdr,
1430                                        conf, desc, &cipher, adj_nob);
1431         }
1432
1433         if (rc != 0) {
1434                 rawobj_free(&cksum);
1435                 return GSS_S_FAILURE;
1436         }
1437
1438         /* fill in checksum */
1439         LASSERT(token->len >= sizeof(*khdr) + cipher.len + ke->ke_hash_size);
1440         memcpy((char *)(khdr + 1) + cipher.len,
1441                cksum.data + cksum.len - ke->ke_hash_size,
1442                ke->ke_hash_size);
1443         rawobj_free(&cksum);
1444
1445         /* final token length */
1446         token->len = sizeof(*khdr) + cipher.len + ke->ke_hash_size;
1447         return GSS_S_COMPLETE;
1448 }
1449
1450 static
1451 __u32 gss_unwrap_kerberos(struct gss_ctx  *gctx,
1452                           rawobj_t        *gsshdr,
1453                           rawobj_t        *token,
1454                           rawobj_t        *msg)
1455 {
1456         struct krb5_ctx     *kctx = gctx->internal_ctx_id;
1457         struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
1458         struct krb5_header  *khdr;
1459         unsigned char       *tmpbuf;
1460         int                  blocksize, bodysize;
1461         rawobj_t             cksum = RAWOBJ_EMPTY;
1462         rawobj_t             cipher_in, plain_out;
1463         rawobj_t             hash_objs[3];
1464         int                  rc = 0;
1465         __u32                major;
1466
1467         LASSERT(ke);
1468
1469         if (token->len < sizeof(*khdr)) {
1470                 CERROR("short signature: %u\n", token->len);
1471                 return GSS_S_DEFECTIVE_TOKEN;
1472         }
1473
1474         khdr = (struct krb5_header *) token->data;
1475
1476         major = verify_krb5_header(kctx, khdr, 1);
1477         if (major != GSS_S_COMPLETE) {
1478                 CERROR("bad krb5 header\n");
1479                 return major;
1480         }
1481
1482         /* block size */
1483         if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1484                 LASSERT(kctx->kc_keye.kb_tfm == NULL);
1485                 blocksize = 1;
1486         } else {
1487                 LASSERT(kctx->kc_keye.kb_tfm);
1488                 blocksize = crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
1489         }
1490
1491         /* expected token layout:
1492          * ----------------------------------------
1493          * | krb5 header | cipher text | checksum |
1494          * ----------------------------------------
1495          */
1496         bodysize = token->len - sizeof(*khdr) - ke->ke_hash_size;
1497
1498         if (bodysize % blocksize) {
1499                 CERROR("odd bodysize %d\n", bodysize);
1500                 return GSS_S_DEFECTIVE_TOKEN;
1501         }
1502
1503         if (bodysize <= ke->ke_conf_size + sizeof(*khdr)) {
1504                 CERROR("incomplete token: bodysize %d\n", bodysize);
1505                 return GSS_S_DEFECTIVE_TOKEN;
1506         }
1507
1508         if (msg->len < bodysize - ke->ke_conf_size - sizeof(*khdr)) {
1509                 CERROR("buffer too small: %u, require %d\n",
1510                        msg->len, bodysize - ke->ke_conf_size);
1511                 return GSS_S_FAILURE;
1512         }
1513
1514         /* decrypting */
1515         OBD_ALLOC_LARGE(tmpbuf, bodysize);
1516         if (!tmpbuf)
1517                 return GSS_S_FAILURE;
1518
1519         major = GSS_S_FAILURE;
1520
1521         cipher_in.data = (__u8 *) (khdr + 1);
1522         cipher_in.len = bodysize;
1523         plain_out.data = tmpbuf;
1524         plain_out.len = bodysize;
1525
1526         if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1527                 rawobj_t                 arc4_keye;
1528                 struct crypto_blkcipher *arc4_tfm;
1529
1530                 cksum.data = token->data + token->len - ke->ke_hash_size;
1531                 cksum.len = ke->ke_hash_size;
1532
1533                 if (krb5_make_checksum(ENCTYPE_ARCFOUR_HMAC, &kctx->kc_keyi,
1534                                        NULL, 1, &cksum, 0, NULL, &arc4_keye)) {
1535                         CERROR("failed to obtain arc4 enc key\n");
1536                         GOTO(arc4_out, rc = -EACCES);
1537                 }
1538
1539                 arc4_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0, 0);
1540                 if (IS_ERR(arc4_tfm)) {
1541                         CERROR("failed to alloc tfm arc4 in ECB mode\n");
1542                         GOTO(arc4_out_key, rc = -EACCES);
1543                 }
1544
1545                 if (crypto_blkcipher_setkey(arc4_tfm,
1546                                          arc4_keye.data, arc4_keye.len)) {
1547                         CERROR("failed to set arc4 key, len %d\n",
1548                                arc4_keye.len);
1549                         GOTO(arc4_out_tfm, rc = -EACCES);
1550                 }
1551
1552                 rc = krb5_encrypt_rawobjs(arc4_tfm, 1,
1553                                           1, &cipher_in, &plain_out, 0);
1554 arc4_out_tfm:
1555                 crypto_free_blkcipher(arc4_tfm);
1556 arc4_out_key:
1557                 rawobj_free(&arc4_keye);
1558 arc4_out:
1559                 cksum = RAWOBJ_EMPTY;
1560         } else {
1561                 rc = krb5_encrypt_rawobjs(kctx->kc_keye.kb_tfm, 0,
1562                                           1, &cipher_in, &plain_out, 0);
1563         }
1564
1565         if (rc != 0) {
1566                 CERROR("error decrypt\n");
1567                 goto out_free;
1568         }
1569         LASSERT(plain_out.len == bodysize);
1570
1571         /* expected clear text layout:
1572          * -----------------------------------------
1573          * | confounder | clear msgs | krb5 header |
1574          * -----------------------------------------
1575          */
1576
1577         /* verify krb5 header in token is not modified */
1578         if (memcmp(khdr, plain_out.data + plain_out.len - sizeof(*khdr),
1579                    sizeof(*khdr))) {
1580                 CERROR("decrypted krb5 header mismatch\n");
1581                 goto out_free;
1582         }
1583
1584         /* verify checksum, compose clear text as layout:
1585          * ------------------------------------------------------
1586          * | confounder | gss header | clear msgs | krb5 header |
1587          * ------------------------------------------------------
1588          */
1589         hash_objs[0].len = ke->ke_conf_size;
1590         hash_objs[0].data = plain_out.data;
1591         hash_objs[1].len = gsshdr->len;
1592         hash_objs[1].data = gsshdr->data;
1593         hash_objs[2].len = plain_out.len - ke->ke_conf_size - sizeof(*khdr);
1594         hash_objs[2].data = plain_out.data + ke->ke_conf_size;
1595         if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyi,
1596                                khdr, 3, hash_objs, 0, NULL, &cksum))
1597                 goto out_free;
1598
1599         LASSERT(cksum.len >= ke->ke_hash_size);
1600         if (memcmp((char *)(khdr + 1) + bodysize,
1601                    cksum.data + cksum.len - ke->ke_hash_size,
1602                    ke->ke_hash_size)) {
1603                 CERROR("checksum mismatch\n");
1604                 goto out_free;
1605         }
1606
1607         msg->len =  bodysize - ke->ke_conf_size - sizeof(*khdr);
1608         memcpy(msg->data, tmpbuf + ke->ke_conf_size, msg->len);
1609
1610         major = GSS_S_COMPLETE;
1611 out_free:
1612         OBD_FREE_LARGE(tmpbuf, bodysize);
1613         rawobj_free(&cksum);
1614         return major;
1615 }
1616
1617 static
1618 __u32 gss_unwrap_bulk_kerberos(struct gss_ctx *gctx,
1619                                struct ptlrpc_bulk_desc *desc,
1620                                rawobj_t *token, int adj_nob)
1621 {
1622         struct krb5_ctx     *kctx = gctx->internal_ctx_id;
1623         struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
1624         struct krb5_header  *khdr;
1625         int                  blocksize;
1626         rawobj_t             cksum = RAWOBJ_EMPTY;
1627         rawobj_t             cipher, plain;
1628         rawobj_t             data_desc[1];
1629         int                  rc;
1630         __u32                major;
1631
1632         LASSERT(ke);
1633
1634         if (token->len < sizeof(*khdr)) {
1635                 CERROR("short signature: %u\n", token->len);
1636                 return GSS_S_DEFECTIVE_TOKEN;
1637         }
1638
1639         khdr = (struct krb5_header *) token->data;
1640
1641         major = verify_krb5_header(kctx, khdr, 1);
1642         if (major != GSS_S_COMPLETE) {
1643                 CERROR("bad krb5 header\n");
1644                 return major;
1645         }
1646
1647         /* block size */
1648         if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1649                 LASSERT(kctx->kc_keye.kb_tfm == NULL);
1650                 blocksize = 1;
1651                 LBUG();
1652         } else {
1653                 LASSERT(kctx->kc_keye.kb_tfm);
1654                 blocksize = crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
1655         }
1656         LASSERT(sizeof(*khdr) >= blocksize && sizeof(*khdr) % blocksize == 0);
1657
1658         /*
1659          * token format is expected as:
1660          * -----------------------------------------------
1661          * | krb5 header | head/tail cipher text | cksum |
1662          * -----------------------------------------------
1663          */
1664         if (token->len < sizeof(*khdr) + blocksize + sizeof(*khdr) +
1665                          ke->ke_hash_size) {
1666                 CERROR("short token size: %u\n", token->len);
1667                 return GSS_S_DEFECTIVE_TOKEN;
1668         }
1669
1670         cipher.data = (__u8 *) (khdr + 1);
1671         cipher.len = blocksize + sizeof(*khdr);
1672         plain.data = cipher.data;
1673         plain.len = cipher.len;
1674
1675         rc = krb5_decrypt_bulk(kctx->kc_keye.kb_tfm, khdr,
1676                                desc, &cipher, &plain, adj_nob);
1677         if (rc)
1678                 return GSS_S_DEFECTIVE_TOKEN;
1679
1680         /*
1681          * verify checksum, compose clear text as layout:
1682          * ------------------------------------------
1683          * | confounder | clear pages | krb5 header |
1684          * ------------------------------------------
1685          */
1686         data_desc[0].data = plain.data;
1687         data_desc[0].len = blocksize;
1688
1689         if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyi,
1690                                khdr, 1, data_desc,
1691                                desc->bd_iov_count, desc->bd_iov,
1692                                &cksum))
1693                 return GSS_S_FAILURE;
1694         LASSERT(cksum.len >= ke->ke_hash_size);
1695
1696         if (memcmp(plain.data + blocksize + sizeof(*khdr),
1697                    cksum.data + cksum.len - ke->ke_hash_size,
1698                    ke->ke_hash_size)) {
1699                 CERROR("checksum mismatch\n");
1700                 rawobj_free(&cksum);
1701                 return GSS_S_BAD_SIG;
1702         }
1703
1704         rawobj_free(&cksum);
1705         return GSS_S_COMPLETE;
1706 }
1707
1708 int gss_display_kerberos(struct gss_ctx        *ctx,
1709                          char                  *buf,
1710                          int                    bufsize)
1711 {
1712         struct krb5_ctx    *kctx = ctx->internal_ctx_id;
1713         int                 written;
1714
1715         written = snprintf(buf, bufsize, "krb5 (%s)",
1716                            enctype2str(kctx->kc_enctype));
1717         return written;
1718 }
1719
1720 static struct gss_api_ops gss_kerberos_ops = {
1721         .gss_import_sec_context     = gss_import_sec_context_kerberos,
1722         .gss_copy_reverse_context   = gss_copy_reverse_context_kerberos,
1723         .gss_inquire_context        = gss_inquire_context_kerberos,
1724         .gss_get_mic                = gss_get_mic_kerberos,
1725         .gss_verify_mic             = gss_verify_mic_kerberos,
1726         .gss_wrap                   = gss_wrap_kerberos,
1727         .gss_unwrap                 = gss_unwrap_kerberos,
1728         .gss_prep_bulk              = gss_prep_bulk_kerberos,
1729         .gss_wrap_bulk              = gss_wrap_bulk_kerberos,
1730         .gss_unwrap_bulk            = gss_unwrap_bulk_kerberos,
1731         .gss_delete_sec_context     = gss_delete_sec_context_kerberos,
1732         .gss_display                = gss_display_kerberos,
1733 };
1734
1735 static struct subflavor_desc gss_kerberos_sfs[] = {
1736         {
1737                 .sf_subflavor   = SPTLRPC_SUBFLVR_KRB5N,
1738                 .sf_qop         = 0,
1739                 .sf_service     = SPTLRPC_SVC_NULL,
1740                 .sf_name        = "krb5n"
1741         },
1742         {
1743                 .sf_subflavor   = SPTLRPC_SUBFLVR_KRB5A,
1744                 .sf_qop         = 0,
1745                 .sf_service     = SPTLRPC_SVC_AUTH,
1746                 .sf_name        = "krb5a"
1747         },
1748         {
1749                 .sf_subflavor   = SPTLRPC_SUBFLVR_KRB5I,
1750                 .sf_qop         = 0,
1751                 .sf_service     = SPTLRPC_SVC_INTG,
1752                 .sf_name        = "krb5i"
1753         },
1754         {
1755                 .sf_subflavor   = SPTLRPC_SUBFLVR_KRB5P,
1756                 .sf_qop         = 0,
1757                 .sf_service     = SPTLRPC_SVC_PRIV,
1758                 .sf_name        = "krb5p"
1759         },
1760 };
1761
1762 /*
1763  * currently we leave module owner NULL
1764  */
1765 static struct gss_api_mech gss_kerberos_mech = {
1766         .gm_owner       = NULL, /*THIS_MODULE, */
1767         .gm_name        = "krb5",
1768         .gm_oid         = (rawobj_t)
1769                                 {9, "\052\206\110\206\367\022\001\002\002"},
1770         .gm_ops         = &gss_kerberos_ops,
1771         .gm_sf_num      = 4,
1772         .gm_sfs         = gss_kerberos_sfs,
1773 };
1774
1775 int __init init_kerberos_module(void)
1776 {
1777         int status;
1778
1779         spin_lock_init(&krb5_seq_lock);
1780
1781         status = lgss_mech_register(&gss_kerberos_mech);
1782         if (status)
1783                 CERROR("Failed to register kerberos gss mechanism!\n");
1784         return status;
1785 }
1786
1787 void cleanup_kerberos_module(void)
1788 {
1789         lgss_mech_unregister(&gss_kerberos_mech);
1790 }