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/globals.h"
22 #include "../libcli/auth/spnego.h"
23 #include "../libcli/auth/ntlmssp.h"
24 #include "ntlmssp_wrap.h"
25 #include "smb_crypt.h"
26 #include "../lib/util/asn1.h"
28 /******************************************************************************
29 Server side encryption.
30 ******************************************************************************/
32 /******************************************************************************
34 ******************************************************************************/
36 struct smb_srv_trans_enc_ctx
{
37 struct smb_trans_enc_state
*es
;
38 struct auth_ntlmssp_state
*auth_ntlmssp_state
; /* Must be kept in sync with pointer in ec->ntlmssp_state. */
41 /******************************************************************************
42 Return global enc context - this must change if we ever do multiple contexts.
43 ******************************************************************************/
45 uint16_t srv_enc_ctx(void)
47 return srv_trans_enc_ctx
->es
->enc_ctx_num
;
50 /******************************************************************************
51 Is this an incoming encrypted packet ?
52 ******************************************************************************/
54 bool is_encrypted_packet(const uint8_t *inbuf
)
59 /* Ignore non-session messages or non 0xFF'E' messages. */
61 || (smb_len(inbuf
) < 8)
62 || !(inbuf
[4] == 0xFF && inbuf
[5] == 'E')) {
66 status
= get_enc_ctx_num(inbuf
, &enc_num
);
67 if (!NT_STATUS_IS_OK(status
)) {
71 /* Encrypted messages are 0xFF'E'<ctx> */
72 if (srv_trans_enc_ctx
&& enc_num
== srv_enc_ctx()) {
78 /******************************************************************************
79 Create an auth_ntlmssp_state and ensure pointer copy is correct.
80 ******************************************************************************/
82 static NTSTATUS
make_auth_ntlmssp(struct smb_srv_trans_enc_ctx
*ec
)
84 NTSTATUS status
= auth_ntlmssp_start(&ec
->auth_ntlmssp_state
);
85 if (!NT_STATUS_IS_OK(status
)) {
86 return nt_status_squash(status
);
90 * We must remember to update the pointer copy for the common
91 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
93 ec
->es
->s
.ntlmssp_state
= auth_ntlmssp_get_ntlmssp_state(ec
->auth_ntlmssp_state
);
97 /******************************************************************************
98 Destroy an auth_ntlmssp_state and ensure pointer copy is correct.
99 ******************************************************************************/
101 static void destroy_auth_ntlmssp(struct smb_srv_trans_enc_ctx
*ec
)
104 * We must remember to update the pointer copy for the common
105 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
108 if (ec
->auth_ntlmssp_state
) {
109 TALLOC_FREE(ec
->auth_ntlmssp_state
);
110 /* The auth_ntlmssp_end killed this already. */
111 ec
->es
->s
.ntlmssp_state
= NULL
;
115 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
117 /******************************************************************************
119 ******************************************************************************/
121 static NTSTATUS
get_srv_gss_creds(const char *service
,
123 gss_cred_usage_t cred_type
,
124 gss_cred_id_t
*p_srv_cred
)
129 gss_buffer_desc input_name
;
130 char *host_princ_s
= NULL
;
131 NTSTATUS status
= NT_STATUS_OK
;
133 gss_OID_desc nt_hostbased_service
=
134 {10, CONST_DISCARD(char *,"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
136 if (asprintf(&host_princ_s
, "%s@%s", service
, name
) == -1) {
137 return NT_STATUS_NO_MEMORY
;
140 input_name
.value
= host_princ_s
;
141 input_name
.length
= strlen(host_princ_s
) + 1;
143 ret
= gss_import_name(&min
,
145 &nt_hostbased_service
,
148 DEBUG(10,("get_srv_gss_creds: imported name %s\n",
151 if (ret
!= GSS_S_COMPLETE
) {
152 SAFE_FREE(host_princ_s
);
153 return map_nt_error_from_gss(ret
, min
);
157 * We're accessing the krb5.keytab file here.
158 * ensure we have permissions to do so.
162 ret
= gss_acquire_cred(&min
,
172 if (ret
!= GSS_S_COMPLETE
) {
173 ADS_STATUS adss
= ADS_ERROR_GSS(ret
, min
);
174 DEBUG(10,("get_srv_gss_creds: gss_acquire_cred failed with %s\n",
176 status
= map_nt_error_from_gss(ret
, min
);
179 SAFE_FREE(host_princ_s
);
180 gss_release_name(&min
, &srv_name
);
184 /******************************************************************************
186 Try and get the cifs/server@realm principal first, then fall back to
188 ******************************************************************************/
190 static NTSTATUS
make_auth_gss(struct smb_srv_trans_enc_ctx
*ec
)
193 gss_cred_id_t srv_cred
;
196 name_to_fqdn(fqdn
, global_myname());
199 status
= get_srv_gss_creds("cifs", fqdn
, GSS_C_ACCEPT
, &srv_cred
);
200 if (!NT_STATUS_IS_OK(status
)) {
201 status
= get_srv_gss_creds("host", fqdn
, GSS_C_ACCEPT
, &srv_cred
);
202 if (!NT_STATUS_IS_OK(status
)) {
203 return nt_status_squash(status
);
207 ec
->es
->s
.gss_state
= SMB_MALLOC_P(struct smb_tran_enc_state_gss
);
208 if (!ec
->es
->s
.gss_state
) {
210 gss_release_cred(&min
, &srv_cred
);
211 return NT_STATUS_NO_MEMORY
;
213 ZERO_STRUCTP(ec
->es
->s
.gss_state
);
214 ec
->es
->s
.gss_state
->creds
= srv_cred
;
216 /* No context yet. */
217 ec
->es
->s
.gss_state
->gss_ctx
= GSS_C_NO_CONTEXT
;
223 /******************************************************************************
224 Shutdown a server encryption context.
225 ******************************************************************************/
227 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx
**pp_ec
)
229 struct smb_srv_trans_enc_ctx
*ec
= *pp_ec
;
236 switch (ec
->es
->smb_enc_type
) {
237 case SMB_TRANS_ENC_NTLM
:
238 destroy_auth_ntlmssp(ec
);
240 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
241 case SMB_TRANS_ENC_GSS
:
245 common_free_encryption_state(&ec
->es
);
252 /******************************************************************************
253 Create a server encryption context.
254 ******************************************************************************/
256 static NTSTATUS
make_srv_encryption_context(enum smb_trans_enc_type smb_enc_type
, struct smb_srv_trans_enc_ctx
**pp_ec
)
258 struct smb_srv_trans_enc_ctx
*ec
;
262 ec
= SMB_MALLOC_P(struct smb_srv_trans_enc_ctx
);
264 return NT_STATUS_NO_MEMORY
;
266 ZERO_STRUCTP(partial_srv_trans_enc_ctx
);
267 ec
->es
= SMB_MALLOC_P(struct smb_trans_enc_state
);
270 return NT_STATUS_NO_MEMORY
;
272 ZERO_STRUCTP(ec
->es
);
273 ec
->es
->smb_enc_type
= smb_enc_type
;
274 switch (smb_enc_type
) {
275 case SMB_TRANS_ENC_NTLM
:
277 NTSTATUS status
= make_auth_ntlmssp(ec
);
278 if (!NT_STATUS_IS_OK(status
)) {
279 srv_free_encryption_context(&ec
);
285 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
286 case SMB_TRANS_ENC_GSS
:
287 /* Acquire our credentials by calling gss_acquire_cred here. */
289 NTSTATUS status
= make_auth_gss(ec
);
290 if (!NT_STATUS_IS_OK(status
)) {
291 srv_free_encryption_context(&ec
);
298 srv_free_encryption_context(&ec
);
299 return NT_STATUS_INVALID_PARAMETER
;
305 /******************************************************************************
306 Free an encryption-allocated buffer.
307 ******************************************************************************/
309 void srv_free_enc_buffer(char *buf
)
311 /* We know this is an smb buffer, and we
312 * didn't malloc, only copy, for a keepalive,
313 * so ignore non-session messages. */
319 if (srv_trans_enc_ctx
) {
320 common_free_enc_buffer(srv_trans_enc_ctx
->es
, buf
);
324 /******************************************************************************
325 Decrypt an incoming buffer.
326 ******************************************************************************/
328 NTSTATUS
srv_decrypt_buffer(char *buf
)
330 /* Ignore non-session messages. */
335 if (srv_trans_enc_ctx
) {
336 return common_decrypt_buffer(srv_trans_enc_ctx
->es
, buf
);
342 /******************************************************************************
343 Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
344 ******************************************************************************/
346 NTSTATUS
srv_encrypt_buffer(char *buf
, char **buf_out
)
350 /* Ignore non-session messages. */
355 if (srv_trans_enc_ctx
) {
356 return common_encrypt_buffer(srv_trans_enc_ctx
->es
, buf
, buf_out
);
358 /* Not encrypting. */
362 /******************************************************************************
363 Do the gss encryption negotiation. Parameters are in/out.
364 Until success we do everything on the partial enc ctx.
365 ******************************************************************************/
367 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
368 static NTSTATUS
srv_enc_spnego_gss_negotiate(unsigned char **ppdata
, size_t *p_data_size
, DATA_BLOB secblob
)
373 gss_buffer_desc in_buf
, out_buf
;
374 struct smb_tran_enc_state_gss
*gss_state
;
375 DATA_BLOB auth_reply
= data_blob_null
;
376 DATA_BLOB response
= data_blob_null
;
379 if (!partial_srv_trans_enc_ctx
) {
380 status
= make_srv_encryption_context(SMB_TRANS_ENC_GSS
, &partial_srv_trans_enc_ctx
);
381 if (!NT_STATUS_IS_OK(status
)) {
386 gss_state
= partial_srv_trans_enc_ctx
->es
->s
.gss_state
;
388 in_buf
.value
= secblob
.data
;
389 in_buf
.length
= secblob
.length
;
391 out_buf
.value
= NULL
;
396 ret
= gss_accept_sec_context(&min
,
400 GSS_C_NO_CHANNEL_BINDINGS
,
402 NULL
, /* Ignore oids. */
403 &out_buf
, /* To return. */
405 NULL
, /* Ingore time. */
406 NULL
); /* Ignore delegated creds. */
409 status
= gss_err_to_ntstatus(ret
, min
);
410 if (ret
!= GSS_S_COMPLETE
&& ret
!= GSS_S_CONTINUE_NEEDED
) {
414 /* Ensure we've got sign+seal available. */
415 if (ret
== GSS_S_COMPLETE
) {
416 if ((flags
& (GSS_C_INTEG_FLAG
|GSS_C_CONF_FLAG
|GSS_C_REPLAY_FLAG
|GSS_C_SEQUENCE_FLAG
)) !=
417 (GSS_C_INTEG_FLAG
|GSS_C_CONF_FLAG
|GSS_C_REPLAY_FLAG
|GSS_C_SEQUENCE_FLAG
)) {
418 DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
419 "for SMB sealing.\n"));
420 gss_release_buffer(&min
, &out_buf
);
421 return NT_STATUS_ACCESS_DENIED
;
425 auth_reply
= data_blob(out_buf
.value
, out_buf
.length
);
426 gss_release_buffer(&min
, &out_buf
);
428 /* Wrap in SPNEGO. */
429 response
= spnego_gen_auth_response(talloc_tos(), &auth_reply
, status
, OID_KERBEROS5
);
430 data_blob_free(&auth_reply
);
433 *ppdata
= (unsigned char *)memdup(response
.data
, response
.length
);
434 if ((*ppdata
) == NULL
&& response
.length
> 0) {
435 status
= NT_STATUS_NO_MEMORY
;
437 *p_data_size
= response
.length
;
439 data_blob_free(&response
);
445 /******************************************************************************
446 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
447 Until success we do everything on the partial enc ctx.
448 ******************************************************************************/
450 static NTSTATUS
srv_enc_ntlm_negotiate(unsigned char **ppdata
, size_t *p_data_size
, DATA_BLOB secblob
, bool spnego_wrap
)
453 DATA_BLOB chal
= data_blob_null
;
454 DATA_BLOB response
= data_blob_null
;
456 status
= make_srv_encryption_context(SMB_TRANS_ENC_NTLM
, &partial_srv_trans_enc_ctx
);
457 if (!NT_STATUS_IS_OK(status
)) {
461 status
= auth_ntlmssp_update(partial_srv_trans_enc_ctx
->auth_ntlmssp_state
, secblob
, &chal
);
463 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
467 response
= spnego_gen_auth_response(talloc_tos(), &chal
, status
, OID_NTLMSSP
);
468 data_blob_free(&chal
);
470 /* Return the raw blob. */
475 *ppdata
= (unsigned char *)memdup(response
.data
, response
.length
);
476 if ((*ppdata
) == NULL
&& response
.length
> 0) {
477 status
= NT_STATUS_NO_MEMORY
;
479 *p_data_size
= response
.length
;
480 data_blob_free(&response
);
485 /******************************************************************************
486 Do the SPNEGO encryption negotiation. Parameters are in/out.
487 Based off code in smbd/sesssionsetup.c
488 Until success we do everything on the partial enc ctx.
489 ******************************************************************************/
491 static NTSTATUS
srv_enc_spnego_negotiate(connection_struct
*conn
,
492 unsigned char **ppdata
,
494 unsigned char **pparam
,
495 size_t *p_param_size
)
498 DATA_BLOB blob
= data_blob_null
;
499 DATA_BLOB secblob
= data_blob_null
;
500 char *kerb_mech
= NULL
;
502 blob
= data_blob_const(*ppdata
, *p_data_size
);
504 status
= parse_spnego_mechanisms(talloc_tos(), blob
, &secblob
, &kerb_mech
);
505 if (!NT_STATUS_IS_OK(status
)) {
506 return nt_status_squash(status
);
509 /* We should have no partial context at this point. */
511 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
514 TALLOC_FREE(kerb_mech
);
516 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
517 status
= srv_enc_spnego_gss_negotiate(ppdata
, p_data_size
, secblob
);
519 /* Currently we don't SPNEGO negotiate
520 * back to NTLMSSP as we do in sessionsetupX. We should... */
521 return NT_STATUS_LOGON_FAILURE
;
524 status
= srv_enc_ntlm_negotiate(ppdata
, p_data_size
, secblob
, true);
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
->es
->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_srv_trans_enc_ctx
*ec
= partial_srv_trans_enc_ctx
;
564 /* We must have a partial context here. */
566 if (!ec
|| !ec
->es
|| ec
->auth_ntlmssp_state
== NULL
|| ec
->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
= auth_ntlmssp_update(ec
->auth_ntlmssp_state
, 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,ec
->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_srv_trans_enc_ctx
*ec
;
626 if (!partial_srv_trans_enc_ctx
) {
627 /* This is the initial step. */
628 status
= srv_enc_ntlm_negotiate(ppdata
, p_data_size
, blob
, false);
629 if (!NT_STATUS_EQUAL(status
,NT_STATUS_MORE_PROCESSING_REQUIRED
) && !NT_STATUS_IS_OK(status
)) {
630 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
631 return nt_status_squash(status
);
636 ec
= partial_srv_trans_enc_ctx
;
637 if (!ec
|| !ec
->es
|| ec
->auth_ntlmssp_state
== NULL
|| ec
->es
->smb_enc_type
!= SMB_TRANS_ENC_NTLM
) {
638 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
639 return NT_STATUS_INVALID_PARAMETER
;
643 status
= auth_ntlmssp_update(partial_srv_trans_enc_ctx
->auth_ntlmssp_state
, blob
, &response
);
645 if (NT_STATUS_IS_OK(status
)) {
646 /* Return the context we're using for this encryption state. */
647 if (!(*pparam
= SMB_MALLOC_ARRAY(unsigned char, 2))) {
648 return NT_STATUS_NO_MEMORY
;
650 SSVAL(*pparam
,0,ec
->es
->enc_ctx_num
);
654 /* Return the raw blob. */
656 *ppdata
= (unsigned char *)memdup(response
.data
, response
.length
);
657 if ((*ppdata
) == NULL
&& response
.length
> 0)
658 return NT_STATUS_NO_MEMORY
;
659 *p_data_size
= response
.length
;
660 data_blob_free(&response
);
664 /******************************************************************************
665 Do the SPNEGO encryption negotiation. Parameters are in/out.
666 ******************************************************************************/
668 NTSTATUS
srv_request_encryption_setup(connection_struct
*conn
,
669 unsigned char **ppdata
,
671 unsigned char **pparam
,
672 size_t *p_param_size
)
674 unsigned char *pdata
= *ppdata
;
679 if (*p_data_size
< 1) {
680 return NT_STATUS_INVALID_PARAMETER
;
683 if (pdata
[0] == ASN1_APPLICATION(0)) {
684 /* its a negTokenTarg packet */
685 return srv_enc_spnego_negotiate(conn
, ppdata
, p_data_size
, pparam
, p_param_size
);
688 if (pdata
[0] == ASN1_CONTEXT(1)) {
689 /* It's an auth packet */
690 return srv_enc_spnego_ntlm_auth(conn
, ppdata
, p_data_size
, pparam
, p_param_size
);
693 /* Maybe it's a raw unwrapped auth ? */
694 if (*p_data_size
< 7) {
695 return NT_STATUS_INVALID_PARAMETER
;
698 if (strncmp((char *)pdata
, "NTLMSSP", 7) == 0) {
699 return srv_enc_raw_ntlm_auth(conn
, ppdata
, p_data_size
, pparam
, p_param_size
);
702 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
704 return NT_STATUS_LOGON_FAILURE
;
707 /******************************************************************************
708 Negotiation was successful - turn on server-side encryption.
709 ******************************************************************************/
711 static NTSTATUS
check_enc_good(struct smb_srv_trans_enc_ctx
*ec
)
713 if (!ec
|| !ec
->es
) {
714 return NT_STATUS_LOGON_FAILURE
;
717 if (ec
->es
->smb_enc_type
== SMB_TRANS_ENC_NTLM
) {
718 if (!auth_ntlmssp_negotiated_sign((ec
->auth_ntlmssp_state
))) {
719 return NT_STATUS_INVALID_PARAMETER
;
722 if (!auth_ntlmssp_negotiated_seal((ec
->auth_ntlmssp_state
))) {
723 return NT_STATUS_INVALID_PARAMETER
;
726 /* Todo - check gssapi case. */
731 /******************************************************************************
732 Negotiation was successful - turn on server-side encryption.
733 ******************************************************************************/
735 NTSTATUS
srv_encryption_start(connection_struct
*conn
)
739 /* Check that we are really doing sign+seal. */
740 status
= check_enc_good(partial_srv_trans_enc_ctx
);
741 if (!NT_STATUS_IS_OK(status
)) {
744 /* Throw away the context we're using currently (if any). */
745 srv_free_encryption_context(&srv_trans_enc_ctx
);
747 /* Steal the partial pointer. Deliberate shallow copy. */
748 srv_trans_enc_ctx
= partial_srv_trans_enc_ctx
;
749 srv_trans_enc_ctx
->es
->enc_on
= true;
751 partial_srv_trans_enc_ctx
= NULL
;
753 DEBUG(1,("srv_encryption_start: context negotiated\n"));
757 /******************************************************************************
758 Shutdown all server contexts.
759 ******************************************************************************/
761 void server_encryption_shutdown(void)
763 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
764 srv_free_encryption_context(&srv_trans_enc_ctx
);