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 auth_ntlmssp_state *auth_ntlmssp_state; /* 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 NTSTATUS status = auth_ntlmssp_prepare(remote_address,
92 &ec->auth_ntlmssp_state);
93 if (!NT_STATUS_IS_OK(status)) {
94 return nt_status_squash(status);
97 auth_ntlmssp_want_feature(ec->auth_ntlmssp_state, NTLMSSP_FEATURE_SEAL);
99 status = auth_ntlmssp_start(ec->auth_ntlmssp_state);
101 if (!NT_STATUS_IS_OK(status)) {
102 return nt_status_squash(status);
106 * We must remember to update the pointer copy for the common
107 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
109 ec->es->s.auth_ntlmssp_state = ec->auth_ntlmssp_state;
113 /******************************************************************************
114 Destroy an auth_ntlmssp_state and ensure pointer copy is correct.
115 ******************************************************************************/
117 static void destroy_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
120 * We must remember to update the pointer copy for the common
121 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
124 if (ec->auth_ntlmssp_state) {
125 TALLOC_FREE(ec->auth_ntlmssp_state);
126 /* The auth_ntlmssp_end killed this already. */
127 ec->es->s.auth_ntlmssp_state = NULL;
131 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
133 /******************************************************************************
135 ******************************************************************************/
137 static NTSTATUS get_srv_gss_creds(const char *service,
139 gss_cred_usage_t cred_type,
140 gss_cred_id_t *p_srv_cred)
145 gss_buffer_desc input_name;
146 char *host_princ_s = NULL;
147 NTSTATUS status = NT_STATUS_OK;
149 gss_OID_desc nt_hostbased_service =
150 {10, discard_const_p(char, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
152 if (asprintf(&host_princ_s, "%s@%s", service, name) == -1) {
153 return NT_STATUS_NO_MEMORY;
156 input_name.value = host_princ_s;
157 input_name.length = strlen(host_princ_s) + 1;
159 ret = gss_import_name(&min,
161 &nt_hostbased_service,
164 DEBUG(10,("get_srv_gss_creds: imported name %s\n",
167 if (ret != GSS_S_COMPLETE) {
168 SAFE_FREE(host_princ_s);
169 return map_nt_error_from_gss(ret, min);
173 * We're accessing the krb5.keytab file here.
174 * ensure we have permissions to do so.
178 ret = gss_acquire_cred(&min,
188 if (ret != GSS_S_COMPLETE) {
189 ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
190 DEBUG(10,("get_srv_gss_creds: gss_acquire_cred failed with %s\n",
192 status = map_nt_error_from_gss(ret, min);
195 SAFE_FREE(host_princ_s);
196 gss_release_name(&min, &srv_name);
200 /******************************************************************************
202 Try and get the cifs/server@realm principal first, then fall back to
204 ******************************************************************************/
206 static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
209 gss_cred_id_t srv_cred;
212 name_to_fqdn(fqdn, lp_netbios_name());
215 status = get_srv_gss_creds("cifs", fqdn, GSS_C_ACCEPT, &srv_cred);
216 if (!NT_STATUS_IS_OK(status)) {
217 status = get_srv_gss_creds("host", fqdn, GSS_C_ACCEPT, &srv_cred);
218 if (!NT_STATUS_IS_OK(status)) {
219 return nt_status_squash(status);
223 ec->es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
224 if (!ec->es->s.gss_state) {
226 gss_release_cred(&min, &srv_cred);
227 return NT_STATUS_NO_MEMORY;
229 ZERO_STRUCTP(ec->es->s.gss_state);
230 ec->es->s.gss_state->creds = srv_cred;
232 /* No context yet. */
233 ec->es->s.gss_state->gss_ctx = GSS_C_NO_CONTEXT;
239 /******************************************************************************
240 Shutdown a server encryption context.
241 ******************************************************************************/
243 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
245 struct smb_srv_trans_enc_ctx *ec = *pp_ec;
252 switch (ec->es->smb_enc_type) {
253 case SMB_TRANS_ENC_NTLM:
254 destroy_auth_ntlmssp(ec);
256 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
257 case SMB_TRANS_ENC_GSS:
261 common_free_encryption_state(&ec->es);
268 /******************************************************************************
269 Create a server encryption context.
270 ******************************************************************************/
272 static NTSTATUS make_srv_encryption_context(const struct tsocket_address *remote_address,
273 enum smb_trans_enc_type smb_enc_type,
274 struct smb_srv_trans_enc_ctx **pp_ec)
276 struct smb_srv_trans_enc_ctx *ec;
280 ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
282 return NT_STATUS_NO_MEMORY;
284 ZERO_STRUCTP(partial_srv_trans_enc_ctx);
285 ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
288 return NT_STATUS_NO_MEMORY;
290 ZERO_STRUCTP(ec->es);
291 ec->es->smb_enc_type = smb_enc_type;
292 switch (smb_enc_type) {
293 case SMB_TRANS_ENC_NTLM:
295 NTSTATUS status = make_auth_ntlmssp(remote_address,
297 if (!NT_STATUS_IS_OK(status)) {
298 srv_free_encryption_context(&ec);
304 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
305 case SMB_TRANS_ENC_GSS:
306 /* Acquire our credentials by calling gss_acquire_cred here. */
308 NTSTATUS status = make_auth_gss(ec);
309 if (!NT_STATUS_IS_OK(status)) {
310 srv_free_encryption_context(&ec);
317 srv_free_encryption_context(&ec);
318 return NT_STATUS_INVALID_PARAMETER;
324 /******************************************************************************
325 Free an encryption-allocated buffer.
326 ******************************************************************************/
328 void srv_free_enc_buffer(struct smbd_server_connection *sconn, char *buf)
330 /* We know this is an smb buffer, and we
331 * didn't malloc, only copy, for a keepalive,
332 * so ignore non-session messages. */
338 if (srv_trans_enc_ctx) {
339 common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
343 /******************************************************************************
344 Decrypt an incoming buffer.
345 ******************************************************************************/
347 NTSTATUS srv_decrypt_buffer(struct smbd_server_connection *sconn, char *buf)
349 /* Ignore non-session messages. */
354 if (srv_trans_enc_ctx) {
355 return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
361 /******************************************************************************
362 Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
363 ******************************************************************************/
365 NTSTATUS srv_encrypt_buffer(struct smbd_server_connection *sconn, char *buf,
370 /* Ignore non-session messages. */
375 if (srv_trans_enc_ctx) {
376 return common_encrypt_buffer(srv_trans_enc_ctx->es, buf, buf_out);
378 /* Not encrypting. */
382 /******************************************************************************
383 Do the gss encryption negotiation. Parameters are in/out.
384 Until success we do everything on the partial enc ctx.
385 ******************************************************************************/
387 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
388 static NTSTATUS srv_enc_spnego_gss_negotiate(const struct tsocket_address *remote_address,
389 unsigned char **ppdata,
396 gss_buffer_desc in_buf, out_buf;
397 struct smb_tran_enc_state_gss *gss_state;
398 DATA_BLOB auth_reply = data_blob_null;
399 DATA_BLOB response = data_blob_null;
402 if (!partial_srv_trans_enc_ctx) {
403 status = make_srv_encryption_context(remote_address,
405 &partial_srv_trans_enc_ctx);
406 if (!NT_STATUS_IS_OK(status)) {
411 gss_state = partial_srv_trans_enc_ctx->es->s.gss_state;
413 in_buf.value = secblob.data;
414 in_buf.length = secblob.length;
416 out_buf.value = NULL;
421 ret = gss_accept_sec_context(&min,
425 GSS_C_NO_CHANNEL_BINDINGS,
427 NULL, /* Ignore oids. */
428 &out_buf, /* To return. */
430 NULL, /* Ingore time. */
431 NULL); /* Ignore delegated creds. */
434 status = gss_err_to_ntstatus(ret, min);
435 if (ret != GSS_S_COMPLETE && ret != GSS_S_CONTINUE_NEEDED) {
439 /* Ensure we've got sign+seal available. */
440 if (ret == GSS_S_COMPLETE) {
441 if ((flags & (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) !=
442 (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) {
443 DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
444 "for SMB sealing.\n"));
445 gss_release_buffer(&min, &out_buf);
446 return NT_STATUS_ACCESS_DENIED;
450 auth_reply = data_blob(out_buf.value, out_buf.length);
451 gss_release_buffer(&min, &out_buf);
453 /* Wrap in SPNEGO. */
454 response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, OID_KERBEROS5);
455 data_blob_free(&auth_reply);
458 *ppdata = (unsigned char *)memdup(response.data, response.length);
459 if ((*ppdata) == NULL && response.length > 0) {
460 status = NT_STATUS_NO_MEMORY;
462 *p_data_size = response.length;
464 data_blob_free(&response);
470 /******************************************************************************
471 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
472 Until success we do everything on the partial enc ctx.
473 ******************************************************************************/
475 static NTSTATUS srv_enc_ntlm_negotiate(const struct tsocket_address *remote_address,
476 unsigned char **ppdata,
482 DATA_BLOB chal = data_blob_null;
483 DATA_BLOB response = data_blob_null;
485 status = make_srv_encryption_context(remote_address,
487 &partial_srv_trans_enc_ctx);
488 if (!NT_STATUS_IS_OK(status)) {
492 status = gensec_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state->gensec_security,
496 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
500 response = spnego_gen_auth_response(talloc_tos(), &chal, status, OID_NTLMSSP);
501 data_blob_free(&chal);
503 /* Return the raw blob. */
508 *ppdata = (unsigned char *)memdup(response.data, response.length);
509 if ((*ppdata) == NULL && response.length > 0) {
510 status = NT_STATUS_NO_MEMORY;
512 *p_data_size = response.length;
513 data_blob_free(&response);
518 /******************************************************************************
519 Do the SPNEGO encryption negotiation. Parameters are in/out.
520 Based off code in smbd/sesssionsetup.c
521 Until success we do everything on the partial enc ctx.
522 ******************************************************************************/
524 static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
525 unsigned char **ppdata,
527 unsigned char **pparam,
528 size_t *p_param_size)
531 DATA_BLOB blob = data_blob_null;
532 DATA_BLOB secblob = data_blob_null;
533 char *kerb_mech = NULL;
535 blob = data_blob_const(*ppdata, *p_data_size);
537 status = parse_spnego_mechanisms(talloc_tos(), blob, &secblob, &kerb_mech);
538 if (!NT_STATUS_IS_OK(status)) {
539 return nt_status_squash(status);
542 /* We should have no partial context at this point. */
544 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
547 TALLOC_FREE(kerb_mech);
549 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
550 status = srv_enc_spnego_gss_negotiate(conn->sconn->remote_address,
555 /* Currently we don't SPNEGO negotiate
556 * back to NTLMSSP as we do in sessionsetupX. We should... */
557 return NT_STATUS_LOGON_FAILURE;
560 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
567 data_blob_free(&secblob);
569 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
570 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
571 return nt_status_squash(status);
574 if (NT_STATUS_IS_OK(status)) {
575 /* Return the context we're using for this encryption state. */
576 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
577 return NT_STATUS_NO_MEMORY;
579 SSVAL(*pparam,0,partial_srv_trans_enc_ctx->es->enc_ctx_num);
586 /******************************************************************************
587 Complete a SPNEGO encryption negotiation. Parameters are in/out.
588 We only get this for a NTLM auth second stage.
589 ******************************************************************************/
591 static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
592 unsigned char **ppdata,
594 unsigned char **pparam,
595 size_t *p_param_size)
598 DATA_BLOB blob = data_blob_null;
599 DATA_BLOB auth = data_blob_null;
600 DATA_BLOB auth_reply = data_blob_null;
601 DATA_BLOB response = data_blob_null;
602 struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
604 /* We must have a partial context here. */
606 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
607 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
608 return NT_STATUS_INVALID_PARAMETER;
611 blob = data_blob_const(*ppdata, *p_data_size);
612 if (!spnego_parse_auth(talloc_tos(), blob, &auth)) {
613 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
614 return NT_STATUS_INVALID_PARAMETER;
617 status = gensec_update(ec->auth_ntlmssp_state->gensec_security, talloc_tos(), NULL, auth, &auth_reply);
618 data_blob_free(&auth);
624 * This field SHALL only be present in the first reply from the
626 * So set mechOID to NULL here.
629 response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, NULL);
630 data_blob_free(&auth_reply);
632 if (NT_STATUS_IS_OK(status)) {
633 /* Return the context we're using for this encryption state. */
634 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
635 return NT_STATUS_NO_MEMORY;
637 SSVAL(*pparam,0,ec->es->enc_ctx_num);
642 *ppdata = (unsigned char *)memdup(response.data, response.length);
643 if ((*ppdata) == NULL && response.length > 0)
644 return NT_STATUS_NO_MEMORY;
645 *p_data_size = response.length;
646 data_blob_free(&response);
650 /******************************************************************************
651 Raw NTLM encryption negotiation. Parameters are in/out.
652 This function does both steps.
653 ******************************************************************************/
655 static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
656 unsigned char **ppdata,
658 unsigned char **pparam,
659 size_t *p_param_size)
662 DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
663 DATA_BLOB response = data_blob_null;
664 struct smb_srv_trans_enc_ctx *ec;
666 if (!partial_srv_trans_enc_ctx) {
667 /* This is the initial step. */
668 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
673 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
674 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
675 return nt_status_squash(status);
680 ec = partial_srv_trans_enc_ctx;
681 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
682 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
683 return NT_STATUS_INVALID_PARAMETER;
687 status = gensec_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state->gensec_security,
691 if (NT_STATUS_IS_OK(status)) {
692 /* Return the context we're using for this encryption state. */
693 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
694 return NT_STATUS_NO_MEMORY;
696 SSVAL(*pparam,0,ec->es->enc_ctx_num);
700 /* Return the raw blob. */
702 *ppdata = (unsigned char *)memdup(response.data, response.length);
703 if ((*ppdata) == NULL && response.length > 0)
704 return NT_STATUS_NO_MEMORY;
705 *p_data_size = response.length;
706 data_blob_free(&response);
710 /******************************************************************************
711 Do the SPNEGO encryption negotiation. Parameters are in/out.
712 ******************************************************************************/
714 NTSTATUS srv_request_encryption_setup(connection_struct *conn,
715 unsigned char **ppdata,
717 unsigned char **pparam,
718 size_t *p_param_size)
720 unsigned char *pdata = *ppdata;
725 if (*p_data_size < 1) {
726 return NT_STATUS_INVALID_PARAMETER;
729 if (pdata[0] == ASN1_APPLICATION(0)) {
730 /* its a negTokenTarg packet */
731 return srv_enc_spnego_negotiate(conn, ppdata, p_data_size, pparam, p_param_size);
734 if (pdata[0] == ASN1_CONTEXT(1)) {
735 /* It's an auth packet */
736 return srv_enc_spnego_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
739 /* Maybe it's a raw unwrapped auth ? */
740 if (*p_data_size < 7) {
741 return NT_STATUS_INVALID_PARAMETER;
744 if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
745 return srv_enc_raw_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
748 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
750 return NT_STATUS_LOGON_FAILURE;
753 /******************************************************************************
754 Negotiation was successful - turn on server-side encryption.
755 ******************************************************************************/
757 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
759 if (!ec || !ec->es) {
760 return NT_STATUS_LOGON_FAILURE;
763 if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
764 if (!auth_ntlmssp_negotiated_sign((ec->auth_ntlmssp_state))) {
765 return NT_STATUS_INVALID_PARAMETER;
768 if (!auth_ntlmssp_negotiated_seal((ec->auth_ntlmssp_state))) {
769 return NT_STATUS_INVALID_PARAMETER;
772 /* Todo - check gssapi case. */
777 /******************************************************************************
778 Negotiation was successful - turn on server-side encryption.
779 ******************************************************************************/
781 NTSTATUS srv_encryption_start(connection_struct *conn)
785 /* Check that we are really doing sign+seal. */
786 status = check_enc_good(partial_srv_trans_enc_ctx);
787 if (!NT_STATUS_IS_OK(status)) {
790 /* Throw away the context we're using currently (if any). */
791 srv_free_encryption_context(&srv_trans_enc_ctx);
793 /* Steal the partial pointer. Deliberate shallow copy. */
794 srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
795 srv_trans_enc_ctx->es->enc_on = true;
797 partial_srv_trans_enc_ctx = NULL;
799 DEBUG(1,("srv_encryption_start: context negotiated\n"));
803 /******************************************************************************
804 Shutdown all server contexts.
805 ******************************************************************************/
807 void server_encryption_shutdown(struct smbd_server_connection *sconn)
809 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
810 srv_free_encryption_context(&srv_trans_enc_ctx);