Fix more entries with back slashes that need to use literal formatting
[Samba/bjacke.git] / source3 / libsmb / ntlmssp_sign.c
blob8413c8066b0a00849d05b796baba7317c972a4ab
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.
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.
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/>.
21 #include "includes.h"
23 #define CLI_SIGN "session key to client-to-server signing key magic constant"
24 #define CLI_SEAL "session key to client-to-server sealing key magic constant"
25 #define SRV_SIGN "session key to server-to-client signing key magic constant"
26 #define SRV_SEAL "session key to server-to-client sealing key magic constant"
28 /**
29 * Some notes on then NTLM2 code:
31 * NTLM2 is a AEAD system. This means that the data encrypted is not
32 * all the data that is signed. In DCE-RPC case, the headers of the
33 * DCE-RPC packets are also signed. This prevents some of the
34 * fun-and-games one might have by changing them.
38 static void calc_ntlmv2_key(unsigned char subkey[16],
39 DATA_BLOB session_key,
40 const char *constant)
42 struct MD5Context ctx3;
43 MD5Init(&ctx3);
44 MD5Update(&ctx3, session_key.data, session_key.length);
45 MD5Update(&ctx3, (const unsigned char *)constant, strlen(constant)+1);
46 MD5Final(subkey, &ctx3);
49 enum ntlmssp_direction {
50 NTLMSSP_SEND,
51 NTLMSSP_RECEIVE
54 static NTSTATUS ntlmssp_make_packet_signature(NTLMSSP_STATE *ntlmssp_state,
55 const uchar *data, size_t length,
56 const uchar *whole_pdu, size_t pdu_length,
57 enum ntlmssp_direction direction,
58 DATA_BLOB *sig,
59 bool encrypt_sig)
61 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
62 HMACMD5Context ctx;
63 uchar seq_num[4];
64 uchar digest[16];
66 *sig = data_blob(NULL, NTLMSSP_SIG_SIZE);
67 if (!sig->data) {
68 return NT_STATUS_NO_MEMORY;
71 switch (direction) {
72 case NTLMSSP_SEND:
73 DEBUG(100,("ntlmssp_make_packet_signature: SEND seq = %u, len = %u, pdu_len = %u\n",
74 ntlmssp_state->ntlm2_send_seq_num,
75 (unsigned int)length,
76 (unsigned int)pdu_length));
78 SIVAL(seq_num, 0, ntlmssp_state->ntlm2_send_seq_num);
79 ntlmssp_state->ntlm2_send_seq_num++;
80 hmac_md5_init_limK_to_64(ntlmssp_state->send_sign_key, 16, &ctx);
81 break;
82 case NTLMSSP_RECEIVE:
84 DEBUG(100,("ntlmssp_make_packet_signature: RECV seq = %u, len = %u, pdu_len = %u\n",
85 ntlmssp_state->ntlm2_recv_seq_num,
86 (unsigned int)length,
87 (unsigned int)pdu_length));
89 SIVAL(seq_num, 0, ntlmssp_state->ntlm2_recv_seq_num);
90 ntlmssp_state->ntlm2_recv_seq_num++;
91 hmac_md5_init_limK_to_64(ntlmssp_state->recv_sign_key, 16, &ctx);
92 break;
95 dump_data_pw("pdu data ", whole_pdu, pdu_length);
97 hmac_md5_update(seq_num, 4, &ctx);
98 hmac_md5_update(whole_pdu, pdu_length, &ctx);
99 hmac_md5_final(digest, &ctx);
101 if (encrypt_sig && (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)) {
102 switch (direction) {
103 case NTLMSSP_SEND:
104 smb_arc4_crypt(ntlmssp_state->send_seal_arc4_state, digest, 8);
105 break;
106 case NTLMSSP_RECEIVE:
107 smb_arc4_crypt(ntlmssp_state->recv_seal_arc4_state, digest, 8);
108 break;
112 SIVAL(sig->data, 0, NTLMSSP_SIGN_VERSION);
113 memcpy(sig->data + 4, digest, 8);
114 memcpy(sig->data + 12, seq_num, 4);
116 dump_data_pw("ntlmssp v2 sig ", sig->data, sig->length);
118 } else {
119 uint32 crc;
120 crc = crc32_calc_buffer((const char *)data, length);
121 if (!msrpc_gen(sig, "dddd", NTLMSSP_SIGN_VERSION, 0, crc, ntlmssp_state->ntlmv1_seq_num)) {
122 return NT_STATUS_NO_MEMORY;
125 ntlmssp_state->ntlmv1_seq_num++;
127 dump_data_pw("ntlmssp hash:\n", ntlmssp_state->ntlmv1_arc4_state,
128 sizeof(ntlmssp_state->ntlmv1_arc4_state));
129 smb_arc4_crypt(ntlmssp_state->ntlmv1_arc4_state, sig->data+4, sig->length-4);
131 return NT_STATUS_OK;
134 NTSTATUS ntlmssp_sign_packet(NTLMSSP_STATE *ntlmssp_state,
135 const uchar *data, size_t length,
136 const uchar *whole_pdu, size_t pdu_length,
137 DATA_BLOB *sig)
139 NTSTATUS nt_status;
141 if (!(ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
142 DEBUG(3, ("NTLMSSP Signing not negotiated - cannot sign packet!\n"));
143 return NT_STATUS_INVALID_PARAMETER;
146 if (!ntlmssp_state->session_key.length) {
147 DEBUG(3, ("NO session key, cannot check sign packet\n"));
148 return NT_STATUS_NO_USER_SESSION_KEY;
151 nt_status = ntlmssp_make_packet_signature(ntlmssp_state,
152 data, length,
153 whole_pdu, pdu_length,
154 NTLMSSP_SEND, sig, True);
156 return nt_status;
160 * Check the signature of an incoming packet
161 * @note caller *must* check that the signature is the size it expects
165 NTSTATUS ntlmssp_check_packet(NTLMSSP_STATE *ntlmssp_state,
166 const uchar *data, size_t length,
167 const uchar *whole_pdu, size_t pdu_length,
168 const DATA_BLOB *sig)
170 DATA_BLOB local_sig;
171 NTSTATUS nt_status;
173 if (!ntlmssp_state->session_key.length) {
174 DEBUG(3, ("NO session key, cannot check packet signature\n"));
175 return NT_STATUS_NO_USER_SESSION_KEY;
178 if (sig->length < 8) {
179 DEBUG(0, ("NTLMSSP packet check failed due to short signature (%lu bytes)!\n",
180 (unsigned long)sig->length));
183 nt_status = ntlmssp_make_packet_signature(ntlmssp_state,
184 data, length,
185 whole_pdu, pdu_length,
186 NTLMSSP_RECEIVE, &local_sig, True);
188 if (!NT_STATUS_IS_OK(nt_status)) {
189 DEBUG(0, ("NTLMSSP packet check failed with %s\n", nt_errstr(nt_status)));
190 data_blob_free(&local_sig);
191 return nt_status;
194 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
195 if (local_sig.length != sig->length ||
196 memcmp(local_sig.data, sig->data, sig->length) != 0) {
197 DEBUG(5, ("BAD SIG NTLM2: wanted signature of\n"));
198 dump_data(5, local_sig.data, local_sig.length);
200 DEBUG(5, ("BAD SIG: got signature of\n"));
201 dump_data(5, sig->data, sig->length);
203 DEBUG(0, ("NTLMSSP NTLM2 packet check failed due to invalid signature!\n"));
204 data_blob_free(&local_sig);
205 return NT_STATUS_ACCESS_DENIED;
207 } else {
208 if (local_sig.length != sig->length ||
209 memcmp(local_sig.data + 8, sig->data + 8, sig->length - 8) != 0) {
210 DEBUG(5, ("BAD SIG NTLM1: wanted signature of\n"));
211 dump_data(5, local_sig.data, local_sig.length);
213 DEBUG(5, ("BAD SIG: got signature of\n"));
214 dump_data(5, sig->data, sig->length);
216 DEBUG(0, ("NTLMSSP NTLM1 packet check failed due to invalid signature!\n"));
217 data_blob_free(&local_sig);
218 return NT_STATUS_ACCESS_DENIED;
221 dump_data_pw("checked ntlmssp signature\n", sig->data, sig->length);
222 DEBUG(10,("ntlmssp_check_packet: NTLMSSP signature OK !\n"));
224 data_blob_free(&local_sig);
225 return NT_STATUS_OK;
229 * Seal data with the NTLMSSP algorithm
233 NTSTATUS ntlmssp_seal_packet(NTLMSSP_STATE *ntlmssp_state,
234 uchar *data, size_t length,
235 uchar *whole_pdu, size_t pdu_length,
236 DATA_BLOB *sig)
238 if (!(ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL)) {
239 DEBUG(3, ("NTLMSSP Sealing not negotiated - cannot seal packet!\n"));
240 return NT_STATUS_INVALID_PARAMETER;
243 if (!ntlmssp_state->session_key.length) {
244 DEBUG(3, ("NO session key, cannot seal packet\n"));
245 return NT_STATUS_NO_USER_SESSION_KEY;
248 DEBUG(10,("ntlmssp_seal_data: seal\n"));
249 dump_data_pw("ntlmssp clear data\n", data, length);
250 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
251 /* The order of these two operations matters - we must first seal the packet,
252 then seal the sequence number - this is becouse the send_seal_hash is not
253 constant, but is is rather updated with each iteration */
254 NTSTATUS nt_status = ntlmssp_make_packet_signature(ntlmssp_state,
255 data, length,
256 whole_pdu, pdu_length,
257 NTLMSSP_SEND, sig, False);
258 if (!NT_STATUS_IS_OK(nt_status)) {
259 return nt_status;
262 smb_arc4_crypt(ntlmssp_state->send_seal_arc4_state, data, length);
263 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
264 smb_arc4_crypt(ntlmssp_state->send_seal_arc4_state, sig->data+4, 8);
266 } else {
267 uint32 crc;
268 crc = crc32_calc_buffer((const char *)data, length);
269 if (!msrpc_gen(sig, "dddd", NTLMSSP_SIGN_VERSION, 0, crc, ntlmssp_state->ntlmv1_seq_num)) {
270 return NT_STATUS_NO_MEMORY;
273 /* The order of these two operations matters - we must first seal the packet,
274 then seal the sequence number - this is becouse the ntlmv1_arc4_state is not
275 constant, but is is rather updated with each iteration */
277 dump_data_pw("ntlmv1 arc4 state:\n", ntlmssp_state->ntlmv1_arc4_state,
278 sizeof(ntlmssp_state->ntlmv1_arc4_state));
279 smb_arc4_crypt(ntlmssp_state->ntlmv1_arc4_state, data, length);
281 dump_data_pw("ntlmv1 arc4 state:\n", ntlmssp_state->ntlmv1_arc4_state,
282 sizeof(ntlmssp_state->ntlmv1_arc4_state));
284 smb_arc4_crypt(ntlmssp_state->ntlmv1_arc4_state, sig->data+4, sig->length-4);
286 ntlmssp_state->ntlmv1_seq_num++;
288 dump_data_pw("ntlmssp signature\n", sig->data, sig->length);
289 dump_data_pw("ntlmssp sealed data\n", data, length);
291 return NT_STATUS_OK;
295 * Unseal data with the NTLMSSP algorithm
299 NTSTATUS ntlmssp_unseal_packet(NTLMSSP_STATE *ntlmssp_state,
300 uchar *data, size_t length,
301 uchar *whole_pdu, size_t pdu_length,
302 DATA_BLOB *sig)
304 if (!ntlmssp_state->session_key.length) {
305 DEBUG(3, ("NO session key, cannot unseal packet\n"));
306 return NT_STATUS_NO_USER_SESSION_KEY;
309 DEBUG(10,("ntlmssp_unseal_packet: seal\n"));
310 dump_data_pw("ntlmssp sealed data\n", data, length);
312 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
313 /* First unseal the data. */
314 smb_arc4_crypt(ntlmssp_state->recv_seal_arc4_state, data, length);
315 dump_data_pw("ntlmv2 clear data\n", data, length);
316 } else {
317 smb_arc4_crypt(ntlmssp_state->ntlmv1_arc4_state, data, length);
318 dump_data_pw("ntlmv1 clear data\n", data, length);
320 return ntlmssp_check_packet(ntlmssp_state, data, length, whole_pdu, pdu_length, sig);
324 Initialise the state for NTLMSSP signing.
326 NTSTATUS ntlmssp_sign_init(NTLMSSP_STATE *ntlmssp_state)
328 unsigned char p24[24];
329 TALLOC_CTX *mem_ctx;
330 ZERO_STRUCT(p24);
332 mem_ctx = talloc_init("weak_keys");
333 if (!mem_ctx) {
334 return NT_STATUS_NO_MEMORY;
337 DEBUG(3, ("NTLMSSP Sign/Seal - Initialising with flags:\n"));
338 debug_ntlmssp_flags(ntlmssp_state->neg_flags);
340 if (ntlmssp_state->session_key.length < 8) {
341 TALLOC_FREE(mem_ctx);
342 DEBUG(3, ("NO session key, cannot intialise signing\n"));
343 return NT_STATUS_NO_USER_SESSION_KEY;
346 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
347 DATA_BLOB weak_session_key = ntlmssp_state->session_key;
348 const char *send_sign_const;
349 const char *send_seal_const;
350 const char *recv_sign_const;
351 const char *recv_seal_const;
353 switch (ntlmssp_state->role) {
354 case NTLMSSP_CLIENT:
355 send_sign_const = CLI_SIGN;
356 send_seal_const = CLI_SEAL;
357 recv_sign_const = SRV_SIGN;
358 recv_seal_const = SRV_SEAL;
359 break;
360 case NTLMSSP_SERVER:
361 send_sign_const = SRV_SIGN;
362 send_seal_const = SRV_SEAL;
363 recv_sign_const = CLI_SIGN;
364 recv_seal_const = CLI_SEAL;
365 break;
366 default:
367 TALLOC_FREE(mem_ctx);
368 return NT_STATUS_INTERNAL_ERROR;
372 Weaken NTLMSSP keys to cope with down-level clients, servers and export restrictions.
373 We probably should have some parameters to control this, once we get NTLM2 working.
376 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_128) {
378 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
379 weak_session_key.length = 7;
380 } else { /* forty bits */
381 weak_session_key.length = 5;
384 dump_data_pw("NTLMSSP weakend master key:\n",
385 weak_session_key.data,
386 weak_session_key.length);
388 /* SEND: sign key */
389 calc_ntlmv2_key(ntlmssp_state->send_sign_key,
390 ntlmssp_state->session_key, send_sign_const);
391 dump_data_pw("NTLMSSP send sign key:\n",
392 ntlmssp_state->send_sign_key, 16);
394 /* SEND: seal ARCFOUR pad */
395 calc_ntlmv2_key(ntlmssp_state->send_seal_key,
396 weak_session_key, send_seal_const);
397 dump_data_pw("NTLMSSP send seal key:\n",
398 ntlmssp_state->send_seal_key, 16);
400 smb_arc4_init(ntlmssp_state->send_seal_arc4_state,
401 ntlmssp_state->send_seal_key, 16);
403 dump_data_pw("NTLMSSP send seal arc4 state:\n",
404 ntlmssp_state->send_seal_arc4_state,
405 sizeof(ntlmssp_state->send_seal_arc4_state));
407 /* RECV: sign key */
408 calc_ntlmv2_key(ntlmssp_state->recv_sign_key,
409 ntlmssp_state->session_key, recv_sign_const);
410 dump_data_pw("NTLMSSP recv send sign key:\n",
411 ntlmssp_state->recv_sign_key, 16);
413 /* RECV: seal ARCFOUR pad */
414 calc_ntlmv2_key(ntlmssp_state->recv_seal_key,
415 weak_session_key, recv_seal_const);
417 dump_data_pw("NTLMSSP recv seal key:\n",
418 ntlmssp_state->recv_seal_key, 16);
420 smb_arc4_init(ntlmssp_state->recv_seal_arc4_state,
421 ntlmssp_state->recv_seal_key, 16);
423 dump_data_pw("NTLMSSP recv seal arc4 state:\n",
424 ntlmssp_state->recv_seal_arc4_state,
425 sizeof(ntlmssp_state->recv_seal_arc4_state));
427 ntlmssp_state->ntlm2_send_seq_num = 0;
428 ntlmssp_state->ntlm2_recv_seq_num = 0;
431 } else {
432 #if 0
433 /* Hmmm. Shouldn't we also weaken keys for ntlmv1 ? JRA. */
435 DATA_BLOB weak_session_key = ntlmssp_state->session_key;
437 Weaken NTLMSSP keys to cope with down-level clients, servers and export restrictions.
438 We probably should have some parameters to control this, once we get NTLM2 working.
441 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_128) {
443 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
444 weak_session_key.length = 6;
445 } else { /* forty bits */
446 weak_session_key.length = 5;
448 dump_data_pw("NTLMSSP weakend master key:\n",
449 weak_session_key.data,
450 weak_session_key.length);
451 #endif
453 DATA_BLOB weak_session_key = ntlmssp_weaken_keys(ntlmssp_state, mem_ctx);
455 DEBUG(5, ("NTLMSSP Sign/Seal - using NTLM1\n"));
457 smb_arc4_init(ntlmssp_state->ntlmv1_arc4_state,
458 weak_session_key.data, weak_session_key.length);
460 dump_data_pw("NTLMv1 arc4 state:\n", ntlmssp_state->ntlmv1_arc4_state,
461 sizeof(ntlmssp_state->ntlmv1_arc4_state));
463 ntlmssp_state->ntlmv1_seq_num = 0;
466 TALLOC_FREE(mem_ctx);
467 return NT_STATUS_OK;