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