2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2004
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "../libcli/auth/schannel.h"
25 #include "../lib/crypto/crypto.h"
27 static void netsec_offset_and_sizes(struct schannel_state
*state
,
29 uint32_t *_min_sig_size
,
30 uint32_t *_used_sig_size
,
31 uint32_t *_checksum_length
,
32 uint32_t *_confounder_ofs
)
34 uint32_t min_sig_size
= 24;
35 uint32_t used_sig_size
= 32;
36 uint32_t checksum_length
= 8;
37 uint32_t confounder_ofs
= 24;
44 *_min_sig_size
= min_sig_size
;
48 *_used_sig_size
= used_sig_size
;
51 if (_checksum_length
) {
52 *_checksum_length
= checksum_length
;
55 if (_confounder_ofs
) {
56 *_confounder_ofs
= confounder_ofs
;
60 /*******************************************************************
61 Encode or Decode the sequence number (which is symmetric)
62 ********************************************************************/
63 static void netsec_do_seq_num(struct schannel_state
*state
,
64 const uint8_t *checksum
,
65 uint32_t checksum_length
,
68 static const uint8_t zeros
[4];
69 uint8_t sequence_key
[16];
72 hmac_md5(state
->creds
->session_key
, zeros
, sizeof(zeros
), digest1
);
73 hmac_md5(digest1
, checksum
, checksum_length
, sequence_key
);
74 arcfour_crypt(seq_num
, sequence_key
, 8);
79 static void netsec_do_seal(struct schannel_state
*state
,
80 const uint8_t seq_num
[8],
81 uint8_t confounder
[8],
82 uint8_t *data
, uint32_t length
)
84 uint8_t sealing_key
[16];
85 static const uint8_t zeros
[4];
90 for (i
= 0; i
< 16; i
++) {
91 sess_kf0
[i
] = state
->creds
->session_key
[i
] ^ 0xf0;
94 hmac_md5(sess_kf0
, zeros
, 4, digest2
);
95 hmac_md5(digest2
, seq_num
, 8, sealing_key
);
97 arcfour_crypt(confounder
, sealing_key
, 8);
98 arcfour_crypt(data
, sealing_key
, length
);
101 /*******************************************************************
102 Create a digest over the entire packet (including the data), and
103 MD5 it with the session key.
104 ********************************************************************/
105 static void netsec_do_sign(struct schannel_state
*state
,
106 const uint8_t *confounder
,
107 const uint8_t *data
, size_t data_len
,
111 uint8_t packet_digest
[16];
112 static const uint8_t zeros
[4];
113 struct MD5Context ctx
;
116 MD5Update(&ctx
, zeros
, 4);
118 SSVAL(header
, 0, NL_SIGN_HMAC_MD5
);
119 SSVAL(header
, 2, NL_SEAL_RC4
);
120 SSVAL(header
, 4, 0xFFFF);
121 SSVAL(header
, 6, 0x0000);
123 MD5Update(&ctx
, header
, 8);
124 MD5Update(&ctx
, confounder
, 8);
126 SSVAL(header
, 0, NL_SIGN_HMAC_MD5
);
127 SSVAL(header
, 2, NL_SEAL_NONE
);
128 SSVAL(header
, 4, 0xFFFF);
129 SSVAL(header
, 6, 0x0000);
131 MD5Update(&ctx
, header
, 8);
133 MD5Update(&ctx
, data
, data_len
);
134 MD5Final(packet_digest
, &ctx
);
136 hmac_md5(state
->creds
->session_key
,
137 packet_digest
, sizeof(packet_digest
),
141 NTSTATUS
netsec_incoming_packet(struct schannel_state
*state
,
144 uint8_t *data
, size_t length
,
145 const DATA_BLOB
*sig
)
147 uint32_t min_sig_size
= 0;
149 uint8_t checksum
[32];
150 uint32_t checksum_length
= sizeof(checksum_length
);
151 uint8_t _confounder
[8];
152 uint8_t *confounder
= NULL
;
153 uint32_t confounder_ofs
= 0;
157 netsec_offset_and_sizes(state
,
164 if (sig
->length
< min_sig_size
) {
165 return NT_STATUS_ACCESS_DENIED
;
169 confounder
= _confounder
;
170 memcpy(confounder
, sig
->data
+confounder_ofs
, 8);
175 RSIVAL(seq_num
, 0, state
->seq_num
);
176 SIVAL(seq_num
, 4, state
->initiator
?0:0x80);
179 netsec_do_seal(state
, seq_num
,
184 netsec_do_sign(state
, confounder
,
188 ret
= memcmp(checksum
, sig
->data
+16, checksum_length
);
190 dump_data_pw("calc digest:", checksum
, checksum_length
);
191 dump_data_pw("wire digest:", sig
->data
+16, checksum_length
);
192 return NT_STATUS_ACCESS_DENIED
;
195 netsec_do_seq_num(state
, checksum
, checksum_length
, seq_num
);
197 ret
= memcmp(seq_num
, sig
->data
+8, 8);
199 dump_data_pw("calc seq num:", seq_num
, 8);
200 dump_data_pw("wire seq num:", sig
->data
+8, 8);
201 return NT_STATUS_ACCESS_DENIED
;
207 NTSTATUS
netsec_outgoing_packet(struct schannel_state
*state
,
210 uint8_t *data
, size_t length
,
213 uint32_t min_sig_size
= 0;
214 uint32_t used_sig_size
= 0;
216 uint8_t checksum
[32];
217 uint32_t checksum_length
= sizeof(checksum_length
);
218 uint8_t _confounder
[8];
219 uint8_t *confounder
= NULL
;
220 uint32_t confounder_ofs
= 0;
223 netsec_offset_and_sizes(state
,
230 RSIVAL(seq_num
, 0, state
->seq_num
);
231 SIVAL(seq_num
, 4, state
->initiator
?0x80:0);
234 confounder
= _confounder
;
235 generate_random_buffer(confounder
, 8);
240 netsec_do_sign(state
, confounder
,
245 netsec_do_seal(state
, seq_num
,
250 netsec_do_seq_num(state
, checksum
, checksum_length
, seq_num
);
252 (*sig
) = data_blob_talloc_zero(mem_ctx
, used_sig_size
);
254 memcpy(sig
->data
, header
, 8);
255 memcpy(sig
->data
+8, seq_num
, 8);
256 memcpy(sig
->data
+16, checksum
, checksum_length
);
259 memcpy(sig
->data
+confounder_ofs
, confounder
, 8);
262 dump_data_pw("signature:", sig
->data
+ 0, 8);
263 dump_data_pw("seq_num :", sig
->data
+ 8, 8);
264 dump_data_pw("digest :", sig
->data
+16, checksum_length
);
265 dump_data_pw("confound :", sig
->data
+confounder_ofs
, 8);