| 1 |
|
| #ifndef PSAML_XML_H |
| 2 |
|
| #define PSAML_XML_H |
| 3 |
|
| |
| 4 |
|
| /* The XML escape for the three documents this dist writes, and the |
| 5 |
|
| * finders over an frx tree by SAML namespace and local name. |
| 6 |
|
| * |
| 7 |
|
| * There is no writer here and there is not going to be one. This dist |
| 8 |
|
| * emits an AuthnRequest, a LogoutRequest and its own SP metadata, all |
| 9 |
|
| * three assembled as strings the way Punk-Feed assembles a feed, and a |
| 10 |
|
| * general serialiser would be more code than the plugin. What that |
| 11 |
|
| * needs from XML is one escape, and the escape is below. |
| 12 |
|
| * |
| 13 |
|
| * READING is the opposite: reading is what a signature is checked over, |
| 14 |
|
| * and none of it happens here. It happens in File::Raw::XML, through |
| 15 |
|
| * frx_abi.h, and the finders below are the only shape in which this dist |
| 16 |
|
| * asks about a tree. Two finders by namespace and local name are the |
| 17 |
|
| * whole of what SAML needs, which is the reason there is no XPath in |
| 18 |
|
| * File::Raw::XML: an XPath transform is the classic signature-bypass |
| 19 |
|
| * surface in XML-DSig, and the way not to have that surface is not to |
| 20 |
|
| * have the language. */ |
| 21 |
|
| |
| 22 |
|
| #include "psaml_abi.h" |
| 23 |
|
| |
| 24 |
|
| /* The namespaces, spelled once. A SAML document that uses a different |
| 25 |
|
| * prefix for these is the normal case, not the exception, so nothing in |
| 26 |
|
| * this dist may ever match on a prefix. */ |
| 27 |
|
| #define PSAML_NS_PROTOCOL "urn:oasis:names:tc:SAML:2.0:protocol" |
| 28 |
|
| #define PSAML_NS_ASSERTION "urn:oasis:names:tc:SAML:2.0:assertion" |
| 29 |
|
| #define PSAML_NS_METADATA "urn:oasis:names:tc:SAML:2.0:metadata" |
| 30 |
|
| #define PSAML_NS_DSIG "http://www.w3.org/2000/09/xmldsig#" |
| 31 |
|
| #define PSAML_NS_XENC "http://www.w3.org/2001/04/xmlenc#" |
| 32 |
|
| |
| 33 |
|
| /* ---- the escape ------------------------------------------------------ */ |
| 34 |
|
| |
| 35 |
|
| /* Worst case is six bytes out per byte in (`"` and `'`). */ |
| 36 |
357 |
| PERL_STATIC_INLINE STRLEN psaml_xml_escaped_max(STRLEN n) { |
| 37 |
357 |
| return n * 6; |
| 38 |
|
| } |
| 39 |
|
| |
| 40 |
|
| /* One escaper for both text and attribute values. |
| 41 |
|
| * |
| 42 |
|
| * Separate escapers are what a canonicaliser needs, because c14n |
| 43 |
|
| * escapes different sets in the two positions and the signature is over |
| 44 |
|
| * the result. Nothing here is canonicalised: these are documents we |
| 45 |
|
| * write and someone else reads, so the safe move is the opposite one, |
| 46 |
|
| * escaping the union everywhere. `>` is escaped although only `]]>` |
| 47 |
|
| * requires it, and quotes are escaped although text does not require |
| 48 |
|
| * them, because an attribute value and a text node then cannot be got |
| 49 |
|
| * wrong by using the wrong one. */ |
| 50 |
357 |
| PERL_STATIC_INLINE STRLEN psaml_xml_escape(char *out, const char *in, STRLEN n) { |
| 51 |
357 |
| char *o = out; |
| 52 |
|
| STRLEN i; |
| 53 |
11353 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
10996 |
357 |
11353 |
|
| 54 |
10996 |
| unsigned char c = (unsigned char)in[i]; |
| 55 |
10996 |
| switch (c) { |
| 56 |
2 |
| case '&': memcpy(o, "&", 5); o += 5; break; |
| 57 |
1 |
| case '<': memcpy(o, "<", 4); o += 4; break; |
| 58 |
1 |
| case '>': memcpy(o, ">", 4); o += 4; break; |
| 59 |
3 |
| case '"': memcpy(o, """, 6); o += 6; break; |
| 60 |
1 |
| case '\'': memcpy(o, "'", 6); o += 6; break; |
| 61 |
10988 |
| default: *o++ = (char)c; break; |
| 62 |
|
| } |
| 63 |
|
| } |
| 64 |
357 |
| return (STRLEN)(o - out); |
| 65 |
|
| } |
| 66 |
|
| |
| 67 |
|
| /* ---- the finders ----------------------------------------------------- */ |
| 68 |
|
| |
| 69 |
|
| /* A direct child by namespace and local name, or NULL. `after` is the |
| 70 |
|
| * previous match, so a caller iterating siblings allocates nothing; |
| 71 |
|
| * pass NULL to start. */ |
| 72 |
1992 |
| PERL_STATIC_INLINE const frx_node *psaml_child(pTHX_ const frx_node *n, const char *ns, |
| 73 |
|
| const char *local, const frx_node *after) { |
| 74 |
1992 |
▶ | if (!n) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return NULL; |
0 |
1992 |
1992 |
|
| 75 |
1992 |
| return psaml_frx(aTHX)->find(n, ns, local, after); |
| 76 |
|
| } |
| 77 |
|
| |
| 78 |
|
| /* Exactly one direct child, or NULL if there are none or more than one. |
| 79 |
|
| * |
| 80 |
|
| * "More than one" is a refusal rather than a first-match, and that is |
| 81 |
|
| * the whole point of this function existing next to psaml_child. A |
| 82 |
|
| * Response with two Assertions, or an Assertion with two Subjects, is |
| 83 |
|
| * the shape a wrapping attack takes: the verifier is invited to check |
| 84 |
|
| * one and read the other. A caller that wants "the" element gets |
| 85 |
|
| * nothing when the document does not contain exactly one, and phase 6 |
| 86 |
|
| * turns that into a named error code. */ |
| 87 |
2946 |
| PERL_STATIC_INLINE const frx_node *psaml_only_child(pTHX_ const frx_node *n, |
| 88 |
|
| const char *ns, const char *local) { |
| 89 |
2946 |
| const frx_abi *F = psaml_frx(aTHX); |
| 90 |
|
| const frx_node *first, *second; |
| 91 |
2946 |
▶ | if (!n) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return NULL; |
0 |
2946 |
2946 |
|
| 92 |
2946 |
| first = F->find(n, ns, local, NULL); |
| 93 |
2946 |
▶ | if (!first) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!first) return NULL; |
1 |
2945 |
2946 |
|
| 94 |
2945 |
| second = F->find(n, ns, local, first); |
| 95 |
2945 |
▶ | return second ? NULL : first; |
Branch: 1 branch
| Branch | True | False | Total |
| return second ? NULL : first; |
2939 |
6 |
2945 |
|
| 96 |
|
| } |
| 97 |
|
| |
| 98 |
|
| /* An attribute value by local name in any namespace, or NULL. SAML's |
| 99 |
|
| * own attributes (ID, Version, Destination, InResponseTo) are all |
| 100 |
|
| * unprefixed and unnamespaced, which is what "any namespace" means |
| 101 |
|
| * here in practice. */ |
| 102 |
3691 |
| PERL_STATIC_INLINE const char *psaml_attr(pTHX_ const frx_node *n, const char *local, |
| 103 |
|
| STRLEN *len) { |
| 104 |
3691 |
▶ | if (!n) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return NULL; |
0 |
3691 |
3691 |
|
| 105 |
3691 |
| return psaml_frx(aTHX)->attr_value(n, NULL, local, len); |
| 106 |
|
| } |
| 107 |
|
| |
| 108 |
|
| #endif /* PSAML_XML_H */ |