Search in sources :

Example 1 with RSAKeyValue

use of org.apache.xml.security.keys.content.keyvalues.RSAKeyValue in project OpenAM by OpenRock.

the class AMSignatureProvider method getPublicKeybyDSARSAkeyValue.

protected PublicKey getPublicKeybyDSARSAkeyValue(Document doc, Element reference) throws XMLSignatureException {
    PublicKey pubKey = null;
    Element dsaKey = (Element) reference.getElementsByTagNameNS(Constants.SignatureSpecNS, SAMLConstants.TAG_DSAKEYVALUE).item(0);
    if (dsaKey != null) {
        // It's DSAKey
        NodeList nodes = dsaKey.getChildNodes();
        int nodeCount = nodes.getLength();
        if (nodeCount > 0) {
            BigInteger p = null, q = null, g = null, y = null;
            for (int i = 0; i < nodeCount; i++) {
                Node currentNode = nodes.item(i);
                if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                    String tagName = currentNode.getLocalName();
                    Node sub = currentNode.getChildNodes().item(0);
                    String value = sub.getNodeValue();
                    BigInteger v = new BigInteger(Base64.decode(SAMLUtilsCommon.removeNewLineChars(value)));
                    if (tagName.equals("P")) {
                        p = v;
                    } else if (tagName.equals("Q")) {
                        q = v;
                    } else if (tagName.equals("G")) {
                        g = v;
                    } else if (tagName.equals("Y")) {
                        y = v;
                    } else {
                        throw new XMLSignatureException(SAMLUtilsCommon.bundle.getString("errorObtainPK"));
                    }
                }
            }
            DSAKeyValue dsaKeyValue = new DSAKeyValue(doc, p, q, g, y);
            try {
                pubKey = dsaKeyValue.getPublicKey();
            } catch (Exception e) {
                throw new XMLSignatureException(SAMLUtilsCommon.bundle.getString("errorObtainPK"));
            }
        }
    } else {
        Element rsaKey = (Element) reference.getElementsByTagNameNS(Constants.SignatureSpecNS, SAMLConstants.TAG_RSAKEYVALUE).item(0);
        if (rsaKey != null) {
            // It's RSAKey
            NodeList nodes = rsaKey.getChildNodes();
            int nodeCount = nodes.getLength();
            BigInteger m = null, e = null;
            if (nodeCount > 0) {
                for (int i = 0; i < nodeCount; i++) {
                    Node currentNode = nodes.item(i);
                    if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                        String tagName = currentNode.getLocalName();
                        Node sub = currentNode.getChildNodes().item(0);
                        String value = sub.getNodeValue();
                        BigInteger v = new BigInteger(Base64.decode(SAMLUtilsCommon.removeNewLineChars(value)));
                        if (tagName.equals("Exponent")) {
                            e = v;
                        } else if (tagName.equals("Modulus")) {
                            m = v;
                        } else {
                            throw new XMLSignatureException(SAMLUtilsCommon.bundle.getString("errorObtainPK"));
                        }
                    }
                }
            }
            RSAKeyValue rsaKeyValue = new RSAKeyValue(doc, m, e);
            try {
                pubKey = rsaKeyValue.getPublicKey();
            } catch (Exception ex) {
                throw new XMLSignatureException(SAMLUtilsCommon.bundle.getString("errorObtainPK"));
            }
        }
    }
    return pubKey;
}
Also used : RSAKeyValue(org.apache.xml.security.keys.content.keyvalues.RSAKeyValue) DSAKeyValue(org.apache.xml.security.keys.content.keyvalues.DSAKeyValue) BigInteger(java.math.BigInteger) TransformerException(javax.xml.transform.TransformerException)

Example 2 with RSAKeyValue

use of org.apache.xml.security.keys.content.keyvalues.RSAKeyValue in project cxf by apache.

the class AbstractSTSClient method writeElementsForRSTPublicKey.

protected void writeElementsForRSTPublicKey(W3CDOMStreamWriter writer, X509Certificate cert) throws Exception {
    writer.writeStartElement("wst", "UseKey", namespace);
    writer.writeStartElement("ds", "KeyInfo", "http://www.w3.org/2000/09/xmldsig#");
    writer.writeNamespace("ds", "http://www.w3.org/2000/09/xmldsig#");
    boolean useCert = useCertificateForConfirmationKeyInfo;
    String useCertStr = (String) getProperty(SecurityConstants.STS_TOKEN_USE_CERT_FOR_KEYINFO);
    if (useCertStr != null) {
        useCert = Boolean.parseBoolean(useCertStr);
    }
    if (useCert) {
        X509Data certElem = new X509Data(writer.getDocument());
        certElem.addCertificate(cert);
        writer.getCurrentNode().appendChild(certElem.getElement());
    } else {
        writer.writeStartElement("ds", "KeyValue", "http://www.w3.org/2000/09/xmldsig#");
        PublicKey key = cert.getPublicKey();
        String pubKeyAlgo = key.getAlgorithm();
        if ("DSA".equalsIgnoreCase(pubKeyAlgo)) {
            DSAKeyValue dsaKeyValue = new DSAKeyValue(writer.getDocument(), key);
            writer.getCurrentNode().appendChild(dsaKeyValue.getElement());
        } else if ("RSA".equalsIgnoreCase(pubKeyAlgo)) {
            RSAKeyValue rsaKeyValue = new RSAKeyValue(writer.getDocument(), key);
            writer.getCurrentNode().appendChild(rsaKeyValue.getElement());
        }
        writer.writeEndElement();
    }
    writer.writeEndElement();
    writer.writeEndElement();
}
Also used : RSAKeyValue(org.apache.xml.security.keys.content.keyvalues.RSAKeyValue) DSAKeyValue(org.apache.xml.security.keys.content.keyvalues.DSAKeyValue) PublicKey(java.security.PublicKey) X509Data(org.apache.xml.security.keys.content.X509Data)

Example 3 with RSAKeyValue

use of org.apache.xml.security.keys.content.keyvalues.RSAKeyValue in project santuario-java by apache.

the class KeyValue method getPublicKey.

/**
 * Method getPublicKey
 *
 * @return the public key
 * @throws XMLSecurityException
 */
public PublicKey getPublicKey() throws XMLSecurityException {
    Element rsa = XMLUtils.selectDsNode(getFirstChild(), Constants._TAG_RSAKEYVALUE, 0);
    if (rsa != null) {
        RSAKeyValue kv = new RSAKeyValue(rsa, this.baseURI);
        return kv.getPublicKey();
    }
    Element dsa = XMLUtils.selectDsNode(getFirstChild(), Constants._TAG_DSAKEYVALUE, 0);
    if (dsa != null) {
        DSAKeyValue kv = new DSAKeyValue(dsa, this.baseURI);
        return kv.getPublicKey();
    }
    return null;
}
Also used : RSAKeyValue(org.apache.xml.security.keys.content.keyvalues.RSAKeyValue) DSAKeyValue(org.apache.xml.security.keys.content.keyvalues.DSAKeyValue) Element(org.w3c.dom.Element)

Example 4 with RSAKeyValue

use of org.apache.xml.security.keys.content.keyvalues.RSAKeyValue in project OpenAM by OpenRock.

the class SecurityUtils method getPublicKey.

/**
     * Returns the <code>PublicKey</code>.
     */
private static PublicKey getPublicKey(Element reference) throws XMLSignatureException {
    PublicKey pubKey = null;
    Document doc = reference.getOwnerDocument();
    Element dsaKey = (Element) reference.getElementsByTagNameNS(Constants.SignatureSpecNS, SAMLConstants.TAG_DSAKEYVALUE).item(0);
    if (dsaKey != null) {
        // It's DSAKey
        NodeList nodes = dsaKey.getChildNodes();
        int nodeCount = nodes.getLength();
        if (nodeCount > 0) {
            BigInteger p = null, q = null, g = null, y = null;
            for (int i = 0; i < nodeCount; i++) {
                Node currentNode = nodes.item(i);
                if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                    String tagName = currentNode.getLocalName();
                    Node sub = currentNode.getChildNodes().item(0);
                    String value = sub.getNodeValue();
                    value = SAMLUtils.removeNewLineChars(value);
                    BigInteger v = new BigInteger(Base64.decode(value));
                    if (tagName.equals("P")) {
                        p = v;
                    } else if (tagName.equals("Q")) {
                        q = v;
                    } else if (tagName.equals("G")) {
                        g = v;
                    } else if (tagName.equals("Y")) {
                        y = v;
                    } else {
                        SAMLUtils.debug.error("Wrong tag name in DSA key.");
                        throw new XMLSignatureException(SAMLUtils.bundle.getString("errorObtainPK"));
                    }
                }
            }
            DSAKeyValue dsaKeyValue = new DSAKeyValue(doc, p, q, g, y);
            try {
                pubKey = dsaKeyValue.getPublicKey();
            } catch (XMLSecurityException xse) {
                SAMLUtils.debug.error("Could not get Public Key from" + " DSA key value.");
                throw new XMLSignatureException(SAMLUtils.bundle.getString("errorObtainPK"));
            }
        }
    } else {
        Element rsaKey = (Element) reference.getElementsByTagNameNS(Constants.SignatureSpecNS, SAMLConstants.TAG_RSAKEYVALUE).item(0);
        if (rsaKey != null) {
            // It's RSAKey
            NodeList nodes = rsaKey.getChildNodes();
            int nodeCount = nodes.getLength();
            BigInteger m = null, e = null;
            if (nodeCount > 0) {
                for (int i = 0; i < nodeCount; i++) {
                    Node currentNode = nodes.item(i);
                    if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                        String tagName = currentNode.getLocalName();
                        Node sub = currentNode.getChildNodes().item(0);
                        String value = sub.getNodeValue();
                        value = SAMLUtils.removeNewLineChars(value);
                        BigInteger v = new BigInteger(Base64.decode(value));
                        if (tagName.equals("Exponent")) {
                            e = v;
                        } else if (tagName.equals("Modulus")) {
                            m = v;
                        } else {
                            SAMLUtils.debug.error("Wrong tag name from " + "RSA key element.");
                            throw new XMLSignatureException(SAMLUtils.bundle.getString("errorObtainPK"));
                        }
                    }
                }
            }
            RSAKeyValue rsaKeyValue = new RSAKeyValue(doc, m, e);
            try {
                pubKey = rsaKeyValue.getPublicKey();
            } catch (XMLSecurityException ex) {
                SAMLUtils.debug.error("Could not get Public Key from" + " RSA key value.");
                throw new XMLSignatureException(SAMLUtils.bundle.getString("errorObtainPK"));
            }
        }
    }
    return pubKey;
}
Also used : RSAKeyValue(org.apache.xml.security.keys.content.keyvalues.RSAKeyValue) DSAKeyValue(org.apache.xml.security.keys.content.keyvalues.DSAKeyValue) PublicKey(java.security.PublicKey) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) BigInteger(java.math.BigInteger) Document(org.w3c.dom.Document) XMLSignatureException(com.sun.identity.saml.xmlsig.XMLSignatureException) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException)

Example 5 with RSAKeyValue

use of org.apache.xml.security.keys.content.keyvalues.RSAKeyValue in project cxf by apache.

the class SimpleBatchSTSClient method writeElementsForRSTPublicKey.

protected void writeElementsForRSTPublicKey(W3CDOMStreamWriter writer, X509Certificate cert) throws Exception {
    writer.writeStartElement("wst", "UseKey", namespace);
    writer.writeStartElement("dsig", "KeyInfo", "http://www.w3.org/2000/09/xmldsig#");
    writer.writeNamespace("dsig", "http://www.w3.org/2000/09/xmldsig#");
    boolean useCert = useCertificateForConfirmationKeyInfo;
    String useCertStr = (String) getProperty(SecurityConstants.STS_TOKEN_USE_CERT_FOR_KEYINFO);
    if (useCertStr != null) {
        useCert = Boolean.parseBoolean(useCertStr);
    }
    if (useCert) {
        X509Data certElem = new X509Data(writer.getDocument());
        certElem.addCertificate(cert);
        writer.getCurrentNode().appendChild(certElem.getElement());
    } else {
        writer.writeStartElement("dsig", "KeyValue", "http://www.w3.org/2000/09/xmldsig#");
        PublicKey key = cert.getPublicKey();
        String pubKeyAlgo = key.getAlgorithm();
        if ("DSA".equalsIgnoreCase(pubKeyAlgo)) {
            DSAKeyValue dsaKeyValue = new DSAKeyValue(writer.getDocument(), key);
            writer.getCurrentNode().appendChild(dsaKeyValue.getElement());
        } else if ("RSA".equalsIgnoreCase(pubKeyAlgo)) {
            RSAKeyValue rsaKeyValue = new RSAKeyValue(writer.getDocument(), key);
            writer.getCurrentNode().appendChild(rsaKeyValue.getElement());
        }
        writer.writeEndElement();
    }
    writer.writeEndElement();
    writer.writeEndElement();
}
Also used : RSAKeyValue(org.apache.xml.security.keys.content.keyvalues.RSAKeyValue) DSAKeyValue(org.apache.xml.security.keys.content.keyvalues.DSAKeyValue) PublicKey(java.security.PublicKey) X509Data(org.apache.xml.security.keys.content.X509Data)

Aggregations

RSAKeyValue (org.apache.xml.security.keys.content.keyvalues.RSAKeyValue)6 DSAKeyValue (org.apache.xml.security.keys.content.keyvalues.DSAKeyValue)5 PublicKey (java.security.PublicKey)3 Element (org.w3c.dom.Element)3 BigInteger (java.math.BigInteger)2 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)2 X509Data (org.apache.xml.security.keys.content.X509Data)2 XMLSignatureException (com.sun.identity.saml.xmlsig.XMLSignatureException)1 TransformerException (javax.xml.transform.TransformerException)1 Document (org.w3c.dom.Document)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1