Changes to make the Solaris C compiler happy.
[Samba.git] / libcli / drsuapi / repl_decrypt.c
blobc370f2e3bf597ecfc4fc8f487394db8f217fd1d7
1 /*
2 Unix SMB/CIFS implementation.
3 Helper functions for applying replicated objects
5 Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2009
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 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "../lib/util/dlinklist.h"
25 #include "librpc/gen_ndr/ndr_misc.h"
26 #include "librpc/gen_ndr/ndr_drsuapi.h"
27 #include "librpc/gen_ndr/ndr_drsblobs.h"
28 #include "../lib/crypto/crypto.h"
29 #include "../libcli/drsuapi/drsuapi.h"
30 #include "libcli/auth/libcli_auth.h"
31 #include "dsdb/samdb/samdb.h"
33 WERROR drsuapi_decrypt_attribute_value(TALLOC_CTX *mem_ctx,
34 const DATA_BLOB *gensec_skey,
35 bool rid_crypt,
36 uint32_t rid,
37 DATA_BLOB *in,
38 DATA_BLOB *out)
40 DATA_BLOB confounder;
41 DATA_BLOB enc_buffer;
43 MD5_CTX md5;
44 uint8_t _enc_key[16];
45 DATA_BLOB enc_key;
47 DATA_BLOB dec_buffer;
49 uint32_t crc32_given;
50 uint32_t crc32_calc;
51 DATA_BLOB checked_buffer;
53 DATA_BLOB plain_buffer;
56 * users with rid == 0 should not exist
58 if (rid_crypt && rid == 0) {
59 return WERR_DS_DRA_INVALID_PARAMETER;
62 /*
63 * the first 16 bytes at the beginning are the confounder
64 * followed by the 4 byte crc32 checksum
66 if (in->length < 20) {
67 return WERR_DS_DRA_INVALID_PARAMETER;
69 confounder = data_blob_const(in->data, 16);
70 enc_buffer = data_blob_const(in->data + 16, in->length - 16);
72 /*
73 * build the encryption key md5 over the session key followed
74 * by the confounder
76 * here the gensec session key is used and
77 * not the dcerpc ncacn_ip_tcp "SystemLibraryDTC" key!
79 enc_key = data_blob_const(_enc_key, sizeof(_enc_key));
80 MD5Init(&md5);
81 MD5Update(&md5, gensec_skey->data, gensec_skey->length);
82 MD5Update(&md5, confounder.data, confounder.length);
83 MD5Final(enc_key.data, &md5);
86 * copy the encrypted buffer part and
87 * decrypt it using the created encryption key using arcfour
89 dec_buffer = data_blob_const(enc_buffer.data, enc_buffer.length);
90 arcfour_crypt_blob(dec_buffer.data, dec_buffer.length, &enc_key);
92 /*
93 * the first 4 byte are the crc32 checksum
94 * of the remaining bytes
96 crc32_given = IVAL(dec_buffer.data, 0);
97 crc32_calc = crc32_calc_buffer(dec_buffer.data + 4 , dec_buffer.length - 4);
98 checked_buffer = data_blob_const(dec_buffer.data + 4, dec_buffer.length - 4);
100 plain_buffer = data_blob_talloc(mem_ctx, checked_buffer.data, checked_buffer.length);
101 W_ERROR_HAVE_NO_MEMORY(plain_buffer.data);
103 if (crc32_given != crc32_calc) {
104 return W_ERROR(HRES_ERROR_V(HRES_SEC_E_DECRYPT_FAILURE));
107 * The following rid_crypt obfuscation isn't session specific
108 * and not really needed here, because we allways know the rid of the
109 * user account.
111 * some attributes with this 'additional encryption' include
112 * dBCSPwd, unicodePwd, ntPwdHistory, lmPwdHistory
114 * But for the rest of samba it's easier when we remove this static
115 * obfuscation here
117 if (rid_crypt) {
118 uint32_t i, num_hashes;
120 if ((checked_buffer.length % 16) != 0) {
121 return WERR_DS_DRA_INVALID_PARAMETER;
124 num_hashes = plain_buffer.length / 16;
125 for (i = 0; i < num_hashes; i++) {
126 uint32_t offset = i * 16;
127 sam_rid_crypt(rid, checked_buffer.data + offset, plain_buffer.data + offset, 0);
131 *out = plain_buffer;
132 return WERR_OK;
135 WERROR drsuapi_decrypt_attribute(TALLOC_CTX *mem_ctx,
136 const DATA_BLOB *gensec_skey,
137 uint32_t rid,
138 uint32_t dsdb_repl_flags,
139 struct drsuapi_DsReplicaAttribute *attr)
141 WERROR status;
142 DATA_BLOB *enc_data;
143 DATA_BLOB plain_data;
144 bool rid_crypt = false;
146 if (attr->value_ctr.num_values == 0) {
147 return WERR_OK;
150 switch (attr->attid) {
151 case DRSUAPI_ATTID_dBCSPwd:
152 case DRSUAPI_ATTID_unicodePwd:
153 case DRSUAPI_ATTID_ntPwdHistory:
154 case DRSUAPI_ATTID_lmPwdHistory:
155 rid_crypt = true;
156 break;
157 case DRSUAPI_ATTID_supplementalCredentials:
158 case DRSUAPI_ATTID_priorValue:
159 case DRSUAPI_ATTID_currentValue:
160 case DRSUAPI_ATTID_trustAuthOutgoing:
161 case DRSUAPI_ATTID_trustAuthIncoming:
162 case DRSUAPI_ATTID_initialAuthOutgoing:
163 case DRSUAPI_ATTID_initialAuthIncoming:
164 break;
165 default:
166 return WERR_OK;
169 if (dsdb_repl_flags & DSDB_REPL_FLAG_EXPECT_NO_SECRETS) {
170 return WERR_TOO_MANY_SECRETS;
173 if (attr->value_ctr.num_values > 1) {
174 return WERR_DS_DRA_INVALID_PARAMETER;
177 if (!attr->value_ctr.values[0].blob) {
178 return WERR_DS_DRA_INVALID_PARAMETER;
181 enc_data = attr->value_ctr.values[0].blob;
183 status = drsuapi_decrypt_attribute_value(mem_ctx,
184 gensec_skey,
185 rid_crypt,
186 rid,
187 enc_data,
188 &plain_data);
189 W_ERROR_NOT_OK_RETURN(status);
191 talloc_free(attr->value_ctr.values[0].blob->data);
192 *attr->value_ctr.values[0].blob = plain_data;
194 return WERR_OK;
197 static WERROR drsuapi_encrypt_attribute_value(TALLOC_CTX *mem_ctx,
198 const DATA_BLOB *gensec_skey,
199 bool rid_crypt,
200 uint32_t rid,
201 DATA_BLOB *in,
202 DATA_BLOB *out)
204 DATA_BLOB rid_crypt_out = data_blob(NULL, 0);
205 DATA_BLOB confounder;
207 MD5_CTX md5;
208 uint8_t _enc_key[16];
209 DATA_BLOB enc_key;
211 DATA_BLOB enc_buffer;
213 uint32_t crc32_calc;
216 * users with rid == 0 should not exist
218 if (rid_crypt && rid == 0) {
219 return WERR_DS_DRA_INVALID_PARAMETER;
223 * The following rid_crypt obfuscation isn't session specific
224 * and not really needed here, because we allways know the rid of the
225 * user account.
227 * some attributes with this 'additional encryption' include
228 * dBCSPwd, unicodePwd, ntPwdHistory, lmPwdHistory
230 * But for the rest of samba it's easier when we remove this static
231 * obfuscation here
233 if (rid_crypt) {
234 uint32_t i, num_hashes;
235 rid_crypt_out = data_blob_talloc(mem_ctx, in->data, in->length);
236 W_ERROR_HAVE_NO_MEMORY(rid_crypt_out.data);
238 if ((rid_crypt_out.length % 16) != 0) {
239 return WERR_DS_DRA_INVALID_PARAMETER;
242 num_hashes = rid_crypt_out.length / 16;
243 for (i = 0; i < num_hashes; i++) {
244 uint32_t offset = i * 16;
245 sam_rid_crypt(rid, in->data + offset, rid_crypt_out.data + offset, 1);
247 in = &rid_crypt_out;
251 * the first 16 bytes at the beginning are the confounder
252 * followed by the 4 byte crc32 checksum
255 enc_buffer = data_blob_talloc(mem_ctx, NULL, in->length+20);
256 if (!enc_buffer.data) {
257 talloc_free(rid_crypt_out.data);
258 return WERR_NOT_ENOUGH_MEMORY;
261 confounder = data_blob_const(enc_buffer.data, 16);
262 generate_random_buffer(confounder.data, confounder.length);
265 * build the encryption key md5 over the session key followed
266 * by the confounder
268 * here the gensec session key is used and
269 * not the dcerpc ncacn_ip_tcp "SystemLibraryDTC" key!
271 enc_key = data_blob_const(_enc_key, sizeof(_enc_key));
272 MD5Init(&md5);
273 MD5Update(&md5, gensec_skey->data, gensec_skey->length);
274 MD5Update(&md5, confounder.data, confounder.length);
275 MD5Final(enc_key.data, &md5);
278 * the first 4 byte are the crc32 checksum
279 * of the remaining bytes
281 crc32_calc = crc32_calc_buffer(in->data, in->length);
282 SIVAL(enc_buffer.data, 16, crc32_calc);
285 * copy the plain buffer part and
286 * encrypt it using the created encryption key using arcfour
288 memcpy(enc_buffer.data+20, in->data, in->length);
289 talloc_free(rid_crypt_out.data);
291 arcfour_crypt_blob(enc_buffer.data+16, enc_buffer.length-16, &enc_key);
293 *out = enc_buffer;
295 return WERR_OK;
299 encrypt a DRSUAPI attribute ready for sending over the wire
300 Only some attribute types are encrypted
302 WERROR drsuapi_encrypt_attribute(TALLOC_CTX *mem_ctx,
303 const DATA_BLOB *gensec_skey,
304 uint32_t rid,
305 struct drsuapi_DsReplicaAttribute *attr)
307 WERROR status;
308 DATA_BLOB *plain_data;
309 DATA_BLOB enc_data;
310 bool rid_crypt = false;
312 if (attr->value_ctr.num_values == 0) {
313 return WERR_OK;
316 switch (attr->attid) {
317 case DRSUAPI_ATTID_dBCSPwd:
318 case DRSUAPI_ATTID_unicodePwd:
319 case DRSUAPI_ATTID_ntPwdHistory:
320 case DRSUAPI_ATTID_lmPwdHistory:
321 rid_crypt = true;
322 break;
323 case DRSUAPI_ATTID_supplementalCredentials:
324 case DRSUAPI_ATTID_priorValue:
325 case DRSUAPI_ATTID_currentValue:
326 case DRSUAPI_ATTID_trustAuthOutgoing:
327 case DRSUAPI_ATTID_trustAuthIncoming:
328 case DRSUAPI_ATTID_initialAuthOutgoing:
329 case DRSUAPI_ATTID_initialAuthIncoming:
330 break;
331 default:
332 return WERR_OK;
335 if (attr->value_ctr.num_values > 1) {
336 return WERR_DS_DRA_INVALID_PARAMETER;
339 if (!attr->value_ctr.values[0].blob) {
340 return WERR_DS_DRA_INVALID_PARAMETER;
343 plain_data = attr->value_ctr.values[0].blob;
345 status = drsuapi_encrypt_attribute_value(mem_ctx,
346 gensec_skey,
347 rid_crypt,
348 rid,
349 plain_data,
350 &enc_data);
351 W_ERROR_NOT_OK_RETURN(status);
353 talloc_free(attr->value_ctr.values[0].blob->data);
354 *attr->value_ctr.values[0].blob = enc_data;
356 return WERR_OK;