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 #include "lib/crypto/gnutls_helpers.h"
39 #include <gnutls/gnutls.h>
40 #include <gnutls/crypto.h>
43 #define DBGC_CLASS DBGC_AUTH
45 struct schannel_state
{
46 struct gensec_security
*gensec
;
49 struct netlogon_creds_CredentialState
*creds
;
50 struct auth_user_info_dc
*user_info_dc
;
53 #define SETUP_SEQNUM(state, buf, initiator) do { \
54 uint8_t *_buf = buf; \
55 uint32_t _seq_num_low = (state)->seq_num & UINT32_MAX; \
56 uint32_t _seq_num_high = (state)->seq_num >> 32; \
58 _seq_num_high |= 0x80000000; \
60 RSIVAL(_buf, 0, _seq_num_low); \
61 RSIVAL(_buf, 4, _seq_num_high); \
64 static struct schannel_state
*netsec_create_state(
65 struct gensec_security
*gensec
,
66 struct netlogon_creds_CredentialState
*creds
,
69 struct schannel_state
*state
;
71 state
= talloc_zero(gensec
, struct schannel_state
);
76 state
->gensec
= gensec
;
77 state
->initiator
= initiator
;
78 state
->creds
= netlogon_creds_copy(state
, creds
);
79 if (state
->creds
== NULL
) {
84 gensec
->private_data
= state
;
89 static void netsec_offset_and_sizes(struct schannel_state
*state
,
91 uint32_t *_min_sig_size
,
92 uint32_t *_used_sig_size
,
93 uint32_t *_checksum_length
,
94 uint32_t *_confounder_ofs
)
96 uint32_t min_sig_size
;
97 uint32_t used_sig_size
;
98 uint32_t checksum_length
;
99 uint32_t confounder_ofs
;
101 if (state
->creds
->negotiate_flags
& NETLOGON_NEG_SUPPORTS_AES
) {
105 * Note: windows has a bug here and uses the old values...
107 * checksum_length = 32;
108 * confounder_ofs = 48;
124 *_min_sig_size
= min_sig_size
;
127 if (_used_sig_size
) {
128 *_used_sig_size
= used_sig_size
;
131 if (_checksum_length
) {
132 *_checksum_length
= checksum_length
;
135 if (_confounder_ofs
) {
136 *_confounder_ofs
= confounder_ofs
;
140 /*******************************************************************
141 Encode or Decode the sequence number (which is symmetric)
142 ********************************************************************/
143 static NTSTATUS
netsec_do_seq_num(struct schannel_state
*state
,
144 const uint8_t *checksum
,
145 uint32_t checksum_length
,
148 if (state
->creds
->negotiate_flags
& NETLOGON_NEG_SUPPORTS_AES
) {
149 gnutls_cipher_hd_t cipher_hnd
= NULL
;
150 gnutls_datum_t key
= {
151 .data
= state
->creds
->session_key
,
152 .size
= sizeof(state
->creds
->session_key
),
155 gnutls_cipher_get_iv_size(GNUTLS_CIPHER_AES_128_CFB8
);
156 uint8_t _iv
[iv_size
];
157 gnutls_datum_t iv
= {
165 memcpy(iv
.data
+ 0, checksum
, 8);
166 memcpy(iv
.data
+ 8, checksum
, 8);
168 rc
= gnutls_cipher_init(&cipher_hnd
,
169 GNUTLS_CIPHER_AES_128_CFB8
,
173 return gnutls_error_to_ntstatus(rc
,
174 NT_STATUS_CRYPTO_SYSTEM_INVALID
);
177 rc
= gnutls_cipher_encrypt(cipher_hnd
, seq_num
, 8);
178 gnutls_cipher_deinit(cipher_hnd
);
180 return gnutls_error_to_ntstatus(rc
,
181 NT_STATUS_CRYPTO_SYSTEM_INVALID
);
185 static const uint8_t zeros
[4];
186 uint8_t _sequence_key
[16];
187 gnutls_cipher_hd_t cipher_hnd
;
188 gnutls_datum_t sequence_key
= {
189 .data
= _sequence_key
,
190 .size
= sizeof(_sequence_key
),
195 rc
= gnutls_hmac_fast(GNUTLS_MAC_MD5
,
196 state
->creds
->session_key
,
197 sizeof(state
->creds
->session_key
),
202 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
205 rc
= gnutls_hmac_fast(GNUTLS_MAC_MD5
,
212 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
217 rc
= gnutls_cipher_init(&cipher_hnd
,
218 GNUTLS_CIPHER_ARCFOUR_128
,
222 ZERO_ARRAY(_sequence_key
);
223 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
226 rc
= gnutls_cipher_encrypt(cipher_hnd
,
229 gnutls_cipher_deinit(cipher_hnd
);
230 ZERO_ARRAY(_sequence_key
);
232 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
241 static NTSTATUS
netsec_do_seal(struct schannel_state
*state
,
242 const uint8_t seq_num
[8],
243 uint8_t confounder
[8],
244 uint8_t *data
, uint32_t length
,
247 if (state
->creds
->negotiate_flags
& NETLOGON_NEG_SUPPORTS_AES
) {
248 gnutls_cipher_hd_t cipher_hnd
= NULL
;
249 uint8_t sess_kf0
[16] = {0};
250 gnutls_datum_t key
= {
252 .size
= sizeof(sess_kf0
),
255 gnutls_cipher_get_iv_size(GNUTLS_CIPHER_AES_128_CFB8
);
256 uint8_t _iv
[iv_size
];
257 gnutls_datum_t iv
= {
264 for (i
= 0; i
< key
.size
; i
++) {
265 key
.data
[i
] = state
->creds
->session_key
[i
] ^ 0xf0;
270 memcpy(iv
.data
+ 0, seq_num
, 8);
271 memcpy(iv
.data
+ 8, seq_num
, 8);
273 rc
= gnutls_cipher_init(&cipher_hnd
,
274 GNUTLS_CIPHER_AES_128_CFB8
,
278 DBG_ERR("ERROR: gnutls_cipher_init: %s\n",
279 gnutls_strerror(rc
));
280 return NT_STATUS_NO_MEMORY
;
284 rc
= gnutls_cipher_encrypt(cipher_hnd
,
288 gnutls_cipher_deinit(cipher_hnd
);
289 return gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
292 rc
= gnutls_cipher_encrypt(cipher_hnd
,
296 gnutls_cipher_deinit(cipher_hnd
);
297 return gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
302 * Workaround bug present in gnutls 3.6.8:
304 * gnutls_cipher_decrypt() uses an optimization
305 * internally that breaks decryption when processing
306 * buffers with their length not being a multiple
310 uint8_t tmp
[16] = { 0, };
311 uint32_t tmp_dlength
= MIN(length
, sizeof(tmp
) - 8);
313 memcpy(tmp
, confounder
, 8);
314 memcpy(tmp
+ 8, data
, tmp_dlength
);
316 rc
= gnutls_cipher_decrypt(cipher_hnd
,
321 gnutls_cipher_deinit(cipher_hnd
);
322 return gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
325 memcpy(confounder
, tmp
, 8);
326 memcpy(data
, tmp
+ 8, tmp_dlength
);
329 if (length
> tmp_dlength
) {
330 rc
= gnutls_cipher_decrypt(cipher_hnd
,
332 length
- tmp_dlength
);
334 gnutls_cipher_deinit(cipher_hnd
);
335 return gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
339 gnutls_cipher_deinit(cipher_hnd
);
341 gnutls_cipher_hd_t cipher_hnd
;
342 uint8_t _sealing_key
[16];
343 gnutls_datum_t sealing_key
= {
344 .data
= _sealing_key
,
345 .size
= sizeof(_sealing_key
),
347 static const uint8_t zeros
[4];
349 uint8_t sess_kf0
[16];
353 for (i
= 0; i
< 16; i
++) {
354 sess_kf0
[i
] = state
->creds
->session_key
[i
] ^ 0xf0;
357 rc
= gnutls_hmac_fast(GNUTLS_MAC_MD5
,
365 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
368 rc
= gnutls_hmac_fast(GNUTLS_MAC_MD5
,
377 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
380 rc
= gnutls_cipher_init(&cipher_hnd
,
381 GNUTLS_CIPHER_ARCFOUR_128
,
385 ZERO_ARRAY(_sealing_key
);
386 return gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
388 rc
= gnutls_cipher_encrypt(cipher_hnd
,
392 ZERO_ARRAY(_sealing_key
);
393 return gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
395 gnutls_cipher_deinit(cipher_hnd
);
396 rc
= gnutls_cipher_init(&cipher_hnd
,
397 GNUTLS_CIPHER_ARCFOUR_128
,
401 ZERO_ARRAY(_sealing_key
);
402 return gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
404 rc
= gnutls_cipher_encrypt(cipher_hnd
,
407 gnutls_cipher_deinit(cipher_hnd
);
408 ZERO_ARRAY(_sealing_key
);
410 return gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
417 /*******************************************************************
418 Create a digest over the entire packet (including the data), and
419 MD5 it with the session key.
420 ********************************************************************/
421 static NTSTATUS
netsec_do_sign(struct schannel_state
*state
,
422 const uint8_t *confounder
,
423 const uint8_t *data
, size_t length
,
427 if (state
->creds
->negotiate_flags
& NETLOGON_NEG_SUPPORTS_AES
) {
428 gnutls_hmac_hd_t hmac_hnd
= NULL
;
431 rc
= gnutls_hmac_init(&hmac_hnd
,
433 state
->creds
->session_key
,
434 sizeof(state
->creds
->session_key
));
436 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
440 SSVAL(header
, 0, NL_SIGN_HMAC_SHA256
);
441 SSVAL(header
, 2, NL_SEAL_AES128
);
442 SSVAL(header
, 4, 0xFFFF);
443 SSVAL(header
, 6, 0x0000);
445 rc
= gnutls_hmac(hmac_hnd
, header
, 8);
447 gnutls_hmac_deinit(hmac_hnd
, NULL
);
448 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
450 rc
= gnutls_hmac(hmac_hnd
, confounder
, 8);
452 gnutls_hmac_deinit(hmac_hnd
, NULL
);
453 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
456 SSVAL(header
, 0, NL_SIGN_HMAC_SHA256
);
457 SSVAL(header
, 2, NL_SEAL_NONE
);
458 SSVAL(header
, 4, 0xFFFF);
459 SSVAL(header
, 6, 0x0000);
461 rc
= gnutls_hmac(hmac_hnd
, header
, 8);
463 gnutls_hmac_deinit(hmac_hnd
, NULL
);
464 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
468 rc
= gnutls_hmac(hmac_hnd
, data
, length
);
470 gnutls_hmac_deinit(hmac_hnd
, NULL
);
471 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
474 gnutls_hmac_deinit(hmac_hnd
, checksum
);
476 uint8_t packet_digest
[16];
477 static const uint8_t zeros
[4];
478 gnutls_hash_hd_t hash_hnd
= NULL
;
481 rc
= gnutls_hash_init(&hash_hnd
, GNUTLS_DIG_MD5
);
483 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
486 rc
= gnutls_hash(hash_hnd
, zeros
, sizeof(zeros
));
488 gnutls_hash_deinit(hash_hnd
, NULL
);
489 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
492 SSVAL(header
, 0, NL_SIGN_HMAC_MD5
);
493 SSVAL(header
, 2, NL_SEAL_RC4
);
494 SSVAL(header
, 4, 0xFFFF);
495 SSVAL(header
, 6, 0x0000);
497 rc
= gnutls_hash(hash_hnd
, header
, 8);
499 gnutls_hash_deinit(hash_hnd
, NULL
);
500 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
502 rc
= gnutls_hash(hash_hnd
, confounder
, 8);
504 gnutls_hash_deinit(hash_hnd
, NULL
);
505 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
508 SSVAL(header
, 0, NL_SIGN_HMAC_MD5
);
509 SSVAL(header
, 2, NL_SEAL_NONE
);
510 SSVAL(header
, 4, 0xFFFF);
511 SSVAL(header
, 6, 0x0000);
513 rc
= gnutls_hash(hash_hnd
, header
, 8);
515 gnutls_hash_deinit(hash_hnd
, NULL
);
516 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
519 rc
= gnutls_hash(hash_hnd
, data
, length
);
521 gnutls_hash_deinit(hash_hnd
, NULL
);
522 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
524 gnutls_hash_deinit(hash_hnd
, packet_digest
);
526 rc
= gnutls_hmac_fast(GNUTLS_MAC_MD5
,
527 state
->creds
->session_key
,
528 sizeof(state
->creds
->session_key
),
530 sizeof(packet_digest
),
532 ZERO_ARRAY(packet_digest
);
534 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
541 static NTSTATUS
netsec_incoming_packet(struct schannel_state
*state
,
543 uint8_t *data
, size_t length
,
544 const uint8_t *whole_pdu
, size_t pdu_length
,
545 const DATA_BLOB
*sig
)
547 uint32_t min_sig_size
= 0;
549 uint8_t checksum
[32];
550 uint32_t checksum_length
= sizeof(checksum_length
);
551 uint8_t _confounder
[8];
552 uint8_t *confounder
= NULL
;
553 uint32_t confounder_ofs
= 0;
556 const uint8_t *sign_data
= NULL
;
557 size_t sign_length
= 0;
560 netsec_offset_and_sizes(state
,
567 if (sig
->length
< min_sig_size
) {
568 return NT_STATUS_ACCESS_DENIED
;
572 confounder
= _confounder
;
573 memcpy(confounder
, sig
->data
+confounder_ofs
, 8);
578 SETUP_SEQNUM(state
, seq_num
, !state
->initiator
);
581 status
= netsec_do_seal(state
,
587 if (!NT_STATUS_IS_OK(status
)) {
588 DBG_WARNING("netsec_do_seal failed: %s\n", nt_errstr(status
));
589 return NT_STATUS_ACCESS_DENIED
;
593 if (state
->gensec
->want_features
& GENSEC_FEATURE_SIGN_PKT_HEADER
) {
594 sign_data
= whole_pdu
;
595 sign_length
= pdu_length
;
598 sign_length
= length
;
601 status
= netsec_do_sign(state
,
607 if (!NT_STATUS_IS_OK(status
)) {
608 DBG_WARNING("netsec_do_sign failed: %s\n", nt_errstr(status
));
609 return NT_STATUS_ACCESS_DENIED
;
612 ret
= mem_equal_const_time(checksum
, sig
->data
+16, checksum_length
);
614 dump_data_pw("calc digest:", checksum
, checksum_length
);
615 dump_data_pw("wire digest:", sig
->data
+16, checksum_length
);
616 return NT_STATUS_ACCESS_DENIED
;
619 status
= netsec_do_seq_num(state
, checksum
, checksum_length
, seq_num
);
620 if (!NT_STATUS_IS_OK(status
)) {
621 DBG_WARNING("netsec_do_seq_num failed: %s\n",
626 ZERO_ARRAY(checksum
);
628 ret
= mem_equal_const_time(seq_num
, sig
->data
+8, 8);
630 dump_data_pw("calc seq num:", seq_num
, 8);
631 dump_data_pw("wire seq num:", sig
->data
+8, 8);
632 return NT_STATUS_ACCESS_DENIED
;
638 static uint32_t netsec_outgoing_sig_size(struct schannel_state
*state
)
640 uint32_t sig_size
= 0;
642 netsec_offset_and_sizes(state
,
652 static NTSTATUS
netsec_outgoing_packet(struct schannel_state
*state
,
655 uint8_t *data
, size_t length
,
656 const uint8_t *whole_pdu
, size_t pdu_length
,
659 uint32_t min_sig_size
= 0;
660 uint32_t used_sig_size
= 0;
662 uint8_t checksum
[32];
663 uint32_t checksum_length
= sizeof(checksum_length
);
664 uint8_t _confounder
[8];
665 uint8_t *confounder
= NULL
;
666 uint32_t confounder_ofs
= 0;
668 const uint8_t *sign_data
= NULL
;
669 size_t sign_length
= 0;
672 netsec_offset_and_sizes(state
,
679 SETUP_SEQNUM(state
, seq_num
, state
->initiator
);
682 confounder
= _confounder
;
683 generate_random_buffer(confounder
, 8);
688 if (state
->gensec
->want_features
& GENSEC_FEATURE_SIGN_PKT_HEADER
) {
689 sign_data
= whole_pdu
;
690 sign_length
= pdu_length
;
693 sign_length
= length
;
696 status
= netsec_do_sign(state
,
702 if (!NT_STATUS_IS_OK(status
)) {
703 DBG_WARNING("netsec_do_sign failed: %s\n", nt_errstr(status
));
704 return NT_STATUS_ACCESS_DENIED
;
708 status
= netsec_do_seal(state
,
714 if (!NT_STATUS_IS_OK(status
)) {
715 DBG_WARNING("netsec_do_seal failed: %s\n",
721 status
= netsec_do_seq_num(state
, checksum
, checksum_length
, seq_num
);
722 if (!NT_STATUS_IS_OK(status
)) {
723 DBG_WARNING("netsec_do_seq_num failed: %s\n",
728 (*sig
) = data_blob_talloc_zero(mem_ctx
, used_sig_size
);
730 memcpy(sig
->data
, header
, 8);
731 memcpy(sig
->data
+8, seq_num
, 8);
732 memcpy(sig
->data
+16, checksum
, checksum_length
);
735 memcpy(sig
->data
+confounder_ofs
, confounder
, 8);
738 dump_data_pw("signature:", sig
->data
+ 0, 8);
739 dump_data_pw("seq_num :", sig
->data
+ 8, 8);
740 dump_data_pw("digest :", sig
->data
+16, checksum_length
);
741 dump_data_pw("confound :", sig
->data
+confounder_ofs
, 8);
746 _PUBLIC_ NTSTATUS
gensec_schannel_init(TALLOC_CTX
*ctx
);
748 static size_t schannel_sig_size(struct gensec_security
*gensec_security
, size_t data_size
)
750 struct schannel_state
*state
=
751 talloc_get_type_abort(gensec_security
->private_data
,
752 struct schannel_state
);
754 return netsec_outgoing_sig_size(state
);
757 struct schannel_update_state
{
762 static NTSTATUS
schannel_update_internal(struct gensec_security
*gensec_security
,
763 TALLOC_CTX
*out_mem_ctx
,
764 const DATA_BLOB in
, DATA_BLOB
*out
);
766 static struct tevent_req
*schannel_update_send(TALLOC_CTX
*mem_ctx
,
767 struct tevent_context
*ev
,
768 struct gensec_security
*gensec_security
,
771 struct tevent_req
*req
;
772 struct schannel_update_state
*state
= NULL
;
775 req
= tevent_req_create(mem_ctx
, &state
,
776 struct schannel_update_state
);
781 status
= schannel_update_internal(gensec_security
,
784 state
->status
= status
;
785 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
786 status
= NT_STATUS_OK
;
788 if (tevent_req_nterror(req
, status
)) {
789 return tevent_req_post(req
, ev
);
792 tevent_req_done(req
);
793 return tevent_req_post(req
, ev
);
796 static NTSTATUS
schannel_update_internal(struct gensec_security
*gensec_security
,
797 TALLOC_CTX
*out_mem_ctx
,
798 const DATA_BLOB in
, DATA_BLOB
*out
)
800 struct schannel_state
*state
=
801 talloc_get_type(gensec_security
->private_data
,
802 struct schannel_state
);
804 enum ndr_err_code ndr_err
;
805 struct NL_AUTH_MESSAGE bind_schannel
= {
808 struct NL_AUTH_MESSAGE bind_schannel_ack
;
809 struct netlogon_creds_CredentialState
*creds
;
810 const char *workstation
;
813 *out
= data_blob(NULL
, 0);
815 if (gensec_security
->dcerpc_auth_level
< DCERPC_AUTH_LEVEL_INTEGRITY
) {
816 switch (gensec_security
->gensec_role
) {
818 return NT_STATUS_INVALID_PARAMETER_MIX
;
820 return NT_STATUS_INVALID_PARAMETER
;
822 return NT_STATUS_INTERNAL_ERROR
;
825 switch (gensec_security
->gensec_role
) {
828 /* we could parse the bind ack, but we don't know what it is yet */
832 creds
= cli_credentials_get_netlogon_creds(gensec_security
->credentials
);
834 return NT_STATUS_INVALID_PARAMETER_MIX
;
837 state
= netsec_create_state(gensec_security
,
838 creds
, true /* initiator */);
840 return NT_STATUS_NO_MEMORY
;
843 bind_schannel
.MessageType
= NL_NEGOTIATE_REQUEST
;
845 bind_schannel
.Flags
= NL_FLAG_OEM_NETBIOS_DOMAIN_NAME
|
846 NL_FLAG_OEM_NETBIOS_COMPUTER_NAME
;
847 bind_schannel
.oem_netbios_domain
.a
= cli_credentials_get_domain(gensec_security
->credentials
);
848 bind_schannel
.oem_netbios_computer
.a
= creds
->computer_name
;
850 if (creds
->secure_channel_type
== SEC_CHAN_DNS_DOMAIN
) {
851 bind_schannel
.Flags
|= NL_FLAG_UTF8_DNS_DOMAIN_NAME
;
852 bind_schannel
.utf8_dns_domain
.u
= cli_credentials_get_realm(gensec_security
->credentials
);
854 bind_schannel
.Flags
|= NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME
;
855 bind_schannel
.utf8_netbios_computer
.u
= creds
->computer_name
;
858 ndr_err
= ndr_push_struct_blob(out
, out_mem_ctx
, &bind_schannel
,
859 (ndr_push_flags_fn_t
)ndr_push_NL_AUTH_MESSAGE
);
860 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
861 status
= ndr_map_error2ntstatus(ndr_err
);
862 DEBUG(3, ("Could not create schannel bind: %s\n",
867 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
871 /* no third leg on this protocol */
872 return NT_STATUS_INVALID_PARAMETER
;
875 /* parse the schannel startup blob */
876 ndr_err
= ndr_pull_struct_blob(&in
, out_mem_ctx
, &bind_schannel
,
877 (ndr_pull_flags_fn_t
)ndr_pull_NL_AUTH_MESSAGE
);
878 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
879 status
= ndr_map_error2ntstatus(ndr_err
);
880 DEBUG(3, ("Could not parse incoming schannel bind: %s\n",
885 if (bind_schannel
.Flags
& NL_FLAG_OEM_NETBIOS_DOMAIN_NAME
) {
886 domain
= bind_schannel
.oem_netbios_domain
.a
;
887 if (strcasecmp_m(domain
, lpcfg_workgroup(gensec_security
->settings
->lp_ctx
)) != 0) {
888 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
889 domain
, lpcfg_workgroup(gensec_security
->settings
->lp_ctx
)));
890 return NT_STATUS_LOGON_FAILURE
;
892 } else if (bind_schannel
.Flags
& NL_FLAG_UTF8_DNS_DOMAIN_NAME
) {
893 domain
= bind_schannel
.utf8_dns_domain
.u
;
894 if (strcasecmp_m(domain
, lpcfg_dnsdomain(gensec_security
->settings
->lp_ctx
)) != 0) {
895 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
896 domain
, lpcfg_dnsdomain(gensec_security
->settings
->lp_ctx
)));
897 return NT_STATUS_LOGON_FAILURE
;
900 DEBUG(3, ("Request for schannel to without domain\n"));
901 return NT_STATUS_LOGON_FAILURE
;
904 if (bind_schannel
.Flags
& NL_FLAG_OEM_NETBIOS_COMPUTER_NAME
) {
905 workstation
= bind_schannel
.oem_netbios_computer
.a
;
906 } else if (bind_schannel
.Flags
& NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME
) {
907 workstation
= bind_schannel
.utf8_netbios_computer
.u
;
909 DEBUG(3, ("Request for schannel to without netbios workstation\n"));
910 return NT_STATUS_LOGON_FAILURE
;
913 status
= schannel_get_creds_state(out_mem_ctx
,
914 gensec_security
->settings
->lp_ctx
,
915 workstation
, &creds
);
916 if (!NT_STATUS_IS_OK(status
)) {
917 DEBUG(3, ("Could not find session key for attempted schannel connection from %s: %s\n",
918 workstation
, nt_errstr(status
)));
919 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_HANDLE
)) {
920 return NT_STATUS_LOGON_FAILURE
;
925 state
= netsec_create_state(gensec_security
,
926 creds
, false /* not initiator */);
928 return NT_STATUS_NO_MEMORY
;
931 status
= auth_anonymous_user_info_dc(state
,
932 lpcfg_netbios_name(gensec_security
->settings
->lp_ctx
),
933 &state
->user_info_dc
);
934 if (!NT_STATUS_IS_OK(status
)) {
938 bind_schannel_ack
.MessageType
= NL_NEGOTIATE_RESPONSE
;
939 bind_schannel_ack
.Flags
= 0;
940 bind_schannel_ack
.Buffer
.dummy
= 0x6c0000; /* actually I think
945 ndr_err
= ndr_push_struct_blob(out
, out_mem_ctx
, &bind_schannel_ack
,
946 (ndr_push_flags_fn_t
)ndr_push_NL_AUTH_MESSAGE
);
947 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
948 status
= ndr_map_error2ntstatus(ndr_err
);
949 DEBUG(3, ("Could not return schannel bind ack for client %s: %s\n",
950 workstation
, nt_errstr(status
)));
956 return NT_STATUS_INVALID_PARAMETER
;
959 static NTSTATUS
schannel_update_recv(struct tevent_req
*req
,
960 TALLOC_CTX
*out_mem_ctx
,
963 struct schannel_update_state
*state
=
965 struct schannel_update_state
);
968 *out
= data_blob_null
;
970 if (tevent_req_is_nterror(req
, &status
)) {
971 tevent_req_received(req
);
975 status
= state
->status
;
976 talloc_steal(out_mem_ctx
, state
->out
.data
);
978 tevent_req_received(req
);
983 * Returns anonymous credentials for schannel, matching Win2k3.
987 static NTSTATUS
schannel_session_info(struct gensec_security
*gensec_security
,
989 struct auth_session_info
**_session_info
)
991 struct schannel_state
*state
=
992 talloc_get_type(gensec_security
->private_data
,
993 struct schannel_state
);
994 struct auth4_context
*auth_ctx
= gensec_security
->auth_context
;
995 struct auth_session_info
*session_info
= NULL
;
996 uint32_t session_info_flags
= 0;
999 if (auth_ctx
== NULL
) {
1000 DEBUG(0, ("Cannot generate a session_info without the auth_context\n"));
1001 return NT_STATUS_INTERNAL_ERROR
;
1004 if (auth_ctx
->generate_session_info
== NULL
) {
1005 DEBUG(0, ("Cannot generate a session_info without the generate_session_info hook\n"));
1006 return NT_STATUS_INTERNAL_ERROR
;
1009 if (gensec_security
->want_features
& GENSEC_FEATURE_UNIX_TOKEN
) {
1010 session_info_flags
|= AUTH_SESSION_INFO_UNIX_TOKEN
;
1013 session_info_flags
|= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES
;
1015 status
= auth_ctx
->generate_session_info(
1018 state
->user_info_dc
,
1019 state
->user_info_dc
->info
->account_name
,
1022 if (!NT_STATUS_IS_OK(status
)) {
1026 *_session_info
= session_info
;
1027 return NT_STATUS_OK
;
1031 * Reduce the attack surface by ensuring schannel is not available when
1034 static NTSTATUS
schannel_server_start(struct gensec_security
*gensec_security
)
1036 enum server_role server_role
1037 = lpcfg_server_role(gensec_security
->settings
->lp_ctx
);
1039 switch (server_role
) {
1040 case ROLE_DOMAIN_BDC
:
1041 case ROLE_DOMAIN_PDC
:
1042 case ROLE_ACTIVE_DIRECTORY_DC
:
1044 return NT_STATUS_OK
;
1046 return NT_STATUS_NOT_IMPLEMENTED
;
1050 static NTSTATUS
schannel_client_start(struct gensec_security
*gensec_security
)
1052 return NT_STATUS_OK
;
1055 static bool schannel_have_feature(struct gensec_security
*gensec_security
,
1058 if (gensec_security
->dcerpc_auth_level
>= DCERPC_AUTH_LEVEL_INTEGRITY
) {
1059 if (feature
& GENSEC_FEATURE_SIGN
) {
1063 if (gensec_security
->dcerpc_auth_level
== DCERPC_AUTH_LEVEL_PRIVACY
) {
1064 if (feature
& GENSEC_FEATURE_SEAL
) {
1068 if (feature
& GENSEC_FEATURE_DCE_STYLE
) {
1071 if (feature
& GENSEC_FEATURE_SIGN_PKT_HEADER
) {
1080 static NTSTATUS
schannel_unseal_packet(struct gensec_security
*gensec_security
,
1081 uint8_t *data
, size_t length
,
1082 const uint8_t *whole_pdu
, size_t pdu_length
,
1083 const DATA_BLOB
*sig
)
1085 struct schannel_state
*state
=
1086 talloc_get_type_abort(gensec_security
->private_data
,
1087 struct schannel_state
);
1089 return netsec_incoming_packet(state
, true,
1090 discard_const_p(uint8_t, data
),
1092 whole_pdu
, pdu_length
,
1097 check the signature on a packet
1099 static NTSTATUS
schannel_check_packet(struct gensec_security
*gensec_security
,
1100 const uint8_t *data
, size_t length
,
1101 const uint8_t *whole_pdu
, size_t pdu_length
,
1102 const DATA_BLOB
*sig
)
1104 struct schannel_state
*state
=
1105 talloc_get_type_abort(gensec_security
->private_data
,
1106 struct schannel_state
);
1108 return netsec_incoming_packet(state
, false,
1109 discard_const_p(uint8_t, data
),
1111 whole_pdu
, pdu_length
,
1117 static NTSTATUS
schannel_seal_packet(struct gensec_security
*gensec_security
,
1118 TALLOC_CTX
*mem_ctx
,
1119 uint8_t *data
, size_t length
,
1120 const uint8_t *whole_pdu
, size_t pdu_length
,
1123 struct schannel_state
*state
=
1124 talloc_get_type_abort(gensec_security
->private_data
,
1125 struct schannel_state
);
1127 return netsec_outgoing_packet(state
, mem_ctx
, true,
1129 whole_pdu
, pdu_length
,
1136 static NTSTATUS
schannel_sign_packet(struct gensec_security
*gensec_security
,
1137 TALLOC_CTX
*mem_ctx
,
1138 const uint8_t *data
, size_t length
,
1139 const uint8_t *whole_pdu
, size_t pdu_length
,
1142 struct schannel_state
*state
=
1143 talloc_get_type_abort(gensec_security
->private_data
,
1144 struct schannel_state
);
1146 return netsec_outgoing_packet(state
, mem_ctx
, false,
1147 discard_const_p(uint8_t, data
),
1149 whole_pdu
, pdu_length
,
1153 static const struct gensec_security_ops gensec_schannel_security_ops
= {
1155 .auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
,
1156 .client_start
= schannel_client_start
,
1157 .server_start
= schannel_server_start
,
1158 .update_send
= schannel_update_send
,
1159 .update_recv
= schannel_update_recv
,
1160 .seal_packet
= schannel_seal_packet
,
1161 .sign_packet
= schannel_sign_packet
,
1162 .check_packet
= schannel_check_packet
,
1163 .unseal_packet
= schannel_unseal_packet
,
1164 .session_info
= schannel_session_info
,
1165 .sig_size
= schannel_sig_size
,
1166 .have_feature
= schannel_have_feature
,
1168 .priority
= GENSEC_SCHANNEL
1171 _PUBLIC_ NTSTATUS
gensec_schannel_init(TALLOC_CTX
*ctx
)
1174 ret
= gensec_register(ctx
, &gensec_schannel_security_ops
);
1175 if (!NT_STATUS_IS_OK(ret
)) {
1176 DEBUG(0,("Failed to register '%s' gensec backend!\n",
1177 gensec_schannel_security_ops
.name
));