/src/mod_auth_openidc/src/http.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 | | * Copyright (C) 2013-2017 Ping Identity Corporation |
23 | | * All rights reserved. |
24 | | * |
25 | | * DISCLAIMER OF WARRANTIES: |
26 | | * |
27 | | * THE SOFTWARE PROVIDED HEREUNDER IS PROVIDED ON AN "AS IS" BASIS, WITHOUT |
28 | | * ANY WARRANTIES OR REPRESENTATIONS EXPRESS, IMPLIED OR STATUTORY; INCLUDING, |
29 | | * WITHOUT LIMITATION, WARRANTIES OF QUALITY, PERFORMANCE, NONINFRINGEMENT, |
30 | | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. NOR ARE THERE ANY |
31 | | * WARRANTIES CREATED BY A COURSE OR DEALING, COURSE OF PERFORMANCE OR TRADE |
32 | | * USAGE. FURTHERMORE, THERE ARE NO WARRANTIES THAT THE SOFTWARE WILL MEET |
33 | | * YOUR NEEDS OR BE FREE FROM ERRORS, OR THAT THE OPERATION OF THE SOFTWARE |
34 | | * WILL BE UNINTERRUPTED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR |
35 | | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
36 | | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES HOWEVER CAUSED AND ON ANY THEORY OF |
37 | | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
38 | | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
39 | | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
40 | | * |
41 | | * @Author: Hans Zandbelt - hans.zandbelt@openidc.com |
42 | | */ |
43 | | |
44 | | #include <stddef.h> |
45 | | #ifndef WIN32 |
46 | | #include <unistd.h> |
47 | | #endif |
48 | | |
49 | | #include <apr_strings.h> |
50 | | |
51 | | #include <curl/curl.h> |
52 | | #include <openssl/opensslv.h> |
53 | | |
54 | | #include "cfg/dir.h" |
55 | | #include "const.h" |
56 | | #include "http.h" |
57 | | #include "http_int.h" |
58 | | #include "metrics.h" |
59 | | #include "mod_auth_openidc.h" |
60 | | #include "proto/proto.h" |
61 | | #include "util/util.h" |
62 | | #include "util/util_cfg.h" |
63 | | |
64 | | /* the reusable-handle pool lives at the bottom of this file */ |
65 | | static CURL *oidc_http_curl_acquire(void); |
66 | | static void oidc_http_curl_release(CURL *curl, apr_byte_t reuse); |
67 | | |
68 | | /* |
69 | | * URL-encode a string: percent-encode every byte outside of the RFC 3986 unreserved set, |
70 | | * matching curl_easy_escape which was used here before (but required a throwaway CURL handle |
71 | | * per call since handles must not be shared between threads) |
72 | | */ |
73 | 1.58k | char *oidc_http_url_encode(const request_rec *r, const char *str) { |
74 | 1.58k | static const char hex[] = "0123456789ABCDEF"; |
75 | 1.58k | char *rv = NULL; |
76 | 1.58k | char *p = NULL; |
77 | 1.58k | size_t len = 0; |
78 | | |
79 | 1.58k | if (str == NULL) |
80 | 0 | return ""; |
81 | | |
82 | 1.58k | len = _oidc_strlen(str); |
83 | | |
84 | | /* worst case each input byte expands to a 3-byte %XX sequence */ |
85 | 1.58k | rv = apr_palloc(r->pool, len * 3 + 1); |
86 | 1.58k | p = rv; |
87 | 28.3k | for (size_t i = 0; i < len; i++) { |
88 | 26.7k | const unsigned char c = (unsigned char)str[i]; |
89 | 26.7k | if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9')) || |
90 | 26.2k | (c == '-') || (c == '.') || (c == '_') || (c == '~')) { |
91 | 26.2k | *p++ = (char)c; |
92 | 26.2k | } else { |
93 | 495 | *p++ = '%'; |
94 | 495 | *p++ = hex[c >> 4]; |
95 | 495 | *p++ = hex[c & 0x0f]; |
96 | 495 | } |
97 | 26.7k | } |
98 | 1.58k | *p = '\0'; |
99 | | |
100 | 1.58k | return rv; |
101 | 1.58k | } |
102 | | |
103 | 0 | static int oidc_http_url_decode_hex_digit(const char c) { |
104 | 0 | if ((c >= '0') && (c <= '9')) |
105 | 0 | return c - '0'; |
106 | 0 | if ((c >= 'A') && (c <= 'F')) |
107 | 0 | return c - 'A' + 10; |
108 | 0 | if ((c >= 'a') && (c <= 'f')) |
109 | 0 | return c - 'a' + 10; |
110 | 0 | return -1; |
111 | 0 | } |
112 | | |
113 | | /* |
114 | | * URL-decode a string: form-decode "+" to space and percent-decode %XX sequences, copying |
115 | | * malformed/truncated %-sequences through literally, matching the curl_easy_unescape based |
116 | | * implementation that was used here before |
117 | | */ |
118 | 0 | char *oidc_http_url_decode(const request_rec *r, const char *str) { |
119 | 0 | char *rv = NULL; |
120 | 0 | char *p = NULL; |
121 | 0 | size_t i = 0; |
122 | 0 | size_t len = 0; |
123 | |
|
124 | 0 | if (str == NULL) |
125 | 0 | return ""; |
126 | | |
127 | 0 | len = _oidc_strlen(str); |
128 | |
|
129 | 0 | rv = apr_palloc(r->pool, len + 1); |
130 | 0 | p = rv; |
131 | 0 | while (i < len) { |
132 | 0 | const char c = str[i]; |
133 | 0 | if (c == '+') { |
134 | 0 | *p++ = ' '; |
135 | 0 | i++; |
136 | 0 | } else if (c == '%') { |
137 | | /* str[i + 1] is within bounds (at worst the NUL terminator); str[i + 2] is only |
138 | | * read when str[i + 1] is a hex digit and thus not the terminator */ |
139 | 0 | const int hi = oidc_http_url_decode_hex_digit(str[i + 1]); |
140 | 0 | const int lo = (hi >= 0) ? oidc_http_url_decode_hex_digit(str[i + 2]) : -1; |
141 | 0 | if (lo >= 0) { |
142 | 0 | *p++ = (char)((hi << 4) | lo); |
143 | 0 | i += 3; |
144 | 0 | } else { |
145 | 0 | *p++ = c; |
146 | 0 | i++; |
147 | 0 | } |
148 | 0 | } else { |
149 | 0 | *p++ = c; |
150 | 0 | i++; |
151 | 0 | } |
152 | 0 | } |
153 | 0 | *p = '\0'; |
154 | |
|
155 | 0 | return rv; |
156 | 0 | } |
157 | | |
158 | | /* the headers whose value is written to the debug log as it is: they carry no credential and no token */ |
159 | | static const char *const _oidc_http_hdr_log_plain[] = {OIDC_HTTP_HDR_ACCEPT, |
160 | | OIDC_HTTP_HDR_CACHE_CONTROL, |
161 | | OIDC_HTTP_HDR_CONTENT_LENGTH, |
162 | | OIDC_HTTP_HDR_CONTENT_TYPE, |
163 | | OIDC_HTTP_HDR_DPOP_NONCE, |
164 | | OIDC_HTTP_HDR_EXPIRES, |
165 | | OIDC_HTTP_HDR_FORWARDED, |
166 | | OIDC_HTTP_HDR_HOST, |
167 | | OIDC_HTTP_HDR_P3P, |
168 | | OIDC_HTTP_HDR_PRAGMA, |
169 | | OIDC_HTTP_HDR_SEC_FETCH_DEST, |
170 | | OIDC_HTTP_HDR_SEC_FETCH_MODE, |
171 | | OIDC_HTTP_HDR_TRACE_PARENT, |
172 | | OIDC_HTTP_HDR_TRACE_STATE, |
173 | | OIDC_HTTP_HDR_USER_AGENT, |
174 | | OIDC_HTTP_HDR_WWW_AUTHENTICATE, |
175 | | OIDC_HTTP_HDR_X_FORWARDED_FOR, |
176 | | OIDC_HTTP_HDR_X_FORWARDED_HOST, |
177 | | OIDC_HTTP_HDR_X_FORWARDED_PORT, |
178 | | OIDC_HTTP_HDR_X_FORWARDED_PROTO, |
179 | | OIDC_HTTP_HDR_X_FRAME_OPTIONS, |
180 | | OIDC_HTTP_HDR_X_REQUESTED_WITH, |
181 | | NULL}; |
182 | | |
183 | | /* what this module passes to the application under its own prefix that is not a token, or data from one */ |
184 | | static const char *const _oidc_http_hdr_log_plain_info[] = {OIDC_APP_INFO_SCOPE, OIDC_APP_INFO_ACCESS_TOKEN_TYPE, |
185 | | OIDC_APP_INFO_ACCESS_TOKEN_EXP, NULL}; |
186 | | |
187 | 693 | static apr_byte_t oidc_http_hdr_name_in(const char *name, const char *const *names) { |
188 | 8.71k | for (; *names != NULL; names++) |
189 | 8.41k | if (_oidc_strcasecmp(name, *names) == 0) |
190 | 396 | return TRUE; |
191 | 297 | return FALSE; |
192 | 693 | } |
193 | | |
194 | | /* |
195 | | * The value of a request or response header as it is written to the debug log. The helpers below log every |
196 | | * header that they read or set, which includes the Cookie and Set-Cookie headers with the session cookie, the |
197 | | * Authorization header, and the tokens that are passed to the application: values that OIDCDebugMaskSecrets |
198 | | * promises to keep out of the log. So unless that is Off, a value is only logged as it is for a header that is |
199 | | * known not to carry a credential or a token, for a claim, and, with the parameters that do carry one masked, |
200 | | * for a redirect; any other value is masked. |
201 | | */ |
202 | 693 | const char *oidc_http_hdr_value_for_log(const request_rec *r, const char *name, const char *value) { |
203 | 693 | const oidc_cfg_t *c = ap_get_module_config(r->server->module_config, &auth_openidc_module); |
204 | 693 | const char *claim_prefix = oidc_cfg_claim_prefix_get(c); |
205 | 693 | const size_t prefix_len = _oidc_strlen(OIDC_DEFAULT_HEADER_PREFIX); |
206 | | |
207 | 693 | if ((name == NULL) || (value == NULL) || (oidc_util_log_mask_secrets(r) == FALSE)) |
208 | 0 | return value; |
209 | | |
210 | 693 | if (oidc_http_hdr_name_in(name, _oidc_http_hdr_log_plain)) |
211 | 396 | return value; |
212 | | |
213 | | /* the tokens and what is derived from them are passed under the fixed prefix, which may be the claim |
214 | | * prefix as well */ |
215 | 297 | if (oidc_util_strnenvcmp(name, OIDC_DEFAULT_HEADER_PREFIX, (int)prefix_len) == 0) { |
216 | 0 | if (oidc_http_hdr_name_in(name + prefix_len, _oidc_http_hdr_log_plain_info)) |
217 | 0 | return value; |
218 | 0 | if (_oidc_strstr(name + prefix_len, "token") || _oidc_strstr(name + prefix_len, "jwt") || |
219 | 0 | _oidc_strstr(name + prefix_len, "userinfo")) |
220 | 0 | return oidc_util_mask_value(r, value); |
221 | 0 | } |
222 | | |
223 | | /* claims are logged as they were; without a prefix there is no telling them from any other header */ |
224 | 297 | if ((_oidc_strlen(claim_prefix) > 0) && |
225 | 297 | (oidc_util_strnenvcmp(name, claim_prefix, (int)_oidc_strlen(claim_prefix)) == 0)) |
226 | 0 | return value; |
227 | | |
228 | | /* a redirect is logged with the values of the parameters that carry a token or a code masked */ |
229 | 297 | if (_oidc_strcasecmp(name, OIDC_HTTP_HDR_LOCATION) == 0) { |
230 | 99 | const char *query = strchr(value, OIDC_CHAR_QUERY); |
231 | 99 | if (query == NULL) |
232 | 0 | return value; |
233 | 99 | return apr_pstrcat(r->pool, apr_pstrmemdup(r->pool, value, (apr_size_t)(query - value) + 1), |
234 | 99 | oidc_http_redact_body_for_log(r, query + 1), NULL); |
235 | 99 | } |
236 | | |
237 | 198 | return oidc_util_mask_value(r, value); |
238 | 297 | } |
239 | | |
240 | | /* |
241 | | * obtain a HTTP request header value |
242 | | */ |
243 | 1.73k | static const char *oidc_http_hdr_in_get(const request_rec *r, const char *name) { |
244 | 1.73k | const char *value = apr_table_get(r->headers_in, name); |
245 | 1.73k | if (value) |
246 | 1.73k | oidc_debug(r, "%s=%s", name, oidc_http_hdr_value_for_log(r, name, value)); |
247 | 1.73k | return value; |
248 | 1.73k | } |
249 | | |
250 | | /* |
251 | | * obtain the left-most element of a multi-valued HTTP header value |
252 | | */ |
253 | 396 | static const char *oidc_http_hdr_in_get_left_most_only(const request_rec *r, const char *name, const char *separator) { |
254 | 396 | char *last = NULL; |
255 | 396 | const char *value = oidc_http_hdr_in_get(r, name); |
256 | 396 | if (value) |
257 | 0 | return apr_strtok(apr_pstrdup(r->pool, value), separator, &last); |
258 | 396 | return NULL; |
259 | 396 | } |
260 | | |
261 | | /* |
262 | | * check if a multi-valued HTTP request header contains a specified value |
263 | | */ |
264 | | static apr_byte_t oidc_http_hdr_in_contains(const request_rec *r, const char *name, const char *separator, |
265 | 372 | const char postfix_separator, const char *needle) { |
266 | 372 | char *ctx = NULL; |
267 | 372 | const char *elem = NULL; |
268 | 372 | const char *value = oidc_http_hdr_in_get(r, name); |
269 | 372 | apr_byte_t rc = FALSE; |
270 | 372 | if (value) { |
271 | 99 | elem = apr_strtok(apr_pstrdup(r->pool, value), separator, &ctx); |
272 | 99 | while (elem != NULL) { |
273 | 99 | while (*elem == OIDC_CHAR_SPACE) |
274 | 0 | elem++; |
275 | 99 | if ((_oidc_strncmp(elem, needle, _oidc_strlen(needle)) == 0) && |
276 | 99 | ((elem[_oidc_strlen(needle)] == '\0') || |
277 | 99 | (elem[_oidc_strlen(needle)] == postfix_separator))) { |
278 | 99 | rc = TRUE; |
279 | 99 | break; |
280 | 99 | } |
281 | 0 | elem = apr_strtok(NULL, separator, &ctx); |
282 | 0 | } |
283 | 99 | } |
284 | 372 | return rc; |
285 | 372 | } |
286 | | |
287 | | /* |
288 | | * copy a header value with CR/LF replaced, to prevent header injection |
289 | | */ |
290 | 99 | static char *oidc_http_hdr_value_sanitize(const request_rec *r, const char *value) { |
291 | 99 | char *s_value = apr_pstrdup(r->pool, value); |
292 | 99 | char *p = NULL; |
293 | 99 | while ((p = strpbrk(s_value, "\r\n"))) |
294 | 0 | *p = OIDC_CHAR_SPACE; |
295 | 99 | return s_value; |
296 | 99 | } |
297 | | |
298 | | /* |
299 | | * set a HTTP response header; table could be headers_out or err_headers_out |
300 | | */ |
301 | 198 | static void oidc_http_hdr_table_set(const request_rec *r, apr_table_t *table, const char *name, const char *value) { |
302 | | |
303 | 198 | if (value != NULL) { |
304 | | |
305 | 99 | const char *s_value = oidc_http_hdr_value_sanitize(r, value); |
306 | | |
307 | 99 | oidc_debug(r, "%s: %s", name, oidc_http_hdr_value_for_log(r, name, s_value)); |
308 | 99 | apr_table_set(table, name, s_value); |
309 | | |
310 | 99 | } else { |
311 | | |
312 | 99 | oidc_debug(r, "unset %s", name); |
313 | 99 | apr_table_unset(table, name); |
314 | 99 | } |
315 | 198 | } |
316 | | |
317 | | /* |
318 | | * append a (sanitized) header to a table without the scan for an existing entry that apr_table_set() |
319 | | * does, for callers that collect many headers and merge them into the request's table at once with |
320 | | * apr_table_overlap(); the name and the value must live as long as the table |
321 | | */ |
322 | 0 | void oidc_http_hdr_table_add(const request_rec *r, apr_table_t *table, const char *name, const char *value) { |
323 | 0 | const char *s_value = oidc_http_hdr_value_sanitize(r, value); |
324 | 0 | oidc_debug(r, "%s: %s", name, oidc_http_hdr_value_for_log(r, name, s_value)); |
325 | 0 | apr_table_addn(table, name, s_value); |
326 | 0 | } |
327 | | |
328 | | /* |
329 | | * set a (regular) HTTP response header |
330 | | */ |
331 | 198 | static void oidc_http_hdr_out_set(const request_rec *r, const char *name, const char *value) { |
332 | 198 | oidc_http_hdr_table_set(r, r->headers_out, name, value); |
333 | 198 | } |
334 | | |
335 | | /* |
336 | | * obtain a HTTP response header value |
337 | | */ |
338 | 99 | static const char *oidc_http_hdr_out_get(const request_rec *r, const char *name) { |
339 | 99 | return apr_table_get(r->headers_out, name); |
340 | 99 | } |
341 | | |
342 | | /* |
343 | | * set a HTTP response header on all responses, included errors and redirects |
344 | | */ |
345 | 198 | void oidc_http_hdr_err_out_add(const request_rec *r, const char *name, const char *value) { |
346 | 198 | oidc_debug(r, "%s: %s", name, oidc_http_hdr_value_for_log(r, name, value)); |
347 | 198 | apr_table_add(r->err_headers_out, name, value); |
348 | 198 | } |
349 | | |
350 | | /* |
351 | | * mark a response that may carry tokens or other sensitive material as non-cacheable, |
352 | | * on all responses including errors and redirects |
353 | | */ |
354 | 0 | void oidc_http_set_no_cache_headers(const request_rec *r) { |
355 | 0 | oidc_http_hdr_err_out_add(r, OIDC_HTTP_HDR_CACHE_CONTROL, "no-cache, no-store"); |
356 | 0 | oidc_http_hdr_err_out_add(r, OIDC_HTTP_HDR_PRAGMA, "no-cache"); |
357 | 0 | } |
358 | | |
359 | | /* |
360 | | * set an HTTP request header |
361 | | */ |
362 | 0 | void oidc_http_hdr_in_set(const request_rec *r, const char *name, const char *value) { |
363 | 0 | oidc_http_hdr_table_set(r, r->headers_in, name, value); |
364 | 0 | } |
365 | | |
366 | | /* |
367 | | * obtain a HTTP request cookie value |
368 | | */ |
369 | 99 | const char *oidc_http_hdr_in_cookie_get(const request_rec *r) { |
370 | 99 | return oidc_http_hdr_in_get(r, OIDC_HTTP_HDR_COOKIE); |
371 | 99 | } |
372 | | |
373 | | /* |
374 | | * set a HTTP request cookie value |
375 | | */ |
376 | 0 | void oidc_http_hdr_in_cookie_set(const request_rec *r, const char *value) { |
377 | 0 | oidc_http_hdr_in_set(r, OIDC_HTTP_HDR_COOKIE, value); |
378 | 0 | } |
379 | | |
380 | | /* |
381 | | * obtain the User-Agent header value from the HTTP request |
382 | | */ |
383 | 99 | const char *oidc_http_hdr_in_user_agent_get(const request_rec *r) { |
384 | 99 | return oidc_http_hdr_in_get(r, OIDC_HTTP_HDR_USER_AGENT); |
385 | 99 | } |
386 | | |
387 | | /* |
388 | | * obtain the X-Forwarded-For header value from the HTTP request |
389 | | */ |
390 | 0 | const char *oidc_http_hdr_in_x_forwarded_for_get(const request_rec *r) { |
391 | 0 | return oidc_http_hdr_in_get_left_most_only(r, OIDC_HTTP_HDR_X_FORWARDED_FOR, OIDC_STR_COMMA OIDC_STR_SPACE); |
392 | 0 | } |
393 | | |
394 | | /* |
395 | | * obtain the Content-Type header value from the HTTP request |
396 | | */ |
397 | 0 | const char *oidc_http_hdr_in_content_type_get(const request_rec *r) { |
398 | 0 | return oidc_http_hdr_in_get(r, OIDC_HTTP_HDR_CONTENT_TYPE); |
399 | 0 | } |
400 | | |
401 | | /* |
402 | | * obtain the Content-Length header value from the HTTP request |
403 | | */ |
404 | 0 | const char *oidc_http_hdr_in_content_length_get(const request_rec *r) { |
405 | 0 | return oidc_http_hdr_in_get(r, OIDC_HTTP_HDR_CONTENT_LENGTH); |
406 | 0 | } |
407 | | |
408 | | /* |
409 | | * obtain the X-Requested-With header value from the HTTP request |
410 | | */ |
411 | 190 | const char *oidc_http_hdr_in_x_requested_with_get(const request_rec *r) { |
412 | 190 | return oidc_http_hdr_in_get(r, OIDC_HTTP_HDR_X_REQUESTED_WITH); |
413 | 190 | } |
414 | | |
415 | | /* |
416 | | * obtain the Sec-Fetch-Mode header value from the HTTP request |
417 | | */ |
418 | 190 | const char *oidc_http_hdr_in_sec_fetch_mode_get(const request_rec *r) { |
419 | 190 | return oidc_http_hdr_in_get(r, OIDC_HTTP_HDR_SEC_FETCH_MODE); |
420 | 190 | } |
421 | | |
422 | | /* |
423 | | * obtain the Sec-Fetch-Dest header value from the HTTP request |
424 | | */ |
425 | 190 | const char *oidc_http_hdr_in_sec_fetch_dest_get(const request_rec *r) { |
426 | 190 | return oidc_http_hdr_in_get(r, OIDC_HTTP_HDR_SEC_FETCH_DEST); |
427 | 190 | } |
428 | | |
429 | | /* |
430 | | * obtain the Accept header value from the HTTP request |
431 | | */ |
432 | 0 | const char *oidc_http_hdr_in_accept_get(const request_rec *r) { |
433 | 0 | return oidc_http_hdr_in_get(r, OIDC_HTTP_HDR_ACCEPT); |
434 | 0 | } |
435 | | |
436 | | /* |
437 | | * check if a specified value exists in the HTTP Accept request header |
438 | | */ |
439 | 372 | apr_byte_t oidc_http_hdr_in_accept_contains(const request_rec *r, const char *needle) { |
440 | 372 | return oidc_http_hdr_in_contains(r, OIDC_HTTP_HDR_ACCEPT, OIDC_STR_COMMA, OIDC_CHAR_SEMI_COLON, needle); |
441 | 372 | } |
442 | | |
443 | | /* |
444 | | * obtain the Authorization header value from the HTTP request |
445 | | */ |
446 | 0 | const char *oidc_http_hdr_in_authorization_get(const request_rec *r) { |
447 | 0 | return oidc_http_hdr_in_get(r, OIDC_HTTP_HDR_AUTHORIZATION); |
448 | 0 | } |
449 | | |
450 | | /* |
451 | | * obtain the X-Forwarded-Proto header value from the HTTP request |
452 | | */ |
453 | 99 | const char *oidc_http_hdr_in_x_forwarded_proto_get(const request_rec *r) { |
454 | 99 | return oidc_http_hdr_in_get_left_most_only(r, OIDC_HTTP_HDR_X_FORWARDED_PROTO, OIDC_STR_COMMA OIDC_STR_SPACE); |
455 | 99 | } |
456 | | |
457 | | /* |
458 | | * obtain the X-Forwarded-Port header value from the HTTP request |
459 | | */ |
460 | 99 | const char *oidc_http_hdr_in_x_forwarded_port_get(const request_rec *r) { |
461 | 99 | return oidc_http_hdr_in_get_left_most_only(r, OIDC_HTTP_HDR_X_FORWARDED_PORT, OIDC_STR_COMMA OIDC_STR_SPACE); |
462 | 99 | } |
463 | | |
464 | | /* |
465 | | * obtain the X-Forwarded-Host header value from the HTTP request |
466 | | */ |
467 | 99 | const char *oidc_http_hdr_in_x_forwarded_host_get(const request_rec *r) { |
468 | 99 | return oidc_http_hdr_in_get_left_most_only(r, OIDC_HTTP_HDR_X_FORWARDED_HOST, OIDC_STR_COMMA OIDC_STR_SPACE); |
469 | 99 | } |
470 | | |
471 | | /* |
472 | | * obtain the Forwarded header value from the HTTP request |
473 | | */ |
474 | 99 | const char *oidc_http_hdr_in_forwarded_get(const request_rec *r) { |
475 | 99 | return oidc_http_hdr_in_get_left_most_only(r, OIDC_HTTP_HDR_FORWARDED, OIDC_STR_COMMA); |
476 | 99 | } |
477 | | |
478 | | /* |
479 | | * obtain the Host header value from the HTTP request |
480 | | */ |
481 | 198 | const char *oidc_http_hdr_in_host_get(const request_rec *r) { |
482 | 198 | return oidc_http_hdr_in_get(r, OIDC_HTTP_HDR_HOST); |
483 | 198 | } |
484 | | |
485 | | /* |
486 | | * obtain the traceparent header value from the HTTP request |
487 | | */ |
488 | 0 | const char *oidc_http_hdr_in_traceparent_get(const request_rec *r) { |
489 | 0 | return oidc_http_hdr_in_get(r, OIDC_HTTP_HDR_TRACE_PARENT); |
490 | 0 | } |
491 | | |
492 | | /* |
493 | | * set the Location header value in the HTTP response |
494 | | */ |
495 | 198 | void oidc_http_hdr_out_location_set(const request_rec *r, const char *value) { |
496 | 198 | oidc_http_hdr_out_set(r, OIDC_HTTP_HDR_LOCATION, value); |
497 | 198 | } |
498 | | |
499 | | /* |
500 | | * obtain the Location header value from the HTTP response |
501 | | */ |
502 | 99 | const char *oidc_http_hdr_out_location_get(const request_rec *r) { |
503 | 99 | return oidc_http_hdr_out_get(r, OIDC_HTTP_HDR_LOCATION); |
504 | 99 | } |
505 | | |
506 | | /* |
507 | | * obtain a specified value from the Forwarded header in the HTTP request |
508 | | */ |
509 | 0 | const char *oidc_http_hdr_forwarded_get(const request_rec *r, const char *elem) { |
510 | 0 | const char *value = NULL; |
511 | 0 | char *ptr = NULL; |
512 | 0 | const char *item = apr_psprintf(r->pool, "%s=", elem); |
513 | 0 | value = oidc_http_hdr_in_forwarded_get(r); |
514 | 0 | value = oidc_util_strcasestr(value, item); |
515 | 0 | if (value) { |
516 | 0 | value += _oidc_strlen(item); |
517 | 0 | ptr = _oidc_strstr(value, ";"); |
518 | 0 | if (ptr) |
519 | 0 | *ptr = '\0'; |
520 | 0 | ptr = _oidc_strstr(value, " "); |
521 | 0 | if (ptr) |
522 | 0 | *ptr = '\0'; |
523 | 0 | } |
524 | 0 | return value ? apr_pstrdup(r->pool, value) : NULL; |
525 | 0 | } |
526 | | |
527 | | /* |
528 | | * normalize a string for use as an HTTP Header Name. Any invalid |
529 | | * characters (per http://tools.ietf.org/html/rfc2616#section-4.2 and |
530 | | * http://tools.ietf.org/html/rfc2616#section-2.2) are replaced with |
531 | | * a dash ('-') character. |
532 | | */ |
533 | 0 | char *oidc_http_hdr_normalize_name(const request_rec *r, const char *str) { |
534 | | /* token = 1*<any CHAR except CTLs or separators> |
535 | | * CTL = <any US-ASCII control character |
536 | | * (octets 0 - 31) and DEL (127)> |
537 | | * separators = "(" | ")" | "<" | ">" | "@" |
538 | | * | "," | ";" | ":" | "\" | <"> |
539 | | * | "/" | "[" | "]" | "?" | "=" |
540 | | * | "{" | "}" | SP | HT */ |
541 | 0 | const char *separators = "()<>@,;:\\\"/[]?={} \t"; |
542 | |
|
543 | 0 | char *ns = apr_pstrdup(r->pool, str); |
544 | 0 | const size_t len = _oidc_strlen(ns); |
545 | 0 | for (size_t i = 0; i < len; i++) { |
546 | 0 | if (ns[i] < 32 || ns[i] == 127) |
547 | 0 | ns[i] = '-'; |
548 | 0 | else if (strchr(separators, ns[i]) != NULL) |
549 | 0 | ns[i] = '-'; |
550 | 0 | } |
551 | 0 | return ns; |
552 | 0 | } |
553 | | |
554 | | /* |
555 | | * callback for CURL to write bytes that come back from an HTTP call |
556 | | */ |
557 | 0 | size_t oidc_http_response_data(void *contents, size_t size, size_t nmemb, void *userp) { |
558 | 0 | size_t realsize = size * nmemb; |
559 | 0 | oidc_curl_resp_data_ctx_t *mem = (oidc_curl_resp_data_ctx_t *)userp; |
560 | | |
561 | | /* check if we don't run over the maximum buffer/memory size for HTTP responses */ |
562 | 0 | if (mem->size + realsize > OIDC_CURL_RESPONSE_DATA_SIZE_MAX) { |
563 | 0 | oidc_error( |
564 | 0 | mem->r, |
565 | 0 | "HTTP response larger than maximum allowed size: current size=%ld, additional size=%ld, max=%d", |
566 | 0 | (long)mem->size, (long)realsize, OIDC_CURL_RESPONSE_DATA_SIZE_MAX); |
567 | 0 | return 0; |
568 | 0 | } |
569 | | |
570 | | /* grow the buffer geometrically: pool memory is only released when the request ends, so allocating a new |
571 | | * buffer of the exact size for every chunk would keep all earlier copies around and use memory quadratic |
572 | | * in the response size */ |
573 | 0 | const size_t needed = mem->size + realsize + 1; |
574 | 0 | if (needed > mem->capacity) { |
575 | 0 | size_t capacity = mem->capacity * 2; |
576 | 0 | if (capacity > OIDC_CURL_RESPONSE_DATA_SIZE_MAX + 1) |
577 | 0 | capacity = OIDC_CURL_RESPONSE_DATA_SIZE_MAX + 1; |
578 | 0 | if (capacity < needed) |
579 | 0 | capacity = needed; |
580 | |
|
581 | 0 | char *newptr = apr_palloc(mem->r->pool, capacity); |
582 | 0 | if (newptr == NULL) { |
583 | 0 | oidc_error(mem->r, "memory allocation for new buffer of %ld bytes failed", (long)capacity); |
584 | 0 | return 0; |
585 | 0 | } |
586 | | |
587 | | /* copy over the data from current memory */ |
588 | 0 | if (mem->size > 0) |
589 | 0 | _oidc_memcpy(newptr, mem->memory, mem->size); |
590 | 0 | mem->memory = newptr; |
591 | 0 | mem->capacity = capacity; |
592 | 0 | } |
593 | | |
594 | | /* append the cURL buffer */ |
595 | 0 | _oidc_memcpy(&(mem->memory[mem->size]), contents, realsize); |
596 | 0 | mem->size += realsize; |
597 | 0 | mem->memory[mem->size] = 0; |
598 | |
|
599 | 0 | return realsize; |
600 | 0 | } |
601 | | |
602 | | /* |
603 | | * callback for CURL to write response headers that come back from an HTTP call |
604 | | */ |
605 | 0 | size_t oidc_http_response_header(const char *buffer, size_t size, size_t nitems, void *userdata) { |
606 | | /* received header is nitems * size long in 'buffer' NOT ZERO TERMINATED */ |
607 | 0 | oidc_curl_resp_hdr_ctx_t *ctx = (oidc_curl_resp_hdr_ctx_t *)userdata; |
608 | 0 | const char *hdr = NULL; |
609 | 0 | char *value = NULL; |
610 | 0 | char *h_name = NULL; |
611 | 0 | apr_ssize_t h_len = 0; |
612 | 0 | int i = 0; |
613 | | |
614 | | /* see if there is a header to search for */ |
615 | 0 | if ((ctx->hdrs == NULL) || (apr_hash_count(ctx->hdrs) == 0)) |
616 | 0 | goto end; |
617 | | |
618 | | /* make hdr a \0 terminated string for easier processing */ |
619 | 0 | hdr = apr_pstrndup(ctx->r->pool, buffer, nitems * size); |
620 | | |
621 | | /* search for a name: value pair */ |
622 | 0 | value = _oidc_strstr(hdr, OIDC_STR_COLON); |
623 | 0 | if (value == NULL) |
624 | 0 | goto end; |
625 | | |
626 | | /* split the header name and value */ |
627 | 0 | *value = '\0'; |
628 | | |
629 | | /* see if there's any header value characters at all after the colon */ |
630 | 0 | if (_oidc_strlen(hdr) < nitems * size) { |
631 | 0 | value++; |
632 | | /* skip spaces after the colon */ |
633 | 0 | while (*value == ' ') |
634 | 0 | value++; |
635 | | /* remove trailing /r/n */ |
636 | 0 | i = (int)_oidc_strlen(value) - 1; |
637 | 0 | while ((i >= 0) && ((value[i] == '\r') || (value[i] == '\n'))) |
638 | 0 | value[i--] = '\0'; |
639 | 0 | } |
640 | | |
641 | | /* check if the caller is interested in the value of the current response header */ |
642 | 0 | for (apr_hash_index_t *hi = apr_hash_first(NULL, ctx->hdrs); hi; hi = apr_hash_next(hi)) { |
643 | 0 | apr_hash_this(hi, (const void **)&h_name, &h_len, NULL); |
644 | 0 | if (_oidc_strcasecmp(hdr, h_name) == 0) { |
645 | 0 | oidc_debug(ctx->r, "returning response header: %s: %s", h_name, |
646 | 0 | oidc_http_hdr_value_for_log(ctx->r, h_name, value)); |
647 | 0 | apr_hash_set(ctx->hdrs, h_name, APR_HASH_KEY_STRING, apr_pstrdup(ctx->r->pool, value)); |
648 | 0 | break; |
649 | 0 | } |
650 | 0 | } |
651 | |
|
652 | 0 | end: |
653 | |
|
654 | 0 | return nitems * size; |
655 | 0 | } |
656 | | |
657 | | /* context structure for encoding parameters */ |
658 | | typedef struct oidc_http_encode_t { |
659 | | request_rec *r; |
660 | | /* the encoded "key=value" fragments, joined once with "&" by the callers below; accumulating |
661 | | * them into a growing string per parameter instead made the work quadratic in the number of |
662 | | * parameters, which a request-controlled parameter count can turn into an OOM (OSS-Fuzz 551746349) */ |
663 | | apr_array_header_t *elems; |
664 | | } oidc_http_encode_t; |
665 | | |
666 | | /* |
667 | | * names of protocol parameters that carry secrets or tokens and must be redacted |
668 | | * before a request URL/body is written to the debug log; the single source of truth |
669 | | * for both the per-parameter check and the body redaction below |
670 | | */ |
671 | | static const char *_oidc_http_sensitive_params[] = { |
672 | | OIDC_PROTO_CLIENT_SECRET, OIDC_PROTO_CLIENT_ASSERTION, OIDC_PROTO_CODE, OIDC_PROTO_CODE_VERIFIER, |
673 | | OIDC_PROTO_REFRESH_TOKEN, OIDC_PROTO_ACCESS_TOKEN, |
674 | | /* inbound: the front-channel authorization response and the |
675 | | * back-channel logout request carry these */ |
676 | | OIDC_PROTO_ID_TOKEN, OIDC_PROTO_LOGOUT_TOKEN, |
677 | | /* outbound, in the URL of the logout request to the OP */ |
678 | | OIDC_PROTO_ID_TOKEN_HINT, |
679 | | /* inbound, on the redirect URI: an access token and a session |
680 | | * identifier respectively */ |
681 | | OIDC_REDIRECT_URI_REQUEST_REMOVE_AT_CACHE, OIDC_REDIRECT_URI_REQUEST_REVOKE_SESSION, NULL}; |
682 | | |
683 | 792 | apr_byte_t oidc_http_param_is_sensitive(const char *key) { |
684 | 9.50k | for (int i = 0; _oidc_http_sensitive_params[i] != NULL; i++) |
685 | 8.71k | if (_oidc_strcmp(key, _oidc_http_sensitive_params[i]) == 0) |
686 | 0 | return TRUE; |
687 | 792 | return FALSE; |
688 | 792 | } |
689 | | |
690 | | /* |
691 | | * TRUE when the name_len-byte parameter name is one of _oidc_http_sensitive_params |
692 | | */ |
693 | 792 | static apr_byte_t oidc_http_param_name_is_sensitive(const char *name, size_t name_len) { |
694 | 9.50k | for (int i = 0; _oidc_http_sensitive_params[i] != NULL; i++) |
695 | 8.71k | if ((_oidc_strlen(_oidc_http_sensitive_params[i]) == name_len) && |
696 | 693 | (_oidc_strncmp(name, _oidc_http_sensitive_params[i], name_len) == 0)) |
697 | 0 | return TRUE; |
698 | 792 | return FALSE; |
699 | 792 | } |
700 | | |
701 | | /* |
702 | | * best-effort redaction of the well-known sensitive parameters listed in |
703 | | * _oidc_http_sensitive_params inside a URL-form-encoded request body, for debug-log |
704 | | * purposes; JSON request bodies do not carry these parameters in this codebase and |
705 | | * are therefore left untouched |
706 | | */ |
707 | 99 | const char *oidc_http_redact_body_for_log(const request_rec *r, const char *data) { |
708 | 99 | size_t n_params = 1; |
709 | | |
710 | 99 | if (data == NULL) |
711 | 0 | return NULL; |
712 | | /* OIDCDebugMaskSecrets Off, or no request pool to build the redacted copy in */ |
713 | 99 | if ((r == NULL) || (oidc_util_log_mask_secrets(r) == FALSE)) |
714 | 0 | return data; |
715 | 99 | apr_pool_t *pool = r->pool; |
716 | | |
717 | | /* |
718 | | * one pass over the "&"-separated parameters into a single buffer: rewriting the whole |
719 | | * string with an apr_psprintf() per redacted value made the work quadratic in the number |
720 | | * of sensitive parameters, which a request-sized body turned into an OOM (OSS-Fuzz |
721 | | * 554483423); a redacted value only ever grows to the 3 bytes of "***", so the result fits |
722 | | * in the input length plus 3 bytes per parameter |
723 | | */ |
724 | 29.3k | for (const char *c = data; *c != '\0'; c++) |
725 | 29.2k | if (*c == OIDC_CHAR_AMP) |
726 | 693 | n_params++; |
727 | 99 | char *result = apr_palloc(pool, _oidc_strlen(data) + (3 * n_params) + 1); |
728 | 99 | char *out = result; |
729 | 99 | const char *param = data; |
730 | | |
731 | 792 | for (;;) { |
732 | 792 | const char *param_end = strchr(param, OIDC_CHAR_AMP); |
733 | 792 | const size_t param_len = (param_end != NULL) ? (size_t)(param_end - param) : _oidc_strlen(param); |
734 | 792 | const char *eq = memchr(param, OIDC_CHAR_EQUAL, param_len); |
735 | | /* only a "<name>=" at the parameter boundary is redacted, so that e.g. "xcode=" is not taken for |
736 | | * "code=" */ |
737 | 792 | const apr_byte_t redact = |
738 | 792 | ((eq != NULL) && (oidc_http_param_name_is_sensitive(param, (size_t)(eq - param)) == TRUE)) ? TRUE |
739 | 792 | : FALSE; |
740 | | /* everything up to and including the "<name>=" is kept */ |
741 | 792 | const size_t keep = (redact == TRUE) ? (size_t)(eq - param) + 1 : param_len; |
742 | | |
743 | 792 | _oidc_memcpy(out, param, keep); |
744 | 792 | out += keep; |
745 | 792 | if (redact == TRUE) { |
746 | 0 | _oidc_memcpy(out, "***", 3); |
747 | 0 | out += 3; |
748 | 0 | } |
749 | 792 | if (param_end == NULL) |
750 | 99 | break; |
751 | 693 | *out = OIDC_CHAR_AMP; |
752 | 693 | out++; |
753 | 693 | param = param_end + 1; |
754 | 693 | } |
755 | 99 | *out = '\0'; |
756 | | |
757 | 99 | return result; |
758 | 99 | } |
759 | | |
760 | | /* Sensitive response members differ from the request parameters redacted above. */ |
761 | | static const char *_oidc_http_sensitive_json_members[] = {OIDC_PROTO_ACCESS_TOKEN, OIDC_PROTO_REFRESH_TOKEN, |
762 | | OIDC_PROTO_ID_TOKEN, OIDC_PROTO_CLIENT_SECRET, |
763 | | "registration_access_token", NULL}; |
764 | | |
765 | | /* |
766 | | * Redact quoted JSON members without parsing, so malformed or non-JSON responses remain |
767 | | * loggable and otherwise unchanged. This scanner assumes a sensitive value has no escaped quote: |
768 | | * the next quote terminates the value. |
769 | | * The input is copied through into a single buffer with each value replaced as it is found: |
770 | | * rewriting the whole string with an apr_psprintf() per member made the work quadratic in the |
771 | | * number of sensitive members, which a response-sized body turned into an OOM (OSS-Fuzz |
772 | | * 554483423). A match consumes at least the member name, the colon and the opening quote and |
773 | | * adds at most the 3 bytes of "***", which bounds the size of the result. |
774 | | */ |
775 | 0 | static const char *oidc_http_redact_json_member(apr_pool_t *pool, const char *data, const char *needle) { |
776 | 0 | const apr_size_t needle_len = _oidc_strlen(needle); |
777 | 0 | const apr_size_t data_len = _oidc_strlen(data); |
778 | 0 | char *result = apr_palloc(pool, data_len + (3 * ((data_len / (needle_len + 2)) + 1)) + 1); |
779 | 0 | char *out = result; |
780 | | /* the input up to here has been copied to the output */ |
781 | 0 | const char *copied = data; |
782 | | /* the scan cursor */ |
783 | 0 | const char *pos = data; |
784 | 0 | size_t len = 0; |
785 | |
|
786 | 0 | for (;;) { |
787 | 0 | const char *match = _oidc_strstr(pos, needle); |
788 | 0 | if (match == NULL) |
789 | 0 | break; |
790 | | /* step over the member name and expect ": " then a quoted value; anything |
791 | | * else (a non-string value, a member name appearing inside a value) is |
792 | | * skipped rather than guessed at */ |
793 | 0 | const char *p = match + needle_len; |
794 | 0 | while ((*p == ' ') || (*p == '\t')) |
795 | 0 | p++; |
796 | 0 | if (*p != OIDC_CHAR_COLON) { |
797 | 0 | pos = match + needle_len; |
798 | 0 | continue; |
799 | 0 | } |
800 | 0 | p++; |
801 | 0 | while ((*p == ' ') || (*p == '\t')) |
802 | 0 | p++; |
803 | 0 | if (*p != OIDC_CHAR_DQUOTE) { |
804 | 0 | pos = match + needle_len; |
805 | 0 | continue; |
806 | 0 | } |
807 | 0 | p++; |
808 | | /* copy everything up to and including the opening quote, then the mask */ |
809 | 0 | len = (size_t)(p - copied); |
810 | 0 | _oidc_memcpy(out, copied, len); |
811 | 0 | out += len; |
812 | 0 | _oidc_memcpy(out, "***", 3); |
813 | 0 | out += 3; |
814 | | /* If the closing quote is missing, mask to the end rather than leak a partial token; the |
815 | | * closing quote itself is copied with the next chunk and the scan resumes after the mask. */ |
816 | 0 | const char *value_end = strchr(p, OIDC_CHAR_DQUOTE); |
817 | 0 | copied = (value_end != NULL) ? value_end : p + _oidc_strlen(p); |
818 | 0 | pos = copied; |
819 | 0 | } |
820 | |
|
821 | 0 | len = _oidc_strlen(copied); |
822 | 0 | _oidc_memcpy(out, copied, len); |
823 | 0 | out += len; |
824 | 0 | *out = '\0'; |
825 | |
|
826 | 0 | return result; |
827 | 0 | } |
828 | | |
829 | 0 | const char *oidc_http_redact_json_for_log(const request_rec *r, const char *data) { |
830 | 0 | const char *result = NULL; |
831 | |
|
832 | 0 | if (data == NULL) |
833 | 0 | return NULL; |
834 | | /* OIDCDebugMaskSecrets Off, or no request pool to build the redacted copy in */ |
835 | 0 | if ((r == NULL) || (oidc_util_log_mask_secrets(r) == FALSE)) |
836 | 0 | return data; |
837 | 0 | apr_pool_t *pool = r->pool; |
838 | |
|
839 | 0 | result = data; |
840 | 0 | for (int i = 0; _oidc_http_sensitive_json_members[i] != NULL; i++) |
841 | 0 | result = oidc_http_redact_json_member( |
842 | 0 | pool, result, apr_pstrcat(pool, "\"", _oidc_http_sensitive_json_members[i], "\"", NULL)); |
843 | |
|
844 | 0 | return result; |
845 | 0 | } |
846 | | |
847 | | /* |
848 | | * add a url-form-encoded name/value pair |
849 | | */ |
850 | 792 | static int oidc_http_add_form_url_encoded_param(void *rec, const char *key, const char *value) { |
851 | 792 | oidc_http_encode_t *ctx = (oidc_http_encode_t *)rec; |
852 | 792 | APR_ARRAY_PUSH(ctx->elems, const char *) = |
853 | 792 | apr_psprintf(ctx->r->pool, "%s=%s", oidc_http_url_encode(ctx->r, key), oidc_http_url_encode(ctx->r, value)); |
854 | 792 | return 1; |
855 | 792 | } |
856 | | |
857 | | /* |
858 | | * add a name/value pair to a form-encoded string used only for debug logging, redacting |
859 | | * the value when the parameter name is a known secret/token |
860 | | */ |
861 | 792 | static int oidc_http_add_form_encoded_param_for_log(void *rec, const char *key, const char *value) { |
862 | 792 | oidc_http_encode_t *ctx = (oidc_http_encode_t *)rec; |
863 | 792 | const char *safe_value = value ? value : ""; |
864 | 792 | const char *v = oidc_http_param_is_sensitive(key) ? "***" : safe_value; |
865 | 792 | APR_ARRAY_PUSH(ctx->elems, const char *) = apr_psprintf(ctx->r->pool, "%s=%s", key, v); |
866 | 792 | return 1; |
867 | 792 | } |
868 | | |
869 | | /* |
870 | | * run one of the per-parameter encoders above over a table and join the collected "key=value" |
871 | | * fragments into a single "&"-separated string in one allocation; returns NULL when there are none |
872 | | */ |
873 | | static char *oidc_http_encode_params(request_rec *r, const apr_table_t *params, |
874 | 198 | int (*encoder)(void *, const char *, const char *)) { |
875 | 198 | oidc_http_encode_t ctx = {r, apr_array_make(r->pool, apr_table_elts(params)->nelts, sizeof(const char *))}; |
876 | 198 | apr_table_do(encoder, &ctx, params, NULL); |
877 | 198 | return (ctx.elems->nelts > 0) ? apr_array_pstrcat(r->pool, ctx.elems, OIDC_CHAR_AMP) : NULL; |
878 | 198 | } |
879 | | |
880 | | /* |
881 | | * construct a URL with query parameters |
882 | | */ |
883 | 99 | char *oidc_http_query_encoded_url(request_rec *r, const char *url, const apr_table_t *params) { |
884 | 99 | char *result = NULL; |
885 | 99 | if (url == NULL) { |
886 | 0 | oidc_error(r, "URL is NULL"); |
887 | 0 | return NULL; |
888 | 0 | } |
889 | 99 | if ((params != NULL) && (apr_table_elts(params)->nelts > 0)) { |
890 | 99 | const char *encoded_params = oidc_http_encode_params(r, params, oidc_http_add_form_url_encoded_param); |
891 | 99 | const char *sep = NULL; |
892 | 99 | if (encoded_params) |
893 | 99 | sep = strchr(url, OIDC_CHAR_QUERY) != NULL ? OIDC_STR_AMP : OIDC_STR_QUERY; |
894 | 99 | result = apr_psprintf(r->pool, "%s%s%s", url, sep ? sep : "", encoded_params ? encoded_params : ""); |
895 | | |
896 | 99 | const char *log_params = oidc_http_encode_params(r, params, oidc_http_add_form_encoded_param_for_log); |
897 | 99 | oidc_debug(r, "url=%s%s%s", url, sep ? sep : "", log_params ? log_params : ""); |
898 | 99 | } else { |
899 | 0 | result = apr_pstrdup(r->pool, url); |
900 | 0 | oidc_debug(r, "url=%s", result); |
901 | 0 | } |
902 | 99 | return result; |
903 | 99 | } |
904 | | |
905 | | /* |
906 | | * construct form-encoded POST data |
907 | | */ |
908 | 0 | char *oidc_http_form_encoded_data(request_rec *r, const apr_table_t *params) { |
909 | 0 | char *data = NULL; |
910 | 0 | if ((params != NULL) && (apr_table_elts(params)->nelts > 0)) { |
911 | 0 | data = oidc_http_encode_params(r, params, oidc_http_add_form_url_encoded_param); |
912 | |
|
913 | 0 | const char *log_params = oidc_http_encode_params(r, params, oidc_http_add_form_encoded_param_for_log); |
914 | 0 | oidc_debug(r, "data=%s", log_params ? log_params : "(null)"); |
915 | 0 | } else { |
916 | 0 | oidc_debug(r, "data=(null)"); |
917 | 0 | } |
918 | 0 | return data; |
919 | 0 | } |
920 | | |
921 | | /* |
922 | | * call curl_easy_setopt with error checking and reporting |
923 | | */ |
924 | | #define OIDC_HTTP_CURL_SETOPT_PARMS(r, curl, code, option, ...) \ |
925 | 0 | code = curl_easy_setopt(curl, option, __VA_ARGS__); \ |
926 | 0 | if (code != CURLE_OK) \ |
927 | 0 | oidc_error(r, "curl_easy_setopt(%s) failed with: %s", #option, curl_easy_strerror(code)) |
928 | | |
929 | 0 | #define OIDC_HTTP_CURL_SETOPT(...) OIDC_HTTP_CURL_SETOPT_PARMS(r, curl, code, __VA_ARGS__) |
930 | | |
931 | | /* |
932 | | * set libcurl SSL options |
933 | | */ |
934 | | |
935 | | #define OIDC_HTTP_CURL_SETOPT_SSL(option, value) \ |
936 | 0 | if (_oidc_strstr(env_var_value, #value) != NULL) { \ |
937 | 0 | oidc_debug(r, "curl_easy_setopt(%s): %s (%ld)", #option, #value, (long)value); \ |
938 | 0 | OIDC_HTTP_CURL_SETOPT(option, (long)value); \ |
939 | 0 | } |
940 | | |
941 | 0 | static void oidc_http_set_curl_ssl_options(request_rec *r, CURL *curl) { |
942 | | // NB: the variable names r, curl, code and env_var_value are used in the OIDC_HTTP_CURL_SETOPT_SSL macro |
943 | 0 | const char *env_var_value = NULL; |
944 | 0 | CURLcode code = CURLE_OK; |
945 | 0 | if (r->subprocess_env != NULL) |
946 | 0 | env_var_value = apr_table_get(r->subprocess_env, OIDC_CURLOPT_SSL_OPTIONS_ENV_VAR_NAME); |
947 | 0 | if (env_var_value == NULL) |
948 | 0 | return; |
949 | 0 | oidc_debug(r, "SSL options environment variable %s=%s found", OIDC_CURLOPT_SSL_OPTIONS_ENV_VAR_NAME, |
950 | 0 | env_var_value); |
951 | 0 | #if LIBCURL_VERSION_NUM >= 0x071900 |
952 | 0 | OIDC_HTTP_CURL_SETOPT_SSL(CURLOPT_SSL_OPTIONS, CURLSSLOPT_ALLOW_BEAST); |
953 | 0 | #endif |
954 | 0 | #if LIBCURL_VERSION_NUM >= 0x072c00 |
955 | 0 | OIDC_HTTP_CURL_SETOPT_SSL(CURLOPT_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE) |
956 | 0 | #endif |
957 | 0 | #if LIBCURL_VERSION_NUM >= 0x074400 |
958 | 0 | OIDC_HTTP_CURL_SETOPT_SSL(CURLOPT_SSL_OPTIONS, CURLSSLOPT_NO_PARTIALCHAIN) |
959 | 0 | #endif |
960 | 0 | #if LIBCURL_VERSION_NUM >= 0x074600 |
961 | 0 | OIDC_HTTP_CURL_SETOPT_SSL(CURLOPT_SSL_OPTIONS, CURLSSLOPT_REVOKE_BEST_EFFORT) |
962 | 0 | #endif |
963 | 0 | #if LIBCURL_VERSION_NUM >= 0x074700 |
964 | 0 | OIDC_HTTP_CURL_SETOPT_SSL(CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA) |
965 | 0 | #endif |
966 | 0 | #if LIBCURL_VERSION_NUM >= 0x072200 |
967 | 0 | OIDC_HTTP_CURL_SETOPT_SSL(CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_0) |
968 | 0 | OIDC_HTTP_CURL_SETOPT_SSL(CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_1) |
969 | 0 | OIDC_HTTP_CURL_SETOPT_SSL(CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2) |
970 | 0 | #endif |
971 | 0 | #if LIBCURL_VERSION_NUM >= 0x073400 |
972 | 0 | OIDC_HTTP_CURL_SETOPT_SSL(CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_3) |
973 | 0 | #endif |
974 | 0 | #if LIBCURL_VERSION_NUM >= 0x073600 |
975 | 0 | OIDC_HTTP_CURL_SETOPT_SSL(CURLOPT_SSLVERSION, CURL_SSLVERSION_MAX_TLSv1_0) |
976 | 0 | OIDC_HTTP_CURL_SETOPT_SSL(CURLOPT_SSLVERSION, CURL_SSLVERSION_MAX_TLSv1_1) |
977 | 0 | OIDC_HTTP_CURL_SETOPT_SSL(CURLOPT_SSLVERSION, CURL_SSLVERSION_MAX_TLSv1_2) |
978 | 0 | OIDC_HTTP_CURL_SETOPT_SSL(CURLOPT_SSLVERSION, CURL_SSLVERSION_MAX_TLSv1_3) |
979 | 0 | #endif |
980 | 0 | } |
981 | | |
982 | | /* |
983 | | * construct our User-Agent header for outgoing requests |
984 | | */ |
985 | 0 | const char *oidc_http_user_agent(request_rec *r) { |
986 | 0 | const char *s_useragent = apr_table_get(r->subprocess_env, OIDC_USER_AGENT_ENV_VAR); |
987 | 0 | if (s_useragent == NULL) { |
988 | 0 | s_useragent = apr_psprintf(r->pool, "[%s:%u:%lu] %s", r->server->server_hostname, |
989 | 0 | r->connection->local_addr->port, (unsigned long)getpid(), NAMEVERSION); |
990 | 0 | s_useragent = apr_psprintf(r->pool, "%s libcurl-%s %s", s_useragent, LIBCURL_VERSION, |
991 | 0 | oidc_util_openssl_version(r->pool)); |
992 | 0 | } |
993 | 0 | return s_useragent; |
994 | 0 | } |
995 | | |
996 | | /* |
997 | | * construct our local address/interface for outgoing requests |
998 | | */ |
999 | 0 | const char *oidc_http_interface(const request_rec *r) { |
1000 | 0 | return apr_table_get(r->subprocess_env, OIDC_CURL_INTERFACE_ENV_VAR); |
1001 | 0 | } |
1002 | | |
1003 | | /* |
1004 | | * configure the CA bundle for SSL server certificate verification, falling |
1005 | | * back to the system curl-ca-bundle.crt on Windows when no explicit bundle |
1006 | | * was configured |
1007 | | */ |
1008 | 0 | static void oidc_http_request_setup_ca_bundle(request_rec *r, CURL *curl, const oidc_cfg_t *c) { |
1009 | | // NB: the variable names r, curl, and code are used in the OIDC_HTTP_CURL_SETOPT macro |
1010 | 0 | CURLcode code = CURLE_OK; |
1011 | |
|
1012 | 0 | if (oidc_cfg_ca_bundle_path_get(c) != NULL) { |
1013 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_CAINFO, oidc_cfg_ca_bundle_path_get(c)); |
1014 | 0 | return; |
1015 | 0 | } |
1016 | |
|
1017 | | #ifdef WIN32 |
1018 | | DWORD buflen; |
1019 | | char *ptr = NULL; |
1020 | | char *retval = (char *)malloc(sizeof(TCHAR) * (MAX_PATH + 1)); |
1021 | | retval[0] = '\0'; |
1022 | | buflen = SearchPath(NULL, "curl-ca-bundle.crt", NULL, MAX_PATH + 1, retval, &ptr); |
1023 | | if (buflen > 0) { |
1024 | | OIDC_HTTP_CURL_SETOPT(CURLOPT_CAINFO, retval); |
1025 | | } else { |
1026 | | oidc_warn(r, "no curl-ca-bundle.crt file found in path"); |
1027 | | } |
1028 | | free(retval); |
1029 | | #endif |
1030 | 0 | } |
1031 | | |
1032 | | /* |
1033 | | * configure the curl handle to use the optional outgoing proxy, including |
1034 | | * its credentials and authentication type when supplied |
1035 | | */ |
1036 | | static void oidc_http_request_setup_proxy(request_rec *r, CURL *curl, |
1037 | 0 | const oidc_http_outgoing_proxy_t *outgoing_proxy) { |
1038 | | // NB: the variable names r, curl, and code are used in the OIDC_HTTP_CURL_SETOPT macro |
1039 | 0 | CURLcode code = CURLE_OK; |
1040 | |
|
1041 | 0 | if (outgoing_proxy->host_port == NULL) |
1042 | 0 | return; |
1043 | | |
1044 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_PROXY, outgoing_proxy->host_port); |
1045 | 0 | if (outgoing_proxy->username_password) { |
1046 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_PROXYUSERPWD, outgoing_proxy->username_password); |
1047 | 0 | } |
1048 | 0 | if (outgoing_proxy->auth_type != (unsigned long)OIDC_CONFIG_POS_INT_UNSET) { |
1049 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_PROXYAUTH, outgoing_proxy->auth_type); |
1050 | 0 | } |
1051 | 0 | } |
1052 | | |
1053 | | /* |
1054 | | * build the list of custom request headers (authorization, content-type, |
1055 | | * traceparent, DPoP) to pass on to curl |
1056 | | */ |
1057 | | struct curl_slist *oidc_http_request_build_header_list(request_rec *r, const oidc_cfg_t *c, const char *content_type, |
1058 | 0 | const char *access_token, const char *dpop) { |
1059 | 0 | struct curl_slist *h_list = NULL; |
1060 | | |
1061 | | /* see if we need to add token in the Bearer/DPoP Authorization header */ |
1062 | 0 | if (access_token != NULL) |
1063 | 0 | h_list = curl_slist_append(h_list, apr_psprintf(r->pool, "%s: %s %s", OIDC_HTTP_HDR_AUTHORIZATION, |
1064 | 0 | dpop ? "DPoP" : "Bearer", access_token)); |
1065 | |
|
1066 | 0 | if (content_type != NULL) |
1067 | 0 | h_list = curl_slist_append(h_list, |
1068 | 0 | apr_psprintf(r->pool, "%s: %s", OIDC_HTTP_HDR_CONTENT_TYPE, content_type)); |
1069 | |
|
1070 | 0 | const char *traceparent = oidc_http_hdr_in_traceparent_get(r); |
1071 | 0 | if (traceparent && oidc_cfg_trace_parent_get(c) != OIDC_TRACE_PARENT_OFF) { |
1072 | 0 | oidc_debug(r, "propagating traceparent header: %s", traceparent); |
1073 | 0 | h_list = |
1074 | 0 | curl_slist_append(h_list, apr_psprintf(r->pool, "%s: %s", OIDC_HTTP_HDR_TRACE_PARENT, traceparent)); |
1075 | | /* tracestate accompanies traceparent (W3C Trace Context section 3.3) so propagate it alongside */ |
1076 | 0 | const char *tracestate = oidc_http_hdr_in_get(r, OIDC_HTTP_HDR_TRACE_STATE); |
1077 | 0 | if (tracestate != NULL) { |
1078 | 0 | oidc_debug(r, "propagating tracestate header: %s", tracestate); |
1079 | 0 | h_list = curl_slist_append( |
1080 | 0 | h_list, apr_psprintf(r->pool, "%s: %s", OIDC_HTTP_HDR_TRACE_STATE, tracestate)); |
1081 | 0 | } |
1082 | 0 | } |
1083 | |
|
1084 | 0 | if (dpop != NULL) { |
1085 | 0 | oidc_debug(r, "appending DPoP header (len=%d)", (int)_oidc_strlen(dpop)); |
1086 | 0 | h_list = curl_slist_append(h_list, apr_psprintf(r->pool, "%s: %s", OIDC_HTTP_HDR_DPOP, dpop)); |
1087 | 0 | } |
1088 | |
|
1089 | 0 | return h_list; |
1090 | 0 | } |
1091 | | |
1092 | | /* |
1093 | | * pass cookies from the incoming request through to the curl handle by |
1094 | | * concatenating the configured cookies into a single Cookie header value |
1095 | | */ |
1096 | 0 | static void oidc_http_request_pass_cookies(request_rec *r, CURL *curl, const apr_array_header_t *pass_cookies) { |
1097 | | // NB: the variable names r, curl, and code are used in the OIDC_HTTP_CURL_SETOPT macro |
1098 | 0 | CURLcode code = CURLE_OK; |
1099 | 0 | char *cookie_string = NULL; |
1100 | |
|
1101 | 0 | if (pass_cookies == NULL) |
1102 | 0 | return; |
1103 | | |
1104 | 0 | for (int i = 0; i < pass_cookies->nelts; i++) { |
1105 | 0 | const char *cookie_name = APR_ARRAY_IDX(pass_cookies, i, const char *); |
1106 | 0 | char *cookie_value = oidc_http_get_cookie(r, cookie_name); |
1107 | 0 | if (cookie_value == NULL) |
1108 | 0 | continue; |
1109 | 0 | cookie_string = (cookie_string == NULL) |
1110 | 0 | ? apr_psprintf(r->pool, "%s=%s", cookie_name, cookie_value) |
1111 | 0 | : apr_psprintf(r->pool, "%s; %s=%s", cookie_string, cookie_name, cookie_value); |
1112 | 0 | } |
1113 | |
|
1114 | 0 | if (cookie_string == NULL) |
1115 | 0 | return; |
1116 | | |
1117 | 0 | oidc_debug(r, "passing browser cookies on backend call: %s", oidc_util_mask_value(r, cookie_string)); |
1118 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_COOKIE, cookie_string); |
1119 | 0 | } |
1120 | | |
1121 | | /* |
1122 | | * execute the curl request honoring the configured retry policy; retries |
1123 | | * are skipped on a request/transfer timeout and short-circuited on success |
1124 | | */ |
1125 | | static apr_byte_t oidc_http_request_perform_with_retries(request_rec *r, const oidc_cfg_t *c, CURL *curl, |
1126 | | const char *url, const char *curl_err, |
1127 | 0 | const oidc_http_timeout_t *http_timeout) { |
1128 | 0 | CURLcode res = CURLE_OK; |
1129 | |
|
1130 | 0 | for (int i = 0; i <= http_timeout->retries; i++) { |
1131 | 0 | res = curl_easy_perform(curl); |
1132 | 0 | if (res == CURLE_OK) |
1133 | 0 | return TRUE; |
1134 | 0 | if (res == CURLE_OPERATION_TIMEDOUT) { |
1135 | | /* in case of a request/transfer timeout (which includes the connect timeout) we'll not retry */ |
1136 | 0 | oidc_error(r, "curl_easy_perform failed with a timeout for %s: [%s]; won't retry", url, |
1137 | 0 | curl_err[0] ? curl_err : "<n/a>"); |
1138 | | /* label with the generic error text: curl_err carries the host name, elapsed time and |
1139 | | * byte counts, so it would record a new label value, and output line, for nearly every |
1140 | | * failure, including for host names supplied in an unauthenticated request */ |
1141 | 0 | OIDC_METRICS_COUNTER_INC_VALUE(r, c, OM_PROVIDER_CONNECT_ERROR, curl_easy_strerror(res)); |
1142 | 0 | return FALSE; |
1143 | 0 | } |
1144 | 0 | oidc_error(r, "curl_easy_perform(%d/%d) failed for %s with: [%s]", i + 1, http_timeout->retries + 1, |
1145 | 0 | url, curl_err[0] ? curl_err : "<n/a>"); |
1146 | 0 | OIDC_METRICS_COUNTER_INC_VALUE(r, c, OM_PROVIDER_CONNECT_ERROR, curl_easy_strerror(res)); |
1147 | | /* in case of a connectivity/network glitch we'll back off before retrying */ |
1148 | 0 | if (i < http_timeout->retries) |
1149 | 0 | apr_sleep(apr_time_from_msec(http_timeout->retry_interval)); |
1150 | 0 | } |
1151 | | |
1152 | 0 | return FALSE; |
1153 | 0 | } |
1154 | | |
1155 | | /* |
1156 | | * execute a HTTP (GET or POST) request |
1157 | | */ |
1158 | | static apr_byte_t oidc_http_request(request_rec *r, const char *url, const char *data, const char *content_type, |
1159 | | const char *basic_auth, const char *access_token, const char *dpop, |
1160 | | int ssl_validate_server, char **response, long *response_code, |
1161 | | apr_hash_t *response_hdrs, const oidc_http_timeout_t *http_timeout, |
1162 | | const oidc_http_outgoing_proxy_t *outgoing_proxy, |
1163 | | const apr_array_header_t *pass_cookies, const char *ssl_cert, const char *ssl_key, |
1164 | 0 | const char *ssl_key_pwd) { |
1165 | | |
1166 | | // NB: the variable names r, curl, and code are used in the OIDC_HTTP_CURL_SETOPT macro |
1167 | 0 | CURL *curl = NULL; |
1168 | 0 | CURLcode code = CURLE_OK; |
1169 | 0 | char curl_err[CURL_ERROR_SIZE]; |
1170 | 0 | oidc_curl_resp_data_ctx_t d_buf = {r, NULL, 0, 0}; |
1171 | 0 | oidc_curl_resp_hdr_ctx_t h_buf = {r, response_hdrs}; |
1172 | 0 | struct curl_slist *h_list = NULL; |
1173 | 0 | long http_code = 0; |
1174 | 0 | apr_byte_t rv = FALSE; |
1175 | 0 | const oidc_cfg_t *c = ap_get_module_config(r->server->module_config, &auth_openidc_module); |
1176 | | |
1177 | | /* do some logging about the inputs */ |
1178 | 0 | oidc_debug(r, |
1179 | 0 | "url=%s, data=%s, content_type=%s, basic_auth=%s, access_token=%s, dpop=%s, ssl_validate_server=%d, " |
1180 | 0 | "request_timeout=%d, connect_timeout=%d, retries=%d, retry_interval=%d, outgoing_proxy=%s:%s:%d, " |
1181 | 0 | "pass_cookies=%pp, ssl_cert=%s, ssl_key=%s, ssl_key_pwd=%s", |
1182 | 0 | url, oidc_http_redact_body_for_log(r, data), content_type, basic_auth ? "****" : "null", |
1183 | 0 | oidc_util_mask_value(r, access_token), dpop, ssl_validate_server, http_timeout->request_timeout, |
1184 | 0 | http_timeout->connect_timeout, http_timeout->retries, http_timeout->retry_interval, |
1185 | 0 | outgoing_proxy->host_port, outgoing_proxy->username_password ? "****" : "(null)", |
1186 | 0 | (int)outgoing_proxy->auth_type, pass_cookies, ssl_cert, ssl_key, ssl_key_pwd ? "****" : "(null)"); |
1187 | | |
1188 | | /* an endpoint that is not configured is a caller error, not a transfer failure: fail here rather |
1189 | | * than hand curl a NULL URL and then back off and retry a request that can never be sent */ |
1190 | 0 | if (url == NULL) { |
1191 | 0 | oidc_error(r, "no URL to send the request to: the endpoint is not configured"); |
1192 | 0 | return FALSE; |
1193 | 0 | } |
1194 | | |
1195 | 0 | curl = oidc_http_curl_acquire(); |
1196 | 0 | if (curl == NULL) { |
1197 | 0 | oidc_error(r, "could not obtain a curl handle"); |
1198 | 0 | goto end; |
1199 | 0 | } |
1200 | | |
1201 | | /* set the error buffer as empty before performing a request */ |
1202 | 0 | curl_err[0] = 0; |
1203 | | |
1204 | | /* some of these are not really required */ |
1205 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_HEADER, 0L); |
1206 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_NOPROGRESS, 1L); |
1207 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_NOSIGNAL, 1L); |
1208 | | /* keep reused connections alive across the (potentially long) intervals between calls */ |
1209 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_TCP_KEEPALIVE, 1L); |
1210 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_ERRORBUFFER, curl_err); |
1211 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_FOLLOWLOCATION, 1L); |
1212 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_MAXREDIRS, 5L); |
1213 | | |
1214 | | /* set the timeouts */ |
1215 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_TIMEOUT, (long)http_timeout->request_timeout); |
1216 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_CONNECTTIMEOUT, (long)http_timeout->connect_timeout); |
1217 | | |
1218 | | /* setup the buffer where the response data will be written to */ |
1219 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_WRITEFUNCTION, oidc_http_response_data); |
1220 | | /* coverity[bad_sizeof] */ |
1221 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_WRITEDATA, &d_buf); |
1222 | | |
1223 | | /* setup the buffer where the response headers will be written to */ |
1224 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_HEADERFUNCTION, oidc_http_response_header); |
1225 | | /* coverity[bad_sizeof] */ |
1226 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_HEADERDATA, &h_buf); |
1227 | |
|
1228 | 0 | #ifndef LIBCURL_NO_CURLPROTO |
1229 | 0 | #if LIBCURL_VERSION_NUM >= 0x075500 |
1230 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_REDIR_PROTOCOLS_STR, "http,https"); |
1231 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_PROTOCOLS_STR, "http,https"); |
1232 | | #else |
1233 | | OIDC_HTTP_CURL_SETOPT(CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); |
1234 | | OIDC_HTTP_CURL_SETOPT(CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); |
1235 | | #endif |
1236 | 0 | #endif |
1237 | | |
1238 | | /* set the options for validating the SSL server certificate that the remote site presents */ |
1239 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_SSL_VERIFYPEER, (ssl_validate_server != FALSE ? 1L : 0L)); |
1240 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_SSL_VERIFYHOST, (ssl_validate_server != FALSE ? 2L : 0L)); |
1241 | |
|
1242 | 0 | oidc_http_set_curl_ssl_options(r, curl); |
1243 | |
|
1244 | 0 | oidc_http_request_setup_ca_bundle(r, curl, c); |
1245 | | |
1246 | | /* identify this HTTP client */ |
1247 | 0 | const char *s_useragent = oidc_http_user_agent(r); |
1248 | 0 | if ((s_useragent != NULL) && (_oidc_strcmp(s_useragent, "") != 0)) { |
1249 | 0 | oidc_debug(r, "set HTTP request header User-Agent to: %s", s_useragent); |
1250 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_USERAGENT, s_useragent); |
1251 | 0 | } |
1252 | | |
1253 | | /* set the local interface if defined */ |
1254 | 0 | const char *s_interface = oidc_http_interface(r); |
1255 | 0 | if ((s_interface != NULL) && (_oidc_strcmp(s_interface, "") != 0)) { |
1256 | 0 | #if LIBCURL_VERSION_NUM >= 0x073000 |
1257 | 0 | oidc_debug(r, "set local interface to: %s", s_interface); |
1258 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_INTERFACE, s_interface); |
1259 | | #else |
1260 | | oidc_warn( |
1261 | | r, "local interface is configured to %s, but the cURL version in use does not support setting this", |
1262 | | s_interface); |
1263 | | #endif |
1264 | 0 | } |
1265 | |
|
1266 | 0 | oidc_http_request_setup_proxy(r, curl, outgoing_proxy); |
1267 | | |
1268 | | /* see if we need to perform HTTP basic authentication to the remote site */ |
1269 | 0 | if (basic_auth != NULL) { |
1270 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
1271 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_USERPWD, basic_auth); |
1272 | 0 | } |
1273 | |
|
1274 | 0 | if (ssl_cert != NULL) { |
1275 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_SSLCERT, ssl_cert); |
1276 | 0 | } |
1277 | 0 | if (ssl_key != NULL) { |
1278 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_SSLKEY, ssl_key); |
1279 | 0 | } |
1280 | 0 | if (ssl_key_pwd != NULL) { |
1281 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_KEYPASSWD, ssl_key_pwd); |
1282 | 0 | } |
1283 | |
|
1284 | 0 | if (data != NULL) { |
1285 | | /* set POST data and switch HTTP method to POST */ |
1286 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_POSTFIELDS, data); |
1287 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_POST, 1L); |
1288 | 0 | } |
1289 | |
|
1290 | 0 | h_list = oidc_http_request_build_header_list(r, c, content_type, access_token, dpop); |
1291 | 0 | if (h_list != NULL) { |
1292 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_HTTPHEADER, h_list); |
1293 | 0 | } |
1294 | |
|
1295 | 0 | oidc_http_request_pass_cookies(r, curl, pass_cookies); |
1296 | | |
1297 | | /* set the target URL */ |
1298 | 0 | OIDC_HTTP_CURL_SETOPT(CURLOPT_URL, url); |
1299 | | |
1300 | | /* call it and record the result */ |
1301 | 0 | rv = oidc_http_request_perform_with_retries(r, c, curl, url, curl_err, http_timeout); |
1302 | 0 | if (rv == FALSE) |
1303 | 0 | goto end; |
1304 | | |
1305 | 0 | curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); |
1306 | 0 | oidc_debug(r, "HTTP response code=%ld", http_code); |
1307 | |
|
1308 | 0 | OIDC_METRICS_COUNTER_INC_VALUE(r, c, OM_PROVIDER_HTTP_RESPONSE_CODE, apr_psprintf(r->pool, "%ld", http_code)); |
1309 | |
|
1310 | 0 | *response = apr_pstrmemdup(r->pool, d_buf.memory, d_buf.size); |
1311 | 0 | if (response_code) |
1312 | 0 | *response_code = http_code; |
1313 | | |
1314 | | /* set and log the response; the token, introspection and registration responses carry |
1315 | | * credentials, so redact them the way the request body above already is */ |
1316 | 0 | oidc_debug(r, "response=%s", oidc_http_redact_json_for_log(r, *response ? *response : "")); |
1317 | |
|
1318 | 0 | end: |
1319 | | |
1320 | | /* cleanup and return the result; a handle that performed successfully goes back to the |
1321 | | * pool with its connection kept alive for the next request to the same endpoint */ |
1322 | 0 | if (h_list != NULL) |
1323 | 0 | curl_slist_free_all(h_list); |
1324 | 0 | oidc_http_curl_release(curl, rv); |
1325 | |
|
1326 | 0 | return rv; |
1327 | 0 | } |
1328 | | |
1329 | | /* |
1330 | | * execute HTTP GET request |
1331 | | */ |
1332 | | apr_byte_t oidc_http_get(request_rec *r, const char *url, const apr_table_t *params, const char *basic_auth, |
1333 | | const char *access_token, const char *dpop, int ssl_validate_server, char **response, |
1334 | | long *response_code, apr_hash_t *response_hdrs, const oidc_http_timeout_t *http_timeout, |
1335 | | const oidc_http_outgoing_proxy_t *outgoing_proxy, const apr_array_header_t *pass_cookies, |
1336 | 0 | const char *ssl_cert, const char *ssl_key, const char *ssl_key_pwd) { |
1337 | 0 | const char *query_url = oidc_http_query_encoded_url(r, url, params); |
1338 | 0 | return oidc_http_request(r, query_url, NULL, NULL, basic_auth, access_token, dpop, ssl_validate_server, |
1339 | 0 | response, response_code, response_hdrs, http_timeout, outgoing_proxy, pass_cookies, |
1340 | 0 | ssl_cert, ssl_key, ssl_key_pwd); |
1341 | 0 | } |
1342 | | |
1343 | | /* |
1344 | | * execute HTTP POST request with form-encoded data |
1345 | | */ |
1346 | | apr_byte_t oidc_http_post_form(request_rec *r, const char *url, const apr_table_t *params, const char *basic_auth, |
1347 | | const char *access_token, const char *dpop, int ssl_validate_server, char **response, |
1348 | | long *response_code, apr_hash_t *response_hdrs, const oidc_http_timeout_t *http_timeout, |
1349 | | const oidc_http_outgoing_proxy_t *outgoing_proxy, const apr_array_header_t *pass_cookies, |
1350 | 0 | const char *ssl_cert, const char *ssl_key, const char *ssl_key_pwd) { |
1351 | 0 | const char *data = oidc_http_form_encoded_data(r, params); |
1352 | 0 | return oidc_http_request(r, url, data, OIDC_HTTP_CONTENT_TYPE_FORM_ENCODED, basic_auth, access_token, dpop, |
1353 | 0 | ssl_validate_server, response, response_code, response_hdrs, http_timeout, |
1354 | 0 | outgoing_proxy, pass_cookies, ssl_cert, ssl_key, ssl_key_pwd); |
1355 | 0 | } |
1356 | | |
1357 | | /* |
1358 | | * execute HTTP POST request with JSON-encoded data |
1359 | | */ |
1360 | | apr_byte_t oidc_http_post_json(request_rec *r, const char *url, const oidc_json_t *json, const char *basic_auth, |
1361 | | const char *access_token, const char *dpop, int ssl_validate_server, char **response, |
1362 | | long *response_code, apr_hash_t *response_hdrs, const oidc_http_timeout_t *http_timeout, |
1363 | | const oidc_http_outgoing_proxy_t *outgoing_proxy, const apr_array_header_t *pass_cookies, |
1364 | 0 | const char *ssl_cert, const char *ssl_key, const char *ssl_key_pwd) { |
1365 | 0 | const char *data = |
1366 | 0 | json != NULL ? oidc_json_encode(r->pool, json, OIDC_JSON_PRESERVE_ORDER | OIDC_JSON_COMPACT) : NULL; |
1367 | 0 | return oidc_http_request(r, url, data, OIDC_HTTP_CONTENT_TYPE_JSON, basic_auth, access_token, dpop, |
1368 | 0 | ssl_validate_server, response, response_code, response_hdrs, http_timeout, |
1369 | 0 | outgoing_proxy, pass_cookies, ssl_cert, ssl_key, ssl_key_pwd); |
1370 | 0 | } |
1371 | | |
1372 | | /* |
1373 | | * get the current path from the request in a normalized way |
1374 | | */ |
1375 | 99 | static char *oidc_http_get_path(request_rec *r) { |
1376 | 99 | size_t i; |
1377 | 99 | const char *p; |
1378 | 99 | p = r->parsed_uri.path; |
1379 | 99 | if ((p == NULL) || (p[0] == '\0')) |
1380 | 0 | return apr_pstrdup(r->pool, OIDC_STR_FORWARD_SLASH); |
1381 | 396 | for (i = _oidc_strlen(p) - 1; i > 0; i--) |
1382 | 297 | if (p[i] == OIDC_CHAR_FORWARD_SLASH) |
1383 | 0 | break; |
1384 | 99 | return apr_pstrndup(r->pool, p, i + 1); |
1385 | 99 | } |
1386 | | |
1387 | | /* |
1388 | | * get the cookie path setting and check that it matches the request path; cook it up if it is not set |
1389 | | */ |
1390 | 99 | static const char *oidc_http_get_cookie_path(request_rec *r) { |
1391 | 99 | const char *rv = NULL; |
1392 | 99 | char *requestPath = oidc_http_get_path(r); |
1393 | 99 | const char *cookie_path = oidc_cfg_dir_cookie_path_get(r); |
1394 | 99 | if (cookie_path != NULL) { |
1395 | 99 | if (_oidc_strncmp(cookie_path, requestPath, _oidc_strlen(cookie_path)) == 0) |
1396 | 99 | rv = cookie_path; |
1397 | 0 | else { |
1398 | 0 | oidc_warn(r, |
1399 | 0 | "" OIDCCookiePath |
1400 | 0 | " (%s) is not a substring of request path, using request path (%s) for cookie", |
1401 | 0 | cookie_path, requestPath); |
1402 | 0 | rv = requestPath; |
1403 | 0 | } |
1404 | 99 | } else { |
1405 | 0 | rv = requestPath; |
1406 | 0 | } |
1407 | 99 | return rv; |
1408 | 99 | } |
1409 | | |
1410 | 0 | #define OIDC_HTTP_COOKIE_FLAG_DOMAIN "Domain" |
1411 | 99 | #define OIDC_HTTP_COOKIE_FLAG_PATH "Path" |
1412 | 0 | #define OIDC_HTTP_COOKIE_FLAG_EXPIRES "Expires" |
1413 | 99 | #define OIDC_HTTP_COOKIE_FLAG_SECURE "Secure" |
1414 | 99 | #define OIDC_HTTP_COOKIE_FLAG_HTTP_ONLY "HttpOnly" |
1415 | | |
1416 | 99 | #define OIDC_HTTP_COOKIE_MAX_SIZE 4093 |
1417 | | |
1418 | 99 | #define OIDC_SET_COOKIE_APPEND_ENV_VAR "OIDC_SET_COOKIE_APPEND" |
1419 | | |
1420 | | /* |
1421 | | * obtain the value configured in the OIDC_SET_COOKIE_APPEND environment variable |
1422 | | * which is to be added to the HTTP Set-Cookie response header |
1423 | | */ |
1424 | 99 | static const char *oidc_http_set_cookie_append_value(request_rec *r) { |
1425 | 99 | const char *env_var_value = NULL; |
1426 | | |
1427 | 99 | if (r->subprocess_env != NULL) |
1428 | 99 | env_var_value = apr_table_get(r->subprocess_env, OIDC_SET_COOKIE_APPEND_ENV_VAR); |
1429 | | |
1430 | 99 | if (env_var_value == NULL) { |
1431 | 99 | oidc_debug(r, "no cookie append environment variable %s found", OIDC_SET_COOKIE_APPEND_ENV_VAR); |
1432 | 99 | return NULL; |
1433 | 99 | } |
1434 | | |
1435 | 0 | oidc_debug(r, "cookie append environment variable %s=%s found", OIDC_SET_COOKIE_APPEND_ENV_VAR, env_var_value); |
1436 | |
|
1437 | 0 | return env_var_value; |
1438 | 99 | } |
1439 | | |
1440 | | /* |
1441 | | * set a cookie in the HTTP response headers |
1442 | | */ |
1443 | | void oidc_http_set_cookie(request_rec *r, const char *cookieName, const char *cookieValue, apr_time_t expires, |
1444 | 99 | const char *ext) { |
1445 | | |
1446 | 99 | const oidc_cfg_t *c = ap_get_module_config(r->server->module_config, &auth_openidc_module); |
1447 | 99 | char *headerString = NULL; |
1448 | 99 | char *expiresString = NULL; |
1449 | 99 | const char *appendString = NULL; |
1450 | | |
1451 | | /* see if we need to clear the cookie */ |
1452 | 99 | if (_oidc_strcmp(cookieValue, "") == 0) |
1453 | 0 | expires = 0; |
1454 | | |
1455 | | /* construct the expire value */ |
1456 | 99 | if (expires != -1) { |
1457 | 0 | expiresString = (char *)apr_pcalloc(r->pool, APR_RFC822_DATE_LEN); |
1458 | 0 | if (apr_rfc822_date(expiresString, expires) != APR_SUCCESS) { |
1459 | 0 | oidc_error(r, "could not set cookie expiry date"); |
1460 | 0 | } |
1461 | 0 | } |
1462 | | |
1463 | | /* construct the cookie value */ |
1464 | 99 | headerString = apr_psprintf(r->pool, "%s=%s", cookieName, cookieValue); |
1465 | | |
1466 | 99 | headerString = |
1467 | 99 | apr_psprintf(r->pool, "%s; %s=%s", headerString, OIDC_HTTP_COOKIE_FLAG_PATH, oidc_http_get_cookie_path(r)); |
1468 | | |
1469 | 99 | if (expiresString != NULL) |
1470 | 0 | headerString = |
1471 | 0 | apr_psprintf(r->pool, "%s; %s=%s", headerString, OIDC_HTTP_COOKIE_FLAG_EXPIRES, expiresString); |
1472 | | |
1473 | 99 | if (oidc_cfg_cookie_domain_get(c) != NULL) |
1474 | 0 | headerString = apr_psprintf(r->pool, "%s; %s=%s", headerString, OIDC_HTTP_COOKIE_FLAG_DOMAIN, |
1475 | 0 | oidc_cfg_cookie_domain_get(c)); |
1476 | | |
1477 | 99 | if (oidc_util_url_cur_is_secure(r, c)) |
1478 | 99 | headerString = apr_psprintf(r->pool, "%s; %s", headerString, OIDC_HTTP_COOKIE_FLAG_SECURE); |
1479 | | |
1480 | 99 | if (oidc_cfg_cookie_http_only_get(c) != FALSE) |
1481 | 99 | headerString = apr_psprintf(r->pool, "%s; %s", headerString, OIDC_HTTP_COOKIE_FLAG_HTTP_ONLY); |
1482 | | |
1483 | 99 | appendString = oidc_http_set_cookie_append_value(r); |
1484 | 99 | if (appendString != NULL) |
1485 | 0 | headerString = apr_psprintf(r->pool, "%s; %s", headerString, appendString); |
1486 | 99 | else if (ext != NULL) |
1487 | 99 | headerString = apr_psprintf(r->pool, "%s; %s", headerString, ext); |
1488 | | |
1489 | | /* sanity check on overall cookie value size */ |
1490 | 99 | if (_oidc_strlen(headerString) > OIDC_HTTP_COOKIE_MAX_SIZE) { |
1491 | 0 | oidc_warn(r, |
1492 | 0 | "the length of the cookie value (%d) is greater than %d(!) bytes, this may not work " |
1493 | 0 | "with all browsers/server combinations: consider switching to a server side caching!", |
1494 | 0 | (int)_oidc_strlen(headerString), OIDC_HTTP_COOKIE_MAX_SIZE); |
1495 | 0 | } |
1496 | | |
1497 | | /* use r->err_headers_out so we always print our headers (even on 302 redirect) - headers_out only |
1498 | | * prints on 2xx responses */ |
1499 | 99 | oidc_http_hdr_err_out_add(r, OIDC_HTTP_HDR_SET_COOKIE, headerString); |
1500 | 99 | } |
1501 | | |
1502 | | /* |
1503 | | * get a cookie from the HTTP request |
1504 | | */ |
1505 | 0 | char *oidc_http_get_cookie(request_rec *r, const char *cookieName) { |
1506 | 0 | const char *cookie = NULL; |
1507 | 0 | char *tokenizerCtx = NULL; |
1508 | 0 | char *rv = NULL; |
1509 | | |
1510 | | /* get the Cookie value */ |
1511 | 0 | char *cookies = apr_pstrdup(r->pool, oidc_http_hdr_in_cookie_get(r)); |
1512 | |
|
1513 | 0 | if (cookies != NULL) { |
1514 | | |
1515 | | /* tokenize on ; to find the cookie we want */ |
1516 | 0 | cookie = apr_strtok(cookies, OIDC_STR_SEMI_COLON, &tokenizerCtx); |
1517 | |
|
1518 | 0 | while (cookie != NULL) { |
1519 | |
|
1520 | 0 | while (*cookie == OIDC_CHAR_SPACE) |
1521 | 0 | cookie++; |
1522 | | |
1523 | | /* see if we've found the cookie that we're looking for */ |
1524 | 0 | if ((_oidc_strncmp(cookie, cookieName, _oidc_strlen(cookieName)) == 0) && |
1525 | 0 | (cookie[_oidc_strlen(cookieName)] == OIDC_CHAR_EQUAL)) { |
1526 | | |
1527 | | /* skip to the meat of the parameter (the value after the '=') */ |
1528 | 0 | cookie += (_oidc_strlen(cookieName) + 1); |
1529 | 0 | rv = apr_pstrdup(r->pool, cookie); |
1530 | |
|
1531 | 0 | break; |
1532 | 0 | } |
1533 | | |
1534 | | /* go to the next cookie */ |
1535 | 0 | cookie = apr_strtok(NULL, OIDC_STR_SEMI_COLON, &tokenizerCtx); |
1536 | 0 | } |
1537 | 0 | } |
1538 | | |
1539 | | /* log what we've found */ |
1540 | | /* in client-cookie session mode the value is the whole session credential */ |
1541 | 0 | oidc_debug(r, "returning \"%s\" = %s", cookieName, |
1542 | 0 | rv ? apr_psprintf(r->pool, "\"%s\"", oidc_util_mask_value(r, rv)) : "<null>"); |
1543 | |
|
1544 | 0 | return rv; |
1545 | 0 | } |
1546 | | |
1547 | 0 | #define OIDC_HTTP_COOKIE_CHUNKS_SEPARATOR "_" |
1548 | 0 | #define OIDC_HTTP_COOKIE_CHUNKS_POSTFIX "chunks" |
1549 | | |
1550 | | /* the largest number of chunks a chunked cookie may be split over; the counter cookie holds it as |
1551 | | * a decimal number, so this bounds both what is written and what is accepted on the way back in */ |
1552 | 0 | #define OIDC_HTTP_COOKIE_CHUNKS_MAX 99 |
1553 | | |
1554 | | /* |
1555 | | * get the name of the cookie that contains the number of chunks |
1556 | | */ |
1557 | 0 | static char *oidc_http_get_chunk_count_name(request_rec *r, const char *cookieName) { |
1558 | 0 | return apr_psprintf(r->pool, "%s%s%s", cookieName, OIDC_HTTP_COOKIE_CHUNKS_SEPARATOR, |
1559 | 0 | OIDC_HTTP_COOKIE_CHUNKS_POSTFIX); |
1560 | 0 | } |
1561 | | |
1562 | | /* |
1563 | | * get the number of cookie chunks set by the browser |
1564 | | */ |
1565 | 0 | static int oidc_http_get_chunked_count(request_rec *r, const char *cookieName) { |
1566 | 0 | int chunkCount = 0; |
1567 | 0 | const char *chunkCountValue = oidc_http_get_cookie(r, oidc_http_get_chunk_count_name(r, cookieName)); |
1568 | 0 | chunkCount = _oidc_str_to_int(chunkCountValue, 0); |
1569 | 0 | return chunkCount; |
1570 | 0 | } |
1571 | | |
1572 | | /* |
1573 | | * get the name of a chunk |
1574 | | */ |
1575 | 0 | static char *oidc_http_get_chunk_cookie_name(request_rec *r, const char *cookieName, int i) { |
1576 | 0 | return apr_psprintf(r->pool, "%s%s%d", cookieName, OIDC_HTTP_COOKIE_CHUNKS_SEPARATOR, i); |
1577 | 0 | } |
1578 | | |
1579 | | /* |
1580 | | * get a cookie value that is split over a number of chunked cookies |
1581 | | */ |
1582 | 0 | char *oidc_http_get_chunked_cookie(request_rec *r, const char *cookieName, int chunkSize) { |
1583 | 0 | char *cookieValue = NULL; |
1584 | 0 | char *chunkValue = NULL; |
1585 | 0 | int chunkCount = 0; |
1586 | 0 | if (chunkSize == 0) |
1587 | 0 | return oidc_http_get_cookie(r, cookieName); |
1588 | 0 | chunkCount = oidc_http_get_chunked_count(r, cookieName); |
1589 | 0 | if (chunkCount == 0) |
1590 | 0 | return oidc_http_get_cookie(r, cookieName); |
1591 | 0 | if ((chunkCount < 0) || (chunkCount > OIDC_HTTP_COOKIE_CHUNKS_MAX)) { |
1592 | 0 | oidc_warn(r, "chunk count out of bounds: %d", chunkCount); |
1593 | 0 | return NULL; |
1594 | 0 | } |
1595 | 0 | for (int i = 0; i < chunkCount; i++) { |
1596 | 0 | chunkValue = oidc_http_get_cookie(r, oidc_http_get_chunk_cookie_name(r, cookieName, i)); |
1597 | 0 | if (chunkValue == NULL) { |
1598 | | /* refuse a partial assembly: a truncated value would fail session decode (or worse) |
1599 | | * and leave the browser stuck resubmitting the broken cookie set */ |
1600 | 0 | oidc_warn(r, "could not find chunk %d; aborting", i); |
1601 | 0 | return NULL; |
1602 | 0 | } |
1603 | 0 | cookieValue = apr_psprintf(r->pool, "%s%s", cookieValue ? cookieValue : "", chunkValue); |
1604 | 0 | } |
1605 | 0 | return cookieValue; |
1606 | 0 | } |
1607 | | |
1608 | | /* |
1609 | | * unset all chunked cookies, including the counter cookie, if they exist |
1610 | | */ |
1611 | | static void oidc_http_clear_chunked_cookie(request_rec *r, const char *cookieName, apr_time_t expires, |
1612 | 0 | const char *ext) { |
1613 | 0 | int chunkCount = oidc_http_get_chunked_count(r, cookieName); |
1614 | 0 | if (chunkCount > 0) { |
1615 | 0 | for (int i = 0; i < chunkCount; i++) |
1616 | 0 | oidc_http_set_cookie(r, oidc_http_get_chunk_cookie_name(r, cookieName, i), "", expires, ext); |
1617 | 0 | oidc_http_set_cookie(r, oidc_http_get_chunk_count_name(r, cookieName), "", expires, ext); |
1618 | 0 | } |
1619 | 0 | } |
1620 | | |
1621 | | /* |
1622 | | * set a cookie value that is split over a number of chunked cookies |
1623 | | */ |
1624 | | apr_byte_t oidc_http_set_chunked_cookie(request_rec *r, const char *cookieName, const char *cookieValue, |
1625 | 0 | apr_time_t expires, int chunkSize, const char *ext) { |
1626 | 0 | int cookieLength = (int)_oidc_strlen(cookieValue); |
1627 | 0 | const char *chunkValue = NULL; |
1628 | | |
1629 | | /* see if we need to chunk at all */ |
1630 | 0 | if ((chunkSize == 0) || ((cookieLength > 0) && (cookieLength < chunkSize))) { |
1631 | 0 | oidc_http_set_cookie(r, cookieName, cookieValue, expires, ext); |
1632 | 0 | oidc_http_clear_chunked_cookie(r, cookieName, expires, ext); |
1633 | 0 | return TRUE; |
1634 | 0 | } |
1635 | | |
1636 | | /* see if we need to clear a possibly chunked cookie */ |
1637 | 0 | if (cookieLength == 0) { |
1638 | 0 | oidc_http_set_cookie(r, cookieName, "", expires, ext); |
1639 | 0 | oidc_http_clear_chunked_cookie(r, cookieName, expires, ext); |
1640 | 0 | return TRUE; |
1641 | 0 | } |
1642 | | |
1643 | | /* Use ceil division without advertising a trailing empty chunk for exact multiples. */ |
1644 | 0 | int chunkCountValue = (cookieLength + chunkSize - 1) / chunkSize; |
1645 | | |
1646 | | /* refuse to write what oidc_http_get_chunked_cookie would refuse to read back: writing it |
1647 | | * anyway leaves the browser holding a value that is dropped on the next request, which for a |
1648 | | * client-side session means authenticating over and over rather than a visible failure */ |
1649 | 0 | if (chunkCountValue > OIDC_HTTP_COOKIE_CHUNKS_MAX) { |
1650 | 0 | oidc_error(r, |
1651 | 0 | "cookie \"%s\" would have to be split over %d chunks, which is more than the maximum of " |
1652 | 0 | "%d; increase " OIDCSessionCookieChunkSize " (currently %d) or store the session in a " |
1653 | 0 | "server-side cache with " OIDCSessionType " \"server-cache\"", |
1654 | 0 | cookieName, chunkCountValue, OIDC_HTTP_COOKIE_CHUNKS_MAX, chunkSize); |
1655 | 0 | return FALSE; |
1656 | 0 | } |
1657 | | |
1658 | 0 | const char *ptr = cookieValue; |
1659 | 0 | for (int i = 0; i < chunkCountValue; i++) { |
1660 | 0 | chunkValue = apr_pstrndup(r->pool, ptr, chunkSize); |
1661 | 0 | ptr += chunkSize; |
1662 | 0 | oidc_http_set_cookie(r, oidc_http_get_chunk_cookie_name(r, cookieName, i), chunkValue, expires, ext); |
1663 | 0 | } |
1664 | 0 | oidc_http_set_cookie(r, oidc_http_get_chunk_count_name(r, cookieName), |
1665 | 0 | apr_psprintf(r->pool, "%d", chunkCountValue), expires, ext); |
1666 | 0 | oidc_http_set_cookie(r, cookieName, "", expires, ext); |
1667 | |
|
1668 | 0 | return TRUE; |
1669 | 0 | } |
1670 | | |
1671 | | /* |
1672 | | * construct the HTTP outgoing proxy options |
1673 | | */ |
1674 | 0 | const char **oidc_http_proxy_auth_options(void) { |
1675 | 0 | static const char *options[] = {OIDC_HTTP_PROXY_AUTH_BASIC, |
1676 | 0 | OIDC_HTTP_PROXY_AUTH_DIGEST, |
1677 | 0 | OIDC_HTTP_PROXY_AUTH_NTLM, |
1678 | 0 | OIDC_HTTP_PROXY_AUTH_ANY, |
1679 | 0 | #ifdef CURLAUTH_NEGOTIATE |
1680 | 0 | OIDC_HTTP_PROXY_AUTH_NEGOTIATE, |
1681 | 0 | #endif |
1682 | 0 | NULL}; |
1683 | 0 | return options; |
1684 | 0 | } |
1685 | | |
1686 | | /* |
1687 | | * return the CURL enum value for the HTTP outgoing proxy options |
1688 | | */ |
1689 | 0 | unsigned long oidc_http_proxy_s2auth(const char *arg) { |
1690 | 0 | if (_oidc_strcmp(arg, OIDC_HTTP_PROXY_AUTH_BASIC) == 0) |
1691 | 0 | return CURLAUTH_BASIC; |
1692 | 0 | if (_oidc_strcmp(arg, OIDC_HTTP_PROXY_AUTH_DIGEST) == 0) |
1693 | 0 | return CURLAUTH_DIGEST; |
1694 | 0 | if (_oidc_strcmp(arg, OIDC_HTTP_PROXY_AUTH_NTLM) == 0) |
1695 | 0 | return CURLAUTH_NTLM; |
1696 | 0 | if (_oidc_strcmp(arg, OIDC_HTTP_PROXY_AUTH_ANY) == 0) |
1697 | 0 | return CURLAUTH_ANY; |
1698 | 0 | #ifdef CURLAUTH_NEGOTIATE |
1699 | 0 | if (_oidc_strcmp(arg, OIDC_HTTP_PROXY_AUTH_NEGOTIATE) == 0) |
1700 | 0 | return CURLAUTH_NEGOTIATE; |
1701 | 0 | #endif |
1702 | 0 | return CURLAUTH_NONE; |
1703 | 0 | } |
1704 | | |
1705 | | /* |
1706 | | * initialize the HTTP/cURL environment |
1707 | | */ |
1708 | 0 | void oidc_http_init(void) { |
1709 | 0 | curl_global_init(CURL_GLOBAL_ALL); |
1710 | 0 | } |
1711 | | |
1712 | | /* |
1713 | | * clean up the HTTP/cURL environment |
1714 | | */ |
1715 | 0 | void oidc_http_cleanup(void) { |
1716 | 0 | curl_global_cleanup(); |
1717 | 0 | } |
1718 | | |
1719 | | /* Pool reset handles to retain their connection, DNS, and TLS caches. */ |
1720 | 0 | #define OIDC_HTTP_CURL_POOL_MAX 16 |
1721 | | |
1722 | | static CURL *_oidc_http_curl_pool[OIDC_HTTP_CURL_POOL_MAX]; |
1723 | | static int _oidc_http_curl_pool_num = 0; |
1724 | | static apr_byte_t _oidc_http_curl_pool_enabled = FALSE; |
1725 | | #if APR_HAS_THREADS |
1726 | | static apr_thread_mutex_t *_oidc_http_curl_pool_mutex = NULL; |
1727 | | #endif |
1728 | | |
1729 | 0 | static void oidc_http_curl_pool_lock(void) { |
1730 | 0 | #if APR_HAS_THREADS |
1731 | 0 | apr_thread_mutex_lock(_oidc_http_curl_pool_mutex); |
1732 | 0 | #endif |
1733 | 0 | } |
1734 | | |
1735 | 0 | static void oidc_http_curl_pool_unlock(void) { |
1736 | 0 | #if APR_HAS_THREADS |
1737 | 0 | apr_thread_mutex_unlock(_oidc_http_curl_pool_mutex); |
1738 | 0 | #endif |
1739 | 0 | } |
1740 | | |
1741 | 0 | static apr_status_t oidc_http_curl_pool_cleanup(void *data) { |
1742 | 0 | for (int i = 0; i < _oidc_http_curl_pool_num; i++) |
1743 | 0 | curl_easy_cleanup(_oidc_http_curl_pool[i]); |
1744 | 0 | _oidc_http_curl_pool_num = 0; |
1745 | 0 | _oidc_http_curl_pool_enabled = FALSE; |
1746 | 0 | #if APR_HAS_THREADS |
1747 | 0 | _oidc_http_curl_pool_mutex = NULL; |
1748 | 0 | #endif |
1749 | 0 | return APR_SUCCESS; |
1750 | 0 | } |
1751 | | |
1752 | 2 | void oidc_http_curl_pool_init(apr_pool_t *pool) { |
1753 | 2 | if (_oidc_http_curl_pool_enabled == TRUE) |
1754 | 0 | return; |
1755 | 2 | #if APR_HAS_THREADS |
1756 | 2 | if (apr_thread_mutex_create(&_oidc_http_curl_pool_mutex, APR_THREAD_MUTEX_DEFAULT, pool) != APR_SUCCESS) |
1757 | 0 | return; |
1758 | 2 | #endif |
1759 | 2 | _oidc_http_curl_pool_num = 0; |
1760 | 2 | _oidc_http_curl_pool_enabled = TRUE; |
1761 | 2 | apr_pool_cleanup_register(pool, NULL, oidc_http_curl_pool_cleanup, apr_pool_cleanup_null); |
1762 | 2 | } |
1763 | | |
1764 | | /* |
1765 | | * forget any handles inherited over fork(): their connections share file descriptors and TLS |
1766 | | * state with the parent, so the child must neither use nor curl_easy_cleanup them (a cleanup |
1767 | | * would write a TLS close-notify into a socket the parent may still be using) |
1768 | | */ |
1769 | 0 | void oidc_http_curl_pool_child_init(void) { |
1770 | 0 | _oidc_http_curl_pool_num = 0; |
1771 | 0 | } |
1772 | | |
1773 | 0 | static CURL *oidc_http_curl_acquire(void) { |
1774 | 0 | CURL *curl = NULL; |
1775 | 0 | if (_oidc_http_curl_pool_enabled == FALSE) |
1776 | 0 | return curl_easy_init(); |
1777 | 0 | oidc_http_curl_pool_lock(); |
1778 | 0 | if (_oidc_http_curl_pool_num > 0) |
1779 | 0 | curl = _oidc_http_curl_pool[--_oidc_http_curl_pool_num]; |
1780 | 0 | oidc_http_curl_pool_unlock(); |
1781 | 0 | return (curl != NULL) ? curl : curl_easy_init(); |
1782 | 0 | } |
1783 | | |
1784 | | /* |
1785 | | * return a handle to the pool after a successful request: the reset clears its options but |
1786 | | * keeps the connection/DNS/TLS-session caches; a handle whose request failed is destroyed |
1787 | | * rather than reused so a poisoned connection state cannot carry over |
1788 | | */ |
1789 | 0 | static void oidc_http_curl_release(CURL *curl, apr_byte_t reuse) { |
1790 | 0 | if (curl == NULL) |
1791 | 0 | return; |
1792 | 0 | if ((reuse == TRUE) && (_oidc_http_curl_pool_enabled == TRUE)) { |
1793 | 0 | curl_easy_reset(curl); |
1794 | 0 | oidc_http_curl_pool_lock(); |
1795 | 0 | if (_oidc_http_curl_pool_num < OIDC_HTTP_CURL_POOL_MAX) { |
1796 | 0 | _oidc_http_curl_pool[_oidc_http_curl_pool_num++] = curl; |
1797 | 0 | curl = NULL; |
1798 | 0 | } |
1799 | 0 | oidc_http_curl_pool_unlock(); |
1800 | 0 | } |
1801 | 0 | if (curl != NULL) |
1802 | 0 | curl_easy_cleanup(curl); |
1803 | 0 | } |