2 * Unix SMB/CIFS implementation.
4 * NTLMSSP Signing routines
5 * Copyright (C) Luke Kenneth Casson Leighton 1996-2001
6 * Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2005
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 Foundation,
20 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "auth/auth.h"
25 #include "auth/ntlmssp/ntlmssp.h"
26 #include "lib/crypto/crypto.h"
28 #define CLI_SIGN "session key to client-to-server signing key magic constant"
29 #define CLI_SEAL "session key to client-to-server sealing key magic constant"
30 #define SRV_SIGN "session key to server-to-client signing key magic constant"
31 #define SRV_SEAL "session key to server-to-client sealing key magic constant"
34 * Some notes on then NTLM2 code:
36 * NTLM2 is a AEAD system. This means that the data encrypted is not
37 * all the data that is signed. In DCE-RPC case, the headers of the
38 * DCE-RPC packets are also signed. This prevents some of the
39 * fun-and-games one might have by changing them.
43 static void calc_ntlmv2_key(TALLOC_CTX *mem_ctx,
45 DATA_BLOB session_key,
48 struct MD5Context ctx3;
49 *subkey = data_blob_talloc(mem_ctx, NULL, 16);
51 MD5Update(&ctx3, session_key.data, session_key.length);
52 MD5Update(&ctx3, constant, strlen(constant)+1);
53 MD5Final(subkey->data, &ctx3);
56 enum ntlmssp_direction {
61 static NTSTATUS ntlmssp_make_packet_signature(struct ntlmssp_state *ntlmssp_state,
62 TALLOC_CTX *sig_mem_ctx,
63 const uint8_t *data, size_t length,
64 const uint8_t *whole_pdu, size_t pdu_length,
65 enum ntlmssp_direction direction,
66 DATA_BLOB *sig, BOOL encrypt_sig)
68 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
74 *sig = data_blob_talloc(sig_mem_ctx, NULL, NTLMSSP_SIG_SIZE);
76 return NT_STATUS_NO_MEMORY;
81 SIVAL(seq_num, 0, ntlmssp_state->ntlm2_send_seq_num);
82 ntlmssp_state->ntlm2_send_seq_num++;
83 hmac_md5_init_limK_to_64(ntlmssp_state->send_sign_key.data,
84 ntlmssp_state->send_sign_key.length, &ctx);
87 SIVAL(seq_num, 0, ntlmssp_state->ntlm2_recv_seq_num);
88 ntlmssp_state->ntlm2_recv_seq_num++;
89 hmac_md5_init_limK_to_64(ntlmssp_state->recv_sign_key.data,
90 ntlmssp_state->recv_sign_key.length, &ctx);
93 hmac_md5_update(seq_num, sizeof(seq_num), &ctx);
94 hmac_md5_update(whole_pdu, pdu_length, &ctx);
95 hmac_md5_final(digest, &ctx);
97 if (encrypt_sig && ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
100 arcfour_crypt_sbox(ntlmssp_state->send_seal_hash, digest, 8);
102 case NTLMSSP_RECEIVE:
103 arcfour_crypt_sbox(ntlmssp_state->recv_seal_hash, digest, 8);
108 SIVAL(sig->data, 0, NTLMSSP_SIGN_VERSION);
109 memcpy(sig->data + 4, digest, 8);
110 memcpy(sig->data + 12, seq_num, 4);
114 crc = crc32_calc_buffer(data, length);
115 if (!msrpc_gen(sig_mem_ctx, sig, "dddd", NTLMSSP_SIGN_VERSION, 0, crc, ntlmssp_state->ntlm_seq_num)) {
116 return NT_STATUS_NO_MEMORY;
118 ntlmssp_state->ntlm_seq_num++;
120 arcfour_crypt_sbox(ntlmssp_state->ntlmssp_hash, sig->data+4, sig->length-4);
122 dump_data_pw("calculated ntlmssp signature\n", sig->data, sig->length);
126 NTSTATUS gensec_ntlmssp_sign_packet(struct gensec_security *gensec_security,
127 TALLOC_CTX *sig_mem_ctx,
128 const uint8_t *data, size_t length,
129 const uint8_t *whole_pdu, size_t pdu_length,
132 struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
133 struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp_state->ntlmssp_state;
135 if (!ntlmssp_state->session_key.length) {
136 DEBUG(3, ("NO session key, cannot check sign packet\n"));
137 return NT_STATUS_NO_USER_SESSION_KEY;
140 if (!ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN) {
141 DEBUG(3, ("NTLMSSP Signing not negotiated - cannot sign packet!\n"));
142 return NT_STATUS_INVALID_PARAMETER;
145 return ntlmssp_make_packet_signature(ntlmssp_state, sig_mem_ctx,
147 whole_pdu, pdu_length,
148 NTLMSSP_SEND, sig, True);
152 * Check the signature of an incoming packet
156 NTSTATUS gensec_ntlmssp_check_packet(struct gensec_security *gensec_security,
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 const DATA_BLOB *sig)
162 struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
163 struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp_state->ntlmssp_state;
168 if (!ntlmssp_state->session_key.length) {
169 DEBUG(3, ("NO session key, cannot check packet signature\n"));
170 return NT_STATUS_NO_USER_SESSION_KEY;
173 if (sig->length < 8) {
174 DEBUG(0, ("NTLMSSP packet check failed due to short signature (%lu bytes)!\n",
175 (unsigned long)sig->length));
178 nt_status = ntlmssp_make_packet_signature(ntlmssp_state, sig_mem_ctx,
180 whole_pdu, pdu_length,
181 NTLMSSP_RECEIVE, &local_sig, True);
183 if (!NT_STATUS_IS_OK(nt_status)) {
184 DEBUG(0, ("NTLMSSP packet check failed with %s\n", nt_errstr(nt_status)));
188 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
189 if (local_sig.length != sig->length ||
190 memcmp(local_sig.data,
191 sig->data, sig->length) != 0) {
192 DEBUG(5, ("BAD SIG NTLM2: wanted signature of\n"));
193 dump_data(5, local_sig.data, local_sig.length);
195 DEBUG(5, ("BAD SIG: got signature of\n"));
196 dump_data(5, sig->data, sig->length);
198 DEBUG(0, ("NTLMSSP NTLM2 packet check failed due to invalid signature!\n"));
199 return NT_STATUS_ACCESS_DENIED;
202 if (local_sig.length != sig->length ||
203 memcmp(local_sig.data + 8,
204 sig->data + 8, sig->length - 8) != 0) {
205 DEBUG(5, ("BAD SIG NTLM1: wanted signature of\n"));
206 dump_data(5, local_sig.data, local_sig.length);
208 DEBUG(5, ("BAD SIG: got signature of\n"));
209 dump_data(5, sig->data, sig->length);
211 DEBUG(0, ("NTLMSSP NTLM1 packet check failed due to invalid signature!\n"));
212 return NT_STATUS_ACCESS_DENIED;
215 dump_data_pw("checked ntlmssp signature\n", sig->data, sig->length);
222 * Seal data with the NTLMSSP algorithm
226 NTSTATUS gensec_ntlmssp_seal_packet(struct gensec_security *gensec_security,
227 TALLOC_CTX *sig_mem_ctx,
228 uint8_t *data, size_t length,
229 const uint8_t *whole_pdu, size_t pdu_length,
232 struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
233 struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp_state->ntlmssp_state;
235 if (!ntlmssp_state->session_key.length) {
236 DEBUG(3, ("NO session key, cannot seal packet\n"));
237 return NT_STATUS_NO_USER_SESSION_KEY;
240 if (!ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL) {
241 DEBUG(3, ("NTLMSSP Sealing not negotiated - cannot seal packet!\n"));
242 return NT_STATUS_INVALID_PARAMETER;
245 DEBUG(10,("ntlmssp_seal_data: seal\n"));
246 dump_data_pw("ntlmssp clear data\n", data, length);
247 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
248 /* The order of these two operations matters - we must first seal the packet,
249 then seal the sequence number - this is becouse the send_seal_hash is not
250 constant, but is is rather updated with each iteration */
251 nt_status = ntlmssp_make_packet_signature(ntlmssp_state, sig_mem_ctx,
253 whole_pdu, pdu_length,
254 NTLMSSP_SEND, sig, False);
255 arcfour_crypt_sbox(ntlmssp_state->send_seal_hash, data, length);
256 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
257 arcfour_crypt_sbox(ntlmssp_state->send_seal_hash, sig->data+4, 8);
261 crc = crc32_calc_buffer(data, length);
262 if (!msrpc_gen(sig_mem_ctx, sig, "dddd", NTLMSSP_SIGN_VERSION, 0, crc, ntlmssp_state->ntlm_seq_num)) {
263 return NT_STATUS_NO_MEMORY;
266 /* The order of these two operations matters - we must
267 first seal the packet, then seal the sequence
268 number - this is becouse the ntlmssp_hash is not
269 constant, but is is rather updated with each
272 arcfour_crypt_sbox(ntlmssp_state->ntlmssp_hash, data, length);
273 arcfour_crypt_sbox(ntlmssp_state->ntlmssp_hash, sig->data+4, sig->length-4);
274 /* increment counter on send */
275 ntlmssp_state->ntlm_seq_num++;
276 nt_status = NT_STATUS_OK;
278 dump_data_pw("ntlmssp signature\n", sig->data, sig->length);
279 dump_data_pw("ntlmssp sealed data\n", data, length);
286 * Unseal data with the NTLMSSP algorithm
291 wrappers for the ntlmssp_*() functions
293 NTSTATUS gensec_ntlmssp_unseal_packet(struct gensec_security *gensec_security,
294 TALLOC_CTX *sig_mem_ctx,
295 uint8_t *data, size_t length,
296 const uint8_t *whole_pdu, size_t pdu_length,
297 const DATA_BLOB *sig)
299 struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
300 struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp_state->ntlmssp_state;
303 if (!ntlmssp_state->session_key.length) {
304 DEBUG(3, ("NO session key, cannot unseal packet\n"));
305 return NT_STATUS_NO_USER_SESSION_KEY;
308 dump_data_pw("ntlmssp sealed data\n", data, length);
309 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
310 arcfour_crypt_sbox(ntlmssp_state->recv_seal_hash, data, length);
312 nt_status = ntlmssp_make_packet_signature(ntlmssp_state, sig_mem_ctx,
314 whole_pdu, pdu_length,
315 NTLMSSP_RECEIVE, &local_sig, True);
316 if (!NT_STATUS_IS_OK(nt_status)) {
320 if (local_sig.length != sig->length ||
321 memcmp(local_sig.data,
322 sig->data, sig->length) != 0) {
323 DEBUG(5, ("BAD SIG NTLM2: wanted signature of\n"));
324 dump_data(5, local_sig.data, local_sig.length);
326 DEBUG(5, ("BAD SIG: got signature of\n"));
327 dump_data(5, sig->data, sig->length);
329 DEBUG(0, ("NTLMSSP NTLM2 packet check failed due to invalid signature!\n"));
330 return NT_STATUS_ACCESS_DENIED;
333 dump_data_pw("ntlmssp clear data\n", data, length);
336 arcfour_crypt_sbox(ntlmssp_state->ntlmssp_hash, data, length);
337 dump_data_pw("ntlmssp clear data\n", data, length);
338 return gensec_ntlmssp_check_packet(gensec_security, sig_mem_ctx, data, length, whole_pdu, pdu_length, sig);
343 Initialise the state for NTLMSSP signing.
345 NTSTATUS ntlmssp_sign_init(struct ntlmssp_state *ntlmssp_state)
350 DEBUG(3, ("NTLMSSP Sign/Seal - Initialising with flags:\n"));
351 debug_ntlmssp_flags(ntlmssp_state->neg_flags);
353 if (!ntlmssp_state->session_key.length) {
354 DEBUG(3, ("NO session key, cannot intialise signing\n"));
355 return NT_STATUS_NO_USER_SESSION_KEY;
358 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
360 DATA_BLOB weak_session_key = ntlmssp_state->session_key;
361 const char *send_sign_const;
362 const char *send_seal_const;
363 const char *recv_sign_const;
364 const char *recv_seal_const;
366 DATA_BLOB send_seal_key;
367 DATA_BLOB recv_seal_key;
369 switch (ntlmssp_state->role) {
371 send_sign_const = CLI_SIGN;
372 send_seal_const = CLI_SEAL;
373 recv_sign_const = SRV_SIGN;
374 recv_seal_const = SRV_SEAL;
377 send_sign_const = SRV_SIGN;
378 send_seal_const = SRV_SEAL;
379 recv_sign_const = CLI_SIGN;
380 recv_seal_const = CLI_SEAL;
383 return NT_STATUS_INTERNAL_ERROR;
386 ntlmssp_state->send_seal_hash = talloc(ntlmssp_state, struct arcfour_state);
387 NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->send_seal_hash);
388 ntlmssp_state->recv_seal_hash = talloc(ntlmssp_state, struct arcfour_state);
389 NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->recv_seal_hash);
392 Weaken NTLMSSP keys to cope with down-level clients, servers and export restrictions.
394 We probably should have some parameters to control this, once we get NTLM2 working.
398 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_128) {
400 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
401 weak_session_key.length = 6;
402 } else { /* forty bits */
403 weak_session_key.length = 5;
405 dump_data_pw("NTLMSSP weakend master key:\n",
406 weak_session_key.data,
407 weak_session_key.length);
410 calc_ntlmv2_key(ntlmssp_state,
411 &ntlmssp_state->send_sign_key,
412 ntlmssp_state->session_key, send_sign_const);
413 dump_data_pw("NTLMSSP send sign key:\n",
414 ntlmssp_state->send_sign_key.data,
415 ntlmssp_state->send_sign_key.length);
417 calc_ntlmv2_key(ntlmssp_state,
419 weak_session_key, send_seal_const);
420 dump_data_pw("NTLMSSP send seal key:\n",
422 send_seal_key.length);
424 arcfour_init(ntlmssp_state->send_seal_hash,
427 dump_data_pw("NTLMSSP send sesl hash:\n",
428 ntlmssp_state->send_seal_hash->sbox,
429 sizeof(ntlmssp_state->send_seal_hash->sbox));
432 calc_ntlmv2_key(ntlmssp_state,
433 &ntlmssp_state->recv_sign_key,
434 ntlmssp_state->session_key, recv_sign_const);
435 dump_data_pw("NTLMSSP recv sign key:\n",
436 ntlmssp_state->recv_sign_key.data,
437 ntlmssp_state->recv_sign_key.length);
439 calc_ntlmv2_key(ntlmssp_state,
441 weak_session_key, recv_seal_const);
442 dump_data_pw("NTLMSSP recv seal key:\n",
444 recv_seal_key.length);
445 arcfour_init(ntlmssp_state->recv_seal_hash,
448 dump_data_pw("NTLMSSP receive seal hash:\n",
449 ntlmssp_state->recv_seal_hash->sbox,
450 sizeof(ntlmssp_state->recv_seal_hash->sbox));
452 DEBUG(5, ("NTLMSSP Sign/Seal - using NTLM1\n"));
454 ntlmssp_state->ntlmssp_hash = talloc(ntlmssp_state, struct arcfour_state);
455 NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->ntlmssp_hash);
457 arcfour_init(ntlmssp_state->ntlmssp_hash,
458 &ntlmssp_state->session_key);
459 dump_data_pw("NTLMSSP hash:\n", ntlmssp_state->ntlmssp_hash->sbox,
460 sizeof(ntlmssp_state->ntlmssp_hash->sbox));
463 ntlmssp_state->ntlm_seq_num = 0;
464 ntlmssp_state->ntlm2_send_seq_num = 0;
465 ntlmssp_state->ntlm2_recv_seq_num = 0;
470 size_t gensec_ntlmssp_sig_size(struct gensec_security *gensec_security)
472 return NTLMSSP_SIG_SIZE;
475 NTSTATUS gensec_ntlmssp_wrap(struct gensec_security *gensec_security,
476 TALLOC_CTX *sig_mem_ctx,
480 struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
484 if (gensec_ntlmssp_state->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL) {
486 *out = data_blob_talloc(sig_mem_ctx, NULL, in->length + NTLMSSP_SIG_SIZE);
487 memcpy(out->data + NTLMSSP_SIG_SIZE, in->data, in->length);
489 nt_status = gensec_ntlmssp_seal_packet(gensec_security, sig_mem_ctx,
490 out->data + NTLMSSP_SIG_SIZE,
491 out->length - NTLMSSP_SIG_SIZE,
492 out->data + NTLMSSP_SIG_SIZE,
493 out->length - NTLMSSP_SIG_SIZE,
496 if (NT_STATUS_IS_OK(nt_status)) {
497 memcpy(out->data, sig.data, NTLMSSP_SIG_SIZE);
501 } else if ((gensec_ntlmssp_state->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
502 || (gensec_ntlmssp_state->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)) {
504 *out = data_blob_talloc(sig_mem_ctx, NULL, in->length + NTLMSSP_SIG_SIZE);
505 memcpy(out->data + NTLMSSP_SIG_SIZE, in->data, in->length);
507 nt_status = gensec_ntlmssp_sign_packet(gensec_security, sig_mem_ctx,
508 out->data + NTLMSSP_SIG_SIZE,
509 out->length - NTLMSSP_SIG_SIZE,
510 out->data + NTLMSSP_SIG_SIZE,
511 out->length - NTLMSSP_SIG_SIZE,
514 if (NT_STATUS_IS_OK(nt_status)) {
515 memcpy(out->data, sig.data, NTLMSSP_SIG_SIZE);
526 NTSTATUS gensec_ntlmssp_unwrap(struct gensec_security *gensec_security,
527 TALLOC_CTX *sig_mem_ctx,
531 struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
534 if (gensec_ntlmssp_state->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL) {
535 if (in->length < NTLMSSP_SIG_SIZE) {
536 return NT_STATUS_INVALID_PARAMETER;
539 sig.length = NTLMSSP_SIG_SIZE;
541 *out = data_blob_talloc(sig_mem_ctx, in->data + NTLMSSP_SIG_SIZE, in->length - NTLMSSP_SIG_SIZE);
543 return gensec_ntlmssp_unseal_packet(gensec_security, sig_mem_ctx,
544 out->data, out->length,
545 out->data, out->length,
548 } else if ((gensec_ntlmssp_state->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
549 || (gensec_ntlmssp_state->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)) {
550 if (in->length < NTLMSSP_SIG_SIZE) {
551 return NT_STATUS_INVALID_PARAMETER;
554 sig.length = NTLMSSP_SIG_SIZE;
556 *out = data_blob_talloc(sig_mem_ctx, in->data + NTLMSSP_SIG_SIZE, in->length - NTLMSSP_SIG_SIZE);
558 return gensec_ntlmssp_check_packet(gensec_security, sig_mem_ctx,
559 out->data, out->length,
560 out->data, out->length,