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"
24 /******************************************************************************
25 Server side encryption.
26 ******************************************************************************/
28 /******************************************************************************
30 ******************************************************************************/
32 struct smb_srv_trans_enc_ctx
{
33 struct smb_trans_enc_state
*es
;
34 AUTH_NTLMSSP_STATE
*auth_ntlmssp_state
; /* Must be kept in sync with pointer in ec->ntlmssp_state. */
37 /******************************************************************************
38 Return global enc context - this must change if we ever do multiple contexts.
39 ******************************************************************************/
41 uint16_t srv_enc_ctx(void)
43 return srv_trans_enc_ctx
->es
->enc_ctx_num
;
46 /******************************************************************************
47 Is this an incoming encrypted packet ?
48 ******************************************************************************/
50 bool is_encrypted_packet(const uint8_t *inbuf
)
55 /* Ignore non-session messages or non 0xFF'E' messages. */
56 if(CVAL(inbuf
,0) || !(inbuf
[4] == 0xFF && inbuf
[5] == 'E')) {
60 status
= get_enc_ctx_num(inbuf
, &enc_num
);
61 if (!NT_STATUS_IS_OK(status
)) {
65 /* Encrypted messages are 0xFF'E'<ctx> */
66 if (srv_trans_enc_ctx
&& enc_num
== srv_enc_ctx()) {
72 /******************************************************************************
73 Create an auth_ntlmssp_state and ensure pointer copy is correct.
74 ******************************************************************************/
76 static NTSTATUS
make_auth_ntlmssp(struct smb_srv_trans_enc_ctx
*ec
)
78 NTSTATUS status
= auth_ntlmssp_start(&ec
->auth_ntlmssp_state
);
79 if (!NT_STATUS_IS_OK(status
)) {
80 return nt_status_squash(status
);
84 * We must remember to update the pointer copy for the common
85 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
87 ec
->es
->s
.ntlmssp_state
= ec
->auth_ntlmssp_state
->ntlmssp_state
;
91 /******************************************************************************
92 Destroy an auth_ntlmssp_state and ensure pointer copy is correct.
93 ******************************************************************************/
95 static void destroy_auth_ntlmssp(struct smb_srv_trans_enc_ctx
*ec
)
98 * We must remember to update the pointer copy for the common
99 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
102 if (ec
->auth_ntlmssp_state
) {
103 auth_ntlmssp_end(&ec
->auth_ntlmssp_state
);
104 /* The auth_ntlmssp_end killed this already. */
105 ec
->es
->s
.ntlmssp_state
= NULL
;
109 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
111 /******************************************************************************
113 ******************************************************************************/
115 static NTSTATUS
get_srv_gss_creds(const char *service
,
117 gss_cred_usage_t cred_type
,
118 gss_cred_id_t
*p_srv_cred
)
123 gss_buffer_desc input_name
;
124 char *host_princ_s
= NULL
;
125 NTSTATUS status
= NT_STATUS_OK
;
127 gss_OID_desc nt_hostbased_service
=
128 {10, CONST_DISCARD(char *,"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
130 if (asprintf(&host_princ_s
, "%s@%s", service
, name
) == -1) {
131 return NT_STATUS_NO_MEMORY
;
134 input_name
.value
= host_princ_s
;
135 input_name
.length
= strlen(host_princ_s
) + 1;
137 ret
= gss_import_name(&min
,
139 &nt_hostbased_service
,
142 DEBUG(10,("get_srv_gss_creds: imported name %s\n",
145 if (ret
!= GSS_S_COMPLETE
) {
146 SAFE_FREE(host_princ_s
);
147 return map_nt_error_from_gss(ret
, min
);
151 * We're accessing the krb5.keytab file here.
152 * ensure we have permissions to do so.
156 ret
= gss_acquire_cred(&min
,
166 if (ret
!= GSS_S_COMPLETE
) {
167 ADS_STATUS adss
= ADS_ERROR_GSS(ret
, min
);
168 DEBUG(10,("get_srv_gss_creds: gss_acquire_cred failed with %s\n",
170 status
= map_nt_error_from_gss(ret
, min
);
173 SAFE_FREE(host_princ_s
);
174 gss_release_name(&min
, &srv_name
);
178 /******************************************************************************
180 Try and get the cifs/server@realm principal first, then fall back to
182 ******************************************************************************/
184 static NTSTATUS
make_auth_gss(struct smb_srv_trans_enc_ctx
*ec
)
187 gss_cred_id_t srv_cred
;
190 name_to_fqdn(fqdn
, global_myname());
193 status
= get_srv_gss_creds("cifs", fqdn
, GSS_C_ACCEPT
, &srv_cred
);
194 if (!NT_STATUS_IS_OK(status
)) {
195 status
= get_srv_gss_creds("host", fqdn
, GSS_C_ACCEPT
, &srv_cred
);
196 if (!NT_STATUS_IS_OK(status
)) {
197 return nt_status_squash(status
);
201 ec
->es
->s
.gss_state
= SMB_MALLOC_P(struct smb_tran_enc_state_gss
);
202 if (!ec
->es
->s
.gss_state
) {
204 gss_release_cred(&min
, &srv_cred
);
205 return NT_STATUS_NO_MEMORY
;
207 ZERO_STRUCTP(ec
->es
->s
.gss_state
);
208 ec
->es
->s
.gss_state
->creds
= srv_cred
;
210 /* No context yet. */
211 ec
->es
->s
.gss_state
->gss_ctx
= GSS_C_NO_CONTEXT
;
217 /******************************************************************************
218 Shutdown a server encryption context.
219 ******************************************************************************/
221 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx
**pp_ec
)
223 struct smb_srv_trans_enc_ctx
*ec
= *pp_ec
;
230 switch (ec
->es
->smb_enc_type
) {
231 case SMB_TRANS_ENC_NTLM
:
232 destroy_auth_ntlmssp(ec
);
234 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
235 case SMB_TRANS_ENC_GSS
:
239 common_free_encryption_state(&ec
->es
);
246 /******************************************************************************
247 Create a server encryption context.
248 ******************************************************************************/
250 static NTSTATUS
make_srv_encryption_context(enum smb_trans_enc_type smb_enc_type
, struct smb_srv_trans_enc_ctx
**pp_ec
)
252 struct smb_srv_trans_enc_ctx
*ec
;
256 ec
= SMB_MALLOC_P(struct smb_srv_trans_enc_ctx
);
258 return NT_STATUS_NO_MEMORY
;
260 ZERO_STRUCTP(partial_srv_trans_enc_ctx
);
261 ec
->es
= SMB_MALLOC_P(struct smb_trans_enc_state
);
264 return NT_STATUS_NO_MEMORY
;
266 ZERO_STRUCTP(ec
->es
);
267 ec
->es
->smb_enc_type
= smb_enc_type
;
268 switch (smb_enc_type
) {
269 case SMB_TRANS_ENC_NTLM
:
271 NTSTATUS status
= make_auth_ntlmssp(ec
);
272 if (!NT_STATUS_IS_OK(status
)) {
273 srv_free_encryption_context(&ec
);
279 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
280 case SMB_TRANS_ENC_GSS
:
281 /* Acquire our credentials by calling gss_acquire_cred here. */
283 NTSTATUS status
= make_auth_gss(ec
);
284 if (!NT_STATUS_IS_OK(status
)) {
285 srv_free_encryption_context(&ec
);
292 srv_free_encryption_context(&ec
);
293 return NT_STATUS_INVALID_PARAMETER
;
299 /******************************************************************************
300 Free an encryption-allocated buffer.
301 ******************************************************************************/
303 void srv_free_enc_buffer(char *buf
)
305 /* We know this is an smb buffer, and we
306 * didn't malloc, only copy, for a keepalive,
307 * so ignore non-session messages. */
313 if (srv_trans_enc_ctx
) {
314 common_free_enc_buffer(srv_trans_enc_ctx
->es
, buf
);
318 /******************************************************************************
319 Decrypt an incoming buffer.
320 ******************************************************************************/
322 NTSTATUS
srv_decrypt_buffer(char *buf
)
324 /* Ignore non-session messages. */
329 if (srv_trans_enc_ctx
) {
330 return common_decrypt_buffer(srv_trans_enc_ctx
->es
, buf
);
336 /******************************************************************************
337 Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
338 ******************************************************************************/
340 NTSTATUS
srv_encrypt_buffer(char *buf
, char **buf_out
)
344 /* Ignore non-session messages. */
349 if (srv_trans_enc_ctx
) {
350 return common_encrypt_buffer(srv_trans_enc_ctx
->es
, buf
, buf_out
);
352 /* Not encrypting. */
356 /******************************************************************************
357 Do the gss encryption negotiation. Parameters are in/out.
358 Until success we do everything on the partial enc ctx.
359 ******************************************************************************/
361 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
362 static NTSTATUS
srv_enc_spnego_gss_negotiate(unsigned char **ppdata
, size_t *p_data_size
, DATA_BLOB secblob
)
367 gss_buffer_desc in_buf
, out_buf
;
368 struct smb_tran_enc_state_gss
*gss_state
;
369 DATA_BLOB auth_reply
= data_blob_null
;
370 DATA_BLOB response
= data_blob_null
;
373 if (!partial_srv_trans_enc_ctx
) {
374 status
= make_srv_encryption_context(SMB_TRANS_ENC_GSS
, &partial_srv_trans_enc_ctx
);
375 if (!NT_STATUS_IS_OK(status
)) {
380 gss_state
= partial_srv_trans_enc_ctx
->es
->s
.gss_state
;
382 in_buf
.value
= secblob
.data
;
383 in_buf
.length
= secblob
.length
;
385 out_buf
.value
= NULL
;
390 ret
= gss_accept_sec_context(&min
,
394 GSS_C_NO_CHANNEL_BINDINGS
,
396 NULL
, /* Ignore oids. */
397 &out_buf
, /* To return. */
399 NULL
, /* Ingore time. */
400 NULL
); /* Ignore delegated creds. */
403 status
= gss_err_to_ntstatus(ret
, min
);
404 if (ret
!= GSS_S_COMPLETE
&& ret
!= GSS_S_CONTINUE_NEEDED
) {
408 /* Ensure we've got sign+seal available. */
409 if (ret
== GSS_S_COMPLETE
) {
410 if ((flags
& (GSS_C_INTEG_FLAG
|GSS_C_CONF_FLAG
|GSS_C_REPLAY_FLAG
|GSS_C_SEQUENCE_FLAG
)) !=
411 (GSS_C_INTEG_FLAG
|GSS_C_CONF_FLAG
|GSS_C_REPLAY_FLAG
|GSS_C_SEQUENCE_FLAG
)) {
412 DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
413 "for SMB sealing.\n"));
414 gss_release_buffer(&min
, &out_buf
);
415 return NT_STATUS_ACCESS_DENIED
;
419 auth_reply
= data_blob(out_buf
.value
, out_buf
.length
);
420 gss_release_buffer(&min
, &out_buf
);
422 /* Wrap in SPNEGO. */
423 response
= spnego_gen_auth_response(&auth_reply
, status
, OID_KERBEROS5
);
424 data_blob_free(&auth_reply
);
427 *ppdata
= (unsigned char *)memdup(response
.data
, response
.length
);
428 if ((*ppdata
) == NULL
&& response
.length
> 0) {
429 status
= NT_STATUS_NO_MEMORY
;
431 *p_data_size
= response
.length
;
433 data_blob_free(&response
);
439 /******************************************************************************
440 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
441 Until success we do everything on the partial enc ctx.
442 ******************************************************************************/
444 static NTSTATUS
srv_enc_ntlm_negotiate(unsigned char **ppdata
, size_t *p_data_size
, DATA_BLOB secblob
, bool spnego_wrap
)
447 DATA_BLOB chal
= data_blob_null
;
448 DATA_BLOB response
= data_blob_null
;
450 status
= make_srv_encryption_context(SMB_TRANS_ENC_NTLM
, &partial_srv_trans_enc_ctx
);
451 if (!NT_STATUS_IS_OK(status
)) {
455 status
= auth_ntlmssp_update(partial_srv_trans_enc_ctx
->auth_ntlmssp_state
, secblob
, &chal
);
457 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
461 response
= spnego_gen_auth_response(&chal
, status
, OID_NTLMSSP
);
462 data_blob_free(&chal
);
464 /* Return the raw blob. */
469 *ppdata
= (unsigned char *)memdup(response
.data
, response
.length
);
470 if ((*ppdata
) == NULL
&& response
.length
> 0) {
471 status
= NT_STATUS_NO_MEMORY
;
473 *p_data_size
= response
.length
;
474 data_blob_free(&response
);
479 /******************************************************************************
480 Do the SPNEGO encryption negotiation. Parameters are in/out.
481 Based off code in smbd/sesssionsetup.c
482 Until success we do everything on the partial enc ctx.
483 ******************************************************************************/
485 static NTSTATUS
srv_enc_spnego_negotiate(connection_struct
*conn
,
486 unsigned char **ppdata
,
488 unsigned char **pparam
,
489 size_t *p_param_size
)
492 DATA_BLOB blob
= data_blob_null
;
493 DATA_BLOB secblob
= data_blob_null
;
494 char *kerb_mech
= NULL
;
496 blob
= data_blob_const(*ppdata
, *p_data_size
);
498 status
= parse_spnego_mechanisms(blob
, &secblob
, &kerb_mech
);
499 if (!NT_STATUS_IS_OK(status
)) {
500 return nt_status_squash(status
);
503 /* We should have no partial context at this point. */
505 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
508 SAFE_FREE(kerb_mech
);
510 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
511 status
= srv_enc_spnego_gss_negotiate(ppdata
, p_data_size
, secblob
);
513 /* Currently we don't SPNEGO negotiate
514 * back to NTLMSSP as we do in sessionsetupX. We should... */
515 return NT_STATUS_LOGON_FAILURE
;
518 status
= srv_enc_ntlm_negotiate(ppdata
, p_data_size
, secblob
, true);
521 data_blob_free(&secblob
);
523 if (!NT_STATUS_EQUAL(status
,NT_STATUS_MORE_PROCESSING_REQUIRED
) && !NT_STATUS_IS_OK(status
)) {
524 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
525 return nt_status_squash(status
);
528 if (NT_STATUS_IS_OK(status
)) {
529 /* Return the context we're using for this encryption state. */
530 if (!(*pparam
= SMB_MALLOC_ARRAY(unsigned char, 2))) {
531 return NT_STATUS_NO_MEMORY
;
533 SSVAL(*pparam
,0,partial_srv_trans_enc_ctx
->es
->enc_ctx_num
);
540 /******************************************************************************
541 Complete a SPNEGO encryption negotiation. Parameters are in/out.
542 We only get this for a NTLM auth second stage.
543 ******************************************************************************/
545 static NTSTATUS
srv_enc_spnego_ntlm_auth(connection_struct
*conn
,
546 unsigned char **ppdata
,
548 unsigned char **pparam
,
549 size_t *p_param_size
)
552 DATA_BLOB blob
= data_blob_null
;
553 DATA_BLOB auth
= data_blob_null
;
554 DATA_BLOB auth_reply
= data_blob_null
;
555 DATA_BLOB response
= data_blob_null
;
556 struct smb_srv_trans_enc_ctx
*ec
= partial_srv_trans_enc_ctx
;
558 /* We must have a partial context here. */
560 if (!ec
|| !ec
->es
|| ec
->auth_ntlmssp_state
== NULL
|| ec
->es
->smb_enc_type
!= SMB_TRANS_ENC_NTLM
) {
561 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
562 return NT_STATUS_INVALID_PARAMETER
;
565 blob
= data_blob_const(*ppdata
, *p_data_size
);
566 if (!spnego_parse_auth(blob
, &auth
)) {
567 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
568 return NT_STATUS_INVALID_PARAMETER
;
571 status
= auth_ntlmssp_update(ec
->auth_ntlmssp_state
, auth
, &auth_reply
);
572 data_blob_free(&auth
);
578 * This field SHALL only be present in the first reply from the
580 * So set mechOID to NULL here.
583 response
= spnego_gen_auth_response(&auth_reply
, status
, NULL
);
584 data_blob_free(&auth_reply
);
586 if (NT_STATUS_IS_OK(status
)) {
587 /* Return the context we're using for this encryption state. */
588 if (!(*pparam
= SMB_MALLOC_ARRAY(unsigned char, 2))) {
589 return NT_STATUS_NO_MEMORY
;
591 SSVAL(*pparam
,0,ec
->es
->enc_ctx_num
);
596 *ppdata
= (unsigned char *)memdup(response
.data
, response
.length
);
597 if ((*ppdata
) == NULL
&& response
.length
> 0)
598 return NT_STATUS_NO_MEMORY
;
599 *p_data_size
= response
.length
;
600 data_blob_free(&response
);
604 /******************************************************************************
605 Raw NTLM encryption negotiation. Parameters are in/out.
606 This function does both steps.
607 ******************************************************************************/
609 static NTSTATUS
srv_enc_raw_ntlm_auth(connection_struct
*conn
,
610 unsigned char **ppdata
,
612 unsigned char **pparam
,
613 size_t *p_param_size
)
616 DATA_BLOB blob
= data_blob_const(*ppdata
, *p_data_size
);
617 DATA_BLOB response
= data_blob_null
;
618 struct smb_srv_trans_enc_ctx
*ec
;
620 if (!partial_srv_trans_enc_ctx
) {
621 /* This is the initial step. */
622 status
= srv_enc_ntlm_negotiate(ppdata
, p_data_size
, blob
, false);
623 if (!NT_STATUS_EQUAL(status
,NT_STATUS_MORE_PROCESSING_REQUIRED
) && !NT_STATUS_IS_OK(status
)) {
624 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
625 return nt_status_squash(status
);
630 ec
= partial_srv_trans_enc_ctx
;
631 if (!ec
|| !ec
->es
|| ec
->auth_ntlmssp_state
== NULL
|| ec
->es
->smb_enc_type
!= SMB_TRANS_ENC_NTLM
) {
632 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
633 return NT_STATUS_INVALID_PARAMETER
;
637 status
= auth_ntlmssp_update(partial_srv_trans_enc_ctx
->auth_ntlmssp_state
, blob
, &response
);
639 if (NT_STATUS_IS_OK(status
)) {
640 /* Return the context we're using for this encryption state. */
641 if (!(*pparam
= SMB_MALLOC_ARRAY(unsigned char, 2))) {
642 return NT_STATUS_NO_MEMORY
;
644 SSVAL(*pparam
,0,ec
->es
->enc_ctx_num
);
648 /* Return the raw blob. */
650 *ppdata
= (unsigned char *)memdup(response
.data
, response
.length
);
651 if ((*ppdata
) == NULL
&& response
.length
> 0)
652 return NT_STATUS_NO_MEMORY
;
653 *p_data_size
= response
.length
;
654 data_blob_free(&response
);
658 /******************************************************************************
659 Do the SPNEGO encryption negotiation. Parameters are in/out.
660 ******************************************************************************/
662 NTSTATUS
srv_request_encryption_setup(connection_struct
*conn
,
663 unsigned char **ppdata
,
665 unsigned char **pparam
,
666 size_t *p_param_size
)
668 unsigned char *pdata
= *ppdata
;
673 if (*p_data_size
< 1) {
674 return NT_STATUS_INVALID_PARAMETER
;
677 if (pdata
[0] == ASN1_APPLICATION(0)) {
678 /* its a negTokenTarg packet */
679 return srv_enc_spnego_negotiate(conn
, ppdata
, p_data_size
, pparam
, p_param_size
);
682 if (pdata
[0] == ASN1_CONTEXT(1)) {
683 /* It's an auth packet */
684 return srv_enc_spnego_ntlm_auth(conn
, ppdata
, p_data_size
, pparam
, p_param_size
);
687 /* Maybe it's a raw unwrapped auth ? */
688 if (*p_data_size
< 7) {
689 return NT_STATUS_INVALID_PARAMETER
;
692 if (strncmp((char *)pdata
, "NTLMSSP", 7) == 0) {
693 return srv_enc_raw_ntlm_auth(conn
, ppdata
, p_data_size
, pparam
, p_param_size
);
696 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
698 return NT_STATUS_LOGON_FAILURE
;
701 /******************************************************************************
702 Negotiation was successful - turn on server-side encryption.
703 ******************************************************************************/
705 static NTSTATUS
check_enc_good(struct smb_srv_trans_enc_ctx
*ec
)
707 if (!ec
|| !ec
->es
) {
708 return NT_STATUS_LOGON_FAILURE
;
711 if (ec
->es
->smb_enc_type
== SMB_TRANS_ENC_NTLM
) {
712 if ((ec
->es
->s
.ntlmssp_state
->neg_flags
& (NTLMSSP_NEGOTIATE_SIGN
|NTLMSSP_NEGOTIATE_SEAL
)) !=
713 (NTLMSSP_NEGOTIATE_SIGN
|NTLMSSP_NEGOTIATE_SEAL
)) {
714 return NT_STATUS_INVALID_PARAMETER
;
717 /* Todo - check gssapi case. */
722 /******************************************************************************
723 Negotiation was successful - turn on server-side encryption.
724 ******************************************************************************/
726 NTSTATUS
srv_encryption_start(connection_struct
*conn
)
730 /* Check that we are really doing sign+seal. */
731 status
= check_enc_good(partial_srv_trans_enc_ctx
);
732 if (!NT_STATUS_IS_OK(status
)) {
735 /* Throw away the context we're using currently (if any). */
736 srv_free_encryption_context(&srv_trans_enc_ctx
);
738 /* Steal the partial pointer. Deliberate shallow copy. */
739 srv_trans_enc_ctx
= partial_srv_trans_enc_ctx
;
740 srv_trans_enc_ctx
->es
->enc_on
= true;
742 partial_srv_trans_enc_ctx
= NULL
;
744 DEBUG(1,("srv_encryption_start: context negotiated\n"));
748 /******************************************************************************
749 Shutdown all server contexts.
750 ******************************************************************************/
752 void server_encryption_shutdown(void)
754 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
755 srv_free_encryption_context(&srv_trans_enc_ctx
);