include/psaml/psaml_metadata.h

filter: Statementstmt 96.7% 118 / 122 Branchbran 63.1% 53 / 84 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.0% 171 / 206 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/request.xs ↑ Summary include/psaml/psaml_reg.h »
1 #ifndef PSAML_METADATA_H
2 #define PSAML_METADATA_H
3  
4 /* SP metadata out, and the fetch that brings IdP metadata in.
5 *
6 * The reading is psaml_idp.h and it takes bytes. This file is the thin
7 * layer that gets the bytes: a URL through the fetch table, or a path off
8 * disk. Keeping them apart is what lets every refusal in the reader be a
9 * string in t/.
10 *
11 * Must be included after psaml_idp.h. */
12  
13 #include "psaml_idp.h"
14 #include "psaml_request.h"
15  
16 /* The specification's media type, not application/xml.
17 *
18 * Punk 0.46 gives $c->xml, which serialises a document and sends
19 * application/xml. That is the right serialiser and the wrong media type:
20 * SAML metadata is application/samlmetadata+xml, most consumers accept
21 * either, and the ones that do not are exactly the strict deployments
22 * this matters to. So the route sets the header itself. */
23 #define PSAML_CT_METADATA "application/samlmetadata+xml"
24  
25 /* ---- SP metadata, written ---------------------------------------------- */
26  
27 /* The document an operator pastes into a provider's setup screen, or
28 * points the provider at.
29 *
30 * Assembled by string with the six-case escape, as everything this dist
31 * writes is. `acs_url` is the ONE URL phase 4 computed at on_compile, and
32 * it is passed in rather than derived here, so this is not a fourth
33 * place that could disagree about what this application's ACS is. */
34 71 PERL_STATIC_INLINE SV *psaml_sp_metadata(pTHX_ SV *entity_id, SV *acs_url,
35 SV *cert, SV *name_id_format,
36 int authn_requests_signed,
37 int want_assertions_signed) {
38 71 SV *out = newSVpvs(
39 "<md:EntityDescriptor"
40 " xmlns:md=\"" PSAML_NS_METADATA "\""
41 " xmlns:ds=\"" PSAML_NS_DSIG "\"");
42 71 psaml_cat_attr(aTHX_ out, "entityID", entity_id);
43 71 sv_catpvs(out, "><md:SPSSODescriptor AuthnRequestsSigned=\"");
44 71 ▶ sv_catpv(out, authn_requests_signed ? "true" : "false");
Branch: 1 branch
BranchTrueFalseTotal
sv_catpv(out, authn_requests_signed ? "true" : "false"); 1 70 71
45 71 sv_catpvs(out, "\" WantAssertionsSigned=\"");
46 71 ▶ sv_catpv(out, want_assertions_signed ? "true" : "false");
Branch: 1 branch
BranchTrueFalseTotal
sv_catpv(out, want_assertions_signed ? "true" : "false"); 70 1 71
47 71 sv_catpvs(out, "\" protocolSupportEnumeration=\"" PSAML_NS_PROTOCOL "\">");
48  
49 /* only when there is one: a KeyDescriptor with no certificate in it is
50 * a document a provider will reject, and an empty element is worse
51 * than an absent one */
52 71 ▶ if (cert && SvOK(cert) && SvCUR(cert)) {
Branch: 3 branches
BranchTrueFalseTotal
if (cert && SvOK(cert) && SvCUR(cert)) { 1 70 71
if (cert && SvOK(cert) && SvCUR(cert)) { 1 0 1
if (cert && SvOK(cert) && SvCUR(cert)) { 1 0 1
53 /* the PEM armour and its newlines are stripped: what goes in the
54 * document is the base64 of the DER and nothing else, which is what
55 * <ds:X509Certificate> means */
56 STRLEN n;
57 1 const char *p = SvPVbyte(cert, n);
58 const char *b, *e;
59 static const char BEG[] = "-----BEGIN CERTIFICATE-----";
60 static const char END[] = "-----END CERTIFICATE-----";
61 1 b = ninstr((char *)p, (char *)p + n,
62 (char *)BEG, (char *)BEG + sizeof BEG - 1);
63 1 e = b ? ninstr((char *)b, (char *)p + n,
64 1 ▶ (char *)END, (char *)END + sizeof END - 1) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
(char *)END, (char *)END + sizeof END - 1) : NULL; 1 0 1
65 1 ▶ if (b && e) {
Branch: 2 branches
BranchTrueFalseTotal
if (b && e) { 1 0 1
if (b && e) { 1 0 1
66 STRLEN i;
67 1 b += sizeof BEG - 1;
68 1 sv_catpvs(out, "<md:KeyDescriptor use=\"signing\"><ds:KeyInfo>"
69 "<ds:X509Data><ds:X509Certificate>");
70 1047 ▶ for (i = 0; b + i < e; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; b + i < e; i++) { 1046 1 1047
71 1046 char c = b[i];
72 1046 ▶ if (c != '\n' && c != '\r' && c != ' ' && c != '\t')
Branch: 4 branches
BranchTrueFalseTotal
if (c != '\n' && c != '\r' && c != ' ' && c != '\t') 1028 18 1046
if (c != '\n' && c != '\r' && c != ' ' && c != '\t') 1028 0 1028
if (c != '\n' && c != '\r' && c != ' ' && c != '\t') 1028 0 1028
if (c != '\n' && c != '\r' && c != ' ' && c != '\t') 1028 0 1028
73 1028 sv_catpvn(out, &c, 1);
74 }
75 1 sv_catpvs(out, "</ds:X509Certificate></ds:X509Data></ds:KeyInfo>"
76 "</md:KeyDescriptor>");
77 }
78 }
79  
80 71 ▶ if (name_id_format && SvOK(name_id_format) && SvCUR(name_id_format)) {
Branch: 3 branches
BranchTrueFalseTotal
if (name_id_format && SvOK(name_id_format) && SvCUR(name_id_format)) { 1 70 71
if (name_id_format && SvOK(name_id_format) && SvCUR(name_id_format)) { 1 0 1
if (name_id_format && SvOK(name_id_format) && SvCUR(name_id_format)) { 1 0 1
81 1 sv_catpvs(out, "<md:NameIDFormat>");
82 1 psaml_cat_escaped(aTHX_ out, name_id_format);
83 1 sv_catpvs(out, "</md:NameIDFormat>");
84 }
85  
86 71 sv_catpvs(out, "<md:AssertionConsumerService index=\"0\" isDefault=\"true\""
87 " Binding=\"" PSAML_BIND_POST "\"");
88 71 psaml_cat_attr(aTHX_ out, "Location", acs_url);
89 71 sv_catpvs(out, "/></md:SPSSODescriptor></md:EntityDescriptor>");
90 71 return out;
91 }
92  
93 /* ---- sign_metadata ----------------------------------------------------- */
94  
95 /* The one place this distribution CREATES an XML signature.
96 *
97 * Some providers refuse unsigned metadata by policy, so it exists; it is
98 * not otherwise needed, and metadata is a public document.
99 *
100 * BE HONEST ABOUT WHAT THIS PROVES. The signer canonicalises through the
101 * same frx c14n the verifier uses and signs through the same jws table,
102 * so a test that signs here and verifies with psaml_signature.h proves
103 * the two agree and nothing more. If the canonicalisation were wrong,
104 * both halves would be wrong together and every such test would pass.
105 * What proves the c14n is File::Raw::XML's transcribed W3C vectors; what
106 * proves this document is a provider accepting it, which is phase 11.
107 *
108 * The Signature is spliced in as the first child by string assembly,
109 * after the document has been canonicalised without it - which is what
110 * `enveloped-signature` means and why the order here is not negotiable. */
111 1 PERL_STATIC_INLINE SV *psaml_sign_document(pTHX_ SV *xml, SV *id,
112 SV *key_pem, SV *cert_pem) {
113 1 const frx_abi *F = psaml_frx(aTHX);
114 1 const jws_abi *J = psaml_jws(aTHX);
115 1 psaml_doc_guard *guard = NULL;
116 frx_doc *doc;
117 const frx_node *target;
118 frx_c14n c;
119 SV *canon, *digest, *si, *si_canon, *sig, *out;
120 1 const char *jws_alg = NULL;
121 const char *sigalg;
122 STRLEN il;
123 const char *ip;
124  
125 1 ip = SvPVbyte(id, il);
126  
127 1 sigalg = psaml_sigalg_for(aTHX_ key_pem, &jws_alg);
128 /* the metadata signature uses the XML-DSig spelling of the algorithm,
129 * which is the same URI phase 5's redirect binding names */
130 1 ▶ if (strEQ(jws_alg, "RS256"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(jws_alg, "RS256")) 1 0 1
131 1 sigalg = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256";
132 else
133 0 sigalg = "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256";
134  
135 1 doc = psaml_parse_response(aTHX_ xml, &guard);
136 1 target = F->by_id(doc, "ID", ip, il);
137 1 ▶ if (!target)
Branch: 1 branch
BranchTrueFalseTotal
if (!target) 0 1 1
138 0 croak("%s: the document to sign has no element with ID '%.*s'",
139 PSAML_WHO, (int)il, ip);
140  
141 1 Zero(&c, 1, frx_c14n);
142 1 c.mode = FRX_C14N_EXC;
143 1 canon = F->c14n(aTHX_ target, &c);
144 1 ▶ if (!canon) croak("%s: the document would not canonicalise", PSAML_WHO);
Branch: 1 branch
BranchTrueFalseTotal
if (!canon) croak("%s: the document would not canonicalise", PSAML_WHO); 0 1 1
145 1 sv_2mortal(canon);
146  
147 {
148 STRLEN cl;
149 1 const unsigned char *cp = (const unsigned char *)SvPVbyte(canon, cl);
150 1 SV *raw = J->sha256(aTHX_ cp, cl);
151 STRLEN dn;
152 const unsigned char *dp;
153 1 ▶ if (!raw) croak("%s: the digest failed", PSAML_WHO);
Branch: 1 branch
BranchTrueFalseTotal
if (!raw) croak("%s: the digest failed", PSAML_WHO); 0 1 1
154 1 sv_2mortal(raw);
155 1 dp = (const unsigned char *)SvPVbyte(raw, dn);
156 1 digest = sv_2mortal(newSV(psaml_b64_encoded_len(dn) + 1));
157 1 SvPOK_on(digest);
158 1 SvCUR_set(digest, psaml_b64_encode(SvPVX(digest), dp, dn));
159 }
160  
161 1 si = sv_2mortal(newSVpvs("<ds:SignedInfo xmlns:ds=\"" PSAML_NS_DSIG "\">"
162 "<ds:CanonicalizationMethod Algorithm=\"" PSAML_C14N_EXC "\"/>"
163 "<ds:SignatureMethod Algorithm=\""));
164 1 sv_catpv(si, sigalg);
165 1 sv_catpvs(si, "\"/><ds:Reference URI=\"#");
166 1 sv_catpvn(si, ip, il);
167 1 sv_catpvs(si, "\"><ds:Transforms>"
168 "<ds:Transform Algorithm=\"" PSAML_TR_ENVELOPED "\"/>"
169 "<ds:Transform Algorithm=\"" PSAML_C14N_EXC "\"/>"
170 "</ds:Transforms>"
171 "<ds:DigestMethod Algorithm=\"http://www.w3.org/2001/04/xmlenc#sha256\"/>"
172 "<ds:DigestValue>");
173 1 sv_catsv(si, digest);
174 1 sv_catpvs(si, "</ds:DigestValue></ds:Reference></ds:SignedInfo>");
175  
176 /* SignedInfo is canonicalised ON ITS OWN, as a document, because that
177 * is what the verifier will do to it */
178 {
179 1 psaml_doc_guard *g2 = NULL;
180 1 frx_doc *d2 = psaml_parse_response(aTHX_ si, &g2);
181 frx_c14n c2;
182 1 Zero(&c2, 1, frx_c14n);
183 1 c2.mode = FRX_C14N_EXC;
184 1 si_canon = F->c14n(aTHX_ F->root(d2), &c2);
185 1 ▶ if (!si_canon) croak("%s: the SignedInfo would not canonicalise",
Branch: 1 branch
BranchTrueFalseTotal
if (!si_canon) croak("%s: the SignedInfo would not canonicalise", 0 1 1
186 PSAML_WHO);
187 1 sv_2mortal(si_canon);
188 }
189  
190 {
191 STRLEN sl, pl;
192 1 ▶ const unsigned char *sp = (const unsigned char *)SvPVbyte(si_canon, sl);
Branch: 1 branch
BranchTrueFalseTotal
const unsigned char *sp = (const unsigned char *)SvPVbyte(si_canon, sl); 1 0 1
193 1 const char *pp = SvPVbyte(key_pem, pl);
194 1 void *k = J->key_from_pem(aTHX_ pp, pl);
195 SV *raw;
196 STRLEN rn;
197 const unsigned char *rp;
198 1 ▶ if (!k) croak("%s: `key` will not parse as a PEM private key", PSAML_WHO);
Branch: 1 branch
BranchTrueFalseTotal
if (!k) croak("%s: `key` will not parse as a PEM private key", PSAML_WHO); 0 1 1
199 1 raw = J->sign(aTHX_ k, jws_alg, strlen(jws_alg), sp, sl);
200 1 J->key_free(aTHX_ k);
201 1 ▶ if (!raw) croak("%s: signing the metadata failed", PSAML_WHO);
Branch: 1 branch
BranchTrueFalseTotal
if (!raw) croak("%s: signing the metadata failed", PSAML_WHO); 0 1 1
202 1 sv_2mortal(raw);
203 1 rp = (const unsigned char *)SvPVbyte(raw, rn);
204 1 sig = sv_2mortal(newSVpvs("<ds:Signature xmlns:ds=\"" PSAML_NS_DSIG "\">"));
205 1 sv_catsv(sig, si);
206 1 sv_catpvs(sig, "<ds:SignatureValue>");
207 {
208 1 SV *b = sv_2mortal(newSV(psaml_b64_encoded_len(rn) + 1));
209 1 SvPOK_on(b);
210 1 SvCUR_set(b, psaml_b64_encode(SvPVX(b), rp, rn));
211 1 sv_catsv(sig, b);
212 }
213 1 sv_catpvs(sig, "</ds:SignatureValue>");
214 1 ▶ if (cert_pem && SvOK(cert_pem) && SvCUR(cert_pem)) {
Branch: 3 branches
BranchTrueFalseTotal
if (cert_pem && SvOK(cert_pem) && SvCUR(cert_pem)) { 1 0 1
if (cert_pem && SvOK(cert_pem) && SvCUR(cert_pem)) { 1 0 1
if (cert_pem && SvOK(cert_pem) && SvCUR(cert_pem)) { 1 0 1
215 STRLEN n;
216 1 const char *p = SvPVbyte(cert_pem, n);
217 const char *b, *e;
218 static const char BEG[] = "-----BEGIN CERTIFICATE-----";
219 static const char END[] = "-----END CERTIFICATE-----";
220 1 b = ninstr((char *)p, (char *)p + n,
221 (char *)BEG, (char *)BEG + sizeof BEG - 1);
222 1 e = b ? ninstr((char *)b, (char *)p + n,
223 1 ▶ (char *)END, (char *)END + sizeof END - 1) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
(char *)END, (char *)END + sizeof END - 1) : NULL; 1 0 1
224 1 ▶ if (b && e) {
Branch: 2 branches
BranchTrueFalseTotal
if (b && e) { 1 0 1
if (b && e) { 1 0 1
225 STRLEN i;
226 1 b += sizeof BEG - 1;
227 1 sv_catpvs(sig, "<ds:KeyInfo><ds:X509Data><ds:X509Certificate>");
228 1047 ▶ for (i = 0; b + i < e; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; b + i < e; i++) { 1046 1 1047
229 1046 char ch = b[i];
230 1046 ▶ if (ch != '\n' && ch != '\r' && ch != ' ' && ch != '\t')
Branch: 4 branches
BranchTrueFalseTotal
if (ch != '\n' && ch != '\r' && ch != ' ' && ch != '\t') 1028 18 1046
if (ch != '\n' && ch != '\r' && ch != ' ' && ch != '\t') 1028 0 1028
if (ch != '\n' && ch != '\r' && ch != ' ' && ch != '\t') 1028 0 1028
if (ch != '\n' && ch != '\r' && ch != ' ' && ch != '\t') 1028 0 1028
231 1028 sv_catpvn(sig, &ch, 1);
232 }
233 1 sv_catpvs(sig, "</ds:X509Certificate></ds:X509Data></ds:KeyInfo>");
234 }
235 }
236 1 sv_catpvs(sig, "</ds:Signature>");
237 }
238  
239 /* Splice it in as the first child of THE SIGNED ELEMENT, which is not
240 * necessarily the root.
241 *
242 * The first version of this put the Signature after the first `>` in
243 * the document, which is the root's start tag. That is correct for
244 * metadata, where the signed element IS the root, and wrong for
245 * anything nested - an Assertion inside a Response, say. The signature
246 * then sat on the Response while its Reference named the Assertion,
247 * and this dist's own verifier refused it with "the Reference names an
248 * element other than the one the Signature is a child of", which is
249 * exactly what that check is for.
250 *
251 * So the start tag is found by its ID attribute and the splice goes
252 * after the `>` that closes it. Quoted attribute values are skipped,
253 * because a `>` inside one is not the end of the tag. */
254 {
255 STRLEN xl;
256 1 const char *xp = SvPVbyte(xml, xl);
257 1 const char *end = xp + xl;
258 1 SV *needle = sv_2mortal(newSVpvs("ID=\""));
259 const char *at, *gt;
260 STRLEN nl2;
261 const char *np2;
262  
263 1 sv_catpvn(needle, ip, il);
264 1 sv_catpvs(needle, "\"");
265 1 np2 = SvPV_const(needle, nl2);
266  
267 1 at = ninstr((char *)xp, (char *)end, (char *)np2, (char *)np2 + nl2);
268 1 ▶ if (!at)
Branch: 1 branch
BranchTrueFalseTotal
if (!at) 0 1 1
269 0 croak("%s: the document does not carry ID=\"%.*s\" as written",
270 PSAML_WHO, (int)il, ip);
271  
272 /* forward to the `>` that closes this start tag */
273 1 gt = at;
274 38 ▶ while (gt < end && *gt != '>') {
Branch: 2 branches
BranchTrueFalseTotal
while (gt < end && *gt != '>') { 38 0 38
while (gt < end && *gt != '>') { 37 1 38
275 37 ▶ if (*gt == '"') {
Branch: 1 branch
BranchTrueFalseTotal
if (*gt == '"') { 4 33 37
276 4 gt++;
277 115 ▶ while (gt < end && *gt != '"') gt++;
Branch: 2 branches
BranchTrueFalseTotal
while (gt < end && *gt != '"') gt++; 115 0 115
while (gt < end && *gt != '"') gt++; 111 4 115
278 }
279 37 gt++;
280 }
281 1 ▶ if (gt >= end)
Branch: 1 branch
BranchTrueFalseTotal
if (gt >= end) 0 1 1
282 0 croak("%s: the element carrying ID=\"%.*s\" has no closing '>'",
283 PSAML_WHO, (int)il, ip);
284  
285 1 out = newSVpvn(xp, (STRLEN)(gt - xp) + 1);
286 1 sv_catsv(out, sig);
287 1 sv_catpvn(out, gt + 1, xl - (STRLEN)(gt - xp) - 1);
288 }
289 1 return out;
290 }
291  
292 #endif /* PSAML_METADATA_H */
« xs/request.xs ↑ Summary include/psaml/psaml_reg.h »