Coverage Report

Created: 2026-11-12 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mod_auth_openidc/src/proto/proto.c
Line
Count
Source
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
20
/***************************************************************************
21
 * Copyright (C) 2017-2026 ZmartZone Holding BV
22
 * All rights reserved.
23
 *
24
 * DISCLAIMER OF WARRANTIES:
25
 *
26
 * THE SOFTWARE PROVIDED HEREUNDER IS PROVIDED ON AN "AS IS" BASIS, WITHOUT
27
 * ANY WARRANTIES OR REPRESENTATIONS EXPRESS, IMPLIED OR STATUTORY; INCLUDING,
28
 * WITHOUT LIMITATION, WARRANTIES OF QUALITY, PERFORMANCE, NONINFRINGEMENT,
29
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  NOR ARE THERE ANY
30
 * WARRANTIES CREATED BY A COURSE OR DEALING, COURSE OF PERFORMANCE OR TRADE
31
 * USAGE.  FURTHERMORE, THERE ARE NO WARRANTIES THAT THE SOFTWARE WILL MEET
32
 * YOUR NEEDS OR BE FREE FROM ERRORS, OR THAT THE OPERATION OF THE SOFTWARE
33
 * WILL BE UNINTERRUPTED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
34
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES HOWEVER CAUSED AND ON ANY THEORY OF
36
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
37
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
 *
40
 * @Author: Hans Zandbelt - hans.zandbelt@openidc.com
41
 */
42
43
#include "proto/proto.h"
44
#include "cfg/dir.h"
45
#include "cfg/parse.h"
46
#include "metadata.h"
47
#include "metrics.h"
48
#include "util/util.h"
49
50
#include <openssl/opensslconf.h>
51
#include <openssl/opensslv.h>
52
53
/* nonce bytes length */
54
1.98k
#define OIDC_PROTO_NONCE_LENGTH 32
55
56
/*
57
 * generate a random value (nonce) to correlate request/response through browser state
58
 */
59
1.98k
apr_byte_t oidc_proto_nonce_gen(request_rec *r, char **nonce) {
60
1.98k
  return oidc_util_rand_str(r, nonce, OIDC_PROTO_NONCE_LENGTH);
61
1.98k
}
62
63
/* jti bytes length */
64
0
#define OIDC_PROTO_JWT_JTI_LEN 16
65
66
/*
67
 * generate a random unique "jti" JWT identifier
68
 */
69
0
char *oidc_proto_jti_gen(request_rec *r) {
70
0
  char *jti = NULL;
71
0
  if (oidc_util_rand_str(r, &jti, OIDC_PROTO_JWT_JTI_LEN) == FALSE) {
72
0
    oidc_error(r, "oidc_util_rand_str returned FALSE");
73
0
  }
74
0
  return jti;
75
0
}
76
77
/*
78
 * return the supported flows
79
 */
80
578k
apr_array_header_t *oidc_proto_supported_flows(apr_pool_t *pool) {
81
578k
  apr_array_header_t *result = apr_array_make(pool, 6, sizeof(const char *));
82
578k
  APR_ARRAY_PUSH(result, const char *) = OIDC_PROTO_RESPONSE_TYPE_CODE;
83
578k
  APR_ARRAY_PUSH(result, const char *) = OIDC_PROTO_RESPONSE_TYPE_IDTOKEN;
84
578k
  APR_ARRAY_PUSH(result, const char *) = OIDC_PROTO_RESPONSE_TYPE_IDTOKEN_TOKEN;
85
578k
  APR_ARRAY_PUSH(result, const char *) = OIDC_PROTO_RESPONSE_TYPE_CODE_IDTOKEN;
86
578k
  APR_ARRAY_PUSH(result, const char *) = OIDC_PROTO_RESPONSE_TYPE_CODE_TOKEN;
87
578k
  APR_ARRAY_PUSH(result, const char *) = OIDC_PROTO_RESPONSE_TYPE_CODE_IDTOKEN_TOKEN;
88
578k
  return result;
89
578k
}
90
91
/*
92
 * check if a particular OpenID Connect flow is supported
93
 */
94
290k
apr_byte_t oidc_proto_flow_is_supported(apr_pool_t *pool, const char *flow) {
95
290k
  apr_array_header_t *flows = oidc_proto_supported_flows(pool);
96
2.01M
  for (int i = 0; i < flows->nelts; i++) {
97
1.73M
    if (oidc_util_spaced_string_equals(pool, flow, APR_ARRAY_IDX(flows, i, const char *)))
98
3.04k
      return TRUE;
99
1.73M
  }
100
287k
  return FALSE;
101
290k
}
102
103
/*
104
 * set the WWW-Authenticate response header according to https://tools.ietf.org/html/rfc6750#section-3
105
 */
106
2.70k
int oidc_proto_return_www_authenticate(request_rec *r, const char *error, const char *error_description) {
107
2.70k
  apr_byte_t accept_token_in = (apr_byte_t)oidc_cfg_dir_oauth_accept_token_in_get(r);
108
2.70k
  char *hdr;
109
2.70k
  if (accept_token_in == OIDC_OAUTH_ACCEPT_TOKEN_IN_BASIC) {
110
3
    hdr = apr_psprintf(r->pool, "%s", OIDC_PROTO_BASIC);
111
2.70k
  } else {
112
2.70k
    hdr = apr_psprintf(r->pool, "%s", OIDC_PROTO_BEARER);
113
2.70k
  }
114
115
2.70k
  if (ap_auth_name(r) != NULL)
116
0
    hdr = apr_psprintf(r->pool, "%s %s=\"%s\"", hdr, OIDC_PROTO_REALM, ap_auth_name(r));
117
2.70k
  if (error != NULL)
118
2.70k
    hdr =
119
2.70k
        apr_psprintf(r->pool, "%s%s %s=\"%s\"", hdr, (ap_auth_name(r) ? "," : ""), OIDC_PROTO_ERROR, error);
120
2.70k
  if (error_description != NULL)
121
2.70k
    hdr = apr_psprintf(r->pool, "%s, %s=\"%s\"", hdr, OIDC_PROTO_ERROR_DESCRIPTION, error_description);
122
2.70k
  oidc_http_hdr_err_out_add(r, OIDC_HTTP_HDR_WWW_AUTHENTICATE, hdr);
123
2.70k
  return HTTP_UNAUTHORIZED;
124
2.70k
}