1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Modifications for Lustre
5 * Copyright 2004 - 2006, Cluster File Systems, Inc.
7 * Author: Eric Mei <ericm@clusterfs.com>
11 * linux/net/sunrpc/gss_krb5_mech.c
12 * linux/net/sunrpc/gss_krb5_crypto.c
13 * linux/net/sunrpc/gss_krb5_seal.c
14 * linux/net/sunrpc/gss_krb5_seqnum.c
15 * linux/net/sunrpc/gss_krb5_unseal.c
17 * Copyright (c) 2001 The Regents of the University of Michigan.
18 * All rights reserved.
20 * Andy Adamson <andros@umich.edu>
21 * J. Bruce Fields <bfields@umich.edu>
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. Neither the name of the University nor the names of its
33 * contributors may be used to endorse or promote products derived
34 * from this software without specific prior written permission.
36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
38 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
44 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
45 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
46 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 # define EXPORT_SYMTAB
53 #define DEBUG_SUBSYSTEM S_SEC
55 #include <linux/init.h>
56 #include <linux/module.h>
57 #include <linux/slab.h>
58 #include <linux/crypto.h>
59 #include <linux/random.h>
60 #include <linux/mutex.h>
62 #include <liblustre.h>
66 #include <obd_class.h>
67 #include <obd_support.h>
68 #include <lustre/lustre_idl.h>
69 #include <lustre_net.h>
70 #include <lustre_import.h>
71 #include <lustre_sec.h>
74 #include "gss_internal.h"
79 spinlock_t krb5_seq_lock = SPIN_LOCK_UNLOCKED;
83 char *ke_enc_name; /* linux tfm name */
84 char *ke_hash_name; /* linux tfm name */
85 int ke_enc_mode; /* linux tfm mode */
86 int ke_hash_size; /* checksum size */
87 int ke_conf_size; /* confounder size */
88 unsigned int ke_hash_hmac:1; /* is hmac? */
92 * NOTE: for aes128-cts and aes256-cts, MIT implementation use CTS encryption.
93 * but currently we simply CBC with padding, because linux doesn't support CTS
94 * yet. this need to be fixed in the future.
96 static struct krb5_enctype enctypes[] = {
97 [ENCTYPE_DES_CBC_RAW] = { /* des-cbc-md5 */
106 [ENCTYPE_DES3_CBC_RAW] = { /* des3-hmac-sha1 */
115 [ENCTYPE_AES128_CTS_HMAC_SHA1_96] = { /* aes128-cts */
116 "aes128-cts-hmac-sha1-96",
124 [ENCTYPE_AES256_CTS_HMAC_SHA1_96] = { /* aes256-cts */
125 "aes256-cts-hmac-sha1-96",
133 [ENCTYPE_ARCFOUR_HMAC] = { /* arcfour-hmac-md5 */
144 #define MAX_ENCTYPES sizeof(enctypes)/sizeof(struct krb5_enctype)
146 static const char * enctype2str(__u32 enctype)
148 if (enctype < MAX_ENCTYPES && enctypes[enctype].ke_dispname)
149 return enctypes[enctype].ke_dispname;
155 int keyblock_init(struct krb5_keyblock *kb, char *alg_name, int alg_mode)
157 kb->kb_tfm = ll_crypto_alloc_blkcipher(alg_name, alg_mode, 0);
158 if (kb->kb_tfm == NULL) {
159 CERROR("failed to alloc tfm: %s, mode %d\n",
164 if (ll_crypto_blkcipher_setkey(kb->kb_tfm, kb->kb_key.data, kb->kb_key.len)) {
165 CERROR("failed to set %s key, len %d\n",
166 alg_name, kb->kb_key.len);
174 int krb5_init_keys(struct krb5_ctx *kctx)
176 struct krb5_enctype *ke;
178 if (kctx->kc_enctype >= MAX_ENCTYPES ||
179 enctypes[kctx->kc_enctype].ke_hash_size == 0) {
180 CERROR("unsupported enctype %x\n", kctx->kc_enctype);
184 ke = &enctypes[kctx->kc_enctype];
186 /* tfm arc4 is stateful, user should alloc-use-free by his own */
187 if (kctx->kc_enctype != ENCTYPE_ARCFOUR_HMAC &&
188 keyblock_init(&kctx->kc_keye, ke->ke_enc_name, ke->ke_enc_mode))
191 /* tfm hmac is stateful, user should alloc-use-free by his own */
192 if (ke->ke_hash_hmac == 0 &&
193 keyblock_init(&kctx->kc_keyi, ke->ke_enc_name, ke->ke_enc_mode))
195 if (ke->ke_hash_hmac == 0 &&
196 keyblock_init(&kctx->kc_keyc, ke->ke_enc_name, ke->ke_enc_mode))
203 void keyblock_free(struct krb5_keyblock *kb)
205 rawobj_free(&kb->kb_key);
207 ll_crypto_free_blkcipher(kb->kb_tfm);
211 int keyblock_dup(struct krb5_keyblock *new, struct krb5_keyblock *kb)
213 return rawobj_dup(&new->kb_key, &kb->kb_key);
217 int get_bytes(char **ptr, const char *end, void *res, int len)
222 if (q > end || q < p)
230 int get_rawobj(char **ptr, const char *end, rawobj_t *res)
236 if (get_bytes(&p, end, &len, sizeof(len)))
240 if (q > end || q < p)
243 OBD_ALLOC(res->data, len);
248 memcpy(res->data, p, len);
254 int get_keyblock(char **ptr, const char *end,
255 struct krb5_keyblock *kb, __u32 keysize)
259 OBD_ALLOC(buf, keysize);
263 if (get_bytes(ptr, end, buf, keysize)) {
264 OBD_FREE(buf, keysize);
268 kb->kb_key.len = keysize;
269 kb->kb_key.data = buf;
274 void delete_context_kerberos(struct krb5_ctx *kctx)
276 rawobj_free(&kctx->kc_mech_used);
278 keyblock_free(&kctx->kc_keye);
279 keyblock_free(&kctx->kc_keyi);
280 keyblock_free(&kctx->kc_keyc);
284 __u32 import_context_rfc1964(struct krb5_ctx *kctx, char *p, char *end)
286 unsigned int tmp_uint, keysize;
289 if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
291 kctx->kc_seed_init = (tmp_uint != 0);
294 if (get_bytes(&p, end, kctx->kc_seed, sizeof(kctx->kc_seed)))
297 /* sign/seal algorithm, not really used now */
298 if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)) ||
299 get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
303 if (get_bytes(&p, end, &kctx->kc_endtime, sizeof(kctx->kc_endtime)))
307 if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
309 kctx->kc_seq_send = tmp_uint;
312 if (get_rawobj(&p, end, &kctx->kc_mech_used))
315 /* old style enc/seq keys in format:
319 * we decompose them to fit into the new context
323 if (get_bytes(&p, end, &kctx->kc_enctype, sizeof(kctx->kc_enctype)))
326 if (get_bytes(&p, end, &keysize, sizeof(keysize)))
329 if (get_keyblock(&p, end, &kctx->kc_keye, keysize))
333 if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)) ||
334 tmp_uint != kctx->kc_enctype)
337 if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)) ||
341 if (get_keyblock(&p, end, &kctx->kc_keyc, keysize))
344 /* old style fallback */
345 if (keyblock_dup(&kctx->kc_keyi, &kctx->kc_keyc))
351 CDEBUG(D_SEC, "succesfully imported rfc1964 context\n");
354 return GSS_S_FAILURE;
357 /* Flags for version 2 context flags */
358 #define KRB5_CTX_FLAG_INITIATOR 0x00000001
359 #define KRB5_CTX_FLAG_CFX 0x00000002
360 #define KRB5_CTX_FLAG_ACCEPTOR_SUBKEY 0x00000004
363 __u32 import_context_rfc4121(struct krb5_ctx *kctx, char *p, char *end)
365 unsigned int tmp_uint, keysize;
368 if (get_bytes(&p, end, &kctx->kc_endtime, sizeof(kctx->kc_endtime)))
372 if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
375 if (tmp_uint & KRB5_CTX_FLAG_INITIATOR)
376 kctx->kc_initiate = 1;
377 if (tmp_uint & KRB5_CTX_FLAG_CFX)
379 if (tmp_uint & KRB5_CTX_FLAG_ACCEPTOR_SUBKEY)
380 kctx->kc_have_acceptor_subkey = 1;
383 if (get_bytes(&p, end, &kctx->kc_seq_send, sizeof(kctx->kc_seq_send)))
387 if (get_bytes(&p, end, &kctx->kc_enctype, sizeof(kctx->kc_enctype)))
390 /* size of each key */
391 if (get_bytes(&p, end, &keysize, sizeof(keysize)))
394 /* number of keys - should always be 3 */
395 if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
399 CERROR("Invalid number of keys: %u\n", tmp_uint);
404 if (get_keyblock(&p, end, &kctx->kc_keye, keysize))
407 if (get_keyblock(&p, end, &kctx->kc_keyi, keysize))
410 if (get_keyblock(&p, end, &kctx->kc_keyc, keysize))
413 CDEBUG(D_SEC, "succesfully imported v2 context\n");
416 return GSS_S_FAILURE;
420 * The whole purpose here is trying to keep user level gss context parsing
421 * from nfs-utils unchanged as possible as we can, they are not quite mature
422 * yet, and many stuff still not clear, like heimdal etc.
425 __u32 gss_import_sec_context_kerberos(rawobj_t *inbuf,
426 struct gss_ctx *gctx)
428 struct krb5_ctx *kctx;
429 char *p = (char *) inbuf->data;
430 char *end = (char *) (inbuf->data + inbuf->len);
431 unsigned int tmp_uint, rc;
433 if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint))) {
434 CERROR("Fail to read version\n");
435 return GSS_S_FAILURE;
438 /* only support 0, 1 for the moment */
440 CERROR("Invalid version %u\n", tmp_uint);
441 return GSS_S_FAILURE;
446 return GSS_S_FAILURE;
448 if (tmp_uint == 0 || tmp_uint == 1) {
449 kctx->kc_initiate = tmp_uint;
450 rc = import_context_rfc1964(kctx, p, end);
452 rc = import_context_rfc4121(kctx, p, end);
456 rc = krb5_init_keys(kctx);
459 delete_context_kerberos(kctx);
462 return GSS_S_FAILURE;
465 gctx->internal_ctx_id = kctx;
466 return GSS_S_COMPLETE;
470 __u32 gss_copy_reverse_context_kerberos(struct gss_ctx *gctx,
471 struct gss_ctx *gctx_new)
473 struct krb5_ctx *kctx = gctx->internal_ctx_id;
474 struct krb5_ctx *knew;
478 return GSS_S_FAILURE;
480 knew->kc_initiate = kctx->kc_initiate ? 0 : 1;
481 knew->kc_cfx = kctx->kc_cfx;
482 knew->kc_seed_init = kctx->kc_seed_init;
483 knew->kc_have_acceptor_subkey = kctx->kc_have_acceptor_subkey;
484 knew->kc_endtime = kctx->kc_endtime;
486 memcpy(knew->kc_seed, kctx->kc_seed, sizeof(kctx->kc_seed));
487 knew->kc_seq_send = kctx->kc_seq_recv;
488 knew->kc_seq_recv = kctx->kc_seq_send;
489 knew->kc_enctype = kctx->kc_enctype;
491 if (rawobj_dup(&knew->kc_mech_used, &kctx->kc_mech_used))
494 if (keyblock_dup(&knew->kc_keye, &kctx->kc_keye))
496 if (keyblock_dup(&knew->kc_keyi, &kctx->kc_keyi))
498 if (keyblock_dup(&knew->kc_keyc, &kctx->kc_keyc))
500 if (krb5_init_keys(knew))
503 gctx_new->internal_ctx_id = knew;
504 CDEBUG(D_SEC, "succesfully copied reverse context\n");
505 return GSS_S_COMPLETE;
508 delete_context_kerberos(knew);
510 return GSS_S_FAILURE;
514 __u32 gss_inquire_context_kerberos(struct gss_ctx *gctx,
515 unsigned long *endtime)
517 struct krb5_ctx *kctx = gctx->internal_ctx_id;
519 *endtime = (unsigned long) ((__u32) kctx->kc_endtime);
520 return GSS_S_COMPLETE;
524 void gss_delete_sec_context_kerberos(void *internal_ctx)
526 struct krb5_ctx *kctx = internal_ctx;
528 delete_context_kerberos(kctx);
533 void buf_to_sg(struct scatterlist *sg, char *ptr, int len)
535 sg->page = virt_to_page(ptr);
536 sg->offset = offset_in_page(ptr);
541 __u32 krb5_encrypt(struct ll_crypto_cipher *tfm,
548 struct blkcipher_desc desc;
549 struct scatterlist sg;
550 __u8 local_iv[16] = {0};
555 desc.info = local_iv;
558 if (length % ll_crypto_blkcipher_blocksize(tfm) != 0) {
559 CERROR("output length %d mismatch blocksize %d\n",
560 length, ll_crypto_blkcipher_blocksize(tfm));
564 if (ll_crypto_blkcipher_ivsize(tfm) > 16) {
565 CERROR("iv size too large %d\n", ll_crypto_blkcipher_ivsize(tfm));
570 memcpy(local_iv, iv, ll_crypto_blkcipher_ivsize(tfm));
572 memcpy(out, in, length);
573 buf_to_sg(&sg, out, length);
576 ret = ll_crypto_blkcipher_decrypt_iv(&desc, &sg, &sg, length);
578 ret = ll_crypto_blkcipher_encrypt_iv(&desc, &sg, &sg, length);
585 int krb5_digest_hmac(struct ll_crypto_hash *tfm,
587 struct krb5_header *khdr,
588 int msgcnt, rawobj_t *msgs,
590 #ifdef HAVE_ASYNC_BLOCK_CIPHER
592 struct hash_desc desc;
593 struct scatterlist sg[1];
596 ll_crypto_hash_setkey(tfm, key->data, key->len);
600 ll_crypto_hash_init(&desc);
602 for (i = 0; i < msgcnt; i++) {
603 if (msgs[i].len == 0)
605 buf_to_sg(sg, (char *) msgs[i].data, msgs[i].len);
606 ll_crypto_hash_update(&desc, sg, msgs[i].len);
610 buf_to_sg(sg, (char *) khdr, sizeof(*khdr));
611 ll_crypto_hash_update(&desc, sg, sizeof(*khdr));
614 return ll_crypto_hash_final(&desc, cksum->data);
616 #else /* HAVE_ASYNC_BLOCK_CIPHER */
618 struct scatterlist sg[1];
619 __u32 keylen = key->len, i;
621 crypto_hmac_init(tfm, key->data, &keylen);
623 for (i = 0; i < msgcnt; i++) {
624 if (msgs[i].len == 0)
626 buf_to_sg(sg, (char *) msgs[i].data, msgs[i].len);
627 crypto_hmac_update(tfm, sg, 1);
631 buf_to_sg(sg, (char *) khdr, sizeof(*khdr));
632 crypto_hmac_update(tfm, sg, 1);
635 crypto_hmac_final(tfm, key->data, &keylen, cksum->data);
638 #endif /* HAVE_ASYNC_BLOCK_CIPHER */
641 int krb5_digest_norm(struct ll_crypto_hash *tfm,
642 struct krb5_keyblock *kb,
643 struct krb5_header *khdr,
644 int msgcnt, rawobj_t *msgs,
647 struct hash_desc desc;
648 struct scatterlist sg[1];
655 ll_crypto_hash_init(&desc);
657 for (i = 0; i < msgcnt; i++) {
658 if (msgs[i].len == 0)
660 buf_to_sg(sg, (char *) msgs[i].data, msgs[i].len);
661 ll_crypto_hash_update(&desc, sg, msgs[i].len);
665 buf_to_sg(sg, (char *) khdr, sizeof(*khdr));
666 ll_crypto_hash_update(&desc, sg, sizeof(*khdr));
669 ll_crypto_hash_final(&desc, cksum->data);
671 return krb5_encrypt(kb->kb_tfm, 0, NULL, cksum->data,
672 cksum->data, cksum->len);
676 * compute (keyed/keyless) checksum against the plain text which appended
677 * with krb5 wire token header.
680 __s32 krb5_make_checksum(__u32 enctype,
681 struct krb5_keyblock *kb,
682 struct krb5_header *khdr,
683 int msgcnt, rawobj_t *msgs,
686 struct krb5_enctype *ke = &enctypes[enctype];
687 struct ll_crypto_hash *tfm;
688 __u32 code = GSS_S_FAILURE;
691 if (!(tfm = ll_crypto_alloc_hash(ke->ke_hash_name, 0, 0))) {
692 CERROR("failed to alloc TFM: %s\n", ke->ke_hash_name);
693 return GSS_S_FAILURE;
696 cksum->len = ll_crypto_hash_digestsize(tfm);
697 OBD_ALLOC(cksum->data, cksum->len);
703 if (ke->ke_hash_hmac)
704 rc = krb5_digest_hmac(tfm, &kb->kb_key,
705 khdr, msgcnt, msgs, cksum);
707 rc = krb5_digest_norm(tfm, kb,
708 khdr, msgcnt, msgs, cksum);
711 code = GSS_S_COMPLETE;
713 ll_crypto_free_hash(tfm);
718 __u32 gss_get_mic_kerberos(struct gss_ctx *gctx,
723 struct krb5_ctx *kctx = gctx->internal_ctx_id;
724 struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
725 struct krb5_header *khdr;
726 unsigned char acceptor_flag;
727 rawobj_t cksum = RAWOBJ_EMPTY;
728 __u32 rc = GSS_S_FAILURE;
730 acceptor_flag = kctx->kc_initiate ? 0 : FLAG_SENDER_IS_ACCEPTOR;
732 /* fill krb5 header */
733 LASSERT(token->len >= sizeof(*khdr));
734 khdr = (struct krb5_header *) token->data;
736 khdr->kh_tok_id = cpu_to_be16(KG_TOK_MIC_MSG);
737 khdr->kh_flags = acceptor_flag;
738 khdr->kh_filler = 0xff;
739 khdr->kh_ec = cpu_to_be16(0xffff);
740 khdr->kh_rrc = cpu_to_be16(0xffff);
741 spin_lock(&krb5_seq_lock);
742 khdr->kh_seq = cpu_to_be64(kctx->kc_seq_send++);
743 spin_unlock(&krb5_seq_lock);
746 if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyc,
747 khdr, msgcnt, msgs, &cksum))
750 LASSERT(cksum.len >= ke->ke_hash_size);
751 LASSERT(token->len >= sizeof(*khdr) + ke->ke_hash_size);
752 memcpy(khdr + 1, cksum.data + cksum.len - ke->ke_hash_size,
755 token->len = sizeof(*khdr) + ke->ke_hash_size;
763 __u32 gss_verify_mic_kerberos(struct gss_ctx *gctx,
768 struct krb5_ctx *kctx = gctx->internal_ctx_id;
769 struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
770 struct krb5_header *khdr;
771 unsigned char acceptor_flag;
772 rawobj_t cksum = RAWOBJ_EMPTY;
773 __u32 rc = GSS_S_FAILURE;
775 acceptor_flag = kctx->kc_initiate ? FLAG_SENDER_IS_ACCEPTOR : 0;
777 if (token->len < sizeof(*khdr)) {
778 CERROR("short signature: %u\n", token->len);
779 return GSS_S_DEFECTIVE_TOKEN;
782 khdr = (struct krb5_header *) token->data;
785 if (be16_to_cpu(khdr->kh_tok_id) != KG_TOK_MIC_MSG) {
786 CERROR("bad token id\n");
787 return GSS_S_DEFECTIVE_TOKEN;
789 if ((khdr->kh_flags & FLAG_SENDER_IS_ACCEPTOR) != acceptor_flag) {
790 CERROR("bad direction flag\n");
791 return GSS_S_BAD_SIG;
793 if (khdr->kh_filler != 0xff) {
794 CERROR("bad filler\n");
795 return GSS_S_DEFECTIVE_TOKEN;
797 if (be16_to_cpu(khdr->kh_ec) != 0xffff ||
798 be16_to_cpu(khdr->kh_rrc) != 0xffff) {
799 CERROR("bad EC or RRC\n");
800 return GSS_S_DEFECTIVE_TOKEN;
803 if (token->len < sizeof(*khdr) + ke->ke_hash_size) {
804 CERROR("short signature: %u, require %d\n",
805 token->len, (int) sizeof(*khdr) + ke->ke_hash_size);
809 if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyc,
810 khdr, msgcnt, msgs, &cksum))
811 return GSS_S_FAILURE;
813 LASSERT(cksum.len >= ke->ke_hash_size);
814 if (memcmp(khdr + 1, cksum.data + cksum.len - ke->ke_hash_size,
816 CERROR("checksum mismatch\n");
828 int add_padding(rawobj_t *msg, int msg_buflen, int blocksize)
832 padding = (blocksize - (msg->len & (blocksize - 1))) &
837 if (msg->len + padding > msg_buflen) {
838 CERROR("bufsize %u too small: datalen %u, padding %u\n",
839 msg_buflen, msg->len, padding);
843 memset(msg->data + msg->len, padding, padding);
849 int krb5_encrypt_rawobjs(struct ll_crypto_cipher *tfm,
856 struct blkcipher_desc desc;
857 struct scatterlist src, dst;
858 __u8 local_iv[16] = {0}, *buf;
865 desc.info = local_iv;
868 for (i = 0; i < inobj_cnt; i++) {
869 LASSERT(buf + inobjs[i].len <= outobj->data + outobj->len);
871 buf_to_sg(&src, inobjs[i].data, inobjs[i].len);
872 buf_to_sg(&dst, buf, outobj->len - datalen);
876 rc = ll_crypto_blkcipher_encrypt(
877 &desc, &dst, &src, src.length);
879 rc = ll_crypto_blkcipher_decrypt(
880 &desc, &dst, &src, src.length);
883 rc = ll_crypto_blkcipher_encrypt_iv(
884 &desc, &dst, &src, src.length);
886 rc = ll_crypto_blkcipher_decrypt_iv(
887 &desc, &dst, &src, src.length);
891 CERROR("encrypt error %d\n", rc);
895 datalen += inobjs[i].len;
896 buf += inobjs[i].len;
899 outobj->len = datalen;
904 __u32 gss_wrap_kerberos(struct gss_ctx *gctx,
910 struct krb5_ctx *kctx = gctx->internal_ctx_id;
911 struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
912 struct krb5_header *khdr;
913 unsigned char acceptor_flag;
915 rawobj_t cksum = RAWOBJ_EMPTY;
916 rawobj_t data_desc[4], cipher;
917 __u8 conf[GSS_MAX_CIPHER_BLOCK];
921 LASSERT(ke->ke_conf_size <= GSS_MAX_CIPHER_BLOCK);
922 LASSERT(kctx->kc_keye.kb_tfm == NULL ||
924 ll_crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm));
927 * final token format:
928 * ---------------------------------------------------
929 * | krb5 header | cipher text | checksum (16 bytes) |
930 * ---------------------------------------------------
933 /* fill krb5 header */
934 LASSERT(token->len >= sizeof(*khdr));
935 khdr = (struct krb5_header *) token->data;
936 acceptor_flag = kctx->kc_initiate ? 0 : FLAG_SENDER_IS_ACCEPTOR;
938 khdr->kh_tok_id = cpu_to_be16(KG_TOK_WRAP_MSG);
939 khdr->kh_flags = acceptor_flag | FLAG_WRAP_CONFIDENTIAL;
940 khdr->kh_filler = 0xff;
941 khdr->kh_ec = cpu_to_be16(0);
942 khdr->kh_rrc = cpu_to_be16(0);
943 spin_lock(&krb5_seq_lock);
944 khdr->kh_seq = cpu_to_be64(kctx->kc_seq_send++);
945 spin_unlock(&krb5_seq_lock);
947 /* generate confounder */
948 get_random_bytes(conf, ke->ke_conf_size);
950 /* get encryption blocksize. note kc_keye might not associated with
951 * a tfm, currently only for arcfour-hmac */
952 if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
953 LASSERT(kctx->kc_keye.kb_tfm == NULL);
956 LASSERT(kctx->kc_keye.kb_tfm);
957 blocksize = ll_crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
959 LASSERT(blocksize <= ke->ke_conf_size);
961 /* padding the message */
962 if (add_padding(msg, msg_buflen, blocksize))
963 return GSS_S_FAILURE;
966 * clear text layout for checksum:
967 * ------------------------------------------------------
968 * | confounder | gss header | clear msgs | krb5 header |
969 * ------------------------------------------------------
971 data_desc[0].data = conf;
972 data_desc[0].len = ke->ke_conf_size;
973 data_desc[1].data = gsshdr->data;
974 data_desc[1].len = gsshdr->len;
975 data_desc[2].data = msg->data;
976 data_desc[2].len = msg->len;
977 data_desc[3].data = (__u8 *) khdr;
978 data_desc[3].len = sizeof(*khdr);
980 /* compute checksum */
981 if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyi,
982 khdr, 4, data_desc, &cksum))
983 return GSS_S_FAILURE;
984 LASSERT(cksum.len >= ke->ke_hash_size);
987 * clear text layout for encryption:
988 * -----------------------------------------
989 * | confounder | clear msgs | krb5 header |
990 * -----------------------------------------
992 data_desc[0].data = conf;
993 data_desc[0].len = ke->ke_conf_size;
994 data_desc[1].data = msg->data;
995 data_desc[1].len = msg->len;
996 data_desc[2].data = (__u8 *) khdr;
997 data_desc[2].len = sizeof(*khdr);
999 /* cipher text will be directly inplace */
1000 cipher.data = (__u8 *) (khdr + 1);
1001 cipher.len = token->len - sizeof(*khdr);
1002 LASSERT(cipher.len >= ke->ke_conf_size + msg->len + sizeof(*khdr));
1004 if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1006 struct ll_crypto_cipher *arc4_tfm;
1008 if (krb5_make_checksum(ENCTYPE_ARCFOUR_HMAC, &kctx->kc_keyi,
1009 NULL, 1, &cksum, &arc4_keye)) {
1010 CERROR("failed to obtain arc4 enc key\n");
1011 GOTO(arc4_out, enc_rc = -EACCES);
1014 arc4_tfm = ll_crypto_alloc_blkcipher("ecb(arc4)", 0, 0);
1015 if (arc4_tfm == NULL) {
1016 CERROR("failed to alloc tfm arc4 in ECB mode\n");
1017 GOTO(arc4_out_key, enc_rc = -EACCES);
1020 if (ll_crypto_blkcipher_setkey(arc4_tfm, arc4_keye.data,
1022 CERROR("failed to set arc4 key, len %d\n",
1024 GOTO(arc4_out_tfm, enc_rc = -EACCES);
1027 enc_rc = krb5_encrypt_rawobjs(arc4_tfm, 1,
1028 3, data_desc, &cipher, 1);
1030 ll_crypto_free_blkcipher(arc4_tfm);
1032 rawobj_free(&arc4_keye);
1034 do {} while(0); /* just to avoid compile warning */
1036 enc_rc = krb5_encrypt_rawobjs(kctx->kc_keye.kb_tfm, 0,
1037 3, data_desc, &cipher, 1);
1041 rawobj_free(&cksum);
1042 return GSS_S_FAILURE;
1045 /* fill in checksum */
1046 LASSERT(token->len >= sizeof(*khdr) + cipher.len + ke->ke_hash_size);
1047 memcpy((char *)(khdr + 1) + cipher.len,
1048 cksum.data + cksum.len - ke->ke_hash_size,
1050 rawobj_free(&cksum);
1052 /* final token length */
1053 token->len = sizeof(*khdr) + cipher.len + ke->ke_hash_size;
1054 return GSS_S_COMPLETE;
1058 __u32 gss_unwrap_kerberos(struct gss_ctx *gctx,
1063 struct krb5_ctx *kctx = gctx->internal_ctx_id;
1064 struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
1065 struct krb5_header *khdr;
1066 unsigned char acceptor_flag;
1067 unsigned char *tmpbuf;
1068 int blocksize, bodysize;
1069 rawobj_t cksum = RAWOBJ_EMPTY;
1070 rawobj_t cipher_in, plain_out;
1071 rawobj_t hash_objs[3];
1072 __u32 rc = GSS_S_FAILURE, enc_rc = 0;
1076 acceptor_flag = kctx->kc_initiate ? FLAG_SENDER_IS_ACCEPTOR : 0;
1078 if (token->len < sizeof(*khdr)) {
1079 CERROR("short signature: %u\n", token->len);
1080 return GSS_S_DEFECTIVE_TOKEN;
1083 khdr = (struct krb5_header *) token->data;
1085 /* sanity check header */
1086 if (be16_to_cpu(khdr->kh_tok_id) != KG_TOK_WRAP_MSG) {
1087 CERROR("bad token id\n");
1088 return GSS_S_DEFECTIVE_TOKEN;
1090 if ((khdr->kh_flags & FLAG_SENDER_IS_ACCEPTOR) != acceptor_flag) {
1091 CERROR("bad direction flag\n");
1092 return GSS_S_BAD_SIG;
1094 if ((khdr->kh_flags & FLAG_WRAP_CONFIDENTIAL) == 0) {
1095 CERROR("missing confidential flag\n");
1096 return GSS_S_BAD_SIG;
1098 if (khdr->kh_filler != 0xff) {
1099 CERROR("bad filler\n");
1100 return GSS_S_DEFECTIVE_TOKEN;
1102 if (be16_to_cpu(khdr->kh_ec) != 0x0 ||
1103 be16_to_cpu(khdr->kh_rrc) != 0x0) {
1104 CERROR("bad EC or RRC\n");
1105 return GSS_S_DEFECTIVE_TOKEN;
1109 if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1110 LASSERT(kctx->kc_keye.kb_tfm == NULL);
1113 LASSERT(kctx->kc_keye.kb_tfm);
1114 blocksize = ll_crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
1117 /* expected token layout:
1118 * ----------------------------------------
1119 * | krb5 header | cipher text | checksum |
1120 * ----------------------------------------
1122 bodysize = token->len - sizeof(*khdr) - ke->ke_hash_size;
1124 if (bodysize % blocksize) {
1125 CERROR("odd bodysize %d\n", bodysize);
1126 return GSS_S_DEFECTIVE_TOKEN;
1129 if (bodysize <= ke->ke_conf_size + sizeof(*khdr)) {
1130 CERROR("incomplete token: bodysize %d\n", bodysize);
1131 return GSS_S_DEFECTIVE_TOKEN;
1134 if (msg->len < bodysize - ke->ke_conf_size - sizeof(*khdr)) {
1135 CERROR("buffer too small: %u, require %d\n",
1136 msg->len, bodysize - ke->ke_conf_size);
1137 return GSS_S_FAILURE;
1141 OBD_ALLOC(tmpbuf, bodysize);
1143 return GSS_S_FAILURE;
1145 cipher_in.data = (__u8 *) (khdr + 1);
1146 cipher_in.len = bodysize;
1147 plain_out.data = tmpbuf;
1148 plain_out.len = bodysize;
1150 if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
1152 struct ll_crypto_cipher *arc4_tfm;
1154 cksum.data = token->data + token->len - ke->ke_hash_size;
1155 cksum.len = ke->ke_hash_size;
1157 if (krb5_make_checksum(ENCTYPE_ARCFOUR_HMAC, &kctx->kc_keyi,
1158 NULL, 1, &cksum, &arc4_keye)) {
1159 CERROR("failed to obtain arc4 enc key\n");
1160 GOTO(arc4_out, enc_rc = -EACCES);
1163 arc4_tfm = ll_crypto_alloc_blkcipher("ecb(arc4)", 0, 0);
1164 if (arc4_tfm == NULL) {
1165 CERROR("failed to alloc tfm arc4 in ECB mode\n");
1166 GOTO(arc4_out_key, enc_rc = -EACCES);
1169 if (ll_crypto_blkcipher_setkey(arc4_tfm,
1170 arc4_keye.data, arc4_keye.len)) {
1171 CERROR("failed to set arc4 key, len %d\n",
1173 GOTO(arc4_out_tfm, enc_rc = -EACCES);
1176 enc_rc = krb5_encrypt_rawobjs(arc4_tfm, 1,
1177 1, &cipher_in, &plain_out, 0);
1179 ll_crypto_free_blkcipher(arc4_tfm);
1181 rawobj_free(&arc4_keye);
1183 cksum = RAWOBJ_EMPTY;
1185 enc_rc = krb5_encrypt_rawobjs(kctx->kc_keye.kb_tfm, 0,
1186 1, &cipher_in, &plain_out, 0);
1190 CERROR("error decrypt\n");
1193 LASSERT(plain_out.len == bodysize);
1195 /* expected clear text layout:
1196 * -----------------------------------------
1197 * | confounder | clear msgs | krb5 header |
1198 * -----------------------------------------
1201 /* verify krb5 header in token is not modified */
1202 if (memcmp(khdr, plain_out.data + plain_out.len - sizeof(*khdr),
1204 CERROR("decrypted krb5 header mismatch\n");
1208 /* verify checksum, compose clear text as layout:
1209 * ------------------------------------------------------
1210 * | confounder | gss header | clear msgs | krb5 header |
1211 * ------------------------------------------------------
1213 hash_objs[0].len = ke->ke_conf_size;
1214 hash_objs[0].data = plain_out.data;
1215 hash_objs[1].len = gsshdr->len;
1216 hash_objs[1].data = gsshdr->data;
1217 hash_objs[2].len = plain_out.len - ke->ke_conf_size;
1218 hash_objs[2].data = plain_out.data + ke->ke_conf_size;
1219 if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyi,
1220 khdr, 3, hash_objs, &cksum))
1223 LASSERT(cksum.len >= ke->ke_hash_size);
1224 if (memcmp((char *)(khdr + 1) + bodysize,
1225 cksum.data + cksum.len - ke->ke_hash_size,
1226 ke->ke_hash_size)) {
1227 CERROR("cksum mismatch\n");
1231 msg->len = bodysize - ke->ke_conf_size - sizeof(*khdr);
1232 memcpy(msg->data, tmpbuf + ke->ke_conf_size, msg->len);
1234 rc = GSS_S_COMPLETE;
1236 OBD_FREE(tmpbuf, bodysize);
1237 rawobj_free(&cksum);
1242 __u32 gss_plain_encrypt_kerberos(struct gss_ctx *ctx,
1248 struct krb5_ctx *kctx = ctx->internal_ctx_id;
1251 rc = krb5_encrypt(kctx->kc_keye.kb_tfm, decrypt,
1252 NULL, in_buf, out_buf, length);
1254 CERROR("plain encrypt error: %d\n", rc);
1259 int gss_display_kerberos(struct gss_ctx *ctx,
1263 struct krb5_ctx *kctx = ctx->internal_ctx_id;
1266 written = snprintf(buf, bufsize, "krb5 (%s)",
1267 enctype2str(kctx->kc_enctype));
1271 static struct gss_api_ops gss_kerberos_ops = {
1272 .gss_import_sec_context = gss_import_sec_context_kerberos,
1273 .gss_copy_reverse_context = gss_copy_reverse_context_kerberos,
1274 .gss_inquire_context = gss_inquire_context_kerberos,
1275 .gss_get_mic = gss_get_mic_kerberos,
1276 .gss_verify_mic = gss_verify_mic_kerberos,
1277 .gss_wrap = gss_wrap_kerberos,
1278 .gss_unwrap = gss_unwrap_kerberos,
1279 .gss_plain_encrypt = gss_plain_encrypt_kerberos,
1280 .gss_delete_sec_context = gss_delete_sec_context_kerberos,
1281 .gss_display = gss_display_kerberos,
1284 static struct subflavor_desc gss_kerberos_sfs[] = {
1286 .sf_subflavor = SPTLRPC_SUBFLVR_KRB5N,
1288 .sf_service = SPTLRPC_SVC_NULL,
1292 .sf_subflavor = SPTLRPC_SUBFLVR_KRB5A,
1294 .sf_service = SPTLRPC_SVC_AUTH,
1298 .sf_subflavor = SPTLRPC_SUBFLVR_KRB5I,
1300 .sf_service = SPTLRPC_SVC_INTG,
1304 .sf_subflavor = SPTLRPC_SUBFLVR_KRB5P,
1306 .sf_service = SPTLRPC_SVC_PRIV,
1312 * currently we leave module owner NULL
1314 static struct gss_api_mech gss_kerberos_mech = {
1315 .gm_owner = NULL, /*THIS_MODULE, */
1317 .gm_oid = (rawobj_t)
1318 {9, "\052\206\110\206\367\022\001\002\002"},
1319 .gm_ops = &gss_kerberos_ops,
1321 .gm_sfs = gss_kerberos_sfs,
1324 int __init init_kerberos_module(void)
1328 status = lgss_mech_register(&gss_kerberos_mech);
1330 CERROR("Failed to register kerberos gss mechanism!\n");
1334 void __exit cleanup_kerberos_module(void)
1336 lgss_mech_unregister(&gss_kerberos_mech);