2 * GSSAPI Security Extensions
3 * RPC Pipe client and server routines
4 * Copyright (C) Simo Sorce 2010.
5 * Copyright (C) Andrew Bartlett 2004-2011.
6 * Copyright (C) Stefan Metzmacher <metze@samba.org> 2004-2005
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/>.
22 /* We support only GSSAPI/KRB5 here */
26 #include "lib/util/tevent_ntstatus.h"
28 #include "libads/kerberos_proto.h"
29 #include "auth/common_auth.h"
30 #include "auth/gensec/gensec.h"
31 #include "auth/gensec/gensec_internal.h"
32 #include "auth/credentials/credentials.h"
33 #include "../librpc/gen_ndr/dcerpc.h"
35 #if defined(HAVE_KRB5)
37 #include "auth/kerberos/pac_utils.h"
38 #include "auth/kerberos/gssapi_helper.h"
41 static char *gse_errstr(TALLOC_CTX
*mem_ctx
, OM_uint32 maj
, OM_uint32 min
);
42 static size_t gensec_gse_sig_size(struct gensec_security
*gensec_security
,
46 gss_ctx_id_t gssapi_context
;
47 gss_name_t server_name
;
48 gss_name_t client_name
;
49 OM_uint32 gss_want_flags
, gss_got_flags
;
50 size_t max_wrap_buf_size
;
53 gss_cred_id_t delegated_cred_handle
;
62 gss_OID_desc gss_mech
;
68 /* free non talloc dependent contexts */
69 static int gse_context_destructor(void *ptr
)
71 struct gse_context
*gse_ctx
;
74 gse_ctx
= talloc_get_type_abort(ptr
, struct gse_context
);
76 if (gse_ctx
->ccache
) {
77 krb5_cc_close(gse_ctx
->k5ctx
, gse_ctx
->ccache
);
78 gse_ctx
->ccache
= NULL
;
80 if (gse_ctx
->keytab
) {
81 krb5_kt_close(gse_ctx
->k5ctx
, gse_ctx
->keytab
);
82 gse_ctx
->keytab
= NULL
;
84 krb5_free_context(gse_ctx
->k5ctx
);
85 gse_ctx
->k5ctx
= NULL
;
87 if (gse_ctx
->gssapi_context
!= GSS_C_NO_CONTEXT
) {
88 (void)gss_delete_sec_context(&gss_min
,
89 &gse_ctx
->gssapi_context
,
92 if (gse_ctx
->server_name
) {
93 (void)gss_release_name(&gss_min
,
94 &gse_ctx
->server_name
);
96 if (gse_ctx
->client_name
) {
97 (void)gss_release_name(&gss_min
,
98 &gse_ctx
->client_name
);
100 if (gse_ctx
->creds
) {
101 (void)gss_release_cred(&gss_min
,
104 if (gse_ctx
->delegated_cred_handle
) {
105 (void)gss_release_cred(&gss_min
,
106 &gse_ctx
->delegated_cred_handle
);
109 /* MIT and Heimdal differ as to if you can call
110 * gss_release_oid() on this OID, generated by
111 * gss_{accept,init}_sec_context(). However, as long as the
112 * oid is gss_mech_krb5 (which it always is at the moment),
113 * then this is a moot point, as both declare this particular
114 * OID static, and so no memory is lost. This assert is in
115 * place to ensure that the programmer who wishes to extend
116 * this code to EAP or other GSS mechanisms determines an
117 * implementation-dependent way of releasing any dynamically
119 SMB_ASSERT(smb_gss_oid_equal(&gse_ctx
->gss_mech
, GSS_C_NO_OID
) ||
120 smb_gss_oid_equal(&gse_ctx
->gss_mech
, gss_mech_krb5
));
125 static NTSTATUS
gse_setup_server_principal(TALLOC_CTX
*mem_ctx
,
126 const char *target_principal
,
128 const char *hostname
,
130 char **pserver_principal
,
131 gss_name_t
*pserver_name
)
133 char *server_principal
= NULL
;
134 gss_buffer_desc name_token
;
136 OM_uint32 maj_stat
, min_stat
= 0;
138 if (target_principal
!= NULL
) {
139 server_principal
= talloc_strdup(mem_ctx
, target_principal
);
140 name_type
= GSS_C_NULL_OID
;
142 server_principal
= talloc_asprintf(mem_ctx
,
147 name_type
= GSS_C_NT_USER_NAME
;
149 if (server_principal
== NULL
) {
150 return NT_STATUS_NO_MEMORY
;
153 name_token
.value
= (uint8_t *)server_principal
;
154 name_token
.length
= strlen(server_principal
);
156 maj_stat
= gss_import_name(&min_stat
,
161 DBG_WARNING("GSS Import name of %s failed: %s\n",
163 gse_errstr(mem_ctx
, maj_stat
, min_stat
));
164 TALLOC_FREE(server_principal
);
165 return NT_STATUS_INVALID_PARAMETER
;
168 *pserver_principal
= server_principal
;
173 static NTSTATUS
gse_context_init(TALLOC_CTX
*mem_ctx
,
174 bool do_sign
, bool do_seal
,
175 const char *ccache_name
,
176 uint32_t add_gss_c_flags
,
177 struct gse_context
**_gse_ctx
)
179 struct gse_context
*gse_ctx
;
180 krb5_error_code k5ret
;
183 gse_ctx
= talloc_zero(mem_ctx
, struct gse_context
);
185 return NT_STATUS_NO_MEMORY
;
187 talloc_set_destructor((TALLOC_CTX
*)gse_ctx
, gse_context_destructor
);
189 gse_ctx
->expire_time
= GENSEC_EXPIRE_TIME_INFINITY
;
190 gse_ctx
->max_wrap_buf_size
= UINT16_MAX
;
192 memcpy(&gse_ctx
->gss_mech
, gss_mech_krb5
, sizeof(gss_OID_desc
));
194 gse_ctx
->gss_want_flags
= GSS_C_MUTUAL_FLAG
|
195 GSS_C_DELEG_POLICY_FLAG
|
199 gse_ctx
->gss_want_flags
|= GSS_C_INTEG_FLAG
;
202 gse_ctx
->gss_want_flags
|= GSS_C_INTEG_FLAG
;
203 gse_ctx
->gss_want_flags
|= GSS_C_CONF_FLAG
;
206 gse_ctx
->gss_want_flags
|= add_gss_c_flags
;
208 /* Initialize Kerberos Context */
209 k5ret
= smb_krb5_init_context_common(&gse_ctx
->k5ctx
);
211 DBG_ERR("kerberos init context failed (%s)\n",
212 error_message(k5ret
));
213 status
= NT_STATUS_INTERNAL_ERROR
;
218 ccache_name
= krb5_cc_default_name(gse_ctx
->k5ctx
);
220 k5ret
= krb5_cc_resolve(gse_ctx
->k5ctx
, ccache_name
,
223 DEBUG(1, ("Failed to resolve credential cache '%s'! (%s)\n",
224 ccache_name
, error_message(k5ret
)));
225 status
= NT_STATUS_INTERNAL_ERROR
;
229 /* TODO: Should we enforce a enc_types list ?
230 ret = krb5_set_default_tgs_ktypes(gse_ctx->k5ctx, enc_types);
237 TALLOC_FREE(gse_ctx
);
241 static NTSTATUS
gse_init_client(TALLOC_CTX
*mem_ctx
,
242 bool do_sign
, bool do_seal
,
243 const char *ccache_name
,
247 const char *username
,
248 const char *password
,
249 uint32_t add_gss_c_flags
,
250 struct gse_context
**_gse_ctx
)
252 struct gse_context
*gse_ctx
;
253 OM_uint32 gss_maj
, gss_min
;
254 #ifdef HAVE_GSS_KRB5_CRED_NO_CI_FLAGS_X
255 gss_buffer_desc empty_buffer
= GSS_C_EMPTY_BUFFER
;
256 gss_OID oid
= discard_const(GSS_KRB5_CRED_NO_CI_FLAGS_X
);
260 if (!server
|| !service
) {
261 return NT_STATUS_INVALID_PARAMETER
;
264 status
= gse_context_init(mem_ctx
, do_sign
, do_seal
,
265 ccache_name
, add_gss_c_flags
,
267 if (!NT_STATUS_IS_OK(status
)) {
268 return NT_STATUS_NO_MEMORY
;
271 /* TODO: get krb5 ticket using username/password, if no valid
272 * one already available in ccache */
274 gss_maj
= smb_gss_krb5_import_cred(&gss_min
,
277 NULL
, /* keytab_principal */
284 kret
= krb5_cc_get_full_name(gse_ctx
->k5ctx
,
291 DEBUG(5, ("smb_gss_krb5_import_cred ccache[%s] failed with [%s] -"
292 "the caller may retry after a kinit.\n",
293 ccache
, gse_errstr(gse_ctx
, gss_maj
, gss_min
)));
295 status
= NT_STATUS_INTERNAL_ERROR
;
299 #ifdef HAVE_GSS_KRB5_CRED_NO_CI_FLAGS_X
301 * Don't force GSS_C_CONF_FLAG and GSS_C_INTEG_FLAG.
303 * This allows us to disable SIGN and SEAL for
304 * AUTH_LEVEL_CONNECT and AUTH_LEVEL_INTEGRITY.
306 * https://groups.yahoo.com/neo/groups/cat-ietf/conversations/topics/575
307 * http://krbdev.mit.edu/rt/Ticket/Display.html?id=6938
309 gss_maj
= gss_set_cred_option(&gss_min
, &gse_ctx
->creds
,
313 DEBUG(0, ("gss_set_cred_option(GSS_KRB5_CRED_NO_CI_FLAGS_X), "
314 "failed with [%s]\n",
315 gse_errstr(gse_ctx
, gss_maj
, gss_min
)));
316 status
= NT_STATUS_INTERNAL_ERROR
;
325 TALLOC_FREE(gse_ctx
);
329 static NTSTATUS
gse_get_client_auth_token(TALLOC_CTX
*mem_ctx
,
330 struct gensec_security
*gensec_security
,
331 const DATA_BLOB
*token_in
,
332 DATA_BLOB
*token_out
)
334 struct gse_context
*gse_ctx
=
335 talloc_get_type_abort(gensec_security
->private_data
,
337 OM_uint32 gss_maj
= 0;
339 gss_buffer_desc in_data
;
340 gss_buffer_desc out_data
;
341 DATA_BLOB blob
= data_blob_null
;
343 OM_uint32 time_rec
= 0;
345 struct cli_credentials
*cli_creds
= gensec_get_credentials(gensec_security
);
346 const char *target_principal
= gensec_get_target_principal(gensec_security
);
347 const char *hostname
= gensec_get_target_hostname(gensec_security
);
348 const char *service
= gensec_get_target_service(gensec_security
);
349 const char *client_realm
= cli_credentials_get_realm(cli_creds
);
350 char *server_principal
= NULL
;
351 char *server_realm
= NULL
;
352 bool fallback
= false;
353 OM_uint32 time_req
= 0;
355 time_req
= gensec_setting_int(gensec_security
->settings
,
357 "requested_life_time",
360 in_data
.value
= token_in
->data
;
361 in_data
.length
= token_in
->length
;
364 * With credentials for administrator@FOREST1.EXAMPLE.COM this patch
365 * changes the target_principal for the ldap service of host
366 * dc2.forest2.example.com from
368 * ldap/dc2.forest2.example.com@FOREST1.EXAMPLE.COM
372 * ldap/dc2.forest2.example.com@FOREST2.EXAMPLE.COM
374 * Typically ldap/dc2.forest2.example.com@FOREST1.EXAMPLE.COM should be
375 * used in order to allow the KDC of FOREST1.EXAMPLE.COM to generate a
376 * referral ticket for krbtgt/FOREST2.EXAMPLE.COM@FOREST1.EXAMPLE.COM.
378 * The problem is that KDCs only return such referral tickets if
379 * there's a forest trust between FOREST1.EXAMPLE.COM and
380 * FOREST2.EXAMPLE.COM. If there's only an external domain trust
381 * between FOREST1.EXAMPLE.COM and FOREST2.EXAMPLE.COM the KDC of
382 * FOREST1.EXAMPLE.COM will respond with S_PRINCIPAL_UNKNOWN when being
383 * asked for ldap/dc2.forest2.example.com@FOREST1.EXAMPLE.COM.
385 * In the case of an external trust the client can still ask explicitly
386 * for krbtgt/FOREST2.EXAMPLE.COM@FOREST1.EXAMPLE.COM and the KDC of
387 * FOREST1.EXAMPLE.COM will generate it.
389 * From there the client can use the
390 * krbtgt/FOREST2.EXAMPLE.COM@FOREST1.EXAMPLE.COM ticket and ask a KDC
391 * of FOREST2.EXAMPLE.COM for a service ticket for
392 * ldap/dc2.forest2.example.com@FOREST2.EXAMPLE.COM.
394 * With Heimdal we'll get the fallback on S_PRINCIPAL_UNKNOWN behavior
395 * when we pass ldap/dc2.forest2.example.com@FOREST2.EXAMPLE.COM as
396 * target principal. As _krb5_get_cred_kdc_any() first calls
397 * get_cred_kdc_referral() (which always starts with the client realm)
398 * and falls back to get_cred_kdc_capath() (which starts with the given
401 * MIT krb5 only tries the given realm of the target principal, if we
402 * want to autodetect support for transitive forest trusts, would have
403 * to do the fallback ourself.
405 #ifndef SAMBA4_USES_HEIMDAL
406 if (gse_ctx
->server_name
== NULL
) {
407 OM_uint32 gss_min2
= 0;
409 status
= gse_setup_server_principal(mem_ctx
,
415 &gse_ctx
->server_name
);
416 if (!NT_STATUS_IS_OK(status
)) {
420 gss_maj
= gss_init_sec_context(&gss_min
,
422 &gse_ctx
->gssapi_context
,
423 gse_ctx
->server_name
,
425 gse_ctx
->gss_want_flags
,
427 GSS_C_NO_CHANNEL_BINDINGS
,
431 &gse_ctx
->gss_got_flags
,
433 if (gss_maj
!= GSS_S_FAILURE
) {
434 goto init_sec_context_done
;
436 if (gss_min
!= (OM_uint32
)KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
) {
437 goto init_sec_context_done
;
439 if (target_principal
!= NULL
) {
440 goto init_sec_context_done
;
444 TALLOC_FREE(server_principal
);
445 gss_release_name(&gss_min2
, &gse_ctx
->server_name
);
447 #endif /* !SAMBA4_USES_HEIMDAL */
449 if (gse_ctx
->server_name
== NULL
) {
450 server_realm
= smb_krb5_get_realm_from_hostname(mem_ctx
,
453 if (server_realm
== NULL
) {
454 return NT_STATUS_NO_MEMORY
;
458 strequal(client_realm
, server_realm
)) {
459 goto init_sec_context_done
;
462 status
= gse_setup_server_principal(mem_ctx
,
468 &gse_ctx
->server_name
);
469 TALLOC_FREE(server_realm
);
470 if (!NT_STATUS_IS_OK(status
)) {
474 TALLOC_FREE(server_principal
);
477 gss_maj
= gss_init_sec_context(&gss_min
,
479 &gse_ctx
->gssapi_context
,
480 gse_ctx
->server_name
,
482 gse_ctx
->gss_want_flags
,
483 time_req
, GSS_C_NO_CHANNEL_BINDINGS
,
484 &in_data
, NULL
, &out_data
,
485 &gse_ctx
->gss_got_flags
, &time_rec
);
486 goto init_sec_context_done
;
488 init_sec_context_done
:
492 /* we are done with it */
493 tv
= timeval_current_ofs(time_rec
, 0);
494 gse_ctx
->expire_time
= timeval_to_nttime(&tv
);
496 status
= NT_STATUS_OK
;
498 case GSS_S_CONTINUE_NEEDED
:
499 /* we will need a third leg */
500 status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
502 case GSS_S_CONTEXT_EXPIRED
:
503 /* Make SPNEGO ignore us, we can't go any further here */
504 DBG_NOTICE("Context expired\n");
505 status
= NT_STATUS_INVALID_PARAMETER
;
509 case (OM_uint32
)KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
:
510 DBG_NOTICE("Server principal not found\n");
511 /* Make SPNEGO ignore us, we can't go any further here */
512 status
= NT_STATUS_INVALID_PARAMETER
;
514 case (OM_uint32
)KRB5KRB_AP_ERR_TKT_EXPIRED
:
515 DBG_NOTICE("Ticket expired\n");
516 /* Make SPNEGO ignore us, we can't go any further here */
517 status
= NT_STATUS_INVALID_PARAMETER
;
519 case (OM_uint32
)KRB5KRB_AP_ERR_TKT_NYV
:
520 DBG_NOTICE("Clockskew\n");
521 /* Make SPNEGO ignore us, we can't go any further here */
522 status
= NT_STATUS_TIME_DIFFERENCE_AT_DC
;
524 case (OM_uint32
)KRB5_KDC_UNREACH
:
525 DBG_NOTICE("KDC unreachable\n");
526 /* Make SPNEGO ignore us, we can't go any further here */
527 status
= NT_STATUS_NO_LOGON_SERVERS
;
529 case (OM_uint32
)KRB5KRB_AP_ERR_MSG_TYPE
:
530 /* Garbage input, possibly from the auto-mech detection */
531 status
= NT_STATUS_INVALID_PARAMETER
;
533 case (OM_uint32
)KRB5KDC_ERR_ETYPE_NOSUPP
:
534 status
= NT_STATUS_KDC_UNKNOWN_ETYPE
;
537 DBG_ERR("gss_init_sec_context failed with [%s](%u)\n",
538 gse_errstr(talloc_tos(), gss_maj
, gss_min
),
540 status
= NT_STATUS_LOGON_FAILURE
;
545 DBG_ERR("gss_init_sec_context failed with [%s]\n",
546 gse_errstr(talloc_tos(), gss_maj
, gss_min
));
547 status
= NT_STATUS_INTERNAL_ERROR
;
551 /* we may be told to return nothing */
552 if (out_data
.length
) {
553 blob
= data_blob_talloc(mem_ctx
, out_data
.value
, out_data
.length
);
555 status
= NT_STATUS_NO_MEMORY
;
558 gss_maj
= gss_release_buffer(&gss_min
, &out_data
);
566 static NTSTATUS
gse_init_server(TALLOC_CTX
*mem_ctx
,
567 bool do_sign
, bool do_seal
,
568 uint32_t add_gss_c_flags
,
569 struct gse_context
**_gse_ctx
)
571 struct gse_context
*gse_ctx
;
572 OM_uint32 gss_maj
, gss_min
;
576 status
= gse_context_init(mem_ctx
, do_sign
, do_seal
,
577 NULL
, add_gss_c_flags
, &gse_ctx
);
578 if (!NT_STATUS_IS_OK(status
)) {
579 return NT_STATUS_NO_MEMORY
;
582 ret
= gse_krb5_get_server_keytab(gse_ctx
->k5ctx
,
585 status
= NT_STATUS_INTERNAL_ERROR
;
589 /* This creates a GSSAPI cred_id_t with the keytab set */
590 gss_maj
= smb_gss_krb5_import_cred(&gss_min
, gse_ctx
->k5ctx
,
591 NULL
, NULL
, gse_ctx
->keytab
,
595 DEBUG(0, ("smb_gss_krb5_import_cred failed with [%s]\n",
596 gse_errstr(gse_ctx
, gss_maj
, gss_min
)));
597 status
= NT_STATUS_INTERNAL_ERROR
;
601 status
= NT_STATUS_OK
;
604 if (!NT_STATUS_IS_OK(status
)) {
605 TALLOC_FREE(gse_ctx
);
612 static NTSTATUS
gse_get_server_auth_token(TALLOC_CTX
*mem_ctx
,
613 struct gensec_security
*gensec_security
,
614 const DATA_BLOB
*token_in
,
615 DATA_BLOB
*token_out
)
617 struct gse_context
*gse_ctx
=
618 talloc_get_type_abort(gensec_security
->private_data
,
620 OM_uint32 gss_maj
, gss_min
;
621 gss_buffer_desc in_data
;
622 gss_buffer_desc out_data
;
623 DATA_BLOB blob
= data_blob_null
;
625 OM_uint32 time_rec
= 0;
628 in_data
.value
= token_in
->data
;
629 in_data
.length
= token_in
->length
;
631 gss_maj
= gss_accept_sec_context(&gss_min
,
632 &gse_ctx
->gssapi_context
,
635 GSS_C_NO_CHANNEL_BINDINGS
,
636 &gse_ctx
->client_name
,
639 &gse_ctx
->gss_got_flags
,
641 &gse_ctx
->delegated_cred_handle
);
644 /* we are done with it */
645 tv
= timeval_current_ofs(time_rec
, 0);
646 gse_ctx
->expire_time
= timeval_to_nttime(&tv
);
648 status
= NT_STATUS_OK
;
650 case GSS_S_CONTINUE_NEEDED
:
651 /* we will need a third leg */
652 status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
655 DEBUG(1, ("gss_accept_sec_context failed with [%s]\n",
656 gse_errstr(talloc_tos(), gss_maj
, gss_min
)));
658 if (gse_ctx
->gssapi_context
) {
659 gss_delete_sec_context(&gss_min
,
660 &gse_ctx
->gssapi_context
,
665 * If we got an output token, make Windows aware of it
666 * by telling it that more processing is needed
668 if (out_data
.length
> 0) {
669 status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
670 /* Fall through to handle the out token */
672 status
= NT_STATUS_LOGON_FAILURE
;
677 /* we may be told to return nothing */
678 if (out_data
.length
) {
679 blob
= data_blob_talloc(mem_ctx
, out_data
.value
, out_data
.length
);
681 status
= NT_STATUS_NO_MEMORY
;
683 gss_maj
= gss_release_buffer(&gss_min
, &out_data
);
692 static char *gse_errstr(TALLOC_CTX
*mem_ctx
, OM_uint32 maj
, OM_uint32 min
)
694 OM_uint32 gss_min
, gss_maj
;
695 gss_buffer_desc msg_min
;
696 gss_buffer_desc msg_maj
;
697 OM_uint32 msg_ctx
= 0;
701 ZERO_STRUCT(msg_min
);
702 ZERO_STRUCT(msg_maj
);
704 gss_maj
= gss_display_status(&gss_min
, maj
, GSS_C_GSS_CODE
,
705 GSS_C_NO_OID
, &msg_ctx
, &msg_maj
);
709 errstr
= talloc_strndup(mem_ctx
,
710 (char *)msg_maj
.value
,
715 gss_maj
= gss_display_status(&gss_min
, min
, GSS_C_MECH_CODE
,
716 (gss_OID
)discard_const(gss_mech_krb5
),
722 errstr
= talloc_strdup_append_buffer(errstr
, ": ");
726 errstr
= talloc_strndup_append_buffer(errstr
,
727 (char *)msg_min
.value
,
735 gss_maj
= gss_release_buffer(&gss_min
, &msg_min
);
738 gss_maj
= gss_release_buffer(&gss_min
, &msg_maj
);
743 static NTSTATUS
gensec_gse_client_start(struct gensec_security
*gensec_security
)
745 struct gse_context
*gse_ctx
;
746 struct cli_credentials
*creds
= gensec_get_credentials(gensec_security
);
748 OM_uint32 want_flags
= 0;
749 bool do_sign
= false, do_seal
= false;
750 const char *hostname
= gensec_get_target_hostname(gensec_security
);
751 const char *service
= gensec_get_target_service(gensec_security
);
752 const char *username
= cli_credentials_get_username(creds
);
753 const char *password
= cli_credentials_get_password(creds
);
754 const char *realm
= cli_credentials_get_realm(creds
);
757 DEBUG(1, ("Could not determine hostname for target computer, cannot use kerberos\n"));
758 return NT_STATUS_INVALID_PARAMETER
;
760 if (is_ipaddress(hostname
)) {
761 DEBUG(2, ("Cannot do GSE to an IP address\n"));
762 return NT_STATUS_INVALID_PARAMETER
;
764 if (strcmp(hostname
, "localhost") == 0) {
765 DEBUG(2, ("GSE to 'localhost' does not make sense\n"));
766 return NT_STATUS_INVALID_PARAMETER
;
769 if (gensec_security
->want_features
& GENSEC_FEATURE_SESSION_KEY
) {
772 if (gensec_security
->want_features
& GENSEC_FEATURE_SIGN
) {
775 if (gensec_security
->want_features
& GENSEC_FEATURE_SEAL
) {
778 if (gensec_security
->want_features
& GENSEC_FEATURE_DCE_STYLE
) {
779 want_flags
|= GSS_C_DCE_STYLE
;
782 nt_status
= gse_init_client(gensec_security
, do_sign
, do_seal
, NULL
,
783 hostname
, service
, realm
,
784 username
, password
, want_flags
,
786 if (!NT_STATUS_IS_OK(nt_status
)) {
789 gensec_security
->private_data
= gse_ctx
;
793 static NTSTATUS
gensec_gse_server_start(struct gensec_security
*gensec_security
)
795 struct gse_context
*gse_ctx
;
797 OM_uint32 want_flags
= 0;
798 bool do_sign
= false, do_seal
= false;
800 if (gensec_security
->want_features
& GENSEC_FEATURE_SIGN
) {
803 if (gensec_security
->want_features
& GENSEC_FEATURE_SEAL
) {
806 if (gensec_security
->want_features
& GENSEC_FEATURE_DCE_STYLE
) {
807 want_flags
|= GSS_C_DCE_STYLE
;
810 nt_status
= gse_init_server(gensec_security
, do_sign
, do_seal
, want_flags
,
812 if (!NT_STATUS_IS_OK(nt_status
)) {
815 gensec_security
->private_data
= gse_ctx
;
819 struct gensec_gse_update_state
{
824 static NTSTATUS
gensec_gse_update_internal(struct gensec_security
*gensec_security
,
829 static struct tevent_req
*gensec_gse_update_send(TALLOC_CTX
*mem_ctx
,
830 struct tevent_context
*ev
,
831 struct gensec_security
*gensec_security
,
834 struct tevent_req
*req
= NULL
;
835 struct gensec_gse_update_state
*state
= NULL
;
838 req
= tevent_req_create(mem_ctx
, &state
,
839 struct gensec_gse_update_state
);
844 status
= gensec_gse_update_internal(gensec_security
,
847 state
->status
= status
;
848 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
849 tevent_req_done(req
);
850 return tevent_req_post(req
, ev
);
852 if (tevent_req_nterror(req
, status
)) {
853 return tevent_req_post(req
, ev
);
856 tevent_req_done(req
);
857 return tevent_req_post(req
, ev
);
860 static NTSTATUS
gensec_gse_update_internal(struct gensec_security
*gensec_security
,
867 switch (gensec_security
->gensec_role
) {
869 status
= gse_get_client_auth_token(mem_ctx
,
874 status
= gse_get_server_auth_token(mem_ctx
,
879 if (!NT_STATUS_IS_OK(status
)) {
886 static NTSTATUS
gensec_gse_update_recv(struct tevent_req
*req
,
887 TALLOC_CTX
*out_mem_ctx
,
890 struct gensec_gse_update_state
*state
=
892 struct gensec_gse_update_state
);
895 *out
= data_blob_null
;
897 if (tevent_req_is_nterror(req
, &status
)) {
898 tevent_req_received(req
);
903 talloc_steal(out_mem_ctx
, state
->out
.data
);
904 status
= state
->status
;
905 tevent_req_received(req
);
909 static NTSTATUS
gensec_gse_wrap(struct gensec_security
*gensec_security
,
914 struct gse_context
*gse_ctx
=
915 talloc_get_type_abort(gensec_security
->private_data
,
917 OM_uint32 maj_stat
, min_stat
;
918 gss_buffer_desc input_token
, output_token
;
920 input_token
.length
= in
->length
;
921 input_token
.value
= in
->data
;
923 maj_stat
= gss_wrap(&min_stat
,
924 gse_ctx
->gssapi_context
,
925 gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
),
930 if (GSS_ERROR(maj_stat
)) {
931 DEBUG(0, ("gensec_gse_wrap: GSS Wrap failed: %s\n",
932 gse_errstr(talloc_tos(), maj_stat
, min_stat
)));
933 return NT_STATUS_ACCESS_DENIED
;
936 *out
= data_blob_talloc(mem_ctx
, output_token
.value
, output_token
.length
);
937 gss_release_buffer(&min_stat
, &output_token
);
939 if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
)
941 return NT_STATUS_ACCESS_DENIED
;
946 static NTSTATUS
gensec_gse_unwrap(struct gensec_security
*gensec_security
,
951 struct gse_context
*gse_ctx
=
952 talloc_get_type_abort(gensec_security
->private_data
,
954 OM_uint32 maj_stat
, min_stat
;
955 gss_buffer_desc input_token
, output_token
;
958 input_token
.length
= in
->length
;
959 input_token
.value
= in
->data
;
961 maj_stat
= gss_unwrap(&min_stat
,
962 gse_ctx
->gssapi_context
,
967 if (GSS_ERROR(maj_stat
)) {
968 DEBUG(0, ("gensec_gse_unwrap: GSS UnWrap failed: %s\n",
969 gse_errstr(talloc_tos(), maj_stat
, min_stat
)));
970 return NT_STATUS_ACCESS_DENIED
;
973 *out
= data_blob_talloc(mem_ctx
, output_token
.value
, output_token
.length
);
974 gss_release_buffer(&min_stat
, &output_token
);
976 if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
)
978 return NT_STATUS_ACCESS_DENIED
;
983 static NTSTATUS
gensec_gse_seal_packet(struct gensec_security
*gensec_security
,
985 uint8_t *data
, size_t length
,
986 const uint8_t *whole_pdu
, size_t pdu_length
,
989 struct gse_context
*gse_ctx
=
990 talloc_get_type_abort(gensec_security
->private_data
,
992 bool hdr_signing
= false;
996 if (gensec_security
->want_features
& GENSEC_FEATURE_SIGN_PKT_HEADER
) {
1000 sig_size
= gensec_gse_sig_size(gensec_security
, length
);
1002 status
= gssapi_seal_packet(gse_ctx
->gssapi_context
,
1004 hdr_signing
, sig_size
,
1006 whole_pdu
, pdu_length
,
1008 if (!NT_STATUS_IS_OK(status
)) {
1009 DEBUG(0, ("gssapi_seal_packet(hdr_signing=%u,sig_size=%zu,"
1010 "data=%zu,pdu=%zu) failed: %s\n",
1011 hdr_signing
, sig_size
, length
, pdu_length
,
1012 nt_errstr(status
)));
1016 return NT_STATUS_OK
;
1019 static NTSTATUS
gensec_gse_unseal_packet(struct gensec_security
*gensec_security
,
1020 uint8_t *data
, size_t length
,
1021 const uint8_t *whole_pdu
, size_t pdu_length
,
1022 const DATA_BLOB
*sig
)
1024 struct gse_context
*gse_ctx
=
1025 talloc_get_type_abort(gensec_security
->private_data
,
1026 struct gse_context
);
1027 bool hdr_signing
= false;
1030 if (gensec_security
->want_features
& GENSEC_FEATURE_SIGN_PKT_HEADER
) {
1034 status
= gssapi_unseal_packet(gse_ctx
->gssapi_context
,
1038 whole_pdu
, pdu_length
,
1040 if (!NT_STATUS_IS_OK(status
)) {
1041 DEBUG(0, ("gssapi_unseal_packet(hdr_signing=%u,sig_size=%zu,"
1042 "data=%zu,pdu=%zu) failed: %s\n",
1043 hdr_signing
, sig
->length
, length
, pdu_length
,
1044 nt_errstr(status
)));
1048 return NT_STATUS_OK
;
1051 static NTSTATUS
gensec_gse_sign_packet(struct gensec_security
*gensec_security
,
1052 TALLOC_CTX
*mem_ctx
,
1053 const uint8_t *data
, size_t length
,
1054 const uint8_t *whole_pdu
, size_t pdu_length
,
1057 struct gse_context
*gse_ctx
=
1058 talloc_get_type_abort(gensec_security
->private_data
,
1059 struct gse_context
);
1060 bool hdr_signing
= false;
1063 if (gensec_security
->want_features
& GENSEC_FEATURE_SIGN_PKT_HEADER
) {
1067 status
= gssapi_sign_packet(gse_ctx
->gssapi_context
,
1071 whole_pdu
, pdu_length
,
1073 if (!NT_STATUS_IS_OK(status
)) {
1074 DEBUG(0, ("gssapi_sign_packet(hdr_signing=%u,"
1075 "data=%zu,pdu=%zu) failed: %s\n",
1076 hdr_signing
, length
, pdu_length
,
1077 nt_errstr(status
)));
1081 return NT_STATUS_OK
;
1084 static NTSTATUS
gensec_gse_check_packet(struct gensec_security
*gensec_security
,
1085 const uint8_t *data
, size_t length
,
1086 const uint8_t *whole_pdu
, size_t pdu_length
,
1087 const DATA_BLOB
*sig
)
1089 struct gse_context
*gse_ctx
=
1090 talloc_get_type_abort(gensec_security
->private_data
,
1091 struct gse_context
);
1092 bool hdr_signing
= false;
1095 if (gensec_security
->want_features
& GENSEC_FEATURE_SIGN_PKT_HEADER
) {
1099 status
= gssapi_check_packet(gse_ctx
->gssapi_context
,
1103 whole_pdu
, pdu_length
,
1105 if (!NT_STATUS_IS_OK(status
)) {
1106 DEBUG(0, ("gssapi_check_packet(hdr_signing=%u,sig_size=%zu"
1107 "data=%zu,pdu=%zu) failed: %s\n",
1108 hdr_signing
, sig
->length
, length
, pdu_length
,
1109 nt_errstr(status
)));
1113 return NT_STATUS_OK
;
1116 /* Try to figure out what features we actually got on the connection */
1117 static bool gensec_gse_have_feature(struct gensec_security
*gensec_security
,
1120 struct gse_context
*gse_ctx
=
1121 talloc_get_type_abort(gensec_security
->private_data
,
1122 struct gse_context
);
1124 if (feature
& GENSEC_FEATURE_SESSION_KEY
) {
1125 return gse_ctx
->gss_got_flags
& GSS_C_INTEG_FLAG
;
1127 if (feature
& GENSEC_FEATURE_SIGN
) {
1128 return gse_ctx
->gss_got_flags
& GSS_C_INTEG_FLAG
;
1130 if (feature
& GENSEC_FEATURE_SEAL
) {
1131 return gse_ctx
->gss_got_flags
& GSS_C_CONF_FLAG
;
1133 if (feature
& GENSEC_FEATURE_DCE_STYLE
) {
1134 return gse_ctx
->gss_got_flags
& GSS_C_DCE_STYLE
;
1136 if (feature
& GENSEC_FEATURE_NEW_SPNEGO
) {
1140 if (!(gse_ctx
->gss_got_flags
& GSS_C_INTEG_FLAG
)) {
1144 status
= gssapi_get_session_key(talloc_tos(),
1145 gse_ctx
->gssapi_context
, NULL
, &keytype
);
1147 * We should do a proper sig on the mechListMic unless
1148 * we know we have to be backwards compatible with
1149 * earlier windows versions.
1151 * Negotiating a non-krb5
1152 * mech for example should be regarded as having
1155 if (NT_STATUS_IS_OK(status
)) {
1157 case ENCTYPE_DES_CBC_CRC
:
1158 case ENCTYPE_DES_CBC_MD5
:
1159 case ENCTYPE_ARCFOUR_HMAC
:
1160 case ENCTYPE_DES3_CBC_SHA1
:
1166 /* We can always do async (rather than strict request/reply) packets. */
1167 if (feature
& GENSEC_FEATURE_ASYNC_REPLIES
) {
1170 if (feature
& GENSEC_FEATURE_SIGN_PKT_HEADER
) {
1176 static NTTIME
gensec_gse_expire_time(struct gensec_security
*gensec_security
)
1178 struct gse_context
*gse_ctx
=
1179 talloc_get_type_abort(gensec_security
->private_data
,
1180 struct gse_context
);
1182 return gse_ctx
->expire_time
;
1186 * Extract the 'sesssion key' needed by SMB signing and ncacn_np
1187 * (for encrypting some passwords).
1189 * This breaks all the abstractions, but what do you expect...
1191 static NTSTATUS
gensec_gse_session_key(struct gensec_security
*gensec_security
,
1192 TALLOC_CTX
*mem_ctx
,
1193 DATA_BLOB
*session_key
)
1195 struct gse_context
*gse_ctx
=
1196 talloc_get_type_abort(gensec_security
->private_data
,
1197 struct gse_context
);
1199 return gssapi_get_session_key(mem_ctx
, gse_ctx
->gssapi_context
, session_key
, NULL
);
1202 /* Get some basic (and authorization) information about the user on
1203 * this session. This uses either the PAC (if present) or a local
1204 * database lookup */
1205 static NTSTATUS
gensec_gse_session_info(struct gensec_security
*gensec_security
,
1206 TALLOC_CTX
*mem_ctx
,
1207 struct auth_session_info
**_session_info
)
1209 struct gse_context
*gse_ctx
=
1210 talloc_get_type_abort(gensec_security
->private_data
,
1211 struct gse_context
);
1213 TALLOC_CTX
*tmp_ctx
;
1214 struct auth_session_info
*session_info
= NULL
;
1215 OM_uint32 maj_stat
, min_stat
;
1216 DATA_BLOB pac_blob
, *pac_blob_ptr
= NULL
;
1218 gss_buffer_desc name_token
;
1219 char *principal_string
;
1221 tmp_ctx
= talloc_named(mem_ctx
, 0, "gensec_gse_session_info context");
1222 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
1224 maj_stat
= gss_display_name(&min_stat
,
1225 gse_ctx
->client_name
,
1228 if (GSS_ERROR(maj_stat
)) {
1229 DEBUG(1, ("GSS display_name failed: %s\n",
1230 gse_errstr(talloc_tos(), maj_stat
, min_stat
)));
1231 talloc_free(tmp_ctx
);
1232 return NT_STATUS_FOOBAR
;
1235 principal_string
= talloc_strndup(tmp_ctx
,
1236 (const char *)name_token
.value
,
1239 gss_release_buffer(&min_stat
, &name_token
);
1241 if (!principal_string
) {
1242 talloc_free(tmp_ctx
);
1243 return NT_STATUS_NO_MEMORY
;
1246 nt_status
= gssapi_obtain_pac_blob(tmp_ctx
, gse_ctx
->gssapi_context
,
1247 gse_ctx
->client_name
,
1250 /* IF we have the PAC - otherwise we need to get this
1251 * data from elsewere
1253 if (NT_STATUS_IS_OK(nt_status
)) {
1254 pac_blob_ptr
= &pac_blob
;
1256 nt_status
= gensec_generate_session_info_pac(tmp_ctx
,
1259 pac_blob_ptr
, principal_string
,
1260 gensec_get_remote_address(gensec_security
),
1262 if (!NT_STATUS_IS_OK(nt_status
)) {
1263 talloc_free(tmp_ctx
);
1267 nt_status
= gensec_gse_session_key(gensec_security
, session_info
,
1268 &session_info
->session_key
);
1269 if (!NT_STATUS_IS_OK(nt_status
)) {
1270 talloc_free(tmp_ctx
);
1274 *_session_info
= talloc_move(mem_ctx
, &session_info
);
1275 talloc_free(tmp_ctx
);
1277 return NT_STATUS_OK
;
1280 static size_t gensec_gse_max_input_size(struct gensec_security
*gensec_security
)
1282 struct gse_context
*gse_ctx
=
1283 talloc_get_type_abort(gensec_security
->private_data
,
1284 struct gse_context
);
1285 OM_uint32 maj_stat
, min_stat
;
1286 OM_uint32 max_input_size
;
1288 maj_stat
= gss_wrap_size_limit(&min_stat
,
1289 gse_ctx
->gssapi_context
,
1290 gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
),
1292 gse_ctx
->max_wrap_buf_size
,
1294 if (GSS_ERROR(maj_stat
)) {
1295 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
1296 DEBUG(1, ("gensec_gssapi_max_input_size: determining signature size with gss_wrap_size_limit failed: %s\n",
1297 gse_errstr(mem_ctx
, maj_stat
, min_stat
)));
1298 talloc_free(mem_ctx
);
1302 return max_input_size
;
1305 /* Find out the maximum output size negotiated on this connection */
1306 static size_t gensec_gse_max_wrapped_size(struct gensec_security
*gensec_security
)
1308 struct gse_context
*gse_ctx
=
1309 talloc_get_type_abort(gensec_security
->private_data
,
1310 struct gse_context
);
1311 return gse_ctx
->max_wrap_buf_size
;
1314 static size_t gensec_gse_sig_size(struct gensec_security
*gensec_security
,
1317 struct gse_context
*gse_ctx
=
1318 talloc_get_type_abort(gensec_security
->private_data
,
1319 struct gse_context
);
1321 if (gse_ctx
->sig_size
> 0) {
1322 return gse_ctx
->sig_size
;
1325 gse_ctx
->sig_size
= gssapi_get_sig_size(gse_ctx
->gssapi_context
,
1327 gse_ctx
->gss_got_flags
,
1329 return gse_ctx
->sig_size
;
1332 static const char *gensec_gse_final_auth_type(struct gensec_security
*gensec_security
)
1334 struct gse_context
*gse_ctx
=
1335 talloc_get_type_abort(gensec_security
->private_data
,
1336 struct gse_context
);
1338 /* Only return the string for GSSAPI/Krb5 */
1339 if (smb_gss_oid_equal(&gse_ctx
->gss_mech
,
1341 return GENSEC_FINAL_AUTH_TYPE_KRB5
;
1343 return "gensec_gse: UNKNOWN MECH";
1347 static const char *gensec_gse_krb5_oids
[] = {
1348 GENSEC_OID_KERBEROS5_OLD
,
1349 GENSEC_OID_KERBEROS5
,
1353 const struct gensec_security_ops gensec_gse_krb5_security_ops
= {
1355 .auth_type
= DCERPC_AUTH_TYPE_KRB5
,
1356 .oid
= gensec_gse_krb5_oids
,
1357 .client_start
= gensec_gse_client_start
,
1358 .server_start
= gensec_gse_server_start
,
1359 .magic
= gensec_magic_check_krb5_oid
,
1360 .update_send
= gensec_gse_update_send
,
1361 .update_recv
= gensec_gse_update_recv
,
1362 .session_key
= gensec_gse_session_key
,
1363 .session_info
= gensec_gse_session_info
,
1364 .sig_size
= gensec_gse_sig_size
,
1365 .sign_packet
= gensec_gse_sign_packet
,
1366 .check_packet
= gensec_gse_check_packet
,
1367 .seal_packet
= gensec_gse_seal_packet
,
1368 .unseal_packet
= gensec_gse_unseal_packet
,
1369 .max_input_size
= gensec_gse_max_input_size
,
1370 .max_wrapped_size
= gensec_gse_max_wrapped_size
,
1371 .wrap
= gensec_gse_wrap
,
1372 .unwrap
= gensec_gse_unwrap
,
1373 .have_feature
= gensec_gse_have_feature
,
1374 .expire_time
= gensec_gse_expire_time
,
1375 .final_auth_type
= gensec_gse_final_auth_type
,
1378 .priority
= GENSEC_GSSAPI
1381 #endif /* HAVE_KRB5 */