VERSION: Bump version number up to 3.6.17.
[Samba.git] / source4 / auth / gensec / schannel.c
blob45e5954b947cc4c1ecd91dd8279222729b8b27d7
1 /*
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/>.
23 #include "includes.h"
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;
36 uint32_t sig_size;
38 sig_size = netsec_outgoing_sig_size(state);
40 return sig_size;
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;
53 NTSTATUS status;
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;
59 const char *domain;
61 *out = data_blob(NULL, 0);
63 switch (gensec_security->gensec_role) {
64 case GENSEC_CLIENT:
65 if (state->state != SCHANNEL_STATE_START) {
66 /* we could parse the bind ack, but we don't know what it is yet */
67 return NT_STATUS_OK;
70 state->creds = talloc_reference(state, cli_credentials_get_netlogon_creds(gensec_security->credentials));
72 bind_schannel.MessageType = NL_NEGOTIATE_REQUEST;
73 #if 0
74 /* to support this we'd need to have access to the full domain name */
75 /* 0x17, 23 */
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);
87 #else
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);
92 #endif
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",
99 nt_errstr(status)));
100 return status;
103 state->state = SCHANNEL_STATE_UPDATE_1;
105 return NT_STATUS_MORE_PROCESSING_REQUIRED;
106 case GENSEC_SERVER:
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",
119 nt_errstr(status)));
120 return status;
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;
137 } else {
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;
146 } else {
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;
160 return status;
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
168 * this does not have
169 * any meaning here
170 * - gd */
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)));
178 return status;
181 state->state = SCHANNEL_STATE_UPDATE_1;
183 return NT_STATUS_OK;
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,
197 TALLOC_CTX *mem_ctx,
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);
203 if (!*creds) {
204 return NT_STATUS_NO_MEMORY;
206 return NT_STATUS_OK;
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);
227 if (!state) {
228 return NT_STATUS_NO_MEMORY;
231 state->state = SCHANNEL_STATE_START;
232 state->seq_num = 0;
233 gensec_security->private_data = state;
235 return NT_STATUS_OK;
238 static NTSTATUS schannel_server_start(struct gensec_security *gensec_security)
240 NTSTATUS status;
241 struct schannel_state *state;
243 status = schannel_start(gensec_security);
244 if (!NT_STATUS_IS_OK(status)) {
245 return status;
248 state = (struct schannel_state *)gensec_security->private_data;
249 state->initiator = false;
251 return NT_STATUS_OK;
254 static NTSTATUS schannel_client_start(struct gensec_security *gensec_security)
256 NTSTATUS status;
257 struct schannel_state *state;
259 status = schannel_start(gensec_security);
260 if (!NT_STATUS_IS_OK(status)) {
261 return status;
264 state = (struct schannel_state *)gensec_security->private_data;
265 state->initiator = true;
267 return NT_STATUS_OK;
271 static bool schannel_have_feature(struct gensec_security *gensec_security,
272 uint32_t feature)
274 if (feature & (GENSEC_FEATURE_SIGN |
275 GENSEC_FEATURE_SEAL)) {
276 return true;
278 if (feature & GENSEC_FEATURE_DCE_STYLE) {
279 return true;
281 if (feature & GENSEC_FEATURE_ASYNC_REPLIES) {
282 return true;
284 return false;
288 unseal a packet
290 static NTSTATUS schannel_unseal_packet(struct gensec_security *gensec_security,
291 TALLOC_CTX *mem_ctx,
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),
302 length, sig);
306 check the signature on a packet
308 static NTSTATUS schannel_check_packet(struct gensec_security *gensec_security,
309 TALLOC_CTX *mem_ctx,
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),
320 length, sig);
323 seal a packet
325 static NTSTATUS schannel_seal_packet(struct gensec_security *gensec_security,
326 TALLOC_CTX *mem_ctx,
327 uint8_t *data, size_t length,
328 const uint8_t *whole_pdu, size_t pdu_length,
329 DATA_BLOB *sig)
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,
336 data, length, sig);
340 sign a packet
342 static NTSTATUS schannel_sign_packet(struct gensec_security *gensec_security,
343 TALLOC_CTX *mem_ctx,
344 const uint8_t *data, size_t length,
345 const uint8_t *whole_pdu, size_t pdu_length,
346 DATA_BLOB *sig)
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),
354 length, sig);
357 static const struct gensec_security_ops gensec_schannel_security_ops = {
358 .name = "schannel",
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,
371 .enabled = true,
372 .priority = GENSEC_SCHANNEL
375 _PUBLIC_ NTSTATUS gensec_schannel_init(void)
377 NTSTATUS ret;
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));
382 return ret;
385 return ret;