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"
26 /******************************************************************************
27 Server side encryption.
28 ******************************************************************************/
30 /******************************************************************************
32 ******************************************************************************/
34 struct smb_srv_trans_enc_ctx
{
35 struct smb_trans_enc_state
*es
;
36 struct auth_ntlmssp_state
*auth_ntlmssp_state
; /* Must be kept in sync with pointer in ec->ntlmssp_state. */
39 /******************************************************************************
40 Return global enc context - this must change if we ever do multiple contexts.
41 ******************************************************************************/
43 uint16_t srv_enc_ctx(void)
45 return srv_trans_enc_ctx
->es
->enc_ctx_num
;
48 /******************************************************************************
49 Is this an incoming encrypted packet ?
50 ******************************************************************************/
52 bool is_encrypted_packet(const uint8_t *inbuf
)
57 /* Ignore non-session messages or non 0xFF'E' messages. */
59 || (smb_len(inbuf
) < 8)
60 || !(inbuf
[4] == 0xFF && inbuf
[5] == 'E')) {
64 status
= get_enc_ctx_num(inbuf
, &enc_num
);
65 if (!NT_STATUS_IS_OK(status
)) {
69 /* Encrypted messages are 0xFF'E'<ctx> */
70 if (srv_trans_enc_ctx
&& enc_num
== srv_enc_ctx()) {
76 /******************************************************************************
77 Create an auth_ntlmssp_state and ensure pointer copy is correct.
78 ******************************************************************************/
80 static NTSTATUS
make_auth_ntlmssp(struct smb_srv_trans_enc_ctx
*ec
)
82 NTSTATUS status
= auth_ntlmssp_start(&ec
->auth_ntlmssp_state
);
83 if (!NT_STATUS_IS_OK(status
)) {
84 return nt_status_squash(status
);
88 * We must remember to update the pointer copy for the common
89 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
91 ec
->es
->s
.ntlmssp_state
= auth_ntlmssp_get_ntlmssp_state(ec
->auth_ntlmssp_state
);
95 /******************************************************************************
96 Destroy an auth_ntlmssp_state and ensure pointer copy is correct.
97 ******************************************************************************/
99 static void destroy_auth_ntlmssp(struct smb_srv_trans_enc_ctx
*ec
)
102 * We must remember to update the pointer copy for the common
103 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
106 if (ec
->auth_ntlmssp_state
) {
107 TALLOC_FREE(ec
->auth_ntlmssp_state
);
108 /* The auth_ntlmssp_end killed this already. */
109 ec
->es
->s
.ntlmssp_state
= NULL
;
113 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
115 /******************************************************************************
117 ******************************************************************************/
119 static NTSTATUS
get_srv_gss_creds(const char *service
,
121 gss_cred_usage_t cred_type
,
122 gss_cred_id_t
*p_srv_cred
)
127 gss_buffer_desc input_name
;
128 char *host_princ_s
= NULL
;
129 NTSTATUS status
= NT_STATUS_OK
;
131 gss_OID_desc nt_hostbased_service
=
132 {10, CONST_DISCARD(char *,"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
134 if (asprintf(&host_princ_s
, "%s@%s", service
, name
) == -1) {
135 return NT_STATUS_NO_MEMORY
;
138 input_name
.value
= host_princ_s
;
139 input_name
.length
= strlen(host_princ_s
) + 1;
141 ret
= gss_import_name(&min
,
143 &nt_hostbased_service
,
146 DEBUG(10,("get_srv_gss_creds: imported name %s\n",
149 if (ret
!= GSS_S_COMPLETE
) {
150 SAFE_FREE(host_princ_s
);
151 return map_nt_error_from_gss(ret
, min
);
155 * We're accessing the krb5.keytab file here.
156 * ensure we have permissions to do so.
160 ret
= gss_acquire_cred(&min
,
170 if (ret
!= GSS_S_COMPLETE
) {
171 ADS_STATUS adss
= ADS_ERROR_GSS(ret
, min
);
172 DEBUG(10,("get_srv_gss_creds: gss_acquire_cred failed with %s\n",
174 status
= map_nt_error_from_gss(ret
, min
);
177 SAFE_FREE(host_princ_s
);
178 gss_release_name(&min
, &srv_name
);
182 /******************************************************************************
184 Try and get the cifs/server@realm principal first, then fall back to
186 ******************************************************************************/
188 static NTSTATUS
make_auth_gss(struct smb_srv_trans_enc_ctx
*ec
)
191 gss_cred_id_t srv_cred
;
194 name_to_fqdn(fqdn
, global_myname());
197 status
= get_srv_gss_creds("cifs", fqdn
, GSS_C_ACCEPT
, &srv_cred
);
198 if (!NT_STATUS_IS_OK(status
)) {
199 status
= get_srv_gss_creds("host", fqdn
, GSS_C_ACCEPT
, &srv_cred
);
200 if (!NT_STATUS_IS_OK(status
)) {
201 return nt_status_squash(status
);
205 ec
->es
->s
.gss_state
= SMB_MALLOC_P(struct smb_tran_enc_state_gss
);
206 if (!ec
->es
->s
.gss_state
) {
208 gss_release_cred(&min
, &srv_cred
);
209 return NT_STATUS_NO_MEMORY
;
211 ZERO_STRUCTP(ec
->es
->s
.gss_state
);
212 ec
->es
->s
.gss_state
->creds
= srv_cred
;
214 /* No context yet. */
215 ec
->es
->s
.gss_state
->gss_ctx
= GSS_C_NO_CONTEXT
;
221 /******************************************************************************
222 Shutdown a server encryption context.
223 ******************************************************************************/
225 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx
**pp_ec
)
227 struct smb_srv_trans_enc_ctx
*ec
= *pp_ec
;
234 switch (ec
->es
->smb_enc_type
) {
235 case SMB_TRANS_ENC_NTLM
:
236 destroy_auth_ntlmssp(ec
);
238 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
239 case SMB_TRANS_ENC_GSS
:
243 common_free_encryption_state(&ec
->es
);
250 /******************************************************************************
251 Create a server encryption context.
252 ******************************************************************************/
254 static NTSTATUS
make_srv_encryption_context(enum smb_trans_enc_type smb_enc_type
, struct smb_srv_trans_enc_ctx
**pp_ec
)
256 struct smb_srv_trans_enc_ctx
*ec
;
260 ec
= SMB_MALLOC_P(struct smb_srv_trans_enc_ctx
);
262 return NT_STATUS_NO_MEMORY
;
264 ZERO_STRUCTP(partial_srv_trans_enc_ctx
);
265 ec
->es
= SMB_MALLOC_P(struct smb_trans_enc_state
);
268 return NT_STATUS_NO_MEMORY
;
270 ZERO_STRUCTP(ec
->es
);
271 ec
->es
->smb_enc_type
= smb_enc_type
;
272 switch (smb_enc_type
) {
273 case SMB_TRANS_ENC_NTLM
:
275 NTSTATUS status
= make_auth_ntlmssp(ec
);
276 if (!NT_STATUS_IS_OK(status
)) {
277 srv_free_encryption_context(&ec
);
283 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
284 case SMB_TRANS_ENC_GSS
:
285 /* Acquire our credentials by calling gss_acquire_cred here. */
287 NTSTATUS status
= make_auth_gss(ec
);
288 if (!NT_STATUS_IS_OK(status
)) {
289 srv_free_encryption_context(&ec
);
296 srv_free_encryption_context(&ec
);
297 return NT_STATUS_INVALID_PARAMETER
;
303 /******************************************************************************
304 Free an encryption-allocated buffer.
305 ******************************************************************************/
307 void srv_free_enc_buffer(char *buf
)
309 /* We know this is an smb buffer, and we
310 * didn't malloc, only copy, for a keepalive,
311 * so ignore non-session messages. */
317 if (srv_trans_enc_ctx
) {
318 common_free_enc_buffer(srv_trans_enc_ctx
->es
, buf
);
322 /******************************************************************************
323 Decrypt an incoming buffer.
324 ******************************************************************************/
326 NTSTATUS
srv_decrypt_buffer(char *buf
)
328 /* Ignore non-session messages. */
333 if (srv_trans_enc_ctx
) {
334 return common_decrypt_buffer(srv_trans_enc_ctx
->es
, buf
);
340 /******************************************************************************
341 Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
342 ******************************************************************************/
344 NTSTATUS
srv_encrypt_buffer(char *buf
, char **buf_out
)
348 /* Ignore non-session messages. */
353 if (srv_trans_enc_ctx
) {
354 return common_encrypt_buffer(srv_trans_enc_ctx
->es
, buf
, buf_out
);
356 /* Not encrypting. */
360 /******************************************************************************
361 Do the gss encryption negotiation. Parameters are in/out.
362 Until success we do everything on the partial enc ctx.
363 ******************************************************************************/
365 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
366 static NTSTATUS
srv_enc_spnego_gss_negotiate(unsigned char **ppdata
, size_t *p_data_size
, DATA_BLOB secblob
)
371 gss_buffer_desc in_buf
, out_buf
;
372 struct smb_tran_enc_state_gss
*gss_state
;
373 DATA_BLOB auth_reply
= data_blob_null
;
374 DATA_BLOB response
= data_blob_null
;
377 if (!partial_srv_trans_enc_ctx
) {
378 status
= make_srv_encryption_context(SMB_TRANS_ENC_GSS
, &partial_srv_trans_enc_ctx
);
379 if (!NT_STATUS_IS_OK(status
)) {
384 gss_state
= partial_srv_trans_enc_ctx
->es
->s
.gss_state
;
386 in_buf
.value
= secblob
.data
;
387 in_buf
.length
= secblob
.length
;
389 out_buf
.value
= NULL
;
394 ret
= gss_accept_sec_context(&min
,
398 GSS_C_NO_CHANNEL_BINDINGS
,
400 NULL
, /* Ignore oids. */
401 &out_buf
, /* To return. */
403 NULL
, /* Ingore time. */
404 NULL
); /* Ignore delegated creds. */
407 status
= gss_err_to_ntstatus(ret
, min
);
408 if (ret
!= GSS_S_COMPLETE
&& ret
!= GSS_S_CONTINUE_NEEDED
) {
412 /* Ensure we've got sign+seal available. */
413 if (ret
== GSS_S_COMPLETE
) {
414 if ((flags
& (GSS_C_INTEG_FLAG
|GSS_C_CONF_FLAG
|GSS_C_REPLAY_FLAG
|GSS_C_SEQUENCE_FLAG
)) !=
415 (GSS_C_INTEG_FLAG
|GSS_C_CONF_FLAG
|GSS_C_REPLAY_FLAG
|GSS_C_SEQUENCE_FLAG
)) {
416 DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
417 "for SMB sealing.\n"));
418 gss_release_buffer(&min
, &out_buf
);
419 return NT_STATUS_ACCESS_DENIED
;
423 auth_reply
= data_blob(out_buf
.value
, out_buf
.length
);
424 gss_release_buffer(&min
, &out_buf
);
426 /* Wrap in SPNEGO. */
427 response
= spnego_gen_auth_response(talloc_tos(), &auth_reply
, status
, OID_KERBEROS5
);
428 data_blob_free(&auth_reply
);
431 *ppdata
= (unsigned char *)memdup(response
.data
, response
.length
);
432 if ((*ppdata
) == NULL
&& response
.length
> 0) {
433 status
= NT_STATUS_NO_MEMORY
;
435 *p_data_size
= response
.length
;
437 data_blob_free(&response
);
443 /******************************************************************************
444 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
445 Until success we do everything on the partial enc ctx.
446 ******************************************************************************/
448 static NTSTATUS
srv_enc_ntlm_negotiate(unsigned char **ppdata
, size_t *p_data_size
, DATA_BLOB secblob
, bool spnego_wrap
)
451 DATA_BLOB chal
= data_blob_null
;
452 DATA_BLOB response
= data_blob_null
;
454 status
= make_srv_encryption_context(SMB_TRANS_ENC_NTLM
, &partial_srv_trans_enc_ctx
);
455 if (!NT_STATUS_IS_OK(status
)) {
459 status
= auth_ntlmssp_update(partial_srv_trans_enc_ctx
->auth_ntlmssp_state
, secblob
, &chal
);
461 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
465 response
= spnego_gen_auth_response(talloc_tos(), &chal
, status
, OID_NTLMSSP
);
466 data_blob_free(&chal
);
468 /* Return the raw blob. */
473 *ppdata
= (unsigned char *)memdup(response
.data
, response
.length
);
474 if ((*ppdata
) == NULL
&& response
.length
> 0) {
475 status
= NT_STATUS_NO_MEMORY
;
477 *p_data_size
= response
.length
;
478 data_blob_free(&response
);
483 /******************************************************************************
484 Do the SPNEGO encryption negotiation. Parameters are in/out.
485 Based off code in smbd/sesssionsetup.c
486 Until success we do everything on the partial enc ctx.
487 ******************************************************************************/
489 static NTSTATUS
srv_enc_spnego_negotiate(connection_struct
*conn
,
490 unsigned char **ppdata
,
492 unsigned char **pparam
,
493 size_t *p_param_size
)
496 DATA_BLOB blob
= data_blob_null
;
497 DATA_BLOB secblob
= data_blob_null
;
498 char *kerb_mech
= NULL
;
500 blob
= data_blob_const(*ppdata
, *p_data_size
);
502 status
= parse_spnego_mechanisms(talloc_tos(), blob
, &secblob
, &kerb_mech
);
503 if (!NT_STATUS_IS_OK(status
)) {
504 return nt_status_squash(status
);
507 /* We should have no partial context at this point. */
509 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
512 TALLOC_FREE(kerb_mech
);
514 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
515 status
= srv_enc_spnego_gss_negotiate(ppdata
, p_data_size
, secblob
);
517 /* Currently we don't SPNEGO negotiate
518 * back to NTLMSSP as we do in sessionsetupX. We should... */
519 return NT_STATUS_LOGON_FAILURE
;
522 status
= srv_enc_ntlm_negotiate(ppdata
, p_data_size
, secblob
, true);
525 data_blob_free(&secblob
);
527 if (!NT_STATUS_EQUAL(status
,NT_STATUS_MORE_PROCESSING_REQUIRED
) && !NT_STATUS_IS_OK(status
)) {
528 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
529 return nt_status_squash(status
);
532 if (NT_STATUS_IS_OK(status
)) {
533 /* Return the context we're using for this encryption state. */
534 if (!(*pparam
= SMB_MALLOC_ARRAY(unsigned char, 2))) {
535 return NT_STATUS_NO_MEMORY
;
537 SSVAL(*pparam
,0,partial_srv_trans_enc_ctx
->es
->enc_ctx_num
);
544 /******************************************************************************
545 Complete a SPNEGO encryption negotiation. Parameters are in/out.
546 We only get this for a NTLM auth second stage.
547 ******************************************************************************/
549 static NTSTATUS
srv_enc_spnego_ntlm_auth(connection_struct
*conn
,
550 unsigned char **ppdata
,
552 unsigned char **pparam
,
553 size_t *p_param_size
)
556 DATA_BLOB blob
= data_blob_null
;
557 DATA_BLOB auth
= data_blob_null
;
558 DATA_BLOB auth_reply
= data_blob_null
;
559 DATA_BLOB response
= data_blob_null
;
560 struct smb_srv_trans_enc_ctx
*ec
= partial_srv_trans_enc_ctx
;
562 /* We must have a partial context here. */
564 if (!ec
|| !ec
->es
|| ec
->auth_ntlmssp_state
== NULL
|| ec
->es
->smb_enc_type
!= SMB_TRANS_ENC_NTLM
) {
565 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
566 return NT_STATUS_INVALID_PARAMETER
;
569 blob
= data_blob_const(*ppdata
, *p_data_size
);
570 if (!spnego_parse_auth(talloc_tos(), blob
, &auth
)) {
571 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
572 return NT_STATUS_INVALID_PARAMETER
;
575 status
= auth_ntlmssp_update(ec
->auth_ntlmssp_state
, auth
, &auth_reply
);
576 data_blob_free(&auth
);
582 * This field SHALL only be present in the first reply from the
584 * So set mechOID to NULL here.
587 response
= spnego_gen_auth_response(talloc_tos(), &auth_reply
, status
, NULL
);
588 data_blob_free(&auth_reply
);
590 if (NT_STATUS_IS_OK(status
)) {
591 /* Return the context we're using for this encryption state. */
592 if (!(*pparam
= SMB_MALLOC_ARRAY(unsigned char, 2))) {
593 return NT_STATUS_NO_MEMORY
;
595 SSVAL(*pparam
,0,ec
->es
->enc_ctx_num
);
600 *ppdata
= (unsigned char *)memdup(response
.data
, response
.length
);
601 if ((*ppdata
) == NULL
&& response
.length
> 0)
602 return NT_STATUS_NO_MEMORY
;
603 *p_data_size
= response
.length
;
604 data_blob_free(&response
);
608 /******************************************************************************
609 Raw NTLM encryption negotiation. Parameters are in/out.
610 This function does both steps.
611 ******************************************************************************/
613 static NTSTATUS
srv_enc_raw_ntlm_auth(connection_struct
*conn
,
614 unsigned char **ppdata
,
616 unsigned char **pparam
,
617 size_t *p_param_size
)
620 DATA_BLOB blob
= data_blob_const(*ppdata
, *p_data_size
);
621 DATA_BLOB response
= data_blob_null
;
622 struct smb_srv_trans_enc_ctx
*ec
;
624 if (!partial_srv_trans_enc_ctx
) {
625 /* This is the initial step. */
626 status
= srv_enc_ntlm_negotiate(ppdata
, p_data_size
, blob
, false);
627 if (!NT_STATUS_EQUAL(status
,NT_STATUS_MORE_PROCESSING_REQUIRED
) && !NT_STATUS_IS_OK(status
)) {
628 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
629 return nt_status_squash(status
);
634 ec
= partial_srv_trans_enc_ctx
;
635 if (!ec
|| !ec
->es
|| ec
->auth_ntlmssp_state
== NULL
|| ec
->es
->smb_enc_type
!= SMB_TRANS_ENC_NTLM
) {
636 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
637 return NT_STATUS_INVALID_PARAMETER
;
641 status
= auth_ntlmssp_update(partial_srv_trans_enc_ctx
->auth_ntlmssp_state
, blob
, &response
);
643 if (NT_STATUS_IS_OK(status
)) {
644 /* Return the context we're using for this encryption state. */
645 if (!(*pparam
= SMB_MALLOC_ARRAY(unsigned char, 2))) {
646 return NT_STATUS_NO_MEMORY
;
648 SSVAL(*pparam
,0,ec
->es
->enc_ctx_num
);
652 /* Return the raw blob. */
654 *ppdata
= (unsigned char *)memdup(response
.data
, response
.length
);
655 if ((*ppdata
) == NULL
&& response
.length
> 0)
656 return NT_STATUS_NO_MEMORY
;
657 *p_data_size
= response
.length
;
658 data_blob_free(&response
);
662 /******************************************************************************
663 Do the SPNEGO encryption negotiation. Parameters are in/out.
664 ******************************************************************************/
666 NTSTATUS
srv_request_encryption_setup(connection_struct
*conn
,
667 unsigned char **ppdata
,
669 unsigned char **pparam
,
670 size_t *p_param_size
)
672 unsigned char *pdata
= *ppdata
;
677 if (*p_data_size
< 1) {
678 return NT_STATUS_INVALID_PARAMETER
;
681 if (pdata
[0] == ASN1_APPLICATION(0)) {
682 /* its a negTokenTarg packet */
683 return srv_enc_spnego_negotiate(conn
, ppdata
, p_data_size
, pparam
, p_param_size
);
686 if (pdata
[0] == ASN1_CONTEXT(1)) {
687 /* It's an auth packet */
688 return srv_enc_spnego_ntlm_auth(conn
, ppdata
, p_data_size
, pparam
, p_param_size
);
691 /* Maybe it's a raw unwrapped auth ? */
692 if (*p_data_size
< 7) {
693 return NT_STATUS_INVALID_PARAMETER
;
696 if (strncmp((char *)pdata
, "NTLMSSP", 7) == 0) {
697 return srv_enc_raw_ntlm_auth(conn
, ppdata
, p_data_size
, pparam
, p_param_size
);
700 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
702 return NT_STATUS_LOGON_FAILURE
;
705 /******************************************************************************
706 Negotiation was successful - turn on server-side encryption.
707 ******************************************************************************/
709 static NTSTATUS
check_enc_good(struct smb_srv_trans_enc_ctx
*ec
)
711 if (!ec
|| !ec
->es
) {
712 return NT_STATUS_LOGON_FAILURE
;
715 if (ec
->es
->smb_enc_type
== SMB_TRANS_ENC_NTLM
) {
716 if (!auth_ntlmssp_negotiated_sign((ec
->auth_ntlmssp_state
))) {
717 return NT_STATUS_INVALID_PARAMETER
;
720 if (!auth_ntlmssp_negotiated_seal((ec
->auth_ntlmssp_state
))) {
721 return NT_STATUS_INVALID_PARAMETER
;
724 /* Todo - check gssapi case. */
729 /******************************************************************************
730 Negotiation was successful - turn on server-side encryption.
731 ******************************************************************************/
733 NTSTATUS
srv_encryption_start(connection_struct
*conn
)
737 /* Check that we are really doing sign+seal. */
738 status
= check_enc_good(partial_srv_trans_enc_ctx
);
739 if (!NT_STATUS_IS_OK(status
)) {
742 /* Throw away the context we're using currently (if any). */
743 srv_free_encryption_context(&srv_trans_enc_ctx
);
745 /* Steal the partial pointer. Deliberate shallow copy. */
746 srv_trans_enc_ctx
= partial_srv_trans_enc_ctx
;
747 srv_trans_enc_ctx
->es
->enc_on
= true;
749 partial_srv_trans_enc_ctx
= NULL
;
751 DEBUG(1,("srv_encryption_start: context negotiated\n"));
755 /******************************************************************************
756 Shutdown all server contexts.
757 ******************************************************************************/
759 void server_encryption_shutdown(void)
761 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
762 srv_free_encryption_context(&srv_trans_enc_ctx
);