This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[ira/wip.git] / source3 / libsmb / ntlmssp_sign.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Version 3.0
4  *  NTLMSSP Signing routines
5  *  Copyright (C) Luke Kenneth Casson Leighton 1996-2001
6  *  Copyright (C) Andrew Bartlett 2003
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 Foundation,
20  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */
22
23 #include "includes.h"
24
25 #define CLI_SIGN "session key to client-to-server signing key magic constant"
26 #define CLI_SEAL "session key to client-to-server sealing key magic constant"
27 #define SRV_SIGN "session key to server-to-client signing key magic constant"
28 #define SRV_SEAL "session key to server-to-client sealing key magic constant"
29
30 static void NTLMSSPcalc_ap( unsigned char *hash, unsigned char *data, int len)
31 {
32     unsigned char index_i = hash[256];
33     unsigned char index_j = hash[257];
34     int ind;
35
36     for (ind = 0; ind < len; ind++)
37     {
38         unsigned char tc;
39         unsigned char t;
40
41         index_i++;
42         index_j += hash[index_i];
43
44         tc = hash[index_i];
45         hash[index_i] = hash[index_j];
46         hash[index_j] = tc;
47
48         t = hash[index_i] + hash[index_j];
49         data[ind] = data[ind] ^ hash[t];
50     }
51
52     hash[256] = index_i;
53     hash[257] = index_j;
54 }
55
56 static void calc_hash(unsigned char *hash, const char *k2, int k2l)
57 {
58         unsigned char j = 0;
59         int ind;
60
61         for (ind = 0; ind < 256; ind++)
62         {
63                 hash[ind] = (unsigned char)ind;
64         }
65
66         for (ind = 0; ind < 256; ind++)
67         {
68                 unsigned char tc;
69
70                 j += (hash[ind] + k2[ind%k2l]);
71
72                 tc = hash[ind];
73                 hash[ind] = hash[j];
74                 hash[j] = tc;
75         }
76
77         hash[256] = 0;
78         hash[257] = 0;
79 }
80
81 static void calc_ntlmv2_hash(unsigned char hash[16], char digest[16],
82                              const char encrypted_response[16], 
83                              const char *constant)
84 {
85         struct MD5Context ctx3;
86
87         MD5Init(&ctx3);
88         MD5Update(&ctx3, encrypted_response, 5);
89         MD5Update(&ctx3, constant, strlen(constant));
90         MD5Final(digest, &ctx3);
91
92         calc_hash(hash, digest, 16);
93 }
94
95 static NTSTATUS ntlmssp_make_packet_signiture(NTLMSSP_CLIENT_STATE *ntlmssp_state,
96                                               const uchar *data, size_t length, 
97                                               DATA_BLOB *sig) 
98 {
99         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
100                 HMACMD5Context ctx;
101                 char seq_num[4];
102                 uchar digest[16];
103                 SIVAL(seq_num, 0, ntlmssp_state->ntlmssp_seq_num);
104
105                 hmac_md5_init_limK_to_64(ntlmssp_state->cli_sign_const, 16, &ctx);
106                 hmac_md5_update(seq_num, 4, &ctx);
107                 hmac_md5_update(data, length, &ctx);
108                 hmac_md5_final(digest, &ctx);
109
110                 if (!msrpc_gen(sig, "Bd", digest, sizeof(digest), ntlmssp_state->ntlmssp_seq_num)) {
111                         return NT_STATUS_NO_MEMORY;
112                 }
113                
114                 NTLMSSPcalc_ap(ntlmssp_state->cli_seal_hash,  sig->data, sig->length);
115         } else {
116                 uint32 crc;
117                 crc = crc32_calc_buffer(data, length);
118                 if (!msrpc_gen(sig, "ddd", 0, crc, ntlmssp_state->ntlmssp_seq_num)) {
119                         return NT_STATUS_NO_MEMORY;
120                 }
121                 
122                 NTLMSSPcalc_ap(ntlmssp_state->ntlmssp_hash, sig->data, sig->length);
123         }
124         return NT_STATUS_OK;
125 }
126
127 NTSTATUS ntlmssp_client_sign_packet(NTLMSSP_CLIENT_STATE *ntlmssp_state,
128                                            const uchar *data, size_t length, 
129                                            DATA_BLOB *sig) 
130 {
131         ntlmssp_state->ntlmssp_seq_num++;
132         return ntlmssp_make_packet_signiture(ntlmssp_state, data, length, sig);
133 }
134
135 /**
136  * Check the signature of an incoming packet 
137  * @note caller *must* check that the signature is the size it expects 
138  *
139  */
140
141 NTSTATUS ntlmssp_client_check_packet(NTLMSSP_CLIENT_STATE *ntlmssp_state,
142                                            const uchar *data, size_t length, 
143                                            const DATA_BLOB *sig) 
144 {
145         DATA_BLOB local_sig;
146         NTSTATUS nt_status;
147
148         if (sig->length < 8) {
149                 DEBUG(0, ("NTLMSSP packet check failed due to short signiture (%u bytes)!\n", 
150                           sig->length));
151         }
152
153         nt_status = ntlmssp_make_packet_signiture(ntlmssp_state, data, 
154                                                   length, &local_sig);
155         
156         if (!NT_STATUS_IS_OK(nt_status)) {
157                 DEBUG(0, ("NTLMSSP packet check failed with %s\n", nt_errstr(nt_status)));
158                 return nt_status;
159         }
160         
161         if (memcmp(sig->data, local_sig.data, MIN(sig->length, local_sig.length)) == 0) {
162                 return NT_STATUS_OK;
163         } else {
164                 DEBUG(0, ("NTLMSSP packet check failed due to invalid signiture!\n"));
165                 return NT_STATUS_ACCESS_DENIED;
166         }
167 }
168
169 /**
170    Initialise the state for NTLMSSP signing.
171 */
172 NTSTATUS ntlmssp_client_sign_init(NTLMSSP_CLIENT_STATE *ntlmssp_state)
173 {
174         unsigned char p24[24];
175         unsigned char lm_hash[16];
176
177         if (!ntlmssp_state->lm_resp.data) {
178                 /* can't sign or check signitures yet */ 
179                 return NT_STATUS_UNSUCCESSFUL;
180         }
181                             
182         E_deshash(ntlmssp_state->password, lm_hash);
183                 
184         NTLMSSPOWFencrypt(lm_hash, ntlmssp_state->lm_resp.data, p24);
185         
186         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
187         {
188                 calc_ntlmv2_hash(ntlmssp_state->cli_sign_hash, ntlmssp_state->cli_sign_const, p24, CLI_SIGN);
189                 calc_ntlmv2_hash(ntlmssp_state->cli_seal_hash, ntlmssp_state->cli_seal_const, p24, CLI_SEAL);
190                 calc_ntlmv2_hash(ntlmssp_state->srv_sign_hash, ntlmssp_state->srv_sign_const, p24, SRV_SIGN);
191                 calc_ntlmv2_hash(ntlmssp_state->srv_seal_hash, ntlmssp_state->srv_seal_const, p24, SRV_SEAL);
192         }
193         else
194         {
195                 char k2[8];
196                 memcpy(k2, p24, 5);
197                 k2[5] = 0xe5;
198                 k2[6] = 0x38;
199                 k2[7] = 0xb0;
200                 
201                 calc_hash(ntlmssp_state->ntlmssp_hash, k2, 8);
202         }
203
204         ntlmssp_state->ntlmssp_seq_num = 0;
205
206         ZERO_STRUCT(lm_hash);
207         return NT_STATUS_OK;
208 }