2 * Modifications for Lustre
4 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
6 * Copyright (c) 2011, 2015, 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, "successfully 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, "successfully 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, "successfully 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_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);
612 buf_to_sg(sg, (char *) khdr, sizeof(*khdr));
613 crypto_hash_update(&desc, sg, sizeof(*khdr));
616 return crypto_hash_final(&desc, cksum->data);
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,
627 struct hash_desc desc;
628 struct scatterlist sg[1];
635 crypto_hash_init(&desc);
637 for (i = 0; i < msgcnt; i++) {
638 if (msgs[i].len == 0)
640 buf_to_sg(sg, (char *) msgs[i].data, msgs[i].len);
641 crypto_hash_update(&desc, sg, msgs[i].len);
644 for (i = 0; i < iovcnt; i++) {
645 if (iovs[i].kiov_len == 0)
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);
655 buf_to_sg(sg, (char *) khdr, sizeof(*khdr));
656 crypto_hash_update(&desc, sg, sizeof(*khdr));
659 crypto_hash_final(&desc, cksum->data);
661 return krb5_encrypt(kb->kb_tfm, 0, NULL, cksum->data,
662 cksum->data, cksum->len);
666 * compute (keyed/keyless) checksum against the plain text which appended
667 * with krb5 wire token header.
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,
677 struct krb5_enctype *ke = &enctypes[enctype];
678 struct crypto_hash *tfm;
679 __u32 code = GSS_S_FAILURE;
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;
687 cksum->len = crypto_hash_digestsize(tfm);
688 OBD_ALLOC_LARGE(cksum->data, cksum->len);
694 if (ke->ke_hash_hmac)
695 rc = krb5_digest_hmac(tfm, &kb->kb_key,
696 khdr, msgcnt, msgs, iovcnt, iovs, cksum);
698 rc = krb5_digest_norm(tfm, kb,
699 khdr, msgcnt, msgs, iovcnt, iovs, cksum);
702 code = GSS_S_COMPLETE;
704 crypto_free_hash(tfm);
708 static void fill_krb5_header(struct krb5_ctx *kctx,
709 struct krb5_header *khdr,
712 unsigned char acceptor_flag;
714 acceptor_flag = kctx->kc_initiate ? 0 : FLAG_SENDER_IS_ACCEPTOR;
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);
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);
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);
734 static __u32 verify_krb5_header(struct krb5_ctx *kctx,
735 struct krb5_header *khdr,
738 unsigned char acceptor_flag;
739 __u16 tok_id, ec_rrc;
741 acceptor_flag = kctx->kc_initiate ? FLAG_SENDER_IS_ACCEPTOR : 0;
744 tok_id = KG_TOK_WRAP_MSG;
747 tok_id = KG_TOK_MIC_MSG;
752 if (be16_to_cpu(khdr->kh_tok_id) != tok_id) {
753 CERROR("bad token id\n");
754 return GSS_S_DEFECTIVE_TOKEN;
756 if ((khdr->kh_flags & FLAG_SENDER_IS_ACCEPTOR) != acceptor_flag) {
757 CERROR("bad direction flag\n");
758 return GSS_S_BAD_SIG;
760 if (privacy && (khdr->kh_flags & FLAG_WRAP_CONFIDENTIAL) == 0) {
761 CERROR("missing confidential flag\n");
762 return GSS_S_BAD_SIG;
764 if (khdr->kh_filler != 0xff) {
765 CERROR("bad filler\n");
766 return GSS_S_DEFECTIVE_TOKEN;
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;
773 return GSS_S_COMPLETE;
777 __u32 gss_get_mic_kerberos(struct gss_ctx *gctx,
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;
789 /* fill krb5 header */
790 LASSERT(token->len >= sizeof(*khdr));
791 khdr = (struct krb5_header *) token->data;
792 fill_krb5_header(kctx, khdr, 0);
795 if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyc,
796 khdr, msgcnt, msgs, iovcnt, iovs, &cksum))
797 return GSS_S_FAILURE;
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,
804 token->len = sizeof(*khdr) + ke->ke_hash_size;
806 return GSS_S_COMPLETE;
810 __u32 gss_verify_mic_kerberos(struct gss_ctx *gctx,
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;
823 if (token->len < sizeof(*khdr)) {
824 CERROR("short signature: %u\n", token->len);
825 return GSS_S_DEFECTIVE_TOKEN;
828 khdr = (struct krb5_header *) token->data;
830 major = verify_krb5_header(kctx, khdr, 0);
831 if (major != GSS_S_COMPLETE) {
832 CERROR("bad krb5 header\n");
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;
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;
848 LASSERT(cksum.len >= ke->ke_hash_size);
849 if (memcmp(khdr + 1, cksum.data + cksum.len - ke->ke_hash_size,
851 CERROR("checksum mismatch\n");
853 return GSS_S_BAD_SIG;
857 return GSS_S_COMPLETE;
861 int add_padding(rawobj_t *msg, int msg_buflen, int blocksize)
865 padding = (blocksize - (msg->len & (blocksize - 1))) &
870 if (msg->len + padding > msg_buflen) {
871 CERROR("bufsize %u too small: datalen %u, padding %u\n",
872 msg_buflen, msg->len, padding);
876 memset(msg->data + msg->len, padding, padding);
882 int krb5_encrypt_rawobjs(struct crypto_blkcipher *tfm,
889 struct blkcipher_desc desc;
890 struct scatterlist src, dst;
891 __u8 local_iv[16] = {0}, *buf;
898 desc.info = local_iv;
901 for (i = 0; i < inobj_cnt; i++) {
902 LASSERT(buf + inobjs[i].len <= outobj->data + outobj->len);
904 buf_to_sg(&src, inobjs[i].data, inobjs[i].len);
905 buf_to_sg(&dst, buf, outobj->len - datalen);
909 rc = crypto_blkcipher_encrypt(
910 &desc, &dst, &src, src.length);
912 rc = crypto_blkcipher_decrypt(
913 &desc, &dst, &src, src.length);
916 rc = crypto_blkcipher_encrypt_iv(
917 &desc, &dst, &src, src.length);
919 rc = crypto_blkcipher_decrypt_iv(
920 &desc, &dst, &src, src.length);
924 CERROR("encrypt error %d\n", rc);
928 datalen += inobjs[i].len;
929 buf += inobjs[i].len;
932 outobj->len = datalen;
937 * if adj_nob != 0, we adjust desc->bd_nob to the actual cipher text size.
940 int krb5_encrypt_bulk(struct crypto_blkcipher *tfm,
941 struct krb5_header *khdr,
943 struct ptlrpc_bulk_desc *desc,
947 struct blkcipher_desc ciph_desc;
948 __u8 local_iv[16] = {0};
949 struct scatterlist src, dst;
950 int blocksize, i, rc, nob = 0;
952 LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
953 LASSERT(desc->bd_iov_count);
954 LASSERT(GET_ENC_KIOV(desc));
956 blocksize = crypto_blkcipher_blocksize(tfm);
957 LASSERT(blocksize > 1);
958 LASSERT(cipher->len == blocksize + sizeof(*khdr));
961 ciph_desc.info = local_iv;
964 /* encrypt confounder */
965 buf_to_sg(&src, confounder, blocksize);
966 buf_to_sg(&dst, cipher->data, blocksize);
968 rc = crypto_blkcipher_encrypt_iv(&ciph_desc, &dst, &src, blocksize);
970 CERROR("error to encrypt confounder: %d\n", rc);
974 /* encrypt clear pages */
975 for (i = 0; i < desc->bd_iov_count; i++) {
976 sg_init_table(&src, 1);
977 sg_set_page(&src, BD_GET_KIOV(desc, i).kiov_page,
978 (BD_GET_KIOV(desc, i).kiov_len +
981 BD_GET_KIOV(desc, i).kiov_offset);
984 sg_init_table(&dst, 1);
985 sg_set_page(&dst, BD_GET_ENC_KIOV(desc, i).kiov_page,
986 src.length, src.offset);
988 BD_GET_ENC_KIOV(desc, i).kiov_offset = dst.offset;
989 BD_GET_ENC_KIOV(desc, i).kiov_len = dst.length;
991 rc = crypto_blkcipher_encrypt_iv(&ciph_desc, &dst, &src,
994 CERROR("error to encrypt page: %d\n", rc);
999 /* encrypt krb5 header */
1000 buf_to_sg(&src, khdr, sizeof(*khdr));
1001 buf_to_sg(&dst, cipher->data + blocksize, sizeof(*khdr));
1003 rc = crypto_blkcipher_encrypt_iv(&ciph_desc, &dst, &src,
1006 CERROR("error to encrypt krb5 header: %d\n", rc);
1017 * desc->bd_nob_transferred is the size of cipher text received.
1018 * desc->bd_nob is the target size of plain text supposed to be.
1020 * if adj_nob != 0, we adjust each page's kiov_len to the actual
1022 * - for client read: we don't know data size for each page, so
1023 * bd_iov[]->kiov_len is set to PAGE_SIZE, but actual data received might
1024 * be smaller, so we need to adjust it according to
1025 * bd_u.bd_kiov.bd_enc_vec[]->kiov_len.
1026 * this means we DO NOT support the situation that server send an odd size
1027 * data in a page which is not the last one.
1028 * - for server write: we knows exactly data size for each page being expected,
1029 * thus kiov_len is accurate already, so we should not adjust it at all.
1030 * and bd_u.bd_kiov.bd_enc_vec[]->kiov_len should be
1031 * round_up(bd_iov[]->kiov_len) which
1032 * should have been done by prep_bulk().
1035 int krb5_decrypt_bulk(struct crypto_blkcipher *tfm,
1036 struct krb5_header *khdr,
1037 struct ptlrpc_bulk_desc *desc,
1042 struct blkcipher_desc ciph_desc;
1043 __u8 local_iv[16] = {0};
1044 struct scatterlist src, dst;
1045 int ct_nob = 0, pt_nob = 0;
1046 int blocksize, i, rc;
1048 LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
1049 LASSERT(desc->bd_iov_count);
1050 LASSERT(GET_ENC_KIOV(desc));
1051 LASSERT(desc->bd_nob_transferred);
1053 blocksize = crypto_blkcipher_blocksize(tfm);
1054 LASSERT(blocksize > 1);
1055 LASSERT(cipher->len == blocksize + sizeof(*khdr));
1057 ciph_desc.tfm = tfm;
1058 ciph_desc.info = local_iv;
1059 ciph_desc.flags = 0;
1061 if (desc->bd_nob_transferred % blocksize) {
1062 CERROR("odd transferred nob: %d\n", desc->bd_nob_transferred);
1066 /* decrypt head (confounder) */
1067 buf_to_sg(&src, cipher->data, blocksize);
1068 buf_to_sg(&dst, plain->data, blocksize);
1070 rc = crypto_blkcipher_decrypt_iv(&ciph_desc, &dst, &src, blocksize);
1072 CERROR("error to decrypt confounder: %d\n", rc);
1076 for (i = 0; i < desc->bd_iov_count && ct_nob < desc->bd_nob_transferred;
1078 if (BD_GET_ENC_KIOV(desc, i).kiov_offset % blocksize
1080 BD_GET_ENC_KIOV(desc, i).kiov_len % blocksize
1082 CERROR("page %d: odd offset %u len %u, blocksize %d\n",
1083 i, BD_GET_ENC_KIOV(desc, i).kiov_offset,
1084 BD_GET_ENC_KIOV(desc, i).kiov_len,
1090 if (ct_nob + BD_GET_ENC_KIOV(desc, i).kiov_len >
1091 desc->bd_nob_transferred)
1092 BD_GET_ENC_KIOV(desc, i).kiov_len =
1093 desc->bd_nob_transferred - ct_nob;
1095 BD_GET_KIOV(desc, i).kiov_len =
1096 BD_GET_ENC_KIOV(desc, i).kiov_len;
1097 if (pt_nob + BD_GET_ENC_KIOV(desc, i).kiov_len >
1099 BD_GET_KIOV(desc, i).kiov_len =
1100 desc->bd_nob - pt_nob;
1102 /* this should be guaranteed by LNET */
1103 LASSERT(ct_nob + BD_GET_ENC_KIOV(desc, i).
1105 desc->bd_nob_transferred);
1106 LASSERT(BD_GET_KIOV(desc, i).kiov_len <=
1107 BD_GET_ENC_KIOV(desc, i).kiov_len);
1110 if (BD_GET_ENC_KIOV(desc, i).kiov_len == 0)
1113 sg_init_table(&src, 1);
1114 sg_set_page(&src, BD_GET_ENC_KIOV(desc, i).kiov_page,
1115 BD_GET_ENC_KIOV(desc, i).kiov_len,
1116 BD_GET_ENC_KIOV(desc, i).kiov_offset);
1118 if (BD_GET_KIOV(desc, i).kiov_len % blocksize == 0)
1119 sg_assign_page(&dst,
1120 BD_GET_KIOV(desc, i).kiov_page);
1122 rc = crypto_blkcipher_decrypt_iv(&ciph_desc, &dst, &src,
1125 CERROR("error to decrypt page: %d\n", rc);
1129 if (BD_GET_KIOV(desc, i).kiov_len % blocksize != 0) {
1130 memcpy(page_address(BD_GET_KIOV(desc, i).kiov_page) +
1131 BD_GET_KIOV(desc, i).kiov_offset,
1132 page_address(BD_GET_ENC_KIOV(desc, i).
1134 BD_GET_KIOV(desc, i).kiov_offset,
1135 BD_GET_KIOV(desc, i).kiov_len);
1138 ct_nob += BD_GET_ENC_KIOV(desc, i).kiov_len;
1139 pt_nob += BD_GET_KIOV(desc, i).kiov_len;
1142 if (unlikely(ct_nob != desc->bd_nob_transferred)) {
1143 CERROR("%d cipher text transferred but only %d decrypted\n",
1144 desc->bd_nob_transferred, ct_nob);
1148 if (unlikely(!adj_nob && pt_nob != desc->bd_nob)) {
1149 CERROR("%d plain text expected but only %d received\n",
1150 desc->bd_nob, pt_nob);
1154 /* if needed, clear up the rest unused iovs */
1156 while (i < desc->bd_iov_count)
1157 BD_GET_KIOV(desc, i++).kiov_len = 0;
1159 /* decrypt tail (krb5 header) */
1160 buf_to_sg(&src, cipher->data + blocksize, sizeof(*khdr));
1161 buf_to_sg(&dst, cipher->data + blocksize, sizeof(*khdr));
1163 rc = crypto_blkcipher_decrypt_iv(&ciph_desc, &dst, &src,
1166 CERROR("error to decrypt tail: %d\n", rc);
1170 if (memcmp(cipher->data + blocksize, khdr, sizeof(*khdr))) {
1171 CERROR("krb5 header doesn't match\n");
1179 __u32 gss_wrap_kerberos(struct gss_ctx *gctx,
1185 struct krb5_ctx *kctx = gctx->internal_ctx_id;
1186 struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
1187 struct krb5_header *khdr;
1189 rawobj_t cksum = RAWOBJ_EMPTY;
1190 rawobj_t data_desc[3], cipher;
1191 __u8 conf[GSS_MAX_CIPHER_BLOCK];
1195 LASSERT(ke->ke_conf_size <= GSS_MAX_CIPHER_BLOCK);
1196 LASSERT(kctx->kc_keye.kb_tfm == NULL ||
1198 crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm));
1201 * final token format:
1202 * ---------------------------------------------------
1203 * | krb5 header | cipher text | checksum (16 bytes) |
1204 * ---------------------------------------------------
1207 /* fill krb5 header */
1208 LASSERT(token->len >= sizeof(*khdr));
1209 khdr = (struct krb5_header *) token->data;
1210 fill_krb5_header(kctx, khdr, 1);
1212 /* generate confounder */
1213 cfs_get_random_bytes(conf, ke->ke_conf_size);
1215 /* get encryption blocksize. note kc_keye might not associated with
1216 * a tfm, currently only for arcfour-hmac */
1217 if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1218 LASSERT(kctx->kc_keye.kb_tfm == NULL);
1221 LASSERT(kctx->kc_keye.kb_tfm);
1222 blocksize = crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
1224 LASSERT(blocksize <= ke->ke_conf_size);
1226 /* padding the message */
1227 if (add_padding(msg, msg_buflen, blocksize))
1228 return GSS_S_FAILURE;
1231 * clear text layout for checksum:
1232 * ------------------------------------------------------
1233 * | confounder | gss header | 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 = gsshdr->data;
1239 data_desc[1].len = gsshdr->len;
1240 data_desc[2].data = msg->data;
1241 data_desc[2].len = msg->len;
1243 /* compute checksum */
1244 if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyi,
1245 khdr, 3, data_desc, 0, NULL, &cksum))
1246 return GSS_S_FAILURE;
1247 LASSERT(cksum.len >= ke->ke_hash_size);
1250 * clear text layout for encryption:
1251 * -----------------------------------------
1252 * | confounder | clear msgs | krb5 header |
1253 * -----------------------------------------
1255 data_desc[0].data = conf;
1256 data_desc[0].len = ke->ke_conf_size;
1257 data_desc[1].data = msg->data;
1258 data_desc[1].len = msg->len;
1259 data_desc[2].data = (__u8 *) khdr;
1260 data_desc[2].len = sizeof(*khdr);
1262 /* cipher text will be directly inplace */
1263 cipher.data = (__u8 *) (khdr + 1);
1264 cipher.len = token->len - sizeof(*khdr);
1265 LASSERT(cipher.len >= ke->ke_conf_size + msg->len + sizeof(*khdr));
1267 if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1269 struct crypto_blkcipher *arc4_tfm;
1271 if (krb5_make_checksum(ENCTYPE_ARCFOUR_HMAC, &kctx->kc_keyi,
1272 NULL, 1, &cksum, 0, NULL, &arc4_keye)) {
1273 CERROR("failed to obtain arc4 enc key\n");
1274 GOTO(arc4_out, rc = -EACCES);
1277 arc4_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0, 0);
1278 if (IS_ERR(arc4_tfm)) {
1279 CERROR("failed to alloc tfm arc4 in ECB mode\n");
1280 GOTO(arc4_out_key, rc = -EACCES);
1283 if (crypto_blkcipher_setkey(arc4_tfm, arc4_keye.data,
1285 CERROR("failed to set arc4 key, len %d\n",
1287 GOTO(arc4_out_tfm, rc = -EACCES);
1290 rc = krb5_encrypt_rawobjs(arc4_tfm, 1,
1291 3, data_desc, &cipher, 1);
1293 crypto_free_blkcipher(arc4_tfm);
1295 rawobj_free(&arc4_keye);
1297 do {} while(0); /* just to avoid compile warning */
1299 rc = krb5_encrypt_rawobjs(kctx->kc_keye.kb_tfm, 0,
1300 3, data_desc, &cipher, 1);
1304 rawobj_free(&cksum);
1305 return GSS_S_FAILURE;
1308 /* fill in checksum */
1309 LASSERT(token->len >= sizeof(*khdr) + cipher.len + ke->ke_hash_size);
1310 memcpy((char *)(khdr + 1) + cipher.len,
1311 cksum.data + cksum.len - ke->ke_hash_size,
1313 rawobj_free(&cksum);
1315 /* final token length */
1316 token->len = sizeof(*khdr) + cipher.len + ke->ke_hash_size;
1317 return GSS_S_COMPLETE;
1321 __u32 gss_prep_bulk_kerberos(struct gss_ctx *gctx,
1322 struct ptlrpc_bulk_desc *desc)
1324 struct krb5_ctx *kctx = gctx->internal_ctx_id;
1327 LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
1328 LASSERT(desc->bd_iov_count);
1329 LASSERT(GET_ENC_KIOV(desc));
1330 LASSERT(kctx->kc_keye.kb_tfm);
1332 blocksize = crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
1334 for (i = 0; i < desc->bd_iov_count; i++) {
1335 LASSERT(BD_GET_ENC_KIOV(desc, i).kiov_page);
1337 * offset should always start at page boundary of either
1338 * client or server side.
1340 if (BD_GET_KIOV(desc, i).kiov_offset & blocksize) {
1341 CERROR("odd offset %d in page %d\n",
1342 BD_GET_KIOV(desc, i).kiov_offset, i);
1343 return GSS_S_FAILURE;
1346 BD_GET_ENC_KIOV(desc, i).kiov_offset =
1347 BD_GET_KIOV(desc, i).kiov_offset;
1348 BD_GET_ENC_KIOV(desc, i).kiov_len =
1349 (BD_GET_KIOV(desc, i).kiov_len +
1350 blocksize - 1) & (~(blocksize - 1));
1353 return GSS_S_COMPLETE;
1357 __u32 gss_wrap_bulk_kerberos(struct gss_ctx *gctx,
1358 struct ptlrpc_bulk_desc *desc,
1359 rawobj_t *token, int adj_nob)
1361 struct krb5_ctx *kctx = gctx->internal_ctx_id;
1362 struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
1363 struct krb5_header *khdr;
1365 rawobj_t cksum = RAWOBJ_EMPTY;
1366 rawobj_t data_desc[1], cipher;
1367 __u8 conf[GSS_MAX_CIPHER_BLOCK];
1370 LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
1372 LASSERT(ke->ke_conf_size <= GSS_MAX_CIPHER_BLOCK);
1375 * final token format:
1376 * --------------------------------------------------
1377 * | krb5 header | head/tail cipher text | checksum |
1378 * --------------------------------------------------
1381 /* fill krb5 header */
1382 LASSERT(token->len >= sizeof(*khdr));
1383 khdr = (struct krb5_header *) token->data;
1384 fill_krb5_header(kctx, khdr, 1);
1386 /* generate confounder */
1387 cfs_get_random_bytes(conf, ke->ke_conf_size);
1389 /* get encryption blocksize. note kc_keye might not associated with
1390 * a tfm, currently only for arcfour-hmac */
1391 if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1392 LASSERT(kctx->kc_keye.kb_tfm == NULL);
1395 LASSERT(kctx->kc_keye.kb_tfm);
1396 blocksize = crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
1400 * we assume the size of krb5_header (16 bytes) must be n * blocksize.
1401 * the bulk token size would be exactly (sizeof(krb5_header) +
1402 * blocksize + sizeof(krb5_header) + hashsize)
1404 LASSERT(blocksize <= ke->ke_conf_size);
1405 LASSERT(sizeof(*khdr) >= blocksize && sizeof(*khdr) % blocksize == 0);
1406 LASSERT(token->len >= sizeof(*khdr) + blocksize + sizeof(*khdr) + 16);
1409 * clear text layout for checksum:
1410 * ------------------------------------------
1411 * | confounder | clear pages | krb5 header |
1412 * ------------------------------------------
1414 data_desc[0].data = conf;
1415 data_desc[0].len = ke->ke_conf_size;
1417 /* compute checksum */
1418 if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyi,
1420 desc->bd_iov_count, GET_KIOV(desc),
1422 return GSS_S_FAILURE;
1423 LASSERT(cksum.len >= ke->ke_hash_size);
1426 * clear text layout for encryption:
1427 * ------------------------------------------
1428 * | confounder | clear pages | krb5 header |
1429 * ------------------------------------------
1431 * ---------- (cipher pages) |
1433 * -------------------------------------------
1434 * | krb5 header | cipher text | cipher text |
1435 * -------------------------------------------
1437 data_desc[0].data = conf;
1438 data_desc[0].len = ke->ke_conf_size;
1440 cipher.data = (__u8 *) (khdr + 1);
1441 cipher.len = blocksize + sizeof(*khdr);
1443 if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1447 rc = krb5_encrypt_bulk(kctx->kc_keye.kb_tfm, khdr,
1448 conf, desc, &cipher, adj_nob);
1452 rawobj_free(&cksum);
1453 return GSS_S_FAILURE;
1456 /* fill in checksum */
1457 LASSERT(token->len >= sizeof(*khdr) + cipher.len + ke->ke_hash_size);
1458 memcpy((char *)(khdr + 1) + cipher.len,
1459 cksum.data + cksum.len - ke->ke_hash_size,
1461 rawobj_free(&cksum);
1463 /* final token length */
1464 token->len = sizeof(*khdr) + cipher.len + ke->ke_hash_size;
1465 return GSS_S_COMPLETE;
1469 __u32 gss_unwrap_kerberos(struct gss_ctx *gctx,
1474 struct krb5_ctx *kctx = gctx->internal_ctx_id;
1475 struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
1476 struct krb5_header *khdr;
1477 unsigned char *tmpbuf;
1478 int blocksize, bodysize;
1479 rawobj_t cksum = RAWOBJ_EMPTY;
1480 rawobj_t cipher_in, plain_out;
1481 rawobj_t hash_objs[3];
1487 if (token->len < sizeof(*khdr)) {
1488 CERROR("short signature: %u\n", token->len);
1489 return GSS_S_DEFECTIVE_TOKEN;
1492 khdr = (struct krb5_header *) token->data;
1494 major = verify_krb5_header(kctx, khdr, 1);
1495 if (major != GSS_S_COMPLETE) {
1496 CERROR("bad krb5 header\n");
1501 if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1502 LASSERT(kctx->kc_keye.kb_tfm == NULL);
1505 LASSERT(kctx->kc_keye.kb_tfm);
1506 blocksize = crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
1509 /* expected token layout:
1510 * ----------------------------------------
1511 * | krb5 header | cipher text | checksum |
1512 * ----------------------------------------
1514 bodysize = token->len - sizeof(*khdr) - ke->ke_hash_size;
1516 if (bodysize % blocksize) {
1517 CERROR("odd bodysize %d\n", bodysize);
1518 return GSS_S_DEFECTIVE_TOKEN;
1521 if (bodysize <= ke->ke_conf_size + sizeof(*khdr)) {
1522 CERROR("incomplete token: bodysize %d\n", bodysize);
1523 return GSS_S_DEFECTIVE_TOKEN;
1526 if (msg->len < bodysize - ke->ke_conf_size - sizeof(*khdr)) {
1527 CERROR("buffer too small: %u, require %d\n",
1528 msg->len, bodysize - ke->ke_conf_size);
1529 return GSS_S_FAILURE;
1533 OBD_ALLOC_LARGE(tmpbuf, bodysize);
1535 return GSS_S_FAILURE;
1537 major = GSS_S_FAILURE;
1539 cipher_in.data = (__u8 *) (khdr + 1);
1540 cipher_in.len = bodysize;
1541 plain_out.data = tmpbuf;
1542 plain_out.len = bodysize;
1544 if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1546 struct crypto_blkcipher *arc4_tfm;
1548 cksum.data = token->data + token->len - ke->ke_hash_size;
1549 cksum.len = ke->ke_hash_size;
1551 if (krb5_make_checksum(ENCTYPE_ARCFOUR_HMAC, &kctx->kc_keyi,
1552 NULL, 1, &cksum, 0, NULL, &arc4_keye)) {
1553 CERROR("failed to obtain arc4 enc key\n");
1554 GOTO(arc4_out, rc = -EACCES);
1557 arc4_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0, 0);
1558 if (IS_ERR(arc4_tfm)) {
1559 CERROR("failed to alloc tfm arc4 in ECB mode\n");
1560 GOTO(arc4_out_key, rc = -EACCES);
1563 if (crypto_blkcipher_setkey(arc4_tfm,
1564 arc4_keye.data, arc4_keye.len)) {
1565 CERROR("failed to set arc4 key, len %d\n",
1567 GOTO(arc4_out_tfm, rc = -EACCES);
1570 rc = krb5_encrypt_rawobjs(arc4_tfm, 1,
1571 1, &cipher_in, &plain_out, 0);
1573 crypto_free_blkcipher(arc4_tfm);
1575 rawobj_free(&arc4_keye);
1577 cksum = RAWOBJ_EMPTY;
1579 rc = krb5_encrypt_rawobjs(kctx->kc_keye.kb_tfm, 0,
1580 1, &cipher_in, &plain_out, 0);
1584 CERROR("error decrypt\n");
1587 LASSERT(plain_out.len == bodysize);
1589 /* expected clear text layout:
1590 * -----------------------------------------
1591 * | confounder | clear msgs | krb5 header |
1592 * -----------------------------------------
1595 /* verify krb5 header in token is not modified */
1596 if (memcmp(khdr, plain_out.data + plain_out.len - sizeof(*khdr),
1598 CERROR("decrypted krb5 header mismatch\n");
1602 /* verify checksum, compose clear text as layout:
1603 * ------------------------------------------------------
1604 * | confounder | gss header | clear msgs | krb5 header |
1605 * ------------------------------------------------------
1607 hash_objs[0].len = ke->ke_conf_size;
1608 hash_objs[0].data = plain_out.data;
1609 hash_objs[1].len = gsshdr->len;
1610 hash_objs[1].data = gsshdr->data;
1611 hash_objs[2].len = plain_out.len - ke->ke_conf_size - sizeof(*khdr);
1612 hash_objs[2].data = plain_out.data + ke->ke_conf_size;
1613 if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyi,
1614 khdr, 3, hash_objs, 0, NULL, &cksum))
1617 LASSERT(cksum.len >= ke->ke_hash_size);
1618 if (memcmp((char *)(khdr + 1) + bodysize,
1619 cksum.data + cksum.len - ke->ke_hash_size,
1620 ke->ke_hash_size)) {
1621 CERROR("checksum mismatch\n");
1625 msg->len = bodysize - ke->ke_conf_size - sizeof(*khdr);
1626 memcpy(msg->data, tmpbuf + ke->ke_conf_size, msg->len);
1628 major = GSS_S_COMPLETE;
1630 OBD_FREE_LARGE(tmpbuf, bodysize);
1631 rawobj_free(&cksum);
1636 __u32 gss_unwrap_bulk_kerberos(struct gss_ctx *gctx,
1637 struct ptlrpc_bulk_desc *desc,
1638 rawobj_t *token, int adj_nob)
1640 struct krb5_ctx *kctx = gctx->internal_ctx_id;
1641 struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
1642 struct krb5_header *khdr;
1644 rawobj_t cksum = RAWOBJ_EMPTY;
1645 rawobj_t cipher, plain;
1646 rawobj_t data_desc[1];
1650 LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
1653 if (token->len < sizeof(*khdr)) {
1654 CERROR("short signature: %u\n", token->len);
1655 return GSS_S_DEFECTIVE_TOKEN;
1658 khdr = (struct krb5_header *) token->data;
1660 major = verify_krb5_header(kctx, khdr, 1);
1661 if (major != GSS_S_COMPLETE) {
1662 CERROR("bad krb5 header\n");
1667 if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1668 LASSERT(kctx->kc_keye.kb_tfm == NULL);
1672 LASSERT(kctx->kc_keye.kb_tfm);
1673 blocksize = crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
1675 LASSERT(sizeof(*khdr) >= blocksize && sizeof(*khdr) % blocksize == 0);
1678 * token format is expected as:
1679 * -----------------------------------------------
1680 * | krb5 header | head/tail cipher text | cksum |
1681 * -----------------------------------------------
1683 if (token->len < sizeof(*khdr) + blocksize + sizeof(*khdr) +
1685 CERROR("short token size: %u\n", token->len);
1686 return GSS_S_DEFECTIVE_TOKEN;
1689 cipher.data = (__u8 *) (khdr + 1);
1690 cipher.len = blocksize + sizeof(*khdr);
1691 plain.data = cipher.data;
1692 plain.len = cipher.len;
1694 rc = krb5_decrypt_bulk(kctx->kc_keye.kb_tfm, khdr,
1695 desc, &cipher, &plain, adj_nob);
1697 return GSS_S_DEFECTIVE_TOKEN;
1700 * verify checksum, compose clear text as layout:
1701 * ------------------------------------------
1702 * | confounder | clear pages | krb5 header |
1703 * ------------------------------------------
1705 data_desc[0].data = plain.data;
1706 data_desc[0].len = blocksize;
1708 if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyi,
1713 return GSS_S_FAILURE;
1714 LASSERT(cksum.len >= ke->ke_hash_size);
1716 if (memcmp(plain.data + blocksize + sizeof(*khdr),
1717 cksum.data + cksum.len - ke->ke_hash_size,
1718 ke->ke_hash_size)) {
1719 CERROR("checksum mismatch\n");
1720 rawobj_free(&cksum);
1721 return GSS_S_BAD_SIG;
1724 rawobj_free(&cksum);
1725 return GSS_S_COMPLETE;
1728 int gss_display_kerberos(struct gss_ctx *ctx,
1732 struct krb5_ctx *kctx = ctx->internal_ctx_id;
1735 written = snprintf(buf, bufsize, "krb5 (%s)",
1736 enctype2str(kctx->kc_enctype));
1740 static struct gss_api_ops gss_kerberos_ops = {
1741 .gss_import_sec_context = gss_import_sec_context_kerberos,
1742 .gss_copy_reverse_context = gss_copy_reverse_context_kerberos,
1743 .gss_inquire_context = gss_inquire_context_kerberos,
1744 .gss_get_mic = gss_get_mic_kerberos,
1745 .gss_verify_mic = gss_verify_mic_kerberos,
1746 .gss_wrap = gss_wrap_kerberos,
1747 .gss_unwrap = gss_unwrap_kerberos,
1748 .gss_prep_bulk = gss_prep_bulk_kerberos,
1749 .gss_wrap_bulk = gss_wrap_bulk_kerberos,
1750 .gss_unwrap_bulk = gss_unwrap_bulk_kerberos,
1751 .gss_delete_sec_context = gss_delete_sec_context_kerberos,
1752 .gss_display = gss_display_kerberos,
1755 static struct subflavor_desc gss_kerberos_sfs[] = {
1757 .sf_subflavor = SPTLRPC_SUBFLVR_KRB5N,
1759 .sf_service = SPTLRPC_SVC_NULL,
1763 .sf_subflavor = SPTLRPC_SUBFLVR_KRB5A,
1765 .sf_service = SPTLRPC_SVC_AUTH,
1769 .sf_subflavor = SPTLRPC_SUBFLVR_KRB5I,
1771 .sf_service = SPTLRPC_SVC_INTG,
1775 .sf_subflavor = SPTLRPC_SUBFLVR_KRB5P,
1777 .sf_service = SPTLRPC_SVC_PRIV,
1783 * currently we leave module owner NULL
1785 static struct gss_api_mech gss_kerberos_mech = {
1786 .gm_owner = NULL, /*THIS_MODULE, */
1788 .gm_oid = (rawobj_t)
1789 {9, "\052\206\110\206\367\022\001\002\002"},
1790 .gm_ops = &gss_kerberos_ops,
1792 .gm_sfs = gss_kerberos_sfs,
1795 int __init init_kerberos_module(void)
1799 spin_lock_init(&krb5_seq_lock);
1801 status = lgss_mech_register(&gss_kerberos_mech);
1803 CERROR("Failed to register kerberos gss mechanism!\n");
1807 void cleanup_kerberos_module(void)
1809 lgss_mech_unregister(&gss_kerberos_mech);