Skip to content

Commit 22f73b3

Browse files
committed
use the vhost config check for generating the passphrase when not set
Signed-off-by: Hans Zandbelt <[email protected]>
1 parent 57a38d0 commit 22f73b3

File tree

11 files changed

+51
-47
lines changed

11 files changed

+51
-47
lines changed

src/cache/common.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ apr_byte_t oidc_cache_get(request_rec *r, const char *section, const char *key,
314314

315315
oidc_debug(r, "enter: %s (section=%s, decrypt=%d, type=%s)", key, s_section, encrypted, cfg->cache.impl->name);
316316

317-
s_secret = oidc_cfg_crypto_passphrase_secret1_get(cfg, r);
317+
s_secret = oidc_cfg_crypto_passphrase_secret1_get(cfg);
318318
if (oidc_cache_get_key(r, key, s_secret, encrypted, &s_key) == FALSE)
319319
goto end;
320320

@@ -385,12 +385,12 @@ apr_byte_t oidc_cache_set(request_rec *r, const char *section, const char *key,
385385
value ? (int)_oidc_strlen(value) : 0, encrypted, apr_time_sec(expiry - apr_time_now()),
386386
cfg->cache.impl->name);
387387

388-
if (oidc_cache_get_key(r, key, oidc_cfg_crypto_passphrase_secret1_get(cfg, r), encrypted, &s_key) == FALSE)
388+
if (oidc_cache_get_key(r, key, oidc_cfg_crypto_passphrase_secret1_get(cfg), encrypted, &s_key) == FALSE)
389389
goto end;
390390

391391
/* see if we need to encrypt */
392392
if ((encrypted == 1) && (value != NULL)) {
393-
if (oidc_cache_crypto_encrypt(r, value, oidc_cfg_crypto_passphrase_get(cfg, r), &encoded) == FALSE)
393+
if (oidc_cache_crypto_encrypt(r, value, oidc_cfg_crypto_passphrase_get(cfg), &encoded) == FALSE)
394394
goto end;
395395
value = encoded;
396396
}

src/cfg/cfg.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,18 @@ const char *oidc_cmd_crypto_passphrase_set(cmd_parms *cmd, void *struct_ptr, con
108108
return rv;
109109
}
110110

111-
const oidc_crypto_passphrase_t *oidc_cfg_crypto_passphrase_get(oidc_cfg_t *cfg, request_rec *r) {
112-
// make sure secret1 is set
113-
oidc_cfg_crypto_passphrase_secret1_get(cfg, r);
111+
const oidc_crypto_passphrase_t *oidc_cfg_crypto_passphrase_get(oidc_cfg_t *cfg) {
114112
return &cfg->crypto_passphrase;
115113
}
116114

117-
const char *oidc_cfg_crypto_passphrase_secret1_get(oidc_cfg_t *cfg, request_rec *r) {
118-
if (cfg->crypto_passphrase.secret1 == NULL)
119-
oidc_util_rand_str(r, (char **)&cfg->crypto_passphrase.secret1, 32);
115+
const char *oidc_cfg_crypto_passphrase_secret1_get(oidc_cfg_t *cfg) {
120116
return cfg->crypto_passphrase.secret1;
121117
}
122118

119+
void oidc_cfg_crypto_passphrase_secret1_set(oidc_cfg_t *cfg, const char *secret) {
120+
cfg->crypto_passphrase.secret1 = secret;
121+
}
122+
123123
const char *oidc_cfg_crypto_passphrase_secret2_get(oidc_cfg_t *cfg) {
124124
return cfg->crypto_passphrase.secret2;
125125
}

src/cfg/cfg.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ void oidc_cfg_child_init(apr_pool_t *pool, oidc_cfg_t *cfg, server_rec *s);
195195
void oidc_cfg_cleanup_child(oidc_cfg_t *cfg, server_rec *s);
196196
const char *oidc_cfg_string_list_add(apr_pool_t *pool, apr_array_header_t **list, const char *arg);
197197
const char *oidc_cfg_endpoint_auth_set(apr_pool_t *pool, oidc_cfg_t *cfg, const char *arg, char **auth, char **alg);
198+
void oidc_cfg_crypto_passphrase_secret1_set(oidc_cfg_t *cfg, const char *secret);
198199

199200
#define OIDC_CFG_MEMBER_FUNC_NAME(member, type, method) oidc_##type##_##member##_##method
200201

@@ -209,9 +210,8 @@ const char *oidc_cfg_endpoint_auth_set(apr_pool_t *pool, oidc_cfg_t *cfg, const
209210

210211
OIDC_CFG_MEMBER_FUNCS_DECL(delete_oldest_state_cookies, int)
211212
OIDC_CFG_MEMBER_FUNCS_DECL(action_on_userinfo_error, oidc_on_error_action_t)
212-
OIDC_CMD_MEMBER_FUNC_DECL(crypto_passphrase_secret1, const char *);
213-
const char *oidc_cfg_crypto_passphrase_secret1_get(oidc_cfg_t *cfg, request_rec *r);
214-
OIDC_CFG_MEMBER_FUNC_GET_DECL(crypto_passphrase_secret2, const char *)
213+
OIDC_CFG_MEMBER_FUNCS_DECL(crypto_passphrase_secret1, const char *)
214+
OIDC_CFG_MEMBER_FUNCS_DECL(crypto_passphrase_secret2, const char *)
215215
OIDC_CFG_MEMBER_FUNCS_DECL(refresh_mutex, oidc_cache_mutex_t *)
216216
OIDC_CFG_MEMBER_FUNCS_DECL(store_id_token, int)
217217
OIDC_CFG_MEMBER_FUNCS_DECL(post_preserve_template, const char *)
@@ -249,11 +249,9 @@ OIDC_CFG_MEMBER_FUNCS_DECL(dpop_api_enabled, int)
249249

250250
// 2 args
251251
OIDC_CFG_MEMBER_FUNCS_DECL(post_preserve_templates, const char *, const char *)
252+
OIDC_CFG_MEMBER_FUNCS_DECL(crypto_passphrase, const oidc_crypto_passphrase_t *, const char *)
252253
OIDC_CFG_MEMBER_FUNCS_DECL(max_number_of_state_cookies, int, const char *)
253254

254-
const char *oidc_cmd_crypto_passphrase_set(cmd_parms *, void *, const char *, const char *);
255-
const oidc_crypto_passphrase_t *oidc_cfg_crypto_passphrase_get(oidc_cfg_t *cfg, request_rec *r);
256-
257255
// 3 args
258256
OIDC_CFG_MEMBER_FUNCS_DECL(cookie_same_site_session, oidc_samesite_cookie_t, const char *, const char *)
259257
OIDC_CFG_MEMBER_FUNC_GET_DECL(cookie_same_site_state, oidc_samesite_cookie_t)

src/mod_auth_openidc.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,9 @@ static int oidc_config_check_vhost_config(apr_pool_t *pool, server_rec *s) {
15641564

15651565
oidc_sdebug(s, "enter");
15661566

1567+
if (oidc_cfg_crypto_passphrase_secret1_get(cfg) == NULL)
1568+
oidc_cfg_crypto_passphrase_secret1_set(cfg, oidc_util_rand_hex_str(NULL, s->process->pconf, 32));
1569+
15671570
if ((oidc_cfg_metadata_dir_get(cfg) != NULL) ||
15681571
(oidc_cfg_provider_issuer_get(oidc_cfg_provider_get(cfg)) != NULL) ||
15691572
(oidc_cfg_provider_metadata_url_get(oidc_cfg_provider_get(cfg)) != NULL)) {

src/proto/state.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ void oidc_proto_state_set_timestamp_now(oidc_proto_state_t *proto_state) {
237237
oidc_proto_state_t *oidc_proto_state_from_cookie(request_rec *r, oidc_cfg_t *c, const char *cookieValue) {
238238
char *s_payload = NULL;
239239
json_t *result = NULL;
240-
oidc_util_jwt_verify(r, oidc_cfg_crypto_passphrase_get(c, r), cookieValue, &s_payload);
240+
oidc_util_jwt_verify(r, oidc_cfg_crypto_passphrase_get(c), cookieValue, &s_payload);
241241
oidc_util_json_decode_object(r, s_payload, &result);
242242
return result;
243243
}
@@ -247,7 +247,7 @@ oidc_proto_state_t *oidc_proto_state_from_cookie(request_rec *r, oidc_cfg_t *c,
247247
*/
248248
char *oidc_proto_state_to_cookie(request_rec *r, oidc_cfg_t *c, oidc_proto_state_t *proto_state) {
249249
char *cookieValue = NULL;
250-
oidc_util_jwt_create(r, oidc_cfg_crypto_passphrase_get(c, r),
250+
oidc_util_jwt_create(r, oidc_cfg_crypto_passphrase_get(c),
251251
oidc_util_json_encode(r->pool, proto_state, JSON_COMPACT), &cookieValue);
252252
return cookieValue;
253253
}

src/session.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ static apr_byte_t oidc_session_encode(request_rec *r, oidc_cfg_t *c, oidc_sessio
7171
if (encrypt == FALSE) {
7272
*s_value = oidc_util_json_encode(r->pool, z->state, JSON_COMPACT);
7373
return (*s_value != NULL);
74-
} else if (oidc_cfg_crypto_passphrase_secret1_get(c, r) == NULL) {
74+
} else if (oidc_cfg_crypto_passphrase_secret1_get(c) == NULL) {
7575
oidc_error(r, "cannot encrypt session state because " OIDCCryptoPassphrase " is not set");
7676
return FALSE;
7777
}
7878

79-
if (oidc_util_jwt_create(r, oidc_cfg_crypto_passphrase_get(c, r),
79+
if (oidc_util_jwt_create(r, oidc_cfg_crypto_passphrase_get(c),
8080
oidc_util_json_encode(r->pool, z->state, JSON_COMPACT), s_value) == FALSE)
8181
return FALSE;
8282

@@ -92,12 +92,12 @@ static apr_byte_t oidc_session_decode(request_rec *r, oidc_cfg_t *c, oidc_sessio
9292

9393
if (encrypt == FALSE) {
9494
return oidc_util_json_decode_object(r, s_json, &z->state);
95-
} else if (oidc_cfg_crypto_passphrase_secret1_get(c, r) == NULL) {
95+
} else if (oidc_cfg_crypto_passphrase_secret1_get(c) == NULL) {
9696
oidc_error(r, "cannot decrypt session state because " OIDCCryptoPassphrase " is not set");
9797
return FALSE;
9898
}
9999

100-
if (oidc_util_jwt_verify(r, oidc_cfg_crypto_passphrase_get(c, r), s_json, &s_payload) == FALSE) {
100+
if (oidc_util_jwt_verify(r, oidc_cfg_crypto_passphrase_get(c), s_json, &s_payload) == FALSE) {
101101
oidc_error(r, "could not verify secure JWT: cache value possibly corrupted");
102102
return FALSE;
103103
}
@@ -111,7 +111,7 @@ static apr_byte_t oidc_session_decode(request_rec *r, oidc_cfg_t *c, oidc_sessio
111111
* generate a unique identifier for a session
112112
*/
113113
void oidc_session_id_new(request_rec *r, oidc_session_t *z) {
114-
z->uuid = oidc_util_rand_hex_str(r, OIDC_SESSION_ID_LEN);
114+
z->uuid = oidc_util_rand_hex_str(r, r->pool, OIDC_SESSION_ID_LEN);
115115
}
116116

117117
/*

src/util/random.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,11 @@ static apr_byte_t _oidc_util_rand_bytes(request_rec *r, unsigned char *buf, apr_
111111
#else
112112
const char *gen = DEV_RANDOM;
113113
#endif
114-
oidc_debug(r, "use [%s] for generating %" APR_SIZE_T_FMT " random bytes", gen, len);
114+
if (r)
115+
oidc_debug(r, "use [%s] for generating %" APR_SIZE_T_FMT " random bytes", gen, len);
115116
rv = _oidc_util_rand(buf, len);
116-
oidc_debug(r, "return: %d", rv);
117+
if (r)
118+
oidc_debug(r, "return: %d", rv);
117119
return rv;
118120
}
119121

@@ -145,11 +147,12 @@ apr_byte_t oidc_util_rand_str(request_rec *r, char **str, int len) {
145147
/*
146148
* generate a random string of (lowercase) hexadecimal characters, representing len bytes
147149
*/
148-
char *oidc_util_rand_hex_str(request_rec *r, int len) {
149-
unsigned char *bytes = apr_pcalloc(r->pool, len);
150+
char *oidc_util_rand_hex_str(request_rec *r, apr_pool_t *pool, int len) {
151+
unsigned char *bytes = apr_pcalloc(pool, len);
150152
if (_oidc_util_rand_bytes(r, bytes, len) != TRUE) {
151-
oidc_error(r, "_oidc_util_rand_bytes returned an error");
153+
if (r)
154+
oidc_error(r, "_oidc_util_rand_bytes returned an error");
152155
return NULL;
153156
}
154-
return oidc_util_hex_encode(r, bytes, len);
157+
return oidc_util_hex_encode(pool, bytes, len);
155158
}

src/util/util.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,10 +406,10 @@ void oidc_util_table_add_query_encoded_params(apr_pool_t *pool, apr_table_t *tab
406406
}
407407
}
408408

409-
char *oidc_util_hex_encode(request_rec *r, const unsigned char *bytes, unsigned int len) {
409+
char *oidc_util_hex_encode(apr_pool_t *pool, const unsigned char *bytes, unsigned int len) {
410410
char *s = "";
411411
for (int i = 0; i < len; i++)
412-
s = apr_psprintf(r->pool, "%s%02x", s, bytes[i]);
412+
s = apr_psprintf(pool, "%s%02x", s, bytes[i]);
413413
return s;
414414
}
415415

src/util/util.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ oidc_jwk_t *oidc_util_key_list_first(const apr_array_header_t *key_list, int kty
117117
// random.c
118118
unsigned int oidc_util_rand_int(unsigned int mod);
119119
apr_byte_t oidc_util_rand_str(request_rec *r, char **output, int len);
120-
char *oidc_util_rand_hex_str(request_rec *r, int len);
120+
char *oidc_util_rand_hex_str(request_rec *r, apr_pool_t *pool, int len);
121121

122122
// url.c
123123
const char *oidc_util_url_cur_host(request_rec *r, oidc_hdr_x_forwarded_t x_forwarded_headers);
@@ -131,7 +131,7 @@ apr_byte_t oidc_util_url_has_parameter(request_rec *r, const char *param);
131131
apr_byte_t oidc_util_url_parameter_get(request_rec *r, char *name, char **value);
132132

133133
// util.c
134-
char *oidc_util_hex_encode(request_rec *r, const unsigned char *bytes, unsigned int len);
134+
char *oidc_util_hex_encode(apr_pool_t *pool, const unsigned char *bytes, unsigned int len);
135135
apr_byte_t oidc_util_hash_string_and_base64url_encode(request_rec *r, const char *openssl_hash_algo, const char *input,
136136
char **output);
137137
int oidc_util_strnenvcmp(const char *a, const char *b, int len);

test/test_cache.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,20 @@ START_TEST(test_cache_encrypt_no_secret) {
169169

170170
/* get cfg and temporarily remove the secret to simulate missing passphrase */
171171
oidc_cfg_t *cfg = oidc_test_cfg_get();
172-
const char *old_secret = oidc_cfg_crypto_passphrase_secret1_get(cfg, r);
172+
const char *old_secret = oidc_cfg_crypto_passphrase_secret1_get(cfg);
173173
cfg->crypto_passphrase.secret1 = NULL;
174174
cfg->cache.encrypt = 1;
175175

176176
/* fail when value is too short and compression fails */
177177
ck_assert_int_eq(oidc_cache_set(r, OIDC_CACHE_SECTION_SESSION, "nokey", "v", expiry), FALSE);
178178

179-
/* do not fail when encryption is on but no secret is set (long enough value to compress) */
179+
/* fail when encryption is on but no secret is set (long enough value to compress) */
180180
ck_assert_int_eq(oidc_cache_set(r, OIDC_CACHE_SECTION_SESSION, "nokey",
181181
"vadadfsssssssssssssssssssssssssssssssssssssssssssssssssssssssss", expiry),
182-
TRUE);
182+
FALSE);
183183

184-
/* get should not fail because a secret should be generated */
185-
ck_assert_int_eq(oidc_cache_get(r, OIDC_CACHE_SECTION_SESSION, "nokey", &value), TRUE);
184+
/* fail because no secret is set */
185+
ck_assert_int_eq(oidc_cache_get(r, OIDC_CACHE_SECTION_SESSION, "nokey", &value), FALSE);
186186

187187
/* restore secret */
188188
cfg->crypto_passphrase.secret1 = (char *)old_secret;
@@ -204,7 +204,7 @@ START_TEST(test_cache_second_passphrase_retry) {
204204

205205
/* prepare cfg and secrets */
206206
oidc_cfg_t *cfg = oidc_test_cfg_get();
207-
const char *old_s1 = oidc_cfg_crypto_passphrase_secret1_get(cfg, r);
207+
const char *old_s1 = oidc_cfg_crypto_passphrase_secret1_get(cfg);
208208
const char *old_s2 = oidc_cfg_crypto_passphrase_secret2_get(cfg);
209209
int old_encrypt = cfg->cache.encrypt;
210210

@@ -275,7 +275,7 @@ START_TEST(test_cache_secret1_empty_secret2_fallback) {
275275

276276
/* prepare cfg and secrets */
277277
oidc_cfg_t *cfg = oidc_test_cfg_get();
278-
const char *old_s1 = oidc_cfg_crypto_passphrase_secret1_get(cfg, r);
278+
const char *old_s1 = oidc_cfg_crypto_passphrase_secret1_get(cfg);
279279
const char *old_s2 = oidc_cfg_crypto_passphrase_secret2_get(cfg);
280280
int old_encrypt = cfg->cache.encrypt;
281281

@@ -342,7 +342,7 @@ START_TEST(test_cache_compression_enabled_set_get) {
342342
/* verify encryption+compression works by trying to create a JWT with the current cfg secret */
343343
oidc_cfg_t *cfg = oidc_test_cfg_get();
344344
oidc_crypto_passphrase_t passphrase;
345-
passphrase.secret1 = oidc_cfg_crypto_passphrase_secret1_get(cfg, r);
345+
passphrase.secret1 = oidc_cfg_crypto_passphrase_secret1_get(cfg);
346346
passphrase.secret2 = oidc_cfg_crypto_passphrase_secret2_get(cfg);
347347
char *encoded = NULL;
348348
apr_byte_t forced_no_compress = FALSE;
@@ -373,7 +373,7 @@ START_TEST(test_cache_compression_enabled_second_passphrase) {
373373

374374
/* prepare cfg and secrets */
375375
oidc_cfg_t *cfg = oidc_test_cfg_get();
376-
const char *old_s1 = oidc_cfg_crypto_passphrase_secret1_get(cfg, r);
376+
const char *old_s1 = oidc_cfg_crypto_passphrase_secret1_get(cfg);
377377
const char *old_s2 = oidc_cfg_crypto_passphrase_secret2_get(cfg);
378378
int old_encrypt = cfg->cache.encrypt;
379379

@@ -384,7 +384,7 @@ START_TEST(test_cache_compression_enabled_second_passphrase) {
384384

385385
/* verify encryption+compression works for this cfg; fall back to no-compress if not */
386386
oidc_crypto_passphrase_t passphrase;
387-
passphrase.secret1 = oidc_cfg_crypto_passphrase_secret1_get(cfg, r);
387+
passphrase.secret1 = oidc_cfg_crypto_passphrase_secret1_get(cfg);
388388
passphrase.secret2 = oidc_cfg_crypto_passphrase_secret2_get(cfg);
389389
char *encoded = NULL;
390390
apr_byte_t forced_no_compress = FALSE;
@@ -433,7 +433,7 @@ START_TEST(test_cache_compression_enabled_empty_secret2_fallback) {
433433

434434
/* prepare cfg and secrets */
435435
oidc_cfg_t *cfg = oidc_test_cfg_get();
436-
const char *old_s1 = oidc_cfg_crypto_passphrase_secret1_get(cfg, r);
436+
const char *old_s1 = oidc_cfg_crypto_passphrase_secret1_get(cfg);
437437
const char *old_s2 = oidc_cfg_crypto_passphrase_secret2_get(cfg);
438438
int old_encrypt = cfg->cache.encrypt;
439439

@@ -443,7 +443,7 @@ START_TEST(test_cache_compression_enabled_empty_secret2_fallback) {
443443

444444
/* verify encryption+compression works; fall back to no-compress if not */
445445
oidc_crypto_passphrase_t passphrase2;
446-
passphrase2.secret1 = oidc_cfg_crypto_passphrase_secret1_get(cfg, r);
446+
passphrase2.secret1 = oidc_cfg_crypto_passphrase_secret1_get(cfg);
447447
passphrase2.secret2 = oidc_cfg_crypto_passphrase_secret2_get(cfg);
448448
char *encoded2 = NULL;
449449
apr_byte_t forced_no_compress = FALSE;

0 commit comments

Comments
 (0)