2 Unix SMB/CIFS implementation.
4 Kerberos backend for GENSEC
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
7 Copyright (C) Stefan Metzmacher <metze@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.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "lib/events/events.h"
26 #include "system/kerberos.h"
27 #include "system/gssapi.h"
28 #include "auth/kerberos/kerberos.h"
29 #include "librpc/gen_ndr/krb5pac.h"
30 #include "auth/auth.h"
32 #include "auth/auth_sam.h"
33 #include "librpc/rpc/dcerpc.h"
34 #include "auth/credentials/credentials.h"
35 #include "auth/credentials/credentials_krb5.h"
36 #include "auth/gensec/gensec.h"
37 #include "auth/gensec/gensec_internal.h"
38 #include "auth/gensec/gensec_proto.h"
39 #include "auth/gensec/gensec_toplevel_proto.h"
40 #include "param/param.h"
41 #include "auth/session_proto.h"
42 #include "gensec_gssapi.h"
43 #include "lib/util/util_net.h"
44 #include "auth/kerberos/pac_utils.h"
46 #ifndef gss_mech_spnego
47 gss_OID_desc spnego_mech_oid_desc
=
48 { 6, discard_const_p(void, "\x2b\x06\x01\x05\x05\x02") };
49 #define gss_mech_spnego (&spnego_mech_oid_desc)
52 _PUBLIC_ NTSTATUS
gensec_gssapi_init(void);
54 static size_t gensec_gssapi_max_input_size(struct gensec_security
*gensec_security
);
55 static size_t gensec_gssapi_max_wrapped_size(struct gensec_security
*gensec_security
);
57 static int gensec_gssapi_destructor(struct gensec_gssapi_state
*gensec_gssapi_state
)
59 OM_uint32 maj_stat
, min_stat
;
61 if (gensec_gssapi_state
->delegated_cred_handle
!= GSS_C_NO_CREDENTIAL
) {
62 maj_stat
= gss_release_cred(&min_stat
,
63 &gensec_gssapi_state
->delegated_cred_handle
);
66 if (gensec_gssapi_state
->gssapi_context
!= GSS_C_NO_CONTEXT
) {
67 maj_stat
= gss_delete_sec_context (&min_stat
,
68 &gensec_gssapi_state
->gssapi_context
,
72 if (gensec_gssapi_state
->server_name
!= GSS_C_NO_NAME
) {
73 maj_stat
= gss_release_name(&min_stat
, &gensec_gssapi_state
->server_name
);
75 if (gensec_gssapi_state
->client_name
!= GSS_C_NO_NAME
) {
76 maj_stat
= gss_release_name(&min_stat
, &gensec_gssapi_state
->client_name
);
79 if (gensec_gssapi_state
->lucid
) {
80 gss_krb5_free_lucid_sec_context(&min_stat
, gensec_gssapi_state
->lucid
);
86 static NTSTATUS
gensec_gssapi_init_lucid(struct gensec_gssapi_state
*gensec_gssapi_state
)
88 OM_uint32 maj_stat
, min_stat
;
90 if (gensec_gssapi_state
->lucid
) {
94 maj_stat
= gss_krb5_export_lucid_sec_context(&min_stat
,
95 &gensec_gssapi_state
->gssapi_context
,
97 (void **)&gensec_gssapi_state
->lucid
);
98 if (maj_stat
!= GSS_S_COMPLETE
) {
99 DEBUG(0,("gensec_gssapi_init_lucid: %s\n",
100 gssapi_error_string(gensec_gssapi_state
,
102 gensec_gssapi_state
->gss_oid
)));
103 return NT_STATUS_INTERNAL_ERROR
;
106 if (gensec_gssapi_state
->lucid
->version
!= 1) {
107 DEBUG(0,("gensec_gssapi_init_lucid: lucid version[%d] != 1\n",
108 gensec_gssapi_state
->lucid
->version
));
109 gss_krb5_free_lucid_sec_context(&min_stat
, gensec_gssapi_state
->lucid
);
110 gensec_gssapi_state
->lucid
= NULL
;
111 return NT_STATUS_INTERNAL_ERROR
;
117 static NTSTATUS
gensec_gssapi_start(struct gensec_security
*gensec_security
)
119 struct gensec_gssapi_state
*gensec_gssapi_state
;
123 gensec_gssapi_state
= talloc_zero(gensec_security
, struct gensec_gssapi_state
);
124 if (!gensec_gssapi_state
) {
125 return NT_STATUS_NO_MEMORY
;
128 gensec_security
->private_data
= gensec_gssapi_state
;
130 gensec_gssapi_state
->gssapi_context
= GSS_C_NO_CONTEXT
;
132 /* TODO: Fill in channel bindings */
133 gensec_gssapi_state
->input_chan_bindings
= GSS_C_NO_CHANNEL_BINDINGS
;
135 gensec_gssapi_state
->server_name
= GSS_C_NO_NAME
;
136 gensec_gssapi_state
->client_name
= GSS_C_NO_NAME
;
138 gensec_gssapi_state
->gss_want_flags
= 0;
139 gensec_gssapi_state
->expire_time
= GENSEC_EXPIRE_TIME_INFINITY
;
141 if (gensec_setting_bool(gensec_security
->settings
, "gensec_gssapi", "delegation_by_kdc_policy", true)) {
142 gensec_gssapi_state
->gss_want_flags
|= GSS_C_DELEG_POLICY_FLAG
;
144 if (gensec_setting_bool(gensec_security
->settings
, "gensec_gssapi", "mutual", true)) {
145 gensec_gssapi_state
->gss_want_flags
|= GSS_C_MUTUAL_FLAG
;
147 if (gensec_setting_bool(gensec_security
->settings
, "gensec_gssapi", "delegation", true)) {
148 gensec_gssapi_state
->gss_want_flags
|= GSS_C_DELEG_FLAG
;
150 if (gensec_setting_bool(gensec_security
->settings
, "gensec_gssapi", "replay", true)) {
151 gensec_gssapi_state
->gss_want_flags
|= GSS_C_REPLAY_FLAG
;
153 if (gensec_setting_bool(gensec_security
->settings
, "gensec_gssapi", "sequence", true)) {
154 gensec_gssapi_state
->gss_want_flags
|= GSS_C_SEQUENCE_FLAG
;
157 if (gensec_security
->want_features
& GENSEC_FEATURE_SIGN
) {
158 gensec_gssapi_state
->gss_want_flags
|= GSS_C_INTEG_FLAG
;
160 if (gensec_security
->want_features
& GENSEC_FEATURE_SEAL
) {
161 gensec_gssapi_state
->gss_want_flags
|= GSS_C_INTEG_FLAG
;
162 gensec_gssapi_state
->gss_want_flags
|= GSS_C_CONF_FLAG
;
164 if (gensec_security
->want_features
& GENSEC_FEATURE_DCE_STYLE
) {
165 gensec_gssapi_state
->gss_want_flags
|= GSS_C_DCE_STYLE
;
168 gensec_gssapi_state
->gss_got_flags
= 0;
170 switch (gensec_security
->ops
->auth_type
) {
171 case DCERPC_AUTH_TYPE_SPNEGO
:
172 gensec_gssapi_state
->gss_oid
= gss_mech_spnego
;
174 case DCERPC_AUTH_TYPE_KRB5
:
176 gensec_gssapi_state
->gss_oid
=
177 discard_const_p(void, gss_mech_krb5
);
181 ret
= smb_krb5_init_context(gensec_gssapi_state
,
183 gensec_security
->settings
->lp_ctx
,
184 &gensec_gssapi_state
->smb_krb5_context
);
186 DEBUG(1,("gensec_gssapi_start: smb_krb5_init_context failed (%s)\n",
187 error_message(ret
)));
188 talloc_free(gensec_gssapi_state
);
189 return NT_STATUS_INTERNAL_ERROR
;
192 gensec_gssapi_state
->client_cred
= NULL
;
193 gensec_gssapi_state
->server_cred
= NULL
;
195 gensec_gssapi_state
->lucid
= NULL
;
197 gensec_gssapi_state
->delegated_cred_handle
= GSS_C_NO_CREDENTIAL
;
199 gensec_gssapi_state
->sasl
= false;
200 gensec_gssapi_state
->sasl_state
= STAGE_GSS_NEG
;
201 gensec_gssapi_state
->sasl_protection
= 0;
203 gensec_gssapi_state
->max_wrap_buf_size
204 = gensec_setting_int(gensec_security
->settings
, "gensec_gssapi", "max wrap buf size", 65536);
205 gensec_gssapi_state
->gss_exchange_count
= 0;
206 gensec_gssapi_state
->sig_size
= 0;
208 talloc_set_destructor(gensec_gssapi_state
, gensec_gssapi_destructor
);
210 #ifdef SAMBA4_USES_HEIMDAL
211 realm
= lpcfg_realm(gensec_security
->settings
->lp_ctx
);
213 ret
= gsskrb5_set_default_realm(realm
);
215 DEBUG(1,("gensec_gssapi_start: gsskrb5_set_default_realm failed\n"));
216 talloc_free(gensec_gssapi_state
);
217 return NT_STATUS_INTERNAL_ERROR
;
221 /* don't do DNS lookups of any kind, it might/will fail for a netbios name */
222 ret
= gsskrb5_set_dns_canonicalize(gensec_setting_bool(gensec_security
->settings
, "krb5", "set_dns_canonicalize", false));
224 DEBUG(1,("gensec_gssapi_start: gsskrb5_set_dns_canonicalize failed\n"));
225 talloc_free(gensec_gssapi_state
);
226 return NT_STATUS_INTERNAL_ERROR
;
232 static NTSTATUS
gensec_gssapi_server_start(struct gensec_security
*gensec_security
)
236 struct gensec_gssapi_state
*gensec_gssapi_state
;
237 struct cli_credentials
*machine_account
;
238 struct gssapi_creds_container
*gcc
;
240 nt_status
= gensec_gssapi_start(gensec_security
);
241 if (!NT_STATUS_IS_OK(nt_status
)) {
245 gensec_gssapi_state
= talloc_get_type(gensec_security
->private_data
, struct gensec_gssapi_state
);
247 machine_account
= gensec_get_credentials(gensec_security
);
249 if (!machine_account
) {
250 DEBUG(3, ("No machine account credentials specified\n"));
251 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
253 ret
= cli_credentials_get_server_gss_creds(machine_account
,
254 gensec_security
->settings
->lp_ctx
, &gcc
);
256 DEBUG(1, ("Aquiring acceptor credentials failed: %s\n",
257 error_message(ret
)));
258 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
262 gensec_gssapi_state
->server_cred
= gcc
;
267 static NTSTATUS
gensec_gssapi_sasl_server_start(struct gensec_security
*gensec_security
)
270 struct gensec_gssapi_state
*gensec_gssapi_state
;
271 nt_status
= gensec_gssapi_server_start(gensec_security
);
273 if (NT_STATUS_IS_OK(nt_status
)) {
274 gensec_gssapi_state
= talloc_get_type(gensec_security
->private_data
, struct gensec_gssapi_state
);
275 gensec_gssapi_state
->sasl
= true;
280 static NTSTATUS
gensec_gssapi_client_creds(struct gensec_security
*gensec_security
,
281 struct tevent_context
*ev
)
283 struct gensec_gssapi_state
*gensec_gssapi_state
;
284 struct gssapi_creds_container
*gcc
;
285 struct cli_credentials
*creds
= gensec_get_credentials(gensec_security
);
286 const char *error_string
;
289 gensec_gssapi_state
= talloc_get_type(gensec_security
->private_data
, struct gensec_gssapi_state
);
291 /* Only run this the first time the update() call is made */
292 if (gensec_gssapi_state
->client_cred
) {
296 ret
= cli_credentials_get_client_gss_creds(creds
,
298 gensec_security
->settings
->lp_ctx
, &gcc
, &error_string
);
303 DEBUG(3, ("Cannot obtain client GSS credentials we need to contact %s : %s\n", gensec_gssapi_state
->target_principal
, error_string
));
304 return NT_STATUS_INVALID_PARAMETER
;
305 case KRB5KDC_ERR_PREAUTH_FAILED
:
306 case KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
:
307 DEBUG(1, ("Wrong username or password: %s\n", error_string
));
308 return NT_STATUS_LOGON_FAILURE
;
309 case KRB5_KDC_UNREACH
:
310 DEBUG(3, ("Cannot reach a KDC we require to contact %s : %s\n", gensec_gssapi_state
->target_principal
, error_string
));
311 return NT_STATUS_NO_LOGON_SERVERS
;
312 case KRB5_CC_NOTFOUND
:
314 DEBUG(2, ("Error obtaining ticket we require to contact %s: (possibly due to clock skew between us and the KDC) %s\n", gensec_gssapi_state
->target_principal
, error_string
));
315 return NT_STATUS_TIME_DIFFERENCE_AT_DC
;
317 DEBUG(1, ("Aquiring initiator credentials failed: %s\n", error_string
));
318 return NT_STATUS_UNSUCCESSFUL
;
321 gensec_gssapi_state
->client_cred
= gcc
;
322 if (!talloc_reference(gensec_gssapi_state
, gcc
)) {
323 return NT_STATUS_NO_MEMORY
;
329 static NTSTATUS
gensec_gssapi_client_start(struct gensec_security
*gensec_security
)
331 struct gensec_gssapi_state
*gensec_gssapi_state
;
332 struct cli_credentials
*creds
= gensec_get_credentials(gensec_security
);
334 gss_buffer_desc name_token
;
336 OM_uint32 maj_stat
, min_stat
;
337 const char *hostname
= gensec_get_target_hostname(gensec_security
);
340 DEBUG(3, ("No hostname for target computer passed in, cannot use kerberos for this connection\n"));
341 return NT_STATUS_INVALID_PARAMETER
;
343 if (is_ipaddress(hostname
)) {
344 DEBUG(2, ("Cannot do GSSAPI to an IP address\n"));
345 return NT_STATUS_INVALID_PARAMETER
;
347 if (strcmp(hostname
, "localhost") == 0) {
348 DEBUG(2, ("GSSAPI to 'localhost' does not make sense\n"));
349 return NT_STATUS_INVALID_PARAMETER
;
352 nt_status
= gensec_gssapi_start(gensec_security
);
353 if (!NT_STATUS_IS_OK(nt_status
)) {
357 gensec_gssapi_state
= talloc_get_type(gensec_security
->private_data
, struct gensec_gssapi_state
);
359 if (cli_credentials_get_impersonate_principal(creds
)) {
360 gensec_gssapi_state
->gss_want_flags
&= ~(GSS_C_DELEG_FLAG
|GSS_C_DELEG_POLICY_FLAG
);
363 gensec_gssapi_state
->target_principal
= gensec_get_target_principal(gensec_security
);
364 if (gensec_gssapi_state
->target_principal
) {
365 name_type
= GSS_C_NULL_OID
;
367 gensec_gssapi_state
->target_principal
= talloc_asprintf(gensec_gssapi_state
, "%s/%s@%s",
368 gensec_get_target_service(gensec_security
),
369 hostname
, lpcfg_realm(gensec_security
->settings
->lp_ctx
));
371 name_type
= GSS_C_NT_USER_NAME
;
373 name_token
.value
= discard_const_p(uint8_t, gensec_gssapi_state
->target_principal
);
374 name_token
.length
= strlen(gensec_gssapi_state
->target_principal
);
377 maj_stat
= gss_import_name (&min_stat
,
380 &gensec_gssapi_state
->server_name
);
382 DEBUG(2, ("GSS Import name of %s failed: %s\n",
383 (char *)name_token
.value
,
384 gssapi_error_string(gensec_gssapi_state
, maj_stat
, min_stat
, gensec_gssapi_state
->gss_oid
)));
385 return NT_STATUS_INVALID_PARAMETER
;
391 static NTSTATUS
gensec_gssapi_sasl_client_start(struct gensec_security
*gensec_security
)
394 struct gensec_gssapi_state
*gensec_gssapi_state
;
395 nt_status
= gensec_gssapi_client_start(gensec_security
);
397 if (NT_STATUS_IS_OK(nt_status
)) {
398 gensec_gssapi_state
= talloc_get_type(gensec_security
->private_data
, struct gensec_gssapi_state
);
399 gensec_gssapi_state
->sasl
= true;
406 * Next state function for the GSSAPI GENSEC mechanism
408 * @param gensec_gssapi_state GSSAPI State
409 * @param out_mem_ctx The TALLOC_CTX for *out to be allocated on
410 * @param in The request, as a DATA_BLOB
411 * @param out The reply, as an talloc()ed DATA_BLOB, on *out_mem_ctx
412 * @return Error, MORE_PROCESSING_REQUIRED if a reply is sent,
413 * or NT_STATUS_OK if the user is authenticated.
416 static NTSTATUS
gensec_gssapi_update(struct gensec_security
*gensec_security
,
417 TALLOC_CTX
*out_mem_ctx
,
418 struct tevent_context
*ev
,
419 const DATA_BLOB in
, DATA_BLOB
*out
)
421 struct gensec_gssapi_state
*gensec_gssapi_state
422 = talloc_get_type(gensec_security
->private_data
, struct gensec_gssapi_state
);
423 NTSTATUS nt_status
= NT_STATUS_LOGON_FAILURE
;
424 OM_uint32 maj_stat
, min_stat
;
426 gss_buffer_desc input_token
, output_token
;
427 gss_OID gss_oid_p
= NULL
;
428 OM_uint32 time_req
= 0;
429 OM_uint32 time_rec
= 0;
432 time_req
= gensec_setting_int(gensec_security
->settings
,
433 "gensec_gssapi", "requested_life_time",
436 input_token
.length
= in
.length
;
437 input_token
.value
= in
.data
;
439 switch (gensec_gssapi_state
->sasl_state
) {
442 switch (gensec_security
->gensec_role
) {
445 #ifdef SAMBA4_USES_HEIMDAL
446 struct gsskrb5_send_to_kdc send_to_kdc
;
450 nt_status
= gensec_gssapi_client_creds(gensec_security
, ev
);
451 if (!NT_STATUS_IS_OK(nt_status
)) {
455 #ifdef SAMBA4_USES_HEIMDAL
456 send_to_kdc
.func
= smb_krb5_send_and_recv_func
;
457 send_to_kdc
.ptr
= ev
;
459 min_stat
= gsskrb5_set_send_to_kdc(&send_to_kdc
);
461 DEBUG(1,("gensec_gssapi_update: gsskrb5_set_send_to_kdc failed\n"));
462 return NT_STATUS_INTERNAL_ERROR
;
465 maj_stat
= gss_init_sec_context(&min_stat
,
466 gensec_gssapi_state
->client_cred
->creds
,
467 &gensec_gssapi_state
->gssapi_context
,
468 gensec_gssapi_state
->server_name
,
469 gensec_gssapi_state
->gss_oid
,
470 gensec_gssapi_state
->gss_want_flags
,
472 gensec_gssapi_state
->input_chan_bindings
,
476 &gensec_gssapi_state
->gss_got_flags
, /* ret flags */
479 gensec_gssapi_state
->gss_oid
= gss_oid_p
;
482 #ifdef SAMBA4_USES_HEIMDAL
483 send_to_kdc
.func
= smb_krb5_send_and_recv_func
;
484 send_to_kdc
.ptr
= NULL
;
486 ret
= gsskrb5_set_send_to_kdc(&send_to_kdc
);
488 DEBUG(1,("gensec_gssapi_update: gsskrb5_set_send_to_kdc failed\n"));
489 return NT_STATUS_INTERNAL_ERROR
;
496 maj_stat
= gss_accept_sec_context(&min_stat
,
497 &gensec_gssapi_state
->gssapi_context
,
498 gensec_gssapi_state
->server_cred
->creds
,
500 gensec_gssapi_state
->input_chan_bindings
,
501 &gensec_gssapi_state
->client_name
,
504 &gensec_gssapi_state
->gss_got_flags
,
506 &gensec_gssapi_state
->delegated_cred_handle
);
508 gensec_gssapi_state
->gss_oid
= gss_oid_p
;
513 return NT_STATUS_INVALID_PARAMETER
;
517 gensec_gssapi_state
->gss_exchange_count
++;
519 if (maj_stat
== GSS_S_COMPLETE
) {
520 *out
= data_blob_talloc(out_mem_ctx
, output_token
.value
, output_token
.length
);
521 gss_release_buffer(&min_stat2
, &output_token
);
523 if (gensec_gssapi_state
->gss_got_flags
& GSS_C_DELEG_FLAG
) {
524 DEBUG(5, ("gensec_gssapi: credentials were delegated\n"));
526 DEBUG(5, ("gensec_gssapi: NO credentials were delegated\n"));
529 tv
= timeval_current_ofs(time_rec
, 0);
530 gensec_gssapi_state
->expire_time
= timeval_to_nttime(&tv
);
532 /* We may have been invoked as SASL, so there
533 * is more work to do */
534 if (gensec_gssapi_state
->sasl
) {
535 gensec_gssapi_state
->sasl_state
= STAGE_SASL_SSF_NEG
;
536 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
538 gensec_gssapi_state
->sasl_state
= STAGE_DONE
;
540 if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
)) {
541 DEBUG(5, ("GSSAPI Connection will be cryptographically sealed\n"));
542 } else if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SIGN
)) {
543 DEBUG(5, ("GSSAPI Connection will be cryptographically signed\n"));
545 DEBUG(5, ("GSSAPI Connection will have no cryptographic protection\n"));
550 } else if (maj_stat
== GSS_S_CONTINUE_NEEDED
) {
551 *out
= data_blob_talloc(out_mem_ctx
, output_token
.value
, output_token
.length
);
552 gss_release_buffer(&min_stat2
, &output_token
);
554 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
555 } else if (maj_stat
== GSS_S_CONTEXT_EXPIRED
) {
558 gss_buffer_desc buffer
;
559 OM_uint32 lifetime
= 0;
560 gss_cred_usage_t usage
;
561 const char *role
= NULL
;
562 DEBUG(0, ("GSS %s Update(krb5)(%d) Update failed, credentials expired during GSSAPI handshake!\n",
564 gensec_gssapi_state
->gss_exchange_count
));
567 switch (gensec_security
->gensec_role
) {
569 creds
= gensec_gssapi_state
->client_cred
->creds
;
573 creds
= gensec_gssapi_state
->server_cred
->creds
;
578 maj_stat
= gss_inquire_cred(&min_stat
,
580 &name
, &lifetime
, &usage
, NULL
);
582 if (maj_stat
== GSS_S_COMPLETE
) {
583 const char *usage_string
;
586 usage_string
= "GSS_C_BOTH";
589 usage_string
= "GSS_C_ACCEPT";
592 usage_string
= "GSS_C_INITIATE";
595 maj_stat
= gss_display_name(&min_stat
, name
, &buffer
, NULL
);
601 DEBUG(0, ("GSSAPI gss_inquire_cred indicates expiry of %*.*s in %u sec for %s\n",
602 (int)buffer
.length
, (int)buffer
.length
, (char *)buffer
.value
,
603 lifetime
, usage_string
));
605 DEBUG(0, ("GSSAPI gss_inquire_cred indicates %*.*s has already expired for %s\n",
606 (int)buffer
.length
, (int)buffer
.length
, (char *)buffer
.value
,
609 gss_release_buffer(&min_stat
, &buffer
);
610 gss_release_name(&min_stat
, &name
);
611 } else if (maj_stat
!= GSS_S_COMPLETE
) {
612 DEBUG(0, ("inquiry of credential lifefime via GSSAPI gss_inquire_cred failed: %s\n",
613 gssapi_error_string(out_mem_ctx
, maj_stat
, min_stat
, gensec_gssapi_state
->gss_oid
)));
615 return NT_STATUS_INVALID_PARAMETER
;
616 } else if (smb_gss_oid_equal(gensec_gssapi_state
->gss_oid
,
619 case KRB5KRB_AP_ERR_TKT_NYV
:
620 DEBUG(1, ("Error with ticket to contact %s: possible clock skew between us and the KDC or target server: %s\n",
621 gensec_gssapi_state
->target_principal
,
622 gssapi_error_string(out_mem_ctx
, maj_stat
, min_stat
, gensec_gssapi_state
->gss_oid
)));
623 return NT_STATUS_TIME_DIFFERENCE_AT_DC
; /* Make SPNEGO ignore us, we can't go any further here */
624 case KRB5KRB_AP_ERR_TKT_EXPIRED
:
625 DEBUG(1, ("Error with ticket to contact %s: ticket is expired, possible clock skew between us and the KDC or target server: %s\n",
626 gensec_gssapi_state
->target_principal
,
627 gssapi_error_string(out_mem_ctx
, maj_stat
, min_stat
, gensec_gssapi_state
->gss_oid
)));
628 return NT_STATUS_INVALID_PARAMETER
; /* Make SPNEGO ignore us, we can't go any further here */
629 case KRB5_KDC_UNREACH
:
630 DEBUG(3, ("Cannot reach a KDC we require in order to obtain a ticetk to %s: %s\n",
631 gensec_gssapi_state
->target_principal
,
632 gssapi_error_string(out_mem_ctx
, maj_stat
, min_stat
, gensec_gssapi_state
->gss_oid
)));
633 return NT_STATUS_NO_LOGON_SERVERS
; /* Make SPNEGO ignore us, we can't go any further here */
634 case KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
:
635 DEBUG(3, ("Server %s is not registered with our KDC: %s\n",
636 gensec_gssapi_state
->target_principal
,
637 gssapi_error_string(out_mem_ctx
, maj_stat
, min_stat
, gensec_gssapi_state
->gss_oid
)));
638 return NT_STATUS_INVALID_PARAMETER
; /* Make SPNEGO ignore us, we can't go any further here */
639 case KRB5KRB_AP_ERR_MSG_TYPE
:
640 /* garbage input, possibly from the auto-mech detection */
641 return NT_STATUS_INVALID_PARAMETER
;
643 DEBUG(1, ("GSS %s Update(krb5)(%d) Update failed: %s\n",
644 gensec_security
->gensec_role
== GENSEC_CLIENT
? "client" : "server",
645 gensec_gssapi_state
->gss_exchange_count
,
646 gssapi_error_string(out_mem_ctx
, maj_stat
, min_stat
, gensec_gssapi_state
->gss_oid
)));
650 DEBUG(1, ("GSS %s Update(%d) failed: %s\n",
651 gensec_security
->gensec_role
== GENSEC_CLIENT
? "client" : "server",
652 gensec_gssapi_state
->gss_exchange_count
,
653 gssapi_error_string(out_mem_ctx
, maj_stat
, min_stat
, gensec_gssapi_state
->gss_oid
)));
659 /* These last two stages are only done if we were invoked as SASL */
660 case STAGE_SASL_SSF_NEG
:
662 switch (gensec_security
->gensec_role
) {
665 uint8_t maxlength_proposed
[4];
666 uint8_t maxlength_accepted
[4];
667 uint8_t security_supported
;
670 input_token
.length
= in
.length
;
671 input_token
.value
= in
.data
;
673 /* As a client, we have just send a
674 * zero-length blob to the server (after the
675 * normal GSSAPI exchange), and it has replied
676 * with it's SASL negotiation */
678 maj_stat
= gss_unwrap(&min_stat
,
679 gensec_gssapi_state
->gssapi_context
,
684 if (GSS_ERROR(maj_stat
)) {
685 DEBUG(1, ("gensec_gssapi_update: GSS UnWrap of SASL protection negotiation failed: %s\n",
686 gssapi_error_string(out_mem_ctx
, maj_stat
, min_stat
, gensec_gssapi_state
->gss_oid
)));
687 return NT_STATUS_ACCESS_DENIED
;
690 if (output_token
.length
< 4) {
691 return NT_STATUS_INVALID_PARAMETER
;
694 memcpy(maxlength_proposed
, output_token
.value
, 4);
695 gss_release_buffer(&min_stat
, &output_token
);
697 /* first byte is the proposed security */
698 security_supported
= maxlength_proposed
[0];
699 maxlength_proposed
[0] = '\0';
701 /* Rest is the proposed max wrap length */
702 gensec_gssapi_state
->max_wrap_buf_size
= MIN(RIVAL(maxlength_proposed
, 0),
703 gensec_gssapi_state
->max_wrap_buf_size
);
704 gensec_gssapi_state
->sasl_protection
= 0;
705 if (security_supported
& NEG_SEAL
) {
706 if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
)) {
707 gensec_gssapi_state
->sasl_protection
|= NEG_SEAL
;
710 if (security_supported
& NEG_SIGN
) {
711 if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SIGN
)) {
712 gensec_gssapi_state
->sasl_protection
|= NEG_SIGN
;
715 if (security_supported
& NEG_NONE
) {
716 gensec_gssapi_state
->sasl_protection
|= NEG_NONE
;
718 if (gensec_gssapi_state
->sasl_protection
== 0) {
719 DEBUG(1, ("Remote server does not support unprotected connections\n"));
720 return NT_STATUS_ACCESS_DENIED
;
723 /* Send back the negotiated max length */
725 RSIVAL(maxlength_accepted
, 0, gensec_gssapi_state
->max_wrap_buf_size
);
727 maxlength_accepted
[0] = gensec_gssapi_state
->sasl_protection
;
729 input_token
.value
= maxlength_accepted
;
730 input_token
.length
= sizeof(maxlength_accepted
);
732 maj_stat
= gss_wrap(&min_stat
,
733 gensec_gssapi_state
->gssapi_context
,
739 if (GSS_ERROR(maj_stat
)) {
740 DEBUG(1, ("GSS Update(SSF_NEG): GSS Wrap failed: %s\n",
741 gssapi_error_string(out_mem_ctx
, maj_stat
, min_stat
, gensec_gssapi_state
->gss_oid
)));
742 return NT_STATUS_ACCESS_DENIED
;
745 *out
= data_blob_talloc(out_mem_ctx
, output_token
.value
, output_token
.length
);
746 gss_release_buffer(&min_stat
, &output_token
);
748 /* quirk: This changes the value that gensec_have_feature returns, to be that after SASL negotiation */
749 gensec_gssapi_state
->sasl_state
= STAGE_DONE
;
751 if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
)) {
752 DEBUG(3, ("SASL/GSSAPI Connection to server will be cryptographically sealed\n"));
753 } else if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SIGN
)) {
754 DEBUG(3, ("SASL/GSSAPI Connection to server will be cryptographically signed\n"));
756 DEBUG(3, ("SASL/GSSAPI Connection to server will have no cryptographically protection\n"));
763 uint8_t maxlength_proposed
[4];
764 uint8_t security_supported
= 0x0;
767 /* As a server, we have just been sent a zero-length blob (note this, but it isn't fatal) */
768 if (in
.length
!= 0) {
769 DEBUG(1, ("SASL/GSSAPI: client sent non-zero length starting SASL negotiation!\n"));
772 /* Give the client some idea what we will support */
774 RSIVAL(maxlength_proposed
, 0, gensec_gssapi_state
->max_wrap_buf_size
);
775 /* first byte is the proposed security */
776 maxlength_proposed
[0] = '\0';
778 gensec_gssapi_state
->sasl_protection
= 0;
779 if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
)) {
780 security_supported
|= NEG_SEAL
;
782 if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SIGN
)) {
783 security_supported
|= NEG_SIGN
;
785 if (security_supported
== 0) {
786 /* If we don't support anything, this must be 0 */
787 RSIVAL(maxlength_proposed
, 0, 0x0);
790 /* TODO: We may not wish to support this */
791 security_supported
|= NEG_NONE
;
792 maxlength_proposed
[0] = security_supported
;
794 input_token
.value
= maxlength_proposed
;
795 input_token
.length
= sizeof(maxlength_proposed
);
797 maj_stat
= gss_wrap(&min_stat
,
798 gensec_gssapi_state
->gssapi_context
,
804 if (GSS_ERROR(maj_stat
)) {
805 DEBUG(1, ("GSS Update(SSF_NEG): GSS Wrap failed: %s\n",
806 gssapi_error_string(out_mem_ctx
, maj_stat
, min_stat
, gensec_gssapi_state
->gss_oid
)));
807 return NT_STATUS_ACCESS_DENIED
;
810 *out
= data_blob_talloc(out_mem_ctx
, output_token
.value
, output_token
.length
);
811 gss_release_buffer(&min_stat
, &output_token
);
813 gensec_gssapi_state
->sasl_state
= STAGE_SASL_SSF_ACCEPT
;
814 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
817 return NT_STATUS_INVALID_PARAMETER
;
821 /* This is s server-only stage */
822 case STAGE_SASL_SSF_ACCEPT
:
824 uint8_t maxlength_accepted
[4];
825 uint8_t security_accepted
;
828 input_token
.length
= in
.length
;
829 input_token
.value
= in
.data
;
831 maj_stat
= gss_unwrap(&min_stat
,
832 gensec_gssapi_state
->gssapi_context
,
837 if (GSS_ERROR(maj_stat
)) {
838 DEBUG(1, ("gensec_gssapi_update: GSS UnWrap of SASL protection negotiation failed: %s\n",
839 gssapi_error_string(out_mem_ctx
, maj_stat
, min_stat
, gensec_gssapi_state
->gss_oid
)));
840 return NT_STATUS_ACCESS_DENIED
;
843 if (output_token
.length
< 4) {
844 return NT_STATUS_INVALID_PARAMETER
;
847 memcpy(maxlength_accepted
, output_token
.value
, 4);
848 gss_release_buffer(&min_stat
, &output_token
);
850 /* first byte is the proposed security */
851 security_accepted
= maxlength_accepted
[0];
852 maxlength_accepted
[0] = '\0';
854 /* Rest is the proposed max wrap length */
855 gensec_gssapi_state
->max_wrap_buf_size
= MIN(RIVAL(maxlength_accepted
, 0),
856 gensec_gssapi_state
->max_wrap_buf_size
);
858 gensec_gssapi_state
->sasl_protection
= 0;
859 if (security_accepted
& NEG_SEAL
) {
860 if (!gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
)) {
861 DEBUG(1, ("Remote client wanted seal, but gensec refused\n"));
862 return NT_STATUS_ACCESS_DENIED
;
864 gensec_gssapi_state
->sasl_protection
|= NEG_SEAL
;
866 if (security_accepted
& NEG_SIGN
) {
867 if (!gensec_have_feature(gensec_security
, GENSEC_FEATURE_SIGN
)) {
868 DEBUG(1, ("Remote client wanted sign, but gensec refused\n"));
869 return NT_STATUS_ACCESS_DENIED
;
871 gensec_gssapi_state
->sasl_protection
|= NEG_SIGN
;
873 if (security_accepted
& NEG_NONE
) {
874 gensec_gssapi_state
->sasl_protection
|= NEG_NONE
;
877 /* quirk: This changes the value that gensec_have_feature returns, to be that after SASL negotiation */
878 gensec_gssapi_state
->sasl_state
= STAGE_DONE
;
879 if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
)) {
880 DEBUG(5, ("SASL/GSSAPI Connection from client will be cryptographically sealed\n"));
881 } else if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SIGN
)) {
882 DEBUG(5, ("SASL/GSSAPI Connection from client will be cryptographically signed\n"));
884 DEBUG(5, ("SASL/GSSAPI Connection from client will have no cryptographic protection\n"));
887 *out
= data_blob(NULL
, 0);
891 return NT_STATUS_INVALID_PARAMETER
;
895 static NTSTATUS
gensec_gssapi_wrap(struct gensec_security
*gensec_security
,
900 struct gensec_gssapi_state
*gensec_gssapi_state
901 = talloc_get_type(gensec_security
->private_data
, struct gensec_gssapi_state
);
902 OM_uint32 maj_stat
, min_stat
;
903 gss_buffer_desc input_token
, output_token
;
905 input_token
.length
= in
->length
;
906 input_token
.value
= in
->data
;
908 maj_stat
= gss_wrap(&min_stat
,
909 gensec_gssapi_state
->gssapi_context
,
910 gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
),
915 if (GSS_ERROR(maj_stat
)) {
916 DEBUG(1, ("gensec_gssapi_wrap: GSS Wrap failed: %s\n",
917 gssapi_error_string(mem_ctx
, maj_stat
, min_stat
, gensec_gssapi_state
->gss_oid
)));
918 return NT_STATUS_ACCESS_DENIED
;
921 *out
= data_blob_talloc(mem_ctx
, output_token
.value
, output_token
.length
);
922 gss_release_buffer(&min_stat
, &output_token
);
924 if (gensec_gssapi_state
->sasl
) {
925 size_t max_wrapped_size
= gensec_gssapi_max_wrapped_size(gensec_security
);
926 if (max_wrapped_size
< out
->length
) {
927 DEBUG(1, ("gensec_gssapi_wrap: when wrapped, INPUT data (%u) is grew to be larger than SASL negotiated maximum output size (%u > %u)\n",
928 (unsigned)in
->length
,
929 (unsigned)out
->length
,
930 (unsigned int)max_wrapped_size
));
931 return NT_STATUS_INVALID_PARAMETER
;
935 if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
)
937 return NT_STATUS_ACCESS_DENIED
;
942 static NTSTATUS
gensec_gssapi_unwrap(struct gensec_security
*gensec_security
,
947 struct gensec_gssapi_state
*gensec_gssapi_state
948 = talloc_get_type(gensec_security
->private_data
, struct gensec_gssapi_state
);
949 OM_uint32 maj_stat
, min_stat
;
950 gss_buffer_desc input_token
, output_token
;
953 input_token
.length
= in
->length
;
954 input_token
.value
= in
->data
;
956 if (gensec_gssapi_state
->sasl
) {
957 size_t max_wrapped_size
= gensec_gssapi_max_wrapped_size(gensec_security
);
958 if (max_wrapped_size
< in
->length
) {
959 DEBUG(1, ("gensec_gssapi_unwrap: WRAPPED data is larger than SASL negotiated maximum size\n"));
960 return NT_STATUS_INVALID_PARAMETER
;
964 maj_stat
= gss_unwrap(&min_stat
,
965 gensec_gssapi_state
->gssapi_context
,
970 if (GSS_ERROR(maj_stat
)) {
971 DEBUG(1, ("gensec_gssapi_unwrap: GSS UnWrap failed: %s\n",
972 gssapi_error_string(mem_ctx
, maj_stat
, min_stat
, gensec_gssapi_state
->gss_oid
)));
973 return NT_STATUS_ACCESS_DENIED
;
976 *out
= data_blob_talloc(mem_ctx
, output_token
.value
, output_token
.length
);
977 gss_release_buffer(&min_stat
, &output_token
);
979 if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
)
981 return NT_STATUS_ACCESS_DENIED
;
986 /* Find out the maximum input size negotiated on this connection */
988 static size_t gensec_gssapi_max_input_size(struct gensec_security
*gensec_security
)
990 struct gensec_gssapi_state
*gensec_gssapi_state
991 = talloc_get_type(gensec_security
->private_data
, struct gensec_gssapi_state
);
992 OM_uint32 maj_stat
, min_stat
;
993 OM_uint32 max_input_size
;
995 maj_stat
= gss_wrap_size_limit(&min_stat
,
996 gensec_gssapi_state
->gssapi_context
,
997 gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
),
999 gensec_gssapi_state
->max_wrap_buf_size
,
1001 if (GSS_ERROR(maj_stat
)) {
1002 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
1003 DEBUG(1, ("gensec_gssapi_max_input_size: determining signature size with gss_wrap_size_limit failed: %s\n",
1004 gssapi_error_string(mem_ctx
, maj_stat
, min_stat
, gensec_gssapi_state
->gss_oid
)));
1005 talloc_free(mem_ctx
);
1009 return max_input_size
;
1012 /* Find out the maximum output size negotiated on this connection */
1013 static size_t gensec_gssapi_max_wrapped_size(struct gensec_security
*gensec_security
)
1015 struct gensec_gssapi_state
*gensec_gssapi_state
= talloc_get_type(gensec_security
->private_data
, struct gensec_gssapi_state
);;
1016 return gensec_gssapi_state
->max_wrap_buf_size
;
1019 static NTSTATUS
gensec_gssapi_seal_packet(struct gensec_security
*gensec_security
,
1020 TALLOC_CTX
*mem_ctx
,
1021 uint8_t *data
, size_t length
,
1022 const uint8_t *whole_pdu
, size_t pdu_length
,
1025 struct gensec_gssapi_state
*gensec_gssapi_state
1026 = talloc_get_type(gensec_security
->private_data
, struct gensec_gssapi_state
);
1027 OM_uint32 maj_stat
, min_stat
;
1028 gss_buffer_desc input_token
, output_token
;
1032 if (gensec_security
->want_features
& GENSEC_FEATURE_SIGN_PKT_HEADER
) {
1033 DEBUG(1, ("gensec_gssapi_seal_packet: "
1034 "GENSEC_FEATURE_SIGN_PKT_HEADER not supported\n"));
1035 return NT_STATUS_ACCESS_DENIED
;
1038 input_token
.length
= length
;
1039 input_token
.value
= data
;
1041 maj_stat
= gss_wrap(&min_stat
,
1042 gensec_gssapi_state
->gssapi_context
,
1043 gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
),
1048 if (GSS_ERROR(maj_stat
)) {
1049 DEBUG(1, ("gensec_gssapi_seal_packet: GSS Wrap failed: %s\n",
1050 gssapi_error_string(mem_ctx
, maj_stat
, min_stat
, gensec_gssapi_state
->gss_oid
)));
1051 return NT_STATUS_ACCESS_DENIED
;
1054 if (output_token
.length
< input_token
.length
) {
1055 DEBUG(1, ("gensec_gssapi_seal_packet: GSS Wrap length [%ld] *less* than caller length [%ld]\n",
1056 (long)output_token
.length
, (long)length
));
1057 return NT_STATUS_INTERNAL_ERROR
;
1059 sig_length
= output_token
.length
- input_token
.length
;
1061 memcpy(data
, ((uint8_t *)output_token
.value
) + sig_length
, length
);
1062 *sig
= data_blob_talloc(mem_ctx
, (uint8_t *)output_token
.value
, sig_length
);
1064 dump_data_pw("gensec_gssapi_seal_packet: sig\n", sig
->data
, sig
->length
);
1065 dump_data_pw("gensec_gssapi_seal_packet: clear\n", data
, length
);
1066 dump_data_pw("gensec_gssapi_seal_packet: sealed\n", ((uint8_t *)output_token
.value
) + sig_length
, output_token
.length
- sig_length
);
1068 gss_release_buffer(&min_stat
, &output_token
);
1070 if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
)
1072 return NT_STATUS_ACCESS_DENIED
;
1074 return NT_STATUS_OK
;
1077 static NTSTATUS
gensec_gssapi_unseal_packet(struct gensec_security
*gensec_security
,
1078 uint8_t *data
, size_t length
,
1079 const uint8_t *whole_pdu
, size_t pdu_length
,
1080 const DATA_BLOB
*sig
)
1082 struct gensec_gssapi_state
*gensec_gssapi_state
1083 = talloc_get_type(gensec_security
->private_data
, struct gensec_gssapi_state
);
1084 OM_uint32 maj_stat
, min_stat
;
1085 gss_buffer_desc input_token
, output_token
;
1087 gss_qop_t qop_state
;
1090 dump_data_pw("gensec_gssapi_unseal_packet: sig\n", sig
->data
, sig
->length
);
1092 if (gensec_security
->want_features
& GENSEC_FEATURE_SIGN_PKT_HEADER
) {
1093 DEBUG(1, ("gensec_gssapi_unseal_packet: "
1094 "GENSEC_FEATURE_SIGN_PKT_HEADER not supported\n"));
1095 return NT_STATUS_ACCESS_DENIED
;
1098 in
= data_blob_talloc(gensec_security
, NULL
, sig
->length
+ length
);
1100 memcpy(in
.data
, sig
->data
, sig
->length
);
1101 memcpy(in
.data
+ sig
->length
, data
, length
);
1103 input_token
.length
= in
.length
;
1104 input_token
.value
= in
.data
;
1106 maj_stat
= gss_unwrap(&min_stat
,
1107 gensec_gssapi_state
->gssapi_context
,
1112 talloc_free(in
.data
);
1113 if (GSS_ERROR(maj_stat
)) {
1114 char *error_string
= gssapi_error_string(NULL
, maj_stat
, min_stat
, gensec_gssapi_state
->gss_oid
);
1115 DEBUG(1, ("gensec_gssapi_unseal_packet: GSS UnWrap failed: %s\n",
1117 talloc_free(error_string
);
1118 return NT_STATUS_ACCESS_DENIED
;
1121 if (output_token
.length
!= length
) {
1122 return NT_STATUS_INTERNAL_ERROR
;
1125 memcpy(data
, output_token
.value
, length
);
1127 gss_release_buffer(&min_stat
, &output_token
);
1129 if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
)
1131 return NT_STATUS_ACCESS_DENIED
;
1133 return NT_STATUS_OK
;
1136 static NTSTATUS
gensec_gssapi_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 gensec_gssapi_state
*gensec_gssapi_state
1143 = talloc_get_type(gensec_security
->private_data
, struct gensec_gssapi_state
);
1144 OM_uint32 maj_stat
, min_stat
;
1145 gss_buffer_desc input_token
, output_token
;
1147 if (gensec_security
->want_features
& GENSEC_FEATURE_SIGN_PKT_HEADER
) {
1148 input_token
.length
= pdu_length
;
1149 input_token
.value
= discard_const_p(uint8_t *, whole_pdu
);
1151 input_token
.length
= length
;
1152 input_token
.value
= discard_const_p(uint8_t *, data
);
1155 maj_stat
= gss_get_mic(&min_stat
,
1156 gensec_gssapi_state
->gssapi_context
,
1160 if (GSS_ERROR(maj_stat
)) {
1161 DEBUG(1, ("GSS GetMic failed: %s\n",
1162 gssapi_error_string(mem_ctx
, maj_stat
, min_stat
, gensec_gssapi_state
->gss_oid
)));
1163 return NT_STATUS_ACCESS_DENIED
;
1166 *sig
= data_blob_talloc(mem_ctx
, (uint8_t *)output_token
.value
, output_token
.length
);
1168 dump_data_pw("gensec_gssapi_sign_packet: sig\n", sig
->data
, sig
->length
);
1170 gss_release_buffer(&min_stat
, &output_token
);
1172 return NT_STATUS_OK
;
1175 static NTSTATUS
gensec_gssapi_check_packet(struct gensec_security
*gensec_security
,
1176 const uint8_t *data
, size_t length
,
1177 const uint8_t *whole_pdu
, size_t pdu_length
,
1178 const DATA_BLOB
*sig
)
1180 struct gensec_gssapi_state
*gensec_gssapi_state
1181 = talloc_get_type(gensec_security
->private_data
, struct gensec_gssapi_state
);
1182 OM_uint32 maj_stat
, min_stat
;
1183 gss_buffer_desc input_token
;
1184 gss_buffer_desc input_message
;
1185 gss_qop_t qop_state
;
1187 dump_data_pw("gensec_gssapi_check_packet: sig\n", sig
->data
, sig
->length
);
1189 if (gensec_security
->want_features
& GENSEC_FEATURE_SIGN_PKT_HEADER
) {
1190 input_message
.length
= pdu_length
;
1191 input_message
.value
= discard_const(whole_pdu
);
1193 input_message
.length
= length
;
1194 input_message
.value
= discard_const(data
);
1197 input_token
.length
= sig
->length
;
1198 input_token
.value
= sig
->data
;
1200 maj_stat
= gss_verify_mic(&min_stat
,
1201 gensec_gssapi_state
->gssapi_context
,
1205 if (GSS_ERROR(maj_stat
)) {
1206 char *error_string
= gssapi_error_string(NULL
, maj_stat
, min_stat
, gensec_gssapi_state
->gss_oid
);
1207 DEBUG(1, ("GSS VerifyMic failed: %s\n", error_string
));
1208 talloc_free(error_string
);
1210 return NT_STATUS_ACCESS_DENIED
;
1213 return NT_STATUS_OK
;
1216 /* Try to figure out what features we actually got on the connection */
1217 static bool gensec_gssapi_have_feature(struct gensec_security
*gensec_security
,
1220 struct gensec_gssapi_state
*gensec_gssapi_state
1221 = talloc_get_type(gensec_security
->private_data
, struct gensec_gssapi_state
);
1222 if (feature
& GENSEC_FEATURE_SIGN
) {
1223 /* If we are going GSSAPI SASL, then we honour the second negotiation */
1224 if (gensec_gssapi_state
->sasl
1225 && gensec_gssapi_state
->sasl_state
== STAGE_DONE
) {
1226 return ((gensec_gssapi_state
->sasl_protection
& NEG_SIGN
)
1227 && (gensec_gssapi_state
->gss_got_flags
& GSS_C_INTEG_FLAG
));
1229 return gensec_gssapi_state
->gss_got_flags
& GSS_C_INTEG_FLAG
;
1231 if (feature
& GENSEC_FEATURE_SEAL
) {
1232 /* If we are going GSSAPI SASL, then we honour the second negotiation */
1233 if (gensec_gssapi_state
->sasl
1234 && gensec_gssapi_state
->sasl_state
== STAGE_DONE
) {
1235 return ((gensec_gssapi_state
->sasl_protection
& NEG_SEAL
)
1236 && (gensec_gssapi_state
->gss_got_flags
& GSS_C_CONF_FLAG
));
1238 return gensec_gssapi_state
->gss_got_flags
& GSS_C_CONF_FLAG
;
1240 if (feature
& GENSEC_FEATURE_SESSION_KEY
) {
1241 /* Only for GSSAPI/Krb5 */
1242 if (smb_gss_oid_equal(gensec_gssapi_state
->gss_oid
,
1247 if (feature
& GENSEC_FEATURE_DCE_STYLE
) {
1248 return gensec_gssapi_state
->gss_got_flags
& GSS_C_DCE_STYLE
;
1250 if (feature
& GENSEC_FEATURE_NEW_SPNEGO
) {
1254 if (!(gensec_gssapi_state
->gss_got_flags
& GSS_C_INTEG_FLAG
)) {
1258 if (gensec_setting_bool(gensec_security
->settings
, "gensec_gssapi", "force_new_spnego", false)) {
1261 if (gensec_setting_bool(gensec_security
->settings
, "gensec_gssapi", "disable_new_spnego", false)) {
1265 status
= gssapi_get_session_key(gensec_gssapi_state
,
1266 gensec_gssapi_state
->gssapi_context
, NULL
, &keytype
);
1268 * We should do a proper sig on the mechListMic unless
1269 * we know we have to be backwards compatible with
1270 * earlier windows versions.
1272 * Negotiating a non-krb5
1273 * mech for example should be regarded as having
1276 if (NT_STATUS_IS_OK(status
)) {
1278 case ENCTYPE_DES_CBC_CRC
:
1279 case ENCTYPE_DES_CBC_MD5
:
1280 case ENCTYPE_ARCFOUR_HMAC
:
1281 case ENCTYPE_DES3_CBC_SHA1
:
1287 /* We can always do async (rather than strict request/reply) packets. */
1288 if (feature
& GENSEC_FEATURE_ASYNC_REPLIES
) {
1291 if (feature
& GENSEC_FEATURE_SIGN_PKT_HEADER
) {
1292 if (gensec_security
->want_features
& GENSEC_FEATURE_SEAL
) {
1293 /* TODO: implement this using gss_wrap_iov() */
1297 if (gensec_security
->want_features
& GENSEC_FEATURE_SIGN
) {
1306 static NTTIME
gensec_gssapi_expire_time(struct gensec_security
*gensec_security
)
1308 struct gensec_gssapi_state
*gensec_gssapi_state
=
1309 talloc_get_type_abort(gensec_security
->private_data
,
1310 struct gensec_gssapi_state
);
1312 return gensec_gssapi_state
->expire_time
;
1316 * Extract the 'sesssion key' needed by SMB signing and ncacn_np
1317 * (for encrypting some passwords).
1319 * This breaks all the abstractions, but what do you expect...
1321 static NTSTATUS
gensec_gssapi_session_key(struct gensec_security
*gensec_security
,
1322 TALLOC_CTX
*mem_ctx
,
1323 DATA_BLOB
*session_key
)
1325 struct gensec_gssapi_state
*gensec_gssapi_state
1326 = talloc_get_type(gensec_security
->private_data
, struct gensec_gssapi_state
);
1327 return gssapi_get_session_key(mem_ctx
, gensec_gssapi_state
->gssapi_context
, session_key
, NULL
);
1330 /* Get some basic (and authorization) information about the user on
1331 * this session. This uses either the PAC (if present) or a local
1332 * database lookup */
1333 static NTSTATUS
gensec_gssapi_session_info(struct gensec_security
*gensec_security
,
1334 TALLOC_CTX
*mem_ctx
,
1335 struct auth_session_info
**_session_info
)
1338 TALLOC_CTX
*tmp_ctx
;
1339 struct gensec_gssapi_state
*gensec_gssapi_state
1340 = talloc_get_type(gensec_security
->private_data
, struct gensec_gssapi_state
);
1341 struct auth_session_info
*session_info
= NULL
;
1342 OM_uint32 maj_stat
, min_stat
;
1343 DATA_BLOB pac_blob
, *pac_blob_ptr
= NULL
;
1345 gss_buffer_desc name_token
;
1346 char *principal_string
;
1348 tmp_ctx
= talloc_named(mem_ctx
, 0, "gensec_gssapi_session_info context");
1349 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
1351 maj_stat
= gss_display_name (&min_stat
,
1352 gensec_gssapi_state
->client_name
,
1355 if (GSS_ERROR(maj_stat
)) {
1356 DEBUG(1, ("GSS display_name failed: %s\n",
1357 gssapi_error_string(tmp_ctx
, maj_stat
, min_stat
, gensec_gssapi_state
->gss_oid
)));
1358 talloc_free(tmp_ctx
);
1359 return NT_STATUS_FOOBAR
;
1362 principal_string
= talloc_strndup(tmp_ctx
,
1363 (const char *)name_token
.value
,
1366 gss_release_buffer(&min_stat
, &name_token
);
1368 if (!principal_string
) {
1369 talloc_free(tmp_ctx
);
1370 return NT_STATUS_NO_MEMORY
;
1373 nt_status
= gssapi_obtain_pac_blob(tmp_ctx
, gensec_gssapi_state
->gssapi_context
,
1374 gensec_gssapi_state
->client_name
,
1377 /* IF we have the PAC - otherwise we need to get this
1378 * data from elsewere - local ldb, or (TODO) lookup of some
1381 if (NT_STATUS_IS_OK(nt_status
)) {
1382 pac_blob_ptr
= &pac_blob
;
1384 nt_status
= gensec_generate_session_info_pac(tmp_ctx
,
1386 gensec_gssapi_state
->smb_krb5_context
,
1387 pac_blob_ptr
, principal_string
,
1388 gensec_get_remote_address(gensec_security
),
1390 if (!NT_STATUS_IS_OK(nt_status
)) {
1391 talloc_free(tmp_ctx
);
1395 nt_status
= gensec_gssapi_session_key(gensec_security
, session_info
, &session_info
->session_key
);
1396 if (!NT_STATUS_IS_OK(nt_status
)) {
1397 talloc_free(tmp_ctx
);
1401 if (!(gensec_gssapi_state
->gss_got_flags
& GSS_C_DELEG_FLAG
)) {
1402 DEBUG(10, ("gensec_gssapi: NO delegated credentials supplied by client\n"));
1404 krb5_error_code ret
;
1405 const char *error_string
;
1407 DEBUG(10, ("gensec_gssapi: delegated credentials supplied by client\n"));
1408 session_info
->credentials
= cli_credentials_init(session_info
);
1409 if (!session_info
->credentials
) {
1410 talloc_free(tmp_ctx
);
1411 return NT_STATUS_NO_MEMORY
;
1414 cli_credentials_set_conf(session_info
->credentials
, gensec_security
->settings
->lp_ctx
);
1415 /* Just so we don't segfault trying to get at a username */
1416 cli_credentials_set_anonymous(session_info
->credentials
);
1418 ret
= cli_credentials_set_client_gss_creds(session_info
->credentials
,
1419 gensec_security
->settings
->lp_ctx
,
1420 gensec_gssapi_state
->delegated_cred_handle
,
1421 CRED_SPECIFIED
, &error_string
);
1423 talloc_free(tmp_ctx
);
1424 DEBUG(2,("Failed to get gss creds: %s\n", error_string
));
1425 return NT_STATUS_NO_MEMORY
;
1428 /* This credential handle isn't useful for password authentication, so ensure nobody tries to do that */
1429 cli_credentials_set_kerberos_state(session_info
->credentials
, CRED_MUST_USE_KERBEROS
);
1431 /* It has been taken from this place... */
1432 gensec_gssapi_state
->delegated_cred_handle
= GSS_C_NO_CREDENTIAL
;
1434 *_session_info
= talloc_steal(mem_ctx
, session_info
);
1435 talloc_free(tmp_ctx
);
1437 return NT_STATUS_OK
;
1440 static size_t gensec_gssapi_sig_size(struct gensec_security
*gensec_security
, size_t data_size
)
1442 struct gensec_gssapi_state
*gensec_gssapi_state
1443 = talloc_get_type(gensec_security
->private_data
, struct gensec_gssapi_state
);
1446 if (gensec_gssapi_state
->sig_size
) {
1447 return gensec_gssapi_state
->sig_size
;
1450 if (gensec_gssapi_state
->gss_got_flags
& GSS_C_CONF_FLAG
) {
1451 gensec_gssapi_state
->sig_size
= 45;
1453 gensec_gssapi_state
->sig_size
= 37;
1456 status
= gensec_gssapi_init_lucid(gensec_gssapi_state
);
1457 if (!NT_STATUS_IS_OK(status
)) {
1458 return gensec_gssapi_state
->sig_size
;
1461 if (gensec_gssapi_state
->lucid
->protocol
== 1) {
1462 if (gensec_gssapi_state
->gss_got_flags
& GSS_C_CONF_FLAG
) {
1464 * TODO: windows uses 76 here, but we don't know
1465 * gss_wrap works with aes keys yet
1467 gensec_gssapi_state
->sig_size
= 76;
1469 gensec_gssapi_state
->sig_size
= 28;
1471 } else if (gensec_gssapi_state
->lucid
->protocol
== 0) {
1472 switch (gensec_gssapi_state
->lucid
->rfc1964_kd
.ctx_key
.type
) {
1473 case ENCTYPE_DES_CBC_CRC
:
1474 case ENCTYPE_ARCFOUR_HMAC
:
1475 case ENCTYPE_ARCFOUR_HMAC_EXP
:
1476 if (gensec_gssapi_state
->gss_got_flags
& GSS_C_CONF_FLAG
) {
1477 gensec_gssapi_state
->sig_size
= 45;
1479 gensec_gssapi_state
->sig_size
= 37;
1482 #ifdef SAMBA4_USES_HEIMDAL
1483 case ENCTYPE_OLD_DES3_CBC_SHA1
:
1484 if (gensec_gssapi_state
->gss_got_flags
& GSS_C_CONF_FLAG
) {
1485 gensec_gssapi_state
->sig_size
= 57;
1487 gensec_gssapi_state
->sig_size
= 49;
1494 return gensec_gssapi_state
->sig_size
;
1497 static const char *gensec_gssapi_krb5_oids
[] = {
1498 GENSEC_OID_KERBEROS5_OLD
,
1499 GENSEC_OID_KERBEROS5
,
1503 static const char *gensec_gssapi_spnego_oids
[] = {
1508 /* As a server, this could in theory accept any GSSAPI mech */
1509 static const struct gensec_security_ops gensec_gssapi_spnego_security_ops
= {
1510 .name
= "gssapi_spnego",
1511 .sasl_name
= "GSS-SPNEGO",
1512 .auth_type
= DCERPC_AUTH_TYPE_SPNEGO
,
1513 .oid
= gensec_gssapi_spnego_oids
,
1514 .client_start
= gensec_gssapi_client_start
,
1515 .server_start
= gensec_gssapi_server_start
,
1516 .magic
= gensec_magic_check_krb5_oid
,
1517 .update
= gensec_gssapi_update
,
1518 .session_key
= gensec_gssapi_session_key
,
1519 .session_info
= gensec_gssapi_session_info
,
1520 .sign_packet
= gensec_gssapi_sign_packet
,
1521 .check_packet
= gensec_gssapi_check_packet
,
1522 .seal_packet
= gensec_gssapi_seal_packet
,
1523 .unseal_packet
= gensec_gssapi_unseal_packet
,
1524 .wrap
= gensec_gssapi_wrap
,
1525 .unwrap
= gensec_gssapi_unwrap
,
1526 .have_feature
= gensec_gssapi_have_feature
,
1527 .expire_time
= gensec_gssapi_expire_time
,
1530 .priority
= GENSEC_GSSAPI
1533 /* As a server, this could in theory accept any GSSAPI mech */
1534 static const struct gensec_security_ops gensec_gssapi_krb5_security_ops
= {
1535 .name
= "gssapi_krb5",
1536 .auth_type
= DCERPC_AUTH_TYPE_KRB5
,
1537 .oid
= gensec_gssapi_krb5_oids
,
1538 .client_start
= gensec_gssapi_client_start
,
1539 .server_start
= gensec_gssapi_server_start
,
1540 .magic
= gensec_magic_check_krb5_oid
,
1541 .update
= gensec_gssapi_update
,
1542 .session_key
= gensec_gssapi_session_key
,
1543 .session_info
= gensec_gssapi_session_info
,
1544 .sig_size
= gensec_gssapi_sig_size
,
1545 .sign_packet
= gensec_gssapi_sign_packet
,
1546 .check_packet
= gensec_gssapi_check_packet
,
1547 .seal_packet
= gensec_gssapi_seal_packet
,
1548 .unseal_packet
= gensec_gssapi_unseal_packet
,
1549 .wrap
= gensec_gssapi_wrap
,
1550 .unwrap
= gensec_gssapi_unwrap
,
1551 .have_feature
= gensec_gssapi_have_feature
,
1552 .expire_time
= gensec_gssapi_expire_time
,
1555 .priority
= GENSEC_GSSAPI
1558 /* As a server, this could in theory accept any GSSAPI mech */
1559 static const struct gensec_security_ops gensec_gssapi_sasl_krb5_security_ops
= {
1560 .name
= "gssapi_krb5_sasl",
1561 .sasl_name
= "GSSAPI",
1562 .client_start
= gensec_gssapi_sasl_client_start
,
1563 .server_start
= gensec_gssapi_sasl_server_start
,
1564 .update
= gensec_gssapi_update
,
1565 .session_key
= gensec_gssapi_session_key
,
1566 .session_info
= gensec_gssapi_session_info
,
1567 .max_input_size
= gensec_gssapi_max_input_size
,
1568 .max_wrapped_size
= gensec_gssapi_max_wrapped_size
,
1569 .wrap
= gensec_gssapi_wrap
,
1570 .unwrap
= gensec_gssapi_unwrap
,
1571 .have_feature
= gensec_gssapi_have_feature
,
1572 .expire_time
= gensec_gssapi_expire_time
,
1575 .priority
= GENSEC_GSSAPI
1578 _PUBLIC_ NTSTATUS
gensec_gssapi_init(void)
1582 ret
= gensec_register(&gensec_gssapi_spnego_security_ops
);
1583 if (!NT_STATUS_IS_OK(ret
)) {
1584 DEBUG(0,("Failed to register '%s' gensec backend!\n",
1585 gensec_gssapi_spnego_security_ops
.name
));
1589 ret
= gensec_register(&gensec_gssapi_krb5_security_ops
);
1590 if (!NT_STATUS_IS_OK(ret
)) {
1591 DEBUG(0,("Failed to register '%s' gensec backend!\n",
1592 gensec_gssapi_krb5_security_ops
.name
));
1596 ret
= gensec_register(&gensec_gssapi_sasl_krb5_security_ops
);
1597 if (!NT_STATUS_IS_OK(ret
)) {
1598 DEBUG(0,("Failed to register '%s' gensec backend!\n",
1599 gensec_gssapi_sasl_krb5_security_ops
.name
));