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"
32 /******************************************************************************
33 Server side encryption.
34 ******************************************************************************/
36 /******************************************************************************
38 ******************************************************************************/
40 struct smb_srv_trans_enc_ctx {
41 struct smb_trans_enc_state *es;
42 struct auth_ntlmssp_state *auth_ntlmssp_state; /* Must be kept in sync with pointer in ec->ntlmssp_state. */
45 /******************************************************************************
46 Return global enc context - this must change if we ever do multiple contexts.
47 ******************************************************************************/
49 static uint16_t srv_enc_ctx(const struct smb_srv_trans_enc_ctx *ec)
51 return ec->es->enc_ctx_num;
54 /******************************************************************************
55 Is this an incoming encrypted packet ?
56 ******************************************************************************/
58 bool is_encrypted_packet(struct smbd_server_connection *sconn,
64 /* Ignore non-session messages or non 0xFF'E' messages. */
66 || (smb_len(inbuf) < 8)
67 || !(inbuf[4] == 0xFF && inbuf[5] == 'E')) {
71 status = get_enc_ctx_num(inbuf, &enc_num);
72 if (!NT_STATUS_IS_OK(status)) {
76 /* Encrypted messages are 0xFF'E'<ctx> */
77 if (srv_trans_enc_ctx && enc_num == srv_enc_ctx(srv_trans_enc_ctx)) {
83 /******************************************************************************
84 Create an auth_ntlmssp_state and ensure pointer copy is correct.
85 ******************************************************************************/
87 static NTSTATUS make_auth_ntlmssp(const struct tsocket_address *remote_address,
88 struct smb_srv_trans_enc_ctx *ec)
90 NTSTATUS status = auth_ntlmssp_prepare(remote_address,
91 &ec->auth_ntlmssp_state);
92 if (!NT_STATUS_IS_OK(status)) {
93 return nt_status_squash(status);
96 auth_ntlmssp_want_feature(ec->auth_ntlmssp_state, NTLMSSP_FEATURE_SEAL);
98 status = auth_ntlmssp_start(ec->auth_ntlmssp_state);
100 if (!NT_STATUS_IS_OK(status)) {
101 return nt_status_squash(status);
105 * We must remember to update the pointer copy for the common
106 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
108 ec->es->s.auth_ntlmssp_state = ec->auth_ntlmssp_state;
112 /******************************************************************************
113 Destroy an auth_ntlmssp_state and ensure pointer copy is correct.
114 ******************************************************************************/
116 static void destroy_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
119 * We must remember to update the pointer copy for the common
120 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
123 if (ec->auth_ntlmssp_state) {
124 TALLOC_FREE(ec->auth_ntlmssp_state);
125 /* The auth_ntlmssp_end killed this already. */
126 ec->es->s.auth_ntlmssp_state = NULL;
130 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
132 /******************************************************************************
134 ******************************************************************************/
136 static NTSTATUS get_srv_gss_creds(const char *service,
138 gss_cred_usage_t cred_type,
139 gss_cred_id_t *p_srv_cred)
144 gss_buffer_desc input_name;
145 char *host_princ_s = NULL;
146 NTSTATUS status = NT_STATUS_OK;
148 gss_OID_desc nt_hostbased_service =
149 {10, discard_const_p(char, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
151 if (asprintf(&host_princ_s, "%s@%s", service, name) == -1) {
152 return NT_STATUS_NO_MEMORY;
155 input_name.value = host_princ_s;
156 input_name.length = strlen(host_princ_s) + 1;
158 ret = gss_import_name(&min,
160 &nt_hostbased_service,
163 DEBUG(10,("get_srv_gss_creds: imported name %s\n",
166 if (ret != GSS_S_COMPLETE) {
167 SAFE_FREE(host_princ_s);
168 return map_nt_error_from_gss(ret, min);
172 * We're accessing the krb5.keytab file here.
173 * ensure we have permissions to do so.
177 ret = gss_acquire_cred(&min,
187 if (ret != GSS_S_COMPLETE) {
188 ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
189 DEBUG(10,("get_srv_gss_creds: gss_acquire_cred failed with %s\n",
191 status = map_nt_error_from_gss(ret, min);
194 SAFE_FREE(host_princ_s);
195 gss_release_name(&min, &srv_name);
199 /******************************************************************************
201 Try and get the cifs/server@realm principal first, then fall back to
203 ******************************************************************************/
205 static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
208 gss_cred_id_t srv_cred;
211 name_to_fqdn(fqdn, lp_netbios_name());
214 status = get_srv_gss_creds("cifs", fqdn, GSS_C_ACCEPT, &srv_cred);
215 if (!NT_STATUS_IS_OK(status)) {
216 status = get_srv_gss_creds("host", fqdn, GSS_C_ACCEPT, &srv_cred);
217 if (!NT_STATUS_IS_OK(status)) {
218 return nt_status_squash(status);
222 ec->es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
223 if (!ec->es->s.gss_state) {
225 gss_release_cred(&min, &srv_cred);
226 return NT_STATUS_NO_MEMORY;
228 ZERO_STRUCTP(ec->es->s.gss_state);
229 ec->es->s.gss_state->creds = srv_cred;
231 /* No context yet. */
232 ec->es->s.gss_state->gss_ctx = GSS_C_NO_CONTEXT;
238 /******************************************************************************
239 Shutdown a server encryption context.
240 ******************************************************************************/
242 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
244 struct smb_srv_trans_enc_ctx *ec = *pp_ec;
251 switch (ec->es->smb_enc_type) {
252 case SMB_TRANS_ENC_NTLM:
253 destroy_auth_ntlmssp(ec);
255 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
256 case SMB_TRANS_ENC_GSS:
260 common_free_encryption_state(&ec->es);
267 /******************************************************************************
268 Create a server encryption context.
269 ******************************************************************************/
271 static NTSTATUS make_srv_encryption_context(const struct tsocket_address *remote_address,
272 enum smb_trans_enc_type smb_enc_type,
273 struct smb_srv_trans_enc_ctx **pp_ec)
275 struct smb_srv_trans_enc_ctx *ec;
279 ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
281 return NT_STATUS_NO_MEMORY;
283 ZERO_STRUCTP(partial_srv_trans_enc_ctx);
284 ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
287 return NT_STATUS_NO_MEMORY;
289 ZERO_STRUCTP(ec->es);
290 ec->es->smb_enc_type = smb_enc_type;
291 switch (smb_enc_type) {
292 case SMB_TRANS_ENC_NTLM:
294 NTSTATUS status = make_auth_ntlmssp(remote_address,
296 if (!NT_STATUS_IS_OK(status)) {
297 srv_free_encryption_context(&ec);
303 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
304 case SMB_TRANS_ENC_GSS:
305 /* Acquire our credentials by calling gss_acquire_cred here. */
307 NTSTATUS status = make_auth_gss(ec);
308 if (!NT_STATUS_IS_OK(status)) {
309 srv_free_encryption_context(&ec);
316 srv_free_encryption_context(&ec);
317 return NT_STATUS_INVALID_PARAMETER;
323 /******************************************************************************
324 Free an encryption-allocated buffer.
325 ******************************************************************************/
327 void srv_free_enc_buffer(struct smbd_server_connection *sconn, char *buf)
329 /* We know this is an smb buffer, and we
330 * didn't malloc, only copy, for a keepalive,
331 * so ignore non-session messages. */
337 if (srv_trans_enc_ctx) {
338 common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
342 /******************************************************************************
343 Decrypt an incoming buffer.
344 ******************************************************************************/
346 NTSTATUS srv_decrypt_buffer(struct smbd_server_connection *sconn, char *buf)
348 /* Ignore non-session messages. */
353 if (srv_trans_enc_ctx) {
354 return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
360 /******************************************************************************
361 Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
362 ******************************************************************************/
364 NTSTATUS srv_encrypt_buffer(struct smbd_server_connection *sconn, char *buf,
369 /* Ignore non-session messages. */
374 if (srv_trans_enc_ctx) {
375 return common_encrypt_buffer(srv_trans_enc_ctx->es, buf, buf_out);
377 /* Not encrypting. */
381 /******************************************************************************
382 Do the gss encryption negotiation. Parameters are in/out.
383 Until success we do everything on the partial enc ctx.
384 ******************************************************************************/
386 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
387 static NTSTATUS srv_enc_spnego_gss_negotiate(const struct tsocket_address *remote_address,
388 unsigned char **ppdata,
395 gss_buffer_desc in_buf, out_buf;
396 struct smb_tran_enc_state_gss *gss_state;
397 DATA_BLOB auth_reply = data_blob_null;
398 DATA_BLOB response = data_blob_null;
401 if (!partial_srv_trans_enc_ctx) {
402 status = make_srv_encryption_context(remote_address,
404 &partial_srv_trans_enc_ctx);
405 if (!NT_STATUS_IS_OK(status)) {
410 gss_state = partial_srv_trans_enc_ctx->es->s.gss_state;
412 in_buf.value = secblob.data;
413 in_buf.length = secblob.length;
415 out_buf.value = NULL;
420 ret = gss_accept_sec_context(&min,
424 GSS_C_NO_CHANNEL_BINDINGS,
426 NULL, /* Ignore oids. */
427 &out_buf, /* To return. */
429 NULL, /* Ingore time. */
430 NULL); /* Ignore delegated creds. */
433 status = gss_err_to_ntstatus(ret, min);
434 if (ret != GSS_S_COMPLETE && ret != GSS_S_CONTINUE_NEEDED) {
438 /* Ensure we've got sign+seal available. */
439 if (ret == GSS_S_COMPLETE) {
440 if ((flags & (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) !=
441 (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) {
442 DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
443 "for SMB sealing.\n"));
444 gss_release_buffer(&min, &out_buf);
445 return NT_STATUS_ACCESS_DENIED;
449 auth_reply = data_blob(out_buf.value, out_buf.length);
450 gss_release_buffer(&min, &out_buf);
452 /* Wrap in SPNEGO. */
453 response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, OID_KERBEROS5);
454 data_blob_free(&auth_reply);
457 *ppdata = (unsigned char *)memdup(response.data, response.length);
458 if ((*ppdata) == NULL && response.length > 0) {
459 status = NT_STATUS_NO_MEMORY;
461 *p_data_size = response.length;
463 data_blob_free(&response);
469 /******************************************************************************
470 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
471 Until success we do everything on the partial enc ctx.
472 ******************************************************************************/
474 static NTSTATUS srv_enc_ntlm_negotiate(const struct tsocket_address *remote_address,
475 unsigned char **ppdata,
481 DATA_BLOB chal = data_blob_null;
482 DATA_BLOB response = data_blob_null;
484 status = make_srv_encryption_context(remote_address,
486 &partial_srv_trans_enc_ctx);
487 if (!NT_STATUS_IS_OK(status)) {
491 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state,
495 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
499 response = spnego_gen_auth_response(talloc_tos(), &chal, status, OID_NTLMSSP);
500 data_blob_free(&chal);
502 /* Return the raw blob. */
507 *ppdata = (unsigned char *)memdup(response.data, response.length);
508 if ((*ppdata) == NULL && response.length > 0) {
509 status = NT_STATUS_NO_MEMORY;
511 *p_data_size = response.length;
512 data_blob_free(&response);
517 /******************************************************************************
518 Do the SPNEGO encryption negotiation. Parameters are in/out.
519 Based off code in smbd/sesssionsetup.c
520 Until success we do everything on the partial enc ctx.
521 ******************************************************************************/
523 static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
524 unsigned char **ppdata,
526 unsigned char **pparam,
527 size_t *p_param_size)
530 DATA_BLOB blob = data_blob_null;
531 DATA_BLOB secblob = data_blob_null;
532 char *kerb_mech = NULL;
534 blob = data_blob_const(*ppdata, *p_data_size);
536 status = parse_spnego_mechanisms(talloc_tos(), blob, &secblob, &kerb_mech);
537 if (!NT_STATUS_IS_OK(status)) {
538 return nt_status_squash(status);
541 /* We should have no partial context at this point. */
543 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
546 TALLOC_FREE(kerb_mech);
548 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
549 status = srv_enc_spnego_gss_negotiate(conn->sconn->remote_address,
554 /* Currently we don't SPNEGO negotiate
555 * back to NTLMSSP as we do in sessionsetupX. We should... */
556 return NT_STATUS_LOGON_FAILURE;
559 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
566 data_blob_free(&secblob);
568 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
569 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
570 return nt_status_squash(status);
573 if (NT_STATUS_IS_OK(status)) {
574 /* Return the context we're using for this encryption state. */
575 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
576 return NT_STATUS_NO_MEMORY;
578 SSVAL(*pparam,0,partial_srv_trans_enc_ctx->es->enc_ctx_num);
585 /******************************************************************************
586 Complete a SPNEGO encryption negotiation. Parameters are in/out.
587 We only get this for a NTLM auth second stage.
588 ******************************************************************************/
590 static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
591 unsigned char **ppdata,
593 unsigned char **pparam,
594 size_t *p_param_size)
597 DATA_BLOB blob = data_blob_null;
598 DATA_BLOB auth = data_blob_null;
599 DATA_BLOB auth_reply = data_blob_null;
600 DATA_BLOB response = data_blob_null;
601 struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
603 /* We must have a partial context here. */
605 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
606 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
607 return NT_STATUS_INVALID_PARAMETER;
610 blob = data_blob_const(*ppdata, *p_data_size);
611 if (!spnego_parse_auth(talloc_tos(), blob, &auth)) {
612 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
613 return NT_STATUS_INVALID_PARAMETER;
616 status = auth_ntlmssp_update(ec->auth_ntlmssp_state, talloc_tos(), auth, &auth_reply);
617 data_blob_free(&auth);
623 * This field SHALL only be present in the first reply from the
625 * So set mechOID to NULL here.
628 response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, NULL);
629 data_blob_free(&auth_reply);
631 if (NT_STATUS_IS_OK(status)) {
632 /* Return the context we're using for this encryption state. */
633 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
634 return NT_STATUS_NO_MEMORY;
636 SSVAL(*pparam,0,ec->es->enc_ctx_num);
641 *ppdata = (unsigned char *)memdup(response.data, response.length);
642 if ((*ppdata) == NULL && response.length > 0)
643 return NT_STATUS_NO_MEMORY;
644 *p_data_size = response.length;
645 data_blob_free(&response);
649 /******************************************************************************
650 Raw NTLM encryption negotiation. Parameters are in/out.
651 This function does both steps.
652 ******************************************************************************/
654 static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
655 unsigned char **ppdata,
657 unsigned char **pparam,
658 size_t *p_param_size)
661 DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
662 DATA_BLOB response = data_blob_null;
663 struct smb_srv_trans_enc_ctx *ec;
665 if (!partial_srv_trans_enc_ctx) {
666 /* This is the initial step. */
667 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
672 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
673 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
674 return nt_status_squash(status);
679 ec = partial_srv_trans_enc_ctx;
680 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
681 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
682 return NT_STATUS_INVALID_PARAMETER;
686 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state,
690 if (NT_STATUS_IS_OK(status)) {
691 /* Return the context we're using for this encryption state. */
692 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
693 return NT_STATUS_NO_MEMORY;
695 SSVAL(*pparam,0,ec->es->enc_ctx_num);
699 /* Return the raw blob. */
701 *ppdata = (unsigned char *)memdup(response.data, response.length);
702 if ((*ppdata) == NULL && response.length > 0)
703 return NT_STATUS_NO_MEMORY;
704 *p_data_size = response.length;
705 data_blob_free(&response);
709 /******************************************************************************
710 Do the SPNEGO encryption negotiation. Parameters are in/out.
711 ******************************************************************************/
713 NTSTATUS srv_request_encryption_setup(connection_struct *conn,
714 unsigned char **ppdata,
716 unsigned char **pparam,
717 size_t *p_param_size)
719 unsigned char *pdata = *ppdata;
724 if (*p_data_size < 1) {
725 return NT_STATUS_INVALID_PARAMETER;
728 if (pdata[0] == ASN1_APPLICATION(0)) {
729 /* its a negTokenTarg packet */
730 return srv_enc_spnego_negotiate(conn, ppdata, p_data_size, pparam, p_param_size);
733 if (pdata[0] == ASN1_CONTEXT(1)) {
734 /* It's an auth packet */
735 return srv_enc_spnego_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
738 /* Maybe it's a raw unwrapped auth ? */
739 if (*p_data_size < 7) {
740 return NT_STATUS_INVALID_PARAMETER;
743 if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
744 return srv_enc_raw_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
747 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
749 return NT_STATUS_LOGON_FAILURE;
752 /******************************************************************************
753 Negotiation was successful - turn on server-side encryption.
754 ******************************************************************************/
756 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
758 if (!ec || !ec->es) {
759 return NT_STATUS_LOGON_FAILURE;
762 if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
763 if (!auth_ntlmssp_negotiated_sign((ec->auth_ntlmssp_state))) {
764 return NT_STATUS_INVALID_PARAMETER;
767 if (!auth_ntlmssp_negotiated_seal((ec->auth_ntlmssp_state))) {
768 return NT_STATUS_INVALID_PARAMETER;
771 /* Todo - check gssapi case. */
776 /******************************************************************************
777 Negotiation was successful - turn on server-side encryption.
778 ******************************************************************************/
780 NTSTATUS srv_encryption_start(connection_struct *conn)
784 /* Check that we are really doing sign+seal. */
785 status = check_enc_good(partial_srv_trans_enc_ctx);
786 if (!NT_STATUS_IS_OK(status)) {
789 /* Throw away the context we're using currently (if any). */
790 srv_free_encryption_context(&srv_trans_enc_ctx);
792 /* Steal the partial pointer. Deliberate shallow copy. */
793 srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
794 srv_trans_enc_ctx->es->enc_on = true;
796 partial_srv_trans_enc_ctx = NULL;
798 DEBUG(1,("srv_encryption_start: context negotiated\n"));
802 /******************************************************************************
803 Shutdown all server contexts.
804 ******************************************************************************/
806 void server_encryption_shutdown(struct smbd_server_connection *sconn)
808 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
809 srv_free_encryption_context(&srv_trans_enc_ctx);