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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "auth/auth.h"
24 #include "auth/ntlmssp/ntlmssp.h"
25 #include "auth/ntlmssp/msrpc_parse.h"
26 #include "../lib/crypto/crypto.h"
27 #include "auth/credentials/credentials.h"
28 #include "auth/gensec/gensec.h"
30 #define CLI_SIGN "session key to client-to-server signing key magic constant"
31 #define CLI_SEAL "session key to client-to-server sealing key magic constant"
32 #define SRV_SIGN "session key to server-to-client signing key magic constant"
33 #define SRV_SEAL "session key to server-to-client sealing key magic constant"
36 * Some notes on the NTLM2 code:
38 * NTLM2 is a AEAD system. This means that the data encrypted is not
39 * all the data that is signed. In DCE-RPC case, the headers of the
40 * DCE-RPC packets are also signed. This prevents some of the
41 * fun-and-games one might have by changing them.
45 static void calc_ntlmv2_key(TALLOC_CTX
*mem_ctx
,
47 DATA_BLOB session_key
,
50 struct MD5Context ctx3
;
51 *subkey
= data_blob_talloc(mem_ctx
, NULL
, 16);
53 MD5Update(&ctx3
, session_key
.data
, session_key
.length
);
54 MD5Update(&ctx3
, (const uint8_t *)constant
, strlen(constant
)+1);
55 MD5Final(subkey
->data
, &ctx3
);
58 enum ntlmssp_direction
{
63 static NTSTATUS
ntlmssp_make_packet_signature(struct gensec_ntlmssp_state
*gensec_ntlmssp_state
,
64 TALLOC_CTX
*sig_mem_ctx
,
65 const uint8_t *data
, size_t length
,
66 const uint8_t *whole_pdu
, size_t pdu_length
,
67 enum ntlmssp_direction direction
,
68 DATA_BLOB
*sig
, bool encrypt_sig
)
70 if (gensec_ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_NTLM2
) {
76 *sig
= data_blob_talloc(sig_mem_ctx
, NULL
, NTLMSSP_SIG_SIZE
);
78 return NT_STATUS_NO_MEMORY
;
83 SIVAL(seq_num
, 0, gensec_ntlmssp_state
->crypt
.ntlm2
.send_seq_num
);
84 gensec_ntlmssp_state
->crypt
.ntlm2
.send_seq_num
++;
85 hmac_md5_init_limK_to_64(gensec_ntlmssp_state
->crypt
.ntlm2
.send_sign_key
.data
,
86 gensec_ntlmssp_state
->crypt
.ntlm2
.send_sign_key
.length
, &ctx
);
89 SIVAL(seq_num
, 0, gensec_ntlmssp_state
->crypt
.ntlm2
.recv_seq_num
);
90 gensec_ntlmssp_state
->crypt
.ntlm2
.recv_seq_num
++;
91 hmac_md5_init_limK_to_64(gensec_ntlmssp_state
->crypt
.ntlm2
.recv_sign_key
.data
,
92 gensec_ntlmssp_state
->crypt
.ntlm2
.recv_sign_key
.length
, &ctx
);
95 hmac_md5_update(seq_num
, sizeof(seq_num
), &ctx
);
96 hmac_md5_update(whole_pdu
, pdu_length
, &ctx
);
97 hmac_md5_final(digest
, &ctx
);
99 if (encrypt_sig
&& gensec_ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_KEY_EXCH
) {
102 arcfour_crypt_sbox(gensec_ntlmssp_state
->crypt
.ntlm2
.send_seal_arcfour_state
, digest
, 8);
104 case NTLMSSP_RECEIVE
:
105 arcfour_crypt_sbox(gensec_ntlmssp_state
->crypt
.ntlm2
.recv_seal_arcfour_state
, digest
, 8);
110 SIVAL(sig
->data
, 0, NTLMSSP_SIGN_VERSION
);
111 memcpy(sig
->data
+ 4, digest
, 8);
112 memcpy(sig
->data
+ 12, seq_num
, 4);
114 DEBUG(10, ("NTLM2: created signature over %llu bytes of input:\n", (unsigned long long)pdu_length
));
115 dump_data(11, sig
->data
, sig
->length
);
119 crc
= crc32_calc_buffer(data
, length
);
120 if (!msrpc_gen(sig_mem_ctx
,
121 sig
, "dddd", NTLMSSP_SIGN_VERSION
, 0, crc
, gensec_ntlmssp_state
->crypt
.ntlm
.seq_num
)) {
122 return NT_STATUS_NO_MEMORY
;
124 gensec_ntlmssp_state
->crypt
.ntlm
.seq_num
++;
126 arcfour_crypt_sbox(gensec_ntlmssp_state
->crypt
.ntlm
.arcfour_state
, sig
->data
+4, sig
->length
-4);
128 DEBUG(10, ("NTLM1: created signature over %llu bytes of input:\n", (unsigned long long)length
));
129 dump_data(11, sig
->data
, sig
->length
);
134 /* TODO: make this non-public */
135 NTSTATUS
gensec_ntlmssp_sign_packet(struct gensec_security
*gensec_security
,
136 TALLOC_CTX
*sig_mem_ctx
,
137 const uint8_t *data
, size_t length
,
138 const uint8_t *whole_pdu
, size_t pdu_length
,
141 struct gensec_ntlmssp_state
*gensec_ntlmssp_state
= (struct gensec_ntlmssp_state
*)gensec_security
->private_data
;
143 return ntlmssp_make_packet_signature(gensec_ntlmssp_state
, sig_mem_ctx
,
145 whole_pdu
, pdu_length
,
146 NTLMSSP_SEND
, sig
, true);
150 * Check the signature of an incoming packet
154 NTSTATUS
gensec_ntlmssp_check_packet(struct gensec_security
*gensec_security
,
155 TALLOC_CTX
*sig_mem_ctx
,
156 const uint8_t *data
, size_t length
,
157 const uint8_t *whole_pdu
, size_t pdu_length
,
158 const DATA_BLOB
*sig
)
160 struct gensec_ntlmssp_state
*gensec_ntlmssp_state
= (struct gensec_ntlmssp_state
*)gensec_security
->private_data
;
165 if (!gensec_ntlmssp_state
->session_key
.length
) {
166 DEBUG(3, ("NO session key, cannot check packet signature\n"));
167 return NT_STATUS_NO_USER_SESSION_KEY
;
170 nt_status
= ntlmssp_make_packet_signature(gensec_ntlmssp_state
, sig_mem_ctx
,
172 whole_pdu
, pdu_length
,
173 NTLMSSP_RECEIVE
, &local_sig
, true);
175 if (!NT_STATUS_IS_OK(nt_status
)) {
176 DEBUG(0, ("NTLMSSP packet check failed with %s\n", nt_errstr(nt_status
)));
180 if (gensec_ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_NTLM2
) {
181 if (local_sig
.length
!= sig
->length
||
182 memcmp(local_sig
.data
,
183 sig
->data
, sig
->length
) != 0) {
184 DEBUG(5, ("BAD SIG NTLM2: wanted signature over %llu bytes of input:\n", (unsigned long long)pdu_length
));
185 dump_data(5, local_sig
.data
, local_sig
.length
);
187 DEBUG(5, ("BAD SIG: got signature over %llu bytes of input:\n", (unsigned long long)pdu_length
));
188 dump_data(5, sig
->data
, sig
->length
);
190 DEBUG(1, ("NTLMSSP NTLM2 packet check failed due to invalid signature on %llu bytes of input!\n", (unsigned long long)pdu_length
));
191 return NT_STATUS_ACCESS_DENIED
;
194 if (local_sig
.length
!= sig
->length
||
195 memcmp(local_sig
.data
+ 8,
196 sig
->data
+ 8, sig
->length
- 8) != 0) {
197 DEBUG(5, ("BAD SIG NTLM1: wanted signature of %llu bytes of input:\n", (unsigned long long)length
));
198 dump_data(5, local_sig
.data
, local_sig
.length
);
200 DEBUG(5, ("BAD SIG: got signature of %llu bytes of input:\n", (unsigned long long)length
));
201 dump_data(5, sig
->data
, sig
->length
);
203 DEBUG(1, ("NTLMSSP NTLM1 packet check failed due to invalid signature on %llu bytes of input:\n", (unsigned long long)length
));
204 return NT_STATUS_ACCESS_DENIED
;
207 dump_data_pw("checked ntlmssp signature\n", sig
->data
, sig
->length
);
214 * Seal data with the NTLMSSP algorithm
218 NTSTATUS
gensec_ntlmssp_seal_packet(struct gensec_security
*gensec_security
,
219 TALLOC_CTX
*sig_mem_ctx
,
220 uint8_t *data
, size_t length
,
221 const uint8_t *whole_pdu
, size_t pdu_length
,
224 struct gensec_ntlmssp_state
*gensec_ntlmssp_state
= (struct gensec_ntlmssp_state
*)gensec_security
->private_data
;
226 if (!gensec_ntlmssp_state
->session_key
.length
) {
227 DEBUG(3, ("NO session key, cannot seal packet\n"));
228 return NT_STATUS_NO_USER_SESSION_KEY
;
231 DEBUG(10,("ntlmssp_seal_data: seal\n"));
232 dump_data_pw("ntlmssp clear data\n", data
, length
);
233 if (gensec_ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_NTLM2
) {
234 /* The order of these two operations matters - we must first seal the packet,
235 then seal the sequence number - this is becouse the send_seal_hash is not
236 constant, but is is rather updated with each iteration */
237 nt_status
= ntlmssp_make_packet_signature(gensec_ntlmssp_state
, sig_mem_ctx
,
239 whole_pdu
, pdu_length
,
240 NTLMSSP_SEND
, sig
, false);
241 arcfour_crypt_sbox(gensec_ntlmssp_state
->crypt
.ntlm2
.send_seal_arcfour_state
, data
, length
);
242 if (gensec_ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_KEY_EXCH
) {
243 arcfour_crypt_sbox(gensec_ntlmssp_state
->crypt
.ntlm2
.send_seal_arcfour_state
, sig
->data
+4, 8);
247 crc
= crc32_calc_buffer(data
, length
);
248 if (!msrpc_gen(sig_mem_ctx
,
249 sig
, "dddd", NTLMSSP_SIGN_VERSION
, 0, crc
, gensec_ntlmssp_state
->crypt
.ntlm
.seq_num
)) {
250 return NT_STATUS_NO_MEMORY
;
253 /* The order of these two operations matters - we must
254 first seal the packet, then seal the sequence
255 number - this is becouse the ntlmssp_hash is not
256 constant, but is is rather updated with each
259 arcfour_crypt_sbox(gensec_ntlmssp_state
->crypt
.ntlm
.arcfour_state
, data
, length
);
260 arcfour_crypt_sbox(gensec_ntlmssp_state
->crypt
.ntlm
.arcfour_state
, sig
->data
+4, sig
->length
-4);
261 /* increment counter on send */
262 gensec_ntlmssp_state
->crypt
.ntlm
.seq_num
++;
263 nt_status
= NT_STATUS_OK
;
265 dump_data_pw("ntlmssp signature\n", sig
->data
, sig
->length
);
266 dump_data_pw("ntlmssp sealed data\n", data
, length
);
273 * Unseal data with the NTLMSSP algorithm
278 wrappers for the ntlmssp_*() functions
280 NTSTATUS
gensec_ntlmssp_unseal_packet(struct gensec_security
*gensec_security
,
281 TALLOC_CTX
*sig_mem_ctx
,
282 uint8_t *data
, size_t length
,
283 const uint8_t *whole_pdu
, size_t pdu_length
,
284 const DATA_BLOB
*sig
)
286 struct gensec_ntlmssp_state
*gensec_ntlmssp_state
= (struct gensec_ntlmssp_state
*)gensec_security
->private_data
;
287 if (!gensec_ntlmssp_state
->session_key
.length
) {
288 DEBUG(3, ("NO session key, cannot unseal packet\n"));
289 return NT_STATUS_NO_USER_SESSION_KEY
;
292 dump_data_pw("ntlmssp sealed data\n", data
, length
);
293 if (gensec_ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_NTLM2
) {
294 arcfour_crypt_sbox(gensec_ntlmssp_state
->crypt
.ntlm2
.recv_seal_arcfour_state
, data
, length
);
296 arcfour_crypt_sbox(gensec_ntlmssp_state
->crypt
.ntlm
.arcfour_state
, data
, length
);
298 dump_data_pw("ntlmssp clear data\n", data
, length
);
299 return gensec_ntlmssp_check_packet(gensec_security
, sig_mem_ctx
, data
, length
, whole_pdu
, pdu_length
, sig
);
303 Initialise the state for NTLMSSP signing.
305 /* TODO: make this non-public */
306 NTSTATUS
ntlmssp_sign_init(struct gensec_ntlmssp_state
*gensec_ntlmssp_state
)
308 TALLOC_CTX
*mem_ctx
= talloc_new(gensec_ntlmssp_state
);
311 return NT_STATUS_NO_MEMORY
;
314 DEBUG(3, ("NTLMSSP Sign/Seal - Initialising with flags:\n"));
315 debug_ntlmssp_flags(gensec_ntlmssp_state
->neg_flags
);
317 if (gensec_ntlmssp_state
->session_key
.length
< 8) {
318 talloc_free(mem_ctx
);
319 DEBUG(3, ("NO session key, cannot intialise signing\n"));
320 return NT_STATUS_NO_USER_SESSION_KEY
;
323 if (gensec_ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_NTLM2
)
325 DATA_BLOB weak_session_key
= gensec_ntlmssp_state
->session_key
;
326 const char *send_sign_const
;
327 const char *send_seal_const
;
328 const char *recv_sign_const
;
329 const char *recv_seal_const
;
331 DATA_BLOB send_seal_key
;
332 DATA_BLOB recv_seal_key
;
334 switch (gensec_ntlmssp_state
->role
) {
336 send_sign_const
= CLI_SIGN
;
337 send_seal_const
= CLI_SEAL
;
338 recv_sign_const
= SRV_SIGN
;
339 recv_seal_const
= SRV_SEAL
;
342 send_sign_const
= SRV_SIGN
;
343 send_seal_const
= SRV_SEAL
;
344 recv_sign_const
= CLI_SIGN
;
345 recv_seal_const
= CLI_SEAL
;
348 talloc_free(mem_ctx
);
349 return NT_STATUS_INTERNAL_ERROR
;
352 gensec_ntlmssp_state
->crypt
.ntlm2
.send_seal_arcfour_state
= talloc(gensec_ntlmssp_state
, struct arcfour_state
);
353 NT_STATUS_HAVE_NO_MEMORY(gensec_ntlmssp_state
->crypt
.ntlm2
.send_seal_arcfour_state
);
354 gensec_ntlmssp_state
->crypt
.ntlm2
.recv_seal_arcfour_state
= talloc(gensec_ntlmssp_state
, struct arcfour_state
);
355 NT_STATUS_HAVE_NO_MEMORY(gensec_ntlmssp_state
->crypt
.ntlm2
.send_seal_arcfour_state
);
358 Weaken NTLMSSP keys to cope with down-level
359 clients, servers and export restrictions.
361 We probably should have some parameters to control
362 this, once we get NTLM2 working.
365 /* Key weakening was not performed on the master key
366 * for NTLM2 (in ntlmssp_weaken_keys()), but must be
367 * done on the encryption subkeys only. That is why
368 * we don't have this code for the ntlmv1 case.
371 if (gensec_ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_128
) {
373 } else if (gensec_ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_56
) {
374 weak_session_key
.length
= 7;
375 } else { /* forty bits */
376 weak_session_key
.length
= 5;
378 dump_data_pw("NTLMSSP weakend master key:\n",
379 weak_session_key
.data
,
380 weak_session_key
.length
);
383 calc_ntlmv2_key(gensec_ntlmssp_state
,
384 &gensec_ntlmssp_state
->crypt
.ntlm2
.send_sign_key
,
385 gensec_ntlmssp_state
->session_key
, send_sign_const
);
386 dump_data_pw("NTLMSSP send sign key:\n",
387 gensec_ntlmssp_state
->crypt
.ntlm2
.send_sign_key
.data
,
388 gensec_ntlmssp_state
->crypt
.ntlm2
.send_sign_key
.length
);
390 /* SEND: seal ARCFOUR pad */
391 calc_ntlmv2_key(mem_ctx
,
393 weak_session_key
, send_seal_const
);
394 dump_data_pw("NTLMSSP send seal key:\n",
396 send_seal_key
.length
);
397 arcfour_init(gensec_ntlmssp_state
->crypt
.ntlm2
.send_seal_arcfour_state
,
399 dump_data_pw("NTLMSSP send sesl hash:\n",
400 gensec_ntlmssp_state
->crypt
.ntlm2
.send_seal_arcfour_state
->sbox
,
401 sizeof(gensec_ntlmssp_state
->crypt
.ntlm2
.send_seal_arcfour_state
->sbox
));
404 calc_ntlmv2_key(gensec_ntlmssp_state
,
405 &gensec_ntlmssp_state
->crypt
.ntlm2
.recv_sign_key
,
406 gensec_ntlmssp_state
->session_key
, recv_sign_const
);
407 dump_data_pw("NTLMSSP recv sign key:\n",
408 gensec_ntlmssp_state
->crypt
.ntlm2
.recv_sign_key
.data
,
409 gensec_ntlmssp_state
->crypt
.ntlm2
.recv_sign_key
.length
);
411 /* RECV: seal ARCFOUR pad */
412 calc_ntlmv2_key(mem_ctx
,
414 weak_session_key
, recv_seal_const
);
415 dump_data_pw("NTLMSSP recv seal key:\n",
417 recv_seal_key
.length
);
418 arcfour_init(gensec_ntlmssp_state
->crypt
.ntlm2
.recv_seal_arcfour_state
,
420 dump_data_pw("NTLMSSP receive seal hash:\n",
421 gensec_ntlmssp_state
->crypt
.ntlm2
.recv_seal_arcfour_state
->sbox
,
422 sizeof(gensec_ntlmssp_state
->crypt
.ntlm2
.recv_seal_arcfour_state
->sbox
));
424 gensec_ntlmssp_state
->crypt
.ntlm2
.send_seq_num
= 0;
425 gensec_ntlmssp_state
->crypt
.ntlm2
.recv_seq_num
= 0;
428 DATA_BLOB weak_session_key
= ntlmssp_weakend_key(gensec_ntlmssp_state
, mem_ctx
);
429 DEBUG(5, ("NTLMSSP Sign/Seal - using NTLM1\n"));
431 gensec_ntlmssp_state
->crypt
.ntlm
.arcfour_state
= talloc(gensec_ntlmssp_state
, struct arcfour_state
);
432 NT_STATUS_HAVE_NO_MEMORY(gensec_ntlmssp_state
->crypt
.ntlm
.arcfour_state
);
434 arcfour_init(gensec_ntlmssp_state
->crypt
.ntlm
.arcfour_state
,
436 dump_data_pw("NTLMSSP hash:\n", gensec_ntlmssp_state
->crypt
.ntlm
.arcfour_state
->sbox
,
437 sizeof(gensec_ntlmssp_state
->crypt
.ntlm
.arcfour_state
->sbox
));
439 gensec_ntlmssp_state
->crypt
.ntlm
.seq_num
= 0;
442 talloc_free(mem_ctx
);
446 size_t gensec_ntlmssp_sig_size(struct gensec_security
*gensec_security
, size_t data_size
)
448 return NTLMSSP_SIG_SIZE
;
451 NTSTATUS
gensec_ntlmssp_wrap(struct gensec_security
*gensec_security
,
452 TALLOC_CTX
*sig_mem_ctx
,
459 if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
)) {
461 *out
= data_blob_talloc(sig_mem_ctx
, NULL
, in
->length
+ NTLMSSP_SIG_SIZE
);
463 return NT_STATUS_NO_MEMORY
;
465 memcpy(out
->data
+ NTLMSSP_SIG_SIZE
, in
->data
, in
->length
);
467 nt_status
= gensec_ntlmssp_seal_packet(gensec_security
, sig_mem_ctx
,
468 out
->data
+ NTLMSSP_SIG_SIZE
,
469 out
->length
- NTLMSSP_SIG_SIZE
,
470 out
->data
+ NTLMSSP_SIG_SIZE
,
471 out
->length
- NTLMSSP_SIG_SIZE
,
474 if (NT_STATUS_IS_OK(nt_status
)) {
475 memcpy(out
->data
, sig
.data
, NTLMSSP_SIG_SIZE
);
479 } else if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SIGN
)) {
481 *out
= data_blob_talloc(sig_mem_ctx
, NULL
, in
->length
+ NTLMSSP_SIG_SIZE
);
483 return NT_STATUS_NO_MEMORY
;
485 memcpy(out
->data
+ NTLMSSP_SIG_SIZE
, in
->data
, in
->length
);
487 nt_status
= gensec_ntlmssp_sign_packet(gensec_security
, sig_mem_ctx
,
488 out
->data
+ NTLMSSP_SIG_SIZE
,
489 out
->length
- NTLMSSP_SIG_SIZE
,
490 out
->data
+ NTLMSSP_SIG_SIZE
,
491 out
->length
- NTLMSSP_SIG_SIZE
,
494 if (NT_STATUS_IS_OK(nt_status
)) {
495 memcpy(out
->data
, sig
.data
, NTLMSSP_SIG_SIZE
);
506 NTSTATUS
gensec_ntlmssp_unwrap(struct gensec_security
*gensec_security
,
507 TALLOC_CTX
*sig_mem_ctx
,
513 if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
)) {
514 if (in
->length
< NTLMSSP_SIG_SIZE
) {
515 return NT_STATUS_INVALID_PARAMETER
;
518 sig
.length
= NTLMSSP_SIG_SIZE
;
520 *out
= data_blob_talloc(sig_mem_ctx
, in
->data
+ NTLMSSP_SIG_SIZE
, in
->length
- NTLMSSP_SIG_SIZE
);
522 return gensec_ntlmssp_unseal_packet(gensec_security
, sig_mem_ctx
,
523 out
->data
, out
->length
,
524 out
->data
, out
->length
,
527 } else if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SIGN
)) {
528 if (in
->length
< NTLMSSP_SIG_SIZE
) {
529 return NT_STATUS_INVALID_PARAMETER
;
532 sig
.length
= NTLMSSP_SIG_SIZE
;
534 *out
= data_blob_talloc(sig_mem_ctx
, in
->data
+ NTLMSSP_SIG_SIZE
, in
->length
- NTLMSSP_SIG_SIZE
);
536 return gensec_ntlmssp_check_packet(gensec_security
, sig_mem_ctx
,
537 out
->data
, out
->length
,
538 out
->data
, out
->length
,