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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "lib/crypto/crypto.h"
26 #include "libcli/auth/schannel.h"
27 #include "libcli/auth/gensec.h"
28 #include "libcli/auth/credentials.h"
30 #define NETSEC_SIGN_SIGNATURE { 0x77, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00 }
31 #define NETSEC_SEAL_SIGNATURE { 0x77, 0x00, 0x7a, 0x00, 0xff, 0xff, 0x00, 0x00 }
33 /*******************************************************************
34 Encode or Decode the sequence number (which is symmetric)
35 ********************************************************************/
36 static void netsec_deal_with_seq_num(struct schannel_state *state,
37 const uint8_t packet_digest[8],
40 static const uint8_t zeros[4];
41 uint8_t sequence_key[16];
44 hmac_md5(state->creds->session_key, zeros, sizeof(zeros), digest1);
45 hmac_md5(digest1, packet_digest, 8, sequence_key);
46 arcfour_crypt(seq_num, sequence_key, 8);
52 /*******************************************************************
53 Calculate the key with which to encode the data payload
54 ********************************************************************/
55 static void netsec_get_sealing_key(const uint8_t session_key[16],
56 const uint8_t seq_num[8],
57 uint8_t sealing_key[16])
59 static const uint8_t zeros[4];
64 for (i = 0; i < 16; i++) {
65 sess_kf0[i] = session_key[i] ^ 0xf0;
68 hmac_md5(sess_kf0, zeros, 4, digest2);
69 hmac_md5(digest2, seq_num, 8, sealing_key);
73 /*******************************************************************
74 Create a digest over the entire packet (including the data), and
75 MD5 it with the session key.
76 ********************************************************************/
77 static void schannel_digest(const uint8_t sess_key[16],
78 const uint8_t netsec_sig[8],
79 const uint8_t *confounder,
80 const uint8_t *data, size_t data_len,
81 uint8_t digest_final[16])
83 uint8_t packet_digest[16];
84 static const uint8_t zeros[4];
85 struct MD5Context ctx;
88 MD5Update(&ctx, zeros, 4);
89 MD5Update(&ctx, netsec_sig, 8);
91 MD5Update(&ctx, confounder, 8);
93 MD5Update(&ctx, data, data_len);
94 MD5Final(packet_digest, &ctx);
96 hmac_md5(sess_key, packet_digest, sizeof(packet_digest), digest_final);
103 NTSTATUS schannel_unseal_packet(struct gensec_security *gensec_security,
105 uint8_t *data, size_t length,
106 const uint8_t *whole_pdu, size_t pdu_length,
109 struct schannel_state *state = gensec_security->private_data;
111 uint8_t digest_final[16];
112 uint8_t confounder[8];
114 uint8_t sealing_key[16];
115 static const uint8_t netsec_sig[8] = NETSEC_SEAL_SIGNATURE;
117 if (sig->length != 32) {
118 return NT_STATUS_ACCESS_DENIED;
121 memcpy(confounder, sig->data+24, 8);
123 RSIVAL(seq_num, 0, state->seq_num);
124 SIVAL(seq_num, 4, state->initiator?0:0x80);
126 netsec_get_sealing_key(state->creds->session_key, seq_num, sealing_key);
127 arcfour_crypt(confounder, sealing_key, 8);
128 arcfour_crypt(data, sealing_key, length);
130 schannel_digest(state->creds->session_key,
131 netsec_sig, confounder,
132 data, length, digest_final);
134 if (memcmp(digest_final, sig->data+16, 8) != 0) {
135 dump_data_pw("calc digest:", digest_final, 8);
136 dump_data_pw("wire digest:", sig->data+16, 8);
137 return NT_STATUS_ACCESS_DENIED;
140 netsec_deal_with_seq_num(state, digest_final, seq_num);
142 if (memcmp(seq_num, sig->data+8, 8) != 0) {
143 dump_data_pw("calc seq num:", seq_num, 8);
144 dump_data_pw("wire seq num:", sig->data+8, 8);
145 return NT_STATUS_ACCESS_DENIED;
152 check the signature on a packet
154 NTSTATUS schannel_check_packet(struct gensec_security *gensec_security,
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 schannel_state *state = gensec_security->private_data;
162 uint8_t digest_final[16];
164 static const uint8_t netsec_sig[8] = NETSEC_SIGN_SIGNATURE;
166 /* w2k sends just 24 bytes and skip the confounder */
167 if (sig->length != 32 && sig->length != 24) {
168 return NT_STATUS_ACCESS_DENIED;
171 RSIVAL(seq_num, 0, state->seq_num);
172 SIVAL(seq_num, 4, state->initiator?0:0x80);
174 dump_data_pw("seq_num:\n", seq_num, 8);
175 dump_data_pw("sess_key:\n", state->creds->session_key, 16);
177 schannel_digest(state->creds->session_key,
179 data, length, digest_final);
181 netsec_deal_with_seq_num(state, digest_final, seq_num);
183 if (memcmp(seq_num, sig->data+8, 8) != 0) {
184 dump_data_pw("calc seq num:", seq_num, 8);
185 dump_data_pw("wire seq num:", sig->data+8, 8);
186 return NT_STATUS_ACCESS_DENIED;
189 if (memcmp(digest_final, sig->data+16, 8) != 0) {
190 dump_data_pw("calc digest:", digest_final, 8);
191 dump_data_pw("wire digest:", sig->data+16, 8);
192 return NT_STATUS_ACCESS_DENIED;
202 NTSTATUS schannel_seal_packet(struct gensec_security *gensec_security,
204 uint8_t *data, size_t length,
205 const uint8_t *whole_pdu, size_t pdu_length,
208 struct schannel_state *state = gensec_security->private_data;
210 uint8_t digest_final[16];
211 uint8_t confounder[8];
213 uint8_t sealing_key[16];
214 static const uint8_t netsec_sig[8] = NETSEC_SEAL_SIGNATURE;
216 generate_random_buffer(confounder, 8);
218 RSIVAL(seq_num, 0, state->seq_num);
219 SIVAL(seq_num, 4, state->initiator?0x80:0);
221 schannel_digest(state->creds->session_key,
222 netsec_sig, confounder,
223 data, length, digest_final);
225 netsec_get_sealing_key(state->creds->session_key, seq_num, sealing_key);
226 arcfour_crypt(confounder, sealing_key, 8);
227 arcfour_crypt(data, sealing_key, length);
229 netsec_deal_with_seq_num(state, digest_final, seq_num);
231 (*sig) = data_blob_talloc(mem_ctx, NULL, 32);
233 memcpy(sig->data, netsec_sig, 8);
234 memcpy(sig->data+8, seq_num, 8);
235 memcpy(sig->data+16, digest_final, 8);
236 memcpy(sig->data+24, confounder, 8);
238 dump_data_pw("signature:", sig->data+ 0, 8);
239 dump_data_pw("seq_num :", sig->data+ 8, 8);
240 dump_data_pw("digest :", sig->data+16, 8);
241 dump_data_pw("confound :", sig->data+24, 8);
250 NTSTATUS schannel_sign_packet(struct gensec_security *gensec_security,
252 const uint8_t *data, size_t length,
253 const uint8_t *whole_pdu, size_t pdu_length,
256 struct schannel_state *state = gensec_security->private_data;
258 uint8_t digest_final[16];
260 static const uint8_t netsec_sig[8] = NETSEC_SIGN_SIGNATURE;
262 RSIVAL(seq_num, 0, state->seq_num);
263 SIVAL(seq_num, 4, state->initiator?0x80:0);
265 schannel_digest(state->creds->session_key,
267 data, length, digest_final);
269 netsec_deal_with_seq_num(state, digest_final, seq_num);
271 (*sig) = data_blob_talloc(mem_ctx, NULL, 32);
273 memcpy(sig->data, netsec_sig, 8);
274 memcpy(sig->data+8, seq_num, 8);
275 memcpy(sig->data+16, digest_final, 8);
276 memset(sig->data+24, 0, 8);
278 dump_data_pw("signature:", sig->data+ 0, 8);
279 dump_data_pw("seq_num :", sig->data+ 8, 8);
280 dump_data_pw("digest :", sig->data+16, 8);
281 dump_data_pw("confound :", sig->data+24, 8);