| 1 |
|
| #ifndef PSAML_B64_H |
| 2 |
|
| #define PSAML_B64_H |
| 3 |
|
| |
| 4 |
|
| /* Standard base64, RFC 4648 section 4, with padding. |
| 5 |
|
| * |
| 6 |
|
| * jws_abi.h has the base64url form only (section 5, unpadded), which is |
| 7 |
|
| * the wrong alphabet here: SAML carries the standard alphabet with `+` |
| 8 |
|
| * and `/` in SAMLRequest, SAMLResponse and every ds:X509Certificate, and |
| 9 |
|
| * decoding one as the other silently produces different bytes rather |
| 10 |
|
| * than an error. |
| 11 |
|
| * |
| 12 |
|
| * Decoding is strict, because this is an input parser at a security |
| 13 |
|
| * boundary. Strict means: |
| 14 |
|
| * - the standard alphabet only; `-` and `_` are refused rather than |
| 15 |
|
| * quietly accepted, so a url-form token cannot arrive here and |
| 16 |
|
| * decode to something plausible |
| 17 |
|
| * - length a multiple of four, padding only at the end, at most two |
| 18 |
|
| * `=`, and nothing after them |
| 19 |
|
| * - the unused low bits of the last group must be zero, so one |
| 20 |
|
| * encoding decodes from one input and a mutated tail is refused |
| 21 |
|
| * rather than ignored |
| 22 |
|
| * |
| 23 |
|
| * Whitespace is the one thing a caller chooses, and the choice is not |
| 24 |
|
| * cosmetic. Base64 inside XML content is line-wrapped by essentially |
| 25 |
|
| * every identity provider, so psaml_b64_decode_xml accepts space, tab, |
| 26 |
|
| * CR and LF between characters; a protocol field like SAMLResponse has |
| 27 |
|
| * no such excuse and goes through psaml_b64_decode, which refuses them. */ |
| 28 |
|
| |
| 29 |
|
| #include <string.h> |
| 30 |
|
| |
| 31 |
|
| static const char PSAML_B64_ALPHA[] = |
| 32 |
|
| "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 33 |
|
| |
| 34 |
|
| /* -1 not in the alphabet, -2 padding, -3 skippable whitespace */ |
| 35 |
176980 |
| PERL_STATIC_INLINE signed char psaml_b64_val(unsigned char c) { |
| 36 |
176980 |
▶ | if (c >= 'A' && c <= 'Z') return (signed char)(c - 'A'); |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= 'A' && c <= 'Z') return (signed char)(c - 'A'); |
147532 |
29448 |
176980 |
| if (c >= 'A' && c <= 'Z') return (signed char)(c - 'A'); |
77522 |
70010 |
147532 |
|
| 37 |
99458 |
▶ | if (c >= 'a' && c <= 'z') return (signed char)(c - 'a' + 26); |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= 'a' && c <= 'z') return (signed char)(c - 'a' + 26); |
70010 |
29448 |
99458 |
| if (c >= 'a' && c <= 'z') return (signed char)(c - 'a' + 26); |
70010 |
0 |
70010 |
|
| 38 |
29448 |
▶ | if (c >= '0' && c <= '9') return (signed char)(c - '0' + 52); |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= '0' && c <= '9') return (signed char)(c - '0' + 52); |
26236 |
3212 |
29448 |
| if (c >= '0' && c <= '9') return (signed char)(c - '0' + 52); |
25683 |
553 |
26236 |
|
| 39 |
3765 |
▶ | if (c == '+') return 62; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '+') return 62; |
1622 |
2143 |
3765 |
|
| 40 |
2143 |
▶ | if (c == '/') return 63; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '/') return 63; |
1342 |
801 |
2143 |
|
| 41 |
801 |
▶ | if (c == '=') return -2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '=') return -2; |
553 |
248 |
801 |
|
| 42 |
248 |
▶ | if (c == ' ' || c == '\t' || c == '\r' || c == '\n') return -3; |
Branch: 4 branches
| Branch | True | False | Total |
| if (c == ' ' || c == '\t' || c == '\r' || c == '\n') return -3; |
247 |
1 |
248 |
| if (c == ' ' || c == '\t' || c == '\r' || c == '\n') return -3; |
247 |
0 |
247 |
| if (c == ' ' || c == '\t' || c == '\r' || c == '\n') return -3; |
247 |
0 |
247 |
| if (c == ' ' || c == '\t' || c == '\r' || c == '\n') return -3; |
242 |
5 |
247 |
|
| 43 |
5 |
| return -1; |
| 44 |
|
| } |
| 45 |
|
| |
| 46 |
70 |
| PERL_STATIC_INLINE STRLEN psaml_b64_encoded_len(STRLEN n) { |
| 47 |
70 |
| return ((n + 2) / 3) * 4; |
| 48 |
|
| } |
| 49 |
|
| |
| 50 |
|
| /* Writes exactly psaml_b64_encoded_len(n) bytes; no NUL. */ |
| 51 |
70 |
| PERL_STATIC_INLINE STRLEN psaml_b64_encode(char *out, const unsigned char *in, STRLEN n) { |
| 52 |
70 |
| char *o = out; |
| 53 |
70 |
| STRLEN i = 0; |
| 54 |
10724 |
▶ | while (i + 3 <= n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i + 3 <= n) { |
10654 |
70 |
10724 |
|
| 55 |
10654 |
| unsigned v = ((unsigned)in[i] << 16) | ((unsigned)in[i+1] << 8) | in[i+2]; |
| 56 |
10654 |
| *o++ = PSAML_B64_ALPHA[(v >> 18) & 63]; |
| 57 |
10654 |
| *o++ = PSAML_B64_ALPHA[(v >> 12) & 63]; |
| 58 |
10654 |
| *o++ = PSAML_B64_ALPHA[(v >> 6) & 63]; |
| 59 |
10654 |
| *o++ = PSAML_B64_ALPHA[ v & 63]; |
| 60 |
10654 |
| i += 3; |
| 61 |
|
| } |
| 62 |
70 |
▶ | if (n - i == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n - i == 1) { |
24 |
46 |
70 |
|
| 63 |
24 |
| unsigned v = (unsigned)in[i] << 16; |
| 64 |
24 |
| *o++ = PSAML_B64_ALPHA[(v >> 18) & 63]; |
| 65 |
24 |
| *o++ = PSAML_B64_ALPHA[(v >> 12) & 63]; |
| 66 |
24 |
| *o++ = '='; |
| 67 |
24 |
| *o++ = '='; |
| 68 |
|
| } |
| 69 |
46 |
▶ | else if (n - i == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (n - i == 2) { |
9 |
37 |
46 |
|
| 70 |
9 |
| unsigned v = ((unsigned)in[i] << 16) | ((unsigned)in[i+1] << 8); |
| 71 |
9 |
| *o++ = PSAML_B64_ALPHA[(v >> 18) & 63]; |
| 72 |
9 |
| *o++ = PSAML_B64_ALPHA[(v >> 12) & 63]; |
| 73 |
9 |
| *o++ = PSAML_B64_ALPHA[(v >> 6) & 63]; |
| 74 |
9 |
| *o++ = '='; |
| 75 |
|
| } |
| 76 |
70 |
| return (STRLEN)(o - out); |
| 77 |
|
| } |
| 78 |
|
| |
| 79 |
|
| /* Upper bound on the decoded size, for allocation. */ |
| 80 |
395 |
| PERL_STATIC_INLINE STRLEN psaml_b64_decoded_max(STRLEN n) { |
| 81 |
395 |
| return (n / 4 + 1) * 3; |
| 82 |
|
| } |
| 83 |
|
| |
| 84 |
|
| /* Returns the decoded length, or (STRLEN)-1 on any refusal. `out` must |
| 85 |
|
| * have room for psaml_b64_decoded_max(n). allow_ws skips space, tab, CR |
| 86 |
|
| * and LF anywhere between characters. */ |
| 87 |
395 |
| PERL_STATIC_INLINE STRLEN psaml_b64_decode_ws(unsigned char *out, const char *in, |
| 88 |
|
| STRLEN n, int allow_ws) { |
| 89 |
395 |
| unsigned char *o = out; |
| 90 |
395 |
| unsigned quad = 0; |
| 91 |
395 |
| int have = 0; /* alphabet characters in the current group */ |
| 92 |
395 |
| int pad = 0; |
| 93 |
|
| STRLEN i; |
| 94 |
|
| |
| 95 |
177361 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
176980 |
381 |
177361 |
|
| 96 |
176980 |
| signed char v = psaml_b64_val((unsigned char)in[i]); |
| 97 |
176980 |
▶ | if (v == -3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (v == -3) { |
243 |
176737 |
176980 |
|
| 98 |
243 |
▶ | if (allow_ws) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (allow_ws) continue; |
240 |
3 |
243 |
|
| 99 |
3 |
| return (STRLEN)-1; |
| 100 |
|
| } |
| 101 |
176737 |
▶ | if (v == -1) return (STRLEN)-1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (v == -1) return (STRLEN)-1; |
5 |
176732 |
176737 |
|
| 102 |
176732 |
▶ | if (v == -2) { /* '=' */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (v == -2) { /* '=' */ |
553 |
176179 |
176732 |
|
| 103 |
553 |
▶ | if (have < 2) return (STRLEN)-1; /* "=" or "A=" is never valid */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (have < 2) return (STRLEN)-1; /* "=" or "A=" is never valid */ |
3 |
550 |
553 |
|
| 104 |
550 |
▶ | if (++pad > 2) return (STRLEN)-1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (++pad > 2) return (STRLEN)-1; |
0 |
550 |
550 |
|
| 105 |
550 |
| quad <<= 6; |
| 106 |
550 |
| have++; |
| 107 |
550 |
▶ | if (have == 4) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (have == 4) { |
370 |
180 |
550 |
|
| 108 |
|
| /* flush what the padding leaves: one byte for "xx==", two for |
| 109 |
|
| * "xxx=", and the bits the padding stands for must be zero */ |
| 110 |
370 |
▶ | if (pad == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pad == 2) { |
180 |
190 |
370 |
|
| 111 |
180 |
▶ | if (quad & 0xFFFFu) return (STRLEN)-1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (quad & 0xFFFFu) return (STRLEN)-1; |
1 |
179 |
180 |
|
| 112 |
179 |
| *o++ = (unsigned char)((quad >> 16) & 0xFF); |
| 113 |
|
| } |
| 114 |
|
| else { |
| 115 |
190 |
▶ | if (quad & 0xFFu) return (STRLEN)-1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (quad & 0xFFu) return (STRLEN)-1; |
0 |
190 |
190 |
|
| 116 |
190 |
| *o++ = (unsigned char)((quad >> 16) & 0xFF); |
| 117 |
190 |
| *o++ = (unsigned char)((quad >> 8) & 0xFF); |
| 118 |
|
| } |
| 119 |
369 |
| have = 0; |
| 120 |
369 |
| quad = 0; |
| 121 |
|
| } |
| 122 |
549 |
| continue; |
| 123 |
|
| } |
| 124 |
176179 |
▶ | if (pad) return (STRLEN)-1; /* data after padding */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (pad) return (STRLEN)-1; /* data after padding */ |
2 |
176177 |
176179 |
|
| 125 |
176177 |
| quad = (quad << 6) | (unsigned)v; |
| 126 |
176177 |
▶ | if (++have == 4) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (++have == 4) { |
43810 |
132367 |
176177 |
|
| 127 |
43810 |
| *o++ = (unsigned char)((quad >> 16) & 0xFF); |
| 128 |
43810 |
| *o++ = (unsigned char)((quad >> 8) & 0xFF); |
| 129 |
43810 |
| *o++ = (unsigned char)( quad & 0xFF); |
| 130 |
43810 |
| have = 0; |
| 131 |
43810 |
| quad = 0; |
| 132 |
|
| } |
| 133 |
|
| } |
| 134 |
381 |
▶ | if (have) return (STRLEN)-1; /* a trailing partial group */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (have) return (STRLEN)-1; /* a trailing partial group */ |
1 |
380 |
381 |
|
| 135 |
380 |
| return (STRLEN)(o - out); |
| 136 |
|
| } |
| 137 |
|
| |
| 138 |
|
| PERL_STATIC_INLINE STRLEN psaml_b64_decode(unsigned char *out, const char *in, STRLEN n) { |
| 139 |
|
| return psaml_b64_decode_ws(out, in, n, 0); |
| 140 |
|
| } |
| 141 |
|
| |
| 142 |
374 |
| PERL_STATIC_INLINE STRLEN psaml_b64_decode_xml(unsigned char *out, const char *in, |
| 143 |
|
| STRLEN n) { |
| 144 |
374 |
| return psaml_b64_decode_ws(out, in, n, 1); |
| 145 |
|
| } |
| 146 |
|
| |
| 147 |
|
| #endif /* PSAML_B64_H */ |