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 3 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, see <http://www.gnu.org/licenses/>.
21 #include "smbd/smbd.h"
22 #include "smbd/globals.h"
23 #include "../libcli/auth/spnego.h"
24 #include "../auth/ntlmssp/ntlmssp.h"
25 #include "ntlmssp_wrap.h"
26 #include "smb_crypt.h"
27 #include "../lib/util/asn1.h"
29 #include "libsmb/libsmb.h"
30 #include "../lib/tsocket/tsocket.h"
31 #include "auth/gensec/gensec.h"
33 /******************************************************************************
34 Server side encryption.
35 ******************************************************************************/
37 /******************************************************************************
39 ******************************************************************************/
41 struct smb_srv_trans_enc_ctx {
42 struct smb_trans_enc_state *es;
43 struct gensec_security *gensec_security; /* Must be kept in sync with pointer in ec->ntlmssp_state. */
46 /******************************************************************************
47 Return global enc context - this must change if we ever do multiple contexts.
48 ******************************************************************************/
50 static uint16_t srv_enc_ctx(const struct smb_srv_trans_enc_ctx *ec)
52 return ec->es->enc_ctx_num;
55 /******************************************************************************
56 Is this an incoming encrypted packet ?
57 ******************************************************************************/
59 bool is_encrypted_packet(struct smbd_server_connection *sconn,
65 /* Ignore non-session messages or non 0xFF'E' messages. */
67 || (smb_len(inbuf) < 8)
68 || !(inbuf[4] == 0xFF && inbuf[5] == 'E')) {
72 status = get_enc_ctx_num(inbuf, &enc_num);
73 if (!NT_STATUS_IS_OK(status)) {
77 /* Encrypted messages are 0xFF'E'<ctx> */
78 if (srv_trans_enc_ctx && enc_num == srv_enc_ctx(srv_trans_enc_ctx)) {
84 /******************************************************************************
85 Create an auth_ntlmssp_state and ensure pointer copy is correct.
86 ******************************************************************************/
88 static NTSTATUS make_auth_ntlmssp(const struct tsocket_address *remote_address,
89 struct smb_srv_trans_enc_ctx *ec)
91 struct auth_ntlmssp_state *auth_ntlmssp_state;
92 NTSTATUS status = auth_ntlmssp_prepare(remote_address,
94 if (!NT_STATUS_IS_OK(status)) {
95 return nt_status_squash(status);
98 gensec_want_feature(auth_ntlmssp_state->gensec_security, GENSEC_FEATURE_SEAL);
100 status = auth_ntlmssp_start(auth_ntlmssp_state);
102 if (!NT_STATUS_IS_OK(status)) {
103 TALLOC_FREE(auth_ntlmssp_state);
104 return nt_status_squash(status);
108 * We must remember to update the pointer copy for the common
109 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
111 ec->es->s.gensec_security = ec->gensec_security;
113 /* We do not need the auth_ntlmssp layer any more, which was
114 * allocated on NULL, so promote gensec_security to the NULL
116 ec->gensec_security = talloc_move(NULL, &auth_ntlmssp_state->gensec_security);
117 TALLOC_FREE(auth_ntlmssp_state);
122 /******************************************************************************
123 Destroy an auth_ntlmssp_state and ensure pointer copy is correct.
124 ******************************************************************************/
126 static void destroy_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
129 * We must remember to update the pointer copy for the common
130 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
133 if (ec->gensec_security) {
134 TALLOC_FREE(ec->gensec_security);
135 /* The auth_ntlmssp_end killed this already. */
136 ec->es->s.gensec_security = NULL;
140 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
142 /******************************************************************************
144 ******************************************************************************/
146 static NTSTATUS get_srv_gss_creds(const char *service,
148 gss_cred_usage_t cred_type,
149 gss_cred_id_t *p_srv_cred)
154 gss_buffer_desc input_name;
155 char *host_princ_s = NULL;
156 NTSTATUS status = NT_STATUS_OK;
158 gss_OID_desc nt_hostbased_service =
159 {10, discard_const_p(char, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
161 if (asprintf(&host_princ_s, "%s@%s", service, name) == -1) {
162 return NT_STATUS_NO_MEMORY;
165 input_name.value = host_princ_s;
166 input_name.length = strlen(host_princ_s) + 1;
168 ret = gss_import_name(&min,
170 &nt_hostbased_service,
173 DEBUG(10,("get_srv_gss_creds: imported name %s\n",
176 if (ret != GSS_S_COMPLETE) {
177 SAFE_FREE(host_princ_s);
178 return map_nt_error_from_gss(ret, min);
182 * We're accessing the krb5.keytab file here.
183 * ensure we have permissions to do so.
187 ret = gss_acquire_cred(&min,
197 if (ret != GSS_S_COMPLETE) {
198 ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
199 DEBUG(10,("get_srv_gss_creds: gss_acquire_cred failed with %s\n",
201 status = map_nt_error_from_gss(ret, min);
204 SAFE_FREE(host_princ_s);
205 gss_release_name(&min, &srv_name);
209 /******************************************************************************
211 Try and get the cifs/server@realm principal first, then fall back to
213 ******************************************************************************/
215 static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
218 gss_cred_id_t srv_cred;
221 name_to_fqdn(fqdn, lp_netbios_name());
224 status = get_srv_gss_creds("cifs", fqdn, GSS_C_ACCEPT, &srv_cred);
225 if (!NT_STATUS_IS_OK(status)) {
226 status = get_srv_gss_creds("host", fqdn, GSS_C_ACCEPT, &srv_cred);
227 if (!NT_STATUS_IS_OK(status)) {
228 return nt_status_squash(status);
232 ec->es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
233 if (!ec->es->s.gss_state) {
235 gss_release_cred(&min, &srv_cred);
236 return NT_STATUS_NO_MEMORY;
238 ZERO_STRUCTP(ec->es->s.gss_state);
239 ec->es->s.gss_state->creds = srv_cred;
241 /* No context yet. */
242 ec->es->s.gss_state->gss_ctx = GSS_C_NO_CONTEXT;
248 /******************************************************************************
249 Shutdown a server encryption context.
250 ******************************************************************************/
252 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
254 struct smb_srv_trans_enc_ctx *ec = *pp_ec;
261 switch (ec->es->smb_enc_type) {
262 case SMB_TRANS_ENC_NTLM:
263 destroy_auth_ntlmssp(ec);
265 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
266 case SMB_TRANS_ENC_GSS:
270 common_free_encryption_state(&ec->es);
277 /******************************************************************************
278 Create a server encryption context.
279 ******************************************************************************/
281 static NTSTATUS make_srv_encryption_context(const struct tsocket_address *remote_address,
282 enum smb_trans_enc_type smb_enc_type,
283 struct smb_srv_trans_enc_ctx **pp_ec)
285 struct smb_srv_trans_enc_ctx *ec;
289 ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
291 return NT_STATUS_NO_MEMORY;
293 ZERO_STRUCTP(partial_srv_trans_enc_ctx);
294 ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
297 return NT_STATUS_NO_MEMORY;
299 ZERO_STRUCTP(ec->es);
300 ec->es->smb_enc_type = smb_enc_type;
301 switch (smb_enc_type) {
302 case SMB_TRANS_ENC_NTLM:
304 NTSTATUS status = make_auth_ntlmssp(remote_address,
306 if (!NT_STATUS_IS_OK(status)) {
307 srv_free_encryption_context(&ec);
313 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
314 case SMB_TRANS_ENC_GSS:
315 /* Acquire our credentials by calling gss_acquire_cred here. */
317 NTSTATUS status = make_auth_gss(ec);
318 if (!NT_STATUS_IS_OK(status)) {
319 srv_free_encryption_context(&ec);
326 srv_free_encryption_context(&ec);
327 return NT_STATUS_INVALID_PARAMETER;
333 /******************************************************************************
334 Free an encryption-allocated buffer.
335 ******************************************************************************/
337 void srv_free_enc_buffer(struct smbd_server_connection *sconn, char *buf)
339 /* We know this is an smb buffer, and we
340 * didn't malloc, only copy, for a keepalive,
341 * so ignore non-session messages. */
347 if (srv_trans_enc_ctx) {
348 common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
352 /******************************************************************************
353 Decrypt an incoming buffer.
354 ******************************************************************************/
356 NTSTATUS srv_decrypt_buffer(struct smbd_server_connection *sconn, char *buf)
358 /* Ignore non-session messages. */
363 if (srv_trans_enc_ctx) {
364 return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
370 /******************************************************************************
371 Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
372 ******************************************************************************/
374 NTSTATUS srv_encrypt_buffer(struct smbd_server_connection *sconn, char *buf,
379 /* Ignore non-session messages. */
384 if (srv_trans_enc_ctx) {
385 return common_encrypt_buffer(srv_trans_enc_ctx->es, buf, buf_out);
387 /* Not encrypting. */
391 /******************************************************************************
392 Do the gss encryption negotiation. Parameters are in/out.
393 Until success we do everything on the partial enc ctx.
394 ******************************************************************************/
396 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
397 static NTSTATUS srv_enc_spnego_gss_negotiate(const struct tsocket_address *remote_address,
398 unsigned char **ppdata,
405 gss_buffer_desc in_buf, out_buf;
406 struct smb_tran_enc_state_gss *gss_state;
407 DATA_BLOB auth_reply = data_blob_null;
408 DATA_BLOB response = data_blob_null;
411 if (!partial_srv_trans_enc_ctx) {
412 status = make_srv_encryption_context(remote_address,
414 &partial_srv_trans_enc_ctx);
415 if (!NT_STATUS_IS_OK(status)) {
420 gss_state = partial_srv_trans_enc_ctx->es->s.gss_state;
422 in_buf.value = secblob.data;
423 in_buf.length = secblob.length;
425 out_buf.value = NULL;
430 ret = gss_accept_sec_context(&min,
434 GSS_C_NO_CHANNEL_BINDINGS,
436 NULL, /* Ignore oids. */
437 &out_buf, /* To return. */
439 NULL, /* Ingore time. */
440 NULL); /* Ignore delegated creds. */
443 status = gss_err_to_ntstatus(ret, min);
444 if (ret != GSS_S_COMPLETE && ret != GSS_S_CONTINUE_NEEDED) {
448 /* Ensure we've got sign+seal available. */
449 if (ret == GSS_S_COMPLETE) {
450 if ((flags & (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) !=
451 (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) {
452 DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
453 "for SMB sealing.\n"));
454 gss_release_buffer(&min, &out_buf);
455 return NT_STATUS_ACCESS_DENIED;
459 auth_reply = data_blob(out_buf.value, out_buf.length);
460 gss_release_buffer(&min, &out_buf);
462 /* Wrap in SPNEGO. */
463 response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, OID_KERBEROS5);
464 data_blob_free(&auth_reply);
467 *ppdata = (unsigned char *)memdup(response.data, response.length);
468 if ((*ppdata) == NULL && response.length > 0) {
469 status = NT_STATUS_NO_MEMORY;
471 *p_data_size = response.length;
473 data_blob_free(&response);
479 /******************************************************************************
480 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
481 Until success we do everything on the partial enc ctx.
482 ******************************************************************************/
484 static NTSTATUS srv_enc_ntlm_negotiate(const struct tsocket_address *remote_address,
485 unsigned char **ppdata,
491 DATA_BLOB chal = data_blob_null;
492 DATA_BLOB response = data_blob_null;
494 status = make_srv_encryption_context(remote_address,
496 &partial_srv_trans_enc_ctx);
497 if (!NT_STATUS_IS_OK(status)) {
501 status = gensec_update(partial_srv_trans_enc_ctx->gensec_security,
505 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
509 response = spnego_gen_auth_response(talloc_tos(), &chal, status, OID_NTLMSSP);
510 data_blob_free(&chal);
512 /* Return the raw blob. */
517 *ppdata = (unsigned char *)memdup(response.data, response.length);
518 if ((*ppdata) == NULL && response.length > 0) {
519 status = NT_STATUS_NO_MEMORY;
521 *p_data_size = response.length;
522 data_blob_free(&response);
527 /******************************************************************************
528 Do the SPNEGO encryption negotiation. Parameters are in/out.
529 Based off code in smbd/sesssionsetup.c
530 Until success we do everything on the partial enc ctx.
531 ******************************************************************************/
533 static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
534 unsigned char **ppdata,
536 unsigned char **pparam,
537 size_t *p_param_size)
540 DATA_BLOB blob = data_blob_null;
541 DATA_BLOB secblob = data_blob_null;
542 char *kerb_mech = NULL;
544 blob = data_blob_const(*ppdata, *p_data_size);
546 status = parse_spnego_mechanisms(talloc_tos(), blob, &secblob, &kerb_mech);
547 if (!NT_STATUS_IS_OK(status)) {
548 return nt_status_squash(status);
551 /* We should have no partial context at this point. */
553 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
556 TALLOC_FREE(kerb_mech);
558 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
559 status = srv_enc_spnego_gss_negotiate(conn->sconn->remote_address,
564 /* Currently we don't SPNEGO negotiate
565 * back to NTLMSSP as we do in sessionsetupX. We should... */
566 return NT_STATUS_LOGON_FAILURE;
569 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
576 data_blob_free(&secblob);
578 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
579 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
580 return nt_status_squash(status);
583 if (NT_STATUS_IS_OK(status)) {
584 /* Return the context we're using for this encryption state. */
585 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
586 return NT_STATUS_NO_MEMORY;
588 SSVAL(*pparam,0,partial_srv_trans_enc_ctx->es->enc_ctx_num);
595 /******************************************************************************
596 Complete a SPNEGO encryption negotiation. Parameters are in/out.
597 We only get this for a NTLM auth second stage.
598 ******************************************************************************/
600 static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
601 unsigned char **ppdata,
603 unsigned char **pparam,
604 size_t *p_param_size)
607 DATA_BLOB blob = data_blob_null;
608 DATA_BLOB auth = data_blob_null;
609 DATA_BLOB auth_reply = data_blob_null;
610 DATA_BLOB response = data_blob_null;
611 struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
613 /* We must have a partial context here. */
615 if (!ec || !ec->es || ec->gensec_security == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
616 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
617 return NT_STATUS_INVALID_PARAMETER;
620 blob = data_blob_const(*ppdata, *p_data_size);
621 if (!spnego_parse_auth(talloc_tos(), blob, &auth)) {
622 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
623 return NT_STATUS_INVALID_PARAMETER;
626 status = gensec_update(ec->gensec_security, talloc_tos(), NULL, auth, &auth_reply);
627 data_blob_free(&auth);
633 * This field SHALL only be present in the first reply from the
635 * So set mechOID to NULL here.
638 response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, NULL);
639 data_blob_free(&auth_reply);
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;
646 SSVAL(*pparam,0,ec->es->enc_ctx_num);
651 *ppdata = (unsigned char *)memdup(response.data, response.length);
652 if ((*ppdata) == NULL && response.length > 0)
653 return NT_STATUS_NO_MEMORY;
654 *p_data_size = response.length;
655 data_blob_free(&response);
659 /******************************************************************************
660 Raw NTLM encryption negotiation. Parameters are in/out.
661 This function does both steps.
662 ******************************************************************************/
664 static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
665 unsigned char **ppdata,
667 unsigned char **pparam,
668 size_t *p_param_size)
671 DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
672 DATA_BLOB response = data_blob_null;
673 struct smb_srv_trans_enc_ctx *ec;
675 if (!partial_srv_trans_enc_ctx) {
676 /* This is the initial step. */
677 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
682 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
683 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
684 return nt_status_squash(status);
689 ec = partial_srv_trans_enc_ctx;
690 if (!ec || !ec->es || ec->gensec_security == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
691 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
692 return NT_STATUS_INVALID_PARAMETER;
696 status = gensec_update(partial_srv_trans_enc_ctx->gensec_security,
700 if (NT_STATUS_IS_OK(status)) {
701 /* Return the context we're using for this encryption state. */
702 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
703 return NT_STATUS_NO_MEMORY;
705 SSVAL(*pparam,0,ec->es->enc_ctx_num);
709 /* Return the raw blob. */
711 *ppdata = (unsigned char *)memdup(response.data, response.length);
712 if ((*ppdata) == NULL && response.length > 0)
713 return NT_STATUS_NO_MEMORY;
714 *p_data_size = response.length;
715 data_blob_free(&response);
719 /******************************************************************************
720 Do the SPNEGO encryption negotiation. Parameters are in/out.
721 ******************************************************************************/
723 NTSTATUS srv_request_encryption_setup(connection_struct *conn,
724 unsigned char **ppdata,
726 unsigned char **pparam,
727 size_t *p_param_size)
729 unsigned char *pdata = *ppdata;
734 if (*p_data_size < 1) {
735 return NT_STATUS_INVALID_PARAMETER;
738 if (pdata[0] == ASN1_APPLICATION(0)) {
739 /* its a negTokenTarg packet */
740 return srv_enc_spnego_negotiate(conn, ppdata, p_data_size, pparam, p_param_size);
743 if (pdata[0] == ASN1_CONTEXT(1)) {
744 /* It's an auth packet */
745 return srv_enc_spnego_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
748 /* Maybe it's a raw unwrapped auth ? */
749 if (*p_data_size < 7) {
750 return NT_STATUS_INVALID_PARAMETER;
753 if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
754 return srv_enc_raw_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
757 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
759 return NT_STATUS_LOGON_FAILURE;
762 /******************************************************************************
763 Negotiation was successful - turn on server-side encryption.
764 ******************************************************************************/
766 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
768 if (!ec || !ec->es) {
769 return NT_STATUS_LOGON_FAILURE;
772 if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
773 if (!gensec_have_feature(ec->gensec_security, GENSEC_FEATURE_SIGN)) {
774 return NT_STATUS_INVALID_PARAMETER;
777 if (!gensec_have_feature(ec->gensec_security, GENSEC_FEATURE_SEAL)) {
778 return NT_STATUS_INVALID_PARAMETER;
781 /* Todo - check gssapi case. */
786 /******************************************************************************
787 Negotiation was successful - turn on server-side encryption.
788 ******************************************************************************/
790 NTSTATUS srv_encryption_start(connection_struct *conn)
794 /* Check that we are really doing sign+seal. */
795 status = check_enc_good(partial_srv_trans_enc_ctx);
796 if (!NT_STATUS_IS_OK(status)) {
799 /* Throw away the context we're using currently (if any). */
800 srv_free_encryption_context(&srv_trans_enc_ctx);
802 /* Steal the partial pointer. Deliberate shallow copy. */
803 srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
804 srv_trans_enc_ctx->es->enc_on = true;
806 partial_srv_trans_enc_ctx = NULL;
808 DEBUG(1,("srv_encryption_start: context negotiated\n"));
812 /******************************************************************************
813 Shutdown all server contexts.
814 ******************************************************************************/
816 void server_encryption_shutdown(struct smbd_server_connection *sconn)
818 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
819 srv_free_encryption_context(&srv_trans_enc_ctx);