include/psaml/psaml_b64.h

filter: Statementstmt 100% 74 / 74 Branchbran 91.6% 55 / 60 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 96.2% 129 / 134 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/psaml/psaml_error.h ↑ Summary xs/util.xs »
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 257496 PERL_STATIC_INLINE signed char psaml_b64_val(unsigned char c) {
36 257496 ▶ if (c >= 'A' && c <= 'Z') return (signed char)(c - 'A');
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'A' && c <= 'Z') return (signed char)(c - 'A'); 215686 41810 257496
if (c >= 'A' && c <= 'Z') return (signed char)(c - 'A'); 113243 102443 215686
37 144253 ▶ if (c >= 'a' && c <= 'z') return (signed char)(c - 'a' + 26);
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'a' && c <= 'z') return (signed char)(c - 'a' + 26); 102443 41810 144253
if (c >= 'a' && c <= 'z') return (signed char)(c - 'a' + 26); 102443 0 102443
38 41810 ▶ if (c >= '0' && c <= '9') return (signed char)(c - '0' + 52);
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') return (signed char)(c - '0' + 52); 37908 3902 41810
if (c >= '0' && c <= '9') return (signed char)(c - '0' + 52); 37275 633 37908
39 4535 ▶ if (c == '+') return 62;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '+') return 62; 2330 2205 4535
40 2205 ▶ if (c == '/') return 63;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '/') return 63; 1324 881 2205
41 881 ▶ if (c == '=') return -2;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '=') return -2; 633 248 881
42 248 ▶ if (c == ' ' || c == '\t' || c == '\r' || c == '\n') return -3;
Branch: 4 branches
BranchTrueFalseTotal
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 81 PERL_STATIC_INLINE STRLEN psaml_b64_encoded_len(STRLEN n) {
47 81 return ((n + 2) / 3) * 4;
48 }
49  
50 /* Writes exactly psaml_b64_encoded_len(n) bytes; no NUL. */
51 81 PERL_STATIC_INLINE STRLEN psaml_b64_encode(char *out, const unsigned char *in, STRLEN n) {
52 81 char *o = out;
53 81 STRLEN i = 0;
54 12462 ▶ while (i + 3 <= n) {
Branch: 1 branch
BranchTrueFalseTotal
while (i + 3 <= n) { 12381 81 12462
55 12381 unsigned v = ((unsigned)in[i] << 16) | ((unsigned)in[i+1] << 8) | in[i+2];
56 12381 *o++ = PSAML_B64_ALPHA[(v >> 18) & 63];
57 12381 *o++ = PSAML_B64_ALPHA[(v >> 12) & 63];
58 12381 *o++ = PSAML_B64_ALPHA[(v >> 6) & 63];
59 12381 *o++ = PSAML_B64_ALPHA[ v & 63];
60 12381 i += 3;
61 }
62 81 ▶ if (n - i == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (n - i == 1) { 24 57 81
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 57 ▶ else if (n - i == 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if (n - i == 2) { 9 48 57
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 81 return (STRLEN)(o - out);
77 }
78  
79 /* Upper bound on the decoded size, for allocation. */
80 451 PERL_STATIC_INLINE STRLEN psaml_b64_decoded_max(STRLEN n) {
81 451 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 451 PERL_STATIC_INLINE STRLEN psaml_b64_decode_ws(unsigned char *out, const char *in,
88 STRLEN n, int allow_ws) {
89 451 unsigned char *o = out;
90 451 unsigned quad = 0;
91 451 int have = 0; /* alphabet characters in the current group */
92 451 int pad = 0;
93 STRLEN i;
94  
95 257933 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 257496 437 257933
96 257496 signed char v = psaml_b64_val((unsigned char)in[i]);
97 257496 ▶ if (v == -3) {
Branch: 1 branch
BranchTrueFalseTotal
if (v == -3) { 243 257253 257496
98 243 ▶ if (allow_ws) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (allow_ws) continue; 240 3 243
99 3 return (STRLEN)-1;
100 }
101 257253 ▶ if (v == -1) return (STRLEN)-1;
Branch: 1 branch
BranchTrueFalseTotal
if (v == -1) return (STRLEN)-1; 5 257248 257253
102 257248 ▶ if (v == -2) { /* '=' */
Branch: 1 branch
BranchTrueFalseTotal
if (v == -2) { /* '=' */ 633 256615 257248
103 633 ▶ if (have < 2) return (STRLEN)-1; /* "=" or "A=" is never valid */
Branch: 1 branch
BranchTrueFalseTotal
if (have < 2) return (STRLEN)-1; /* "=" or "A=" is never valid */ 3 630 633
104 630 ▶ if (++pad > 2) return (STRLEN)-1;
Branch: 1 branch
BranchTrueFalseTotal
if (++pad > 2) return (STRLEN)-1; 0 630 630
105 630 quad <<= 6;
106 630 have++;
107 630 ▶ if (have == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (have == 4) { 421 209 630
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 421 ▶ if (pad == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (pad == 2) { 209 212 421
111 209 ▶ if (quad & 0xFFFFu) return (STRLEN)-1;
Branch: 1 branch
BranchTrueFalseTotal
if (quad & 0xFFFFu) return (STRLEN)-1; 1 208 209
112 208 *o++ = (unsigned char)((quad >> 16) & 0xFF);
113 }
114 else {
115 212 ▶ if (quad & 0xFFu) return (STRLEN)-1;
Branch: 1 branch
BranchTrueFalseTotal
if (quad & 0xFFu) return (STRLEN)-1; 0 212 212
116 212 *o++ = (unsigned char)((quad >> 16) & 0xFF);
117 212 *o++ = (unsigned char)((quad >> 8) & 0xFF);
118 }
119 420 have = 0;
120 420 quad = 0;
121 }
122 629 continue;
123 }
124 256615 ▶ if (pad) return (STRLEN)-1; /* data after padding */
Branch: 1 branch
BranchTrueFalseTotal
if (pad) return (STRLEN)-1; /* data after padding */ 2 256613 256615
125 256613 quad = (quad << 6) | (unsigned)v;
126 256613 ▶ if (++have == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (++have == 4) { 63888 192725 256613
127 63888 *o++ = (unsigned char)((quad >> 16) & 0xFF);
128 63888 *o++ = (unsigned char)((quad >> 8) & 0xFF);
129 63888 *o++ = (unsigned char)( quad & 0xFF);
130 63888 have = 0;
131 63888 quad = 0;
132 }
133 }
134 437 ▶ if (have) return (STRLEN)-1; /* a trailing partial group */
Branch: 1 branch
BranchTrueFalseTotal
if (have) return (STRLEN)-1; /* a trailing partial group */ 1 436 437
135 436 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 430 PERL_STATIC_INLINE STRLEN psaml_b64_decode_xml(unsigned char *out, const char *in,
143 STRLEN n) {
144 430 return psaml_b64_decode_ws(out, in, n, 1);
145 }
146  
147 #endif /* PSAML_B64_H */
« include/psaml/psaml_error.h ↑ Summary xs/util.xs »