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->s.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->s.ntlmssp_state = NULL;
88 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
90 /******************************************************************************
92 ******************************************************************************/
94 static NTSTATUS get_gss_creds(const char *service,
96 gss_cred_usage_t cred_type,
97 gss_cred_id_t *p_srv_cred)
102 gss_buffer_desc input_name;
103 char *host_princ_s = NULL;
104 NTSTATUS status = NT_STATUS_OK;
106 asprintf(&host_princ_s, "%s@%s", service, name);
107 if (host_princ_s == NULL) {
108 return NT_STATUS_NO_MEMORY;
111 input_name.value = host_princ_s;
112 input_name.length = strlen(host_princ_s) + 1;
114 ret = gss_import_name(&min,
116 GSS_C_NT_HOSTBASED_SERVICE,
119 if (ret != GSS_S_COMPLETE) {
120 SAFE_FREE(host_princ_s);
121 return map_nt_error_from_gss(ret, min);
124 ret = gss_acquire_cred(&min,
133 if (ret != GSS_S_COMPLETE) {
134 status = map_nt_error_from_gss(ret, min);
137 SAFE_FREE(host_princ_s);
138 gss_release_name(&min, &srv_name);
142 /******************************************************************************
144 ******************************************************************************/
146 static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
149 gss_cred_id_t srv_cred;
152 name_to_fqdn(fqdn, global_myname());
155 status = get_gss_creds("cifs", fqdn, GSS_C_ACCEPT, &srv_cred);
156 if (!NT_STATUS_IS_OK(status)) {
157 status = get_gss_creds("host", fqdn, GSS_C_ACCEPT, &srv_cred);
158 if (!NT_STATUS_IS_OK(status)) {
159 return nt_status_squash(status);
167 /******************************************************************************
168 Shutdown a server encryption context.
169 ******************************************************************************/
171 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
173 struct smb_srv_trans_enc_ctx *ec = *pp_ec;
180 switch (ec->es->smb_enc_type) {
181 case SMB_TRANS_ENC_NTLM:
182 destroy_auth_ntlmssp(ec);
184 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
185 case SMB_TRANS_ENC_GSS:
189 common_free_encryption_state(&ec->es);
196 /******************************************************************************
197 Create a server encryption context.
198 ******************************************************************************/
200 static struct smb_srv_trans_enc_ctx *make_srv_encryption_context(enum smb_trans_enc_type smb_enc_type)
202 struct smb_srv_trans_enc_ctx *ec;
204 ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
208 ZERO_STRUCTP(partial_srv_trans_enc_ctx);
209 ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
214 ZERO_STRUCTP(ec->es);
215 ec->es->smb_enc_type = smb_enc_type;
216 switch (smb_enc_type) {
217 case SMB_TRANS_ENC_NTLM:
219 NTSTATUS status = make_auth_ntlmssp(ec);
220 if (!NT_STATUS_IS_OK(status)) {
221 srv_free_encryption_context(&ec);
227 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
228 case SMB_TRANS_ENC_GSS:
229 /* Acquire our credentials by calling gss_acquire_cred here. */
231 NTSTATUS status = make_auth_gss(ec);
232 if (!NT_STATUS_IS_OK(status)) {
233 srv_free_encryption_context(&ec);
240 srv_free_encryption_context(&ec);
246 /******************************************************************************
247 Free an encryption-allocated buffer.
248 ******************************************************************************/
250 void srv_free_enc_buffer(char *buf)
252 /* We know this is an smb buffer, and we
253 * didn't malloc, only copy, for a keepalive,
254 * so ignore session keepalives. */
256 if(CVAL(buf,0) == SMBkeepalive) {
260 if (srv_trans_enc_ctx) {
261 common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
265 /******************************************************************************
266 Decrypt an incoming buffer.
267 ******************************************************************************/
269 NTSTATUS srv_decrypt_buffer(char *buf)
271 /* Ignore session keepalives. */
272 if(CVAL(buf,0) == SMBkeepalive) {
276 if (srv_trans_enc_ctx) {
277 return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
283 /******************************************************************************
284 Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
285 ******************************************************************************/
287 NTSTATUS srv_encrypt_buffer(char *buf, char **buf_out)
291 /* Ignore session keepalives. */
292 if(CVAL(buf,0) == SMBkeepalive) {
296 if (srv_trans_enc_ctx) {
297 return common_encrypt_buffer(srv_trans_enc_ctx->es, buf, buf_out);
299 /* Not encrypting. */
303 /******************************************************************************
304 Do the gss encryption negotiation. Parameters are in/out.
305 Until success we do everything on the partial enc ctx.
306 ******************************************************************************/
308 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
309 static NTSTATUS srv_enc_spnego_gss_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob)
311 if (!partial_srv_trans_enc_ctx) {
312 partial_srv_trans_enc_ctx = make_srv_encryption_context(SMB_TRANS_ENC_GSS);
313 if (!partial_srv_trans_enc_ctx) {
314 return NT_STATUS_NO_MEMORY;
318 return NT_STATUS_NOT_SUPPORTED;
322 /******************************************************************************
323 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
324 Until success we do everything on the partial enc ctx.
325 ******************************************************************************/
327 static NTSTATUS srv_enc_ntlm_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob, BOOL spnego_wrap)
330 DATA_BLOB chal = data_blob(NULL, 0);
331 DATA_BLOB response = data_blob(NULL, 0);
333 partial_srv_trans_enc_ctx = make_srv_encryption_context(SMB_TRANS_ENC_NTLM);
334 if (!partial_srv_trans_enc_ctx) {
335 return NT_STATUS_NO_MEMORY;
338 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, secblob, &chal);
340 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
344 response = spnego_gen_auth_response(&chal, status, OID_NTLMSSP);
345 data_blob_free(&chal);
347 /* Return the raw blob. */
352 *ppdata = response.data;
353 *p_data_size = response.length;
357 /******************************************************************************
358 Do the SPNEGO encryption negotiation. Parameters are in/out.
359 Based off code in smbd/sesssionsetup.c
360 Until success we do everything on the partial enc ctx.
361 ******************************************************************************/
363 static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
364 unsigned char **ppdata,
366 unsigned char **pparam,
367 size_t *p_param_size)
370 DATA_BLOB blob = data_blob(NULL,0);
371 DATA_BLOB secblob = data_blob(NULL, 0);
372 BOOL got_kerberos_mechanism = False;
374 blob = data_blob_const(*ppdata, *p_data_size);
376 status = parse_spnego_mechanisms(blob, &secblob, &got_kerberos_mechanism);
377 if (!NT_STATUS_IS_OK(status)) {
378 return nt_status_squash(status);
381 /* We should have no partial context at this point. */
383 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
385 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
386 if (got_kerberos_mechanism && lp_use_kerberos_keytab() ) {
387 status = srv_enc_spnego_gss_negotiate(ppdata, p_data_size, secblob);
391 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, secblob, True);
394 data_blob_free(&secblob);
396 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
397 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
398 return nt_status_squash(status);
401 if (NT_STATUS_IS_OK(status)) {
402 /* Return the context we're using for this encryption state. */
403 *pparam = SMB_MALLOC(2);
405 return NT_STATUS_NO_MEMORY;
407 SSVAL(*pparam,0,partial_srv_trans_enc_ctx->es->enc_ctx_num);
414 /******************************************************************************
415 Complete a SPNEGO encryption negotiation. Parameters are in/out.
416 We only get this for a NTLM auth second stage.
417 ******************************************************************************/
419 static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
420 unsigned char **ppdata,
422 unsigned char **pparam,
423 size_t *p_param_size)
426 DATA_BLOB blob = data_blob(NULL,0);
427 DATA_BLOB auth = data_blob(NULL,0);
428 DATA_BLOB auth_reply = data_blob(NULL,0);
429 DATA_BLOB response = data_blob(NULL,0);
430 struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
432 /* We must have a partial context here. */
434 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
435 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
436 return NT_STATUS_INVALID_PARAMETER;
439 blob = data_blob_const(*ppdata, *p_data_size);
440 if (!spnego_parse_auth(blob, &auth)) {
441 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
442 return NT_STATUS_INVALID_PARAMETER;
445 status = auth_ntlmssp_update(ec->auth_ntlmssp_state, auth, &auth_reply);
446 data_blob_free(&auth);
448 response = spnego_gen_auth_response(&auth_reply, status, OID_NTLMSSP);
449 data_blob_free(&auth_reply);
451 if (NT_STATUS_IS_OK(status)) {
452 /* Return the context we're using for this encryption state. */
453 *pparam = SMB_MALLOC(2);
455 return NT_STATUS_NO_MEMORY;
457 SSVAL(*pparam,0,ec->es->enc_ctx_num);
462 *ppdata = response.data;
463 *p_data_size = response.length;
467 /******************************************************************************
468 Raw NTLM encryption negotiation. Parameters are in/out.
469 This function does both steps.
470 ******************************************************************************/
472 static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
473 unsigned char **ppdata,
475 unsigned char **pparam,
476 size_t *p_param_size)
479 DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
480 DATA_BLOB response = data_blob(NULL,0);
481 struct smb_srv_trans_enc_ctx *ec;
483 if (!partial_srv_trans_enc_ctx) {
484 /* This is the initial step. */
485 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, blob, False);
486 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
487 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
488 return nt_status_squash(status);
493 ec = partial_srv_trans_enc_ctx;
494 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
495 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
496 return NT_STATUS_INVALID_PARAMETER;
500 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, blob, &response);
502 if (NT_STATUS_IS_OK(status)) {
503 /* Return the context we're using for this encryption state. */
504 *pparam = SMB_MALLOC(2);
506 return NT_STATUS_NO_MEMORY;
508 SSVAL(*pparam,0,ec->es->enc_ctx_num);
512 /* Return the raw blob. */
514 *ppdata = response.data;
515 *p_data_size = response.length;
519 /******************************************************************************
520 Do the SPNEGO encryption negotiation. Parameters are in/out.
521 ******************************************************************************/
523 NTSTATUS srv_request_encryption_setup(connection_struct *conn,
524 unsigned char **ppdata,
526 unsigned char **pparam,
527 size_t *p_param_size)
529 unsigned char *pdata = *ppdata;
534 if (*p_data_size < 1) {
535 return NT_STATUS_INVALID_PARAMETER;
538 if (pdata[0] == ASN1_APPLICATION(0)) {
539 /* its a negTokenTarg packet */
540 return srv_enc_spnego_negotiate(conn, ppdata, p_data_size, pparam, p_param_size);
543 if (pdata[0] == ASN1_CONTEXT(1)) {
544 /* It's an auth packet */
545 return srv_enc_spnego_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
548 /* Maybe it's a raw unwrapped auth ? */
549 if (*p_data_size < 7) {
550 return NT_STATUS_INVALID_PARAMETER;
553 if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
554 return srv_enc_raw_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
557 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
559 return NT_STATUS_LOGON_FAILURE;
562 /******************************************************************************
563 Negotiation was successful - turn on server-side encryption.
564 ******************************************************************************/
566 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
568 if (!ec || !ec->es) {
569 return NT_STATUS_LOGON_FAILURE;
572 if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
573 if ((ec->es->s.ntlmssp_state->neg_flags & (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) !=
574 (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) {
575 return NT_STATUS_INVALID_PARAMETER;
578 /* Todo - check gssapi case. */
583 /******************************************************************************
584 Negotiation was successful - turn on server-side encryption.
585 ******************************************************************************/
587 NTSTATUS srv_encryption_start(connection_struct *conn)
591 /* Check that we are really doing sign+seal. */
592 status = check_enc_good(partial_srv_trans_enc_ctx);
593 if (!NT_STATUS_IS_OK(status)) {
596 /* Throw away the context we're using currently (if any). */
597 srv_free_encryption_context(&srv_trans_enc_ctx);
599 /* Steal the partial pointer. Deliberate shallow copy. */
600 srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
601 srv_trans_enc_ctx->es->enc_on = True;
603 partial_srv_trans_enc_ctx = NULL;
607 /******************************************************************************
608 Shutdown all server contexts.
609 ******************************************************************************/
611 void server_encryption_shutdown(void)
613 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
614 srv_free_encryption_context(&srv_trans_enc_ctx);