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/>.
22 /******************************************************************************
23 Server side encryption.
24 ******************************************************************************/
26 /******************************************************************************
28 ******************************************************************************/
30 struct smb_srv_trans_enc_ctx
{
31 struct smb_trans_enc_state
*es
;
32 AUTH_NTLMSSP_STATE
*auth_ntlmssp_state
; /* Must be kept in sync with pointer in ec->ntlmssp_state. */
35 static struct smb_srv_trans_enc_ctx
*partial_srv_trans_enc_ctx
;
36 static struct smb_srv_trans_enc_ctx
*srv_trans_enc_ctx
;
38 /******************************************************************************
39 Return global enc context - this must change if we ever do multiple contexts.
40 ******************************************************************************/
42 uint16_t srv_enc_ctx(void)
44 return srv_trans_enc_ctx
->es
->enc_ctx_num
;
47 /******************************************************************************
48 Is this an incoming encrypted packet ?
49 ******************************************************************************/
51 bool is_encrypted_packet(const uint8_t *inbuf
)
56 /* Ignore non-session messages or non 0xFF'E' messages. */
57 if(CVAL(inbuf
,0) || !(inbuf
[4] == 0xFF && inbuf
[5] == 'E')) {
61 status
= get_enc_ctx_num(inbuf
, &enc_num
);
62 if (!NT_STATUS_IS_OK(status
)) {
66 /* Encrypted messages are 0xFF'E'<ctx> */
67 if (srv_trans_enc_ctx
&& enc_num
== srv_enc_ctx()) {
73 /******************************************************************************
74 Create an auth_ntlmssp_state and ensure pointer copy is correct.
75 ******************************************************************************/
77 static NTSTATUS
make_auth_ntlmssp(struct smb_srv_trans_enc_ctx
*ec
)
79 NTSTATUS status
= auth_ntlmssp_start(&ec
->auth_ntlmssp_state
);
80 if (!NT_STATUS_IS_OK(status
)) {
81 return nt_status_squash(status
);
85 * We must remember to update the pointer copy for the common
86 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
88 ec
->es
->s
.ntlmssp_state
= ec
->auth_ntlmssp_state
->ntlmssp_state
;
92 /******************************************************************************
93 Destroy an auth_ntlmssp_state and ensure pointer copy is correct.
94 ******************************************************************************/
96 static void destroy_auth_ntlmssp(struct smb_srv_trans_enc_ctx
*ec
)
99 * We must remember to update the pointer copy for the common
100 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
103 if (ec
->auth_ntlmssp_state
) {
104 auth_ntlmssp_end(&ec
->auth_ntlmssp_state
);
105 /* The auth_ntlmssp_end killed this already. */
106 ec
->es
->s
.ntlmssp_state
= NULL
;
110 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
112 /******************************************************************************
114 ******************************************************************************/
116 static NTSTATUS
get_srv_gss_creds(const char *service
,
118 gss_cred_usage_t cred_type
,
119 gss_cred_id_t
*p_srv_cred
)
124 gss_buffer_desc input_name
;
125 char *host_princ_s
= NULL
;
126 NTSTATUS status
= NT_STATUS_OK
;
128 gss_OID_desc nt_hostbased_service
=
129 {10, CONST_DISCARD(char *,"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
131 asprintf(&host_princ_s
, "%s@%s", service
, name
);
132 if (host_princ_s
== NULL
) {
133 return NT_STATUS_NO_MEMORY
;
136 input_name
.value
= host_princ_s
;
137 input_name
.length
= strlen(host_princ_s
) + 1;
139 ret
= gss_import_name(&min
,
141 &nt_hostbased_service
,
144 DEBUG(10,("get_srv_gss_creds: imported name %s\n",
147 if (ret
!= GSS_S_COMPLETE
) {
148 SAFE_FREE(host_princ_s
);
149 return map_nt_error_from_gss(ret
, min
);
153 * We're accessing the krb5.keytab file here.
154 * ensure we have permissions to do so.
158 ret
= gss_acquire_cred(&min
,
168 if (ret
!= GSS_S_COMPLETE
) {
169 ADS_STATUS adss
= ADS_ERROR_GSS(ret
, min
);
170 DEBUG(10,("get_srv_gss_creds: gss_acquire_cred failed with %s\n",
172 status
= map_nt_error_from_gss(ret
, min
);
175 SAFE_FREE(host_princ_s
);
176 gss_release_name(&min
, &srv_name
);
180 /******************************************************************************
182 Try and get the cifs/server@realm principal first, then fall back to
184 ******************************************************************************/
186 static NTSTATUS
make_auth_gss(struct smb_srv_trans_enc_ctx
*ec
)
189 gss_cred_id_t srv_cred
;
192 name_to_fqdn(fqdn
, global_myname());
195 status
= get_srv_gss_creds("cifs", fqdn
, GSS_C_ACCEPT
, &srv_cred
);
196 if (!NT_STATUS_IS_OK(status
)) {
197 status
= get_srv_gss_creds("host", fqdn
, GSS_C_ACCEPT
, &srv_cred
);
198 if (!NT_STATUS_IS_OK(status
)) {
199 return nt_status_squash(status
);
203 ec
->es
->s
.gss_state
= SMB_MALLOC_P(struct smb_tran_enc_state_gss
);
204 if (!ec
->es
->s
.gss_state
) {
206 gss_release_cred(&min
, &srv_cred
);
207 return NT_STATUS_NO_MEMORY
;
209 ZERO_STRUCTP(ec
->es
->s
.gss_state
);
210 ec
->es
->s
.gss_state
->creds
= srv_cred
;
212 /* No context yet. */
213 ec
->es
->s
.gss_state
->gss_ctx
= GSS_C_NO_CONTEXT
;
219 /******************************************************************************
220 Shutdown a server encryption context.
221 ******************************************************************************/
223 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx
**pp_ec
)
225 struct smb_srv_trans_enc_ctx
*ec
= *pp_ec
;
232 switch (ec
->es
->smb_enc_type
) {
233 case SMB_TRANS_ENC_NTLM
:
234 destroy_auth_ntlmssp(ec
);
236 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
237 case SMB_TRANS_ENC_GSS
:
241 common_free_encryption_state(&ec
->es
);
248 /******************************************************************************
249 Create a server encryption context.
250 ******************************************************************************/
252 static NTSTATUS
make_srv_encryption_context(enum smb_trans_enc_type smb_enc_type
, struct smb_srv_trans_enc_ctx
**pp_ec
)
254 struct smb_srv_trans_enc_ctx
*ec
;
258 ec
= SMB_MALLOC_P(struct smb_srv_trans_enc_ctx
);
260 return NT_STATUS_NO_MEMORY
;
262 ZERO_STRUCTP(partial_srv_trans_enc_ctx
);
263 ec
->es
= SMB_MALLOC_P(struct smb_trans_enc_state
);
266 return NT_STATUS_NO_MEMORY
;
268 ZERO_STRUCTP(ec
->es
);
269 ec
->es
->smb_enc_type
= smb_enc_type
;
270 switch (smb_enc_type
) {
271 case SMB_TRANS_ENC_NTLM
:
273 NTSTATUS status
= make_auth_ntlmssp(ec
);
274 if (!NT_STATUS_IS_OK(status
)) {
275 srv_free_encryption_context(&ec
);
281 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
282 case SMB_TRANS_ENC_GSS
:
283 /* Acquire our credentials by calling gss_acquire_cred here. */
285 NTSTATUS status
= make_auth_gss(ec
);
286 if (!NT_STATUS_IS_OK(status
)) {
287 srv_free_encryption_context(&ec
);
294 srv_free_encryption_context(&ec
);
295 return NT_STATUS_INVALID_PARAMETER
;
301 /******************************************************************************
302 Free an encryption-allocated buffer.
303 ******************************************************************************/
305 void srv_free_enc_buffer(char *buf
)
307 /* We know this is an smb buffer, and we
308 * didn't malloc, only copy, for a keepalive,
309 * so ignore non-session messages. */
315 if (srv_trans_enc_ctx
) {
316 common_free_enc_buffer(srv_trans_enc_ctx
->es
, buf
);
320 /******************************************************************************
321 Decrypt an incoming buffer.
322 ******************************************************************************/
324 NTSTATUS
srv_decrypt_buffer(char *buf
)
326 /* Ignore non-session messages. */
331 if (srv_trans_enc_ctx
) {
332 return common_decrypt_buffer(srv_trans_enc_ctx
->es
, buf
);
338 /******************************************************************************
339 Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
340 ******************************************************************************/
342 NTSTATUS
srv_encrypt_buffer(char *buf
, char **buf_out
)
346 /* Ignore non-session messages. */
351 if (srv_trans_enc_ctx
) {
352 return common_encrypt_buffer(srv_trans_enc_ctx
->es
, buf
, buf_out
);
354 /* Not encrypting. */
358 /******************************************************************************
359 Do the gss encryption negotiation. Parameters are in/out.
360 Until success we do everything on the partial enc ctx.
361 ******************************************************************************/
363 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
364 static NTSTATUS
srv_enc_spnego_gss_negotiate(unsigned char **ppdata
, size_t *p_data_size
, DATA_BLOB secblob
)
369 gss_buffer_desc in_buf
, out_buf
;
370 struct smb_tran_enc_state_gss
*gss_state
;
371 DATA_BLOB auth_reply
= data_blob_null
;
372 DATA_BLOB response
= data_blob_null
;
375 if (!partial_srv_trans_enc_ctx
) {
376 status
= make_srv_encryption_context(SMB_TRANS_ENC_GSS
, &partial_srv_trans_enc_ctx
);
377 if (!NT_STATUS_IS_OK(status
)) {
382 gss_state
= partial_srv_trans_enc_ctx
->es
->s
.gss_state
;
384 in_buf
.value
= secblob
.data
;
385 in_buf
.length
= secblob
.length
;
387 out_buf
.value
= NULL
;
392 ret
= gss_accept_sec_context(&min
,
396 GSS_C_NO_CHANNEL_BINDINGS
,
398 NULL
, /* Ignore oids. */
399 &out_buf
, /* To return. */
401 NULL
, /* Ingore time. */
402 NULL
); /* Ignore delegated creds. */
405 status
= gss_err_to_ntstatus(ret
, min
);
406 if (ret
!= GSS_S_COMPLETE
&& ret
!= GSS_S_CONTINUE_NEEDED
) {
410 /* Ensure we've got sign+seal available. */
411 if (ret
== GSS_S_COMPLETE
) {
412 if ((flags
& (GSS_C_INTEG_FLAG
|GSS_C_CONF_FLAG
|GSS_C_REPLAY_FLAG
|GSS_C_SEQUENCE_FLAG
)) !=
413 (GSS_C_INTEG_FLAG
|GSS_C_CONF_FLAG
|GSS_C_REPLAY_FLAG
|GSS_C_SEQUENCE_FLAG
)) {
414 DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
415 "for SMB sealing.\n"));
416 gss_release_buffer(&min
, &out_buf
);
417 return NT_STATUS_ACCESS_DENIED
;
421 auth_reply
= data_blob(out_buf
.value
, out_buf
.length
);
422 gss_release_buffer(&min
, &out_buf
);
424 /* Wrap in SPNEGO. */
425 response
= spnego_gen_auth_response(&auth_reply
, status
, OID_KERBEROS5
);
426 data_blob_free(&auth_reply
);
429 *ppdata
= response
.data
;
430 *p_data_size
= response
.length
;
436 /******************************************************************************
437 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
438 Until success we do everything on the partial enc ctx.
439 ******************************************************************************/
441 static NTSTATUS
srv_enc_ntlm_negotiate(unsigned char **ppdata
, size_t *p_data_size
, DATA_BLOB secblob
, bool spnego_wrap
)
444 DATA_BLOB chal
= data_blob_null
;
445 DATA_BLOB response
= data_blob_null
;
447 status
= make_srv_encryption_context(SMB_TRANS_ENC_NTLM
, &partial_srv_trans_enc_ctx
);
448 if (!NT_STATUS_IS_OK(status
)) {
452 status
= auth_ntlmssp_update(partial_srv_trans_enc_ctx
->auth_ntlmssp_state
, secblob
, &chal
);
454 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
458 response
= spnego_gen_auth_response(&chal
, status
, OID_NTLMSSP
);
459 data_blob_free(&chal
);
461 /* Return the raw blob. */
466 *ppdata
= response
.data
;
467 *p_data_size
= response
.length
;
471 /******************************************************************************
472 Do the SPNEGO encryption negotiation. Parameters are in/out.
473 Based off code in smbd/sesssionsetup.c
474 Until success we do everything on the partial enc ctx.
475 ******************************************************************************/
477 static NTSTATUS
srv_enc_spnego_negotiate(connection_struct
*conn
,
478 unsigned char **ppdata
,
480 unsigned char **pparam
,
481 size_t *p_param_size
)
484 DATA_BLOB blob
= data_blob_null
;
485 DATA_BLOB secblob
= data_blob_null
;
486 char *kerb_mech
= NULL
;
488 blob
= data_blob_const(*ppdata
, *p_data_size
);
490 status
= parse_spnego_mechanisms(blob
, &secblob
, &kerb_mech
);
491 if (!NT_STATUS_IS_OK(status
)) {
492 return nt_status_squash(status
);
495 /* We should have no partial context at this point. */
497 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
500 SAFE_FREE(kerb_mech
);
502 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
503 status
= srv_enc_spnego_gss_negotiate(ppdata
, p_data_size
, secblob
);
505 /* Currently we don't SPNEGO negotiate
506 * back to NTLMSSP as we do in sessionsetupX. We should... */
507 return NT_STATUS_LOGON_FAILURE
;
510 status
= srv_enc_ntlm_negotiate(ppdata
, p_data_size
, secblob
, true);
513 data_blob_free(&secblob
);
515 if (!NT_STATUS_EQUAL(status
,NT_STATUS_MORE_PROCESSING_REQUIRED
) && !NT_STATUS_IS_OK(status
)) {
516 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
517 return nt_status_squash(status
);
520 if (NT_STATUS_IS_OK(status
)) {
521 /* Return the context we're using for this encryption state. */
522 if (!(*pparam
= SMB_MALLOC_ARRAY(unsigned char, 2))) {
523 return NT_STATUS_NO_MEMORY
;
525 SSVAL(*pparam
,0,partial_srv_trans_enc_ctx
->es
->enc_ctx_num
);
532 /******************************************************************************
533 Complete a SPNEGO encryption negotiation. Parameters are in/out.
534 We only get this for a NTLM auth second stage.
535 ******************************************************************************/
537 static NTSTATUS
srv_enc_spnego_ntlm_auth(connection_struct
*conn
,
538 unsigned char **ppdata
,
540 unsigned char **pparam
,
541 size_t *p_param_size
)
544 DATA_BLOB blob
= data_blob_null
;
545 DATA_BLOB auth
= data_blob_null
;
546 DATA_BLOB auth_reply
= data_blob_null
;
547 DATA_BLOB response
= data_blob_null
;
548 struct smb_srv_trans_enc_ctx
*ec
= partial_srv_trans_enc_ctx
;
550 /* We must have a partial context here. */
552 if (!ec
|| !ec
->es
|| ec
->auth_ntlmssp_state
== NULL
|| ec
->es
->smb_enc_type
!= SMB_TRANS_ENC_NTLM
) {
553 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
554 return NT_STATUS_INVALID_PARAMETER
;
557 blob
= data_blob_const(*ppdata
, *p_data_size
);
558 if (!spnego_parse_auth(blob
, &auth
)) {
559 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
560 return NT_STATUS_INVALID_PARAMETER
;
563 status
= auth_ntlmssp_update(ec
->auth_ntlmssp_state
, auth
, &auth_reply
);
564 data_blob_free(&auth
);
570 * This field SHALL only be present in the first reply from the
572 * So set mechOID to NULL here.
575 response
= spnego_gen_auth_response(&auth_reply
, status
, NULL
);
576 data_blob_free(&auth_reply
);
578 if (NT_STATUS_IS_OK(status
)) {
579 /* Return the context we're using for this encryption state. */
580 if (!(*pparam
= SMB_MALLOC_ARRAY(unsigned char, 2))) {
581 return NT_STATUS_NO_MEMORY
;
583 SSVAL(*pparam
,0,ec
->es
->enc_ctx_num
);
588 *ppdata
= response
.data
;
589 *p_data_size
= response
.length
;
593 /******************************************************************************
594 Raw NTLM encryption negotiation. Parameters are in/out.
595 This function does both steps.
596 ******************************************************************************/
598 static NTSTATUS
srv_enc_raw_ntlm_auth(connection_struct
*conn
,
599 unsigned char **ppdata
,
601 unsigned char **pparam
,
602 size_t *p_param_size
)
605 DATA_BLOB blob
= data_blob_const(*ppdata
, *p_data_size
);
606 DATA_BLOB response
= data_blob_null
;
607 struct smb_srv_trans_enc_ctx
*ec
;
609 if (!partial_srv_trans_enc_ctx
) {
610 /* This is the initial step. */
611 status
= srv_enc_ntlm_negotiate(ppdata
, p_data_size
, blob
, false);
612 if (!NT_STATUS_EQUAL(status
,NT_STATUS_MORE_PROCESSING_REQUIRED
) && !NT_STATUS_IS_OK(status
)) {
613 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
614 return nt_status_squash(status
);
619 ec
= partial_srv_trans_enc_ctx
;
620 if (!ec
|| !ec
->es
|| ec
->auth_ntlmssp_state
== NULL
|| ec
->es
->smb_enc_type
!= SMB_TRANS_ENC_NTLM
) {
621 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
622 return NT_STATUS_INVALID_PARAMETER
;
626 status
= auth_ntlmssp_update(partial_srv_trans_enc_ctx
->auth_ntlmssp_state
, blob
, &response
);
628 if (NT_STATUS_IS_OK(status
)) {
629 /* Return the context we're using for this encryption state. */
630 if (!(*pparam
= SMB_MALLOC_ARRAY(unsigned char, 2))) {
631 return NT_STATUS_NO_MEMORY
;
633 SSVAL(*pparam
,0,ec
->es
->enc_ctx_num
);
637 /* Return the raw blob. */
639 *ppdata
= response
.data
;
640 *p_data_size
= response
.length
;
644 /******************************************************************************
645 Do the SPNEGO encryption negotiation. Parameters are in/out.
646 ******************************************************************************/
648 NTSTATUS
srv_request_encryption_setup(connection_struct
*conn
,
649 unsigned char **ppdata
,
651 unsigned char **pparam
,
652 size_t *p_param_size
)
654 unsigned char *pdata
= *ppdata
;
659 if (*p_data_size
< 1) {
660 return NT_STATUS_INVALID_PARAMETER
;
663 if (pdata
[0] == ASN1_APPLICATION(0)) {
664 /* its a negTokenTarg packet */
665 return srv_enc_spnego_negotiate(conn
, ppdata
, p_data_size
, pparam
, p_param_size
);
668 if (pdata
[0] == ASN1_CONTEXT(1)) {
669 /* It's an auth packet */
670 return srv_enc_spnego_ntlm_auth(conn
, ppdata
, p_data_size
, pparam
, p_param_size
);
673 /* Maybe it's a raw unwrapped auth ? */
674 if (*p_data_size
< 7) {
675 return NT_STATUS_INVALID_PARAMETER
;
678 if (strncmp((char *)pdata
, "NTLMSSP", 7) == 0) {
679 return srv_enc_raw_ntlm_auth(conn
, ppdata
, p_data_size
, pparam
, p_param_size
);
682 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
684 return NT_STATUS_LOGON_FAILURE
;
687 /******************************************************************************
688 Negotiation was successful - turn on server-side encryption.
689 ******************************************************************************/
691 static NTSTATUS
check_enc_good(struct smb_srv_trans_enc_ctx
*ec
)
693 if (!ec
|| !ec
->es
) {
694 return NT_STATUS_LOGON_FAILURE
;
697 if (ec
->es
->smb_enc_type
== SMB_TRANS_ENC_NTLM
) {
698 if ((ec
->es
->s
.ntlmssp_state
->neg_flags
& (NTLMSSP_NEGOTIATE_SIGN
|NTLMSSP_NEGOTIATE_SEAL
)) !=
699 (NTLMSSP_NEGOTIATE_SIGN
|NTLMSSP_NEGOTIATE_SEAL
)) {
700 return NT_STATUS_INVALID_PARAMETER
;
703 /* Todo - check gssapi case. */
708 /******************************************************************************
709 Negotiation was successful - turn on server-side encryption.
710 ******************************************************************************/
712 NTSTATUS
srv_encryption_start(connection_struct
*conn
)
716 /* Check that we are really doing sign+seal. */
717 status
= check_enc_good(partial_srv_trans_enc_ctx
);
718 if (!NT_STATUS_IS_OK(status
)) {
721 /* Throw away the context we're using currently (if any). */
722 srv_free_encryption_context(&srv_trans_enc_ctx
);
724 /* Steal the partial pointer. Deliberate shallow copy. */
725 srv_trans_enc_ctx
= partial_srv_trans_enc_ctx
;
726 srv_trans_enc_ctx
->es
->enc_on
= true;
728 partial_srv_trans_enc_ctx
= NULL
;
730 DEBUG(1,("srv_encryption_start: context negotiated\n"));
734 /******************************************************************************
735 Shutdown all server contexts.
736 ******************************************************************************/
738 void server_encryption_shutdown(void)
740 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
741 srv_free_encryption_context(&srv_trans_enc_ctx
);