2 Unix SMB/CIFS implementation.
3 SMB Transport encryption (sealing) code - server code.
4 Copyright (C) Jeremy Allison 2007.
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.
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.
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.
23 /******************************************************************************
24 Server side encryption.
25 ******************************************************************************/
27 /******************************************************************************
29 ******************************************************************************/
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. */
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;
39 /******************************************************************************
40 Is server encryption on ?
41 ******************************************************************************/
43 BOOL srv_encryption_on(void)
45 if (srv_trans_enc_ctx) {
46 return common_encryption_on(srv_trans_enc_ctx->es);
51 /******************************************************************************
52 Create an auth_ntlmssp_state and ensure pointer copy is correct.
53 ******************************************************************************/
55 static NTSTATUS make_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
57 NTSTATUS status = auth_ntlmssp_start(&ec->auth_ntlmssp_state);
58 if (!NT_STATUS_IS_OK(status)) {
59 return nt_status_squash(status);
63 * We must remember to update the pointer copy for the common
64 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
66 ec->es->ntlmssp_state = ec->auth_ntlmssp_state->ntlmssp_state;
70 /******************************************************************************
71 Destroy an auth_ntlmssp_state and ensure pointer copy is correct.
72 ******************************************************************************/
74 static void destroy_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
77 * We must remember to update the pointer copy for the common
78 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
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;
88 /******************************************************************************
89 Shutdown a server encryption context.
90 ******************************************************************************/
92 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
94 struct smb_srv_trans_enc_ctx *ec = *pp_ec;
101 switch (ec->es->smb_enc_type) {
102 case SMB_TRANS_ENC_NTLM:
103 destroy_auth_ntlmssp(ec);
105 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
106 case SMB_TRANS_ENC_GSS:
110 common_free_encryption_state(&ec->es);
117 /******************************************************************************
118 Create a server encryption context.
119 ******************************************************************************/
121 static struct smb_srv_trans_enc_ctx *make_srv_encryption_context(enum smb_trans_enc_type smb_enc_type)
123 struct smb_srv_trans_enc_ctx *ec;
125 ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
129 ZERO_STRUCTP(partial_srv_trans_enc_ctx);
130 ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
135 ZERO_STRUCTP(ec->es);
136 ec->es->smb_enc_type = smb_enc_type;
137 switch (smb_enc_type) {
138 case SMB_TRANS_ENC_NTLM:
140 NTSTATUS status = make_auth_ntlmssp(ec);
141 if (!NT_STATUS_IS_OK(status)) {
142 srv_free_encryption_context(&ec);
148 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
149 case SMB_TRANS_ENC_GSS:
150 /* Acquire our credentials by calling gss_acquire_cred here. */
154 srv_free_encryption_context(&ec);
160 /******************************************************************************
161 Free an encryption-allocated buffer.
162 ******************************************************************************/
164 void srv_free_enc_buffer(char *buf)
166 if (srv_trans_enc_ctx) {
167 common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
171 /******************************************************************************
172 Decrypt an incoming buffer.
173 ******************************************************************************/
175 NTSTATUS srv_decrypt_buffer(char *buf)
177 if (srv_trans_enc_ctx) {
178 return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
183 /******************************************************************************
184 Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
185 ******************************************************************************/
187 NTSTATUS srv_encrypt_buffer(char *buffer, char **buf_out)
189 if (srv_trans_enc_ctx) {
190 return common_encrypt_buffer(srv_trans_enc_ctx->es, buffer, buf_out);
192 /* Not encrypting. */
197 /******************************************************************************
198 Do the gss encryption negotiation. Parameters are in/out.
199 Until success we do everything on the partial enc ctx.
200 ******************************************************************************/
202 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
203 static NTSTATUS srv_enc_spnego_gss_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob)
205 if (!partial_srv_trans_enc_ctx) {
206 partial_srv_trans_enc_ctx = make_srv_encryption_context(SMB_TRANS_ENC_GSS);
207 if (!partial_srv_trans_enc_ctx) {
208 return NT_STATUS_NO_MEMORY;
212 return NT_STATUS_NOT_SUPPORTED;
216 /******************************************************************************
217 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
218 Until success we do everything on the partial enc ctx.
219 ******************************************************************************/
221 static NTSTATUS srv_enc_ntlm_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob, BOOL spnego_wrap)
224 DATA_BLOB chal = data_blob(NULL, 0);
225 DATA_BLOB response = data_blob(NULL, 0);
227 partial_srv_trans_enc_ctx = make_srv_encryption_context(SMB_TRANS_ENC_NTLM);
228 if (!partial_srv_trans_enc_ctx) {
229 return NT_STATUS_NO_MEMORY;
232 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, secblob, &chal);
234 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
238 response = spnego_gen_auth_response(&chal, status, OID_NTLMSSP);
239 data_blob_free(&chal);
241 /* Return the raw blob. */
246 *ppdata = response.data;
247 *p_data_size = response.length;
251 /******************************************************************************
252 Do the SPNEGO encryption negotiation. Parameters are in/out.
253 Based off code in smbd/sesssionsetup.c
254 Until success we do everything on the partial enc ctx.
255 ******************************************************************************/
257 static NTSTATUS srv_enc_spnego_negotiate(unsigned char **ppdata, size_t *p_data_size)
260 DATA_BLOB blob = data_blob(NULL,0);
261 DATA_BLOB secblob = data_blob(NULL, 0);
262 BOOL got_kerberos_mechanism = False;
264 blob = data_blob_const(*ppdata, *p_data_size);
266 status = parse_spnego_mechanisms(blob, &secblob, &got_kerberos_mechanism);
267 if (!NT_STATUS_IS_OK(status)) {
268 return nt_status_squash(status);
271 /* We should have no partial context at this point. */
273 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
275 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
276 if (got_kerberos_mechanism && lp_use_kerberos_keytab() ) {
277 status = srv_enc_spnego_gss_negotiate(ppdata, p_data_size, secblob);
281 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, secblob, True);
284 data_blob_free(&secblob);
286 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
287 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
293 /******************************************************************************
294 Complete a SPNEGO encryption negotiation. Parameters are in/out.
295 We only get this for a NTLM auth second stage.
296 ******************************************************************************/
298 static NTSTATUS srv_enc_spnego_ntlm_auth(unsigned char **ppdata, size_t *p_data_size)
301 DATA_BLOB blob = data_blob(NULL,0);
302 DATA_BLOB auth = data_blob(NULL,0);
303 DATA_BLOB auth_reply = data_blob(NULL,0);
304 DATA_BLOB response = data_blob(NULL,0);
305 struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
307 /* We must have a partial context here. */
309 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
310 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
311 return NT_STATUS_INVALID_PARAMETER;
314 blob = data_blob_const(*ppdata, *p_data_size);
315 if (!spnego_parse_auth(blob, &auth)) {
316 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
317 return NT_STATUS_INVALID_PARAMETER;
320 status = auth_ntlmssp_update(ec->auth_ntlmssp_state, auth, &auth_reply);
321 data_blob_free(&auth);
323 response = spnego_gen_auth_response(&auth_reply, status, OID_NTLMSSP);
324 data_blob_free(&auth_reply);
327 *ppdata = response.data;
328 *p_data_size = response.length;
332 /******************************************************************************
333 Raw NTLM encryption negotiation. Parameters are in/out.
334 This function does both steps.
335 ******************************************************************************/
337 static NTSTATUS srv_enc_raw_ntlm_auth(unsigned char **ppdata, size_t *p_data_size)
340 DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
341 DATA_BLOB response = data_blob(NULL,0);
342 struct smb_srv_trans_enc_ctx *ec;
344 if (!partial_srv_trans_enc_ctx) {
345 /* This is the initial step. */
346 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, blob, False);
347 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
348 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
349 return nt_status_squash(status);
354 ec = partial_srv_trans_enc_ctx;
355 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
356 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
357 return NT_STATUS_INVALID_PARAMETER;
361 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, blob, &response);
363 /* Return the raw blob. */
365 *ppdata = response.data;
366 *p_data_size = response.length;
370 /******************************************************************************
371 Do the SPNEGO encryption negotiation. Parameters are in/out.
372 ******************************************************************************/
374 NTSTATUS srv_request_encryption_setup(unsigned char **ppdata, size_t *p_data_size)
376 unsigned char *pdata = *ppdata;
378 if (*p_data_size < 1) {
379 return NT_STATUS_INVALID_PARAMETER;
382 if (pdata[0] == ASN1_APPLICATION(0)) {
384 * Until success we do everything on the partial
387 /* its a negTokenTarg packet */
388 return srv_enc_spnego_negotiate(ppdata, p_data_size);
391 if (pdata[0] == ASN1_CONTEXT(1)) {
392 /* It's an auth packet */
393 return srv_enc_spnego_ntlm_auth(ppdata, p_data_size);
396 /* Maybe it's a raw unwrapped auth ? */
397 if (*p_data_size < 7) {
398 return NT_STATUS_INVALID_PARAMETER;
401 if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
402 return srv_enc_raw_ntlm_auth(ppdata, p_data_size);
405 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
407 return NT_STATUS_LOGON_FAILURE;
410 /******************************************************************************
411 Negotiation was successful - turn on server-side encryption.
412 ******************************************************************************/
414 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
416 if (!ec || !ec->es) {
417 return NT_STATUS_LOGON_FAILURE;
420 if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
421 if ((ec->es->ntlmssp_state->neg_flags & (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) !=
422 (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) {
423 return NT_STATUS_INVALID_PARAMETER;
426 /* Todo - check gssapi case. */
431 /******************************************************************************
432 Negotiation was successful - turn on server-side encryption.
433 ******************************************************************************/
435 NTSTATUS srv_encryption_start(void)
439 /* Check that we are really doing sign+seal. */
440 status = check_enc_good(partial_srv_trans_enc_ctx);
441 if (!NT_STATUS_IS_OK(status)) {
444 /* Throw away the context we're using currently (if any). */
445 srv_free_encryption_context(&srv_trans_enc_ctx);
447 /* Steal the partial pointer. Deliberate shallow copy. */
448 srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
449 srv_trans_enc_ctx->es->enc_on = True;
451 partial_srv_trans_enc_ctx = NULL;
455 /******************************************************************************
456 Shutdown all server contexts.
457 ******************************************************************************/
459 void server_encryption_shutdown(void)
461 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
462 srv_free_encryption_context(&srv_trans_enc_ctx);