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_prepare(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);
97 status = auth_ntlmssp_start(ec->auth_ntlmssp_state);
99 if (!NT_STATUS_IS_OK(status)) {
100 return nt_status_squash(status);
104 * We must remember to update the pointer copy for the common
105 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
107 ec->es->s.auth_ntlmssp_state = ec->auth_ntlmssp_state;
111 /******************************************************************************
112 Destroy an auth_ntlmssp_state and ensure pointer copy is correct.
113 ******************************************************************************/
115 static void destroy_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
118 * We must remember to update the pointer copy for the common
119 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
122 if (ec->auth_ntlmssp_state) {
123 TALLOC_FREE(ec->auth_ntlmssp_state);
124 /* The auth_ntlmssp_end killed this already. */
125 ec->es->s.auth_ntlmssp_state = NULL;
129 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
131 /******************************************************************************
133 ******************************************************************************/
135 static NTSTATUS get_srv_gss_creds(const char *service,
137 gss_cred_usage_t cred_type,
138 gss_cred_id_t *p_srv_cred)
143 gss_buffer_desc input_name;
144 char *host_princ_s = NULL;
145 NTSTATUS status = NT_STATUS_OK;
147 gss_OID_desc nt_hostbased_service =
148 {10, discard_const_p(char, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
150 if (asprintf(&host_princ_s, "%s@%s", service, name) == -1) {
151 return NT_STATUS_NO_MEMORY;
154 input_name.value = host_princ_s;
155 input_name.length = strlen(host_princ_s) + 1;
157 ret = gss_import_name(&min,
159 &nt_hostbased_service,
162 DEBUG(10,("get_srv_gss_creds: imported name %s\n",
165 if (ret != GSS_S_COMPLETE) {
166 SAFE_FREE(host_princ_s);
167 return map_nt_error_from_gss(ret, min);
171 * We're accessing the krb5.keytab file here.
172 * ensure we have permissions to do so.
176 ret = gss_acquire_cred(&min,
186 if (ret != GSS_S_COMPLETE) {
187 ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
188 DEBUG(10,("get_srv_gss_creds: gss_acquire_cred failed with %s\n",
190 status = map_nt_error_from_gss(ret, min);
193 SAFE_FREE(host_princ_s);
194 gss_release_name(&min, &srv_name);
198 /******************************************************************************
200 Try and get the cifs/server@realm principal first, then fall back to
202 ******************************************************************************/
204 static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
207 gss_cred_id_t srv_cred;
210 name_to_fqdn(fqdn, lp_netbios_name());
213 status = get_srv_gss_creds("cifs", fqdn, GSS_C_ACCEPT, &srv_cred);
214 if (!NT_STATUS_IS_OK(status)) {
215 status = get_srv_gss_creds("host", fqdn, GSS_C_ACCEPT, &srv_cred);
216 if (!NT_STATUS_IS_OK(status)) {
217 return nt_status_squash(status);
221 ec->es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
222 if (!ec->es->s.gss_state) {
224 gss_release_cred(&min, &srv_cred);
225 return NT_STATUS_NO_MEMORY;
227 ZERO_STRUCTP(ec->es->s.gss_state);
228 ec->es->s.gss_state->creds = srv_cred;
230 /* No context yet. */
231 ec->es->s.gss_state->gss_ctx = GSS_C_NO_CONTEXT;
237 /******************************************************************************
238 Shutdown a server encryption context.
239 ******************************************************************************/
241 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
243 struct smb_srv_trans_enc_ctx *ec = *pp_ec;
250 switch (ec->es->smb_enc_type) {
251 case SMB_TRANS_ENC_NTLM:
252 destroy_auth_ntlmssp(ec);
254 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
255 case SMB_TRANS_ENC_GSS:
259 common_free_encryption_state(&ec->es);
266 /******************************************************************************
267 Create a server encryption context.
268 ******************************************************************************/
270 static NTSTATUS make_srv_encryption_context(const struct tsocket_address *remote_address,
271 enum smb_trans_enc_type smb_enc_type,
272 struct smb_srv_trans_enc_ctx **pp_ec)
274 struct smb_srv_trans_enc_ctx *ec;
278 ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
280 return NT_STATUS_NO_MEMORY;
282 ZERO_STRUCTP(partial_srv_trans_enc_ctx);
283 ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
286 return NT_STATUS_NO_MEMORY;
288 ZERO_STRUCTP(ec->es);
289 ec->es->smb_enc_type = smb_enc_type;
290 switch (smb_enc_type) {
291 case SMB_TRANS_ENC_NTLM:
293 NTSTATUS status = make_auth_ntlmssp(remote_address,
295 if (!NT_STATUS_IS_OK(status)) {
296 srv_free_encryption_context(&ec);
302 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
303 case SMB_TRANS_ENC_GSS:
304 /* Acquire our credentials by calling gss_acquire_cred here. */
306 NTSTATUS status = make_auth_gss(ec);
307 if (!NT_STATUS_IS_OK(status)) {
308 srv_free_encryption_context(&ec);
315 srv_free_encryption_context(&ec);
316 return NT_STATUS_INVALID_PARAMETER;
322 /******************************************************************************
323 Free an encryption-allocated buffer.
324 ******************************************************************************/
326 void srv_free_enc_buffer(char *buf)
328 /* We know this is an smb buffer, and we
329 * didn't malloc, only copy, for a keepalive,
330 * so ignore non-session messages. */
336 if (srv_trans_enc_ctx) {
337 common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
341 /******************************************************************************
342 Decrypt an incoming buffer.
343 ******************************************************************************/
345 NTSTATUS srv_decrypt_buffer(char *buf)
347 /* Ignore non-session messages. */
352 if (srv_trans_enc_ctx) {
353 return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
359 /******************************************************************************
360 Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
361 ******************************************************************************/
363 NTSTATUS srv_encrypt_buffer(char *buf, char **buf_out)
367 /* Ignore non-session messages. */
372 if (srv_trans_enc_ctx) {
373 return common_encrypt_buffer(srv_trans_enc_ctx->es, buf, buf_out);
375 /* Not encrypting. */
379 /******************************************************************************
380 Do the gss encryption negotiation. Parameters are in/out.
381 Until success we do everything on the partial enc ctx.
382 ******************************************************************************/
384 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
385 static NTSTATUS srv_enc_spnego_gss_negotiate(const struct tsocket_address *remote_address,
386 unsigned char **ppdata,
393 gss_buffer_desc in_buf, out_buf;
394 struct smb_tran_enc_state_gss *gss_state;
395 DATA_BLOB auth_reply = data_blob_null;
396 DATA_BLOB response = data_blob_null;
399 if (!partial_srv_trans_enc_ctx) {
400 status = make_srv_encryption_context(remote_address,
402 &partial_srv_trans_enc_ctx);
403 if (!NT_STATUS_IS_OK(status)) {
408 gss_state = partial_srv_trans_enc_ctx->es->s.gss_state;
410 in_buf.value = secblob.data;
411 in_buf.length = secblob.length;
413 out_buf.value = NULL;
418 ret = gss_accept_sec_context(&min,
422 GSS_C_NO_CHANNEL_BINDINGS,
424 NULL, /* Ignore oids. */
425 &out_buf, /* To return. */
427 NULL, /* Ingore time. */
428 NULL); /* Ignore delegated creds. */
431 status = gss_err_to_ntstatus(ret, min);
432 if (ret != GSS_S_COMPLETE && ret != GSS_S_CONTINUE_NEEDED) {
436 /* Ensure we've got sign+seal available. */
437 if (ret == GSS_S_COMPLETE) {
438 if ((flags & (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) !=
439 (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) {
440 DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
441 "for SMB sealing.\n"));
442 gss_release_buffer(&min, &out_buf);
443 return NT_STATUS_ACCESS_DENIED;
447 auth_reply = data_blob(out_buf.value, out_buf.length);
448 gss_release_buffer(&min, &out_buf);
450 /* Wrap in SPNEGO. */
451 response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, OID_KERBEROS5);
452 data_blob_free(&auth_reply);
455 *ppdata = (unsigned char *)memdup(response.data, response.length);
456 if ((*ppdata) == NULL && response.length > 0) {
457 status = NT_STATUS_NO_MEMORY;
459 *p_data_size = response.length;
461 data_blob_free(&response);
467 /******************************************************************************
468 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
469 Until success we do everything on the partial enc ctx.
470 ******************************************************************************/
472 static NTSTATUS srv_enc_ntlm_negotiate(const struct tsocket_address *remote_address,
473 unsigned char **ppdata,
479 DATA_BLOB chal = data_blob_null;
480 DATA_BLOB response = data_blob_null;
482 status = make_srv_encryption_context(remote_address,
484 &partial_srv_trans_enc_ctx);
485 if (!NT_STATUS_IS_OK(status)) {
489 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state,
490 partial_srv_trans_enc_ctx->auth_ntlmssp_state,
493 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
497 response = spnego_gen_auth_response(talloc_tos(), &chal, status, OID_NTLMSSP);
498 data_blob_free(&chal);
500 /* Return the raw blob. */
505 *ppdata = (unsigned char *)memdup(response.data, response.length);
506 if ((*ppdata) == NULL && response.length > 0) {
507 status = NT_STATUS_NO_MEMORY;
509 *p_data_size = response.length;
510 data_blob_free(&response);
515 /******************************************************************************
516 Do the SPNEGO encryption negotiation. Parameters are in/out.
517 Based off code in smbd/sesssionsetup.c
518 Until success we do everything on the partial enc ctx.
519 ******************************************************************************/
521 static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
522 unsigned char **ppdata,
524 unsigned char **pparam,
525 size_t *p_param_size)
528 DATA_BLOB blob = data_blob_null;
529 DATA_BLOB secblob = data_blob_null;
530 char *kerb_mech = NULL;
532 blob = data_blob_const(*ppdata, *p_data_size);
534 status = parse_spnego_mechanisms(talloc_tos(), blob, &secblob, &kerb_mech);
535 if (!NT_STATUS_IS_OK(status)) {
536 return nt_status_squash(status);
539 /* We should have no partial context at this point. */
541 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
544 TALLOC_FREE(kerb_mech);
546 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
547 status = srv_enc_spnego_gss_negotiate(conn->sconn->remote_address,
552 /* Currently we don't SPNEGO negotiate
553 * back to NTLMSSP as we do in sessionsetupX. We should... */
554 return NT_STATUS_LOGON_FAILURE;
557 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
564 data_blob_free(&secblob);
566 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
567 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
568 return nt_status_squash(status);
571 if (NT_STATUS_IS_OK(status)) {
572 /* Return the context we're using for this encryption state. */
573 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
574 return NT_STATUS_NO_MEMORY;
576 SSVAL(*pparam,0,partial_srv_trans_enc_ctx->es->enc_ctx_num);
583 /******************************************************************************
584 Complete a SPNEGO encryption negotiation. Parameters are in/out.
585 We only get this for a NTLM auth second stage.
586 ******************************************************************************/
588 static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
589 unsigned char **ppdata,
591 unsigned char **pparam,
592 size_t *p_param_size)
595 DATA_BLOB blob = data_blob_null;
596 DATA_BLOB auth = data_blob_null;
597 DATA_BLOB auth_reply = data_blob_null;
598 DATA_BLOB response = data_blob_null;
599 struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
601 /* We must have a partial context here. */
603 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
604 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
605 return NT_STATUS_INVALID_PARAMETER;
608 blob = data_blob_const(*ppdata, *p_data_size);
609 if (!spnego_parse_auth(talloc_tos(), blob, &auth)) {
610 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
611 return NT_STATUS_INVALID_PARAMETER;
614 status = auth_ntlmssp_update(ec->auth_ntlmssp_state, talloc_tos(), auth, &auth_reply);
615 data_blob_free(&auth);
621 * This field SHALL only be present in the first reply from the
623 * So set mechOID to NULL here.
626 response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, NULL);
627 data_blob_free(&auth_reply);
629 if (NT_STATUS_IS_OK(status)) {
630 /* Return the context we're using for this encryption state. */
631 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
632 return NT_STATUS_NO_MEMORY;
634 SSVAL(*pparam,0,ec->es->enc_ctx_num);
639 *ppdata = (unsigned char *)memdup(response.data, response.length);
640 if ((*ppdata) == NULL && response.length > 0)
641 return NT_STATUS_NO_MEMORY;
642 *p_data_size = response.length;
643 data_blob_free(&response);
647 /******************************************************************************
648 Raw NTLM encryption negotiation. Parameters are in/out.
649 This function does both steps.
650 ******************************************************************************/
652 static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
653 unsigned char **ppdata,
655 unsigned char **pparam,
656 size_t *p_param_size)
659 DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
660 DATA_BLOB response = data_blob_null;
661 struct smb_srv_trans_enc_ctx *ec;
663 if (!partial_srv_trans_enc_ctx) {
664 /* This is the initial step. */
665 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
670 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
671 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
672 return nt_status_squash(status);
677 ec = partial_srv_trans_enc_ctx;
678 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
679 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
680 return NT_STATUS_INVALID_PARAMETER;
684 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state,
688 if (NT_STATUS_IS_OK(status)) {
689 /* Return the context we're using for this encryption state. */
690 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
691 return NT_STATUS_NO_MEMORY;
693 SSVAL(*pparam,0,ec->es->enc_ctx_num);
697 /* Return the raw blob. */
699 *ppdata = (unsigned char *)memdup(response.data, response.length);
700 if ((*ppdata) == NULL && response.length > 0)
701 return NT_STATUS_NO_MEMORY;
702 *p_data_size = response.length;
703 data_blob_free(&response);
707 /******************************************************************************
708 Do the SPNEGO encryption negotiation. Parameters are in/out.
709 ******************************************************************************/
711 NTSTATUS srv_request_encryption_setup(connection_struct *conn,
712 unsigned char **ppdata,
714 unsigned char **pparam,
715 size_t *p_param_size)
717 unsigned char *pdata = *ppdata;
722 if (*p_data_size < 1) {
723 return NT_STATUS_INVALID_PARAMETER;
726 if (pdata[0] == ASN1_APPLICATION(0)) {
727 /* its a negTokenTarg packet */
728 return srv_enc_spnego_negotiate(conn, ppdata, p_data_size, pparam, p_param_size);
731 if (pdata[0] == ASN1_CONTEXT(1)) {
732 /* It's an auth packet */
733 return srv_enc_spnego_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
736 /* Maybe it's a raw unwrapped auth ? */
737 if (*p_data_size < 7) {
738 return NT_STATUS_INVALID_PARAMETER;
741 if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
742 return srv_enc_raw_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
745 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
747 return NT_STATUS_LOGON_FAILURE;
750 /******************************************************************************
751 Negotiation was successful - turn on server-side encryption.
752 ******************************************************************************/
754 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
756 if (!ec || !ec->es) {
757 return NT_STATUS_LOGON_FAILURE;
760 if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
761 if (!auth_ntlmssp_negotiated_sign((ec->auth_ntlmssp_state))) {
762 return NT_STATUS_INVALID_PARAMETER;
765 if (!auth_ntlmssp_negotiated_seal((ec->auth_ntlmssp_state))) {
766 return NT_STATUS_INVALID_PARAMETER;
769 /* Todo - check gssapi case. */
774 /******************************************************************************
775 Negotiation was successful - turn on server-side encryption.
776 ******************************************************************************/
778 NTSTATUS srv_encryption_start(connection_struct *conn)
782 /* Check that we are really doing sign+seal. */
783 status = check_enc_good(partial_srv_trans_enc_ctx);
784 if (!NT_STATUS_IS_OK(status)) {
787 /* Throw away the context we're using currently (if any). */
788 srv_free_encryption_context(&srv_trans_enc_ctx);
790 /* Steal the partial pointer. Deliberate shallow copy. */
791 srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
792 srv_trans_enc_ctx->es->enc_on = true;
794 partial_srv_trans_enc_ctx = NULL;
796 DEBUG(1,("srv_encryption_start: context negotiated\n"));
800 /******************************************************************************
801 Shutdown all server contexts.
802 ******************************************************************************/
804 void server_encryption_shutdown(void)
806 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
807 srv_free_encryption_context(&srv_trans_enc_ctx);