summaryrefslogtreecommitdiffstats
path: root/libcli/smb/smb2_signing.c
blob: 94ff51f2e931d583a76e2114cdcbf7da12c5e976 (plain)
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
/*
   Unix SMB/CIFS implementation.
   SMB2 signing

   Copyright (C) Stefan Metzmacher 2009

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "system/filesys.h"
#include <gnutls/gnutls.h>
#include <gnutls/crypto.h>
#define SMB2_SIGNING_KEY_GNUTLS_TYPES 1
#include "../libcli/smb/smb_common.h"
#include "../lib/crypto/crypto.h"
#include "lib/util/iov_buf.h"

#include "lib/crypto/gnutls_helpers.h"

void smb2_signing_derivations_fill_const_stack(struct smb2_signing_derivations *ds,
					       enum protocol_types protocol,
					       const DATA_BLOB preauth_hash)
{
	*ds = (struct smb2_signing_derivations) { .signing = NULL, };

	if (protocol >= PROTOCOL_SMB3_11) {
		struct smb2_signing_derivation *d = NULL;

		SMB_ASSERT(preauth_hash.length != 0);

		d = &ds->__signing;
		ds->signing = d;
		d->label = data_blob_string_const_null("SMBSigningKey");
		d->context = preauth_hash;

		d = &ds->__cipher_c2s;
		ds->cipher_c2s = d;
		d->label = data_blob_string_const_null("SMBC2SCipherKey");
		d->context = preauth_hash;

		d = &ds->__cipher_s2c;
		ds->cipher_s2c = d;
		d->label = data_blob_string_const_null("SMBS2CCipherKey");
		d->context = preauth_hash;

		d = &ds->__application;
		ds->application = d;
		d->label = data_blob_string_const_null("SMBAppKey");
		d->context = preauth_hash;

	} else if (protocol >= PROTOCOL_SMB3_00) {
		struct smb2_signing_derivation *d = NULL;

		d = &ds->__signing;
		ds->signing = d;
		d->label = data_blob_string_const_null("SMB2AESCMAC");
		d->context = data_blob_string_const_null("SmbSign");

		d = &ds->__cipher_c2s;
		ds->cipher_c2s = d;
		d->label = data_blob_string_const_null("SMB2AESCCM");
		d->context = data_blob_string_const_null("ServerIn ");

		d = &ds->__cipher_s2c;
		ds->cipher_s2c = d;
		d->label = data_blob_string_const_null("SMB2AESCCM");
		d->context = data_blob_string_const_null("ServerOut");

		d = &ds->__application;
		ds->application = d;
		d->label = data_blob_string_const_null("SMB2APP");
		d->context = data_blob_string_const_null("SmbRpc");
	}
}

static int smb2_signing_key_destructor(struct smb2_signing_key *key)
{
	if (key->hmac_hnd != NULL) {
		gnutls_hmac_deinit(key->hmac_hnd, NULL);
		key->hmac_hnd = NULL;
	}

	if (key->cipher_hnd != NULL) {
		gnutls_aead_cipher_deinit(key->cipher_hnd);
		key->cipher_hnd = NULL;
	}

	return 0;
}

NTSTATUS smb2_signing_key_copy(TALLOC_CTX *mem_ctx,
			       const struct smb2_signing_key *src,
			       struct smb2_signing_key **_dst)
{
	struct smb2_signing_key *dst = NULL;

	dst = talloc_zero(mem_ctx, struct smb2_signing_key);
	if (dst == NULL) {
		return NT_STATUS_NO_MEMORY;
	}
	talloc_set_destructor(dst, smb2_signing_key_destructor);

	dst->sign_algo_id = src->sign_algo_id;
	dst->cipher_algo_id = src->cipher_algo_id;

	if (src->blob.length == 0) {
		*_dst = dst;
		return NT_STATUS_OK;
	}

	dst->blob = data_blob_talloc_zero(dst, src->blob.length);
	if (dst->blob.length == 0) {
		TALLOC_FREE(dst);
		return NT_STATUS_NO_MEMORY;
	}
	talloc_keep_secret(dst->blob.data);
	memcpy(dst->blob.data, src->blob.data, dst->blob.length);

	*_dst = dst;
	return NT_STATUS_OK;
}

static NTSTATUS smb2_signing_key_create(TALLOC_CTX *mem_ctx,
					uint16_t sign_algo_id,
					uint16_t cipher_algo_id,
					const DATA_BLOB *master_key,
					const struct smb2_signing_derivation *d,
					struct smb2_signing_key **_key)
{
	struct smb2_signing_key *key = NULL;
	size_t in_key_length = 16;
	size_t out_key_length = 16;
	NTSTATUS status;

	if (sign_algo_id != SMB2_SIGNING_INVALID_ALGO) {
		SMB_ASSERT(cipher_algo_id == SMB2_ENCRYPTION_INVALID_ALGO);
	}
	if (cipher_algo_id != SMB2_ENCRYPTION_INVALID_ALGO) {
		SMB_ASSERT(sign_algo_id == SMB2_SIGNING_INVALID_ALGO);
	}

	key = talloc_zero(mem_ctx, struct smb2_signing_key);
	if (key == NULL) {
		return NT_STATUS_NO_MEMORY;
	}
	talloc_set_destructor(key, smb2_signing_key_destructor);

	key->sign_algo_id = sign_algo_id;
	key->cipher_algo_id = cipher_algo_id;

	if (master_key == NULL) {
		SMB_ASSERT(d == NULL);

		*_key = key;
		return NT_STATUS_OK;
	}

	/*
	 * Per default use the full key.
	 */
	in_key_length = out_key_length = master_key->length;
	switch (sign_algo_id) {
	case SMB2_SIGNING_INVALID_ALGO:
		/*
		 * This means we're processing cipher_algo_id below
		 */
		break;
	case SMB2_SIGNING_MD5_SMB1:
		SMB_ASSERT(d == NULL);
		break;
	case SMB2_SIGNING_HMAC_SHA256:
	case SMB2_SIGNING_AES128_CMAC:
	case SMB2_SIGNING_AES128_GMAC:
		/*
		 * signing keys are padded or truncated to
		 * 16 bytes.
		 *
		 * Even with master_key->length = 0,
		 * we need to use 16 zeros.
		 */
		in_key_length = out_key_length = 16;
		break;
	default:
		DBG_ERR("sign_algo_id[%u] not supported\n", sign_algo_id);
		return NT_STATUS_HMAC_NOT_SUPPORTED;
	}
	switch (cipher_algo_id) {
	case SMB2_ENCRYPTION_INVALID_ALGO:
		/*
		 * This means we're processing sign_algo_id above
		 */
		break;
	case SMB2_ENCRYPTION_NONE:
		/*
		 * No encryption negotiated.
		 */
		break;
	case SMB2_ENCRYPTION_AES128_CCM:
	case SMB2_ENCRYPTION_AES128_GCM:
		/*
		 * encryption keys are padded or truncated to
		 * 16 bytes.
		 */
		if (master_key->length == 0) {
			DBG_ERR("cipher_algo_id[%u] without key\n",
				cipher_algo_id);
			return NT_STATUS_NO_USER_SESSION_KEY;
		}
		in_key_length = out_key_length = 16;
		break;
	case SMB2_ENCRYPTION_AES256_CCM:
	case SMB2_ENCRYPTION_AES256_GCM:
		/*
		 * AES256 uses the available input and
		 * generated a 32 byte encryption key.
		 */
		if (master_key->length == 0) {
			DBG_ERR("cipher_algo_id[%u] without key\n",
				cipher_algo_id);
			return NT_STATUS_NO_USER_SESSION_KEY;
		}
		out_key_length = 32;
		break;
	default:
		DBG_ERR("cipher_algo_id[%u] not supported\n", cipher_algo_id);
		return NT_STATUS_FWP_INCOMPATIBLE_CIPHER_CONFIG;
	}

	if (out_key_length == 0) {
		*_key = key;
		return NT_STATUS_OK;
	}

	key->blob = data_blob_talloc_zero(key, out_key_length);
	if (key->blob.length == 0) {
		TALLOC_FREE(key);
		return NT_STATUS_NO_MEMORY;
	}
	talloc_keep_secret(key->blob.data);
	memcpy(key->blob.data,
	       master_key->data,
	       MIN(key->blob.length, master_key->length));

	if (d == NULL) {
		*_key = key;
		return NT_STATUS_OK;
	}

	status = samba_gnutls_sp800_108_derive_key(key->blob.data,
						   in_key_length,
						   NULL,
						   0,
						   d->label.data,
						   d->label.length,
						   d->context.data,
						   d->context.length,
						   GNUTLS_MAC_SHA256,
						   key->blob.data,
						   out_key_length);
	if (!NT_STATUS_IS_OK(status)) {
		TALLOC_FREE(key);
		return status;
	}

	*_key = key;
	return NT_STATUS_OK;
}

NTSTATUS smb2_signing_key_sign_create(TALLOC_CTX *mem_ctx,
				      uint16_t sign_algo_id,
				      const DATA_BLOB *master_key,
				      const struct smb2_signing_derivation *d,
				      struct smb2_signing_key **_key)
{
	return smb2_signing_key_create(mem_ctx,
				       sign_algo_id,
				       SMB2_ENCRYPTION_INVALID_ALGO,
				       master_key,
				       d,
				       _key);
}

NTSTATUS smb2_signing_key_cipher_create(TALLOC_CTX *mem_ctx,
					uint16_t cipher_algo_id,
					const DATA_BLOB *master_key,
					const struct smb2_signing_derivation *d,
					struct smb2_signing_key **_key)
{
	return smb2_signing_key_create(mem_ctx,
				       SMB2_SIGNING_INVALID_ALGO,
				       cipher_algo_id,
				       master_key,
				       d,
				       _key);
}

bool smb2_signing_key_valid(const struct smb2_signing_key *key)
{
	if (key == NULL) {
		return false;
	}

	if (key->blob.length == 0 || key->blob.data == NULL) {
		return false;
	}

	return true;
}

static NTSTATUS smb2_signing_gmac(gnutls_aead_cipher_hd_t cipher_hnd,
				  const uint8_t *iv, size_t iv_size,
				  const giovec_t *auth_iov, uint8_t auth_iovcnt,
				  uint8_t *tag, size_t _tag_size)
{
	size_t tag_size = _tag_size;
	int rc;

	rc = gnutls_aead_cipher_encryptv2(cipher_hnd,
					  iv, iv_size,
					  auth_iov, auth_iovcnt,
					  NULL, 0,
					  tag, &tag_size);
	if (rc < 0) {
		return gnutls_error_to_ntstatus(rc, NT_STATUS_HMAC_NOT_SUPPORTED);
	}

	return NT_STATUS_OK;
}

static NTSTATUS smb2_signing_calc_signature(struct smb2_signing_key *signing_key,
					    uint16_t sign_algo_id,
					    const struct iovec *vector,
					    int count,
					    uint8_t signature[16])
{
	const uint8_t *hdr = (uint8_t *)vector[0].iov_base;
	uint16_t opcode;
	uint32_t flags;
	uint64_t msg_id;
	static const uint8_t zero_sig[16] = { 0, };
	gnutls_mac_algorithm_t hmac_algo = GNUTLS_MAC_UNKNOWN;
	int i;

	/*
	 * We expect
	 * - SMB2 HDR
	 * - SMB2 BODY FIXED
	 * - (optional) SMB2 BODY DYN
	 * - (optional) PADDING
	 */
	SMB_ASSERT(count >= 2);
	SMB_ASSERT(vector[0].iov_len == SMB2_HDR_BODY);
	SMB_ASSERT(count <= 4);

	opcode = SVAL(hdr, SMB2_HDR_OPCODE);
	flags = IVAL(hdr, SMB2_HDR_FLAGS);
	if (flags & SMB2_HDR_FLAG_REDIRECT) {
		NTSTATUS pdu_status = NT_STATUS(IVAL(hdr, SMB2_HDR_STATUS));
		if (NT_STATUS_EQUAL(pdu_status, NT_STATUS_PENDING)) {
			DBG_ERR("opcode[%u] NT_STATUS_PENDING\n", opcode);
			return NT_STATUS_INTERNAL_ERROR;
		}
		if (opcode == SMB2_OP_CANCEL) {
			DBG_ERR("SMB2_OP_CANCEL response should not be signed\n");
			return NT_STATUS_INTERNAL_ERROR;
		}
	}
	msg_id = BVAL(hdr, SMB2_HDR_MESSAGE_ID);
	if (msg_id == 0) {
		if (opcode != SMB2_OP_CANCEL ||
		    sign_algo_id >= SMB2_SIGNING_AES128_GMAC)
		{
			DBG_ERR("opcode[%u] msg_id == 0\n", opcode);
			return NT_STATUS_INTERNAL_ERROR;
		}
		/*
		 * Legacy algorithms allow MID 0
		 * for cancel requests
		 */
	}
	if (msg_id == UINT64_MAX) {
		DBG_ERR("opcode[%u] msg_id == UINT64_MAX\n", opcode);
		return NT_STATUS_INTERNAL_ERROR;
	}

	switch (sign_algo_id) {
	case SMB2_SIGNING_AES128_GMAC: {
		gnutls_cipher_algorithm_t algo = GNUTLS_CIPHER_AES_128_GCM;
		uint32_t key_size = gnutls_cipher_get_key_size(algo);
		uint32_t iv_size = gnutls_cipher_get_iv_size(algo);
		size_t tag_size = gnutls_cipher_get_tag_size(algo);
		gnutls_datum_t key = {
			.data = signing_key->blob.data,
			.size = MIN(signing_key->blob.length, key_size),
		};
		uint64_t high_bits = 0;
		uint8_t iv[AES_BLOCK_SIZE] = {0};
		giovec_t auth_iov[count+1];
		size_t auth_iovcnt = 0;
		NTSTATUS status;
		int rc;

		high_bits = flags & SMB2_HDR_FLAG_REDIRECT;
		if (opcode == SMB2_OP_CANCEL) {
			high_bits |= SMB2_HDR_FLAG_ASYNC;
		}
		SBVAL(iv, 0, msg_id);
		SBVAL(iv, 8, high_bits);

		if (signing_key->cipher_hnd == NULL) {
			rc = gnutls_aead_cipher_init(&signing_key->cipher_hnd,
						     algo,
					             &key);
			if (rc < 0) {
				return gnutls_error_to_ntstatus(rc,
						NT_STATUS_HMAC_NOT_SUPPORTED);
			}
		}

		SMB_ASSERT(key_size == 16);
		SMB_ASSERT(iv_size == 12);
		SMB_ASSERT(tag_size == 16);

		auth_iov[auth_iovcnt++] = (giovec_t) {
			.iov_base = discard_const_p(uint8_t, hdr),
			.iov_len  = SMB2_HDR_SIGNATURE,
		};
		auth_iov[auth_iovcnt++] = (giovec_t) {
			.iov_base = discard_const_p(uint8_t, zero_sig),
			.iov_len  = 16,
		};
		for (i=1; i < count; i++) {
			auth_iov[auth_iovcnt++] = (giovec_t) {
				.iov_base = discard_const_p(uint8_t, vector[i].iov_base),
				.iov_len  = vector[i].iov_len,
			};
		}

		status = smb2_signing_gmac(signing_key->cipher_hnd,
					   iv,
					   iv_size,
					   auth_iov,
					   auth_iovcnt,
					   signature,
					   tag_size);
		if (!NT_STATUS_IS_OK(status)) {
			return status;
		}

		return NT_STATUS_OK;
	}	break;

	case SMB2_SIGNING_AES128_CMAC:
		hmac_algo = GNUTLS_MAC_AES_CMAC_128;
		break;
	case SMB2_SIGNING_HMAC_SHA256:
		hmac_algo = GNUTLS_MAC_SHA256;
		break;

	default:
		return NT_STATUS_HMAC_NOT_SUPPORTED;
	}

	if (hmac_algo != GNUTLS_MAC_UNKNOWN) {
		uint8_t digest[gnutls_hmac_get_len(hmac_algo)];
		gnutls_datum_t key = {
			.data = signing_key->blob.data,
			.size = MIN(signing_key->blob.length, 16),
		};
		int rc;

		if (signing_key->hmac_hnd == NULL) {
			rc = gnutls_hmac_init(&signing_key->hmac_hnd,
					      hmac_algo,
					      key.data,
					      key.size);
			if (rc < 0) {
				return gnutls_error_to_ntstatus(rc,
						NT_STATUS_HMAC_NOT_SUPPORTED);
			}
		}

		rc = gnutls_hmac(signing_key->hmac_hnd, hdr, SMB2_HDR_SIGNATURE);
		if (rc < 0) {
			return gnutls_error_to_ntstatus(rc,
						NT_STATUS_HMAC_NOT_SUPPORTED);
		}
		rc = gnutls_hmac(signing_key->hmac_hnd, zero_sig, 16);
		if (rc < 0) {
			return gnutls_error_to_ntstatus(rc,
						NT_STATUS_HMAC_NOT_SUPPORTED);
		}

		for (i = 1; i < count; i++) {
			rc = gnutls_hmac(signing_key->hmac_hnd,
					 vector[i].iov_base,
					 vector[i].iov_len);
			if (rc < 0) {
				return gnutls_error_to_ntstatus(rc,
						NT_STATUS_HMAC_NOT_SUPPORTED);
			}
		}
		gnutls_hmac_output(signing_key->hmac_hnd, digest);
		memcpy(signature, digest, 16);
		ZERO_ARRAY(digest);
		return NT_STATUS_OK;
	}

	return NT_STATUS_HMAC_NOT_SUPPORTED;
}

NTSTATUS smb2_signing_sign_pdu(struct smb2_signing_key *signing_key,
			       struct iovec *vector,
			       int count)
{
	uint16_t sign_algo_id;
	uint8_t *hdr;
	uint64_t session_id;
	uint8_t res[16];
	NTSTATUS status;

	/*
	 * We expect
	 * - SMB2 HDR
	 * - SMB2 BODY FIXED
	 * - (optional) SMB2 BODY DYN
	 * - (optional) PADDING
	 */
	SMB_ASSERT(count >= 2);
	SMB_ASSERT(vector[0].iov_len == SMB2_HDR_BODY);
	SMB_ASSERT(count <= 4);

	hdr = (uint8_t *)vector[0].iov_base;

	session_id = BVAL(hdr, SMB2_HDR_SESSION_ID);
	if (session_id == 0) {
		/*
		 * do not sign messages with a zero session_id.
		 * See MS-SMB2 3.2.4.1.1
		 */
		return NT_STATUS_OK;
	}

	if (!smb2_signing_key_valid(signing_key)) {
		DBG_WARNING("No signing key for SMB2 signing\n");
		return NT_STATUS_ACCESS_DENIED;
	}

	memset(hdr + SMB2_HDR_SIGNATURE, 0, 16);

	SIVAL(hdr, SMB2_HDR_FLAGS, IVAL(hdr, SMB2_HDR_FLAGS) | SMB2_HDR_FLAG_SIGNED);

	sign_algo_id = signing_key->sign_algo_id;

	status = smb2_signing_calc_signature(signing_key,
					     sign_algo_id,
					     vector,
					     count,
					     res);
	if (!NT_STATUS_IS_OK(status)) {
		DBG_ERR("smb2_signing_calc_signature(sign_algo_id=%u) - %s\n",
			(unsigned)sign_algo_id, nt_errstr(status));
		if (NT_STATUS_EQUAL(status, NT_STATUS_INTERNAL_ERROR)) {
			smb_panic(__location__);
		}
		return status;
	}

	DEBUG(5,("signed SMB2 message (sign_algo_id=%u)\n",
		 (unsigned)sign_algo_id));

	memcpy(hdr + SMB2_HDR_SIGNATURE, res, 16);

	return NT_STATUS_OK;
}

NTSTATUS smb2_signing_check_pdu(struct smb2_signing_key *signing_key,
				const struct iovec *vector,
				int count)
{
	uint16_t sign_algo_id;
	const uint8_t *hdr;
	const uint8_t *sig;
	uint64_t session_id;
	uint8_t res[16];
	NTSTATUS status;

	/*
	 * We expect
	 * - SMB2 HDR
	 * - SMB2 BODY FIXED
	 * - (optional) SMB2 BODY DYN
	 * - (optional) PADDING
	 */
	SMB_ASSERT(count >= 2);
	SMB_ASSERT(vector[0].iov_len == SMB2_HDR_BODY);
	SMB_ASSERT(count <= 4);

	hdr = (const uint8_t *)vector[0].iov_base;

	session_id = BVAL(hdr, SMB2_HDR_SESSION_ID);
	if (session_id == 0) {
		/*
		 * do not sign messages with a zero session_id.
		 * See MS-SMB2 3.2.4.1.1
		 */
		return NT_STATUS_OK;
	}

	if (!smb2_signing_key_valid(signing_key)) {
		/* we don't have the session key yet */
		return NT_STATUS_OK;
	}

	sig = hdr+SMB2_HDR_SIGNATURE;

	sign_algo_id = signing_key->sign_algo_id;

	status = smb2_signing_calc_signature(signing_key,
					     sign_algo_id,
					     vector,
					     count,
					     res);
	if (!NT_STATUS_IS_OK(status)) {
		DBG_ERR("smb2_signing_calc_signature(sign_algo_id=%u) - %s\n",
			(unsigned)sign_algo_id, nt_errstr(status));
		if (NT_STATUS_EQUAL(status, NT_STATUS_INTERNAL_ERROR)) {
			status = NT_STATUS_ACCESS_DENIED;
		}
		return status;
	}

	if (!mem_equal_const_time(res, sig, 16)) {
		DEBUG(0,("Bad SMB2 (sign_algo_id=%u) signature for message\n",
			 (unsigned)sign_algo_id));
		dump_data(0, sig, 16);
		dump_data(0, res, 16);
		return NT_STATUS_ACCESS_DENIED;
	}

	return NT_STATUS_OK;
}

NTSTATUS smb2_signing_encrypt_pdu(struct smb2_signing_key *encryption_key,
				  struct iovec *vector,
				  int count)
{
	bool use_encryptv2 = false;
	uint16_t cipher_id;
	uint8_t *tf;
	size_t a_total;
	ssize_t m_total;
	uint32_t iv_size = 0;
	uint32_t key_size = 0;
	size_t tag_size = 0;
	gnutls_cipher_algorithm_t algo = 0;
	gnutls_datum_t key;
	gnutls_datum_t iv;
	NTSTATUS status;
	int rc;

	if (count < 1) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	if (vector[0].iov_len != SMB2_TF_HDR_SIZE) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	tf = (uint8_t *)vector[0].iov_base;

	if (!smb2_signing_key_valid(encryption_key)) {
		DBG_WARNING("No encryption key for SMB2 signing\n");
		return NT_STATUS_ACCESS_DENIED;
	}
	cipher_id = encryption_key->cipher_algo_id;

	a_total = SMB2_TF_HDR_SIZE - SMB2_TF_NONCE;

	m_total = iov_buflen(&vector[1], count-1);
	if (m_total == -1) {
		return NT_STATUS_BUFFER_TOO_SMALL;
	}

	SSVAL(tf, SMB2_TF_FLAGS, SMB2_TF_FLAGS_ENCRYPTED);
	SIVAL(tf, SMB2_TF_MSG_SIZE, m_total);

	switch (cipher_id) {
	case SMB2_ENCRYPTION_AES128_CCM:
		algo = GNUTLS_CIPHER_AES_128_CCM;
		iv_size = SMB2_AES_128_CCM_NONCE_SIZE;
#ifdef ALLOW_GNUTLS_AEAD_CIPHER_ENCRYPTV2_AES_CCM
		use_encryptv2 = true;
#endif
		break;
	case SMB2_ENCRYPTION_AES128_GCM:
		algo = GNUTLS_CIPHER_AES_128_GCM;
		iv_size = gnutls_cipher_get_iv_size(algo);
		use_encryptv2 = true;
		break;
	case SMB2_ENCRYPTION_AES256_CCM:
		algo = GNUTLS_CIPHER_AES_256_CCM;
		iv_size = SMB2_AES_128_CCM_NONCE_SIZE;
#ifdef ALLOW_GNUTLS_AEAD_CIPHER_ENCRYPTV2_AES_CCM
		use_encryptv2 = true;
#endif
		break;
	case SMB2_ENCRYPTION_AES256_GCM:
		algo = GNUTLS_CIPHER_AES_256_GCM;
		iv_size = gnutls_cipher_get_iv_size(algo);
		use_encryptv2 = true;
		break;
	default:
		return NT_STATUS_INVALID_PARAMETER;
	}

	key_size = gnutls_cipher_get_key_size(algo);
	tag_size = gnutls_cipher_get_tag_size(algo);

	if (key_size != encryption_key->blob.length) {
		return NT_STATUS_INTERNAL_ERROR;
	}

	if (tag_size != 16) {
		return NT_STATUS_INTERNAL_ERROR;
	}

	key = (gnutls_datum_t) {
		.data = encryption_key->blob.data,
		.size = key_size,
	};

	iv = (gnutls_datum_t) {
		.data = tf + SMB2_TF_NONCE,
		.size = iv_size,
	};

	if (encryption_key->cipher_hnd == NULL) {
		rc = gnutls_aead_cipher_init(&encryption_key->cipher_hnd,
					algo,
					&key);
		if (rc < 0) {
			status = gnutls_error_to_ntstatus(rc, NT_STATUS_INTERNAL_ERROR);
			goto out;
		}
	}

	memset(tf + SMB2_TF_NONCE + iv_size,
	       0,
	       16 - iv_size);

	if (use_encryptv2) {
		uint8_t tag[tag_size];
		giovec_t auth_iov[1];

		auth_iov[0] = (giovec_t) {
			.iov_base = tf + SMB2_TF_NONCE,
			.iov_len  = a_total,
		};

		rc = gnutls_aead_cipher_encryptv2(encryption_key->cipher_hnd,
						  iv.data,
						  iv.size,
						  auth_iov,
						  1,
						  &vector[1],
						  count - 1,
						  tag,
						  &tag_size);
		if (rc < 0) {
			status = gnutls_error_to_ntstatus(rc, NT_STATUS_INTERNAL_ERROR);
			goto out;
		}

		memcpy(tf + SMB2_TF_SIGNATURE, tag, tag_size);
	} else
	{
		size_t ptext_size = m_total;
		uint8_t *ptext = NULL;
		size_t ctext_size = m_total + tag_size;
		uint8_t *ctext = NULL;
		size_t len = 0;
		int i;
		TALLOC_CTX *tmp_ctx = NULL;

		/*
		 * If we come from python bindings, we don't have a stackframe
		 * around, so use the NULL context.
		 *
		 * This is fine as we make sure we free the memory.
		 */
		if (talloc_stackframe_exists()) {
			tmp_ctx = talloc_tos();
		}

		ptext = talloc_size(tmp_ctx, ptext_size);
		if (ptext == NULL) {
			status = NT_STATUS_NO_MEMORY;
			goto out;
		}

		ctext = talloc_size(tmp_ctx, ctext_size);
		if (ctext == NULL) {
			TALLOC_FREE(ptext);
			status = NT_STATUS_NO_MEMORY;
			goto out;
		}

		for (i = 1; i < count; i++) {
			if (vector[i].iov_base != NULL) {
				memcpy(ptext + len,
				       vector[i].iov_base,
				       vector[i].iov_len);
			}

			len += vector[i].iov_len;
			if (len > ptext_size) {
				TALLOC_FREE(ptext);
				TALLOC_FREE(ctext);
				status = NT_STATUS_INTERNAL_ERROR;
				goto out;
			}
		}

		rc = gnutls_aead_cipher_encrypt(encryption_key->cipher_hnd,
						iv.data,
						iv.size,
						tf + SMB2_TF_NONCE,
						a_total,
						tag_size,
						ptext,
						ptext_size,
						ctext,
						&ctext_size);
		if (rc < 0 || ctext_size != m_total + tag_size) {
			TALLOC_FREE(ptext);
			TALLOC_FREE(ctext);
			status = gnutls_error_to_ntstatus(rc, NT_STATUS_INTERNAL_ERROR);
			goto out;
		}

		len = 0;
		for (i = 1; i < count; i++) {
			if (vector[i].iov_base != NULL) {
				memcpy(vector[i].iov_base,
				       ctext + len,
				       vector[i].iov_len);
			}

			len += vector[i].iov_len;
		}

		memcpy(tf + SMB2_TF_SIGNATURE, ctext + m_total, tag_size);

		TALLOC_FREE(ptext);
		TALLOC_FREE(ctext);
	}

	DBG_INFO("Encrypted SMB2 message\n");

	status = NT_STATUS_OK;
out:
	return status;
}

NTSTATUS smb2_signing_decrypt_pdu(struct smb2_signing_key *decryption_key,
				  struct iovec *vector,
				  int count)
{
	bool use_encryptv2 = false;
	uint16_t cipher_id;
	uint8_t *tf;
	uint16_t flags;
	size_t a_total;
	ssize_t m_total;
	uint32_t msg_size = 0;
	uint32_t iv_size = 0;
	uint32_t key_size = 0;
	size_t tag_size = 0;
	gnutls_cipher_algorithm_t algo = 0;
	gnutls_datum_t key;
	gnutls_datum_t iv;
	NTSTATUS status;
	int rc;

	if (count < 1) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	if (vector[0].iov_len != SMB2_TF_HDR_SIZE) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	tf = (uint8_t *)vector[0].iov_base;

	if (!smb2_signing_key_valid(decryption_key)) {
		DBG_WARNING("No decryption key for SMB2 signing\n");
		return NT_STATUS_ACCESS_DENIED;
	}
	cipher_id = decryption_key->cipher_algo_id;

	a_total = SMB2_TF_HDR_SIZE - SMB2_TF_NONCE;

	m_total = iov_buflen(&vector[1], count-1);
	if (m_total == -1) {
		return NT_STATUS_BUFFER_TOO_SMALL;
	}

	flags = SVAL(tf, SMB2_TF_FLAGS);
	msg_size = IVAL(tf, SMB2_TF_MSG_SIZE);

	if (flags != SMB2_TF_FLAGS_ENCRYPTED) {
		return NT_STATUS_ACCESS_DENIED;
	}

	if (msg_size != m_total) {
		return NT_STATUS_INTERNAL_ERROR;
	}

	switch (cipher_id) {
	case SMB2_ENCRYPTION_AES128_CCM:
		algo = GNUTLS_CIPHER_AES_128_CCM;
		iv_size = SMB2_AES_128_CCM_NONCE_SIZE;
#ifdef ALLOW_GNUTLS_AEAD_CIPHER_ENCRYPTV2_AES_CCM
		use_encryptv2 = true;
#endif
		break;
	case SMB2_ENCRYPTION_AES128_GCM:
		algo = GNUTLS_CIPHER_AES_128_GCM;
		iv_size = gnutls_cipher_get_iv_size(algo);
		use_encryptv2 = true;
		break;
	case SMB2_ENCRYPTION_AES256_CCM:
		algo = GNUTLS_CIPHER_AES_256_CCM;
		iv_size = SMB2_AES_128_CCM_NONCE_SIZE;
#ifdef ALLOW_GNUTLS_AEAD_CIPHER_ENCRYPTV2_AES_CCM
		use_encryptv2 = true;
#endif
		break;
	case SMB2_ENCRYPTION_AES256_GCM:
		algo = GNUTLS_CIPHER_AES_256_GCM;
		iv_size = gnutls_cipher_get_iv_size(algo);
		use_encryptv2 = true;
		break;
	default:
		return NT_STATUS_INVALID_PARAMETER;
	}

	key_size = gnutls_cipher_get_key_size(algo);
	tag_size = gnutls_cipher_get_tag_size(algo);

	if (key_size != decryption_key->blob.length) {
		return NT_STATUS_INTERNAL_ERROR;
	}

	if (tag_size != 16) {
		return NT_STATUS_INTERNAL_ERROR;
	}

	key = (gnutls_datum_t) {
		.data = decryption_key->blob.data,
		.size = key_size,
	};

	iv = (gnutls_datum_t) {
		.data = tf + SMB2_TF_NONCE,
		.size = iv_size,
	};

	if (decryption_key->cipher_hnd == NULL) {
		rc = gnutls_aead_cipher_init(&decryption_key->cipher_hnd,
					     algo,
					     &key);
		if (rc < 0) {
			status = gnutls_error_to_ntstatus(rc, NT_STATUS_INTERNAL_ERROR);
			goto out;
		}
	}

	if (use_encryptv2) {
		giovec_t auth_iov[1];

		auth_iov[0] = (giovec_t) {
			.iov_base = tf + SMB2_TF_NONCE,
			.iov_len  = a_total,
		};

		rc = gnutls_aead_cipher_decryptv2(decryption_key->cipher_hnd,
						  iv.data,
						  iv.size,
						  auth_iov,
						  1,
						  &vector[1],
						  count - 1,
						  tf + SMB2_TF_SIGNATURE,
						  tag_size);
		if (rc < 0) {
			status = gnutls_error_to_ntstatus(rc, NT_STATUS_INTERNAL_ERROR);
			goto out;
		}
	} else
	{
		size_t ctext_size = m_total + tag_size;
		uint8_t *ctext = NULL;
		size_t ptext_size = m_total;
		uint8_t *ptext = NULL;
		size_t len = 0;
		int i;
		TALLOC_CTX *tmp_ctx = NULL;

		/*
		 * If we come from python bindings, we don't have a stackframe
		 * around, so use the NULL context.
		 *
		 * This is fine as we make sure we free the memory.
		 */
		if (talloc_stackframe_exists()) {
			tmp_ctx = talloc_tos();
		}

		/* GnuTLS doesn't have a iovec API for decryption yet */

		ptext = talloc_size(tmp_ctx, ptext_size);
		if (ptext == NULL) {
			status = NT_STATUS_NO_MEMORY;
			goto out;
		}

		ctext = talloc_size(tmp_ctx, ctext_size);
		if (ctext == NULL) {
			TALLOC_FREE(ptext);
			status = NT_STATUS_NO_MEMORY;
			goto out;
		}


		for (i = 1; i < count; i++) {
			memcpy(ctext + len,
			       vector[i].iov_base,
			       vector[i].iov_len);

			len += vector[i].iov_len;
		}
		if (len != m_total) {
			TALLOC_FREE(ptext);
			TALLOC_FREE(ctext);
			status = NT_STATUS_INTERNAL_ERROR;
			goto out;
		}

		memcpy(ctext + len,
		       tf + SMB2_TF_SIGNATURE,
		       tag_size);

		/* This function will verify the tag */
		rc = gnutls_aead_cipher_decrypt(decryption_key->cipher_hnd,
						iv.data,
						iv.size,
						tf + SMB2_TF_NONCE,
						a_total,
						tag_size,
						ctext,
						ctext_size,
						ptext,
						&ptext_size);
		if (rc < 0) {
			TALLOC_FREE(ptext);
			TALLOC_FREE(ctext);
			status = gnutls_error_to_ntstatus(rc, NT_STATUS_INTERNAL_ERROR);
			goto out;
		}
		if (ptext_size != m_total) {
			TALLOC_FREE(ptext);
			TALLOC_FREE(ctext);
			rc = GNUTLS_E_SHORT_MEMORY_BUFFER;
			status = gnutls_error_to_ntstatus(rc, NT_STATUS_INTERNAL_ERROR);
			goto out;
		}

		len = 0;
		for (i = 1; i < count; i++) {
			memcpy(vector[i].iov_base,
			       ptext + len,
			       vector[i].iov_len);

			len += vector[i].iov_len;
		}

		TALLOC_FREE(ptext);
		TALLOC_FREE(ctext);
	}

	DBG_INFO("Decrypted SMB2 message\n");

	status = NT_STATUS_OK;
out:
	return status;
}