本文整理汇总了Java中javax.xml.crypto.dsig.Reference.validate方法的典型用法代码示例。如果您正苦于以下问题:Java Reference.validate方法的具体用法?Java Reference.validate怎么用?Java Reference.validate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.crypto.dsig.Reference
的用法示例。
在下文中一共展示了Reference.validate方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: validate
import javax.xml.crypto.dsig.Reference; //导入方法依赖的package包/类
public synchronized boolean validate()
throws MarshalException,
XMLSignatureException {
// Find Signature element.
NodeList list = document.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
if (list.getLength() == 0) {
throw new RuntimeException("Cannot find Signature element");
}
// Create a DOMValidateContext and specify a KeySelector
// and document context.
DOMValidateContext validateContext = new DOMValidateContext(new X509CertificateKeySelector(), list.item(0));
// Unmarshal the XMLSignature.
XMLSignature signature = this.signatureFactory.unmarshalXMLSignature(validateContext);
// Validate the XMLSignature.
if (signature.validate(validateContext)) {
return true;
} else {
Iterator<?> i = signature.getSignedInfo().getReferences().iterator();
for (int j = 0; i.hasNext(); j++) {
System.out.print("ref[" + j + "] -> ");
Reference ref = (Reference) i.next();
System.out.print(ref.getURI());
System.out.print(", ");
System.out.print(ref.getDigestMethod().toString());
System.out.print(", ");
System.out.print(ref.getId());
boolean refValid = ref.validate(validateContext);
System.out.print(", validity status: " + refValid + "\r\n");
}
return false;
}
}
示例2: verifySignature
import javax.xml.crypto.dsig.Reference; //导入方法依赖的package包/类
public boolean verifySignature( Document doc )
throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException,
MarshalException, XMLSignatureException
{
// doc.normalize();
// doc.normalizeDocument();
// LOG.debug("Verifying Document:\n" + domToString(doc));
Element envelope = doc.getDocumentElement();
List<Element> headerList = findChildren( envelope, "Header", envelope.getNamespaceURI() );
if ( headerList.size() != 1 )
{
LOG.warn( "Could not find SOAP Header" );
return false;
}
Element header = headerList.get( 0 );
List<Element> securityList = findChildren( header, "Security", URI_NS_WSSE_1_0 );
if ( securityList.size() != 1 )
{
LOG.warn( "Found " + securityList.size() + " WS Security Headers" );
return false;
}
Element security = securityList.get( 0 );
List<Element> signatureList = findChildren( security, "Signature", XMLSignature.XMLNS );
if ( signatureList.size() != 1 )
{
LOG.warn( "There are " + signatureList.size() + " Signature Elements" );
return false;
}
Element signature = signatureList.get( 0 );
// keypair
DOMValidateContext valContext = new DOMValidateContext( new KeyValueKeySelector(), signature );
// valContext.setURIDereferencer(valContext.getURIDereferencer());
markWsuIdAttributes( doc );
XMLSignatureFactory fac = XMLSignatureFactory.getInstance();
XMLSignature xmlSignature = fac.unmarshalXMLSignature( valContext );
boolean allvalid = xmlSignature.validate( valContext );
if ( !allvalid )
{
@SuppressWarnings( "unchecked" )
Iterator<Reference> itRef = xmlSignature.getSignedInfo().getReferences().iterator();
for ( int j = 0; itRef.hasNext(); j++ )
{ // j is only used if we
// calculate the Reference
// Digest, which is
// out-commmented for
// performance issues...
Reference r = itRef.next();
// These Lines are usefull for debugging, but take
// performance...
// **************************************************************
boolean refValid = r.validate( valContext );
LOG.debug( "ref[" + j + "] validity status: " + refValid );
LOG.debug( printBytes( r.getCalculatedDigestValue() ) + "(Calculated)" );
LOG.debug( printBytes( r.getDigestValue() ) + "(Saved value)" );
LOG.debug( "Type: " + r.getType() + " / URI: " + r.getURI() );
}
// These Lines are usefull for debugging, but take performance...
// **************************************************************
boolean sv = xmlSignature.getSignatureValue().validate( valContext );
LOG.info( "signature validation status: " + sv );
}
// Policy is OK, now validating
LOG.info( " ==> Signature is " + ( allvalid ? "VALID" : "invalid" ) );
return xmlSignature.validate( valContext );
}
示例3: validateSignature
import javax.xml.crypto.dsig.Reference; //导入方法依赖的package包/类
@Nonnull
public static XMLDSigValidationResult validateSignature (@Nonnull final Document aDoc,
@Nonnull final KeySelector aKeySelector) throws XMLSignatureException
{
ValueEnforcer.notNull (aDoc, "Document");
ValueEnforcer.notNull (aKeySelector, "KeySelector");
// Find Signature element.
final NodeList aSignatureNL = aDoc.getElementsByTagNameNS (XMLSignature.XMLNS, XMLDSigSetup.ELEMENT_SIGNATURE);
if (aSignatureNL.getLength () != 1)
throw new IllegalArgumentException ("Cannot find exactly one Signature element");
final Element aSignatureElement = (Element) aSignatureNL.item (0);
// Create a DOM XMLSignatureFactory that will be used to validate the
// enveloped signature.
final XMLSignatureFactory aSignatureFactory = XMLDSigSetup.getXMLSignatureFactory ();
// Create a DOMValidateContext and specify a KeySelector
// and document context.
final DOMValidateContext aValidationContext = new DOMValidateContext (aKeySelector, aSignatureElement);
// aValidationContext.setProperty
// ("org.jcp.xml.dsig.internal.dom.SignatureProvider", new XMLDSigRI ());
// Unmarshal the XMLSignature.
XMLSignature aSignature;
try
{
aSignature = aSignatureFactory.unmarshalXMLSignature (aValidationContext);
}
catch (final MarshalException ex)
{
s_aLogger.error ("Failed to read XML signature: " + ex.getClass ().getName () + " - " + ex.getMessage ());
return XMLDSigValidationResult.createSignatureError ();
}
// Validate the XMLSignature.
if (aSignature.validate (aValidationContext))
return XMLDSigValidationResult.createSuccess ();
// Core validation failed. Check the signature value.
if (!aSignature.getSignatureValue ().validate (aValidationContext))
return XMLDSigValidationResult.createSignatureError ();
// Check the validation status of each Reference.
final ICommonsList <Integer> aInvalidReferences = new CommonsArrayList <> ();
final Iterator <?> it = aSignature.getSignedInfo ().getReferences ().iterator ();
for (int nIndex = 0; it.hasNext (); nIndex++)
{
final Reference aReference = (Reference) it.next ();
if (!aReference.validate (aValidationContext))
{
aInvalidReferences.add (Integer.valueOf (nIndex));
}
}
return XMLDSigValidationResult.createReferenceErrors (aInvalidReferences);
}
示例4: verifySignature
import javax.xml.crypto.dsig.Reference; //导入方法依赖的package包/类
private boolean verifySignature(Element element, PublicKey validatingKey, AtomicReference<String> OutReadableErrorMessage) {
if (OutReadableErrorMessage == null) {
OutReadableErrorMessage = new AtomicReference<String>();
}
XMLSignatureFactory fac = initXMLSigFactory();
NodeList nl = element.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
if (nl.getLength() == 0) {
throw new RuntimeException("Cannot find Signature element");
}
DOMValidateContext valContext = new DOMValidateContext(validatingKey, nl.item(0));
try {
valContext.setProperty("javax.xml.crypto.dsig.cacheReference", Boolean.TRUE);
XMLSignature signature = fac.unmarshalXMLSignature(valContext);
boolean coreValidity = signature.validate(valContext);
// Check core validation status.
if (coreValidity == false) {
logger.warn("Signature failed core validation");
boolean sv = signature.getSignatureValue().validate(valContext);
logger.debug("signature validation status: " + sv);
OutReadableErrorMessage.set("signature validation failed: " + sv + "." + OutReadableErrorMessage.get());
// Check the validation status of each Reference.
@SuppressWarnings("unchecked")
Iterator<Reference> i = signature.getSignedInfo().getReferences().iterator();
//System.out.println("---------------------------------------------");
for (int j = 0; i.hasNext(); j++) {
Reference ref = (Reference) i.next();
boolean refValid = ref.validate(valContext);
logger.debug(j);
logger.debug("ref[" + j + "] validity status: " + refValid);
if (!refValid) {
OutReadableErrorMessage.set("signature reference " + j + " invalid. " + OutReadableErrorMessage.get());
}
logger.debug("Ref type: " + ref.getType() + ", URI: " + ref.getURI());
for (Object xform : ref.getTransforms()) {
logger.debug("Transform: " + xform);
}
String calcDigValStr = digestToString(ref.getCalculatedDigestValue());
String expectedDigValStr = digestToString(ref.getDigestValue());
logger.warn(" Calc Digest: " + calcDigValStr);
logger.warn("Expected Digest: " + expectedDigValStr);
if (!calcDigValStr.equalsIgnoreCase(expectedDigValStr)) {
OutReadableErrorMessage.set("digest mismatch for signature ref " + j + "." + OutReadableErrorMessage.get());
}
}
} else {
logger.info("Signature passed core validation");
}
return coreValidity;
} catch (Exception e) {
OutReadableErrorMessage.set("signature validation failed: " + e.getMessage() + OutReadableErrorMessage.get());
logger.fatal(e);
return false;
}
}
示例5: verifySignature
import javax.xml.crypto.dsig.Reference; //导入方法依赖的package包/类
public static boolean verifySignature(Element element, PublicKey validatingKey) {
XMLSignatureFactory fac = initXMLSigFactory();
NodeList nl = element.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
if (nl.getLength() == 0) {
throw new RuntimeException("Cannot find Signature element");
}
DOMValidateContext valContext = new DOMValidateContext(validatingKey, nl.item(0));
try {
valContext.setProperty("javax.xml.crypto.dsig.cacheReference", Boolean.TRUE);
XMLSignature signature = fac.unmarshalXMLSignature(valContext);
boolean coreValidity = signature.validate(valContext);
// Check core validation status.
if (coreValidity == false) {
System.err.println("Signature failed core validation");
boolean sv = signature.getSignatureValue().validate(valContext);
System.out.println("signature validation status: " + sv);
// Check the validation status of each Reference.
@SuppressWarnings("unchecked")
Iterator<Reference> i = signature.getSignedInfo().getReferences().iterator();
System.out.println("---------------------------------------------");
for (int j = 0; i.hasNext(); j++) {
Reference ref = (Reference) i.next();
boolean refValid = ref.validate(valContext);
System.out.println("ref[" + j + "] validity status: " + refValid);
System.out.println("Ref type: " + ref.getType() + ", URI: " + ref.getURI());
for (Object xform : ref.getTransforms()) {
System.out.println("Transform: " + xform);
}
String calcDigValStr = digestToString(ref.getCalculatedDigestValue());
String expectedDigValStr = digestToString(ref.getDigestValue());
System.out.println(" Calc Digest: " + calcDigValStr);
System.out.println("Expected Digest: " + expectedDigValStr);
InputStream is = ref.getDigestInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
is.close();
System.out.println("---------------------------------------------");
}
} else {
System.out.println("Signature passed core validation");
}
return coreValidity;
} catch (Exception e) {
throw new RuntimeException(e);
}
}