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/smbd.h"
22 #include "smbd/globals.h"
23 #include "../libcli/auth/spnego.h"
24 #include "../libcli/auth/ntlmssp.h"
25 #include "ntlmssp_wrap.h"
26 #include "smb_crypt.h"
27 #include "../lib/util/asn1.h"
29 #include "libsmb/libsmb.h"
30 #include "../lib/tsocket/tsocket.h"
32 /******************************************************************************
33 Server side encryption.
34 ******************************************************************************/
36 /******************************************************************************
38 ******************************************************************************/
40 struct smb_srv_trans_enc_ctx {
41 struct smb_trans_enc_state *es;
42 struct auth_ntlmssp_state *auth_ntlmssp_state; /* Must be kept in sync with pointer in ec->ntlmssp_state. */
45 /******************************************************************************
46 Return global enc context - this must change if we ever do multiple contexts.
47 ******************************************************************************/
49 uint16_t srv_enc_ctx(void)
51 return srv_trans_enc_ctx->es->enc_ctx_num;
54 /******************************************************************************
55 Is this an incoming encrypted packet ?
56 ******************************************************************************/
58 bool is_encrypted_packet(const uint8_t *inbuf)
63 /* Ignore non-session messages or non 0xFF'E' messages. */
65 || (smb_len(inbuf) < 8)
66 || !(inbuf[4] == 0xFF && inbuf[5] == 'E')) {
70 status = get_enc_ctx_num(inbuf, &enc_num);
71 if (!NT_STATUS_IS_OK(status)) {
75 /* Encrypted messages are 0xFF'E'<ctx> */
76 if (srv_trans_enc_ctx && enc_num == srv_enc_ctx()) {
82 /******************************************************************************
83 Create an auth_ntlmssp_state and ensure pointer copy is correct.
84 ******************************************************************************/
86 static NTSTATUS make_auth_ntlmssp(const struct tsocket_address *remote_address,
87 struct smb_srv_trans_enc_ctx *ec)
89 NTSTATUS status = auth_ntlmssp_start(remote_address,
90 &ec->auth_ntlmssp_state);
91 if (!NT_STATUS_IS_OK(status)) {
92 return nt_status_squash(status);
96 * We must remember to update the pointer copy for the common
97 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
99 ec->es->s.auth_ntlmssp_state = ec->auth_ntlmssp_state;
103 /******************************************************************************
104 Destroy an auth_ntlmssp_state and ensure pointer copy is correct.
105 ******************************************************************************/
107 static void destroy_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
110 * We must remember to update the pointer copy for the common
111 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
114 if (ec->auth_ntlmssp_state) {
115 TALLOC_FREE(ec->auth_ntlmssp_state);
116 /* The auth_ntlmssp_end killed this already. */
117 ec->es->s.auth_ntlmssp_state = NULL;
121 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
123 /******************************************************************************
125 ******************************************************************************/
127 static NTSTATUS get_srv_gss_creds(const char *service,
129 gss_cred_usage_t cred_type,
130 gss_cred_id_t *p_srv_cred)
135 gss_buffer_desc input_name;
136 char *host_princ_s = NULL;
137 NTSTATUS status = NT_STATUS_OK;
139 gss_OID_desc nt_hostbased_service =
140 {10, discard_const_p(char, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
142 if (asprintf(&host_princ_s, "%s@%s", service, name) == -1) {
143 return NT_STATUS_NO_MEMORY;
146 input_name.value = host_princ_s;
147 input_name.length = strlen(host_princ_s) + 1;
149 ret = gss_import_name(&min,
151 &nt_hostbased_service,
154 DEBUG(10,("get_srv_gss_creds: imported name %s\n",
157 if (ret != GSS_S_COMPLETE) {
158 SAFE_FREE(host_princ_s);
159 return map_nt_error_from_gss(ret, min);
163 * We're accessing the krb5.keytab file here.
164 * ensure we have permissions to do so.
168 ret = gss_acquire_cred(&min,
178 if (ret != GSS_S_COMPLETE) {
179 ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
180 DEBUG(10,("get_srv_gss_creds: gss_acquire_cred failed with %s\n",
182 status = map_nt_error_from_gss(ret, min);
185 SAFE_FREE(host_princ_s);
186 gss_release_name(&min, &srv_name);
190 /******************************************************************************
192 Try and get the cifs/server@realm principal first, then fall back to
194 ******************************************************************************/
196 static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
199 gss_cred_id_t srv_cred;
202 name_to_fqdn(fqdn, lp_netbios_name());
205 status = get_srv_gss_creds("cifs", fqdn, GSS_C_ACCEPT, &srv_cred);
206 if (!NT_STATUS_IS_OK(status)) {
207 status = get_srv_gss_creds("host", fqdn, GSS_C_ACCEPT, &srv_cred);
208 if (!NT_STATUS_IS_OK(status)) {
209 return nt_status_squash(status);
213 ec->es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
214 if (!ec->es->s.gss_state) {
216 gss_release_cred(&min, &srv_cred);
217 return NT_STATUS_NO_MEMORY;
219 ZERO_STRUCTP(ec->es->s.gss_state);
220 ec->es->s.gss_state->creds = srv_cred;
222 /* No context yet. */
223 ec->es->s.gss_state->gss_ctx = GSS_C_NO_CONTEXT;
229 /******************************************************************************
230 Shutdown a server encryption context.
231 ******************************************************************************/
233 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
235 struct smb_srv_trans_enc_ctx *ec = *pp_ec;
242 switch (ec->es->smb_enc_type) {
243 case SMB_TRANS_ENC_NTLM:
244 destroy_auth_ntlmssp(ec);
246 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
247 case SMB_TRANS_ENC_GSS:
251 common_free_encryption_state(&ec->es);
258 /******************************************************************************
259 Create a server encryption context.
260 ******************************************************************************/
262 static NTSTATUS make_srv_encryption_context(const struct tsocket_address *remote_address,
263 enum smb_trans_enc_type smb_enc_type,
264 struct smb_srv_trans_enc_ctx **pp_ec)
266 struct smb_srv_trans_enc_ctx *ec;
270 ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
272 return NT_STATUS_NO_MEMORY;
274 ZERO_STRUCTP(partial_srv_trans_enc_ctx);
275 ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
278 return NT_STATUS_NO_MEMORY;
280 ZERO_STRUCTP(ec->es);
281 ec->es->smb_enc_type = smb_enc_type;
282 switch (smb_enc_type) {
283 case SMB_TRANS_ENC_NTLM:
285 NTSTATUS status = make_auth_ntlmssp(remote_address,
287 if (!NT_STATUS_IS_OK(status)) {
288 srv_free_encryption_context(&ec);
294 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
295 case SMB_TRANS_ENC_GSS:
296 /* Acquire our credentials by calling gss_acquire_cred here. */
298 NTSTATUS status = make_auth_gss(ec);
299 if (!NT_STATUS_IS_OK(status)) {
300 srv_free_encryption_context(&ec);
307 srv_free_encryption_context(&ec);
308 return NT_STATUS_INVALID_PARAMETER;
314 /******************************************************************************
315 Free an encryption-allocated buffer.
316 ******************************************************************************/
318 void srv_free_enc_buffer(char *buf)
320 /* We know this is an smb buffer, and we
321 * didn't malloc, only copy, for a keepalive,
322 * so ignore non-session messages. */
328 if (srv_trans_enc_ctx) {
329 common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
333 /******************************************************************************
334 Decrypt an incoming buffer.
335 ******************************************************************************/
337 NTSTATUS srv_decrypt_buffer(char *buf)
339 /* Ignore non-session messages. */
344 if (srv_trans_enc_ctx) {
345 return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
351 /******************************************************************************
352 Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
353 ******************************************************************************/
355 NTSTATUS srv_encrypt_buffer(char *buf, char **buf_out)
359 /* Ignore non-session messages. */
364 if (srv_trans_enc_ctx) {
365 return common_encrypt_buffer(srv_trans_enc_ctx->es, buf, buf_out);
367 /* Not encrypting. */
371 /******************************************************************************
372 Do the gss encryption negotiation. Parameters are in/out.
373 Until success we do everything on the partial enc ctx.
374 ******************************************************************************/
376 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
377 static NTSTATUS srv_enc_spnego_gss_negotiate(const struct tsocket_address *remote_address,
378 unsigned char **ppdata,
385 gss_buffer_desc in_buf, out_buf;
386 struct smb_tran_enc_state_gss *gss_state;
387 DATA_BLOB auth_reply = data_blob_null;
388 DATA_BLOB response = data_blob_null;
391 if (!partial_srv_trans_enc_ctx) {
392 status = make_srv_encryption_context(remote_address,
394 &partial_srv_trans_enc_ctx);
395 if (!NT_STATUS_IS_OK(status)) {
400 gss_state = partial_srv_trans_enc_ctx->es->s.gss_state;
402 in_buf.value = secblob.data;
403 in_buf.length = secblob.length;
405 out_buf.value = NULL;
410 ret = gss_accept_sec_context(&min,
414 GSS_C_NO_CHANNEL_BINDINGS,
416 NULL, /* Ignore oids. */
417 &out_buf, /* To return. */
419 NULL, /* Ingore time. */
420 NULL); /* Ignore delegated creds. */
423 status = gss_err_to_ntstatus(ret, min);
424 if (ret != GSS_S_COMPLETE && ret != GSS_S_CONTINUE_NEEDED) {
428 /* Ensure we've got sign+seal available. */
429 if (ret == GSS_S_COMPLETE) {
430 if ((flags & (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) !=
431 (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) {
432 DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
433 "for SMB sealing.\n"));
434 gss_release_buffer(&min, &out_buf);
435 return NT_STATUS_ACCESS_DENIED;
439 auth_reply = data_blob(out_buf.value, out_buf.length);
440 gss_release_buffer(&min, &out_buf);
442 /* Wrap in SPNEGO. */
443 response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, OID_KERBEROS5);
444 data_blob_free(&auth_reply);
447 *ppdata = (unsigned char *)memdup(response.data, response.length);
448 if ((*ppdata) == NULL && response.length > 0) {
449 status = NT_STATUS_NO_MEMORY;
451 *p_data_size = response.length;
453 data_blob_free(&response);
459 /******************************************************************************
460 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
461 Until success we do everything on the partial enc ctx.
462 ******************************************************************************/
464 static NTSTATUS srv_enc_ntlm_negotiate(const struct tsocket_address *remote_address,
465 unsigned char **ppdata,
471 DATA_BLOB chal = data_blob_null;
472 DATA_BLOB response = data_blob_null;
474 status = make_srv_encryption_context(remote_address,
476 &partial_srv_trans_enc_ctx);
477 if (!NT_STATUS_IS_OK(status)) {
481 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, secblob, &chal);
483 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
487 response = spnego_gen_auth_response(talloc_tos(), &chal, status, OID_NTLMSSP);
488 data_blob_free(&chal);
490 /* Return the raw blob. */
495 *ppdata = (unsigned char *)memdup(response.data, response.length);
496 if ((*ppdata) == NULL && response.length > 0) {
497 status = NT_STATUS_NO_MEMORY;
499 *p_data_size = response.length;
500 data_blob_free(&response);
505 /******************************************************************************
506 Do the SPNEGO encryption negotiation. Parameters are in/out.
507 Based off code in smbd/sesssionsetup.c
508 Until success we do everything on the partial enc ctx.
509 ******************************************************************************/
511 static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
512 unsigned char **ppdata,
514 unsigned char **pparam,
515 size_t *p_param_size)
518 DATA_BLOB blob = data_blob_null;
519 DATA_BLOB secblob = data_blob_null;
520 char *kerb_mech = NULL;
522 blob = data_blob_const(*ppdata, *p_data_size);
524 status = parse_spnego_mechanisms(talloc_tos(), blob, &secblob, &kerb_mech);
525 if (!NT_STATUS_IS_OK(status)) {
526 return nt_status_squash(status);
529 /* We should have no partial context at this point. */
531 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
534 TALLOC_FREE(kerb_mech);
536 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
537 status = srv_enc_spnego_gss_negotiate(conn->sconn->remote_address,
542 /* Currently we don't SPNEGO negotiate
543 * back to NTLMSSP as we do in sessionsetupX. We should... */
544 return NT_STATUS_LOGON_FAILURE;
547 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
554 data_blob_free(&secblob);
556 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
557 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
558 return nt_status_squash(status);
561 if (NT_STATUS_IS_OK(status)) {
562 /* Return the context we're using for this encryption state. */
563 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
564 return NT_STATUS_NO_MEMORY;
566 SSVAL(*pparam,0,partial_srv_trans_enc_ctx->es->enc_ctx_num);
573 /******************************************************************************
574 Complete a SPNEGO encryption negotiation. Parameters are in/out.
575 We only get this for a NTLM auth second stage.
576 ******************************************************************************/
578 static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
579 unsigned char **ppdata,
581 unsigned char **pparam,
582 size_t *p_param_size)
585 DATA_BLOB blob = data_blob_null;
586 DATA_BLOB auth = data_blob_null;
587 DATA_BLOB auth_reply = data_blob_null;
588 DATA_BLOB response = data_blob_null;
589 struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
591 /* We must have a partial context here. */
593 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
594 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
595 return NT_STATUS_INVALID_PARAMETER;
598 blob = data_blob_const(*ppdata, *p_data_size);
599 if (!spnego_parse_auth(talloc_tos(), blob, &auth)) {
600 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
601 return NT_STATUS_INVALID_PARAMETER;
604 status = auth_ntlmssp_update(ec->auth_ntlmssp_state, auth, &auth_reply);
605 data_blob_free(&auth);
611 * This field SHALL only be present in the first reply from the
613 * So set mechOID to NULL here.
616 response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, NULL);
617 data_blob_free(&auth_reply);
619 if (NT_STATUS_IS_OK(status)) {
620 /* Return the context we're using for this encryption state. */
621 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
622 return NT_STATUS_NO_MEMORY;
624 SSVAL(*pparam,0,ec->es->enc_ctx_num);
629 *ppdata = (unsigned char *)memdup(response.data, response.length);
630 if ((*ppdata) == NULL && response.length > 0)
631 return NT_STATUS_NO_MEMORY;
632 *p_data_size = response.length;
633 data_blob_free(&response);
637 /******************************************************************************
638 Raw NTLM encryption negotiation. Parameters are in/out.
639 This function does both steps.
640 ******************************************************************************/
642 static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
643 unsigned char **ppdata,
645 unsigned char **pparam,
646 size_t *p_param_size)
649 DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
650 DATA_BLOB response = data_blob_null;
651 struct smb_srv_trans_enc_ctx *ec;
653 if (!partial_srv_trans_enc_ctx) {
654 /* This is the initial step. */
655 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
660 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
661 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
662 return nt_status_squash(status);
667 ec = partial_srv_trans_enc_ctx;
668 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
669 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
670 return NT_STATUS_INVALID_PARAMETER;
674 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, blob, &response);
676 if (NT_STATUS_IS_OK(status)) {
677 /* Return the context we're using for this encryption state. */
678 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
679 return NT_STATUS_NO_MEMORY;
681 SSVAL(*pparam,0,ec->es->enc_ctx_num);
685 /* Return the raw blob. */
687 *ppdata = (unsigned char *)memdup(response.data, response.length);
688 if ((*ppdata) == NULL && response.length > 0)
689 return NT_STATUS_NO_MEMORY;
690 *p_data_size = response.length;
691 data_blob_free(&response);
695 /******************************************************************************
696 Do the SPNEGO encryption negotiation. Parameters are in/out.
697 ******************************************************************************/
699 NTSTATUS srv_request_encryption_setup(connection_struct *conn,
700 unsigned char **ppdata,
702 unsigned char **pparam,
703 size_t *p_param_size)
705 unsigned char *pdata = *ppdata;
710 if (*p_data_size < 1) {
711 return NT_STATUS_INVALID_PARAMETER;
714 if (pdata[0] == ASN1_APPLICATION(0)) {
715 /* its a negTokenTarg packet */
716 return srv_enc_spnego_negotiate(conn, ppdata, p_data_size, pparam, p_param_size);
719 if (pdata[0] == ASN1_CONTEXT(1)) {
720 /* It's an auth packet */
721 return srv_enc_spnego_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
724 /* Maybe it's a raw unwrapped auth ? */
725 if (*p_data_size < 7) {
726 return NT_STATUS_INVALID_PARAMETER;
729 if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
730 return srv_enc_raw_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
733 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
735 return NT_STATUS_LOGON_FAILURE;
738 /******************************************************************************
739 Negotiation was successful - turn on server-side encryption.
740 ******************************************************************************/
742 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
744 if (!ec || !ec->es) {
745 return NT_STATUS_LOGON_FAILURE;
748 if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
749 if (!auth_ntlmssp_negotiated_sign((ec->auth_ntlmssp_state))) {
750 return NT_STATUS_INVALID_PARAMETER;
753 if (!auth_ntlmssp_negotiated_seal((ec->auth_ntlmssp_state))) {
754 return NT_STATUS_INVALID_PARAMETER;
757 /* Todo - check gssapi case. */
762 /******************************************************************************
763 Negotiation was successful - turn on server-side encryption.
764 ******************************************************************************/
766 NTSTATUS srv_encryption_start(connection_struct *conn)
770 /* Check that we are really doing sign+seal. */
771 status = check_enc_good(partial_srv_trans_enc_ctx);
772 if (!NT_STATUS_IS_OK(status)) {
775 /* Throw away the context we're using currently (if any). */
776 srv_free_encryption_context(&srv_trans_enc_ctx);
778 /* Steal the partial pointer. Deliberate shallow copy. */
779 srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
780 srv_trans_enc_ctx->es->enc_on = true;
782 partial_srv_trans_enc_ctx = NULL;
784 DEBUG(1,("srv_encryption_start: context negotiated\n"));
788 /******************************************************************************
789 Shutdown all server contexts.
790 ******************************************************************************/
792 void server_encryption_shutdown(void)
794 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
795 srv_free_encryption_context(&srv_trans_enc_ctx);