s3-selftest: Remove some unnecessary comma
[Samba/gebeck_regimport.git] / libcli / auth / schannel_sign.c
blob29a97b92820020c4bf573ce4d6b02306705f0e2b
1 /*
2 Unix SMB/CIFS implementation.
4 schannel library code
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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "../libcli/auth/schannel.h"
25 #include "../lib/crypto/crypto.h"
27 static void netsec_offset_and_sizes(struct schannel_state *state,
28 bool do_seal,
29 uint32_t *_min_sig_size,
30 uint32_t *_used_sig_size,
31 uint32_t *_checksum_length,
32 uint32_t *_confounder_ofs)
34 uint32_t min_sig_size = 24;
35 uint32_t used_sig_size = 32;
36 uint32_t checksum_length = 8;
37 uint32_t confounder_ofs = 24;
39 if (do_seal) {
40 min_sig_size += 8;
43 if (_min_sig_size) {
44 *_min_sig_size = min_sig_size;
47 if (_used_sig_size) {
48 *_used_sig_size = used_sig_size;
51 if (_checksum_length) {
52 *_checksum_length = checksum_length;
55 if (_confounder_ofs) {
56 *_confounder_ofs = confounder_ofs;
60 /*******************************************************************
61 Encode or Decode the sequence number (which is symmetric)
62 ********************************************************************/
63 static void netsec_do_seq_num(struct schannel_state *state,
64 const uint8_t *checksum,
65 uint32_t checksum_length,
66 uint8_t seq_num[8])
68 static const uint8_t zeros[4];
69 uint8_t sequence_key[16];
70 uint8_t digest1[16];
72 hmac_md5(state->creds->session_key, zeros, sizeof(zeros), digest1);
73 hmac_md5(digest1, checksum, checksum_length, sequence_key);
74 arcfour_crypt(seq_num, sequence_key, 8);
76 state->seq_num++;
79 static void netsec_do_seal(struct schannel_state *state,
80 const uint8_t seq_num[8],
81 uint8_t confounder[8],
82 uint8_t *data, uint32_t length)
84 uint8_t sealing_key[16];
85 static const uint8_t zeros[4];
86 uint8_t digest2[16];
87 uint8_t sess_kf0[16];
88 int i;
90 for (i = 0; i < 16; i++) {
91 sess_kf0[i] = state->creds->session_key[i] ^ 0xf0;
94 hmac_md5(sess_kf0, zeros, 4, digest2);
95 hmac_md5(digest2, seq_num, 8, sealing_key);
97 arcfour_crypt(confounder, sealing_key, 8);
98 arcfour_crypt(data, sealing_key, length);
101 /*******************************************************************
102 Create a digest over the entire packet (including the data), and
103 MD5 it with the session key.
104 ********************************************************************/
105 static void netsec_do_sign(struct schannel_state *state,
106 const uint8_t *confounder,
107 const uint8_t *data, size_t data_len,
108 uint8_t header[8],
109 uint8_t *checksum)
111 uint8_t packet_digest[16];
112 static const uint8_t zeros[4];
113 struct MD5Context ctx;
115 MD5Init(&ctx);
116 MD5Update(&ctx, zeros, 4);
117 if (confounder) {
118 SSVAL(header, 0, NL_SIGN_HMAC_MD5);
119 SSVAL(header, 2, NL_SEAL_RC4);
120 SSVAL(header, 4, 0xFFFF);
121 SSVAL(header, 6, 0x0000);
123 MD5Update(&ctx, header, 8);
124 MD5Update(&ctx, confounder, 8);
125 } else {
126 SSVAL(header, 0, NL_SIGN_HMAC_MD5);
127 SSVAL(header, 2, NL_SEAL_NONE);
128 SSVAL(header, 4, 0xFFFF);
129 SSVAL(header, 6, 0x0000);
131 MD5Update(&ctx, header, 8);
133 MD5Update(&ctx, data, data_len);
134 MD5Final(packet_digest, &ctx);
136 hmac_md5(state->creds->session_key,
137 packet_digest, sizeof(packet_digest),
138 checksum);
141 NTSTATUS netsec_incoming_packet(struct schannel_state *state,
142 bool do_unseal,
143 uint8_t *data, size_t length,
144 const DATA_BLOB *sig)
146 uint32_t min_sig_size = 0;
147 uint8_t header[8];
148 uint8_t checksum[32];
149 uint32_t checksum_length = sizeof(checksum_length);
150 uint8_t _confounder[8];
151 uint8_t *confounder = NULL;
152 uint32_t confounder_ofs = 0;
153 uint8_t seq_num[8];
154 int ret;
156 netsec_offset_and_sizes(state,
157 do_unseal,
158 &min_sig_size,
159 NULL,
160 &checksum_length,
161 &confounder_ofs);
163 if (sig->length < min_sig_size) {
164 return NT_STATUS_ACCESS_DENIED;
167 if (do_unseal) {
168 confounder = _confounder;
169 memcpy(confounder, sig->data+confounder_ofs, 8);
170 } else {
171 confounder = NULL;
174 RSIVAL(seq_num, 0, state->seq_num);
175 SIVAL(seq_num, 4, state->initiator?0:0x80);
177 if (do_unseal) {
178 netsec_do_seal(state, seq_num,
179 confounder,
180 data, length);
183 netsec_do_sign(state, confounder,
184 data, length,
185 header, checksum);
187 ret = memcmp(checksum, sig->data+16, checksum_length);
188 if (ret != 0) {
189 dump_data_pw("calc digest:", checksum, checksum_length);
190 dump_data_pw("wire digest:", sig->data+16, checksum_length);
191 return NT_STATUS_ACCESS_DENIED;
194 netsec_do_seq_num(state, checksum, checksum_length, seq_num);
196 ret = memcmp(seq_num, sig->data+8, 8);
197 if (ret != 0) {
198 dump_data_pw("calc seq num:", seq_num, 8);
199 dump_data_pw("wire seq num:", sig->data+8, 8);
200 return NT_STATUS_ACCESS_DENIED;
203 return NT_STATUS_OK;
206 uint32_t netsec_outgoing_sig_size(struct schannel_state *state)
208 uint32_t sig_size = 0;
210 netsec_offset_and_sizes(state,
211 true,
212 NULL,
213 &sig_size,
214 NULL,
215 NULL);
217 return sig_size;
220 NTSTATUS netsec_outgoing_packet(struct schannel_state *state,
221 TALLOC_CTX *mem_ctx,
222 bool do_seal,
223 uint8_t *data, size_t length,
224 DATA_BLOB *sig)
226 uint32_t min_sig_size = 0;
227 uint32_t used_sig_size = 0;
228 uint8_t header[8];
229 uint8_t checksum[32];
230 uint32_t checksum_length = sizeof(checksum_length);
231 uint8_t _confounder[8];
232 uint8_t *confounder = NULL;
233 uint32_t confounder_ofs = 0;
234 uint8_t seq_num[8];
236 netsec_offset_and_sizes(state,
237 do_seal,
238 &min_sig_size,
239 &used_sig_size,
240 &checksum_length,
241 &confounder_ofs);
243 RSIVAL(seq_num, 0, state->seq_num);
244 SIVAL(seq_num, 4, state->initiator?0x80:0);
246 if (do_seal) {
247 confounder = _confounder;
248 generate_random_buffer(confounder, 8);
249 } else {
250 confounder = NULL;
253 netsec_do_sign(state, confounder,
254 data, length,
255 header, checksum);
257 if (do_seal) {
258 netsec_do_seal(state, seq_num,
259 confounder,
260 data, length);
263 netsec_do_seq_num(state, checksum, checksum_length, seq_num);
265 (*sig) = data_blob_talloc_zero(mem_ctx, used_sig_size);
267 memcpy(sig->data, header, 8);
268 memcpy(sig->data+8, seq_num, 8);
269 memcpy(sig->data+16, checksum, checksum_length);
271 if (confounder) {
272 memcpy(sig->data+confounder_ofs, confounder, 8);
275 dump_data_pw("signature:", sig->data+ 0, 8);
276 dump_data_pw("seq_num :", sig->data+ 8, 8);
277 dump_data_pw("digest :", sig->data+16, checksum_length);
278 dump_data_pw("confound :", sig->data+confounder_ofs, 8);
280 return NT_STATUS_OK;