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 "../libcli/smb/smb_seal.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 /******************************************************************************
38 Return global enc context - this must change if we ever do multiple contexts.
39 ******************************************************************************/
41 static uint16_t srv_enc_ctx(const struct smb_trans_enc_state
*es
)
43 return es
->enc_ctx_num
;
46 /******************************************************************************
47 Is this an incoming encrypted packet ?
48 ******************************************************************************/
50 bool is_encrypted_packet(struct smbd_server_connection
*sconn
,
56 /* Ignore non-session messages or non 0xFF'E' messages. */
58 || (smb_len(inbuf
) < 8)
59 || !(inbuf
[4] == 0xFF && inbuf
[5] == 'E')) {
63 status
= get_enc_ctx_num(inbuf
, &enc_num
);
64 if (!NT_STATUS_IS_OK(status
)) {
68 /* Encrypted messages are 0xFF'E'<ctx> */
69 if (srv_trans_enc_ctx
&& enc_num
== srv_enc_ctx(srv_trans_enc_ctx
)) {
75 /******************************************************************************
76 Create an auth_ntlmssp_state and ensure pointer copy is correct.
77 ******************************************************************************/
79 static NTSTATUS
make_auth_ntlmssp(const struct tsocket_address
*remote_address
,
80 struct smb_trans_enc_state
*es
)
82 struct auth_generic_state
*auth_ntlmssp_state
;
83 NTSTATUS status
= auth_ntlmssp_prepare(remote_address
,
85 if (!NT_STATUS_IS_OK(status
)) {
86 return nt_status_squash(status
);
89 gensec_want_feature(auth_ntlmssp_state
->gensec_security
, GENSEC_FEATURE_SEAL
);
91 status
= auth_ntlmssp_start(auth_ntlmssp_state
);
93 if (!NT_STATUS_IS_OK(status
)) {
94 TALLOC_FREE(auth_ntlmssp_state
);
95 return nt_status_squash(status
);
98 /* We do not need the auth_ntlmssp layer any more, which was
99 * allocated on NULL, so promote gensec_security to the NULL
101 es
->s
.gensec_security
= talloc_move(NULL
, &auth_ntlmssp_state
->gensec_security
);
102 TALLOC_FREE(auth_ntlmssp_state
);
107 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
109 /******************************************************************************
111 ******************************************************************************/
113 static NTSTATUS
get_srv_gss_creds(const char *service
,
115 gss_cred_usage_t cred_type
,
116 gss_cred_id_t
*p_srv_cred
)
121 gss_buffer_desc input_name
;
122 char *host_princ_s
= NULL
;
123 NTSTATUS status
= NT_STATUS_OK
;
125 gss_OID_desc nt_hostbased_service
=
126 {10, discard_const_p(char, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
128 if (asprintf(&host_princ_s
, "%s@%s", service
, name
) == -1) {
129 return NT_STATUS_NO_MEMORY
;
132 input_name
.value
= host_princ_s
;
133 input_name
.length
= strlen(host_princ_s
) + 1;
135 ret
= gss_import_name(&min
,
137 &nt_hostbased_service
,
140 DEBUG(10,("get_srv_gss_creds: imported name %s\n",
143 if (ret
!= GSS_S_COMPLETE
) {
144 SAFE_FREE(host_princ_s
);
145 return map_nt_error_from_gss(ret
, min
);
149 * We're accessing the krb5.keytab file here.
150 * ensure we have permissions to do so.
154 ret
= gss_acquire_cred(&min
,
164 if (ret
!= GSS_S_COMPLETE
) {
165 ADS_STATUS adss
= ADS_ERROR_GSS(ret
, min
);
166 DEBUG(10,("get_srv_gss_creds: gss_acquire_cred failed with %s\n",
168 status
= map_nt_error_from_gss(ret
, min
);
171 SAFE_FREE(host_princ_s
);
172 gss_release_name(&min
, &srv_name
);
176 /******************************************************************************
178 Try and get the cifs/server@realm principal first, then fall back to
180 ******************************************************************************/
182 static NTSTATUS
make_auth_gss(struct smb_trans_enc_state
*es
)
185 gss_cred_id_t srv_cred
;
188 name_to_fqdn(fqdn
, lp_netbios_name());
191 status
= get_srv_gss_creds("cifs", fqdn
, GSS_C_ACCEPT
, &srv_cred
);
192 if (!NT_STATUS_IS_OK(status
)) {
193 status
= get_srv_gss_creds("host", fqdn
, GSS_C_ACCEPT
, &srv_cred
);
194 if (!NT_STATUS_IS_OK(status
)) {
195 return nt_status_squash(status
);
199 es
->s
.gss_state
= SMB_MALLOC_P(struct smb_tran_enc_state_gss
);
200 if (!es
->s
.gss_state
) {
202 gss_release_cred(&min
, &srv_cred
);
203 return NT_STATUS_NO_MEMORY
;
205 ZERO_STRUCTP(es
->s
.gss_state
);
206 es
->s
.gss_state
->creds
= srv_cred
;
208 /* No context yet. */
209 es
->s
.gss_state
->gss_ctx
= GSS_C_NO_CONTEXT
;
215 /******************************************************************************
216 Shutdown a server encryption context.
217 ******************************************************************************/
219 static void srv_free_encryption_context(struct smb_trans_enc_state
**pp_es
)
221 struct smb_trans_enc_state
*es
= *pp_es
;
227 common_free_encryption_state(&es
);
233 /******************************************************************************
234 Create a server encryption context.
235 ******************************************************************************/
237 static NTSTATUS
make_srv_encryption_context(const struct tsocket_address
*remote_address
,
238 enum smb_trans_enc_type smb_enc_type
,
239 struct smb_trans_enc_state
**pp_es
)
241 struct smb_trans_enc_state
*es
;
245 ZERO_STRUCTP(partial_srv_trans_enc_ctx
);
246 es
= SMB_MALLOC_P(struct smb_trans_enc_state
);
248 return NT_STATUS_NO_MEMORY
;
251 es
->smb_enc_type
= smb_enc_type
;
252 switch (smb_enc_type
) {
253 case SMB_TRANS_ENC_NTLM
:
255 NTSTATUS status
= make_auth_ntlmssp(remote_address
,
257 if (!NT_STATUS_IS_OK(status
)) {
258 srv_free_encryption_context(&es
);
264 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
265 case SMB_TRANS_ENC_GSS
:
266 /* Acquire our credentials by calling gss_acquire_cred here. */
268 NTSTATUS status
= make_auth_gss(es
);
269 if (!NT_STATUS_IS_OK(status
)) {
270 srv_free_encryption_context(&es
);
277 srv_free_encryption_context(&es
);
278 return NT_STATUS_INVALID_PARAMETER
;
284 /******************************************************************************
285 Free an encryption-allocated buffer.
286 ******************************************************************************/
288 void srv_free_enc_buffer(struct smbd_server_connection
*sconn
, char *buf
)
290 /* We know this is an smb buffer, and we
291 * didn't malloc, only copy, for a keepalive,
292 * so ignore non-session messages. */
298 if (srv_trans_enc_ctx
) {
299 common_free_enc_buffer(srv_trans_enc_ctx
, buf
);
303 /******************************************************************************
304 Decrypt an incoming buffer.
305 ******************************************************************************/
307 NTSTATUS
srv_decrypt_buffer(struct smbd_server_connection
*sconn
, char *buf
)
309 /* Ignore non-session messages. */
314 if (srv_trans_enc_ctx
) {
315 return common_decrypt_buffer(srv_trans_enc_ctx
, buf
);
321 /******************************************************************************
322 Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
323 ******************************************************************************/
325 NTSTATUS
srv_encrypt_buffer(struct smbd_server_connection
*sconn
, char *buf
,
330 /* Ignore non-session messages. */
335 if (srv_trans_enc_ctx
) {
336 return common_encrypt_buffer(srv_trans_enc_ctx
, buf
, buf_out
);
338 /* Not encrypting. */
342 /******************************************************************************
343 Do the gss encryption negotiation. Parameters are in/out.
344 Until success we do everything on the partial enc ctx.
345 ******************************************************************************/
347 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
348 static NTSTATUS
srv_enc_spnego_gss_negotiate(const struct tsocket_address
*remote_address
,
349 unsigned char **ppdata
,
356 gss_buffer_desc in_buf
, out_buf
;
357 struct smb_tran_enc_state_gss
*gss_state
;
358 DATA_BLOB auth_reply
= data_blob_null
;
359 DATA_BLOB response
= data_blob_null
;
362 if (!partial_srv_trans_enc_ctx
) {
363 status
= make_srv_encryption_context(remote_address
,
365 &partial_srv_trans_enc_ctx
);
366 if (!NT_STATUS_IS_OK(status
)) {
371 gss_state
= partial_srv_trans_enc_ctx
->s
.gss_state
;
373 in_buf
.value
= secblob
.data
;
374 in_buf
.length
= secblob
.length
;
376 out_buf
.value
= NULL
;
381 ret
= gss_accept_sec_context(&min
,
385 GSS_C_NO_CHANNEL_BINDINGS
,
387 NULL
, /* Ignore oids. */
388 &out_buf
, /* To return. */
390 NULL
, /* Ingore time. */
391 NULL
); /* Ignore delegated creds. */
394 status
= gss_err_to_ntstatus(ret
, min
);
395 if (ret
!= GSS_S_COMPLETE
&& ret
!= GSS_S_CONTINUE_NEEDED
) {
399 /* Ensure we've got sign+seal available. */
400 if (ret
== GSS_S_COMPLETE
) {
401 if ((flags
& (GSS_C_INTEG_FLAG
|GSS_C_CONF_FLAG
|GSS_C_REPLAY_FLAG
|GSS_C_SEQUENCE_FLAG
)) !=
402 (GSS_C_INTEG_FLAG
|GSS_C_CONF_FLAG
|GSS_C_REPLAY_FLAG
|GSS_C_SEQUENCE_FLAG
)) {
403 DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
404 "for SMB sealing.\n"));
405 gss_release_buffer(&min
, &out_buf
);
406 return NT_STATUS_ACCESS_DENIED
;
410 auth_reply
= data_blob(out_buf
.value
, out_buf
.length
);
411 gss_release_buffer(&min
, &out_buf
);
413 /* Wrap in SPNEGO. */
414 response
= spnego_gen_auth_response(talloc_tos(), &auth_reply
, status
, OID_KERBEROS5
);
415 data_blob_free(&auth_reply
);
418 *ppdata
= (unsigned char *)memdup(response
.data
, response
.length
);
419 if ((*ppdata
) == NULL
&& response
.length
> 0) {
420 status
= NT_STATUS_NO_MEMORY
;
422 *p_data_size
= response
.length
;
424 data_blob_free(&response
);
430 /******************************************************************************
431 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
432 Until success we do everything on the partial enc ctx.
433 ******************************************************************************/
435 static NTSTATUS
srv_enc_ntlm_negotiate(const struct tsocket_address
*remote_address
,
436 unsigned char **ppdata
,
442 DATA_BLOB chal
= data_blob_null
;
443 DATA_BLOB response
= data_blob_null
;
445 status
= make_srv_encryption_context(remote_address
,
447 &partial_srv_trans_enc_ctx
);
448 if (!NT_STATUS_IS_OK(status
)) {
452 status
= gensec_update(partial_srv_trans_enc_ctx
->s
.gensec_security
,
456 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
460 response
= spnego_gen_auth_response(talloc_tos(), &chal
, status
, OID_NTLMSSP
);
461 data_blob_free(&chal
);
463 /* Return the raw blob. */
468 *ppdata
= (unsigned char *)memdup(response
.data
, response
.length
);
469 if ((*ppdata
) == NULL
&& response
.length
> 0) {
470 status
= NT_STATUS_NO_MEMORY
;
472 *p_data_size
= response
.length
;
473 data_blob_free(&response
);
478 /******************************************************************************
479 Do the SPNEGO encryption negotiation. Parameters are in/out.
480 Based off code in smbd/sesssionsetup.c
481 Until success we do everything on the partial enc ctx.
482 ******************************************************************************/
484 static NTSTATUS
srv_enc_spnego_negotiate(connection_struct
*conn
,
485 unsigned char **ppdata
,
487 unsigned char **pparam
,
488 size_t *p_param_size
)
491 DATA_BLOB blob
= data_blob_null
;
492 DATA_BLOB secblob
= data_blob_null
;
493 char *kerb_mech
= NULL
;
495 blob
= data_blob_const(*ppdata
, *p_data_size
);
497 status
= parse_spnego_mechanisms(talloc_tos(), blob
, &secblob
, &kerb_mech
);
498 if (!NT_STATUS_IS_OK(status
)) {
499 return nt_status_squash(status
);
502 /* We should have no partial context at this point. */
504 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
507 TALLOC_FREE(kerb_mech
);
509 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
510 status
= srv_enc_spnego_gss_negotiate(conn
->sconn
->remote_address
,
515 /* Currently we don't SPNEGO negotiate
516 * back to NTLMSSP as we do in sessionsetupX. We should... */
517 return NT_STATUS_LOGON_FAILURE
;
520 status
= srv_enc_ntlm_negotiate(conn
->sconn
->remote_address
,
527 data_blob_free(&secblob
);
529 if (!NT_STATUS_EQUAL(status
,NT_STATUS_MORE_PROCESSING_REQUIRED
) && !NT_STATUS_IS_OK(status
)) {
530 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
531 return nt_status_squash(status
);
534 if (NT_STATUS_IS_OK(status
)) {
535 /* Return the context we're using for this encryption state. */
536 if (!(*pparam
= SMB_MALLOC_ARRAY(unsigned char, 2))) {
537 return NT_STATUS_NO_MEMORY
;
539 SSVAL(*pparam
,0,partial_srv_trans_enc_ctx
->enc_ctx_num
);
546 /******************************************************************************
547 Complete a SPNEGO encryption negotiation. Parameters are in/out.
548 We only get this for a NTLM auth second stage.
549 ******************************************************************************/
551 static NTSTATUS
srv_enc_spnego_ntlm_auth(connection_struct
*conn
,
552 unsigned char **ppdata
,
554 unsigned char **pparam
,
555 size_t *p_param_size
)
558 DATA_BLOB blob
= data_blob_null
;
559 DATA_BLOB auth
= data_blob_null
;
560 DATA_BLOB auth_reply
= data_blob_null
;
561 DATA_BLOB response
= data_blob_null
;
562 struct smb_trans_enc_state
*es
= partial_srv_trans_enc_ctx
;
564 /* We must have a partial context here. */
566 if (!es
|| es
->s
.gensec_security
== NULL
|| es
->smb_enc_type
!= SMB_TRANS_ENC_NTLM
) {
567 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
568 return NT_STATUS_INVALID_PARAMETER
;
571 blob
= data_blob_const(*ppdata
, *p_data_size
);
572 if (!spnego_parse_auth(talloc_tos(), blob
, &auth
)) {
573 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
574 return NT_STATUS_INVALID_PARAMETER
;
577 status
= gensec_update(es
->s
.gensec_security
, talloc_tos(), NULL
, auth
, &auth_reply
);
578 data_blob_free(&auth
);
584 * This field SHALL only be present in the first reply from the
586 * So set mechOID to NULL here.
589 response
= spnego_gen_auth_response(talloc_tos(), &auth_reply
, status
, NULL
);
590 data_blob_free(&auth_reply
);
592 if (NT_STATUS_IS_OK(status
)) {
593 /* Return the context we're using for this encryption state. */
594 if (!(*pparam
= SMB_MALLOC_ARRAY(unsigned char, 2))) {
595 return NT_STATUS_NO_MEMORY
;
597 SSVAL(*pparam
,0,es
->enc_ctx_num
);
602 *ppdata
= (unsigned char *)memdup(response
.data
, response
.length
);
603 if ((*ppdata
) == NULL
&& response
.length
> 0)
604 return NT_STATUS_NO_MEMORY
;
605 *p_data_size
= response
.length
;
606 data_blob_free(&response
);
610 /******************************************************************************
611 Raw NTLM encryption negotiation. Parameters are in/out.
612 This function does both steps.
613 ******************************************************************************/
615 static NTSTATUS
srv_enc_raw_ntlm_auth(connection_struct
*conn
,
616 unsigned char **ppdata
,
618 unsigned char **pparam
,
619 size_t *p_param_size
)
622 DATA_BLOB blob
= data_blob_const(*ppdata
, *p_data_size
);
623 DATA_BLOB response
= data_blob_null
;
624 struct smb_trans_enc_state
*es
;
626 if (!partial_srv_trans_enc_ctx
) {
627 /* This is the initial step. */
628 status
= srv_enc_ntlm_negotiate(conn
->sconn
->remote_address
,
633 if (!NT_STATUS_EQUAL(status
,NT_STATUS_MORE_PROCESSING_REQUIRED
) && !NT_STATUS_IS_OK(status
)) {
634 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
635 return nt_status_squash(status
);
640 es
= partial_srv_trans_enc_ctx
;
641 if (!es
|| es
->s
.gensec_security
== NULL
|| es
->smb_enc_type
!= SMB_TRANS_ENC_NTLM
) {
642 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
643 return NT_STATUS_INVALID_PARAMETER
;
647 status
= gensec_update(partial_srv_trans_enc_ctx
->s
.gensec_security
,
651 if (NT_STATUS_IS_OK(status
)) {
652 /* Return the context we're using for this encryption state. */
653 if (!(*pparam
= SMB_MALLOC_ARRAY(unsigned char, 2))) {
654 return NT_STATUS_NO_MEMORY
;
656 SSVAL(*pparam
, 0, es
->enc_ctx_num
);
660 /* Return the raw blob. */
662 *ppdata
= (unsigned char *)memdup(response
.data
, response
.length
);
663 if ((*ppdata
) == NULL
&& response
.length
> 0)
664 return NT_STATUS_NO_MEMORY
;
665 *p_data_size
= response
.length
;
666 data_blob_free(&response
);
670 /******************************************************************************
671 Do the SPNEGO encryption negotiation. Parameters are in/out.
672 ******************************************************************************/
674 NTSTATUS
srv_request_encryption_setup(connection_struct
*conn
,
675 unsigned char **ppdata
,
677 unsigned char **pparam
,
678 size_t *p_param_size
)
680 unsigned char *pdata
= *ppdata
;
685 if (*p_data_size
< 1) {
686 return NT_STATUS_INVALID_PARAMETER
;
689 if (pdata
[0] == ASN1_APPLICATION(0)) {
690 /* its a negTokenTarg packet */
691 return srv_enc_spnego_negotiate(conn
, ppdata
, p_data_size
, pparam
, p_param_size
);
694 if (pdata
[0] == ASN1_CONTEXT(1)) {
695 /* It's an auth packet */
696 return srv_enc_spnego_ntlm_auth(conn
, ppdata
, p_data_size
, pparam
, p_param_size
);
699 /* Maybe it's a raw unwrapped auth ? */
700 if (*p_data_size
< 7) {
701 return NT_STATUS_INVALID_PARAMETER
;
704 if (strncmp((char *)pdata
, "NTLMSSP", 7) == 0) {
705 return srv_enc_raw_ntlm_auth(conn
, ppdata
, p_data_size
, pparam
, p_param_size
);
708 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
710 return NT_STATUS_LOGON_FAILURE
;
713 /******************************************************************************
714 Negotiation was successful - turn on server-side encryption.
715 ******************************************************************************/
717 static NTSTATUS
check_enc_good(struct smb_trans_enc_state
*es
)
720 return NT_STATUS_LOGON_FAILURE
;
723 if (es
->smb_enc_type
== SMB_TRANS_ENC_NTLM
) {
724 if (!gensec_have_feature(es
->s
.gensec_security
, GENSEC_FEATURE_SIGN
)) {
725 return NT_STATUS_INVALID_PARAMETER
;
728 if (!gensec_have_feature(es
->s
.gensec_security
, GENSEC_FEATURE_SEAL
)) {
729 return NT_STATUS_INVALID_PARAMETER
;
732 /* Todo - check gssapi case. */
737 /******************************************************************************
738 Negotiation was successful - turn on server-side encryption.
739 ******************************************************************************/
741 NTSTATUS
srv_encryption_start(connection_struct
*conn
)
745 /* Check that we are really doing sign+seal. */
746 status
= check_enc_good(partial_srv_trans_enc_ctx
);
747 if (!NT_STATUS_IS_OK(status
)) {
750 /* Throw away the context we're using currently (if any). */
751 srv_free_encryption_context(&srv_trans_enc_ctx
);
753 /* Steal the partial pointer. Deliberate shallow copy. */
754 srv_trans_enc_ctx
= partial_srv_trans_enc_ctx
;
755 srv_trans_enc_ctx
->enc_on
= true;
757 partial_srv_trans_enc_ctx
= NULL
;
759 DEBUG(1,("srv_encryption_start: context negotiated\n"));
763 /******************************************************************************
764 Shutdown all server contexts.
765 ******************************************************************************/
767 void server_encryption_shutdown(struct smbd_server_connection
*sconn
)
769 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
770 srv_free_encryption_context(&srv_trans_enc_ctx
);