packaging: Update READMEs to reflect current status.
[Samba.git] / libcli / drsuapi / repl_decrypt.c
blob54d288853423420c802610b64e1e212f9d13b245
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/arcfour.h"
29 #include "zlib.h"
30 #include "../libcli/drsuapi/drsuapi.h"
31 #include "libcli/auth/libcli_auth.h"
32 #include "dsdb/samdb/samdb.h"
34 #include <gnutls/gnutls.h>
35 #include <gnutls/crypto.h>
37 WERROR drsuapi_decrypt_attribute_value(TALLOC_CTX *mem_ctx,
38 const DATA_BLOB *gensec_skey,
39 bool rid_crypt,
40 uint32_t rid,
41 DATA_BLOB *in,
42 DATA_BLOB *out)
44 DATA_BLOB confounder;
45 DATA_BLOB enc_buffer;
47 gnutls_hash_hd_t hash_hnd = NULL;
48 uint8_t _enc_key[16];
49 DATA_BLOB enc_key;
51 DATA_BLOB dec_buffer;
53 uint32_t crc32_given;
54 uint32_t crc32_calc;
55 DATA_BLOB checked_buffer;
57 DATA_BLOB plain_buffer;
58 WERROR result;
59 int rc;
62 * users with rid == 0 should not exist
64 if (rid_crypt && rid == 0) {
65 return WERR_DS_DRA_INVALID_PARAMETER;
68 /*
69 * the first 16 bytes at the beginning are the confounder
70 * followed by the 4 byte crc32 checksum
72 if (in->length < 20) {
73 return WERR_DS_DRA_INVALID_PARAMETER;
75 confounder = data_blob_const(in->data, 16);
76 enc_buffer = data_blob_const(in->data + 16, in->length - 16);
78 /*
79 * build the encryption key md5 over the session key followed
80 * by the confounder
82 * here the gensec session key is used and
83 * not the dcerpc ncacn_ip_tcp "SystemLibraryDTC" key!
85 enc_key = data_blob_const(_enc_key, sizeof(_enc_key));
87 rc = gnutls_hash_init(&hash_hnd, GNUTLS_DIG_MD5);
88 if (rc < 0) {
89 result = WERR_NOT_ENOUGH_MEMORY;
90 goto out;
92 rc = gnutls_hash(hash_hnd, gensec_skey->data, gensec_skey->length);
93 if (rc < 0) {
94 gnutls_hash_deinit(hash_hnd, NULL);
95 result = WERR_INTERNAL_ERROR;
96 goto out;
98 rc = gnutls_hash(hash_hnd, confounder.data, confounder.length);
99 if (rc < 0) {
100 gnutls_hash_deinit(hash_hnd, NULL);
101 result = WERR_INTERNAL_ERROR;
102 goto out;
105 gnutls_hash_deinit(hash_hnd, enc_key.data);
108 * copy the encrypted buffer part and
109 * decrypt it using the created encryption key using arcfour
111 dec_buffer = data_blob_const(enc_buffer.data, enc_buffer.length);
112 arcfour_crypt_blob(dec_buffer.data, dec_buffer.length, &enc_key);
114 ZERO_ARRAY_LEN(enc_key.data, enc_key.length);
117 * the first 4 byte are the crc32 checksum
118 * of the remaining bytes
120 crc32_given = IVAL(dec_buffer.data, 0);
121 crc32_calc = crc32(0, Z_NULL, 0);
122 crc32_calc = crc32(crc32_calc,
123 dec_buffer.data + 4 ,
124 dec_buffer.length - 4);
125 checked_buffer = data_blob_const(dec_buffer.data + 4, dec_buffer.length - 4);
127 plain_buffer = data_blob_talloc(mem_ctx, checked_buffer.data, checked_buffer.length);
128 W_ERROR_HAVE_NO_MEMORY(plain_buffer.data);
130 if (crc32_given != crc32_calc) {
131 result = W_ERROR(HRES_ERROR_V(HRES_SEC_E_DECRYPT_FAILURE));
132 goto out;
135 * The following rid_crypt obfuscation isn't session specific
136 * and not really needed here, because we allways know the rid of the
137 * user account.
139 * some attributes with this 'additional encryption' include
140 * dBCSPwd, unicodePwd, ntPwdHistory, lmPwdHistory
142 * But for the rest of samba it's easier when we remove this static
143 * obfuscation here
145 if (rid_crypt) {
146 uint32_t i, num_hashes;
148 if ((checked_buffer.length % 16) != 0) {
149 result = WERR_DS_DRA_INVALID_PARAMETER;
150 goto out;
153 num_hashes = plain_buffer.length / 16;
154 for (i = 0; i < num_hashes; i++) {
155 uint32_t offset = i * 16;
156 sam_rid_crypt(rid, checked_buffer.data + offset, plain_buffer.data + offset, 0);
160 *out = plain_buffer;
161 result = WERR_OK;
162 out:
163 return result;
166 WERROR drsuapi_decrypt_attribute(TALLOC_CTX *mem_ctx,
167 const DATA_BLOB *gensec_skey,
168 uint32_t rid,
169 uint32_t dsdb_repl_flags,
170 struct drsuapi_DsReplicaAttribute *attr)
172 WERROR status;
173 DATA_BLOB *enc_data;
174 DATA_BLOB plain_data;
175 bool rid_crypt = false;
177 if (attr->value_ctr.num_values == 0) {
178 return WERR_OK;
181 switch (attr->attid) {
182 case DRSUAPI_ATTID_dBCSPwd:
183 case DRSUAPI_ATTID_unicodePwd:
184 case DRSUAPI_ATTID_ntPwdHistory:
185 case DRSUAPI_ATTID_lmPwdHistory:
186 rid_crypt = true;
187 break;
188 case DRSUAPI_ATTID_supplementalCredentials:
189 case DRSUAPI_ATTID_priorValue:
190 case DRSUAPI_ATTID_currentValue:
191 case DRSUAPI_ATTID_trustAuthOutgoing:
192 case DRSUAPI_ATTID_trustAuthIncoming:
193 case DRSUAPI_ATTID_initialAuthOutgoing:
194 case DRSUAPI_ATTID_initialAuthIncoming:
195 break;
196 default:
197 return WERR_OK;
200 if (dsdb_repl_flags & DSDB_REPL_FLAG_EXPECT_NO_SECRETS) {
201 return WERR_TOO_MANY_SECRETS;
204 if (attr->value_ctr.num_values > 1) {
205 return WERR_DS_DRA_INVALID_PARAMETER;
208 if (!attr->value_ctr.values[0].blob) {
209 return WERR_DS_DRA_INVALID_PARAMETER;
212 enc_data = attr->value_ctr.values[0].blob;
214 status = drsuapi_decrypt_attribute_value(mem_ctx,
215 gensec_skey,
216 rid_crypt,
217 rid,
218 enc_data,
219 &plain_data);
220 W_ERROR_NOT_OK_RETURN(status);
222 talloc_free(attr->value_ctr.values[0].blob->data);
223 *attr->value_ctr.values[0].blob = plain_data;
225 return WERR_OK;
228 static WERROR drsuapi_encrypt_attribute_value(TALLOC_CTX *mem_ctx,
229 const DATA_BLOB *gensec_skey,
230 bool rid_crypt,
231 uint32_t rid,
232 DATA_BLOB *in,
233 DATA_BLOB *out)
235 DATA_BLOB rid_crypt_out = data_blob(NULL, 0);
236 DATA_BLOB confounder;
238 gnutls_hash_hd_t hash_hnd = NULL;
239 uint8_t _enc_key[16];
240 DATA_BLOB enc_key;
242 DATA_BLOB enc_buffer;
244 uint32_t crc32_calc;
245 WERROR result;
246 int rc;
249 * users with rid == 0 should not exist
251 if (rid_crypt && rid == 0) {
252 return WERR_DS_DRA_INVALID_PARAMETER;
256 * The following rid_crypt obfuscation isn't session specific
257 * and not really needed here, because we allways know the rid of the
258 * user account.
260 * some attributes with this 'additional encryption' include
261 * dBCSPwd, unicodePwd, ntPwdHistory, lmPwdHistory
263 * But for the rest of samba it's easier when we remove this static
264 * obfuscation here
266 if (rid_crypt) {
267 uint32_t i, num_hashes;
268 rid_crypt_out = data_blob_talloc(mem_ctx, in->data, in->length);
269 W_ERROR_HAVE_NO_MEMORY(rid_crypt_out.data);
271 if ((rid_crypt_out.length % 16) != 0) {
272 return WERR_DS_DRA_INVALID_PARAMETER;
275 num_hashes = rid_crypt_out.length / 16;
276 for (i = 0; i < num_hashes; i++) {
277 uint32_t offset = i * 16;
278 sam_rid_crypt(rid, in->data + offset, rid_crypt_out.data + offset, 1);
280 in = &rid_crypt_out;
284 * the first 16 bytes at the beginning are the confounder
285 * followed by the 4 byte crc32 checksum
288 enc_buffer = data_blob_talloc(mem_ctx, NULL, in->length+20);
289 if (!enc_buffer.data) {
290 talloc_free(rid_crypt_out.data);
291 return WERR_NOT_ENOUGH_MEMORY;
294 confounder = data_blob_const(enc_buffer.data, 16);
295 generate_random_buffer(confounder.data, confounder.length);
298 * build the encryption key md5 over the session key followed
299 * by the confounder
301 * here the gensec session key is used and
302 * not the dcerpc ncacn_ip_tcp "SystemLibraryDTC" key!
304 enc_key = data_blob_const(_enc_key, sizeof(_enc_key));
306 rc = gnutls_hash_init(&hash_hnd, GNUTLS_DIG_MD5);
307 if (rc < 0) {
308 result = WERR_NOT_ENOUGH_MEMORY;
309 goto out;
312 rc = gnutls_hash(hash_hnd, gensec_skey->data, gensec_skey->length);
313 if (rc < 0) {
314 gnutls_hash_deinit(hash_hnd, NULL);
315 result = WERR_INTERNAL_ERROR;
316 goto out;
318 rc = gnutls_hash(hash_hnd, confounder.data, confounder.length);
319 if (rc < 0) {
320 gnutls_hash_deinit(hash_hnd, NULL);
321 result = WERR_INTERNAL_ERROR;
322 goto out;
324 gnutls_hash_deinit(hash_hnd, enc_key.data);
327 * the first 4 byte are the crc32 checksum
328 * of the remaining bytes
330 crc32_calc = crc32(0, Z_NULL, 0);
331 crc32_calc = crc32(crc32_calc, in->data, in->length);
332 SIVAL(enc_buffer.data, 16, crc32_calc);
335 * copy the plain buffer part and
336 * encrypt it using the created encryption key using arcfour
338 memcpy(enc_buffer.data+20, in->data, in->length);
339 talloc_free(rid_crypt_out.data);
341 arcfour_crypt_blob(enc_buffer.data+16, enc_buffer.length-16, &enc_key);
343 ZERO_ARRAY_LEN(enc_key.data, enc_key.length);
345 *out = enc_buffer;
346 result = WERR_OK;
347 out:
348 return result;
352 encrypt a DRSUAPI attribute ready for sending over the wire
353 Only some attribute types are encrypted
355 WERROR drsuapi_encrypt_attribute(TALLOC_CTX *mem_ctx,
356 const DATA_BLOB *gensec_skey,
357 uint32_t rid,
358 struct drsuapi_DsReplicaAttribute *attr)
360 WERROR status;
361 DATA_BLOB *plain_data;
362 DATA_BLOB enc_data;
363 bool rid_crypt = false;
365 if (attr->value_ctr.num_values == 0) {
366 return WERR_OK;
369 switch (attr->attid) {
370 case DRSUAPI_ATTID_dBCSPwd:
371 case DRSUAPI_ATTID_unicodePwd:
372 case DRSUAPI_ATTID_ntPwdHistory:
373 case DRSUAPI_ATTID_lmPwdHistory:
374 rid_crypt = true;
375 break;
376 case DRSUAPI_ATTID_supplementalCredentials:
377 case DRSUAPI_ATTID_priorValue:
378 case DRSUAPI_ATTID_currentValue:
379 case DRSUAPI_ATTID_trustAuthOutgoing:
380 case DRSUAPI_ATTID_trustAuthIncoming:
381 case DRSUAPI_ATTID_initialAuthOutgoing:
382 case DRSUAPI_ATTID_initialAuthIncoming:
383 break;
384 default:
385 return WERR_OK;
388 if (attr->value_ctr.num_values > 1) {
389 return WERR_DS_DRA_INVALID_PARAMETER;
392 if (!attr->value_ctr.values[0].blob) {
393 return WERR_DS_DRA_INVALID_PARAMETER;
396 plain_data = attr->value_ctr.values[0].blob;
398 status = drsuapi_encrypt_attribute_value(mem_ctx,
399 gensec_skey,
400 rid_crypt,
401 rid,
402 plain_data,
403 &enc_data);
404 W_ERROR_NOT_OK_RETURN(status);
406 talloc_free(attr->value_ctr.values[0].blob->data);
407 *attr->value_ctr.values[0].blob = enc_data;
409 return WERR_OK;