Search in sources :

Example 6 with KeyInfo

use of javax.xml.crypto.dsig.keyinfo.KeyInfo in project keycloak by keycloak.

the class SamlDescriptorIDPKeysExtractor method parse.

public MultivaluedHashMap<String, KeyInfo> parse(InputStream stream) throws ParsingException {
    MultivaluedHashMap<String, KeyInfo> res = new MultivaluedHashMap<>();
    try {
        DocumentBuilder builder = DocumentUtil.getDocumentBuilder();
        Document doc = builder.parse(stream);
        XPathExpression expr = xpath.compile("//m:EntityDescriptor/m:IDPSSODescriptor/m:KeyDescriptor");
        NodeList keyDescriptors = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
        for (int i = 0; i < keyDescriptors.getLength(); i++) {
            Node keyDescriptor = keyDescriptors.item(i);
            Element keyDescriptorEl = (Element) keyDescriptor;
            KeyInfo ki = processKeyDescriptor(keyDescriptorEl);
            if (ki != null) {
                String use = keyDescriptorEl.getAttribute(JBossSAMLConstants.USE.get());
                res.add(use, ki);
            }
        }
    } catch (SAXException | IOException | ParserConfigurationException | MarshalException | XPathExpressionException e) {
        throw new ParsingException("Error parsing SAML descriptor", e);
    }
    return res;
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) MarshalException(javax.xml.crypto.MarshalException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) KeyInfo(javax.xml.crypto.dsig.keyinfo.KeyInfo) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParsingException(org.keycloak.saml.common.exceptions.ParsingException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 7 with KeyInfo

use of javax.xml.crypto.dsig.keyinfo.KeyInfo in project santuario-java by apache.

the class KeyInfoTest method testgetContent.

@org.junit.Test
@SuppressWarnings("unchecked")
public void testgetContent() {
    KeyInfo[] infos = new KeyInfo[2];
    infos[0] = fac.newKeyInfo(Collections.singletonList(fac.newKeyName("foo")), "skeleton");
    infos[1] = fac.newKeyInfo(Collections.singletonList(fac.newKeyName("foo")));
    for (int j = 0; j < infos.length; j++) {
        KeyInfo ki = infos[j];
        List<XMLStructure> li = ki.getContent();
        assertNotNull(ki.getContent());
        Object[] content = li.toArray();
        for (int i = 0; i < content.length; i++) {
            if (!(content[i] instanceof XMLStructure)) {
                fail("KeyInfo element has the wrong type");
            }
        }
    }
}
Also used : KeyInfo(javax.xml.crypto.dsig.keyinfo.KeyInfo) XMLStructure(javax.xml.crypto.XMLStructure)

Example 8 with KeyInfo

use of javax.xml.crypto.dsig.keyinfo.KeyInfo in project santuario-java by apache.

the class KeyInfoTest method testMarshal.

@org.junit.Test
public void testMarshal() throws Exception {
    KeyInfo ki = fac.newKeyInfo(Collections.singletonList(fac.newKeyName("foo")), "keyid");
    try {
        ki.marshal(null, null);
        fail("Should raise a NullPointerException");
    } catch (NullPointerException npe) {
    }
    Document doc = XMLUtils.createDocumentBuilder(false).newDocument();
    Element elem = doc.createElementNS("http://acme.org", "parent");
    doc.appendChild(elem);
    DOMStructure parent = new DOMStructure(elem);
    try {
        ki.marshal(parent, null);
    } catch (Exception e) {
        fail("Should not throw an exception: " + e);
    }
    Element kiElem = DOMUtils.getFirstChildElement(elem);
    if (!kiElem.getLocalName().equals("KeyInfo")) {
        fail("Should be KeyInfo element: " + kiElem.getLocalName());
    }
    Element knElem = DOMUtils.getFirstChildElement(kiElem);
    if (!knElem.getLocalName().equals("KeyName")) {
        fail("Should be KeyName element: " + knElem.getLocalName());
    }
    // check if key info is inserted before nextSibling
    doc = XMLUtils.createDocumentBuilder(false).newDocument();
    elem = doc.createElementNS("http://acme.org", "parent");
    doc.appendChild(elem);
    Element nextSib = doc.createElementNS("http://acme.org", "nextSib");
    elem.appendChild(nextSib);
    Key key = new Key() {

        private static final long serialVersionUID = 1L;

        @Override
        public String getAlgorithm() {
            return null;
        }

        @Override
        public byte[] getEncoded() {
            return null;
        }

        @Override
        public String getFormat() {
            return null;
        }
    };
    DOMSignContext ctx = new DOMSignContext(key, elem, nextSib);
    parent = new DOMStructure(elem);
    ki.marshal(parent, ctx);
    // no need for catching/calling fail() explicitly ... if it fails, it fails ...
    assertEquals(elem.getFirstChild().getLocalName(), "KeyInfo");
}
Also used : KeyInfo(javax.xml.crypto.dsig.keyinfo.KeyInfo) DOMSignContext(javax.xml.crypto.dsig.dom.DOMSignContext) Element(org.w3c.dom.Element) DOMStructure(javax.xml.crypto.dom.DOMStructure) Document(org.w3c.dom.Document) Key(java.security.Key)

Example 9 with KeyInfo

use of javax.xml.crypto.dsig.keyinfo.KeyInfo in project santuario-java by apache.

the class KeyInfoTest method testisFeatureSupported.

@org.junit.Test
public void testisFeatureSupported() {
    KeyInfo ki = fac.newKeyInfo(Collections.singletonList(fac.newKeyName("foo")), "keyid");
    try {
        ki.isFeatureSupported(null);
        fail("Should raise a NPE for null feature");
    } catch (NullPointerException npe) {
    }
    assertTrue(!ki.isFeatureSupported("not supported"));
}
Also used : KeyInfo(javax.xml.crypto.dsig.keyinfo.KeyInfo)

Example 10 with KeyInfo

use of javax.xml.crypto.dsig.keyinfo.KeyInfo in project santuario-java by apache.

the class KeyInfoTest method testConstructor.

@org.junit.Test
public void testConstructor() {
    final String id = "keyId";
    // test newKeyInfo(List, String id)
    KeyInfo ki = fac.newKeyInfo(Collections.singletonList(fac.newKeyName("foo")), id);
    assertEquals(id, ki.getId());
    try {
        ki = fac.newKeyInfo(null, id);
        fail("Should raise a NullPointerException");
    } catch (NullPointerException npe) {
    }
    // test newKeyInfo(List)
    ki = fac.newKeyInfo(Collections.singletonList(fac.newKeyName("foo")));
}
Also used : KeyInfo(javax.xml.crypto.dsig.keyinfo.KeyInfo)

Aggregations

KeyInfo (javax.xml.crypto.dsig.keyinfo.KeyInfo)24 DOMSignContext (javax.xml.crypto.dsig.dom.DOMSignContext)10 SignedInfo (javax.xml.crypto.dsig.SignedInfo)9 KeyInfoFactory (javax.xml.crypto.dsig.keyinfo.KeyInfoFactory)9 Reference (javax.xml.crypto.dsig.Reference)8 XMLSignature (javax.xml.crypto.dsig.XMLSignature)8 X509Data (javax.xml.crypto.dsig.keyinfo.X509Data)8 Node (org.w3c.dom.Node)8 Transform (javax.xml.crypto.dsig.Transform)7 XMLSignatureFactory (javax.xml.crypto.dsig.XMLSignatureFactory)7 Document (org.w3c.dom.Document)7 Element (org.w3c.dom.Element)7 ArrayList (java.util.ArrayList)6 CanonicalizationMethod (javax.xml.crypto.dsig.CanonicalizationMethod)6 X509Certificate (java.security.cert.X509Certificate)4 URIReference (javax.xml.crypto.URIReference)4 DOMStructure (javax.xml.crypto.dom.DOMStructure)4 NodeList (org.w3c.dom.NodeList)4 XMLStructure (javax.xml.crypto.XMLStructure)3 KeyValue (javax.xml.crypto.dsig.keyinfo.KeyValue)3