2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2004
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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "lib/crypto/crypto.h"
26 struct schannel_state {
27 uint8_t session_key[16];
32 #define NETSEC_SIGN_SIGNATURE { 0x77, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00 }
33 #define NETSEC_SEAL_SIGNATURE { 0x77, 0x00, 0x7a, 0x00, 0xff, 0xff, 0x00, 0x00 }
35 /*******************************************************************
36 Encode or Decode the sequence number (which is symmetric)
37 ********************************************************************/
38 static void netsec_deal_with_seq_num(struct schannel_state *state,
39 const uint8_t packet_digest[8],
42 static const uint8_t zeros[4];
43 uint8_t sequence_key[16];
46 hmac_md5(state->session_key, zeros, sizeof(zeros), digest1);
47 hmac_md5(digest1, packet_digest, 8, sequence_key);
48 arcfour_crypt(seq_num, sequence_key, 8);
54 /*******************************************************************
55 Calculate the key with which to encode the data payload
56 ********************************************************************/
57 static void netsec_get_sealing_key(const uint8_t session_key[16],
58 const uint8_t seq_num[8],
59 uint8_t sealing_key[16])
61 static const uint8_t zeros[4];
66 for (i = 0; i < 16; i++) {
67 sess_kf0[i] = session_key[i] ^ 0xf0;
70 hmac_md5(sess_kf0, zeros, 4, digest2);
71 hmac_md5(digest2, seq_num, 8, sealing_key);
75 /*******************************************************************
76 Create a digest over the entire packet (including the data), and
77 MD5 it with the session key.
78 ********************************************************************/
79 static void schannel_digest(const uint8_t sess_key[16],
80 const uint8_t netsec_sig[8],
81 const uint8_t *confounder,
82 const uint8_t *data, size_t data_len,
83 uint8_t digest_final[16])
85 uint8_t packet_digest[16];
86 static const uint8_t zeros[4];
87 struct MD5Context ctx;
90 MD5Update(&ctx, zeros, 4);
91 MD5Update(&ctx, netsec_sig, 8);
93 MD5Update(&ctx, confounder, 8);
95 MD5Update(&ctx, data, data_len);
96 MD5Final(packet_digest, &ctx);
98 hmac_md5(sess_key, packet_digest, sizeof(packet_digest), digest_final);
105 NTSTATUS schannel_unseal_packet(struct schannel_state *state,
107 uint8_t *data, size_t length,
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->session_key, seq_num, sealing_key);
126 arcfour_crypt(confounder, sealing_key, 8);
127 arcfour_crypt(data, sealing_key, length);
129 schannel_digest(state->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 schannel_state *state,
154 const uint8_t *data, size_t length,
155 const DATA_BLOB *sig)
157 uint8_t digest_final[16];
159 static const uint8_t netsec_sig[8] = NETSEC_SIGN_SIGNATURE;
161 if (sig->length != 32) {
162 return NT_STATUS_ACCESS_DENIED;
165 RSIVAL(seq_num, 0, state->seq_num);
166 SIVAL(seq_num, 4, state->initiator?0:0x80);
168 dump_data_pw("seq_num:\n", seq_num, 8);
169 dump_data_pw("sess_key:\n", state->session_key, 16);
171 schannel_digest(state->session_key,
173 data, length, digest_final);
175 netsec_deal_with_seq_num(state, digest_final, seq_num);
177 if (memcmp(seq_num, sig->data+8, 8) != 0) {
178 dump_data_pw("calc seq num:", seq_num, 8);
179 dump_data_pw("wire seq num:", sig->data+8, 8);
180 return NT_STATUS_ACCESS_DENIED;
183 if (memcmp(digest_final, sig->data+16, 8) != 0) {
184 dump_data_pw("calc digest:", digest_final, 8);
185 dump_data_pw("wire digest:", sig->data+16, 8);
186 return NT_STATUS_ACCESS_DENIED;
196 NTSTATUS schannel_seal_packet(struct schannel_state *state,
198 uint8_t *data, size_t length,
201 uint8_t digest_final[16];
202 uint8_t confounder[8];
204 uint8_t sealing_key[16];
205 static const uint8_t netsec_sig[8] = NETSEC_SEAL_SIGNATURE;
207 generate_random_buffer(confounder, 8);
209 RSIVAL(seq_num, 0, state->seq_num);
210 SIVAL(seq_num, 4, state->initiator?0x80:0);
212 schannel_digest(state->session_key,
213 netsec_sig, confounder,
214 data, length, digest_final);
216 netsec_get_sealing_key(state->session_key, seq_num, sealing_key);
217 arcfour_crypt(confounder, sealing_key, 8);
218 arcfour_crypt(data, sealing_key, length);
220 netsec_deal_with_seq_num(state, digest_final, seq_num);
222 (*sig) = data_blob_talloc(mem_ctx, NULL, 32);
224 memcpy(sig->data, netsec_sig, 8);
225 memcpy(sig->data+8, seq_num, 8);
226 memcpy(sig->data+16, digest_final, 8);
227 memcpy(sig->data+24, confounder, 8);
229 dump_data_pw("signature:", sig->data+ 0, 8);
230 dump_data_pw("seq_num :", sig->data+ 8, 8);
231 dump_data_pw("digest :", sig->data+16, 8);
232 dump_data_pw("confound :", sig->data+24, 8);
241 NTSTATUS schannel_sign_packet(struct schannel_state *state,
243 const uint8_t *data, size_t length,
246 uint8_t digest_final[16];
248 static const uint8_t netsec_sig[8] = NETSEC_SIGN_SIGNATURE;
250 RSIVAL(seq_num, 0, state->seq_num);
251 SIVAL(seq_num, 4, state->initiator?0x80:0);
253 schannel_digest(state->session_key,
255 data, length, digest_final);
257 netsec_deal_with_seq_num(state, digest_final, seq_num);
259 (*sig) = data_blob_talloc(mem_ctx, NULL, 32);
261 memcpy(sig->data, netsec_sig, 8);
262 memcpy(sig->data+8, seq_num, 8);
263 memcpy(sig->data+16, digest_final, 8);
264 memset(sig->data+24, 0, 8);
266 dump_data_pw("signature:", sig->data+ 0, 8);
267 dump_data_pw("seq_num :", sig->data+ 8, 8);
268 dump_data_pw("digest :", sig->data+16, 8);
269 dump_data_pw("confound :", sig->data+24, 8);
275 destroy an schannel context
277 void schannel_end(struct schannel_state **state)
286 create an schannel context state
288 NTSTATUS schannel_start(struct schannel_state **state,
289 const uint8_t session_key[16],
292 (*state) = talloc_p(NULL, struct schannel_state);
294 return NT_STATUS_NO_MEMORY;
297 memcpy((*state)->session_key, session_key, 16);
298 (*state)->initiator = initiator;
299 (*state)->seq_num = 0;