include/psaml/psaml_signature.h

filter: Statementstmt 84.5% 148 / 175 Branchbran 53.2% 83 / 156 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 69.7% 231 / 331 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/metadata.xs ↑ Summary include/psaml/psaml_abi.h »
1 #ifndef PSAML_SIGNATURE_H
2 #define PSAML_SIGNATURE_H
3  
4 /* XML-DSig, verified.
5 *
6 * ONE RULE, applied without exception, and everything else in this file
7 * is machinery for it:
8 *
9 * THE ELEMENT WHOSE CHILD THE SIGNATURE IS, IS THE ELEMENT THE
10 * SIGNATURE COVERS, IS THE ELEMENT THE IDENTITY IS READ FROM.
11 *
12 * The reason SAML libraries have had a decade of authentication bypasses
13 * is that the signed thing and the read thing were allowed to differ. An
14 * attacker takes a signed assertion for their own account, adds an
15 * unsigned assertion for the administrator's, and arranges the document
16 * so the verifier finds one and the reader finds the other. Every
17 * published variant is that. So this file returns the verified node, the
18 * caller holds it in one local, and every read below it starts from that
19 * pointer. There is no second lookup and no search from the root.
20 *
21 * Must be included after psaml_xml.h, psaml_b64.h and psaml_error.h. */
22  
23 #include "psaml_abi.h"
24 #include "psaml_xml.h"
25 #include "psaml_b64.h"
26 #include "psaml_error.h"
27  
28 #define PSAML_C14N_EXC "http://www.w3.org/2001/10/xml-exc-c14n#"
29 #define PSAML_C14N_EXC_WC "http://www.w3.org/2001/10/xml-exc-c14n#WithComments"
30 #define PSAML_TR_ENVELOPED "http://www.w3.org/2000/09/xmldsig#enveloped-signature"
31  
32 #define PSAML_MAX_PREFIXES 64
33  
34 /* The accepted SignatureMethods, and what each is called in jws_abi.
35 *
36 * There is deliberately no rsa-sha1 here, and allow_sha1 does not add
37 * one. See PSAML_DIGESTS below for where that option does reach. */
38 typedef struct {
39 const char *uri;
40 const char *alg; /* the jws algorithm name */
41 } psaml_sigalg_map;
42  
43 static const psaml_sigalg_map PSAML_SIGALGS[] = {
44 { "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", "RS256" },
45 { "http://www.w3.org/2001/04/xmldsig-more#rsa-sha384", "RS384" },
46 { "http://www.w3.org/2001/04/xmldsig-more#rsa-sha512", "RS512" },
47 { "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256", "ES256" },
48 { "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha384", "ES384" },
49 { "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha512", "ES512" },
50 { NULL, NULL }
51 };
52  
53 /* DigestMethod URIs.
54 *
55 * SHA-256 and, under allow_sha1, SHA-1. NOT 384 or 512, and that is a
56 * limitation of jws_abi rather than a decision: the table carries sha256
57 * and sha1 as one-shot digests and nothing else, so this dist cannot
58 * compute a SHA-384 or SHA-512 reference digest at all. They are listed
59 * here so the refusal names them specifically instead of reporting an
60 * unknown algorithm, and the fix is two entries appended to jws_abi.
61 *
62 * This bites nothing in practice: every identity provider in the field
63 * digests with SHA-256, and the SignatureMethod side, which does go
64 * through jws `verify`, takes all of RS/ES 256, 384 and 512 already.
65 *
66 * SHA-1 is reachable only on the DIGEST side. There is no rsa-sha1 in
67 * PSAML_SIGALGS and allow_sha1 does not add one: JWS has no RS1, and
68 * adding one so SAML can accept a signature algorithm deprecated since
69 * 2011 is the wrong direction. The digest side is the less harmful of
70 * the two and is where the option reaches. */
71 typedef struct { const char *uri; int bits; int needs_allow_sha1; } psaml_dig;
72  
73 static const psaml_dig PSAML_DIGESTS[] = {
74 { "http://www.w3.org/2001/04/xmlenc#sha256", 256, 0 },
75 { "http://www.w3.org/2000/09/xmldsig#sha1", 160, 1 },
76 { "http://www.w3.org/2001/04/xmldsig-more#sha384", 384, 0 }, /* refused */
77 { "http://www.w3.org/2001/04/xmlenc#sha512", 512, 0 }, /* refused */
78 { NULL, 0, 0 }
79 };
80  
81 /* Returns the bit length, 0 for an unknown or disallowed algorithm. */
82 185 PERL_STATIC_INLINE int psaml_digest_bits(const char *uri, STRLEN n,
83 int allow_sha1) {
84 int i;
85 187 ▶ for (i = 0; PSAML_DIGESTS[i].uri; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; PSAML_DIGESTS[i].uri; i++) { 187 0 187
86 187 STRLEN ul = strlen(PSAML_DIGESTS[i].uri);
87 187 ▶ if (n == ul && memEQ(uri, PSAML_DIGESTS[i].uri, ul)) {
Branch: 2 branches
BranchTrueFalseTotal
if (n == ul && memEQ(uri, PSAML_DIGESTS[i].uri, ul)) { 185 2 187
if (n == ul && memEQ(uri, PSAML_DIGESTS[i].uri, ul)) { 185 0 185
88 185 ▶ if (PSAML_DIGESTS[i].needs_allow_sha1 && !allow_sha1) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (PSAML_DIGESTS[i].needs_allow_sha1 && !allow_sha1) return 0; 2 183 185
if (PSAML_DIGESTS[i].needs_allow_sha1 && !allow_sha1) return 0; 1 1 2
89 184 return PSAML_DIGESTS[i].bits;
90 }
91 }
92 0 return 0;
93 }
94  
95 /* ---- small helpers over the tree -------------------------------------- */
96  
97 /* An attribute compared to a literal, whole. */
98 1209 PERL_STATIC_INLINE int psaml_attr_is(pTHX_ const frx_node *n,
99 const char *name, const char *want) {
100 STRLEN vl;
101 1209 const char *v = psaml_attr(aTHX_ n, name, &vl);
102 1209 STRLEN wl = strlen(want);
103 1209 ▶ return v && vl == wl && memEQ(v, want, wl);
Branch: 3 branches
BranchTrueFalseTotal
return v && vl == wl && memEQ(v, want, wl); 1194 15 1209
return v && vl == wl && memEQ(v, want, wl); 1017 177 1194
return v && vl == wl && memEQ(v, want, wl); 1016 1 1017
104 }
105  
106 /* The direct-child ds:Signature, or NULL.
107 *
108 * DIRECT CHILD, and that is the first of the nine rules rather than an
109 * implementation convenience. A Signature inside Extensions, inside
110 * Subject, or inside another assertion's Advice is not looked at, and its
111 * presence does not make anything signed. A verifier that searched the
112 * subtree for "a signature" is a verifier that can be handed one over a
113 * different element. */
114 535 PERL_STATIC_INLINE const frx_node *psaml_sig_of(pTHX_ const frx_node *n) {
115 535 return psaml_child(aTHX_ n, PSAML_NS_DSIG, "Signature", NULL);
116 }
117  
118 /* Collect an InclusiveNamespaces PrefixList into a caller-owned array of
119 * borrowed pointers. Returns the count.
120 *
121 * The list is whitespace-separated prefixes; "#default" names the default
122 * namespace. The strings are cut out of the attribute value, which lives
123 * in the document's arena, so they are NUL-terminated copies made here
124 * because the value is one string and c14n wants separate ones. */
125 353 PERL_STATIC_INLINE int psaml_prefix_list(pTHX_ const frx_node *transform,
126 char *buf, STRLEN buflen,
127 const char **out, int max) {
128 const frx_node *inc;
129 const char *v;
130 STRLEN vl, i, start;
131 353 int n = 0;
132 353 ▶ if (!transform) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!transform) return 0; 0 353 353
133 353 inc = psaml_child(aTHX_ transform, PSAML_C14N_EXC, "InclusiveNamespaces",
134 NULL);
135 353 ▶ if (!inc) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!inc) return 0; 353 0 353
136 0 v = psaml_attr(aTHX_ inc, "PrefixList", &vl);
137 0 ▶ if (!v || !vl) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!v || !vl) return 0; 0 0 0
if (!v || !vl) return 0; 0 0 0
138 0 ▶ if (vl >= buflen) vl = buflen - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (vl >= buflen) vl = buflen - 1; 0 0 0
139 0 memcpy(buf, v, vl);
140 0 buf[vl] = '\0';
141 0 i = 0;
142 0 ▶ while (i < vl && n < max) {
Branch: 2 branches
BranchTrueFalseTotal
while (i < vl && n < max) { 0 0 0
while (i < vl && n < max) { 0 0 0
143 0 ▶ while (i < vl && (buf[i] == ' ' || buf[i] == '\t'
Branch: 3 branches
BranchTrueFalseTotal
while (i < vl && (buf[i] == ' ' || buf[i] == '\t' 0 0 0
while (i < vl && (buf[i] == ' ' || buf[i] == '\t' 0 0 0
while (i < vl && (buf[i] == ' ' || buf[i] == '\t' 0 0 0
144 0 ▶ || buf[i] == '\r' || buf[i] == '\n')) i++;
Branch: 2 branches
BranchTrueFalseTotal
|| buf[i] == '\r' || buf[i] == '\n')) i++; 0 0 0
|| buf[i] == '\r' || buf[i] == '\n')) i++; 0 0 0
145 0 ▶ if (i >= vl) break;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= vl) break; 0 0 0
146 0 start = i;
147 0 ▶ while (i < vl && !(buf[i] == ' ' || buf[i] == '\t'
Branch: 3 branches
BranchTrueFalseTotal
while (i < vl && !(buf[i] == ' ' || buf[i] == '\t' 0 0 0
while (i < vl && !(buf[i] == ' ' || buf[i] == '\t' 0 0 0
while (i < vl && !(buf[i] == ' ' || buf[i] == '\t' 0 0 0
148 0 ▶ || buf[i] == '\r' || buf[i] == '\n')) i++;
Branch: 2 branches
BranchTrueFalseTotal
|| buf[i] == '\r' || buf[i] == '\n')) i++; 0 0 0
|| buf[i] == '\r' || buf[i] == '\n')) i++; 0 0 0
149 0 buf[i < vl ? i : vl] = '\0';
150 0 out[n++] = buf + start;
151 0 i++;
152 }
153 0 return n;
154 }
155  
156 /* ---- the verification ------------------------------------------------- */
157  
158 typedef struct {
159 const frx_doc *doc;
160 AV *keys; /* PEM certificates or public keys, as SVs */
161 int allow_sha1;
162 } psaml_verify_ctx;
163  
164 /* A configured signing key, which is usually a CERTIFICATE.
165 *
166 * SAML metadata carries <ds:X509Certificate>, and what a deployer pastes
167 * into `certs` is a PEM certificate, not a PUBLIC KEY block. key_from_pem
168 * reads the latter and refuses the former, so this is where phase 2's
169 * key_from_x509_der earns its place: try the key form first, and on
170 * failure strip the PEM armour and hand the DER to the certificate entry.
171 *
172 * Both forms are accepted because a deployer who does have a bare public
173 * key should not be told to wrap it in a certificate to satisfy us.
174 * Returns NULL for anything that is neither. */
175 170 PERL_STATIC_INLINE void *psaml_key_from_config(pTHX_ SV *sv) {
176 170 const jws_abi *J = psaml_jws(aTHX);
177 STRLEN n, dl;
178 170 const char *p = SvPVbyte(sv, n);
179 const char *b, *e;
180 SV *der;
181 void *k;
182  
183 170 k = J->key_from_pem(aTHX_ p, n);
184 170 ▶ if (k) return k;
Branch: 1 branch
BranchTrueFalseTotal
if (k) return k; 160 10 170
185  
186 {
187 static const char BEG[] = "-----BEGIN CERTIFICATE-----";
188 static const char END[] = "-----END CERTIFICATE-----";
189 10 b = ninstr((char *)p, (char *)p + n,
190 (char *)BEG, (char *)BEG + sizeof BEG - 1);
191 10 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 10 10
192 10 b += sizeof BEG - 1;
193 10 e = ninstr((char *)b, (char *)p + n,
194 (char *)END, (char *)END + sizeof END - 1);
195 10 ▶ if (!e) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return NULL; 0 10 10
196 }
197  
198 10 der = sv_2mortal(newSV(psaml_b64_decoded_max((STRLEN)(e - b)) + 1));
199 10 SvPOK_on(der);
200 10 dl = psaml_b64_decode_xml((unsigned char *)SvPVX(der), b, (STRLEN)(e - b));
201 10 ▶ if (dl == (STRLEN)-1) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (dl == (STRLEN)-1) return NULL; 0 10 10
202 10 SvCUR_set(der, dl);
203 10 return J->key_from_x509_der(aTHX_
204 10 (const unsigned char *)SvPVX(der), dl);
205 }
206  
207 /* Verify the direct-child signature of `parent`.
208 *
209 * Returns 1 when a signature is present and verifies, 0 when there is no
210 * direct-child signature at all, and THROWS on a signature that is
211 * present and wrong. The three outcomes are distinct because
212 * `require_signed` needs to tell "unsigned" from "badly signed": the
213 * first may be allowed by policy, the second never is. */
214 535 PERL_STATIC_INLINE int psaml_verify_element(pTHX_ psaml_verify_ctx *ctx,
215 const frx_node *parent) {
216 535 const frx_abi *F = psaml_frx(aTHX);
217 535 const jws_abi *J = psaml_jws(aTHX);
218 const frx_node *sig, *si, *cm, *sm, *ref, *trs, *t1, *t2, *dm, *dv, *sv_node;
219 const char *v;
220 STRLEN vl;
221 int i;
222  
223 535 sig = psaml_sig_of(aTHX_ parent);
224 535 ▶ if (!sig) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!sig) return 0; 328 207 535
225  
226 207 si = psaml_only_child(aTHX_ sig, PSAML_NS_DSIG, "SignedInfo");
227 207 ▶ if (!si)
Branch: 1 branch
BranchTrueFalseTotal
if (!si) 0 207 207
228 0 psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE,
229 "the Signature has no single SignedInfo");
230  
231 /* 2: CanonicalizationMethod must be exclusive c14n */
232 207 cm = psaml_only_child(aTHX_ si, PSAML_NS_DSIG, "CanonicalizationMethod");
233 208 ▶ if (!cm || !(psaml_attr_is(aTHX_ cm, "Algorithm", PSAML_C14N_EXC)
Branch: 1 branch
BranchTrueFalseTotal
if (!cm || !(psaml_attr_is(aTHX_ cm, "Algorithm", PSAML_C14N_EXC) 207 0 207
234 1 || psaml_attr_is(aTHX_ cm, "Algorithm", PSAML_C14N_EXC_WC)))
235 1 psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE,
236 "CanonicalizationMethod is not exclusive c14n");
237  
238 /* 3: SignatureMethod from the table */
239 206 sm = psaml_only_child(aTHX_ si, PSAML_NS_DSIG, "SignatureMethod");
240 206 ▶ if (!sm) psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE,
Branch: 1 branch
BranchTrueFalseTotal
if (!sm) psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, 0 206 206
241 "the SignedInfo has no single SignatureMethod");
242 206 v = psaml_attr(aTHX_ sm, "Algorithm", &vl);
243 206 ▶ if (!v) psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE,
Branch: 1 branch
BranchTrueFalseTotal
if (!v) psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, 0 206 206
244 "SignatureMethod has no Algorithm");
245 {
246 206 const char *jws_alg = NULL;
247 248 ▶ for (i = 0; PSAML_SIGALGS[i].uri; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; PSAML_SIGALGS[i].uri; i++) { 241 7 248
248 241 STRLEN ul = strlen(PSAML_SIGALGS[i].uri);
249 241 ▶ if (vl == ul && memEQ(v, PSAML_SIGALGS[i].uri, ul)) {
Branch: 2 branches
BranchTrueFalseTotal
if (vl == ul && memEQ(v, PSAML_SIGALGS[i].uri, ul)) { 199 42 241
if (vl == ul && memEQ(v, PSAML_SIGALGS[i].uri, ul)) { 199 0 199
250 199 jws_alg = PSAML_SIGALGS[i].alg;
251 199 break;
252 }
253 }
254 206 ▶ if (!jws_alg)
Branch: 1 branch
BranchTrueFalseTotal
if (!jws_alg) 7 199 206
255 7 psaml_throw(aTHX_ PSAML_E_ALG_REFUSED,
256 "SignatureMethod '%.*s' is not accepted", (int)vl, v);
257  
258 /* 4: exactly one Reference */
259 199 ref = psaml_only_child(aTHX_ si, PSAML_NS_DSIG, "Reference");
260 199 ▶ if (!ref)
Branch: 1 branch
BranchTrueFalseTotal
if (!ref) 2 197 199
261 2 psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE,
262 "the SignedInfo does not have exactly one Reference");
263  
264 /* 4 and 5: the URI is '#' plus the PARENT's ID, and the node that id
265 * resolves to is the SAME POINTER as the parent.
266 *
267 * The pointer comparison is the whole defence. Comparing the id
268 * strings would pass for a document containing a second element that
269 * merely carries the same-looking id, and comparing element names
270 * would pass for any assertion at all. by_id was built at parse over
271 * `ID` alone and a duplicate value was refused there, so the node it
272 * returns is the only one, and requiring it to BE the signature's
273 * parent closes the gap between what was signed and what will be
274 * read. */
275 197 v = psaml_attr(aTHX_ ref, "URI", &vl);
276 197 ▶ if (!v || vl < 2 || v[0] != '#')
Branch: 3 branches
BranchTrueFalseTotal
if (!v || vl < 2 || v[0] != '#') 197 0 197
if (!v || vl < 2 || v[0] != '#') 196 1 197
if (!v || vl < 2 || v[0] != '#') 0 196 196
277 1 psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE,
278 "the Reference URI is not a same-document '#id'");
279 {
280 196 const frx_node *target = F->by_id(ctx->doc, "ID", v + 1, vl - 1);
281 196 ▶ if (!target || target != parent)
Branch: 2 branches
BranchTrueFalseTotal
if (!target || target != parent) 196 0 196
if (!target || target != parent) 8 188 196
282 8 psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE,
283 "the Reference names an element other than the one the "
284 "Signature is a child of");
285 }
286  
287 /* 5: Transforms exactly enveloped-signature then exclusive c14n */
288 188 trs = psaml_only_child(aTHX_ ref, PSAML_NS_DSIG, "Transforms");
289 188 ▶ if (!trs)
Branch: 1 branch
BranchTrueFalseTotal
if (!trs) 0 188 188
290 0 psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, "the Reference has no Transforms");
291 188 t1 = psaml_child(aTHX_ trs, PSAML_NS_DSIG, "Transform", NULL);
292 188 ▶ t2 = t1 ? psaml_child(aTHX_ trs, PSAML_NS_DSIG, "Transform", t1) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
t2 = t1 ? psaml_child(aTHX_ trs, PSAML_NS_DSIG, "Transform", t1) : NULL; 188 0 188
293 188 ▶ if (!t1 || !t2 || psaml_child(aTHX_ trs, PSAML_NS_DSIG, "Transform", t2))
Branch: 3 branches
BranchTrueFalseTotal
if (!t1 || !t2 || psaml_child(aTHX_ trs, PSAML_NS_DSIG, "Transform", t2)) 188 0 188
if (!t1 || !t2 || psaml_child(aTHX_ trs, PSAML_NS_DSIG, "Transform", t2)) 188 0 188
if (!t1 || !t2 || psaml_child(aTHX_ trs, PSAML_NS_DSIG, "Transform", t2)) 0 188 188
294 0 psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE,
295 "Transforms must be exactly enveloped-signature then "
296 "exclusive c14n");
297 188 ▶ if (!psaml_attr_is(aTHX_ t1, "Algorithm", PSAML_TR_ENVELOPED))
Branch: 1 branch
BranchTrueFalseTotal
if (!psaml_attr_is(aTHX_ t1, "Algorithm", PSAML_TR_ENVELOPED)) 1 187 188
298 1 psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE,
299 "the first Transform is not enveloped-signature");
300 189 if (!(psaml_attr_is(aTHX_ t2, "Algorithm", PSAML_C14N_EXC)
301 2 || psaml_attr_is(aTHX_ t2, "Algorithm", PSAML_C14N_EXC_WC)))
302 2 psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE,
303 "the second Transform is not exclusive c14n. XPath, XSLT "
304 "and base64 transforms are refused: those are what let a "
305 "signature cover something other than what it appears to");
306  
307 /* 6: DigestMethod */
308 185 dm = psaml_only_child(aTHX_ ref, PSAML_NS_DSIG, "DigestMethod");
309 185 ▶ if (!dm) psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE,
Branch: 1 branch
BranchTrueFalseTotal
if (!dm) psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, 0 185 185
310 "the Reference has no single DigestMethod");
311 185 v = psaml_attr(aTHX_ dm, "Algorithm", &vl);
312 {
313 185 ▶ int bits = v ? psaml_digest_bits(v, vl, ctx->allow_sha1) : 0;
Branch: 1 branch
BranchTrueFalseTotal
int bits = v ? psaml_digest_bits(v, vl, ctx->allow_sha1) : 0; 185 0 185
314 SV *canon, *digest, *want;
315 const frx_node *without[1];
316 frx_c14n c;
317  
318 185 ▶ if (!bits)
Branch: 1 branch
BranchTrueFalseTotal
if (!bits) 1 184 185
319 1 ▶ psaml_throw(aTHX_ PSAML_E_ALG_REFUSED,
Branch: 2 branches
BranchTrueFalseTotal
psaml_throw(aTHX_ PSAML_E_ALG_REFUSED, 1 0 1
psaml_throw(aTHX_ PSAML_E_ALG_REFUSED, 1 0 1
320 "DigestMethod '%.*s' is not accepted",
321 (int)(v ? vl : 0), v ? v : "");
322  
323 /* 7: the digest, over c14n of the PARENT with the Signature
324 * excluded - which is what `enveloped-signature` means - and the
325 * TRANSFORM's PrefixList. */
326 {
327 char pbuf[512];
328 const char *prefixes[PSAML_MAX_PREFIXES];
329 184 int np = psaml_prefix_list(aTHX_ t2, pbuf, sizeof pbuf,
330 prefixes, PSAML_MAX_PREFIXES);
331 184 Zero(&c, 1, frx_c14n);
332 184 c.mode = FRX_C14N_EXC;
333 184 c.comments = 0;
334 184 ▶ c.prefix_list = np ? prefixes : NULL;
Branch: 1 branch
BranchTrueFalseTotal
c.prefix_list = np ? prefixes : NULL; 0 184 184
335 184 c.n_prefix = np;
336 184 without[0] = sig;
337 184 c.without = without;
338 184 c.n_without = 1;
339 184 canon = F->c14n(aTHX_ parent, &c);
340 }
341 184 ▶ if (!canon)
Branch: 1 branch
BranchTrueFalseTotal
if (!canon) 0 184 184
342 0 psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE,
343 "the signed element would not canonicalise");
344 184 sv_2mortal(canon);
345 {
346 STRLEN cl;
347 const unsigned char *cp =
348 184 (const unsigned char *)SvPVbyte(canon, cl);
349 1 digest = bits == 160 ? J->sha1(aTHX_ cp, cl)
350 367 ▶ : bits == 256 ? J->sha256(aTHX_ cp, cl)
Branch: 1 branch
BranchTrueFalseTotal
: bits == 256 ? J->sha256(aTHX_ cp, cl) 1 183 184
351 183 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 183 0 183
352 184 ▶ if (!digest)
Branch: 1 branch
BranchTrueFalseTotal
if (!digest) 0 184 184
353 0 psaml_throw(aTHX_ PSAML_E_ALG_REFUSED,
354 "DigestMethod SHA-%d is recognised but this build "
355 "cannot compute it: jws_abi carries sha256 and sha1 "
356 "and no other one-shot digest", bits);
357 184 sv_2mortal(digest);
358 }
359  
360 184 dv = psaml_only_child(aTHX_ ref, PSAML_NS_DSIG, "DigestValue");
361 184 ▶ if (!dv) psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE,
Branch: 1 branch
BranchTrueFalseTotal
if (!dv) psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, 0 184 184
362 "the Reference has no single DigestValue");
363 {
364 184 SV *raw = sv_2mortal(F->text(aTHX_ dv));
365 STRLEN rl;
366 184 const char *rp = SvPVbyte(raw, rl);
367 184 SV *dec = sv_2mortal(newSV(psaml_b64_decoded_max(rl) + 1));
368 STRLEN dl;
369 184 SvPOK_on(dec);
370 184 dl = psaml_b64_decode_xml((unsigned char *)SvPVX(dec), rp, rl);
371 184 ▶ if (dl == (STRLEN)-1)
Branch: 1 branch
BranchTrueFalseTotal
if (dl == (STRLEN)-1) 0 184 184
372 0 psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE,
373 "the DigestValue is not base64");
374 184 SvCUR_set(dec, dl);
375 184 want = dec;
376 }
377 {
378 STRLEN al, bl;
379 const unsigned char *ap =
380 184 ▶ (const unsigned char *)SvPVbyte(digest, al);
Branch: 1 branch
BranchTrueFalseTotal
(const unsigned char *)SvPVbyte(digest, al); 184 0 184
381 const unsigned char *bp =
382 184 (const unsigned char *)SvPVbyte(want, bl);
383 184 ▶ if (!J->ct_eq(aTHX_ ap, al, bp, bl))
Branch: 1 branch
BranchTrueFalseTotal
if (!J->ct_eq(aTHX_ ap, al, bp, bl)) 15 169 184
384 15 psaml_throw(aTHX_ PSAML_E_BAD_DIGEST,
385 "the reference digest does not match the element");
386 }
387 }
388  
389 /* 8: the signature, over c14n of SignedInfo with the
390 * CanonicalizationMethod's OWN PrefixList.
391 *
392 * A DIFFERENT InclusiveNamespaces from the transform's above.
393 * Confusing the two is a signature that never verifies, with nothing
394 * in any log to say why, and it is the single easiest mistake to
395 * make in this file: both elements are called InclusiveNamespaces,
396 * both carry a PrefixList, and they sit four lines apart in the
397 * document. */
398 {
399 char pbuf[512];
400 const char *prefixes[PSAML_MAX_PREFIXES];
401 169 int np = psaml_prefix_list(aTHX_ cm, pbuf, sizeof pbuf,
402 prefixes, PSAML_MAX_PREFIXES);
403 frx_c14n c;
404 SV *canon;
405 STRLEN cl, sl;
406 const unsigned char *cp;
407 const char *sp;
408 SV *sigval, *sigraw;
409 SSize_t k, nkeys;
410 169 int ok = 0;
411  
412 169 Zero(&c, 1, frx_c14n);
413 169 c.mode = FRX_C14N_EXC;
414 169 c.comments = psaml_attr_is(aTHX_ cm, "Algorithm", PSAML_C14N_EXC_WC);
415 169 ▶ c.prefix_list = np ? prefixes : NULL;
Branch: 1 branch
BranchTrueFalseTotal
c.prefix_list = np ? prefixes : NULL; 0 169 169
416 169 c.n_prefix = np;
417 169 canon = F->c14n(aTHX_ si, &c);
418 169 ▶ if (!canon)
Branch: 1 branch
BranchTrueFalseTotal
if (!canon) 0 169 169
419 0 psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE,
420 "the SignedInfo would not canonicalise");
421 169 sv_2mortal(canon);
422 169 cp = (const unsigned char *)SvPVbyte(canon, cl);
423  
424 169 sv_node = psaml_only_child(aTHX_ sig, PSAML_NS_DSIG, "SignatureValue");
425 169 ▶ if (!sv_node)
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_node) 0 169 169
426 0 psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE,
427 "the Signature has no single SignatureValue");
428 169 sigval = sv_2mortal(F->text(aTHX_ sv_node));
429 169 sp = SvPVbyte(sigval, sl);
430 169 sigraw = sv_2mortal(newSV(psaml_b64_decoded_max(sl) + 1));
431 169 SvPOK_on(sigraw);
432 {
433 169 STRLEN dl = psaml_b64_decode_xml((unsigned char *)SvPVX(sigraw),
434 sp, sl);
435 169 ▶ if (dl == (STRLEN)-1)
Branch: 1 branch
BranchTrueFalseTotal
if (dl == (STRLEN)-1) 0 169 169
436 0 psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE,
437 "the SignatureValue is not base64");
438 169 SvCUR_set(sigraw, dl);
439 }
440  
441 /* 9: KeyInfo is NOT trusted. It is whatever the sender put there.
442 * Every key tried is one the deployer configured; any one passing
443 * is enough, and all failing is bad_signature. */
444 169 ▶ nkeys = ctx->keys ? (av_len(ctx->keys) + 1) : 0;
Branch: 1 branch
BranchTrueFalseTotal
nkeys = ctx->keys ? (av_len(ctx->keys) + 1) : 0; 169 0 169
445 169 ▶ if (!nkeys)
Branch: 1 branch
BranchTrueFalseTotal
if (!nkeys) 1 168 169
446 1 psaml_throw(aTHX_ PSAML_E_NO_KEY,
447 "no signing key is configured for this provider");
448 338 ▶ for (k = 0; k < nkeys && !ok; k++) {
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; k < nkeys && !ok; k++) { 170 168 338
for (k = 0; k < nkeys && !ok; k++) { 170 0 170
449 170 SV **e = av_fetch(ctx->keys, k, 0);
450 STRLEN pl, gl;
451 const char *pp;
452 const unsigned char *gp;
453 void *key;
454 170 ▶ if (!e || !*e || !SvOK(*e)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!e || !*e || !SvOK(*e)) continue; 170 0 170
if (!e || !*e || !SvOK(*e)) continue; 170 0 170
if (!e || !*e || !SvOK(*e)) continue; 0 170 170
455 170 key = psaml_key_from_config(aTHX_ *e);
456 170 ▶ if (!key) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!key) continue; 0 170 170
457 PERL_UNUSED_VAR(pp);
458 PERL_UNUSED_VAR(pl);
459 170 gp = (const unsigned char *)SvPVbyte(sigraw, gl);
460 170 ok = J->verify(aTHX_ key, jws_alg, strlen(jws_alg), cp, cl, gp, gl);
461 170 J->key_free(aTHX_ key);
462 }
463 168 ▶ if (!ok)
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) 10 158 168
464 10 psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE,
465 "no configured key verifies the signature");
466 }
467 }
468 158 return 1;
469 }
470  
471 #endif /* PSAML_SIGNATURE_H */
« xs/metadata.xs ↑ Summary include/psaml/psaml_abi.h »