| 1 |
|
| #ifndef PSAML_SIGNATURE_H |
| 2 |
|
| #define PSAML_SIGNATURE_H |
| 3 |
|
| |
| 4 |
|
| /* XML-DSig, verified. |
| 5 |
|
| * |
| 6 |
|
| * ONE RULE, applied without exception, and everything else in this file |
| 7 |
|
| * is machinery for it: |
| 8 |
|
| * |
| 9 |
|
| * THE ELEMENT WHOSE CHILD THE SIGNATURE IS, IS THE ELEMENT THE |
| 10 |
|
| * SIGNATURE COVERS, IS THE ELEMENT THE IDENTITY IS READ FROM. |
| 11 |
|
| * |
| 12 |
|
| * The reason SAML libraries have had a decade of authentication bypasses |
| 13 |
|
| * is that the signed thing and the read thing were allowed to differ. An |
| 14 |
|
| * attacker takes a signed assertion for their own account, adds an |
| 15 |
|
| * unsigned assertion for the administrator's, and arranges the document |
| 16 |
|
| * so the verifier finds one and the reader finds the other. Every |
| 17 |
|
| * published variant is that. So this file returns the verified node, the |
| 18 |
|
| * caller holds it in one local, and every read below it starts from that |
| 19 |
|
| * pointer. There is no second lookup and no search from the root. |
| 20 |
|
| * |
| 21 |
|
| * Must be included after psaml_xml.h, psaml_b64.h and psaml_error.h. */ |
| 22 |
|
| |
| 23 |
|
| #include "psaml_abi.h" |
| 24 |
|
| #include "psaml_xml.h" |
| 25 |
|
| #include "psaml_b64.h" |
| 26 |
|
| #include "psaml_error.h" |
| 27 |
|
| |
| 28 |
|
| #define PSAML_C14N_EXC "http://www.w3.org/2001/10/xml-exc-c14n#" |
| 29 |
|
| #define PSAML_C14N_EXC_WC "http://www.w3.org/2001/10/xml-exc-c14n#WithComments" |
| 30 |
|
| #define PSAML_TR_ENVELOPED "http://www.w3.org/2000/09/xmldsig#enveloped-signature" |
| 31 |
|
| |
| 32 |
|
| #define PSAML_MAX_PREFIXES 64 |
| 33 |
|
| |
| 34 |
|
| /* The accepted SignatureMethods, and what each is called in jws_abi. |
| 35 |
|
| * |
| 36 |
|
| * There is deliberately no rsa-sha1 here, and allow_sha1 does not add |
| 37 |
|
| * one. See PSAML_DIGESTS below for where that option does reach. */ |
| 38 |
|
| typedef struct { |
| 39 |
|
| const char *uri; |
| 40 |
|
| const char *alg; /* the jws algorithm name */ |
| 41 |
|
| } psaml_sigalg_map; |
| 42 |
|
| |
| 43 |
|
| static const psaml_sigalg_map PSAML_SIGALGS[] = { |
| 44 |
|
| { "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", "RS256" }, |
| 45 |
|
| { "http://www.w3.org/2001/04/xmldsig-more#rsa-sha384", "RS384" }, |
| 46 |
|
| { "http://www.w3.org/2001/04/xmldsig-more#rsa-sha512", "RS512" }, |
| 47 |
|
| { "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256", "ES256" }, |
| 48 |
|
| { "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha384", "ES384" }, |
| 49 |
|
| { "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha512", "ES512" }, |
| 50 |
|
| { NULL, NULL } |
| 51 |
|
| }; |
| 52 |
|
| |
| 53 |
|
| /* DigestMethod URIs. |
| 54 |
|
| * |
| 55 |
|
| * SHA-256 and, under allow_sha1, SHA-1. NOT 384 or 512, and that is a |
| 56 |
|
| * limitation of jws_abi rather than a decision: the table carries sha256 |
| 57 |
|
| * and sha1 as one-shot digests and nothing else, so this dist cannot |
| 58 |
|
| * compute a SHA-384 or SHA-512 reference digest at all. They are listed |
| 59 |
|
| * here so the refusal names them specifically instead of reporting an |
| 60 |
|
| * unknown algorithm, and the fix is two entries appended to jws_abi. |
| 61 |
|
| * |
| 62 |
|
| * This bites nothing in practice: every identity provider in the field |
| 63 |
|
| * digests with SHA-256, and the SignatureMethod side, which does go |
| 64 |
|
| * through jws `verify`, takes all of RS/ES 256, 384 and 512 already. |
| 65 |
|
| * |
| 66 |
|
| * SHA-1 is reachable only on the DIGEST side. There is no rsa-sha1 in |
| 67 |
|
| * PSAML_SIGALGS and allow_sha1 does not add one: JWS has no RS1, and |
| 68 |
|
| * adding one so SAML can accept a signature algorithm deprecated since |
| 69 |
|
| * 2011 is the wrong direction. The digest side is the less harmful of |
| 70 |
|
| * the two and is where the option reaches. */ |
| 71 |
|
| typedef struct { const char *uri; int bits; int needs_allow_sha1; } psaml_dig; |
| 72 |
|
| |
| 73 |
|
| static const psaml_dig PSAML_DIGESTS[] = { |
| 74 |
|
| { "http://www.w3.org/2001/04/xmlenc#sha256", 256, 0 }, |
| 75 |
|
| { "http://www.w3.org/2000/09/xmldsig#sha1", 160, 1 }, |
| 76 |
|
| { "http://www.w3.org/2001/04/xmldsig-more#sha384", 384, 0 }, /* refused */ |
| 77 |
|
| { "http://www.w3.org/2001/04/xmlenc#sha512", 512, 0 }, /* refused */ |
| 78 |
|
| { NULL, 0, 0 } |
| 79 |
|
| }; |
| 80 |
|
| |
| 81 |
|
| /* Returns the bit length, 0 for an unknown or disallowed algorithm. */ |
| 82 |
166 |
| PERL_STATIC_INLINE int psaml_digest_bits(const char *uri, STRLEN n, |
| 83 |
|
| int allow_sha1) { |
| 84 |
|
| int i; |
| 85 |
168 |
▶ | for (i = 0; PSAML_DIGESTS[i].uri; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; PSAML_DIGESTS[i].uri; i++) { |
168 |
0 |
168 |
|
| 86 |
168 |
| STRLEN ul = strlen(PSAML_DIGESTS[i].uri); |
| 87 |
168 |
▶ | if (n == ul && memEQ(uri, PSAML_DIGESTS[i].uri, ul)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (n == ul && memEQ(uri, PSAML_DIGESTS[i].uri, ul)) { |
166 |
2 |
168 |
| if (n == ul && memEQ(uri, PSAML_DIGESTS[i].uri, ul)) { |
166 |
0 |
166 |
|
| 88 |
166 |
▶ | if (PSAML_DIGESTS[i].needs_allow_sha1 && !allow_sha1) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (PSAML_DIGESTS[i].needs_allow_sha1 && !allow_sha1) return 0; |
2 |
164 |
166 |
| if (PSAML_DIGESTS[i].needs_allow_sha1 && !allow_sha1) return 0; |
1 |
1 |
2 |
|
| 89 |
165 |
| return PSAML_DIGESTS[i].bits; |
| 90 |
|
| } |
| 91 |
|
| } |
| 92 |
0 |
| return 0; |
| 93 |
|
| } |
| 94 |
|
| |
| 95 |
|
| /* ---- small helpers over the tree -------------------------------------- */ |
| 96 |
|
| |
| 97 |
|
| /* An attribute compared to a literal, whole. */ |
| 98 |
1097 |
| PERL_STATIC_INLINE int psaml_attr_is(pTHX_ const frx_node *n, |
| 99 |
|
| const char *name, const char *want) { |
| 100 |
|
| STRLEN vl; |
| 101 |
1097 |
| const char *v = psaml_attr(aTHX_ n, name, &vl); |
| 102 |
1097 |
| STRLEN wl = strlen(want); |
| 103 |
1097 |
▶ | return v && vl == wl && memEQ(v, want, wl); |
Branch: 3 branches
| Branch | True | False | Total |
| return v && vl == wl && memEQ(v, want, wl); |
1082 |
15 |
1097 |
| return v && vl == wl && memEQ(v, want, wl); |
923 |
159 |
1082 |
| return v && vl == wl && memEQ(v, want, wl); |
922 |
1 |
923 |
|
| 104 |
|
| } |
| 105 |
|
| |
| 106 |
|
| /* The direct-child ds:Signature, or NULL. |
| 107 |
|
| * |
| 108 |
|
| * DIRECT CHILD, and that is the first of the nine rules rather than an |
| 109 |
|
| * implementation convenience. A Signature inside Extensions, inside |
| 110 |
|
| * Subject, or inside another assertion's Advice is not looked at, and its |
| 111 |
|
| * presence does not make anything signed. A verifier that searched the |
| 112 |
|
| * subtree for "a signature" is a verifier that can be handed one over a |
| 113 |
|
| * different element. */ |
| 114 |
497 |
| PERL_STATIC_INLINE const frx_node *psaml_sig_of(pTHX_ const frx_node *n) { |
| 115 |
497 |
| return psaml_child(aTHX_ n, PSAML_NS_DSIG, "Signature", NULL); |
| 116 |
|
| } |
| 117 |
|
| |
| 118 |
|
| /* Collect an InclusiveNamespaces PrefixList into a caller-owned array of |
| 119 |
|
| * borrowed pointers. Returns the count. |
| 120 |
|
| * |
| 121 |
|
| * The list is whitespace-separated prefixes; "#default" names the default |
| 122 |
|
| * namespace. The strings are cut out of the attribute value, which lives |
| 123 |
|
| * in the document's arena, so they are NUL-terminated copies made here |
| 124 |
|
| * because the value is one string and c14n wants separate ones. */ |
| 125 |
316 |
| PERL_STATIC_INLINE int psaml_prefix_list(pTHX_ const frx_node *transform, |
| 126 |
|
| char *buf, STRLEN buflen, |
| 127 |
|
| const char **out, int max) { |
| 128 |
|
| const frx_node *inc; |
| 129 |
|
| const char *v; |
| 130 |
|
| STRLEN vl, i, start; |
| 131 |
316 |
| int n = 0; |
| 132 |
316 |
▶ | if (!transform) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!transform) return 0; |
0 |
316 |
316 |
|
| 133 |
316 |
| inc = psaml_child(aTHX_ transform, PSAML_C14N_EXC, "InclusiveNamespaces", |
| 134 |
|
| NULL); |
| 135 |
316 |
▶ | if (!inc) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!inc) return 0; |
316 |
0 |
316 |
|
| 136 |
0 |
| v = psaml_attr(aTHX_ inc, "PrefixList", &vl); |
| 137 |
0 |
▶ | if (!v || !vl) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!v || !vl) return 0; |
0 |
0 |
0 |
| if (!v || !vl) return 0; |
0 |
0 |
0 |
|
| 138 |
0 |
▶ | if (vl >= buflen) vl = buflen - 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (vl >= buflen) vl = buflen - 1; |
0 |
0 |
0 |
|
| 139 |
0 |
| memcpy(buf, v, vl); |
| 140 |
0 |
| buf[vl] = '\0'; |
| 141 |
0 |
| i = 0; |
| 142 |
0 |
▶ | while (i < vl && n < max) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (i < vl && n < max) { |
0 |
0 |
0 |
| while (i < vl && n < max) { |
0 |
0 |
0 |
|
| 143 |
0 |
▶ | while (i < vl && (buf[i] == ' ' || buf[i] == '\t' |
Branch: 3 branches
| Branch | True | False | Total |
| while (i < vl && (buf[i] == ' ' || buf[i] == '\t' |
0 |
0 |
0 |
| while (i < vl && (buf[i] == ' ' || buf[i] == '\t' |
0 |
0 |
0 |
| while (i < vl && (buf[i] == ' ' || buf[i] == '\t' |
0 |
0 |
0 |
|
| 144 |
0 |
▶ | || buf[i] == '\r' || buf[i] == '\n')) i++; |
Branch: 2 branches
| Branch | True | False | Total |
| || buf[i] == '\r' || buf[i] == '\n')) i++; |
0 |
0 |
0 |
| || buf[i] == '\r' || buf[i] == '\n')) i++; |
0 |
0 |
0 |
|
| 145 |
0 |
▶ | if (i >= vl) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i >= vl) break; |
0 |
0 |
0 |
|
| 146 |
0 |
| start = i; |
| 147 |
0 |
▶ | while (i < vl && !(buf[i] == ' ' || buf[i] == '\t' |
Branch: 3 branches
| Branch | True | False | Total |
| while (i < vl && !(buf[i] == ' ' || buf[i] == '\t' |
0 |
0 |
0 |
| while (i < vl && !(buf[i] == ' ' || buf[i] == '\t' |
0 |
0 |
0 |
| while (i < vl && !(buf[i] == ' ' || buf[i] == '\t' |
0 |
0 |
0 |
|
| 148 |
0 |
▶ | || buf[i] == '\r' || buf[i] == '\n')) i++; |
Branch: 2 branches
| Branch | True | False | Total |
| || buf[i] == '\r' || buf[i] == '\n')) i++; |
0 |
0 |
0 |
| || buf[i] == '\r' || buf[i] == '\n')) i++; |
0 |
0 |
0 |
|
| 149 |
0 |
| buf[i < vl ? i : vl] = '\0'; |
| 150 |
0 |
| out[n++] = buf + start; |
| 151 |
0 |
| i++; |
| 152 |
|
| } |
| 153 |
0 |
| return n; |
| 154 |
|
| } |
| 155 |
|
| |
| 156 |
|
| /* ---- the verification ------------------------------------------------- */ |
| 157 |
|
| |
| 158 |
|
| typedef struct { |
| 159 |
|
| const frx_doc *doc; |
| 160 |
|
| AV *keys; /* PEM certificates or public keys, as SVs */ |
| 161 |
|
| int allow_sha1; |
| 162 |
|
| } psaml_verify_ctx; |
| 163 |
|
| |
| 164 |
|
| /* A configured signing key, which is usually a CERTIFICATE. |
| 165 |
|
| * |
| 166 |
|
| * SAML metadata carries <ds:X509Certificate>, and what a deployer pastes |
| 167 |
|
| * into `certs` is a PEM certificate, not a PUBLIC KEY block. key_from_pem |
| 168 |
|
| * reads the latter and refuses the former, so this is where phase 2's |
| 169 |
|
| * key_from_x509_der earns its place: try the key form first, and on |
| 170 |
|
| * failure strip the PEM armour and hand the DER to the certificate entry. |
| 171 |
|
| * |
| 172 |
|
| * Both forms are accepted because a deployer who does have a bare public |
| 173 |
|
| * key should not be told to wrap it in a certificate to satisfy us. |
| 174 |
|
| * Returns NULL for anything that is neither. */ |
| 175 |
152 |
| PERL_STATIC_INLINE void *psaml_key_from_config(pTHX_ SV *sv) { |
| 176 |
152 |
| const jws_abi *J = psaml_jws(aTHX); |
| 177 |
|
| STRLEN n, dl; |
| 178 |
152 |
| const char *p = SvPVbyte(sv, n); |
| 179 |
|
| const char *b, *e; |
| 180 |
|
| SV *der; |
| 181 |
|
| void *k; |
| 182 |
|
| |
| 183 |
152 |
| k = J->key_from_pem(aTHX_ p, n); |
| 184 |
152 |
▶ | if (k) return k; |
Branch: 1 branch
| Branch | True | False | Total |
| if (k) return k; |
142 |
10 |
152 |
|
| 185 |
|
| |
| 186 |
|
| { |
| 187 |
|
| static const char BEG[] = "-----BEGIN CERTIFICATE-----"; |
| 188 |
|
| static const char END[] = "-----END CERTIFICATE-----"; |
| 189 |
10 |
| b = ninstr((char *)p, (char *)p + n, |
| 190 |
|
| (char *)BEG, (char *)BEG + sizeof BEG - 1); |
| 191 |
10 |
▶ | if (!b) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!b) return NULL; |
0 |
10 |
10 |
|
| 192 |
10 |
| b += sizeof BEG - 1; |
| 193 |
10 |
| e = ninstr((char *)b, (char *)p + n, |
| 194 |
|
| (char *)END, (char *)END + sizeof END - 1); |
| 195 |
10 |
▶ | if (!e) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) return NULL; |
0 |
10 |
10 |
|
| 196 |
|
| } |
| 197 |
|
| |
| 198 |
10 |
| der = sv_2mortal(newSV(psaml_b64_decoded_max((STRLEN)(e - b)) + 1)); |
| 199 |
10 |
| SvPOK_on(der); |
| 200 |
10 |
| dl = psaml_b64_decode_xml((unsigned char *)SvPVX(der), b, (STRLEN)(e - b)); |
| 201 |
10 |
▶ | if (dl == (STRLEN)-1) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (dl == (STRLEN)-1) return NULL; |
0 |
10 |
10 |
|
| 202 |
10 |
| SvCUR_set(der, dl); |
| 203 |
10 |
| return J->key_from_x509_der(aTHX_ |
| 204 |
10 |
| (const unsigned char *)SvPVX(der), dl); |
| 205 |
|
| } |
| 206 |
|
| |
| 207 |
|
| /* Verify the direct-child signature of `parent`. |
| 208 |
|
| * |
| 209 |
|
| * Returns 1 when a signature is present and verifies, 0 when there is no |
| 210 |
|
| * direct-child signature at all, and THROWS on a signature that is |
| 211 |
|
| * present and wrong. The three outcomes are distinct because |
| 212 |
|
| * `require_signed` needs to tell "unsigned" from "badly signed": the |
| 213 |
|
| * first may be allowed by policy, the second never is. */ |
| 214 |
497 |
| PERL_STATIC_INLINE int psaml_verify_element(pTHX_ psaml_verify_ctx *ctx, |
| 215 |
|
| const frx_node *parent) { |
| 216 |
497 |
| const frx_abi *F = psaml_frx(aTHX); |
| 217 |
497 |
| const jws_abi *J = psaml_jws(aTHX); |
| 218 |
|
| const frx_node *sig, *si, *cm, *sm, *ref, *trs, *t1, *t2, *dm, *dv, *sv_node; |
| 219 |
|
| const char *v; |
| 220 |
|
| STRLEN vl; |
| 221 |
|
| int i; |
| 222 |
|
| |
| 223 |
497 |
| sig = psaml_sig_of(aTHX_ parent); |
| 224 |
497 |
▶ | if (!sig) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sig) return 0; |
309 |
188 |
497 |
|
| 225 |
|
| |
| 226 |
188 |
| si = psaml_only_child(aTHX_ sig, PSAML_NS_DSIG, "SignedInfo"); |
| 227 |
188 |
▶ | if (!si) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!si) |
0 |
188 |
188 |
|
| 228 |
0 |
| psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
| 229 |
|
| "the Signature has no single SignedInfo"); |
| 230 |
|
| |
| 231 |
|
| /* 2: CanonicalizationMethod must be exclusive c14n */ |
| 232 |
188 |
| cm = psaml_only_child(aTHX_ si, PSAML_NS_DSIG, "CanonicalizationMethod"); |
| 233 |
189 |
▶ | if (!cm || !(psaml_attr_is(aTHX_ cm, "Algorithm", PSAML_C14N_EXC) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cm || !(psaml_attr_is(aTHX_ cm, "Algorithm", PSAML_C14N_EXC) |
188 |
0 |
188 |
|
| 234 |
1 |
| || psaml_attr_is(aTHX_ cm, "Algorithm", PSAML_C14N_EXC_WC))) |
| 235 |
1 |
| psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
| 236 |
|
| "CanonicalizationMethod is not exclusive c14n"); |
| 237 |
|
| |
| 238 |
|
| /* 3: SignatureMethod from the table */ |
| 239 |
187 |
| sm = psaml_only_child(aTHX_ si, PSAML_NS_DSIG, "SignatureMethod"); |
| 240 |
187 |
▶ | if (!sm) psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sm) psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
0 |
187 |
187 |
|
| 241 |
|
| "the SignedInfo has no single SignatureMethod"); |
| 242 |
187 |
| v = psaml_attr(aTHX_ sm, "Algorithm", &vl); |
| 243 |
187 |
▶ | if (!v) psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
Branch: 1 branch
| Branch | True | False | Total |
| if (!v) psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
0 |
187 |
187 |
|
| 244 |
|
| "SignatureMethod has no Algorithm"); |
| 245 |
|
| { |
| 246 |
187 |
| const char *jws_alg = NULL; |
| 247 |
229 |
▶ | for (i = 0; PSAML_SIGALGS[i].uri; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; PSAML_SIGALGS[i].uri; i++) { |
222 |
7 |
229 |
|
| 248 |
222 |
| STRLEN ul = strlen(PSAML_SIGALGS[i].uri); |
| 249 |
222 |
▶ | if (vl == ul && memEQ(v, PSAML_SIGALGS[i].uri, ul)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (vl == ul && memEQ(v, PSAML_SIGALGS[i].uri, ul)) { |
180 |
42 |
222 |
| if (vl == ul && memEQ(v, PSAML_SIGALGS[i].uri, ul)) { |
180 |
0 |
180 |
|
| 250 |
180 |
| jws_alg = PSAML_SIGALGS[i].alg; |
| 251 |
180 |
| break; |
| 252 |
|
| } |
| 253 |
|
| } |
| 254 |
187 |
▶ | if (!jws_alg) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!jws_alg) |
7 |
180 |
187 |
|
| 255 |
7 |
| psaml_throw(aTHX_ PSAML_E_ALG_REFUSED, |
| 256 |
|
| "SignatureMethod '%.*s' is not accepted", (int)vl, v); |
| 257 |
|
| |
| 258 |
|
| /* 4: exactly one Reference */ |
| 259 |
180 |
| ref = psaml_only_child(aTHX_ si, PSAML_NS_DSIG, "Reference"); |
| 260 |
180 |
▶ | if (!ref) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ref) |
2 |
178 |
180 |
|
| 261 |
2 |
| psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
| 262 |
|
| "the SignedInfo does not have exactly one Reference"); |
| 263 |
|
| |
| 264 |
|
| /* 4 and 5: the URI is '#' plus the PARENT's ID, and the node that id |
| 265 |
|
| * resolves to is the SAME POINTER as the parent. |
| 266 |
|
| * |
| 267 |
|
| * The pointer comparison is the whole defence. Comparing the id |
| 268 |
|
| * strings would pass for a document containing a second element that |
| 269 |
|
| * merely carries the same-looking id, and comparing element names |
| 270 |
|
| * would pass for any assertion at all. by_id was built at parse over |
| 271 |
|
| * `ID` alone and a duplicate value was refused there, so the node it |
| 272 |
|
| * returns is the only one, and requiring it to BE the signature's |
| 273 |
|
| * parent closes the gap between what was signed and what will be |
| 274 |
|
| * read. */ |
| 275 |
178 |
| v = psaml_attr(aTHX_ ref, "URI", &vl); |
| 276 |
178 |
▶ | if (!v || vl < 2 || v[0] != '#') |
Branch: 3 branches
| Branch | True | False | Total |
| if (!v || vl < 2 || v[0] != '#') |
178 |
0 |
178 |
| if (!v || vl < 2 || v[0] != '#') |
177 |
1 |
178 |
| if (!v || vl < 2 || v[0] != '#') |
0 |
177 |
177 |
|
| 277 |
1 |
| psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
| 278 |
|
| "the Reference URI is not a same-document '#id'"); |
| 279 |
|
| { |
| 280 |
177 |
| const frx_node *target = F->by_id(ctx->doc, "ID", v + 1, vl - 1); |
| 281 |
177 |
▶ | if (!target || target != parent) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!target || target != parent) |
177 |
0 |
177 |
| if (!target || target != parent) |
8 |
169 |
177 |
|
| 282 |
8 |
| psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
| 283 |
|
| "the Reference names an element other than the one the " |
| 284 |
|
| "Signature is a child of"); |
| 285 |
|
| } |
| 286 |
|
| |
| 287 |
|
| /* 5: Transforms exactly enveloped-signature then exclusive c14n */ |
| 288 |
169 |
| trs = psaml_only_child(aTHX_ ref, PSAML_NS_DSIG, "Transforms"); |
| 289 |
169 |
▶ | if (!trs) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!trs) |
0 |
169 |
169 |
|
| 290 |
0 |
| psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, "the Reference has no Transforms"); |
| 291 |
169 |
| t1 = psaml_child(aTHX_ trs, PSAML_NS_DSIG, "Transform", NULL); |
| 292 |
169 |
▶ | t2 = t1 ? psaml_child(aTHX_ trs, PSAML_NS_DSIG, "Transform", t1) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| t2 = t1 ? psaml_child(aTHX_ trs, PSAML_NS_DSIG, "Transform", t1) : NULL; |
169 |
0 |
169 |
|
| 293 |
169 |
▶ | if (!t1 || !t2 || psaml_child(aTHX_ trs, PSAML_NS_DSIG, "Transform", t2)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!t1 || !t2 || psaml_child(aTHX_ trs, PSAML_NS_DSIG, "Transform", t2)) |
169 |
0 |
169 |
| if (!t1 || !t2 || psaml_child(aTHX_ trs, PSAML_NS_DSIG, "Transform", t2)) |
169 |
0 |
169 |
| if (!t1 || !t2 || psaml_child(aTHX_ trs, PSAML_NS_DSIG, "Transform", t2)) |
0 |
169 |
169 |
|
| 294 |
0 |
| psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
| 295 |
|
| "Transforms must be exactly enveloped-signature then " |
| 296 |
|
| "exclusive c14n"); |
| 297 |
169 |
▶ | if (!psaml_attr_is(aTHX_ t1, "Algorithm", PSAML_TR_ENVELOPED)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!psaml_attr_is(aTHX_ t1, "Algorithm", PSAML_TR_ENVELOPED)) |
1 |
168 |
169 |
|
| 298 |
1 |
| psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
| 299 |
|
| "the first Transform is not enveloped-signature"); |
| 300 |
170 |
| if (!(psaml_attr_is(aTHX_ t2, "Algorithm", PSAML_C14N_EXC) |
| 301 |
2 |
| || psaml_attr_is(aTHX_ t2, "Algorithm", PSAML_C14N_EXC_WC))) |
| 302 |
2 |
| psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
| 303 |
|
| "the second Transform is not exclusive c14n. XPath, XSLT " |
| 304 |
|
| "and base64 transforms are refused: those are what let a " |
| 305 |
|
| "signature cover something other than what it appears to"); |
| 306 |
|
| |
| 307 |
|
| /* 6: DigestMethod */ |
| 308 |
166 |
| dm = psaml_only_child(aTHX_ ref, PSAML_NS_DSIG, "DigestMethod"); |
| 309 |
166 |
▶ | if (!dm) psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
Branch: 1 branch
| Branch | True | False | Total |
| if (!dm) psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
0 |
166 |
166 |
|
| 310 |
|
| "the Reference has no single DigestMethod"); |
| 311 |
166 |
| v = psaml_attr(aTHX_ dm, "Algorithm", &vl); |
| 312 |
|
| { |
| 313 |
166 |
▶ | int bits = v ? psaml_digest_bits(v, vl, ctx->allow_sha1) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| int bits = v ? psaml_digest_bits(v, vl, ctx->allow_sha1) : 0; |
166 |
0 |
166 |
|
| 314 |
|
| SV *canon, *digest, *want; |
| 315 |
|
| const frx_node *without[1]; |
| 316 |
|
| frx_c14n c; |
| 317 |
|
| |
| 318 |
166 |
▶ | if (!bits) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bits) |
1 |
165 |
166 |
|
| 319 |
1 |
▶ | psaml_throw(aTHX_ PSAML_E_ALG_REFUSED, |
Branch: 2 branches
| Branch | True | False | Total |
| psaml_throw(aTHX_ PSAML_E_ALG_REFUSED, |
1 |
0 |
1 |
| psaml_throw(aTHX_ PSAML_E_ALG_REFUSED, |
1 |
0 |
1 |
|
| 320 |
|
| "DigestMethod '%.*s' is not accepted", |
| 321 |
|
| (int)(v ? vl : 0), v ? v : ""); |
| 322 |
|
| |
| 323 |
|
| /* 7: the digest, over c14n of the PARENT with the Signature |
| 324 |
|
| * excluded - which is what `enveloped-signature` means - and the |
| 325 |
|
| * TRANSFORM's PrefixList. */ |
| 326 |
|
| { |
| 327 |
|
| char pbuf[512]; |
| 328 |
|
| const char *prefixes[PSAML_MAX_PREFIXES]; |
| 329 |
165 |
| int np = psaml_prefix_list(aTHX_ t2, pbuf, sizeof pbuf, |
| 330 |
|
| prefixes, PSAML_MAX_PREFIXES); |
| 331 |
165 |
| Zero(&c, 1, frx_c14n); |
| 332 |
165 |
| c.mode = FRX_C14N_EXC; |
| 333 |
165 |
| c.comments = 0; |
| 334 |
165 |
▶ | c.prefix_list = np ? prefixes : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| c.prefix_list = np ? prefixes : NULL; |
0 |
165 |
165 |
|
| 335 |
165 |
| c.n_prefix = np; |
| 336 |
165 |
| without[0] = sig; |
| 337 |
165 |
| c.without = without; |
| 338 |
165 |
| c.n_without = 1; |
| 339 |
165 |
| canon = F->c14n(aTHX_ parent, &c); |
| 340 |
|
| } |
| 341 |
165 |
▶ | if (!canon) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!canon) |
0 |
165 |
165 |
|
| 342 |
0 |
| psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
| 343 |
|
| "the signed element would not canonicalise"); |
| 344 |
165 |
| sv_2mortal(canon); |
| 345 |
|
| { |
| 346 |
|
| STRLEN cl; |
| 347 |
|
| const unsigned char *cp = |
| 348 |
165 |
| (const unsigned char *)SvPVbyte(canon, cl); |
| 349 |
1 |
| digest = bits == 160 ? J->sha1(aTHX_ cp, cl) |
| 350 |
329 |
▶ | : bits == 256 ? J->sha256(aTHX_ cp, cl) |
Branch: 1 branch
| Branch | True | False | Total |
| : bits == 256 ? J->sha256(aTHX_ cp, cl) |
1 |
164 |
165 |
|
| 351 |
164 |
▶ | : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| : NULL; |
164 |
0 |
164 |
|
| 352 |
165 |
▶ | if (!digest) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!digest) |
0 |
165 |
165 |
|
| 353 |
0 |
| psaml_throw(aTHX_ PSAML_E_ALG_REFUSED, |
| 354 |
|
| "DigestMethod SHA-%d is recognised but this build " |
| 355 |
|
| "cannot compute it: jws_abi carries sha256 and sha1 " |
| 356 |
|
| "and no other one-shot digest", bits); |
| 357 |
165 |
| sv_2mortal(digest); |
| 358 |
|
| } |
| 359 |
|
| |
| 360 |
165 |
| dv = psaml_only_child(aTHX_ ref, PSAML_NS_DSIG, "DigestValue"); |
| 361 |
165 |
▶ | if (!dv) psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
Branch: 1 branch
| Branch | True | False | Total |
| if (!dv) psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
0 |
165 |
165 |
|
| 362 |
|
| "the Reference has no single DigestValue"); |
| 363 |
|
| { |
| 364 |
165 |
| SV *raw = sv_2mortal(F->text(aTHX_ dv)); |
| 365 |
|
| STRLEN rl; |
| 366 |
165 |
| const char *rp = SvPVbyte(raw, rl); |
| 367 |
165 |
| SV *dec = sv_2mortal(newSV(psaml_b64_decoded_max(rl) + 1)); |
| 368 |
|
| STRLEN dl; |
| 369 |
165 |
| SvPOK_on(dec); |
| 370 |
165 |
| dl = psaml_b64_decode_xml((unsigned char *)SvPVX(dec), rp, rl); |
| 371 |
165 |
▶ | if (dl == (STRLEN)-1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (dl == (STRLEN)-1) |
0 |
165 |
165 |
|
| 372 |
0 |
| psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
| 373 |
|
| "the DigestValue is not base64"); |
| 374 |
165 |
| SvCUR_set(dec, dl); |
| 375 |
165 |
| want = dec; |
| 376 |
|
| } |
| 377 |
|
| { |
| 378 |
|
| STRLEN al, bl; |
| 379 |
|
| const unsigned char *ap = |
| 380 |
165 |
▶ | (const unsigned char *)SvPVbyte(digest, al); |
Branch: 1 branch
| Branch | True | False | Total |
| (const unsigned char *)SvPVbyte(digest, al); |
165 |
0 |
165 |
|
| 381 |
|
| const unsigned char *bp = |
| 382 |
165 |
| (const unsigned char *)SvPVbyte(want, bl); |
| 383 |
165 |
▶ | if (!J->ct_eq(aTHX_ ap, al, bp, bl)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!J->ct_eq(aTHX_ ap, al, bp, bl)) |
14 |
151 |
165 |
|
| 384 |
14 |
| psaml_throw(aTHX_ PSAML_E_BAD_DIGEST, |
| 385 |
|
| "the reference digest does not match the element"); |
| 386 |
|
| } |
| 387 |
|
| } |
| 388 |
|
| |
| 389 |
|
| /* 8: the signature, over c14n of SignedInfo with the |
| 390 |
|
| * CanonicalizationMethod's OWN PrefixList. |
| 391 |
|
| * |
| 392 |
|
| * A DIFFERENT InclusiveNamespaces from the transform's above. |
| 393 |
|
| * Confusing the two is a signature that never verifies, with nothing |
| 394 |
|
| * in any log to say why, and it is the single easiest mistake to |
| 395 |
|
| * make in this file: both elements are called InclusiveNamespaces, |
| 396 |
|
| * both carry a PrefixList, and they sit four lines apart in the |
| 397 |
|
| * document. */ |
| 398 |
|
| { |
| 399 |
|
| char pbuf[512]; |
| 400 |
|
| const char *prefixes[PSAML_MAX_PREFIXES]; |
| 401 |
151 |
| int np = psaml_prefix_list(aTHX_ cm, pbuf, sizeof pbuf, |
| 402 |
|
| prefixes, PSAML_MAX_PREFIXES); |
| 403 |
|
| frx_c14n c; |
| 404 |
|
| SV *canon; |
| 405 |
|
| STRLEN cl, sl; |
| 406 |
|
| const unsigned char *cp; |
| 407 |
|
| const char *sp; |
| 408 |
|
| SV *sigval, *sigraw; |
| 409 |
|
| SSize_t k, nkeys; |
| 410 |
151 |
| int ok = 0; |
| 411 |
|
| |
| 412 |
151 |
| Zero(&c, 1, frx_c14n); |
| 413 |
151 |
| c.mode = FRX_C14N_EXC; |
| 414 |
151 |
| c.comments = psaml_attr_is(aTHX_ cm, "Algorithm", PSAML_C14N_EXC_WC); |
| 415 |
151 |
▶ | c.prefix_list = np ? prefixes : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| c.prefix_list = np ? prefixes : NULL; |
0 |
151 |
151 |
|
| 416 |
151 |
| c.n_prefix = np; |
| 417 |
151 |
| canon = F->c14n(aTHX_ si, &c); |
| 418 |
151 |
▶ | if (!canon) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!canon) |
0 |
151 |
151 |
|
| 419 |
0 |
| psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
| 420 |
|
| "the SignedInfo would not canonicalise"); |
| 421 |
151 |
| sv_2mortal(canon); |
| 422 |
151 |
| cp = (const unsigned char *)SvPVbyte(canon, cl); |
| 423 |
|
| |
| 424 |
151 |
| sv_node = psaml_only_child(aTHX_ sig, PSAML_NS_DSIG, "SignatureValue"); |
| 425 |
151 |
▶ | if (!sv_node) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_node) |
0 |
151 |
151 |
|
| 426 |
0 |
| psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
| 427 |
|
| "the Signature has no single SignatureValue"); |
| 428 |
151 |
| sigval = sv_2mortal(F->text(aTHX_ sv_node)); |
| 429 |
151 |
| sp = SvPVbyte(sigval, sl); |
| 430 |
151 |
| sigraw = sv_2mortal(newSV(psaml_b64_decoded_max(sl) + 1)); |
| 431 |
151 |
| SvPOK_on(sigraw); |
| 432 |
|
| { |
| 433 |
151 |
| STRLEN dl = psaml_b64_decode_xml((unsigned char *)SvPVX(sigraw), |
| 434 |
|
| sp, sl); |
| 435 |
151 |
▶ | if (dl == (STRLEN)-1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (dl == (STRLEN)-1) |
0 |
151 |
151 |
|
| 436 |
0 |
| psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
| 437 |
|
| "the SignatureValue is not base64"); |
| 438 |
151 |
| SvCUR_set(sigraw, dl); |
| 439 |
|
| } |
| 440 |
|
| |
| 441 |
|
| /* 9: KeyInfo is NOT trusted. It is whatever the sender put there. |
| 442 |
|
| * Every key tried is one the deployer configured; any one passing |
| 443 |
|
| * is enough, and all failing is bad_signature. */ |
| 444 |
151 |
▶ | nkeys = ctx->keys ? (av_len(ctx->keys) + 1) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| nkeys = ctx->keys ? (av_len(ctx->keys) + 1) : 0; |
151 |
0 |
151 |
|
| 445 |
151 |
▶ | if (!nkeys) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nkeys) |
1 |
150 |
151 |
|
| 446 |
1 |
| psaml_throw(aTHX_ PSAML_E_NO_KEY, |
| 447 |
|
| "no signing key is configured for this provider"); |
| 448 |
302 |
▶ | for (k = 0; k < nkeys && !ok; k++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (k = 0; k < nkeys && !ok; k++) { |
152 |
150 |
302 |
| for (k = 0; k < nkeys && !ok; k++) { |
152 |
0 |
152 |
|
| 449 |
152 |
| SV **e = av_fetch(ctx->keys, k, 0); |
| 450 |
|
| STRLEN pl, gl; |
| 451 |
|
| const char *pp; |
| 452 |
|
| const unsigned char *gp; |
| 453 |
|
| void *key; |
| 454 |
152 |
▶ | if (!e || !*e || !SvOK(*e)) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!e || !*e || !SvOK(*e)) continue; |
152 |
0 |
152 |
| if (!e || !*e || !SvOK(*e)) continue; |
152 |
0 |
152 |
| if (!e || !*e || !SvOK(*e)) continue; |
0 |
152 |
152 |
|
| 455 |
152 |
| key = psaml_key_from_config(aTHX_ *e); |
| 456 |
152 |
▶ | if (!key) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!key) continue; |
0 |
152 |
152 |
|
| 457 |
|
| PERL_UNUSED_VAR(pp); |
| 458 |
|
| PERL_UNUSED_VAR(pl); |
| 459 |
152 |
| gp = (const unsigned char *)SvPVbyte(sigraw, gl); |
| 460 |
152 |
| ok = J->verify(aTHX_ key, jws_alg, strlen(jws_alg), cp, cl, gp, gl); |
| 461 |
152 |
| J->key_free(aTHX_ key); |
| 462 |
|
| } |
| 463 |
150 |
▶ | if (!ok) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) |
10 |
140 |
150 |
|
| 464 |
10 |
| psaml_throw(aTHX_ PSAML_E_BAD_SIGNATURE, |
| 465 |
|
| "no configured key verifies the signature"); |
| 466 |
|
| } |
| 467 |
|
| } |
| 468 |
140 |
| return 1; |
| 469 |
|
| } |
| 470 |
|
| |
| 471 |
|
| #endif /* PSAML_SIGNATURE_H */ |