b4549ddefd014418d5ab041985c90da45b66d1fa
[samba.git] / source4 / auth / gensec / 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 "auth/auth.h"
27 #include "auth/gensec/schannel.h"
28
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 }
31
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],
37                                      uint8_t seq_num[8])
38 {
39         static const uint8_t zeros[4];
40         uint8_t sequence_key[16];
41         uint8_t digest1[16];
42
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);
46
47         state->seq_num++;
48 }
49
50
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]) 
57 {
58         static const uint8_t zeros[4];
59         uint8_t digest2[16];
60         uint8_t sess_kf0[16];
61         int i;
62
63         for (i = 0; i < 16; i++) {
64                 sess_kf0[i] = session_key[i] ^ 0xf0;
65         }
66         
67         hmac_md5(sess_kf0, zeros, 4, digest2);
68         hmac_md5(digest2, seq_num, 8, sealing_key);
69 }
70
71
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]) 
81 {
82         uint8_t packet_digest[16];
83         static const uint8_t zeros[4];
84         struct MD5Context ctx;
85         
86         MD5Init(&ctx);
87         MD5Update(&ctx, zeros, 4);
88         MD5Update(&ctx, netsec_sig, 8);
89         if (confounder) {
90                 MD5Update(&ctx, confounder, 8);
91         }
92         MD5Update(&ctx, data, data_len);
93         MD5Final(packet_digest, &ctx);
94         
95         hmac_md5(sess_key, packet_digest, sizeof(packet_digest), digest_final);
96 }
97
98
99 /*
100   unseal a packet
101 */
102 NTSTATUS schannel_unseal_packet(struct gensec_security *gensec_security, 
103                                 TALLOC_CTX *mem_ctx, 
104                                 uint8_t *data, size_t length, 
105                                 const uint8_t *whole_pdu, size_t pdu_length, 
106                                 const DATA_BLOB *sig)
107 {
108         struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
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->creds->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->creds->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 gensec_security *gensec_security, 
154                                TALLOC_CTX *mem_ctx, 
155                                const uint8_t *data, size_t length, 
156                                const uint8_t *whole_pdu, size_t pdu_length, 
157                                const DATA_BLOB *sig)
158 {
159         struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
160
161         uint8_t digest_final[16];
162         uint8_t seq_num[8];
163         static const uint8_t netsec_sig[8] = NETSEC_SIGN_SIGNATURE;
164
165         /* w2k sends just 24 bytes and skip the confounder */
166         if (sig->length != 32 && sig->length != 24) {
167                 return NT_STATUS_ACCESS_DENIED;
168         }
169
170         RSIVAL(seq_num, 0, state->seq_num);
171         SIVAL(seq_num, 4, state->initiator?0:0x80);
172
173         dump_data_pw("seq_num:\n", seq_num, 8);
174         dump_data_pw("sess_key:\n", state->creds->session_key, 16);
175
176         schannel_digest(state->creds->session_key, 
177                         netsec_sig, NULL, 
178                         data, length, digest_final);
179
180         netsec_deal_with_seq_num(state, digest_final, seq_num);
181
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;
186         }
187
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;
192         }
193
194         return NT_STATUS_OK;
195 }
196
197
198 /*
199   seal a packet
200 */
201 NTSTATUS schannel_seal_packet(struct gensec_security *gensec_security, 
202                               TALLOC_CTX *mem_ctx, 
203                               uint8_t *data, size_t length, 
204                               const uint8_t *whole_pdu, size_t pdu_length, 
205                               DATA_BLOB *sig)
206 {
207         struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
208
209         uint8_t digest_final[16];
210         uint8_t confounder[8];
211         uint8_t seq_num[8];
212         uint8_t sealing_key[16];
213         static const uint8_t netsec_sig[8] = NETSEC_SEAL_SIGNATURE;
214
215         generate_random_buffer(confounder, 8);
216
217         RSIVAL(seq_num, 0, state->seq_num);
218         SIVAL(seq_num, 4, state->initiator?0x80:0);
219
220         schannel_digest(state->creds->session_key, 
221                         netsec_sig, confounder, 
222                         data, length, digest_final);
223
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);
227
228         netsec_deal_with_seq_num(state, digest_final, seq_num);
229
230         (*sig) = data_blob_talloc(mem_ctx, NULL, 32);
231
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);
236
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);
241
242         return NT_STATUS_OK;
243 }
244
245
246 /*
247   sign a packet
248 */
249 NTSTATUS schannel_sign_packet(struct gensec_security *gensec_security, 
250                               TALLOC_CTX *mem_ctx, 
251                               const uint8_t *data, size_t length, 
252                               const uint8_t *whole_pdu, size_t pdu_length, 
253                               DATA_BLOB *sig)
254 {
255         struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
256
257         uint8_t digest_final[16];
258         uint8_t seq_num[8];
259         static const uint8_t netsec_sig[8] = NETSEC_SIGN_SIGNATURE;
260
261         RSIVAL(seq_num, 0, state->seq_num);
262         SIVAL(seq_num, 4, state->initiator?0x80:0);
263
264         schannel_digest(state->creds->session_key, 
265                         netsec_sig, NULL, 
266                         data, length, digest_final);
267
268         netsec_deal_with_seq_num(state, digest_final, seq_num);
269
270         (*sig) = data_blob_talloc(mem_ctx, NULL, 32);
271
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);
276
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);
281
282         return NT_STATUS_OK;
283 }