reference.java
来自「JAVA 所有包」· Java 代码 · 共 751 行 · 第 1/2 页
JAVA
751 行
if (resolver == null) { Object exArgs[] = { URI }; throw new ReferenceNotInitializedException( "signature.Verification.Reference.NoInput", exArgs); } resolver.addProperties(this._manifest._resolverProperties); XMLSignatureInput input = resolver.resolve(URIAttr, this._baseURI); return input; } catch (ResourceResolverException ex) { throw new ReferenceNotInitializedException("empty", ex); } catch (XMLSecurityException ex) { throw new ReferenceNotInitializedException("empty", ex); } } /** * Returns the data which is referenced by the URI attribute. This method * only works works after a call to verify. * @return a XMLSignature with a byte array. * @throws ReferenceNotInitializedException * * @deprecated use getContentsBeforeTransformation */ public XMLSignatureInput getTransformsInput() throws ReferenceNotInitializedException { XMLSignatureInput input=getContentsBeforeTransformation(); XMLSignatureInput result; try { result = new XMLSignatureInput(input.getBytes()); } catch (CanonicalizationException ex) { throw new ReferenceNotInitializedException("empty", ex); } catch (IOException ex) { throw new ReferenceNotInitializedException("empty", ex); } result.setSourceURI(input.getSourceURI()); return result; } private XMLSignatureInput getContentsAfterTransformation(XMLSignatureInput input, OutputStream os) throws XMLSignatureException { try { Transforms transforms = this.getTransforms(); XMLSignatureInput output = null; if (transforms != null) { output = transforms.performTransforms(input,os); this._transformsOutput = output;//new XMLSignatureInput(output.getBytes()); //this._transformsOutput.setSourceURI(output.getSourceURI()); } else { output = input; } return output; } catch (ResourceResolverException ex) { throw new XMLSignatureException("empty", ex); } catch (CanonicalizationException ex) { throw new XMLSignatureException("empty", ex); } catch (InvalidCanonicalizerException ex) { throw new XMLSignatureException("empty", ex); } catch (TransformationException ex) { throw new XMLSignatureException("empty", ex); } catch (XMLSecurityException ex) { throw new XMLSignatureException("empty", ex); } } /** * Returns the XMLSignatureInput which is the result of the Transforms. * @return a XMLSignatureInput with all transformations applied. * @throws XMLSignatureException */ public XMLSignatureInput getContentsAfterTransformation() throws XMLSignatureException { XMLSignatureInput input = this.getContentsBeforeTransformation(); return this.getContentsAfterTransformation(input, null); } /** * This method returns the XMLSignatureInput which represents the node set before * some kind of canonicalization is applied for the first time. * @return Gets a the node doing everything till the first c14n is needed * * @throws XMLSignatureException */ public XMLSignatureInput getNodesetBeforeFirstCanonicalization() throws XMLSignatureException { try { XMLSignatureInput input = this.getContentsBeforeTransformation(); XMLSignatureInput output = input; Transforms transforms = this.getTransforms(); if (transforms != null) { doTransforms: for (int i = 0; i < transforms.getLength(); i++) { 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)) { break doTransforms; } output = t.performTransform(output, null); } output.setSourceURI(input.getSourceURI()); } return output; } catch (IOException ex) { throw new XMLSignatureException("empty", ex); } catch (ResourceResolverException ex) { throw new XMLSignatureException("empty", ex); } catch (CanonicalizationException ex) { throw new XMLSignatureException("empty", ex); } catch (InvalidCanonicalizerException ex) { throw new XMLSignatureException("empty", ex); } catch (TransformationException ex) { throw new XMLSignatureException("empty", ex); } catch (XMLSecurityException ex) { throw new XMLSignatureException("empty", ex); } } /** * Method getHTMLRepresentation * @return The HTML of the transformation * @throws XMLSignatureException */ public String getHTMLRepresentation() throws XMLSignatureException { try { XMLSignatureInput nodes = this.getNodesetBeforeFirstCanonicalization(); Set inclusiveNamespaces = new HashSet(); { Transforms transforms = this.getTransforms(); Transform c14nTransform = null; if (transforms != null) { doTransforms: for (int i = 0; i < transforms.getLength(); i++) { 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)) { c14nTransform = t; break doTransforms; } } } if (c14nTransform != null) { if (c14nTransform .length(InclusiveNamespaces .ExclusiveCanonicalizationNamespace, InclusiveNamespaces ._TAG_EC_INCLUSIVENAMESPACES) == 1) { // there is one InclusiveNamespaces element InclusiveNamespaces in = new InclusiveNamespaces( XMLUtils.selectNode( c14nTransform.getElement().getFirstChild(), InclusiveNamespaces.ExclusiveCanonicalizationNamespace, InclusiveNamespaces._TAG_EC_INCLUSIVENAMESPACES,0), this.getBaseURI()); inclusiveNamespaces = InclusiveNamespaces.prefixStr2Set( in.getInclusiveNamespaces()); } } } return nodes.getHTMLRepresentation(inclusiveNamespaces); } catch (TransformationException ex) { throw new XMLSignatureException("empty", ex); } catch (InvalidTransformException ex) { throw new XMLSignatureException("empty", ex); } catch (XMLSecurityException ex) { throw new XMLSignatureException("empty", ex); } } /** * This method only works works after a call to verify. * @return the transformed output(i.e. what is going to be digested). */ public XMLSignatureInput getTransformsOutput() { return this._transformsOutput; } /** * This method returns the {@link XMLSignatureInput} which is referenced by the * <CODE>URI</CODE> Attribute. * @param os where to write the transformation can be null. * @return the element to digest * * @throws XMLSignatureException * @see Manifest#verifyReferences() */ protected XMLSignatureInput dereferenceURIandPerformTransforms(OutputStream os) throws XMLSignatureException { try { XMLSignatureInput input = this.getContentsBeforeTransformation(); XMLSignatureInput output = this.getContentsAfterTransformation(input, os); /* at this stage, this._transformsInput and this._transformsOutput * contain a huge amount of nodes. When we do not cache these nodes * but only preserve the octets, the memory footprint is dramatically * reduced. */ this._transformsOutput = output; return output; } catch (XMLSecurityException ex) { throw new ReferenceNotInitializedException("empty", ex); } } /** * Method getTransforms * * @return The transforms that applied this reference. * @throws InvalidTransformException * @throws TransformationException * @throws XMLSecurityException * @throws XMLSignatureException */ public Transforms getTransforms() throws XMLSignatureException, InvalidTransformException, TransformationException, XMLSecurityException { Element transformsElement = XMLUtils.selectDsNode(this._constructionElement.getFirstChild(), Constants._TAG_TRANSFORMS,0); if (transformsElement != null) { Transforms transforms = new Transforms(transformsElement, this._baseURI); return transforms; } return null; } /** * Method getReferencedBytes * * @return the bytes that will be used to generated digest. * @throws ReferenceNotInitializedException * @throws XMLSignatureException */ public byte[] getReferencedBytes() throws ReferenceNotInitializedException, XMLSignatureException { try { XMLSignatureInput output=this.dereferenceURIandPerformTransforms(null); byte[] signedBytes = output.getBytes(); return signedBytes; } catch (IOException ex) { throw new ReferenceNotInitializedException("empty", ex); } catch (CanonicalizationException ex) { throw new ReferenceNotInitializedException("empty", ex); } } /** * Method resolverResult * * @return reference Calculate the digest of this reference. * @throws ReferenceNotInitializedException * @throws XMLSignatureException */ private byte[] calculateDigest() throws ReferenceNotInitializedException, XMLSignatureException { try { MessageDigestAlgorithm mda = this.getMessageDigestAlgorithm(); mda.reset(); DigesterOutputStream diOs=new DigesterOutputStream(mda); OutputStream os=new UnsyncBufferedOutputStream(diOs); XMLSignatureInput output=this.dereferenceURIandPerformTransforms(os); output.updateOutputStream(os); os.flush(); //this.getReferencedBytes(diOs); //mda.update(data); return diOs.getDigestValue(); } catch (XMLSecurityException ex) { throw new ReferenceNotInitializedException("empty", ex); } catch (IOException ex) { throw new ReferenceNotInitializedException("empty", ex); } } /** * Returns the digest value. * * @return the digest value. * @throws Base64DecodingException if Reference contains no proper base64 encoded data. * @throws XMLSecurityException if the Reference does not contain a DigestValue element */ public byte[] getDigestValue() throws Base64DecodingException, XMLSecurityException { Element digestValueElem = XMLUtils.selectDsNode(this._constructionElement.getFirstChild() ,Constants._TAG_DIGESTVALUE,0); if (digestValueElem == null) { // The required element is not in the XML! Object[] exArgs ={ Constants._TAG_DIGESTVALUE, Constants.SignatureSpecNS }; throw new XMLSecurityException( "signature.Verification.NoSignatureElement", exArgs); } byte[] elemDig = Base64.decode(digestValueElem); return elemDig; } /** * Tests reference valdiation is success or false * * @return true if reference valdiation is success, otherwise false * @throws ReferenceNotInitializedException * @throws XMLSecurityException */ public boolean verify() throws ReferenceNotInitializedException, XMLSecurityException { byte[] elemDig = this.getDigestValue(); byte[] calcDig = this.calculateDigest(); boolean equal = MessageDigestAlgorithm.isEqual(elemDig, calcDig); if (!equal) { log.log(java.util.logging.Level.WARNING, "Verification failed for URI \"" + this.getURI() + "\""); } else { if (log.isLoggable(java.util.logging.Level.INFO)) log.log(java.util.logging.Level.INFO, "Verification successful for URI \"" + this.getURI() + "\""); } return equal; } /** * Method getBaseLocalName * @inheritDoc * */ public String getBaseLocalName() { return Constants._TAG_REFERENCE; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?