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/>.
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/md5.h"
29 #include "../lib/crypto/arcfour.h"
31 #include "../libcli/drsuapi/drsuapi.h"
32 #include "libcli/auth/libcli_auth.h"
33 #include "dsdb/samdb/samdb.h"
35 #include <gnutls/gnutls.h>
36 #include <gnutls/crypto.h>
38 WERROR drsuapi_decrypt_attribute_value(TALLOC_CTX *mem_ctx,
39 const DATA_BLOB *gensec_skey,
48 gnutls_hash_hd_t hash_hnd = NULL;
56 DATA_BLOB checked_buffer;
58 DATA_BLOB plain_buffer;
63 * users with rid == 0 should not exist
65 if (rid_crypt && rid == 0) {
66 return WERR_DS_DRA_INVALID_PARAMETER;
70 * the first 16 bytes at the beginning are the confounder
71 * followed by the 4 byte crc32 checksum
73 if (in->length < 20) {
74 return WERR_DS_DRA_INVALID_PARAMETER;
76 confounder = data_blob_const(in->data, 16);
77 enc_buffer = data_blob_const(in->data + 16, in->length - 16);
80 * build the encryption key md5 over the session key followed
83 * here the gensec session key is used and
84 * not the dcerpc ncacn_ip_tcp "SystemLibraryDTC" key!
86 enc_key = data_blob_const(_enc_key, sizeof(_enc_key));
88 rc = gnutls_hash_init(&hash_hnd, GNUTLS_DIG_MD5);
90 result = WERR_NOT_ENOUGH_MEMORY;
93 rc = gnutls_hash(hash_hnd, gensec_skey->data, gensec_skey->length);
95 gnutls_hash_deinit(hash_hnd, NULL);
96 result = WERR_INTERNAL_ERROR;
99 rc = gnutls_hash(hash_hnd, confounder.data, confounder.length);
101 gnutls_hash_deinit(hash_hnd, NULL);
102 result = WERR_INTERNAL_ERROR;
106 gnutls_hash_deinit(hash_hnd, enc_key.data);
109 * copy the encrypted buffer part and
110 * decrypt it using the created encryption key using arcfour
112 dec_buffer = data_blob_const(enc_buffer.data, enc_buffer.length);
113 arcfour_crypt_blob(dec_buffer.data, dec_buffer.length, &enc_key);
115 ZERO_ARRAY_LEN(enc_key.data, enc_key.length);
118 * the first 4 byte are the crc32 checksum
119 * of the remaining bytes
121 crc32_given = IVAL(dec_buffer.data, 0);
122 crc32_calc = crc32(0, Z_NULL, 0);
123 crc32_calc = crc32(crc32_calc,
124 dec_buffer.data + 4 ,
125 dec_buffer.length - 4);
126 checked_buffer = data_blob_const(dec_buffer.data + 4, dec_buffer.length - 4);
128 plain_buffer = data_blob_talloc(mem_ctx, checked_buffer.data, checked_buffer.length);
129 W_ERROR_HAVE_NO_MEMORY(plain_buffer.data);
131 if (crc32_given != crc32_calc) {
132 result = W_ERROR(HRES_ERROR_V(HRES_SEC_E_DECRYPT_FAILURE));
136 * The following rid_crypt obfuscation isn't session specific
137 * and not really needed here, because we allways know the rid of the
140 * some attributes with this 'additional encryption' include
141 * dBCSPwd, unicodePwd, ntPwdHistory, lmPwdHistory
143 * But for the rest of samba it's easier when we remove this static
147 uint32_t i, num_hashes;
149 if ((checked_buffer.length % 16) != 0) {
150 result = WERR_DS_DRA_INVALID_PARAMETER;
154 num_hashes = plain_buffer.length / 16;
155 for (i = 0; i < num_hashes; i++) {
156 uint32_t offset = i * 16;
157 sam_rid_crypt(rid, checked_buffer.data + offset, plain_buffer.data + offset, 0);
167 WERROR drsuapi_decrypt_attribute(TALLOC_CTX *mem_ctx,
168 const DATA_BLOB *gensec_skey,
170 uint32_t dsdb_repl_flags,
171 struct drsuapi_DsReplicaAttribute *attr)
175 DATA_BLOB plain_data;
176 bool rid_crypt = false;
178 if (attr->value_ctr.num_values == 0) {
182 switch (attr->attid) {
183 case DRSUAPI_ATTID_dBCSPwd:
184 case DRSUAPI_ATTID_unicodePwd:
185 case DRSUAPI_ATTID_ntPwdHistory:
186 case DRSUAPI_ATTID_lmPwdHistory:
189 case DRSUAPI_ATTID_supplementalCredentials:
190 case DRSUAPI_ATTID_priorValue:
191 case DRSUAPI_ATTID_currentValue:
192 case DRSUAPI_ATTID_trustAuthOutgoing:
193 case DRSUAPI_ATTID_trustAuthIncoming:
194 case DRSUAPI_ATTID_initialAuthOutgoing:
195 case DRSUAPI_ATTID_initialAuthIncoming:
201 if (dsdb_repl_flags & DSDB_REPL_FLAG_EXPECT_NO_SECRETS) {
202 return WERR_TOO_MANY_SECRETS;
205 if (attr->value_ctr.num_values > 1) {
206 return WERR_DS_DRA_INVALID_PARAMETER;
209 if (!attr->value_ctr.values[0].blob) {
210 return WERR_DS_DRA_INVALID_PARAMETER;
213 enc_data = attr->value_ctr.values[0].blob;
215 status = drsuapi_decrypt_attribute_value(mem_ctx,
221 W_ERROR_NOT_OK_RETURN(status);
223 talloc_free(attr->value_ctr.values[0].blob->data);
224 *attr->value_ctr.values[0].blob = plain_data;
229 static WERROR drsuapi_encrypt_attribute_value(TALLOC_CTX *mem_ctx,
230 const DATA_BLOB *gensec_skey,
236 DATA_BLOB rid_crypt_out = data_blob(NULL, 0);
237 DATA_BLOB confounder;
240 uint8_t _enc_key[16];
243 DATA_BLOB enc_buffer;
248 * users with rid == 0 should not exist
250 if (rid_crypt && rid == 0) {
251 return WERR_DS_DRA_INVALID_PARAMETER;
255 * The following rid_crypt obfuscation isn't session specific
256 * and not really needed here, because we allways know the rid of the
259 * some attributes with this 'additional encryption' include
260 * dBCSPwd, unicodePwd, ntPwdHistory, lmPwdHistory
262 * But for the rest of samba it's easier when we remove this static
266 uint32_t i, num_hashes;
267 rid_crypt_out = data_blob_talloc(mem_ctx, in->data, in->length);
268 W_ERROR_HAVE_NO_MEMORY(rid_crypt_out.data);
270 if ((rid_crypt_out.length % 16) != 0) {
271 return WERR_DS_DRA_INVALID_PARAMETER;
274 num_hashes = rid_crypt_out.length / 16;
275 for (i = 0; i < num_hashes; i++) {
276 uint32_t offset = i * 16;
277 sam_rid_crypt(rid, in->data + offset, rid_crypt_out.data + offset, 1);
283 * the first 16 bytes at the beginning are the confounder
284 * followed by the 4 byte crc32 checksum
287 enc_buffer = data_blob_talloc(mem_ctx, NULL, in->length+20);
288 if (!enc_buffer.data) {
289 talloc_free(rid_crypt_out.data);
290 return WERR_NOT_ENOUGH_MEMORY;
293 confounder = data_blob_const(enc_buffer.data, 16);
294 generate_random_buffer(confounder.data, confounder.length);
297 * build the encryption key md5 over the session key followed
300 * here the gensec session key is used and
301 * not the dcerpc ncacn_ip_tcp "SystemLibraryDTC" key!
303 enc_key = data_blob_const(_enc_key, sizeof(_enc_key));
305 MD5Update(&md5, gensec_skey->data, gensec_skey->length);
306 MD5Update(&md5, confounder.data, confounder.length);
307 MD5Final(enc_key.data, &md5);
310 * the first 4 byte are the crc32 checksum
311 * of the remaining bytes
313 crc32_calc = crc32(0, Z_NULL, 0);
314 crc32_calc = crc32(crc32_calc, in->data, in->length);
315 SIVAL(enc_buffer.data, 16, crc32_calc);
318 * copy the plain buffer part and
319 * encrypt it using the created encryption key using arcfour
321 memcpy(enc_buffer.data+20, in->data, in->length);
322 talloc_free(rid_crypt_out.data);
324 arcfour_crypt_blob(enc_buffer.data+16, enc_buffer.length-16, &enc_key);
332 encrypt a DRSUAPI attribute ready for sending over the wire
333 Only some attribute types are encrypted
335 WERROR drsuapi_encrypt_attribute(TALLOC_CTX *mem_ctx,
336 const DATA_BLOB *gensec_skey,
338 struct drsuapi_DsReplicaAttribute *attr)
341 DATA_BLOB *plain_data;
343 bool rid_crypt = false;
345 if (attr->value_ctr.num_values == 0) {
349 switch (attr->attid) {
350 case DRSUAPI_ATTID_dBCSPwd:
351 case DRSUAPI_ATTID_unicodePwd:
352 case DRSUAPI_ATTID_ntPwdHistory:
353 case DRSUAPI_ATTID_lmPwdHistory:
356 case DRSUAPI_ATTID_supplementalCredentials:
357 case DRSUAPI_ATTID_priorValue:
358 case DRSUAPI_ATTID_currentValue:
359 case DRSUAPI_ATTID_trustAuthOutgoing:
360 case DRSUAPI_ATTID_trustAuthIncoming:
361 case DRSUAPI_ATTID_initialAuthOutgoing:
362 case DRSUAPI_ATTID_initialAuthIncoming:
368 if (attr->value_ctr.num_values > 1) {
369 return WERR_DS_DRA_INVALID_PARAMETER;
372 if (!attr->value_ctr.values[0].blob) {
373 return WERR_DS_DRA_INVALID_PARAMETER;
376 plain_data = attr->value_ctr.values[0].blob;
378 status = drsuapi_encrypt_attribute_value(mem_ctx,
384 W_ERROR_NOT_OK_RETURN(status);
386 talloc_free(attr->value_ctr.values[0].blob->data);
387 *attr->value_ctr.values[0].blob = enc_data;