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
|
/*
Unix SMB/CIFS implementation.
dcerpc schannel operations
Copyright (C) Andrew Tridgell 2004
Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
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 <tevent.h>
#include "lib/util/tevent_ntstatus.h"
#include "librpc/gen_ndr/ndr_schannel.h"
#include "auth/auth.h"
#include "auth/credentials/credentials.h"
#include "auth/gensec/gensec.h"
#include "auth/gensec/gensec_internal.h"
#include "auth/gensec/gensec_proto.h"
#include "../libcli/auth/schannel.h"
#include "librpc/gen_ndr/dcerpc.h"
#include "param/param.h"
#include "auth/gensec/gensec_toplevel_proto.h"
#include "libds/common/roles.h"
#include "lib/crypto/gnutls_helpers.h"
#include <gnutls/gnutls.h>
#include <gnutls/crypto.h>
#undef DBGC_CLASS
#define DBGC_CLASS DBGC_AUTH
struct schannel_state {
struct gensec_security *gensec;
uint64_t seq_num;
bool initiator;
struct netlogon_creds_CredentialState *creds;
struct auth_user_info_dc *user_info_dc;
};
#define SETUP_SEQNUM(state, buf, initiator) do { \
uint8_t *_buf = buf; \
uint32_t _seq_num_low = (state)->seq_num & UINT32_MAX; \
uint32_t _seq_num_high = (state)->seq_num >> 32; \
if (initiator) { \
_seq_num_high |= 0x80000000; \
} \
RSIVAL(_buf, 0, _seq_num_low); \
RSIVAL(_buf, 4, _seq_num_high); \
} while(0)
static struct schannel_state *netsec_create_state(
struct gensec_security *gensec,
struct netlogon_creds_CredentialState *creds,
bool initiator)
{
struct schannel_state *state;
state = talloc_zero(gensec, struct schannel_state);
if (state == NULL) {
return NULL;
}
state->gensec = gensec;
state->initiator = initiator;
state->creds = netlogon_creds_copy(state, creds);
if (state->creds == NULL) {
talloc_free(state);
return NULL;
}
gensec->private_data = state;
return state;
}
static void netsec_offset_and_sizes(struct schannel_state *state,
bool do_seal,
uint32_t *_min_sig_size,
uint32_t *_used_sig_size,
uint32_t *_checksum_length,
uint32_t *_confounder_ofs)
{
uint32_t min_sig_size;
uint32_t used_sig_size;
uint32_t checksum_length;
uint32_t confounder_ofs;
if (state->creds->negotiate_flags & NETLOGON_NEG_SUPPORTS_AES) {
min_sig_size = 48;
used_sig_size = 56;
/*
* Note: windows has a bug here and uses the old values...
*
* checksum_length = 32;
* confounder_ofs = 48;
*/
checksum_length = 8;
confounder_ofs = 24;
} else {
min_sig_size = 24;
used_sig_size = 32;
checksum_length = 8;
confounder_ofs = 24;
}
if (do_seal) {
min_sig_size += 8;
}
if (_min_sig_size) {
*_min_sig_size = min_sig_size;
}
if (_used_sig_size) {
*_used_sig_size = used_sig_size;
}
if (_checksum_length) {
*_checksum_length = checksum_length;
}
if (_confounder_ofs) {
*_confounder_ofs = confounder_ofs;
}
}
/*******************************************************************
Encode or Decode the sequence number (which is symmetric)
********************************************************************/
static NTSTATUS netsec_do_seq_num(struct schannel_state *state,
const uint8_t *checksum,
uint32_t checksum_length,
uint8_t seq_num[8])
{
if (state->creds->negotiate_flags & NETLOGON_NEG_SUPPORTS_AES) {
gnutls_cipher_hd_t cipher_hnd = NULL;
gnutls_datum_t key = {
.data = state->creds->session_key,
.size = sizeof(state->creds->session_key),
};
uint32_t iv_size =
gnutls_cipher_get_iv_size(GNUTLS_CIPHER_AES_128_CFB8);
uint8_t _iv[iv_size];
gnutls_datum_t iv = {
.data = _iv,
.size = iv_size,
};
int rc;
ZERO_ARRAY(_iv);
memcpy(iv.data + 0, checksum, 8);
memcpy(iv.data + 8, checksum, 8);
rc = gnutls_cipher_init(&cipher_hnd,
GNUTLS_CIPHER_AES_128_CFB8,
&key,
&iv);
if (rc < 0) {
return gnutls_error_to_ntstatus(rc,
NT_STATUS_CRYPTO_SYSTEM_INVALID);
}
rc = gnutls_cipher_encrypt(cipher_hnd, seq_num, 8);
gnutls_cipher_deinit(cipher_hnd);
if (rc < 0) {
return gnutls_error_to_ntstatus(rc,
NT_STATUS_CRYPTO_SYSTEM_INVALID);
}
} else {
static const uint8_t zeros[4];
uint8_t _sequence_key[16];
gnutls_cipher_hd_t cipher_hnd;
gnutls_datum_t sequence_key = {
.data = _sequence_key,
.size = sizeof(_sequence_key),
};
uint8_t digest1[16];
int rc;
rc = gnutls_hmac_fast(GNUTLS_MAC_MD5,
state->creds->session_key,
sizeof(state->creds->session_key),
zeros,
sizeof(zeros),
digest1);
if (rc < 0) {
return gnutls_error_to_ntstatus(rc, NT_STATUS_HMAC_NOT_SUPPORTED);
}
rc = gnutls_hmac_fast(GNUTLS_MAC_MD5,
digest1,
sizeof(digest1),
checksum,
checksum_length,
_sequence_key);
ZERO_ARRAY(digest1);
if (rc < 0) {
return gnutls_error_to_ntstatus(rc, NT_STATUS_HMAC_NOT_SUPPORTED);
}
rc = gnutls_cipher_init(&cipher_hnd,
GNUTLS_CIPHER_ARCFOUR_128,
&sequence_key,
NULL);
if (rc < 0) {
ZERO_ARRAY(_sequence_key);
return gnutls_error_to_ntstatus(rc, NT_STATUS_HMAC_NOT_SUPPORTED);
}
rc = gnutls_cipher_encrypt(cipher_hnd,
seq_num,
8);
gnutls_cipher_deinit(cipher_hnd);
ZERO_ARRAY(_sequence_key);
if (rc < 0) {
return gnutls_error_to_ntstatus(rc, NT_STATUS_HMAC_NOT_SUPPORTED);
}
}
state->seq_num++;
return NT_STATUS_OK;
}
static NTSTATUS netsec_do_seal(struct schannel_state *state,
const uint8_t seq_num[8],
uint8_t confounder[8],
uint8_t *data, uint32_t length,
bool forward)
{
if (state->creds->negotiate_flags & NETLOGON_NEG_SUPPORTS_AES) {
gnutls_cipher_hd_t cipher_hnd = NULL;
uint8_t sess_kf0[16] = {0};
gnutls_datum_t key = {
.data = sess_kf0,
.size = sizeof(sess_kf0),
};
uint32_t iv_size =
gnutls_cipher_get_iv_size(GNUTLS_CIPHER_AES_128_CFB8);
uint8_t _iv[iv_size];
gnutls_datum_t iv = {
.data = _iv,
.size = iv_size,
};
uint32_t i;
int rc;
for (i = 0; i < key.size; i++) {
key.data[i] = state->creds->session_key[i] ^ 0xf0;
}
ZERO_ARRAY(_iv);
memcpy(iv.data + 0, seq_num, 8);
memcpy(iv.data + 8, seq_num, 8);
rc = gnutls_cipher_init(&cipher_hnd,
GNUTLS_CIPHER_AES_128_CFB8,
&key,
&iv);
if (rc < 0) {
DBG_ERR("ERROR: gnutls_cipher_init: %s\n",
gnutls_strerror(rc));
return NT_STATUS_NO_MEMORY;
}
if (forward) {
rc = gnutls_cipher_encrypt(cipher_hnd,
confounder,
8);
if (rc < 0) {
gnutls_cipher_deinit(cipher_hnd);
return gnutls_error_to_ntstatus(rc, NT_STATUS_CRYPTO_SYSTEM_INVALID);
}
rc = gnutls_cipher_encrypt(cipher_hnd,
data,
length);
if (rc < 0) {
gnutls_cipher_deinit(cipher_hnd);
return gnutls_error_to_ntstatus(rc, NT_STATUS_CRYPTO_SYSTEM_INVALID);
}
} else {
/*
* Workaround bug present in gnutls 3.6.8:
*
* gnutls_cipher_decrypt() uses an optimization
* internally that breaks decryption when processing
* buffers with their length not being a multiple
* of the blocksize.
*/
uint8_t tmp[16] = { 0, };
uint32_t tmp_dlength = MIN(length, sizeof(tmp) - 8);
memcpy(tmp, confounder, 8);
memcpy(tmp + 8, data, tmp_dlength);
rc = gnutls_cipher_decrypt(cipher_hnd,
tmp,
8 + tmp_dlength);
if (rc < 0) {
ZERO_STRUCT(tmp);
gnutls_cipher_deinit(cipher_hnd);
return gnutls_error_to_ntstatus(rc, NT_STATUS_CRYPTO_SYSTEM_INVALID);
}
memcpy(confounder, tmp, 8);
memcpy(data, tmp + 8, tmp_dlength);
ZERO_STRUCT(tmp);
if (length > tmp_dlength) {
rc = gnutls_cipher_decrypt(cipher_hnd,
data + tmp_dlength,
length - tmp_dlength);
if (rc < 0) {
gnutls_cipher_deinit(cipher_hnd);
return gnutls_error_to_ntstatus(rc, NT_STATUS_CRYPTO_SYSTEM_INVALID);
}
}
}
gnutls_cipher_deinit(cipher_hnd);
} else {
gnutls_cipher_hd_t cipher_hnd;
uint8_t _sealing_key[16];
gnutls_datum_t sealing_key = {
.data = _sealing_key,
.size = sizeof(_sealing_key),
};
static const uint8_t zeros[4];
uint8_t digest2[16];
uint8_t sess_kf0[16];
int rc;
int i;
for (i = 0; i < 16; i++) {
sess_kf0[i] = state->creds->session_key[i] ^ 0xf0;
}
rc = gnutls_hmac_fast(GNUTLS_MAC_MD5,
sess_kf0,
sizeof(sess_kf0),
zeros,
4,
digest2);
if (rc < 0) {
ZERO_ARRAY(digest2);
return gnutls_error_to_ntstatus(rc, NT_STATUS_HMAC_NOT_SUPPORTED);
}
rc = gnutls_hmac_fast(GNUTLS_MAC_MD5,
digest2,
sizeof(digest2),
seq_num,
8,
_sealing_key);
ZERO_ARRAY(digest2);
if (rc < 0) {
return gnutls_error_to_ntstatus(rc, NT_STATUS_HMAC_NOT_SUPPORTED);
}
rc = gnutls_cipher_init(&cipher_hnd,
GNUTLS_CIPHER_ARCFOUR_128,
&sealing_key,
NULL);
if (rc < 0) {
ZERO_ARRAY(_sealing_key);
return gnutls_error_to_ntstatus(rc, NT_STATUS_CRYPTO_SYSTEM_INVALID);
}
rc = gnutls_cipher_encrypt(cipher_hnd,
confounder,
8);
if (rc < 0) {
ZERO_ARRAY(_sealing_key);
return gnutls_error_to_ntstatus(rc, NT_STATUS_CRYPTO_SYSTEM_INVALID);
}
gnutls_cipher_deinit(cipher_hnd);
rc = gnutls_cipher_init(&cipher_hnd,
GNUTLS_CIPHER_ARCFOUR_128,
&sealing_key,
NULL);
if (rc < 0) {
ZERO_ARRAY(_sealing_key);
return gnutls_error_to_ntstatus(rc, NT_STATUS_CRYPTO_SYSTEM_INVALID);
}
rc = gnutls_cipher_encrypt(cipher_hnd,
data,
length);
gnutls_cipher_deinit(cipher_hnd);
ZERO_ARRAY(_sealing_key);
if (rc < 0) {
return gnutls_error_to_ntstatus(rc, NT_STATUS_CRYPTO_SYSTEM_INVALID);
}
}
return NT_STATUS_OK;
}
/*******************************************************************
Create a digest over the entire packet (including the data), and
MD5 it with the session key.
********************************************************************/
static NTSTATUS netsec_do_sign(struct schannel_state *state,
const uint8_t *confounder,
const uint8_t *data, size_t length,
uint8_t header[8],
uint8_t *checksum)
{
if (state->creds->negotiate_flags & NETLOGON_NEG_SUPPORTS_AES) {
gnutls_hmac_hd_t hmac_hnd = NULL;
int rc;
rc = gnutls_hmac_init(&hmac_hnd,
GNUTLS_MAC_SHA256,
state->creds->session_key,
sizeof(state->creds->session_key));
if (rc < 0) {
return gnutls_error_to_ntstatus(rc, NT_STATUS_HMAC_NOT_SUPPORTED);
}
if (confounder) {
SSVAL(header, 0, NL_SIGN_HMAC_SHA256);
SSVAL(header, 2, NL_SEAL_AES128);
SSVAL(header, 4, 0xFFFF);
SSVAL(header, 6, 0x0000);
rc = gnutls_hmac(hmac_hnd, header, 8);
if (rc < 0) {
gnutls_hmac_deinit(hmac_hnd, NULL);
return gnutls_error_to_ntstatus(rc, NT_STATUS_HMAC_NOT_SUPPORTED);
}
rc = gnutls_hmac(hmac_hnd, confounder, 8);
if (rc < 0) {
gnutls_hmac_deinit(hmac_hnd, NULL);
return gnutls_error_to_ntstatus(rc, NT_STATUS_HMAC_NOT_SUPPORTED);
}
} else {
SSVAL(header, 0, NL_SIGN_HMAC_SHA256);
SSVAL(header, 2, NL_SEAL_NONE);
SSVAL(header, 4, 0xFFFF);
SSVAL(header, 6, 0x0000);
rc = gnutls_hmac(hmac_hnd, header, 8);
if (rc < 0) {
gnutls_hmac_deinit(hmac_hnd, NULL);
return gnutls_error_to_ntstatus(rc, NT_STATUS_HMAC_NOT_SUPPORTED);
}
}
rc = gnutls_hmac(hmac_hnd, data, length);
if (rc < 0) {
gnutls_hmac_deinit(hmac_hnd, NULL);
return gnutls_error_to_ntstatus(rc, NT_STATUS_HMAC_NOT_SUPPORTED);
}
gnutls_hmac_deinit(hmac_hnd, checksum);
} else {
uint8_t packet_digest[16];
static const uint8_t zeros[4];
gnutls_hash_hd_t hash_hnd = NULL;
int rc;
rc = gnutls_hash_init(&hash_hnd, GNUTLS_DIG_MD5);
if (rc < 0) {
return gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED);
}
rc = gnutls_hash(hash_hnd, zeros, sizeof(zeros));
if (rc < 0) {
gnutls_hash_deinit(hash_hnd, NULL);
return gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED);
}
if (confounder) {
SSVAL(header, 0, NL_SIGN_HMAC_MD5);
SSVAL(header, 2, NL_SEAL_RC4);
SSVAL(header, 4, 0xFFFF);
SSVAL(header, 6, 0x0000);
rc = gnutls_hash(hash_hnd, header, 8);
if (rc < 0) {
gnutls_hash_deinit(hash_hnd, NULL);
return gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED);
}
rc = gnutls_hash(hash_hnd, confounder, 8);
if (rc < 0) {
gnutls_hash_deinit(hash_hnd, NULL);
return gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED);
}
} else {
SSVAL(header, 0, NL_SIGN_HMAC_MD5);
SSVAL(header, 2, NL_SEAL_NONE);
SSVAL(header, 4, 0xFFFF);
SSVAL(header, 6, 0x0000);
rc = gnutls_hash(hash_hnd, header, 8);
if (rc < 0) {
gnutls_hash_deinit(hash_hnd, NULL);
return gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED);
}
}
rc = gnutls_hash(hash_hnd, data, length);
if (rc < 0) {
gnutls_hash_deinit(hash_hnd, NULL);
return gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED);
}
gnutls_hash_deinit(hash_hnd, packet_digest);
rc = gnutls_hmac_fast(GNUTLS_MAC_MD5,
state->creds->session_key,
sizeof(state->creds->session_key),
packet_digest,
sizeof(packet_digest),
checksum);
ZERO_ARRAY(packet_digest);
if (rc < 0) {
return gnutls_error_to_ntstatus(rc, NT_STATUS_HMAC_NOT_SUPPORTED);
}
}
return NT_STATUS_OK;
}
static NTSTATUS netsec_incoming_packet(struct schannel_state *state,
bool do_unseal,
uint8_t *data, size_t length,
const uint8_t *whole_pdu, size_t pdu_length,
const DATA_BLOB *sig)
{
uint32_t min_sig_size = 0;
uint8_t header[8];
uint8_t checksum[32];
uint32_t checksum_length = sizeof(checksum_length);
uint8_t _confounder[8];
uint8_t *confounder = NULL;
uint32_t confounder_ofs = 0;
uint8_t seq_num[8];
bool ret;
const uint8_t *sign_data = NULL;
size_t sign_length = 0;
NTSTATUS status;
netsec_offset_and_sizes(state,
do_unseal,
&min_sig_size,
NULL,
&checksum_length,
&confounder_ofs);
if (sig->length < min_sig_size) {
return NT_STATUS_ACCESS_DENIED;
}
if (do_unseal) {
confounder = _confounder;
memcpy(confounder, sig->data+confounder_ofs, 8);
} else {
confounder = NULL;
}
SETUP_SEQNUM(state, seq_num, !state->initiator);
if (do_unseal) {
status = netsec_do_seal(state,
seq_num,
confounder,
data,
length,
false);
if (!NT_STATUS_IS_OK(status)) {
DBG_WARNING("netsec_do_seal failed: %s\n", nt_errstr(status));
return NT_STATUS_ACCESS_DENIED;
}
}
if (state->gensec->want_features & GENSEC_FEATURE_SIGN_PKT_HEADER) {
sign_data = whole_pdu;
sign_length = pdu_length;
} else {
sign_data = data;
sign_length = length;
}
status = netsec_do_sign(state,
confounder,
sign_data,
sign_length,
header,
checksum);
if (!NT_STATUS_IS_OK(status)) {
DBG_WARNING("netsec_do_sign failed: %s\n", nt_errstr(status));
return NT_STATUS_ACCESS_DENIED;
}
ret = mem_equal_const_time(checksum, sig->data+16, checksum_length);
if (!ret) {
dump_data_pw("calc digest:", checksum, checksum_length);
dump_data_pw("wire digest:", sig->data+16, checksum_length);
return NT_STATUS_ACCESS_DENIED;
}
status = netsec_do_seq_num(state, checksum, checksum_length, seq_num);
if (!NT_STATUS_IS_OK(status)) {
DBG_WARNING("netsec_do_seq_num failed: %s\n",
nt_errstr(status));
return status;
}
ZERO_ARRAY(checksum);
ret = mem_equal_const_time(seq_num, sig->data+8, 8);
if (!ret) {
dump_data_pw("calc seq num:", seq_num, 8);
dump_data_pw("wire seq num:", sig->data+8, 8);
return NT_STATUS_ACCESS_DENIED;
}
return NT_STATUS_OK;
}
static uint32_t netsec_outgoing_sig_size(struct schannel_state *state)
{
uint32_t sig_size = 0;
netsec_offset_and_sizes(state,
true,
NULL,
&sig_size,
NULL,
NULL);
return sig_size;
}
static NTSTATUS netsec_outgoing_packet(struct schannel_state *state,
TALLOC_CTX *mem_ctx,
bool do_seal,
uint8_t *data, size_t length,
const uint8_t *whole_pdu, size_t pdu_length,
DATA_BLOB *sig)
{
uint32_t min_sig_size = 0;
uint32_t used_sig_size = 0;
uint8_t header[8];
uint8_t checksum[32];
uint32_t checksum_length = sizeof(checksum_length);
uint8_t _confounder[8];
uint8_t *confounder = NULL;
uint32_t confounder_ofs = 0;
uint8_t seq_num[8];
const uint8_t *sign_data = NULL;
size_t sign_length = 0;
NTSTATUS status;
netsec_offset_and_sizes(state,
do_seal,
&min_sig_size,
&used_sig_size,
&checksum_length,
&confounder_ofs);
SETUP_SEQNUM(state, seq_num, state->initiator);
if (do_seal) {
confounder = _confounder;
generate_random_buffer(confounder, 8);
} else {
confounder = NULL;
}
if (state->gensec->want_features & GENSEC_FEATURE_SIGN_PKT_HEADER) {
sign_data = whole_pdu;
sign_length = pdu_length;
} else {
sign_data = data;
sign_length = length;
}
status = netsec_do_sign(state,
confounder,
sign_data,
sign_length,
header,
checksum);
if (!NT_STATUS_IS_OK(status)) {
DBG_WARNING("netsec_do_sign failed: %s\n", nt_errstr(status));
return NT_STATUS_ACCESS_DENIED;
}
if (do_seal) {
status = netsec_do_seal(state,
seq_num,
confounder,
data,
length,
true);
if (!NT_STATUS_IS_OK(status)) {
DBG_WARNING("netsec_do_seal failed: %s\n",
nt_errstr(status));
return status;
}
}
status = netsec_do_seq_num(state, checksum, checksum_length, seq_num);
if (!NT_STATUS_IS_OK(status)) {
DBG_WARNING("netsec_do_seq_num failed: %s\n",
nt_errstr(status));
return status;
}
(*sig) = data_blob_talloc_zero(mem_ctx, used_sig_size);
memcpy(sig->data, header, 8);
memcpy(sig->data+8, seq_num, 8);
memcpy(sig->data+16, checksum, checksum_length);
if (confounder) {
memcpy(sig->data+confounder_ofs, confounder, 8);
}
dump_data_pw("signature:", sig->data+ 0, 8);
dump_data_pw("seq_num :", sig->data+ 8, 8);
dump_data_pw("digest :", sig->data+16, checksum_length);
dump_data_pw("confound :", sig->data+confounder_ofs, 8);
return NT_STATUS_OK;
}
_PUBLIC_ NTSTATUS gensec_schannel_init(TALLOC_CTX *ctx);
static size_t schannel_sig_size(struct gensec_security *gensec_security, size_t data_size)
{
struct schannel_state *state =
talloc_get_type_abort(gensec_security->private_data,
struct schannel_state);
return netsec_outgoing_sig_size(state);
}
struct schannel_update_state {
NTSTATUS status;
DATA_BLOB out;
};
static NTSTATUS schannel_update_internal(struct gensec_security *gensec_security,
TALLOC_CTX *out_mem_ctx,
const DATA_BLOB in, DATA_BLOB *out);
static struct tevent_req *schannel_update_send(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct gensec_security *gensec_security,
const DATA_BLOB in)
{
struct tevent_req *req;
struct schannel_update_state *state = NULL;
NTSTATUS status;
req = tevent_req_create(mem_ctx, &state,
struct schannel_update_state);
if (req == NULL) {
return NULL;
}
status = schannel_update_internal(gensec_security,
state, in,
&state->out);
state->status = status;
if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
status = NT_STATUS_OK;
}
if (tevent_req_nterror(req, status)) {
return tevent_req_post(req, ev);
}
tevent_req_done(req);
return tevent_req_post(req, ev);
}
static NTSTATUS schannel_update_internal(struct gensec_security *gensec_security,
TALLOC_CTX *out_mem_ctx,
const DATA_BLOB in, DATA_BLOB *out)
{
struct schannel_state *state =
talloc_get_type(gensec_security->private_data,
struct schannel_state);
NTSTATUS status;
enum ndr_err_code ndr_err;
struct NL_AUTH_MESSAGE bind_schannel = {
.Flags = 0,
};
struct NL_AUTH_MESSAGE bind_schannel_ack;
struct netlogon_creds_CredentialState *creds;
const char *workstation;
const char *domain;
*out = data_blob(NULL, 0);
if (gensec_security->dcerpc_auth_level < DCERPC_AUTH_LEVEL_INTEGRITY) {
switch (gensec_security->gensec_role) {
case GENSEC_CLIENT:
return NT_STATUS_INVALID_PARAMETER_MIX;
case GENSEC_SERVER:
return NT_STATUS_INVALID_PARAMETER;
}
return NT_STATUS_INTERNAL_ERROR;
}
switch (gensec_security->gensec_role) {
case GENSEC_CLIENT:
if (state != NULL) {
/* we could parse the bind ack, but we don't know what it is yet */
return NT_STATUS_OK;
}
creds = cli_credentials_get_netlogon_creds(gensec_security->credentials);
if (creds == NULL) {
return NT_STATUS_INVALID_PARAMETER_MIX;
}
state = netsec_create_state(gensec_security,
creds, true /* initiator */);
if (state == NULL) {
return NT_STATUS_NO_MEMORY;
}
bind_schannel.MessageType = NL_NEGOTIATE_REQUEST;
bind_schannel.Flags = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
NL_FLAG_OEM_NETBIOS_COMPUTER_NAME;
bind_schannel.oem_netbios_domain.a = cli_credentials_get_domain(gensec_security->credentials);
bind_schannel.oem_netbios_computer.a = creds->computer_name;
if (creds->secure_channel_type == SEC_CHAN_DNS_DOMAIN) {
bind_schannel.Flags |= NL_FLAG_UTF8_DNS_DOMAIN_NAME;
bind_schannel.utf8_dns_domain.u = cli_credentials_get_realm(gensec_security->credentials);
bind_schannel.Flags |= NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME;
bind_schannel.utf8_netbios_computer.u = creds->computer_name;
}
ndr_err = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel,
(ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
status = ndr_map_error2ntstatus(ndr_err);
DEBUG(3, ("Could not create schannel bind: %s\n",
nt_errstr(status)));
return status;
}
return NT_STATUS_MORE_PROCESSING_REQUIRED;
case GENSEC_SERVER:
if (state != NULL) {
/* no third leg on this protocol */
return NT_STATUS_INVALID_PARAMETER;
}
/* parse the schannel startup blob */
ndr_err = ndr_pull_struct_blob(&in, out_mem_ctx, &bind_schannel,
(ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_MESSAGE);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
status = ndr_map_error2ntstatus(ndr_err);
DEBUG(3, ("Could not parse incoming schannel bind: %s\n",
nt_errstr(status)));
return status;
}
if (bind_schannel.Flags & NL_FLAG_OEM_NETBIOS_DOMAIN_NAME) {
domain = bind_schannel.oem_netbios_domain.a;
if (strcasecmp_m(domain, lpcfg_workgroup(gensec_security->settings->lp_ctx)) != 0) {
DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
domain, lpcfg_workgroup(gensec_security->settings->lp_ctx)));
return NT_STATUS_LOGON_FAILURE;
}
} else if (bind_schannel.Flags & NL_FLAG_UTF8_DNS_DOMAIN_NAME) {
domain = bind_schannel.utf8_dns_domain.u;
if (strcasecmp_m(domain, lpcfg_dnsdomain(gensec_security->settings->lp_ctx)) != 0) {
DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
domain, lpcfg_dnsdomain(gensec_security->settings->lp_ctx)));
return NT_STATUS_LOGON_FAILURE;
}
} else {
DEBUG(3, ("Request for schannel to without domain\n"));
return NT_STATUS_LOGON_FAILURE;
}
if (bind_schannel.Flags & NL_FLAG_OEM_NETBIOS_COMPUTER_NAME) {
workstation = bind_schannel.oem_netbios_computer.a;
} else if (bind_schannel.Flags & NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME) {
workstation = bind_schannel.utf8_netbios_computer.u;
} else {
DEBUG(3, ("Request for schannel to without netbios workstation\n"));
return NT_STATUS_LOGON_FAILURE;
}
status = schannel_get_creds_state(out_mem_ctx,
gensec_security->settings->lp_ctx,
workstation, &creds);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(3, ("Could not find session key for attempted schannel connection from %s: %s\n",
workstation, nt_errstr(status)));
if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
return NT_STATUS_LOGON_FAILURE;
}
return status;
}
state = netsec_create_state(gensec_security,
creds, false /* not initiator */);
if (state == NULL) {
return NT_STATUS_NO_MEMORY;
}
status = auth_anonymous_user_info_dc(state,
lpcfg_netbios_name(gensec_security->settings->lp_ctx),
&state->user_info_dc);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
bind_schannel_ack.MessageType = NL_NEGOTIATE_RESPONSE;
bind_schannel_ack.Flags = 0;
bind_schannel_ack.Buffer.dummy = 0x6c0000; /* actually I think
* this does not have
* any meaning here
* - gd */
ndr_err = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel_ack,
(ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
status = ndr_map_error2ntstatus(ndr_err);
DEBUG(3, ("Could not return schannel bind ack for client %s: %s\n",
workstation, nt_errstr(status)));
return status;
}
return NT_STATUS_OK;
}
return NT_STATUS_INVALID_PARAMETER;
}
static NTSTATUS schannel_update_recv(struct tevent_req *req,
TALLOC_CTX *out_mem_ctx,
DATA_BLOB *out)
{
struct schannel_update_state *state =
tevent_req_data(req,
struct schannel_update_state);
NTSTATUS status;
*out = data_blob_null;
if (tevent_req_is_nterror(req, &status)) {
tevent_req_received(req);
return status;
}
status = state->status;
talloc_steal(out_mem_ctx, state->out.data);
*out = state->out;
tevent_req_received(req);
return status;
}
/**
* Returns anonymous credentials for schannel, matching Win2k3.
*
*/
static NTSTATUS schannel_session_info(struct gensec_security *gensec_security,
TALLOC_CTX *mem_ctx,
struct auth_session_info **_session_info)
{
struct schannel_state *state =
talloc_get_type(gensec_security->private_data,
struct schannel_state);
struct auth4_context *auth_ctx = gensec_security->auth_context;
struct auth_session_info *session_info = NULL;
uint32_t session_info_flags = 0;
NTSTATUS status;
if (auth_ctx == NULL) {
DEBUG(0, ("Cannot generate a session_info without the auth_context\n"));
return NT_STATUS_INTERNAL_ERROR;
}
if (auth_ctx->generate_session_info == NULL) {
DEBUG(0, ("Cannot generate a session_info without the generate_session_info hook\n"));
return NT_STATUS_INTERNAL_ERROR;
}
if (gensec_security->want_features & GENSEC_FEATURE_UNIX_TOKEN) {
session_info_flags |= AUTH_SESSION_INFO_UNIX_TOKEN;
}
session_info_flags |= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES;
status = auth_ctx->generate_session_info(
auth_ctx,
mem_ctx,
state->user_info_dc,
state->user_info_dc->info->account_name,
session_info_flags,
&session_info);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
*_session_info = session_info;
return NT_STATUS_OK;
}
/*
* Reduce the attack surface by ensuring schannel is not available when
* we are not a DC
*/
static NTSTATUS schannel_server_start(struct gensec_security *gensec_security)
{
enum server_role server_role
= lpcfg_server_role(gensec_security->settings->lp_ctx);
switch (server_role) {
case ROLE_DOMAIN_BDC:
case ROLE_DOMAIN_PDC:
case ROLE_ACTIVE_DIRECTORY_DC:
case ROLE_IPA_DC:
return NT_STATUS_OK;
default:
return NT_STATUS_NOT_IMPLEMENTED;
}
}
static NTSTATUS schannel_client_start(struct gensec_security *gensec_security)
{
return NT_STATUS_OK;
}
static bool schannel_have_feature(struct gensec_security *gensec_security,
uint32_t feature)
{
if (gensec_security->dcerpc_auth_level >= DCERPC_AUTH_LEVEL_INTEGRITY) {
if (feature & GENSEC_FEATURE_SIGN) {
return true;
}
}
if (gensec_security->dcerpc_auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
if (feature & GENSEC_FEATURE_SEAL) {
return true;
}
}
if (feature & GENSEC_FEATURE_DCE_STYLE) {
return true;
}
if (feature & GENSEC_FEATURE_SIGN_PKT_HEADER) {
return true;
}
return false;
}
/*
unseal a packet
*/
static NTSTATUS schannel_unseal_packet(struct gensec_security *gensec_security,
uint8_t *data, size_t length,
const uint8_t *whole_pdu, size_t pdu_length,
const DATA_BLOB *sig)
{
struct schannel_state *state =
talloc_get_type_abort(gensec_security->private_data,
struct schannel_state);
return netsec_incoming_packet(state, true,
discard_const_p(uint8_t, data),
length,
whole_pdu, pdu_length,
sig);
}
/*
check the signature on a packet
*/
static NTSTATUS schannel_check_packet(struct gensec_security *gensec_security,
const uint8_t *data, size_t length,
const uint8_t *whole_pdu, size_t pdu_length,
const DATA_BLOB *sig)
{
struct schannel_state *state =
talloc_get_type_abort(gensec_security->private_data,
struct schannel_state);
return netsec_incoming_packet(state, false,
discard_const_p(uint8_t, data),
length,
whole_pdu, pdu_length,
sig);
}
/*
seal a packet
*/
static NTSTATUS schannel_seal_packet(struct gensec_security *gensec_security,
TALLOC_CTX *mem_ctx,
uint8_t *data, size_t length,
const uint8_t *whole_pdu, size_t pdu_length,
DATA_BLOB *sig)
{
struct schannel_state *state =
talloc_get_type_abort(gensec_security->private_data,
struct schannel_state);
return netsec_outgoing_packet(state, mem_ctx, true,
data, length,
whole_pdu, pdu_length,
sig);
}
/*
sign a packet
*/
static NTSTATUS schannel_sign_packet(struct gensec_security *gensec_security,
TALLOC_CTX *mem_ctx,
const uint8_t *data, size_t length,
const uint8_t *whole_pdu, size_t pdu_length,
DATA_BLOB *sig)
{
struct schannel_state *state =
talloc_get_type_abort(gensec_security->private_data,
struct schannel_state);
return netsec_outgoing_packet(state, mem_ctx, false,
discard_const_p(uint8_t, data),
length,
whole_pdu, pdu_length,
sig);
}
static const struct gensec_security_ops gensec_schannel_security_ops = {
.name = "schannel",
.auth_type = DCERPC_AUTH_TYPE_SCHANNEL,
.client_start = schannel_client_start,
.server_start = schannel_server_start,
.update_send = schannel_update_send,
.update_recv = schannel_update_recv,
.seal_packet = schannel_seal_packet,
.sign_packet = schannel_sign_packet,
.check_packet = schannel_check_packet,
.unseal_packet = schannel_unseal_packet,
.session_info = schannel_session_info,
.sig_size = schannel_sig_size,
.have_feature = schannel_have_feature,
.enabled = true,
.priority = GENSEC_SCHANNEL
};
_PUBLIC_ NTSTATUS gensec_schannel_init(TALLOC_CTX *ctx)
{
NTSTATUS ret;
ret = gensec_register(ctx, &gensec_schannel_security_ops);
if (!NT_STATUS_IS_OK(ret)) {
DEBUG(0,("Failed to register '%s' gensec backend!\n",
gensec_schannel_security_ops.name));
return ret;
}
return ret;
}
|