include/psaml/psaml_response.h

filter: Statementstmt 97.2% 208 / 214 Branchbran 71.3% 147 / 206 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.5% 355 / 420 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/psaml/psaml_reg.h ↑ Summary include/psaml/psaml_idp.h »
1 #ifndef PSAML_RESPONSE_H
2 #define PSAML_RESPONSE_H
3  
4 /* The checks, in order, and the identity.
5 *
6 * THE ORDER IS FIXED and it is not an optimisation: cheap before
7 * expensive, structural before cryptographic, and THE SIGNATURE BEFORE
8 * ANYTHING THE SIGNATURE PROTECTS IS READ. A verifier that reads the
9 * Issuer to pick a key, or the Conditions to fail fast, has read
10 * attacker-controlled bytes and acted on them before proving anyone sent
11 * them.
12 *
13 * After psaml_signature.h has returned, the verified element is held in
14 * ONE LOCAL and every read starts from that pointer. There is no second
15 * lookup, no search from the document root, and no "find the assertion".
16 *
17 * Must be included after psaml_signature.h. */
18  
19 #include "psaml_signature.h"
20 #include "psaml_time.h"
21 #include "psaml_request.h"
22  
23 /* Everything one verification needs. Passed by pointer so the checks read
24 * as a list rather than a function with fourteen arguments. */
25 typedef struct {
26 const char *idp_name;
27 const char *entity_id; /* ours: the audience and the Issuer we expect */
28 const char *idp_entity_id; /* theirs: what Issuer must say */
29 const char *acs_url;
30 AV *keys;
31 IV now;
32 IV skew;
33 int allow_sha1;
34 int allow_idp_initiated;
35 const char *require_signed;
36 SV *flow_id; /* the InResponseTo we expect, or NULL */
37 SV *seen_cb; /* replay: a coderef, or NULL */
38 } psaml_checks;
39  
40 /* ---- the document's lifetime ------------------------------------------ */
41  
42 /* Every check below can throw, and a thrown error unwinds past any free
43 * written after the call. So the document is registered for destruction
44 * the moment it exists and released on the way out, which covers the
45 * croak path and the return path with one mechanism.
46 *
47 * The exception, and it is a crash rather than a leak if it is got
48 * wrong: frx_abi's doc_to_sv TAKES OWNERSHIP. A document handed to Perl
49 * through it must not also be freed here, so the pointer is cleared
50 * before handing it over and this destructor sees NULL. Nothing in this
51 * phase does that; phase 7's operator tooling will. */
52 typedef struct { frx_doc *doc; } psaml_doc_guard;
53  
54 348 static void psaml_doc_release(pTHX_ void *p) {
55 348 psaml_doc_guard *g = (psaml_doc_guard *)p;
56 348 ▶ if (g) {
Branch: 1 branch
BranchTrueFalseTotal
if (g) { 348 0 348
57 348 ▶ if (g->doc) psaml_frx(aTHX)->doc_free(aTHX_ g->doc);
Branch: 1 branch
BranchTrueFalseTotal
if (g->doc) psaml_frx(aTHX)->doc_free(aTHX_ g->doc); 348 0 348
58 348 Safefree(g);
59 }
60 348 }
61  
62 /* ---- parsing ----------------------------------------------------------- */
63  
64 /* `ID` and only `ID`.
65 *
66 * by_id is what the Reference check resolves through, so what is indexed
67 * decides what a Reference can name. Indexing `Id` or `xml:id` as well
68 * would let a signature point at an element the provider never marked,
69 * and a duplicate value was already refused at parse, so this one name
70 * is the whole of the index. */
71 static const char *const PSAML_ID_ATTRS[] = { "ID" };
72  
73 411 PERL_STATIC_INLINE frx_doc *psaml_parse_response(pTHX_ SV *bytes,
74 psaml_doc_guard **guard) {
75 411 const frx_abi *F = psaml_frx(aTHX);
76 frx_opts o;
77 411 SV *err = NULL;
78 STRLEN n;
79 411 const char *p = SvPVbyte(bytes, n);
80 frx_doc *d;
81 psaml_doc_guard *g;
82  
83 411 F->opts_init(&o);
84 411 o.id_attrs = PSAML_ID_ATTRS;
85 411 o.n_id_attrs = 1;
86 411 o.max_depth = 100; /* a Response is five deep; 100 is generous */
87  
88 411 d = F->parse(aTHX_ p, n, &o, &err);
89 411 ▶ if (!d)
Branch: 1 branch
BranchTrueFalseTotal
if (!d) 63 348 411
90 63 psaml_throw(aTHX_ PSAML_E_XML_PARSE, "%s",
91 126 ▶ err ? SvPV_nolen(err) : "the document would not parse");
Branch: 1 branch
BranchTrueFalseTotal
err ? SvPV_nolen(err) : "the document would not parse"); 63 0 63
92  
93 348 Newxz(g, 1, psaml_doc_guard);
94 348 g->doc = d;
95 348 SAVEDESTRUCTOR_X(psaml_doc_release, g);
96 348 *guard = g;
97 348 return d;
98 }
99  
100 /* ---- small readers ---------------------------------------------------- */
101  
102 323 PERL_STATIC_INLINE SV *psaml_node_text(pTHX_ const frx_node *n) {
103 323 ▶ return n ? sv_2mortal(psaml_frx(aTHX)->text(aTHX_ n)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return n ? sv_2mortal(psaml_frx(aTHX)->text(aTHX_ n)) : NULL; 323 0 323
104 }
105  
106 260 PERL_STATIC_INLINE int psaml_sv_is(pTHX_ SV *sv, const char *want) {
107 STRLEN n;
108 const char *p;
109 260 ▶ if (!sv || !SvOK(sv)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return 0; 260 0 260
if (!sv || !SvOK(sv)) return 0; 0 260 260
110 260 p = SvPV_const(sv, n);
111 260 ▶ return n == strlen(want) && memEQ(p, want, n);
Branch: 2 branches
BranchTrueFalseTotal
return n == strlen(want) && memEQ(p, want, n); 251 9 260
return n == strlen(want) && memEQ(p, want, n); 251 0 251
112 }
113  
114 1139 PERL_STATIC_INLINE int psaml_str_is(const char *p, STRLEN n,
115 const char *want) {
116 1139 STRLEN w = strlen(want);
117 1139 ▶ return p && n == w && memEQ(p, want, w);
Branch: 3 branches
BranchTrueFalseTotal
return p && n == w && memEQ(p, want, w); 1139 0 1139
return p && n == w && memEQ(p, want, w); 1067 72 1139
return p && n == w && memEQ(p, want, w); 1064 3 1067
118 }
119  
120 /* An xs:dateTime attribute as epoch seconds; 0 and *have = 0 when absent,
121 * a throw when present and unparseable. A timestamp we cannot read is not
122 * a timestamp we may ignore: every one of them gates the login. */
123 499 PERL_STATIC_INLINE IV psaml_attr_time(pTHX_ const frx_node *n,
124 const char *name, int *have) {
125 STRLEN vl;
126 499 const char *v = psaml_attr(aTHX_ n, name, &vl);
127 IV t;
128 499 *have = 0;
129 499 ▶ if (!v) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) return 0; 0 499 499
130 499 ▶ if (!psaml_time_parse(v, vl, &t))
Branch: 1 branch
BranchTrueFalseTotal
if (!psaml_time_parse(v, vl, &t)) 1 498 499
131 1 psaml_throw(aTHX_ PSAML_E_BAD_DATETIME,
132 "%s is not an xs:dateTime this dist accepts: '%.*s'",
133 name, (int)vl, v);
134 498 *have = 1;
135 498 return t;
136 }
137  
138 /* ---- the identity ------------------------------------------------------ */
139  
140 /* Push a value onto the arrayref at key, creating it.
141 *
142 * MERGED, never overwritten. A provider that sends the same Name twice is
143 * not a provider making a mistake, it is how several of them send a
144 * multi-valued attribute, and taking the last would silently drop a
145 * group. */
146 231 PERL_STATIC_INLINE void psaml_push_attr(pTHX_ HV *into, const char *k,
147 STRLEN kl, SV *val) {
148 231 SV **e = hv_fetch(into, k, (I32)kl, 0);
149 AV *av;
150 231 ▶ if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) {
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) { 58 173 231
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) { 58 0 58
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) { 58 0 58
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) { 58 0 58
151 58 av = (AV *)SvRV(*e);
152 }
153 else {
154 173 av = newAV();
155 173 (void)hv_store(into, k, (I32)kl, newRV_noinc((SV *)av), 0);
156 }
157 231 av_push(av, val);
158 231 }
159  
160 62 PERL_STATIC_INLINE void psaml_read_attributes(pTHX_ const frx_node *assertion,
161 HV *attrs, HV *friendly) {
162 62 const frx_abi *F = psaml_frx(aTHX);
163 62 const frx_node *stmt = NULL;
164 124 while ((stmt = F->find(assertion, PSAML_NS_ASSERTION,
165 124 ▶ "AttributeStatement", stmt)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
"AttributeStatement", stmt)) != NULL) { 62 62 124
166 62 const frx_node *at = NULL;
167 181 ▶ while ((at = F->find(stmt, PSAML_NS_ASSERTION, "Attribute", at)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while ((at = F->find(stmt, PSAML_NS_ASSERTION, "Attribute", at)) != NULL) { 119 62 181
168 STRLEN nl, fl;
169 119 const char *nm = psaml_attr(aTHX_ at, "Name", &nl);
170 119 const char *fr = psaml_attr(aTHX_ at, "FriendlyName", &fl);
171 119 const frx_node *val = NULL;
172 119 ▶ if (!nm) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!nm) continue; 1 118 119
173 291 while ((val = F->find(at, PSAML_NS_ASSERTION,
174 291 ▶ "AttributeValue", val)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
"AttributeValue", val)) != NULL) { 173 118 291
175 /* xsi:type is ignored and a NameID-shaped value is just its text:
176 * an attribute value's type is the provider's business and the
177 * application wants the string either way. An empty value is
178 * kept as an empty string rather than dropped, so the count of
179 * values a provider sent is the count the application sees. */
180 173 SV *t = F->text(aTHX_ val);
181 173 psaml_push_attr(aTHX_ attrs, nm, nl, t);
182 173 ▶ if (fr && fl)
Branch: 2 branches
BranchTrueFalseTotal
if (fr && fl) 58 115 173
if (fr && fl) 58 0 58
183 58 psaml_push_attr(aTHX_ friendly, fr, fl,
184 newSVsv(t));
185 }
186 }
187 }
188 62 }
189  
190 /* ---- the verification -------------------------------------------------- */
191  
192 /* Verify a Response and return the identity as a +1 hashref.
193 *
194 * `bytes` is the DECODED XML; the base64 and the size cap were phase 5's
195 * psaml_decode_field, because those belong to the transport. */
196 378 PERL_STATIC_INLINE SV *psaml_verify_response(pTHX_ SV *bytes,
197 psaml_checks *o) {
198 378 const frx_abi *F = psaml_frx(aTHX);
199 378 psaml_doc_guard *guard = NULL;
200 frx_doc *doc;
201 const frx_node *root, *assertion, *conds, *subject, *nameid, *authn;
202 psaml_verify_ctx vctx;
203 378 int sig_on_response = 0, sig_on_assertion = 0;
204 HV *out;
205 STRLEN vl;
206 const char *v;
207  
208 378 doc = psaml_parse_response(aTHX_ bytes, &guard);
209  
210 /* ---- structure, before any cryptography --------------------------- */
211  
212 316 root = F->root(doc);
213 {
214 STRLEN nsl, ll;
215 316 const char *ns = F->ns(root, &nsl);
216 316 const char *lo = F->local(root, &ll);
217 316 ▶ if (!psaml_str_is(ns, nsl, PSAML_NS_PROTOCOL)
Branch: 1 branch
BranchTrueFalseTotal
if (!psaml_str_is(ns, nsl, PSAML_NS_PROTOCOL) 256 60 316
218 256 ▶ || !psaml_str_is(lo, ll, "Response"))
Branch: 1 branch
BranchTrueFalseTotal
|| !psaml_str_is(lo, ll, "Response")) 0 256 256
219 60 psaml_throw(aTHX_ PSAML_E_XML_SHAPE,
220 "the root element is not samlp:Response");
221 }
222 256 ▶ if (!psaml_attr_is(aTHX_ root, "Version", "2.0"))
Branch: 1 branch
BranchTrueFalseTotal
if (!psaml_attr_is(aTHX_ root, "Version", "2.0")) 1 255 256
223 1 psaml_throw(aTHX_ PSAML_E_XML_SHAPE, "the Response is not Version 2.0");
224  
225 /* Status, before anything else is looked at: a provider that refused
226 * says so here, and the nested code plus any StatusMessage go in the
227 * message because a bare `Requester` with no detail is what a provider
228 * returns when the ACS URL is wrong, and that is the commonest
229 * first-integration failure there is. */
230 {
231 255 const frx_node *status = psaml_only_child(aTHX_ root, PSAML_NS_PROTOCOL,
232 "Status");
233 255 const frx_node *code = status
234 255 ▶ ? psaml_only_child(aTHX_ status, PSAML_NS_PROTOCOL, "StatusCode") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? psaml_only_child(aTHX_ status, PSAML_NS_PROTOCOL, "StatusCode") : NULL; 255 0 255
235 255 ▶ if (!code)
Branch: 1 branch
BranchTrueFalseTotal
if (!code) 0 255 255
236 0 psaml_throw(aTHX_ PSAML_E_XML_SHAPE, "the Response has no StatusCode");
237 255 v = psaml_attr(aTHX_ code, "Value", &vl);
238 255 ▶ if (!psaml_str_is(v, vl, "urn:oasis:names:tc:SAML:2.0:status:Success")) {
Branch: 1 branch
BranchTrueFalseTotal
if (!psaml_str_is(v, vl, "urn:oasis:names:tc:SAML:2.0:status:Success")) { 1 254 255
239 1 const frx_node *sub = psaml_child(aTHX_ code, PSAML_NS_PROTOCOL,
240 "StatusCode", NULL);
241 1 const frx_node *msg = psaml_child(aTHX_ status, PSAML_NS_PROTOCOL,
242 "StatusMessage", NULL);
243 1 STRLEN sl = 0;
244 1 ▶ const char *sp = sub ? psaml_attr(aTHX_ sub, "Value", &sl) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *sp = sub ? psaml_attr(aTHX_ sub, "Value", &sl) : NULL; 0 1 1
245 1 ▶ SV *m = msg ? psaml_node_text(aTHX_ msg) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *m = msg ? psaml_node_text(aTHX_ msg) : NULL; 1 0 1
246 3 ▶ psaml_throw(aTHX_ PSAML_E_STATUS,
Branch: 5 branches
BranchTrueFalseTotal
psaml_throw(aTHX_ PSAML_E_STATUS, 1 0 1
psaml_throw(aTHX_ PSAML_E_STATUS, 1 0 1
psaml_throw(aTHX_ PSAML_E_STATUS, 0 1 1
psaml_throw(aTHX_ PSAML_E_STATUS, 0 1 1
psaml_throw(aTHX_ PSAML_E_STATUS, 1 0 1
247 "the provider returned %.*s%s%.*s%s%s",
248 (int)vl, v ? v : "",
249 sp ? " / " : "", (int)sl, sp ? sp : "",
250 1 ▶ m && SvCUR(m) ? ": " : "",
Branch: 1 branch
BranchTrueFalseTotal
m && SvCUR(m) ? ": " : "", 1 0 1
251 2 ▶ m && SvCUR(m) ? SvPV_nolen(m) : "");
Branch: 1 branch
BranchTrueFalseTotal
m && SvCUR(m) ? SvPV_nolen(m) : ""); 1 0 1
252 }
253 }
254  
255 /* EncryptedAssertion is refused and never decrypted. XML-Enc is a
256 * second cryptographic surface with its own padding-oracle history, and
257 * the fix is at the other end, so the message names it. */
258 254 ▶ if (psaml_child(aTHX_ root, PSAML_NS_ASSERTION, "EncryptedAssertion", NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (psaml_child(aTHX_ root, PSAML_NS_ASSERTION, "EncryptedAssertion", NULL)) 1 253 254
259 1 psaml_throw(aTHX_ PSAML_E_ENCRYPTED,
260 "this Response carries an EncryptedAssertion. Turn assertion "
261 "encryption off for this application at the identity "
262 "provider; the transport is already TLS");
263  
264 253 assertion = psaml_only_child(aTHX_ root, PSAML_NS_ASSERTION, "Assertion");
265 253 ▶ if (!assertion)
Branch: 1 branch
BranchTrueFalseTotal
if (!assertion) 5 248 253
266 5 psaml_throw(aTHX_ PSAML_E_XML_SHAPE,
267 "the Response does not carry exactly one Assertion");
268  
269 /* ---- the signatures ------------------------------------------------ */
270  
271 248 vctx.doc = doc;
272 248 vctx.keys = o->keys;
273 248 vctx.allow_sha1 = o->allow_sha1;
274  
275 248 sig_on_response = psaml_verify_element(aTHX_ &vctx, root);
276 247 sig_on_assertion = psaml_verify_element(aTHX_ &vctx, assertion);
277  
278 /* `either` is the default because providers differ on which they sign
279 * by default and a fresh integration should not fail on that. A
280 * verified signature on the Response covers the Assertion inside it:
281 * the reference digest is over the whole subtree. */
282 {
283 201 ▶ const char *r = o->require_signed ? o->require_signed : "either";
Branch: 1 branch
BranchTrueFalseTotal
const char *r = o->require_signed ? o->require_signed : "either"; 201 0 201
284 2 ▶ int ok = strEQ(r, "both") ? (sig_on_response && sig_on_assertion)
Branch: 2 branches
BranchTrueFalseTotal
int ok = strEQ(r, "both") ? (sig_on_response && sig_on_assertion) 1 1 2
int ok = strEQ(r, "both") ? (sig_on_response && sig_on_assertion) 1 0 1
285 402 ▶ : strEQ(r, "response") ? sig_on_response
Branch: 1 branch
BranchTrueFalseTotal
: strEQ(r, "response") ? sig_on_response 2 199 201
286 397 ▶ : strEQ(r, "assertion") ? sig_on_assertion
Branch: 1 branch
BranchTrueFalseTotal
: strEQ(r, "assertion") ? sig_on_assertion 198 1 199
287 198 ▶ : (sig_on_response || sig_on_assertion);
Branch: 3 branches
BranchTrueFalseTotal
: (sig_on_response || sig_on_assertion); 196 2 198
: (sig_on_response || sig_on_assertion); 195 1 196
: (sig_on_response || sig_on_assertion); 132 63 195
288 201 ▶ if (!ok)
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) 65 136 201
289 65 ▶ psaml_throw(aTHX_ PSAML_E_NO_SIGNATURE,
Branch: 2 branches
BranchTrueFalseTotal
psaml_throw(aTHX_ PSAML_E_NO_SIGNATURE, 1 64 65
psaml_throw(aTHX_ PSAML_E_NO_SIGNATURE, 1 64 65
290 "require_signed is '%s' and that is not satisfied "
291 "(Response %s, Assertion %s)", r,
292 sig_on_response ? "signed" : "unsigned",
293 sig_on_assertion ? "signed" : "unsigned");
294 }
295  
296 /* ---- and only now is anything read -------------------------------- */
297  
298 /* Issuer, from the verified element. When only the Response is signed,
299 * its signature covers the Assertion, so the Assertion's Issuer is
300 * equally proven; when only the Assertion is signed, the Response's
301 * Issuer is not, and is not checked. */
302 {
303 136 const frx_node *iss = psaml_only_child(aTHX_ assertion,
304 PSAML_NS_ASSERTION, "Issuer");
305 136 SV *t = psaml_node_text(aTHX_ iss);
306 136 ▶ if (!t || !psaml_sv_is(aTHX_ t, o->idp_entity_id))
Branch: 2 branches
BranchTrueFalseTotal
if (!t || !psaml_sv_is(aTHX_ t, o->idp_entity_id)) 136 0 136
if (!t || !psaml_sv_is(aTHX_ t, o->idp_entity_id)) 8 128 136
307 8 psaml_throw(aTHX_ PSAML_E_UNKNOWN_ISSUER,
308 "the Assertion's Issuer is not this provider's entity id");
309 }
310  
311 /* Destination, when present, must be our ACS URL. */
312 128 v = psaml_attr(aTHX_ root, "Destination", &vl);
313 128 ▶ if (v && !psaml_str_is(v, vl, o->acs_url))
Branch: 2 branches
BranchTrueFalseTotal
if (v && !psaml_str_is(v, vl, o->acs_url)) 128 0 128
if (v && !psaml_str_is(v, vl, o->acs_url)) 1 127 128
314 1 psaml_throw(aTHX_ PSAML_E_BAD_DESTINATION,
315 "the Response's Destination is not this application's "
316 "assertion consumer URL");
317  
318 /* Conditions. skew applies on both edges, and 120 seconds is the
319 * default because providers' clocks are worse than anyone expects. */
320 127 conds = psaml_only_child(aTHX_ assertion, PSAML_NS_ASSERTION, "Conditions");
321 127 ▶ if (conds) {
Branch: 1 branch
BranchTrueFalseTotal
if (conds) { 127 0 127
322 int have;
323 127 IV nb = psaml_attr_time(aTHX_ conds, "NotBefore", &have);
324 127 ▶ if (have && nb > o->now + o->skew)
Branch: 2 branches
BranchTrueFalseTotal
if (have && nb > o->now + o->skew) 127 0 127
if (have && nb > o->now + o->skew) 1 126 127
325 1 psaml_throw(aTHX_ PSAML_E_NOT_YET_VALID,
326 "the Assertion is not valid until later");
327 {
328 126 IV na = psaml_attr_time(aTHX_ conds, "NotOnOrAfter", &have);
329 126 ▶ if (have && na <= o->now - o->skew)
Branch: 2 branches
BranchTrueFalseTotal
if (have && na <= o->now - o->skew) 126 0 126
if (have && na <= o->now - o->skew) 2 124 126
330 2 psaml_throw(aTHX_ PSAML_E_EXPIRED, "the Assertion has expired");
331 }
332 /* the audience must name us; any one AudienceRestriction naming the
333 * entity id is enough, and none of them naming it is a refusal */
334 {
335 124 const frx_node *ar = NULL;
336 124 int found = 0, any = 0;
337 125 while ((ar = F->find(conds, PSAML_NS_ASSERTION,
338 125 ▶ "AudienceRestriction", ar)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
"AudienceRestriction", ar)) != NULL) { 124 1 125
339 124 const frx_node *aud = NULL;
340 124 any = 1;
341 125 while ((aud = F->find(ar, PSAML_NS_ASSERTION, "Audience",
342 125 ▶ aud)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
aud)) != NULL) { 124 1 125
343 124 SV *t = psaml_node_text(aTHX_ aud);
344 124 ▶ if (t && psaml_sv_is(aTHX_ t, o->entity_id)) { found = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (t && psaml_sv_is(aTHX_ t, o->entity_id)) { found = 1; break; } 124 0 124
if (t && psaml_sv_is(aTHX_ t, o->entity_id)) { found = 1; break; } 123 1 124
345 }
346 124 ▶ if (found) break;
Branch: 1 branch
BranchTrueFalseTotal
if (found) break; 123 1 124
347 }
348 124 ▶ if (any && !found)
Branch: 2 branches
BranchTrueFalseTotal
if (any && !found) 124 0 124
if (any && !found) 1 123 124
349 1 psaml_throw(aTHX_ PSAML_E_BAD_AUDIENCE,
350 "no AudienceRestriction names this application");
351 }
352 }
353  
354 /* Subject: a NameID, and a bearer SubjectConfirmation whose Recipient
355 * is our ACS URL and whose window has not closed. */
356 123 subject = psaml_only_child(aTHX_ assertion, PSAML_NS_ASSERTION, "Subject");
357 123 ▶ if (!subject)
Branch: 1 branch
BranchTrueFalseTotal
if (!subject) 0 123 123
358 0 psaml_throw(aTHX_ PSAML_E_XML_SHAPE, "the Assertion has no single Subject");
359 123 nameid = psaml_only_child(aTHX_ subject, PSAML_NS_ASSERTION, "NameID");
360 123 ▶ if (!nameid)
Branch: 1 branch
BranchTrueFalseTotal
if (!nameid) 0 123 123
361 0 psaml_throw(aTHX_ PSAML_E_XML_SHAPE, "the Subject has no single NameID");
362  
363 {
364 123 const frx_node *sc = NULL;
365 123 const frx_node *good = NULL;
366 123 SV *in_response_to = NULL;
367 123 while ((sc = F->find(subject, PSAML_NS_ASSERTION,
368 123 ▶ "SubjectConfirmation", sc)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
"SubjectConfirmation", sc)) != NULL) { 123 0 123
369 const frx_node *scd;
370 int have;
371 123 ▶ if (!psaml_attr_is(aTHX_ sc, "Method",
Branch: 1 branch
BranchTrueFalseTotal
if (!psaml_attr_is(aTHX_ sc, "Method", 0 123 123
372 "urn:oasis:names:tc:SAML:2.0:cm:bearer"))
373 0 continue;
374 123 scd = psaml_child(aTHX_ sc, PSAML_NS_ASSERTION,
375 "SubjectConfirmationData", NULL);
376 123 ▶ if (!scd) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!scd) continue; 0 123 123
377 123 v = psaml_attr(aTHX_ scd, "Recipient", &vl);
378 123 ▶ if (v && !psaml_str_is(v, vl, o->acs_url))
Branch: 2 branches
BranchTrueFalseTotal
if (v && !psaml_str_is(v, vl, o->acs_url)) 123 0 123
if (v && !psaml_str_is(v, vl, o->acs_url)) 1 122 123
379 1 psaml_throw(aTHX_ PSAML_E_BAD_DESTINATION,
380 "the SubjectConfirmationData Recipient is not this "
381 "application's assertion consumer URL");
382 {
383 122 IV na = psaml_attr_time(aTHX_ scd, "NotOnOrAfter", &have);
384 121 ▶ if (have && na <= o->now - o->skew)
Branch: 2 branches
BranchTrueFalseTotal
if (have && na <= o->now - o->skew) 121 0 121
if (have && na <= o->now - o->skew) 0 121 121
385 0 psaml_throw(aTHX_ PSAML_E_EXPIRED,
386 "the SubjectConfirmationData window has closed");
387 }
388 121 v = psaml_attr(aTHX_ scd, "InResponseTo", &vl);
389 121 ▶ if (v) in_response_to = sv_2mortal(newSVpvn(v, vl));
Branch: 1 branch
BranchTrueFalseTotal
if (v) in_response_to = sv_2mortal(newSVpvn(v, vl)); 112 9 121
390 121 good = sc;
391 121 break;
392 }
393 121 ▶ if (!good)
Branch: 1 branch
BranchTrueFalseTotal
if (!good) 0 121 121
394 0 psaml_throw(aTHX_ PSAML_E_XML_SHAPE,
395 "the Subject has no bearer SubjectConfirmation");
396  
397 /* InResponseTo against the flow. Both directions matter: a response
398 * to a request we did not send is unsolicited, and a response naming
399 * a different request is not ours. */
400 121 ▶ if (o->flow_id && SvOK(o->flow_id)) {
Branch: 2 branches
BranchTrueFalseTotal
if (o->flow_id && SvOK(o->flow_id)) { 121 0 121
if (o->flow_id && SvOK(o->flow_id)) { 59 62 121
401 59 ▶ if (!in_response_to)
Branch: 1 branch
BranchTrueFalseTotal
if (!in_response_to) 1 58 59
402 1 psaml_throw(aTHX_ PSAML_E_BAD_IN_RESPONSE,
403 "a login was started but the Response answers nothing");
404 58 ▶ if (!sv_eq(in_response_to, o->flow_id))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_eq(in_response_to, o->flow_id)) 1 57 58
405 1 psaml_throw(aTHX_ PSAML_E_BAD_IN_RESPONSE,
406 "the Response answers a different request");
407 }
408 62 ▶ else if (in_response_to) {
Branch: 1 branch
BranchTrueFalseTotal
else if (in_response_to) { 54 8 62
409 54 psaml_throw(aTHX_ PSAML_E_BAD_IN_RESPONSE,
410 "the Response answers a request this application has no "
411 "record of starting");
412 }
413 8 ▶ else if (!o->allow_idp_initiated) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!o->allow_idp_initiated) { 1 7 8
414 /* Off by default: an attacker who gets a victim's browser to POST a
415 * valid assertion for the ATTACKER'S account signs the victim into
416 * the attacker's account, and everything the victim then does is
417 * visible to the attacker. */
418 1 psaml_throw(aTHX_ PSAML_E_UNSOLICITED,
419 "this is an identity-provider-initiated login and "
420 "allow_idp_initiated is off");
421 }
422 }
423  
424 /* An AuthnStatement is what makes this an authentication rather than an
425 * attribute query. */
426 64 authn = psaml_child(aTHX_ assertion, PSAML_NS_ASSERTION,
427 "AuthnStatement", NULL);
428 64 ▶ if (!authn)
Branch: 1 branch
BranchTrueFalseTotal
if (!authn) 1 63 64
429 1 psaml_throw(aTHX_ PSAML_E_XML_SHAPE, "the Assertion has no AuthnStatement");
430  
431 /* Replay. The caller supplies the store, because whether it is shared
432 * across the worker pool is the application's decision and this
433 * function has no application. */
434 63 v = psaml_attr(aTHX_ assertion, "ID", &vl);
435 63 ▶ if (o->seen_cb && SvOK(o->seen_cb) && v) {
Branch: 3 branches
BranchTrueFalseTotal
if (o->seen_cb && SvOK(o->seen_cb) && v) { 3 60 63
if (o->seen_cb && SvOK(o->seen_cb) && v) { 3 0 3
if (o->seen_cb && SvOK(o->seen_cb) && v) { 3 0 3
436 3 dSP;
437 int count;
438 3 SV *id = sv_2mortal(newSVpvn(v, vl));
439 int seen;
440 3 ENTER; SAVETMPS;
441 3 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 3 3
442 3 ▶ XPUSHs(id);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(id); 0 3 3
443 3 PUTBACK;
444 3 count = call_sv(o->seen_cb, G_SCALAR);
445 3 SPAGAIN;
446 3 ▶ seen = count > 0 ? SvTRUE(TOPs) : 0;
Branch: 1 branch
BranchTrueFalseTotal
seen = count > 0 ? SvTRUE(TOPs) : 0; 3 0 3
447 3 ▶ if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) (void)POPs; 3 0 3
448 3 PUTBACK;
449 3 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 3 0 3
450 3 ▶ if (seen)
Branch: 1 branch
BranchTrueFalseTotal
if (seen) 1 2 3
451 1 psaml_throw(aTHX_ PSAML_E_REPLAY,
452 "this assertion has been presented before");
453 }
454  
455 /* ---- the identity, from the verified element and no other ---------- */
456  
457 62 out = newHV();
458 62 ▶ (void)hv_stores(out, "idp", newSVpv(o->idp_name ? o->idp_name : "", 0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(out, "idp", newSVpv(o->idp_name ? o->idp_name : "", 0)); 62 0 62
459 62 ▶ if (v) (void)hv_stores(out, "assertion_id", newSVpvn(v, vl));
Branch: 1 branch
BranchTrueFalseTotal
if (v) (void)hv_stores(out, "assertion_id", newSVpvn(v, vl)); 62 0 62
460 {
461 62 SV *t = psaml_node_text(aTHX_ nameid);
462 62 ▶ (void)hv_stores(out, "name_id", t ? newSVsv(t) : newSVpvs(""));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(out, "name_id", t ? newSVsv(t) : newSVpvs("")); 62 0 62
463 }
464 62 v = psaml_attr(aTHX_ nameid, "Format", &vl);
465 62 ▶ if (v) (void)hv_stores(out, "name_id_format", newSVpvn(v, vl));
Branch: 1 branch
BranchTrueFalseTotal
if (v) (void)hv_stores(out, "name_id_format", newSVpvn(v, vl)); 62 0 62
466 62 v = psaml_attr(aTHX_ authn, "SessionIndex", &vl);
467 62 ▶ if (v) (void)hv_stores(out, "session_index", newSVpvn(v, vl));
Branch: 1 branch
BranchTrueFalseTotal
if (v) (void)hv_stores(out, "session_index", newSVpvn(v, vl)); 62 0 62
468 {
469 int have;
470 62 IV t = psaml_attr_time(aTHX_ authn, "AuthnInstant", &have);
471 62 ▶ if (have) (void)hv_stores(out, "authn_instant", newSViv(t));
Branch: 1 branch
BranchTrueFalseTotal
if (have) (void)hv_stores(out, "authn_instant", newSViv(t)); 62 0 62
472 62 ▶ if (conds) {
Branch: 1 branch
BranchTrueFalseTotal
if (conds) { 62 0 62
473 62 t = psaml_attr_time(aTHX_ conds, "NotOnOrAfter", &have);
474 62 ▶ if (have) (void)hv_stores(out, "not_on_or_after", newSViv(t));
Branch: 1 branch
BranchTrueFalseTotal
if (have) (void)hv_stores(out, "not_on_or_after", newSViv(t)); 62 0 62
475 }
476 }
477 {
478 62 HV *attrs = newHV(), *friendly = newHV();
479 62 psaml_read_attributes(aTHX_ assertion, attrs, friendly);
480 62 (void)hv_stores(out, "attributes", newRV_noinc((SV *)attrs));
481 62 (void)hv_stores(out, "friendly", newRV_noinc((SV *)friendly));
482 }
483 /* the bytes as received, for an application that must keep an audit
484 * record of what it accepted */
485 62 (void)hv_stores(out, "raw", newSVsv(bytes));
486  
487 62 return newRV_noinc((SV *)out);
488 }
489  
490 #endif /* PSAML_RESPONSE_H */
« include/psaml/psaml_reg.h ↑ Summary include/psaml/psaml_idp.h »