2 Unix SMB/CIFS implementation.
4 dcerpc schannel operations
6 Copyright (C) Andrew Tridgell 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 wrappers for the schannel_*() functions
28 static NTSTATUS schan_unseal_packet(struct dcerpc_security *dcerpc_security,
30 uint8_t *data, size_t length, DATA_BLOB *sig)
32 struct schannel_state *schannel_state = dcerpc_security->private;
33 return schannel_unseal_packet(schannel_state, mem_ctx, data, length, sig);
36 static NTSTATUS schan_check_packet(struct dcerpc_security *dcerpc_security,
38 const uint8_t *data, size_t length,
41 struct schannel_state *schannel_state = dcerpc_security->private;
42 return schannel_check_packet(schannel_state, data, length, sig);
45 static NTSTATUS schan_seal_packet(struct dcerpc_security *dcerpc_security,
47 uint8_t *data, size_t length,
50 struct schannel_state *schannel_state = dcerpc_security->private;
51 return schannel_seal_packet(schannel_state, mem_ctx, data, length, sig);
54 static NTSTATUS schan_sign_packet(struct dcerpc_security *dcerpc_security,
56 const uint8_t *data, size_t length,
59 struct schannel_state *schannel_state = dcerpc_security->private;
60 return schannel_sign_packet(schannel_state, mem_ctx, data, length, sig);
63 static NTSTATUS schan_session_key(struct dcerpc_security *dcerpc_security,
64 DATA_BLOB *session_key)
66 return NT_STATUS_NOT_IMPLEMENTED;
69 static void schan_security_end(struct dcerpc_security *dcerpc_security)
71 struct schannel_state *schannel_state = dcerpc_security->private;
72 schannel_end(&schannel_state);
77 get a schannel key using a netlogon challenge on a secondary pipe
79 NTSTATUS dcerpc_schannel_key(struct dcerpc_pipe *p,
84 uint8_t new_session_key[8])
87 struct dcerpc_pipe *p2;
88 struct netr_ServerReqChallenge r;
89 struct netr_ServerAuthenticate2 a;
90 struct netr_Credential credentials1, credentials2, credentials3;
92 struct creds_CredentialState creds;
93 const char *workgroup, *workstation;
94 uint32_t negotiate_flags = 0;
96 workstation = username;
100 step 1 - establish a netlogon connection, with no authentication
102 status = dcerpc_secondary_smb(p, &p2,
103 DCERPC_NETLOGON_NAME,
104 DCERPC_NETLOGON_UUID,
105 DCERPC_NETLOGON_VERSION);
109 step 2 - request a netlogon challenge
111 r.in.server_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dcerpc_server_name(p));
112 r.in.computer_name = workstation;
113 r.in.credentials = &credentials1;
114 r.out.credentials = &credentials2;
116 generate_random_buffer(credentials1.data, sizeof(credentials1.data), False);
118 status = dcerpc_netr_ServerReqChallenge(p2, p->mem_ctx, &r);
119 if (!NT_STATUS_IS_OK(status)) {
124 step 3 - authenticate on the netlogon pipe
126 E_md4hash(password, mach_pwd);
127 creds_client_init(&creds, &credentials1, &credentials2, mach_pwd, &credentials3);
129 a.in.server_name = r.in.server_name;
130 a.in.username = talloc_asprintf(p->mem_ctx, "%s$", workstation);
131 a.in.secure_channel_type = chan_type;
132 a.in.computer_name = workstation;
133 a.in.negotiate_flags = &negotiate_flags;
134 a.out.negotiate_flags = &negotiate_flags;
135 a.in.credentials = &credentials3;
136 a.out.credentials = &credentials3;
138 status = dcerpc_netr_ServerAuthenticate2(p2, p->mem_ctx, &a);
139 if (!NT_STATUS_IS_OK(status)) {
143 if (!creds_client_check(&creds, a.out.credentials)) {
144 return NT_STATUS_UNSUCCESSFUL;
148 the schannel session key is now in creds.session_key
150 we no longer need the netlogon pipe open
152 dcerpc_pipe_close(p2);
154 memcpy(new_session_key, creds.session_key, 8);
161 do a schannel style bind on a dcerpc pipe with the given schannel
162 key. The username is usually of the form HOSTNAME$ and the password
163 is the domain trust password
165 NTSTATUS dcerpc_bind_auth_schannel_key(struct dcerpc_pipe *p,
166 const char *uuid, unsigned version,
168 const char *username,
169 const uint8_t session_key[8])
172 uint8_t full_session_key[16];
173 struct schannel_state *schannel_state;
174 const char *workgroup, *workstation;
176 memcpy(full_session_key, session_key, 8);
177 memset(full_session_key+8, 0, 8);
179 workstation = username;
183 perform a bind with security type schannel
185 p->auth_info = talloc(p->mem_ctx, sizeof(*p->auth_info));
187 status = NT_STATUS_NO_MEMORY;
191 p->auth_info->auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
193 if (p->flags & DCERPC_SEAL) {
194 p->auth_info->auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
196 /* note that DCERPC_AUTH_LEVEL_NONE does not make any
197 sense, and would be rejected by the server */
198 p->auth_info->auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
200 p->auth_info->auth_pad_length = 0;
201 p->auth_info->auth_reserved = 0;
202 p->auth_info->auth_context_id = random();
203 p->security_state = NULL;
205 p->auth_info->credentials = data_blob_talloc(p->mem_ctx,
208 strlen(workgroup)+1 +
209 strlen(workstation)+1);
210 if (!p->auth_info->credentials.data) {
211 return NT_STATUS_NO_MEMORY;
214 /* oh, this is ugly! */
215 SIVAL(p->auth_info->credentials.data, 0, 0);
216 SIVAL(p->auth_info->credentials.data, 4, 3);
217 memcpy(p->auth_info->credentials.data+8, workgroup, strlen(workgroup)+1);
218 memcpy(p->auth_info->credentials.data+8+strlen(workgroup)+1,
219 workstation, strlen(workstation)+1);
221 /* send the authenticated bind request */
222 status = dcerpc_bind_byuuid(p, p->mem_ctx, uuid, version);
223 if (!NT_STATUS_IS_OK(status)) {
227 p->security_state = talloc_p(p->mem_ctx, struct dcerpc_security);
228 if (!p->security_state) {
229 status = NT_STATUS_NO_MEMORY;
233 schannel_state = talloc_p(p->mem_ctx, struct schannel_state);
234 if (!schannel_state) {
235 status = NT_STATUS_NO_MEMORY;
239 status = schannel_start(&schannel_state, full_session_key, True);
240 if (!NT_STATUS_IS_OK(status)) {
244 dump_data_pw("session key:\n", schannel_state->session_key, 16);
246 p->security_state->private = schannel_state;
247 p->security_state->unseal_packet = schan_unseal_packet;
248 p->security_state->check_packet = schan_check_packet;
249 p->security_state->seal_packet = schan_seal_packet;
250 p->security_state->sign_packet = schan_sign_packet;
251 p->security_state->session_key = schan_session_key;
252 p->security_state->security_end = schan_security_end;
260 do a schannel style bind on a dcerpc pipe. The username is usually
261 of the form HOSTNAME$ and the password is the domain trust password
263 NTSTATUS dcerpc_bind_auth_schannel(struct dcerpc_pipe *p,
264 const char *uuid, unsigned version,
266 const char *username,
267 const char *password)
270 uint8_t session_key[8];
272 status = dcerpc_schannel_key(p, domain, username, password,
273 lp_server_role() == ROLE_DOMAIN_BDC? SEC_CHAN_BDC:SEC_CHAN_WKSTA,
275 if (!NT_STATUS_IS_OK(status)) {
279 status = dcerpc_bind_auth_schannel_key(p, uuid, version, domain, username, session_key);