/src/mod_auth_openidc/src/proto/pkce.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 "util/util.h" |
45 | | |
46 | 4.59k | static apr_byte_t oidc_proto_pkce_code_verifier_gen(request_rec *r, char **code_verifier) { |
47 | 4.59k | return oidc_util_rand_str(r, code_verifier, OIDC_PROTO_CODE_VERIFIER_LENGTH); |
48 | 4.59k | } |
49 | | /* |
50 | | * PCKE "plain" proto state |
51 | | */ |
52 | 0 | static apr_byte_t oidc_proto_pkce_state_plain(request_rec *r, char **state) { |
53 | 0 | return oidc_proto_pkce_code_verifier_gen(r, state); |
54 | 0 | } |
55 | | |
56 | | /* |
57 | | * PCKE "plain" code_challenge |
58 | | */ |
59 | 0 | static apr_byte_t oidc_proto_pkce_challenge_plain(request_rec *r, const char *state, char **code_challenge) { |
60 | 0 | *code_challenge = apr_pstrdup(r->pool, state); |
61 | 0 | return TRUE; |
62 | 0 | } |
63 | | |
64 | | /* |
65 | | * PCKE "plain" code_verifier |
66 | | */ |
67 | 0 | static apr_byte_t oidc_proto_pkce_verifier_plain(request_rec *r, const char *state, char **code_verifier) { |
68 | 0 | *code_verifier = apr_pstrdup(r->pool, state); |
69 | 0 | return TRUE; |
70 | 0 | } |
71 | | |
72 | | /* |
73 | | * PCKE "s256" proto state |
74 | | */ |
75 | 4.59k | static apr_byte_t oidc_proto_pkce_state_s256(request_rec *r, char **state) { |
76 | 4.59k | return oidc_proto_pkce_code_verifier_gen(r, state); |
77 | 4.59k | } |
78 | | |
79 | | /* |
80 | | * PCKE "s256" code_challenge |
81 | | */ |
82 | 4.59k | static apr_byte_t oidc_proto_pkce_challenge_s256(request_rec *r, const char *state, char **code_challenge) { |
83 | 4.59k | if (oidc_util_hash_string_and_base64url_encode(r, OIDC_JOSE_ALG_SHA256, state, code_challenge) == FALSE) { |
84 | 0 | oidc_error(r, "oidc_util_hash_string_and_base64url_encode returned an error for the code verifier"); |
85 | 0 | return FALSE; |
86 | 0 | } |
87 | 4.59k | return TRUE; |
88 | 4.59k | } |
89 | | |
90 | | /* |
91 | | * PCKE "s256" code_verifier |
92 | | */ |
93 | 255 | static apr_byte_t oidc_proto_pkce_verifier_s256(request_rec *r, const char *state, char **code_verifier) { |
94 | 255 | *code_verifier = apr_pstrdup(r->pool, state); |
95 | 255 | return TRUE; |
96 | 255 | } |
97 | | |
98 | | /* |
99 | | * PKCE plain |
100 | | */ |
101 | | oidc_proto_pkce_t oidc_pkce_plain = {OIDC_PKCE_METHOD_PLAIN, oidc_proto_pkce_state_plain, |
102 | | oidc_proto_pkce_verifier_plain, oidc_proto_pkce_challenge_plain}; |
103 | | |
104 | | /* |
105 | | * PKCE s256 |
106 | | */ |
107 | | oidc_proto_pkce_t oidc_pkce_s256 = {OIDC_PKCE_METHOD_S256, oidc_proto_pkce_state_s256, oidc_proto_pkce_verifier_s256, |
108 | | oidc_proto_pkce_challenge_s256}; |
109 | | |
110 | | /* |
111 | | * PKCE none |
112 | | */ |
113 | | oidc_proto_pkce_t oidc_pkce_none = {OIDC_PKCE_METHOD_NONE, NULL, NULL, NULL}; |