include/cjws/cjws_compact.h

filter: Statementstmt 95.8% 162 / 169 Branchbran 63.9% 101 / 158 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.4% 263 / 327 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« JWS.xs ↑ Summary include/cjws/cjws_b64.h »
1 #ifndef CJWS_COMPACT_H
2 #define CJWS_COMPACT_H
3  
4 /* The JWS compact serialization pipeline, entirely in C. Header JSON is
5 * encoded and decoded through File::Raw::JSON's C ABI (frj_abi.h),
6 * resolved lazily via the house consumer pattern - File::Raw::JSON is a
7 * hard PREREQ, so a resolution failure is a boot-environment error. */
8  
9 #include "frj_abi.h"
10 #include "cjws_key.h"
11 #include "cjws_jws.h"
12 #include "cjws_b64.h"
13  
14 static const frj_abi *CJWS_FRJ = NULL;
15 static int CJWS_FRJ_TRIED = 0;
16  
17 74 static const frj_abi *cjws_frj(pTHX) {
18 74 ▶ if (!CJWS_FRJ && !CJWS_FRJ_TRIED) {
Branch: 2 branches
BranchTrueFalseTotal
if (!CJWS_FRJ && !CJWS_FRJ_TRIED) { 5 69 74
if (!CJWS_FRJ && !CJWS_FRJ_TRIED) { 5 0 5
19 5 dSP; int count; IV p = 0;
20 5 CJWS_FRJ_TRIED = 1;
21 5 eval_pv("require File::Raw::JSON;", FALSE);
22 5 SPAGAIN; /* the require may have reallocated the stack */
23 5 ▶ if (!SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV)) { 5 0 5
if (!SvTRUE(ERRSV)) { 5 0 5
24 5 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 5 5
25 5 count = call_pv("File::Raw::JSON::_abi_ptr", G_SCALAR | G_EVAL);
26 5 SPAGAIN;
27 5 ▶ if (!SvTRUE(ERRSV) && count > 0) p = POPi;
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 5 0 5
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 5 0 5
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 5 0 5
28 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
29 5 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 5 0 5
30 5 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 5 0 5
31 5 const frj_abi *a = INT2PTR(const frj_abi *, p);
32 5 ▶ if (a && a->abi_version >= FRJ_ABI_VERSION) CJWS_FRJ = a;
Branch: 2 branches
BranchTrueFalseTotal
if (a && a->abi_version >= FRJ_ABI_VERSION) CJWS_FRJ = a; 5 0 5
if (a && a->abi_version >= FRJ_ABI_VERSION) CJWS_FRJ = a; 5 0 5
33 }
34 }
35 }
36 74 ▶ if (!CJWS_FRJ)
Branch: 1 branch
BranchTrueFalseTotal
if (!CJWS_FRJ) 0 74 74
37 0 croak("Crypt::JWS: File::Raw::JSON with a compatible C ABI is "
38 "required (FRJ_ABI_VERSION %d)", FRJ_ABI_VERSION);
39 74 return CJWS_FRJ;
40 }
41  
42 /* b64url an SV's bytes into a fresh owned SV. */
43 27 static SV *cjws_b64url_sv(pTHX_ const unsigned char *p, STRLEN n) {
44 27 SV *out = newSV(cjws_b64url_encoded_len(n) + 1);
45 STRLEN o;
46 27 SvPOK_on(out);
47 27 o = cjws_b64url_encode(SvPVX(out), p, n);
48 27 SvCUR_set(out, o);
49 27 *SvEND(out) = '\0';
50 27 return out;
51 }
52  
53 /* Decode a b64url span into a fresh owned SV, or NULL on bad input. */
54 125 static SV *cjws_b64url_decode_sv(pTHX_ const char *p, STRLEN n) {
55 125 SV *out = newSV((n / 4 + 1) * 3 + 1);
56 STRLEN o;
57 125 SvPOK_on(out);
58 125 o = cjws_b64url_decode((unsigned char *)SvPVX(out), p, n);
59 125 ▶ if (o == (STRLEN)-1) {
Branch: 1 branch
BranchTrueFalseTotal
if (o == (STRLEN)-1) { 1 124 125
60 1 SvREFCNT_dec(out);
61 1 return NULL;
62 }
63 124 SvCUR_set(out, o);
64 124 *SvEND(out) = '\0';
65 124 return out;
66 }
67  
68 /* Validate an allowlist AV: every entry must be a supported alg name.
69 * Croaks on policy errors - an invalid allowlist is caller misuse, not
70 * an invalid token. */
71 47 static void cjws_check_allowlist(pTHX_ AV *algs) {
72 47 SSize_t i, n = av_count(algs);
73 47 ▶ if (!n)
Branch: 1 branch
BranchTrueFalseTotal
if (!n) 1 46 47
74 1 croak("Crypt::JWS::verify: an algs allowlist is required");
75 94 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 49 45 94
76 49 SV **e = av_fetch(algs, i, 0);
77 char fam;
78 int bits;
79 STRLEN len;
80 98 ▶ const char *s = (e && *e && SvOK(*e)) ? SvPV_const(*e, len) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
const char *s = (e && *e && SvOK(*e)) ? SvPV_const(*e, len) : NULL; 49 0 49
const char *s = (e && *e && SvOK(*e)) ? SvPV_const(*e, len) : NULL; 49 0 49
const char *s = (e && *e && SvOK(*e)) ? SvPV_const(*e, len) : NULL; 49 0 49
81 49 ▶ if (!s || !cjws_alg_parse(s, len, &fam, &bits))
Branch: 2 branches
BranchTrueFalseTotal
if (!s || !cjws_alg_parse(s, len, &fam, &bits)) 49 0 49
if (!s || !cjws_alg_parse(s, len, &fam, &bits)) 1 48 49
82 1 ▶ croak("Crypt::JWS::verify: unsupported alg '%s' in allowlist",
Branch: 1 branch
BranchTrueFalseTotal
croak("Crypt::JWS::verify: unsupported alg '%s' in allowlist", 1 0 1
83 s ? s : "(undef)");
84 }
85 45 }
86  
87 36 static int cjws_alg_in_list(pTHX_ AV *algs, const char *alg, STRLEN alglen) {
88 36 SSize_t i, n = av_count(algs);
89 38 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 37 1 38
90 37 SV **e = av_fetch(algs, i, 0);
91 STRLEN len;
92 const char *s;
93 37 ▶ if (!e || !*e || !SvOK(*e)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!e || !*e || !SvOK(*e)) continue; 37 0 37
if (!e || !*e || !SvOK(*e)) continue; 37 0 37
if (!e || !*e || !SvOK(*e)) continue; 0 37 37
94 37 s = SvPV_const(*e, len);
95 37 ▶ if (len == alglen && memEQ(s, alg, len)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (len == alglen && memEQ(s, alg, len)) return 1; 37 0 37
if (len == alglen && memEQ(s, alg, len)) return 1; 35 2 37
96 }
97 1 return 0;
98 }
99  
100 /* Build the compact serialization. header_hv carries everything except
101 * alg (stored by the caller into the HV before the call, so extras can
102 * never clobber it). Returns an owned SV. */
103 25 static SV *cjws_compact_sign(pTHX_ cjws_key *key, const char *alg,
104 STRLEN alglen, HV *header_hv,
105 const unsigned char *payload, STRLEN plen) {
106 25 const frj_abi *J = cjws_frj(aTHX);
107 25 SV *href = sv_2mortal(newRV_inc((SV *)header_hv));
108 25 SV *hjson = J->encode(aTHX_ href, NULL);
109 SV *out;
110 25 STRLEN hjlen, siglen = 0, inlen;
111 const char *hj;
112 unsigned char *sig;
113  
114 25 ▶ if (!hjson)
Branch: 1 branch
BranchTrueFalseTotal
if (!hjson) 0 25 25
115 0 croak("Crypt::JWS: header JSON encoding failed");
116 25 sv_2mortal(hjson);
117 25 hj = SvPV_const(hjson, hjlen);
118  
119 25 out = newSV(cjws_b64url_encoded_len(hjlen)
120 + cjws_b64url_encoded_len(plen) + 2 + 96);
121 25 SvPOK_on(out);
122 25 SvCUR_set(out, 0);
123 25 SvCUR_set(out, cjws_b64url_encode(SvPVX(out),
124 (const unsigned char *)hj, hjlen));
125 25 sv_catpvs(out, ".");
126 {
127 25 STRLEN cur = SvCUR(out);
128 25 ▶ SvGROW(out, cur + cjws_b64url_encoded_len(plen) + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(out, cur + cjws_b64url_encoded_len(plen) + 1); 25 0 25
SvGROW(out, cur + cjws_b64url_encoded_len(plen) + 1); 0 25 25
129 25 SvCUR_set(out, cur + cjws_b64url_encode(SvPVX(out) + cur, payload, plen));
130 }
131  
132 25 inlen = SvCUR(out);
133 25 sig = cjws_sign_raw(aTHX_ key, alg, alglen,
134 25 (const unsigned char *)SvPVX(out), inlen, &siglen);
135 25 ▶ if (!sig) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sig) { 1 24 25
136 1 SvREFCNT_dec(out);
137 1 croak("Crypt::JWS: sign failed (wrong key type for %.*s, public "
138 "key, or OpenSSL error)", (int)alglen, alg);
139 }
140 24 sv_catpvs(out, ".");
141 {
142 24 STRLEN cur = SvCUR(out);
143 24 ▶ SvGROW(out, cur + cjws_b64url_encoded_len(siglen) + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(out, cur + cjws_b64url_encoded_len(siglen) + 1); 24 0 24
SvGROW(out, cur + cjws_b64url_encoded_len(siglen) + 1); 11 13 24
144 24 SvCUR_set(out, cur + cjws_b64url_encode(SvPVX(out) + cur, sig, siglen));
145 }
146 24 *SvEND(out) = '\0';
147 24 OPENSSL_cleanse(sig, siglen);
148 24 OPENSSL_free(sig);
149 24 return out;
150 }
151  
152 /* Split "a.b.c" into three spans. Returns 1 on exactly two dots. */
153 44 static int cjws_split3(const char *t, STRLEN tlen,
154 const char **h, STRLEN *hl,
155 const char **p, STRLEN *pl,
156 const char **s, STRLEN *sl) {
157 44 const char *d1 = (const char *)memchr(t, '.', tlen);
158 const char *d2;
159 44 ▶ if (!d1) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!d1) return 0; 2 42 44
160 42 d2 = (const char *)memchr(d1 + 1, '.', (size_t)(t + tlen - d1 - 1));
161 42 ▶ if (!d2) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!d2) return 0; 1 41 42
162 41 ▶ if (memchr(d2 + 1, '.', (size_t)(t + tlen - d2 - 1))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(d2 + 1, '.', (size_t)(t + tlen - d2 - 1))) return 0; 1 40 41
163 40 *h = t; *hl = (STRLEN)(d1 - t);
164 40 *p = d1 + 1; *pl = (STRLEN)(d2 - d1 - 1);
165 40 *s = d2 + 1; *sl = (STRLEN)(t + tlen - d2 - 1);
166 40 return 1;
167 }
168  
169 /* Decode a header span into a hashref SV (owned), or NULL. Malformed
170 * JSON must be an invalid-token condition, not an exception, and the
171 * frj C entry croaks on bad input - so this one decode goes through the
172 * Perl function under G_EVAL. Header decoding happens once per verify;
173 * the signature crypto stays pure C. */
174 50 static SV *cjws_decode_header(pTHX_ const char *h64, STRLEN hlen) {
175 50 SV *hjson = cjws_b64url_decode_sv(aTHX_ h64, hlen);
176 50 SV *header = NULL;
177 50 ▶ if (!hjson) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!hjson) return NULL; 1 49 50
178 49 (void)cjws_frj(aTHX); /* loads File::Raw::JSON, croaks if absent */
179 {
180 49 dSP;
181 int count;
182 49 ENTER; SAVETMPS;
183 49 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 49 49
184 49 ▶ mXPUSHs(hjson); /* hand ownership to the call frame */
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(hjson); /* hand ownership to the call frame */ 0 49 49
185 49 PUTBACK;
186 49 count = call_pv("File::Raw::JSON::file_json_decode",
187 G_SCALAR | G_EVAL);
188 49 SPAGAIN;
189 97 ▶ if (!SvTRUE(ERRSV) && count > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) { 49 0 49
if (!SvTRUE(ERRSV) && count > 0) { 48 1 49
if (!SvTRUE(ERRSV) && count > 0) { 48 0 48
190 48 SV *out = POPs;
191 48 ▶ if (SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVHV) 48 0 48
if (SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVHV) 47 1 48
192 47 header = SvREFCNT_inc_simple_NN(out);
193 }
194 1 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 1 0 1
195 49 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 49 0 49
196 }
197 49 return header;
198 }
199  
200 /* The full verify pipeline. key_sv is a Crypt::JWS::Key or a coderef
201 * (called with the decoded header hashref; returns a key or undef).
202 * Returns owned payload SV, or NULL for every invalid-token condition. */
203 47 static SV *cjws_compact_verify(pTHX_ SV *token_sv, SV *key_sv, AV *algs) {
204 STRLEN tlen, hlen, plen, slen, alglen;
205 const char *t, *h64, *p64, *s64, *alg;
206 47 SV *header = NULL, *payload = NULL, *sig = NULL;
207 SV **algsv;
208 47 cjws_key *key = NULL;
209  
210 47 cjws_check_allowlist(aTHX_ algs);
211  
212 45 ▶ if (!SvOK(token_sv)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(token_sv)) return NULL; 1 44 45
213 44 t = SvPV_const(token_sv, tlen);
214 44 ▶ if (!cjws_split3(t, tlen, &h64, &hlen, &p64, &plen, &s64, &slen))
Branch: 1 branch
BranchTrueFalseTotal
if (!cjws_split3(t, tlen, &h64, &hlen, &p64, &plen, &s64, &slen)) 4 40 44
215 4 return NULL;
216 40 ▶ if (!hlen || !slen) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!hlen || !slen) return NULL; 39 1 40
if (!hlen || !slen) return NULL; 0 39 39
217  
218 39 header = cjws_decode_header(aTHX_ h64, hlen);
219 39 ▶ if (!header) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!header) return NULL; 3 36 39
220  
221 36 algsv = hv_fetchs((HV *)SvRV(header), "alg", 0);
222 36 ▶ if (!algsv || !*algsv || !SvOK(*algsv) || SvROK(*algsv))
Branch: 4 branches
BranchTrueFalseTotal
if (!algsv || !*algsv || !SvOK(*algsv) || SvROK(*algsv)) 36 0 36
if (!algsv || !*algsv || !SvOK(*algsv) || SvROK(*algsv)) 36 0 36
if (!algsv || !*algsv || !SvOK(*algsv) || SvROK(*algsv)) 36 0 36
if (!algsv || !*algsv || !SvOK(*algsv) || SvROK(*algsv)) 0 36 36
223 0 goto fail;
224 36 alg = SvPV_const(*algsv, alglen);
225 36 ▶ if (!cjws_alg_in_list(aTHX_ algs, alg, alglen))
Branch: 1 branch
BranchTrueFalseTotal
if (!cjws_alg_in_list(aTHX_ algs, alg, alglen)) 1 35 36
226 1 goto fail;
227  
228 35 ▶ if (SvROK(key_sv) && SvTYPE(SvRV(key_sv)) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(key_sv) && SvTYPE(SvRV(key_sv)) == SVt_PVCV) { 35 0 35
if (SvROK(key_sv) && SvTYPE(SvRV(key_sv)) == SVt_PVCV) { 2 33 35
229 /* kid routing: the resolver sees the (unverified) header */
230 2 dSP;
231 int count;
232 2 SV *resolved = NULL;
233 2 ENTER; SAVETMPS;
234 2 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2 2
235 2 ▶ XPUSHs(header);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(header); 0 2 2
236 2 PUTBACK;
237 2 count = call_sv(key_sv, G_SCALAR | G_EVAL);
238 2 SPAGAIN;
239 2 ▶ if (!SvTRUE(ERRSV) && count > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) { 2 0 2
if (!SvTRUE(ERRSV) && count > 0) { 2 0 2
if (!SvTRUE(ERRSV) && count > 0) { 2 0 2
240 2 resolved = POPs;
241 2 ▶ if (SvOK(resolved)) SvREFCNT_inc_simple_void_NN(resolved);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(resolved)) SvREFCNT_inc_simple_void_NN(resolved); 1 1 2
242 1 else resolved = NULL;
243 }
244 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
245 2 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2 0 2
246 2 ▶ if (SvTRUE(ERRSV) || !resolved) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV) || !resolved) { 2 0 2
if (SvTRUE(ERRSV) || !resolved) { 2 0 2
if (SvTRUE(ERRSV) || !resolved) { 1 1 2
247 1 SvREFCNT_dec(resolved);
248 1 goto fail;
249 }
250 /* Mortalize AFTER the FREETMPS above: the resolver may have handed
251 * back a key nothing else holds, and the struct must outlive the
252 * verify below. The enclosing XSUB's frame frees it. */
253 1 sv_2mortal(resolved);
254 1 ▶ if (!(SvROK(resolved) && sv_isobject(resolved)
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(resolved) && sv_isobject(resolved) 1 0 1
if (!(SvROK(resolved) && sv_isobject(resolved) 1 0 1
255 1 ▶ && sv_derived_from(resolved, "Crypt::JWS::Key")))
Branch: 1 branch
BranchTrueFalseTotal
&& sv_derived_from(resolved, "Crypt::JWS::Key"))) 0 1 1
256 0 goto fail;
257 1 key = INT2PTR(cjws_key *, SvIV(SvRV(resolved)));
258 }
259 33 ▶ else if (SvROK(key_sv) && sv_isobject(key_sv)
Branch: 2 branches
BranchTrueFalseTotal
else if (SvROK(key_sv) && sv_isobject(key_sv) 33 0 33
else if (SvROK(key_sv) && sv_isobject(key_sv) 33 0 33
260 33 ▶ && sv_derived_from(key_sv, "Crypt::JWS::Key")) {
Branch: 1 branch
BranchTrueFalseTotal
&& sv_derived_from(key_sv, "Crypt::JWS::Key")) { 33 0 33
261 33 key = INT2PTR(cjws_key *, SvIV(SvRV(key_sv)));
262 }
263 else {
264 0 croak("Crypt::JWS::verify: key must be a Crypt::JWS::Key or a coderef");
265 }
266  
267 34 sig = cjws_b64url_decode_sv(aTHX_ s64, slen);
268 34 ▶ if (!sig) goto fail;
Branch: 1 branch
BranchTrueFalseTotal
if (!sig) goto fail; 0 34 34
269  
270 34 ▶ if (!cjws_verify_raw(aTHX_ key, alg, alglen,
Branch: 1 branch
BranchTrueFalseTotal
if (!cjws_verify_raw(aTHX_ key, alg, alglen, 11 23 34
271 (const unsigned char *)h64,
272 34 (STRLEN)(s64 - 1 - h64), /* "h64.p64" span */
273 34 (const unsigned char *)SvPVX(sig), SvCUR(sig)))
274 11 goto fail;
275  
276 23 payload = cjws_b64url_decode_sv(aTHX_ p64, plen);
277  
278 36 fail:
279 36 SvREFCNT_dec(header);
280 36 SvREFCNT_dec(sig);
281 36 return payload;
282 }
283  
284 #endif
« JWS.xs ↑ Summary include/cjws/cjws_b64.h »