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 uint16_t srv_enc_ctx(void)
51 return srv_trans_enc_ctx->es->enc_ctx_num;
54 /******************************************************************************
55 Is this an incoming encrypted packet ?
56 ******************************************************************************/
58 bool is_encrypted_packet(const uint8_t *inbuf)
63 /* Ignore non-session messages or non 0xFF'E' messages. */
65 || (smb_len(inbuf) < 8)
66 || !(inbuf[4] == 0xFF && inbuf[5] == 'E')) {
70 status = get_enc_ctx_num(inbuf, &enc_num);
71 if (!NT_STATUS_IS_OK(status)) {
75 /* Encrypted messages are 0xFF'E'<ctx> */
76 if (srv_trans_enc_ctx && enc_num == srv_enc_ctx()) {
82 /******************************************************************************
83 Create an auth_ntlmssp_state and ensure pointer copy is correct.
84 ******************************************************************************/
86 static NTSTATUS make_auth_ntlmssp(const struct tsocket_address *remote_address,
87 struct smb_srv_trans_enc_ctx *ec)
89 NTSTATUS status = auth_ntlmssp_start(remote_address,
90 &ec->auth_ntlmssp_state);
91 if (!NT_STATUS_IS_OK(status)) {
92 return nt_status_squash(status);
95 auth_ntlmssp_want_feature(ec->auth_ntlmssp_state, NTLMSSP_FEATURE_SEAL);
98 * We must remember to update the pointer copy for the common
99 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
101 ec->es->s.auth_ntlmssp_state = ec->auth_ntlmssp_state;
105 /******************************************************************************
106 Destroy an auth_ntlmssp_state and ensure pointer copy is correct.
107 ******************************************************************************/
109 static void destroy_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
112 * We must remember to update the pointer copy for the common
113 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
116 if (ec->auth_ntlmssp_state) {
117 TALLOC_FREE(ec->auth_ntlmssp_state);
118 /* The auth_ntlmssp_end killed this already. */
119 ec->es->s.auth_ntlmssp_state = NULL;
123 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
125 /******************************************************************************
127 ******************************************************************************/
129 static NTSTATUS get_srv_gss_creds(const char *service,
131 gss_cred_usage_t cred_type,
132 gss_cred_id_t *p_srv_cred)
137 gss_buffer_desc input_name;
138 char *host_princ_s = NULL;
139 NTSTATUS status = NT_STATUS_OK;
141 gss_OID_desc nt_hostbased_service =
142 {10, discard_const_p(char, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
144 if (asprintf(&host_princ_s, "%s@%s", service, name) == -1) {
145 return NT_STATUS_NO_MEMORY;
148 input_name.value = host_princ_s;
149 input_name.length = strlen(host_princ_s) + 1;
151 ret = gss_import_name(&min,
153 &nt_hostbased_service,
156 DEBUG(10,("get_srv_gss_creds: imported name %s\n",
159 if (ret != GSS_S_COMPLETE) {
160 SAFE_FREE(host_princ_s);
161 return map_nt_error_from_gss(ret, min);
165 * We're accessing the krb5.keytab file here.
166 * ensure we have permissions to do so.
170 ret = gss_acquire_cred(&min,
180 if (ret != GSS_S_COMPLETE) {
181 ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
182 DEBUG(10,("get_srv_gss_creds: gss_acquire_cred failed with %s\n",
184 status = map_nt_error_from_gss(ret, min);
187 SAFE_FREE(host_princ_s);
188 gss_release_name(&min, &srv_name);
192 /******************************************************************************
194 Try and get the cifs/server@realm principal first, then fall back to
196 ******************************************************************************/
198 static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
201 gss_cred_id_t srv_cred;
204 name_to_fqdn(fqdn, lp_netbios_name());
207 status = get_srv_gss_creds("cifs", fqdn, GSS_C_ACCEPT, &srv_cred);
208 if (!NT_STATUS_IS_OK(status)) {
209 status = get_srv_gss_creds("host", fqdn, GSS_C_ACCEPT, &srv_cred);
210 if (!NT_STATUS_IS_OK(status)) {
211 return nt_status_squash(status);
215 ec->es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
216 if (!ec->es->s.gss_state) {
218 gss_release_cred(&min, &srv_cred);
219 return NT_STATUS_NO_MEMORY;
221 ZERO_STRUCTP(ec->es->s.gss_state);
222 ec->es->s.gss_state->creds = srv_cred;
224 /* No context yet. */
225 ec->es->s.gss_state->gss_ctx = GSS_C_NO_CONTEXT;
231 /******************************************************************************
232 Shutdown a server encryption context.
233 ******************************************************************************/
235 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
237 struct smb_srv_trans_enc_ctx *ec = *pp_ec;
244 switch (ec->es->smb_enc_type) {
245 case SMB_TRANS_ENC_NTLM:
246 destroy_auth_ntlmssp(ec);
248 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
249 case SMB_TRANS_ENC_GSS:
253 common_free_encryption_state(&ec->es);
260 /******************************************************************************
261 Create a server encryption context.
262 ******************************************************************************/
264 static NTSTATUS make_srv_encryption_context(const struct tsocket_address *remote_address,
265 enum smb_trans_enc_type smb_enc_type,
266 struct smb_srv_trans_enc_ctx **pp_ec)
268 struct smb_srv_trans_enc_ctx *ec;
272 ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
274 return NT_STATUS_NO_MEMORY;
276 ZERO_STRUCTP(partial_srv_trans_enc_ctx);
277 ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
280 return NT_STATUS_NO_MEMORY;
282 ZERO_STRUCTP(ec->es);
283 ec->es->smb_enc_type = smb_enc_type;
284 switch (smb_enc_type) {
285 case SMB_TRANS_ENC_NTLM:
287 NTSTATUS status = make_auth_ntlmssp(remote_address,
289 if (!NT_STATUS_IS_OK(status)) {
290 srv_free_encryption_context(&ec);
296 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
297 case SMB_TRANS_ENC_GSS:
298 /* Acquire our credentials by calling gss_acquire_cred here. */
300 NTSTATUS status = make_auth_gss(ec);
301 if (!NT_STATUS_IS_OK(status)) {
302 srv_free_encryption_context(&ec);
309 srv_free_encryption_context(&ec);
310 return NT_STATUS_INVALID_PARAMETER;
316 /******************************************************************************
317 Free an encryption-allocated buffer.
318 ******************************************************************************/
320 void srv_free_enc_buffer(char *buf)
322 /* We know this is an smb buffer, and we
323 * didn't malloc, only copy, for a keepalive,
324 * so ignore non-session messages. */
330 if (srv_trans_enc_ctx) {
331 common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
335 /******************************************************************************
336 Decrypt an incoming buffer.
337 ******************************************************************************/
339 NTSTATUS srv_decrypt_buffer(char *buf)
341 /* Ignore non-session messages. */
346 if (srv_trans_enc_ctx) {
347 return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
353 /******************************************************************************
354 Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
355 ******************************************************************************/
357 NTSTATUS srv_encrypt_buffer(char *buf, char **buf_out)
361 /* Ignore non-session messages. */
366 if (srv_trans_enc_ctx) {
367 return common_encrypt_buffer(srv_trans_enc_ctx->es, buf, buf_out);
369 /* Not encrypting. */
373 /******************************************************************************
374 Do the gss encryption negotiation. Parameters are in/out.
375 Until success we do everything on the partial enc ctx.
376 ******************************************************************************/
378 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
379 static NTSTATUS srv_enc_spnego_gss_negotiate(const struct tsocket_address *remote_address,
380 unsigned char **ppdata,
387 gss_buffer_desc in_buf, out_buf;
388 struct smb_tran_enc_state_gss *gss_state;
389 DATA_BLOB auth_reply = data_blob_null;
390 DATA_BLOB response = data_blob_null;
393 if (!partial_srv_trans_enc_ctx) {
394 status = make_srv_encryption_context(remote_address,
396 &partial_srv_trans_enc_ctx);
397 if (!NT_STATUS_IS_OK(status)) {
402 gss_state = partial_srv_trans_enc_ctx->es->s.gss_state;
404 in_buf.value = secblob.data;
405 in_buf.length = secblob.length;
407 out_buf.value = NULL;
412 ret = gss_accept_sec_context(&min,
416 GSS_C_NO_CHANNEL_BINDINGS,
418 NULL, /* Ignore oids. */
419 &out_buf, /* To return. */
421 NULL, /* Ingore time. */
422 NULL); /* Ignore delegated creds. */
425 status = gss_err_to_ntstatus(ret, min);
426 if (ret != GSS_S_COMPLETE && ret != GSS_S_CONTINUE_NEEDED) {
430 /* Ensure we've got sign+seal available. */
431 if (ret == GSS_S_COMPLETE) {
432 if ((flags & (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) !=
433 (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) {
434 DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
435 "for SMB sealing.\n"));
436 gss_release_buffer(&min, &out_buf);
437 return NT_STATUS_ACCESS_DENIED;
441 auth_reply = data_blob(out_buf.value, out_buf.length);
442 gss_release_buffer(&min, &out_buf);
444 /* Wrap in SPNEGO. */
445 response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, OID_KERBEROS5);
446 data_blob_free(&auth_reply);
449 *ppdata = (unsigned char *)memdup(response.data, response.length);
450 if ((*ppdata) == NULL && response.length > 0) {
451 status = NT_STATUS_NO_MEMORY;
453 *p_data_size = response.length;
455 data_blob_free(&response);
461 /******************************************************************************
462 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
463 Until success we do everything on the partial enc ctx.
464 ******************************************************************************/
466 static NTSTATUS srv_enc_ntlm_negotiate(const struct tsocket_address *remote_address,
467 unsigned char **ppdata,
473 DATA_BLOB chal = data_blob_null;
474 DATA_BLOB response = data_blob_null;
476 status = make_srv_encryption_context(remote_address,
478 &partial_srv_trans_enc_ctx);
479 if (!NT_STATUS_IS_OK(status)) {
483 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state,
484 partial_srv_trans_enc_ctx->auth_ntlmssp_state,
487 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
491 response = spnego_gen_auth_response(talloc_tos(), &chal, status, OID_NTLMSSP);
492 data_blob_free(&chal);
494 /* Return the raw blob. */
499 *ppdata = (unsigned char *)memdup(response.data, response.length);
500 if ((*ppdata) == NULL && response.length > 0) {
501 status = NT_STATUS_NO_MEMORY;
503 *p_data_size = response.length;
504 data_blob_free(&response);
509 /******************************************************************************
510 Do the SPNEGO encryption negotiation. Parameters are in/out.
511 Based off code in smbd/sesssionsetup.c
512 Until success we do everything on the partial enc ctx.
513 ******************************************************************************/
515 static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
516 unsigned char **ppdata,
518 unsigned char **pparam,
519 size_t *p_param_size)
522 DATA_BLOB blob = data_blob_null;
523 DATA_BLOB secblob = data_blob_null;
524 char *kerb_mech = NULL;
526 blob = data_blob_const(*ppdata, *p_data_size);
528 status = parse_spnego_mechanisms(talloc_tos(), blob, &secblob, &kerb_mech);
529 if (!NT_STATUS_IS_OK(status)) {
530 return nt_status_squash(status);
533 /* We should have no partial context at this point. */
535 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
538 TALLOC_FREE(kerb_mech);
540 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
541 status = srv_enc_spnego_gss_negotiate(conn->sconn->remote_address,
546 /* Currently we don't SPNEGO negotiate
547 * back to NTLMSSP as we do in sessionsetupX. We should... */
548 return NT_STATUS_LOGON_FAILURE;
551 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
558 data_blob_free(&secblob);
560 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
561 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
562 return nt_status_squash(status);
565 if (NT_STATUS_IS_OK(status)) {
566 /* Return the context we're using for this encryption state. */
567 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
568 return NT_STATUS_NO_MEMORY;
570 SSVAL(*pparam,0,partial_srv_trans_enc_ctx->es->enc_ctx_num);
577 /******************************************************************************
578 Complete a SPNEGO encryption negotiation. Parameters are in/out.
579 We only get this for a NTLM auth second stage.
580 ******************************************************************************/
582 static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
583 unsigned char **ppdata,
585 unsigned char **pparam,
586 size_t *p_param_size)
589 DATA_BLOB blob = data_blob_null;
590 DATA_BLOB auth = data_blob_null;
591 DATA_BLOB auth_reply = data_blob_null;
592 DATA_BLOB response = data_blob_null;
593 struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
595 /* We must have a partial context here. */
597 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
598 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
599 return NT_STATUS_INVALID_PARAMETER;
602 blob = data_blob_const(*ppdata, *p_data_size);
603 if (!spnego_parse_auth(talloc_tos(), blob, &auth)) {
604 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
605 return NT_STATUS_INVALID_PARAMETER;
608 status = auth_ntlmssp_update(ec->auth_ntlmssp_state, talloc_tos(), auth, &auth_reply);
609 data_blob_free(&auth);
615 * This field SHALL only be present in the first reply from the
617 * So set mechOID to NULL here.
620 response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, NULL);
621 data_blob_free(&auth_reply);
623 if (NT_STATUS_IS_OK(status)) {
624 /* Return the context we're using for this encryption state. */
625 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
626 return NT_STATUS_NO_MEMORY;
628 SSVAL(*pparam,0,ec->es->enc_ctx_num);
633 *ppdata = (unsigned char *)memdup(response.data, response.length);
634 if ((*ppdata) == NULL && response.length > 0)
635 return NT_STATUS_NO_MEMORY;
636 *p_data_size = response.length;
637 data_blob_free(&response);
641 /******************************************************************************
642 Raw NTLM encryption negotiation. Parameters are in/out.
643 This function does both steps.
644 ******************************************************************************/
646 static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
647 unsigned char **ppdata,
649 unsigned char **pparam,
650 size_t *p_param_size)
653 DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
654 DATA_BLOB response = data_blob_null;
655 struct smb_srv_trans_enc_ctx *ec;
657 if (!partial_srv_trans_enc_ctx) {
658 /* This is the initial step. */
659 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
664 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
665 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
666 return nt_status_squash(status);
671 ec = partial_srv_trans_enc_ctx;
672 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
673 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
674 return NT_STATUS_INVALID_PARAMETER;
678 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state,
682 if (NT_STATUS_IS_OK(status)) {
683 /* Return the context we're using for this encryption state. */
684 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
685 return NT_STATUS_NO_MEMORY;
687 SSVAL(*pparam,0,ec->es->enc_ctx_num);
691 /* Return the raw blob. */
693 *ppdata = (unsigned char *)memdup(response.data, response.length);
694 if ((*ppdata) == NULL && response.length > 0)
695 return NT_STATUS_NO_MEMORY;
696 *p_data_size = response.length;
697 data_blob_free(&response);
701 /******************************************************************************
702 Do the SPNEGO encryption negotiation. Parameters are in/out.
703 ******************************************************************************/
705 NTSTATUS srv_request_encryption_setup(connection_struct *conn,
706 unsigned char **ppdata,
708 unsigned char **pparam,
709 size_t *p_param_size)
711 unsigned char *pdata = *ppdata;
716 if (*p_data_size < 1) {
717 return NT_STATUS_INVALID_PARAMETER;
720 if (pdata[0] == ASN1_APPLICATION(0)) {
721 /* its a negTokenTarg packet */
722 return srv_enc_spnego_negotiate(conn, ppdata, p_data_size, pparam, p_param_size);
725 if (pdata[0] == ASN1_CONTEXT(1)) {
726 /* It's an auth packet */
727 return srv_enc_spnego_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
730 /* Maybe it's a raw unwrapped auth ? */
731 if (*p_data_size < 7) {
732 return NT_STATUS_INVALID_PARAMETER;
735 if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
736 return srv_enc_raw_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
739 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
741 return NT_STATUS_LOGON_FAILURE;
744 /******************************************************************************
745 Negotiation was successful - turn on server-side encryption.
746 ******************************************************************************/
748 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
750 if (!ec || !ec->es) {
751 return NT_STATUS_LOGON_FAILURE;
754 if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
755 if (!auth_ntlmssp_negotiated_sign((ec->auth_ntlmssp_state))) {
756 return NT_STATUS_INVALID_PARAMETER;
759 if (!auth_ntlmssp_negotiated_seal((ec->auth_ntlmssp_state))) {
760 return NT_STATUS_INVALID_PARAMETER;
763 /* Todo - check gssapi case. */
768 /******************************************************************************
769 Negotiation was successful - turn on server-side encryption.
770 ******************************************************************************/
772 NTSTATUS srv_encryption_start(connection_struct *conn)
776 /* Check that we are really doing sign+seal. */
777 status = check_enc_good(partial_srv_trans_enc_ctx);
778 if (!NT_STATUS_IS_OK(status)) {
781 /* Throw away the context we're using currently (if any). */
782 srv_free_encryption_context(&srv_trans_enc_ctx);
784 /* Steal the partial pointer. Deliberate shallow copy. */
785 srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
786 srv_trans_enc_ctx->es->enc_on = true;
788 partial_srv_trans_enc_ctx = NULL;
790 DEBUG(1,("srv_encryption_start: context negotiated\n"));
794 /******************************************************************************
795 Shutdown all server contexts.
796 ******************************************************************************/
798 void server_encryption_shutdown(void)
800 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
801 srv_free_encryption_context(&srv_trans_enc_ctx);