Coverage Report

Created: 2026-12-01 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mod_auth_openidc/src/proto/jwks.c
Line
Count
Source
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
20
/***************************************************************************
21
 * Copyright (C) 2017-2026 ZmartZone Holding BV
22
 * All rights reserved.
23
 *
24
 * DISCLAIMER OF WARRANTIES:
25
 *
26
 * THE SOFTWARE PROVIDED HEREUNDER IS PROVIDED ON AN "AS IS" BASIS, WITHOUT
27
 * ANY WARRANTIES OR REPRESENTATIONS EXPRESS, IMPLIED OR STATUTORY; INCLUDING,
28
 * WITHOUT LIMITATION, WARRANTIES OF QUALITY, PERFORMANCE, NONINFRINGEMENT,
29
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  NOR ARE THERE ANY
30
 * WARRANTIES CREATED BY A COURSE OR DEALING, COURSE OF PERFORMANCE OR TRADE
31
 * USAGE.  FURTHERMORE, THERE ARE NO WARRANTIES THAT THE SOFTWARE WILL MEET
32
 * YOUR NEEDS OR BE FREE FROM ERRORS, OR THAT THE OPERATION OF THE SOFTWARE
33
 * WILL BE UNINTERRUPTED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
34
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES HOWEVER CAUSED AND ON ANY THEORY OF
36
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
37
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
 *
40
 * @Author: Hans Zandbelt - hans.zandbelt@openidc.com
41
 */
42
43
#include "metadata.h"
44
#include "proto/proto.h"
45
#include "util/util.h"
46
47
/*
48
 * when no kid/x5t was specified, include the JWK in the result if it is usable for signing;
49
 * takes ownership of jwk (either inserts it into result or destroys it)
50
 */
51
static void oidc_proto_jwks_key_include_any(request_rec *r, oidc_jwk_t *jwk, const oidc_json_t *elem,
52
0
              apr_hash_t *result) {
53
0
  const char *use = oidc_json_string_value(oidc_json_object_get(elem, OIDC_JOSE_JWK_USE_STR));
54
0
  if ((use != NULL) && (_oidc_strcmp(use, OIDC_JOSE_JWK_SIG_STR) != 0)) {
55
0
    oidc_debug(r, "skipping key because of non-matching \"%s\": \"%s\"", OIDC_JOSE_JWK_USE_STR, use);
56
0
    oidc_jwk_destroy(jwk);
57
0
    return;
58
0
  }
59
60
0
  char *jwk_json = NULL;
61
0
  oidc_jose_error_t err;
62
0
  oidc_jwk_to_json(r->pool, jwk, &jwk_json, &err);
63
0
  oidc_debug(r, "no kid/x5t to match, include matching key type: %s", jwk_json);
64
  // a key without a "kid" gets its position as the name: nothing is ever removed from the list
65
0
  const char *name = (jwk->kid != NULL) ? jwk->kid : apr_psprintf(r->pool, "%d", apr_hash_count(result));
66
  // a key that is already there under that name, i.e. a "kid" that the provider repeats or that equals the
67
  // position of an earlier key without one, would be dropped from the list without being released
68
0
  oidc_jwk_t *replaced = apr_hash_get(result, name, APR_HASH_KEY_STRING);
69
0
  if (replaced != NULL) {
70
0
    apr_hash_set(result, name, APR_HASH_KEY_STRING, NULL);
71
0
    oidc_jwk_destroy(replaced);
72
0
  }
73
0
  apr_hash_set(result, name, APR_HASH_KEY_STRING, jwk);
74
0
}
75
76
/*
77
 * try a single JWKS entry against the JWT header;
78
 * returns TRUE when a specific kid/x5t match was found so the caller can stop iterating
79
 */
80
static apr_byte_t oidc_proto_jwks_key_apply(request_rec *r, oidc_jwt_t *jwt, const oidc_json_t *elem, const char *x5t,
81
0
              apr_hash_t *result) {
82
0
  oidc_jwk_t *jwk = NULL;
83
0
  oidc_jose_error_t err;
84
0
  char *jwk_json = NULL;
85
0
  char *s_x5t = NULL;
86
87
0
  if (oidc_jwk_parse_json(r->pool, elem, &jwk, &err) == FALSE) {
88
0
    oidc_warn(r, "oidc_jwk_parse_json failed: %s", oidc_jose_e2s(r->pool, err));
89
0
    return FALSE;
90
0
  }
91
92
  /* skip keys whose type does not match the JWT algorithm */
93
0
  if (oidc_jwt_alg2kty(jwt) != jwk->kty) {
94
0
    oidc_debug(r,
95
0
         "skipping non matching kty=%d for kid=%s because it doesn't match requested kty=%d, kid=%s",
96
0
         jwk->kty, jwk->kid, oidc_jwt_alg2kty(jwt), jwt->header.kid);
97
0
    oidc_jwk_destroy(jwk);
98
0
    return FALSE;
99
0
  }
100
101
  /* no specific kid/x5t requested: include any sig-usable key with a matching type */
102
0
  if ((jwt->header.kid == NULL) && (x5t == NULL)) {
103
0
    oidc_proto_jwks_key_include_any(r, jwk, elem, result);
104
0
    return FALSE;
105
0
  }
106
107
  /* compare the requested kid against the current element */
108
0
  if ((jwt->header.kid != NULL) && (jwk->kid != NULL) && (_oidc_strcmp(jwt->header.kid, jwk->kid) == 0)) {
109
0
    oidc_jwk_to_json(r->pool, jwk, &jwk_json, &err);
110
0
    oidc_debug(r, "found matching kid: \"%s\" for jwk: %s", jwt->header.kid, jwk_json);
111
0
    apr_hash_set(result, jwt->header.kid, APR_HASH_KEY_STRING, jwk);
112
0
    return TRUE;
113
0
  }
114
115
  /* compare the requested thumbprint against the current element */
116
0
  oidc_json_object_get_string(r->pool, elem, OIDC_JOSE_JWK_X5T_STR, &s_x5t, NULL);
117
0
  if ((s_x5t != NULL) && (x5t != NULL) && (_oidc_strcmp(x5t, s_x5t) == 0)) {
118
0
    oidc_jwk_to_json(r->pool, jwk, &jwk_json, &err);
119
0
    oidc_debug(r, "found matching %s: \"%s\" for jwk: %s", OIDC_JOSE_JWK_X5T_STR, x5t, jwk_json);
120
0
    apr_hash_set(result, x5t, APR_HASH_KEY_STRING, jwk);
121
0
    return TRUE;
122
0
  }
123
124
  /* the right key type but no matching kid/x5t */
125
0
  oidc_jwk_destroy(jwk);
126
0
  return FALSE;
127
0
}
128
129
/*
130
 * get the key from the JWKs that corresponds with the key specified in the header
131
 */
132
static apr_byte_t oidc_proto_jwks_key_get(request_rec *r, oidc_jwt_t *jwt, const oidc_json_t *j_jwks,
133
0
            apr_hash_t *result) {
134
135
  /* get the (optional) thumbprint for comparison */
136
0
  const char *x5t = oidc_jwt_hdr_get(jwt, OIDC_JOSE_JWK_X5T_STR);
137
0
  oidc_debug(r, "search for kid \"%s\" or thumbprint x5t \"%s\"", jwt->header.kid, x5t);
138
139
  /* get the "keys" JSON array from the JWKs object */
140
0
  const oidc_json_t *keys = oidc_json_object_get(j_jwks, OIDC_JOSE_JWKS_KEYS_STR);
141
0
  if ((keys == NULL) || !(oidc_json_is_array(keys))) {
142
0
    oidc_error(r, "\"%s\" array element is not a JSON array", OIDC_JOSE_JWKS_KEYS_STR);
143
0
    return FALSE;
144
0
  }
145
146
0
  for (size_t i = 0; i < oidc_json_array_size(keys); i++) {
147
0
    if (oidc_proto_jwks_key_apply(r, jwt, oidc_json_array_get(keys, i), x5t, result) == TRUE)
148
0
      break;
149
0
  }
150
151
0
  return TRUE;
152
0
}
153
154
/*
155
 * get the keys from the (possibly cached) set of JWKs on the jwk_uri that corresponds with the key specified in the
156
 * header
157
 */
158
apr_byte_t oidc_proto_jwks_uri_keys(request_rec *r, oidc_cfg_t *cfg, oidc_jwt_t *jwt, const oidc_jwks_uri_t *jwks_uri,
159
0
            int ssl_validate_server, apr_hash_t *keys, apr_byte_t *force_refresh) {
160
161
0
  oidc_json_t *j_jwks = NULL;
162
163
  /* get the set of JSON Web Keys for this provider (possibly by downloading them from the specified
164
   * provider->jwk_uri) */
165
0
  oidc_metadata_jwks_get(r, cfg, jwks_uri, ssl_validate_server, &j_jwks, force_refresh);
166
0
  if (j_jwks == NULL) {
167
0
    oidc_error(r, "could not %s JSON Web Keys", *force_refresh ? "refresh" : "get");
168
0
    return FALSE;
169
0
  }
170
171
  /* Select by kid, or all keys when absent. Errors follow the same refresh path as a miss. */
172
0
  oidc_proto_jwks_key_get(r, jwt, j_jwks, keys);
173
174
  /* no need anymore for the parsed oidc_json_t contents, release the it */
175
0
  oidc_json_decref(j_jwks);
176
177
  /* if we've got no keys and we did not do a fresh download, then the cache may be stale */
178
0
  if ((apr_hash_count(keys) < 1) && (*force_refresh == FALSE)) {
179
180
    /* we did not get a key, but we have not refreshed the JWKs from the jwks_uri yet */
181
0
    oidc_warn(r, "could not find a key in the cached JSON Web Keys, doing a forced refresh in case keys "
182
0
           "were rolled over");
183
    /* get the set of JSON Web Keys forcing a fresh download from the specified JWKs URI */
184
0
    *force_refresh = TRUE;
185
0
    return oidc_proto_jwks_uri_keys(r, cfg, jwt, jwks_uri, ssl_validate_server, keys, force_refresh);
186
0
  }
187
188
0
  oidc_debug(r, "returning %d key(s) obtained from the (possibly cached) JWKs URI", apr_hash_count(keys));
189
190
0
  return TRUE;
191
0
}