2 Unix SMB/CIFS implementation.
4 dcerpc schannel operations
6 Copyright (C) Andrew Tridgell 2004
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "lib/util/tevent_ntstatus.h"
26 #include "librpc/gen_ndr/ndr_schannel.h"
27 #include "auth/auth.h"
28 #include "auth/credentials/credentials.h"
29 #include "auth/gensec/gensec.h"
30 #include "auth/gensec/gensec_internal.h"
31 #include "auth/gensec/gensec_proto.h"
32 #include "../libcli/auth/schannel.h"
33 #include "librpc/gen_ndr/dcerpc.h"
34 #include "param/param.h"
35 #include "auth/gensec/gensec_toplevel_proto.h"
36 #include "libds/common/roles.h"
38 #ifndef HAVE_GNUTLS_AES_CFB8
39 #include "lib/crypto/aes.h"
42 #include "lib/crypto/gnutls_helpers.h"
43 #include <gnutls/gnutls.h>
44 #include <gnutls/crypto.h>
47 #define DBGC_CLASS DBGC_AUTH
49 struct schannel_state
{
50 struct gensec_security
*gensec
;
53 struct netlogon_creds_CredentialState
*creds
;
54 struct auth_user_info_dc
*user_info_dc
;
57 #define SETUP_SEQNUM(state, buf, initiator) do { \
58 uint8_t *_buf = buf; \
59 uint32_t _seq_num_low = (state)->seq_num & UINT32_MAX; \
60 uint32_t _seq_num_high = (state)->seq_num >> 32; \
62 _seq_num_high |= 0x80000000; \
64 RSIVAL(_buf, 0, _seq_num_low); \
65 RSIVAL(_buf, 4, _seq_num_high); \
68 static struct schannel_state
*netsec_create_state(
69 struct gensec_security
*gensec
,
70 struct netlogon_creds_CredentialState
*creds
,
73 struct schannel_state
*state
;
75 state
= talloc_zero(gensec
, struct schannel_state
);
80 state
->gensec
= gensec
;
81 state
->initiator
= initiator
;
82 state
->creds
= netlogon_creds_copy(state
, creds
);
83 if (state
->creds
== NULL
) {
88 gensec
->private_data
= state
;
93 static void netsec_offset_and_sizes(struct schannel_state
*state
,
95 uint32_t *_min_sig_size
,
96 uint32_t *_used_sig_size
,
97 uint32_t *_checksum_length
,
98 uint32_t *_confounder_ofs
)
100 uint32_t min_sig_size
;
101 uint32_t used_sig_size
;
102 uint32_t checksum_length
;
103 uint32_t confounder_ofs
;
105 if (state
->creds
->negotiate_flags
& NETLOGON_NEG_SUPPORTS_AES
) {
109 * Note: windows has a bug here and uses the old values...
111 * checksum_length = 32;
112 * confounder_ofs = 48;
128 *_min_sig_size
= min_sig_size
;
131 if (_used_sig_size
) {
132 *_used_sig_size
= used_sig_size
;
135 if (_checksum_length
) {
136 *_checksum_length
= checksum_length
;
139 if (_confounder_ofs
) {
140 *_confounder_ofs
= confounder_ofs
;
144 /*******************************************************************
145 Encode or Decode the sequence number (which is symmetric)
146 ********************************************************************/
147 static NTSTATUS
netsec_do_seq_num(struct schannel_state
*state
,
148 const uint8_t *checksum
,
149 uint32_t checksum_length
,
152 if (state
->creds
->negotiate_flags
& NETLOGON_NEG_SUPPORTS_AES
) {
153 #ifdef HAVE_GNUTLS_AES_CFB8
154 gnutls_cipher_hd_t cipher_hnd
= NULL
;
155 gnutls_datum_t key
= {
156 .data
= state
->creds
->session_key
,
157 .size
= sizeof(state
->creds
->session_key
),
160 gnutls_cipher_get_iv_size(GNUTLS_CIPHER_AES_128_CFB8
);
161 uint8_t _iv
[iv_size
];
162 gnutls_datum_t iv
= {
170 memcpy(iv
.data
+ 0, checksum
, 8);
171 memcpy(iv
.data
+ 8, checksum
, 8);
173 rc
= gnutls_cipher_init(&cipher_hnd
,
174 GNUTLS_CIPHER_AES_128_CFB8
,
178 return gnutls_error_to_ntstatus(rc
,
179 NT_STATUS_CRYPTO_SYSTEM_INVALID
);
182 rc
= gnutls_cipher_encrypt(cipher_hnd
, seq_num
, 8);
183 gnutls_cipher_deinit(cipher_hnd
);
185 return gnutls_error_to_ntstatus(rc
,
186 NT_STATUS_CRYPTO_SYSTEM_INVALID
);
189 #else /* NOT HAVE_GNUTLS_AES_CFB8 */
191 uint8_t iv
[AES_BLOCK_SIZE
];
193 AES_set_encrypt_key(state
->creds
->session_key
, 128, &key
);
195 memcpy(iv
+0, checksum
, 8);
196 memcpy(iv
+8, checksum
, 8);
198 aes_cfb8_encrypt(seq_num
, seq_num
, 8, &key
, iv
, AES_ENCRYPT
);
199 #endif /* HAVE_GNUTLS_AES_CFB8 */
201 static const uint8_t zeros
[4];
202 uint8_t _sequence_key
[16];
203 gnutls_cipher_hd_t cipher_hnd
;
204 gnutls_datum_t sequence_key
= {
205 .data
= _sequence_key
,
206 .size
= sizeof(_sequence_key
),
211 rc
= gnutls_hmac_fast(GNUTLS_MAC_MD5
,
212 state
->creds
->session_key
,
213 sizeof(state
->creds
->session_key
),
218 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
221 rc
= gnutls_hmac_fast(GNUTLS_MAC_MD5
,
228 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
233 rc
= gnutls_cipher_init(&cipher_hnd
,
234 GNUTLS_CIPHER_ARCFOUR_128
,
238 ZERO_ARRAY(_sequence_key
);
239 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
242 rc
= gnutls_cipher_encrypt(cipher_hnd
,
245 gnutls_cipher_deinit(cipher_hnd
);
246 ZERO_ARRAY(_sequence_key
);
248 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
257 static NTSTATUS
netsec_do_seal(struct schannel_state
*state
,
258 const uint8_t seq_num
[8],
259 uint8_t confounder
[8],
260 uint8_t *data
, uint32_t length
,
263 if (state
->creds
->negotiate_flags
& NETLOGON_NEG_SUPPORTS_AES
) {
264 #ifdef HAVE_GNUTLS_AES_CFB8
265 gnutls_cipher_hd_t cipher_hnd
= NULL
;
266 uint8_t sess_kf0
[16] = {0};
267 gnutls_datum_t key
= {
269 .size
= sizeof(sess_kf0
),
272 gnutls_cipher_get_iv_size(GNUTLS_CIPHER_AES_128_CFB8
);
273 uint8_t _iv
[iv_size
];
274 gnutls_datum_t iv
= {
281 for (i
= 0; i
< key
.size
; i
++) {
282 key
.data
[i
] = state
->creds
->session_key
[i
] ^ 0xf0;
287 memcpy(iv
.data
+ 0, seq_num
, 8);
288 memcpy(iv
.data
+ 8, seq_num
, 8);
290 rc
= gnutls_cipher_init(&cipher_hnd
,
291 GNUTLS_CIPHER_AES_128_CFB8
,
295 DBG_ERR("ERROR: gnutls_cipher_init: %s\n",
296 gnutls_strerror(rc
));
297 return NT_STATUS_NO_MEMORY
;
301 rc
= gnutls_cipher_encrypt(cipher_hnd
,
305 gnutls_cipher_deinit(cipher_hnd
);
306 return gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
309 rc
= gnutls_cipher_encrypt(cipher_hnd
,
313 gnutls_cipher_deinit(cipher_hnd
);
314 return gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
319 * Workaround bug present in gnutls 3.6.8:
321 * gnutls_cipher_decrypt() uses an optimization
322 * internally that breaks decryption when processing
323 * buffers with their length not being a multiple
327 uint8_t tmp
[16] = { 0, };
328 uint32_t tmp_dlength
= MIN(length
, sizeof(tmp
) - 8);
330 memcpy(tmp
, confounder
, 8);
331 memcpy(tmp
+ 8, data
, tmp_dlength
);
333 rc
= gnutls_cipher_decrypt(cipher_hnd
,
338 gnutls_cipher_deinit(cipher_hnd
);
339 return gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
342 memcpy(confounder
, tmp
, 8);
343 memcpy(data
, tmp
+ 8, tmp_dlength
);
346 if (length
> tmp_dlength
) {
347 rc
= gnutls_cipher_decrypt(cipher_hnd
,
349 length
- tmp_dlength
);
351 gnutls_cipher_deinit(cipher_hnd
);
352 return gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
356 gnutls_cipher_deinit(cipher_hnd
);
357 #else /* NOT HAVE_GNUTLS_AES_CFB8 */
359 uint8_t iv
[AES_BLOCK_SIZE
];
360 uint8_t sess_kf0
[16];
363 for (i
= 0; i
< 16; i
++) {
364 sess_kf0
[i
] = state
->creds
->session_key
[i
] ^ 0xf0;
367 AES_set_encrypt_key(sess_kf0
, 128, &key
);
369 memcpy(iv
+0, seq_num
, 8);
370 memcpy(iv
+8, seq_num
, 8);
373 aes_cfb8_encrypt(confounder
, confounder
, 8, &key
, iv
, AES_ENCRYPT
);
374 aes_cfb8_encrypt(data
, data
, length
, &key
, iv
, AES_ENCRYPT
);
376 aes_cfb8_encrypt(confounder
, confounder
, 8, &key
, iv
, AES_DECRYPT
);
377 aes_cfb8_encrypt(data
, data
, length
, &key
, iv
, AES_DECRYPT
);
379 #endif /* HAVE_GNUTLS_AES_CFB8 */
381 gnutls_cipher_hd_t cipher_hnd
;
382 uint8_t _sealing_key
[16];
383 gnutls_datum_t sealing_key
= {
384 .data
= _sealing_key
,
385 .size
= sizeof(_sealing_key
),
387 static const uint8_t zeros
[4];
389 uint8_t sess_kf0
[16];
393 for (i
= 0; i
< 16; i
++) {
394 sess_kf0
[i
] = state
->creds
->session_key
[i
] ^ 0xf0;
397 rc
= gnutls_hmac_fast(GNUTLS_MAC_MD5
,
405 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
408 rc
= gnutls_hmac_fast(GNUTLS_MAC_MD5
,
417 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
420 rc
= gnutls_cipher_init(&cipher_hnd
,
421 GNUTLS_CIPHER_ARCFOUR_128
,
425 ZERO_ARRAY(_sealing_key
);
426 return gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
428 rc
= gnutls_cipher_encrypt(cipher_hnd
,
432 ZERO_ARRAY(_sealing_key
);
433 return gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
435 gnutls_cipher_deinit(cipher_hnd
);
436 rc
= gnutls_cipher_init(&cipher_hnd
,
437 GNUTLS_CIPHER_ARCFOUR_128
,
441 ZERO_ARRAY(_sealing_key
);
442 return gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
444 rc
= gnutls_cipher_encrypt(cipher_hnd
,
447 gnutls_cipher_deinit(cipher_hnd
);
448 ZERO_ARRAY(_sealing_key
);
450 return gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
457 /*******************************************************************
458 Create a digest over the entire packet (including the data), and
459 MD5 it with the session key.
460 ********************************************************************/
461 static NTSTATUS
netsec_do_sign(struct schannel_state
*state
,
462 const uint8_t *confounder
,
463 const uint8_t *data
, size_t length
,
467 if (state
->creds
->negotiate_flags
& NETLOGON_NEG_SUPPORTS_AES
) {
468 gnutls_hmac_hd_t hmac_hnd
= NULL
;
471 rc
= gnutls_hmac_init(&hmac_hnd
,
473 state
->creds
->session_key
,
474 sizeof(state
->creds
->session_key
));
476 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
480 SSVAL(header
, 0, NL_SIGN_HMAC_SHA256
);
481 SSVAL(header
, 2, NL_SEAL_AES128
);
482 SSVAL(header
, 4, 0xFFFF);
483 SSVAL(header
, 6, 0x0000);
485 rc
= gnutls_hmac(hmac_hnd
, header
, 8);
487 gnutls_hmac_deinit(hmac_hnd
, NULL
);
488 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
490 rc
= gnutls_hmac(hmac_hnd
, confounder
, 8);
492 gnutls_hmac_deinit(hmac_hnd
, NULL
);
493 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
496 SSVAL(header
, 0, NL_SIGN_HMAC_SHA256
);
497 SSVAL(header
, 2, NL_SEAL_NONE
);
498 SSVAL(header
, 4, 0xFFFF);
499 SSVAL(header
, 6, 0x0000);
501 rc
= gnutls_hmac(hmac_hnd
, header
, 8);
503 gnutls_hmac_deinit(hmac_hnd
, NULL
);
504 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
508 rc
= gnutls_hmac(hmac_hnd
, data
, length
);
510 gnutls_hmac_deinit(hmac_hnd
, NULL
);
511 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
514 gnutls_hmac_deinit(hmac_hnd
, checksum
);
516 uint8_t packet_digest
[16];
517 static const uint8_t zeros
[4];
518 gnutls_hash_hd_t hash_hnd
= NULL
;
521 rc
= gnutls_hash_init(&hash_hnd
, GNUTLS_DIG_MD5
);
523 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
526 rc
= gnutls_hash(hash_hnd
, zeros
, sizeof(zeros
));
528 gnutls_hash_deinit(hash_hnd
, NULL
);
529 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
532 SSVAL(header
, 0, NL_SIGN_HMAC_MD5
);
533 SSVAL(header
, 2, NL_SEAL_RC4
);
534 SSVAL(header
, 4, 0xFFFF);
535 SSVAL(header
, 6, 0x0000);
537 rc
= gnutls_hash(hash_hnd
, header
, 8);
539 gnutls_hash_deinit(hash_hnd
, NULL
);
540 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
542 rc
= gnutls_hash(hash_hnd
, confounder
, 8);
544 gnutls_hash_deinit(hash_hnd
, NULL
);
545 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
548 SSVAL(header
, 0, NL_SIGN_HMAC_MD5
);
549 SSVAL(header
, 2, NL_SEAL_NONE
);
550 SSVAL(header
, 4, 0xFFFF);
551 SSVAL(header
, 6, 0x0000);
553 rc
= gnutls_hash(hash_hnd
, header
, 8);
555 gnutls_hash_deinit(hash_hnd
, NULL
);
556 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
559 rc
= gnutls_hash(hash_hnd
, data
, length
);
561 gnutls_hash_deinit(hash_hnd
, NULL
);
562 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
564 gnutls_hash_deinit(hash_hnd
, packet_digest
);
566 rc
= gnutls_hmac_fast(GNUTLS_MAC_MD5
,
567 state
->creds
->session_key
,
568 sizeof(state
->creds
->session_key
),
570 sizeof(packet_digest
),
572 ZERO_ARRAY(packet_digest
);
574 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
581 static NTSTATUS
netsec_incoming_packet(struct schannel_state
*state
,
583 uint8_t *data
, size_t length
,
584 const uint8_t *whole_pdu
, size_t pdu_length
,
585 const DATA_BLOB
*sig
)
587 uint32_t min_sig_size
= 0;
589 uint8_t checksum
[32];
590 uint32_t checksum_length
= sizeof(checksum_length
);
591 uint8_t _confounder
[8];
592 uint8_t *confounder
= NULL
;
593 uint32_t confounder_ofs
= 0;
596 const uint8_t *sign_data
= NULL
;
597 size_t sign_length
= 0;
600 netsec_offset_and_sizes(state
,
607 if (sig
->length
< min_sig_size
) {
608 return NT_STATUS_ACCESS_DENIED
;
612 confounder
= _confounder
;
613 memcpy(confounder
, sig
->data
+confounder_ofs
, 8);
618 SETUP_SEQNUM(state
, seq_num
, !state
->initiator
);
621 status
= netsec_do_seal(state
,
627 if (!NT_STATUS_IS_OK(status
)) {
628 DBG_WARNING("netsec_do_seal failed: %s\n", nt_errstr(status
));
629 return NT_STATUS_ACCESS_DENIED
;
633 if (state
->gensec
->want_features
& GENSEC_FEATURE_SIGN_PKT_HEADER
) {
634 sign_data
= whole_pdu
;
635 sign_length
= pdu_length
;
638 sign_length
= length
;
641 status
= netsec_do_sign(state
,
647 if (!NT_STATUS_IS_OK(status
)) {
648 DBG_WARNING("netsec_do_sign failed: %s\n", nt_errstr(status
));
649 return NT_STATUS_ACCESS_DENIED
;
652 ret
= mem_equal_const_time(checksum
, sig
->data
+16, checksum_length
);
654 dump_data_pw("calc digest:", checksum
, checksum_length
);
655 dump_data_pw("wire digest:", sig
->data
+16, checksum_length
);
656 return NT_STATUS_ACCESS_DENIED
;
659 status
= netsec_do_seq_num(state
, checksum
, checksum_length
, seq_num
);
660 if (!NT_STATUS_IS_OK(status
)) {
661 DBG_WARNING("netsec_do_seq_num failed: %s\n",
666 ZERO_ARRAY(checksum
);
668 ret
= mem_equal_const_time(seq_num
, sig
->data
+8, 8);
670 dump_data_pw("calc seq num:", seq_num
, 8);
671 dump_data_pw("wire seq num:", sig
->data
+8, 8);
672 return NT_STATUS_ACCESS_DENIED
;
678 static uint32_t netsec_outgoing_sig_size(struct schannel_state
*state
)
680 uint32_t sig_size
= 0;
682 netsec_offset_and_sizes(state
,
692 static NTSTATUS
netsec_outgoing_packet(struct schannel_state
*state
,
695 uint8_t *data
, size_t length
,
696 const uint8_t *whole_pdu
, size_t pdu_length
,
699 uint32_t min_sig_size
= 0;
700 uint32_t used_sig_size
= 0;
702 uint8_t checksum
[32];
703 uint32_t checksum_length
= sizeof(checksum_length
);
704 uint8_t _confounder
[8];
705 uint8_t *confounder
= NULL
;
706 uint32_t confounder_ofs
= 0;
708 const uint8_t *sign_data
= NULL
;
709 size_t sign_length
= 0;
712 netsec_offset_and_sizes(state
,
719 SETUP_SEQNUM(state
, seq_num
, state
->initiator
);
722 confounder
= _confounder
;
723 generate_random_buffer(confounder
, 8);
728 if (state
->gensec
->want_features
& GENSEC_FEATURE_SIGN_PKT_HEADER
) {
729 sign_data
= whole_pdu
;
730 sign_length
= pdu_length
;
733 sign_length
= length
;
736 status
= netsec_do_sign(state
,
742 if (!NT_STATUS_IS_OK(status
)) {
743 DBG_WARNING("netsec_do_sign failed: %s\n", nt_errstr(status
));
744 return NT_STATUS_ACCESS_DENIED
;
748 status
= netsec_do_seal(state
,
754 if (!NT_STATUS_IS_OK(status
)) {
755 DBG_WARNING("netsec_do_seal failed: %s\n",
761 status
= netsec_do_seq_num(state
, checksum
, checksum_length
, seq_num
);
762 if (!NT_STATUS_IS_OK(status
)) {
763 DBG_WARNING("netsec_do_seq_num failed: %s\n",
768 (*sig
) = data_blob_talloc_zero(mem_ctx
, used_sig_size
);
770 memcpy(sig
->data
, header
, 8);
771 memcpy(sig
->data
+8, seq_num
, 8);
772 memcpy(sig
->data
+16, checksum
, checksum_length
);
775 memcpy(sig
->data
+confounder_ofs
, confounder
, 8);
778 dump_data_pw("signature:", sig
->data
+ 0, 8);
779 dump_data_pw("seq_num :", sig
->data
+ 8, 8);
780 dump_data_pw("digest :", sig
->data
+16, checksum_length
);
781 dump_data_pw("confound :", sig
->data
+confounder_ofs
, 8);
786 _PUBLIC_ NTSTATUS
gensec_schannel_init(TALLOC_CTX
*ctx
);
788 static size_t schannel_sig_size(struct gensec_security
*gensec_security
, size_t data_size
)
790 struct schannel_state
*state
=
791 talloc_get_type_abort(gensec_security
->private_data
,
792 struct schannel_state
);
794 return netsec_outgoing_sig_size(state
);
797 struct schannel_update_state
{
802 static NTSTATUS
schannel_update_internal(struct gensec_security
*gensec_security
,
803 TALLOC_CTX
*out_mem_ctx
,
804 const DATA_BLOB in
, DATA_BLOB
*out
);
806 static struct tevent_req
*schannel_update_send(TALLOC_CTX
*mem_ctx
,
807 struct tevent_context
*ev
,
808 struct gensec_security
*gensec_security
,
811 struct tevent_req
*req
;
812 struct schannel_update_state
*state
= NULL
;
815 req
= tevent_req_create(mem_ctx
, &state
,
816 struct schannel_update_state
);
821 status
= schannel_update_internal(gensec_security
,
824 state
->status
= status
;
825 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
826 status
= NT_STATUS_OK
;
828 if (tevent_req_nterror(req
, status
)) {
829 return tevent_req_post(req
, ev
);
832 tevent_req_done(req
);
833 return tevent_req_post(req
, ev
);
836 static NTSTATUS
schannel_update_internal(struct gensec_security
*gensec_security
,
837 TALLOC_CTX
*out_mem_ctx
,
838 const DATA_BLOB in
, DATA_BLOB
*out
)
840 struct schannel_state
*state
=
841 talloc_get_type(gensec_security
->private_data
,
842 struct schannel_state
);
844 enum ndr_err_code ndr_err
;
845 struct NL_AUTH_MESSAGE bind_schannel
= {
848 struct NL_AUTH_MESSAGE bind_schannel_ack
;
849 struct netlogon_creds_CredentialState
*creds
;
850 const char *workstation
;
853 *out
= data_blob(NULL
, 0);
855 if (gensec_security
->dcerpc_auth_level
< DCERPC_AUTH_LEVEL_INTEGRITY
) {
856 switch (gensec_security
->gensec_role
) {
858 return NT_STATUS_INVALID_PARAMETER_MIX
;
860 return NT_STATUS_INVALID_PARAMETER
;
862 return NT_STATUS_INTERNAL_ERROR
;
865 switch (gensec_security
->gensec_role
) {
868 /* we could parse the bind ack, but we don't know what it is yet */
872 creds
= cli_credentials_get_netlogon_creds(gensec_security
->credentials
);
874 return NT_STATUS_INVALID_PARAMETER_MIX
;
877 state
= netsec_create_state(gensec_security
,
878 creds
, true /* initiator */);
880 return NT_STATUS_NO_MEMORY
;
883 bind_schannel
.MessageType
= NL_NEGOTIATE_REQUEST
;
885 bind_schannel
.Flags
= NL_FLAG_OEM_NETBIOS_DOMAIN_NAME
|
886 NL_FLAG_OEM_NETBIOS_COMPUTER_NAME
;
887 bind_schannel
.oem_netbios_domain
.a
= cli_credentials_get_domain(gensec_security
->credentials
);
888 bind_schannel
.oem_netbios_computer
.a
= creds
->computer_name
;
890 if (creds
->secure_channel_type
== SEC_CHAN_DNS_DOMAIN
) {
891 bind_schannel
.Flags
|= NL_FLAG_UTF8_DNS_DOMAIN_NAME
;
892 bind_schannel
.utf8_dns_domain
.u
= cli_credentials_get_realm(gensec_security
->credentials
);
894 bind_schannel
.Flags
|= NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME
;
895 bind_schannel
.utf8_netbios_computer
.u
= creds
->computer_name
;
898 ndr_err
= ndr_push_struct_blob(out
, out_mem_ctx
, &bind_schannel
,
899 (ndr_push_flags_fn_t
)ndr_push_NL_AUTH_MESSAGE
);
900 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
901 status
= ndr_map_error2ntstatus(ndr_err
);
902 DEBUG(3, ("Could not create schannel bind: %s\n",
907 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
911 /* no third leg on this protocol */
912 return NT_STATUS_INVALID_PARAMETER
;
915 /* parse the schannel startup blob */
916 ndr_err
= ndr_pull_struct_blob(&in
, out_mem_ctx
, &bind_schannel
,
917 (ndr_pull_flags_fn_t
)ndr_pull_NL_AUTH_MESSAGE
);
918 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
919 status
= ndr_map_error2ntstatus(ndr_err
);
920 DEBUG(3, ("Could not parse incoming schannel bind: %s\n",
925 if (bind_schannel
.Flags
& NL_FLAG_OEM_NETBIOS_DOMAIN_NAME
) {
926 domain
= bind_schannel
.oem_netbios_domain
.a
;
927 if (strcasecmp_m(domain
, lpcfg_workgroup(gensec_security
->settings
->lp_ctx
)) != 0) {
928 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
929 domain
, lpcfg_workgroup(gensec_security
->settings
->lp_ctx
)));
930 return NT_STATUS_LOGON_FAILURE
;
932 } else if (bind_schannel
.Flags
& NL_FLAG_UTF8_DNS_DOMAIN_NAME
) {
933 domain
= bind_schannel
.utf8_dns_domain
.u
;
934 if (strcasecmp_m(domain
, lpcfg_dnsdomain(gensec_security
->settings
->lp_ctx
)) != 0) {
935 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
936 domain
, lpcfg_dnsdomain(gensec_security
->settings
->lp_ctx
)));
937 return NT_STATUS_LOGON_FAILURE
;
940 DEBUG(3, ("Request for schannel to without domain\n"));
941 return NT_STATUS_LOGON_FAILURE
;
944 if (bind_schannel
.Flags
& NL_FLAG_OEM_NETBIOS_COMPUTER_NAME
) {
945 workstation
= bind_schannel
.oem_netbios_computer
.a
;
946 } else if (bind_schannel
.Flags
& NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME
) {
947 workstation
= bind_schannel
.utf8_netbios_computer
.u
;
949 DEBUG(3, ("Request for schannel to without netbios workstation\n"));
950 return NT_STATUS_LOGON_FAILURE
;
953 status
= schannel_get_creds_state(out_mem_ctx
,
954 gensec_security
->settings
->lp_ctx
,
955 workstation
, &creds
);
956 if (!NT_STATUS_IS_OK(status
)) {
957 DEBUG(3, ("Could not find session key for attempted schannel connection from %s: %s\n",
958 workstation
, nt_errstr(status
)));
959 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_HANDLE
)) {
960 return NT_STATUS_LOGON_FAILURE
;
965 state
= netsec_create_state(gensec_security
,
966 creds
, false /* not initiator */);
968 return NT_STATUS_NO_MEMORY
;
971 status
= auth_anonymous_user_info_dc(state
,
972 lpcfg_netbios_name(gensec_security
->settings
->lp_ctx
),
973 &state
->user_info_dc
);
974 if (!NT_STATUS_IS_OK(status
)) {
978 bind_schannel_ack
.MessageType
= NL_NEGOTIATE_RESPONSE
;
979 bind_schannel_ack
.Flags
= 0;
980 bind_schannel_ack
.Buffer
.dummy
= 0x6c0000; /* actually I think
985 ndr_err
= ndr_push_struct_blob(out
, out_mem_ctx
, &bind_schannel_ack
,
986 (ndr_push_flags_fn_t
)ndr_push_NL_AUTH_MESSAGE
);
987 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
988 status
= ndr_map_error2ntstatus(ndr_err
);
989 DEBUG(3, ("Could not return schannel bind ack for client %s: %s\n",
990 workstation
, nt_errstr(status
)));
996 return NT_STATUS_INVALID_PARAMETER
;
999 static NTSTATUS
schannel_update_recv(struct tevent_req
*req
,
1000 TALLOC_CTX
*out_mem_ctx
,
1003 struct schannel_update_state
*state
=
1004 tevent_req_data(req
,
1005 struct schannel_update_state
);
1008 *out
= data_blob_null
;
1010 if (tevent_req_is_nterror(req
, &status
)) {
1011 tevent_req_received(req
);
1015 status
= state
->status
;
1016 talloc_steal(out_mem_ctx
, state
->out
.data
);
1018 tevent_req_received(req
);
1023 * Returns anonymous credentials for schannel, matching Win2k3.
1027 static NTSTATUS
schannel_session_info(struct gensec_security
*gensec_security
,
1028 TALLOC_CTX
*mem_ctx
,
1029 struct auth_session_info
**_session_info
)
1031 struct schannel_state
*state
=
1032 talloc_get_type(gensec_security
->private_data
,
1033 struct schannel_state
);
1034 struct auth4_context
*auth_ctx
= gensec_security
->auth_context
;
1035 struct auth_session_info
*session_info
= NULL
;
1036 uint32_t session_info_flags
= 0;
1039 if (auth_ctx
== NULL
) {
1040 DEBUG(0, ("Cannot generate a session_info without the auth_context\n"));
1041 return NT_STATUS_INTERNAL_ERROR
;
1044 if (auth_ctx
->generate_session_info
== NULL
) {
1045 DEBUG(0, ("Cannot generate a session_info without the generate_session_info hook\n"));
1046 return NT_STATUS_INTERNAL_ERROR
;
1049 if (gensec_security
->want_features
& GENSEC_FEATURE_UNIX_TOKEN
) {
1050 session_info_flags
|= AUTH_SESSION_INFO_UNIX_TOKEN
;
1053 session_info_flags
|= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES
;
1055 status
= auth_ctx
->generate_session_info(
1058 state
->user_info_dc
,
1059 state
->user_info_dc
->info
->account_name
,
1062 if (!NT_STATUS_IS_OK(status
)) {
1066 *_session_info
= session_info
;
1067 return NT_STATUS_OK
;
1071 * Reduce the attack surface by ensuring schannel is not availble when
1074 static NTSTATUS
schannel_server_start(struct gensec_security
*gensec_security
)
1076 enum server_role server_role
1077 = lpcfg_server_role(gensec_security
->settings
->lp_ctx
);
1079 switch (server_role
) {
1080 case ROLE_DOMAIN_BDC
:
1081 case ROLE_DOMAIN_PDC
:
1082 case ROLE_ACTIVE_DIRECTORY_DC
:
1084 return NT_STATUS_OK
;
1086 return NT_STATUS_NOT_IMPLEMENTED
;
1090 static NTSTATUS
schannel_client_start(struct gensec_security
*gensec_security
)
1092 return NT_STATUS_OK
;
1095 static bool schannel_have_feature(struct gensec_security
*gensec_security
,
1098 if (gensec_security
->dcerpc_auth_level
>= DCERPC_AUTH_LEVEL_INTEGRITY
) {
1099 if (feature
& GENSEC_FEATURE_SIGN
) {
1103 if (gensec_security
->dcerpc_auth_level
== DCERPC_AUTH_LEVEL_PRIVACY
) {
1104 if (feature
& GENSEC_FEATURE_SEAL
) {
1108 if (feature
& GENSEC_FEATURE_DCE_STYLE
) {
1111 if (feature
& GENSEC_FEATURE_SIGN_PKT_HEADER
) {
1120 static NTSTATUS
schannel_unseal_packet(struct gensec_security
*gensec_security
,
1121 uint8_t *data
, size_t length
,
1122 const uint8_t *whole_pdu
, size_t pdu_length
,
1123 const DATA_BLOB
*sig
)
1125 struct schannel_state
*state
=
1126 talloc_get_type_abort(gensec_security
->private_data
,
1127 struct schannel_state
);
1129 return netsec_incoming_packet(state
, true,
1130 discard_const_p(uint8_t, data
),
1132 whole_pdu
, pdu_length
,
1137 check the signature on a packet
1139 static NTSTATUS
schannel_check_packet(struct gensec_security
*gensec_security
,
1140 const uint8_t *data
, size_t length
,
1141 const uint8_t *whole_pdu
, size_t pdu_length
,
1142 const DATA_BLOB
*sig
)
1144 struct schannel_state
*state
=
1145 talloc_get_type_abort(gensec_security
->private_data
,
1146 struct schannel_state
);
1148 return netsec_incoming_packet(state
, false,
1149 discard_const_p(uint8_t, data
),
1151 whole_pdu
, pdu_length
,
1157 static NTSTATUS
schannel_seal_packet(struct gensec_security
*gensec_security
,
1158 TALLOC_CTX
*mem_ctx
,
1159 uint8_t *data
, size_t length
,
1160 const uint8_t *whole_pdu
, size_t pdu_length
,
1163 struct schannel_state
*state
=
1164 talloc_get_type_abort(gensec_security
->private_data
,
1165 struct schannel_state
);
1167 return netsec_outgoing_packet(state
, mem_ctx
, true,
1169 whole_pdu
, pdu_length
,
1176 static NTSTATUS
schannel_sign_packet(struct gensec_security
*gensec_security
,
1177 TALLOC_CTX
*mem_ctx
,
1178 const uint8_t *data
, size_t length
,
1179 const uint8_t *whole_pdu
, size_t pdu_length
,
1182 struct schannel_state
*state
=
1183 talloc_get_type_abort(gensec_security
->private_data
,
1184 struct schannel_state
);
1186 return netsec_outgoing_packet(state
, mem_ctx
, false,
1187 discard_const_p(uint8_t, data
),
1189 whole_pdu
, pdu_length
,
1193 static const struct gensec_security_ops gensec_schannel_security_ops
= {
1195 .auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
,
1196 .client_start
= schannel_client_start
,
1197 .server_start
= schannel_server_start
,
1198 .update_send
= schannel_update_send
,
1199 .update_recv
= schannel_update_recv
,
1200 .seal_packet
= schannel_seal_packet
,
1201 .sign_packet
= schannel_sign_packet
,
1202 .check_packet
= schannel_check_packet
,
1203 .unseal_packet
= schannel_unseal_packet
,
1204 .session_info
= schannel_session_info
,
1205 .sig_size
= schannel_sig_size
,
1206 .have_feature
= schannel_have_feature
,
1208 .priority
= GENSEC_SCHANNEL
1211 _PUBLIC_ NTSTATUS
gensec_schannel_init(TALLOC_CTX
*ctx
)
1214 ret
= gensec_register(ctx
, &gensec_schannel_security_ops
);
1215 if (!NT_STATUS_IS_OK(ret
)) {
1216 DEBUG(0,("Failed to register '%s' gensec backend!\n",
1217 gensec_schannel_security_ops
.name
));