2 * Unix SMB/CIFS implementation.
4 * NTLMSSP Signing routines
5 * Copyright (C) Andrew Bartlett 2003-2005
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/>.
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"
27 #include "../auth/ntlmssp/ntlmssp_private.h"
30 #define DBGC_CLASS DBGC_AUTH
32 #define CLI_SIGN "session key to client-to-server signing key magic constant"
33 #define CLI_SEAL "session key to client-to-server sealing key magic constant"
34 #define SRV_SIGN "session key to server-to-client signing key magic constant"
35 #define SRV_SEAL "session key to server-to-client sealing key magic constant"
38 * Some notes on the NTLM2 code:
40 * NTLM2 is a AEAD system. This means that the data encrypted is not
41 * all the data that is signed. In DCE-RPC case, the headers of the
42 * DCE-RPC packets are also signed. This prevents some of the
43 * fun-and-games one might have by changing them.
47 static void dump_arc4_state(const char *description,
48 struct arcfour_state *state)
50 dump_data_pw(description, state->sbox, sizeof(state->sbox));
53 static void calc_ntlmv2_key(uint8_t subkey[16],
54 DATA_BLOB session_key,
59 MD5Update(&ctx3, session_key.data, session_key.length);
60 MD5Update(&ctx3, (const uint8_t *)constant, strlen(constant)+1);
61 MD5Final(subkey, &ctx3);
64 enum ntlmssp_direction {
69 static NTSTATUS ntlmssp_make_packet_signature(struct ntlmssp_state *ntlmssp_state,
70 TALLOC_CTX *sig_mem_ctx,
71 const uint8_t *data, size_t length,
72 const uint8_t *whole_pdu, size_t pdu_length,
73 enum ntlmssp_direction direction,
74 DATA_BLOB *sig, bool encrypt_sig)
76 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
81 *sig = data_blob_talloc(sig_mem_ctx, NULL, NTLMSSP_SIG_SIZE);
83 return NT_STATUS_NO_MEMORY;
88 DEBUG(100,("ntlmssp_make_packet_signature: SEND seq = %u, len = %u, pdu_len = %u\n",
89 ntlmssp_state->crypt->ntlm2.sending.seq_num,
91 (unsigned int)pdu_length));
93 SIVAL(seq_num, 0, ntlmssp_state->crypt->ntlm2.sending.seq_num);
94 ntlmssp_state->crypt->ntlm2.sending.seq_num++;
95 hmac_md5_init_limK_to_64(ntlmssp_state->crypt->ntlm2.sending.sign_key, 16, &ctx);
99 DEBUG(100,("ntlmssp_make_packet_signature: RECV seq = %u, len = %u, pdu_len = %u\n",
100 ntlmssp_state->crypt->ntlm2.receiving.seq_num,
101 (unsigned int)length,
102 (unsigned int)pdu_length));
104 SIVAL(seq_num, 0, ntlmssp_state->crypt->ntlm2.receiving.seq_num);
105 ntlmssp_state->crypt->ntlm2.receiving.seq_num++;
106 hmac_md5_init_limK_to_64(ntlmssp_state->crypt->ntlm2.receiving.sign_key, 16, &ctx);
110 dump_data_pw("pdu data ", whole_pdu, pdu_length);
112 hmac_md5_update(seq_num, sizeof(seq_num), &ctx);
113 hmac_md5_update(whole_pdu, pdu_length, &ctx);
114 hmac_md5_final(digest, &ctx);
116 if (encrypt_sig && (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)) {
119 arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm2.sending.seal_state,
122 case NTLMSSP_RECEIVE:
123 arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm2.receiving.seal_state,
129 SIVAL(sig->data, 0, NTLMSSP_SIGN_VERSION);
130 memcpy(sig->data + 4, digest, 8);
131 memcpy(sig->data + 12, seq_num, 4);
133 dump_data_pw("ntlmssp v2 sig ", sig->data, sig->length);
139 crc = crc32(0, Z_NULL, 0);
140 crc = crc32(crc, data, length);
142 status = msrpc_gen(sig_mem_ctx,
144 NTLMSSP_SIGN_VERSION, 0, crc,
145 ntlmssp_state->crypt->ntlm.seq_num);
146 if (!NT_STATUS_IS_OK(status)) {
150 ntlmssp_state->crypt->ntlm.seq_num++;
152 dump_arc4_state("ntlmssp hash: \n",
153 &ntlmssp_state->crypt->ntlm.seal_state);
154 arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm.seal_state,
155 sig->data+4, sig->length-4);
160 NTSTATUS ntlmssp_sign_packet(struct ntlmssp_state *ntlmssp_state,
161 TALLOC_CTX *sig_mem_ctx,
162 const uint8_t *data, size_t length,
163 const uint8_t *whole_pdu, size_t pdu_length,
168 if (!(ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
169 DEBUG(3, ("NTLMSSP Signing not negotiated - cannot sign packet!\n"));
170 return NT_STATUS_INVALID_PARAMETER;
173 if (!ntlmssp_state->session_key.length) {
174 DEBUG(3, ("NO session key, cannot check sign packet\n"));
175 return NT_STATUS_NO_USER_SESSION_KEY;
178 nt_status = ntlmssp_make_packet_signature(ntlmssp_state,
181 whole_pdu, pdu_length,
182 NTLMSSP_SEND, sig, true);
188 * Check the signature of an incoming packet
189 * @note caller *must* check that the signature is the size it expects
193 NTSTATUS ntlmssp_check_packet(struct ntlmssp_state *ntlmssp_state,
194 const uint8_t *data, size_t length,
195 const uint8_t *whole_pdu, size_t pdu_length,
196 const DATA_BLOB *sig)
202 if (!ntlmssp_state->session_key.length) {
203 DEBUG(3, ("NO session key, cannot check packet signature\n"));
204 return NT_STATUS_NO_USER_SESSION_KEY;
207 if (sig->length < 8) {
208 DEBUG(0, ("NTLMSSP packet check failed due to short signature (%lu bytes)!\n",
209 (unsigned long)sig->length));
212 tmp_ctx = talloc_new(ntlmssp_state);
214 return NT_STATUS_NO_MEMORY;
217 nt_status = ntlmssp_make_packet_signature(ntlmssp_state,
220 whole_pdu, pdu_length,
224 if (!NT_STATUS_IS_OK(nt_status)) {
225 DEBUG(0,("NTLMSSP packet sig creation failed with %s\n",
226 nt_errstr(nt_status)));
227 talloc_free(tmp_ctx);
231 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
232 if (local_sig.length != sig->length ||
233 memcmp(local_sig.data, sig->data, sig->length) != 0) {
234 DEBUG(5, ("BAD SIG NTLM2: wanted signature of\n"));
235 dump_data(5, local_sig.data, local_sig.length);
237 DEBUG(5, ("BAD SIG: got signature of\n"));
238 dump_data(5, sig->data, sig->length);
240 DEBUG(0, ("NTLMSSP NTLM2 packet check failed due to invalid signature!\n"));
241 talloc_free(tmp_ctx);
242 return NT_STATUS_ACCESS_DENIED;
245 if (local_sig.length != sig->length ||
246 memcmp(local_sig.data + 8, sig->data + 8, sig->length - 8) != 0) {
247 DEBUG(5, ("BAD SIG NTLM1: wanted signature of\n"));
248 dump_data(5, local_sig.data, local_sig.length);
250 DEBUG(5, ("BAD SIG: got signature of\n"));
251 dump_data(5, sig->data, sig->length);
253 DEBUG(0, ("NTLMSSP NTLM1 packet check failed due to invalid signature!\n"));
254 talloc_free(tmp_ctx);
255 return NT_STATUS_ACCESS_DENIED;
258 dump_data_pw("checked ntlmssp signature\n", sig->data, sig->length);
259 DEBUG(10,("ntlmssp_check_packet: NTLMSSP signature OK !\n"));
261 talloc_free(tmp_ctx);
266 * Seal data with the NTLMSSP algorithm
270 NTSTATUS ntlmssp_seal_packet(struct ntlmssp_state *ntlmssp_state,
271 TALLOC_CTX *sig_mem_ctx,
272 uint8_t *data, size_t length,
273 const uint8_t *whole_pdu, size_t pdu_length,
276 if (!(ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL)) {
277 DEBUG(3, ("NTLMSSP Sealing not negotiated - cannot seal packet!\n"));
278 return NT_STATUS_INVALID_PARAMETER;
281 if (!(ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
282 DEBUG(3, ("NTLMSSP Sealing not negotiated - cannot seal packet!\n"));
283 return NT_STATUS_INVALID_PARAMETER;
286 if (!ntlmssp_state->session_key.length) {
287 DEBUG(3, ("NO session key, cannot seal packet\n"));
288 return NT_STATUS_NO_USER_SESSION_KEY;
291 DEBUG(10,("ntlmssp_seal_data: seal\n"));
292 dump_data_pw("ntlmssp clear data\n", data, length);
293 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
296 * The order of these two operations matters - we
297 * must first seal the packet, then seal the
298 * sequence number - this is because the
299 * send_seal_hash is not constant, but is is rather
300 * updated with each iteration
302 nt_status = ntlmssp_make_packet_signature(ntlmssp_state,
305 whole_pdu, pdu_length,
308 if (!NT_STATUS_IS_OK(nt_status)) {
312 arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm2.sending.seal_state,
314 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
315 arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm2.sending.seal_state,
322 crc = crc32(0, Z_NULL, 0);
323 crc = crc32(crc, data, length);
325 status = msrpc_gen(sig_mem_ctx,
327 NTLMSSP_SIGN_VERSION, 0, crc,
328 ntlmssp_state->crypt->ntlm.seq_num);
329 if (!NT_STATUS_IS_OK(status)) {
334 * The order of these two operations matters - we
335 * must first seal the packet, then seal the
336 * sequence number - this is because the ntlmv1_arc4_state
337 * is not constant, but is is rather updated with
341 dump_arc4_state("ntlmv1 arc4 state:\n",
342 &ntlmssp_state->crypt->ntlm.seal_state);
343 arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm.seal_state,
346 dump_arc4_state("ntlmv1 arc4 state:\n",
347 &ntlmssp_state->crypt->ntlm.seal_state);
349 arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm.seal_state,
350 sig->data+4, sig->length-4);
352 ntlmssp_state->crypt->ntlm.seq_num++;
354 dump_data_pw("ntlmssp signature\n", sig->data, sig->length);
355 dump_data_pw("ntlmssp sealed data\n", data, length);
361 * Unseal data with the NTLMSSP algorithm
365 NTSTATUS ntlmssp_unseal_packet(struct ntlmssp_state *ntlmssp_state,
366 uint8_t *data, size_t length,
367 const uint8_t *whole_pdu, size_t pdu_length,
368 const DATA_BLOB *sig)
371 if (!ntlmssp_state->session_key.length) {
372 DEBUG(3, ("NO session key, cannot unseal packet\n"));
373 return NT_STATUS_NO_USER_SESSION_KEY;
376 DEBUG(10,("ntlmssp_unseal_packet: seal\n"));
377 dump_data_pw("ntlmssp sealed data\n", data, length);
379 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
380 /* First unseal the data. */
381 arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm2.receiving.seal_state,
383 dump_data_pw("ntlmv2 clear data\n", data, length);
385 arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm.seal_state,
387 dump_data_pw("ntlmv1 clear data\n", data, length);
389 status = ntlmssp_check_packet(ntlmssp_state,
391 whole_pdu, pdu_length,
394 if (!NT_STATUS_IS_OK(status)) {
395 DEBUG(1,("NTLMSSP packet check for unseal failed due to invalid signature on %llu bytes of input:\n",
396 (unsigned long long)length));
401 NTSTATUS ntlmssp_wrap(struct ntlmssp_state *ntlmssp_state,
402 TALLOC_CTX *out_mem_ctx,
409 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL) {
410 if (in->length + NTLMSSP_SIG_SIZE < in->length) {
411 return NT_STATUS_INVALID_PARAMETER;
414 *out = data_blob_talloc(out_mem_ctx, NULL, in->length + NTLMSSP_SIG_SIZE);
416 return NT_STATUS_NO_MEMORY;
418 memcpy(out->data + NTLMSSP_SIG_SIZE, in->data, in->length);
420 nt_status = ntlmssp_seal_packet(ntlmssp_state, out_mem_ctx,
421 out->data + NTLMSSP_SIG_SIZE,
422 out->length - NTLMSSP_SIG_SIZE,
423 out->data + NTLMSSP_SIG_SIZE,
424 out->length - NTLMSSP_SIG_SIZE,
427 if (NT_STATUS_IS_OK(nt_status)) {
428 memcpy(out->data, sig.data, NTLMSSP_SIG_SIZE);
429 talloc_free(sig.data);
433 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN) {
434 if (in->length + NTLMSSP_SIG_SIZE < in->length) {
435 return NT_STATUS_INVALID_PARAMETER;
438 *out = data_blob_talloc(out_mem_ctx, NULL, in->length + NTLMSSP_SIG_SIZE);
440 return NT_STATUS_NO_MEMORY;
442 memcpy(out->data + NTLMSSP_SIG_SIZE, in->data, in->length);
444 nt_status = ntlmssp_sign_packet(ntlmssp_state, out_mem_ctx,
445 out->data + NTLMSSP_SIG_SIZE,
446 out->length - NTLMSSP_SIG_SIZE,
447 out->data + NTLMSSP_SIG_SIZE,
448 out->length - NTLMSSP_SIG_SIZE,
451 if (NT_STATUS_IS_OK(nt_status)) {
452 memcpy(out->data, sig.data, NTLMSSP_SIG_SIZE);
453 talloc_free(sig.data);
457 *out = data_blob_talloc(out_mem_ctx, in->data, in->length);
459 return NT_STATUS_NO_MEMORY;
465 NTSTATUS ntlmssp_unwrap(struct ntlmssp_state *ntlmssp_state,
466 TALLOC_CTX *out_mem_ctx,
472 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL) {
473 if (in->length < NTLMSSP_SIG_SIZE) {
474 return NT_STATUS_INVALID_PARAMETER;
477 sig.length = NTLMSSP_SIG_SIZE;
479 *out = data_blob_talloc(out_mem_ctx, in->data + NTLMSSP_SIG_SIZE, in->length - NTLMSSP_SIG_SIZE);
481 return ntlmssp_unseal_packet(ntlmssp_state,
482 out->data, out->length,
483 out->data, out->length,
486 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN) {
487 if (in->length < NTLMSSP_SIG_SIZE) {
488 return NT_STATUS_INVALID_PARAMETER;
491 sig.length = NTLMSSP_SIG_SIZE;
493 *out = data_blob_talloc(out_mem_ctx, in->data + NTLMSSP_SIG_SIZE, in->length - NTLMSSP_SIG_SIZE);
495 return ntlmssp_check_packet(ntlmssp_state,
496 out->data, out->length,
497 out->data, out->length,
500 *out = data_blob_talloc(out_mem_ctx, in->data, in->length);
502 return NT_STATUS_NO_MEMORY;
509 Initialise the state for NTLMSSP signing.
511 NTSTATUS ntlmssp_sign_reset(struct ntlmssp_state *ntlmssp_state,
514 DEBUG(3, ("NTLMSSP Sign/Seal - Initialising with flags:\n"));
515 debug_ntlmssp_flags(ntlmssp_state->neg_flags);
517 if (ntlmssp_state->crypt == NULL) {
518 return NT_STATUS_INVALID_PARAMETER_MIX;
521 if (ntlmssp_state->force_wrap_seal &&
522 (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN))
525 * We need to handle NTLMSSP_NEGOTIATE_SIGN as
526 * NTLMSSP_NEGOTIATE_SEAL if GENSEC_FEATURE_LDAP_STYLE
529 * The negotiation of flags (and authentication)
530 * is completed when ntlmssp_sign_init() is called
531 * so we can safely pretent NTLMSSP_NEGOTIATE_SEAL
534 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
537 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
538 DATA_BLOB weak_session_key = ntlmssp_state->session_key;
539 const char *send_sign_const;
540 const char *send_seal_const;
541 const char *recv_sign_const;
542 const char *recv_seal_const;
543 uint8_t send_seal_key[16];
544 DATA_BLOB send_seal_blob = data_blob_const(send_seal_key,
545 sizeof(send_seal_key));
546 uint8_t recv_seal_key[16];
547 DATA_BLOB recv_seal_blob = data_blob_const(recv_seal_key,
548 sizeof(recv_seal_key));
550 switch (ntlmssp_state->role) {
552 send_sign_const = CLI_SIGN;
553 send_seal_const = CLI_SEAL;
554 recv_sign_const = SRV_SIGN;
555 recv_seal_const = SRV_SEAL;
558 send_sign_const = SRV_SIGN;
559 send_seal_const = SRV_SEAL;
560 recv_sign_const = CLI_SIGN;
561 recv_seal_const = CLI_SEAL;
564 return NT_STATUS_INTERNAL_ERROR;
568 * Weaken NTLMSSP keys to cope with down-level
569 * clients, servers and export restrictions.
571 * We probably should have some parameters to
572 * control this, once we get NTLM2 working.
574 * Key weakening was not performed on the master key
575 * for NTLM2, but must be done on the encryption subkeys only.
578 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_128) {
580 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
581 weak_session_key.length = 7;
582 } else { /* forty bits */
583 weak_session_key.length = 5;
586 dump_data_pw("NTLMSSP weakend master key:\n",
587 weak_session_key.data,
588 weak_session_key.length);
591 calc_ntlmv2_key(ntlmssp_state->crypt->ntlm2.sending.sign_key,
592 ntlmssp_state->session_key, send_sign_const);
593 dump_data_pw("NTLMSSP send sign key:\n",
594 ntlmssp_state->crypt->ntlm2.sending.sign_key, 16);
596 /* SEND: seal ARCFOUR pad */
597 calc_ntlmv2_key(send_seal_key,
598 weak_session_key, send_seal_const);
599 dump_data_pw("NTLMSSP send seal key:\n", send_seal_key, 16);
601 arcfour_init(&ntlmssp_state->crypt->ntlm2.sending.seal_state,
604 dump_arc4_state("NTLMSSP send seal arc4 state:\n",
605 &ntlmssp_state->crypt->ntlm2.sending.seal_state);
609 ntlmssp_state->crypt->ntlm2.sending.seq_num = 0;
613 calc_ntlmv2_key(ntlmssp_state->crypt->ntlm2.receiving.sign_key,
614 ntlmssp_state->session_key, recv_sign_const);
615 dump_data_pw("NTLMSSP recv sign key:\n",
616 ntlmssp_state->crypt->ntlm2.receiving.sign_key, 16);
618 /* RECV: seal ARCFOUR pad */
619 calc_ntlmv2_key(recv_seal_key,
620 weak_session_key, recv_seal_const);
621 dump_data_pw("NTLMSSP recv seal key:\n", recv_seal_key, 16);
623 arcfour_init(&ntlmssp_state->crypt->ntlm2.receiving.seal_state,
626 dump_arc4_state("NTLMSSP recv seal arc4 state:\n",
627 &ntlmssp_state->crypt->ntlm2.receiving.seal_state);
631 ntlmssp_state->crypt->ntlm2.receiving.seq_num = 0;
634 uint8_t weak_session_key[8];
635 DATA_BLOB seal_session_key = ntlmssp_state->session_key;
636 bool do_weak = false;
638 DEBUG(5, ("NTLMSSP Sign/Seal - using NTLM1\n"));
641 * Key weakening not performed on the master key for NTLM2
642 * and does not occour for NTLM1. Therefore we only need
643 * to do this for the LM_KEY.
645 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
651 * We certainly don't want to 'extend' the length...
653 if (seal_session_key.length < 16) {
654 /* TODO: is this really correct? */
659 memcpy(weak_session_key, seal_session_key.data, 8);
660 seal_session_key = data_blob_const(weak_session_key, 8);
663 * LM key doesn't support 128 bit crypto, so this is
664 * the best we can do. If you negotiate 128 bit, but
665 * not 56, you end up with 40 bit...
667 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
668 weak_session_key[7] = 0xa0;
669 } else { /* forty bits */
670 weak_session_key[5] = 0xe5;
671 weak_session_key[6] = 0x38;
672 weak_session_key[7] = 0xb0;
676 arcfour_init(&ntlmssp_state->crypt->ntlm.seal_state,
679 dump_arc4_state("NTLMv1 arc4 state:\n",
680 &ntlmssp_state->crypt->ntlm.seal_state);
683 ntlmssp_state->crypt->ntlm.seq_num = 0;
690 NTSTATUS ntlmssp_sign_init(struct ntlmssp_state *ntlmssp_state)
692 if (ntlmssp_state->session_key.length < 8) {
693 DEBUG(3, ("NO session key, cannot initialise signing\n"));
694 return NT_STATUS_NO_USER_SESSION_KEY;
697 ntlmssp_state->crypt = talloc_zero(ntlmssp_state,
698 union ntlmssp_crypt_state);
699 if (ntlmssp_state->crypt == NULL) {
700 return NT_STATUS_NO_MEMORY;
703 return ntlmssp_sign_reset(ntlmssp_state, true);