2 Unix SMB/CIFS implementation.
4 dcerpc schannel operations
6 Copyright (C) Andrew Tridgell 2004
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "librpc/gen_ndr/ndr_schannel.h"
25 #include "auth/auth.h"
26 #include "auth/credentials/credentials.h"
27 #include "auth/gensec/gensec.h"
28 #include "auth/gensec/gensec_proto.h"
29 #include "../libcli/auth/schannel.h"
30 #include "librpc/rpc/dcerpc.h"
31 #include "param/param.h"
33 static size_t schannel_sig_size(struct gensec_security
*gensec_security
, size_t data_size
)
35 struct schannel_state
*state
= (struct schannel_state
*)gensec_security
->private_data
;
38 sig_size
= netsec_outgoing_sig_size(state
);
43 static NTSTATUS
schannel_session_key(struct gensec_security
*gensec_security
,
44 DATA_BLOB
*session_key
)
46 return NT_STATUS_NOT_IMPLEMENTED
;
49 static NTSTATUS
schannel_update(struct gensec_security
*gensec_security
, TALLOC_CTX
*out_mem_ctx
,
50 const DATA_BLOB in
, DATA_BLOB
*out
)
52 struct schannel_state
*state
= (struct schannel_state
*)gensec_security
->private_data
;
54 enum ndr_err_code ndr_err
;
55 struct NL_AUTH_MESSAGE bind_schannel
;
56 struct NL_AUTH_MESSAGE bind_schannel_ack
;
57 struct netlogon_creds_CredentialState
*creds
;
58 const char *workstation
;
61 *out
= data_blob(NULL
, 0);
63 switch (gensec_security
->gensec_role
) {
65 if (state
->state
!= SCHANNEL_STATE_START
) {
66 /* we could parse the bind ack, but we don't know what it is yet */
70 state
->creds
= talloc_reference(state
, cli_credentials_get_netlogon_creds(gensec_security
->credentials
));
72 bind_schannel
.MessageType
= NL_NEGOTIATE_REQUEST
;
74 /* to support this we'd need to have access to the full domain name */
76 bind_schannel
.Flags
= NL_FLAG_OEM_NETBIOS_DOMAIN_NAME
|
77 NL_FLAG_OEM_NETBIOS_COMPUTER_NAME
|
78 NL_FLAG_UTF8_DNS_DOMAIN_NAME
|
79 NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME
;
80 bind_schannel
.oem_netbios_domain
.a
= cli_credentials_get_domain(gensec_security
->credentials
);
81 bind_schannel
.oem_netbios_computer
.a
= cli_credentials_get_workstation(gensec_security
->credentials
);
82 bind_schannel
.utf8_dns_domain
= cli_credentials_get_realm(gensec_security
->credentials
);
83 /* w2k3 refuses us if we use the full DNS workstation?
84 why? perhaps because we don't fill in the dNSHostName
85 attribute in the machine account? */
86 bind_schannel
.utf8_netbios_computer
= cli_credentials_get_workstation(gensec_security
->credentials
);
88 bind_schannel
.Flags
= NL_FLAG_OEM_NETBIOS_DOMAIN_NAME
|
89 NL_FLAG_OEM_NETBIOS_COMPUTER_NAME
;
90 bind_schannel
.oem_netbios_domain
.a
= cli_credentials_get_domain(gensec_security
->credentials
);
91 bind_schannel
.oem_netbios_computer
.a
= cli_credentials_get_workstation(gensec_security
->credentials
);
94 ndr_err
= ndr_push_struct_blob(out
, out_mem_ctx
, &bind_schannel
,
95 (ndr_push_flags_fn_t
)ndr_push_NL_AUTH_MESSAGE
);
96 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
97 status
= ndr_map_error2ntstatus(ndr_err
);
98 DEBUG(3, ("Could not create schannel bind: %s\n",
103 state
->state
= SCHANNEL_STATE_UPDATE_1
;
105 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
108 if (state
->state
!= SCHANNEL_STATE_START
) {
109 /* no third leg on this protocol */
110 return NT_STATUS_INVALID_PARAMETER
;
113 /* parse the schannel startup blob */
114 ndr_err
= ndr_pull_struct_blob(&in
, out_mem_ctx
, &bind_schannel
,
115 (ndr_pull_flags_fn_t
)ndr_pull_NL_AUTH_MESSAGE
);
116 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
117 status
= ndr_map_error2ntstatus(ndr_err
);
118 DEBUG(3, ("Could not parse incoming schannel bind: %s\n",
123 if (bind_schannel
.Flags
& NL_FLAG_OEM_NETBIOS_DOMAIN_NAME
) {
124 domain
= bind_schannel
.oem_netbios_domain
.a
;
125 if (strcasecmp_m(domain
, lpcfg_workgroup(gensec_security
->settings
->lp_ctx
)) != 0) {
126 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
127 domain
, lpcfg_workgroup(gensec_security
->settings
->lp_ctx
)));
128 return NT_STATUS_LOGON_FAILURE
;
130 } else if (bind_schannel
.Flags
& NL_FLAG_UTF8_DNS_DOMAIN_NAME
) {
131 domain
= bind_schannel
.utf8_dns_domain
.u
;
132 if (strcasecmp_m(domain
, lpcfg_dnsdomain(gensec_security
->settings
->lp_ctx
)) != 0) {
133 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
134 domain
, lpcfg_dnsdomain(gensec_security
->settings
->lp_ctx
)));
135 return NT_STATUS_LOGON_FAILURE
;
138 DEBUG(3, ("Request for schannel to without domain\n"));
139 return NT_STATUS_LOGON_FAILURE
;
142 if (bind_schannel
.Flags
& NL_FLAG_OEM_NETBIOS_COMPUTER_NAME
) {
143 workstation
= bind_schannel
.oem_netbios_computer
.a
;
144 } else if (bind_schannel
.Flags
& NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME
) {
145 workstation
= bind_schannel
.utf8_netbios_computer
.u
;
147 DEBUG(3, ("Request for schannel to without netbios workstation\n"));
148 return NT_STATUS_LOGON_FAILURE
;
151 status
= schannel_get_creds_state(out_mem_ctx
,
152 lpcfg_private_dir(gensec_security
->settings
->lp_ctx
),
153 workstation
, &creds
);
154 if (!NT_STATUS_IS_OK(status
)) {
155 DEBUG(3, ("Could not find session key for attempted schannel connection from %s: %s\n",
156 workstation
, nt_errstr(status
)));
157 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_HANDLE
)) {
158 return NT_STATUS_LOGON_FAILURE
;
163 state
->creds
= talloc_steal(state
, creds
);
165 bind_schannel_ack
.MessageType
= NL_NEGOTIATE_RESPONSE
;
166 bind_schannel_ack
.Flags
= 0;
167 bind_schannel_ack
.Buffer
.dummy
= 0x6c0000; /* actually I think
172 ndr_err
= ndr_push_struct_blob(out
, out_mem_ctx
, &bind_schannel_ack
,
173 (ndr_push_flags_fn_t
)ndr_push_NL_AUTH_MESSAGE
);
174 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
175 status
= ndr_map_error2ntstatus(ndr_err
);
176 DEBUG(3, ("Could not return schannel bind ack for client %s: %s\n",
177 workstation
, nt_errstr(status
)));
181 state
->state
= SCHANNEL_STATE_UPDATE_1
;
185 return NT_STATUS_INVALID_PARAMETER
;
189 * Return the struct netlogon_creds_CredentialState.
191 * Make sure not to call this unless gensec is using schannel...
194 /* TODO: make this non-public */
196 _PUBLIC_ NTSTATUS
dcerpc_schannel_creds(struct gensec_security
*gensec_security
,
198 struct netlogon_creds_CredentialState
**creds
)
200 struct schannel_state
*state
= talloc_get_type(gensec_security
->private_data
, struct schannel_state
);
202 *creds
= talloc_reference(mem_ctx
, state
->creds
);
204 return NT_STATUS_NO_MEMORY
;
211 * Returns anonymous credentials for schannel, matching Win2k3.
215 static NTSTATUS
schannel_session_info(struct gensec_security
*gensec_security
,
216 struct auth_session_info
**_session_info
)
218 struct schannel_state
*state
= talloc_get_type(gensec_security
->private_data
, struct schannel_state
);
219 return auth_anonymous_session_info(state
, gensec_security
->settings
->lp_ctx
, _session_info
);
222 static NTSTATUS
schannel_start(struct gensec_security
*gensec_security
)
224 struct schannel_state
*state
;
226 state
= talloc(gensec_security
, struct schannel_state
);
228 return NT_STATUS_NO_MEMORY
;
231 state
->state
= SCHANNEL_STATE_START
;
233 gensec_security
->private_data
= state
;
238 static NTSTATUS
schannel_server_start(struct gensec_security
*gensec_security
)
241 struct schannel_state
*state
;
243 status
= schannel_start(gensec_security
);
244 if (!NT_STATUS_IS_OK(status
)) {
248 state
= (struct schannel_state
*)gensec_security
->private_data
;
249 state
->initiator
= false;
254 static NTSTATUS
schannel_client_start(struct gensec_security
*gensec_security
)
257 struct schannel_state
*state
;
259 status
= schannel_start(gensec_security
);
260 if (!NT_STATUS_IS_OK(status
)) {
264 state
= (struct schannel_state
*)gensec_security
->private_data
;
265 state
->initiator
= true;
271 static bool schannel_have_feature(struct gensec_security
*gensec_security
,
274 if (feature
& (GENSEC_FEATURE_SIGN
|
275 GENSEC_FEATURE_SEAL
)) {
278 if (feature
& GENSEC_FEATURE_DCE_STYLE
) {
281 if (feature
& GENSEC_FEATURE_ASYNC_REPLIES
) {
290 static NTSTATUS
schannel_unseal_packet(struct gensec_security
*gensec_security
,
292 uint8_t *data
, size_t length
,
293 const uint8_t *whole_pdu
, size_t pdu_length
,
294 const DATA_BLOB
*sig
)
296 struct schannel_state
*state
=
297 talloc_get_type(gensec_security
->private_data
,
298 struct schannel_state
);
300 return netsec_incoming_packet(state
, mem_ctx
, true,
301 discard_const_p(uint8_t, data
),
306 check the signature on a packet
308 static NTSTATUS
schannel_check_packet(struct gensec_security
*gensec_security
,
310 const uint8_t *data
, size_t length
,
311 const uint8_t *whole_pdu
, size_t pdu_length
,
312 const DATA_BLOB
*sig
)
314 struct schannel_state
*state
=
315 talloc_get_type(gensec_security
->private_data
,
316 struct schannel_state
);
318 return netsec_incoming_packet(state
, mem_ctx
, false,
319 discard_const_p(uint8_t, data
),
325 static NTSTATUS
schannel_seal_packet(struct gensec_security
*gensec_security
,
327 uint8_t *data
, size_t length
,
328 const uint8_t *whole_pdu
, size_t pdu_length
,
331 struct schannel_state
*state
=
332 talloc_get_type(gensec_security
->private_data
,
333 struct schannel_state
);
335 return netsec_outgoing_packet(state
, mem_ctx
, true,
342 static NTSTATUS
schannel_sign_packet(struct gensec_security
*gensec_security
,
344 const uint8_t *data
, size_t length
,
345 const uint8_t *whole_pdu
, size_t pdu_length
,
348 struct schannel_state
*state
=
349 talloc_get_type(gensec_security
->private_data
,
350 struct schannel_state
);
352 return netsec_outgoing_packet(state
, mem_ctx
, false,
353 discard_const_p(uint8_t, data
),
357 static const struct gensec_security_ops gensec_schannel_security_ops
= {
359 .auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
,
360 .client_start
= schannel_client_start
,
361 .server_start
= schannel_server_start
,
362 .update
= schannel_update
,
363 .seal_packet
= schannel_seal_packet
,
364 .sign_packet
= schannel_sign_packet
,
365 .check_packet
= schannel_check_packet
,
366 .unseal_packet
= schannel_unseal_packet
,
367 .session_key
= schannel_session_key
,
368 .session_info
= schannel_session_info
,
369 .sig_size
= schannel_sig_size
,
370 .have_feature
= schannel_have_feature
,
372 .priority
= GENSEC_SCHANNEL
375 _PUBLIC_ NTSTATUS
gensec_schannel_init(void)
378 ret
= gensec_register(&gensec_schannel_security_ops
);
379 if (!NT_STATUS_IS_OK(ret
)) {
380 DEBUG(0,("Failed to register '%s' gensec backend!\n",
381 gensec_schannel_security_ops
.name
));