|
1 | 1 | package com.onelogin.saml; |
2 | 2 |
|
3 | | -import com.onelogin.AccountSettings; |
4 | 3 | import java.io.ByteArrayInputStream; |
5 | 4 | import java.io.IOException; |
6 | 5 | import java.lang.reflect.Method; |
7 | 6 | import java.security.cert.CertificateException; |
8 | 7 | import java.security.cert.X509Certificate; |
9 | 8 | import java.util.ArrayList; |
| 9 | +import java.util.Calendar; |
10 | 10 | import java.util.HashMap; |
| 11 | +import java.util.TimeZone; |
| 12 | + |
11 | 13 | import javax.xml.XMLConstants; |
12 | 14 | import javax.xml.crypto.dsig.XMLSignature; |
13 | 15 | import javax.xml.crypto.dsig.XMLSignatureFactory; |
14 | 16 | import javax.xml.crypto.dsig.dom.DOMValidateContext; |
15 | 17 | import javax.xml.parsers.DocumentBuilder; |
16 | 18 | import javax.xml.parsers.DocumentBuilderFactory; |
17 | 19 | import javax.xml.parsers.ParserConfigurationException; |
| 20 | + |
18 | 21 | import org.apache.commons.codec.binary.Base64; |
19 | 22 | import org.w3c.dom.Document; |
20 | 23 | import org.w3c.dom.Element; |
|
23 | 26 | import org.w3c.dom.NodeList; |
24 | 27 | import org.xml.sax.SAXException; |
25 | 28 |
|
| 29 | +import com.onelogin.AccountSettings; |
| 30 | + |
26 | 31 | public class Response { |
27 | 32 |
|
28 | 33 | private Document xmlDoc; |
29 | | - private Integer Assertions; |
| 34 | + private NodeList assertions; |
30 | 35 | private Element rootElement; |
31 | 36 | private final AccountSettings accountSettings; |
32 | 37 | private final Certificate certificate; |
| 38 | + private String currentUrl; |
33 | 39 |
|
34 | 40 | public Response(AccountSettings accountSettings) throws CertificateException { |
35 | | - this.accountSettings = accountSettings; |
| 41 | + this.accountSettings = accountSettings; |
36 | 42 | certificate = new Certificate(); |
37 | 43 | certificate.loadCertificate(this.accountSettings.getCertificate()); |
38 | 44 | } |
39 | 45 |
|
40 | | - public void loadXml(String xml) throws ParserConfigurationException, SAXException, IOException { |
41 | | - DocumentBuilderFactory fty = DocumentBuilderFactory.newInstance(); |
| 46 | + public void loadXml(String xml) throws ParserConfigurationException, SAXException, IOException { |
| 47 | + DocumentBuilderFactory fty = DocumentBuilderFactory.newInstance(); |
42 | 48 | fty.setNamespaceAware(true); |
43 | 49 | // XMLConstants with FEATURE_SECURE_PROCESSING prevents external document access. (XXE/XEE Possible Attacks). |
44 | 50 | fty.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); |
45 | | - DocumentBuilder builder = fty.newDocumentBuilder(); |
46 | | - ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes()); |
47 | | - xmlDoc = builder.parse(bais); |
| 51 | + DocumentBuilder builder = fty.newDocumentBuilder(); |
| 52 | + ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes()); |
| 53 | + xmlDoc = builder.parse(bais); |
48 | 54 | } |
49 | 55 |
|
50 | | - public void loadXmlFromBase64(String response) throws ParserConfigurationException, SAXException, IOException { |
| 56 | + public void loadXmlFromBase64(String response) throws ParserConfigurationException, SAXException, IOException { |
51 | 57 | Base64 base64 = new Base64(); |
52 | 58 | byte[] decodedB = base64.decode(response); |
53 | 59 | String decodedS = new String(decodedB); |
54 | 60 | loadXml(decodedS); |
55 | 61 | } |
56 | 62 |
|
57 | 63 | // isValid() function should be called to make basic security checks to responses. |
58 | | - public boolean isValid() throws Exception { |
| 64 | + public boolean isValid() throws Exception { |
59 | 65 | // Security Checks |
60 | | - rootElement = xmlDoc.getDocumentElement(); |
61 | | - Assertions = xmlDoc.getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:assertion", "Assertion").getLength(); |
| 66 | + rootElement = xmlDoc.getDocumentElement(); |
| 67 | + assertions = xmlDoc.getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:assertion", "Assertion"); |
62 | 68 | xmlDoc.getDocumentElement().normalize(); |
63 | 69 |
|
64 | | - if (Assertions > 1) { |
65 | | - throw new Exception("SAML Response must contain 1 Assertion."); |
| 70 | + // Check SAML version |
| 71 | + String attName = rootElement.getAttribute("Version"); |
| 72 | + if (!attName.equals("2.0")) { |
| 73 | + throw new Exception("Unsupported SAML Version."); |
66 | 74 | } |
67 | | - |
68 | | - String attName = rootElement.getAttribute("ID"); |
| 75 | + |
| 76 | + // Check ID in the response |
| 77 | + attName = rootElement.getAttribute("ID"); |
69 | 78 | if (attName.equals("")) { |
70 | 79 | throw new Exception("Missing ID attribute on SAML Response."); |
71 | 80 | } |
72 | | - |
73 | | - attName = rootElement.getAttribute("Version"); |
74 | | - if (!attName.equals("2.0")) { |
75 | | - throw new Exception("Unsupported SAML Version."); |
| 81 | + |
| 82 | + if (assertions == null || assertions.getLength() != 1) { |
| 83 | + throw new Exception("SAML Response must contain 1 Assertion."); |
76 | 84 | } |
77 | 85 |
|
78 | | - NodeList nodes = xmlDoc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature"); |
79 | | - |
| 86 | + NodeList nodes = xmlDoc.getElementsByTagNameNS("*", "Signature"); |
80 | 87 | if (nodes == null || nodes.getLength() == 0) { |
81 | 88 | throw new Exception("Can't find signature in Document."); |
82 | 89 | } |
83 | 90 |
|
84 | | - if (setIdAttributeExists()) { |
85 | | - tagIdAttributes(xmlDoc); |
| 91 | + // Check destination |
| 92 | + String destinationUrl = rootElement.getAttribute("Destination"); |
| 93 | + if (destinationUrl != null) { |
| 94 | + if(!destinationUrl.equals(currentUrl)){ |
| 95 | + throw new Exception("The response was received at " + currentUrl + " instead of " + destinationUrl); |
| 96 | + } |
86 | 97 | } |
87 | | - |
88 | | - X509Certificate cert = certificate.getX509Cert(); |
89 | | - DOMValidateContext ctx = new DOMValidateContext(cert.getPublicKey(), nodes.item(0)); |
90 | | - XMLSignatureFactory sigF = XMLSignatureFactory.getInstance("DOM"); |
91 | | - XMLSignature xmlSignature = sigF.unmarshalXMLSignature(ctx); |
| 98 | + |
| 99 | + // Check Audience |
| 100 | + NodeList nodeAudience = xmlDoc.getElementsByTagNameNS("*", "Audience"); |
| 101 | + String audienceUrl = nodeAudience.item(0).getChildNodes().item(0).getNodeValue(); |
| 102 | + if (audienceUrl != null) { |
| 103 | + if(!audienceUrl.equals(currentUrl)){ |
| 104 | + throw new Exception(audienceUrl + " is not a valid audience for this Response"); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Check SubjectConfirmation, at least one SubjectConfirmation must be valid |
| 109 | + NodeList nodeSubConf = xmlDoc.getElementsByTagNameNS("*", "SubjectConfirmation"); |
| 110 | + boolean validSubjectConfirmation = true; |
| 111 | + for(int i = 0; i < nodeSubConf.getLength(); i++){ |
| 112 | + Node method = nodeSubConf.item(i).getAttributes().getNamedItem("Method"); |
| 113 | + if(method != null && !method.getNodeValue().equals("urn:oasis:names:tc:SAML:2.0:cm:bearer")){ |
| 114 | + continue; |
| 115 | + } |
| 116 | + NodeList childs = nodeSubConf.item(i).getChildNodes(); |
| 117 | + for(int c = 0; c < childs.getLength(); c++){ |
| 118 | + if(childs.item(c).getLocalName().equals("SubjectConfirmationData")){ |
| 119 | + Node inResponseTo = childs.item(c).getAttributes().getNamedItem("InResponseTo"); |
| 120 | +// if(inResponseTo != null && !inResponseTo.getNodeValue().equals("ID of the AuthNRequest")){ |
| 121 | +// validSubjectConfirmation = false; |
| 122 | +// } |
| 123 | + Node recipient = childs.item(c).getAttributes().getNamedItem("Recipient"); |
| 124 | + if(recipient != null && !recipient.getNodeValue().equals(currentUrl)){ |
| 125 | + validSubjectConfirmation = false; |
| 126 | + } |
| 127 | + Node notOnOrAfter = childs.item(c).getAttributes().getNamedItem("NotOnOrAfter"); |
| 128 | + if(notOnOrAfter != null){ |
| 129 | + final Calendar notOnOrAfterDate = javax.xml.bind.DatatypeConverter.parseDateTime(notOnOrAfter.getNodeValue()); |
| 130 | + Calendar now = Calendar.getInstance(TimeZone.getTimeZone("UTC")); |
| 131 | + if(notOnOrAfterDate.before(now)){ |
| 132 | + validSubjectConfirmation = false; |
| 133 | + } |
| 134 | + } |
| 135 | + Node notBefore = childs.item(c).getAttributes().getNamedItem("NotBefore"); |
| 136 | + if(notBefore != null){ |
| 137 | + final Calendar notBeforeDate = javax.xml.bind.DatatypeConverter.parseDateTime(notBefore.getNodeValue()); |
| 138 | + Calendar now = Calendar.getInstance(TimeZone.getTimeZone("UTC")); |
| 139 | + if(notBeforeDate.before(now)){ |
| 140 | + validSubjectConfirmation = false; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + if (!validSubjectConfirmation) { |
| 147 | + throw new Exception("A valid SubjectConfirmation was not found on this Response"); |
| 148 | + } |
| 149 | + |
| 150 | + |
| 151 | +// if (setIdAttributeExists()) { |
| 152 | +// tagIdAttributes(xmlDoc); |
| 153 | +// } |
| 154 | + |
| 155 | + X509Certificate cert = certificate.getX509Cert(); |
| 156 | + DOMValidateContext ctx = new DOMValidateContext(cert.getPublicKey(), nodes.item(0)); |
| 157 | + XMLSignatureFactory sigF = XMLSignatureFactory.getInstance("DOM"); |
| 158 | + XMLSignature xmlSignature = sigF.unmarshalXMLSignature(ctx); |
92 | 159 |
|
93 | 160 | return xmlSignature.validate(ctx); |
94 | 161 | } |
@@ -141,7 +208,10 @@ private boolean setIdAttributeExists() { |
141 | 208 | } |
142 | 209 |
|
143 | 210 | private void tagIdAttributes(Document xmlDoc) { |
144 | | - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
| 211 | + throw new UnsupportedOperationException("Not supported yet."); |
145 | 212 | } |
146 | 213 |
|
| 214 | + public void setDestinationUrl(String urld){ |
| 215 | + currentUrl = urld; |
| 216 | + } |
147 | 217 | } |
0 commit comments