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 DEBUG(10,("get_srv_gss_creds: imported name %s\n",
125 if (ret != GSS_S_COMPLETE) {
126 SAFE_FREE(host_princ_s);
127 return map_nt_error_from_gss(ret, min);
131 * We're accessing the krb5.keytab file here.
132 * ensure we have permissions to do so.
136 ret = gss_acquire_cred(&min,
146 if (ret != GSS_S_COMPLETE) {
147 ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
148 DEBUG(10,("get_srv_gss_creds: gss_acquire_cred failed with %s\n",
150 status = map_nt_error_from_gss(ret, min);
153 SAFE_FREE(host_princ_s);
154 gss_release_name(&min, &srv_name);
158 /******************************************************************************
160 Try and get the cifs/server@realm principal first, then fall back to
162 ******************************************************************************/
164 static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
167 gss_cred_id_t srv_cred;
170 name_to_fqdn(fqdn, global_myname());
173 status = get_srv_gss_creds("cifs", fqdn, GSS_C_ACCEPT, &srv_cred);
174 if (!NT_STATUS_IS_OK(status)) {
175 status = get_srv_gss_creds("host", fqdn, GSS_C_ACCEPT, &srv_cred);
176 if (!NT_STATUS_IS_OK(status)) {
177 return nt_status_squash(status);
181 ec->es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
182 if (!ec->es->s.gss_state) {
184 gss_release_cred(&min, &srv_cred);
185 return NT_STATUS_NO_MEMORY;
187 ZERO_STRUCTP(ec->es->s.gss_state);
188 ec->es->s.gss_state->creds = srv_cred;
190 /* No context yet. */
191 ec->es->s.gss_state->gss_ctx = GSS_C_NO_CONTEXT;
197 /******************************************************************************
198 Shutdown a server encryption context.
199 ******************************************************************************/
201 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
203 struct smb_srv_trans_enc_ctx *ec = *pp_ec;
210 switch (ec->es->smb_enc_type) {
211 case SMB_TRANS_ENC_NTLM:
212 destroy_auth_ntlmssp(ec);
214 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
215 case SMB_TRANS_ENC_GSS:
219 common_free_encryption_state(&ec->es);
226 /******************************************************************************
227 Create a server encryption context.
228 ******************************************************************************/
230 static NTSTATUS make_srv_encryption_context(enum smb_trans_enc_type smb_enc_type, struct smb_srv_trans_enc_ctx **pp_ec)
232 struct smb_srv_trans_enc_ctx *ec;
236 ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
238 return NT_STATUS_NO_MEMORY;
240 ZERO_STRUCTP(partial_srv_trans_enc_ctx);
241 ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
244 return NT_STATUS_NO_MEMORY;
246 ZERO_STRUCTP(ec->es);
247 ec->es->smb_enc_type = smb_enc_type;
248 switch (smb_enc_type) {
249 case SMB_TRANS_ENC_NTLM:
251 NTSTATUS status = make_auth_ntlmssp(ec);
252 if (!NT_STATUS_IS_OK(status)) {
253 srv_free_encryption_context(&ec);
259 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
260 case SMB_TRANS_ENC_GSS:
261 /* Acquire our credentials by calling gss_acquire_cred here. */
263 NTSTATUS status = make_auth_gss(ec);
264 if (!NT_STATUS_IS_OK(status)) {
265 srv_free_encryption_context(&ec);
272 srv_free_encryption_context(&ec);
273 return NT_STATUS_INVALID_PARAMETER;
279 /******************************************************************************
280 Free an encryption-allocated buffer.
281 ******************************************************************************/
283 void srv_free_enc_buffer(char *buf)
285 /* We know this is an smb buffer, and we
286 * didn't malloc, only copy, for a keepalive,
287 * so ignore session keepalives. */
289 if(CVAL(buf,0) == SMBkeepalive) {
293 if (srv_trans_enc_ctx) {
294 common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
298 /******************************************************************************
299 Decrypt an incoming buffer.
300 ******************************************************************************/
302 NTSTATUS srv_decrypt_buffer(char *buf)
304 /* Ignore session keepalives. */
305 if(CVAL(buf,0) == SMBkeepalive) {
309 if (srv_trans_enc_ctx) {
310 return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
316 /******************************************************************************
317 Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
318 ******************************************************************************/
320 NTSTATUS srv_encrypt_buffer(char *buf, char **buf_out)
324 /* Ignore session keepalives. */
325 if(CVAL(buf,0) == SMBkeepalive) {
329 if (srv_trans_enc_ctx) {
330 return common_encrypt_buffer(srv_trans_enc_ctx->es, buf, buf_out);
332 /* Not encrypting. */
336 /******************************************************************************
337 Do the gss encryption negotiation. Parameters are in/out.
338 Until success we do everything on the partial enc ctx.
339 ******************************************************************************/
341 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
342 static NTSTATUS srv_enc_spnego_gss_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob)
347 gss_buffer_desc in_buf, out_buf;
348 struct smb_tran_enc_state_gss *gss_state;
349 DATA_BLOB auth_reply = data_blob(NULL,0);
350 DATA_BLOB response = data_blob(NULL,0);
353 if (!partial_srv_trans_enc_ctx) {
354 status = make_srv_encryption_context(SMB_TRANS_ENC_GSS, &partial_srv_trans_enc_ctx);
355 if (!NT_STATUS_IS_OK(status)) {
360 gss_state = partial_srv_trans_enc_ctx->es->s.gss_state;
362 in_buf.value = secblob.data;
363 in_buf.length = secblob.length;
365 out_buf.value = NULL;
368 ret = gss_accept_sec_context(&min,
372 GSS_C_NO_CHANNEL_BINDINGS,
374 NULL, /* Ignore oids. */
375 &out_buf, /* To return. */
377 NULL, /* Ingore time. */
378 NULL); /* Ignore delegated creds. */
380 status = gss_err_to_ntstatus(ret, min);
381 if (ret != GSS_S_COMPLETE && ret != GSS_S_CONTINUE_NEEDED) {
385 /* Ensure we've got sign+seal available. */
386 if (ret == GSS_S_COMPLETE) {
387 if ((flags & (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) !=
388 (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) {
389 DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
390 "for SMB sealing.\n"));
391 gss_release_buffer(&min, &out_buf);
392 return NT_STATUS_ACCESS_DENIED;
396 auth_reply = data_blob(out_buf.value, out_buf.length);
397 gss_release_buffer(&min, &out_buf);
399 /* Wrap in SPNEGO. */
400 response = spnego_gen_auth_response(&auth_reply, status, OID_KERBEROS5);
401 data_blob_free(&auth_reply);
404 *ppdata = response.data;
405 *p_data_size = response.length;
411 /******************************************************************************
412 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
413 Until success we do everything on the partial enc ctx.
414 ******************************************************************************/
416 static NTSTATUS srv_enc_ntlm_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob, BOOL spnego_wrap)
419 DATA_BLOB chal = data_blob(NULL, 0);
420 DATA_BLOB response = data_blob(NULL, 0);
422 status = make_srv_encryption_context(SMB_TRANS_ENC_NTLM, &partial_srv_trans_enc_ctx);
423 if (!NT_STATUS_IS_OK(status)) {
427 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, secblob, &chal);
429 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
433 response = spnego_gen_auth_response(&chal, status, OID_NTLMSSP);
434 data_blob_free(&chal);
436 /* Return the raw blob. */
441 *ppdata = response.data;
442 *p_data_size = response.length;
446 /******************************************************************************
447 Do the SPNEGO encryption negotiation. Parameters are in/out.
448 Based off code in smbd/sesssionsetup.c
449 Until success we do everything on the partial enc ctx.
450 ******************************************************************************/
452 static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
453 unsigned char **ppdata,
455 unsigned char **pparam,
456 size_t *p_param_size)
459 DATA_BLOB blob = data_blob(NULL,0);
460 DATA_BLOB secblob = data_blob(NULL, 0);
461 BOOL got_kerberos_mechanism = False;
463 blob = data_blob_const(*ppdata, *p_data_size);
465 status = parse_spnego_mechanisms(blob, &secblob, &got_kerberos_mechanism);
466 if (!NT_STATUS_IS_OK(status)) {
467 return nt_status_squash(status);
470 /* We should have no partial context at this point. */
472 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
474 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
475 if (got_kerberos_mechanism && lp_use_kerberos_keytab() ) {
476 status = srv_enc_spnego_gss_negotiate(ppdata, p_data_size, secblob);
480 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, secblob, True);
483 data_blob_free(&secblob);
485 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
486 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
487 return nt_status_squash(status);
490 if (NT_STATUS_IS_OK(status)) {
491 /* Return the context we're using for this encryption state. */
492 *pparam = SMB_MALLOC(2);
494 return NT_STATUS_NO_MEMORY;
496 SSVAL(*pparam,0,partial_srv_trans_enc_ctx->es->enc_ctx_num);
503 /******************************************************************************
504 Complete a SPNEGO encryption negotiation. Parameters are in/out.
505 We only get this for a NTLM auth second stage.
506 ******************************************************************************/
508 static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
509 unsigned char **ppdata,
511 unsigned char **pparam,
512 size_t *p_param_size)
515 DATA_BLOB blob = data_blob(NULL,0);
516 DATA_BLOB auth = data_blob(NULL,0);
517 DATA_BLOB auth_reply = data_blob(NULL,0);
518 DATA_BLOB response = data_blob(NULL,0);
519 struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
521 /* We must have a partial context here. */
523 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
524 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
525 return NT_STATUS_INVALID_PARAMETER;
528 blob = data_blob_const(*ppdata, *p_data_size);
529 if (!spnego_parse_auth(blob, &auth)) {
530 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
531 return NT_STATUS_INVALID_PARAMETER;
534 status = auth_ntlmssp_update(ec->auth_ntlmssp_state, auth, &auth_reply);
535 data_blob_free(&auth);
537 response = spnego_gen_auth_response(&auth_reply, status, OID_NTLMSSP);
538 data_blob_free(&auth_reply);
540 if (NT_STATUS_IS_OK(status)) {
541 /* Return the context we're using for this encryption state. */
542 *pparam = SMB_MALLOC(2);
544 return NT_STATUS_NO_MEMORY;
546 SSVAL(*pparam,0,ec->es->enc_ctx_num);
551 *ppdata = response.data;
552 *p_data_size = response.length;
556 /******************************************************************************
557 Raw NTLM encryption negotiation. Parameters are in/out.
558 This function does both steps.
559 ******************************************************************************/
561 static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
562 unsigned char **ppdata,
564 unsigned char **pparam,
565 size_t *p_param_size)
568 DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
569 DATA_BLOB response = data_blob(NULL,0);
570 struct smb_srv_trans_enc_ctx *ec;
572 if (!partial_srv_trans_enc_ctx) {
573 /* This is the initial step. */
574 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, blob, False);
575 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
576 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
577 return nt_status_squash(status);
582 ec = partial_srv_trans_enc_ctx;
583 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
584 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
585 return NT_STATUS_INVALID_PARAMETER;
589 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, blob, &response);
591 if (NT_STATUS_IS_OK(status)) {
592 /* Return the context we're using for this encryption state. */
593 *pparam = SMB_MALLOC(2);
595 return NT_STATUS_NO_MEMORY;
597 SSVAL(*pparam,0,ec->es->enc_ctx_num);
601 /* Return the raw blob. */
603 *ppdata = response.data;
604 *p_data_size = response.length;
608 /******************************************************************************
609 Do the SPNEGO encryption negotiation. Parameters are in/out.
610 ******************************************************************************/
612 NTSTATUS srv_request_encryption_setup(connection_struct *conn,
613 unsigned char **ppdata,
615 unsigned char **pparam,
616 size_t *p_param_size)
618 unsigned char *pdata = *ppdata;
623 if (*p_data_size < 1) {
624 return NT_STATUS_INVALID_PARAMETER;
627 if (pdata[0] == ASN1_APPLICATION(0)) {
628 /* its a negTokenTarg packet */
629 return srv_enc_spnego_negotiate(conn, ppdata, p_data_size, pparam, p_param_size);
632 if (pdata[0] == ASN1_CONTEXT(1)) {
633 /* It's an auth packet */
634 return srv_enc_spnego_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
637 /* Maybe it's a raw unwrapped auth ? */
638 if (*p_data_size < 7) {
639 return NT_STATUS_INVALID_PARAMETER;
642 if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
643 return srv_enc_raw_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
646 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
648 return NT_STATUS_LOGON_FAILURE;
651 /******************************************************************************
652 Negotiation was successful - turn on server-side encryption.
653 ******************************************************************************/
655 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
657 if (!ec || !ec->es) {
658 return NT_STATUS_LOGON_FAILURE;
661 if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
662 if ((ec->es->s.ntlmssp_state->neg_flags & (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) !=
663 (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) {
664 return NT_STATUS_INVALID_PARAMETER;
667 /* Todo - check gssapi case. */
672 /******************************************************************************
673 Negotiation was successful - turn on server-side encryption.
674 ******************************************************************************/
676 NTSTATUS srv_encryption_start(connection_struct *conn)
680 /* Check that we are really doing sign+seal. */
681 status = check_enc_good(partial_srv_trans_enc_ctx);
682 if (!NT_STATUS_IS_OK(status)) {
685 /* Throw away the context we're using currently (if any). */
686 srv_free_encryption_context(&srv_trans_enc_ctx);
688 /* Steal the partial pointer. Deliberate shallow copy. */
689 srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
690 srv_trans_enc_ctx->es->enc_on = True;
692 partial_srv_trans_enc_ctx = NULL;
696 /******************************************************************************
697 Shutdown all server contexts.
698 ******************************************************************************/
700 void server_encryption_shutdown(void)
702 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
703 srv_free_encryption_context(&srv_trans_enc_ctx);