2 * Modifications for Lustre
4 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
6 * Copyright (c) 2011, 2014, Intel Corporation.
8 * Author: Eric Mei <ericm@clusterfs.com>
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
18 * Copyright (c) 2001 The Regents of the University of Michigan.
19 * All rights reserved.
21 * Andy Adamson <andros@umich.edu>
22 * J. Bruce Fields <bfields@umich.edu>
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
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.
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.
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>
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>
67 #include "gss_internal.h"
72 static spinlock_t krb5_seq_lock;
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? */
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.
89 static struct krb5_enctype enctypes[] = {
90 [ENCTYPE_DES_CBC_RAW] = { /* des-cbc-md5 */
99 [ENCTYPE_DES3_CBC_RAW] = { /* des3-hmac-sha1 */
108 [ENCTYPE_AES128_CTS_HMAC_SHA1_96] = { /* aes128-cts */
109 "aes128-cts-hmac-sha1-96",
117 [ENCTYPE_AES256_CTS_HMAC_SHA1_96] = { /* aes256-cts */
118 "aes256-cts-hmac-sha1-96",
126 [ENCTYPE_ARCFOUR_HMAC] = { /* arcfour-hmac-md5 */
137 #define MAX_ENCTYPES sizeof(enctypes)/sizeof(struct krb5_enctype)
139 static const char * enctype2str(__u32 enctype)
141 if (enctype < MAX_ENCTYPES && enctypes[enctype].ke_dispname)
142 return enctypes[enctype].ke_dispname;
148 int keyblock_init(struct krb5_keyblock *kb, char *alg_name, int alg_mode)
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",
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);
167 int krb5_init_keys(struct krb5_ctx *kctx)
169 struct krb5_enctype *ke;
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);
177 ke = &enctypes[kctx->kc_enctype];
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))
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))
188 if (ke->ke_hash_hmac == 0 &&
189 keyblock_init(&kctx->kc_keyc, ke->ke_enc_name, ke->ke_enc_mode))
196 void keyblock_free(struct krb5_keyblock *kb)
198 rawobj_free(&kb->kb_key);
200 crypto_free_blkcipher(kb->kb_tfm);
204 int keyblock_dup(struct krb5_keyblock *new, struct krb5_keyblock *kb)
206 return rawobj_dup(&new->kb_key, &kb->kb_key);
210 int get_bytes(char **ptr, const char *end, void *res, int len)
215 if (q > end || q < p)
223 int get_rawobj(char **ptr, const char *end, rawobj_t *res)
229 if (get_bytes(&p, end, &len, sizeof(len)))
233 if (q > end || q < p)
236 OBD_ALLOC_LARGE(res->data, len);
241 memcpy(res->data, p, len);
247 int get_keyblock(char **ptr, const char *end,
248 struct krb5_keyblock *kb, __u32 keysize)
252 OBD_ALLOC_LARGE(buf, keysize);
256 if (get_bytes(ptr, end, buf, keysize)) {
257 OBD_FREE_LARGE(buf, keysize);
261 kb->kb_key.len = keysize;
262 kb->kb_key.data = buf;
267 void delete_context_kerberos(struct krb5_ctx *kctx)
269 rawobj_free(&kctx->kc_mech_used);
271 keyblock_free(&kctx->kc_keye);
272 keyblock_free(&kctx->kc_keyi);
273 keyblock_free(&kctx->kc_keyc);
277 __u32 import_context_rfc1964(struct krb5_ctx *kctx, char *p, char *end)
279 unsigned int tmp_uint, keysize;
282 if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
284 kctx->kc_seed_init = (tmp_uint != 0);
287 if (get_bytes(&p, end, kctx->kc_seed, sizeof(kctx->kc_seed)))
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)))
296 if (get_bytes(&p, end, &kctx->kc_endtime, sizeof(kctx->kc_endtime)))
300 if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
302 kctx->kc_seq_send = tmp_uint;
305 if (get_rawobj(&p, end, &kctx->kc_mech_used))
308 /* old style enc/seq keys in format:
312 * we decompose them to fit into the new context
316 if (get_bytes(&p, end, &kctx->kc_enctype, sizeof(kctx->kc_enctype)))
319 if (get_bytes(&p, end, &keysize, sizeof(keysize)))
322 if (get_keyblock(&p, end, &kctx->kc_keye, keysize))
326 if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)) ||
327 tmp_uint != kctx->kc_enctype)
330 if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)) ||
334 if (get_keyblock(&p, end, &kctx->kc_keyc, keysize))
337 /* old style fallback */
338 if (keyblock_dup(&kctx->kc_keyi, &kctx->kc_keyc))
344 CDEBUG(D_SEC, "succesfully imported rfc1964 context\n");
347 return GSS_S_FAILURE;
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
356 __u32 import_context_rfc4121(struct krb5_ctx *kctx, char *p, char *end)
358 unsigned int tmp_uint, keysize;
361 if (get_bytes(&p, end, &kctx->kc_endtime, sizeof(kctx->kc_endtime)))
365 if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
368 if (tmp_uint & KRB5_CTX_FLAG_INITIATOR)
369 kctx->kc_initiate = 1;
370 if (tmp_uint & KRB5_CTX_FLAG_CFX)
372 if (tmp_uint & KRB5_CTX_FLAG_ACCEPTOR_SUBKEY)
373 kctx->kc_have_acceptor_subkey = 1;
376 if (get_bytes(&p, end, &kctx->kc_seq_send, sizeof(kctx->kc_seq_send)))
380 if (get_bytes(&p, end, &kctx->kc_enctype, sizeof(kctx->kc_enctype)))
383 /* size of each key */
384 if (get_bytes(&p, end, &keysize, sizeof(keysize)))
387 /* number of keys - should always be 3 */
388 if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
392 CERROR("Invalid number of keys: %u\n", tmp_uint);
397 if (get_keyblock(&p, end, &kctx->kc_keye, keysize))
400 if (get_keyblock(&p, end, &kctx->kc_keyi, keysize))
403 if (get_keyblock(&p, end, &kctx->kc_keyc, keysize))
406 CDEBUG(D_SEC, "succesfully imported v2 context\n");
409 return GSS_S_FAILURE;
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.
418 __u32 gss_import_sec_context_kerberos(rawobj_t *inbuf,
419 struct gss_ctx *gctx)
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;
426 if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint))) {
427 CERROR("Fail to read version\n");
428 return GSS_S_FAILURE;
431 /* only support 0, 1 for the moment */
433 CERROR("Invalid version %u\n", tmp_uint);
434 return GSS_S_FAILURE;
439 return GSS_S_FAILURE;
441 if (tmp_uint == 0 || tmp_uint == 1) {
442 kctx->kc_initiate = tmp_uint;
443 rc = import_context_rfc1964(kctx, p, end);
445 rc = import_context_rfc4121(kctx, p, end);
449 rc = krb5_init_keys(kctx);
452 delete_context_kerberos(kctx);
455 return GSS_S_FAILURE;
458 gctx->internal_ctx_id = kctx;
459 return GSS_S_COMPLETE;
463 __u32 gss_copy_reverse_context_kerberos(struct gss_ctx *gctx,
464 struct gss_ctx *gctx_new)
466 struct krb5_ctx *kctx = gctx->internal_ctx_id;
467 struct krb5_ctx *knew;
471 return GSS_S_FAILURE;
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;
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;
484 if (rawobj_dup(&knew->kc_mech_used, &kctx->kc_mech_used))
487 if (keyblock_dup(&knew->kc_keye, &kctx->kc_keye))
489 if (keyblock_dup(&knew->kc_keyi, &kctx->kc_keyi))
491 if (keyblock_dup(&knew->kc_keyc, &kctx->kc_keyc))
493 if (krb5_init_keys(knew))
496 gctx_new->internal_ctx_id = knew;
497 CDEBUG(D_SEC, "succesfully copied reverse context\n");
498 return GSS_S_COMPLETE;
501 delete_context_kerberos(knew);
503 return GSS_S_FAILURE;
507 __u32 gss_inquire_context_kerberos(struct gss_ctx *gctx,
508 unsigned long *endtime)
510 struct krb5_ctx *kctx = gctx->internal_ctx_id;
512 *endtime = (unsigned long) ((__u32) kctx->kc_endtime);
513 return GSS_S_COMPLETE;
517 void gss_delete_sec_context_kerberos(void *internal_ctx)
519 struct krb5_ctx *kctx = internal_ctx;
521 delete_context_kerberos(kctx);
526 void buf_to_sg(struct scatterlist *sg, void *ptr, int len)
528 sg_init_table(sg, 1);
529 sg_set_buf(sg, ptr, len);
533 __u32 krb5_encrypt(struct crypto_blkcipher *tfm,
540 struct blkcipher_desc desc;
541 struct scatterlist sg;
542 __u8 local_iv[16] = {0};
547 desc.info = local_iv;
550 if (length % crypto_blkcipher_blocksize(tfm) != 0) {
551 CERROR("output length %d mismatch blocksize %d\n",
552 length, crypto_blkcipher_blocksize(tfm));
556 if (crypto_blkcipher_ivsize(tfm) > 16) {
557 CERROR("iv size too large %d\n", crypto_blkcipher_ivsize(tfm));
562 memcpy(local_iv, iv, crypto_blkcipher_ivsize(tfm));
564 memcpy(out, in, length);
565 buf_to_sg(&sg, out, length);
568 ret = crypto_blkcipher_decrypt_iv(&desc, &sg, &sg, length);
570 ret = crypto_blkcipher_encrypt_iv(&desc, &sg, &sg, length);
577 int krb5_digest_hmac(struct crypto_hash *tfm,
579 struct krb5_header *khdr,
580 int msgcnt, rawobj_t *msgs,
581 int iovcnt, lnet_kiov_t *iovs,
584 struct hash_desc desc;
585 struct scatterlist sg[1];
588 crypto_hash_setkey(tfm, key->data, key->len);
592 crypto_hash_init(&desc);
594 for (i = 0; i < msgcnt; i++) {
595 if (msgs[i].len == 0)
597 buf_to_sg(sg, (char *) msgs[i].data, msgs[i].len);
598 crypto_hash_update(&desc, sg, msgs[i].len);
601 for (i = 0; i < iovcnt; i++) {
602 if (iovs[i].kiov_len == 0)
605 sg_set_page(&sg[0], iovs[i].kiov_page, iovs[i].kiov_len,
606 iovs[i].kiov_offset);
607 crypto_hash_update(&desc, sg, iovs[i].kiov_len);
611 buf_to_sg(sg, (char *) khdr, sizeof(*khdr));
612 crypto_hash_update(&desc, sg, sizeof(*khdr));
615 return crypto_hash_final(&desc, cksum->data);
619 int krb5_digest_norm(struct crypto_hash *tfm,
620 struct krb5_keyblock *kb,
621 struct krb5_header *khdr,
622 int msgcnt, rawobj_t *msgs,
623 int iovcnt, lnet_kiov_t *iovs,
626 struct hash_desc desc;
627 struct scatterlist sg[1];
634 crypto_hash_init(&desc);
636 for (i = 0; i < msgcnt; i++) {
637 if (msgs[i].len == 0)
639 buf_to_sg(sg, (char *) msgs[i].data, msgs[i].len);
640 crypto_hash_update(&desc, sg, msgs[i].len);
643 for (i = 0; i < iovcnt; i++) {
644 if (iovs[i].kiov_len == 0)
647 sg_set_page(&sg[0], iovs[i].kiov_page, iovs[i].kiov_len,
648 iovs[i].kiov_offset);
649 crypto_hash_update(&desc, sg, iovs[i].kiov_len);
653 buf_to_sg(sg, (char *) khdr, sizeof(*khdr));
654 crypto_hash_update(&desc, sg, sizeof(*khdr));
657 crypto_hash_final(&desc, cksum->data);
659 return krb5_encrypt(kb->kb_tfm, 0, NULL, cksum->data,
660 cksum->data, cksum->len);
664 * compute (keyed/keyless) checksum against the plain text which appended
665 * with krb5 wire token header.
668 __s32 krb5_make_checksum(__u32 enctype,
669 struct krb5_keyblock *kb,
670 struct krb5_header *khdr,
671 int msgcnt, rawobj_t *msgs,
672 int iovcnt, lnet_kiov_t *iovs,
675 struct krb5_enctype *ke = &enctypes[enctype];
676 struct crypto_hash *tfm;
677 __u32 code = GSS_S_FAILURE;
680 if (!(tfm = crypto_alloc_hash(ke->ke_hash_name, 0, 0))) {
681 CERROR("failed to alloc TFM: %s\n", ke->ke_hash_name);
682 return GSS_S_FAILURE;
685 cksum->len = crypto_hash_digestsize(tfm);
686 OBD_ALLOC_LARGE(cksum->data, cksum->len);
692 if (ke->ke_hash_hmac)
693 rc = krb5_digest_hmac(tfm, &kb->kb_key,
694 khdr, msgcnt, msgs, iovcnt, iovs, cksum);
696 rc = krb5_digest_norm(tfm, kb,
697 khdr, msgcnt, msgs, iovcnt, iovs, cksum);
700 code = GSS_S_COMPLETE;
702 crypto_free_hash(tfm);
706 static void fill_krb5_header(struct krb5_ctx *kctx,
707 struct krb5_header *khdr,
710 unsigned char acceptor_flag;
712 acceptor_flag = kctx->kc_initiate ? 0 : FLAG_SENDER_IS_ACCEPTOR;
715 khdr->kh_tok_id = cpu_to_be16(KG_TOK_WRAP_MSG);
716 khdr->kh_flags = acceptor_flag | FLAG_WRAP_CONFIDENTIAL;
717 khdr->kh_ec = cpu_to_be16(0);
718 khdr->kh_rrc = cpu_to_be16(0);
720 khdr->kh_tok_id = cpu_to_be16(KG_TOK_MIC_MSG);
721 khdr->kh_flags = acceptor_flag;
722 khdr->kh_ec = cpu_to_be16(0xffff);
723 khdr->kh_rrc = cpu_to_be16(0xffff);
726 khdr->kh_filler = 0xff;
727 spin_lock(&krb5_seq_lock);
728 khdr->kh_seq = cpu_to_be64(kctx->kc_seq_send++);
729 spin_unlock(&krb5_seq_lock);
732 static __u32 verify_krb5_header(struct krb5_ctx *kctx,
733 struct krb5_header *khdr,
736 unsigned char acceptor_flag;
737 __u16 tok_id, ec_rrc;
739 acceptor_flag = kctx->kc_initiate ? FLAG_SENDER_IS_ACCEPTOR : 0;
742 tok_id = KG_TOK_WRAP_MSG;
745 tok_id = KG_TOK_MIC_MSG;
750 if (be16_to_cpu(khdr->kh_tok_id) != tok_id) {
751 CERROR("bad token id\n");
752 return GSS_S_DEFECTIVE_TOKEN;
754 if ((khdr->kh_flags & FLAG_SENDER_IS_ACCEPTOR) != acceptor_flag) {
755 CERROR("bad direction flag\n");
756 return GSS_S_BAD_SIG;
758 if (privacy && (khdr->kh_flags & FLAG_WRAP_CONFIDENTIAL) == 0) {
759 CERROR("missing confidential flag\n");
760 return GSS_S_BAD_SIG;
762 if (khdr->kh_filler != 0xff) {
763 CERROR("bad filler\n");
764 return GSS_S_DEFECTIVE_TOKEN;
766 if (be16_to_cpu(khdr->kh_ec) != ec_rrc ||
767 be16_to_cpu(khdr->kh_rrc) != ec_rrc) {
768 CERROR("bad EC or RRC\n");
769 return GSS_S_DEFECTIVE_TOKEN;
771 return GSS_S_COMPLETE;
775 __u32 gss_get_mic_kerberos(struct gss_ctx *gctx,
782 struct krb5_ctx *kctx = gctx->internal_ctx_id;
783 struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
784 struct krb5_header *khdr;
785 rawobj_t cksum = RAWOBJ_EMPTY;
787 /* fill krb5 header */
788 LASSERT(token->len >= sizeof(*khdr));
789 khdr = (struct krb5_header *) token->data;
790 fill_krb5_header(kctx, khdr, 0);
793 if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyc,
794 khdr, msgcnt, msgs, iovcnt, iovs, &cksum))
795 return GSS_S_FAILURE;
797 LASSERT(cksum.len >= ke->ke_hash_size);
798 LASSERT(token->len >= sizeof(*khdr) + ke->ke_hash_size);
799 memcpy(khdr + 1, cksum.data + cksum.len - ke->ke_hash_size,
802 token->len = sizeof(*khdr) + ke->ke_hash_size;
804 return GSS_S_COMPLETE;
808 __u32 gss_verify_mic_kerberos(struct gss_ctx *gctx,
815 struct krb5_ctx *kctx = gctx->internal_ctx_id;
816 struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
817 struct krb5_header *khdr;
818 rawobj_t cksum = RAWOBJ_EMPTY;
821 if (token->len < sizeof(*khdr)) {
822 CERROR("short signature: %u\n", token->len);
823 return GSS_S_DEFECTIVE_TOKEN;
826 khdr = (struct krb5_header *) token->data;
828 major = verify_krb5_header(kctx, khdr, 0);
829 if (major != GSS_S_COMPLETE) {
830 CERROR("bad krb5 header\n");
834 if (token->len < sizeof(*khdr) + ke->ke_hash_size) {
835 CERROR("short signature: %u, require %d\n",
836 token->len, (int) sizeof(*khdr) + ke->ke_hash_size);
837 return GSS_S_FAILURE;
840 if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyc,
841 khdr, msgcnt, msgs, iovcnt, iovs, &cksum)) {
842 CERROR("failed to make checksum\n");
843 return GSS_S_FAILURE;
846 LASSERT(cksum.len >= ke->ke_hash_size);
847 if (memcmp(khdr + 1, cksum.data + cksum.len - ke->ke_hash_size,
849 CERROR("checksum mismatch\n");
851 return GSS_S_BAD_SIG;
855 return GSS_S_COMPLETE;
859 int add_padding(rawobj_t *msg, int msg_buflen, int blocksize)
863 padding = (blocksize - (msg->len & (blocksize - 1))) &
868 if (msg->len + padding > msg_buflen) {
869 CERROR("bufsize %u too small: datalen %u, padding %u\n",
870 msg_buflen, msg->len, padding);
874 memset(msg->data + msg->len, padding, padding);
880 int krb5_encrypt_rawobjs(struct crypto_blkcipher *tfm,
887 struct blkcipher_desc desc;
888 struct scatterlist src, dst;
889 __u8 local_iv[16] = {0}, *buf;
896 desc.info = local_iv;
899 for (i = 0; i < inobj_cnt; i++) {
900 LASSERT(buf + inobjs[i].len <= outobj->data + outobj->len);
902 buf_to_sg(&src, inobjs[i].data, inobjs[i].len);
903 buf_to_sg(&dst, buf, outobj->len - datalen);
907 rc = crypto_blkcipher_encrypt(
908 &desc, &dst, &src, src.length);
910 rc = crypto_blkcipher_decrypt(
911 &desc, &dst, &src, src.length);
914 rc = crypto_blkcipher_encrypt_iv(
915 &desc, &dst, &src, src.length);
917 rc = crypto_blkcipher_decrypt_iv(
918 &desc, &dst, &src, src.length);
922 CERROR("encrypt error %d\n", rc);
926 datalen += inobjs[i].len;
927 buf += inobjs[i].len;
930 outobj->len = datalen;
935 * if adj_nob != 0, we adjust desc->bd_nob to the actual cipher text size.
938 int krb5_encrypt_bulk(struct crypto_blkcipher *tfm,
939 struct krb5_header *khdr,
941 struct ptlrpc_bulk_desc *desc,
945 struct blkcipher_desc ciph_desc;
946 __u8 local_iv[16] = {0};
947 struct scatterlist src, dst;
948 int blocksize, i, rc, nob = 0;
950 LASSERT(desc->bd_iov_count);
951 LASSERT(desc->bd_enc_iov);
953 blocksize = crypto_blkcipher_blocksize(tfm);
954 LASSERT(blocksize > 1);
955 LASSERT(cipher->len == blocksize + sizeof(*khdr));
958 ciph_desc.info = local_iv;
961 /* encrypt confounder */
962 buf_to_sg(&src, confounder, blocksize);
963 buf_to_sg(&dst, cipher->data, blocksize);
965 rc = crypto_blkcipher_encrypt_iv(&ciph_desc, &dst, &src, blocksize);
967 CERROR("error to encrypt confounder: %d\n", rc);
971 /* encrypt clear pages */
972 for (i = 0; i < desc->bd_iov_count; i++) {
973 sg_set_page(&src, desc->bd_iov[i].kiov_page,
974 (desc->bd_iov[i].kiov_len + blocksize - 1) &
976 desc->bd_iov[i].kiov_offset);
979 sg_set_page(&dst, desc->bd_enc_iov[i].kiov_page, src.length,
982 desc->bd_enc_iov[i].kiov_offset = dst.offset;
983 desc->bd_enc_iov[i].kiov_len = dst.length;
985 rc = crypto_blkcipher_encrypt_iv(&ciph_desc, &dst, &src,
988 CERROR("error to encrypt page: %d\n", rc);
993 /* encrypt krb5 header */
994 buf_to_sg(&src, khdr, sizeof(*khdr));
995 buf_to_sg(&dst, cipher->data + blocksize, sizeof(*khdr));
997 rc = crypto_blkcipher_encrypt_iv(&ciph_desc, &dst, &src,
1000 CERROR("error to encrypt krb5 header: %d\n", rc);
1011 * desc->bd_nob_transferred is the size of cipher text received.
1012 * desc->bd_nob is the target size of plain text supposed to be.
1014 * if adj_nob != 0, we adjust each page's kiov_len to the actual
1016 * - for client read: we don't know data size for each page, so
1017 * bd_iov[]->kiov_len is set to PAGE_SIZE, but actual data received might
1018 * be smaller, so we need to adjust it according to bd_enc_iov[]->kiov_len.
1019 * this means we DO NOT support the situation that server send an odd size
1020 * data in a page which is not the last one.
1021 * - for server write: we knows exactly data size for each page being expected,
1022 * thus kiov_len is accurate already, so we should not adjust it at all.
1023 * and bd_enc_iov[]->kiov_len should be round_up(bd_iov[]->kiov_len) which
1024 * should have been done by prep_bulk().
1027 int krb5_decrypt_bulk(struct crypto_blkcipher *tfm,
1028 struct krb5_header *khdr,
1029 struct ptlrpc_bulk_desc *desc,
1034 struct blkcipher_desc ciph_desc;
1035 __u8 local_iv[16] = {0};
1036 struct scatterlist src, dst;
1037 int ct_nob = 0, pt_nob = 0;
1038 int blocksize, i, rc;
1040 LASSERT(desc->bd_iov_count);
1041 LASSERT(desc->bd_enc_iov);
1042 LASSERT(desc->bd_nob_transferred);
1044 blocksize = crypto_blkcipher_blocksize(tfm);
1045 LASSERT(blocksize > 1);
1046 LASSERT(cipher->len == blocksize + sizeof(*khdr));
1048 ciph_desc.tfm = tfm;
1049 ciph_desc.info = local_iv;
1050 ciph_desc.flags = 0;
1052 if (desc->bd_nob_transferred % blocksize) {
1053 CERROR("odd transferred nob: %d\n", desc->bd_nob_transferred);
1057 /* decrypt head (confounder) */
1058 buf_to_sg(&src, cipher->data, blocksize);
1059 buf_to_sg(&dst, plain->data, blocksize);
1061 rc = crypto_blkcipher_decrypt_iv(&ciph_desc, &dst, &src, blocksize);
1063 CERROR("error to decrypt confounder: %d\n", rc);
1067 for (i = 0; i < desc->bd_iov_count && ct_nob < desc->bd_nob_transferred;
1069 if (desc->bd_enc_iov[i].kiov_offset % blocksize != 0 ||
1070 desc->bd_enc_iov[i].kiov_len % blocksize != 0) {
1071 CERROR("page %d: odd offset %u len %u, blocksize %d\n",
1072 i, desc->bd_enc_iov[i].kiov_offset,
1073 desc->bd_enc_iov[i].kiov_len, blocksize);
1078 if (ct_nob + desc->bd_enc_iov[i].kiov_len >
1079 desc->bd_nob_transferred)
1080 desc->bd_enc_iov[i].kiov_len =
1081 desc->bd_nob_transferred - ct_nob;
1083 desc->bd_iov[i].kiov_len = desc->bd_enc_iov[i].kiov_len;
1084 if (pt_nob + desc->bd_enc_iov[i].kiov_len >desc->bd_nob)
1085 desc->bd_iov[i].kiov_len = desc->bd_nob -pt_nob;
1087 /* this should be guaranteed by LNET */
1088 LASSERT(ct_nob + desc->bd_enc_iov[i].kiov_len <=
1089 desc->bd_nob_transferred);
1090 LASSERT(desc->bd_iov[i].kiov_len <=
1091 desc->bd_enc_iov[i].kiov_len);
1094 if (desc->bd_enc_iov[i].kiov_len == 0)
1097 sg_set_page(&src, desc->bd_enc_iov[i].kiov_page,
1098 desc->bd_enc_iov[i].kiov_len,
1099 desc->bd_enc_iov[i].kiov_offset);
1101 if (desc->bd_iov[i].kiov_len % blocksize == 0)
1102 sg_assign_page(&dst, desc->bd_iov[i].kiov_page);
1104 rc = crypto_blkcipher_decrypt_iv(&ciph_desc, &dst, &src,
1107 CERROR("error to decrypt page: %d\n", rc);
1111 if (desc->bd_iov[i].kiov_len % blocksize != 0) {
1112 memcpy(page_address(desc->bd_iov[i].kiov_page) +
1113 desc->bd_iov[i].kiov_offset,
1114 page_address(desc->bd_enc_iov[i].kiov_page) +
1115 desc->bd_iov[i].kiov_offset,
1116 desc->bd_iov[i].kiov_len);
1119 ct_nob += desc->bd_enc_iov[i].kiov_len;
1120 pt_nob += desc->bd_iov[i].kiov_len;
1123 if (unlikely(ct_nob != desc->bd_nob_transferred)) {
1124 CERROR("%d cipher text transferred but only %d decrypted\n",
1125 desc->bd_nob_transferred, ct_nob);
1129 if (unlikely(!adj_nob && pt_nob != desc->bd_nob)) {
1130 CERROR("%d plain text expected but only %d received\n",
1131 desc->bd_nob, pt_nob);
1135 /* if needed, clear up the rest unused iovs */
1137 while (i < desc->bd_iov_count)
1138 desc->bd_iov[i++].kiov_len = 0;
1140 /* decrypt tail (krb5 header) */
1141 buf_to_sg(&src, cipher->data + blocksize, sizeof(*khdr));
1142 buf_to_sg(&dst, cipher->data + blocksize, sizeof(*khdr));
1144 rc = crypto_blkcipher_decrypt_iv(&ciph_desc, &dst, &src,
1147 CERROR("error to decrypt tail: %d\n", rc);
1151 if (memcmp(cipher->data + blocksize, khdr, sizeof(*khdr))) {
1152 CERROR("krb5 header doesn't match\n");
1160 __u32 gss_wrap_kerberos(struct gss_ctx *gctx,
1166 struct krb5_ctx *kctx = gctx->internal_ctx_id;
1167 struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
1168 struct krb5_header *khdr;
1170 rawobj_t cksum = RAWOBJ_EMPTY;
1171 rawobj_t data_desc[3], cipher;
1172 __u8 conf[GSS_MAX_CIPHER_BLOCK];
1176 LASSERT(ke->ke_conf_size <= GSS_MAX_CIPHER_BLOCK);
1177 LASSERT(kctx->kc_keye.kb_tfm == NULL ||
1179 crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm));
1182 * final token format:
1183 * ---------------------------------------------------
1184 * | krb5 header | cipher text | checksum (16 bytes) |
1185 * ---------------------------------------------------
1188 /* fill krb5 header */
1189 LASSERT(token->len >= sizeof(*khdr));
1190 khdr = (struct krb5_header *) token->data;
1191 fill_krb5_header(kctx, khdr, 1);
1193 /* generate confounder */
1194 cfs_get_random_bytes(conf, ke->ke_conf_size);
1196 /* get encryption blocksize. note kc_keye might not associated with
1197 * a tfm, currently only for arcfour-hmac */
1198 if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1199 LASSERT(kctx->kc_keye.kb_tfm == NULL);
1202 LASSERT(kctx->kc_keye.kb_tfm);
1203 blocksize = crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
1205 LASSERT(blocksize <= ke->ke_conf_size);
1207 /* padding the message */
1208 if (add_padding(msg, msg_buflen, blocksize))
1209 return GSS_S_FAILURE;
1212 * clear text layout for checksum:
1213 * ------------------------------------------------------
1214 * | confounder | gss header | clear msgs | krb5 header |
1215 * ------------------------------------------------------
1217 data_desc[0].data = conf;
1218 data_desc[0].len = ke->ke_conf_size;
1219 data_desc[1].data = gsshdr->data;
1220 data_desc[1].len = gsshdr->len;
1221 data_desc[2].data = msg->data;
1222 data_desc[2].len = msg->len;
1224 /* compute checksum */
1225 if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyi,
1226 khdr, 3, data_desc, 0, NULL, &cksum))
1227 return GSS_S_FAILURE;
1228 LASSERT(cksum.len >= ke->ke_hash_size);
1231 * clear text layout for encryption:
1232 * -----------------------------------------
1233 * | confounder | clear msgs | krb5 header |
1234 * -----------------------------------------
1236 data_desc[0].data = conf;
1237 data_desc[0].len = ke->ke_conf_size;
1238 data_desc[1].data = msg->data;
1239 data_desc[1].len = msg->len;
1240 data_desc[2].data = (__u8 *) khdr;
1241 data_desc[2].len = sizeof(*khdr);
1243 /* cipher text will be directly inplace */
1244 cipher.data = (__u8 *) (khdr + 1);
1245 cipher.len = token->len - sizeof(*khdr);
1246 LASSERT(cipher.len >= ke->ke_conf_size + msg->len + sizeof(*khdr));
1248 if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1250 struct crypto_blkcipher *arc4_tfm;
1252 if (krb5_make_checksum(ENCTYPE_ARCFOUR_HMAC, &kctx->kc_keyi,
1253 NULL, 1, &cksum, 0, NULL, &arc4_keye)) {
1254 CERROR("failed to obtain arc4 enc key\n");
1255 GOTO(arc4_out, rc = -EACCES);
1258 arc4_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0, 0);
1259 if (IS_ERR(arc4_tfm)) {
1260 CERROR("failed to alloc tfm arc4 in ECB mode\n");
1261 GOTO(arc4_out_key, rc = -EACCES);
1264 if (crypto_blkcipher_setkey(arc4_tfm, arc4_keye.data,
1266 CERROR("failed to set arc4 key, len %d\n",
1268 GOTO(arc4_out_tfm, rc = -EACCES);
1271 rc = krb5_encrypt_rawobjs(arc4_tfm, 1,
1272 3, data_desc, &cipher, 1);
1274 crypto_free_blkcipher(arc4_tfm);
1276 rawobj_free(&arc4_keye);
1278 do {} while(0); /* just to avoid compile warning */
1280 rc = krb5_encrypt_rawobjs(kctx->kc_keye.kb_tfm, 0,
1281 3, data_desc, &cipher, 1);
1285 rawobj_free(&cksum);
1286 return GSS_S_FAILURE;
1289 /* fill in checksum */
1290 LASSERT(token->len >= sizeof(*khdr) + cipher.len + ke->ke_hash_size);
1291 memcpy((char *)(khdr + 1) + cipher.len,
1292 cksum.data + cksum.len - ke->ke_hash_size,
1294 rawobj_free(&cksum);
1296 /* final token length */
1297 token->len = sizeof(*khdr) + cipher.len + ke->ke_hash_size;
1298 return GSS_S_COMPLETE;
1302 __u32 gss_prep_bulk_kerberos(struct gss_ctx *gctx,
1303 struct ptlrpc_bulk_desc *desc)
1305 struct krb5_ctx *kctx = gctx->internal_ctx_id;
1308 LASSERT(desc->bd_iov_count);
1309 LASSERT(desc->bd_enc_iov);
1310 LASSERT(kctx->kc_keye.kb_tfm);
1312 blocksize = crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
1314 for (i = 0; i < desc->bd_iov_count; i++) {
1315 LASSERT(desc->bd_enc_iov[i].kiov_page);
1317 * offset should always start at page boundary of either
1318 * client or server side.
1320 if (desc->bd_iov[i].kiov_offset & blocksize) {
1321 CERROR("odd offset %d in page %d\n",
1322 desc->bd_iov[i].kiov_offset, i);
1323 return GSS_S_FAILURE;
1326 desc->bd_enc_iov[i].kiov_offset = desc->bd_iov[i].kiov_offset;
1327 desc->bd_enc_iov[i].kiov_len = (desc->bd_iov[i].kiov_len +
1328 blocksize - 1) & (~(blocksize - 1));
1331 return GSS_S_COMPLETE;
1335 __u32 gss_wrap_bulk_kerberos(struct gss_ctx *gctx,
1336 struct ptlrpc_bulk_desc *desc,
1337 rawobj_t *token, int adj_nob)
1339 struct krb5_ctx *kctx = gctx->internal_ctx_id;
1340 struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
1341 struct krb5_header *khdr;
1343 rawobj_t cksum = RAWOBJ_EMPTY;
1344 rawobj_t data_desc[1], cipher;
1345 __u8 conf[GSS_MAX_CIPHER_BLOCK];
1349 LASSERT(ke->ke_conf_size <= GSS_MAX_CIPHER_BLOCK);
1352 * final token format:
1353 * --------------------------------------------------
1354 * | krb5 header | head/tail cipher text | checksum |
1355 * --------------------------------------------------
1358 /* fill krb5 header */
1359 LASSERT(token->len >= sizeof(*khdr));
1360 khdr = (struct krb5_header *) token->data;
1361 fill_krb5_header(kctx, khdr, 1);
1363 /* generate confounder */
1364 cfs_get_random_bytes(conf, ke->ke_conf_size);
1366 /* get encryption blocksize. note kc_keye might not associated with
1367 * a tfm, currently only for arcfour-hmac */
1368 if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1369 LASSERT(kctx->kc_keye.kb_tfm == NULL);
1372 LASSERT(kctx->kc_keye.kb_tfm);
1373 blocksize = crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
1377 * we assume the size of krb5_header (16 bytes) must be n * blocksize.
1378 * the bulk token size would be exactly (sizeof(krb5_header) +
1379 * blocksize + sizeof(krb5_header) + hashsize)
1381 LASSERT(blocksize <= ke->ke_conf_size);
1382 LASSERT(sizeof(*khdr) >= blocksize && sizeof(*khdr) % blocksize == 0);
1383 LASSERT(token->len >= sizeof(*khdr) + blocksize + sizeof(*khdr) + 16);
1386 * clear text layout for checksum:
1387 * ------------------------------------------
1388 * | confounder | clear pages | krb5 header |
1389 * ------------------------------------------
1391 data_desc[0].data = conf;
1392 data_desc[0].len = ke->ke_conf_size;
1394 /* compute checksum */
1395 if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyi,
1397 desc->bd_iov_count, desc->bd_iov,
1399 return GSS_S_FAILURE;
1400 LASSERT(cksum.len >= ke->ke_hash_size);
1403 * clear text layout for encryption:
1404 * ------------------------------------------
1405 * | confounder | clear pages | krb5 header |
1406 * ------------------------------------------
1408 * ---------- (cipher pages) |
1410 * -------------------------------------------
1411 * | krb5 header | cipher text | cipher text |
1412 * -------------------------------------------
1414 data_desc[0].data = conf;
1415 data_desc[0].len = ke->ke_conf_size;
1417 cipher.data = (__u8 *) (khdr + 1);
1418 cipher.len = blocksize + sizeof(*khdr);
1420 if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1424 rc = krb5_encrypt_bulk(kctx->kc_keye.kb_tfm, khdr,
1425 conf, desc, &cipher, adj_nob);
1429 rawobj_free(&cksum);
1430 return GSS_S_FAILURE;
1433 /* fill in checksum */
1434 LASSERT(token->len >= sizeof(*khdr) + cipher.len + ke->ke_hash_size);
1435 memcpy((char *)(khdr + 1) + cipher.len,
1436 cksum.data + cksum.len - ke->ke_hash_size,
1438 rawobj_free(&cksum);
1440 /* final token length */
1441 token->len = sizeof(*khdr) + cipher.len + ke->ke_hash_size;
1442 return GSS_S_COMPLETE;
1446 __u32 gss_unwrap_kerberos(struct gss_ctx *gctx,
1451 struct krb5_ctx *kctx = gctx->internal_ctx_id;
1452 struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
1453 struct krb5_header *khdr;
1454 unsigned char *tmpbuf;
1455 int blocksize, bodysize;
1456 rawobj_t cksum = RAWOBJ_EMPTY;
1457 rawobj_t cipher_in, plain_out;
1458 rawobj_t hash_objs[3];
1464 if (token->len < sizeof(*khdr)) {
1465 CERROR("short signature: %u\n", token->len);
1466 return GSS_S_DEFECTIVE_TOKEN;
1469 khdr = (struct krb5_header *) token->data;
1471 major = verify_krb5_header(kctx, khdr, 1);
1472 if (major != GSS_S_COMPLETE) {
1473 CERROR("bad krb5 header\n");
1478 if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1479 LASSERT(kctx->kc_keye.kb_tfm == NULL);
1482 LASSERT(kctx->kc_keye.kb_tfm);
1483 blocksize = crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
1486 /* expected token layout:
1487 * ----------------------------------------
1488 * | krb5 header | cipher text | checksum |
1489 * ----------------------------------------
1491 bodysize = token->len - sizeof(*khdr) - ke->ke_hash_size;
1493 if (bodysize % blocksize) {
1494 CERROR("odd bodysize %d\n", bodysize);
1495 return GSS_S_DEFECTIVE_TOKEN;
1498 if (bodysize <= ke->ke_conf_size + sizeof(*khdr)) {
1499 CERROR("incomplete token: bodysize %d\n", bodysize);
1500 return GSS_S_DEFECTIVE_TOKEN;
1503 if (msg->len < bodysize - ke->ke_conf_size - sizeof(*khdr)) {
1504 CERROR("buffer too small: %u, require %d\n",
1505 msg->len, bodysize - ke->ke_conf_size);
1506 return GSS_S_FAILURE;
1510 OBD_ALLOC_LARGE(tmpbuf, bodysize);
1512 return GSS_S_FAILURE;
1514 major = GSS_S_FAILURE;
1516 cipher_in.data = (__u8 *) (khdr + 1);
1517 cipher_in.len = bodysize;
1518 plain_out.data = tmpbuf;
1519 plain_out.len = bodysize;
1521 if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1523 struct crypto_blkcipher *arc4_tfm;
1525 cksum.data = token->data + token->len - ke->ke_hash_size;
1526 cksum.len = ke->ke_hash_size;
1528 if (krb5_make_checksum(ENCTYPE_ARCFOUR_HMAC, &kctx->kc_keyi,
1529 NULL, 1, &cksum, 0, NULL, &arc4_keye)) {
1530 CERROR("failed to obtain arc4 enc key\n");
1531 GOTO(arc4_out, rc = -EACCES);
1534 arc4_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0, 0);
1535 if (IS_ERR(arc4_tfm)) {
1536 CERROR("failed to alloc tfm arc4 in ECB mode\n");
1537 GOTO(arc4_out_key, rc = -EACCES);
1540 if (crypto_blkcipher_setkey(arc4_tfm,
1541 arc4_keye.data, arc4_keye.len)) {
1542 CERROR("failed to set arc4 key, len %d\n",
1544 GOTO(arc4_out_tfm, rc = -EACCES);
1547 rc = krb5_encrypt_rawobjs(arc4_tfm, 1,
1548 1, &cipher_in, &plain_out, 0);
1550 crypto_free_blkcipher(arc4_tfm);
1552 rawobj_free(&arc4_keye);
1554 cksum = RAWOBJ_EMPTY;
1556 rc = krb5_encrypt_rawobjs(kctx->kc_keye.kb_tfm, 0,
1557 1, &cipher_in, &plain_out, 0);
1561 CERROR("error decrypt\n");
1564 LASSERT(plain_out.len == bodysize);
1566 /* expected clear text layout:
1567 * -----------------------------------------
1568 * | confounder | clear msgs | krb5 header |
1569 * -----------------------------------------
1572 /* verify krb5 header in token is not modified */
1573 if (memcmp(khdr, plain_out.data + plain_out.len - sizeof(*khdr),
1575 CERROR("decrypted krb5 header mismatch\n");
1579 /* verify checksum, compose clear text as layout:
1580 * ------------------------------------------------------
1581 * | confounder | gss header | clear msgs | krb5 header |
1582 * ------------------------------------------------------
1584 hash_objs[0].len = ke->ke_conf_size;
1585 hash_objs[0].data = plain_out.data;
1586 hash_objs[1].len = gsshdr->len;
1587 hash_objs[1].data = gsshdr->data;
1588 hash_objs[2].len = plain_out.len - ke->ke_conf_size - sizeof(*khdr);
1589 hash_objs[2].data = plain_out.data + ke->ke_conf_size;
1590 if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyi,
1591 khdr, 3, hash_objs, 0, NULL, &cksum))
1594 LASSERT(cksum.len >= ke->ke_hash_size);
1595 if (memcmp((char *)(khdr + 1) + bodysize,
1596 cksum.data + cksum.len - ke->ke_hash_size,
1597 ke->ke_hash_size)) {
1598 CERROR("checksum mismatch\n");
1602 msg->len = bodysize - ke->ke_conf_size - sizeof(*khdr);
1603 memcpy(msg->data, tmpbuf + ke->ke_conf_size, msg->len);
1605 major = GSS_S_COMPLETE;
1607 OBD_FREE_LARGE(tmpbuf, bodysize);
1608 rawobj_free(&cksum);
1613 __u32 gss_unwrap_bulk_kerberos(struct gss_ctx *gctx,
1614 struct ptlrpc_bulk_desc *desc,
1615 rawobj_t *token, int adj_nob)
1617 struct krb5_ctx *kctx = gctx->internal_ctx_id;
1618 struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
1619 struct krb5_header *khdr;
1621 rawobj_t cksum = RAWOBJ_EMPTY;
1622 rawobj_t cipher, plain;
1623 rawobj_t data_desc[1];
1629 if (token->len < sizeof(*khdr)) {
1630 CERROR("short signature: %u\n", token->len);
1631 return GSS_S_DEFECTIVE_TOKEN;
1634 khdr = (struct krb5_header *) token->data;
1636 major = verify_krb5_header(kctx, khdr, 1);
1637 if (major != GSS_S_COMPLETE) {
1638 CERROR("bad krb5 header\n");
1643 if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1644 LASSERT(kctx->kc_keye.kb_tfm == NULL);
1648 LASSERT(kctx->kc_keye.kb_tfm);
1649 blocksize = crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
1651 LASSERT(sizeof(*khdr) >= blocksize && sizeof(*khdr) % blocksize == 0);
1654 * token format is expected as:
1655 * -----------------------------------------------
1656 * | krb5 header | head/tail cipher text | cksum |
1657 * -----------------------------------------------
1659 if (token->len < sizeof(*khdr) + blocksize + sizeof(*khdr) +
1661 CERROR("short token size: %u\n", token->len);
1662 return GSS_S_DEFECTIVE_TOKEN;
1665 cipher.data = (__u8 *) (khdr + 1);
1666 cipher.len = blocksize + sizeof(*khdr);
1667 plain.data = cipher.data;
1668 plain.len = cipher.len;
1670 rc = krb5_decrypt_bulk(kctx->kc_keye.kb_tfm, khdr,
1671 desc, &cipher, &plain, adj_nob);
1673 return GSS_S_DEFECTIVE_TOKEN;
1676 * verify checksum, compose clear text as layout:
1677 * ------------------------------------------
1678 * | confounder | clear pages | krb5 header |
1679 * ------------------------------------------
1681 data_desc[0].data = plain.data;
1682 data_desc[0].len = blocksize;
1684 if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyi,
1686 desc->bd_iov_count, desc->bd_iov,
1688 return GSS_S_FAILURE;
1689 LASSERT(cksum.len >= ke->ke_hash_size);
1691 if (memcmp(plain.data + blocksize + sizeof(*khdr),
1692 cksum.data + cksum.len - ke->ke_hash_size,
1693 ke->ke_hash_size)) {
1694 CERROR("checksum mismatch\n");
1695 rawobj_free(&cksum);
1696 return GSS_S_BAD_SIG;
1699 rawobj_free(&cksum);
1700 return GSS_S_COMPLETE;
1703 int gss_display_kerberos(struct gss_ctx *ctx,
1707 struct krb5_ctx *kctx = ctx->internal_ctx_id;
1710 written = snprintf(buf, bufsize, "krb5 (%s)",
1711 enctype2str(kctx->kc_enctype));
1715 static struct gss_api_ops gss_kerberos_ops = {
1716 .gss_import_sec_context = gss_import_sec_context_kerberos,
1717 .gss_copy_reverse_context = gss_copy_reverse_context_kerberos,
1718 .gss_inquire_context = gss_inquire_context_kerberos,
1719 .gss_get_mic = gss_get_mic_kerberos,
1720 .gss_verify_mic = gss_verify_mic_kerberos,
1721 .gss_wrap = gss_wrap_kerberos,
1722 .gss_unwrap = gss_unwrap_kerberos,
1723 .gss_prep_bulk = gss_prep_bulk_kerberos,
1724 .gss_wrap_bulk = gss_wrap_bulk_kerberos,
1725 .gss_unwrap_bulk = gss_unwrap_bulk_kerberos,
1726 .gss_delete_sec_context = gss_delete_sec_context_kerberos,
1727 .gss_display = gss_display_kerberos,
1730 static struct subflavor_desc gss_kerberos_sfs[] = {
1732 .sf_subflavor = SPTLRPC_SUBFLVR_KRB5N,
1734 .sf_service = SPTLRPC_SVC_NULL,
1738 .sf_subflavor = SPTLRPC_SUBFLVR_KRB5A,
1740 .sf_service = SPTLRPC_SVC_AUTH,
1744 .sf_subflavor = SPTLRPC_SUBFLVR_KRB5I,
1746 .sf_service = SPTLRPC_SVC_INTG,
1750 .sf_subflavor = SPTLRPC_SUBFLVR_KRB5P,
1752 .sf_service = SPTLRPC_SVC_PRIV,
1758 * currently we leave module owner NULL
1760 static struct gss_api_mech gss_kerberos_mech = {
1761 .gm_owner = NULL, /*THIS_MODULE, */
1763 .gm_oid = (rawobj_t)
1764 {9, "\052\206\110\206\367\022\001\002\002"},
1765 .gm_ops = &gss_kerberos_ops,
1767 .gm_sfs = gss_kerberos_sfs,
1770 int __init init_kerberos_module(void)
1774 spin_lock_init(&krb5_seq_lock);
1776 status = lgss_mech_register(&gss_kerberos_mech);
1778 CERROR("Failed to register kerberos gss mechanism!\n");
1782 void cleanup_kerberos_module(void)
1784 lgss_mech_unregister(&gss_kerberos_mech);