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
->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
->ntlmssp_state
= NULL
;
88 /******************************************************************************
89 Shutdown a server encryption context.
90 ******************************************************************************/
92 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx
**pp_ec
)
94 struct smb_srv_trans_enc_ctx
*ec
= *pp_ec
;
101 if (ec
->es
->smb_enc_type
== SMB_TRANS_ENC_NTLM
) {
102 destroy_auth_ntlmssp(ec
);
104 common_free_encryption_state(&ec
->es
);
111 /******************************************************************************
112 Create a server encryption context.
113 ******************************************************************************/
115 static struct smb_srv_trans_enc_ctx
*make_srv_encryption_context(enum smb_trans_enc_type smb_enc_type
)
117 struct smb_srv_trans_enc_ctx
*ec
;
119 ec
= SMB_MALLOC_P(struct smb_srv_trans_enc_ctx
);
123 ZERO_STRUCTP(partial_srv_trans_enc_ctx
);
124 ec
->es
= SMB_MALLOC_P(struct smb_trans_enc_state
);
129 ZERO_STRUCTP(ec
->es
);
130 ec
->es
->smb_enc_type
= smb_enc_type
;
131 if (smb_enc_type
== SMB_TRANS_ENC_NTLM
) {
132 NTSTATUS status
= make_auth_ntlmssp(ec
);
133 if (!NT_STATUS_IS_OK(status
)) {
134 srv_free_encryption_context(&ec
);
141 /******************************************************************************
142 Free an encryption-allocated buffer.
143 ******************************************************************************/
145 void srv_free_enc_buffer(char *buf
)
147 if (srv_trans_enc_ctx
) {
148 common_free_enc_buffer(srv_trans_enc_ctx
->es
, buf
);
152 /******************************************************************************
153 Decrypt an incoming buffer.
154 ******************************************************************************/
156 NTSTATUS
srv_decrypt_buffer(char *buf
)
158 if (srv_trans_enc_ctx
) {
159 return common_decrypt_buffer(srv_trans_enc_ctx
->es
, buf
);
164 /******************************************************************************
165 Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
166 ******************************************************************************/
168 NTSTATUS
srv_encrypt_buffer(char *buffer
, char **buf_out
)
170 if (srv_trans_enc_ctx
) {
171 return common_encrypt_buffer(srv_trans_enc_ctx
->es
, buffer
, buf_out
);
173 /* Not encrypting. */
178 /******************************************************************************
179 Do the gss encryption negotiation. Parameters are in/out.
180 Until success we do everything on the partial enc ctx.
181 ******************************************************************************/
183 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
184 static NTSTATUS
srv_enc_spnego_gss_negotiate(unsigned char **ppdata
, size_t *p_data_size
, DATA_BLOB secblob
)
186 return NT_STATUS_NOT_SUPPORTED
;
190 /******************************************************************************
191 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
192 Until success we do everything on the partial enc ctx.
193 ******************************************************************************/
195 static NTSTATUS
srv_enc_ntlm_negotiate(unsigned char **ppdata
, size_t *p_data_size
, DATA_BLOB secblob
, BOOL spnego_wrap
)
198 DATA_BLOB chal
= data_blob(NULL
, 0);
199 DATA_BLOB response
= data_blob(NULL
, 0);
201 partial_srv_trans_enc_ctx
= make_srv_encryption_context(SMB_TRANS_ENC_NTLM
);
202 if (!partial_srv_trans_enc_ctx
) {
203 return NT_STATUS_NO_MEMORY
;
206 status
= auth_ntlmssp_update(partial_srv_trans_enc_ctx
->auth_ntlmssp_state
, secblob
, &chal
);
208 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
212 response
= spnego_gen_auth_response(&chal
, status
, OID_NTLMSSP
);
213 data_blob_free(&chal
);
215 /* Return the raw blob. */
220 *ppdata
= response
.data
;
221 *p_data_size
= response
.length
;
225 /******************************************************************************
226 Do the SPNEGO encryption negotiation. Parameters are in/out.
227 Based off code in smbd/sesssionsetup.c
228 Until success we do everything on the partial enc ctx.
229 ******************************************************************************/
231 static NTSTATUS
srv_enc_spnego_negotiate(unsigned char **ppdata
, size_t *p_data_size
)
234 DATA_BLOB blob
= data_blob(NULL
,0);
235 DATA_BLOB secblob
= data_blob(NULL
, 0);
236 BOOL got_kerberos_mechanism
= False
;
238 blob
= data_blob_const(*ppdata
, *p_data_size
);
240 status
= parse_spnego_mechanisms(blob
, &secblob
, &got_kerberos_mechanism
);
241 if (!NT_STATUS_IS_OK(status
)) {
242 return nt_status_squash(status
);
245 /* We should have no partial context at this point. */
247 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
249 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
250 if (got_kerberos_mechanism
&& lp_use_kerberos_keytab() ) {
251 status
= srv_enc_spnego_gss_negotiate(ppdata
, p_data_size
, secblob
);
255 status
= srv_enc_ntlm_negotiate(ppdata
, p_data_size
, secblob
, True
);
258 data_blob_free(&secblob
);
260 if (!NT_STATUS_EQUAL(status
,NT_STATUS_MORE_PROCESSING_REQUIRED
) && !NT_STATUS_IS_OK(status
)) {
261 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
267 /******************************************************************************
268 Complete a SPNEGO encryption negotiation. Parameters are in/out.
269 We only get this for a NTLM auth second stage.
270 ******************************************************************************/
272 static NTSTATUS
srv_enc_spnego_ntlm_auth(unsigned char **ppdata
, size_t *p_data_size
)
275 DATA_BLOB blob
= data_blob(NULL
,0);
276 DATA_BLOB auth
= data_blob(NULL
,0);
277 DATA_BLOB auth_reply
= data_blob(NULL
,0);
278 DATA_BLOB response
= data_blob(NULL
,0);
279 struct smb_srv_trans_enc_ctx
*ec
= partial_srv_trans_enc_ctx
;
281 /* We must have a partial context here. */
283 if (!ec
|| !ec
->es
|| ec
->auth_ntlmssp_state
== NULL
|| ec
->es
->smb_enc_type
!= SMB_TRANS_ENC_NTLM
) {
284 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
285 return NT_STATUS_INVALID_PARAMETER
;
288 blob
= data_blob_const(*ppdata
, *p_data_size
);
289 if (!spnego_parse_auth(blob
, &auth
)) {
290 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
291 return NT_STATUS_INVALID_PARAMETER
;
294 status
= auth_ntlmssp_update(ec
->auth_ntlmssp_state
, auth
, &auth_reply
);
295 data_blob_free(&auth
);
297 response
= spnego_gen_auth_response(&auth_reply
, status
, OID_NTLMSSP
);
298 data_blob_free(&auth_reply
);
301 *ppdata
= response
.data
;
302 *p_data_size
= response
.length
;
306 /******************************************************************************
307 Raw NTLM encryption negotiation. Parameters are in/out.
308 This function does both steps.
309 ******************************************************************************/
311 static NTSTATUS
srv_enc_raw_ntlm_auth(unsigned char **ppdata
, size_t *p_data_size
)
314 DATA_BLOB blob
= data_blob_const(*ppdata
, *p_data_size
);
315 DATA_BLOB response
= data_blob(NULL
,0);
316 struct smb_srv_trans_enc_ctx
*ec
;
318 if (!partial_srv_trans_enc_ctx
) {
319 /* This is the initial step. */
320 status
= srv_enc_ntlm_negotiate(ppdata
, p_data_size
, blob
, False
);
321 if (!NT_STATUS_EQUAL(status
,NT_STATUS_MORE_PROCESSING_REQUIRED
) && !NT_STATUS_IS_OK(status
)) {
322 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
323 return nt_status_squash(status
);
328 ec
= partial_srv_trans_enc_ctx
;
329 if (!ec
|| !ec
->es
|| ec
->auth_ntlmssp_state
== NULL
|| ec
->es
->smb_enc_type
!= SMB_TRANS_ENC_NTLM
) {
330 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
331 return NT_STATUS_INVALID_PARAMETER
;
335 status
= auth_ntlmssp_update(partial_srv_trans_enc_ctx
->auth_ntlmssp_state
, blob
, &response
);
337 /* Return the raw blob. */
339 *ppdata
= response
.data
;
340 *p_data_size
= response
.length
;
344 /******************************************************************************
345 Do the SPNEGO encryption negotiation. Parameters are in/out.
346 ******************************************************************************/
348 NTSTATUS
srv_request_encryption_setup(unsigned char **ppdata
, size_t *p_data_size
)
350 unsigned char *pdata
= *ppdata
;
352 if (*p_data_size
< 1) {
353 return NT_STATUS_INVALID_PARAMETER
;
356 if (pdata
[0] == ASN1_APPLICATION(0)) {
358 * Until success we do everything on the partial
361 /* its a negTokenTarg packet */
362 return srv_enc_spnego_negotiate(ppdata
, p_data_size
);
365 if (pdata
[0] == ASN1_CONTEXT(1)) {
366 /* It's an auth packet */
367 return srv_enc_spnego_ntlm_auth(ppdata
, p_data_size
);
370 /* Maybe it's a raw unwrapped auth ? */
371 if (*p_data_size
< 7) {
372 return NT_STATUS_INVALID_PARAMETER
;
375 if (strncmp((char *)pdata
, "NTLMSSP", 7) == 0) {
376 return srv_enc_raw_ntlm_auth(ppdata
, p_data_size
);
379 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
381 return NT_STATUS_LOGON_FAILURE
;
384 /******************************************************************************
385 Negotiation was successful - turn on server-side encryption.
386 ******************************************************************************/
388 static NTSTATUS
check_enc_good(struct smb_srv_trans_enc_ctx
*ec
)
390 if (!ec
|| !ec
->es
) {
391 return NT_STATUS_LOGON_FAILURE
;
394 if (ec
->es
->smb_enc_type
== SMB_TRANS_ENC_NTLM
) {
395 if ((ec
->es
->ntlmssp_state
->neg_flags
& (NTLMSSP_NEGOTIATE_SIGN
|NTLMSSP_NEGOTIATE_SEAL
)) !=
396 (NTLMSSP_NEGOTIATE_SIGN
|NTLMSSP_NEGOTIATE_SEAL
)) {
397 return NT_STATUS_INVALID_PARAMETER
;
400 /* Todo - check gssapi case. */
405 /******************************************************************************
406 Negotiation was successful - turn on server-side encryption.
407 ******************************************************************************/
409 NTSTATUS
srv_encryption_start(void)
413 /* Check that we are really doing sign+seal. */
414 status
= check_enc_good(partial_srv_trans_enc_ctx
);
415 if (!NT_STATUS_IS_OK(status
)) {
418 /* Throw away the context we're using currently (if any). */
419 srv_free_encryption_context(&srv_trans_enc_ctx
);
421 /* Steal the partial pointer. Deliberate shallow copy. */
422 srv_trans_enc_ctx
= partial_srv_trans_enc_ctx
;
423 srv_trans_enc_ctx
->es
->enc_on
= True
;
425 partial_srv_trans_enc_ctx
= NULL
;
429 /******************************************************************************
430 Shutdown all server contexts.
431 ******************************************************************************/
433 void server_encryption_shutdown(void)
435 srv_free_encryption_context(&partial_srv_trans_enc_ctx
);
436 srv_free_encryption_context(&srv_trans_enc_ctx
);