Search in sources :

Example 61 with XMLCipher

use of org.apache.xml.security.encryption.XMLCipher in project santuario-java by apache.

the class KeyInfo method itemEncryptedKey.

/**
 * Method itemEncryptedKey
 *
 * @param i
 * @return the asked EncryptedKey element, null if the index is too big
 * @throws XMLSecurityException
 */
public EncryptedKey itemEncryptedKey(int i) throws XMLSecurityException {
    if (encryptedKeys != null) {
        return encryptedKeys.get(i);
    }
    Element e = XMLUtils.selectXencNode(getFirstChild(), EncryptionConstants._TAG_ENCRYPTEDKEY, i);
    if (e != null) {
        XMLCipher cipher = XMLCipher.getInstance();
        cipher.init(XMLCipher.UNWRAP_MODE, null);
        return cipher.loadEncryptedKey(e);
    }
    return null;
}
Also used : Element(org.w3c.dom.Element) XMLCipher(org.apache.xml.security.encryption.XMLCipher)

Example 62 with XMLCipher

use of org.apache.xml.security.encryption.XMLCipher in project santuario-java by apache.

the class XMLEncryption11Test method decryptElement.

/**
 * Method decryptElement
 *
 * Take a key, encryption type and a document, find an encrypted element
 * decrypt it and return the resulting document
 *
 * @param filename File to decrypt from
 * @param key The Key to use for decryption
 */
private Document decryptElement(Document doc, Key rsaKey, X509Certificate rsaCert) throws Exception {
    // Create the XMLCipher element
    XMLCipher cipher = XMLCipher.getInstance();
    // Need to pre-load the Encrypted Data so we can get the key info
    Element ee = (Element) doc.getElementsByTagNameNS("http://www.w3.org/2001/04/xmlenc#", "EncryptedData").item(0);
    cipher.init(XMLCipher.DECRYPT_MODE, null);
    EncryptedData encryptedData = cipher.loadEncryptedData(doc, ee);
    KeyInfo ki = encryptedData.getKeyInfo();
    EncryptedKey encryptedKey = ki.itemEncryptedKey(0);
    KeyInfo kiek = encryptedKey.getKeyInfo();
    X509Data certData = kiek.itemX509Data(0);
    XMLX509Certificate xcert = certData.itemCertificate(0);
    X509Certificate cert = xcert.getX509Certificate();
    assertTrue(rsaCert.equals(cert));
    XMLCipher cipher2 = XMLCipher.getInstance();
    cipher2.init(XMLCipher.UNWRAP_MODE, rsaKey);
    Key key = cipher2.decryptKey(encryptedKey, encryptedData.getEncryptionMethod().getAlgorithm());
    cipher.init(XMLCipher.DECRYPT_MODE, key);
    Document dd = cipher.doFinal(doc, ee);
    return dd;
}
Also used : XMLX509Certificate(org.apache.xml.security.keys.content.x509.XMLX509Certificate) KeyInfo(org.apache.xml.security.keys.KeyInfo) EncryptedKey(org.apache.xml.security.encryption.EncryptedKey) Element(org.w3c.dom.Element) XMLCipher(org.apache.xml.security.encryption.XMLCipher) EncryptedData(org.apache.xml.security.encryption.EncryptedData) Document(org.w3c.dom.Document) X509Data(org.apache.xml.security.keys.content.X509Data) X509Certificate(java.security.cert.X509Certificate) XMLX509Certificate(org.apache.xml.security.keys.content.x509.XMLX509Certificate) EncryptedKey(org.apache.xml.security.encryption.EncryptedKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey)

Example 63 with XMLCipher

use of org.apache.xml.security.encryption.XMLCipher in project santuario-java by apache.

the class XMLEncryption11Test method createEncryptedKey.

/**
 * Create an EncryptedKey object using the given parameters.
 */
private EncryptedKey createEncryptedKey(Document doc, X509Certificate rsaCert, Key sessionKey, String encryptionMethod, String digestMethod, String mgfAlgorithm, byte[] oaepParams) throws Exception {
    // Create the XMLCipher element
    XMLCipher cipher = XMLCipher.getInstance(encryptionMethod, null, digestMethod);
    cipher.init(XMLCipher.WRAP_MODE, rsaCert.getPublicKey());
    EncryptedKey encryptedKey = cipher.encryptKey(doc, sessionKey, mgfAlgorithm, oaepParams);
    KeyInfo builderKeyInfo = encryptedKey.getKeyInfo();
    if (builderKeyInfo == null) {
        builderKeyInfo = new KeyInfo(doc);
        encryptedKey.setKeyInfo(builderKeyInfo);
    }
    X509Data x509Data = new X509Data(doc);
    x509Data.addCertificate(rsaCert);
    builderKeyInfo.add(x509Data);
    return encryptedKey;
}
Also used : EncryptedKey(org.apache.xml.security.encryption.EncryptedKey) KeyInfo(org.apache.xml.security.keys.KeyInfo) XMLCipher(org.apache.xml.security.encryption.XMLCipher) X509Data(org.apache.xml.security.keys.content.X509Data)

Example 64 with XMLCipher

use of org.apache.xml.security.encryption.XMLCipher in project santuario-java by apache.

the class SymmetricEncryptionAlgorithmTest method encrypt.

private void encrypt(String algorithm, Document document, List<String> localNames, Key encryptingKey) throws Exception {
    XMLCipher cipher = XMLCipher.getInstance(algorithm);
    cipher.init(XMLCipher.ENCRYPT_MODE, encryptingKey);
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();
    xpath.setNamespaceContext(new DSNamespaceContext());
    for (String localName : localNames) {
        String expression = "//*[local-name()='" + localName + "']";
        Element elementToEncrypt = (Element) xpath.evaluate(expression, document, XPathConstants.NODE);
        Assert.assertNotNull(elementToEncrypt);
        document = cipher.doFinal(document, elementToEncrypt, false);
    }
    NodeList nodeList = document.getElementsByTagNameNS(XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(), XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart());
    Assert.assertTrue(nodeList.getLength() > 0);
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) DSNamespaceContext(org.apache.xml.security.test.dom.DSNamespaceContext) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) XMLCipher(org.apache.xml.security.encryption.XMLCipher)

Example 65 with XMLCipher

use of org.apache.xml.security.encryption.XMLCipher in project santuario-java by apache.

the class BaltimoreEncTest method decryptElement.

/**
 * Method decryptElement
 *
 * Take a key, encryption type and a file, find an encrypted element
 * decrypt it and return the resulting document
 *
 * @param filename File to decrypt from
 */
private Document decryptElement(String filename) throws Exception {
    XMLCipher cipher;
    // Parse the document in question
    String basedir = System.getProperty("basedir");
    if (basedir != null && !"".equals(basedir)) {
        filename = basedir + "/" + filename;
    }
    File f = new File(filename);
    DocumentBuilder db = XMLUtils.createDocumentBuilder(false);
    Document doc = db.parse(new java.io.FileInputStream(f));
    // Now we have the document, lets build the XMLCipher element
    Element ee = null;
    // Create the XMLCipher element
    cipher = XMLCipher.getInstance();
    // Need to pre-load the Encrypted Data so we can get the key info
    ee = (Element) doc.getElementsByTagName("EncryptedData").item(0);
    cipher.init(XMLCipher.DECRYPT_MODE, null);
    EncryptedData encryptedData = cipher.loadEncryptedData(doc, ee);
    Key key = findKey(encryptedData);
    cipher.init(XMLCipher.DECRYPT_MODE, key);
    Document dd = cipher.doFinal(doc, ee);
    return dd;
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) XMLCipher(org.apache.xml.security.encryption.XMLCipher) EncryptedData(org.apache.xml.security.encryption.EncryptedData) Document(org.w3c.dom.Document) File(java.io.File) EncryptedKey(org.apache.xml.security.encryption.EncryptedKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) SecretKey(javax.crypto.SecretKey)

Aggregations

XMLCipher (org.apache.xml.security.encryption.XMLCipher)74 Document (org.w3c.dom.Document)50 EncryptedKey (org.apache.xml.security.encryption.EncryptedKey)47 NodeList (org.w3c.dom.NodeList)44 SecretKey (javax.crypto.SecretKey)40 Element (org.w3c.dom.Element)33 DocumentBuilder (javax.xml.parsers.DocumentBuilder)30 InputStream (java.io.InputStream)29 KeyGenerator (javax.crypto.KeyGenerator)25 ArrayList (java.util.ArrayList)22 EncryptedData (org.apache.xml.security.encryption.EncryptedData)21 Key (java.security.Key)18 ByteArrayInputStream (java.io.ByteArrayInputStream)16 KeyInfo (org.apache.xml.security.keys.KeyInfo)16 PrivateKey (java.security.PrivateKey)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 DOMSource (javax.xml.transform.dom.DOMSource)13 XMLStreamReader (javax.xml.stream.XMLStreamReader)11 StreamResult (javax.xml.transform.stream.StreamResult)11 InboundXMLSec (org.apache.xml.security.stax.ext.InboundXMLSec)11