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,
143 uint8_t *data, size_t length,
144 const DATA_BLOB *sig)
146 uint32_t min_sig_size = 0;
148 uint8_t checksum[32];
149 uint32_t checksum_length = sizeof(checksum_length);
150 uint8_t _confounder[8];
151 uint8_t *confounder = NULL;
152 uint32_t confounder_ofs = 0;
156 netsec_offset_and_sizes(state,
163 if (sig->length < min_sig_size) {
164 return NT_STATUS_ACCESS_DENIED;
168 confounder = _confounder;
169 memcpy(confounder, sig->data+confounder_ofs, 8);
174 RSIVAL(seq_num, 0, state->seq_num);
175 SIVAL(seq_num, 4, state->initiator?0:0x80);
178 netsec_do_seal(state, seq_num,
183 netsec_do_sign(state, confounder,
187 ret = memcmp(checksum, sig->data+16, checksum_length);
189 dump_data_pw("calc digest:", checksum, checksum_length);
190 dump_data_pw("wire digest:", sig->data+16, checksum_length);
191 return NT_STATUS_ACCESS_DENIED;
194 netsec_do_seq_num(state, checksum, checksum_length, seq_num);
196 ret = memcmp(seq_num, sig->data+8, 8);
198 dump_data_pw("calc seq num:", seq_num, 8);
199 dump_data_pw("wire seq num:", sig->data+8, 8);
200 return NT_STATUS_ACCESS_DENIED;
206 uint32_t netsec_outgoing_sig_size(struct schannel_state *state)
208 uint32_t sig_size = 0;
210 netsec_offset_and_sizes(state,
220 NTSTATUS netsec_outgoing_packet(struct schannel_state *state,
223 uint8_t *data, size_t length,
226 uint32_t min_sig_size = 0;
227 uint32_t used_sig_size = 0;
229 uint8_t checksum[32];
230 uint32_t checksum_length = sizeof(checksum_length);
231 uint8_t _confounder[8];
232 uint8_t *confounder = NULL;
233 uint32_t confounder_ofs = 0;
236 netsec_offset_and_sizes(state,
243 RSIVAL(seq_num, 0, state->seq_num);
244 SIVAL(seq_num, 4, state->initiator?0x80:0);
247 confounder = _confounder;
248 generate_random_buffer(confounder, 8);
253 netsec_do_sign(state, confounder,
258 netsec_do_seal(state, seq_num,
263 netsec_do_seq_num(state, checksum, checksum_length, seq_num);
265 (*sig) = data_blob_talloc_zero(mem_ctx, used_sig_size);
267 memcpy(sig->data, header, 8);
268 memcpy(sig->data+8, seq_num, 8);
269 memcpy(sig->data+16, checksum, checksum_length);
272 memcpy(sig->data+confounder_ofs, confounder, 8);
275 dump_data_pw("signature:", sig->data+ 0, 8);
276 dump_data_pw("seq_num :", sig->data+ 8, 8);
277 dump_data_pw("digest :", sig->data+16, checksum_length);
278 dump_data_pw("confound :", sig->data+confounder_ofs, 8);