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 "auth/auth.h"
27 #include "auth/gensec/schannel.h"
29 #define NETSEC_SIGN_SIGNATURE { 0x77, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00 }
30 #define NETSEC_SEAL_SIGNATURE { 0x77, 0x00, 0x7a, 0x00, 0xff, 0xff, 0x00, 0x00 }
32 /*******************************************************************
33 Encode or Decode the sequence number (which is symmetric)
34 ********************************************************************/
35 static void netsec_deal_with_seq_num(struct schannel_state *state,
36 const uint8_t packet_digest[8],
39 static const uint8_t zeros[4];
40 uint8_t sequence_key[16];
43 hmac_md5(state->creds->session_key, zeros, sizeof(zeros), digest1);
44 hmac_md5(digest1, packet_digest, 8, sequence_key);
45 arcfour_crypt(seq_num, sequence_key, 8);
51 /*******************************************************************
52 Calculate the key with which to encode the data payload
53 ********************************************************************/
54 static void netsec_get_sealing_key(const uint8_t session_key[16],
55 const uint8_t seq_num[8],
56 uint8_t sealing_key[16])
58 static const uint8_t zeros[4];
63 for (i = 0; i < 16; i++) {
64 sess_kf0[i] = session_key[i] ^ 0xf0;
67 hmac_md5(sess_kf0, zeros, 4, digest2);
68 hmac_md5(digest2, seq_num, 8, sealing_key);
72 /*******************************************************************
73 Create a digest over the entire packet (including the data), and
74 MD5 it with the session key.
75 ********************************************************************/
76 static void schannel_digest(const uint8_t sess_key[16],
77 const uint8_t netsec_sig[8],
78 const uint8_t *confounder,
79 const uint8_t *data, size_t data_len,
80 uint8_t digest_final[16])
82 uint8_t packet_digest[16];
83 static const uint8_t zeros[4];
84 struct MD5Context ctx;
87 MD5Update(&ctx, zeros, 4);
88 MD5Update(&ctx, netsec_sig, 8);
90 MD5Update(&ctx, confounder, 8);
92 MD5Update(&ctx, data, data_len);
93 MD5Final(packet_digest, &ctx);
95 hmac_md5(sess_key, packet_digest, sizeof(packet_digest), digest_final);
102 NTSTATUS schannel_unseal_packet(struct gensec_security *gensec_security,
104 uint8_t *data, size_t length,
105 const uint8_t *whole_pdu, size_t pdu_length,
106 const DATA_BLOB *sig)
108 struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
110 uint8_t digest_final[16];
111 uint8_t confounder[8];
113 uint8_t sealing_key[16];
114 static const uint8_t netsec_sig[8] = NETSEC_SEAL_SIGNATURE;
116 if (sig->length != 32) {
117 return NT_STATUS_ACCESS_DENIED;
120 memcpy(confounder, sig->data+24, 8);
122 RSIVAL(seq_num, 0, state->seq_num);
123 SIVAL(seq_num, 4, state->initiator?0:0x80);
125 netsec_get_sealing_key(state->creds->session_key, seq_num, sealing_key);
126 arcfour_crypt(confounder, sealing_key, 8);
127 arcfour_crypt(data, sealing_key, length);
129 schannel_digest(state->creds->session_key,
130 netsec_sig, confounder,
131 data, length, digest_final);
133 if (memcmp(digest_final, sig->data+16, 8) != 0) {
134 dump_data_pw("calc digest:", digest_final, 8);
135 dump_data_pw("wire digest:", sig->data+16, 8);
136 return NT_STATUS_ACCESS_DENIED;
139 netsec_deal_with_seq_num(state, digest_final, seq_num);
141 if (memcmp(seq_num, sig->data+8, 8) != 0) {
142 dump_data_pw("calc seq num:", seq_num, 8);
143 dump_data_pw("wire seq num:", sig->data+8, 8);
144 return NT_STATUS_ACCESS_DENIED;
151 check the signature on a packet
153 NTSTATUS schannel_check_packet(struct gensec_security *gensec_security,
155 const uint8_t *data, size_t length,
156 const uint8_t *whole_pdu, size_t pdu_length,
157 const DATA_BLOB *sig)
159 struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
161 uint8_t digest_final[16];
163 static const uint8_t netsec_sig[8] = NETSEC_SIGN_SIGNATURE;
165 /* w2k sends just 24 bytes and skip the confounder */
166 if (sig->length != 32 && sig->length != 24) {
167 return NT_STATUS_ACCESS_DENIED;
170 RSIVAL(seq_num, 0, state->seq_num);
171 SIVAL(seq_num, 4, state->initiator?0:0x80);
173 dump_data_pw("seq_num:\n", seq_num, 8);
174 dump_data_pw("sess_key:\n", state->creds->session_key, 16);
176 schannel_digest(state->creds->session_key,
178 data, length, digest_final);
180 netsec_deal_with_seq_num(state, digest_final, seq_num);
182 if (memcmp(seq_num, sig->data+8, 8) != 0) {
183 dump_data_pw("calc seq num:", seq_num, 8);
184 dump_data_pw("wire seq num:", sig->data+8, 8);
185 return NT_STATUS_ACCESS_DENIED;
188 if (memcmp(digest_final, sig->data+16, 8) != 0) {
189 dump_data_pw("calc digest:", digest_final, 8);
190 dump_data_pw("wire digest:", sig->data+16, 8);
191 return NT_STATUS_ACCESS_DENIED;
201 NTSTATUS schannel_seal_packet(struct gensec_security *gensec_security,
203 uint8_t *data, size_t length,
204 const uint8_t *whole_pdu, size_t pdu_length,
207 struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
209 uint8_t digest_final[16];
210 uint8_t confounder[8];
212 uint8_t sealing_key[16];
213 static const uint8_t netsec_sig[8] = NETSEC_SEAL_SIGNATURE;
215 generate_random_buffer(confounder, 8);
217 RSIVAL(seq_num, 0, state->seq_num);
218 SIVAL(seq_num, 4, state->initiator?0x80:0);
220 schannel_digest(state->creds->session_key,
221 netsec_sig, confounder,
222 data, length, digest_final);
224 netsec_get_sealing_key(state->creds->session_key, seq_num, sealing_key);
225 arcfour_crypt(confounder, sealing_key, 8);
226 arcfour_crypt(data, sealing_key, length);
228 netsec_deal_with_seq_num(state, digest_final, seq_num);
230 (*sig) = data_blob_talloc(mem_ctx, NULL, 32);
232 memcpy(sig->data, netsec_sig, 8);
233 memcpy(sig->data+8, seq_num, 8);
234 memcpy(sig->data+16, digest_final, 8);
235 memcpy(sig->data+24, confounder, 8);
237 dump_data_pw("signature:", sig->data+ 0, 8);
238 dump_data_pw("seq_num :", sig->data+ 8, 8);
239 dump_data_pw("digest :", sig->data+16, 8);
240 dump_data_pw("confound :", sig->data+24, 8);
249 NTSTATUS schannel_sign_packet(struct gensec_security *gensec_security,
251 const uint8_t *data, size_t length,
252 const uint8_t *whole_pdu, size_t pdu_length,
255 struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
257 uint8_t digest_final[16];
259 static const uint8_t netsec_sig[8] = NETSEC_SIGN_SIGNATURE;
261 RSIVAL(seq_num, 0, state->seq_num);
262 SIVAL(seq_num, 4, state->initiator?0x80:0);
264 schannel_digest(state->creds->session_key,
266 data, length, digest_final);
268 netsec_deal_with_seq_num(state, digest_final, seq_num);
270 (*sig) = data_blob_talloc(mem_ctx, NULL, 32);
272 memcpy(sig->data, netsec_sig, 8);
273 memcpy(sig->data+8, seq_num, 8);
274 memcpy(sig->data+16, digest_final, 8);
275 memset(sig->data+24, 0, 8);
277 dump_data_pw("signature:", sig->data+ 0, 8);
278 dump_data_pw("seq_num :", sig->data+ 8, 8);
279 dump_data_pw("digest :", sig->data+16, 8);
280 dump_data_pw("confound :", sig->data+24, 8);