Author: coheigea Date: Fri Oct 15 13:49:29 2010 New Revision: 1022918 URL: http://svn.apache.org/viewvc?rev=1022918&view=rev Log: More JSR-105 work. - Added back in the EnvelopeIdResolver and a URIReference implementation. - This fixes most of the JDK 1.6 test failures on trunk. 14 tests are still failing due to a potential bug in the XML-Sec code in JDK 1.6. - Converted the SAML code to use JSR-105.
Added: webservices/wss4j/trunk/src/org/apache/ws/security/message/DOMURIDereferencer.java webservices/wss4j/trunk/src/org/apache/ws/security/message/EnvelopeIdResolver.java Modified: webservices/wss4j/trunk/ (props changed) webservices/wss4j/trunk/pom.xml webservices/wss4j/trunk/src/org/apache/ws/security/WSConstants.java webservices/wss4j/trunk/src/org/apache/ws/security/WSDocInfo.java (contents, props changed) webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecDKSign.java webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecEncryptedKey.java webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecSignature.java webservices/wss4j/trunk/src/org/apache/ws/security/message/token/BinarySecurity.java webservices/wss4j/trunk/src/org/apache/ws/security/message/token/DOMX509Data.java webservices/wss4j/trunk/src/org/apache/ws/security/processor/SignatureProcessor.java webservices/wss4j/trunk/src/org/apache/ws/security/saml/SAMLIssuerImpl.java webservices/wss4j/trunk/src/org/apache/ws/security/saml/SAMLUtil.java webservices/wss4j/trunk/src/org/apache/ws/security/saml/WSSecSignatureSAML.java webservices/wss4j/trunk/src/org/apache/ws/security/transform/STRTransform.java webservices/wss4j/trunk/test/wssec/TestWSSecurityNewST3.java Propchange: webservices/wss4j/trunk/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Fri Oct 15 13:49:29 2010 @@ -1 +1 @@ -/webservices/wss4j/branches/1_5_x-fixes:996180,996298,996339,997529,997535,1001926,1002116 +/webservices/wss4j/branches/1_5_x-fixes:996180,996298,996339,997529,997535,1001926,1002116,1002347 Modified: webservices/wss4j/trunk/pom.xml URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/pom.xml?rev=1022918&r1=1022917&r2=1022918&view=diff ============================================================================== --- webservices/wss4j/trunk/pom.xml (original) +++ webservices/wss4j/trunk/pom.xml Fri Oct 15 13:49:29 2010 @@ -122,8 +122,6 @@ <url>http://www.apache.org/</url> </organization> - - <build> <sourceDirectory>src</sourceDirectory> <testSourceDirectory>test</testSourceDirectory> @@ -312,8 +310,6 @@ <defaultGoal>install</defaultGoal> </build> - - <profiles> <profile> @@ -358,23 +354,9 @@ </build> </profile> <profile> - <id>jdk14</id> - <activation> - <activeByDefault>true</activeByDefault> - <jdk>1.4</jdk> - </activation> - <dependencies> - <dependency> - <groupId>org.bouncycastle</groupId> - <artifactId>bcprov-jdk14</artifactId> - <version>${bcprov.jdk14.version}</version> - <scope>test</scope> - </dependency> - </dependencies> - </profile> - <profile> <id>jdk15</id> - <activation> + <activation> + <activeByDefault>true</activeByDefault> <jdk>1.5</jdk> </activation> <dependencies> @@ -385,6 +367,20 @@ <scope>test</scope> </dependency> </dependencies> + </profile> + <profile> + <id>jdk16</id> + <activation> + <jdk>1.6</jdk> + </activation> + <dependencies> + <dependency> + <groupId>org.bouncycastle</groupId> + <artifactId>bcprov-jdk16</artifactId> + <version>${bcprov.jdk16.version}</version> + <scope>test</scope> + </dependency> + </dependencies> </profile> <profile> <id>fastinstall</id> @@ -405,8 +401,8 @@ <xmlsec.version>1.4.3</xmlsec.version> <opensaml.version>1.1</opensaml.version> <xml.apis.version>1.3.04</xml.apis.version> - <bcprov.jdk14.version>1.45</bcprov.jdk14.version> - <bcprov.jdk15.version>1.45</bcprov.jdk15.version> + <bcprov.jdk15.version>1.45</bcprov.jdk15.version> + <bcprov.jdk16.version>1.45</bcprov.jdk16.version> <commons.logging.version>1.1</commons.logging.version> <xalan.version>2.7.1</xalan.version> <xerces.version>2.9.1</xerces.version> @@ -543,9 +539,9 @@ </url> </repository> <snapshotRepository> - <id>apache-snapshots</id> - <name>Apache Development Repository</name> - <url>scpexe://people.apache.org/www/people.apache.org/repo/m2-snapshot-repository</url> + <id>apache.snapshots.https</id> + <name>Apache Development Snapshot Repository</name> + <url>https://repository.apache.org/content/repositories/snapshots</url> <uniqueVersion>false</uniqueVersion> </snapshotRepository> <site> Modified: webservices/wss4j/trunk/src/org/apache/ws/security/WSConstants.java URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/org/apache/ws/security/WSConstants.java?rev=1022918&r1=1022917&r2=1022918&view=diff ============================================================================== --- webservices/wss4j/trunk/src/org/apache/ws/security/WSConstants.java (original) +++ webservices/wss4j/trunk/src/org/apache/ws/security/WSConstants.java Fri Oct 15 13:49:29 2010 @@ -152,6 +152,8 @@ public class WSConstants { public static final String X509_SERIAL_NUMBER_LN = "X509SerialNumber"; public static final String X509_DATA_LN = "X509Data"; public static final String X509_CERT_LN = "X509Certificate"; + public static final String KEYINFO_LN = "KeyInfo"; + public static final String KEYVALUE_LN = "KeyValue"; public static final String ELEM_ENVELOPE = "Envelope"; public static final String ELEM_HEADER = "Header"; Modified: webservices/wss4j/trunk/src/org/apache/ws/security/WSDocInfo.java URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/org/apache/ws/security/WSDocInfo.java?rev=1022918&r1=1022917&r2=1022918&view=diff ============================================================================== --- webservices/wss4j/trunk/src/org/apache/ws/security/WSDocInfo.java (original) +++ webservices/wss4j/trunk/src/org/apache/ws/security/WSDocInfo.java Fri Oct 15 13:49:29 2010 @@ -49,6 +49,7 @@ public class WSDocInfo { List bstList = null; Element assertion = null; List processors = null; + List securityTokenReferences = null; public WSDocInfo(Document doc) { // @@ -61,6 +62,35 @@ public class WSDocInfo { // this.doc = doc.getDocumentElement().getOwnerDocument(); } + + /** + * Set a SecurityTokenReference element. + */ + public void setSecurityTokenReference(Element securityTokenRef) { + if (securityTokenReferences == null) { + securityTokenReferences = new Vector(); + } + securityTokenReferences.add(securityTokenRef); + } + + /** + * Get a SecurityTokenReference for the given (wsu) Id + * + * @param uri is the relative uri (starts with #) of the id + * @return the STR element or null if nothing found + */ + public Element getSecurityTokenReference(String uri) { + if (securityTokenReferences != null) { + for (Iterator iter = securityTokenReferences.iterator(); iter.hasNext();) { + Element elem = (Element)iter.next(); + String cId = elem.getAttributeNS(WSConstants.WSU_NS, "Id"); + if (uri.equals(cId)) { + return elem; + } + } + } + return null; + } /** * Clears the info data except the hash code Propchange: webservices/wss4j/trunk/src/org/apache/ws/security/WSDocInfo.java ------------------------------------------------------------------------------ --- svn:mergeinfo (added) +++ svn:mergeinfo Fri Oct 15 13:49:29 2010 @@ -0,0 +1 @@ +/webservices/wss4j/branches/1_5_x-fixes/src/org/apache/ws/security/WSDocInfo.java:947604,996180,996298,996339,997529,997535,1001926,1002116,1002347 Added: webservices/wss4j/trunk/src/org/apache/ws/security/message/DOMURIDereferencer.java URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/org/apache/ws/security/message/DOMURIDereferencer.java?rev=1022918&view=auto ============================================================================== --- webservices/wss4j/trunk/src/org/apache/ws/security/message/DOMURIDereferencer.java (added) +++ webservices/wss4j/trunk/src/org/apache/ws/security/message/DOMURIDereferencer.java Fri Oct 15 13:49:29 2010 @@ -0,0 +1,111 @@ +/** + * 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.ws.security.message; + +import org.w3c.dom.Attr; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +import org.apache.ws.security.WSDocInfo; +import org.apache.xml.security.utils.IdResolver; +import org.apache.xml.security.utils.resolver.ResourceResolver; +import org.apache.xml.security.signature.XMLSignatureInput; + +import org.jcp.xml.dsig.internal.dom.DOMSubTreeData; + +import javax.xml.crypto.Data; +import javax.xml.crypto.URIDereferencer; +import javax.xml.crypto.URIReference; +import javax.xml.crypto.URIReferenceException; +import javax.xml.crypto.XMLCryptoContext; +import javax.xml.crypto.dom.DOMCryptoContext; +import javax.xml.crypto.dom.DOMURIReference; + +/** + * DOM-based implementation of URIDereferencer. + */ +public class DOMURIDereferencer implements URIDereferencer { + + private WSDocInfo wsDocInfo; + + /** + * @param docInfo The WSDocInfo object to be used for resolving elements + */ + public void setWsDocInfo(WSDocInfo docInfo) { + wsDocInfo = docInfo; + } + + public Data dereference(URIReference uriRef, XMLCryptoContext context) + throws URIReferenceException { + + if (uriRef == null) { + throw new NullPointerException("uriRef cannot be null"); + } + if (context == null) { + throw new NullPointerException("context cannot be null"); + } + + DOMURIReference domRef = (DOMURIReference) uriRef; + Attr uriAttr = (Attr) domRef.getHere(); + String uri = uriRef.getURI(); + DOMCryptoContext dcc = (DOMCryptoContext) context; + + // Check if same-document URI and register ID + if (uri != null && uri.length() != 0 && uri.charAt(0) == '#') { + String id = uri.substring(1); + + if (id.startsWith("xpointer(id(")) { + int i1 = id.indexOf('\''); + int i2 = id.indexOf('\'', i1+1); + id = id.substring(i1+1, i2); + } + + // this is a bit of a hack to check for registered + // IDRefs and manually register them with Apache's IdResolver + // map which includes builtin schema knowledge of DSig/Enc IDs + Node referencedElem = dcc.getElementById(id); + if (referencedElem != null) { + IdResolver.registerElementById((Element) referencedElem, id); + } + } + + try { + String baseURI = context.getBaseURI(); + // + // Try to resolve the element directly using the EnvelopeIdResolver first + // + XMLSignatureInput in = null; + EnvelopeIdResolver envelopeResolver = + (EnvelopeIdResolver)EnvelopeIdResolver.getInstance(); + if (envelopeResolver.engineCanResolve(uriAttr, baseURI)) { + envelopeResolver.setWsDocInfo(wsDocInfo); + in = envelopeResolver.engineResolve(uriAttr, baseURI); + } else { + ResourceResolver resolver = + ResourceResolver.getInstance(uriAttr, baseURI); + in = resolver.resolve(uriAttr, baseURI); + } + + return new DOMSubTreeData(in.getSubNode(), in.isExcludeComments()); + } catch (Exception e) { + throw new URIReferenceException(e); + } + } +} Added: webservices/wss4j/trunk/src/org/apache/ws/security/message/EnvelopeIdResolver.java URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/org/apache/ws/security/message/EnvelopeIdResolver.java?rev=1022918&view=auto ============================================================================== --- webservices/wss4j/trunk/src/org/apache/ws/security/message/EnvelopeIdResolver.java (added) +++ webservices/wss4j/trunk/src/org/apache/ws/security/message/EnvelopeIdResolver.java Fri Oct 15 13:49:29 2010 @@ -0,0 +1,183 @@ +/** + * 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.ws.security.message; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.ws.security.WSConstants; +import org.apache.ws.security.WSDocInfo; +import org.apache.ws.security.util.WSSecurityUtil; +import org.apache.xml.security.signature.XMLSignatureInput; +import org.apache.xml.security.utils.resolver.ResourceResolverException; +import org.apache.xml.security.utils.resolver.ResourceResolverSpi; +import org.apache.xml.utils.URI; +import org.w3c.dom.Attr; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +/** + * XML-Security resolver that is used for resolving same-document URIs like URI="#id". + * It is designed to work only with SOAPEnvelopes. + * <p/> + * + * @author Davanum Srinivas (d...@yahoo.com). + */ +public class EnvelopeIdResolver extends ResourceResolverSpi { + private static Log log = + LogFactory.getLog(EnvelopeIdResolver.class.getName()); + + private static EnvelopeIdResolver resolver = null; + + private boolean doDebug = false; + + private WSDocInfo wsDocInfo; + + /** + * Singleton instance of the resolver. + * <p/> + * + * @return TODO + */ + public synchronized static ResourceResolverSpi getInstance() { + if (resolver == null) { + resolver = new EnvelopeIdResolver(); + } + return resolver; + } + + private EnvelopeIdResolver() { + } + + /** + * @param docInfo The WSDocInfo object to be used for resolving elements + */ + public void setWsDocInfo(WSDocInfo docInfo) { + wsDocInfo = docInfo; + } + + /** + * This is the workhorse method used to resolve resources. + * <p/> + * + * @param uri + * @param BaseURI + * @return TODO + * @throws ResourceResolverException + */ + public XMLSignatureInput engineResolve(Attr uri, String BaseURI) + throws ResourceResolverException { + + doDebug = log.isDebugEnabled(); + + String uriNodeValue = uri.getNodeValue(); + + if (doDebug) { + log.debug("enter engineResolve, look for: " + uriNodeValue); + } + + Document doc = uri.getOwnerDocument(); + + /* + * URI="#chapter1" + * Identifies a node-set containing the element with ID attribute + * value 'chapter1' of the XML resource containing the signature. + * XML Signature (and its applications) modify this node-set to + * include the element plus all descendants including namespaces and + * attributes -- but not comments. + */ + + /* + * First check to see if the element that we require is a SecurityTokenReference + * that is stored in WSDocInfo. + */ + String id = uriNodeValue.substring(1); + Element selectedElem = null; + if (wsDocInfo != null) { + selectedElem = wsDocInfo.getSecurityTokenReference(id); + } + + /* + * Then lookup the SOAP Body element (processed by default) and + * check if it contains a matching Id + */ + if (selectedElem == null) { + selectedElem = WSSecurityUtil.findBodyElement(doc); + if (selectedElem == null) { + throw new ResourceResolverException("generic.EmptyMessage", + new Object[]{"Body element not found"}, + uri, + BaseURI); + } + String cId = selectedElem.getAttributeNS(WSConstants.WSU_NS, "Id"); + + /* + * If Body Id match fails, look for a generic Id (without a namespace) + * that matches the URI. If that lookup fails, try to get a namespace + * qualified Id that matches the URI. + */ + if (!id.equals(cId)) { + cId = null; + + if ((selectedElem = WSSecurityUtil.getElementByWsuId(doc, uriNodeValue)) != null) { + cId = selectedElem.getAttributeNS(WSConstants.WSU_NS, "Id"); + } else if ((selectedElem = WSSecurityUtil.getElementByGenId(doc, uriNodeValue)) != null) { + cId = selectedElem.getAttribute("Id"); + } + if (cId == null) { + throw new ResourceResolverException("generic.EmptyMessage", + new Object[]{"Id not found"}, + uri, + BaseURI); + } + } + } + + XMLSignatureInput result = new XMLSignatureInput(selectedElem); + result.setMIMEType("text/xml"); + try { + URI uriNew = new URI(new URI(BaseURI), uri.getNodeValue()); + result.setSourceURI(uriNew.toString()); + } catch (URI.MalformedURIException ex) { + result.setSourceURI(BaseURI); + } + if (doDebug) { + log.debug("exit engineResolve, result: " + result); + } + return result; + } + + /** + * This method helps the ResourceResolver to decide whether a + * ResourceResolverSpi is able to perform the requested action. + * <p/> + * + * @param uri + * @param BaseURI + * @return TODO + */ + public boolean engineCanResolve(Attr uri, String BaseURI) { + if (uri == null) { + return false; + } + String uriNodeValue = uri.getNodeValue(); + return uriNodeValue.startsWith("#"); + } + +} Modified: webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecDKSign.java URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecDKSign.java?rev=1022918&r1=1022917&r2=1022918&view=diff ============================================================================== --- webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecDKSign.java (original) +++ webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecDKSign.java Fri Oct 15 13:49:29 2010 @@ -35,6 +35,7 @@ import org.w3c.dom.Document; import java.util.List; import java.util.Vector; +import javax.xml.crypto.URIDereferencer; import javax.xml.crypto.XMLStructure; import javax.xml.crypto.dom.DOMStructure; import javax.xml.crypto.dsig.CanonicalizationMethod; @@ -215,6 +216,9 @@ public class WSSecDKSign extends WSSecDe WSConstants.C14N_EXCL_OMIT_COMMENTS_PREFIX ); } + URIDereferencer dereferencer = new DOMURIDereferencer(); + ((DOMURIDereferencer)dereferencer).setWsDocInfo(wsDocInfo); + signContext.setURIDereferencer(new DOMURIDereferencer()); sig.sign(signContext); signatureValue = sig.getSignatureValue().getValue(); Modified: webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecEncryptedKey.java URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecEncryptedKey.java?rev=1022918&r1=1022917&r2=1022918&view=diff ============================================================================== --- webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecEncryptedKey.java (original) +++ webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecEncryptedKey.java Fri Oct 15 13:49:29 2010 @@ -41,8 +41,6 @@ import org.apache.ws.security.message.to import org.apache.ws.security.util.UUIDGenerator; import org.apache.ws.security.util.WSSecurityUtil; -import org.apache.xml.security.keys.KeyInfo; - import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Text; @@ -247,7 +245,6 @@ public class WSSecEncryptedKey extends W } encryptedKeyElement.setAttributeNS(null, "Id", encKeyId); - KeyInfo keyInfo = new KeyInfo(document); SecurityTokenReference secToken = new SecurityTokenReference(document); switch (keyIdentifierType) { @@ -296,11 +293,14 @@ public class WSSecEncryptedKey extends W default: throw new WSSecurityException(WSSecurityException.FAILURE, "unsupportedKeyId"); } - keyInfo.addUnknownElement(secToken.getElement()); - Element keyInfoElement = keyInfo.getElement(); + Element keyInfoElement = + document.createElementNS( + WSConstants.SIG_NS, WSConstants.SIG_PREFIX + ":" + WSConstants.KEYINFO_LN + ); keyInfoElement.setAttributeNS( WSConstants.XMLNS_NS, "xmlns:" + WSConstants.SIG_PREFIX, WSConstants.SIG_NS ); + keyInfoElement.appendChild(secToken.getElement()); encryptedKeyElement.appendChild(keyInfoElement); Element xencCipherValue = createCipherValue(document, encryptedKeyElement); Modified: webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecSignature.java URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecSignature.java?rev=1022918&r1=1022917&r2=1022918&view=diff ============================================================================== --- webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecSignature.java (original) +++ webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecSignature.java Fri Oct 15 13:49:29 2010 @@ -46,6 +46,7 @@ import java.security.cert.X509Certificat import java.util.List; import java.util.Vector; +import javax.xml.crypto.URIDereferencer; import javax.xml.crypto.XMLStructure; import javax.xml.crypto.dom.DOMStructure; import javax.xml.crypto.dsig.CanonicalizationMethod; @@ -246,6 +247,7 @@ public class WSSecSignature extends WSSe } if (keyIdentifierType != WSConstants.KEY_VALUE) { XMLStructure structure = new DOMStructure(secRef.getElement()); + wsDocInfo.setSecurityTokenReference(secRef.getElement()); keyInfo = keyInfoFactory.newKeyInfo( java.util.Collections.singletonList(structure), keyInfoUri @@ -412,6 +414,9 @@ public class WSSecSignature extends WSSe WSConstants.C14N_EXCL_OMIT_COMMENTS_PREFIX ); } + URIDereferencer dereferencer = new DOMURIDereferencer(); + ((DOMURIDereferencer)dereferencer).setWsDocInfo(wsDocInfo); + signContext.setURIDereferencer(new DOMURIDereferencer()); sig.sign(signContext); signatureValue = sig.getSignatureValue().getValue(); Modified: webservices/wss4j/trunk/src/org/apache/ws/security/message/token/BinarySecurity.java URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/org/apache/ws/security/message/token/BinarySecurity.java?rev=1022918&r1=1022917&r2=1022918&view=diff ============================================================================== --- webservices/wss4j/trunk/src/org/apache/ws/security/message/token/BinarySecurity.java (original) +++ webservices/wss4j/trunk/src/org/apache/ws/security/message/token/BinarySecurity.java Fri Oct 15 13:49:29 2010 @@ -159,16 +159,16 @@ public class BinarySecurity { */ public byte[] getToken() { Node node = element.getFirstChild(); - StringBuffer buffer = new StringBuffer(); + StringBuilder builder = new StringBuilder(); while (node != null) { if (Node.TEXT_NODE == node.getNodeType()) { - buffer.append(((Text)node).getData()); + builder.append(((Text)node).getData()); } node = node.getNextSibling(); } try { - return Base64.decode(buffer.toString()); + return Base64.decode(builder.toString()); } catch (Exception ex) { if (LOG.isDebugEnabled()) { LOG.debug(ex.getMessage(), ex); Modified: webservices/wss4j/trunk/src/org/apache/ws/security/message/token/DOMX509Data.java URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/org/apache/ws/security/message/token/DOMX509Data.java?rev=1022918&r1=1022917&r2=1022918&view=diff ============================================================================== --- webservices/wss4j/trunk/src/org/apache/ws/security/message/token/DOMX509Data.java (original) +++ webservices/wss4j/trunk/src/org/apache/ws/security/message/token/DOMX509Data.java Fri Oct 15 13:49:29 2010 @@ -20,7 +20,9 @@ package org.apache.ws.security.message.token; import org.apache.ws.security.WSConstants; +import org.apache.ws.security.WSSecurityException; import org.apache.ws.security.util.DOM2Writer; +import org.apache.ws.security.util.WSSecurityUtil; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -30,6 +32,22 @@ import org.w3c.dom.Node; */ public final class DOMX509Data { private final Element element; + private DOMX509IssuerSerial x509IssuerSerial; + + /** + * Constructor. + */ + public DOMX509Data(Element x509DataElement) throws WSSecurityException { + element = x509DataElement; + // + // Parse X509IssuerSerial child + // + Element issuerSerialElement = + WSSecurityUtil.getDirectChildElement( + element, WSConstants.X509_ISSUER_SERIAL_LN, WSConstants.SIG_NS + ); + x509IssuerSerial = new DOMX509IssuerSerial(issuerSerialElement); + } /** * Constructor. @@ -43,6 +61,22 @@ public final class DOMX509Data { element.appendChild(domIssuerSerial.getElement()); } + /** + * Return true if this X509Data element contains a X509IssuerSerial element + */ + public boolean containsIssuerSerial() { + if (x509IssuerSerial == null) { + return false; + } + return true; + } + + /** + * Return a DOMX509IssuerSerial object in this X509Data structure + */ + public DOMX509IssuerSerial getIssuerSerial() { + return x509IssuerSerial; + } /** * return the dom element. Modified: webservices/wss4j/trunk/src/org/apache/ws/security/processor/SignatureProcessor.java URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/org/apache/ws/security/processor/SignatureProcessor.java?rev=1022918&r1=1022917&r2=1022918&view=diff ============================================================================== --- webservices/wss4j/trunk/src/org/apache/ws/security/processor/SignatureProcessor.java (original) +++ webservices/wss4j/trunk/src/org/apache/ws/security/processor/SignatureProcessor.java Fri Oct 15 13:49:29 2010 @@ -35,6 +35,7 @@ import org.apache.ws.security.WSSecurity import org.apache.ws.security.WSSecurityException; import org.apache.ws.security.WSUsernameTokenPrincipal; import org.apache.ws.security.components.crypto.Crypto; +import org.apache.ws.security.message.DOMURIDereferencer; import org.apache.ws.security.message.token.BinarySecurity; import org.apache.ws.security.message.token.DerivedKeyToken; import org.apache.ws.security.message.token.PKIPathSecurity; @@ -57,6 +58,7 @@ import javax.security.auth.callback.Call import javax.xml.crypto.MarshalException; import javax.xml.crypto.NodeSetData; +import javax.xml.crypto.URIDereferencer; import javax.xml.crypto.XMLStructure; import javax.xml.crypto.dom.DOMStructure; import javax.xml.crypto.dsig.Reference; @@ -390,6 +392,9 @@ public class SignatureProcessor implemen } XMLValidateContext context = new DOMValidateContext(key, elem); context.setProperty("javax.xml.crypto.dsig.cacheReference", Boolean.TRUE); + URIDereferencer dereferencer = new DOMURIDereferencer(); + ((DOMURIDereferencer)dereferencer).setWsDocInfo(wsDocInfo); + context.setURIDereferencer(new DOMURIDereferencer()); try { XMLSignature xmlSignature = signatureFactory.unmarshalXMLSignature(context); boolean signatureOk = xmlSignature.validate(context); Modified: webservices/wss4j/trunk/src/org/apache/ws/security/saml/SAMLIssuerImpl.java URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/org/apache/ws/security/saml/SAMLIssuerImpl.java?rev=1022918&r1=1022917&r2=1022918&view=diff ============================================================================== --- webservices/wss4j/trunk/src/org/apache/ws/security/saml/SAMLIssuerImpl.java (original) +++ webservices/wss4j/trunk/src/org/apache/ws/security/saml/SAMLIssuerImpl.java Fri Oct 15 13:49:29 2010 @@ -22,15 +22,10 @@ package org.apache.ws.security.saml; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.ws.security.WSConstants; +import org.apache.ws.security.WSSConfig; import org.apache.ws.security.WSSecurityException; import org.apache.ws.security.components.crypto.Crypto; import org.apache.ws.security.components.crypto.CryptoFactory; -import org.apache.xml.security.exceptions.XMLSecurityException; -import org.apache.xml.security.keys.KeyInfo; -import org.apache.xml.security.keys.content.X509Data; -import org.apache.xml.security.keys.content.keyvalues.DSAKeyValue; -import org.apache.xml.security.keys.content.keyvalues.RSAKeyValue; -import org.apache.xml.security.signature.XMLSignature; import org.opensaml.SAMLAssertion; import org.opensaml.SAMLAuthenticationStatement; import org.opensaml.SAMLException; @@ -40,6 +35,7 @@ import org.opensaml.SAMLSubject; import org.w3c.dom.Document; import org.w3c.dom.Element; +import java.security.KeyException; import java.security.PublicKey; import java.security.cert.X509Certificate; import java.util.Arrays; @@ -47,6 +43,14 @@ import java.util.Collection; import java.util.Date; import java.util.Properties; +import javax.xml.crypto.MarshalException; +import javax.xml.crypto.XMLStructure; +import javax.xml.crypto.dom.DOMStructure; +import javax.xml.crypto.dsig.keyinfo.KeyInfo; +import javax.xml.crypto.dsig.keyinfo.KeyInfoFactory; +import javax.xml.crypto.dsig.keyinfo.KeyValue; +import javax.xml.crypto.dsig.keyinfo.X509Data; + /** * Builds a WS SAML Assertion and inserts it into the SOAP Envelope. Refer to * the WS specification, SAML Token profile @@ -73,6 +77,9 @@ public class SAMLIssuerImpl implements S private Crypto userCrypto = null; private String username = null; + private KeyInfoFactory keyInfoFactory = KeyInfoFactory.getInstance("DOM"); + private WSSConfig wssConfig = WSSConfig.getDefaultWSConfig(); + /** * Flag indicating what format to put the subject's key material in when * NOT using Sender Vouches as the confirmation method. The default is @@ -183,53 +190,60 @@ public class SAMLIssuerImpl implements S Arrays.asList(statements)); if (!senderVouches) { - KeyInfo ki = new KeyInfo(instanceDoc); + KeyInfo keyInfo = null; try { X509Certificate[] certs = userCrypto.getCertificates(username); + String keyInfoUri = + wssConfig.getIdAllocator().createSecureId("KI-", keyInfo); if (sendKeyValue) { PublicKey key = certs[0].getPublicKey(); - String pubKeyAlgo = key.getAlgorithm(); - - if ("DSA".equalsIgnoreCase(pubKeyAlgo)) { - DSAKeyValue dsaKeyValue = new DSAKeyValue(instanceDoc, key); - ki.add(dsaKeyValue); - } else if ("RSA".equalsIgnoreCase(pubKeyAlgo)) { - RSAKeyValue rsaKeyValue = new RSAKeyValue(instanceDoc, key); - ki.add(rsaKeyValue); - } + KeyValue keyValue = keyInfoFactory.newKeyValue(key); + keyInfo = + keyInfoFactory.newKeyInfo( + java.util.Collections.singletonList(keyValue), keyInfoUri + ); } else { - X509Data certElem = new X509Data(instanceDoc); - certElem.addCertificate(certs[0]); - ki.add(certElem); + X509Data x509Data = + keyInfoFactory.newX509Data(java.util.Collections.singletonList(certs[0])); + keyInfo = + keyInfoFactory.newKeyInfo( + java.util.Collections.singletonList(x509Data), keyInfoUri + ); } + + Element keyInfoParent = instanceDoc.createElement("KeyInfoParent"); + XMLStructure structure = new DOMStructure(keyInfoParent); + keyInfo.marshal(structure, null); + Element keyInfoElement = (Element)keyInfoParent.getFirstChild(); + subject.setKeyInfo(keyInfoElement); } catch (WSSecurityException ex) { if (log.isDebugEnabled()) { log.debug(ex.getMessage(), ex); } return null; - } catch (XMLSecurityException ex) { + } catch (MarshalException ex) { + if (log.isDebugEnabled()) { + log.debug(ex.getMessage(), ex); + } + return null; + } catch (KeyException ex) { if (log.isDebugEnabled()) { log.debug(ex.getMessage(), ex); } return null; } - Element keyInfoElement = ki.getElement(); - keyInfoElement.setAttributeNS(WSConstants.XMLNS_NS, "xmlns:" - + WSConstants.SIG_PREFIX, WSConstants.SIG_NS); - - subject.setKeyInfo(ki); // prepare to sign the SAML token try { X509Certificate[] issuerCerts = issuerCrypto.getCertificates(issuerKeyName); - String sigAlgo = XMLSignature.ALGO_ID_SIGNATURE_RSA; + String sigAlgo = WSConstants.RSA_SHA1; String pubKeyAlgo = issuerCerts[0].getPublicKey().getAlgorithm(); log.debug("automatic sig algo detection: " + pubKeyAlgo); if (pubKeyAlgo.equalsIgnoreCase("DSA")) { - sigAlgo = XMLSignature.ALGO_ID_SIGNATURE_DSA; + sigAlgo = WSConstants.DSA; } java.security.Key issuerPK = issuerCrypto.getPrivateKey(issuerKeyName, Modified: webservices/wss4j/trunk/src/org/apache/ws/security/saml/SAMLUtil.java URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/org/apache/ws/security/saml/SAMLUtil.java?rev=1022918&r1=1022917&r2=1022918&view=diff ============================================================================== --- webservices/wss4j/trunk/src/org/apache/ws/security/saml/SAMLUtil.java (original) +++ webservices/wss4j/trunk/src/org/apache/ws/security/saml/SAMLUtil.java Fri Oct 15 13:49:29 2010 @@ -29,10 +29,6 @@ import org.apache.ws.security.components import org.apache.ws.security.processor.EncryptedKeyProcessor; import org.apache.ws.security.util.Base64; import org.apache.ws.security.util.WSSecurityUtil; -import org.apache.xml.security.exceptions.XMLSecurityException; -import org.apache.xml.security.keys.KeyInfo; -import org.apache.xml.security.keys.content.X509Data; -import org.apache.xml.security.keys.content.x509.XMLX509Certificate; import org.opensaml.SAMLAssertion; import org.opensaml.SAMLAttributeStatement; import org.opensaml.SAMLAuthenticationStatement; @@ -47,11 +43,18 @@ import org.w3c.dom.Text; import javax.security.auth.callback.Callback; import javax.security.auth.callback.CallbackHandler; +import javax.xml.crypto.XMLStructure; +import javax.xml.crypto.dom.DOMStructure; +import javax.xml.crypto.dsig.keyinfo.KeyInfo; +import javax.xml.crypto.dsig.keyinfo.KeyInfoFactory; +import javax.xml.crypto.dsig.keyinfo.KeyValue; +import javax.xml.crypto.dsig.keyinfo.X509Data; import javax.xml.namespace.QName; import java.security.PublicKey; import java.security.cert.X509Certificate; import java.util.Iterator; +import java.util.List; /** * Utility methods for SAML stuff @@ -134,32 +137,35 @@ public class SAMLUtil { Element e = samlSubj.getKeyInfo(); X509Certificate[] certs = null; + KeyInfoFactory keyInfoFactory = KeyInfoFactory.getInstance("DOM"); + XMLStructure keyInfoStructure = new DOMStructure(e); + try { - KeyInfo ki = new KeyInfo(e, null); - - if (ki.containsX509Data()) { - X509Data data = ki.itemX509Data(0); - XMLX509Certificate certElem = null; - if (data != null && data.containsCertificate()) { - certElem = data.itemCertificate(0); - } - if (certElem != null) { - X509Certificate cert = certElem.getX509Certificate(); - certs = new X509Certificate[1]; - certs[0] = cert; - return new SAMLKeyInfo(assertion, certs); + KeyInfo keyInfo = keyInfoFactory.unmarshalKeyInfo(keyInfoStructure); + List list = keyInfo.getContent(); + + for (int i = 0; i < list.size(); i++) { + XMLStructure xmlStructure = (XMLStructure) list.get(i); + if (xmlStructure instanceof KeyValue) { + PublicKey publicKey = ((KeyValue)xmlStructure).getPublicKey(); + return new SAMLKeyInfo(assertion, publicKey); + } else if (xmlStructure instanceof X509Data) { + List x509Data = ((X509Data)xmlStructure).getContent(); + for (int j = 0; j < x509Data.size(); j++) { + Object x509obj = x509Data.get(j); + if (x509obj instanceof X509Certificate) { + certs = new X509Certificate[1]; + certs[0] = (X509Certificate)x509obj; + return new SAMLKeyInfo(assertion, certs); + } + } } - } else if (ki.containsKeyValue()) { - PublicKey pk = ki.getPublicKey(); - return new SAMLKeyInfo(assertion, pk); } - - } catch (XMLSecurityException e3) { + } catch (Exception ex) { throw new WSSecurityException(WSSecurityException.FAILURE, "invalidSAMLsecurity", - new Object[]{"cannot get certificate (key holder)"}, e3); + new Object[]{"cannot get certificate or key "}, ex); } - } else { throw new WSSecurityException(WSSecurityException.FAILURE, "invalidSAMLsecurity", @@ -227,27 +233,34 @@ public class SAMLUtil { // } Element e = samlSubj.getKeyInfo(); X509Certificate[] certs = null; + KeyInfoFactory keyInfoFactory = KeyInfoFactory.getInstance("DOM"); + XMLStructure keyInfoStructure = new DOMStructure(e); + try { - KeyInfo ki = new KeyInfo(e, null); + KeyInfo keyInfo = keyInfoFactory.unmarshalKeyInfo(keyInfoStructure); + List list = keyInfo.getContent(); - if (ki.containsX509Data()) { - X509Data data = ki.itemX509Data(0); - XMLX509Certificate certElem = null; - if (data != null && data.containsCertificate()) { - certElem = data.itemCertificate(0); - } - if (certElem != null) { - X509Certificate cert = certElem.getX509Certificate(); - certs = new X509Certificate[1]; - certs[0] = cert; + for (int i = 0; i < list.size(); i++) { + XMLStructure xmlStructure = (XMLStructure) list.get(i); + if (xmlStructure instanceof X509Data) { + List x509Data = ((X509Data)xmlStructure).getContent(); + for (int j = 0; j < x509Data.size(); j++) { + Object x509obj = x509Data.get(j); + if (x509obj instanceof X509Certificate) { + certs = new X509Certificate[1]; + certs[0] = (X509Certificate)x509obj; + break; + } + } } } // TODO: get alias name for cert, check against username set by caller - } catch (XMLSecurityException e3) { + } catch (Exception ex) { throw new WSSecurityException(WSSecurityException.FAILURE, "invalidSAMLsecurity", - new Object[]{"cannot get certificate (key holder)"}, e3); + new Object[]{"cannot get certificate or key "}, ex); } + return certs; } Modified: webservices/wss4j/trunk/src/org/apache/ws/security/saml/WSSecSignatureSAML.java URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/org/apache/ws/security/saml/WSSecSignatureSAML.java?rev=1022918&r1=1022917&r2=1022918&view=diff ============================================================================== --- webservices/wss4j/trunk/src/org/apache/ws/security/saml/WSSecSignatureSAML.java (original) +++ webservices/wss4j/trunk/src/org/apache/ws/security/saml/WSSecSignatureSAML.java Fri Oct 15 13:49:29 2010 @@ -55,7 +55,10 @@ import javax.xml.crypto.dsig.SignedInfo; import javax.xml.crypto.dsig.XMLSignContext; import javax.xml.crypto.dsig.XMLSignatureFactory; import javax.xml.crypto.dsig.dom.DOMSignContext; +import javax.xml.crypto.dsig.keyinfo.KeyInfo; import javax.xml.crypto.dsig.keyinfo.KeyInfoFactory; +import javax.xml.crypto.dsig.keyinfo.KeyValue; +import javax.xml.crypto.dsig.keyinfo.X509Data; import javax.xml.crypto.dsig.spec.C14NMethodParameterSpec; import javax.xml.crypto.dsig.spec.ExcC14NParameterSpec; @@ -70,6 +73,8 @@ public class WSSecSignatureSAML extends private Crypto issuerCrypto = null; private String issuerKeyName = null; private String issuerKeyPW = null; + + private KeyInfoFactory keyInfoFactory = KeyInfoFactory.getInstance("DOM"); /** * Constructor. @@ -260,22 +265,26 @@ public class WSSecSignatureSAML extends } Element e = samlSubj.getKeyInfo(); try { - org.apache.xml.security.keys.KeyInfo ki = - new org.apache.xml.security.keys.KeyInfo(e, null); - - if (ki.containsX509Data()) { - org.apache.xml.security.keys.content.X509Data data = ki.itemX509Data(0); - org.apache.xml.security.keys.content.x509.XMLX509Certificate certElem = null; - if (data != null && data.containsCertificate()) { - certElem = data.itemCertificate(0); + XMLStructure keyInfoStructure = new DOMStructure(e); + KeyInfo keyInfo = keyInfoFactory.unmarshalKeyInfo(keyInfoStructure); + List list = keyInfo.getContent(); + + for (int i = 0; i < list.size(); i++) { + XMLStructure xmlStructure = (XMLStructure) list.get(i); + if (xmlStructure instanceof KeyValue) { + publicKey = ((KeyValue)xmlStructure).getPublicKey(); + break; + } else if (xmlStructure instanceof X509Data) { + List x509Data = ((X509Data)xmlStructure).getContent(); + for (int j = 0; j < x509Data.size(); j++) { + Object x509obj = x509Data.get(j); + if (x509obj instanceof X509Certificate) { + certs = new X509Certificate[1]; + certs[0] = (X509Certificate)x509obj; + break; + } + } } - if (certElem != null) { - X509Certificate cert = certElem.getX509Certificate(); - certs = new X509Certificate[1]; - certs[0] = cert; - } - } else if (ki.containsKeyValue()) { - publicKey = ki.getPublicKey(); } // TODO: get alias name for cert, check against username set by // caller @@ -376,6 +385,7 @@ public class WSSecSignatureSAML extends ref.setValueType(WSConstants.WSS_SAML_KI_VALUE_TYPE); secRefSaml.setReference(ref); } + wsDocInfo.setSecurityTokenReference(secRefSaml.getElement()); } } catch (Exception ex) { throw new WSSecurityException( @@ -427,6 +437,8 @@ public class WSSecSignatureSAML extends } } XMLStructure structure = new DOMStructure(secRef.getElement()); + wsDocInfo.setSecurityTokenReference(secRef.getElement()); + keyInfo = keyInfoFactory.newKeyInfo( java.util.Collections.singletonList(structure), keyInfoUri Modified: webservices/wss4j/trunk/src/org/apache/ws/security/transform/STRTransform.java URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/org/apache/ws/security/transform/STRTransform.java?rev=1022918&r1=1022917&r2=1022918&view=diff ============================================================================== --- webservices/wss4j/trunk/src/org/apache/ws/security/transform/STRTransform.java (original) +++ webservices/wss4j/trunk/src/org/apache/ws/security/transform/STRTransform.java Fri Oct 15 13:49:29 2010 @@ -28,7 +28,6 @@ import org.apache.ws.security.util.WSSec import org.apache.xml.security.signature.XMLSignatureInput; import org.jcp.xml.dsig.internal.dom.ApacheData; -import org.jcp.xml.dsig.internal.dom.ApacheOctetStreamData; import org.jcp.xml.dsig.internal.dom.DOMSubTreeData; import org.jcp.xml.dsig.internal.dom.DOMUtils; @@ -219,7 +218,7 @@ public class STRTransform extends Transf output.updateOutputStream(os); return null; } - return new ApacheOctetStreamData(output); + return new OctetStreamData(output.getOctetStream()); } catch (Exception ex) { throw new TransformException(ex); } Modified: webservices/wss4j/trunk/test/wssec/TestWSSecurityNewST3.java URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/test/wssec/TestWSSecurityNewST3.java?rev=1022918&r1=1022917&r2=1022918&view=diff ============================================================================== --- webservices/wss4j/trunk/test/wssec/TestWSSecurityNewST3.java (original) +++ webservices/wss4j/trunk/test/wssec/TestWSSecurityNewST3.java Fri Oct 15 13:49:29 2010 @@ -76,6 +76,7 @@ public class TestWSSecurityNewST3 extend */ public TestWSSecurityNewST3(String name) { super(name); + secEngine.getWssConfig(); } /** --------------------------------------------------------------------- To unsubscribe, e-mail: wss4j-dev-unsubscr...@ws.apache.org For additional commands, e-mail: wss4j-dev-h...@ws.apache.org