Line data Source code
1 : /*
2 : MIT License
3 :
4 : Copyright (c) 2026 - A bunch of nerds
5 :
6 : Permission is hereby granted, free of charge, to any person obtaining a copy
7 : of this software and associated documentation files (the "Software"), to deal
8 : in the Software without restriction, including without limitation the rights
9 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 : copies of the Software, and to permit persons to whom the Software is
11 : furnished to do so, subject to the following conditions:
12 :
13 : The above copyright notice and this permission notice shall be included in all
14 : copies or substantial portions of the Software.
15 :
16 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 : SOFTWARE.
23 : */
24 :
25 : #include <chttp.h>
26 : #include <common.h>
27 : #include <internal/test_helpers/chttp.h>
28 : #include <stdbool.h>
29 : #include <stdint.h>
30 : #include <stdio.h>
31 : #include <string.h>
32 :
33 : // Base64 (RFC 4648, '+' and '/', '=' padding)
34 : //
35 : // The encoding alphabet, indexed by a 6-bit value.
36 : static const char g_chttp_base64_alphabet[] =
37 : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
38 :
39 : // Maps a byte of the alphabet to its 6-bit value and every other byte, '='
40 : // included, to -1. It's filled once on first use, because an initializer can't
41 : // default 61 unnamed slots to -1 (0 already means 'A').
42 : static signed char g_chttp_base64_decode_table[256];
43 : static ccol__once_flag g_chttp_base64_decode_table_once = CCOL__ONCE_INIT;
44 :
45 : // Fills g_chttp_base64_decode_table, which the decoder reads. It runs once,
46 : // through g_chttp_base64_decode_table_once, the first time a string is
47 : // decoded.
48 1 : static void chttp_base64_decode_table_init(void) {
49 1 : memset(g_chttp_base64_decode_table, -1, sizeof(g_chttp_base64_decode_table));
50 27 : for (int i = 0; i < 26; i++) {
51 26 : g_chttp_base64_decode_table[(unsigned char)('A' + i)] = (signed char)i;
52 26 : g_chttp_base64_decode_table[(unsigned char)('a' + i)] =
53 26 : (signed char)(26 + i);
54 : }
55 11 : for (int i = 0; i < 10; i++)
56 10 : g_chttp_base64_decode_table[(unsigned char)('0' + i)] =
57 10 : (signed char)(52 + i);
58 1 : g_chttp_base64_decode_table[(unsigned char)'+'] = 62;
59 1 : g_chttp_base64_decode_table[(unsigned char)'/'] = 63;
60 1 : }
61 :
62 : // Encodes len bytes of data as base64, for chttp_basic_auth_mp() and for
63 : // callers that build header values. It gives a new NUL-terminated string from
64 : // mp, with its length in *out_len when out_len isn't NULL, or NULL for a NULL
65 : // data with len > 0, a failed allocation, or a len whose encoded size
66 : // ((len + 2) / 3 * 4 + 1) would overflow size_t.
67 65835 : char *chttp_base64_encode_mp(ccol_memmgmt_procs *mp, const void *data,
68 : size_t len, size_t *out_len) {
69 65835 : if (!data && len > 0) return NULL;
70 65834 : if (len > (SIZE_MAX / 4) * 3 - 4) return NULL;
71 :
72 65833 : const unsigned char *p = (const unsigned char *)data;
73 65833 : size_t enc_len = ((len + 2) / 3) * 4;
74 65833 : char *out = (char *)ccol__mem_alloc(mp, enc_len + 1);
75 65833 : if (!out) return NULL;
76 :
77 : size_t oi = 0, i = 0;
78 65944 : for (; i + 3 <= len; i += 3) {
79 113 : uint32_t n =
80 113 : ((uint32_t)p[i] << 16) | ((uint32_t)p[i + 1] << 8) | (uint32_t)p[i + 2];
81 113 : out[oi++] = g_chttp_base64_alphabet[(n >> 18) & 0x3F];
82 113 : out[oi++] = g_chttp_base64_alphabet[(n >> 12) & 0x3F];
83 113 : out[oi++] = g_chttp_base64_alphabet[(n >> 6) & 0x3F];
84 113 : out[oi++] = g_chttp_base64_alphabet[n & 0x3F];
85 : }
86 65831 : size_t rem = len - i;
87 65831 : if (rem == 1) {
88 261 : uint32_t n = (uint32_t)p[i] << 16;
89 261 : out[oi++] = g_chttp_base64_alphabet[(n >> 18) & 0x3F];
90 261 : out[oi++] = g_chttp_base64_alphabet[(n >> 12) & 0x3F];
91 261 : out[oi++] = '=';
92 261 : out[oi++] = '=';
93 65570 : } else if (rem == 2) {
94 65542 : uint32_t n = ((uint32_t)p[i] << 16) | ((uint32_t)p[i + 1] << 8);
95 65542 : out[oi++] = g_chttp_base64_alphabet[(n >> 18) & 0x3F];
96 65542 : out[oi++] = g_chttp_base64_alphabet[(n >> 12) & 0x3F];
97 65542 : out[oi++] = g_chttp_base64_alphabet[(n >> 6) & 0x3F];
98 65542 : out[oi++] = '=';
99 : }
100 65831 : out[oi] = '\0';
101 :
102 65831 : if (out_len) *out_len = enc_len;
103 : return out;
104 : }
105 :
106 : // Decodes the NUL-terminated base64 string b64_input, for callers that read
107 : // encoded header values. It gives a new buffer from mp (NUL-terminated too)
108 : // with the decoded length in *out_len, or NULL for malformed input or a
109 : // failed allocation. '=' may only be the last one or two bytes, so a pre-scan
110 : // gives the output an exact size, and the group loop refuses a '=' anywhere
111 : // else before it could write past it. A padded final group must have its
112 : // unused low bits zero (RFC 4648 section 3.5), so "QR==" is refused and each
113 : // byte string has one spelling. Reads the shared decode table and initializes
114 : // it once.
115 266276 : void *chttp_base64_decode_mp(ccol_memmgmt_procs *mp, const char *b64_input,
116 : size_t *out_len) {
117 266276 : if (!b64_input) return NULL;
118 :
119 266275 : size_t in_len = strlen(b64_input);
120 266275 : if (in_len == 0) {
121 2 : char *out = (char *)ccol__mem_alloc(mp, 1);
122 2 : if (!out) return NULL;
123 1 : out[0] = '\0';
124 1 : if (out_len) *out_len = 0;
125 1 : return out;
126 : }
127 266273 : if (in_len % 4 != 0) return NULL;
128 :
129 266270 : ccol__call_once(g_chttp_base64_decode_table_once,
130 : chttp_base64_decode_table_init);
131 :
132 266270 : size_t pad = 0;
133 266270 : if (b64_input[in_len - 1] == '=') {
134 266256 : pad = 1;
135 266256 : if (in_len >= 2 && b64_input[in_len - 2] == '=') pad = 2;
136 : }
137 :
138 266270 : size_t groups = in_len / 4;
139 266270 : size_t dec_len = groups * 3 - pad;
140 266270 : unsigned char *out = (unsigned char *)ccol__mem_alloc(mp, dec_len + 1);
141 266270 : if (!out) return NULL;
142 :
143 : size_t oi = 0;
144 332092 : for (size_t g = 0; g < groups; g++) {
145 266284 : const char *chunk = b64_input + g * 4;
146 266284 : bool last_group = (g == groups - 1);
147 266284 : int vals[4];
148 266284 : bool seen_pad = false;
149 1331402 : for (int k = 0; k < 4; k++) {
150 1065126 : char c = chunk[k];
151 1065126 : if (c == '=') {
152 270364 : if (!last_group || k < 2) {
153 4 : ccol__mem_free(mp, out);
154 200461 : return NULL;
155 : }
156 270360 : seen_pad = true;
157 270360 : vals[k] = -1;
158 : } else {
159 794762 : if (seen_pad) {
160 1 : ccol__mem_free(mp, out);
161 1 : return NULL;
162 : }
163 794761 : int v = g_chttp_base64_decode_table[(unsigned char)c];
164 794761 : if (v < 0) {
165 3 : ccol__mem_free(mp, out);
166 3 : return NULL;
167 : }
168 794758 : vals[k] = v;
169 : }
170 : }
171 :
172 266276 : if ((vals[2] < 0 && (vals[1] & 0x0F) != 0) ||
173 262172 : (vals[2] >= 0 && vals[3] < 0 && (vals[2] & 0x03) != 0)) {
174 200453 : ccol__mem_free(mp, out);
175 200453 : return NULL;
176 : }
177 :
178 65823 : uint32_t n = ((uint32_t)vals[0] << 18) | ((uint32_t)vals[1] << 12) |
179 65823 : ((uint32_t)(vals[2] < 0 ? 0 : vals[2]) << 6) |
180 65823 : (uint32_t)(vals[3] < 0 ? 0 : vals[3]);
181 65823 : out[oi++] = (unsigned char)((n >> 16) & 0xFF);
182 65823 : if (vals[2] >= 0) out[oi++] = (unsigned char)((n >> 8) & 0xFF);
183 65823 : if (vals[3] >= 0) out[oi++] = (unsigned char)(n & 0xFF);
184 : }
185 65808 : out[oi] = '\0';
186 :
187 65808 : if (out_len) *out_len = oi;
188 : return out;
189 : }
190 :
191 : // HTTP Basic auth (RFC 7617)
192 : //
193 : // Builds "Basic <base64(username:password)>" in a new string from mp. It's the
194 : // body of chttp_basic_auth_mp(); the lengths are parameters so the white-box
195 : // hook can push the overflow guard with fake lengths. The guard leaves room
196 : // for the ':' and the NUL. It gives NULL when the size would overflow or an
197 : // allocation fails.
198 31 : static char *chttp_basic_auth_len(ccol_memmgmt_procs *mp, const char *username,
199 : size_t user_len, const char *password,
200 : size_t pass_len) {
201 31 : if (pass_len > SIZE_MAX - user_len || user_len + pass_len > SIZE_MAX - 2)
202 : return NULL;
203 30 : size_t combined_len = user_len + 1 + pass_len;
204 30 : char *combined = (char *)ccol__mem_alloc(mp, combined_len + 1);
205 30 : if (!combined) return NULL;
206 29 : snprintf(combined, combined_len + 1, "%s:%s", username, password);
207 :
208 29 : size_t b64_len = 0;
209 29 : char *b64 = chttp_base64_encode_mp(mp, combined, combined_len, &b64_len);
210 29 : ccol__mem_free(mp, combined);
211 29 : if (!b64) return NULL;
212 :
213 28 : size_t auth_len = 6 + b64_len;
214 28 : char *out = (char *)ccol__mem_alloc(mp, auth_len + 1);
215 28 : if (!out) {
216 1 : ccol__mem_free(mp, b64);
217 1 : return NULL;
218 : }
219 27 : snprintf(out, auth_len + 1, "Basic %s", b64);
220 27 : ccol__mem_free(mp, b64);
221 : return out;
222 : }
223 :
224 : // True when s holds a byte below 0x20 or DEL (0x7F). chttp_basic_auth_mp()
225 : // uses it to refuse credentials RFC 7617 forbids.
226 : static bool chttp_has_ctl(const char *s) {
227 419 : for (const unsigned char *p = (const unsigned char *)s; *p; p++)
228 355 : if (*p < 0x20 || *p == 0x7F) return true;
229 : return false;
230 : }
231 :
232 : // Builds the Basic auth header value for username and password, so a caller
233 : // can authenticate a request. It gives a new string from mp, or NULL for a
234 : // NULL string, a failed allocation or a refused input. It refuses a colon in
235 : // the username (the recipient cuts at the first colon, so "a:b" + "c" would
236 : // read as "a" + "b:c") and a control byte in either string, as RFC 7617
237 : // section 2 requires. A colon in the password is fine.
238 55 : char *chttp_basic_auth_mp(ccol_memmgmt_procs *mp, const char *username,
239 : const char *password) {
240 55 : if (!username || !password) return NULL;
241 52 : if (strchr(username, ':')) return NULL;
242 82 : if (chttp_has_ctl(username) || chttp_has_ctl(password)) return NULL;
243 29 : return chttp_basic_auth_len(mp, username, strlen(username), password,
244 : strlen(password));
245 : }
246 :
247 : #ifdef RUNNING_UNIT_TESTS
248 : // Runs chttp_basic_auth_len() with fake lengths and tells whether the guard
249 : // refused, so a test can reach the guard without huge strings. Only lengths
250 : // over the guard's threshold are safe.
251 2 : bool chttp__basic_auth_overflow_guard_for_tests(ccol_memmgmt_procs *mp,
252 : const char *username,
253 : size_t fake_user_len,
254 : const char *password,
255 : size_t fake_pass_len) {
256 2 : char *r = chttp_basic_auth_len(mp, username, fake_user_len, password,
257 : fake_pass_len);
258 2 : bool rejected = (r == NULL);
259 2 : ccol__mem_free(mp, r);
260 2 : return rejected;
261 : }
262 : #endif
|