1 | /*
|
---|
2 | * Unix SMB/CIFS implementation.
|
---|
3 | * Version 3.0
|
---|
4 | * NTLMSSP Signing routines
|
---|
5 | * Copyright (C) Andrew Bartlett 2003-2005
|
---|
6 | *
|
---|
7 | * This program is free software; you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation; either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This program is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with this program; if not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "includes.h"
|
---|
22 | #include "../auth/ntlmssp/ntlmssp.h"
|
---|
23 | #include "../libcli/auth/libcli_auth.h"
|
---|
24 | #include "../lib/crypto/md5.h"
|
---|
25 | #include "../lib/crypto/hmacmd5.h"
|
---|
26 | #include "../lib/crypto/crc32.h"
|
---|
27 | #include "../auth/ntlmssp/ntlmssp_private.h"
|
---|
28 |
|
---|
29 | #define CLI_SIGN "session key to client-to-server signing key magic constant"
|
---|
30 | #define CLI_SEAL "session key to client-to-server sealing key magic constant"
|
---|
31 | #define SRV_SIGN "session key to server-to-client signing key magic constant"
|
---|
32 | #define SRV_SEAL "session key to server-to-client sealing key magic constant"
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Some notes on the NTLM2 code:
|
---|
36 | *
|
---|
37 | * NTLM2 is a AEAD system. This means that the data encrypted is not
|
---|
38 | * all the data that is signed. In DCE-RPC case, the headers of the
|
---|
39 | * DCE-RPC packets are also signed. This prevents some of the
|
---|
40 | * fun-and-games one might have by changing them.
|
---|
41 | *
|
---|
42 | */
|
---|
43 |
|
---|
44 | static void dump_arc4_state(const char *description,
|
---|
45 | struct arcfour_state *state)
|
---|
46 | {
|
---|
47 | dump_data_pw(description, state->sbox, sizeof(state->sbox));
|
---|
48 | }
|
---|
49 |
|
---|
50 | static void calc_ntlmv2_key(uint8_t subkey[16],
|
---|
51 | DATA_BLOB session_key,
|
---|
52 | const char *constant)
|
---|
53 | {
|
---|
54 | MD5_CTX ctx3;
|
---|
55 | MD5Init(&ctx3);
|
---|
56 | MD5Update(&ctx3, session_key.data, session_key.length);
|
---|
57 | MD5Update(&ctx3, (const uint8_t *)constant, strlen(constant)+1);
|
---|
58 | MD5Final(subkey, &ctx3);
|
---|
59 | }
|
---|
60 |
|
---|
61 | enum ntlmssp_direction {
|
---|
62 | NTLMSSP_SEND,
|
---|
63 | NTLMSSP_RECEIVE
|
---|
64 | };
|
---|
65 |
|
---|
66 | static NTSTATUS ntlmssp_make_packet_signature(struct ntlmssp_state *ntlmssp_state,
|
---|
67 | TALLOC_CTX *sig_mem_ctx,
|
---|
68 | const uint8_t *data, size_t length,
|
---|
69 | const uint8_t *whole_pdu, size_t pdu_length,
|
---|
70 | enum ntlmssp_direction direction,
|
---|
71 | DATA_BLOB *sig, bool encrypt_sig)
|
---|
72 | {
|
---|
73 | if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
|
---|
74 | HMACMD5Context ctx;
|
---|
75 | uint8_t digest[16];
|
---|
76 | uint8_t seq_num[4];
|
---|
77 |
|
---|
78 | *sig = data_blob_talloc(sig_mem_ctx, NULL, NTLMSSP_SIG_SIZE);
|
---|
79 | if (!sig->data) {
|
---|
80 | return NT_STATUS_NO_MEMORY;
|
---|
81 | }
|
---|
82 |
|
---|
83 | switch (direction) {
|
---|
84 | case NTLMSSP_SEND:
|
---|
85 | DEBUG(100,("ntlmssp_make_packet_signature: SEND seq = %u, len = %u, pdu_len = %u\n",
|
---|
86 | ntlmssp_state->crypt->ntlm2.sending.seq_num,
|
---|
87 | (unsigned int)length,
|
---|
88 | (unsigned int)pdu_length));
|
---|
89 |
|
---|
90 | SIVAL(seq_num, 0, ntlmssp_state->crypt->ntlm2.sending.seq_num);
|
---|
91 | ntlmssp_state->crypt->ntlm2.sending.seq_num++;
|
---|
92 | hmac_md5_init_limK_to_64(ntlmssp_state->crypt->ntlm2.sending.sign_key, 16, &ctx);
|
---|
93 | break;
|
---|
94 | case NTLMSSP_RECEIVE:
|
---|
95 |
|
---|
96 | DEBUG(100,("ntlmssp_make_packet_signature: RECV seq = %u, len = %u, pdu_len = %u\n",
|
---|
97 | ntlmssp_state->crypt->ntlm2.receiving.seq_num,
|
---|
98 | (unsigned int)length,
|
---|
99 | (unsigned int)pdu_length));
|
---|
100 |
|
---|
101 | SIVAL(seq_num, 0, ntlmssp_state->crypt->ntlm2.receiving.seq_num);
|
---|
102 | ntlmssp_state->crypt->ntlm2.receiving.seq_num++;
|
---|
103 | hmac_md5_init_limK_to_64(ntlmssp_state->crypt->ntlm2.receiving.sign_key, 16, &ctx);
|
---|
104 | break;
|
---|
105 | }
|
---|
106 |
|
---|
107 | dump_data_pw("pdu data ", whole_pdu, pdu_length);
|
---|
108 |
|
---|
109 | hmac_md5_update(seq_num, sizeof(seq_num), &ctx);
|
---|
110 | hmac_md5_update(whole_pdu, pdu_length, &ctx);
|
---|
111 | hmac_md5_final(digest, &ctx);
|
---|
112 |
|
---|
113 | if (encrypt_sig && (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)) {
|
---|
114 | switch (direction) {
|
---|
115 | case NTLMSSP_SEND:
|
---|
116 | arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm2.sending.seal_state,
|
---|
117 | digest, 8);
|
---|
118 | break;
|
---|
119 | case NTLMSSP_RECEIVE:
|
---|
120 | arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm2.receiving.seal_state,
|
---|
121 | digest, 8);
|
---|
122 | break;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | SIVAL(sig->data, 0, NTLMSSP_SIGN_VERSION);
|
---|
127 | memcpy(sig->data + 4, digest, 8);
|
---|
128 | memcpy(sig->data + 12, seq_num, 4);
|
---|
129 |
|
---|
130 | dump_data_pw("ntlmssp v2 sig ", sig->data, sig->length);
|
---|
131 |
|
---|
132 | } else {
|
---|
133 | NTSTATUS status;
|
---|
134 | uint32_t crc;
|
---|
135 |
|
---|
136 | crc = crc32_calc_buffer(data, length);
|
---|
137 |
|
---|
138 | status = msrpc_gen(sig_mem_ctx,
|
---|
139 | sig, "dddd",
|
---|
140 | NTLMSSP_SIGN_VERSION, 0, crc,
|
---|
141 | ntlmssp_state->crypt->ntlm.seq_num);
|
---|
142 | if (!NT_STATUS_IS_OK(status)) {
|
---|
143 | return status;
|
---|
144 | }
|
---|
145 |
|
---|
146 | ntlmssp_state->crypt->ntlm.seq_num++;
|
---|
147 |
|
---|
148 | dump_arc4_state("ntlmssp hash: \n",
|
---|
149 | &ntlmssp_state->crypt->ntlm.seal_state);
|
---|
150 | arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm.seal_state,
|
---|
151 | sig->data+4, sig->length-4);
|
---|
152 | }
|
---|
153 | return NT_STATUS_OK;
|
---|
154 | }
|
---|
155 |
|
---|
156 | NTSTATUS ntlmssp_sign_packet(struct ntlmssp_state *ntlmssp_state,
|
---|
157 | TALLOC_CTX *sig_mem_ctx,
|
---|
158 | const uint8_t *data, size_t length,
|
---|
159 | const uint8_t *whole_pdu, size_t pdu_length,
|
---|
160 | DATA_BLOB *sig)
|
---|
161 | {
|
---|
162 | NTSTATUS nt_status;
|
---|
163 |
|
---|
164 | if (!(ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
|
---|
165 | DEBUG(3, ("NTLMSSP Signing not negotiated - cannot sign packet!\n"));
|
---|
166 | return NT_STATUS_INVALID_PARAMETER;
|
---|
167 | }
|
---|
168 |
|
---|
169 | if (!ntlmssp_state->session_key.length) {
|
---|
170 | DEBUG(3, ("NO session key, cannot check sign packet\n"));
|
---|
171 | return NT_STATUS_NO_USER_SESSION_KEY;
|
---|
172 | }
|
---|
173 |
|
---|
174 | nt_status = ntlmssp_make_packet_signature(ntlmssp_state,
|
---|
175 | sig_mem_ctx,
|
---|
176 | data, length,
|
---|
177 | whole_pdu, pdu_length,
|
---|
178 | NTLMSSP_SEND, sig, true);
|
---|
179 |
|
---|
180 | return nt_status;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Check the signature of an incoming packet
|
---|
185 | * @note caller *must* check that the signature is the size it expects
|
---|
186 | *
|
---|
187 | */
|
---|
188 |
|
---|
189 | NTSTATUS ntlmssp_check_packet(struct ntlmssp_state *ntlmssp_state,
|
---|
190 | const uint8_t *data, size_t length,
|
---|
191 | const uint8_t *whole_pdu, size_t pdu_length,
|
---|
192 | const DATA_BLOB *sig)
|
---|
193 | {
|
---|
194 | DATA_BLOB local_sig;
|
---|
195 | NTSTATUS nt_status;
|
---|
196 | TALLOC_CTX *tmp_ctx;
|
---|
197 |
|
---|
198 | if (!ntlmssp_state->session_key.length) {
|
---|
199 | DEBUG(3, ("NO session key, cannot check packet signature\n"));
|
---|
200 | return NT_STATUS_NO_USER_SESSION_KEY;
|
---|
201 | }
|
---|
202 |
|
---|
203 | if (sig->length < 8) {
|
---|
204 | DEBUG(0, ("NTLMSSP packet check failed due to short signature (%lu bytes)!\n",
|
---|
205 | (unsigned long)sig->length));
|
---|
206 | }
|
---|
207 |
|
---|
208 | tmp_ctx = talloc_new(ntlmssp_state);
|
---|
209 | if (!tmp_ctx) {
|
---|
210 | return NT_STATUS_NO_MEMORY;
|
---|
211 | }
|
---|
212 |
|
---|
213 | nt_status = ntlmssp_make_packet_signature(ntlmssp_state,
|
---|
214 | tmp_ctx,
|
---|
215 | data, length,
|
---|
216 | whole_pdu, pdu_length,
|
---|
217 | NTLMSSP_RECEIVE,
|
---|
218 | &local_sig, true);
|
---|
219 |
|
---|
220 | if (!NT_STATUS_IS_OK(nt_status)) {
|
---|
221 | DEBUG(0,("NTLMSSP packet sig creation failed with %s\n",
|
---|
222 | nt_errstr(nt_status)));
|
---|
223 | talloc_free(tmp_ctx);
|
---|
224 | return nt_status;
|
---|
225 | }
|
---|
226 |
|
---|
227 | if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
|
---|
228 | if (local_sig.length != sig->length ||
|
---|
229 | memcmp(local_sig.data, sig->data, sig->length) != 0) {
|
---|
230 | DEBUG(5, ("BAD SIG NTLM2: wanted signature of\n"));
|
---|
231 | dump_data(5, local_sig.data, local_sig.length);
|
---|
232 |
|
---|
233 | DEBUG(5, ("BAD SIG: got signature of\n"));
|
---|
234 | dump_data(5, sig->data, sig->length);
|
---|
235 |
|
---|
236 | DEBUG(0, ("NTLMSSP NTLM2 packet check failed due to invalid signature!\n"));
|
---|
237 | talloc_free(tmp_ctx);
|
---|
238 | return NT_STATUS_ACCESS_DENIED;
|
---|
239 | }
|
---|
240 | } else {
|
---|
241 | if (local_sig.length != sig->length ||
|
---|
242 | memcmp(local_sig.data + 8, sig->data + 8, sig->length - 8) != 0) {
|
---|
243 | DEBUG(5, ("BAD SIG NTLM1: wanted signature of\n"));
|
---|
244 | dump_data(5, local_sig.data, local_sig.length);
|
---|
245 |
|
---|
246 | DEBUG(5, ("BAD SIG: got signature of\n"));
|
---|
247 | dump_data(5, sig->data, sig->length);
|
---|
248 |
|
---|
249 | DEBUG(0, ("NTLMSSP NTLM1 packet check failed due to invalid signature!\n"));
|
---|
250 | talloc_free(tmp_ctx);
|
---|
251 | return NT_STATUS_ACCESS_DENIED;
|
---|
252 | }
|
---|
253 | }
|
---|
254 | dump_data_pw("checked ntlmssp signature\n", sig->data, sig->length);
|
---|
255 | DEBUG(10,("ntlmssp_check_packet: NTLMSSP signature OK !\n"));
|
---|
256 |
|
---|
257 | talloc_free(tmp_ctx);
|
---|
258 | return NT_STATUS_OK;
|
---|
259 | }
|
---|
260 |
|
---|
261 | /**
|
---|
262 | * Seal data with the NTLMSSP algorithm
|
---|
263 | *
|
---|
264 | */
|
---|
265 |
|
---|
266 | NTSTATUS ntlmssp_seal_packet(struct ntlmssp_state *ntlmssp_state,
|
---|
267 | TALLOC_CTX *sig_mem_ctx,
|
---|
268 | uint8_t *data, size_t length,
|
---|
269 | const uint8_t *whole_pdu, size_t pdu_length,
|
---|
270 | DATA_BLOB *sig)
|
---|
271 | {
|
---|
272 | if (!(ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL)) {
|
---|
273 | DEBUG(3, ("NTLMSSP Sealing not negotiated - cannot seal packet!\n"));
|
---|
274 | return NT_STATUS_INVALID_PARAMETER;
|
---|
275 | }
|
---|
276 |
|
---|
277 | if (!(ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
|
---|
278 | DEBUG(3, ("NTLMSSP Sealing not negotiated - cannot seal packet!\n"));
|
---|
279 | return NT_STATUS_INVALID_PARAMETER;
|
---|
280 | }
|
---|
281 |
|
---|
282 | if (!ntlmssp_state->session_key.length) {
|
---|
283 | DEBUG(3, ("NO session key, cannot seal packet\n"));
|
---|
284 | return NT_STATUS_NO_USER_SESSION_KEY;
|
---|
285 | }
|
---|
286 |
|
---|
287 | DEBUG(10,("ntlmssp_seal_data: seal\n"));
|
---|
288 | dump_data_pw("ntlmssp clear data\n", data, length);
|
---|
289 | if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
|
---|
290 | NTSTATUS nt_status;
|
---|
291 | /*
|
---|
292 | * The order of these two operations matters - we
|
---|
293 | * must first seal the packet, then seal the
|
---|
294 | * sequence number - this is because the
|
---|
295 | * send_seal_hash is not constant, but is is rather
|
---|
296 | * updated with each iteration
|
---|
297 | */
|
---|
298 | nt_status = ntlmssp_make_packet_signature(ntlmssp_state,
|
---|
299 | sig_mem_ctx,
|
---|
300 | data, length,
|
---|
301 | whole_pdu, pdu_length,
|
---|
302 | NTLMSSP_SEND,
|
---|
303 | sig, false);
|
---|
304 | if (!NT_STATUS_IS_OK(nt_status)) {
|
---|
305 | return nt_status;
|
---|
306 | }
|
---|
307 |
|
---|
308 | arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm2.sending.seal_state,
|
---|
309 | data, length);
|
---|
310 | if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
|
---|
311 | arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm2.sending.seal_state,
|
---|
312 | sig->data+4, 8);
|
---|
313 | }
|
---|
314 | } else {
|
---|
315 | NTSTATUS status;
|
---|
316 | uint32_t crc;
|
---|
317 |
|
---|
318 | crc = crc32_calc_buffer(data, length);
|
---|
319 |
|
---|
320 | status = msrpc_gen(sig_mem_ctx,
|
---|
321 | sig, "dddd",
|
---|
322 | NTLMSSP_SIGN_VERSION, 0, crc,
|
---|
323 | ntlmssp_state->crypt->ntlm.seq_num);
|
---|
324 | if (!NT_STATUS_IS_OK(status)) {
|
---|
325 | return status;
|
---|
326 | }
|
---|
327 |
|
---|
328 | /*
|
---|
329 | * The order of these two operations matters - we
|
---|
330 | * must first seal the packet, then seal the
|
---|
331 | * sequence number - this is because the ntlmv1_arc4_state
|
---|
332 | * is not constant, but is is rather updated with
|
---|
333 | * each iteration
|
---|
334 | */
|
---|
335 |
|
---|
336 | dump_arc4_state("ntlmv1 arc4 state:\n",
|
---|
337 | &ntlmssp_state->crypt->ntlm.seal_state);
|
---|
338 | arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm.seal_state,
|
---|
339 | data, length);
|
---|
340 |
|
---|
341 | dump_arc4_state("ntlmv1 arc4 state:\n",
|
---|
342 | &ntlmssp_state->crypt->ntlm.seal_state);
|
---|
343 |
|
---|
344 | arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm.seal_state,
|
---|
345 | sig->data+4, sig->length-4);
|
---|
346 |
|
---|
347 | ntlmssp_state->crypt->ntlm.seq_num++;
|
---|
348 | }
|
---|
349 | dump_data_pw("ntlmssp signature\n", sig->data, sig->length);
|
---|
350 | dump_data_pw("ntlmssp sealed data\n", data, length);
|
---|
351 |
|
---|
352 | return NT_STATUS_OK;
|
---|
353 | }
|
---|
354 |
|
---|
355 | /**
|
---|
356 | * Unseal data with the NTLMSSP algorithm
|
---|
357 | *
|
---|
358 | */
|
---|
359 |
|
---|
360 | NTSTATUS ntlmssp_unseal_packet(struct ntlmssp_state *ntlmssp_state,
|
---|
361 | uint8_t *data, size_t length,
|
---|
362 | const uint8_t *whole_pdu, size_t pdu_length,
|
---|
363 | const DATA_BLOB *sig)
|
---|
364 | {
|
---|
365 | NTSTATUS status;
|
---|
366 | if (!ntlmssp_state->session_key.length) {
|
---|
367 | DEBUG(3, ("NO session key, cannot unseal packet\n"));
|
---|
368 | return NT_STATUS_NO_USER_SESSION_KEY;
|
---|
369 | }
|
---|
370 |
|
---|
371 | DEBUG(10,("ntlmssp_unseal_packet: seal\n"));
|
---|
372 | dump_data_pw("ntlmssp sealed data\n", data, length);
|
---|
373 |
|
---|
374 | if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
|
---|
375 | /* First unseal the data. */
|
---|
376 | arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm2.receiving.seal_state,
|
---|
377 | data, length);
|
---|
378 | dump_data_pw("ntlmv2 clear data\n", data, length);
|
---|
379 | } else {
|
---|
380 | arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm.seal_state,
|
---|
381 | data, length);
|
---|
382 | dump_data_pw("ntlmv1 clear data\n", data, length);
|
---|
383 | }
|
---|
384 | status = ntlmssp_check_packet(ntlmssp_state,
|
---|
385 | data, length,
|
---|
386 | whole_pdu, pdu_length,
|
---|
387 | sig);
|
---|
388 |
|
---|
389 | if (!NT_STATUS_IS_OK(status)) {
|
---|
390 | DEBUG(1,("NTLMSSP packet check for unseal failed due to invalid signature on %llu bytes of input:\n",
|
---|
391 | (unsigned long long)length));
|
---|
392 | }
|
---|
393 | return status;
|
---|
394 | }
|
---|
395 |
|
---|
396 | NTSTATUS ntlmssp_wrap(struct ntlmssp_state *ntlmssp_state,
|
---|
397 | TALLOC_CTX *out_mem_ctx,
|
---|
398 | const DATA_BLOB *in,
|
---|
399 | DATA_BLOB *out)
|
---|
400 | {
|
---|
401 | NTSTATUS nt_status;
|
---|
402 | DATA_BLOB sig;
|
---|
403 |
|
---|
404 | if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL) {
|
---|
405 | if (in->length + NTLMSSP_SIG_SIZE < in->length) {
|
---|
406 | return NT_STATUS_INVALID_PARAMETER;
|
---|
407 | }
|
---|
408 |
|
---|
409 | *out = data_blob_talloc(out_mem_ctx, NULL, in->length + NTLMSSP_SIG_SIZE);
|
---|
410 | if (!out->data) {
|
---|
411 | return NT_STATUS_NO_MEMORY;
|
---|
412 | }
|
---|
413 | memcpy(out->data + NTLMSSP_SIG_SIZE, in->data, in->length);
|
---|
414 |
|
---|
415 | nt_status = ntlmssp_seal_packet(ntlmssp_state, out_mem_ctx,
|
---|
416 | out->data + NTLMSSP_SIG_SIZE,
|
---|
417 | out->length - NTLMSSP_SIG_SIZE,
|
---|
418 | out->data + NTLMSSP_SIG_SIZE,
|
---|
419 | out->length - NTLMSSP_SIG_SIZE,
|
---|
420 | &sig);
|
---|
421 |
|
---|
422 | if (NT_STATUS_IS_OK(nt_status)) {
|
---|
423 | memcpy(out->data, sig.data, NTLMSSP_SIG_SIZE);
|
---|
424 | talloc_free(sig.data);
|
---|
425 | }
|
---|
426 | return nt_status;
|
---|
427 |
|
---|
428 | } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN) {
|
---|
429 | if (in->length + NTLMSSP_SIG_SIZE < in->length) {
|
---|
430 | return NT_STATUS_INVALID_PARAMETER;
|
---|
431 | }
|
---|
432 |
|
---|
433 | *out = data_blob_talloc(out_mem_ctx, NULL, in->length + NTLMSSP_SIG_SIZE);
|
---|
434 | if (!out->data) {
|
---|
435 | return NT_STATUS_NO_MEMORY;
|
---|
436 | }
|
---|
437 | memcpy(out->data + NTLMSSP_SIG_SIZE, in->data, in->length);
|
---|
438 |
|
---|
439 | nt_status = ntlmssp_sign_packet(ntlmssp_state, out_mem_ctx,
|
---|
440 | out->data + NTLMSSP_SIG_SIZE,
|
---|
441 | out->length - NTLMSSP_SIG_SIZE,
|
---|
442 | out->data + NTLMSSP_SIG_SIZE,
|
---|
443 | out->length - NTLMSSP_SIG_SIZE,
|
---|
444 | &sig);
|
---|
445 |
|
---|
446 | if (NT_STATUS_IS_OK(nt_status)) {
|
---|
447 | memcpy(out->data, sig.data, NTLMSSP_SIG_SIZE);
|
---|
448 | talloc_free(sig.data);
|
---|
449 | }
|
---|
450 | return nt_status;
|
---|
451 | } else {
|
---|
452 | *out = data_blob_talloc(out_mem_ctx, in->data, in->length);
|
---|
453 | if (!out->data) {
|
---|
454 | return NT_STATUS_NO_MEMORY;
|
---|
455 | }
|
---|
456 | return NT_STATUS_OK;
|
---|
457 | }
|
---|
458 | }
|
---|
459 |
|
---|
460 | NTSTATUS ntlmssp_unwrap(struct ntlmssp_state *ntlmssp_state,
|
---|
461 | TALLOC_CTX *out_mem_ctx,
|
---|
462 | const DATA_BLOB *in,
|
---|
463 | DATA_BLOB *out)
|
---|
464 | {
|
---|
465 | DATA_BLOB sig;
|
---|
466 |
|
---|
467 | if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL) {
|
---|
468 | if (in->length < NTLMSSP_SIG_SIZE) {
|
---|
469 | return NT_STATUS_INVALID_PARAMETER;
|
---|
470 | }
|
---|
471 | sig.data = in->data;
|
---|
472 | sig.length = NTLMSSP_SIG_SIZE;
|
---|
473 |
|
---|
474 | *out = data_blob_talloc(out_mem_ctx, in->data + NTLMSSP_SIG_SIZE, in->length - NTLMSSP_SIG_SIZE);
|
---|
475 |
|
---|
476 | return ntlmssp_unseal_packet(ntlmssp_state,
|
---|
477 | out->data, out->length,
|
---|
478 | out->data, out->length,
|
---|
479 | &sig);
|
---|
480 |
|
---|
481 | } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN) {
|
---|
482 | if (in->length < NTLMSSP_SIG_SIZE) {
|
---|
483 | return NT_STATUS_INVALID_PARAMETER;
|
---|
484 | }
|
---|
485 | sig.data = in->data;
|
---|
486 | sig.length = NTLMSSP_SIG_SIZE;
|
---|
487 |
|
---|
488 | *out = data_blob_talloc(out_mem_ctx, in->data + NTLMSSP_SIG_SIZE, in->length - NTLMSSP_SIG_SIZE);
|
---|
489 |
|
---|
490 | return ntlmssp_check_packet(ntlmssp_state,
|
---|
491 | out->data, out->length,
|
---|
492 | out->data, out->length,
|
---|
493 | &sig);
|
---|
494 | } else {
|
---|
495 | *out = data_blob_talloc(out_mem_ctx, in->data, in->length);
|
---|
496 | if (!out->data) {
|
---|
497 | return NT_STATUS_NO_MEMORY;
|
---|
498 | }
|
---|
499 | return NT_STATUS_OK;
|
---|
500 | }
|
---|
501 | }
|
---|
502 |
|
---|
503 | /**
|
---|
504 | Initialise the state for NTLMSSP signing.
|
---|
505 | */
|
---|
506 | NTSTATUS ntlmssp_sign_reset(struct ntlmssp_state *ntlmssp_state,
|
---|
507 | bool reset_seqnums)
|
---|
508 | {
|
---|
509 | DEBUG(3, ("NTLMSSP Sign/Seal - Initialising with flags:\n"));
|
---|
510 | debug_ntlmssp_flags(ntlmssp_state->neg_flags);
|
---|
511 |
|
---|
512 | if (ntlmssp_state->crypt == NULL) {
|
---|
513 | return NT_STATUS_INVALID_PARAMETER_MIX;
|
---|
514 | }
|
---|
515 |
|
---|
516 | if (ntlmssp_state->force_wrap_seal &&
|
---|
517 | (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN))
|
---|
518 | {
|
---|
519 | /*
|
---|
520 | * We need to handle NTLMSSP_NEGOTIATE_SIGN as
|
---|
521 | * NTLMSSP_NEGOTIATE_SEAL if GENSEC_FEATURE_LDAP_STYLE
|
---|
522 | * is requested.
|
---|
523 | *
|
---|
524 | * The negotiation of flags (and authentication)
|
---|
525 | * is completed when ntlmssp_sign_init() is called
|
---|
526 | * so we can safely pretent NTLMSSP_NEGOTIATE_SEAL
|
---|
527 | * was negotiated.
|
---|
528 | */
|
---|
529 | ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
|
---|
530 | }
|
---|
531 |
|
---|
532 | if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
|
---|
533 | DATA_BLOB weak_session_key = ntlmssp_state->session_key;
|
---|
534 | const char *send_sign_const;
|
---|
535 | const char *send_seal_const;
|
---|
536 | const char *recv_sign_const;
|
---|
537 | const char *recv_seal_const;
|
---|
538 | uint8_t send_seal_key[16];
|
---|
539 | DATA_BLOB send_seal_blob = data_blob_const(send_seal_key,
|
---|
540 | sizeof(send_seal_key));
|
---|
541 | uint8_t recv_seal_key[16];
|
---|
542 | DATA_BLOB recv_seal_blob = data_blob_const(recv_seal_key,
|
---|
543 | sizeof(recv_seal_key));
|
---|
544 |
|
---|
545 | switch (ntlmssp_state->role) {
|
---|
546 | case NTLMSSP_CLIENT:
|
---|
547 | send_sign_const = CLI_SIGN;
|
---|
548 | send_seal_const = CLI_SEAL;
|
---|
549 | recv_sign_const = SRV_SIGN;
|
---|
550 | recv_seal_const = SRV_SEAL;
|
---|
551 | break;
|
---|
552 | case NTLMSSP_SERVER:
|
---|
553 | send_sign_const = SRV_SIGN;
|
---|
554 | send_seal_const = SRV_SEAL;
|
---|
555 | recv_sign_const = CLI_SIGN;
|
---|
556 | recv_seal_const = CLI_SEAL;
|
---|
557 | break;
|
---|
558 | default:
|
---|
559 | return NT_STATUS_INTERNAL_ERROR;
|
---|
560 | }
|
---|
561 |
|
---|
562 | /*
|
---|
563 | * Weaken NTLMSSP keys to cope with down-level
|
---|
564 | * clients, servers and export restrictions.
|
---|
565 | *
|
---|
566 | * We probably should have some parameters to
|
---|
567 | * control this, once we get NTLM2 working.
|
---|
568 | *
|
---|
569 | * Key weakening was not performed on the master key
|
---|
570 | * for NTLM2, but must be done on the encryption subkeys only.
|
---|
571 | */
|
---|
572 |
|
---|
573 | if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_128) {
|
---|
574 | /* nothing to do */
|
---|
575 | } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
|
---|
576 | weak_session_key.length = 7;
|
---|
577 | } else { /* forty bits */
|
---|
578 | weak_session_key.length = 5;
|
---|
579 | }
|
---|
580 |
|
---|
581 | dump_data_pw("NTLMSSP weakend master key:\n",
|
---|
582 | weak_session_key.data,
|
---|
583 | weak_session_key.length);
|
---|
584 |
|
---|
585 | /* SEND: sign key */
|
---|
586 | calc_ntlmv2_key(ntlmssp_state->crypt->ntlm2.sending.sign_key,
|
---|
587 | ntlmssp_state->session_key, send_sign_const);
|
---|
588 | dump_data_pw("NTLMSSP send sign key:\n",
|
---|
589 | ntlmssp_state->crypt->ntlm2.sending.sign_key, 16);
|
---|
590 |
|
---|
591 | /* SEND: seal ARCFOUR pad */
|
---|
592 | calc_ntlmv2_key(send_seal_key,
|
---|
593 | weak_session_key, send_seal_const);
|
---|
594 | dump_data_pw("NTLMSSP send seal key:\n", send_seal_key, 16);
|
---|
595 |
|
---|
596 | arcfour_init(&ntlmssp_state->crypt->ntlm2.sending.seal_state,
|
---|
597 | &send_seal_blob);
|
---|
598 |
|
---|
599 | dump_arc4_state("NTLMSSP send seal arc4 state:\n",
|
---|
600 | &ntlmssp_state->crypt->ntlm2.sending.seal_state);
|
---|
601 |
|
---|
602 | /* SEND: seq num */
|
---|
603 | if (reset_seqnums) {
|
---|
604 | ntlmssp_state->crypt->ntlm2.sending.seq_num = 0;
|
---|
605 | }
|
---|
606 |
|
---|
607 | /* RECV: sign key */
|
---|
608 | calc_ntlmv2_key(ntlmssp_state->crypt->ntlm2.receiving.sign_key,
|
---|
609 | ntlmssp_state->session_key, recv_sign_const);
|
---|
610 | dump_data_pw("NTLMSSP recv sign key:\n",
|
---|
611 | ntlmssp_state->crypt->ntlm2.receiving.sign_key, 16);
|
---|
612 |
|
---|
613 | /* RECV: seal ARCFOUR pad */
|
---|
614 | calc_ntlmv2_key(recv_seal_key,
|
---|
615 | weak_session_key, recv_seal_const);
|
---|
616 | dump_data_pw("NTLMSSP recv seal key:\n", recv_seal_key, 16);
|
---|
617 |
|
---|
618 | arcfour_init(&ntlmssp_state->crypt->ntlm2.receiving.seal_state,
|
---|
619 | &recv_seal_blob);
|
---|
620 |
|
---|
621 | dump_arc4_state("NTLMSSP recv seal arc4 state:\n",
|
---|
622 | &ntlmssp_state->crypt->ntlm2.receiving.seal_state);
|
---|
623 |
|
---|
624 | /* RECV: seq num */
|
---|
625 | if (reset_seqnums) {
|
---|
626 | ntlmssp_state->crypt->ntlm2.receiving.seq_num = 0;
|
---|
627 | }
|
---|
628 | } else {
|
---|
629 | uint8_t weak_session_key[8];
|
---|
630 | DATA_BLOB seal_session_key = ntlmssp_state->session_key;
|
---|
631 | bool do_weak = false;
|
---|
632 |
|
---|
633 | DEBUG(5, ("NTLMSSP Sign/Seal - using NTLM1\n"));
|
---|
634 |
|
---|
635 | /*
|
---|
636 | * Key weakening not performed on the master key for NTLM2
|
---|
637 | * and does not occour for NTLM1. Therefore we only need
|
---|
638 | * to do this for the LM_KEY.
|
---|
639 | */
|
---|
640 | if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
|
---|
641 | do_weak = true;
|
---|
642 | }
|
---|
643 |
|
---|
644 | /*
|
---|
645 | * Nothing to weaken.
|
---|
646 | * We certainly don't want to 'extend' the length...
|
---|
647 | */
|
---|
648 | if (seal_session_key.length < 16) {
|
---|
649 | /* TODO: is this really correct? */
|
---|
650 | do_weak = false;
|
---|
651 | }
|
---|
652 |
|
---|
653 | if (do_weak) {
|
---|
654 | memcpy(weak_session_key, seal_session_key.data, 8);
|
---|
655 | seal_session_key = data_blob_const(weak_session_key, 8);
|
---|
656 |
|
---|
657 | /*
|
---|
658 | * LM key doesn't support 128 bit crypto, so this is
|
---|
659 | * the best we can do. If you negotiate 128 bit, but
|
---|
660 | * not 56, you end up with 40 bit...
|
---|
661 | */
|
---|
662 | if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
|
---|
663 | weak_session_key[7] = 0xa0;
|
---|
664 | } else { /* forty bits */
|
---|
665 | weak_session_key[5] = 0xe5;
|
---|
666 | weak_session_key[6] = 0x38;
|
---|
667 | weak_session_key[7] = 0xb0;
|
---|
668 | }
|
---|
669 | }
|
---|
670 |
|
---|
671 | arcfour_init(&ntlmssp_state->crypt->ntlm.seal_state,
|
---|
672 | &seal_session_key);
|
---|
673 |
|
---|
674 | dump_arc4_state("NTLMv1 arc4 state:\n",
|
---|
675 | &ntlmssp_state->crypt->ntlm.seal_state);
|
---|
676 |
|
---|
677 | if (reset_seqnums) {
|
---|
678 | ntlmssp_state->crypt->ntlm.seq_num = 0;
|
---|
679 | }
|
---|
680 | }
|
---|
681 |
|
---|
682 | return NT_STATUS_OK;
|
---|
683 | }
|
---|
684 |
|
---|
685 | NTSTATUS ntlmssp_sign_init(struct ntlmssp_state *ntlmssp_state)
|
---|
686 | {
|
---|
687 | if (ntlmssp_state->session_key.length < 8) {
|
---|
688 | DEBUG(3, ("NO session key, cannot intialise signing\n"));
|
---|
689 | return NT_STATUS_NO_USER_SESSION_KEY;
|
---|
690 | }
|
---|
691 |
|
---|
692 | ntlmssp_state->crypt = talloc_zero(ntlmssp_state,
|
---|
693 | union ntlmssp_crypt_state);
|
---|
694 | if (ntlmssp_state->crypt == NULL) {
|
---|
695 | return NT_STATUS_NO_MEMORY;
|
---|
696 | }
|
---|
697 |
|
---|
698 | return ntlmssp_sign_reset(ntlmssp_state, true);
|
---|
699 | }
|
---|