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/arcfour.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
,
47 gnutls_hash_hd_t hash_hnd
= NULL
;
55 DATA_BLOB checked_buffer
;
57 DATA_BLOB plain_buffer
;
62 * users with rid == 0 should not exist
64 if (rid_crypt
&& rid
== 0) {
65 return WERR_DS_DRA_INVALID_PARAMETER
;
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);
79 * build the encryption key md5 over the session key followed
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
);
89 result
= WERR_NOT_ENOUGH_MEMORY
;
92 rc
= gnutls_hash(hash_hnd
, gensec_skey
->data
, gensec_skey
->length
);
94 gnutls_hash_deinit(hash_hnd
, NULL
);
95 result
= WERR_INTERNAL_ERROR
;
98 rc
= gnutls_hash(hash_hnd
, confounder
.data
, confounder
.length
);
100 gnutls_hash_deinit(hash_hnd
, NULL
);
101 result
= WERR_INTERNAL_ERROR
;
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
));
135 * The following rid_crypt obfuscation isn't session specific
136 * and not really needed here, because we allways know the rid of the
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
146 uint32_t i
, num_hashes
;
148 if ((checked_buffer
.length
% 16) != 0) {
149 result
= WERR_DS_DRA_INVALID_PARAMETER
;
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);
166 WERROR
drsuapi_decrypt_attribute(TALLOC_CTX
*mem_ctx
,
167 const DATA_BLOB
*gensec_skey
,
169 uint32_t dsdb_repl_flags
,
170 struct drsuapi_DsReplicaAttribute
*attr
)
174 DATA_BLOB plain_data
;
175 bool rid_crypt
= false;
177 if (attr
->value_ctr
.num_values
== 0) {
181 switch (attr
->attid
) {
182 case DRSUAPI_ATTID_dBCSPwd
:
183 case DRSUAPI_ATTID_unicodePwd
:
184 case DRSUAPI_ATTID_ntPwdHistory
:
185 case DRSUAPI_ATTID_lmPwdHistory
:
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
:
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
,
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
;
228 static WERROR
drsuapi_encrypt_attribute_value(TALLOC_CTX
*mem_ctx
,
229 const DATA_BLOB
*gensec_skey
,
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];
242 DATA_BLOB enc_buffer
;
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
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
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);
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
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
);
308 result
= WERR_NOT_ENOUGH_MEMORY
;
312 rc
= gnutls_hash(hash_hnd
, gensec_skey
->data
, gensec_skey
->length
);
314 gnutls_hash_deinit(hash_hnd
, NULL
);
315 result
= WERR_INTERNAL_ERROR
;
318 rc
= gnutls_hash(hash_hnd
, confounder
.data
, confounder
.length
);
320 gnutls_hash_deinit(hash_hnd
, NULL
);
321 result
= WERR_INTERNAL_ERROR
;
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
);
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
,
358 struct drsuapi_DsReplicaAttribute
*attr
)
361 DATA_BLOB
*plain_data
;
363 bool rid_crypt
= false;
365 if (attr
->value_ctr
.num_values
== 0) {
369 switch (attr
->attid
) {
370 case DRSUAPI_ATTID_dBCSPwd
:
371 case DRSUAPI_ATTID_unicodePwd
:
372 case DRSUAPI_ATTID_ntPwdHistory
:
373 case DRSUAPI_ATTID_lmPwdHistory
:
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
:
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
,
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
;