s3-ntlmssp Remove auth_ntlmssp_negotiated_sign() and auth_ntlmssp_negotiated_seal()
[rusty/samba.git] / source3 / smbd / seal.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB Transport encryption (sealing) code - server code.
4    Copyright (C) Jeremy Allison 2007.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "smbd/smbd.h"
22 #include "smbd/globals.h"
23 #include "../libcli/auth/spnego.h"
24 #include "../auth/ntlmssp/ntlmssp.h"
25 #include "ntlmssp_wrap.h"
26 #include "smb_crypt.h"
27 #include "../lib/util/asn1.h"
28 #include "auth.h"
29 #include "libsmb/libsmb.h"
30 #include "../lib/tsocket/tsocket.h"
31 #include "auth/gensec/gensec.h"
32
33 /******************************************************************************
34  Server side encryption.
35 ******************************************************************************/
36
37 /******************************************************************************
38  Global server state.
39 ******************************************************************************/
40
41 struct smb_srv_trans_enc_ctx {
42         struct smb_trans_enc_state *es;
43         struct auth_ntlmssp_state *auth_ntlmssp_state; /* Must be kept in sync with pointer in ec->ntlmssp_state. */
44 };
45
46 /******************************************************************************
47  Return global enc context - this must change if we ever do multiple contexts.
48 ******************************************************************************/
49
50 static uint16_t srv_enc_ctx(const struct smb_srv_trans_enc_ctx *ec)
51 {
52         return ec->es->enc_ctx_num;
53 }
54
55 /******************************************************************************
56  Is this an incoming encrypted packet ?
57 ******************************************************************************/
58
59 bool is_encrypted_packet(struct smbd_server_connection *sconn,
60                          const uint8_t *inbuf)
61 {
62         NTSTATUS status;
63         uint16_t enc_num;
64
65         /* Ignore non-session messages or non 0xFF'E' messages. */
66         if(CVAL(inbuf,0)
67            || (smb_len(inbuf) < 8)
68            || !(inbuf[4] == 0xFF && inbuf[5] == 'E')) {
69                 return false;
70         }
71
72         status = get_enc_ctx_num(inbuf, &enc_num);
73         if (!NT_STATUS_IS_OK(status)) {
74                 return false;
75         }
76
77         /* Encrypted messages are 0xFF'E'<ctx> */
78         if (srv_trans_enc_ctx && enc_num == srv_enc_ctx(srv_trans_enc_ctx)) {
79                 return true;
80         }
81         return false;
82 }
83
84 /******************************************************************************
85  Create an auth_ntlmssp_state and ensure pointer copy is correct.
86 ******************************************************************************/
87
88 static NTSTATUS make_auth_ntlmssp(const struct tsocket_address *remote_address,
89                                   struct smb_srv_trans_enc_ctx *ec)
90 {
91         NTSTATUS status = auth_ntlmssp_prepare(remote_address,
92                                              &ec->auth_ntlmssp_state);
93         if (!NT_STATUS_IS_OK(status)) {
94                 return nt_status_squash(status);
95         }
96
97         auth_ntlmssp_want_feature(ec->auth_ntlmssp_state, NTLMSSP_FEATURE_SEAL);
98
99         status = auth_ntlmssp_start(ec->auth_ntlmssp_state);
100
101         if (!NT_STATUS_IS_OK(status)) {
102                 return nt_status_squash(status);
103         }
104
105         /*
106          * We must remember to update the pointer copy for the common
107          * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
108          */
109         ec->es->s.auth_ntlmssp_state = ec->auth_ntlmssp_state;
110         return status;
111 }
112
113 /******************************************************************************
114  Destroy an auth_ntlmssp_state and ensure pointer copy is correct.
115 ******************************************************************************/
116
117 static void destroy_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
118 {
119         /*
120          * We must remember to update the pointer copy for the common
121          * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
122          */
123
124         if (ec->auth_ntlmssp_state) {
125                 TALLOC_FREE(ec->auth_ntlmssp_state);
126                 /* The auth_ntlmssp_end killed this already. */
127                 ec->es->s.auth_ntlmssp_state = NULL;
128         }
129 }
130
131 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
132
133 /******************************************************************************
134  Import a name.
135 ******************************************************************************/
136
137 static NTSTATUS get_srv_gss_creds(const char *service,
138                                 const char *name,
139                                 gss_cred_usage_t cred_type,
140                                 gss_cred_id_t *p_srv_cred)
141 {
142         OM_uint32 ret;
143         OM_uint32 min;
144         gss_name_t srv_name;
145         gss_buffer_desc input_name;
146         char *host_princ_s = NULL;
147         NTSTATUS status = NT_STATUS_OK;
148
149         gss_OID_desc nt_hostbased_service =
150         {10, discard_const_p(char, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
151
152         if (asprintf(&host_princ_s, "%s@%s", service, name) == -1) {
153                 return NT_STATUS_NO_MEMORY;
154         }
155
156         input_name.value = host_princ_s;
157         input_name.length = strlen(host_princ_s) + 1;
158
159         ret = gss_import_name(&min,
160                                 &input_name,
161                                 &nt_hostbased_service,
162                                 &srv_name);
163
164         DEBUG(10,("get_srv_gss_creds: imported name %s\n",
165                 host_princ_s ));
166
167         if (ret != GSS_S_COMPLETE) {
168                 SAFE_FREE(host_princ_s);
169                 return map_nt_error_from_gss(ret, min);
170         }
171
172         /*
173          * We're accessing the krb5.keytab file here.
174          * ensure we have permissions to do so.
175          */
176         become_root();
177
178         ret = gss_acquire_cred(&min,
179                                 srv_name,
180                                 GSS_C_INDEFINITE,
181                                 GSS_C_NULL_OID_SET,
182                                 cred_type,
183                                 p_srv_cred,
184                                 NULL,
185                                 NULL);
186         unbecome_root();
187
188         if (ret != GSS_S_COMPLETE) {
189                 ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
190                 DEBUG(10,("get_srv_gss_creds: gss_acquire_cred failed with %s\n",
191                         ads_errstr(adss)));
192                 status = map_nt_error_from_gss(ret, min);
193         }
194
195         SAFE_FREE(host_princ_s);
196         gss_release_name(&min, &srv_name);
197         return status;
198 }
199
200 /******************************************************************************
201  Create a gss state.
202  Try and get the cifs/server@realm principal first, then fall back to
203  host/server@realm.
204 ******************************************************************************/
205
206 static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
207 {
208         NTSTATUS status;
209         gss_cred_id_t srv_cred;
210         fstring fqdn;
211
212         name_to_fqdn(fqdn, lp_netbios_name());
213         strlower_m(fqdn);
214
215         status = get_srv_gss_creds("cifs", fqdn, GSS_C_ACCEPT, &srv_cred);
216         if (!NT_STATUS_IS_OK(status)) {
217                 status = get_srv_gss_creds("host", fqdn, GSS_C_ACCEPT, &srv_cred);
218                 if (!NT_STATUS_IS_OK(status)) {
219                         return nt_status_squash(status);
220                 }
221         }
222
223         ec->es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
224         if (!ec->es->s.gss_state) {
225                 OM_uint32 min;
226                 gss_release_cred(&min, &srv_cred);
227                 return NT_STATUS_NO_MEMORY;
228         }
229         ZERO_STRUCTP(ec->es->s.gss_state);
230         ec->es->s.gss_state->creds = srv_cred;
231
232         /* No context yet. */
233         ec->es->s.gss_state->gss_ctx = GSS_C_NO_CONTEXT;
234
235         return NT_STATUS_OK;
236 }
237 #endif
238
239 /******************************************************************************
240  Shutdown a server encryption context.
241 ******************************************************************************/
242
243 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
244 {
245         struct smb_srv_trans_enc_ctx *ec = *pp_ec;
246
247         if (!ec) {
248                 return;
249         }
250
251         if (ec->es) {
252                 switch (ec->es->smb_enc_type) {
253                         case SMB_TRANS_ENC_NTLM:
254                                 destroy_auth_ntlmssp(ec);
255                                 break;
256 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
257                         case SMB_TRANS_ENC_GSS:
258                                 break;
259 #endif
260                 }
261                 common_free_encryption_state(&ec->es);
262         }
263
264         SAFE_FREE(ec);
265         *pp_ec = NULL;
266 }
267
268 /******************************************************************************
269  Create a server encryption context.
270 ******************************************************************************/
271
272 static NTSTATUS make_srv_encryption_context(const struct tsocket_address *remote_address,
273                                             enum smb_trans_enc_type smb_enc_type,
274                                             struct smb_srv_trans_enc_ctx **pp_ec)
275 {
276         struct smb_srv_trans_enc_ctx *ec;
277
278         *pp_ec = NULL;
279
280         ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
281         if (!ec) {
282                 return NT_STATUS_NO_MEMORY;
283         }
284         ZERO_STRUCTP(partial_srv_trans_enc_ctx);
285         ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
286         if (!ec->es) {
287                 SAFE_FREE(ec);
288                 return NT_STATUS_NO_MEMORY;
289         }
290         ZERO_STRUCTP(ec->es);
291         ec->es->smb_enc_type = smb_enc_type;
292         switch (smb_enc_type) {
293                 case SMB_TRANS_ENC_NTLM:
294                         {
295                                 NTSTATUS status = make_auth_ntlmssp(remote_address,
296                                                                     ec);
297                                 if (!NT_STATUS_IS_OK(status)) {
298                                         srv_free_encryption_context(&ec);
299                                         return status;
300                                 }
301                         }
302                         break;
303
304 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
305                 case SMB_TRANS_ENC_GSS:
306                         /* Acquire our credentials by calling gss_acquire_cred here. */
307                         {
308                                 NTSTATUS status = make_auth_gss(ec);
309                                 if (!NT_STATUS_IS_OK(status)) {
310                                         srv_free_encryption_context(&ec);
311                                         return status;
312                                 }
313                         }
314                         break;
315 #endif
316                 default:
317                         srv_free_encryption_context(&ec);
318                         return NT_STATUS_INVALID_PARAMETER;
319         }
320         *pp_ec = ec;
321         return NT_STATUS_OK;
322 }
323
324 /******************************************************************************
325  Free an encryption-allocated buffer.
326 ******************************************************************************/
327
328 void srv_free_enc_buffer(struct smbd_server_connection *sconn, char *buf)
329 {
330         /* We know this is an smb buffer, and we
331          * didn't malloc, only copy, for a keepalive,
332          * so ignore non-session messages. */
333
334         if(CVAL(buf,0)) {
335                 return;
336         }
337
338         if (srv_trans_enc_ctx) {
339                 common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
340         }
341 }
342
343 /******************************************************************************
344  Decrypt an incoming buffer.
345 ******************************************************************************/
346
347 NTSTATUS srv_decrypt_buffer(struct smbd_server_connection *sconn, char *buf)
348 {
349         /* Ignore non-session messages. */
350         if(CVAL(buf,0)) {
351                 return NT_STATUS_OK;
352         }
353
354         if (srv_trans_enc_ctx) {
355                 return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
356         }
357
358         return NT_STATUS_OK;
359 }
360
361 /******************************************************************************
362  Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
363 ******************************************************************************/
364
365 NTSTATUS srv_encrypt_buffer(struct smbd_server_connection *sconn, char *buf,
366                             char **buf_out)
367 {
368         *buf_out = buf;
369
370         /* Ignore non-session messages. */
371         if(CVAL(buf,0)) {
372                 return NT_STATUS_OK;
373         }
374
375         if (srv_trans_enc_ctx) {
376                 return common_encrypt_buffer(srv_trans_enc_ctx->es, buf, buf_out);
377         }
378         /* Not encrypting. */
379         return NT_STATUS_OK;
380 }
381
382 /******************************************************************************
383  Do the gss encryption negotiation. Parameters are in/out.
384  Until success we do everything on the partial enc ctx.
385 ******************************************************************************/
386
387 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
388 static NTSTATUS srv_enc_spnego_gss_negotiate(const struct tsocket_address *remote_address,
389                                              unsigned char **ppdata,
390                                              size_t *p_data_size,
391                                              DATA_BLOB secblob)
392 {
393         OM_uint32 ret;
394         OM_uint32 min;
395         OM_uint32 flags = 0;
396         gss_buffer_desc in_buf, out_buf;
397         struct smb_tran_enc_state_gss *gss_state;
398         DATA_BLOB auth_reply = data_blob_null;
399         DATA_BLOB response = data_blob_null;
400         NTSTATUS status;
401
402         if (!partial_srv_trans_enc_ctx) {
403                 status = make_srv_encryption_context(remote_address,
404                                                      SMB_TRANS_ENC_GSS,
405                                                      &partial_srv_trans_enc_ctx);
406                 if (!NT_STATUS_IS_OK(status)) {
407                         return status;
408                 }
409         }
410
411         gss_state = partial_srv_trans_enc_ctx->es->s.gss_state;
412
413         in_buf.value = secblob.data;
414         in_buf.length = secblob.length;
415
416         out_buf.value = NULL;
417         out_buf.length = 0;
418
419         become_root();
420
421         ret = gss_accept_sec_context(&min,
422                                 &gss_state->gss_ctx,
423                                 gss_state->creds,
424                                 &in_buf,
425                                 GSS_C_NO_CHANNEL_BINDINGS,
426                                 NULL,
427                                 NULL,           /* Ignore oids. */
428                                 &out_buf,       /* To return. */
429                                 &flags,
430                                 NULL,           /* Ingore time. */
431                                 NULL);          /* Ignore delegated creds. */
432         unbecome_root();
433
434         status = gss_err_to_ntstatus(ret, min);
435         if (ret != GSS_S_COMPLETE && ret != GSS_S_CONTINUE_NEEDED) {
436                 return status;
437         }
438
439         /* Ensure we've got sign+seal available. */
440         if (ret == GSS_S_COMPLETE) {
441                 if ((flags & (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) !=
442                                 (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) {
443                         DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
444                                 "for SMB sealing.\n"));
445                         gss_release_buffer(&min, &out_buf);
446                         return NT_STATUS_ACCESS_DENIED;
447                 }
448         }
449
450         auth_reply = data_blob(out_buf.value, out_buf.length);
451         gss_release_buffer(&min, &out_buf);
452
453         /* Wrap in SPNEGO. */
454         response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, OID_KERBEROS5);
455         data_blob_free(&auth_reply);
456
457         SAFE_FREE(*ppdata);
458         *ppdata = (unsigned char *)memdup(response.data, response.length);
459         if ((*ppdata) == NULL && response.length > 0) {
460                 status = NT_STATUS_NO_MEMORY;
461         }
462         *p_data_size = response.length;
463
464         data_blob_free(&response);
465
466         return status;
467 }
468 #endif
469
470 /******************************************************************************
471  Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
472  Until success we do everything on the partial enc ctx.
473 ******************************************************************************/
474
475 static NTSTATUS srv_enc_ntlm_negotiate(const struct tsocket_address *remote_address,
476                                        unsigned char **ppdata,
477                                        size_t *p_data_size,
478                                        DATA_BLOB secblob,
479                                        bool spnego_wrap)
480 {
481         NTSTATUS status;
482         DATA_BLOB chal = data_blob_null;
483         DATA_BLOB response = data_blob_null;
484
485         status = make_srv_encryption_context(remote_address,
486                                              SMB_TRANS_ENC_NTLM,
487                                              &partial_srv_trans_enc_ctx);
488         if (!NT_STATUS_IS_OK(status)) {
489                 return status;
490         }
491
492         status = gensec_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state->gensec_security,
493                                talloc_tos(), NULL,
494                                secblob, &chal);
495
496         /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
497          * for success ... */
498
499         if (spnego_wrap) {
500                 response = spnego_gen_auth_response(talloc_tos(), &chal, status, OID_NTLMSSP);
501                 data_blob_free(&chal);
502         } else {
503                 /* Return the raw blob. */
504                 response = chal;
505         }
506
507         SAFE_FREE(*ppdata);
508         *ppdata = (unsigned char *)memdup(response.data, response.length);
509         if ((*ppdata) == NULL && response.length > 0) {
510                 status = NT_STATUS_NO_MEMORY;
511         }
512         *p_data_size = response.length;
513         data_blob_free(&response);
514
515         return status;
516 }
517
518 /******************************************************************************
519  Do the SPNEGO encryption negotiation. Parameters are in/out.
520  Based off code in smbd/sesssionsetup.c
521  Until success we do everything on the partial enc ctx.
522 ******************************************************************************/
523
524 static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
525                                         unsigned char **ppdata,
526                                         size_t *p_data_size,
527                                         unsigned char **pparam,
528                                         size_t *p_param_size)
529 {
530         NTSTATUS status;
531         DATA_BLOB blob = data_blob_null;
532         DATA_BLOB secblob = data_blob_null;
533         char *kerb_mech = NULL;
534
535         blob = data_blob_const(*ppdata, *p_data_size);
536
537         status = parse_spnego_mechanisms(talloc_tos(), blob, &secblob, &kerb_mech);
538         if (!NT_STATUS_IS_OK(status)) {
539                 return nt_status_squash(status);
540         }
541
542         /* We should have no partial context at this point. */
543
544         srv_free_encryption_context(&partial_srv_trans_enc_ctx);
545
546         if (kerb_mech) {
547                 TALLOC_FREE(kerb_mech);
548
549 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
550                 status = srv_enc_spnego_gss_negotiate(conn->sconn->remote_address,
551                                                       ppdata,
552                                                       p_data_size,
553                                                       secblob);
554 #else
555                 /* Currently we don't SPNEGO negotiate
556                  * back to NTLMSSP as we do in sessionsetupX. We should... */
557                 return NT_STATUS_LOGON_FAILURE;
558 #endif
559         } else {
560                 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
561                                                 ppdata,
562                                                 p_data_size,
563                                                 secblob,
564                                                 true);
565         }
566
567         data_blob_free(&secblob);
568
569         if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
570                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
571                 return nt_status_squash(status);
572         }
573
574         if (NT_STATUS_IS_OK(status)) {
575                 /* Return the context we're using for this encryption state. */
576                 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
577                         return NT_STATUS_NO_MEMORY;
578                 }
579                 SSVAL(*pparam,0,partial_srv_trans_enc_ctx->es->enc_ctx_num);
580                 *p_param_size = 2;
581         }
582
583         return status;
584 }
585
586 /******************************************************************************
587  Complete a SPNEGO encryption negotiation. Parameters are in/out.
588  We only get this for a NTLM auth second stage.
589 ******************************************************************************/
590
591 static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
592                                         unsigned char **ppdata,
593                                         size_t *p_data_size,
594                                         unsigned char **pparam,
595                                         size_t *p_param_size)
596 {
597         NTSTATUS status;
598         DATA_BLOB blob = data_blob_null;
599         DATA_BLOB auth = data_blob_null;
600         DATA_BLOB auth_reply = data_blob_null;
601         DATA_BLOB response = data_blob_null;
602         struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
603
604         /* We must have a partial context here. */
605
606         if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
607                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
608                 return NT_STATUS_INVALID_PARAMETER;
609         }
610
611         blob = data_blob_const(*ppdata, *p_data_size);
612         if (!spnego_parse_auth(talloc_tos(), blob, &auth)) {
613                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
614                 return NT_STATUS_INVALID_PARAMETER;
615         }
616
617         status = gensec_update(ec->auth_ntlmssp_state->gensec_security, talloc_tos(), NULL, auth, &auth_reply);
618         data_blob_free(&auth);
619
620         /* From RFC4178.
621          *
622          *    supportedMech
623          *
624          *          This field SHALL only be present in the first reply from the
625          *                target.
626          * So set mechOID to NULL here.
627          */
628
629         response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, NULL);
630         data_blob_free(&auth_reply);
631
632         if (NT_STATUS_IS_OK(status)) {
633                 /* Return the context we're using for this encryption state. */
634                 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
635                         return NT_STATUS_NO_MEMORY;
636                 }
637                 SSVAL(*pparam,0,ec->es->enc_ctx_num);
638                 *p_param_size = 2;
639         }
640
641         SAFE_FREE(*ppdata);
642         *ppdata = (unsigned char *)memdup(response.data, response.length);
643         if ((*ppdata) == NULL && response.length > 0)
644                 return NT_STATUS_NO_MEMORY;
645         *p_data_size = response.length;
646         data_blob_free(&response);
647         return status;
648 }
649
650 /******************************************************************************
651  Raw NTLM encryption negotiation. Parameters are in/out.
652  This function does both steps.
653 ******************************************************************************/
654
655 static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
656                                         unsigned char **ppdata,
657                                         size_t *p_data_size,
658                                         unsigned char **pparam,
659                                         size_t *p_param_size)
660 {
661         NTSTATUS status;
662         DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
663         DATA_BLOB response = data_blob_null;
664         struct smb_srv_trans_enc_ctx *ec;
665
666         if (!partial_srv_trans_enc_ctx) {
667                 /* This is the initial step. */
668                 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
669                                                 ppdata,
670                                                 p_data_size,
671                                                 blob,
672                                                 false);
673                 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
674                         srv_free_encryption_context(&partial_srv_trans_enc_ctx);
675                         return nt_status_squash(status);
676                 }
677                 return status;
678         }
679
680         ec = partial_srv_trans_enc_ctx;
681         if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
682                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
683                 return NT_STATUS_INVALID_PARAMETER;
684         }
685
686         /* Second step. */
687         status = gensec_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state->gensec_security,
688                                talloc_tos(), NULL,
689                                blob, &response);
690
691         if (NT_STATUS_IS_OK(status)) {
692                 /* Return the context we're using for this encryption state. */
693                 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
694                         return NT_STATUS_NO_MEMORY;
695                 }
696                 SSVAL(*pparam,0,ec->es->enc_ctx_num);
697                 *p_param_size = 2;
698         }
699
700         /* Return the raw blob. */
701         SAFE_FREE(*ppdata);
702         *ppdata = (unsigned char *)memdup(response.data, response.length);
703         if ((*ppdata) == NULL && response.length > 0)
704                 return NT_STATUS_NO_MEMORY;
705         *p_data_size = response.length;
706         data_blob_free(&response);
707         return status;
708 }
709
710 /******************************************************************************
711  Do the SPNEGO encryption negotiation. Parameters are in/out.
712 ******************************************************************************/
713
714 NTSTATUS srv_request_encryption_setup(connection_struct *conn,
715                                         unsigned char **ppdata,
716                                         size_t *p_data_size,
717                                         unsigned char **pparam,
718                                         size_t *p_param_size)
719 {
720         unsigned char *pdata = *ppdata;
721
722         SAFE_FREE(*pparam);
723         *p_param_size = 0;
724
725         if (*p_data_size < 1) {
726                 return NT_STATUS_INVALID_PARAMETER;
727         }
728
729         if (pdata[0] == ASN1_APPLICATION(0)) {
730                 /* its a negTokenTarg packet */
731                 return srv_enc_spnego_negotiate(conn, ppdata, p_data_size, pparam, p_param_size);
732         }
733
734         if (pdata[0] == ASN1_CONTEXT(1)) {
735                 /* It's an auth packet */
736                 return srv_enc_spnego_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
737         }
738
739         /* Maybe it's a raw unwrapped auth ? */
740         if (*p_data_size < 7) {
741                 return NT_STATUS_INVALID_PARAMETER;
742         }
743
744         if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
745                 return srv_enc_raw_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
746         }
747
748         DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
749
750         return NT_STATUS_LOGON_FAILURE;
751 }
752
753 /******************************************************************************
754  Negotiation was successful - turn on server-side encryption.
755 ******************************************************************************/
756
757 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
758 {
759         if (!ec || !ec->es) {
760                 return NT_STATUS_LOGON_FAILURE;
761         }
762
763         if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
764                 if (!gensec_have_feature(ec->auth_ntlmssp_state->gensec_security, GENSEC_FEATURE_SIGN)) {
765                         return NT_STATUS_INVALID_PARAMETER;
766                 }
767
768                 if (!gensec_have_feature(ec->auth_ntlmssp_state->gensec_security, GENSEC_FEATURE_SEAL)) {
769                         return NT_STATUS_INVALID_PARAMETER;
770                 }
771         }
772         /* Todo - check gssapi case. */
773
774         return NT_STATUS_OK;
775 }
776
777 /******************************************************************************
778  Negotiation was successful - turn on server-side encryption.
779 ******************************************************************************/
780
781 NTSTATUS srv_encryption_start(connection_struct *conn)
782 {
783         NTSTATUS status;
784
785         /* Check that we are really doing sign+seal. */
786         status = check_enc_good(partial_srv_trans_enc_ctx);
787         if (!NT_STATUS_IS_OK(status)) {
788                 return status;
789         }
790         /* Throw away the context we're using currently (if any). */
791         srv_free_encryption_context(&srv_trans_enc_ctx);
792
793         /* Steal the partial pointer. Deliberate shallow copy. */
794         srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
795         srv_trans_enc_ctx->es->enc_on = true;
796
797         partial_srv_trans_enc_ctx = NULL;
798
799         DEBUG(1,("srv_encryption_start: context negotiated\n"));
800         return NT_STATUS_OK;
801 }
802
803 /******************************************************************************
804  Shutdown all server contexts.
805 ******************************************************************************/
806
807 void server_encryption_shutdown(struct smbd_server_connection *sconn)
808 {
809         srv_free_encryption_context(&partial_srv_trans_enc_ctx);
810         srv_free_encryption_context(&srv_trans_enc_ctx);
811 }