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
|
/*
Unix SMB/CIFS implementation.
SMB Transport encryption (sealing) code - server code.
Copyright (C) Jeremy Allison 2007.
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 "smbd/smbd.h"
#include "smbd/globals.h"
#include "../libcli/auth/spnego.h"
#include "../libcli/auth/ntlmssp.h"
#include "ntlmssp_wrap.h"
#include "smb_crypt.h"
#include "../lib/util/asn1.h"
#include "auth.h"
#include "libsmb/libsmb.h"
#include "../lib/tsocket/tsocket.h"
/******************************************************************************
Server side encryption.
******************************************************************************/
/******************************************************************************
Global server state.
******************************************************************************/
struct smb_srv_trans_enc_ctx {
struct smb_trans_enc_state *es;
struct auth_ntlmssp_state *auth_ntlmssp_state; /* Must be kept in sync with pointer in ec->ntlmssp_state. */
};
/******************************************************************************
Return global enc context - this must change if we ever do multiple contexts.
******************************************************************************/
static uint16_t srv_enc_ctx(const struct smb_srv_trans_enc_ctx *ec)
{
return ec->es->enc_ctx_num;
}
/******************************************************************************
Is this an incoming encrypted packet ?
******************************************************************************/
bool is_encrypted_packet(struct smbd_server_connection *sconn,
const uint8_t *inbuf)
{
NTSTATUS status;
uint16_t enc_num;
/* Ignore non-session messages or non 0xFF'E' messages. */
if(CVAL(inbuf,0)
|| (smb_len(inbuf) < 8)
|| !(inbuf[4] == 0xFF && inbuf[5] == 'E')) {
return false;
}
status = get_enc_ctx_num(inbuf, &enc_num);
if (!NT_STATUS_IS_OK(status)) {
return false;
}
/* Encrypted messages are 0xFF'E'<ctx> */
if (srv_trans_enc_ctx && enc_num == srv_enc_ctx(srv_trans_enc_ctx)) {
return true;
}
return false;
}
/******************************************************************************
Create an auth_ntlmssp_state and ensure pointer copy is correct.
******************************************************************************/
static NTSTATUS make_auth_ntlmssp(const struct tsocket_address *remote_address,
struct smb_srv_trans_enc_ctx *ec)
{
NTSTATUS status = auth_ntlmssp_prepare(remote_address,
&ec->auth_ntlmssp_state);
if (!NT_STATUS_IS_OK(status)) {
return nt_status_squash(status);
}
auth_ntlmssp_want_feature(ec->auth_ntlmssp_state, NTLMSSP_FEATURE_SEAL);
status = auth_ntlmssp_start(ec->auth_ntlmssp_state);
if (!NT_STATUS_IS_OK(status)) {
return nt_status_squash(status);
}
/*
* We must remember to update the pointer copy for the common
* functions after any auth_ntlmssp_start/auth_ntlmssp_end.
*/
ec->es->s.auth_ntlmssp_state = ec->auth_ntlmssp_state;
return status;
}
/******************************************************************************
Destroy an auth_ntlmssp_state and ensure pointer copy is correct.
******************************************************************************/
static void destroy_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
{
/*
* We must remember to update the pointer copy for the common
* functions after any auth_ntlmssp_start/auth_ntlmssp_end.
*/
if (ec->auth_ntlmssp_state) {
TALLOC_FREE(ec->auth_ntlmssp_state);
/* The auth_ntlmssp_end killed this already. */
ec->es->s.auth_ntlmssp_state = NULL;
}
}
#if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
/******************************************************************************
Import a name.
******************************************************************************/
static NTSTATUS get_srv_gss_creds(const char *service,
const char *name,
gss_cred_usage_t cred_type,
gss_cred_id_t *p_srv_cred)
{
OM_uint32 ret;
OM_uint32 min;
gss_name_t srv_name;
gss_buffer_desc input_name;
char *host_princ_s = NULL;
NTSTATUS status = NT_STATUS_OK;
gss_OID_desc nt_hostbased_service =
{10, discard_const_p(char, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
if (asprintf(&host_princ_s, "%s@%s", service, name) == -1) {
return NT_STATUS_NO_MEMORY;
}
input_name.value = host_princ_s;
input_name.length = strlen(host_princ_s) + 1;
ret = gss_import_name(&min,
&input_name,
&nt_hostbased_service,
&srv_name);
DEBUG(10,("get_srv_gss_creds: imported name %s\n",
host_princ_s ));
if (ret != GSS_S_COMPLETE) {
SAFE_FREE(host_princ_s);
return map_nt_error_from_gss(ret, min);
}
/*
* We're accessing the krb5.keytab file here.
* ensure we have permissions to do so.
*/
become_root();
ret = gss_acquire_cred(&min,
srv_name,
GSS_C_INDEFINITE,
GSS_C_NULL_OID_SET,
cred_type,
p_srv_cred,
NULL,
NULL);
unbecome_root();
if (ret != GSS_S_COMPLETE) {
ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
DEBUG(10,("get_srv_gss_creds: gss_acquire_cred failed with %s\n",
ads_errstr(adss)));
status = map_nt_error_from_gss(ret, min);
}
SAFE_FREE(host_princ_s);
gss_release_name(&min, &srv_name);
return status;
}
/******************************************************************************
Create a gss state.
Try and get the cifs/server@realm principal first, then fall back to
host/server@realm.
******************************************************************************/
static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
{
NTSTATUS status;
gss_cred_id_t srv_cred;
fstring fqdn;
name_to_fqdn(fqdn, lp_netbios_name());
strlower_m(fqdn);
status = get_srv_gss_creds("cifs", fqdn, GSS_C_ACCEPT, &srv_cred);
if (!NT_STATUS_IS_OK(status)) {
status = get_srv_gss_creds("host", fqdn, GSS_C_ACCEPT, &srv_cred);
if (!NT_STATUS_IS_OK(status)) {
return nt_status_squash(status);
}
}
ec->es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
if (!ec->es->s.gss_state) {
OM_uint32 min;
gss_release_cred(&min, &srv_cred);
return NT_STATUS_NO_MEMORY;
}
ZERO_STRUCTP(ec->es->s.gss_state);
ec->es->s.gss_state->creds = srv_cred;
/* No context yet. */
ec->es->s.gss_state->gss_ctx = GSS_C_NO_CONTEXT;
return NT_STATUS_OK;
}
#endif
/******************************************************************************
Shutdown a server encryption context.
******************************************************************************/
static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
{
struct smb_srv_trans_enc_ctx *ec = *pp_ec;
if (!ec) {
return;
}
if (ec->es) {
switch (ec->es->smb_enc_type) {
case SMB_TRANS_ENC_NTLM:
destroy_auth_ntlmssp(ec);
break;
#if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
case SMB_TRANS_ENC_GSS:
break;
#endif
}
common_free_encryption_state(&ec->es);
}
SAFE_FREE(ec);
*pp_ec = NULL;
}
/******************************************************************************
Create a server encryption context.
******************************************************************************/
static NTSTATUS make_srv_encryption_context(const struct tsocket_address *remote_address,
enum smb_trans_enc_type smb_enc_type,
struct smb_srv_trans_enc_ctx **pp_ec)
{
struct smb_srv_trans_enc_ctx *ec;
*pp_ec = NULL;
ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
if (!ec) {
return NT_STATUS_NO_MEMORY;
}
ZERO_STRUCTP(partial_srv_trans_enc_ctx);
ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
if (!ec->es) {
SAFE_FREE(ec);
return NT_STATUS_NO_MEMORY;
}
ZERO_STRUCTP(ec->es);
ec->es->smb_enc_type = smb_enc_type;
switch (smb_enc_type) {
case SMB_TRANS_ENC_NTLM:
{
NTSTATUS status = make_auth_ntlmssp(remote_address,
ec);
if (!NT_STATUS_IS_OK(status)) {
srv_free_encryption_context(&ec);
return status;
}
}
break;
#if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
case SMB_TRANS_ENC_GSS:
/* Acquire our credentials by calling gss_acquire_cred here. */
{
NTSTATUS status = make_auth_gss(ec);
if (!NT_STATUS_IS_OK(status)) {
srv_free_encryption_context(&ec);
return status;
}
}
break;
#endif
default:
srv_free_encryption_context(&ec);
return NT_STATUS_INVALID_PARAMETER;
}
*pp_ec = ec;
return NT_STATUS_OK;
}
/******************************************************************************
Free an encryption-allocated buffer.
******************************************************************************/
void srv_free_enc_buffer(struct smbd_server_connection *sconn, char *buf)
{
/* We know this is an smb buffer, and we
* didn't malloc, only copy, for a keepalive,
* so ignore non-session messages. */
if(CVAL(buf,0)) {
return;
}
if (srv_trans_enc_ctx) {
common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
}
}
/******************************************************************************
Decrypt an incoming buffer.
******************************************************************************/
NTSTATUS srv_decrypt_buffer(char *buf)
{
/* Ignore non-session messages. */
if(CVAL(buf,0)) {
return NT_STATUS_OK;
}
if (srv_trans_enc_ctx) {
return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
}
return NT_STATUS_OK;
}
/******************************************************************************
Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
******************************************************************************/
NTSTATUS srv_encrypt_buffer(char *buf, char **buf_out)
{
*buf_out = buf;
/* Ignore non-session messages. */
if(CVAL(buf,0)) {
return NT_STATUS_OK;
}
if (srv_trans_enc_ctx) {
return common_encrypt_buffer(srv_trans_enc_ctx->es, buf, buf_out);
}
/* Not encrypting. */
return NT_STATUS_OK;
}
/******************************************************************************
Do the gss encryption negotiation. Parameters are in/out.
Until success we do everything on the partial enc ctx.
******************************************************************************/
#if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
static NTSTATUS srv_enc_spnego_gss_negotiate(const struct tsocket_address *remote_address,
unsigned char **ppdata,
size_t *p_data_size,
DATA_BLOB secblob)
{
OM_uint32 ret;
OM_uint32 min;
OM_uint32 flags = 0;
gss_buffer_desc in_buf, out_buf;
struct smb_tran_enc_state_gss *gss_state;
DATA_BLOB auth_reply = data_blob_null;
DATA_BLOB response = data_blob_null;
NTSTATUS status;
if (!partial_srv_trans_enc_ctx) {
status = make_srv_encryption_context(remote_address,
SMB_TRANS_ENC_GSS,
&partial_srv_trans_enc_ctx);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
}
gss_state = partial_srv_trans_enc_ctx->es->s.gss_state;
in_buf.value = secblob.data;
in_buf.length = secblob.length;
out_buf.value = NULL;
out_buf.length = 0;
become_root();
ret = gss_accept_sec_context(&min,
&gss_state->gss_ctx,
gss_state->creds,
&in_buf,
GSS_C_NO_CHANNEL_BINDINGS,
NULL,
NULL, /* Ignore oids. */
&out_buf, /* To return. */
&flags,
NULL, /* Ingore time. */
NULL); /* Ignore delegated creds. */
unbecome_root();
status = gss_err_to_ntstatus(ret, min);
if (ret != GSS_S_COMPLETE && ret != GSS_S_CONTINUE_NEEDED) {
return status;
}
/* Ensure we've got sign+seal available. */
if (ret == GSS_S_COMPLETE) {
if ((flags & (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) !=
(GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) {
DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
"for SMB sealing.\n"));
gss_release_buffer(&min, &out_buf);
return NT_STATUS_ACCESS_DENIED;
}
}
auth_reply = data_blob(out_buf.value, out_buf.length);
gss_release_buffer(&min, &out_buf);
/* Wrap in SPNEGO. */
response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, OID_KERBEROS5);
data_blob_free(&auth_reply);
SAFE_FREE(*ppdata);
*ppdata = (unsigned char *)memdup(response.data, response.length);
if ((*ppdata) == NULL && response.length > 0) {
status = NT_STATUS_NO_MEMORY;
}
*p_data_size = response.length;
data_blob_free(&response);
return status;
}
#endif
/******************************************************************************
Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
Until success we do everything on the partial enc ctx.
******************************************************************************/
static NTSTATUS srv_enc_ntlm_negotiate(const struct tsocket_address *remote_address,
unsigned char **ppdata,
size_t *p_data_size,
DATA_BLOB secblob,
bool spnego_wrap)
{
NTSTATUS status;
DATA_BLOB chal = data_blob_null;
DATA_BLOB response = data_blob_null;
status = make_srv_encryption_context(remote_address,
SMB_TRANS_ENC_NTLM,
&partial_srv_trans_enc_ctx);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state,
partial_srv_trans_enc_ctx->auth_ntlmssp_state,
secblob, &chal);
/* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
* for success ... */
if (spnego_wrap) {
response = spnego_gen_auth_response(talloc_tos(), &chal, status, OID_NTLMSSP);
data_blob_free(&chal);
} else {
/* Return the raw blob. */
response = chal;
}
SAFE_FREE(*ppdata);
*ppdata = (unsigned char *)memdup(response.data, response.length);
if ((*ppdata) == NULL && response.length > 0) {
status = NT_STATUS_NO_MEMORY;
}
*p_data_size = response.length;
data_blob_free(&response);
return status;
}
/******************************************************************************
Do the SPNEGO encryption negotiation. Parameters are in/out.
Based off code in smbd/sesssionsetup.c
Until success we do everything on the partial enc ctx.
******************************************************************************/
static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
unsigned char **ppdata,
size_t *p_data_size,
unsigned char **pparam,
size_t *p_param_size)
{
NTSTATUS status;
DATA_BLOB blob = data_blob_null;
DATA_BLOB secblob = data_blob_null;
char *kerb_mech = NULL;
blob = data_blob_const(*ppdata, *p_data_size);
status = parse_spnego_mechanisms(talloc_tos(), blob, &secblob, &kerb_mech);
if (!NT_STATUS_IS_OK(status)) {
return nt_status_squash(status);
}
/* We should have no partial context at this point. */
srv_free_encryption_context(&partial_srv_trans_enc_ctx);
if (kerb_mech) {
TALLOC_FREE(kerb_mech);
#if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
status = srv_enc_spnego_gss_negotiate(conn->sconn->remote_address,
ppdata,
p_data_size,
secblob);
#else
/* Currently we don't SPNEGO negotiate
* back to NTLMSSP as we do in sessionsetupX. We should... */
return NT_STATUS_LOGON_FAILURE;
#endif
} else {
status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
ppdata,
p_data_size,
secblob,
true);
}
data_blob_free(&secblob);
if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
srv_free_encryption_context(&partial_srv_trans_enc_ctx);
return nt_status_squash(status);
}
if (NT_STATUS_IS_OK(status)) {
/* Return the context we're using for this encryption state. */
if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
return NT_STATUS_NO_MEMORY;
}
SSVAL(*pparam,0,partial_srv_trans_enc_ctx->es->enc_ctx_num);
*p_param_size = 2;
}
return status;
}
/******************************************************************************
Complete a SPNEGO encryption negotiation. Parameters are in/out.
We only get this for a NTLM auth second stage.
******************************************************************************/
static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
unsigned char **ppdata,
size_t *p_data_size,
unsigned char **pparam,
size_t *p_param_size)
{
NTSTATUS status;
DATA_BLOB blob = data_blob_null;
DATA_BLOB auth = data_blob_null;
DATA_BLOB auth_reply = data_blob_null;
DATA_BLOB response = data_blob_null;
struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
/* We must have a partial context here. */
if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
srv_free_encryption_context(&partial_srv_trans_enc_ctx);
return NT_STATUS_INVALID_PARAMETER;
}
blob = data_blob_const(*ppdata, *p_data_size);
if (!spnego_parse_auth(talloc_tos(), blob, &auth)) {
srv_free_encryption_context(&partial_srv_trans_enc_ctx);
return NT_STATUS_INVALID_PARAMETER;
}
status = auth_ntlmssp_update(ec->auth_ntlmssp_state, talloc_tos(), auth, &auth_reply);
data_blob_free(&auth);
/* From RFC4178.
*
* supportedMech
*
* This field SHALL only be present in the first reply from the
* target.
* So set mechOID to NULL here.
*/
response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, NULL);
data_blob_free(&auth_reply);
if (NT_STATUS_IS_OK(status)) {
/* Return the context we're using for this encryption state. */
if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
return NT_STATUS_NO_MEMORY;
}
SSVAL(*pparam,0,ec->es->enc_ctx_num);
*p_param_size = 2;
}
SAFE_FREE(*ppdata);
*ppdata = (unsigned char *)memdup(response.data, response.length);
if ((*ppdata) == NULL && response.length > 0)
return NT_STATUS_NO_MEMORY;
*p_data_size = response.length;
data_blob_free(&response);
return status;
}
/******************************************************************************
Raw NTLM encryption negotiation. Parameters are in/out.
This function does both steps.
******************************************************************************/
static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
unsigned char **ppdata,
size_t *p_data_size,
unsigned char **pparam,
size_t *p_param_size)
{
NTSTATUS status;
DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
DATA_BLOB response = data_blob_null;
struct smb_srv_trans_enc_ctx *ec;
if (!partial_srv_trans_enc_ctx) {
/* This is the initial step. */
status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
ppdata,
p_data_size,
blob,
false);
if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
srv_free_encryption_context(&partial_srv_trans_enc_ctx);
return nt_status_squash(status);
}
return status;
}
ec = partial_srv_trans_enc_ctx;
if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
srv_free_encryption_context(&partial_srv_trans_enc_ctx);
return NT_STATUS_INVALID_PARAMETER;
}
/* Second step. */
status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state,
talloc_tos(),
blob, &response);
if (NT_STATUS_IS_OK(status)) {
/* Return the context we're using for this encryption state. */
if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
return NT_STATUS_NO_MEMORY;
}
SSVAL(*pparam,0,ec->es->enc_ctx_num);
*p_param_size = 2;
}
/* Return the raw blob. */
SAFE_FREE(*ppdata);
*ppdata = (unsigned char *)memdup(response.data, response.length);
if ((*ppdata) == NULL && response.length > 0)
return NT_STATUS_NO_MEMORY;
*p_data_size = response.length;
data_blob_free(&response);
return status;
}
/******************************************************************************
Do the SPNEGO encryption negotiation. Parameters are in/out.
******************************************************************************/
NTSTATUS srv_request_encryption_setup(connection_struct *conn,
unsigned char **ppdata,
size_t *p_data_size,
unsigned char **pparam,
size_t *p_param_size)
{
unsigned char *pdata = *ppdata;
SAFE_FREE(*pparam);
*p_param_size = 0;
if (*p_data_size < 1) {
return NT_STATUS_INVALID_PARAMETER;
}
if (pdata[0] == ASN1_APPLICATION(0)) {
/* its a negTokenTarg packet */
return srv_enc_spnego_negotiate(conn, ppdata, p_data_size, pparam, p_param_size);
}
if (pdata[0] == ASN1_CONTEXT(1)) {
/* It's an auth packet */
return srv_enc_spnego_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
}
/* Maybe it's a raw unwrapped auth ? */
if (*p_data_size < 7) {
return NT_STATUS_INVALID_PARAMETER;
}
if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
return srv_enc_raw_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
}
DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
return NT_STATUS_LOGON_FAILURE;
}
/******************************************************************************
Negotiation was successful - turn on server-side encryption.
******************************************************************************/
static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
{
if (!ec || !ec->es) {
return NT_STATUS_LOGON_FAILURE;
}
if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
if (!auth_ntlmssp_negotiated_sign((ec->auth_ntlmssp_state))) {
return NT_STATUS_INVALID_PARAMETER;
}
if (!auth_ntlmssp_negotiated_seal((ec->auth_ntlmssp_state))) {
return NT_STATUS_INVALID_PARAMETER;
}
}
/* Todo - check gssapi case. */
return NT_STATUS_OK;
}
/******************************************************************************
Negotiation was successful - turn on server-side encryption.
******************************************************************************/
NTSTATUS srv_encryption_start(connection_struct *conn)
{
NTSTATUS status;
/* Check that we are really doing sign+seal. */
status = check_enc_good(partial_srv_trans_enc_ctx);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
/* Throw away the context we're using currently (if any). */
srv_free_encryption_context(&srv_trans_enc_ctx);
/* Steal the partial pointer. Deliberate shallow copy. */
srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
srv_trans_enc_ctx->es->enc_on = true;
partial_srv_trans_enc_ctx = NULL;
DEBUG(1,("srv_encryption_start: context negotiated\n"));
return NT_STATUS_OK;
}
/******************************************************************************
Shutdown all server contexts.
******************************************************************************/
void server_encryption_shutdown(void)
{
srv_free_encryption_context(&partial_srv_trans_enc_ctx);
srv_free_encryption_context(&srv_trans_enc_ctx);
}
|