| /** |
| * 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 java.util.List; |
| |
| import org.apache.wss4j.common.WSEncryptionPart; |
| import org.apache.wss4j.common.crypto.Crypto; |
| import org.apache.wss4j.common.crypto.CryptoFactory; |
| import org.apache.wss4j.common.util.SOAPUtil; |
| import org.apache.wss4j.common.util.XMLUtils; |
| import org.apache.wss4j.dom.WSConstants; |
| |
| import org.apache.wss4j.dom.engine.WSSConfig; |
| import org.apache.wss4j.dom.engine.WSSecurityEngine; |
| import org.apache.wss4j.dom.handler.WSHandlerResult; |
| |
| import org.junit.jupiter.api.Test; |
| import org.w3c.dom.Document; |
| |
| |
| /** |
| * Test signing with an existing wsu namespace defined with a different prefix to "wsu" |
| */ |
| public class SignatureWSS651Test { |
| private static final org.slf4j.Logger LOG = |
| org.slf4j.LoggerFactory.getLogger(SignatureWSS651Test.class); |
| |
| private static final String SAMPLE_SOAP_MSG_WSU_NS = |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" |
| + "<SOAP-ENV:Envelope " |
| + "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" " |
| + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " |
| + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " |
| + "xmlns:u=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" " |
| + ">" |
| + "<SOAP-ENV:Body>" |
| + "<add xmlns=\"http://ws.apache.org/counter/counter_port_type\">" |
| + "<value xmlns=\"\">15</value>" |
| + "</add>" |
| + "</SOAP-ENV:Body>" |
| + "</SOAP-ENV:Envelope>"; |
| |
| private WSSecurityEngine secEngine = new WSSecurityEngine(); |
| private Crypto crypto; |
| |
| public SignatureWSS651Test() throws Exception { |
| WSSConfig.init(); |
| crypto = CryptoFactory.getInstance(); |
| } |
| |
| @Test |
| public void testSignedTimestamp() throws Exception { |
| Document doc = SOAPUtil.toSOAPPart(SAMPLE_SOAP_MSG_WSU_NS); |
| WSSecHeader secHeader = new WSSecHeader(doc); |
| secHeader.insertSecurityHeader(); |
| |
| WSSecTimestamp timestamp = new WSSecTimestamp(secHeader); |
| timestamp.setTimeToLive(300); |
| timestamp.build(); |
| |
| WSSecSignature builder = new WSSecSignature(secHeader); |
| builder.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security"); |
| |
| // builder.setAddInclusivePrefixes(false); |
| |
| WSEncryptionPart encP = |
| new WSEncryptionPart( |
| "Timestamp", |
| WSConstants.WSU_NS, |
| ""); |
| builder.getParts().add(encP); |
| |
| builder.prepare(crypto); |
| |
| List<javax.xml.crypto.dsig.Reference> referenceList = |
| builder.addReferencesToSign(builder.getParts()); |
| |
| builder.computeSignature(referenceList, false, null); |
| |
| String outputString = XMLUtils.prettyDocumentToString(doc); |
| |
| if (LOG.isDebugEnabled()) { |
| LOG.debug("After Signing...."); |
| LOG.debug(outputString); |
| } |
| |
| verify(doc); |
| |
| Document doc2 = SOAPUtil.toSOAPPart(outputString); |
| verify(doc2); |
| } |
| |
| /** |
| * Verifies the soap envelope. |
| * This method verifies all the signature generated. |
| * |
| * @param env soap envelope |
| * @throws java.lang.Exception Thrown when there is a problem in verification |
| */ |
| private WSHandlerResult verify(Document doc) throws Exception { |
| return secEngine.processSecurityHeader(doc, null, null, crypto); |
| } |
| |
| } |