Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
SANTUARIO-546 - refactor Reference class. Thanks to Alanscut <wp_scut…
Browse files Browse the repository at this point in the history
…@163.com> for the patch.

git-svn-id: https://svn.apache.org/repos/asf/santuario/xml-security-java/trunk@1877775 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
coheigea committed May 15, 2020
1 parent 487be76 commit 83ea8fa
Showing 1 changed file with 21 additions and 40 deletions.
61 changes: 21 additions & 40 deletions src/main/java/org/apache/xml/security/signature/Reference.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import java.io.OutputStream;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.apache.xml.security.algorithms.Algorithm;
import org.apache.xml.security.algorithms.MessageDigestAlgorithm;
import org.apache.xml.security.c14n.CanonicalizationException;
import org.apache.xml.security.c14n.InvalidCanonicalizerException;
import org.apache.xml.security.exceptions.XMLSecurityException;
import org.apache.xml.security.signature.reference.ReferenceData;
import org.apache.xml.security.signature.reference.ReferenceNodeSetData;
Expand Down Expand Up @@ -137,6 +137,19 @@ public class Reference extends SignatureElementProxy {

private ReferenceData referenceData;

private static final Set<String> TRANSFORM_ALGORITHMS;

static {
Set<String> algorithms = new HashSet<>();
algorithms.add(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);
algorithms.add(Transforms.TRANSFORM_C14N_EXCL_WITH_COMMENTS);
algorithms.add(Transforms.TRANSFORM_C14N_OMIT_COMMENTS);
algorithms.add(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
algorithms.add(Transforms.TRANSFORM_C14N11_OMIT_COMMENTS);
algorithms.add(Transforms.TRANSFORM_C14N11_WITH_COMMENTS);
TRANSFORM_ALGORITHMS = Collections.unmodifiableSet(algorithms);
}

/**
* Constructor Reference
*
Expand All @@ -148,7 +161,6 @@ public class Reference extends SignatureElementProxy {
* @param messageDigestAlgorithm {@link MessageDigestAlgorithm Digest algorithm} which is
* applied to the data
* TODO should we throw XMLSignatureException if MessageDigestAlgoURI is wrong?
* @throws XMLSignatureException
*/
protected Reference(
Document doc, String baseURI, String referenceURI, Manifest manifest,
Expand Down Expand Up @@ -273,7 +285,7 @@ public MessageDigestAlgorithm getMessageDigestAlgorithm() throws XMLSignatureExc

String uri = digestMethodElem.getAttributeNS(null, Constants._ATT_ALGORITHM);

if ("".equals(uri)) {
if (uri.isEmpty()) {
return null;
}

Expand Down Expand Up @@ -340,7 +352,7 @@ public void setType(String type) {
}

/**
* Return the <code>type</code> atttibute of the Reference indicate whether an
* Return the <code>type</code> attribute of the Reference indicate whether an
* <code>ds:Object</code>, <code>ds:SignatureProperty</code>, or <code>ds:Manifest</code>
* element
*
Expand Down Expand Up @@ -444,14 +456,6 @@ private XMLSignatureInput getContentsAfterTransformation(
}

return output;
} catch (ResourceResolverException ex) {
throw new XMLSignatureException(ex);
} catch (CanonicalizationException ex) {
throw new XMLSignatureException(ex);
} catch (InvalidCanonicalizerException ex) {
throw new XMLSignatureException(ex);
} catch (TransformationException ex) {
throw new XMLSignatureException(ex);
} catch (XMLSecurityException ex) {
throw new XMLSignatureException(ex);
}
Expand Down Expand Up @@ -490,12 +494,7 @@ public XMLSignatureInput getNodesetBeforeFirstCanonicalization()
Transform t = transforms.item(i);
String uri = t.getURI();

if (uri.equals(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS)
|| uri.equals(Transforms.TRANSFORM_C14N_EXCL_WITH_COMMENTS)
|| uri.equals(Transforms.TRANSFORM_C14N_OMIT_COMMENTS)
|| uri.equals(Transforms.TRANSFORM_C14N_WITH_COMMENTS)
|| uri.equals(Transforms.TRANSFORM_C14N11_OMIT_COMMENTS)
|| uri.equals(Transforms.TRANSFORM_C14N11_WITH_COMMENTS)) {
if (TRANSFORM_ALGORITHMS.contains(uri)) {
break;
}

Expand All @@ -505,17 +504,7 @@ public XMLSignatureInput getNodesetBeforeFirstCanonicalization()
output.setSourceURI(input.getSourceURI());
}
return output;
} catch (IOException ex) {
throw new XMLSignatureException(ex);
} catch (ResourceResolverException ex) {
throw new XMLSignatureException(ex);
} catch (CanonicalizationException ex) {
throw new XMLSignatureException(ex);
} catch (InvalidCanonicalizerException ex) {
throw new XMLSignatureException(ex);
} catch (TransformationException ex) {
throw new XMLSignatureException(ex);
} catch (XMLSecurityException ex) {
} catch (IOException | XMLSecurityException ex) {
throw new XMLSignatureException(ex);
}
}
Expand Down Expand Up @@ -566,10 +555,6 @@ public String getHTMLRepresentation() throws XMLSignatureException {
}

return nodes.getHTMLRepresentation(inclusiveNamespaces);
} catch (TransformationException ex) {
throw new XMLSignatureException(ex);
} catch (InvalidTransformException ex) {
throw new XMLSignatureException(ex);
} catch (XMLSecurityException ex) {
throw new XMLSignatureException(ex);
}
Expand Down Expand Up @@ -625,7 +610,7 @@ private void cacheDereferencedElement(XMLSignatureInput input) {
public Iterator<Node> iterator() {
return new Iterator<Node>() {

Iterator<Node> sIterator = s.iterator();
final Iterator<Node> sIterator = s.iterator();

@Override
public boolean hasNext() {
Expand Down Expand Up @@ -690,9 +675,7 @@ public byte[] getReferencedBytes()
try {
XMLSignatureInput output = this.dereferenceURIandPerformTransforms(null);
return output.getBytes();
} catch (IOException ex) {
throw new ReferenceNotInitializedException(ex);
} catch (CanonicalizationException ex) {
} catch (IOException | CanonicalizationException ex) {
throw new ReferenceNotInitializedException(ex);
}
}
Expand Down Expand Up @@ -748,9 +731,7 @@ private byte[] calculateDigest(boolean validating)
//mda.update(data);

return diOs.getDigestValue();
} catch (XMLSecurityException ex) {
throw new ReferenceNotInitializedException(ex);
} catch (IOException ex) {
} catch (XMLSecurityException | IOException ex) {
throw new ReferenceNotInitializedException(ex);
}
}
Expand Down

0 comments on commit 83ea8fa

Please sign in to comment.