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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /******************************************************************************
24 Server side encryption.
25 ******************************************************************************/
27 /******************************************************************************
29 ******************************************************************************/
31 struct smb_srv_trans_enc_ctx
{
32 struct smb_trans_enc_state
*es
;
33 AUTH_NTLMSSP_STATE
*auth_ntlmssp_state
; /* Must be kept in sync with pointer in ec->ntlmssp_state. */
36 static struct smb_srv_trans_enc_ctx
*partial_srv_trans_enc_ctx
;
37 static struct smb_srv_trans_enc_ctx
*srv_trans_enc_ctx
;
39 /******************************************************************************
40 Is server encryption on ?
41 ******************************************************************************/
43 BOOL
srv_encryption_on(void)
45 if (srv_trans_enc_ctx
) {
46 return common_encryption_on(srv_trans_enc_ctx
->es
);
51 /******************************************************************************
52 Create an auth_ntlmssp_state and ensure pointer copy is correct.
53 ******************************************************************************/
55 static NTSTATUS
make_auth_ntlmssp(struct smb_srv_trans_enc_ctx
*ec
)
57 NTSTATUS status
= auth_ntlmssp_start(&ec
->auth_ntlmssp_state
);
58 if (!NT_STATUS_IS_OK(status
)) {
59 return nt_status_squash(status
);
63 * We must remember to update the pointer copy for the common
64 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
66 ec
->es
->s
.ntlmssp_state
= ec
->auth_ntlmssp_state
->ntlmssp_state
;
70 /******************************************************************************
71 Destroy an auth_ntlmssp_state and ensure pointer copy is correct.
72 ******************************************************************************/
74 static void destroy_auth_ntlmssp(struct smb_srv_trans_enc_ctx
*ec
)
77 * We must remember to update the pointer copy for the common
78 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
81 if (ec
->auth_ntlmssp_state
) {
82 auth_ntlmssp_end(&ec
->auth_ntlmssp_state
);
83 /* The auth_ntlmssp_end killed this already. */
84 ec
->es
->s
.ntlmssp_state
= NULL
;
88 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
90 /******************************************************************************
92 ******************************************************************************/
94 static NTSTATUS
get_srv_gss_creds(const char *service
,
96 gss_cred_usage_t cred_type
,
97 gss_cred_id_t
*p_srv_cred
)
102 gss_buffer_desc input_name
;
103 char *host_princ_s
= NULL
;
104 NTSTATUS status
= NT_STATUS_OK
;
106 gss_OID_desc nt_hostbased_service
=
107 {10, CONST_DISCARD(char *,"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
109 asprintf(&host_princ_s
, "%s@%s", service
, name
);
110 if (host_princ_s
== NULL
) {
111 return NT_STATUS_NO_MEMORY
;
114 input_name
.value
= host_princ_s
;
115 input_name
.length
= strlen(host_princ_s
) + 1;
117 ret
= gss_import_name(&min
,
119 &nt_hostbased_service
,
122 if (ret
!= GSS_S_COMPLETE
) {
123 SAFE_FREE(host_princ_s
);
124 return map_nt_error_from_gss(ret
, min
);
127 ret
= gss_acquire_cred(&min
,
136 if (ret
!= GSS_S_COMPLETE
) {
137 status
= map_nt_error_from_gss(ret
, min
);
140 SAFE_FREE(host_princ_s
);
141 gss_release_name(&min
, &srv_name
);
145 /******************************************************************************
147 Try and get the cifs/server@realm principal first, then fall back to
149 ******************************************************************************/
151 static NTSTATUS
make_auth_gss(struct smb_srv_trans_enc_ctx
*ec
)
154 gss_cred_id_t srv_cred
;
157 name_to_fqdn(fqdn
, global_myname());
160 status
= get_srv_gss_creds("cifs", fqdn
, GSS_C_ACCEPT
, &srv_cred
);
161 if (!NT_STATUS_IS_OK(status
)) {
162 status
= get_srv_gss_creds("host", fqdn
, GSS_C_ACCEPT
, &srv_cred
);
163 if (!NT_STATUS_IS_OK(status
)) {
164 return nt_status_squash(status
);
168 ec
->es
->s
.gss_state
= SMB_MALLOC_P(struct smb_tran_enc_state_gss
);
169 if (!ec
->es
->s
.gss_state
) {
171 gss_release_cred(&min
, &srv_cred
);
172 return NT_STATUS_NO_MEMORY
;
174 ZERO_STRUCTP(ec
->es
->s
.gss_state
);
175 ec
->es
->s
.gss_state
->creds
= srv_cred
;
177 /* No context yet. */
178 ec
->es
->s
.gss_state
->gss_ctx
= GSS_C_NO_CONTEXT
;
184 /******************************************************************************
185 Shutdown a server encryption context.
186 ******************************************************************************/
188 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx
**pp_ec
)
190 struct smb_srv_trans_enc_ctx
*ec
= *pp_ec
;
197 switch (ec
->es
->smb_enc_type
) {
198 case SMB_TRANS_ENC_NTLM
:
199 destroy_auth_ntlmssp(ec
);
201 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
202 case SMB_TRANS_ENC_GSS
:
206 common_free_encryption_state(&ec
->es
);
213 /******************************************************************************
214 Create a server encryption context.
215 ******************************************************************************/
217 static NTSTATUS
make_srv_encryption_context(enum smb_trans_enc_type smb_enc_type
, struct smb_srv_trans_enc_ctx
**pp_ec
)
219 struct smb_srv_trans_enc_ctx
*ec
;
223 ec
= SMB_MALLOC_P(struct smb_srv_trans_enc_ctx
);
225 return NT_STATUS_NO_MEMORY
;
227 ZERO_STRUCTP(partial_srv_trans_enc_ctx
);
228 ec
->es
= SMB_MALLOC_P(struct smb_trans_enc_state
);
231 return NT_STATUS_NO_MEMORY
;
233 ZERO_STRUCTP(ec
->es
);
234 ec
->es
->smb_enc_type
= smb_enc_type
;
235 switch (smb_enc_type
) {
236 case SMB_TRANS_ENC_NTLM
:
238 NTSTATUS status
= make_auth_ntlmssp(ec
);
239 if (!NT_STATUS_IS_OK(status
)) {
240 srv_free_encryption_context(&ec
);
246 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
247 case SMB_TRANS_ENC_GSS
:
248 /* Acquire our credentials by calling gss_acquire_cred here. */
250 NTSTATUS status
= make_auth_gss(ec
);
251 if (!NT_STATUS_IS_OK(status
)) {
252 srv_free_encryption_context(&ec
);
259 srv_free_encryption_context(&ec
);
260 return NT_STATUS_INVALID_PARAMETER
;
266 /******************************************************************************
267 Free an encryption-allocated buffer.
268 ******************************************************************************/
270 void srv_free_enc_buffer(char *buf
)
272 /* We know this is an smb buffer, and we
273 * didn't malloc, only copy, for a keepalive,
274 * so ignore session keepalives. */
276 if(CVAL(buf
,0) == SMBkeepalive
) {
280 if (srv_trans_enc_ctx
) {
281 common_free_enc_buffer(srv_trans_enc_ctx
->es
, buf
);
285 /******************************************************************************
286 Decrypt an incoming buffer.
287 ******************************************************************************/
289 NTSTATUS
srv_decrypt_buffer(char *buf
)
291 /* Ignore session keepalives. */
292 if(CVAL(buf
,0) == SMBkeepalive
) {
296 if (srv_trans_enc_ctx
) {
297 return common_decrypt_buffer(srv_trans_enc_ctx
->es
, buf
);
303 /******************************************************************************
304 Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
305 ******************************************************************************/
307 NTSTATUS
srv_encrypt_buffer(char *buf
, char **buf_out
)
311 /* Ignore session keepalives. */
312 if(CVAL(buf
,0) == SMBkeepalive
) {
316 if (srv_trans_enc_ctx
) {
317 return common_encrypt_buffer(srv_trans_enc_ctx
->es
, buf
, buf_out
);
319 /* Not encrypting. */
323 /******************************************************************************
324 Do the gss encryption negotiation. Parameters are in/out.
325 Until success we do everything on the partial enc ctx.
326 ******************************************************************************/
328 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
329 static NTSTATUS
srv_enc_spnego_gss_negotiate(unsigned char **ppdata
, size_t *p_data_size
, DATA_BLOB secblob
)
334 gss_buffer_desc in_buf
, out_buf
;
335 struct smb_tran_enc_state_gss
*gss_state
;
337 if (!partial_srv_trans_enc_ctx
) {
338 NTSTATUS status
= make_srv_encryption_context(SMB_TRANS_ENC_GSS
, &partial_srv_trans_enc_ctx
);
339 if (!NT_STATUS_IS_OK(status
)) {
344 gss_state
= partial_srv_trans_enc_ctx
->es
->s
.gss_state
;
346 in_buf
.value
= secblob
.data
;
347 in_buf
.length
= secblob
.length
;
349 out_buf
.value
= NULL
;
352 ret
= gss_accept_sec_context(&min
,
356 GSS_C_NO_CHANNEL_BINDINGS
,
358 NULL
, /* Ignore oids. */
359 &out_buf
, /* To return. */
361 NULL
, /* Ingore time. */
362 NULL
); /* Ignore delegated creds. */
364 if (ret
!= GSS_S_COMPLETE
&& ret
!= GSS_S_CONTINUE_NEEDED
) {
365 return gss_err_to_ntstatus(ret
, min
);
368 /* Ensure we've got sign+seal available. */
369 if (ret
== GSS_S_COMPLETE
) {
370 if ((flags
& (GSS_C_INTEG_FLAG
|GSS_C_CONF_FLAG
|GSS_C_REPLAY_FLAG
|GSS_C_SEQUENCE_FLAG
)) !=
371 (GSS_C_INTEG_FLAG
|GSS_C_CONF_FLAG
|GSS_C_REPLAY_FLAG
|GSS_C_SEQUENCE_FLAG
)) {
372 DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
373 "for SMB sealing.\n"));
374 gss_release_buffer(&min
, &out_buf
);
375 return NT_STATUS_ACCESS_DENIED
;
380 *ppdata
= memdup(out_buf
.value
, out_buf
.length
);
382 gss_release_buffer(&min
, &out_buf
);
383 return NT_STATUS_NO_MEMORY
;
385 *p_data_size
= out_buf
.length
;
386 gss_release_buffer(&min
, &out_buf
);
388 if (ret
!= GSS_S_CONTINUE_NEEDED
) {
389 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
396 /******************************************************************************
397 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
398 Until success we do everything on the partial enc ctx.
399 ******************************************************************************/
401 static NTSTATUS
srv_enc_ntlm_negotiate(unsigned char **ppdata
, size_t *p_data_size
, DATA_BLOB secblob
, BOOL spnego_wrap
)
404 DATA_BLOB chal
= data_blob(NULL
, 0);
405 DATA_BLOB response
= data_blob(NULL
, 0);
407 status
= make_srv_encryption_context(SMB_TRANS_ENC_NTLM
, &partial_srv_trans_enc_ctx
);
408 if (!NT_STATUS_IS_OK(status
)) {
412 status
= auth_ntlmssp_update(partial_srv_trans_enc_ctx
->auth_ntlmssp_state
, secblob
, &chal
);
414 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
418 response
= spnego_gen_auth_response(&chal
, status
, OID_NTLMSSP
);
419 data_blob_free(&chal
);
421 /* Return the raw blob. */
426 *ppdata
= response
.data
;
427 *p_data_size
= response
.length
;
431 /******************************************************************************
432 Do the SPNEGO encryption negotiation. Parameters are in/out.
433 Based off code in smbd/sesssionsetup.c
434 Until success we do everything on the partial enc ctx.
435 ******************************************************************************/
437 static NTSTATUS
srv_enc_spnego_negotiate(connection_struct
*conn
,
438 unsigned char **ppdata
,
440 unsigned char **pparam
,
441 size_t *p_param_size
)
444 DATA_BLOB blob
= data_blob(NULL
,0);
445 DATA_BLOB secblob
= data_blob(NULL
, 0);
446 BOOL got_kerberos_mechanism
= False
;
448 blob
= data_blob_const(*ppdata
, *p_data_size
);
450 status
= parse_spnego_mechanisms(blob
, &secblob
, &got_kerberos_mechanism
);
451 if (!NT_STATUS_IS_OK(status
)) {
452 return nt_status_squash(status
);
455 /* We should have no partial context at this point. */
457 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
459 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
460 if (got_kerberos_mechanism
&& lp_use_kerberos_keytab() ) {
461 status
= srv_enc_spnego_gss_negotiate(ppdata
, p_data_size
, secblob
);
465 status
= srv_enc_ntlm_negotiate(ppdata
, p_data_size
, secblob
, True
);
468 data_blob_free(&secblob
);
470 if (!NT_STATUS_EQUAL(status
,NT_STATUS_MORE_PROCESSING_REQUIRED
) && !NT_STATUS_IS_OK(status
)) {
471 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
472 return nt_status_squash(status
);
475 if (NT_STATUS_IS_OK(status
)) {
476 /* Return the context we're using for this encryption state. */
477 *pparam
= SMB_MALLOC(2);
479 return NT_STATUS_NO_MEMORY
;
481 SSVAL(*pparam
,0,partial_srv_trans_enc_ctx
->es
->enc_ctx_num
);
488 /******************************************************************************
489 Complete a SPNEGO encryption negotiation. Parameters are in/out.
490 We only get this for a NTLM auth second stage.
491 ******************************************************************************/
493 static NTSTATUS
srv_enc_spnego_ntlm_auth(connection_struct
*conn
,
494 unsigned char **ppdata
,
496 unsigned char **pparam
,
497 size_t *p_param_size
)
500 DATA_BLOB blob
= data_blob(NULL
,0);
501 DATA_BLOB auth
= data_blob(NULL
,0);
502 DATA_BLOB auth_reply
= data_blob(NULL
,0);
503 DATA_BLOB response
= data_blob(NULL
,0);
504 struct smb_srv_trans_enc_ctx
*ec
= partial_srv_trans_enc_ctx
;
506 /* We must have a partial context here. */
508 if (!ec
|| !ec
->es
|| ec
->auth_ntlmssp_state
== NULL
|| ec
->es
->smb_enc_type
!= SMB_TRANS_ENC_NTLM
) {
509 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
510 return NT_STATUS_INVALID_PARAMETER
;
513 blob
= data_blob_const(*ppdata
, *p_data_size
);
514 if (!spnego_parse_auth(blob
, &auth
)) {
515 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
516 return NT_STATUS_INVALID_PARAMETER
;
519 status
= auth_ntlmssp_update(ec
->auth_ntlmssp_state
, auth
, &auth_reply
);
520 data_blob_free(&auth
);
522 response
= spnego_gen_auth_response(&auth_reply
, status
, OID_NTLMSSP
);
523 data_blob_free(&auth_reply
);
525 if (NT_STATUS_IS_OK(status
)) {
526 /* Return the context we're using for this encryption state. */
527 *pparam
= SMB_MALLOC(2);
529 return NT_STATUS_NO_MEMORY
;
531 SSVAL(*pparam
,0,ec
->es
->enc_ctx_num
);
536 *ppdata
= response
.data
;
537 *p_data_size
= response
.length
;
541 /******************************************************************************
542 Raw NTLM encryption negotiation. Parameters are in/out.
543 This function does both steps.
544 ******************************************************************************/
546 static NTSTATUS
srv_enc_raw_ntlm_auth(connection_struct
*conn
,
547 unsigned char **ppdata
,
549 unsigned char **pparam
,
550 size_t *p_param_size
)
553 DATA_BLOB blob
= data_blob_const(*ppdata
, *p_data_size
);
554 DATA_BLOB response
= data_blob(NULL
,0);
555 struct smb_srv_trans_enc_ctx
*ec
;
557 if (!partial_srv_trans_enc_ctx
) {
558 /* This is the initial step. */
559 status
= srv_enc_ntlm_negotiate(ppdata
, p_data_size
, blob
, False
);
560 if (!NT_STATUS_EQUAL(status
,NT_STATUS_MORE_PROCESSING_REQUIRED
) && !NT_STATUS_IS_OK(status
)) {
561 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
562 return nt_status_squash(status
);
567 ec
= partial_srv_trans_enc_ctx
;
568 if (!ec
|| !ec
->es
|| ec
->auth_ntlmssp_state
== NULL
|| ec
->es
->smb_enc_type
!= SMB_TRANS_ENC_NTLM
) {
569 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
570 return NT_STATUS_INVALID_PARAMETER
;
574 status
= auth_ntlmssp_update(partial_srv_trans_enc_ctx
->auth_ntlmssp_state
, blob
, &response
);
576 if (NT_STATUS_IS_OK(status
)) {
577 /* Return the context we're using for this encryption state. */
578 *pparam
= SMB_MALLOC(2);
580 return NT_STATUS_NO_MEMORY
;
582 SSVAL(*pparam
,0,ec
->es
->enc_ctx_num
);
586 /* Return the raw blob. */
588 *ppdata
= response
.data
;
589 *p_data_size
= response
.length
;
593 /******************************************************************************
594 Do the SPNEGO encryption negotiation. Parameters are in/out.
595 ******************************************************************************/
597 NTSTATUS
srv_request_encryption_setup(connection_struct
*conn
,
598 unsigned char **ppdata
,
600 unsigned char **pparam
,
601 size_t *p_param_size
)
603 unsigned char *pdata
= *ppdata
;
608 if (*p_data_size
< 1) {
609 return NT_STATUS_INVALID_PARAMETER
;
612 if (pdata
[0] == ASN1_APPLICATION(0)) {
613 /* its a negTokenTarg packet */
614 return srv_enc_spnego_negotiate(conn
, ppdata
, p_data_size
, pparam
, p_param_size
);
617 if (pdata
[0] == ASN1_CONTEXT(1)) {
618 /* It's an auth packet */
619 return srv_enc_spnego_ntlm_auth(conn
, ppdata
, p_data_size
, pparam
, p_param_size
);
622 /* Maybe it's a raw unwrapped auth ? */
623 if (*p_data_size
< 7) {
624 return NT_STATUS_INVALID_PARAMETER
;
627 if (strncmp((char *)pdata
, "NTLMSSP", 7) == 0) {
628 return srv_enc_raw_ntlm_auth(conn
, ppdata
, p_data_size
, pparam
, p_param_size
);
631 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
633 return NT_STATUS_LOGON_FAILURE
;
636 /******************************************************************************
637 Negotiation was successful - turn on server-side encryption.
638 ******************************************************************************/
640 static NTSTATUS
check_enc_good(struct smb_srv_trans_enc_ctx
*ec
)
642 if (!ec
|| !ec
->es
) {
643 return NT_STATUS_LOGON_FAILURE
;
646 if (ec
->es
->smb_enc_type
== SMB_TRANS_ENC_NTLM
) {
647 if ((ec
->es
->s
.ntlmssp_state
->neg_flags
& (NTLMSSP_NEGOTIATE_SIGN
|NTLMSSP_NEGOTIATE_SEAL
)) !=
648 (NTLMSSP_NEGOTIATE_SIGN
|NTLMSSP_NEGOTIATE_SEAL
)) {
649 return NT_STATUS_INVALID_PARAMETER
;
652 /* Todo - check gssapi case. */
657 /******************************************************************************
658 Negotiation was successful - turn on server-side encryption.
659 ******************************************************************************/
661 NTSTATUS
srv_encryption_start(connection_struct
*conn
)
665 /* Check that we are really doing sign+seal. */
666 status
= check_enc_good(partial_srv_trans_enc_ctx
);
667 if (!NT_STATUS_IS_OK(status
)) {
670 /* Throw away the context we're using currently (if any). */
671 srv_free_encryption_context(&srv_trans_enc_ctx
);
673 /* Steal the partial pointer. Deliberate shallow copy. */
674 srv_trans_enc_ctx
= partial_srv_trans_enc_ctx
;
675 srv_trans_enc_ctx
->es
->enc_on
= True
;
677 partial_srv_trans_enc_ctx
= NULL
;
681 /******************************************************************************
682 Shutdown all server contexts.
683 ******************************************************************************/
685 void server_encryption_shutdown(void)
687 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
688 srv_free_encryption_context(&srv_trans_enc_ctx
);