| 1 | /* |
| 2 | * Copyright (c) 2013-2022 Joris Vink <[email protected]> |
| 3 | * |
| 4 | * Permission to use, copy, modify, and distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/time.h> |
| 19 | |
| 20 | #include <ctype.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdarg.h> |
| 23 | #include <string.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <time.h> |
| 26 | #include <limits.h> |
| 27 | |
| 28 | #include "kore.h"cxx . . $ `<file>/include/kore/kore.h`/ |
| 29 | |
| 30 | static structcxx . . $ $anonymous_type_3ae283e7918f36df_0# { |
| 31 | char *namecxx . . $ $anonymous_type_3ae283e7918f36df_0#name.; |
| 32 | int valuecxx . . $ $anonymous_type_3ae283e7918f36df_0#value.; |
| 33 | } month_namescxx . . $ month_names.[] = { |
| 34 | { "Jan", 0 }, |
| 35 | { "Feb", 1 }, |
| 36 | { "Mar", 2 }, |
| 37 | { "Apr", 3 }, |
| 38 | { "May", 4 }, |
| 39 | { "Jun", 5 }, |
| 40 | { "Jul", 6 }, |
| 41 | { "Aug", 7 }, |
| 42 | { "Sep", 8 }, |
| 43 | { "Oct", 9 }, |
| 44 | { "Nov", 10 }, |
| 45 | { "Dec", 11 }, |
| 46 | { NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0 }, |
| 47 | }; |
| 48 | |
| 49 | static void fatal_logcxx . . $ fatal_log(ac175dd46a4e4d7f).(const char *, va_listcxx . . $ va_list#); |
| 50 | static int utils_base64_encodecxx . . $ utils_base64_encode(7ffd3441919b2dc7).(const void *, size_t, char **, |
| 51 | const char *, int); |
| 52 | static int utils_base64_decodecxx . . $ utils_base64_decode(d7df88a5fd644616).(const char *, u_int8_tcxx . . $ u_int8_t# **, |
| 53 | size_t *, const char *, int); |
| 54 | static int utils_x509name_tobufcxx . . $ utils_x509name_tobuf(9d1db61f1a65c689).(void *, int, int, const char *, |
| 55 | const void *, size_t, int); |
| 56 | |
| 57 | static char b64_tablecxx . . $ b64_table.[] = \ |
| 58 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 59 | |
| 60 | static char b64url_tablecxx . . $ b64url_table.[] = \ |
| 61 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; |
| 62 | |
| 63 | /* b64_table and b64url_table are the same size. */ |
| 64 | #define B64_TABLE_LENcxx . . $ `src/utils.c:64:9`! (sizeof(b64_table)) |
| 65 | |
| 66 | size_t |
| 67 | kore_strlcpycxx . . $ kore_strlcpy(55abe9014c6d0e39).(char *dst, const char *src, const size_t len) |
| 68 | { |
| 69 | char *d = dst; |
| 70 | const char *s = src; |
| 71 | const char *end = dst + len - 1; |
| 72 | |
| 73 | if (len == 0) |
| 74 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("kore_strlcpy: len == 0"); |
| 75 | |
| 76 | while ((*d = *s) != '\0') { |
| 77 | if (d == end) { |
| 78 | *d = '\0'; |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | d++; |
| 83 | s++; |
| 84 | } |
| 85 | |
| 86 | while (*s != '\0') |
| 87 | s++; |
| 88 | |
| 89 | return (s - src); |
| 90 | } |
| 91 | |
| 92 | int |
| 93 | kore_snprintfcxx . . $ kore_snprintf(25c15efbc44ee233).(char *str, size_t size, int *len, const char *fmt, ...) |
| 94 | { |
| 95 | int l; |
| 96 | va_listcxx . . $ va_list# args; |
| 97 | |
| 98 | va_startcxx . . $ `<external>/8105065126723003039/stdarg.h:50:9`!(args, fmt); |
| 99 | l = vsnprintfcxx . . $ vsnprintf(a85eea89d1bd834f).(str, size, fmt, args); |
| 100 | va_endcxx . . $ `<external>/8105065126723003039/stdarg.h:52:9`!(args); |
| 101 | |
| 102 | if (l == -1 || (size_t)l >= size) |
| 103 | return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!); |
| 104 | |
| 105 | if (len != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 106 | *len = l; |
| 107 | |
| 108 | return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!); |
| 109 | } |
| 110 | |
| 111 | long long |
| 112 | kore_strtonumcxx . . $ kore_strtonum(1c42d1a447034a9d).(const char *str, int base, long long min, long long max, int *err) |
| 113 | { |
| 114 | long long l; |
| 115 | char *ep; |
| 116 | |
| 117 | if (min > max) { |
| 118 | *err = KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!; |
| 119 | return (0); |
| 120 | } |
| 121 | |
| 122 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = 0; |
| 123 | l = strtollcxx . . $ strtoll(c50972e7488cbac6).(str, &ep, base); |
| 124 | if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != 0 || str == ep || *ep != '\0') { |
| 125 | *err = KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!; |
| 126 | return (0); |
| 127 | } |
| 128 | |
| 129 | if (l < min) { |
| 130 | *err = KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!; |
| 131 | return (0); |
| 132 | } |
| 133 | |
| 134 | if (l > max) { |
| 135 | *err = KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!; |
| 136 | return (0); |
| 137 | } |
| 138 | |
| 139 | *err = KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!; |
| 140 | return (l); |
| 141 | } |
| 142 | |
| 143 | u_int64_tcxx . . $ u_int64_t# |
| 144 | kore_strtonum64cxx . . $ kore_strtonum64(6aece162c3872803).(const char *str, int sign, int *err) |
| 145 | { |
| 146 | u_int64_tcxx . . $ u_int64_t# l; |
| 147 | long long ll; |
| 148 | char *ep; |
| 149 | int check; |
| 150 | |
| 151 | l = 0; |
| 152 | check = 1; |
| 153 | |
| 154 | ll = strtollcxx . . $ strtoll(c50972e7488cbac6).(str, &ep, 10); |
| 155 | if ((errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`! || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ERANGEcxx . . $ `<external>/8987895939074593727/errno-base.h:38:9`!) && |
| 156 | (ll == LLONG_MINcxx . . $ `<external>/67164224788392828/limits.h:140:10`! || ll == LLONG_MAXcxx . . $ `<external>/67164224788392828/limits.h:142:10`!)) { |
| 157 | if (sign) { |
| 158 | *err = KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!; |
| 159 | return (0); |
| 160 | } |
| 161 | |
| 162 | check = 0; |
| 163 | } |
| 164 | |
| 165 | if (!sign) { |
| 166 | l = strtoullcxx . . $ strtoull(fa707f2e7d9ad629).(str, &ep, 10); |
| 167 | if ((errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`! || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ERANGEcxx . . $ `<external>/8987895939074593727/errno-base.h:38:9`!) && l == ULONG_MAXcxx . . $ `<external>/67164224788392828/limits.h:135:9`!) { |
| 168 | *err = KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!; |
| 169 | return (0); |
| 170 | } |
| 171 | |
| 172 | if (check && ll < 0) { |
| 173 | *err = KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!; |
| 174 | return (0); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if (str == ep || *ep != '\0') { |
| 179 | *err = KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!; |
| 180 | return (0); |
| 181 | } |
| 182 | |
| 183 | *err = KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!; |
| 184 | return ((sign) ? (u_int64_tcxx . . $ u_int64_t#)ll : l); |
| 185 | } |
| 186 | |
| 187 | double |
| 188 | kore_strtodoublecxx . . $ kore_strtodouble(bb8c3842a270ac85).(const char *str, long double min, long double max, int *err) |
| 189 | { |
| 190 | double d; |
| 191 | char *ep; |
| 192 | |
| 193 | if (min > max) { |
| 194 | *err = KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!; |
| 195 | return (0); |
| 196 | } |
| 197 | |
| 198 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = 0; |
| 199 | d = strtodcxx . . $ strtod(3dfe5620a0d3160e).(str, &ep); |
| 200 | if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ERANGEcxx . . $ `<external>/8987895939074593727/errno-base.h:38:9`! || str == ep || *ep != '\0') { |
| 201 | *err = KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!; |
| 202 | return (0); |
| 203 | } |
| 204 | |
| 205 | if (d < min) { |
| 206 | *err = KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!; |
| 207 | return (0); |
| 208 | } |
| 209 | |
| 210 | if (d > max) { |
| 211 | *err = KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!; |
| 212 | return (0); |
| 213 | } |
| 214 | |
| 215 | *err = KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!; |
| 216 | return (d); |
| 217 | } |
| 218 | |
| 219 | int |
| 220 | kore_split_stringcxx . . $ kore_split_string(87b756e43bd5489c).(char *input, const char *delim, char **out, size_t ele) |
| 221 | { |
| 222 | int count; |
| 223 | char **ap; |
| 224 | |
| 225 | if (ele == 0) |
| 226 | return (0); |
| 227 | |
| 228 | count = 0; |
| 229 | for (ap = out; ap < &out[ele - 1] && |
| 230 | (*ap = strsepcxx . . $ strsep(f4782bac5b8a260e).(&input, delim)) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;) { |
| 231 | if (**ap != '\0') { |
| 232 | ap++; |
| 233 | count++; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | *ap = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 238 | return (count); |
| 239 | } |
| 240 | |
| 241 | void |
| 242 | kore_strip_charscxx . . $ kore_strip_chars(28f0fb8875227cc8).(char *in, const char strip, char **out) |
| 243 | { |
| 244 | u_int32_tcxx . . $ u_int32_t# len; |
| 245 | char *s, *p; |
| 246 | |
| 247 | len = strlencxx . . $ strlen(751346f8406fd082).(in); |
| 248 | *out = kore_malloccxx . . $ kore_malloc(bfcfa4d6b7f7ef64).(len + 1); |
| 249 | p = *out; |
| 250 | |
| 251 | for (s = in; s < (in + len); s++) { |
| 252 | if (*s == strip) |
| 253 | continue; |
| 254 | |
| 255 | *p++ = *s; |
| 256 | } |
| 257 | |
| 258 | *p = '\0'; |
| 259 | } |
| 260 | |
| 261 | time_tcxx . . $ time_t# |
| 262 | kore_date_to_timecxx . . $ kore_date_to_time(3c95bd14d5d5d286).(const char *http_date) |
| 263 | { |
| 264 | time_tcxx . . $ time_t# t; |
| 265 | int err, i; |
| 266 | struct tmcxx . . $ tm# tm, *ltm; |
| 267 | char *args[7], *tbuf[5], *sdup; |
| 268 | |
| 269 | timecxx . . $ time(7f38134db6521d23).(&t); |
| 270 | ltm = localtimecxx . . $ localtime(9477a1348abbbc5).(&t); |
| 271 | sdup = kore_strdupcxx . . $ kore_strdup(a8b8e32347182212).(http_date); |
| 272 | |
| 273 | t = KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!; |
| 274 | |
| 275 | if (kore_split_stringcxx . . $ kore_split_string(87b756e43bd5489c).(sdup, " ", args, 7) != 6) { |
| 276 | kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "misformed http-date: '%s'", http_date); |
| 277 | goto out; |
| 278 | } |
| 279 | |
| 280 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&tm, 0, sizeof(tm)); |
| 281 | |
| 282 | tm.tm_yearcxx . . $ tm#tm_year. = kore_strtonumcxx . . $ kore_strtonum(1c42d1a447034a9d).(args[3], 10, 1900, 2068, &err) - 1900; |
| 283 | if (err == KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!) { |
| 284 | kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "misformed year in http-date: '%s'", |
| 285 | http_date); |
| 286 | goto out; |
| 287 | } |
| 288 | |
| 289 | for (i = 0; month_namescxx . . $ month_names.[i].namecxx . . $ $anonymous_type_3ae283e7918f36df_0#name. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; i++) { |
| 290 | if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(month_namescxx . . $ month_names.[i].namecxx . . $ $anonymous_type_3ae283e7918f36df_0#name., args[2])) { |
| 291 | tm.tm_moncxx . . $ tm#tm_mon. = month_namescxx . . $ month_names.[i].valuecxx . . $ $anonymous_type_3ae283e7918f36df_0#value.; |
| 292 | break; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | if (month_namescxx . . $ month_names.[i].namecxx . . $ $anonymous_type_3ae283e7918f36df_0#name. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 297 | kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "misformed month in http-date: '%s'", |
| 298 | http_date); |
| 299 | goto out; |
| 300 | } |
| 301 | |
| 302 | tm.tm_mdaycxx . . $ tm#tm_mday. = kore_strtonumcxx . . $ kore_strtonum(1c42d1a447034a9d).(args[1], 10, 1, 31, &err); |
| 303 | if (err == KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!) { |
| 304 | kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "misformed mday in http-date: '%s'", |
| 305 | http_date); |
| 306 | goto out; |
| 307 | } |
| 308 | |
| 309 | if (kore_split_stringcxx . . $ kore_split_string(87b756e43bd5489c).(args[4], ":", tbuf, 5) != 3) { |
| 310 | kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "misformed HH:MM:SS in http-date: '%s'", |
| 311 | http_date); |
| 312 | goto out; |
| 313 | } |
| 314 | |
| 315 | tm.tm_hourcxx . . $ tm#tm_hour. = kore_strtonumcxx . . $ kore_strtonum(1c42d1a447034a9d).(tbuf[0], 10, 0, 23, &err); |
| 316 | if (err == KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!) { |
| 317 | kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "misformed hour in http-date: '%s'", |
| 318 | http_date); |
| 319 | goto out; |
| 320 | } |
| 321 | |
| 322 | tm.tm_mincxx . . $ tm#tm_min. = kore_strtonumcxx . . $ kore_strtonum(1c42d1a447034a9d).(tbuf[1], 10, 0, 59, &err); |
| 323 | if (err == KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!) { |
| 324 | kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "misformed minutes in http-date: '%s'", |
| 325 | http_date); |
| 326 | goto out; |
| 327 | } |
| 328 | |
| 329 | tm.tm_seccxx . . $ tm#tm_sec. = kore_strtonumcxx . . $ kore_strtonum(1c42d1a447034a9d).(tbuf[2], 10, 0, 60, &err); |
| 330 | if (err == KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!) { |
| 331 | kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "misformed seconds in http-date: '%s'", |
| 332 | http_date); |
| 333 | goto out; |
| 334 | } |
| 335 | |
| 336 | tm.tm_isdstcxx . . $ tm#tm_isdst. = ltm->tm_isdstcxx . . $ tm#tm_isdst.; |
| 337 | t = mktimecxx . . $ mktime(237c3516b2cfac2c).(&tm) + ltm->tm_gmtoffcxx . . $ tm#tm_gmtoff.; |
| 338 | if (t == -1) { |
| 339 | t = 0; |
| 340 | kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "mktime() on '%s' failed", http_date); |
| 341 | } |
| 342 | |
| 343 | out: |
| 344 | kore_freecxx . . $ kore_free(5d22bdacc48458e8).(sdup); |
| 345 | return (t); |
| 346 | } |
| 347 | |
| 348 | char * |
| 349 | kore_time_to_datecxx . . $ kore_time_to_date(e7db8e11a8f87cd6).(time_tcxx . . $ time_t# now) |
| 350 | { |
| 351 | struct tmcxx . . $ tm# *tm; |
| 352 | static time_tcxx . . $ time_t# last = 0; |
| 353 | static char tbuf[32]; |
| 354 | |
| 355 | if (now != last) { |
| 356 | last = now; |
| 357 | |
| 358 | tm = gmtimecxx . . $ gmtime(9477a1348abbbc5).(&now); |
| 359 | if (!strftimecxx . . $ strftime(98e75925167c4f1b).(tbuf, sizeof(tbuf), "%a, %d %b %Y %T GMT", tm)) |
| 360 | return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 361 | } |
| 362 | |
| 363 | return (tbuf); |
| 364 | } |
| 365 | |
| 366 | u_int64_tcxx . . $ u_int64_t# |
| 367 | kore_time_mscxx . . $ kore_time_ms(f708dce9a8856c70).(void) |
| 368 | { |
| 369 | struct timespeccxx . . $ timespec# ts; |
| 370 | |
| 371 | (void)clock_gettimecxx . . $ clock_gettime(65aaefb2502b4987).(CLOCK_MONOTONICcxx . . $ `<external>/89459239771508533/time.h:48:10`!, &ts); |
| 372 | |
| 373 | return ((u_int64_tcxx . . $ u_int64_t#)(ts.tv_seccxx . . $ timespec#tv_sec. * 1000 + (ts.tv_nseccxx . . $ timespec#tv_nsec. / 1000000))); |
| 374 | } |
| 375 | |
| 376 | int |
| 377 | kore_base64url_encodecxx . . $ kore_base64url_encode(addc80fedde60fc).(const void *data, size_t len, char **out, int flags) |
| 378 | { |
| 379 | return (utils_base64_encodecxx . . $ utils_base64_encode(7ffd3441919b2dc7).(data, len, out, b64url_tablecxx . . $ b64url_table., flags)); |
| 380 | } |
| 381 | |
| 382 | int |
| 383 | kore_base64_encodecxx . . $ kore_base64_encode(e8e709264bfeee74).(const void *data, size_t len, char **out) |
| 384 | { |
| 385 | return (utils_base64_encodecxx . . $ utils_base64_encode(7ffd3441919b2dc7).(data, len, out, b64_tablecxx . . $ b64_table., 0)); |
| 386 | } |
| 387 | |
| 388 | int |
| 389 | kore_base64url_decodecxx . . $ kore_base64url_decode(3852b0f706c07621).(const char *in, u_int8_tcxx . . $ u_int8_t# **out, size_t *olen, int flags) |
| 390 | { |
| 391 | return (utils_base64_decodecxx . . $ utils_base64_decode(d7df88a5fd644616).(in, out, olen, b64url_tablecxx . . $ b64url_table., flags)); |
| 392 | } |
| 393 | |
| 394 | int |
| 395 | kore_base64_decodecxx . . $ kore_base64_decode(a4f12eb4cc00fe0a).(const char *in, u_int8_tcxx . . $ u_int8_t# **out, size_t *olen) |
| 396 | { |
| 397 | return (utils_base64_decodecxx . . $ utils_base64_decode(d7df88a5fd644616).(in, out, olen, b64_tablecxx . . $ b64_table., 0)); |
| 398 | } |
| 399 | |
| 400 | void * |
| 401 | kore_mem_findcxx . . $ kore_mem_find(d6408e6ff8ad1a7e).(void *src, size_t slen, const void *needle, size_t len) |
| 402 | { |
| 403 | size_t pos; |
| 404 | |
| 405 | for (pos = 0; pos < slen; pos++) { |
| 406 | if ( *((u_int8_tcxx . . $ u_int8_t# *)src + pos) != *(const u_int8_tcxx . . $ u_int8_t# *)needle) |
| 407 | continue; |
| 408 | |
| 409 | if ((slen - pos) < len) |
| 410 | return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 411 | |
| 412 | if (!memcmpcxx . . $ memcmp(36f949851efa3055).((u_int8_tcxx . . $ u_int8_t# *)src + pos, needle, len)) |
| 413 | return ((u_int8_tcxx . . $ u_int8_t# *)src + pos); |
| 414 | } |
| 415 | |
| 416 | return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 417 | } |
| 418 | |
| 419 | char * |
| 420 | kore_text_trimcxx . . $ kore_text_trim(dff97f9eb74e1544).(char *string, size_t len) |
| 421 | { |
| 422 | char *end; |
| 423 | |
| 424 | if (len == 0) |
| 425 | return (string); |
| 426 | |
| 427 | end = (string + len) - 1; |
| 428 | while (isspacecxx . . $ `<external>/17904084882347303253/ctype.h:197:10`!(*(unsigned char *)string) && string < end) |
| 429 | string++; |
| 430 | |
| 431 | while (isspacecxx . . $ `<external>/17904084882347303253/ctype.h:197:10`!(*(unsigned char *)end) && end > string) |
| 432 | *(end)-- = '\0'; |
| 433 | |
| 434 | return (string); |
| 435 | } |
| 436 | |
| 437 | char * |
| 438 | kore_read_linecxx . . $ kore_read_line(8c0cb784ce79381a).(FILEcxx . . $ FILE# *fp, char *in, size_t len) |
| 439 | { |
| 440 | char *p, *t; |
| 441 | |
| 442 | if (fgetscxx . . $ fgets(efb0aaba7ac866ae).(in, len, fp) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 443 | return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 444 | |
| 445 | p = in; |
| 446 | in[strcspncxx . . $ strcspn(335c1dc86f5c867f).(in, "\n")] = '\0'; |
| 447 | |
| 448 | while (isspacecxx . . $ `<external>/17904084882347303253/ctype.h:197:10`!(*(unsigned char *)p)) |
| 449 | p++; |
| 450 | |
| 451 | if (p[0] == '#' || p[0] == '\0') { |
| 452 | p[0] = '\0'; |
| 453 | return (p); |
| 454 | } |
| 455 | |
| 456 | for (t = p; *t != '\0'; t++) { |
| 457 | if (*t == '\t') |
| 458 | *t = ' '; |
| 459 | } |
| 460 | |
| 461 | return (p); |
| 462 | } |
| 463 | |
| 464 | const char * |
| 465 | kore_worker_namecxx . . $ kore_worker_name(f7ae7e56280f18ae).(int id) |
| 466 | { |
| 467 | static char buf[64]; |
| 468 | |
| 469 | switch (id) { |
| 470 | case KORE_WORKER_KEYMGRcxx . . $ `include/kore/kore.h:652:9`!: |
| 471 | (void)snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, sizeof(buf), "[keymgr]"); |
| 472 | break; |
| 473 | case KORE_WORKER_ACMEcxx . . $ `include/kore/kore.h:653:9`!: |
| 474 | (void)snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, sizeof(buf), "[acme]"); |
| 475 | break; |
| 476 | default: |
| 477 | (void)snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, sizeof(buf), "[wrk %d]", id); |
| 478 | break; |
| 479 | } |
| 480 | |
| 481 | return (buf); |
| 482 | } |
| 483 | |
| 484 | int |
| 485 | kore_x509_issuer_namecxx . . $ kore_x509_issuer_name(9d8cf00306b7b334).(struct connectioncxx . . $ connection# *c, char **out, int flags) |
| 486 | { |
| 487 | struct kore_bufcxx . . $ kore_buf# buf; |
| 488 | KORE_X509_NAMEScxx . . $ KORE_X509_NAMES# *name; |
| 489 | |
| 490 | if ((name = kore_tls_x509_issuer_namecxx . . $ kore_tls_x509_issuer_name(6f423c880c279b5c).(c)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 491 | return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!); |
| 492 | |
| 493 | kore_buf_initcxx . . $ kore_buf_init(bd90e0817a56138f).(&buf, 1024); |
| 494 | |
| 495 | if (!kore_tls_x509name_foreachcxx . . $ kore_tls_x509name_foreach(cf8858a95eca5aae).(name, flags, &buf, |
| 496 | utils_x509name_tobufcxx . . $ utils_x509name_tobuf(9d1db61f1a65c689).)) { |
| 497 | kore_buf_cleanupcxx . . $ kore_buf_cleanup(2648f45d13d14762).(&buf); |
| 498 | return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!); |
| 499 | } |
| 500 | |
| 501 | *out = kore_buf_stringifycxx . . $ kore_buf_stringify(4bc5b263e10e2f7b).(&buf, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 502 | |
| 503 | buf.offsetcxx . . $ kore_buf#offset. = 0; |
| 504 | buf.datacxx . . $ kore_buf#data. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 505 | |
| 506 | return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!); |
| 507 | } |
| 508 | |
| 509 | int |
| 510 | kore_x509_subject_namecxx . . $ kore_x509_subject_name(9d8cf00306b7b334).(struct connectioncxx . . $ connection# *c, char **out, int flags) |
| 511 | { |
| 512 | struct kore_bufcxx . . $ kore_buf# buf; |
| 513 | KORE_X509_NAMEScxx . . $ KORE_X509_NAMES# *name; |
| 514 | |
| 515 | if ((name = kore_tls_x509_subject_namecxx . . $ kore_tls_x509_subject_name(6f423c880c279b5c).(c)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 516 | return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!); |
| 517 | |
| 518 | kore_buf_initcxx . . $ kore_buf_init(bd90e0817a56138f).(&buf, 1024); |
| 519 | |
| 520 | if (!kore_tls_x509name_foreachcxx . . $ kore_tls_x509name_foreach(cf8858a95eca5aae).(name, flags, &buf, |
| 521 | utils_x509name_tobufcxx . . $ utils_x509name_tobuf(9d1db61f1a65c689).)) { |
| 522 | kore_buf_cleanupcxx . . $ kore_buf_cleanup(2648f45d13d14762).(&buf); |
| 523 | return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!); |
| 524 | } |
| 525 | |
| 526 | *out = kore_buf_stringifycxx . . $ kore_buf_stringify(4bc5b263e10e2f7b).(&buf, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 527 | |
| 528 | buf.offsetcxx . . $ kore_buf#offset. = 0; |
| 529 | buf.datacxx . . $ kore_buf#data. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 530 | |
| 531 | return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!); |
| 532 | } |
| 533 | |
| 534 | void |
| 535 | fatalcxx . . $ fatal(f6bc48f0426f26ee).(const char *fmt, ...) |
| 536 | { |
| 537 | va_listcxx . . $ va_list# args; |
| 538 | |
| 539 | va_startcxx . . $ `<external>/8105065126723003039/stdarg.h:50:9`!(args, fmt); |
| 540 | fatal_logcxx . . $ fatal_log(ac175dd46a4e4d7f).(fmt, args); |
| 541 | va_endcxx . . $ `<external>/8105065126723003039/stdarg.h:52:9`!(args); |
| 542 | |
| 543 | exitcxx . . $ exit(3ca292c338df9bb3).(1); |
| 544 | } |
| 545 | |
| 546 | void |
| 547 | fatalxcxx . . $ fatalx(f6bc48f0426f26ee).(const char *fmt, ...) |
| 548 | { |
| 549 | va_listcxx . . $ va_list# args; |
| 550 | |
| 551 | /* In case people call fatalx() from the parent context. */ |
| 552 | if (workercxx . . $ worker. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 553 | kore_msg_sendcxx . . $ kore_msg_send(6afaa3cccbe95a50).(KORE_MSG_PARENTcxx . . $ `include/kore/kore.h:679:9`!, KORE_MSG_FATALXcxx . . $ `include/kore/kore.h:672:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0); |
| 554 | |
| 555 | va_startcxx . . $ `<external>/8105065126723003039/stdarg.h:50:9`!(args, fmt); |
| 556 | fatal_logcxx . . $ fatal_log(ac175dd46a4e4d7f).(fmt, args); |
| 557 | va_endcxx . . $ `<external>/8105065126723003039/stdarg.h:52:9`!(args); |
| 558 | |
| 559 | exitcxx . . $ exit(3ca292c338df9bb3).(1); |
| 560 | } |
| 561 | |
| 562 | static void |
| 563 | fatal_logcxx . . $ fatal_log(ac175dd46a4e4d7f).(const char *fmt, va_listcxx . . $ va_list# args) |
| 564 | { |
| 565 | char buf[2048]; |
| 566 | |
| 567 | (void)vsnprintfcxx . . $ vsnprintf(a85eea89d1bd834f).(buf, sizeof(buf), fmt, args); |
| 568 | kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!, "fatal: %s", buf); |
| 569 | |
| 570 | if (workercxx . . $ worker. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! && workercxx . . $ worker.->idcxx . . $ kore_worker#id. == KORE_WORKER_KEYMGRcxx . . $ `include/kore/kore.h:652:9`!) |
| 571 | kore_keymgr_cleanupcxx . . $ kore_keymgr_cleanup(d4f767463ce0a6b3).(1); |
| 572 | } |
| 573 | |
| 574 | static int |
| 575 | utils_x509name_tobufcxx . . $ utils_x509name_tobuf(9d1db61f1a65c689).(void *udata, int islast, int nid, const char *field, |
| 576 | const void *data, size_t len, int flags) |
| 577 | { |
| 578 | struct kore_bufcxx . . $ kore_buf# *buf = udata; |
| 579 | |
| 580 | if (flags & KORE_X509_COMMON_NAME_ONLYcxx . . $ `include/kore/kore.h:121:9`!) { |
| 581 | if (nid == KORE_X509_NAME_COMMON_NAMEcxx . . $ `include/kore/kore.h:817:9`!) |
| 582 | kore_buf_appendcxx . . $ kore_buf_append(4909ebf68ec24f4d).(buf, data, len); |
| 583 | } else { |
| 584 | kore_buf_appendfcxx . . $ kore_buf_appendf(e492ffcef53d871e).(buf, "%s=", field); |
| 585 | kore_buf_appendcxx . . $ kore_buf_append(4909ebf68ec24f4d).(buf, data, len); |
| 586 | if (!islast) |
| 587 | kore_buf_appendfcxx . . $ kore_buf_appendf(e492ffcef53d871e).(buf, " "); |
| 588 | } |
| 589 | |
| 590 | return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!); |
| 591 | } |
| 592 | |
| 593 | static int |
| 594 | utils_base64_encodecxx . . $ utils_base64_encode(7ffd3441919b2dc7).(const void *data, size_t len, char **out, |
| 595 | const char *table, int flags) |
| 596 | { |
| 597 | u_int8_tcxx . . $ u_int8_t# n; |
| 598 | size_t nb; |
| 599 | const u_int8_tcxx . . $ u_int8_t# *ptr; |
| 600 | u_int32_tcxx . . $ u_int32_t# bytes; |
| 601 | struct kore_bufcxx . . $ kore_buf# result; |
| 602 | |
| 603 | nb = 0; |
| 604 | ptr = data; |
| 605 | kore_buf_initcxx . . $ kore_buf_init(bd90e0817a56138f).(&result, (len / 3) * 4); |
| 606 | |
| 607 | while (len > 0) { |
| 608 | if (len > 2) { |
| 609 | nb = 3; |
| 610 | bytes = *ptr++ << 16; |
| 611 | bytes |= *ptr++ << 8; |
| 612 | bytes |= *ptr++; |
| 613 | } else if (len > 1) { |
| 614 | nb = 2; |
| 615 | bytes = *ptr++ << 16; |
| 616 | bytes |= *ptr++ << 8; |
| 617 | } else if (len == 1) { |
| 618 | nb = 1; |
| 619 | bytes = *ptr++ << 16; |
| 620 | } else { |
| 621 | kore_buf_cleanupcxx . . $ kore_buf_cleanup(2648f45d13d14762).(&result); |
| 622 | return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!); |
| 623 | } |
| 624 | |
| 625 | n = (bytes >> 18) & 0x3f; |
| 626 | kore_buf_appendcxx . . $ kore_buf_append(4909ebf68ec24f4d).(&result, &(table[n]), 1); |
| 627 | n = (bytes >> 12) & 0x3f; |
| 628 | kore_buf_appendcxx . . $ kore_buf_append(4909ebf68ec24f4d).(&result, &(table[n]), 1); |
| 629 | if (nb > 1) { |
| 630 | n = (bytes >> 6) & 0x3f; |
| 631 | kore_buf_appendcxx . . $ kore_buf_append(4909ebf68ec24f4d).(&result, &(table[n]), 1); |
| 632 | if (nb > 2) { |
| 633 | n = bytes & 0x3f; |
| 634 | kore_buf_appendcxx . . $ kore_buf_append(4909ebf68ec24f4d).(&result, &(table[n]), 1); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | len -= nb; |
| 639 | } |
| 640 | |
| 641 | if (!(flags & KORE_BASE64_RAWcxx . . $ `include/kore/kore.h:95:9`!)) { |
| 642 | switch (nb) { |
| 643 | case 1: |
| 644 | kore_buf_appendfcxx . . $ kore_buf_appendf(e492ffcef53d871e).(&result, "=="); |
| 645 | break; |
| 646 | case 2: |
| 647 | kore_buf_appendfcxx . . $ kore_buf_appendf(e492ffcef53d871e).(&result, "="); |
| 648 | break; |
| 649 | case 3: |
| 650 | break; |
| 651 | default: |
| 652 | kore_buf_cleanupcxx . . $ kore_buf_cleanup(2648f45d13d14762).(&result); |
| 653 | return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | /* result.data gets taken over so no need to cleanup result. */ |
| 658 | *out = kore_buf_stringifycxx . . $ kore_buf_stringify(4bc5b263e10e2f7b).(&result, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 659 | |
| 660 | return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!); |
| 661 | } |
| 662 | |
| 663 | static int |
| 664 | utils_base64_decodecxx . . $ utils_base64_decode(d7df88a5fd644616).(const char *in, u_int8_tcxx . . $ u_int8_t# **out, size_t *olen, |
| 665 | const char *table, int flags) |
| 666 | { |
| 667 | int i, c; |
| 668 | u_int8_tcxx . . $ u_int8_t# d, n, o; |
| 669 | struct kore_bufcxx . . $ kore_buf# *res, buf; |
| 670 | const char *ptr, *pad; |
| 671 | u_int32_tcxx . . $ u_int32_t# b, len, plen, idx; |
| 672 | |
| 673 | i = 4; |
| 674 | b = 0; |
| 675 | d = 0; |
| 676 | c = 0; |
| 677 | len = strlencxx . . $ strlen(751346f8406fd082).(in); |
| 678 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&buf, 0, sizeof(buf)); |
| 679 | |
| 680 | if (flags & KORE_BASE64_RAWcxx . . $ `include/kore/kore.h:95:9`!) { |
| 681 | switch (len % 4) { |
| 682 | case 2: |
| 683 | plen = 2; |
| 684 | pad = "=="; |
| 685 | break; |
| 686 | case 3: |
| 687 | plen = 1; |
| 688 | pad = "="; |
| 689 | break; |
| 690 | default: |
| 691 | return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!); |
| 692 | } |
| 693 | |
| 694 | kore_buf_initcxx . . $ kore_buf_init(bd90e0817a56138f).(&buf, len + plen); |
| 695 | kore_buf_appendcxx . . $ kore_buf_append(4909ebf68ec24f4d).(&buf, in, len); |
| 696 | kore_buf_appendcxx . . $ kore_buf_append(4909ebf68ec24f4d).(&buf, pad, plen); |
| 697 | |
| 698 | len = len + plen; |
| 699 | ptr = (const char *)buf.datacxx . . $ kore_buf#data.; |
| 700 | } else { |
| 701 | ptr = in; |
| 702 | } |
| 703 | |
| 704 | res = kore_buf_alloccxx . . $ kore_buf_alloc(ae982dcc82edde78).(len); |
| 705 | |
| 706 | for (idx = 0; idx < len; idx++) { |
| 707 | c = ptr[idx]; |
| 708 | if (c == '=') |
| 709 | break; |
| 710 | |
| 711 | for (o = 0; o < B64_TABLE_LENcxx . . $ `src/utils.c:64:9`!; o++) { |
| 712 | if (table[o] == c) { |
| 713 | d = o; |
| 714 | break; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | if (o == B64_TABLE_LENcxx . . $ `src/utils.c:64:9`!) { |
| 719 | *out = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 720 | kore_buf_freecxx . . $ kore_buf_free(2648f45d13d14762).(res); |
| 721 | kore_buf_cleanupcxx . . $ kore_buf_cleanup(2648f45d13d14762).(&buf); |
| 722 | return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!); |
| 723 | } |
| 724 | |
| 725 | b |= (d & 0x3f) << ((i - 1) * 6); |
| 726 | i--; |
| 727 | if (i == 0) { |
| 728 | for (i = 2; i >= 0; i--) { |
| 729 | n = (b >> (8 * i)); |
| 730 | kore_buf_appendcxx . . $ kore_buf_append(4909ebf68ec24f4d).(res, &n, 1); |
| 731 | } |
| 732 | |
| 733 | b = 0; |
| 734 | i = 4; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | if (c == '=') { |
| 739 | if (i > 2) { |
| 740 | *out = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 741 | kore_buf_freecxx . . $ kore_buf_free(2648f45d13d14762).(res); |
| 742 | kore_buf_cleanupcxx . . $ kore_buf_cleanup(2648f45d13d14762).(&buf); |
| 743 | return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!); |
| 744 | } |
| 745 | |
| 746 | o = i; |
| 747 | for (i = 2; i >= o; i--) { |
| 748 | n = (b >> (8 * i)); |
| 749 | kore_buf_appendcxx . . $ kore_buf_append(4909ebf68ec24f4d).(res, &n, 1); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | kore_buf_cleanupcxx . . $ kore_buf_cleanup(2648f45d13d14762).(&buf); |
| 754 | *out = kore_buf_releasecxx . . $ kore_buf_release(d59a61570302ba3d).(res, olen); |
| 755 | |
| 756 | return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!); |
| 757 | } |