aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/lustre/lustre/ptlrpc/gss/gss_krb5_mech.c
blob: d03f6c114171b849c7161dd3ea96dae6582ad6b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
/*
 * Modifications for Lustre
 *
 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
 *
 * Copyright (c) 2011, 2012, Intel Corporation.
 *
 * Author: Eric Mei <ericm@clusterfs.com>
 */

/*
 *  linux/net/sunrpc/gss_krb5_mech.c
 *  linux/net/sunrpc/gss_krb5_crypto.c
 *  linux/net/sunrpc/gss_krb5_seal.c
 *  linux/net/sunrpc/gss_krb5_seqnum.c
 *  linux/net/sunrpc/gss_krb5_unseal.c
 *
 *  Copyright (c) 2001 The Regents of the University of Michigan.
 *  All rights reserved.
 *
 *  Andy Adamson <andros@umich.edu>
 *  J. Bruce Fields <bfields@umich.edu>
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *  3. Neither the name of the University nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#define DEBUG_SUBSYSTEM S_SEC
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/crypto.h>
#include <linux/mutex.h>

#include <obd.h>
#include <obd_class.h>
#include <obd_support.h>
#include <lustre/lustre_idl.h>
#include <lustre_net.h>
#include <lustre_import.h>
#include <lustre_sec.h>

#include "gss_err.h"
#include "gss_internal.h"
#include "gss_api.h"
#include "gss_asn1.h"
#include "gss_krb5.h"

static spinlock_t krb5_seq_lock;

struct krb5_enctype {
	char	   *ke_dispname;
	char	   *ke_enc_name;	    /* linux tfm name */
	char	   *ke_hash_name;	   /* linux tfm name */
	int	     ke_enc_mode;	    /* linux tfm mode */
	int	     ke_hash_size;	   /* checksum size */
	int	     ke_conf_size;	   /* confounder size */
	unsigned int    ke_hash_hmac:1;	 /* is hmac? */
};

/*
 * NOTE: for aes128-cts and aes256-cts, MIT implementation use CTS encryption.
 * but currently we simply CBC with padding, because linux doesn't support CTS
 * yet. this need to be fixed in the future.
 */
static struct krb5_enctype enctypes[] = {
	[ENCTYPE_DES_CBC_RAW] = {	       /* des-cbc-md5 */
		"des-cbc-md5",
		"cbc(des)",
		"md5",
		0,
		16,
		8,
		0,
	},
	[ENCTYPE_DES3_CBC_RAW] = {	      /* des3-hmac-sha1 */
		"des3-hmac-sha1",
		"cbc(des3_ede)",
		"hmac(sha1)",
		0,
		20,
		8,
		1,
	},
	[ENCTYPE_AES128_CTS_HMAC_SHA1_96] = {   /* aes128-cts */
		"aes128-cts-hmac-sha1-96",
		"cbc(aes)",
		"hmac(sha1)",
		0,
		12,
		16,
		1,
	},
	[ENCTYPE_AES256_CTS_HMAC_SHA1_96] = {   /* aes256-cts */
		"aes256-cts-hmac-sha1-96",
		"cbc(aes)",
		"hmac(sha1)",
		0,
		12,
		16,
		1,
	},
	[ENCTYPE_ARCFOUR_HMAC] = {	      /* arcfour-hmac-md5 */
		"arcfour-hmac-md5",
		"ecb(arc4)",
		"hmac(md5)",
		0,
		16,
		8,
		1,
	},
};

#define MAX_ENCTYPES    sizeof(enctypes)/sizeof(struct krb5_enctype)

static const char * enctype2str(__u32 enctype)
{
	if (enctype < MAX_ENCTYPES && enctypes[enctype].ke_dispname)
		return enctypes[enctype].ke_dispname;

	return "unknown";
}

static
int keyblock_init(struct krb5_keyblock *kb, char *alg_name, int alg_mode)
{
	kb->kb_tfm = crypto_alloc_blkcipher(alg_name, alg_mode, 0);
	if (IS_ERR(kb->kb_tfm)) {
		CERROR("failed to alloc tfm: %s, mode %d\n",
		       alg_name, alg_mode);
		return -1;
	}

	if (crypto_blkcipher_setkey(kb->kb_tfm, kb->kb_key.data, kb->kb_key.len)) {
		CERROR("failed to set %s key, len %d\n",
		       alg_name, kb->kb_key.len);
		return -1;
	}

	return 0;
}

static
int krb5_init_keys(struct krb5_ctx *kctx)
{
	struct krb5_enctype *ke;

	if (kctx->kc_enctype >= MAX_ENCTYPES ||
	    enctypes[kctx->kc_enctype].ke_hash_size == 0) {
		CERROR("unsupported enctype %x\n", kctx->kc_enctype);
		return -1;
	}

	ke = &enctypes[kctx->kc_enctype];

	/* tfm arc4 is stateful, user should alloc-use-free by his own */
	if (kctx->kc_enctype != ENCTYPE_ARCFOUR_HMAC &&
	    keyblock_init(&kctx->kc_keye, ke->ke_enc_name, ke->ke_enc_mode))
		return -1;

	/* tfm hmac is stateful, user should alloc-use-free by his own */
	if (ke->ke_hash_hmac == 0 &&
	    keyblock_init(&kctx->kc_keyi, ke->ke_enc_name, ke->ke_enc_mode))
		return -1;
	if (ke->ke_hash_hmac == 0 &&
	    keyblock_init(&kctx->kc_keyc, ke->ke_enc_name, ke->ke_enc_mode))
		return -1;

	return 0;
}

static
void keyblock_free(struct krb5_keyblock *kb)
{
	rawobj_free(&kb->kb_key);
	if (kb->kb_tfm)
		crypto_free_blkcipher(kb->kb_tfm);
}

static
int keyblock_dup(struct krb5_keyblock *new, struct krb5_keyblock *kb)
{
	return rawobj_dup(&new->kb_key, &kb->kb_key);
}

static
int get_bytes(char **ptr, const char *end, void *res, int len)
{
	char *p, *q;
	p = *ptr;
	q = p + len;
	if (q > end || q < p)
		return -1;
	memcpy(res, p, len);
	*ptr = q;
	return 0;
}

static
int get_rawobj(char **ptr, const char *end, rawobj_t *res)
{
	char   *p, *q;
	__u32   len;

	p = *ptr;
	if (get_bytes(&p, end, &len, sizeof(len)))
		return -1;

	q = p + len;
	if (q > end || q < p)
		return -1;

	OBD_ALLOC_LARGE(res->data, len);
	if (!res->data)
		return -1;

	res->len = len;
	memcpy(res->data, p, len);
	*ptr = q;
	return 0;
}

static
int get_keyblock(char **ptr, const char *end,
		 struct krb5_keyblock *kb, __u32 keysize)
{
	char *buf;

	OBD_ALLOC_LARGE(buf, keysize);
	if (buf == NULL)
		return -1;

	if (get_bytes(ptr, end, buf, keysize)) {
		OBD_FREE_LARGE(buf, keysize);
		return -1;
	}

	kb->kb_key.len = keysize;
	kb->kb_key.data = buf;
	return 0;
}

static
void delete_context_kerberos(struct krb5_ctx *kctx)
{
	rawobj_free(&kctx->kc_mech_used);

	keyblock_free(&kctx->kc_keye);
	keyblock_free(&kctx->kc_keyi);
	keyblock_free(&kctx->kc_keyc);
}

static
__u32 import_context_rfc1964(struct krb5_ctx *kctx, char *p, char *end)
{
	unsigned int    tmp_uint, keysize;

	/* seed_init flag */
	if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
		goto out_err;
	kctx->kc_seed_init = (tmp_uint != 0);

	/* seed */
	if (get_bytes(&p, end, kctx->kc_seed, sizeof(kctx->kc_seed)))
		goto out_err;

	/* sign/seal algorithm, not really used now */
	if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)) ||
	    get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
		goto out_err;

	/* end time */
	if (get_bytes(&p, end, &kctx->kc_endtime, sizeof(kctx->kc_endtime)))
		goto out_err;

	/* seq send */
	if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
		goto out_err;
	kctx->kc_seq_send = tmp_uint;

	/* mech oid */
	if (get_rawobj(&p, end, &kctx->kc_mech_used))
		goto out_err;

	/* old style enc/seq keys in format:
	 *   - enctype (u32)
	 *   - keysize (u32)
	 *   - keydata
	 * we decompose them to fit into the new context
	 */

	/* enc key */
	if (get_bytes(&p, end, &kctx->kc_enctype, sizeof(kctx->kc_enctype)))
		goto out_err;

	if (get_bytes(&p, end, &keysize, sizeof(keysize)))
		goto out_err;

	if (get_keyblock(&p, end, &kctx->kc_keye, keysize))
		goto out_err;

	/* seq key */
	if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)) ||
	    tmp_uint != kctx->kc_enctype)
		goto out_err;

	if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)) ||
	    tmp_uint != keysize)
		goto out_err;

	if (get_keyblock(&p, end, &kctx->kc_keyc, keysize))
		goto out_err;

	/* old style fallback */
	if (keyblock_dup(&kctx->kc_keyi, &kctx->kc_keyc))
		goto out_err;

	if (p != end)
		goto out_err;

	CDEBUG(D_SEC, "successfully imported rfc1964 context\n");
	return 0;
out_err:
	return GSS_S_FAILURE;
}

/* Flags for version 2 context flags */
#define KRB5_CTX_FLAG_INITIATOR		0x00000001
#define KRB5_CTX_FLAG_CFX		0x00000002
#define KRB5_CTX_FLAG_ACCEPTOR_SUBKEY	0x00000004

static
__u32 import_context_rfc4121(struct krb5_ctx *kctx, char *p, char *end)
{
	unsigned int    tmp_uint, keysize;

	/* end time */
	if (get_bytes(&p, end, &kctx->kc_endtime, sizeof(kctx->kc_endtime)))
		goto out_err;

	/* flags */
	if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
		goto out_err;

	if (tmp_uint & KRB5_CTX_FLAG_INITIATOR)
		kctx->kc_initiate = 1;
	if (tmp_uint & KRB5_CTX_FLAG_CFX)
		kctx->kc_cfx = 1;
	if (tmp_uint & KRB5_CTX_FLAG_ACCEPTOR_SUBKEY)
		kctx->kc_have_acceptor_subkey = 1;

	/* seq send */
	if (get_bytes(&p, end, &kctx->kc_seq_send, sizeof(kctx->kc_seq_send)))
		goto out_err;

	/* enctype */
	if (get_bytes(&p, end, &kctx->kc_enctype, sizeof(kctx->kc_enctype)))
		goto out_err;

	/* size of each key */
	if (get_bytes(&p, end, &keysize, sizeof(keysize)))
		goto out_err;

	/* number of keys - should always be 3 */
	if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint)))
		goto out_err;

	if (tmp_uint != 3) {
		CERROR("Invalid number of keys: %u\n", tmp_uint);
		goto out_err;
	}

	/* ke */
	if (get_keyblock(&p, end, &kctx->kc_keye, keysize))
		goto out_err;
	/* ki */
	if (get_keyblock(&p, end, &kctx->kc_keyi, keysize))
		goto out_err;
	/* ki */
	if (get_keyblock(&p, end, &kctx->kc_keyc, keysize))
		goto out_err;

	CDEBUG(D_SEC, "successfully imported v2 context\n");
	return 0;
out_err:
	return GSS_S_FAILURE;
}

/*
 * The whole purpose here is trying to keep user level gss context parsing
 * from nfs-utils unchanged as possible as we can, they are not quite mature
 * yet, and many stuff still not clear, like heimdal etc.
 */
static
__u32 gss_import_sec_context_kerberos(rawobj_t *inbuf,
				      struct gss_ctx *gctx)
{
	struct krb5_ctx *kctx;
	char	    *p = (char *) inbuf->data;
	char	    *end = (char *) (inbuf->data + inbuf->len);
	unsigned int     tmp_uint, rc;

	if (get_bytes(&p, end, &tmp_uint, sizeof(tmp_uint))) {
		CERROR("Fail to read version\n");
		return GSS_S_FAILURE;
	}

	/* only support 0, 1 for the moment */
	if (tmp_uint > 2) {
		CERROR("Invalid version %u\n", tmp_uint);
		return GSS_S_FAILURE;
	}

	OBD_ALLOC_PTR(kctx);
	if (!kctx)
		return GSS_S_FAILURE;

	if (tmp_uint == 0 || tmp_uint == 1) {
		kctx->kc_initiate = tmp_uint;
		rc = import_context_rfc1964(kctx, p, end);
	} else {
		rc = import_context_rfc4121(kctx, p, end);
	}

	if (rc == 0)
		rc = krb5_init_keys(kctx);

	if (rc) {
		delete_context_kerberos(kctx);
		OBD_FREE_PTR(kctx);

		return GSS_S_FAILURE;
	}

	gctx->internal_ctx_id = kctx;
	return GSS_S_COMPLETE;
}

static
__u32 gss_copy_reverse_context_kerberos(struct gss_ctx *gctx,
					struct gss_ctx *gctx_new)
{
	struct krb5_ctx *kctx = gctx->internal_ctx_id;
	struct krb5_ctx *knew;

	OBD_ALLOC_PTR(knew);
	if (!knew)
		return GSS_S_FAILURE;

	knew->kc_initiate = kctx->kc_initiate ? 0 : 1;
	knew->kc_cfx = kctx->kc_cfx;
	knew->kc_seed_init = kctx->kc_seed_init;
	knew->kc_have_acceptor_subkey = kctx->kc_have_acceptor_subkey;
	knew->kc_endtime = kctx->kc_endtime;

	memcpy(knew->kc_seed, kctx->kc_seed, sizeof(kctx->kc_seed));
	knew->kc_seq_send = kctx->kc_seq_recv;
	knew->kc_seq_recv = kctx->kc_seq_send;
	knew->kc_enctype = kctx->kc_enctype;

	if (rawobj_dup(&knew->kc_mech_used, &kctx->kc_mech_used))
		goto out_err;

	if (keyblock_dup(&knew->kc_keye, &kctx->kc_keye))
		goto out_err;
	if (keyblock_dup(&knew->kc_keyi, &kctx->kc_keyi))
		goto out_err;
	if (keyblock_dup(&knew->kc_keyc, &kctx->kc_keyc))
		goto out_err;
	if (krb5_init_keys(knew))
		goto out_err;

	gctx_new->internal_ctx_id = knew;
	CDEBUG(D_SEC, "successfully copied reverse context\n");
	return GSS_S_COMPLETE;

out_err:
	delete_context_kerberos(knew);
	OBD_FREE_PTR(knew);
	return GSS_S_FAILURE;
}

static
__u32 gss_inquire_context_kerberos(struct gss_ctx *gctx,
				   unsigned long  *endtime)
{
	struct krb5_ctx *kctx = gctx->internal_ctx_id;

	*endtime = (unsigned long) ((__u32) kctx->kc_endtime);
	return GSS_S_COMPLETE;
}

static
void gss_delete_sec_context_kerberos(void *internal_ctx)
{
	struct krb5_ctx *kctx = internal_ctx;

	delete_context_kerberos(kctx);
	OBD_FREE_PTR(kctx);
}

static
void buf_to_sg(struct scatterlist *sg, void *ptr, int len)
{
	sg_set_buf(sg, ptr, len);
}

static
__u32 krb5_encrypt(struct crypto_blkcipher *tfm,
		   int decrypt,
		   void * iv,
		   void * in,
		   void * out,
		   int length)
{
	struct blkcipher_desc desc;
	struct scatterlist    sg;
	__u8 local_iv[16] = {0};
	__u32 ret = -EINVAL;

	LASSERT(tfm);
	desc.tfm  = tfm;
	desc.info = local_iv;
	desc.flags= 0;

	if (length % crypto_blkcipher_blocksize(tfm) != 0) {
		CERROR("output length %d mismatch blocksize %d\n",
		       length, crypto_blkcipher_blocksize(tfm));
		goto out;
	}

	if (crypto_blkcipher_ivsize(tfm) > 16) {
		CERROR("iv size too large %d\n", crypto_blkcipher_ivsize(tfm));
		goto out;
	}

	if (iv)
		memcpy(local_iv, iv, crypto_blkcipher_ivsize(tfm));

	memcpy(out, in, length);
	buf_to_sg(&sg, out, length);

	if (decrypt)
		ret = crypto_blkcipher_decrypt_iv(&desc, &sg, &sg, length);
	else
		ret = crypto_blkcipher_encrypt_iv(&desc, &sg, &sg, length);

out:
	return(ret);
}


static inline
int krb5_digest_hmac(struct crypto_hash *tfm,
		     rawobj_t *key,
		     struct krb5_header *khdr,
		     int msgcnt, rawobj_t *msgs,
		     int iovcnt, lnet_kiov_t *iovs,
		     rawobj_t *cksum)
{
	struct hash_desc   desc;
	struct scatterlist sg[1];
	int		i;

	crypto_hash_setkey(tfm, key->data, key->len);
	desc.tfm  = tfm;
	desc.flags= 0;

	crypto_hash_init(&desc);

	for (i = 0; i < msgcnt; i++) {
		if (msgs[i].len == 0)
			continue;
		buf_to_sg(sg, (char *) msgs[i].data, msgs[i].len);
		crypto_hash_update(&desc, sg, msgs[i].len);
	}

	for (i = 0; i < iovcnt; i++) {
		if (iovs[i].kiov_len == 0)
			continue;

		sg_set_page(&sg[0], iovs[i].kiov_page, iovs[i].kiov_len,
			    iovs[i].kiov_offset);
		crypto_hash_update(&desc, sg, iovs[i].kiov_len);
	}

	if (khdr) {
		buf_to_sg(sg, (char *) khdr, sizeof(*khdr));
		crypto_hash_update(&desc, sg, sizeof(*khdr));
	}

	return crypto_hash_final(&desc, cksum->data);
}


static inline
int krb5_digest_norm(struct crypto_hash *tfm,
		     struct krb5_keyblock *kb,
		     struct krb5_header *khdr,
		     int msgcnt, rawobj_t *msgs,
		     int iovcnt, lnet_kiov_t *iovs,
		     rawobj_t *cksum)
{
	struct hash_desc   desc;
	struct scatterlist sg[1];
	int		i;

	LASSERT(kb->kb_tfm);
	desc.tfm  = tfm;
	desc.flags= 0;

	crypto_hash_init(&desc);

	for (i = 0; i < msgcnt; i++) {
		if (msgs[i].len == 0)
			continue;
		buf_to_sg(sg, (char *) msgs[i].data, msgs[i].len);
		crypto_hash_update(&desc, sg, msgs[i].len);
	}

	for (i = 0; i < iovcnt; i++) {
		if (iovs[i].kiov_len == 0)
			continue;

		sg_set_page(&sg[0], iovs[i].kiov_page, iovs[i].kiov_len,
			    iovs[i].kiov_offset);
		crypto_hash_update(&desc, sg, iovs[i].kiov_len);
	}

	if (khdr) {
		buf_to_sg(sg, (char *) khdr, sizeof(*khdr));
		crypto_hash_update(&desc, sg, sizeof(*khdr));
	}

	crypto_hash_final(&desc, cksum->data);

	return krb5_encrypt(kb->kb_tfm, 0, NULL, cksum->data,
			    cksum->data, cksum->len);
}

/*
 * compute (keyed/keyless) checksum against the plain text which appended
 * with krb5 wire token header.
 */
static
__s32 krb5_make_checksum(__u32 enctype,
			 struct krb5_keyblock *kb,
			 struct krb5_header *khdr,
			 int msgcnt, rawobj_t *msgs,
			 int iovcnt, lnet_kiov_t *iovs,
			 rawobj_t *cksum)
{
	struct krb5_enctype   *ke = &enctypes[enctype];
	struct crypto_hash *tfm;
	__u32		  code = GSS_S_FAILURE;
	int		    rc;

	tfm = ll_crypto_alloc_hash(ke->ke_hash_name, 0, 0);
	if (!tfm) {
		CERROR("failed to alloc TFM: %s\n", ke->ke_hash_name);
		return GSS_S_FAILURE;
	}

	cksum->len = crypto_hash_digestsize(tfm);
	OBD_ALLOC_LARGE(cksum->data, cksum->len);
	if (!cksum->data) {
		cksum->len = 0;
		goto out_tfm;
	}

	if (ke->ke_hash_hmac)
		rc = krb5_digest_hmac(tfm, &kb->kb_key,
				      khdr, msgcnt, msgs, iovcnt, iovs, cksum);
	else
		rc = krb5_digest_norm(tfm, kb,
				      khdr, msgcnt, msgs, iovcnt, iovs, cksum);

	if (rc == 0)
		code = GSS_S_COMPLETE;
out_tfm:
	crypto_free_hash(tfm);
	return code;
}

static void fill_krb5_header(struct krb5_ctx *kctx,
			     struct krb5_header *khdr,
			     int privacy)
{
	unsigned char acceptor_flag;

	acceptor_flag = kctx->kc_initiate ? 0 : FLAG_SENDER_IS_ACCEPTOR;

	if (privacy) {
		khdr->kh_tok_id = cpu_to_be16(KG_TOK_WRAP_MSG);
		khdr->kh_flags = acceptor_flag | FLAG_WRAP_CONFIDENTIAL;
		khdr->kh_ec = cpu_to_be16(0);
		khdr->kh_rrc = cpu_to_be16(0);
	} else {
		khdr->kh_tok_id = cpu_to_be16(KG_TOK_MIC_MSG);
		khdr->kh_flags = acceptor_flag;
		khdr->kh_ec = cpu_to_be16(0xffff);
		khdr->kh_rrc = cpu_to_be16(0xffff);
	}

	khdr->kh_filler = 0xff;
	spin_lock(&krb5_seq_lock);
	khdr->kh_seq = cpu_to_be64(kctx->kc_seq_send++);
	spin_unlock(&krb5_seq_lock);
}

static __u32 verify_krb5_header(struct krb5_ctx *kctx,
				struct krb5_header *khdr,
				int privacy)
{
	unsigned char acceptor_flag;
	__u16	 tok_id, ec_rrc;

	acceptor_flag = kctx->kc_initiate ? FLAG_SENDER_IS_ACCEPTOR : 0;

	if (privacy) {
		tok_id = KG_TOK_WRAP_MSG;
		ec_rrc = 0x0;
	} else {
		tok_id = KG_TOK_MIC_MSG;
		ec_rrc = 0xffff;
	}

	/* sanity checks */
	if (be16_to_cpu(khdr->kh_tok_id) != tok_id) {
		CERROR("bad token id\n");
		return GSS_S_DEFECTIVE_TOKEN;
	}
	if ((khdr->kh_flags & FLAG_SENDER_IS_ACCEPTOR) != acceptor_flag) {
		CERROR("bad direction flag\n");
		return GSS_S_BAD_SIG;
	}
	if (privacy && (khdr->kh_flags & FLAG_WRAP_CONFIDENTIAL) == 0) {
		CERROR("missing confidential flag\n");
		return GSS_S_BAD_SIG;
	}
	if (khdr->kh_filler != 0xff) {
		CERROR("bad filler\n");
		return GSS_S_DEFECTIVE_TOKEN;
	}
	if (be16_to_cpu(khdr->kh_ec) != ec_rrc ||
	    be16_to_cpu(khdr->kh_rrc) != ec_rrc) {
		CERROR("bad EC or RRC\n");
		return GSS_S_DEFECTIVE_TOKEN;
	}
	return GSS_S_COMPLETE;
}

static
__u32 gss_get_mic_kerberos(struct gss_ctx *gctx,
			   int msgcnt,
			   rawobj_t *msgs,
			   int iovcnt,
			   lnet_kiov_t *iovs,
			   rawobj_t *token)
{
	struct krb5_ctx     *kctx = gctx->internal_ctx_id;
	struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
	struct krb5_header  *khdr;
	rawobj_t	     cksum = RAWOBJ_EMPTY;

	/* fill krb5 header */
	LASSERT(token->len >= sizeof(*khdr));
	khdr = (struct krb5_header *) token->data;
	fill_krb5_header(kctx, khdr, 0);

	/* checksum */
	if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyc,
			       khdr, msgcnt, msgs, iovcnt, iovs, &cksum))
		return GSS_S_FAILURE;

	LASSERT(cksum.len >= ke->ke_hash_size);
	LASSERT(token->len >= sizeof(*khdr) + ke->ke_hash_size);
	memcpy(khdr + 1, cksum.data + cksum.len - ke->ke_hash_size,
	       ke->ke_hash_size);

	token->len = sizeof(*khdr) + ke->ke_hash_size;
	rawobj_free(&cksum);
	return GSS_S_COMPLETE;
}

static
__u32 gss_verify_mic_kerberos(struct gss_ctx *gctx,
			      int msgcnt,
			      rawobj_t *msgs,
			      int iovcnt,
			      lnet_kiov_t *iovs,
			      rawobj_t *token)
{
	struct krb5_ctx     *kctx = gctx->internal_ctx_id;
	struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
	struct krb5_header  *khdr;
	rawobj_t	     cksum = RAWOBJ_EMPTY;
	__u32		major;

	if (token->len < sizeof(*khdr)) {
		CERROR("short signature: %u\n", token->len);
		return GSS_S_DEFECTIVE_TOKEN;
	}

	khdr = (struct krb5_header *) token->data;

	major = verify_krb5_header(kctx, khdr, 0);
	if (major != GSS_S_COMPLETE) {
		CERROR("bad krb5 header\n");
		return major;
	}

	if (token->len < sizeof(*khdr) + ke->ke_hash_size) {
		CERROR("short signature: %u, require %d\n",
		       token->len, (int) sizeof(*khdr) + ke->ke_hash_size);
		return GSS_S_FAILURE;
	}

	if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyc,
			       khdr, msgcnt, msgs, iovcnt, iovs, &cksum)) {
		CERROR("failed to make checksum\n");
		return GSS_S_FAILURE;
	}

	LASSERT(cksum.len >= ke->ke_hash_size);
	if (memcmp(khdr + 1, cksum.data + cksum.len - ke->ke_hash_size,
		   ke->ke_hash_size)) {
		CERROR("checksum mismatch\n");
		rawobj_free(&cksum);
		return GSS_S_BAD_SIG;
	}

	rawobj_free(&cksum);
	return GSS_S_COMPLETE;
}

static
int add_padding(rawobj_t *msg, int msg_buflen, int blocksize)
{
	int padding;

	padding = (blocksize - (msg->len & (blocksize - 1))) &
		  (blocksize - 1);
	if (!padding)
		return 0;

	if (msg->len + padding > msg_buflen) {
		CERROR("bufsize %u too small: datalen %u, padding %u\n",
			msg_buflen, msg->len, padding);
		return -EINVAL;
	}

	memset(msg->data + msg->len, padding, padding);
	msg->len += padding;
	return 0;
}

static
int krb5_encrypt_rawobjs(struct crypto_blkcipher *tfm,
			 int mode_ecb,
			 int inobj_cnt,
			 rawobj_t *inobjs,
			 rawobj_t *outobj,
			 int enc)
{
	struct blkcipher_desc desc;
	struct scatterlist    src, dst;
	__u8		  local_iv[16] = {0}, *buf;
	__u32		 datalen = 0;
	int		   i, rc;

	buf = outobj->data;
	desc.tfm  = tfm;
	desc.info = local_iv;
	desc.flags = 0;

	for (i = 0; i < inobj_cnt; i++) {
		LASSERT(buf + inobjs[i].len <= outobj->data + outobj->len);

		buf_to_sg(&src, inobjs[i].data, inobjs[i].len);
		buf_to_sg(&dst, buf, outobj->len - datalen);

		if (mode_ecb) {
			if (enc)
				rc = crypto_blkcipher_encrypt(
					&desc, &dst, &src, src.length);
			else
				rc = crypto_blkcipher_decrypt(
					&desc, &dst, &src, src.length);
		} else {
			if (enc)
				rc = crypto_blkcipher_encrypt_iv(
					&desc, &dst, &src, src.length);
			else
				rc = crypto_blkcipher_decrypt_iv(
					&desc, &dst, &src, src.length);
		}

		if (rc) {
			CERROR("encrypt error %d\n", rc);
			return rc;
		}

		datalen += inobjs[i].len;
		buf += inobjs[i].len;
	}

	outobj->len = datalen;
	return 0;
}

/*
 * if adj_nob != 0, we adjust desc->bd_nob to the actual cipher text size.
 */
static
int krb5_encrypt_bulk(struct crypto_blkcipher *tfm,
		      struct krb5_header *khdr,
		      char *confounder,
		      struct ptlrpc_bulk_desc *desc,
		      rawobj_t *cipher,
		      int adj_nob)
{
	struct blkcipher_desc   ciph_desc;
	__u8		    local_iv[16] = {0};
	struct scatterlist      src, dst;
	int		     blocksize, i, rc, nob = 0;

	LASSERT(desc->bd_iov_count);
	LASSERT(desc->bd_enc_iov);

	blocksize = crypto_blkcipher_blocksize(tfm);
	LASSERT(blocksize > 1);
	LASSERT(cipher->len == blocksize + sizeof(*khdr));

	ciph_desc.tfm  = tfm;
	ciph_desc.info = local_iv;
	ciph_desc.flags = 0;

	/* encrypt confounder */
	buf_to_sg(&src, confounder, blocksize);
	buf_to_sg(&dst, cipher->data, blocksize);

	rc = crypto_blkcipher_encrypt_iv(&ciph_desc, &dst, &src, blocksize);
	if (rc) {
		CERROR("error to encrypt confounder: %d\n", rc);
		return rc;
	}

	/* encrypt clear pages */
	for (i = 0; i < desc->bd_iov_count; i++) {
		sg_set_page(&src, desc->bd_iov[i].kiov_page,
			    (desc->bd_iov[i].kiov_len + blocksize - 1) &
			    (~(blocksize - 1)),
			    desc->bd_iov[i].kiov_offset);
		if (adj_nob)
			nob += src.length;
		sg_set_page(&dst, desc->bd_enc_iov[i].kiov_page, src.length,
			    src.offset);

		desc->bd_enc_iov[i].kiov_offset = dst.offset;
		desc->bd_enc_iov[i].kiov_len = dst.length;

		rc = crypto_blkcipher_encrypt_iv(&ciph_desc, &dst, &src,
						    src.length);
		if (rc) {
			CERROR("error to encrypt page: %d\n", rc);
			return rc;
		}
	}

	/* encrypt krb5 header */
	buf_to_sg(&src, khdr, sizeof(*khdr));
	buf_to_sg(&dst, cipher->data + blocksize, sizeof(*khdr));

	rc = crypto_blkcipher_encrypt_iv(&ciph_desc,
					    &dst, &src, sizeof(*khdr));
	if (rc) {
		CERROR("error to encrypt krb5 header: %d\n", rc);
		return rc;
	}

	if (adj_nob)
		desc->bd_nob = nob;

	return 0;
}

/*
 * desc->bd_nob_transferred is the size of cipher text received.
 * desc->bd_nob is the target size of plain text supposed to be.
 *
 * if adj_nob != 0, we adjust each page's kiov_len to the actual
 * plain text size.
 * - for client read: we don't know data size for each page, so
 *   bd_iov[]->kiov_len is set to PAGE_SIZE, but actual data received might
 *   be smaller, so we need to adjust it according to bd_enc_iov[]->kiov_len.
 *   this means we DO NOT support the situation that server send an odd size
 *   data in a page which is not the last one.
 * - for server write: we knows exactly data size for each page being expected,
 *   thus kiov_len is accurate already, so we should not adjust it at all.
 *   and bd_enc_iov[]->kiov_len should be round_up(bd_iov[]->kiov_len) which
 *   should have been done by prep_bulk().
 */
static
int krb5_decrypt_bulk(struct crypto_blkcipher *tfm,
		      struct krb5_header *khdr,
		      struct ptlrpc_bulk_desc *desc,
		      rawobj_t *cipher,
		      rawobj_t *plain,
		      int adj_nob)
{
	struct blkcipher_desc   ciph_desc;
	__u8		    local_iv[16] = {0};
	struct scatterlist      src, dst;
	int		     ct_nob = 0, pt_nob = 0;
	int		     blocksize, i, rc;

	LASSERT(desc->bd_iov_count);
	LASSERT(desc->bd_enc_iov);
	LASSERT(desc->bd_nob_transferred);

	blocksize = crypto_blkcipher_blocksize(tfm);
	LASSERT(blocksize > 1);
	LASSERT(cipher->len == blocksize + sizeof(*khdr));

	ciph_desc.tfm  = tfm;
	ciph_desc.info = local_iv;
	ciph_desc.flags = 0;

	if (desc->bd_nob_transferred % blocksize) {
		CERROR("odd transferred nob: %d\n", desc->bd_nob_transferred);
		return -EPROTO;
	}

	/* decrypt head (confounder) */
	buf_to_sg(&src, cipher->data, blocksize);
	buf_to_sg(&dst, plain->data, blocksize);

	rc = crypto_blkcipher_decrypt_iv(&ciph_desc, &dst, &src, blocksize);
	if (rc) {
		CERROR("error to decrypt confounder: %d\n", rc);
		return rc;
	}

	for (i = 0; i < desc->bd_iov_count && ct_nob < desc->bd_nob_transferred;
	     i++) {
		if (desc->bd_enc_iov[i].kiov_offset % blocksize != 0 ||
		    desc->bd_enc_iov[i].kiov_len % blocksize != 0) {
			CERROR("page %d: odd offset %u len %u, blocksize %d\n",
			       i, desc->bd_enc_iov[i].kiov_offset,
			       desc->bd_enc_iov[i].kiov_len, blocksize);
			return -EFAULT;
		}

		if (adj_nob) {
			if (ct_nob + desc->bd_enc_iov[i].kiov_len >
			    desc->bd_nob_transferred)
				desc->bd_enc_iov[i].kiov_len =
					desc->bd_nob_transferred - ct_nob;

			desc->bd_iov[i].kiov_len = desc->bd_enc_iov[i].kiov_len;
			if (pt_nob + desc->bd_enc_iov[i].kiov_len >desc->bd_nob)
				desc->bd_iov[i].kiov_len = desc->bd_nob -pt_nob;
		} else {
			/* this should be guaranteed by LNET */
			LASSERT(ct_nob + desc->bd_enc_iov[i].kiov_len <=
				desc->bd_nob_transferred);
			LASSERT(desc->bd_iov[i].kiov_len <=
				desc->bd_enc_iov[i].kiov_len);
		}

		if (desc->bd_enc_iov[i].kiov_len == 0)
			continue;

		sg_set_page(&src, desc->bd_enc_iov[i].kiov_page,
			    desc->bd_enc_iov[i].kiov_len,
			    desc->bd_enc_iov[i].kiov_offset);
		dst = src;
		if (desc->bd_iov[i].kiov_len % blocksize == 0)
			sg_assign_page(&dst, desc->bd_iov[i].kiov_page);

		rc = crypto_blkcipher_decrypt_iv(&ciph_desc, &dst, &src,
						    src.length);
		if (rc) {
			CERROR("error to decrypt page: %d\n", rc);
			return rc;
		}

		if (desc->bd_iov[i].kiov_len % blocksize != 0) {
			memcpy(page_address(desc->bd_iov[i].kiov_page) +
			       desc->bd_iov[i].kiov_offset,
			       page_address(desc->bd_enc_iov[i].kiov_page) +
			       desc->bd_iov[i].kiov_offset,
			       desc->bd_iov[i].kiov_len);
		}

		ct_nob += desc->bd_enc_iov[i].kiov_len;
		pt_nob += desc->bd_iov[i].kiov_len;
	}

	if (unlikely(ct_nob != desc->bd_nob_transferred)) {
		CERROR("%d cipher text transferred but only %d decrypted\n",
		       desc->bd_nob_transferred, ct_nob);
		return -EFAULT;
	}

	if (unlikely(!adj_nob && pt_nob != desc->bd_nob)) {
		CERROR("%d plain text expected but only %d received\n",
		       desc->bd_nob, pt_nob);
		return -EFAULT;
	}

	/* if needed, clear up the rest unused iovs */
	if (adj_nob)
		while (i < desc->bd_iov_count)
			desc->bd_iov[i++].kiov_len = 0;

	/* decrypt tail (krb5 header) */
	buf_to_sg(&src, cipher->data + blocksize, sizeof(*khdr));
	buf_to_sg(&dst, cipher->data + blocksize, sizeof(*khdr));

	rc = crypto_blkcipher_decrypt_iv(&ciph_desc,
					    &dst, &src, sizeof(*khdr));
	if (rc) {
		CERROR("error to decrypt tail: %d\n", rc);
		return rc;
	}

	if (memcmp(cipher->data + blocksize, khdr, sizeof(*khdr))) {
		CERROR("krb5 header doesn't match\n");
		return -EACCES;
	}

	return 0;
}

static
__u32 gss_wrap_kerberos(struct gss_ctx *gctx,
			rawobj_t *gsshdr,
			rawobj_t *msg,
			int msg_buflen,
			rawobj_t *token)
{
	struct krb5_ctx     *kctx = gctx->internal_ctx_id;
	struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
	struct krb5_header  *khdr;
	int		  blocksize;
	rawobj_t	     cksum = RAWOBJ_EMPTY;
	rawobj_t	     data_desc[3], cipher;
	__u8		 conf[GSS_MAX_CIPHER_BLOCK];
	int		  rc = 0;

	LASSERT(ke);
	LASSERT(ke->ke_conf_size <= GSS_MAX_CIPHER_BLOCK);
	LASSERT(kctx->kc_keye.kb_tfm == NULL ||
		ke->ke_conf_size >=
		crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm));

	/*
	 * final token format:
	 * ---------------------------------------------------
	 * | krb5 header | cipher text | checksum (16 bytes) |
	 * ---------------------------------------------------
	 */

	/* fill krb5 header */
	LASSERT(token->len >= sizeof(*khdr));
	khdr = (struct krb5_header *) token->data;
	fill_krb5_header(kctx, khdr, 1);

	/* generate confounder */
	cfs_get_random_bytes(conf, ke->ke_conf_size);

	/* get encryption blocksize. note kc_keye might not associated with
	 * a tfm, currently only for arcfour-hmac */
	if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
		LASSERT(kctx->kc_keye.kb_tfm == NULL);
		blocksize = 1;
	} else {
		LASSERT(kctx->kc_keye.kb_tfm);
		blocksize = crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
	}
	LASSERT(blocksize <= ke->ke_conf_size);

	/* padding the message */
	if (add_padding(msg, msg_buflen, blocksize))
		return GSS_S_FAILURE;

	/*
	 * clear text layout for checksum:
	 * ------------------------------------------------------
	 * | confounder | gss header | clear msgs | krb5 header |
	 * ------------------------------------------------------
	 */
	data_desc[0].data = conf;
	data_desc[0].len = ke->ke_conf_size;
	data_desc[1].data = gsshdr->data;
	data_desc[1].len = gsshdr->len;
	data_desc[2].data = msg->data;
	data_desc[2].len = msg->len;

	/* compute checksum */
	if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyi,
			       khdr, 3, data_desc, 0, NULL, &cksum))
		return GSS_S_FAILURE;
	LASSERT(cksum.len >= ke->ke_hash_size);

	/*
	 * clear text layout for encryption:
	 * -----------------------------------------
	 * | confounder | clear msgs | krb5 header |
	 * -----------------------------------------
	 */
	data_desc[0].data = conf;
	data_desc[0].len = ke->ke_conf_size;
	data_desc[1].data = msg->data;
	data_desc[1].len = msg->len;
	data_desc[2].data = (__u8 *) khdr;
	data_desc[2].len = sizeof(*khdr);

	/* cipher text will be directly inplace */
	cipher.data = (__u8 *) (khdr + 1);
	cipher.len = token->len - sizeof(*khdr);
	LASSERT(cipher.len >= ke->ke_conf_size + msg->len + sizeof(*khdr));

	if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
		rawobj_t		 arc4_keye;
		struct crypto_blkcipher *arc4_tfm;

		if (krb5_make_checksum(ENCTYPE_ARCFOUR_HMAC, &kctx->kc_keyi,
				       NULL, 1, &cksum, 0, NULL, &arc4_keye)) {
			CERROR("failed to obtain arc4 enc key\n");
			GOTO(arc4_out, rc = -EACCES);
		}

		arc4_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0, 0);
		if (IS_ERR(arc4_tfm)) {
			CERROR("failed to alloc tfm arc4 in ECB mode\n");
			GOTO(arc4_out_key, rc = -EACCES);
		}

		if (crypto_blkcipher_setkey(arc4_tfm, arc4_keye.data,
					       arc4_keye.len)) {
			CERROR("failed to set arc4 key, len %d\n",
			       arc4_keye.len);
			GOTO(arc4_out_tfm, rc = -EACCES);
		}

		rc = krb5_encrypt_rawobjs(arc4_tfm, 1,
					  3, data_desc, &cipher, 1);
arc4_out_tfm:
		crypto_free_blkcipher(arc4_tfm);
arc4_out_key:
		rawobj_free(&arc4_keye);
arc4_out:
		do {} while (0); /* just to avoid compile warning */
	} else {
		rc = krb5_encrypt_rawobjs(kctx->kc_keye.kb_tfm, 0,
					  3, data_desc, &cipher, 1);
	}

	if (rc != 0) {
		rawobj_free(&cksum);
		return GSS_S_FAILURE;
	}

	/* fill in checksum */
	LASSERT(token->len >= sizeof(*khdr) + cipher.len + ke->ke_hash_size);
	memcpy((char *)(khdr + 1) + cipher.len,
	       cksum.data + cksum.len - ke->ke_hash_size,
	       ke->ke_hash_size);
	rawobj_free(&cksum);

	/* final token length */
	token->len = sizeof(*khdr) + cipher.len + ke->ke_hash_size;
	return GSS_S_COMPLETE;
}

static
__u32 gss_prep_bulk_kerberos(struct gss_ctx *gctx,
			     struct ptlrpc_bulk_desc *desc)
{
	struct krb5_ctx     *kctx = gctx->internal_ctx_id;
	int		  blocksize, i;

	LASSERT(desc->bd_iov_count);
	LASSERT(desc->bd_enc_iov);
	LASSERT(kctx->kc_keye.kb_tfm);

	blocksize = crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);

	for (i = 0; i < desc->bd_iov_count; i++) {
		LASSERT(desc->bd_enc_iov[i].kiov_page);
		/*
		 * offset should always start at page boundary of either
		 * client or server side.
		 */
		if (desc->bd_iov[i].kiov_offset & blocksize) {
			CERROR("odd offset %d in page %d\n",
			       desc->bd_iov[i].kiov_offset, i);
			return GSS_S_FAILURE;
		}

		desc->bd_enc_iov[i].kiov_offset = desc->bd_iov[i].kiov_offset;
		desc->bd_enc_iov[i].kiov_len = (desc->bd_iov[i].kiov_len +
						blocksize - 1) & (~(blocksize - 1));
	}

	return GSS_S_COMPLETE;
}

static
__u32 gss_wrap_bulk_kerberos(struct gss_ctx *gctx,
			     struct ptlrpc_bulk_desc *desc,
			     rawobj_t *token, int adj_nob)
{
	struct krb5_ctx     *kctx = gctx->internal_ctx_id;
	struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
	struct krb5_header  *khdr;
	int		  blocksize;
	rawobj_t	     cksum = RAWOBJ_EMPTY;
	rawobj_t	     data_desc[1], cipher;
	__u8		 conf[GSS_MAX_CIPHER_BLOCK];
	int		  rc = 0;

	LASSERT(ke);
	LASSERT(ke->ke_conf_size <= GSS_MAX_CIPHER_BLOCK);

	/*
	 * final token format:
	 * --------------------------------------------------
	 * | krb5 header | head/tail cipher text | checksum |
	 * --------------------------------------------------
	 */

	/* fill krb5 header */
	LASSERT(token->len >= sizeof(*khdr));
	khdr = (struct krb5_header *) token->data;
	fill_krb5_header(kctx, khdr, 1);

	/* generate confounder */
	cfs_get_random_bytes(conf, ke->ke_conf_size);

	/* get encryption blocksize. note kc_keye might not associated with
	 * a tfm, currently only for arcfour-hmac */
	if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
		LASSERT(kctx->kc_keye.kb_tfm == NULL);
		blocksize = 1;
	} else {
		LASSERT(kctx->kc_keye.kb_tfm);
		blocksize = crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
	}

	/*
	 * we assume the size of krb5_header (16 bytes) must be n * blocksize.
	 * the bulk token size would be exactly (sizeof(krb5_header) +
	 * blocksize + sizeof(krb5_header) + hashsize)
	 */
	LASSERT(blocksize <= ke->ke_conf_size);
	LASSERT(sizeof(*khdr) >= blocksize && sizeof(*khdr) % blocksize == 0);
	LASSERT(token->len >= sizeof(*khdr) + blocksize + sizeof(*khdr) + 16);

	/*
	 * clear text layout for checksum:
	 * ------------------------------------------
	 * | confounder | clear pages | krb5 header |
	 * ------------------------------------------
	 */
	data_desc[0].data = conf;
	data_desc[0].len = ke->ke_conf_size;

	/* compute checksum */
	if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyi,
			       khdr, 1, data_desc,
			       desc->bd_iov_count, desc->bd_iov,
			       &cksum))
		return GSS_S_FAILURE;
	LASSERT(cksum.len >= ke->ke_hash_size);

	/*
	 * clear text layout for encryption:
	 * ------------------------------------------
	 * | confounder | clear pages | krb5 header |
	 * ------------------------------------------
	 *	|	      |	     |
	 *	----------  (cipher pages)   |
	 * result token:   |		   |
	 * -------------------------------------------
	 * | krb5 header | cipher text | cipher text |
	 * -------------------------------------------
	 */
	data_desc[0].data = conf;
	data_desc[0].len = ke->ke_conf_size;

	cipher.data = (__u8 *) (khdr + 1);
	cipher.len = blocksize + sizeof(*khdr);

	if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
		LBUG();
		rc = 0;
	} else {
		rc = krb5_encrypt_bulk(kctx->kc_keye.kb_tfm, khdr,
				       conf, desc, &cipher, adj_nob);
	}

	if (rc != 0) {
		rawobj_free(&cksum);
		return GSS_S_FAILURE;
	}

	/* fill in checksum */
	LASSERT(token->len >= sizeof(*khdr) + cipher.len + ke->ke_hash_size);
	memcpy((char *)(khdr + 1) + cipher.len,
	       cksum.data + cksum.len - ke->ke_hash_size,
	       ke->ke_hash_size);
	rawobj_free(&cksum);

	/* final token length */
	token->len = sizeof(*khdr) + cipher.len + ke->ke_hash_size;
	return GSS_S_COMPLETE;
}

static
__u32 gss_unwrap_kerberos(struct gss_ctx  *gctx,
			  rawobj_t	*gsshdr,
			  rawobj_t	*token,
			  rawobj_t	*msg)
{
	struct krb5_ctx     *kctx = gctx->internal_ctx_id;
	struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
	struct krb5_header  *khdr;
	unsigned char       *tmpbuf;
	int		  blocksize, bodysize;
	rawobj_t	     cksum = RAWOBJ_EMPTY;
	rawobj_t	     cipher_in, plain_out;
	rawobj_t	     hash_objs[3];
	int		  rc = 0;
	__u32		major;

	LASSERT(ke);

	if (token->len < sizeof(*khdr)) {
		CERROR("short signature: %u\n", token->len);
		return GSS_S_DEFECTIVE_TOKEN;
	}

	khdr = (struct krb5_header *) token->data;

	major = verify_krb5_header(kctx, khdr, 1);
	if (major != GSS_S_COMPLETE) {
		CERROR("bad krb5 header\n");
		return major;
	}

	/* block size */
	if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
		LASSERT(kctx->kc_keye.kb_tfm == NULL);
		blocksize = 1;
	} else {
		LASSERT(kctx->kc_keye.kb_tfm);
		blocksize = crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
	}

	/* expected token layout:
	 * ----------------------------------------
	 * | krb5 header | cipher text | checksum |
	 * ----------------------------------------
	 */
	bodysize = token->len - sizeof(*khdr) - ke->ke_hash_size;

	if (bodysize % blocksize) {
		CERROR("odd bodysize %d\n", bodysize);
		return GSS_S_DEFECTIVE_TOKEN;
	}

	if (bodysize <= ke->ke_conf_size + sizeof(*khdr)) {
		CERROR("incomplete token: bodysize %d\n", bodysize);
		return GSS_S_DEFECTIVE_TOKEN;
	}

	if (msg->len < bodysize - ke->ke_conf_size - sizeof(*khdr)) {
		CERROR("buffer too small: %u, require %d\n",
		       msg->len, bodysize - ke->ke_conf_size);
		return GSS_S_FAILURE;
	}

	/* decrypting */
	OBD_ALLOC_LARGE(tmpbuf, bodysize);
	if (!tmpbuf)
		return GSS_S_FAILURE;

	major = GSS_S_FAILURE;

	cipher_in.data = (__u8 *) (khdr + 1);
	cipher_in.len = bodysize;
	plain_out.data = tmpbuf;
	plain_out.len = bodysize;

	if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
		rawobj_t		 arc4_keye;
		struct crypto_blkcipher *arc4_tfm;

		cksum.data = token->data + token->len - ke->ke_hash_size;
		cksum.len = ke->ke_hash_size;

		if (krb5_make_checksum(ENCTYPE_ARCFOUR_HMAC, &kctx->kc_keyi,
				       NULL, 1, &cksum, 0, NULL, &arc4_keye)) {
			CERROR("failed to obtain arc4 enc key\n");
			GOTO(arc4_out, rc = -EACCES);
		}

		arc4_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0, 0);
		if (IS_ERR(arc4_tfm)) {
			CERROR("failed to alloc tfm arc4 in ECB mode\n");
			GOTO(arc4_out_key, rc = -EACCES);
		}

		if (crypto_blkcipher_setkey(arc4_tfm,
					 arc4_keye.data, arc4_keye.len)) {
			CERROR("failed to set arc4 key, len %d\n",
			       arc4_keye.len);
			GOTO(arc4_out_tfm, rc = -EACCES);
		}

		rc = krb5_encrypt_rawobjs(arc4_tfm, 1,
					  1, &cipher_in, &plain_out, 0);
arc4_out_tfm:
		crypto_free_blkcipher(arc4_tfm);
arc4_out_key:
		rawobj_free(&arc4_keye);
arc4_out:
		cksum = RAWOBJ_EMPTY;
	} else {
		rc = krb5_encrypt_rawobjs(kctx->kc_keye.kb_tfm, 0,
					  1, &cipher_in, &plain_out, 0);
	}

	if (rc != 0) {
		CERROR("error decrypt\n");
		goto out_free;
	}
	LASSERT(plain_out.len == bodysize);

	/* expected clear text layout:
	 * -----------------------------------------
	 * | confounder | clear msgs | krb5 header |
	 * -----------------------------------------
	 */

	/* verify krb5 header in token is not modified */
	if (memcmp(khdr, plain_out.data + plain_out.len - sizeof(*khdr),
		   sizeof(*khdr))) {
		CERROR("decrypted krb5 header mismatch\n");
		goto out_free;
	}

	/* verify checksum, compose clear text as layout:
	 * ------------------------------------------------------
	 * | confounder | gss header | clear msgs | krb5 header |
	 * ------------------------------------------------------
	 */
	hash_objs[0].len = ke->ke_conf_size;
	hash_objs[0].data = plain_out.data;
	hash_objs[1].len = gsshdr->len;
	hash_objs[1].data = gsshdr->data;
	hash_objs[2].len = plain_out.len - ke->ke_conf_size - sizeof(*khdr);
	hash_objs[2].data = plain_out.data + ke->ke_conf_size;
	if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyi,
			       khdr, 3, hash_objs, 0, NULL, &cksum))
		goto out_free;

	LASSERT(cksum.len >= ke->ke_hash_size);
	if (memcmp((char *)(khdr + 1) + bodysize,
		   cksum.data + cksum.len - ke->ke_hash_size,
		   ke->ke_hash_size)) {
		CERROR("checksum mismatch\n");
		goto out_free;
	}

	msg->len =  bodysize - ke->ke_conf_size - sizeof(*khdr);
	memcpy(msg->data, tmpbuf + ke->ke_conf_size, msg->len);

	major = GSS_S_COMPLETE;
out_free:
	OBD_FREE_LARGE(tmpbuf, bodysize);
	rawobj_free(&cksum);
	return major;
}

static
__u32 gss_unwrap_bulk_kerberos(struct gss_ctx *gctx,
			       struct ptlrpc_bulk_desc *desc,
			       rawobj_t *token, int adj_nob)
{
	struct krb5_ctx     *kctx = gctx->internal_ctx_id;
	struct krb5_enctype *ke = &enctypes[kctx->kc_enctype];
	struct krb5_header  *khdr;
	int		  blocksize;
	rawobj_t	     cksum = RAWOBJ_EMPTY;
	rawobj_t	     cipher, plain;
	rawobj_t	     data_desc[1];
	int		  rc;
	__u32		major;

	LASSERT(ke);

	if (token->len < sizeof(*khdr)) {
		CERROR("short signature: %u\n", token->len);
		return GSS_S_DEFECTIVE_TOKEN;
	}

	khdr = (struct krb5_header *) token->data;

	major = verify_krb5_header(kctx, khdr, 1);
	if (major != GSS_S_COMPLETE) {
		CERROR("bad krb5 header\n");
		return major;
	}

	/* block size */
	if (kctx->kc_enctype == ENCTYPE_ARCFOUR_HMAC) {
		LASSERT(kctx->kc_keye.kb_tfm == NULL);
		blocksize = 1;
		LBUG();
	} else {
		LASSERT(kctx->kc_keye.kb_tfm);
		blocksize = crypto_blkcipher_blocksize(kctx->kc_keye.kb_tfm);
	}
	LASSERT(sizeof(*khdr) >= blocksize && sizeof(*khdr) % blocksize == 0);

	/*
	 * token format is expected as:
	 * -----------------------------------------------
	 * | krb5 header | head/tail cipher text | cksum |
	 * -----------------------------------------------
	 */
	if (token->len < sizeof(*khdr) + blocksize + sizeof(*khdr) +
			 ke->ke_hash_size) {
		CERROR("short token size: %u\n", token->len);
		return GSS_S_DEFECTIVE_TOKEN;
	}

	cipher.data = (__u8 *) (khdr + 1);
	cipher.len = blocksize + sizeof(*khdr);
	plain.data = cipher.data;
	plain.len = cipher.len;

	rc = krb5_decrypt_bulk(kctx->kc_keye.kb_tfm, khdr,
			       desc, &cipher, &plain, adj_nob);
	if (rc)
		return GSS_S_DEFECTIVE_TOKEN;

	/*
	 * verify checksum, compose clear text as layout:
	 * ------------------------------------------
	 * | confounder | clear pages | krb5 header |
	 * ------------------------------------------
	 */
	data_desc[0].data = plain.data;
	data_desc[0].len = blocksize;

	if (krb5_make_checksum(kctx->kc_enctype, &kctx->kc_keyi,
			       khdr, 1, data_desc,
			       desc->bd_iov_count, desc->bd_iov,
			       &cksum))
		return GSS_S_FAILURE;
	LASSERT(cksum.len >= ke->ke_hash_size);

	if (memcmp(plain.data + blocksize + sizeof(*khdr),
		   cksum.data + cksum.len - ke->ke_hash_size,
		   ke->ke_hash_size)) {
		CERROR("checksum mismatch\n");
		rawobj_free(&cksum);
		return GSS_S_BAD_SIG;
	}

	rawobj_free(&cksum);
	return GSS_S_COMPLETE;
}

int gss_display_kerberos(struct gss_ctx	*ctx,
			 char		  *buf,
			 int		    bufsize)
{
	struct krb5_ctx    *kctx = ctx->internal_ctx_id;
	int		 written;

	written = snprintf(buf, bufsize, "krb5 (%s)",
			   enctype2str(kctx->kc_enctype));
	return written;
}

static struct gss_api_ops gss_kerberos_ops = {
	.gss_import_sec_context     = gss_import_sec_context_kerberos,
	.gss_copy_reverse_context   = gss_copy_reverse_context_kerberos,
	.gss_inquire_context	= gss_inquire_context_kerberos,
	.gss_get_mic		= gss_get_mic_kerberos,
	.gss_verify_mic	     = gss_verify_mic_kerberos,
	.gss_wrap		   = gss_wrap_kerberos,
	.gss_unwrap		 = gss_unwrap_kerberos,
	.gss_prep_bulk	      = gss_prep_bulk_kerberos,
	.gss_wrap_bulk	      = gss_wrap_bulk_kerberos,
	.gss_unwrap_bulk	    = gss_unwrap_bulk_kerberos,
	.gss_delete_sec_context     = gss_delete_sec_context_kerberos,
	.gss_display		= gss_display_kerberos,
};

static struct subflavor_desc gss_kerberos_sfs[] = {
	{
		.sf_subflavor   = SPTLRPC_SUBFLVR_KRB5N,
		.sf_qop	 = 0,
		.sf_service     = SPTLRPC_SVC_NULL,
		.sf_name	= "krb5n"
	},
	{
		.sf_subflavor   = SPTLRPC_SUBFLVR_KRB5A,
		.sf_qop	 = 0,
		.sf_service     = SPTLRPC_SVC_AUTH,
		.sf_name	= "krb5a"
	},
	{
		.sf_subflavor   = SPTLRPC_SUBFLVR_KRB5I,
		.sf_qop	 = 0,
		.sf_service     = SPTLRPC_SVC_INTG,
		.sf_name	= "krb5i"
	},
	{
		.sf_subflavor   = SPTLRPC_SUBFLVR_KRB5P,
		.sf_qop	 = 0,
		.sf_service     = SPTLRPC_SVC_PRIV,
		.sf_name	= "krb5p"
	},
};

/*
 * currently we leave module owner NULL
 */
static struct gss_api_mech gss_kerberos_mech = {
	.gm_owner       = NULL, /*THIS_MODULE, */
	.gm_name	= "krb5",
	.gm_oid	 = (rawobj_t)
				{9, "\052\206\110\206\367\022\001\002\002"},
	.gm_ops	 = &gss_kerberos_ops,
	.gm_sf_num      = 4,
	.gm_sfs	 = gss_kerberos_sfs,
};

int __init init_kerberos_module(void)
{
	int status;

	spin_lock_init(&krb5_seq_lock);

	status = lgss_mech_register(&gss_kerberos_mech);
	if (status)
		CERROR("Failed to register kerberos gss mechanism!\n");
	return status;
}

void __exit cleanup_kerberos_module(void)
{
	lgss_mech_unregister(&gss_kerberos_mech);
}