r6028: A MAJOR update to intergrate the new credentails system fully with
[samba.git] / source4 / libcli / auth / schannel_sign.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    schannel library code
5
6    Copyright (C) Andrew Tridgell 2004
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
8    
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.
13    
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.
18    
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.
22 */
23
24 #include "includes.h"
25 #include "lib/crypto/crypto.h"
26 #include "libcli/auth/schannel.h"
27 #include "libcli/auth/gensec.h"
28 #include "libcli/auth/credentials.h"
29
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 }
32
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],
38                                      uint8_t seq_num[8])
39 {
40         static const uint8_t zeros[4];
41         uint8_t sequence_key[16];
42         uint8_t digest1[16];
43
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);
47
48         state->seq_num++;
49 }
50
51
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]) 
58 {
59         static const uint8_t zeros[4];
60         uint8_t digest2[16];
61         uint8_t sess_kf0[16];
62         int i;
63
64         for (i = 0; i < 16; i++) {
65                 sess_kf0[i] = session_key[i] ^ 0xf0;
66         }
67         
68         hmac_md5(sess_kf0, zeros, 4, digest2);
69         hmac_md5(digest2, seq_num, 8, sealing_key);
70 }
71
72
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]) 
82 {
83         uint8_t packet_digest[16];
84         static const uint8_t zeros[4];
85         struct MD5Context ctx;
86         
87         MD5Init(&ctx);
88         MD5Update(&ctx, zeros, 4);
89         MD5Update(&ctx, netsec_sig, 8);
90         if (confounder) {
91                 MD5Update(&ctx, confounder, 8);
92         }
93         MD5Update(&ctx, data, data_len);
94         MD5Final(packet_digest, &ctx);
95         
96         hmac_md5(sess_key, packet_digest, sizeof(packet_digest), digest_final);
97 }
98
99
100 /*
101   unseal a packet
102 */
103 NTSTATUS schannel_unseal_packet(struct gensec_security *gensec_security, 
104                                 TALLOC_CTX *mem_ctx, 
105                                 uint8_t *data, size_t length, 
106                                 const uint8_t *whole_pdu, size_t pdu_length, 
107                                 DATA_BLOB *sig)
108 {
109         struct schannel_state *state = gensec_security->private_data;
110         
111         uint8_t digest_final[16];
112         uint8_t confounder[8];
113         uint8_t seq_num[8];
114         uint8_t sealing_key[16];
115         static const uint8_t netsec_sig[8] = NETSEC_SEAL_SIGNATURE;
116
117         if (sig->length != 32) {
118                 return NT_STATUS_ACCESS_DENIED;
119         }
120
121         memcpy(confounder, sig->data+24, 8);
122
123         RSIVAL(seq_num, 0, state->seq_num);
124         SIVAL(seq_num, 4, state->initiator?0:0x80);
125
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);
129
130         schannel_digest(state->creds->session_key, 
131                         netsec_sig, confounder, 
132                         data, length, digest_final);
133
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;
138         }
139
140         netsec_deal_with_seq_num(state, digest_final, seq_num);
141
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;
146         }
147
148         return NT_STATUS_OK;
149 }
150
151 /*
152   check the signature on a packet
153 */
154 NTSTATUS schannel_check_packet(struct gensec_security *gensec_security, 
155                                TALLOC_CTX *mem_ctx, 
156                                const uint8_t *data, size_t length, 
157                                const uint8_t *whole_pdu, size_t pdu_length, 
158                                const DATA_BLOB *sig)
159 {
160         struct schannel_state *state = gensec_security->private_data;
161
162         uint8_t digest_final[16];
163         uint8_t seq_num[8];
164         static const uint8_t netsec_sig[8] = NETSEC_SIGN_SIGNATURE;
165
166         /* w2k sends just 24 bytes and skip the confounder */
167         if (sig->length != 32 && sig->length != 24) {
168                 return NT_STATUS_ACCESS_DENIED;
169         }
170
171         RSIVAL(seq_num, 0, state->seq_num);
172         SIVAL(seq_num, 4, state->initiator?0:0x80);
173
174         dump_data_pw("seq_num:\n", seq_num, 8);
175         dump_data_pw("sess_key:\n", state->creds->session_key, 16);
176
177         schannel_digest(state->creds->session_key, 
178                         netsec_sig, NULL, 
179                         data, length, digest_final);
180
181         netsec_deal_with_seq_num(state, digest_final, seq_num);
182
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;
187         }
188
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;
193         }
194
195         return NT_STATUS_OK;
196 }
197
198
199 /*
200   seal a packet
201 */
202 NTSTATUS schannel_seal_packet(struct gensec_security *gensec_security, 
203                               TALLOC_CTX *mem_ctx, 
204                               uint8_t *data, size_t length, 
205                               const uint8_t *whole_pdu, size_t pdu_length, 
206                               DATA_BLOB *sig)
207 {
208         struct schannel_state *state = gensec_security->private_data;
209
210         uint8_t digest_final[16];
211         uint8_t confounder[8];
212         uint8_t seq_num[8];
213         uint8_t sealing_key[16];
214         static const uint8_t netsec_sig[8] = NETSEC_SEAL_SIGNATURE;
215
216         generate_random_buffer(confounder, 8);
217
218         RSIVAL(seq_num, 0, state->seq_num);
219         SIVAL(seq_num, 4, state->initiator?0x80:0);
220
221         schannel_digest(state->creds->session_key, 
222                         netsec_sig, confounder, 
223                         data, length, digest_final);
224
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);
228
229         netsec_deal_with_seq_num(state, digest_final, seq_num);
230
231         (*sig) = data_blob_talloc(mem_ctx, NULL, 32);
232
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);
237
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);
242
243         return NT_STATUS_OK;
244 }
245
246
247 /*
248   sign a packet
249 */
250 NTSTATUS schannel_sign_packet(struct gensec_security *gensec_security, 
251                               TALLOC_CTX *mem_ctx, 
252                               const uint8_t *data, size_t length, 
253                               const uint8_t *whole_pdu, size_t pdu_length, 
254                               DATA_BLOB *sig)
255 {
256         struct schannel_state *state = gensec_security->private_data;
257
258         uint8_t digest_final[16];
259         uint8_t seq_num[8];
260         static const uint8_t netsec_sig[8] = NETSEC_SIGN_SIGNATURE;
261
262         RSIVAL(seq_num, 0, state->seq_num);
263         SIVAL(seq_num, 4, state->initiator?0x80:0);
264
265         schannel_digest(state->creds->session_key, 
266                         netsec_sig, NULL, 
267                         data, length, digest_final);
268
269         netsec_deal_with_seq_num(state, digest_final, seq_num);
270
271         (*sig) = data_blob_talloc(mem_ctx, NULL, 32);
272
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);
277
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);
282
283         return NT_STATUS_OK;
284 }