include/psaml/psaml_response.h

filter: Statementstmt 96.4% 245 / 254 Branchbran 71.1% 175 / 246 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.0% 420 / 500 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/psaml/psaml_clos.h ↑ Summary include/psaml/psaml_reg.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 367 static void psaml_doc_release(pTHX_ void *p) {
55 367 psaml_doc_guard *g = (psaml_doc_guard *)p;
56 367 ▶ if (g) {
Branch: 1 branch
BranchTrueFalseTotal
if (g) { 367 0 367
57 367 ▶ 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); 367 0 367
58 367 Safefree(g);
59 }
60 367 }
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 430 PERL_STATIC_INLINE frx_doc *psaml_parse_response(pTHX_ SV *bytes,
74 psaml_doc_guard **guard) {
75 430 const frx_abi *F = psaml_frx(aTHX);
76 frx_opts o;
77 430 SV *err = NULL;
78 STRLEN n;
79 430 const char *p = SvPVbyte(bytes, n);
80 frx_doc *d;
81 psaml_doc_guard *g;
82  
83 430 F->opts_init(&o);
84 430 o.id_attrs = PSAML_ID_ATTRS;
85 430 o.n_id_attrs = 1;
86 430 o.max_depth = 100; /* a Response is five deep; 100 is generous */
87  
88 430 d = F->parse(aTHX_ p, n, &o, &err);
89 430 ▶ if (!d)
Branch: 1 branch
BranchTrueFalseTotal
if (!d) 63 367 430
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 367 Newxz(g, 1, psaml_doc_guard);
94 367 g->doc = d;
95 367 SAVEDESTRUCTOR_X(psaml_doc_release, g);
96 367 *guard = g;
97 367 return d;
98 }
99  
100 /* ---- small readers ---------------------------------------------------- */
101  
102 375 PERL_STATIC_INLINE SV *psaml_node_text(pTHX_ const frx_node *n) {
103 375 ▶ 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; 375 0 375
104 }
105  
106 296 PERL_STATIC_INLINE int psaml_sv_is(pTHX_ SV *sv, const char *want) {
107 STRLEN n;
108 const char *p;
109 296 ▶ if (!sv || !SvOK(sv)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return 0; 296 0 296
if (!sv || !SvOK(sv)) return 0; 0 296 296
110 296 p = SvPV_const(sv, n);
111 296 ▶ return n == strlen(want) && memEQ(p, want, n);
Branch: 2 branches
BranchTrueFalseTotal
return n == strlen(want) && memEQ(p, want, n); 287 9 296
return n == strlen(want) && memEQ(p, want, n); 287 0 287
112 }
113  
114 1232 PERL_STATIC_INLINE int psaml_str_is(const char *p, STRLEN n,
115 const char *want) {
116 1232 STRLEN w = strlen(want);
117 1232 ▶ return p && n == w && memEQ(p, want, w);
Branch: 3 branches
BranchTrueFalseTotal
return p && n == w && memEQ(p, want, w); 1232 0 1232
return p && n == w && memEQ(p, want, w); 1160 72 1232
return p && n == w && memEQ(p, want, w); 1157 3 1160
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 585 PERL_STATIC_INLINE IV psaml_attr_time(pTHX_ const frx_node *n,
124 const char *name, int *have) {
125 STRLEN vl;
126 585 const char *v = psaml_attr(aTHX_ n, name, &vl);
127 IV t;
128 585 *have = 0;
129 585 ▶ if (!v) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) return 0; 3 582 585
130 582 ▶ if (!psaml_time_parse(v, vl, &t))
Branch: 1 branch
BranchTrueFalseTotal
if (!psaml_time_parse(v, vl, &t)) 1 581 582
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 581 *have = 1;
135 581 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 295 PERL_STATIC_INLINE void psaml_push_attr(pTHX_ HV *into, const char *k,
147 STRLEN kl, SV *val) {
148 295 SV **e = hv_fetch(into, k, (I32)kl, 0);
149 AV *av;
150 295 ▶ if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) {
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) { 74 221 295
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) { 74 0 74
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) { 74 0 74
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) { 74 0 74
151 74 av = (AV *)SvRV(*e);
152 }
153 else {
154 221 av = newAV();
155 221 (void)hv_store(into, k, (I32)kl, newRV_noinc((SV *)av), 0);
156 }
157 295 av_push(av, val);
158 295 }
159  
160 78 PERL_STATIC_INLINE void psaml_read_attributes(pTHX_ const frx_node *assertion,
161 HV *attrs, HV *friendly) {
162 78 const frx_abi *F = psaml_frx(aTHX);
163 78 const frx_node *stmt = NULL;
164 156 while ((stmt = F->find(assertion, PSAML_NS_ASSERTION,
165 156 ▶ "AttributeStatement", stmt)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
"AttributeStatement", stmt)) != NULL) { 78 78 156
166 78 const frx_node *at = NULL;
167 229 ▶ 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) { 151 78 229
168 STRLEN nl, fl;
169 151 const char *nm = psaml_attr(aTHX_ at, "Name", &nl);
170 151 const char *fr = psaml_attr(aTHX_ at, "FriendlyName", &fl);
171 151 const frx_node *val = NULL;
172 151 ▶ if (!nm) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!nm) continue; 1 150 151
173 371 while ((val = F->find(at, PSAML_NS_ASSERTION,
174 371 ▶ "AttributeValue", val)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
"AttributeValue", val)) != NULL) { 221 150 371
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 221 SV *t = F->text(aTHX_ val);
181 221 psaml_push_attr(aTHX_ attrs, nm, nl, t);
182 221 ▶ if (fr && fl)
Branch: 2 branches
BranchTrueFalseTotal
if (fr && fl) 74 147 221
if (fr && fl) 74 0 74
183 74 psaml_push_attr(aTHX_ friendly, fr, fl,
184 newSVsv(t));
185 }
186 }
187 }
188 78 }
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 397 PERL_STATIC_INLINE SV *psaml_verify_response(pTHX_ SV *bytes,
197 psaml_checks *o) {
198 397 const frx_abi *F = psaml_frx(aTHX);
199 397 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 397 int sig_on_response = 0, sig_on_assertion = 0;
204 HV *out;
205 STRLEN vl;
206 const char *v;
207 /* The bearer window, carried out of the Subject block: it is the
208 * length of time a replay cache has to remember this assertion for,
209 * and the identity hands it to whoever keeps that cache. */
210 397 IV conf_na = 0;
211  
212 397 doc = psaml_parse_response(aTHX_ bytes, &guard);
213  
214 /* ---- structure, before any cryptography --------------------------- */
215  
216 335 root = F->root(doc);
217 {
218 STRLEN nsl, ll;
219 335 const char *ns = F->ns(root, &nsl);
220 335 const char *lo = F->local(root, &ll);
221 335 ▶ if (!psaml_str_is(ns, nsl, PSAML_NS_PROTOCOL)
Branch: 1 branch
BranchTrueFalseTotal
if (!psaml_str_is(ns, nsl, PSAML_NS_PROTOCOL) 275 60 335
222 275 ▶ || !psaml_str_is(lo, ll, "Response"))
Branch: 1 branch
BranchTrueFalseTotal
|| !psaml_str_is(lo, ll, "Response")) 0 275 275
223 60 psaml_throw(aTHX_ PSAML_E_XML_SHAPE,
224 "the root element is not samlp:Response");
225 }
226 275 ▶ if (!psaml_attr_is(aTHX_ root, "Version", "2.0"))
Branch: 1 branch
BranchTrueFalseTotal
if (!psaml_attr_is(aTHX_ root, "Version", "2.0")) 1 274 275
227 1 psaml_throw(aTHX_ PSAML_E_XML_SHAPE, "the Response is not Version 2.0");
228  
229 /* Status, before anything else is looked at: a provider that refused
230 * says so here, and the nested code plus any StatusMessage go in the
231 * message because a bare `Requester` with no detail is what a provider
232 * returns when the ACS URL is wrong, and that is the commonest
233 * first-integration failure there is. */
234 {
235 274 const frx_node *status = psaml_only_child(aTHX_ root, PSAML_NS_PROTOCOL,
236 "Status");
237 274 const frx_node *code = status
238 274 ▶ ? psaml_only_child(aTHX_ status, PSAML_NS_PROTOCOL, "StatusCode") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? psaml_only_child(aTHX_ status, PSAML_NS_PROTOCOL, "StatusCode") : NULL; 274 0 274
239 274 ▶ if (!code)
Branch: 1 branch
BranchTrueFalseTotal
if (!code) 0 274 274
240 0 psaml_throw(aTHX_ PSAML_E_XML_SHAPE, "the Response has no StatusCode");
241 274 v = psaml_attr(aTHX_ code, "Value", &vl);
242 274 ▶ 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 273 274
243 1 const frx_node *sub = psaml_child(aTHX_ code, PSAML_NS_PROTOCOL,
244 "StatusCode", NULL);
245 1 const frx_node *msg = psaml_child(aTHX_ status, PSAML_NS_PROTOCOL,
246 "StatusMessage", NULL);
247 1 STRLEN sl = 0;
248 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
249 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
250 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
251 "the provider returned %.*s%s%.*s%s%s",
252 (int)vl, v ? v : "",
253 sp ? " / " : "", (int)sl, sp ? sp : "",
254 1 ▶ m && SvCUR(m) ? ": " : "",
Branch: 1 branch
BranchTrueFalseTotal
m && SvCUR(m) ? ": " : "", 1 0 1
255 2 ▶ m && SvCUR(m) ? SvPV_nolen(m) : "");
Branch: 1 branch
BranchTrueFalseTotal
m && SvCUR(m) ? SvPV_nolen(m) : ""); 1 0 1
256 }
257 }
258  
259 /* EncryptedAssertion is refused and never decrypted. XML-Enc is a
260 * second cryptographic surface with its own padding-oracle history, and
261 * the fix is at the other end, so the message names it. */
262 273 ▶ 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 272 273
263 1 psaml_throw(aTHX_ PSAML_E_ENCRYPTED,
264 "this Response carries an EncryptedAssertion. Turn assertion "
265 "encryption off for this application at the identity "
266 "provider; the transport is already TLS");
267  
268 272 assertion = psaml_only_child(aTHX_ root, PSAML_NS_ASSERTION, "Assertion");
269 272 ▶ if (!assertion)
Branch: 1 branch
BranchTrueFalseTotal
if (!assertion) 5 267 272
270 5 psaml_throw(aTHX_ PSAML_E_XML_SHAPE,
271 "the Response does not carry exactly one Assertion");
272  
273 /* ---- the signatures ------------------------------------------------ */
274  
275 267 vctx.doc = doc;
276 267 vctx.keys = o->keys;
277 267 vctx.allow_sha1 = o->allow_sha1;
278  
279 267 sig_on_response = psaml_verify_element(aTHX_ &vctx, root);
280 266 sig_on_assertion = psaml_verify_element(aTHX_ &vctx, assertion);
281  
282 /* `either` is the default because providers differ on which they sign
283 * by default and a fresh integration should not fail on that. A
284 * verified signature on the Response covers the Assertion inside it:
285 * the reference digest is over the whole subtree. */
286 {
287 219 ▶ const char *r = o->require_signed ? o->require_signed : "either";
Branch: 1 branch
BranchTrueFalseTotal
const char *r = o->require_signed ? o->require_signed : "either"; 219 0 219
288 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
289 438 ▶ : strEQ(r, "response") ? sig_on_response
Branch: 1 branch
BranchTrueFalseTotal
: strEQ(r, "response") ? sig_on_response 2 217 219
290 433 ▶ : strEQ(r, "assertion") ? sig_on_assertion
Branch: 1 branch
BranchTrueFalseTotal
: strEQ(r, "assertion") ? sig_on_assertion 216 1 217
291 216 ▶ : (sig_on_response || sig_on_assertion);
Branch: 3 branches
BranchTrueFalseTotal
: (sig_on_response || sig_on_assertion); 214 2 216
: (sig_on_response || sig_on_assertion); 213 1 214
: (sig_on_response || sig_on_assertion); 150 63 213
292 219 ▶ if (!ok)
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) 65 154 219
293 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
294 "require_signed is '%s' and that is not satisfied "
295 "(Response %s, Assertion %s)", r,
296 sig_on_response ? "signed" : "unsigned",
297 sig_on_assertion ? "signed" : "unsigned");
298 }
299  
300 /* ---- and only now is anything read -------------------------------- */
301  
302 /* Issuer, from the verified element. When only the Response is signed,
303 * its signature covers the Assertion, so the Assertion's Issuer is
304 * equally proven; when only the Assertion is signed, the Response's
305 * Issuer is not, and is not checked. */
306 {
307 154 const frx_node *iss = psaml_only_child(aTHX_ assertion,
308 PSAML_NS_ASSERTION, "Issuer");
309 154 SV *t = psaml_node_text(aTHX_ iss);
310 154 ▶ 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)) 154 0 154
if (!t || !psaml_sv_is(aTHX_ t, o->idp_entity_id)) 8 146 154
311 8 psaml_throw(aTHX_ PSAML_E_UNKNOWN_ISSUER,
312 "the Assertion's Issuer is not this provider's entity id");
313 }
314  
315 /* Destination, when present, must be our ACS URL. */
316 146 v = psaml_attr(aTHX_ root, "Destination", &vl);
317 146 ▶ if (v && !psaml_str_is(v, vl, o->acs_url))
Branch: 2 branches
BranchTrueFalseTotal
if (v && !psaml_str_is(v, vl, o->acs_url)) 146 0 146
if (v && !psaml_str_is(v, vl, o->acs_url)) 1 145 146
318 1 psaml_throw(aTHX_ PSAML_E_BAD_DESTINATION,
319 "the Response's Destination is not this application's "
320 "assertion consumer URL");
321  
322 /* Conditions. skew applies on both edges, and 120 seconds is the
323 * default because providers' clocks are worse than anyone expects. */
324 145 conds = psaml_only_child(aTHX_ assertion, PSAML_NS_ASSERTION, "Conditions");
325 145 ▶ if (conds) {
Branch: 1 branch
BranchTrueFalseTotal
if (conds) { 145 0 145
326 int have;
327 145 IV nb = psaml_attr_time(aTHX_ conds, "NotBefore", &have);
328 145 ▶ if (have && nb > o->now + o->skew)
Branch: 2 branches
BranchTrueFalseTotal
if (have && nb > o->now + o->skew) 145 0 145
if (have && nb > o->now + o->skew) 1 144 145
329 1 psaml_throw(aTHX_ PSAML_E_NOT_YET_VALID,
330 "the Assertion is not valid until later");
331 {
332 144 IV na = psaml_attr_time(aTHX_ conds, "NotOnOrAfter", &have);
333 144 ▶ if (have && na <= o->now - o->skew)
Branch: 2 branches
BranchTrueFalseTotal
if (have && na <= o->now - o->skew) 143 1 144
if (have && na <= o->now - o->skew) 2 141 143
334 2 psaml_throw(aTHX_ PSAML_E_EXPIRED, "the Assertion has expired");
335 }
336 /* the audience must name us; any one AudienceRestriction naming the
337 * entity id is enough, and none of them naming it is a refusal */
338 {
339 142 const frx_node *ar = NULL;
340 142 int found = 0, any = 0;
341 143 while ((ar = F->find(conds, PSAML_NS_ASSERTION,
342 143 ▶ "AudienceRestriction", ar)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
"AudienceRestriction", ar)) != NULL) { 142 1 143
343 142 const frx_node *aud = NULL;
344 142 any = 1;
345 143 while ((aud = F->find(ar, PSAML_NS_ASSERTION, "Audience",
346 143 ▶ aud)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
aud)) != NULL) { 142 1 143
347 142 SV *t = psaml_node_text(aTHX_ aud);
348 142 ▶ 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; } 142 0 142
if (t && psaml_sv_is(aTHX_ t, o->entity_id)) { found = 1; break; } 141 1 142
349 }
350 142 ▶ if (found) break;
Branch: 1 branch
BranchTrueFalseTotal
if (found) break; 141 1 142
351 }
352 142 ▶ if (any && !found)
Branch: 2 branches
BranchTrueFalseTotal
if (any && !found) 142 0 142
if (any && !found) 1 141 142
353 1 psaml_throw(aTHX_ PSAML_E_BAD_AUDIENCE,
354 "no AudienceRestriction names this application");
355 }
356 }
357  
358 /* Subject: a NameID, and a bearer SubjectConfirmation whose Recipient
359 * is our ACS URL and whose window has not closed. */
360 141 subject = psaml_only_child(aTHX_ assertion, PSAML_NS_ASSERTION, "Subject");
361 141 ▶ if (!subject)
Branch: 1 branch
BranchTrueFalseTotal
if (!subject) 0 141 141
362 0 psaml_throw(aTHX_ PSAML_E_XML_SHAPE, "the Assertion has no single Subject");
363 141 nameid = psaml_only_child(aTHX_ subject, PSAML_NS_ASSERTION, "NameID");
364 141 ▶ if (!nameid)
Branch: 1 branch
BranchTrueFalseTotal
if (!nameid) 0 141 141
365 0 psaml_throw(aTHX_ PSAML_E_XML_SHAPE, "the Subject has no single NameID");
366  
367 {
368 141 const frx_node *sc = NULL;
369 141 const frx_node *good = NULL;
370 141 SV *in_response_to = NULL;
371 141 while ((sc = F->find(subject, PSAML_NS_ASSERTION,
372 141 ▶ "SubjectConfirmation", sc)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
"SubjectConfirmation", sc)) != NULL) { 141 0 141
373 const frx_node *scd;
374 int have;
375 141 ▶ if (!psaml_attr_is(aTHX_ sc, "Method",
Branch: 1 branch
BranchTrueFalseTotal
if (!psaml_attr_is(aTHX_ sc, "Method", 0 141 141
376 "urn:oasis:names:tc:SAML:2.0:cm:bearer"))
377 0 continue;
378 141 scd = psaml_child(aTHX_ sc, PSAML_NS_ASSERTION,
379 "SubjectConfirmationData", NULL);
380 141 ▶ if (!scd) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!scd) continue; 0 141 141
381 141 v = psaml_attr(aTHX_ scd, "Recipient", &vl);
382 141 ▶ if (v && !psaml_str_is(v, vl, o->acs_url))
Branch: 2 branches
BranchTrueFalseTotal
if (v && !psaml_str_is(v, vl, o->acs_url)) 141 0 141
if (v && !psaml_str_is(v, vl, o->acs_url)) 1 140 141
383 1 psaml_throw(aTHX_ PSAML_E_BAD_DESTINATION,
384 "the SubjectConfirmationData Recipient is not this "
385 "application's assertion consumer URL");
386 /* NotOnOrAfter is REQUIRED here, not optional.
387 *
388 * Profiles section 4.1.4.2 makes it mandatory on the bearer
389 * SubjectConfirmationData, and it is the only attribute in the
390 * document that bounds how long the assertion is worth anything.
391 * Treating it as optional - which this did until 0.03 - left an
392 * assertion with no window at all unbounded in time: IssueInstant
393 * is not a freshness check and Conditions/@NotOnOrAfter is
394 * likewise only enforced when present, so a Response minted a
395 * year ago verified exactly as well as one minted a second ago.
396 * With allow_idp_initiated on there was then nothing whatsoever
397 * to stop it being presented again for ever. */
398 {
399 140 IV na = psaml_attr_time(aTHX_ scd, "NotOnOrAfter", &have);
400 139 ▶ if (!have)
Branch: 1 branch
BranchTrueFalseTotal
if (!have) 2 137 139
401 2 psaml_throw(aTHX_ PSAML_E_XML_SHAPE,
402 "the bearer SubjectConfirmationData has no "
403 "NotOnOrAfter, which the Web Browser SSO profile "
404 "requires and which bounds how long this assertion "
405 "can be replayed");
406 137 ▶ if (na <= o->now - o->skew)
Branch: 1 branch
BranchTrueFalseTotal
if (na <= o->now - o->skew) 0 137 137
407 0 psaml_throw(aTHX_ PSAML_E_EXPIRED,
408 "the SubjectConfirmationData window has closed");
409 137 conf_na = na;
410 }
411 137 v = psaml_attr(aTHX_ scd, "InResponseTo", &vl);
412 137 ▶ if (v) in_response_to = sv_2mortal(newSVpvn(v, vl));
Branch: 1 branch
BranchTrueFalseTotal
if (v) in_response_to = sv_2mortal(newSVpvn(v, vl)); 126 11 137
413 137 good = sc;
414 137 break;
415 }
416 137 ▶ if (!good)
Branch: 1 branch
BranchTrueFalseTotal
if (!good) 0 137 137
417 0 psaml_throw(aTHX_ PSAML_E_XML_SHAPE,
418 "the Subject has no bearer SubjectConfirmation");
419  
420 /* InResponseTo against the flow. Both directions matter: a response
421 * to a request we did not send is unsolicited, and a response naming
422 * a different request is not ours. */
423 137 ▶ if (o->flow_id && SvOK(o->flow_id)) {
Branch: 2 branches
BranchTrueFalseTotal
if (o->flow_id && SvOK(o->flow_id)) { 137 0 137
if (o->flow_id && SvOK(o->flow_id)) { 73 64 137
424 73 ▶ if (!in_response_to)
Branch: 1 branch
BranchTrueFalseTotal
if (!in_response_to) 1 72 73
425 1 psaml_throw(aTHX_ PSAML_E_BAD_IN_RESPONSE,
426 "a login was started but the Response answers nothing");
427 72 ▶ if (!sv_eq(in_response_to, o->flow_id))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_eq(in_response_to, o->flow_id)) 1 71 72
428 1 psaml_throw(aTHX_ PSAML_E_BAD_IN_RESPONSE,
429 "the Response answers a different request");
430 }
431 64 ▶ else if (in_response_to) {
Branch: 1 branch
BranchTrueFalseTotal
else if (in_response_to) { 54 10 64
432 54 psaml_throw(aTHX_ PSAML_E_BAD_IN_RESPONSE,
433 "the Response answers a request this application has no "
434 "record of starting");
435 }
436 10 ▶ else if (!o->allow_idp_initiated) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!o->allow_idp_initiated) { 1 9 10
437 /* Off by default: an attacker who gets a victim's browser to POST a
438 * valid assertion for the ATTACKER'S account signs the victim into
439 * the attacker's account, and everything the victim then does is
440 * visible to the attacker. */
441 1 psaml_throw(aTHX_ PSAML_E_UNSOLICITED,
442 "this is an identity-provider-initiated login and "
443 "allow_idp_initiated is off");
444 }
445 }
446  
447 /* An AuthnStatement is what makes this an authentication rather than an
448 * attribute query. */
449 80 authn = psaml_child(aTHX_ assertion, PSAML_NS_ASSERTION,
450 "AuthnStatement", NULL);
451 80 ▶ if (!authn)
Branch: 1 branch
BranchTrueFalseTotal
if (!authn) 1 79 80
452 1 psaml_throw(aTHX_ PSAML_E_XML_SHAPE, "the Assertion has no AuthnStatement");
453  
454 /* Replay. The caller supplies the store, because whether it is shared
455 * across the worker pool is the application's decision and this
456 * function has no application. */
457 79 v = psaml_attr(aTHX_ assertion, "ID", &vl);
458 79 ▶ if (o->seen_cb && SvOK(o->seen_cb) && v) {
Branch: 3 branches
BranchTrueFalseTotal
if (o->seen_cb && SvOK(o->seen_cb) && v) { 3 76 79
if (o->seen_cb && SvOK(o->seen_cb) && v) { 3 0 3
if (o->seen_cb && SvOK(o->seen_cb) && v) { 3 0 3
459 3 dSP;
460 int count;
461 3 SV *id = sv_2mortal(newSVpvn(v, vl));
462 int seen;
463 3 ENTER; SAVETMPS;
464 3 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 3 3
465 3 ▶ XPUSHs(id);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(id); 0 3 3
466 3 PUTBACK;
467 3 count = call_sv(o->seen_cb, G_SCALAR);
468 3 SPAGAIN;
469 3 ▶ seen = count > 0 ? SvTRUE(TOPs) : 0;
Branch: 1 branch
BranchTrueFalseTotal
seen = count > 0 ? SvTRUE(TOPs) : 0; 3 0 3
470 3 ▶ if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) (void)POPs; 3 0 3
471 3 PUTBACK;
472 3 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 3 0 3
473 3 ▶ if (seen)
Branch: 1 branch
BranchTrueFalseTotal
if (seen) 1 2 3
474 1 psaml_throw(aTHX_ PSAML_E_REPLAY,
475 "this assertion has been presented before");
476 }
477  
478 /* ---- the identity, from the verified element and no other ---------- */
479  
480 78 out = newHV();
481 78 ▶ (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)); 78 0 78
482 78 ▶ 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)); 78 0 78
483 {
484 78 SV *t = psaml_node_text(aTHX_ nameid);
485 78 ▶ (void)hv_stores(out, "name_id", t ? newSVsv(t) : newSVpvs(""));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(out, "name_id", t ? newSVsv(t) : newSVpvs("")); 78 0 78
486 }
487 78 v = psaml_attr(aTHX_ nameid, "Format", &vl);
488 78 ▶ 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)); 78 0 78
489 78 v = psaml_attr(aTHX_ authn, "SessionIndex", &vl);
490 78 ▶ 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)); 78 0 78
491 {
492 int have;
493 78 IV t = psaml_attr_time(aTHX_ authn, "AuthnInstant", &have);
494 78 ▶ if (have) (void)hv_stores(out, "authn_instant", newSViv(t));
Branch: 1 branch
BranchTrueFalseTotal
if (have) (void)hv_stores(out, "authn_instant", newSViv(t)); 78 0 78
495 78 ▶ if (conds) {
Branch: 1 branch
BranchTrueFalseTotal
if (conds) { 78 0 78
496 78 t = psaml_attr_time(aTHX_ conds, "NotOnOrAfter", &have);
497 78 ▶ 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)); 78 0 78
498 }
499 }
500 /* How long a replay cache must remember this id for. Profiles section
501 * 4.1.4.5 names the bearer SubjectConfirmationData's NotOnOrAfter and
502 * not the Conditions one, because that is the window the assertion can
503 * actually be presented in; past it every other check refuses anyway,
504 * so a cache entry that outlives it is only occupying space. */
505 78 (void)hv_stores(out, "replay_until", newSViv(conf_na));
506 {
507 78 HV *attrs = newHV(), *friendly = newHV();
508 78 psaml_read_attributes(aTHX_ assertion, attrs, friendly);
509 78 (void)hv_stores(out, "attributes", newRV_noinc((SV *)attrs));
510 78 (void)hv_stores(out, "friendly", newRV_noinc((SV *)friendly));
511 }
512 /* the bytes as received, for an application that must keep an audit
513 * record of what it accepted */
514 78 (void)hv_stores(out, "raw", newSVsv(bytes));
515  
516 78 return newRV_noinc((SV *)out);
517 }
518  
519 /* ---- the replay store ------------------------------------------------- */
520  
521 /* True if this assertion has been presented before.
522 *
523 * Called by the ACS route between a successful verification and
524 * on_login. AFTER, because an id out of an unverified document is
525 * attacker-chosen and writing it to the store would let anyone fill the
526 * cache with whatever they liked; BEFORE, because on_login is where the
527 * session is established and one assertion must not buy two.
528 *
529 * `seen` is either a coderef - the application's own store, called with
530 * the context, the id and the window - or the name of one of the `cache`
531 * keyword's stores. The second is the default, because Profiles section
532 * 4.1.4.5 makes the check a MUST and the deployment that configured
533 * nothing is the one that needs it most.
534 *
535 * CROAKS rather than returning 0 when it cannot tell. "the store is
536 * unreachable" and "the assertion is fresh" must not produce the same
537 * login, so the caller wraps this in G_EVAL and turns a failure into a
538 * `config` refusal. Fail closed.
539 *
540 * The get-then-set is not atomic, and Punk::Cache has no compare-and-set
541 * to make it so. Two genuinely simultaneous presentations of one
542 * assertion can therefore both read absent and both proceed. That is a
543 * window of milliseconds against the minutes the assertion is otherwise
544 * replayable for, and closing it needs an addition to Punk::Cache rather
545 * than anything here; it is written down in the POD rather than left for
546 * somebody to discover. */
547 185 PERL_STATIC_INLINE int psaml_replayed(pTHX_ SV *c, SV *seen, SV *identity,
548 IV skew) {
549 HV *ih;
550 SV *id, *until;
551  
552 185 ▶ if (!identity || !SvROK(identity) || SvTYPE(SvRV(identity)) != SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (!identity || !SvROK(identity) || SvTYPE(SvRV(identity)) != SVt_PVHV) 185 0 185
if (!identity || !SvROK(identity) || SvTYPE(SvRV(identity)) != SVt_PVHV) 185 0 185
if (!identity || !SvROK(identity) || SvTYPE(SvRV(identity)) != SVt_PVHV) 0 185 185
553 0 croak("%s: the replay check was handed no identity", PSAML_WHO);
554 185 ih = (HV *)SvRV(identity);
555  
556 185 id = psaml_hget(aTHX_ ih, "assertion_id");
557 185 ▶ if (!id || !SvOK(id) || !SvCUR(id))
Branch: 3 branches
BranchTrueFalseTotal
if (!id || !SvOK(id) || !SvCUR(id)) 132 53 185
if (!id || !SvOK(id) || !SvCUR(id)) 132 0 132
if (!id || !SvOK(id) || !SvCUR(id)) 0 132 132
558 53 croak("%s: the Assertion has no ID, so a replay of it cannot be "
559 "detected", PSAML_WHO);
560 132 until = psaml_hget(aTHX_ ih, "replay_until");
561  
562 /* the application's own store */
563 132 ▶ if (psaml_is_code(aTHX_ seen)) {
Branch: 1 branch
BranchTrueFalseTotal
if (psaml_is_code(aTHX_ seen)) { 109 23 132
564 SV *args[3], *r;
565 int out;
566 109 args[0] = c;
567 109 args[1] = id;
568 109 ▶ args[2] = (until && SvOK(until)) ? until : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
args[2] = (until && SvOK(until)) ? until : &PL_sv_undef; 56 53 109
args[2] = (until && SvOK(until)) ? until : &PL_sv_undef; 56 0 56
569 109 r = psaml_call_cv(aTHX_ seen, args, 3);
570 108 ▶ out = (r && SvTRUE(r)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
out = (r && SvTRUE(r)) ? 1 : 0; 108 0 108
out = (r && SvTRUE(r)) ? 1 : 0; 1 107 108
571 108 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 108 0 108
572 108 return out;
573 }
574  
575 23 ▶ if (!seen || !SvOK(seen) || !SvCUR(seen))
Branch: 3 branches
BranchTrueFalseTotal
if (!seen || !SvOK(seen) || !SvCUR(seen)) 23 0 23
if (!seen || !SvOK(seen) || !SvCUR(seen)) 23 0 23
if (!seen || !SvOK(seen) || !SvCUR(seen)) 0 23 23
576 0 croak("%s: the replay check has no store: `seen` is neither a coderef "
577 "nor the name of a `cache` store", PSAML_WHO);
578  
579 {
580 SV *store, *key, *got, *args[3];
581 IV ttl;
582  
583 /* $c->cache($name). It croaks on an undeclared store, which on_compile
584 * has already refused, so reaching that croak means the store was
585 * taken away after boot and the login fails rather than passes. */
586 23 args[0] = seen;
587 23 store = psaml_call(aTHX_ c, "cache", args, 1);
588 23 ▶ if (!store)
Branch: 1 branch
BranchTrueFalseTotal
if (!store) 0 23 23
589 0 croak("%s: there is no `cache` store named '%s' to remember "
590 "assertion ids in", PSAML_WHO, SvPV_nolen(seen));
591 23 sv_2mortal(store);
592  
593 /* Prefixed, because the store is shared with whatever else the
594 * application caches and an assertion id is provider-chosen. */
595 23 key = sv_2mortal(newSVpvs("punk_saml.replay:"));
596 23 sv_catsv(key, id);
597  
598 23 args[0] = key;
599 23 got = psaml_call(aTHX_ store, "get", args, 1);
600 23 ▶ if (got) { SvREFCNT_dec(got); return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (got) { SvREFCNT_dec(got); return 1; } 5 18 23
601  
602 /* Remembered for as long as the assertion would still be ACCEPTED:
603 * the bearer window plus the skew the verifier allows past it. An
604 * entry that expired at the window alone would stop remembering the
605 * id while that id was still good for a login.
606 *
607 * A ttl of 0 means NO EXPIRY in Punk::Cache, so the floor is 1 and
608 * never 0: an assertion whose window has already closed gets a
609 * near-useless entry, which is right, because it will not be
610 * accepted again on its own merits either. */
611 18 ▶ ttl = ((until && SvOK(until)) ? SvIV(until) : 0) + skew - (IV)time(NULL);
Branch: 2 branches
BranchTrueFalseTotal
ttl = ((until && SvOK(until)) ? SvIV(until) : 0) + skew - (IV)time(NULL); 18 0 18
ttl = ((until && SvOK(until)) ? SvIV(until) : 0) + skew - (IV)time(NULL); 18 0 18
612 18 ▶ if (ttl < 1) ttl = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ttl < 1) ttl = 1; 0 18 18
613  
614 18 args[0] = key;
615 18 args[1] = sv_2mortal(newSVpvs("1"));
616 18 args[2] = sv_2mortal(newSViv(ttl));
617 18 SvREFCNT_dec(psaml_call(aTHX_ store, "set", args, 3));
618 18 return 0;
619 }
620 }
621  
622 #endif /* PSAML_RESPONSE_H */
« include/psaml/psaml_clos.h ↑ Summary include/psaml/psaml_reg.h »