use of org.apache.cxf.ws.security.wss4j.StaxSerializer in project cxf by apache.
the class AsymmetricBindingHandler method doEncryption.
private WSSecBase doEncryption(AbstractTokenWrapper recToken, List<WSEncryptionPart> encrParts, boolean externalRef) {
// Do encryption
if (recToken != null && recToken.getToken() != null && !encrParts.isEmpty()) {
AbstractToken encrToken = recToken.getToken();
assertPolicy(recToken);
assertPolicy(encrToken);
AlgorithmSuite algorithmSuite = abinding.getAlgorithmSuite();
if (encrToken.getDerivedKeys() == DerivedKeys.RequireDerivedKeys) {
return doEncryptionDerived(recToken, encrToken, encrParts, algorithmSuite);
}
try {
WSSecEncrypt encr = new WSSecEncrypt(secHeader);
encr.setEncryptionSerializer(new StaxSerializer());
encr.setIdAllocator(wssConfig.getIdAllocator());
encr.setCallbackLookup(callbackLookup);
encr.setAttachmentCallbackHandler(new AttachmentCallbackHandler(message));
encr.setStoreBytesInAttachment(storeBytesInAttachment);
encr.setExpandXopInclude(isExpandXopInclude());
encr.setWsDocInfo(wsDocInfo);
Crypto crypto = getEncryptionCrypto();
SecurityToken securityToken = getSecurityToken();
if (!isRequestor() && securityToken != null && recToken.getToken() instanceof SamlToken) {
String tokenType = securityToken.getTokenType();
if (WSS4JConstants.WSS_SAML_TOKEN_TYPE.equals(tokenType) || WSS4JConstants.SAML_NS.equals(tokenType)) {
encr.setCustomEKTokenValueType(WSS4JConstants.WSS_SAML_KI_VALUE_TYPE);
encr.setKeyIdentifierType(WSConstants.CUSTOM_KEY_IDENTIFIER);
encr.setCustomEKTokenId(securityToken.getId());
} else if (WSS4JConstants.WSS_SAML2_TOKEN_TYPE.equals(tokenType) || WSS4JConstants.SAML2_NS.equals(tokenType)) {
encr.setCustomEKTokenValueType(WSS4JConstants.WSS_SAML2_KI_VALUE_TYPE);
encr.setKeyIdentifierType(WSConstants.CUSTOM_KEY_IDENTIFIER);
encr.setCustomEKTokenId(securityToken.getId());
} else {
setKeyIdentifierType(encr, encrToken);
}
} else {
setKeyIdentifierType(encr, encrToken);
}
//
if (!isRequestor() && securityToken != null && securityToken.getX509Certificate() != null) {
encr.setUseThisCert(securityToken.getX509Certificate());
} else if (!isRequestor() && securityToken != null && securityToken.getKey() instanceof PublicKey) {
encr.setUseThisPublicKey((PublicKey) securityToken.getKey());
} else {
setEncryptionUser(encr, encrToken, false, crypto);
}
if (!encr.isCertSet() && encr.getUseThisPublicKey() == null && crypto == null) {
unassertPolicy(recToken, "Missing security configuration. " + "Make sure jaxws:client element is configured " + "with a " + SecurityConstants.ENCRYPT_PROPERTIES + " value.");
}
AlgorithmSuiteType algType = algorithmSuite.getAlgorithmSuiteType();
encr.setSymmetricEncAlgorithm(algType.getEncryption());
encr.setKeyEncAlgo(algType.getAsymmetricKeyWrap());
encr.setMGFAlgorithm(algType.getMGFAlgo());
encr.setDigestAlgorithm(algType.getEncryptionDigest());
encr.prepare(crypto);
Element encryptedKeyElement = encr.getEncryptedKeyElement();
List<Element> attachments = encr.getAttachmentEncryptedDataElements();
// Encrypt, get hold of the ref list and add it
if (externalRef) {
Element refList = encr.encryptForRef(null, encrParts);
if (refList != null) {
insertBeforeBottomUp(refList);
}
if (attachments != null) {
for (Element attachment : attachments) {
this.insertBeforeBottomUp(attachment);
}
}
if (refList != null || (attachments != null && !attachments.isEmpty())) {
this.addEncryptedKeyElement(encryptedKeyElement);
}
} else {
Element refList = encr.encryptForRef(null, encrParts);
if (refList != null || (attachments != null && !attachments.isEmpty())) {
this.addEncryptedKeyElement(encryptedKeyElement);
}
// Add internal refs
if (refList != null) {
encryptedKeyElement.appendChild(refList);
}
if (attachments != null) {
for (Element attachment : attachments) {
this.addEncryptedKeyElement(attachment);
}
}
}
// Put BST before EncryptedKey element
if (encr.getBSTTokenId() != null) {
encr.prependBSTElementToHeader();
}
return encr;
} catch (WSSecurityException e) {
LOG.log(Level.FINE, e.getMessage(), e);
unassertPolicy(recToken, e);
}
}
return null;
}
use of org.apache.cxf.ws.security.wss4j.StaxSerializer in project testcases by coheigea.
the class EncryptionSerializerBenchmark method decryptionStaxSerializer.
@Benchmark
@Fork(1)
@Warmup(iterations = 5)
@Measurement(iterations = 20)
public void decryptionStaxSerializer() throws Exception {
Serializer serializer = new StaxSerializer();
Document encryptedDoc = doEncryption(null);
doDecryption(encryptedDoc, serializer);
}
use of org.apache.cxf.ws.security.wss4j.StaxSerializer 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();
}
use of org.apache.cxf.ws.security.wss4j.StaxSerializer 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();
}
use of org.apache.cxf.ws.security.wss4j.StaxSerializer 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();
}
Aggregations