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 "../libcli/auth/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(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(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(char *buf, char **buf_out)
368 /* Ignore non-session messages. */
373 if (srv_trans_enc_ctx) {
374 return common_encrypt_buffer(srv_trans_enc_ctx->es, buf, buf_out);
376 /* Not encrypting. */
380 /******************************************************************************
381 Do the gss encryption negotiation. Parameters are in/out.
382 Until success we do everything on the partial enc ctx.
383 ******************************************************************************/
385 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
386 static NTSTATUS srv_enc_spnego_gss_negotiate(const struct tsocket_address *remote_address,
387 unsigned char **ppdata,
394 gss_buffer_desc in_buf, out_buf;
395 struct smb_tran_enc_state_gss *gss_state;
396 DATA_BLOB auth_reply = data_blob_null;
397 DATA_BLOB response = data_blob_null;
400 if (!partial_srv_trans_enc_ctx) {
401 status = make_srv_encryption_context(remote_address,
403 &partial_srv_trans_enc_ctx);
404 if (!NT_STATUS_IS_OK(status)) {
409 gss_state = partial_srv_trans_enc_ctx->es->s.gss_state;
411 in_buf.value = secblob.data;
412 in_buf.length = secblob.length;
414 out_buf.value = NULL;
419 ret = gss_accept_sec_context(&min,
423 GSS_C_NO_CHANNEL_BINDINGS,
425 NULL, /* Ignore oids. */
426 &out_buf, /* To return. */
428 NULL, /* Ingore time. */
429 NULL); /* Ignore delegated creds. */
432 status = gss_err_to_ntstatus(ret, min);
433 if (ret != GSS_S_COMPLETE && ret != GSS_S_CONTINUE_NEEDED) {
437 /* Ensure we've got sign+seal available. */
438 if (ret == GSS_S_COMPLETE) {
439 if ((flags & (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) !=
440 (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) {
441 DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
442 "for SMB sealing.\n"));
443 gss_release_buffer(&min, &out_buf);
444 return NT_STATUS_ACCESS_DENIED;
448 auth_reply = data_blob(out_buf.value, out_buf.length);
449 gss_release_buffer(&min, &out_buf);
451 /* Wrap in SPNEGO. */
452 response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, OID_KERBEROS5);
453 data_blob_free(&auth_reply);
456 *ppdata = (unsigned char *)memdup(response.data, response.length);
457 if ((*ppdata) == NULL && response.length > 0) {
458 status = NT_STATUS_NO_MEMORY;
460 *p_data_size = response.length;
462 data_blob_free(&response);
468 /******************************************************************************
469 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
470 Until success we do everything on the partial enc ctx.
471 ******************************************************************************/
473 static NTSTATUS srv_enc_ntlm_negotiate(const struct tsocket_address *remote_address,
474 unsigned char **ppdata,
480 DATA_BLOB chal = data_blob_null;
481 DATA_BLOB response = data_blob_null;
483 status = make_srv_encryption_context(remote_address,
485 &partial_srv_trans_enc_ctx);
486 if (!NT_STATUS_IS_OK(status)) {
490 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state,
491 partial_srv_trans_enc_ctx->auth_ntlmssp_state,
494 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
498 response = spnego_gen_auth_response(talloc_tos(), &chal, status, OID_NTLMSSP);
499 data_blob_free(&chal);
501 /* Return the raw blob. */
506 *ppdata = (unsigned char *)memdup(response.data, response.length);
507 if ((*ppdata) == NULL && response.length > 0) {
508 status = NT_STATUS_NO_MEMORY;
510 *p_data_size = response.length;
511 data_blob_free(&response);
516 /******************************************************************************
517 Do the SPNEGO encryption negotiation. Parameters are in/out.
518 Based off code in smbd/sesssionsetup.c
519 Until success we do everything on the partial enc ctx.
520 ******************************************************************************/
522 static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
523 unsigned char **ppdata,
525 unsigned char **pparam,
526 size_t *p_param_size)
529 DATA_BLOB blob = data_blob_null;
530 DATA_BLOB secblob = data_blob_null;
531 char *kerb_mech = NULL;
533 blob = data_blob_const(*ppdata, *p_data_size);
535 status = parse_spnego_mechanisms(talloc_tos(), blob, &secblob, &kerb_mech);
536 if (!NT_STATUS_IS_OK(status)) {
537 return nt_status_squash(status);
540 /* We should have no partial context at this point. */
542 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
545 TALLOC_FREE(kerb_mech);
547 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
548 status = srv_enc_spnego_gss_negotiate(conn->sconn->remote_address,
553 /* Currently we don't SPNEGO negotiate
554 * back to NTLMSSP as we do in sessionsetupX. We should... */
555 return NT_STATUS_LOGON_FAILURE;
558 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
565 data_blob_free(&secblob);
567 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
568 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
569 return nt_status_squash(status);
572 if (NT_STATUS_IS_OK(status)) {
573 /* Return the context we're using for this encryption state. */
574 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
575 return NT_STATUS_NO_MEMORY;
577 SSVAL(*pparam,0,partial_srv_trans_enc_ctx->es->enc_ctx_num);
584 /******************************************************************************
585 Complete a SPNEGO encryption negotiation. Parameters are in/out.
586 We only get this for a NTLM auth second stage.
587 ******************************************************************************/
589 static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
590 unsigned char **ppdata,
592 unsigned char **pparam,
593 size_t *p_param_size)
596 DATA_BLOB blob = data_blob_null;
597 DATA_BLOB auth = data_blob_null;
598 DATA_BLOB auth_reply = data_blob_null;
599 DATA_BLOB response = data_blob_null;
600 struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
602 /* We must have a partial context here. */
604 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
605 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
606 return NT_STATUS_INVALID_PARAMETER;
609 blob = data_blob_const(*ppdata, *p_data_size);
610 if (!spnego_parse_auth(talloc_tos(), blob, &auth)) {
611 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
612 return NT_STATUS_INVALID_PARAMETER;
615 status = auth_ntlmssp_update(ec->auth_ntlmssp_state, talloc_tos(), auth, &auth_reply);
616 data_blob_free(&auth);
622 * This field SHALL only be present in the first reply from the
624 * So set mechOID to NULL here.
627 response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, NULL);
628 data_blob_free(&auth_reply);
630 if (NT_STATUS_IS_OK(status)) {
631 /* Return the context we're using for this encryption state. */
632 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
633 return NT_STATUS_NO_MEMORY;
635 SSVAL(*pparam,0,ec->es->enc_ctx_num);
640 *ppdata = (unsigned char *)memdup(response.data, response.length);
641 if ((*ppdata) == NULL && response.length > 0)
642 return NT_STATUS_NO_MEMORY;
643 *p_data_size = response.length;
644 data_blob_free(&response);
648 /******************************************************************************
649 Raw NTLM encryption negotiation. Parameters are in/out.
650 This function does both steps.
651 ******************************************************************************/
653 static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
654 unsigned char **ppdata,
656 unsigned char **pparam,
657 size_t *p_param_size)
660 DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
661 DATA_BLOB response = data_blob_null;
662 struct smb_srv_trans_enc_ctx *ec;
664 if (!partial_srv_trans_enc_ctx) {
665 /* This is the initial step. */
666 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
671 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
672 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
673 return nt_status_squash(status);
678 ec = partial_srv_trans_enc_ctx;
679 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
680 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
681 return NT_STATUS_INVALID_PARAMETER;
685 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state,
689 if (NT_STATUS_IS_OK(status)) {
690 /* Return the context we're using for this encryption state. */
691 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
692 return NT_STATUS_NO_MEMORY;
694 SSVAL(*pparam,0,ec->es->enc_ctx_num);
698 /* Return the raw blob. */
700 *ppdata = (unsigned char *)memdup(response.data, response.length);
701 if ((*ppdata) == NULL && response.length > 0)
702 return NT_STATUS_NO_MEMORY;
703 *p_data_size = response.length;
704 data_blob_free(&response);
708 /******************************************************************************
709 Do the SPNEGO encryption negotiation. Parameters are in/out.
710 ******************************************************************************/
712 NTSTATUS srv_request_encryption_setup(connection_struct *conn,
713 unsigned char **ppdata,
715 unsigned char **pparam,
716 size_t *p_param_size)
718 unsigned char *pdata = *ppdata;
723 if (*p_data_size < 1) {
724 return NT_STATUS_INVALID_PARAMETER;
727 if (pdata[0] == ASN1_APPLICATION(0)) {
728 /* its a negTokenTarg packet */
729 return srv_enc_spnego_negotiate(conn, ppdata, p_data_size, pparam, p_param_size);
732 if (pdata[0] == ASN1_CONTEXT(1)) {
733 /* It's an auth packet */
734 return srv_enc_spnego_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
737 /* Maybe it's a raw unwrapped auth ? */
738 if (*p_data_size < 7) {
739 return NT_STATUS_INVALID_PARAMETER;
742 if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
743 return srv_enc_raw_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
746 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
748 return NT_STATUS_LOGON_FAILURE;
751 /******************************************************************************
752 Negotiation was successful - turn on server-side encryption.
753 ******************************************************************************/
755 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
757 if (!ec || !ec->es) {
758 return NT_STATUS_LOGON_FAILURE;
761 if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
762 if (!auth_ntlmssp_negotiated_sign((ec->auth_ntlmssp_state))) {
763 return NT_STATUS_INVALID_PARAMETER;
766 if (!auth_ntlmssp_negotiated_seal((ec->auth_ntlmssp_state))) {
767 return NT_STATUS_INVALID_PARAMETER;
770 /* Todo - check gssapi case. */
775 /******************************************************************************
776 Negotiation was successful - turn on server-side encryption.
777 ******************************************************************************/
779 NTSTATUS srv_encryption_start(connection_struct *conn)
783 /* Check that we are really doing sign+seal. */
784 status = check_enc_good(partial_srv_trans_enc_ctx);
785 if (!NT_STATUS_IS_OK(status)) {
788 /* Throw away the context we're using currently (if any). */
789 srv_free_encryption_context(&srv_trans_enc_ctx);
791 /* Steal the partial pointer. Deliberate shallow copy. */
792 srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
793 srv_trans_enc_ctx->es->enc_on = true;
795 partial_srv_trans_enc_ctx = NULL;
797 DEBUG(1,("srv_encryption_start: context negotiated\n"));
801 /******************************************************************************
802 Shutdown all server contexts.
803 ******************************************************************************/
805 void server_encryption_shutdown(void)
807 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
808 srv_free_encryption_context(&srv_trans_enc_ctx);