blob: 2449af985b94acb37ec35bb0b8b0bd72ddaa0536 [file] [log] [blame]
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.wss4j.dom.message;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.apache.wss4j.common.crypto.Crypto;
import org.apache.wss4j.common.crypto.CryptoFactory;
import org.apache.wss4j.common.util.XMLUtils;
import org.apache.wss4j.dom.WSConstants;
import org.apache.wss4j.dom.common.KeystoreCallbackHandler;
import org.apache.wss4j.dom.common.SOAPUtil;
import org.apache.wss4j.dom.engine.WSSConfig;
import org.apache.wss4j.dom.engine.WSSecurityEngine;
import org.apache.wss4j.dom.engine.WSSecurityEngineResult;
import org.apache.wss4j.dom.handler.RequestData;
import org.apache.wss4j.dom.handler.WSHandlerResult;
import org.apache.wss4j.dom.str.STRParser.REFERENCE_TYPE;
import org.apache.wss4j.dom.util.WSSecurityUtil;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
* A set of test-cases for signing and verifying SOAP requests using a specific provider
*/
public class SignatureProviderTest {
private static final org.slf4j.Logger LOG =
org.slf4j.LoggerFactory.getLogger(SignatureProviderTest.class);
private WSSecurityEngine secEngine = new WSSecurityEngine();
public SignatureProviderTest() throws Exception {
WSSConfig.init();
}
@Test
public void testBouncyCastleSignature() throws Exception {
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
WSSecSignature builder = new WSSecSignature(secHeader);
builder.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security");
builder.setKeyIdentifierType(WSConstants.ISSUER_SERIAL);
builder.setSignatureProvider(new BouncyCastleProvider());
LOG.info("Before Signing IS....");
Crypto crypto = CryptoFactory.getInstance();
Document signedDoc = builder.build(crypto);
if (LOG.isDebugEnabled()) {
LOG.debug("Signed message with IssuerSerial key identifier:");
String outputString =
XMLUtils.prettyDocumentToString(signedDoc);
LOG.debug(outputString);
}
LOG.info("After Signing IS....");
WSHandlerResult results = verify(signedDoc, crypto);
WSSecurityEngineResult actionResult =
results.getActionResults().get(WSConstants.SIGN).get(0);
assertNotNull(actionResult.get(WSSecurityEngineResult.TAG_X509_CERTIFICATE));
assertNotNull(actionResult.get(WSSecurityEngineResult.TAG_X509_REFERENCE_TYPE));
REFERENCE_TYPE referenceType =
(REFERENCE_TYPE)actionResult.get(WSSecurityEngineResult.TAG_X509_REFERENCE_TYPE);
assertTrue(referenceType == REFERENCE_TYPE.ISSUER_SERIAL);
}
private WSHandlerResult verify(Document doc, Crypto crypto) throws Exception {
RequestData data = new RequestData();
data.setWssConfig(WSSConfig.getNewInstance());
data.setSigVerCrypto(crypto);
data.setDecCrypto(crypto);
data.setSignatureProvider(new BouncyCastleProvider());
data.setCallbackHandler(new KeystoreCallbackHandler());
Element securityHeader = WSSecurityUtil.getSecurityHeader(doc, null);
return secEngine.processSecurityHeader(securityHeader, data);
}
}