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 asprintf(&host_princ_s, "%s@%s", service, name);
107 if (host_princ_s == NULL) {
108 return NT_STATUS_NO_MEMORY;
111 input_name.value = host_princ_s;
112 input_name.length = strlen(host_princ_s) + 1;
114 ret = gss_import_name(&min,
116 GSS_C_NT_HOSTBASED_SERVICE,
119 if (ret != GSS_S_COMPLETE) {
120 SAFE_FREE(host_princ_s);
121 return map_nt_error_from_gss(ret, min);
124 ret = gss_acquire_cred(&min,
133 if (ret != GSS_S_COMPLETE) {
134 status = map_nt_error_from_gss(ret, min);
137 SAFE_FREE(host_princ_s);
138 gss_release_name(&min, &srv_name);
142 /******************************************************************************
144 Try and get the cifs/server@realm principal first, then fall back to
146 ******************************************************************************/
148 static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
151 gss_cred_id_t srv_cred;
154 name_to_fqdn(fqdn, global_myname());
157 status = get_srv_gss_creds("cifs", fqdn, GSS_C_ACCEPT, &srv_cred);
158 if (!NT_STATUS_IS_OK(status)) {
159 status = get_srv_gss_creds("host", fqdn, GSS_C_ACCEPT, &srv_cred);
160 if (!NT_STATUS_IS_OK(status)) {
161 return nt_status_squash(status);
165 ec->es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
166 if (!ec->es->s.gss_state) {
168 gss_release_cred(&min, &srv_cred);
169 return NT_STATUS_NO_MEMORY;
171 ZERO_STRUCTP(ec->es->s.gss_state);
172 ec->es->s.gss_state->creds = srv_cred;
174 /* No context yet. */
175 ec->es->s.gss_state->gss_ctx = GSS_C_NO_CONTEXT;
181 /******************************************************************************
182 Shutdown a server encryption context.
183 ******************************************************************************/
185 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
187 struct smb_srv_trans_enc_ctx *ec = *pp_ec;
194 switch (ec->es->smb_enc_type) {
195 case SMB_TRANS_ENC_NTLM:
196 destroy_auth_ntlmssp(ec);
198 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
199 case SMB_TRANS_ENC_GSS:
203 common_free_encryption_state(&ec->es);
210 /******************************************************************************
211 Create a server encryption context.
212 ******************************************************************************/
214 static NTSTATUS make_srv_encryption_context(enum smb_trans_enc_type smb_enc_type, struct smb_srv_trans_enc_ctx **pp_ec)
216 struct smb_srv_trans_enc_ctx *ec;
220 ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
222 return NT_STATUS_NO_MEMORY;
224 ZERO_STRUCTP(partial_srv_trans_enc_ctx);
225 ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
228 return NT_STATUS_NO_MEMORY;
230 ZERO_STRUCTP(ec->es);
231 ec->es->smb_enc_type = smb_enc_type;
232 switch (smb_enc_type) {
233 case SMB_TRANS_ENC_NTLM:
235 NTSTATUS status = make_auth_ntlmssp(ec);
236 if (!NT_STATUS_IS_OK(status)) {
237 srv_free_encryption_context(&ec);
243 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
244 case SMB_TRANS_ENC_GSS:
245 /* Acquire our credentials by calling gss_acquire_cred here. */
247 NTSTATUS status = make_auth_gss(ec);
248 if (!NT_STATUS_IS_OK(status)) {
249 srv_free_encryption_context(&ec);
256 srv_free_encryption_context(&ec);
257 return NT_STATUS_INVALID_PARAMETER;
263 /******************************************************************************
264 Free an encryption-allocated buffer.
265 ******************************************************************************/
267 void srv_free_enc_buffer(char *buf)
269 /* We know this is an smb buffer, and we
270 * didn't malloc, only copy, for a keepalive,
271 * so ignore session keepalives. */
273 if(CVAL(buf,0) == SMBkeepalive) {
277 if (srv_trans_enc_ctx) {
278 common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
282 /******************************************************************************
283 Decrypt an incoming buffer.
284 ******************************************************************************/
286 NTSTATUS srv_decrypt_buffer(char *buf)
288 /* Ignore session keepalives. */
289 if(CVAL(buf,0) == SMBkeepalive) {
293 if (srv_trans_enc_ctx) {
294 return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
300 /******************************************************************************
301 Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
302 ******************************************************************************/
304 NTSTATUS srv_encrypt_buffer(char *buf, char **buf_out)
308 /* Ignore session keepalives. */
309 if(CVAL(buf,0) == SMBkeepalive) {
313 if (srv_trans_enc_ctx) {
314 return common_encrypt_buffer(srv_trans_enc_ctx->es, buf, buf_out);
316 /* Not encrypting. */
320 /******************************************************************************
321 Do the gss encryption negotiation. Parameters are in/out.
322 Until success we do everything on the partial enc ctx.
323 ******************************************************************************/
325 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
326 static NTSTATUS srv_enc_spnego_gss_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob)
331 gss_buffer_desc in_buf, out_buf;
332 struct smb_tran_enc_state_gss *gss_state;
334 if (!partial_srv_trans_enc_ctx) {
335 NTSTATUS status = make_srv_encryption_context(SMB_TRANS_ENC_GSS, &partial_srv_trans_enc_ctx);
336 if (!NT_STATUS_IS_OK(status)) {
341 gss_state = partial_srv_trans_enc_ctx->es->s.gss_state;
343 in_buf.value = secblob.data;
344 in_buf.length = secblob.length;
346 out_buf.value = NULL;
349 ret = gss_accept_sec_context(&min,
353 GSS_C_NO_CHANNEL_BINDINGS,
355 NULL, /* Ignore oids. */
356 &out_buf, /* To return. */
358 NULL, /* Ingore time. */
359 NULL); /* Ignore delegated creds. */
361 if (ret != GSS_S_COMPLETE && ret != GSS_S_CONTINUE_NEEDED) {
362 return gss_err_to_ntstatus(ret, min);
365 /* Ensure we've got sign+seal available. */
366 if (ret == GSS_S_COMPLETE) {
367 if ((flags & (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) !=
368 (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) {
369 DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
370 "for SMB sealing.\n"));
371 gss_release_buffer(&min, &out_buf);
372 return NT_STATUS_ACCESS_DENIED;
377 *ppdata = memdup(out_buf.value, out_buf.length);
379 gss_release_buffer(&min, &out_buf);
380 return NT_STATUS_NO_MEMORY;
382 *p_data_size = out_buf.length;
383 gss_release_buffer(&min, &out_buf);
385 if (ret != GSS_S_CONTINUE_NEEDED) {
386 return NT_STATUS_MORE_PROCESSING_REQUIRED;
393 /******************************************************************************
394 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
395 Until success we do everything on the partial enc ctx.
396 ******************************************************************************/
398 static NTSTATUS srv_enc_ntlm_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob, BOOL spnego_wrap)
401 DATA_BLOB chal = data_blob(NULL, 0);
402 DATA_BLOB response = data_blob(NULL, 0);
404 status = make_srv_encryption_context(SMB_TRANS_ENC_NTLM, &partial_srv_trans_enc_ctx);
405 if (!NT_STATUS_IS_OK(status)) {
409 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, secblob, &chal);
411 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
415 response = spnego_gen_auth_response(&chal, status, OID_NTLMSSP);
416 data_blob_free(&chal);
418 /* Return the raw blob. */
423 *ppdata = response.data;
424 *p_data_size = response.length;
428 /******************************************************************************
429 Do the SPNEGO encryption negotiation. Parameters are in/out.
430 Based off code in smbd/sesssionsetup.c
431 Until success we do everything on the partial enc ctx.
432 ******************************************************************************/
434 static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
435 unsigned char **ppdata,
437 unsigned char **pparam,
438 size_t *p_param_size)
441 DATA_BLOB blob = data_blob(NULL,0);
442 DATA_BLOB secblob = data_blob(NULL, 0);
443 BOOL got_kerberos_mechanism = False;
445 blob = data_blob_const(*ppdata, *p_data_size);
447 status = parse_spnego_mechanisms(blob, &secblob, &got_kerberos_mechanism);
448 if (!NT_STATUS_IS_OK(status)) {
449 return nt_status_squash(status);
452 /* We should have no partial context at this point. */
454 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
456 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
457 if (got_kerberos_mechanism && lp_use_kerberos_keytab() ) {
458 status = srv_enc_spnego_gss_negotiate(ppdata, p_data_size, secblob);
462 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, secblob, True);
465 data_blob_free(&secblob);
467 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
468 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
469 return nt_status_squash(status);
472 if (NT_STATUS_IS_OK(status)) {
473 /* Return the context we're using for this encryption state. */
474 *pparam = SMB_MALLOC(2);
476 return NT_STATUS_NO_MEMORY;
478 SSVAL(*pparam,0,partial_srv_trans_enc_ctx->es->enc_ctx_num);
485 /******************************************************************************
486 Complete a SPNEGO encryption negotiation. Parameters are in/out.
487 We only get this for a NTLM auth second stage.
488 ******************************************************************************/
490 static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
491 unsigned char **ppdata,
493 unsigned char **pparam,
494 size_t *p_param_size)
497 DATA_BLOB blob = data_blob(NULL,0);
498 DATA_BLOB auth = data_blob(NULL,0);
499 DATA_BLOB auth_reply = data_blob(NULL,0);
500 DATA_BLOB response = data_blob(NULL,0);
501 struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
503 /* We must have a partial context here. */
505 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
506 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
507 return NT_STATUS_INVALID_PARAMETER;
510 blob = data_blob_const(*ppdata, *p_data_size);
511 if (!spnego_parse_auth(blob, &auth)) {
512 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
513 return NT_STATUS_INVALID_PARAMETER;
516 status = auth_ntlmssp_update(ec->auth_ntlmssp_state, auth, &auth_reply);
517 data_blob_free(&auth);
519 response = spnego_gen_auth_response(&auth_reply, status, OID_NTLMSSP);
520 data_blob_free(&auth_reply);
522 if (NT_STATUS_IS_OK(status)) {
523 /* Return the context we're using for this encryption state. */
524 *pparam = SMB_MALLOC(2);
526 return NT_STATUS_NO_MEMORY;
528 SSVAL(*pparam,0,ec->es->enc_ctx_num);
533 *ppdata = response.data;
534 *p_data_size = response.length;
538 /******************************************************************************
539 Raw NTLM encryption negotiation. Parameters are in/out.
540 This function does both steps.
541 ******************************************************************************/
543 static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
544 unsigned char **ppdata,
546 unsigned char **pparam,
547 size_t *p_param_size)
550 DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
551 DATA_BLOB response = data_blob(NULL,0);
552 struct smb_srv_trans_enc_ctx *ec;
554 if (!partial_srv_trans_enc_ctx) {
555 /* This is the initial step. */
556 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, blob, False);
557 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
558 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
559 return nt_status_squash(status);
564 ec = partial_srv_trans_enc_ctx;
565 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
566 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
567 return NT_STATUS_INVALID_PARAMETER;
571 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, blob, &response);
573 if (NT_STATUS_IS_OK(status)) {
574 /* Return the context we're using for this encryption state. */
575 *pparam = SMB_MALLOC(2);
577 return NT_STATUS_NO_MEMORY;
579 SSVAL(*pparam,0,ec->es->enc_ctx_num);
583 /* Return the raw blob. */
585 *ppdata = response.data;
586 *p_data_size = response.length;
590 /******************************************************************************
591 Do the SPNEGO encryption negotiation. Parameters are in/out.
592 ******************************************************************************/
594 NTSTATUS srv_request_encryption_setup(connection_struct *conn,
595 unsigned char **ppdata,
597 unsigned char **pparam,
598 size_t *p_param_size)
600 unsigned char *pdata = *ppdata;
605 if (*p_data_size < 1) {
606 return NT_STATUS_INVALID_PARAMETER;
609 if (pdata[0] == ASN1_APPLICATION(0)) {
610 /* its a negTokenTarg packet */
611 return srv_enc_spnego_negotiate(conn, ppdata, p_data_size, pparam, p_param_size);
614 if (pdata[0] == ASN1_CONTEXT(1)) {
615 /* It's an auth packet */
616 return srv_enc_spnego_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
619 /* Maybe it's a raw unwrapped auth ? */
620 if (*p_data_size < 7) {
621 return NT_STATUS_INVALID_PARAMETER;
624 if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
625 return srv_enc_raw_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
628 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
630 return NT_STATUS_LOGON_FAILURE;
633 /******************************************************************************
634 Negotiation was successful - turn on server-side encryption.
635 ******************************************************************************/
637 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
639 if (!ec || !ec->es) {
640 return NT_STATUS_LOGON_FAILURE;
643 if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
644 if ((ec->es->s.ntlmssp_state->neg_flags & (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) !=
645 (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) {
646 return NT_STATUS_INVALID_PARAMETER;
649 /* Todo - check gssapi case. */
654 /******************************************************************************
655 Negotiation was successful - turn on server-side encryption.
656 ******************************************************************************/
658 NTSTATUS srv_encryption_start(connection_struct *conn)
662 /* Check that we are really doing sign+seal. */
663 status = check_enc_good(partial_srv_trans_enc_ctx);
664 if (!NT_STATUS_IS_OK(status)) {
667 /* Throw away the context we're using currently (if any). */
668 srv_free_encryption_context(&srv_trans_enc_ctx);
670 /* Steal the partial pointer. Deliberate shallow copy. */
671 srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
672 srv_trans_enc_ctx->es->enc_on = True;
674 partial_srv_trans_enc_ctx = NULL;
678 /******************************************************************************
679 Shutdown all server contexts.
680 ******************************************************************************/
682 void server_encryption_shutdown(void)
684 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
685 srv_free_encryption_context(&srv_trans_enc_ctx);