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