include/psaml/psaml_idp.h

filter: Statementstmt 96.0% 123 / 128 Branchbran 80.0% 64 / 80 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.9% 187 / 208 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/psaml/psaml_reg.h ↑ Summary include/psaml/psaml_request.h »
1 #ifndef PSAML_IDP_H
2 #define PSAML_IDP_H
3  
4 /* A provider, read out of its metadata.
5 *
6 * THE SEAM: reading is pure and takes bytes. Fetching is a thin layer
7 * over it in psaml_metadata.h. That is not tidiness. Every refusal
8 * below - a POST-only endpoint, a federation file with no chosen
9 * entity, a document with no usable key - is a decision that must be
10 * tested, and a reader that took a URL could only be tested against a
11 * network. This one is tested against strings.
12 *
13 * What comes back is a hashref, not an opaque struct, because
14 * `punk saml idp` prints it, the tests read it, and phase 6 wants the
15 * certificates as the PEM its psaml_key_from_config already reads.
16 *
17 * Must be included after psaml_xml.h, psaml_b64.h and psaml_error.h. */
18  
19 #include "psaml_abi.h"
20 #include "psaml_xml.h"
21 #include "psaml_b64.h"
22 #include "psaml_error.h"
23 #include "psaml_response.h"
24  
25 #define PSAML_BIND_REDIRECT \
26 "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"
27  
28 /* ---- certificates ------------------------------------------------------ */
29  
30 /* The DER of an <ds:X509Certificate>, whose content is base64 wrapped at
31 * whatever column the provider chose. psaml_b64_decode_xml is the variant
32 * that tolerates the wrapping and still refuses the url alphabet. */
33 21 PERL_STATIC_INLINE SV *psaml_cert_der(pTHX_ const frx_node *certnode) {
34 21 SV *txt = sv_2mortal(psaml_frx(aTHX)->text(aTHX_ certnode));
35 STRLEN n, dl;
36 21 const char *p = SvPVbyte(txt, n);
37 21 SV *der = newSV(psaml_b64_decoded_max(n) + 1);
38 21 SvPOK_on(der);
39 21 dl = psaml_b64_decode_xml((unsigned char *)SvPVX(der), p, n);
40 21 ▶ if (dl == (STRLEN)-1) {
Branch: 1 branch
BranchTrueFalseTotal
if (dl == (STRLEN)-1) { 0 21 21
41 0 SvREFCNT_dec(der);
42 0 return NULL;
43 }
44 21 SvCUR_set(der, dl);
45 21 *SvEND(der) = '\0';
46 21 return der;
47 }
48  
49 /* PEM, wrapped at 64, from DER. The certificate is kept in the form a
50 * deployer would paste and phase 6's psaml_key_from_config already
51 * reads, so a certificate that came from metadata and one that came from
52 * a `certs` option are the same thing downstream. */
53 20 PERL_STATIC_INLINE SV *psaml_der_to_pem(pTHX_ SV *der) {
54 STRLEN n, bl, i;
55 20 const unsigned char *p = (const unsigned char *)SvPVbyte(der, n);
56 20 SV *b64 = sv_2mortal(newSV(psaml_b64_encoded_len(n) + 1));
57 20 SV *out = newSVpvs("-----BEGIN CERTIFICATE-----\n");
58 const char *bp;
59 20 SvPOK_on(b64);
60 20 bl = psaml_b64_encode(SvPVX(b64), p, n);
61 20 SvCUR_set(b64, bl);
62 20 bp = SvPVX(b64);
63 360 ▶ for (i = 0; i < bl; i += 64) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < bl; i += 64) { 340 20 360
64 340 STRLEN take = (bl - i) < 64 ? (bl - i) : 64;
65 340 sv_catpvn(out, bp + i, take);
66 340 sv_catpvs(out, "\n");
67 }
68 20 sv_catpvs(out, "-----END CERTIFICATE-----\n");
69 20 return out;
70 }
71  
72 /* The fingerprint, as hex, over the SAME DER the key was made from.
73 *
74 * The same bytes matters: a provider's console shows the fingerprint of
75 * the certificate file, and an operator comparing what `punk saml idp`
76 * prints against that console is comparing two digests of one thing or
77 * discovering they are not. */
78 20 PERL_STATIC_INLINE SV *psaml_fingerprint(pTHX_ SV *der) {
79 static const char hex[] = "0123456789abcdef";
80 STRLEN n, i;
81 20 const unsigned char *p = (const unsigned char *)SvPVbyte(der, n);
82 20 SV *raw = psaml_jws(aTHX)->sha256(aTHX_ p, n);
83 SV *out;
84 const unsigned char *d;
85 STRLEN dn;
86 20 ▶ if (!raw) return newSVpvs("");
Branch: 1 branch
BranchTrueFalseTotal
if (!raw) return newSVpvs(""); 0 20 20
87 20 sv_2mortal(raw);
88 20 d = (const unsigned char *)SvPVbyte(raw, dn);
89 20 out = newSVpvs("");
90 660 ▶ for (i = 0; i < dn; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < dn; i++) { 640 20 660
91 char pair[2];
92 640 pair[0] = hex[(d[i] >> 4) & 0xF];
93 640 pair[1] = hex[d[i] & 0xF];
94 640 sv_catpvn(out, pair, 2);
95 }
96 20 return out;
97 }
98  
99 /* ---- reading ----------------------------------------------------------- */
100  
101 /* Pick the EntityDescriptor to read.
102 *
103 * A federation file is an EntitiesDescriptor holding many, and which one
104 * this application talks to is not something the file can say. Without
105 * `want`, the croak LISTS the ids found, because that is the message an
106 * operator can act on in one step rather than one that sends them back
107 * to the file with a text editor. */
108 28 PERL_STATIC_INLINE const frx_node *psaml_pick_entity(pTHX_ const frx_node *root,
109 const char *want) {
110 28 const frx_abi *F = psaml_frx(aTHX);
111 STRLEN ll;
112 28 const char *lo = F->local(root, &ll);
113  
114 28 ▶ if (psaml_str_is(lo, ll, "EntityDescriptor")) {
Branch: 1 branch
BranchTrueFalseTotal
if (psaml_str_is(lo, ll, "EntityDescriptor")) { 23 5 28
115 23 ▶ if (want && *want) {
Branch: 2 branches
BranchTrueFalseTotal
if (want && *want) { 3 20 23
if (want && *want) { 3 0 3
116 STRLEN el;
117 3 const char *e = psaml_attr(aTHX_ root, "entityID", &el);
118 3 ▶ if (!psaml_str_is(e, el, want))
Branch: 1 branch
BranchTrueFalseTotal
if (!psaml_str_is(e, el, want)) 1 2 3
119 1 ▶ psaml_throw(aTHX_ PSAML_E_CONFIG,
Branch: 2 branches
BranchTrueFalseTotal
psaml_throw(aTHX_ PSAML_E_CONFIG, 1 0 1
psaml_throw(aTHX_ PSAML_E_CONFIG, 1 0 1
120 "this metadata describes '%.*s', not the configured "
121 "entity_id '%s'", (int)(e ? el : 0), e ? e : "", want);
122 }
123 22 return root;
124 }
125 5 ▶ if (!psaml_str_is(lo, ll, "EntitiesDescriptor"))
Branch: 1 branch
BranchTrueFalseTotal
if (!psaml_str_is(lo, ll, "EntitiesDescriptor")) 2 3 5
126 2 psaml_throw(aTHX_ PSAML_E_CONFIG,
127 "the metadata root is neither EntityDescriptor nor "
128 "EntitiesDescriptor");
129  
130 {
131 3 const frx_node *e = NULL;
132 3 SV *found = sv_2mortal(newSVpvs(""));
133 3 int n = 0;
134 8 while ((e = F->find(root, PSAML_NS_METADATA, "EntityDescriptor",
135 8 ▶ e)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
e)) != NULL) { 6 2 8
136 STRLEN il;
137 6 const char *id = psaml_attr(aTHX_ e, "entityID", &il);
138 6 ▶ if (want && *want && psaml_str_is(id, il, want)) return e;
Branch: 3 branches
BranchTrueFalseTotal
if (want && *want && psaml_str_is(id, il, want)) return e; 4 2 6
if (want && *want && psaml_str_is(id, il, want)) return e; 4 0 4
if (want && *want && psaml_str_is(id, il, want)) return e; 1 3 4
139 5 ▶ if (n++) sv_catpvs(found, ", ");
Branch: 1 branch
BranchTrueFalseTotal
if (n++) sv_catpvs(found, ", "); 2 3 5
140 5 ▶ if (id) sv_catpvn(found, id, il);
Branch: 1 branch
BranchTrueFalseTotal
if (id) sv_catpvn(found, id, il); 5 0 5
141 }
142 2 ▶ if (want && *want)
Branch: 2 branches
BranchTrueFalseTotal
if (want && *want) 1 1 2
if (want && *want) 1 0 1
143 1 psaml_throw(aTHX_ PSAML_E_CONFIG,
144 "no entity '%s' in this federation metadata (found: %s)",
145 2 ▶ want, SvCUR(found) ? SvPV_nolen(found) : "none");
Branch: 1 branch
BranchTrueFalseTotal
want, SvCUR(found) ? SvPV_nolen(found) : "none"); 1 0 1
146 1 psaml_throw(aTHX_ PSAML_E_CONFIG,
147 "this is federation metadata describing %d entities; name "
148 "the one to use with entity_id (found: %s)", n,
149 2 ▶ SvCUR(found) ? SvPV_nolen(found) : "none");
Branch: 1 branch
BranchTrueFalseTotal
SvCUR(found) ? SvPV_nolen(found) : "none"); 1 0 1
150 }
151 0 return NULL;
152 }
153  
154 /* Read a provider out of metadata bytes. Returns a +1 hashref. */
155 29 PERL_STATIC_INLINE SV *psaml_idp_from_metadata(pTHX_ SV *bytes,
156 const char *want_entity,
157 int enforce_validity) {
158 29 const frx_abi *F = psaml_frx(aTHX);
159 29 psaml_doc_guard *guard = NULL;
160 frx_doc *doc;
161 const frx_node *root, *ent, *idpd, *kd;
162 29 HV *out = newHV();
163 29 SV *rv = newRV_noinc((SV *)out);
164 29 AV *certs = newAV(), *prints = newAV(), *formats = newAV();
165 STRLEN vl;
166 const char *v;
167  
168 29 sv_2mortal(rv);
169 29 (void)hv_stores(out, "certs", newRV_noinc((SV *)certs));
170 29 (void)hv_stores(out, "fingerprints", newRV_noinc((SV *)prints));
171 29 (void)hv_stores(out, "name_id_formats", newRV_noinc((SV *)formats));
172  
173 29 doc = psaml_parse_response(aTHX_ bytes, &guard);
174 28 root = F->root(doc);
175  
176 28 ent = psaml_pick_entity(aTHX_ root, want_entity);
177 23 v = psaml_attr(aTHX_ ent, "entityID", &vl);
178 23 ▶ if (!v || !vl)
Branch: 2 branches
BranchTrueFalseTotal
if (!v || !vl) 23 0 23
if (!v || !vl) 0 23 23
179 0 psaml_throw(aTHX_ PSAML_E_CONFIG,
180 "the EntityDescriptor has no entityID");
181 23 (void)hv_stores(out, "entity_id", newSVpvn(v, vl));
182  
183 23 idpd = psaml_child(aTHX_ ent, PSAML_NS_METADATA, "IDPSSODescriptor", NULL);
184 23 ▶ if (!idpd)
Branch: 1 branch
BranchTrueFalseTotal
if (!idpd) 1 22 23
185 1 psaml_throw(aTHX_ PSAML_E_CONFIG,
186 "'%.*s' publishes no IDPSSODescriptor, so it is not an "
187 "identity provider", (int)vl, v);
188  
189 /* The SSO URL, from the REDIRECT binding.
190 *
191 * A provider offering only POST is refused here, at boot, rather than
192 * at the first login: phase 5 sends by redirect, and a request sent by
193 * redirect to a POST-only endpoint is an error the provider phrases
194 * unhelpfully, at a user, hours later. */
195 {
196 22 const frx_node *sso = NULL;
197 22 int saw_any = 0;
198 23 while ((sso = F->find(idpd, PSAML_NS_METADATA,
199 23 ▶ "SingleSignOnService", sso)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
"SingleSignOnService", sso)) != NULL) { 21 2 23
200 21 saw_any = 1;
201 21 ▶ if (psaml_attr_is(aTHX_ sso, "Binding", PSAML_BIND_REDIRECT)) {
Branch: 1 branch
BranchTrueFalseTotal
if (psaml_attr_is(aTHX_ sso, "Binding", PSAML_BIND_REDIRECT)) { 20 1 21
202 20 const char *loc = psaml_attr(aTHX_ sso, "Location", &vl);
203 20 ▶ if (loc && vl) {
Branch: 2 branches
BranchTrueFalseTotal
if (loc && vl) { 20 0 20
if (loc && vl) { 20 0 20
204 20 (void)hv_stores(out, "sso_url", newSVpvn(loc, vl));
205 20 break;
206 }
207 }
208 }
209 22 ▶ if (!hv_exists(out, "sso_url", 7))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_exists(out, "sso_url", 7)) 2 20 22
210 2 ▶ psaml_throw(aTHX_ PSAML_E_CONFIG,
Branch: 1 branch
BranchTrueFalseTotal
psaml_throw(aTHX_ PSAML_E_CONFIG, 1 1 2
211 saw_any
212 ? "this provider publishes no HTTP-Redirect "
213 "SingleSignOnService. This plugin sends AuthnRequests "
214 "by redirect, which is the binding every provider "
215 "supports; ask for it to be enabled"
216 : "this provider publishes no SingleSignOnService at all");
217 }
218  
219 /* Every KeyDescriptor that is for signing.
220 *
221 * `use="signing"` OR NO `use` ATTRIBUTE. The specification says a
222 * missing `use` means the key serves both purposes, and providers omit
223 * it constantly; a reader that required the attribute would find no
224 * keys in perfectly ordinary metadata. `use="encryption"` is not a
225 * signing key and is skipped.
226 *
227 * All of them are kept, in order. A provider mid-rotation publishes
228 * the outgoing and the incoming certificate together, and phase 6
229 * tries each. */
230 20 kd = NULL;
231 41 while ((kd = F->find(idpd, PSAML_NS_METADATA, "KeyDescriptor",
232 41 ▶ kd)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
kd)) != NULL) { 22 19 41
233 const frx_node *ki, *x5d, *x5c;
234 22 const char *u = psaml_attr(aTHX_ kd, "use", &vl);
235 22 ▶ if (u && !psaml_str_is(u, vl, "signing")) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (u && !psaml_str_is(u, vl, "signing")) continue; 21 1 22
if (u && !psaml_str_is(u, vl, "signing")) continue; 1 20 21
236 21 ki = psaml_child(aTHX_ kd, PSAML_NS_DSIG, "KeyInfo", NULL);
237 21 ▶ if (!ki) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ki) continue; 0 21 21
238 21 x5d = NULL;
239 41 ▶ while ((x5d = F->find(ki, PSAML_NS_DSIG, "X509Data", x5d)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while ((x5d = F->find(ki, PSAML_NS_DSIG, "X509Data", x5d)) != NULL) { 21 20 41
240 21 x5c = NULL;
241 41 while ((x5c = F->find(x5d, PSAML_NS_DSIG, "X509Certificate",
242 41 ▶ x5c)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
x5c)) != NULL) { 21 20 41
243 21 SV *der = psaml_cert_der(aTHX_ x5c);
244 void *probe;
245 21 ▶ if (!der)
Branch: 1 branch
BranchTrueFalseTotal
if (!der) 0 21 21
246 0 psaml_throw(aTHX_ PSAML_E_CONFIG,
247 "an X509Certificate in this metadata is not base64");
248 21 sv_2mortal(der);
249 /* proven readable here rather than at the first login: a
250 * certificate that will not parse is a metadata problem and the
251 * operator is standing right here */
252 21 probe = psaml_jws(aTHX)->key_from_x509_der(aTHX_
253 21 (const unsigned char *)SvPVX(der), SvCUR(der));
254 21 ▶ if (!probe)
Branch: 1 branch
BranchTrueFalseTotal
if (!probe) 1 20 21
255 1 psaml_throw(aTHX_ PSAML_E_CONFIG,
256 "an X509Certificate in this metadata will not parse "
257 "as an X.509 certificate with an RSA or EC key");
258 20 psaml_jws(aTHX)->key_free(aTHX_ probe);
259 20 av_push(certs, psaml_der_to_pem(aTHX_ der));
260 20 av_push(prints, psaml_fingerprint(aTHX_ der));
261 }
262 }
263 }
264 19 ▶ if (av_len(certs) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(certs) < 0) 1 18 19
265 1 psaml_throw(aTHX_ PSAML_E_CONFIG,
266 "this provider's metadata publishes no signing certificate. "
267 "Every KeyDescriptor was use=\"encryption\", or there were "
268 "none");
269  
270 /* Not enforced by default, and this is a deliberate refusal to be
271 * strict: providers routinely sign with certificates that expired
272 * months ago, the trust here is the configured key rather than a
273 * chain, and refusing one helps nobody. The dates are printed by
274 * `punk saml idp` so an operator sees a rotation coming. */
275 PERL_UNUSED_VAR(enforce_validity);
276  
277 {
278 18 const frx_node *nf = NULL;
279 25 while ((nf = F->find(idpd, PSAML_NS_METADATA, "NameIDFormat",
280 25 ▶ nf)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
nf)) != NULL) { 7 18 25
281 7 SV *t = F->text(aTHX_ nf);
282 7 av_push(formats, t);
283 }
284 }
285  
286 18 (void)hv_stores(out, "want_authn_requests_signed",
287 newSViv(psaml_attr_is(aTHX_ idpd,
288 "WantAuthnRequestsSigned", "true")));
289  
290 18 return SvREFCNT_inc_simple_NN(rv);
291 }
292  
293 #endif /* PSAML_IDP_H */
« include/psaml/psaml_reg.h ↑ Summary include/psaml/psaml_request.h »