2 Unix SMB/CIFS implementation.
4 Generic Authentication Interface
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2006
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "system/network.h"
26 #include "lib/tsocket/tsocket.h"
27 #include "lib/util/tevent_ntstatus.h"
28 #include "auth/gensec/gensec.h"
29 #include "auth/gensec/gensec_internal.h"
30 #include "librpc/gen_ndr/dcerpc.h"
33 wrappers for the gensec function pointers
35 _PUBLIC_ NTSTATUS
gensec_unseal_packet(struct gensec_security
*gensec_security
,
36 uint8_t *data
, size_t length
,
37 const uint8_t *whole_pdu
, size_t pdu_length
,
40 if (!gensec_security
->ops
->unseal_packet
) {
41 return NT_STATUS_NOT_IMPLEMENTED
;
43 if (!gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
)) {
44 return NT_STATUS_INVALID_PARAMETER
;
47 return gensec_security
->ops
->unseal_packet(gensec_security
,
49 whole_pdu
, pdu_length
,
53 _PUBLIC_ NTSTATUS
gensec_check_packet(struct gensec_security
*gensec_security
,
54 const uint8_t *data
, size_t length
,
55 const uint8_t *whole_pdu
, size_t pdu_length
,
58 if (!gensec_security
->ops
->check_packet
) {
59 return NT_STATUS_NOT_IMPLEMENTED
;
61 if (!gensec_have_feature(gensec_security
, GENSEC_FEATURE_SIGN
)) {
62 return NT_STATUS_INVALID_PARAMETER
;
65 return gensec_security
->ops
->check_packet(gensec_security
, data
, length
, whole_pdu
, pdu_length
, sig
);
68 _PUBLIC_ NTSTATUS
gensec_seal_packet(struct gensec_security
*gensec_security
,
70 uint8_t *data
, size_t length
,
71 const uint8_t *whole_pdu
, size_t pdu_length
,
74 if (!gensec_security
->ops
->seal_packet
) {
75 return NT_STATUS_NOT_IMPLEMENTED
;
77 if (!gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
)) {
78 return NT_STATUS_INVALID_PARAMETER
;
80 if (!gensec_have_feature(gensec_security
, GENSEC_FEATURE_SIGN
)) {
81 return NT_STATUS_INVALID_PARAMETER
;
84 return gensec_security
->ops
->seal_packet(gensec_security
, mem_ctx
, data
, length
, whole_pdu
, pdu_length
, sig
);
87 _PUBLIC_ NTSTATUS
gensec_sign_packet(struct gensec_security
*gensec_security
,
89 const uint8_t *data
, size_t length
,
90 const uint8_t *whole_pdu
, size_t pdu_length
,
93 if (!gensec_security
->ops
->sign_packet
) {
94 return NT_STATUS_NOT_IMPLEMENTED
;
96 if (!gensec_have_feature(gensec_security
, GENSEC_FEATURE_SIGN
)) {
97 return NT_STATUS_INVALID_PARAMETER
;
100 return gensec_security
->ops
->sign_packet(gensec_security
, mem_ctx
, data
, length
, whole_pdu
, pdu_length
, sig
);
103 _PUBLIC_
size_t gensec_sig_size(struct gensec_security
*gensec_security
, size_t data_size
)
105 if (!gensec_security
->ops
->sig_size
) {
108 if (!gensec_have_feature(gensec_security
, GENSEC_FEATURE_SIGN
)) {
112 return gensec_security
->ops
->sig_size(gensec_security
, data_size
);
115 _PUBLIC_
size_t gensec_max_wrapped_size(struct gensec_security
*gensec_security
)
117 if (!gensec_security
->ops
->max_wrapped_size
) {
121 return gensec_security
->ops
->max_wrapped_size(gensec_security
);
124 _PUBLIC_
size_t gensec_max_input_size(struct gensec_security
*gensec_security
)
126 if (!gensec_security
->ops
->max_input_size
) {
127 return (1 << 17) - gensec_sig_size(gensec_security
, 1 << 17);
130 return gensec_security
->ops
->max_input_size(gensec_security
);
133 _PUBLIC_ NTSTATUS
gensec_wrap(struct gensec_security
*gensec_security
,
138 if (!gensec_security
->ops
->wrap
) {
139 return NT_STATUS_NOT_IMPLEMENTED
;
141 return gensec_security
->ops
->wrap(gensec_security
, mem_ctx
, in
, out
);
144 _PUBLIC_ NTSTATUS
gensec_unwrap(struct gensec_security
*gensec_security
,
149 if (!gensec_security
->ops
->unwrap
) {
150 return NT_STATUS_NOT_IMPLEMENTED
;
152 return gensec_security
->ops
->unwrap(gensec_security
, mem_ctx
, in
, out
);
155 _PUBLIC_ NTSTATUS
gensec_session_key(struct gensec_security
*gensec_security
,
157 DATA_BLOB
*session_key
)
159 if (!gensec_have_feature(gensec_security
, GENSEC_FEATURE_SESSION_KEY
)) {
160 return NT_STATUS_NO_USER_SESSION_KEY
;
163 if (!gensec_security
->ops
->session_key
) {
164 return NT_STATUS_NOT_IMPLEMENTED
;
167 return gensec_security
->ops
->session_key(gensec_security
, mem_ctx
, session_key
);
171 * Return the credentials of a logged on user, including session keys
174 * Only valid after a successful authentication
176 * May only be called once per authentication.
180 _PUBLIC_ NTSTATUS
gensec_session_info(struct gensec_security
*gensec_security
,
182 struct auth_session_info
**session_info
)
184 if (!gensec_security
->ops
->session_info
) {
185 return NT_STATUS_NOT_IMPLEMENTED
;
187 return gensec_security
->ops
->session_info(gensec_security
, mem_ctx
, session_info
);
190 _PUBLIC_
void gensec_set_max_update_size(struct gensec_security
*gensec_security
,
191 uint32_t max_update_size
)
193 gensec_security
->max_update_size
= max_update_size
;
196 _PUBLIC_
size_t gensec_max_update_size(struct gensec_security
*gensec_security
)
198 if (gensec_security
->max_update_size
== 0) {
202 return gensec_security
->max_update_size
;
206 * Next state function for the GENSEC state machine
208 * @param gensec_security GENSEC State
209 * @param out_mem_ctx The TALLOC_CTX for *out to be allocated on
210 * @param in The request, as a DATA_BLOB
211 * @param out The reply, as an talloc()ed DATA_BLOB, on *out_mem_ctx
212 * @return Error, MORE_PROCESSING_REQUIRED if a reply is sent,
213 * or NT_STATUS_OK if the user is authenticated.
216 _PUBLIC_ NTSTATUS
gensec_update(struct gensec_security
*gensec_security
, TALLOC_CTX
*out_mem_ctx
,
217 struct tevent_context
*ev
,
218 const DATA_BLOB in
, DATA_BLOB
*out
)
221 const struct gensec_security_ops
*ops
= gensec_security
->ops
;
222 TALLOC_CTX
*frame
= NULL
;
223 struct tevent_req
*subreq
= NULL
;
226 if (ops
->update_send
== NULL
) {
228 status
= ops
->update(gensec_security
, out_mem_ctx
,
230 if (!NT_STATUS_IS_OK(status
)) {
235 * Because callers using the
236 * gensec_start_mech_by_auth_type() never call
237 * gensec_want_feature(), it isn't sensible for them
238 * to have to call gensec_have_feature() manually, and
239 * these are not points of negotiation, but are
240 * asserted by the client
242 switch (gensec_security
->dcerpc_auth_level
) {
243 case DCERPC_AUTH_LEVEL_INTEGRITY
:
244 if (!gensec_have_feature(gensec_security
, GENSEC_FEATURE_SIGN
)) {
245 DEBUG(0,("Did not manage to negotiate mandetory feature "
246 "SIGN for dcerpc auth_level %u\n",
247 gensec_security
->dcerpc_auth_level
));
248 return NT_STATUS_ACCESS_DENIED
;
251 case DCERPC_AUTH_LEVEL_PRIVACY
:
252 if (!gensec_have_feature(gensec_security
, GENSEC_FEATURE_SIGN
)) {
253 DEBUG(0,("Did not manage to negotiate mandetory feature "
254 "SIGN for dcerpc auth_level %u\n",
255 gensec_security
->dcerpc_auth_level
));
256 return NT_STATUS_ACCESS_DENIED
;
258 if (!gensec_have_feature(gensec_security
, GENSEC_FEATURE_SEAL
)) {
259 DEBUG(0,("Did not manage to negotiate mandetory feature "
260 "SEAL for dcerpc auth_level %u\n",
261 gensec_security
->dcerpc_auth_level
));
262 return NT_STATUS_ACCESS_DENIED
;
272 frame
= talloc_stackframe();
274 subreq
= ops
->update_send(frame
, ev
, gensec_security
, in
);
275 if (subreq
== NULL
) {
276 status
= NT_STATUS_NO_MEMORY
;
279 ok
= tevent_req_poll_ntstatus(subreq
, ev
, &status
);
283 status
= ops
->update_recv(subreq
, out_mem_ctx
, out
);
289 struct gensec_update_state
{
290 const struct gensec_security_ops
*ops
;
291 struct tevent_req
*subreq
;
292 struct gensec_security
*gensec_security
;
296 * only for sync backends, we should remove this
297 * once all backends are async.
299 struct tevent_immediate
*im
;
303 static void gensec_update_async_trigger(struct tevent_context
*ctx
,
304 struct tevent_immediate
*im
,
306 static void gensec_update_subreq_done(struct tevent_req
*subreq
);
309 * Next state function for the GENSEC state machine async version
311 * @param mem_ctx The memory context for the request
312 * @param ev The event context for the request
313 * @param gensec_security GENSEC State
314 * @param in The request, as a DATA_BLOB
316 * @return The request handle or NULL on no memory failure
319 _PUBLIC_
struct tevent_req
*gensec_update_send(TALLOC_CTX
*mem_ctx
,
320 struct tevent_context
*ev
,
321 struct gensec_security
*gensec_security
,
324 struct tevent_req
*req
;
325 struct gensec_update_state
*state
= NULL
;
327 req
= tevent_req_create(mem_ctx
, &state
,
328 struct gensec_update_state
);
333 state
->ops
= gensec_security
->ops
;
334 state
->gensec_security
= gensec_security
;
336 if (state
->ops
->update_send
== NULL
) {
338 state
->im
= tevent_create_immediate(state
);
339 if (tevent_req_nomem(state
->im
, req
)) {
340 return tevent_req_post(req
, ev
);
343 tevent_schedule_immediate(state
->im
, ev
,
344 gensec_update_async_trigger
,
350 state
->subreq
= state
->ops
->update_send(state
, ev
, gensec_security
, in
);
351 if (tevent_req_nomem(state
->subreq
, req
)) {
352 return tevent_req_post(req
, ev
);
355 tevent_req_set_callback(state
->subreq
,
356 gensec_update_subreq_done
,
362 static void gensec_update_async_trigger(struct tevent_context
*ctx
,
363 struct tevent_immediate
*im
,
366 struct tevent_req
*req
=
367 talloc_get_type_abort(private_data
, struct tevent_req
);
368 struct gensec_update_state
*state
=
369 tevent_req_data(req
, struct gensec_update_state
);
372 status
= state
->ops
->update(state
->gensec_security
, state
, ctx
,
373 state
->in
, &state
->out
);
374 if (tevent_req_nterror(req
, status
)) {
378 tevent_req_done(req
);
381 static void gensec_update_subreq_done(struct tevent_req
*subreq
)
383 struct tevent_req
*req
=
384 tevent_req_callback_data(subreq
,
386 struct gensec_update_state
*state
=
388 struct gensec_update_state
);
391 state
->subreq
= NULL
;
393 status
= state
->ops
->update_recv(subreq
, state
, &state
->out
);
395 if (tevent_req_nterror(req
, status
)) {
400 * Because callers using the
401 * gensec_start_mech_by_authtype() never call
402 * gensec_want_feature(), it isn't sensible for them
403 * to have to call gensec_have_feature() manually, and
404 * these are not points of negotiation, but are
405 * asserted by the client
407 switch (state
->gensec_security
->dcerpc_auth_level
) {
408 case DCERPC_AUTH_LEVEL_INTEGRITY
:
409 if (!gensec_have_feature(state
->gensec_security
, GENSEC_FEATURE_SIGN
)) {
410 DEBUG(0,("Did not manage to negotiate mandetory feature "
411 "SIGN for dcerpc auth_level %u\n",
412 state
->gensec_security
->dcerpc_auth_level
));
413 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
417 case DCERPC_AUTH_LEVEL_PRIVACY
:
418 if (!gensec_have_feature(state
->gensec_security
, GENSEC_FEATURE_SIGN
)) {
419 DEBUG(0,("Did not manage to negotiate mandetory feature "
420 "SIGN for dcerpc auth_level %u\n",
421 state
->gensec_security
->dcerpc_auth_level
));
422 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
425 if (!gensec_have_feature(state
->gensec_security
, GENSEC_FEATURE_SEAL
)) {
426 DEBUG(0,("Did not manage to negotiate mandetory feature "
427 "SEAL for dcerpc auth_level %u\n",
428 state
->gensec_security
->dcerpc_auth_level
));
429 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
437 tevent_req_done(req
);
441 * Next state function for the GENSEC state machine
443 * @param req request state
444 * @param out_mem_ctx The TALLOC_CTX for *out to be allocated on
445 * @param out The reply, as an talloc()ed DATA_BLOB, on *out_mem_ctx
446 * @return Error, MORE_PROCESSING_REQUIRED if a reply is sent,
447 * or NT_STATUS_OK if the user is authenticated.
449 _PUBLIC_ NTSTATUS
gensec_update_recv(struct tevent_req
*req
,
450 TALLOC_CTX
*out_mem_ctx
,
453 struct gensec_update_state
*state
=
454 tevent_req_data(req
, struct gensec_update_state
);
457 if (tevent_req_is_nterror(req
, &status
)) {
458 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
459 tevent_req_received(req
);
463 status
= NT_STATUS_OK
;
467 talloc_steal(out_mem_ctx
, out
->data
);
469 tevent_req_received(req
);
474 * Set the requirement for a certain feature on the connection
478 _PUBLIC_
void gensec_want_feature(struct gensec_security
*gensec_security
,
481 if (!gensec_security
->ops
|| !gensec_security
->ops
->want_feature
) {
482 gensec_security
->want_features
|= feature
;
485 gensec_security
->ops
->want_feature(gensec_security
, feature
);
489 * Check the requirement for a certain feature on the connection
493 _PUBLIC_
bool gensec_have_feature(struct gensec_security
*gensec_security
,
496 if (!gensec_security
->ops
->have_feature
) {
500 /* We might 'have' features that we don't 'want', because the
501 * other end demanded them, or we can't neotiate them off */
502 return gensec_security
->ops
->have_feature(gensec_security
, feature
);
505 _PUBLIC_ NTTIME
gensec_expire_time(struct gensec_security
*gensec_security
)
507 if (!gensec_security
->ops
->expire_time
) {
508 return GENSEC_EXPIRE_TIME_INFINITY
;
511 return gensec_security
->ops
->expire_time(gensec_security
);
514 * Return the credentials structure associated with a GENSEC context
518 _PUBLIC_
struct cli_credentials
*gensec_get_credentials(struct gensec_security
*gensec_security
)
520 if (!gensec_security
) {
523 return gensec_security
->credentials
;
527 * Set the target service (such as 'http' or 'host') on a GENSEC context - ensures it is talloc()ed
531 _PUBLIC_ NTSTATUS
gensec_set_target_service(struct gensec_security
*gensec_security
, const char *service
)
533 gensec_security
->target
.service
= talloc_strdup(gensec_security
, service
);
534 if (!gensec_security
->target
.service
) {
535 return NT_STATUS_NO_MEMORY
;
540 _PUBLIC_
const char *gensec_get_target_service(struct gensec_security
*gensec_security
)
542 if (gensec_security
->target
.service
) {
543 return gensec_security
->target
.service
;
550 * Set the target hostname (suitable for kerberos resolutation) on a GENSEC context - ensures it is talloc()ed
554 _PUBLIC_ NTSTATUS
gensec_set_target_hostname(struct gensec_security
*gensec_security
, const char *hostname
)
556 gensec_security
->target
.hostname
= talloc_strdup(gensec_security
, hostname
);
557 if (hostname
&& !gensec_security
->target
.hostname
) {
558 return NT_STATUS_NO_MEMORY
;
563 _PUBLIC_
const char *gensec_get_target_hostname(struct gensec_security
*gensec_security
)
565 /* We allow the target hostname to be overriden for testing purposes */
566 if (gensec_security
->settings
->target_hostname
) {
567 return gensec_security
->settings
->target_hostname
;
570 if (gensec_security
->target
.hostname
) {
571 return gensec_security
->target
.hostname
;
574 /* We could add use the 'set sockaddr' call, and do a reverse
575 * lookup, but this would be both insecure (compromising the
576 * way kerberos works) and add DNS timeouts */
581 * Set (and copy) local and peer socket addresses onto a socket
582 * context on the GENSEC context.
584 * This is so that kerberos can include these addresses in
585 * cryptographic tokens, to avoid certain attacks.
589 * @brief Set the local gensec address.
591 * @param gensec_security The gensec security context to use.
593 * @param remote The local address to set.
595 * @return On success NT_STATUS_OK is returned or an NT_STATUS
598 _PUBLIC_ NTSTATUS
gensec_set_local_address(struct gensec_security
*gensec_security
,
599 const struct tsocket_address
*local
)
601 TALLOC_FREE(gensec_security
->local_addr
);
607 gensec_security
->local_addr
= tsocket_address_copy(local
, gensec_security
);
608 if (gensec_security
->local_addr
== NULL
) {
609 return NT_STATUS_NO_MEMORY
;
616 * @brief Set the remote gensec address.
618 * @param gensec_security The gensec security context to use.
620 * @param remote The remote address to set.
622 * @return On success NT_STATUS_OK is returned or an NT_STATUS
625 _PUBLIC_ NTSTATUS
gensec_set_remote_address(struct gensec_security
*gensec_security
,
626 const struct tsocket_address
*remote
)
628 TALLOC_FREE(gensec_security
->remote_addr
);
630 if (remote
== NULL
) {
634 gensec_security
->remote_addr
= tsocket_address_copy(remote
, gensec_security
);
635 if (gensec_security
->remote_addr
== NULL
) {
636 return NT_STATUS_NO_MEMORY
;
643 * @brief Get the local address from a gensec security context.
645 * @param gensec_security The security context to get the address from.
647 * @return The address as tsocket_address which could be NULL if
650 _PUBLIC_
const struct tsocket_address
*gensec_get_local_address(struct gensec_security
*gensec_security
)
652 if (gensec_security
== NULL
) {
655 return gensec_security
->local_addr
;
659 * @brief Get the remote address from a gensec security context.
661 * @param gensec_security The security context to get the address from.
663 * @return The address as tsocket_address which could be NULL if
666 _PUBLIC_
const struct tsocket_address
*gensec_get_remote_address(struct gensec_security
*gensec_security
)
668 if (gensec_security
== NULL
) {
671 return gensec_security
->remote_addr
;
675 * Set the target principal (assuming it it known, say from the SPNEGO reply)
676 * - ensures it is talloc()ed
680 _PUBLIC_ NTSTATUS
gensec_set_target_principal(struct gensec_security
*gensec_security
, const char *principal
)
682 gensec_security
->target
.principal
= talloc_strdup(gensec_security
, principal
);
683 if (!gensec_security
->target
.principal
) {
684 return NT_STATUS_NO_MEMORY
;
689 _PUBLIC_
const char *gensec_get_target_principal(struct gensec_security
*gensec_security
)
691 if (gensec_security
->target
.principal
) {
692 return gensec_security
->target
.principal
;