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_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 ******************************************************************************/
146 static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
149 gss_cred_id_t srv_cred;
152 name_to_fqdn(fqdn, global_myname());
155 status = get_gss_creds("cifs", fqdn, GSS_C_ACCEPT, &srv_cred);
156 if (!NT_STATUS_IS_OK(status)) {
157 status = get_gss_creds("host", fqdn, GSS_C_ACCEPT, &srv_cred);
158 if (!NT_STATUS_IS_OK(status)) {
159 return nt_status_squash(status);
167 /******************************************************************************
168 Shutdown a server encryption context.
169 ******************************************************************************/
171 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
173 struct smb_srv_trans_enc_ctx *ec = *pp_ec;
180 switch (ec->es->smb_enc_type) {
181 case SMB_TRANS_ENC_NTLM:
182 destroy_auth_ntlmssp(ec);
184 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
185 case SMB_TRANS_ENC_GSS:
189 common_free_encryption_state(&ec->es);
196 /******************************************************************************
197 Create a server encryption context.
198 ******************************************************************************/
200 static struct smb_srv_trans_enc_ctx *make_srv_encryption_context(enum smb_trans_enc_type smb_enc_type)
202 struct smb_srv_trans_enc_ctx *ec;
204 ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
208 ZERO_STRUCTP(partial_srv_trans_enc_ctx);
209 ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
214 ZERO_STRUCTP(ec->es);
215 ec->es->smb_enc_type = smb_enc_type;
216 switch (smb_enc_type) {
217 case SMB_TRANS_ENC_NTLM:
219 NTSTATUS status = make_auth_ntlmssp(ec);
220 if (!NT_STATUS_IS_OK(status)) {
221 srv_free_encryption_context(&ec);
227 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
228 case SMB_TRANS_ENC_GSS:
229 /* Acquire our credentials by calling gss_acquire_cred here. */
231 NTSTATUS status = make_auth_gss(ec);
232 if (!NT_STATUS_IS_OK(status)) {
233 srv_free_encryption_context(&ec);
240 srv_free_encryption_context(&ec);
246 /******************************************************************************
247 Free an encryption-allocated buffer.
248 ******************************************************************************/
250 void srv_free_enc_buffer(char *buf)
252 if (srv_trans_enc_ctx) {
253 common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
257 /******************************************************************************
258 Decrypt an incoming buffer.
259 ******************************************************************************/
261 NTSTATUS srv_decrypt_buffer(char *buf)
263 if (srv_trans_enc_ctx) {
264 return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
269 /******************************************************************************
270 Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
271 ******************************************************************************/
273 NTSTATUS srv_encrypt_buffer(char *buffer, char **buf_out)
275 if (srv_trans_enc_ctx) {
276 return common_encrypt_buffer(srv_trans_enc_ctx->es, buffer, buf_out);
278 /* Not encrypting. */
283 /******************************************************************************
284 Do the gss encryption negotiation. Parameters are in/out.
285 Until success we do everything on the partial enc ctx.
286 ******************************************************************************/
288 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
289 static NTSTATUS srv_enc_spnego_gss_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob)
291 if (!partial_srv_trans_enc_ctx) {
292 partial_srv_trans_enc_ctx = make_srv_encryption_context(SMB_TRANS_ENC_GSS);
293 if (!partial_srv_trans_enc_ctx) {
294 return NT_STATUS_NO_MEMORY;
298 return NT_STATUS_NOT_SUPPORTED;
302 /******************************************************************************
303 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
304 Until success we do everything on the partial enc ctx.
305 ******************************************************************************/
307 static NTSTATUS srv_enc_ntlm_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob, BOOL spnego_wrap)
310 DATA_BLOB chal = data_blob(NULL, 0);
311 DATA_BLOB response = data_blob(NULL, 0);
313 partial_srv_trans_enc_ctx = make_srv_encryption_context(SMB_TRANS_ENC_NTLM);
314 if (!partial_srv_trans_enc_ctx) {
315 return NT_STATUS_NO_MEMORY;
318 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, secblob, &chal);
320 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
324 response = spnego_gen_auth_response(&chal, status, OID_NTLMSSP);
325 data_blob_free(&chal);
327 /* Return the raw blob. */
332 *ppdata = response.data;
333 *p_data_size = response.length;
337 /******************************************************************************
338 Do the SPNEGO encryption negotiation. Parameters are in/out.
339 Based off code in smbd/sesssionsetup.c
340 Until success we do everything on the partial enc ctx.
341 ******************************************************************************/
343 static NTSTATUS srv_enc_spnego_negotiate(unsigned char **ppdata, size_t *p_data_size)
346 DATA_BLOB blob = data_blob(NULL,0);
347 DATA_BLOB secblob = data_blob(NULL, 0);
348 BOOL got_kerberos_mechanism = False;
350 blob = data_blob_const(*ppdata, *p_data_size);
352 status = parse_spnego_mechanisms(blob, &secblob, &got_kerberos_mechanism);
353 if (!NT_STATUS_IS_OK(status)) {
354 return nt_status_squash(status);
357 /* We should have no partial context at this point. */
359 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
361 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
362 if (got_kerberos_mechanism && lp_use_kerberos_keytab() ) {
363 status = srv_enc_spnego_gss_negotiate(ppdata, p_data_size, secblob);
367 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, secblob, True);
370 data_blob_free(&secblob);
372 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
373 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
379 /******************************************************************************
380 Complete a SPNEGO encryption negotiation. Parameters are in/out.
381 We only get this for a NTLM auth second stage.
382 ******************************************************************************/
384 static NTSTATUS srv_enc_spnego_ntlm_auth(unsigned char **ppdata, size_t *p_data_size)
387 DATA_BLOB blob = data_blob(NULL,0);
388 DATA_BLOB auth = data_blob(NULL,0);
389 DATA_BLOB auth_reply = data_blob(NULL,0);
390 DATA_BLOB response = data_blob(NULL,0);
391 struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
393 /* We must have a partial context here. */
395 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
396 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
397 return NT_STATUS_INVALID_PARAMETER;
400 blob = data_blob_const(*ppdata, *p_data_size);
401 if (!spnego_parse_auth(blob, &auth)) {
402 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
403 return NT_STATUS_INVALID_PARAMETER;
406 status = auth_ntlmssp_update(ec->auth_ntlmssp_state, auth, &auth_reply);
407 data_blob_free(&auth);
409 response = spnego_gen_auth_response(&auth_reply, status, OID_NTLMSSP);
410 data_blob_free(&auth_reply);
413 *ppdata = response.data;
414 *p_data_size = response.length;
418 /******************************************************************************
419 Raw NTLM encryption negotiation. Parameters are in/out.
420 This function does both steps.
421 ******************************************************************************/
423 static NTSTATUS srv_enc_raw_ntlm_auth(unsigned char **ppdata, size_t *p_data_size)
426 DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
427 DATA_BLOB response = data_blob(NULL,0);
428 struct smb_srv_trans_enc_ctx *ec;
430 if (!partial_srv_trans_enc_ctx) {
431 /* This is the initial step. */
432 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, blob, False);
433 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
434 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
435 return nt_status_squash(status);
440 ec = partial_srv_trans_enc_ctx;
441 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
442 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
443 return NT_STATUS_INVALID_PARAMETER;
447 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, blob, &response);
449 /* Return the raw blob. */
451 *ppdata = response.data;
452 *p_data_size = response.length;
456 /******************************************************************************
457 Do the SPNEGO encryption negotiation. Parameters are in/out.
458 ******************************************************************************/
460 NTSTATUS srv_request_encryption_setup(unsigned char **ppdata, size_t *p_data_size)
462 unsigned char *pdata = *ppdata;
464 if (*p_data_size < 1) {
465 return NT_STATUS_INVALID_PARAMETER;
468 if (pdata[0] == ASN1_APPLICATION(0)) {
470 * Until success we do everything on the partial
473 /* its a negTokenTarg packet */
474 return srv_enc_spnego_negotiate(ppdata, p_data_size);
477 if (pdata[0] == ASN1_CONTEXT(1)) {
478 /* It's an auth packet */
479 return srv_enc_spnego_ntlm_auth(ppdata, p_data_size);
482 /* Maybe it's a raw unwrapped auth ? */
483 if (*p_data_size < 7) {
484 return NT_STATUS_INVALID_PARAMETER;
487 if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
488 return srv_enc_raw_ntlm_auth(ppdata, p_data_size);
491 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
493 return NT_STATUS_LOGON_FAILURE;
496 /******************************************************************************
497 Negotiation was successful - turn on server-side encryption.
498 ******************************************************************************/
500 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
502 if (!ec || !ec->es) {
503 return NT_STATUS_LOGON_FAILURE;
506 if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
507 if ((ec->es->s.ntlmssp_state->neg_flags & (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) !=
508 (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) {
509 return NT_STATUS_INVALID_PARAMETER;
512 /* Todo - check gssapi case. */
517 /******************************************************************************
518 Negotiation was successful - turn on server-side encryption.
519 ******************************************************************************/
521 NTSTATUS srv_encryption_start(void)
525 /* Check that we are really doing sign+seal. */
526 status = check_enc_good(partial_srv_trans_enc_ctx);
527 if (!NT_STATUS_IS_OK(status)) {
530 /* Throw away the context we're using currently (if any). */
531 srv_free_encryption_context(&srv_trans_enc_ctx);
533 /* Steal the partial pointer. Deliberate shallow copy. */
534 srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
535 srv_trans_enc_ctx->es->enc_on = True;
537 partial_srv_trans_enc_ctx = NULL;
541 /******************************************************************************
542 Shutdown all server contexts.
543 ******************************************************************************/
545 void server_encryption_shutdown(void)
547 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
548 srv_free_encryption_context(&srv_trans_enc_ctx);