r21894: Some refactoring of server side encryption context. Support
[abartlet/samba.git/.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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 /******************************************************************************
24  Server side encryption.
25 ******************************************************************************/
26
27 /******************************************************************************
28  Global server state.
29 ******************************************************************************/
30
31 struct smb_srv_trans_enc_ctx {
32         struct smb_trans_enc_state *es;
33         AUTH_NTLMSSP_STATE *auth_ntlmssp_state; /* Must be kept in sync with pointer in ec->ntlmssp_state. */
34 };
35
36 static struct smb_srv_trans_enc_ctx *partial_srv_trans_enc_ctx;
37 static struct smb_srv_trans_enc_ctx *srv_trans_enc_ctx;
38
39 /******************************************************************************
40  Is server encryption on ?
41 ******************************************************************************/
42
43 BOOL srv_encryption_on(void)
44 {
45         if (srv_trans_enc_ctx) {
46                 return common_encryption_on(srv_trans_enc_ctx->es);
47         }
48         return False;
49 }
50
51 /******************************************************************************
52  Create an auth_ntlmssp_state and ensure pointer copy is correct.
53 ******************************************************************************/
54
55 static NTSTATUS make_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
56 {
57         NTSTATUS status = auth_ntlmssp_start(&ec->auth_ntlmssp_state);
58         if (!NT_STATUS_IS_OK(status)) {
59                 return nt_status_squash(status);
60         }
61
62         /*
63          * We must remember to update the pointer copy for the common
64          * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
65          */
66         ec->es->ntlmssp_state = ec->auth_ntlmssp_state->ntlmssp_state;
67         return status;
68 }
69
70 /******************************************************************************
71  Destroy an auth_ntlmssp_state and ensure pointer copy is correct.
72 ******************************************************************************/
73
74 static void destroy_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
75 {
76         /*
77          * We must remember to update the pointer copy for the common
78          * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
79          */
80
81         if (ec->auth_ntlmssp_state) {
82                 auth_ntlmssp_end(&ec->auth_ntlmssp_state);
83                 /* The auth_ntlmssp_end killed this already. */
84                 ec->es->ntlmssp_state = NULL;
85         }
86 }
87
88 /******************************************************************************
89  Shutdown a server encryption context.
90 ******************************************************************************/
91
92 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
93 {
94         struct smb_srv_trans_enc_ctx *ec = *pp_ec;
95
96         if (!ec) {
97                 return;
98         }
99
100         if (ec->es) {
101                 if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
102                         destroy_auth_ntlmssp(ec);
103                 }
104                 common_free_encryption_state(&ec->es);
105         }
106
107         SAFE_FREE(ec);
108         *pp_ec = NULL;
109 }
110
111 /******************************************************************************
112  Create a server encryption context.
113 ******************************************************************************/
114
115 static struct smb_srv_trans_enc_ctx *make_srv_encryption_context(enum smb_trans_enc_type smb_enc_type)
116 {
117         struct smb_srv_trans_enc_ctx *ec;
118
119         ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
120         if (!ec) {
121                 return NULL;
122         }
123         ZERO_STRUCTP(partial_srv_trans_enc_ctx);
124         ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
125         if (!ec->es) {
126                 SAFE_FREE(ec);
127                 return NULL;
128         }
129         ZERO_STRUCTP(ec->es);
130         ec->es->smb_enc_type = smb_enc_type;
131         if (smb_enc_type == SMB_TRANS_ENC_NTLM) {
132                 NTSTATUS status = make_auth_ntlmssp(ec);
133                 if (!NT_STATUS_IS_OK(status)) {
134                         srv_free_encryption_context(&ec);
135                         return NULL;
136                 }
137         }
138         return ec;
139 }
140
141 /******************************************************************************
142  Free an encryption-allocated buffer.
143 ******************************************************************************/
144
145 void srv_free_enc_buffer(char *buf)
146 {
147         if (srv_trans_enc_ctx) {
148                 return common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
149         }
150 }
151
152 /******************************************************************************
153  Decrypt an incoming buffer.
154 ******************************************************************************/
155
156 NTSTATUS srv_decrypt_buffer(char *buf)
157 {
158         if (srv_trans_enc_ctx) {
159                 return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
160         }
161         return NT_STATUS_OK;
162 }
163
164 /******************************************************************************
165  Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
166 ******************************************************************************/
167
168 NTSTATUS srv_encrypt_buffer(char *buffer, char **buf_out)
169 {
170         if (srv_trans_enc_ctx) {
171                 return common_encrypt_buffer(srv_trans_enc_ctx->es, buffer, buf_out);
172         }
173         /* Not encrypting. */
174         *buf_out = buffer;
175         return NT_STATUS_OK;
176 }
177
178 /******************************************************************************
179  Do the gss encryption negotiation. Parameters are in/out.
180  Until success we do everything on the partial enc ctx.
181 ******************************************************************************/
182
183 #if defined(HAVE_GSSAPI_SUPPORT) && defined(HAVE_KRB5)
184 static NTSTATUS srv_enc_spnego_gss_negotiate(char **ppdata, size_t *p_data_size, DATA_BLOB secblob)
185 {
186         return NT_STATUS_NOT_SUPPORTED;
187 }
188 #endif
189
190 /******************************************************************************
191  Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
192  Until success we do everything on the partial enc ctx.
193 ******************************************************************************/
194
195 static NTSTATUS srv_enc_ntlm_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob, BOOL spnego_wrap)
196 {
197         NTSTATUS status;
198         DATA_BLOB chal = data_blob(NULL, 0);
199         DATA_BLOB response = data_blob(NULL, 0);
200
201         partial_srv_trans_enc_ctx = make_srv_encryption_context(SMB_TRANS_ENC_NTLM);
202         if (!partial_srv_trans_enc_ctx) {
203                 return NT_STATUS_NO_MEMORY;
204         }
205
206         status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, secblob, &chal);
207
208         /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
209          * for success ... */
210
211         if (spnego_wrap) {
212                 response = spnego_gen_auth_response(&chal, status, OID_NTLMSSP);
213                 data_blob_free(&chal);
214         } else {
215                 /* Return the raw blob. */
216                 response = chal;
217         }
218
219         SAFE_FREE(*ppdata);
220         *ppdata = response.data;
221         *p_data_size = response.length;
222         return status;
223 }
224
225 /******************************************************************************
226  Do the SPNEGO encryption negotiation. Parameters are in/out.
227  Based off code in smbd/sesssionsetup.c
228  Until success we do everything on the partial enc ctx.
229 ******************************************************************************/
230
231 static NTSTATUS srv_enc_spnego_negotiate(unsigned char **ppdata, size_t *p_data_size)
232 {
233         NTSTATUS status;
234         DATA_BLOB blob = data_blob(NULL,0);
235         DATA_BLOB secblob = data_blob(NULL, 0);
236         BOOL got_kerberos_mechanism = False;
237
238         blob = data_blob_const(*ppdata, *p_data_size);
239
240         status = parse_spnego_mechanisms(blob, &secblob, &got_kerberos_mechanism);
241         if (!NT_STATUS_IS_OK(status)) {
242                 return nt_status_squash(status);
243         }
244
245         /* We should have no partial context at this point. */
246
247         srv_free_encryption_context(&partial_srv_trans_enc_ctx);
248
249 #if defined(HAVE_GSSAPI_SUPPORT) && defined(HAVE_KRB5)
250         if (got_kerberos_mechanism && lp_use_kerberos_keytab()) ) {
251                 status = srv_enc_spnego_gss_negotiate(ppdata, p_data_size, secblob);
252         } else 
253 #endif
254         {
255                 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, secblob, True);
256         }
257
258         data_blob_free(&secblob);
259
260         if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
261                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
262         }
263
264         return status;
265 }
266
267 /******************************************************************************
268  Complete a SPNEGO encryption negotiation. Parameters are in/out.
269  We only get this for a NTLM auth second stage.
270 ******************************************************************************/
271
272 static NTSTATUS srv_enc_spnego_ntlm_auth(unsigned char **ppdata, size_t *p_data_size)
273 {
274         NTSTATUS status;
275         DATA_BLOB blob = data_blob(NULL,0);
276         DATA_BLOB auth = data_blob(NULL,0);
277         DATA_BLOB auth_reply = data_blob(NULL,0);
278         DATA_BLOB response = data_blob(NULL,0);
279         struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
280
281         /* We must have a partial context here. */
282
283         if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
284                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
285                 return NT_STATUS_INVALID_PARAMETER;
286         }
287
288         blob = data_blob_const(*ppdata, *p_data_size);
289         if (!spnego_parse_auth(blob, &auth)) {
290                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
291                 return NT_STATUS_INVALID_PARAMETER;
292         }
293
294         status = auth_ntlmssp_update(ec->auth_ntlmssp_state, auth, &auth_reply);
295         data_blob_free(&auth);
296
297         response = spnego_gen_auth_response(&auth_reply, status, OID_NTLMSSP);
298         data_blob_free(&auth_reply);
299
300         SAFE_FREE(*ppdata);
301         *ppdata = response.data;
302         *p_data_size = response.length;
303         return status;
304 }
305
306 /******************************************************************************
307  Raw NTLM encryption negotiation. Parameters are in/out.
308  This function does both steps.
309 ******************************************************************************/
310
311 static NTSTATUS srv_enc_raw_ntlm_auth(unsigned char **ppdata, size_t *p_data_size)
312 {
313         NTSTATUS status;
314         DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
315         DATA_BLOB response = data_blob(NULL,0);
316         struct smb_srv_trans_enc_ctx *ec;
317
318         if (!partial_srv_trans_enc_ctx) {
319                 /* This is the initial step. */
320                 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, blob, False);
321                 if (!NT_STATUS_IS_OK(status)) {
322                         srv_free_encryption_context(&partial_srv_trans_enc_ctx);
323                         return nt_status_squash(status);
324                 }
325                 return status;
326         }
327
328         ec = partial_srv_trans_enc_ctx;
329         if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
330                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
331                 return NT_STATUS_INVALID_PARAMETER;
332         }
333
334         /* Second step. */
335         status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, blob, &response);
336
337         /* Return the raw blob. */
338         SAFE_FREE(*ppdata);
339         *ppdata = response.data;
340         *p_data_size = response.length;
341         return status;
342 }
343
344 /******************************************************************************
345  Do the SPNEGO encryption negotiation. Parameters are in/out.
346 ******************************************************************************/
347
348 NTSTATUS srv_request_encryption_setup(unsigned char **ppdata, size_t *p_data_size)
349 {
350         unsigned char *pdata = *ppdata;
351
352         if (*p_data_size < 1) {
353                 return NT_STATUS_INVALID_PARAMETER;
354         }
355
356         if (pdata[0] == ASN1_APPLICATION(0)) {
357                 /* 
358                  * Until success we do everything on the partial
359                  * enc state.
360                  */
361                 /* its a negTokenTarg packet */
362                 return srv_enc_spnego_negotiate(ppdata, p_data_size);
363         }
364
365         if (pdata[0] == ASN1_CONTEXT(1)) {
366                 /* It's an auth packet */
367                 return srv_enc_spnego_ntlm_auth(ppdata, p_data_size);
368         }
369
370         /* Maybe it's a raw unwrapped auth ? */
371         if (*p_data_size < 7) {
372                 return NT_STATUS_INVALID_PARAMETER;
373         }
374
375         if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
376                 return srv_enc_raw_ntlm_auth(ppdata, p_data_size);
377         }
378
379         DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
380
381         return NT_STATUS_LOGON_FAILURE;
382 }
383
384 /******************************************************************************
385  Negotiation was successful - turn on server-side encryption.
386 ******************************************************************************/
387
388 void srv_encryption_start(void)
389 {
390         /* Throw away the context we're using currently (if any). */
391         srv_free_encryption_context(&srv_trans_enc_ctx);
392
393         /* Steal the partial pointer. Deliberate shallow copy. */
394         srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
395         srv_trans_enc_ctx->es->enc_on = True;
396
397         partial_srv_trans_enc_ctx = NULL;
398 }
399
400 /******************************************************************************
401  Shutdown all server contexts.
402 ******************************************************************************/
403
404 void server_encryption_shutdown(void)
405 {
406         srv_free_encryption_context(&partial_srv_trans_enc_ctx);
407         srv_free_encryption_context(&srv_trans_enc_ctx);
408 }