51b8690c97aed46412e0953003f6cd700766da52
[abartlet/samba.git/.git] / source4 / libcli / auth / schannel.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    schannel library code
5
6    Copyright (C) Andrew Tridgell 2004
7    
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
24 #include "lib/crypto/crypto.h"
25
26 struct schannel_state {
27         uint8_t session_key[16];
28         uint32_t seq_num;
29         BOOL initiator;
30 };
31
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 }
34
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],
40                                      uint8_t seq_num[8])
41 {
42         static const uint8_t zeros[4];
43         uint8_t sequence_key[16];
44         uint8_t digest1[16];
45
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);
49
50         state->seq_num++;
51 }
52
53
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]) 
60 {
61         static const uint8_t zeros[4];
62         uint8_t digest2[16];
63         uint8_t sess_kf0[16];
64         int i;
65
66         for (i = 0; i < 16; i++) {
67                 sess_kf0[i] = session_key[i] ^ 0xf0;
68         }
69         
70         hmac_md5(sess_kf0, zeros, 4, digest2);
71         hmac_md5(digest2, seq_num, 8, sealing_key);
72 }
73
74
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]) 
84 {
85         uint8_t packet_digest[16];
86         static const uint8_t zeros[4];
87         struct MD5Context ctx;
88         
89         MD5Init(&ctx);
90         MD5Update(&ctx, zeros, 4);
91         MD5Update(&ctx, netsec_sig, 8);
92         if (confounder) {
93                 MD5Update(&ctx, confounder, 8);
94         }
95         MD5Update(&ctx, data, data_len);
96         MD5Final(packet_digest, &ctx);
97         
98         hmac_md5(sess_key, packet_digest, sizeof(packet_digest), digest_final);
99 }
100
101
102 /*
103   unseal a packet
104 */
105 NTSTATUS schannel_unseal_packet(struct schannel_state *state,
106                                 TALLOC_CTX *mem_ctx, 
107                                 uint8_t *data, size_t length, 
108                                 DATA_BLOB *sig)
109 {
110         uint8_t digest_final[16];
111         uint8_t confounder[8];
112         uint8_t seq_num[8];
113         uint8_t sealing_key[16];
114         static const uint8_t netsec_sig[8] = NETSEC_SEAL_SIGNATURE;
115
116         if (sig->length != 32) {
117                 return NT_STATUS_ACCESS_DENIED;
118         }
119
120         memcpy(confounder, sig->data+24, 8);
121
122         RSIVAL(seq_num, 0, state->seq_num);
123         SIVAL(seq_num, 4, state->initiator?0:0x80);
124
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);
128
129         schannel_digest(state->session_key, 
130                         netsec_sig, confounder, 
131                         data, length, digest_final);
132
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;
137         }
138
139         netsec_deal_with_seq_num(state, digest_final, seq_num);
140
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;
145         }
146
147         return NT_STATUS_OK;
148 }
149
150 /*
151   check the signature on a packet
152 */
153 NTSTATUS schannel_check_packet(struct schannel_state *state, 
154                                const uint8_t *data, size_t length, 
155                                const DATA_BLOB *sig)
156 {
157         uint8_t digest_final[16];
158         uint8_t seq_num[8];
159         static const uint8_t netsec_sig[8] = NETSEC_SIGN_SIGNATURE;
160
161         if (sig->length != 32) {
162                 return NT_STATUS_ACCESS_DENIED;
163         }
164
165         RSIVAL(seq_num, 0, state->seq_num);
166         SIVAL(seq_num, 4, state->initiator?0:0x80);
167
168         dump_data_pw("seq_num:\n", seq_num, 8);
169         dump_data_pw("sess_key:\n", state->session_key, 16);
170
171         schannel_digest(state->session_key, 
172                         netsec_sig, NULL, 
173                         data, length, digest_final);
174
175         netsec_deal_with_seq_num(state, digest_final, seq_num);
176
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;
181         }
182
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;
187         }
188
189         return NT_STATUS_OK;
190 }
191
192
193 /*
194   seal a packet
195 */
196 NTSTATUS schannel_seal_packet(struct schannel_state *state, 
197                               TALLOC_CTX *mem_ctx, 
198                               uint8_t *data, size_t length, 
199                               DATA_BLOB *sig)
200 {
201         uint8_t digest_final[16];
202         uint8_t confounder[8];
203         uint8_t seq_num[8];
204         uint8_t sealing_key[16];
205         static const uint8_t netsec_sig[8] = NETSEC_SEAL_SIGNATURE;
206
207         generate_random_buffer(confounder, 8);
208
209         RSIVAL(seq_num, 0, state->seq_num);
210         SIVAL(seq_num, 4, state->initiator?0x80:0);
211
212         schannel_digest(state->session_key, 
213                         netsec_sig, confounder, 
214                         data, length, digest_final);
215
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);
219
220         netsec_deal_with_seq_num(state, digest_final, seq_num);
221
222         (*sig) = data_blob_talloc(mem_ctx, NULL, 32);
223
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);
228
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);
233
234         return NT_STATUS_OK;
235 }
236
237
238 /*
239   sign a packet
240 */
241 NTSTATUS schannel_sign_packet(struct schannel_state *state, 
242                               TALLOC_CTX *mem_ctx, 
243                               const uint8_t *data, size_t length, 
244                               DATA_BLOB *sig)
245 {
246         uint8_t digest_final[16];
247         uint8_t seq_num[8];
248         static const uint8_t netsec_sig[8] = NETSEC_SIGN_SIGNATURE;
249
250         RSIVAL(seq_num, 0, state->seq_num);
251         SIVAL(seq_num, 4, state->initiator?0x80:0);
252
253         schannel_digest(state->session_key, 
254                         netsec_sig, NULL, 
255                         data, length, digest_final);
256
257         netsec_deal_with_seq_num(state, digest_final, seq_num);
258
259         (*sig) = data_blob_talloc(mem_ctx, NULL, 32);
260
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);
265
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);
270
271         return NT_STATUS_OK;
272 }
273
274 /*
275   destroy an schannel context
276  */
277 void schannel_end(struct schannel_state **state)
278 {
279         if (*state) {
280                 talloc_free(*state);
281                 (*state) = NULL;
282         }
283 }
284
285 /*
286   create an schannel context state
287 */
288 NTSTATUS schannel_start(struct schannel_state **state,
289                         const uint8_t session_key[16],
290                         BOOL initiator)
291 {
292         (*state) = talloc_p(NULL, struct schannel_state);
293         if (!(*state)) {
294                 return NT_STATUS_NO_MEMORY;
295         }
296
297         memcpy((*state)->session_key, session_key, 16);
298         (*state)->initiator = initiator;
299         (*state)->seq_num = 0;
300
301         return NT_STATUS_OK;
302 }