Search in sources :

Example 1 with TransformSerializer

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

the class SignedEncryptedTest method secureAndVerify.

public void secureAndVerify(TransformerFactory transformerFactory, boolean useDocumentSerializer) throws Exception {
    DocumentBuilder builder = XMLUtils.createDocumentBuilder(false);
    Document document = null;
    try (InputStream is = new ByteArrayInputStream(SAMPLE_MSG.getBytes(StandardCharsets.UTF_8))) {
        document = builder.parse(is);
    }
    // Set up the Key
    KeyPairGenerator rsaKeygen = KeyPairGenerator.getInstance("RSA");
    KeyPair kp = rsaKeygen.generateKeyPair();
    PrivateKey priv = kp.getPrivate();
    PublicKey pub = kp.getPublic();
    XMLSignature sig = new XMLSignature(document, "", XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1, Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);
    Element sigElement = sig.getElement();
    document.getDocumentElement().appendChild(sigElement);
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();
    xpath.setNamespaceContext(new DSNamespaceContext());
    Element element = (Element) xpath.evaluate("//*[local-name()='Body']", document, XPathConstants.NODE);
    String id = UUID.randomUUID().toString();
    element.setAttributeNS(null, "Id", id);
    element.setIdAttributeNS(null, "Id", true);
    Transforms transforms = new Transforms(document);
    transforms.addTransform(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);
    sig.addDocument("#" + id, transforms, Constants.ALGO_ID_DIGEST_SHA1);
    sig.addKeyInfo(pub);
    sig.sign(priv);
    KeyGenerator keygen = KeyGenerator.getInstance("AES");
    keygen.init(256);
    SecretKey secretKey = keygen.generateKey();
    XMLCipher cipher = XMLCipher.getInstance(XMLCipher.AES_128);
    cipher.init(XMLCipher.ENCRYPT_MODE, secretKey);
    document = cipher.doFinal(document, element, true);
    XMLCipher deCipher = XMLCipher.getInstance(XMLCipher.AES_128);
    if (transformerFactory != null && deCipher.getSerializer() instanceof TransformSerializer) {
        Field f = deCipher.getSerializer().getClass().getDeclaredField("transformerFactory");
        f.setAccessible(true);
        f.set(deCipher.getSerializer(), transformerFactory);
    }
    if (useDocumentSerializer) {
        deCipher.setSerializer(new DocumentSerializer());
    }
    deCipher.init(XMLCipher.DECRYPT_MODE, secretKey);
    deCipher.doFinal(document, element, true);
    XMLSignature xmlSignatureVerifier = new XMLSignature(sigElement, "");
    Assert.assertTrue(xmlSignatureVerifier.checkSignatureValue(pub));
}
Also used : XPath(javax.xml.xpath.XPath) KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PublicKey(java.security.PublicKey) Element(org.w3c.dom.Element) Transforms(org.apache.xml.security.transforms.Transforms) XMLCipher(org.apache.xml.security.encryption.XMLCipher) KeyPairGenerator(java.security.KeyPairGenerator) Document(org.w3c.dom.Document) TransformSerializer(org.apache.xml.security.encryption.TransformSerializer) XPathFactory(javax.xml.xpath.XPathFactory) Field(java.lang.reflect.Field) SecretKey(javax.crypto.SecretKey) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLSignature(org.apache.xml.security.signature.XMLSignature) DSNamespaceContext(org.apache.xml.security.test.dom.DSNamespaceContext) DocumentSerializer(org.apache.xml.security.encryption.DocumentSerializer) KeyGenerator(javax.crypto.KeyGenerator)

Example 2 with TransformSerializer

use of org.apache.xml.security.encryption.TransformSerializer in project testcases by coheigea.

the class PerformanceMemoryTest method testRunFirstOutboundEncryptionMemoryPerformance.

@Test
public void testRunFirstOutboundEncryptionMemoryPerformance() throws Exception {
    System.out.println("Testing Outbound Encryption Memory Performance");
    FileWriter outEncryptionSamplesWriter = new FileWriter("target/encryptionOutMemorySamples.txt", false);
    Serializer documentSerializer = new DocumentSerializer();
    Serializer staxSerializer = new StaxSerializer();
    Serializer transformSerializer = new TransformSerializer();
    for (int i = 1; i <= runs; i++) {
        System.out.println("Run " + i);
        File file = generateLargeXMLFile(i * xmlResizeFactor);
        int startTagCount = countXMLStartTags(file);
        outEncryptionSamplesWriter.write("" + startTagCount);
        long startMem = getUsedMemory();
        MemorySamplerThread mst = new MemorySamplerThread(startMem);
        Thread thread = new Thread(mst);
        thread.setPriority(9);
        thread.start();
        File encryptedFile = doDOMEncryptionOutbound(file, startTagCount, documentSerializer);
        mst.setStop(true);
        thread.join();
        outEncryptionSamplesWriter.write(" " + mst.getMaxUsedMemory());
        startMem = getUsedMemory();
        mst = new MemorySamplerThread(startMem);
        thread = new Thread(mst);
        thread.setPriority(9);
        thread.start();
        doDOMEncryptionOutbound(file, startTagCount, staxSerializer);
        mst.setStop(true);
        thread.join();
        outEncryptionSamplesWriter.write(" " + mst.getMaxUsedMemory());
        startMem = getUsedMemory();
        mst = new MemorySamplerThread(startMem);
        thread = new Thread(mst);
        thread.setPriority(9);
        thread.start();
        doDOMEncryptionOutbound(file, startTagCount, transformSerializer);
        mst.setStop(true);
        thread.join();
        outEncryptionSamplesWriter.write(" " + mst.getMaxUsedMemory());
        encryptedFiles.put(startTagCount, encryptedFile);
        outEncryptionSamplesWriter.write("\n");
    }
    outEncryptionSamplesWriter.close();
}
Also used : StaxSerializer(org.apache.cxf.ws.security.wss4j.StaxSerializer) DocumentSerializer(org.apache.xml.security.encryption.DocumentSerializer) FileWriter(java.io.FileWriter) File(java.io.File) DocumentSerializer(org.apache.xml.security.encryption.DocumentSerializer) StaxSerializer(org.apache.cxf.ws.security.wss4j.StaxSerializer) Serializer(org.apache.xml.security.encryption.Serializer) TransformSerializer(org.apache.xml.security.encryption.TransformSerializer) TransformSerializer(org.apache.xml.security.encryption.TransformSerializer) Test(org.junit.Test)

Example 3 with TransformSerializer

use of org.apache.xml.security.encryption.TransformSerializer in project testcases by coheigea.

the class PerformanceMemoryTest method testRunSecondInboundDecryptionMemoryPerformance.

@Test
public void testRunSecondInboundDecryptionMemoryPerformance() throws Exception {
    System.out.println("Testing Inbound Decryption Memory Performance");
    FileWriter inEncryptionSamplesWriter = new FileWriter("target/encryptionInMemorySamples.txt", false);
    Serializer documentSerializer = new DocumentSerializer();
    Serializer staxSerializer = new StaxSerializer();
    Serializer transformSerializer = new TransformSerializer();
    int run = 1;
    Iterator<Map.Entry<Integer, File>> mapIterator = encryptedFiles.entrySet().iterator();
    while (mapIterator.hasNext()) {
        Map.Entry<Integer, File> entry = mapIterator.next();
        System.out.println("Run " + (run++));
        File file = entry.getValue();
        Integer startTagCount = entry.getKey();
        inEncryptionSamplesWriter.write("" + startTagCount);
        long startMem = getUsedMemory();
        MemorySamplerThread mst = new MemorySamplerThread(startMem);
        Thread thread = new Thread(mst);
        thread.setPriority(9);
        thread.start();
        doDOMDecryptionInbound(file, startTagCount, documentSerializer);
        mst.setStop(true);
        thread.join();
        inEncryptionSamplesWriter.write(" " + mst.getMaxUsedMemory());
        startMem = getUsedMemory();
        mst = new MemorySamplerThread(startMem);
        thread = new Thread(mst);
        thread.setPriority(9);
        thread.start();
        doDOMDecryptionInbound(file, startTagCount, staxSerializer);
        mst.setStop(true);
        thread.join();
        inEncryptionSamplesWriter.write(" " + mst.getMaxUsedMemory());
        startMem = getUsedMemory();
        mst = new MemorySamplerThread(startMem);
        thread = new Thread(mst);
        thread.setPriority(9);
        thread.start();
        doDOMDecryptionInbound(file, startTagCount, transformSerializer);
        mst.setStop(true);
        thread.join();
        inEncryptionSamplesWriter.write(" " + mst.getMaxUsedMemory());
        inEncryptionSamplesWriter.write("\n");
    }
    inEncryptionSamplesWriter.close();
}
Also used : FileWriter(java.io.FileWriter) TransformSerializer(org.apache.xml.security.encryption.TransformSerializer) StaxSerializer(org.apache.cxf.ws.security.wss4j.StaxSerializer) DocumentSerializer(org.apache.xml.security.encryption.DocumentSerializer) File(java.io.File) DocumentSerializer(org.apache.xml.security.encryption.DocumentSerializer) StaxSerializer(org.apache.cxf.ws.security.wss4j.StaxSerializer) Serializer(org.apache.xml.security.encryption.Serializer) TransformSerializer(org.apache.xml.security.encryption.TransformSerializer) Test(org.junit.Test)

Example 4 with TransformSerializer

use of org.apache.xml.security.encryption.TransformSerializer in project testcases by coheigea.

the class PerformanceTimingTest method testRunSecondInboundDecryptionTimePerformance.

@Test
public void testRunSecondInboundDecryptionTimePerformance() throws Exception {
    System.out.println("Testing Inbound Decryption Time Performance");
    FileWriter inEncryptionSamplesWriter = new FileWriter("target/encryptionInTimeSamples.txt", false);
    Serializer documentSerializer = new DocumentSerializer();
    Serializer staxSerializer = new StaxSerializer();
    Serializer transformSerializer = new TransformSerializer();
    int run = 1;
    Iterator<Map.Entry<Integer, File>> mapIterator = encryptedFiles.entrySet().iterator();
    while (mapIterator.hasNext()) {
        Map.Entry<Integer, File> entry = mapIterator.next();
        System.out.println("Run " + (run++));
        File file = entry.getValue();
        Integer startTagCount = entry.getKey();
        inEncryptionSamplesWriter.write("" + startTagCount);
        long start = System.currentTimeMillis();
        doDOMDecryptionInbound(file, startTagCount, documentSerializer);
        inEncryptionSamplesWriter.write(" " + ((System.currentTimeMillis() - start) / 1000.0));
        doGC();
        start = System.currentTimeMillis();
        doDOMDecryptionInbound(file, startTagCount, staxSerializer);
        inEncryptionSamplesWriter.write(" " + ((System.currentTimeMillis() - start) / 1000.0));
        doGC();
        start = System.currentTimeMillis();
        doDOMDecryptionInbound(file, startTagCount, transformSerializer);
        inEncryptionSamplesWriter.write(" " + ((System.currentTimeMillis() - start) / 1000.0));
        doGC();
        inEncryptionSamplesWriter.write("\n");
    }
    inEncryptionSamplesWriter.close();
}
Also used : FileWriter(java.io.FileWriter) TransformSerializer(org.apache.xml.security.encryption.TransformSerializer) StaxSerializer(org.apache.cxf.ws.security.wss4j.StaxSerializer) DocumentSerializer(org.apache.xml.security.encryption.DocumentSerializer) TreeMap(java.util.TreeMap) Map(java.util.Map) File(java.io.File) DocumentSerializer(org.apache.xml.security.encryption.DocumentSerializer) StaxSerializer(org.apache.cxf.ws.security.wss4j.StaxSerializer) Serializer(org.apache.xml.security.encryption.Serializer) TransformSerializer(org.apache.xml.security.encryption.TransformSerializer) Test(org.junit.Test)

Example 5 with TransformSerializer

use of org.apache.xml.security.encryption.TransformSerializer in project testcases by coheigea.

the class PerformanceTimingTest method testRunFirstOutboundEncryptionTimePerformance.

@Test
public void testRunFirstOutboundEncryptionTimePerformance() throws Exception {
    System.out.println("Testing Outbound Encryption Time Performance");
    FileWriter outEncryptionSamplesWriter = new FileWriter("target/encryptionOutTimeSamples.txt", false);
    Serializer documentSerializer = new DocumentSerializer();
    Serializer staxSerializer = new StaxSerializer();
    Serializer transformSerializer = new TransformSerializer();
    for (int i = 1; i <= runs; i++) {
        System.out.println("Run " + i);
        File file = generateLargeXMLFile(i * xmlResizeFactor);
        int startTagCount = countXMLStartTags(file);
        outEncryptionSamplesWriter.write("" + startTagCount);
        long start = System.currentTimeMillis();
        File encryptedFile = doDOMEncryptionOutbound(file, startTagCount, documentSerializer);
        outEncryptionSamplesWriter.write(" " + ((System.currentTimeMillis() - start) / 1000.0));
        doGC();
        start = System.currentTimeMillis();
        doDOMEncryptionOutbound(file, startTagCount, staxSerializer);
        outEncryptionSamplesWriter.write(" " + ((System.currentTimeMillis() - start) / 1000.0));
        doGC();
        start = System.currentTimeMillis();
        doDOMEncryptionOutbound(file, startTagCount, transformSerializer);
        outEncryptionSamplesWriter.write(" " + ((System.currentTimeMillis() - start) / 1000.0));
        doGC();
        encryptedFiles.put(startTagCount, encryptedFile);
        outEncryptionSamplesWriter.write("\n");
    }
    outEncryptionSamplesWriter.close();
}
Also used : StaxSerializer(org.apache.cxf.ws.security.wss4j.StaxSerializer) DocumentSerializer(org.apache.xml.security.encryption.DocumentSerializer) FileWriter(java.io.FileWriter) File(java.io.File) DocumentSerializer(org.apache.xml.security.encryption.DocumentSerializer) StaxSerializer(org.apache.cxf.ws.security.wss4j.StaxSerializer) Serializer(org.apache.xml.security.encryption.Serializer) TransformSerializer(org.apache.xml.security.encryption.TransformSerializer) TransformSerializer(org.apache.xml.security.encryption.TransformSerializer) Test(org.junit.Test)

Aggregations

DocumentSerializer (org.apache.xml.security.encryption.DocumentSerializer)5 TransformSerializer (org.apache.xml.security.encryption.TransformSerializer)5 File (java.io.File)4 FileWriter (java.io.FileWriter)4 StaxSerializer (org.apache.cxf.ws.security.wss4j.StaxSerializer)4 Serializer (org.apache.xml.security.encryption.Serializer)4 Test (org.junit.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 KeyPair (java.security.KeyPair)1 KeyPairGenerator (java.security.KeyPairGenerator)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 KeyGenerator (javax.crypto.KeyGenerator)1 SecretKey (javax.crypto.SecretKey)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 XPath (javax.xml.xpath.XPath)1