lib/kadm5: find_db_spec do not leak 'info'
[heimdal.git] / kdc / token_validator.c
blob858fdfa7b2142fc1e1e37a4c16c494c64aa2afa6
1 /*
2 * Copyright (c) 2019 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "kdc_locl.h"
35 #include "token_validator_plugin.h"
37 struct plctx {
38 const char *realm;
39 const char *token_kind;
40 krb5_data *token;
41 const char * const *audiences;
42 size_t naudiences;
43 krb5_boolean result;
44 krb5_principal actual_principal;
45 krb5_times token_times;
48 static krb5_error_code KRB5_LIB_CALL
49 plcallback(krb5_context context, const void *plug, void *plugctx, void *userctx)
51 const krb5plugin_token_validator_ftable *validator = plug;
52 krb5_error_code ret;
53 struct plctx *plctx = userctx;
55 ret = validator->validate(plugctx, context, plctx->realm,
56 plctx->token_kind, plctx->token,
57 plctx->audiences, plctx->naudiences,
58 &plctx->result, &plctx->actual_principal,
59 &plctx->token_times);
60 if (ret) {
61 krb5_free_principal(context, plctx->actual_principal);
62 plctx->actual_principal = NULL;
64 return ret;
67 static const char *plugin_deps[] = { "krb5", NULL };
69 static struct heim_plugin_data token_validator_data = {
70 "kdc",
71 KDC_PLUGIN_BEARER,
73 plugin_deps,
74 krb5_get_instance
78 * Invoke a plugin to validate a JWT/SAML/OIDC token and partially-evaluate
79 * access control.
81 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL
82 kdc_validate_token(krb5_context context,
83 const char *realm,
84 const char *token_kind,
85 krb5_data *token,
86 const char * const *audiences,
87 size_t naudiences,
88 krb5_principal *actual_principal,
89 krb5_times *token_times)
91 krb5_error_code ret;
92 struct plctx ctx;
94 memset(&ctx, 0, sizeof(ctx));
95 ctx.realm = realm;
96 ctx.token_kind = token_kind;
97 ctx.token = token;
98 ctx.audiences = audiences;
99 ctx.naudiences = naudiences;
100 ctx.result = FALSE;
101 ctx.actual_principal = NULL;
103 krb5_clear_error_message(context);
104 ret = _krb5_plugin_run_f(context, &token_validator_data, 0, &ctx,
105 plcallback);
106 if (ret == 0 && ctx.result && actual_principal) {
107 *actual_principal = ctx.actual_principal;
108 ctx.actual_principal = NULL;
111 if (token_times)
112 *token_times = ctx.token_times;
114 krb5_free_principal(context, ctx.actual_principal);
115 if (ret)
116 krb5_prepend_error_message(context, ret, "bearer token validation "
117 "failed: ");
118 else if (!ctx.result)
119 krb5_set_error_message(context, ret = EACCES,
120 "bearer token validation failed");
121 return ret;