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 "libads/kerberos_proto.h"
27 #include "auth/common_auth.h"
28 #include "auth/gensec/gensec.h"
29 #include "auth/credentials/credentials.h"
30 #include "../librpc/gen_ndr/dcerpc.h"
32 #if defined(HAVE_KRB5)
34 #include "auth/kerberos/pac_utils.h"
37 static char *gse_errstr(TALLOC_CTX
*mem_ctx
, OM_uint32 maj
, OM_uint32 min
);
40 gss_ctx_id_t gssapi_context
;
41 gss_name_t server_name
;
42 gss_name_t client_name
;
43 OM_uint32 gss_want_flags
, gss_got_flags
;
45 gss_cred_id_t delegated_cred_handle
;
54 gss_OID_desc gss_mech
;
60 /* free non talloc dependent contexts */
61 static int gse_context_destructor(void *ptr
)
63 struct gse_context
*gse_ctx
;
66 gse_ctx
= talloc_get_type_abort(ptr
, struct gse_context
);
68 if (gse_ctx
->ccache
) {
69 krb5_cc_close(gse_ctx
->k5ctx
, gse_ctx
->ccache
);
70 gse_ctx
->ccache
= NULL
;
72 if (gse_ctx
->keytab
) {
73 krb5_kt_close(gse_ctx
->k5ctx
, gse_ctx
->keytab
);
74 gse_ctx
->keytab
= NULL
;
76 krb5_free_context(gse_ctx
->k5ctx
);
77 gse_ctx
->k5ctx
= NULL
;
79 if (gse_ctx
->gssapi_context
!= GSS_C_NO_CONTEXT
) {
80 (void)gss_delete_sec_context(&gss_min
,
81 &gse_ctx
->gssapi_context
,
84 if (gse_ctx
->server_name
) {
85 (void)gss_release_name(&gss_min
,
86 &gse_ctx
->server_name
);
88 if (gse_ctx
->client_name
) {
89 (void)gss_release_name(&gss_min
,
90 &gse_ctx
->client_name
);
93 (void)gss_release_cred(&gss_min
,
96 if (gse_ctx
->delegated_cred_handle
) {
97 (void)gss_release_cred(&gss_min
,
98 &gse_ctx
->delegated_cred_handle
);
101 /* MIT and Heimdal differ as to if you can call
102 * gss_release_oid() on this OID, generated by
103 * gss_{accept,init}_sec_context(). However, as long as the
104 * oid is gss_mech_krb5 (which it always is at the moment),
105 * then this is a moot point, as both declare this particular
106 * OID static, and so no memory is lost. This assert is in
107 * place to ensure that the programmer who wishes to extend
108 * this code to EAP or other GSS mechanisms determines an
109 * implementation-dependent way of releasing any dynamically
111 SMB_ASSERT(smb_gss_oid_equal(&gse_ctx
->gss_mech
, GSS_C_NO_OID
) ||
112 smb_gss_oid_equal(&gse_ctx
->gss_mech
, gss_mech_krb5
));
117 static NTSTATUS
gse_context_init(TALLOC_CTX
*mem_ctx
,
118 bool do_sign
, bool do_seal
,
119 const char *ccache_name
,
120 uint32_t add_gss_c_flags
,
121 struct gse_context
**_gse_ctx
)
123 struct gse_context
*gse_ctx
;
124 krb5_error_code k5ret
;
127 gse_ctx
= talloc_zero(mem_ctx
, struct gse_context
);
129 return NT_STATUS_NO_MEMORY
;
131 talloc_set_destructor((TALLOC_CTX
*)gse_ctx
, gse_context_destructor
);
133 gse_ctx
->expire_time
= GENSEC_EXPIRE_TIME_INFINITY
;
135 memcpy(&gse_ctx
->gss_mech
, gss_mech_krb5
, sizeof(gss_OID_desc
));
137 gse_ctx
->gss_want_flags
= GSS_C_MUTUAL_FLAG
|
139 GSS_C_DELEG_POLICY_FLAG
|
143 gse_ctx
->gss_want_flags
|= GSS_C_INTEG_FLAG
;
146 gse_ctx
->gss_want_flags
|= GSS_C_INTEG_FLAG
;
147 gse_ctx
->gss_want_flags
|= GSS_C_CONF_FLAG
;
150 gse_ctx
->gss_want_flags
|= add_gss_c_flags
;
152 /* Initialize Kerberos Context */
153 initialize_krb5_error_table();
155 k5ret
= krb5_init_context(&gse_ctx
->k5ctx
);
157 DEBUG(0, ("Failed to initialize kerberos context! (%s)\n",
158 error_message(k5ret
)));
159 status
= NT_STATUS_INTERNAL_ERROR
;
164 ccache_name
= krb5_cc_default_name(gse_ctx
->k5ctx
);
166 k5ret
= krb5_cc_resolve(gse_ctx
->k5ctx
, ccache_name
,
169 DEBUG(1, ("Failed to resolve credential cache! (%s)\n",
170 error_message(k5ret
)));
171 status
= NT_STATUS_INTERNAL_ERROR
;
175 /* TODO: Should we enforce a enc_types list ?
176 ret = krb5_set_default_tgs_ktypes(gse_ctx->k5ctx, enc_types);
183 TALLOC_FREE(gse_ctx
);
187 static NTSTATUS
gse_init_client(TALLOC_CTX
*mem_ctx
,
188 bool do_sign
, bool do_seal
,
189 const char *ccache_name
,
192 const char *username
,
193 const char *password
,
194 uint32_t add_gss_c_flags
,
195 struct gse_context
**_gse_ctx
)
197 struct gse_context
*gse_ctx
;
198 OM_uint32 gss_maj
, gss_min
;
199 gss_buffer_desc name_buffer
= {0, NULL
};
200 gss_OID_set_desc mech_set
;
203 if (!server
|| !service
) {
204 return NT_STATUS_INVALID_PARAMETER
;
207 status
= gse_context_init(mem_ctx
, do_sign
, do_seal
,
208 ccache_name
, add_gss_c_flags
,
210 if (!NT_STATUS_IS_OK(status
)) {
211 return NT_STATUS_NO_MEMORY
;
214 /* Guess the realm based on the supplied service, and avoid the GSS libs
215 doing DNS lookups which may fail.
217 TODO: Loop with the KDC on some more combinations (local
218 realm in particular), possibly falling back to
219 GSS_C_NT_HOSTBASED_SERVICE
221 name_buffer
.value
= kerberos_get_principal_from_service_hostname(
222 gse_ctx
, service
, server
, lp_realm());
223 if (!name_buffer
.value
) {
224 status
= NT_STATUS_NO_MEMORY
;
227 name_buffer
.length
= strlen((char *)name_buffer
.value
);
228 gss_maj
= gss_import_name(&gss_min
, &name_buffer
,
230 &gse_ctx
->server_name
);
232 DEBUG(0, ("gss_import_name failed for %s, with [%s]\n",
233 (char *)name_buffer
.value
,
234 gse_errstr(gse_ctx
, gss_maj
, gss_min
)));
235 status
= NT_STATUS_INTERNAL_ERROR
;
239 /* TODO: get krb5 ticket using username/password, if no valid
240 * one already available in ccache */
243 mech_set
.elements
= &gse_ctx
->gss_mech
;
245 gss_maj
= gss_acquire_cred(&gss_min
,
253 DEBUG(0, ("gss_acquire_creds failed for %s, with [%s]\n",
254 (char *)name_buffer
.value
,
255 gse_errstr(gse_ctx
, gss_maj
, gss_min
)));
256 status
= NT_STATUS_INTERNAL_ERROR
;
261 TALLOC_FREE(name_buffer
.value
);
265 TALLOC_FREE(name_buffer
.value
);
266 TALLOC_FREE(gse_ctx
);
270 static NTSTATUS
gse_get_client_auth_token(TALLOC_CTX
*mem_ctx
,
271 struct gse_context
*gse_ctx
,
272 const DATA_BLOB
*token_in
,
273 DATA_BLOB
*token_out
)
275 OM_uint32 gss_maj
, gss_min
;
276 gss_buffer_desc in_data
;
277 gss_buffer_desc out_data
;
278 DATA_BLOB blob
= data_blob_null
;
280 OM_uint32 time_rec
= 0;
283 in_data
.value
= token_in
->data
;
284 in_data
.length
= token_in
->length
;
286 gss_maj
= gss_init_sec_context(&gss_min
,
288 &gse_ctx
->gssapi_context
,
289 gse_ctx
->server_name
,
291 gse_ctx
->gss_want_flags
,
292 0, GSS_C_NO_CHANNEL_BINDINGS
,
293 &in_data
, NULL
, &out_data
,
294 &gse_ctx
->gss_got_flags
, &time_rec
);
297 /* we are done with it */
298 tv
= timeval_current_ofs(time_rec
, 0);
299 gse_ctx
->expire_time
= timeval_to_nttime(&tv
);
301 status
= NT_STATUS_OK
;
303 case GSS_S_CONTINUE_NEEDED
:
304 /* we will need a third leg */
305 status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
308 DEBUG(0, ("gss_init_sec_context failed with [%s]\n",
309 gse_errstr(talloc_tos(), gss_maj
, gss_min
)));
310 status
= NT_STATUS_INTERNAL_ERROR
;
314 /* we may be told to return nothing */
315 if (out_data
.length
) {
316 blob
= data_blob_talloc(mem_ctx
, out_data
.value
, out_data
.length
);
318 status
= NT_STATUS_NO_MEMORY
;
321 gss_maj
= gss_release_buffer(&gss_min
, &out_data
);
329 static NTSTATUS
gse_init_server(TALLOC_CTX
*mem_ctx
,
330 bool do_sign
, bool do_seal
,
331 uint32_t add_gss_c_flags
,
332 struct gse_context
**_gse_ctx
)
334 struct gse_context
*gse_ctx
;
335 OM_uint32 gss_maj
, gss_min
;
339 status
= gse_context_init(mem_ctx
, do_sign
, do_seal
,
340 NULL
, add_gss_c_flags
, &gse_ctx
);
341 if (!NT_STATUS_IS_OK(status
)) {
342 return NT_STATUS_NO_MEMORY
;
345 ret
= gse_krb5_get_server_keytab(gse_ctx
->k5ctx
,
348 status
= NT_STATUS_INTERNAL_ERROR
;
352 #ifdef HAVE_GSS_KRB5_IMPORT_CRED
354 /* This creates a GSSAPI cred_id_t with the keytab set */
355 gss_maj
= gss_krb5_import_cred(&gss_min
, NULL
, NULL
, gse_ctx
->keytab
,
359 && gss_maj
!= (GSS_S_CALL_BAD_STRUCTURE
|GSS_S_BAD_NAME
)) {
360 DEBUG(0, ("gss_krb5_import_cred failed with [%s]\n",
361 gse_errstr(gse_ctx
, gss_maj
, gss_min
)));
362 status
= NT_STATUS_INTERNAL_ERROR
;
365 /* This is the error the MIT krb5 1.9 gives when it
366 * implements the function, but we do not specify the
367 * principal. However, when we specify the principal
368 * as host$@REALM the GSS acceptor fails with 'wrong
369 * principal in request'. Work around the issue by
370 * falling back to the alternate approach below. */
371 } else if (gss_maj
== (GSS_S_CALL_BAD_STRUCTURE
|GSS_S_BAD_NAME
))
374 * This call sets the default keytab for the whole server, not
375 * just for this context. Need to find a way that does not alter
376 * the state of the whole server ... */
379 gss_OID_set_desc mech_set
;
381 ret
= smb_krb5_keytab_name(gse_ctx
, gse_ctx
->k5ctx
,
382 gse_ctx
->keytab
, &ktname
);
384 status
= NT_STATUS_INTERNAL_ERROR
;
388 ret
= gsskrb5_register_acceptor_identity(ktname
);
390 status
= NT_STATUS_INTERNAL_ERROR
;
395 mech_set
.elements
= &gse_ctx
->gss_mech
;
397 gss_maj
= gss_acquire_cred(&gss_min
,
406 DEBUG(0, ("gss_acquire_creds failed with [%s]\n",
407 gse_errstr(gse_ctx
, gss_maj
, gss_min
)));
408 status
= NT_STATUS_INTERNAL_ERROR
;
413 status
= NT_STATUS_OK
;
416 if (!NT_STATUS_IS_OK(status
)) {
417 TALLOC_FREE(gse_ctx
);
424 static NTSTATUS
gse_get_server_auth_token(TALLOC_CTX
*mem_ctx
,
425 struct gse_context
*gse_ctx
,
426 const DATA_BLOB
*token_in
,
427 DATA_BLOB
*token_out
)
429 OM_uint32 gss_maj
, gss_min
;
430 gss_buffer_desc in_data
;
431 gss_buffer_desc out_data
;
432 DATA_BLOB blob
= data_blob_null
;
434 OM_uint32 time_rec
= 0;
437 in_data
.value
= token_in
->data
;
438 in_data
.length
= token_in
->length
;
440 gss_maj
= gss_accept_sec_context(&gss_min
,
441 &gse_ctx
->gssapi_context
,
444 GSS_C_NO_CHANNEL_BINDINGS
,
445 &gse_ctx
->client_name
,
448 &gse_ctx
->gss_got_flags
,
450 &gse_ctx
->delegated_cred_handle
);
453 /* we are done with it */
454 tv
= timeval_current_ofs(time_rec
, 0);
455 gse_ctx
->expire_time
= timeval_to_nttime(&tv
);
457 status
= NT_STATUS_OK
;
459 case GSS_S_CONTINUE_NEEDED
:
460 /* we will need a third leg */
461 status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
464 DEBUG(1, ("gss_accept_sec_context failed with [%s]\n",
465 gse_errstr(talloc_tos(), gss_maj
, gss_min
)));
467 if (gse_ctx
->gssapi_context
) {
468 gss_delete_sec_context(&gss_min
,
469 &gse_ctx
->gssapi_context
,
473 status
= NT_STATUS_LOGON_FAILURE
;
477 /* we may be told to return nothing */
478 if (out_data
.length
) {
479 blob
= data_blob_talloc(mem_ctx
, out_data
.value
, out_data
.length
);
481 status
= NT_STATUS_NO_MEMORY
;
483 gss_maj
= gss_release_buffer(&gss_min
, &out_data
);
492 static char *gse_errstr(TALLOC_CTX
*mem_ctx
, OM_uint32 maj
, OM_uint32 min
)
494 OM_uint32 gss_min
, gss_maj
;
495 gss_buffer_desc msg_min
;
496 gss_buffer_desc msg_maj
;
497 OM_uint32 msg_ctx
= 0;
501 ZERO_STRUCT(msg_min
);
502 ZERO_STRUCT(msg_maj
);
504 gss_maj
= gss_display_status(&gss_min
, maj
, GSS_C_GSS_CODE
,
505 GSS_C_NO_OID
, &msg_ctx
, &msg_maj
);
509 errstr
= talloc_strndup(mem_ctx
,
510 (char *)msg_maj
.value
,
515 gss_maj
= gss_display_status(&gss_min
, min
, GSS_C_MECH_CODE
,
516 (gss_OID
)discard_const(gss_mech_krb5
),
522 errstr
= talloc_strdup_append_buffer(errstr
, ": ");
526 errstr
= talloc_strndup_append_buffer(errstr
,
527 (char *)msg_min
.value
,
535 gss_maj
= gss_release_buffer(&gss_min
, &msg_min
);
538 gss_maj
= gss_release_buffer(&gss_min
, &msg_maj
);
543 static size_t gse_get_signature_length(struct gse_context
*gse_ctx
,
544 bool seal
, size_t payload_size
)
546 OM_uint32 gss_min
, gss_maj
;
547 gss_iov_buffer_desc iov
[2];
551 * gss_wrap_iov_length() only needs the type and length
553 iov
[0].type
= GSS_IOV_BUFFER_TYPE_HEADER
;
554 iov
[0].buffer
.value
= NULL
;
555 iov
[0].buffer
.length
= 0;
556 iov
[1].type
= GSS_IOV_BUFFER_TYPE_DATA
;
557 iov
[1].buffer
.value
= NULL
;
558 iov
[1].buffer
.length
= payload_size
;
560 gss_maj
= gss_wrap_iov_length(&gss_min
, gse_ctx
->gssapi_context
,
561 seal
, GSS_C_QOP_DEFAULT
,
564 DEBUG(0, ("gss_wrap_iov_length failed with [%s]\n",
565 gse_errstr(talloc_tos(), gss_maj
, gss_min
)));
569 return iov
[0].buffer
.length
;
572 static NTSTATUS
gse_seal(TALLOC_CTX
*mem_ctx
, struct gse_context
*gse_ctx
,
573 DATA_BLOB
*data
, DATA_BLOB
*signature
)
575 OM_uint32 gss_min
, gss_maj
;
576 gss_iov_buffer_desc iov
[2];
577 int req_seal
= 1; /* setting to 1 means we request sign+seal */
581 /* allocate the memory ourselves so we do not need to talloc_memdup */
582 signature
->length
= gse_get_signature_length(gse_ctx
, true, data
->length
);
583 if (!signature
->length
) {
584 return NT_STATUS_INTERNAL_ERROR
;
586 signature
->data
= (uint8_t *)talloc_size(mem_ctx
, signature
->length
);
587 if (!signature
->data
) {
588 return NT_STATUS_NO_MEMORY
;
590 iov
[0].type
= GSS_IOV_BUFFER_TYPE_HEADER
;
591 iov
[0].buffer
.value
= signature
->data
;
592 iov
[0].buffer
.length
= signature
->length
;
594 /* data is encrypted in place, which is ok */
595 iov
[1].type
= GSS_IOV_BUFFER_TYPE_DATA
;
596 iov
[1].buffer
.value
= data
->data
;
597 iov
[1].buffer
.length
= data
->length
;
599 gss_maj
= gss_wrap_iov(&gss_min
, gse_ctx
->gssapi_context
,
600 req_seal
, GSS_C_QOP_DEFAULT
,
603 DEBUG(0, ("gss_wrap_iov failed with [%s]\n",
604 gse_errstr(talloc_tos(), gss_maj
, gss_min
)));
605 status
= NT_STATUS_ACCESS_DENIED
;
610 DEBUG(0, ("gss_wrap_iov says data was not sealed!\n"));
611 status
= NT_STATUS_ACCESS_DENIED
;
615 status
= NT_STATUS_OK
;
617 DEBUG(10, ("Sealed %d bytes, and got %d bytes header/signature.\n",
618 (int)iov
[1].buffer
.length
, (int)iov
[0].buffer
.length
));
624 static NTSTATUS
gse_unseal(TALLOC_CTX
*mem_ctx
, struct gse_context
*gse_ctx
,
625 DATA_BLOB
*data
, const DATA_BLOB
*signature
)
627 OM_uint32 gss_min
, gss_maj
;
628 gss_iov_buffer_desc iov
[2];
632 iov
[0].type
= GSS_IOV_BUFFER_TYPE_HEADER
;
633 iov
[0].buffer
.value
= signature
->data
;
634 iov
[0].buffer
.length
= signature
->length
;
636 /* data is decrypted in place, which is ok */
637 iov
[1].type
= GSS_IOV_BUFFER_TYPE_DATA
;
638 iov
[1].buffer
.value
= data
->data
;
639 iov
[1].buffer
.length
= data
->length
;
641 gss_maj
= gss_unwrap_iov(&gss_min
, gse_ctx
->gssapi_context
,
642 &sealed
, NULL
, iov
, 2);
644 DEBUG(0, ("gss_unwrap_iov failed with [%s]\n",
645 gse_errstr(talloc_tos(), gss_maj
, gss_min
)));
646 status
= NT_STATUS_ACCESS_DENIED
;
651 DEBUG(0, ("gss_unwrap_iov says data is not sealed!\n"));
652 status
= NT_STATUS_ACCESS_DENIED
;
656 status
= NT_STATUS_OK
;
658 DEBUG(10, ("Unsealed %d bytes, with %d bytes header/signature.\n",
659 (int)iov
[1].buffer
.length
, (int)iov
[0].buffer
.length
));
665 static NTSTATUS
gse_sign(TALLOC_CTX
*mem_ctx
, struct gse_context
*gse_ctx
,
666 DATA_BLOB
*data
, DATA_BLOB
*signature
)
668 OM_uint32 gss_min
, gss_maj
;
669 gss_buffer_desc in_data
= { 0, NULL
};
670 gss_buffer_desc out_data
= { 0, NULL
};
673 in_data
.value
= data
->data
;
674 in_data
.length
= data
->length
;
676 gss_maj
= gss_get_mic(&gss_min
, gse_ctx
->gssapi_context
,
678 &in_data
, &out_data
);
680 DEBUG(0, ("gss_get_mic failed with [%s]\n",
681 gse_errstr(talloc_tos(), gss_maj
, gss_min
)));
682 status
= NT_STATUS_ACCESS_DENIED
;
686 *signature
= data_blob_talloc(mem_ctx
,
687 out_data
.value
, out_data
.length
);
688 if (!signature
->data
) {
689 status
= NT_STATUS_NO_MEMORY
;
693 status
= NT_STATUS_OK
;
696 if (out_data
.value
) {
697 gss_maj
= gss_release_buffer(&gss_min
, &out_data
);
702 static NTSTATUS
gse_sigcheck(TALLOC_CTX
*mem_ctx
, struct gse_context
*gse_ctx
,
703 const DATA_BLOB
*data
, const DATA_BLOB
*signature
)
705 OM_uint32 gss_min
, gss_maj
;
706 gss_buffer_desc in_data
= { 0, NULL
};
707 gss_buffer_desc in_token
= { 0, NULL
};
710 in_data
.value
= data
->data
;
711 in_data
.length
= data
->length
;
712 in_token
.value
= signature
->data
;
713 in_token
.length
= signature
->length
;
715 gss_maj
= gss_verify_mic(&gss_min
, gse_ctx
->gssapi_context
,
716 &in_data
, &in_token
, NULL
);
718 DEBUG(0, ("gss_verify_mic failed with [%s]\n",
719 gse_errstr(talloc_tos(), gss_maj
, gss_min
)));
720 status
= NT_STATUS_ACCESS_DENIED
;
724 status
= NT_STATUS_OK
;
730 static NTSTATUS
gensec_gse_client_start(struct gensec_security
*gensec_security
)
732 struct gse_context
*gse_ctx
;
733 struct cli_credentials
*creds
= gensec_get_credentials(gensec_security
);
735 OM_uint32 want_flags
= 0;
736 bool do_sign
= false, do_seal
= false;
737 const char *hostname
= gensec_get_target_hostname(gensec_security
);
738 const char *service
= gensec_get_target_service(gensec_security
);
739 const char *username
= cli_credentials_get_username(creds
);
740 const char *password
= cli_credentials_get_password(creds
);
743 DEBUG(1, ("Could not determine hostname for target computer, cannot use kerberos\n"));
744 return NT_STATUS_INVALID_PARAMETER
;
746 if (is_ipaddress(hostname
)) {
747 DEBUG(2, ("Cannot do GSE to an IP address\n"));
748 return NT_STATUS_INVALID_PARAMETER
;
750 if (strcmp(hostname
, "localhost") == 0) {
751 DEBUG(2, ("GSE to 'localhost' does not make sense\n"));
752 return NT_STATUS_INVALID_PARAMETER
;
755 if (gensec_security
->want_features
& GENSEC_FEATURE_SIGN
) {
758 if (gensec_security
->want_features
& GENSEC_FEATURE_SEAL
) {
761 if (gensec_security
->want_features
& GENSEC_FEATURE_DCE_STYLE
) {
762 want_flags
|= GSS_C_DCE_STYLE
;
765 nt_status
= gse_init_client(gensec_security
, do_sign
, do_seal
, NULL
,
767 username
, password
, want_flags
,
769 if (!NT_STATUS_IS_OK(nt_status
)) {
772 gensec_security
->private_data
= gse_ctx
;
776 static NTSTATUS
gensec_gse_server_start(struct gensec_security
*gensec_security
)
778 struct gse_context
*gse_ctx
;
780 OM_uint32 want_flags
= 0;
781 bool do_sign
= false, do_seal
= false;
783 if (gensec_security
->want_features
& GENSEC_FEATURE_SIGN
) {
786 if (gensec_security
->want_features
& GENSEC_FEATURE_SEAL
) {
789 if (gensec_security
->want_features
& GENSEC_FEATURE_DCE_STYLE
) {
790 want_flags
|= GSS_C_DCE_STYLE
;
793 nt_status
= gse_init_server(gensec_security
, do_sign
, do_seal
, want_flags
,
795 if (!NT_STATUS_IS_OK(nt_status
)) {
798 gensec_security
->private_data
= gse_ctx
;
803 * Next state function for the GSE GENSEC mechanism
805 * @param gensec_gse_state GSE State
806 * @param mem_ctx The TALLOC_CTX for *out to be allocated on
807 * @param in The request, as a DATA_BLOB
808 * @param out The reply, as an talloc()ed DATA_BLOB, on *mem_ctx
809 * @return Error, MORE_PROCESSING_REQUIRED if a reply is sent,
810 * or NT_STATUS_OK if the user is authenticated.
813 static NTSTATUS
gensec_gse_update(struct gensec_security
*gensec_security
,
815 struct tevent_context
*ev
,
816 const DATA_BLOB in
, DATA_BLOB
*out
)
819 struct gse_context
*gse_ctx
=
820 talloc_get_type_abort(gensec_security
->private_data
,
823 switch (gensec_security
->gensec_role
) {
825 status
= gse_get_client_auth_token(mem_ctx
, gse_ctx
,
829 status
= gse_get_server_auth_token(mem_ctx
, gse_ctx
,
833 if (!NT_STATUS_IS_OK(status
)) {
840 static NTSTATUS
gensec_gse_wrap(struct gensec_security
*gensec_security
,
845 struct gse_context
*gse_ctx
=
846 talloc_get_type_abort(gensec_security
->private_data
,
848 OM_uint32 maj_stat
, min_stat
;
849 gss_buffer_desc input_token
, output_token
;
851 input_token
.length
= in
->length
;
852 input_token
.value
= in
->data
;
854 maj_stat
= gss_wrap(&min_stat
,
855 gse_ctx
->gssapi_context
,
856 gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
),
861 if (GSS_ERROR(maj_stat
)) {
862 DEBUG(0, ("gensec_gse_wrap: GSS Wrap failed: %s\n",
863 gse_errstr(talloc_tos(), maj_stat
, min_stat
)));
864 return NT_STATUS_ACCESS_DENIED
;
867 *out
= data_blob_talloc(mem_ctx
, output_token
.value
, output_token
.length
);
868 gss_release_buffer(&min_stat
, &output_token
);
870 if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
)
872 return NT_STATUS_ACCESS_DENIED
;
877 static NTSTATUS
gensec_gse_unwrap(struct gensec_security
*gensec_security
,
882 struct gse_context
*gse_ctx
=
883 talloc_get_type_abort(gensec_security
->private_data
,
885 OM_uint32 maj_stat
, min_stat
;
886 gss_buffer_desc input_token
, output_token
;
889 input_token
.length
= in
->length
;
890 input_token
.value
= in
->data
;
892 maj_stat
= gss_unwrap(&min_stat
,
893 gse_ctx
->gssapi_context
,
898 if (GSS_ERROR(maj_stat
)) {
899 DEBUG(0, ("gensec_gse_unwrap: GSS UnWrap failed: %s\n",
900 gse_errstr(talloc_tos(), maj_stat
, min_stat
)));
901 return NT_STATUS_ACCESS_DENIED
;
904 *out
= data_blob_talloc(mem_ctx
, output_token
.value
, output_token
.length
);
905 gss_release_buffer(&min_stat
, &output_token
);
907 if (gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
)
909 return NT_STATUS_ACCESS_DENIED
;
914 static NTSTATUS
gensec_gse_seal_packet(struct gensec_security
*gensec_security
,
916 uint8_t *data
, size_t length
,
917 const uint8_t *whole_pdu
, size_t pdu_length
,
920 struct gse_context
*gse_ctx
=
921 talloc_get_type_abort(gensec_security
->private_data
,
923 DATA_BLOB payload
= data_blob_const(data
, length
);
924 return gse_seal(mem_ctx
, gse_ctx
, &payload
, sig
);
927 static NTSTATUS
gensec_gse_unseal_packet(struct gensec_security
*gensec_security
,
928 uint8_t *data
, size_t length
,
929 const uint8_t *whole_pdu
, size_t pdu_length
,
930 const DATA_BLOB
*sig
)
932 struct gse_context
*gse_ctx
=
933 talloc_get_type_abort(gensec_security
->private_data
,
935 DATA_BLOB payload
= data_blob_const(data
, length
);
936 return gse_unseal(talloc_tos() /* unused */, gse_ctx
, &payload
, sig
);
939 static NTSTATUS
gensec_gse_sign_packet(struct gensec_security
*gensec_security
,
941 const uint8_t *data
, size_t length
,
942 const uint8_t *whole_pdu
, size_t pdu_length
,
945 struct gse_context
*gse_ctx
=
946 talloc_get_type_abort(gensec_security
->private_data
,
948 DATA_BLOB payload
= data_blob_const(data
, length
);
949 return gse_sign(mem_ctx
, gse_ctx
, &payload
, sig
);
952 static NTSTATUS
gensec_gse_check_packet(struct gensec_security
*gensec_security
,
953 const uint8_t *data
, size_t length
,
954 const uint8_t *whole_pdu
, size_t pdu_length
,
955 const DATA_BLOB
*sig
)
957 struct gse_context
*gse_ctx
=
958 talloc_get_type_abort(gensec_security
->private_data
,
960 DATA_BLOB payload
= data_blob_const(data
, length
);
961 return gse_sigcheck(NULL
, gse_ctx
, &payload
, sig
);
964 /* Try to figure out what features we actually got on the connection */
965 static bool gensec_gse_have_feature(struct gensec_security
*gensec_security
,
968 struct gse_context
*gse_ctx
=
969 talloc_get_type_abort(gensec_security
->private_data
,
972 if (feature
& GENSEC_FEATURE_SIGN
) {
973 return gse_ctx
->gss_got_flags
& GSS_C_INTEG_FLAG
;
975 if (feature
& GENSEC_FEATURE_SEAL
) {
976 return gse_ctx
->gss_got_flags
& GSS_C_CONF_FLAG
;
978 if (feature
& GENSEC_FEATURE_SESSION_KEY
) {
979 /* Only for GSE/Krb5 */
980 if (smb_gss_oid_equal(gse_ctx
->ret_mech
, gss_mech_krb5
)) {
984 if (feature
& GENSEC_FEATURE_DCE_STYLE
) {
985 return gse_ctx
->gss_got_flags
& GSS_C_DCE_STYLE
;
987 if (feature
& GENSEC_FEATURE_NEW_SPNEGO
) {
991 if (!(gse_ctx
->gss_got_flags
& GSS_C_INTEG_FLAG
)) {
995 status
= gssapi_get_session_key(talloc_tos(),
996 gse_ctx
->gssapi_context
, NULL
, &keytype
);
998 * We should do a proper sig on the mechListMic unless
999 * we know we have to be backwards compatible with
1000 * earlier windows versions.
1002 * Negotiating a non-krb5
1003 * mech for example should be regarded as having
1006 if (NT_STATUS_IS_OK(status
)) {
1008 case ENCTYPE_DES_CBC_CRC
:
1009 case ENCTYPE_DES_CBC_MD5
:
1010 case ENCTYPE_ARCFOUR_HMAC
:
1011 case ENCTYPE_DES3_CBC_SHA1
:
1017 /* We can always do async (rather than strict request/reply) packets. */
1018 if (feature
& GENSEC_FEATURE_ASYNC_REPLIES
) {
1024 static NTTIME
gensec_gse_expire_time(struct gensec_security
*gensec_security
)
1026 struct gse_context
*gse_ctx
=
1027 talloc_get_type_abort(gensec_security
->private_data
,
1028 struct gse_context
);
1030 return gse_ctx
->expire_time
;
1034 * Extract the 'sesssion key' needed by SMB signing and ncacn_np
1035 * (for encrypting some passwords).
1037 * This breaks all the abstractions, but what do you expect...
1039 static NTSTATUS
gensec_gse_session_key(struct gensec_security
*gensec_security
,
1040 TALLOC_CTX
*mem_ctx
,
1041 DATA_BLOB
*session_key
)
1043 struct gse_context
*gse_ctx
=
1044 talloc_get_type_abort(gensec_security
->private_data
,
1045 struct gse_context
);
1047 return gssapi_get_session_key(mem_ctx
, gse_ctx
->gssapi_context
, session_key
, NULL
);
1050 /* Get some basic (and authorization) information about the user on
1051 * this session. This uses either the PAC (if present) or a local
1052 * database lookup */
1053 static NTSTATUS
gensec_gse_session_info(struct gensec_security
*gensec_security
,
1054 TALLOC_CTX
*mem_ctx
,
1055 struct auth_session_info
**_session_info
)
1057 struct gse_context
*gse_ctx
=
1058 talloc_get_type_abort(gensec_security
->private_data
,
1059 struct gse_context
);
1061 TALLOC_CTX
*tmp_ctx
;
1062 struct auth_session_info
*session_info
= NULL
;
1063 OM_uint32 maj_stat
, min_stat
;
1064 DATA_BLOB pac_blob
, *pac_blob_ptr
= NULL
;
1066 gss_buffer_desc name_token
;
1067 char *principal_string
;
1069 tmp_ctx
= talloc_named(mem_ctx
, 0, "gensec_gse_session_info context");
1070 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
1072 maj_stat
= gss_display_name(&min_stat
,
1073 gse_ctx
->client_name
,
1076 if (GSS_ERROR(maj_stat
)) {
1077 DEBUG(1, ("GSS display_name failed: %s\n",
1078 gse_errstr(talloc_tos(), maj_stat
, min_stat
)));
1079 talloc_free(tmp_ctx
);
1080 return NT_STATUS_FOOBAR
;
1083 principal_string
= talloc_strndup(tmp_ctx
,
1084 (const char *)name_token
.value
,
1087 gss_release_buffer(&min_stat
, &name_token
);
1089 if (!principal_string
) {
1090 talloc_free(tmp_ctx
);
1091 return NT_STATUS_NO_MEMORY
;
1094 nt_status
= gssapi_obtain_pac_blob(tmp_ctx
, gse_ctx
->gssapi_context
,
1095 gse_ctx
->client_name
,
1098 /* IF we have the PAC - otherwise we need to get this
1099 * data from elsewere
1101 if (NT_STATUS_IS_OK(nt_status
)) {
1102 pac_blob_ptr
= &pac_blob
;
1104 nt_status
= gensec_generate_session_info_pac(tmp_ctx
,
1107 pac_blob_ptr
, principal_string
,
1108 gensec_get_remote_address(gensec_security
),
1110 if (!NT_STATUS_IS_OK(nt_status
)) {
1111 talloc_free(tmp_ctx
);
1115 nt_status
= gensec_gse_session_key(gensec_security
, session_info
,
1116 &session_info
->session_key
);
1117 if (!NT_STATUS_IS_OK(nt_status
)) {
1118 talloc_free(tmp_ctx
);
1122 *_session_info
= talloc_move(mem_ctx
, &session_info
);
1123 talloc_free(tmp_ctx
);
1125 return NT_STATUS_OK
;
1128 static size_t gensec_gse_sig_size(struct gensec_security
*gensec_security
,
1131 struct gse_context
*gse_ctx
=
1132 talloc_get_type_abort(gensec_security
->private_data
,
1133 struct gse_context
);
1135 return gse_get_signature_length(gse_ctx
,
1136 gensec_security
->want_features
& GENSEC_FEATURE_SEAL
,
1140 static const char *gensec_gse_krb5_oids
[] = {
1141 GENSEC_OID_KERBEROS5_OLD
,
1142 GENSEC_OID_KERBEROS5
,
1146 const struct gensec_security_ops gensec_gse_krb5_security_ops
= {
1148 .auth_type
= DCERPC_AUTH_TYPE_KRB5
,
1149 .oid
= gensec_gse_krb5_oids
,
1150 .client_start
= gensec_gse_client_start
,
1151 .server_start
= gensec_gse_server_start
,
1152 .magic
= gensec_magic_check_krb5_oid
,
1153 .update
= gensec_gse_update
,
1154 .session_key
= gensec_gse_session_key
,
1155 .session_info
= gensec_gse_session_info
,
1156 .sig_size
= gensec_gse_sig_size
,
1157 .sign_packet
= gensec_gse_sign_packet
,
1158 .check_packet
= gensec_gse_check_packet
,
1159 .seal_packet
= gensec_gse_seal_packet
,
1160 .unseal_packet
= gensec_gse_unseal_packet
,
1161 .wrap
= gensec_gse_wrap
,
1162 .unwrap
= gensec_gse_unwrap
,
1163 .have_feature
= gensec_gse_have_feature
,
1164 .expire_time
= gensec_gse_expire_time
,
1167 .priority
= GENSEC_GSSAPI
1170 #endif /* HAVE_KRB5 */