Coverage Report

Created: 2026-09-01 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mod_auth_openidc/src/util/key.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 "util/util.h"
44
45
#include <openssl/evp.h>
46
47
/* iteration count for PBKDF2-HMAC-SHA256, per current OWASP guidance; this only runs once per
48
 * (re)start, in the single-threaded post_config phase -- see oidc_cfg_crypto_passphrase_post_config */
49
38
#define OIDC_UTIL_KEY_DERIVE_ITERATIONS 210000
50
/* fixed, non-secret, application-specific salt: it exists for domain separation (so a
51
 * precomputed table built against some other use of PBKDF2-HMAC-SHA256 does not apply here),
52
 * not to protect against an attacker who has the source code */
53
76
#define OIDC_UTIL_KEY_DERIVE_SALT "mod_auth_openidc-crypto-passphrase-v1"
54
55
/*
56
 * stretch an operator-supplied OIDCCryptoPassphrase value into "out_len" bytes of key material
57
 * using PBKDF2-HMAC-SHA256, so that a low-entropy passphrase cannot be brute-forced offline at
58
 * the speed of a single SHA-256 evaluation
59
 */
60
38
apr_byte_t oidc_util_key_derive_passphrase_key(const char *passphrase, unsigned char *out, apr_size_t out_len) {
61
38
  if ((passphrase == NULL) || (_oidc_strlen(passphrase) == 0))
62
0
    return FALSE;
63
38
  return (PKCS5_PBKDF2_HMAC(passphrase, (int)_oidc_strlen(passphrase),
64
38
          (const unsigned char *)OIDC_UTIL_KEY_DERIVE_SALT,
65
38
          (int)(sizeof(OIDC_UTIL_KEY_DERIVE_SALT) - 1), OIDC_UTIL_KEY_DERIVE_ITERATIONS,
66
38
          EVP_sha256(), (int)out_len, out) == 1)
67
38
       ? TRUE
68
38
       : FALSE;
69
38
}
70
71
/*
72
 * create a symmetric key from a client_secret
73
 */
74
apr_byte_t oidc_util_key_symmetric_create(request_rec *r, const char *client_secret, unsigned int r_key_len,
75
38.8k
            const char *hash_algo, apr_byte_t set_kid, oidc_jwk_t **jwk) {
76
38.8k
  oidc_jose_error_t err = {{'\0'}, 0, {'\0'}, {'\0'}};
77
38.8k
  const unsigned char *key = NULL;
78
38.8k
  unsigned int key_len;
79
80
38.8k
  if ((client_secret != NULL) && (_oidc_strlen(client_secret) > 0)) {
81
82
38.8k
    if (hash_algo == NULL) {
83
27.1k
      key = (const unsigned char *)client_secret;
84
27.1k
      key_len = (unsigned int)_oidc_strlen(client_secret);
85
27.1k
    } else {
86
      /* hash the client_secret first, this is OpenID Connect specific */
87
11.7k
      unsigned char *hashed = NULL;
88
11.7k
      if (oidc_jose_hash_bytes(r->pool, hash_algo, (const unsigned char *)client_secret,
89
11.7k
             (unsigned int)_oidc_strlen(client_secret), &hashed, &key_len,
90
11.7k
             &err) == FALSE)
91
0
        return FALSE;
92
11.7k
      key = hashed;
93
11.7k
    }
94
95
38.8k
    if ((key != NULL) && (key_len > 0)) {
96
38.8k
      if ((r_key_len != 0) && (key_len >= r_key_len))
97
4.40k
        key_len = r_key_len;
98
38.8k
      *jwk = oidc_jwk_create_symmetric_key(r->pool, NULL, key, key_len, set_kid, &err);
99
38.8k
    }
100
101
38.8k
    if (*jwk == NULL) {
102
0
      oidc_error(r, "could not create JWK from the provided secret: %s", oidc_jose_e2s(r->pool, err));
103
0
      return FALSE;
104
0
    }
105
38.8k
  }
106
107
38.8k
  return TRUE;
108
38.8k
}
109
110
/*
111
 * merge an array of JWKs and a JWK into a single hashtable
112
 */
113
25.7k
apr_hash_t *oidc_util_key_symmetric_merge(apr_pool_t *pool, const apr_array_header_t *keys, const oidc_jwk_t *jwk) {
114
25.7k
  apr_hash_t *result = apr_hash_make(pool);
115
25.7k
  const oidc_jwk_t *elem = NULL;
116
25.7k
  if (keys != NULL) {
117
17.9k
    for (int i = 0; i < keys->nelts; i++) {
118
0
      elem = APR_ARRAY_IDX(keys, i, const oidc_jwk_t *);
119
0
      if (elem->kid != NULL)
120
0
        apr_hash_set(result, elem->kid, APR_HASH_KEY_STRING, elem);
121
0
    }
122
17.9k
  }
123
25.7k
  if (jwk != NULL) {
124
25.7k
    apr_hash_set(result, jwk->kid, APR_HASH_KEY_STRING, jwk);
125
25.7k
  }
126
25.7k
  return result;
127
25.7k
}
128
129
/*
130
 * merge the provided array of keys (k2) into a hash table of keys (k1)
131
 */
132
1.83k
apr_hash_t *oidc_util_key_sets_merge(apr_pool_t *pool, apr_hash_t *k1, const apr_array_header_t *k2) {
133
1.83k
  apr_hash_t *rv = k1 ? apr_hash_copy(pool, k1) : apr_hash_make(pool);
134
1.83k
  const oidc_jwk_t *jwk = NULL;
135
1.83k
  if (k2 != NULL) {
136
0
    for (int i = 0; i < k2->nelts; i++) {
137
0
      jwk = APR_ARRAY_IDX(k2, i, const oidc_jwk_t *);
138
0
      if (jwk->kid != NULL)
139
0
        apr_hash_set(rv, jwk->kid, APR_HASH_KEY_STRING, jwk);
140
0
    }
141
0
  }
142
1.83k
  return rv;
143
1.83k
}
144
145
/*
146
 * merge two hash tables with key sets
147
 */
148
9.62k
apr_hash_t *oidc_util_key_sets_hash_merge(apr_pool_t *pool, apr_hash_t *k1, apr_hash_t *k2) {
149
9.62k
  if (k1 == NULL) {
150
0
    if (k2 == NULL)
151
0
      return apr_hash_make(pool);
152
0
    return k2;
153
0
  }
154
9.62k
  if (k2 == NULL)
155
0
    return k1;
156
9.62k
  return apr_hash_overlay(pool, k1, k2);
157
9.62k
}
158
159
/*
160
 * return the first JWK that matches a provided key type and use from an array of JWKs
161
 */
162
0
oidc_jwk_t *oidc_util_key_list_first(const apr_array_header_t *key_list, int kty, const char *use) {
163
0
  oidc_jwk_t *rv = NULL;
164
0
  oidc_jwk_t *jwk = NULL;
165
0
  for (int i = 0; key_list && (i < key_list->nelts); i++) {
166
0
    jwk = APR_ARRAY_IDX(key_list, i, oidc_jwk_t *);
167
0
    if ((kty != -1) && (jwk->kty != kty))
168
0
      continue;
169
0
    if ((use == NULL) || (jwk->use == NULL) || (_oidc_strncmp(jwk->use, use, _oidc_strlen(use)) == 0)) {
170
0
      rv = jwk;
171
0
      break;
172
0
    }
173
0
  }
174
0
  return rv;
175
0
}