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