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. */
61 status = get_enc_ctx_num(inbuf, &enc_num);
62 if (!NT_STATUS_IS_OK(status)) {
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 asprintf(&host_princ_s, "%s@%s", service, name);
131 if (host_princ_s == NULL) {
132 return NT_STATUS_NO_MEMORY;
135 input_name.value = host_princ_s;
136 input_name.length = strlen(host_princ_s) + 1;
138 ret = gss_import_name(&min,
140 &nt_hostbased_service,
143 DEBUG(10,("get_srv_gss_creds: imported name %s\n",
146 if (ret != GSS_S_COMPLETE) {
147 SAFE_FREE(host_princ_s);
148 return map_nt_error_from_gss(ret, min);
152 * We're accessing the krb5.keytab file here.
153 * ensure we have permissions to do so.
157 ret = gss_acquire_cred(&min,
167 if (ret != GSS_S_COMPLETE) {
168 ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
169 DEBUG(10,("get_srv_gss_creds: gss_acquire_cred failed with %s\n",
171 status = map_nt_error_from_gss(ret, min);
174 SAFE_FREE(host_princ_s);
175 gss_release_name(&min, &srv_name);
179 /******************************************************************************
181 Try and get the cifs/server@realm principal first, then fall back to
183 ******************************************************************************/
185 static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
188 gss_cred_id_t srv_cred;
191 name_to_fqdn(fqdn, global_myname());
194 status = get_srv_gss_creds("cifs", fqdn, GSS_C_ACCEPT, &srv_cred);
195 if (!NT_STATUS_IS_OK(status)) {
196 status = get_srv_gss_creds("host", fqdn, GSS_C_ACCEPT, &srv_cred);
197 if (!NT_STATUS_IS_OK(status)) {
198 return nt_status_squash(status);
202 ec->es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
203 if (!ec->es->s.gss_state) {
205 gss_release_cred(&min, &srv_cred);
206 return NT_STATUS_NO_MEMORY;
208 ZERO_STRUCTP(ec->es->s.gss_state);
209 ec->es->s.gss_state->creds = srv_cred;
211 /* No context yet. */
212 ec->es->s.gss_state->gss_ctx = GSS_C_NO_CONTEXT;
218 /******************************************************************************
219 Shutdown a server encryption context.
220 ******************************************************************************/
222 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
224 struct smb_srv_trans_enc_ctx *ec = *pp_ec;
231 switch (ec->es->smb_enc_type) {
232 case SMB_TRANS_ENC_NTLM:
233 destroy_auth_ntlmssp(ec);
235 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
236 case SMB_TRANS_ENC_GSS:
240 common_free_encryption_state(&ec->es);
247 /******************************************************************************
248 Create a server encryption context.
249 ******************************************************************************/
251 static NTSTATUS make_srv_encryption_context(enum smb_trans_enc_type smb_enc_type, struct smb_srv_trans_enc_ctx **pp_ec)
253 struct smb_srv_trans_enc_ctx *ec;
257 ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
259 return NT_STATUS_NO_MEMORY;
261 ZERO_STRUCTP(partial_srv_trans_enc_ctx);
262 ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
265 return NT_STATUS_NO_MEMORY;
267 ZERO_STRUCTP(ec->es);
268 ec->es->smb_enc_type = smb_enc_type;
269 switch (smb_enc_type) {
270 case SMB_TRANS_ENC_NTLM:
272 NTSTATUS status = make_auth_ntlmssp(ec);
273 if (!NT_STATUS_IS_OK(status)) {
274 srv_free_encryption_context(&ec);
280 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
281 case SMB_TRANS_ENC_GSS:
282 /* Acquire our credentials by calling gss_acquire_cred here. */
284 NTSTATUS status = make_auth_gss(ec);
285 if (!NT_STATUS_IS_OK(status)) {
286 srv_free_encryption_context(&ec);
293 srv_free_encryption_context(&ec);
294 return NT_STATUS_INVALID_PARAMETER;
300 /******************************************************************************
301 Free an encryption-allocated buffer.
302 ******************************************************************************/
304 void srv_free_enc_buffer(char *buf)
306 /* We know this is an smb buffer, and we
307 * didn't malloc, only copy, for a keepalive,
308 * so ignore non-session messages. */
314 if (srv_trans_enc_ctx) {
315 common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
319 /******************************************************************************
320 Decrypt an incoming buffer.
321 ******************************************************************************/
323 NTSTATUS srv_decrypt_buffer(char *buf)
325 /* Ignore non-session messages. */
330 if (srv_trans_enc_ctx) {
331 return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
337 /******************************************************************************
338 Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
339 ******************************************************************************/
341 NTSTATUS srv_encrypt_buffer(char *buf, char **buf_out)
345 /* Ignore non-session messages. */
350 if (srv_trans_enc_ctx) {
351 return common_encrypt_buffer(srv_trans_enc_ctx->es, buf, buf_out);
353 /* Not encrypting. */
357 /******************************************************************************
358 Do the gss encryption negotiation. Parameters are in/out.
359 Until success we do everything on the partial enc ctx.
360 ******************************************************************************/
362 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
363 static NTSTATUS srv_enc_spnego_gss_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob)
368 gss_buffer_desc in_buf, out_buf;
369 struct smb_tran_enc_state_gss *gss_state;
370 DATA_BLOB auth_reply = data_blob_null;
371 DATA_BLOB response = data_blob_null;
374 if (!partial_srv_trans_enc_ctx) {
375 status = make_srv_encryption_context(SMB_TRANS_ENC_GSS, &partial_srv_trans_enc_ctx);
376 if (!NT_STATUS_IS_OK(status)) {
381 gss_state = partial_srv_trans_enc_ctx->es->s.gss_state;
383 in_buf.value = secblob.data;
384 in_buf.length = secblob.length;
386 out_buf.value = NULL;
391 ret = gss_accept_sec_context(&min,
395 GSS_C_NO_CHANNEL_BINDINGS,
397 NULL, /* Ignore oids. */
398 &out_buf, /* To return. */
400 NULL, /* Ingore time. */
401 NULL); /* Ignore delegated creds. */
404 status = gss_err_to_ntstatus(ret, min);
405 if (ret != GSS_S_COMPLETE && ret != GSS_S_CONTINUE_NEEDED) {
409 /* Ensure we've got sign+seal available. */
410 if (ret == GSS_S_COMPLETE) {
411 if ((flags & (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) !=
412 (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) {
413 DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
414 "for SMB sealing.\n"));
415 gss_release_buffer(&min, &out_buf);
416 return NT_STATUS_ACCESS_DENIED;
420 auth_reply = data_blob(out_buf.value, out_buf.length);
421 gss_release_buffer(&min, &out_buf);
423 /* Wrap in SPNEGO. */
424 response = spnego_gen_auth_response(&auth_reply, status, OID_KERBEROS5);
425 data_blob_free(&auth_reply);
428 *ppdata = response.data;
429 *p_data_size = response.length;
435 /******************************************************************************
436 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
437 Until success we do everything on the partial enc ctx.
438 ******************************************************************************/
440 static NTSTATUS srv_enc_ntlm_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob, bool spnego_wrap)
443 DATA_BLOB chal = data_blob_null;
444 DATA_BLOB response = data_blob_null;
446 status = make_srv_encryption_context(SMB_TRANS_ENC_NTLM, &partial_srv_trans_enc_ctx);
447 if (!NT_STATUS_IS_OK(status)) {
451 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, secblob, &chal);
453 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
457 response = spnego_gen_auth_response(&chal, status, OID_NTLMSSP);
458 data_blob_free(&chal);
460 /* Return the raw blob. */
465 *ppdata = response.data;
466 *p_data_size = response.length;
470 /******************************************************************************
471 Do the SPNEGO encryption negotiation. Parameters are in/out.
472 Based off code in smbd/sesssionsetup.c
473 Until success we do everything on the partial enc ctx.
474 ******************************************************************************/
476 static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
477 unsigned char **ppdata,
479 unsigned char **pparam,
480 size_t *p_param_size)
483 DATA_BLOB blob = data_blob_null;
484 DATA_BLOB secblob = data_blob_null;
485 bool got_kerberos_mechanism = false;
487 blob = data_blob_const(*ppdata, *p_data_size);
489 status = parse_spnego_mechanisms(blob, &secblob, &got_kerberos_mechanism);
490 if (!NT_STATUS_IS_OK(status)) {
491 return nt_status_squash(status);
494 /* We should have no partial context at this point. */
496 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
498 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
499 if (got_kerberos_mechanism && lp_use_kerberos_keytab() ) {
500 status = srv_enc_spnego_gss_negotiate(ppdata, p_data_size, secblob);
504 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, secblob, true);
507 data_blob_free(&secblob);
509 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
510 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
511 return nt_status_squash(status);
514 if (NT_STATUS_IS_OK(status)) {
515 /* Return the context we're using for this encryption state. */
516 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
517 return NT_STATUS_NO_MEMORY;
519 SSVAL(*pparam,0,partial_srv_trans_enc_ctx->es->enc_ctx_num);
526 /******************************************************************************
527 Complete a SPNEGO encryption negotiation. Parameters are in/out.
528 We only get this for a NTLM auth second stage.
529 ******************************************************************************/
531 static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
532 unsigned char **ppdata,
534 unsigned char **pparam,
535 size_t *p_param_size)
538 DATA_BLOB blob = data_blob_null;
539 DATA_BLOB auth = data_blob_null;
540 DATA_BLOB auth_reply = data_blob_null;
541 DATA_BLOB response = data_blob_null;
542 struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
544 /* We must have a partial context here. */
546 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
547 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
548 return NT_STATUS_INVALID_PARAMETER;
551 blob = data_blob_const(*ppdata, *p_data_size);
552 if (!spnego_parse_auth(blob, &auth)) {
553 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
554 return NT_STATUS_INVALID_PARAMETER;
557 status = auth_ntlmssp_update(ec->auth_ntlmssp_state, auth, &auth_reply);
558 data_blob_free(&auth);
560 response = spnego_gen_auth_response(&auth_reply, status, OID_NTLMSSP);
561 data_blob_free(&auth_reply);
563 if (NT_STATUS_IS_OK(status)) {
564 /* Return the context we're using for this encryption state. */
565 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
566 return NT_STATUS_NO_MEMORY;
568 SSVAL(*pparam,0,ec->es->enc_ctx_num);
573 *ppdata = response.data;
574 *p_data_size = response.length;
578 /******************************************************************************
579 Raw NTLM encryption negotiation. Parameters are in/out.
580 This function does both steps.
581 ******************************************************************************/
583 static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
584 unsigned char **ppdata,
586 unsigned char **pparam,
587 size_t *p_param_size)
590 DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
591 DATA_BLOB response = data_blob_null;
592 struct smb_srv_trans_enc_ctx *ec;
594 if (!partial_srv_trans_enc_ctx) {
595 /* This is the initial step. */
596 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, blob, false);
597 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
598 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
599 return nt_status_squash(status);
604 ec = partial_srv_trans_enc_ctx;
605 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
606 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
607 return NT_STATUS_INVALID_PARAMETER;
611 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, blob, &response);
613 if (NT_STATUS_IS_OK(status)) {
614 /* Return the context we're using for this encryption state. */
615 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
616 return NT_STATUS_NO_MEMORY;
618 SSVAL(*pparam,0,ec->es->enc_ctx_num);
622 /* Return the raw blob. */
624 *ppdata = response.data;
625 *p_data_size = response.length;
629 /******************************************************************************
630 Do the SPNEGO encryption negotiation. Parameters are in/out.
631 ******************************************************************************/
633 NTSTATUS srv_request_encryption_setup(connection_struct *conn,
634 unsigned char **ppdata,
636 unsigned char **pparam,
637 size_t *p_param_size)
639 unsigned char *pdata = *ppdata;
644 if (*p_data_size < 1) {
645 return NT_STATUS_INVALID_PARAMETER;
648 if (pdata[0] == ASN1_APPLICATION(0)) {
649 /* its a negTokenTarg packet */
650 return srv_enc_spnego_negotiate(conn, ppdata, p_data_size, pparam, p_param_size);
653 if (pdata[0] == ASN1_CONTEXT(1)) {
654 /* It's an auth packet */
655 return srv_enc_spnego_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
658 /* Maybe it's a raw unwrapped auth ? */
659 if (*p_data_size < 7) {
660 return NT_STATUS_INVALID_PARAMETER;
663 if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
664 return srv_enc_raw_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
667 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
669 return NT_STATUS_LOGON_FAILURE;
672 /******************************************************************************
673 Negotiation was successful - turn on server-side encryption.
674 ******************************************************************************/
676 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
678 if (!ec || !ec->es) {
679 return NT_STATUS_LOGON_FAILURE;
682 if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
683 if ((ec->es->s.ntlmssp_state->neg_flags & (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) !=
684 (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) {
685 return NT_STATUS_INVALID_PARAMETER;
688 /* Todo - check gssapi case. */
693 /******************************************************************************
694 Negotiation was successful - turn on server-side encryption.
695 ******************************************************************************/
697 NTSTATUS srv_encryption_start(connection_struct *conn)
701 /* Check that we are really doing sign+seal. */
702 status = check_enc_good(partial_srv_trans_enc_ctx);
703 if (!NT_STATUS_IS_OK(status)) {
706 /* Throw away the context we're using currently (if any). */
707 srv_free_encryption_context(&srv_trans_enc_ctx);
709 /* Steal the partial pointer. Deliberate shallow copy. */
710 srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
711 srv_trans_enc_ctx->es->enc_on = true;
713 partial_srv_trans_enc_ctx = NULL;
718 /******************************************************************************
719 Shutdown all server contexts.
720 ******************************************************************************/
722 void server_encryption_shutdown(void)
724 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
725 srv_free_encryption_context(&srv_trans_enc_ctx);