本文整理汇总了Java中javax.xml.crypto.dsig.dom.DOMValidateContext类的典型用法代码示例。如果您正苦于以下问题:Java DOMValidateContext类的具体用法?Java DOMValidateContext怎么用?Java DOMValidateContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DOMValidateContext类属于javax.xml.crypto.dsig.dom包,在下文中一共展示了DOMValidateContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: validate
import javax.xml.crypto.dsig.dom.DOMValidateContext; //导入依赖的package包/类
public Result validate ( final Document doc ) throws Exception
{
final NodeList nl = doc.getElementsByTagNameNS ( XMLSignature.XMLNS, "Signature" ); //$NON-NLS-1$
if ( nl.getLength () == 0 )
{
return new Result ( StatusCodes.VALIDATE_NO_SIGNATURE_DATA, "No signature data found" );
}
final DOMValidateContext dvc = new DOMValidateContext ( this.keySelector, nl.item ( 0 ) );
final XMLSignature signature = this.factory.unmarshalXMLSignature ( dvc );
try
{
final boolean result = signature.validate ( dvc );
return new Result ( result, signature );
}
catch ( final XMLSignatureException e )
{
logger.debug ( "Failed to perform validation", e );
return Result.INVALID;
}
}
示例2: testCreateDSA2048Signature
import javax.xml.crypto.dsig.dom.DOMValidateContext; //导入依赖的package包/类
@org.junit.Test
public void testCreateDSA2048Signature() throws Exception {
// check if SHA256withDSA is supported
boolean gotSHA256withDSA = false;
try {
Signature.getInstance("SHA256withDSA");
gotSHA256withDSA = true;
} catch (NoSuchAlgorithmException e) {}
org.junit.Assume.assumeTrue(gotSHA256withDSA);
SignatureMethod sm = fac.newSignatureMethod(DSA_SHA256, null);
SignedInfo si = createSignedInfo(sm);
KeyInfo ki = kifac.newKeyInfo(Collections.singletonList
(kifac.newKeyValue((PublicKey)TestUtils.getPublicKey("DSA", 2048))));
XMLSignature sig = fac.newXMLSignature(si, ki, objs, id, sigValueId);
Document doc = TestUtils.newDocument();
XMLSignContext signContext =
new DOMSignContext(TestUtils.getPrivateKey("DSA", 2048), doc);
signContext.setURIDereferencer(ud);
sig.sign(signContext);
XMLValidateContext validateContext = new DOMValidateContext
(TestUtils.getPublicKey("DSA", 2048), doc.getDocumentElement());
validateContext.setURIDereferencer(ud);
assertTrue(sig.validate(validateContext));
}
示例3: validate
import javax.xml.crypto.dsig.dom.DOMValidateContext; //导入依赖的package包/类
public boolean validate(DOMValidateContext vc) throws Exception {
XMLSignatureFactory factory = XMLSignatureFactory.getInstance
("DOM", new org.apache.jcp.xml.dsig.internal.dom.XMLDSigRI());
XMLSignature signature = factory.unmarshalXMLSignature(vc);
boolean coreValidity = signature.validate(vc);
// Check core validation status
if (coreValidity == false) {
// check the validation status of each Reference
@SuppressWarnings("unchecked")
Iterator<Reference> i = signature.getSignedInfo().getReferences().iterator();
while (i.hasNext()) {
Reference reference = i.next();
reference.validate(vc);
}
}
return coreValidity;
}
示例4: getXMLValidateContext
import javax.xml.crypto.dsig.dom.DOMValidateContext; //导入依赖的package包/类
public static XMLValidateContext getXMLValidateContext(String type,
File input,
String tag)
throws Exception {
if (type.equalsIgnoreCase("dom")) {
DocumentBuilder docBuilder = XMLUtils.createDocumentBuilder(false, false);
Document doc = docBuilder.parse(input);
if (tag == null) {
return new DOMValidateContext
(TestUtils.getPublicKey("RSA", 512),
doc.getDocumentElement());
} else {
NodeList list = doc.getElementsByTagName(tag);
return new DOMValidateContext
(TestUtils.getPublicKey("RSA", 512), list.item(0));
}
} else {
throw new Exception("Unsupported XMLValidateContext type: " + type);
}
}
示例5: testLocalFilesystem
import javax.xml.crypto.dsig.dom.DOMValidateContext; //导入依赖的package包/类
@org.junit.Test
public void testLocalFilesystem() throws Exception {
String file = "signature-external-c14n-xmlatrs.xml";
DOMValidateContext vc =
validator.getValidateContext(
file, new KeySelectors.SecretKeySelector("secret".getBytes("ASCII"))
);
vc.setProperty("org.apache.jcp.xml.dsig.secureValidation", Boolean.FALSE);
boolean coreValidity = validator.validate(vc);
assertTrue("Signature failed core validation", coreValidity);
vc.setProperty("org.apache.jcp.xml.dsig.secureValidation", Boolean.TRUE);
try {
validator.validate(vc);
fail("Failure expected when secure validation is enabled");
} catch (XMLSignatureException ex) {
assertTrue(ex.getMessage().contains("URIReferenceException"));
}
}
示例6: explainValidationProblem
import javax.xml.crypto.dsig.dom.DOMValidateContext; //导入依赖的package包/类
private static String explainValidationProblem(
DOMValidateContext context, XMLSignature signature)
throws XMLSignatureException {
@SuppressWarnings("unchecked") // Safe by specification.
List<Reference> references = signature.getSignedInfo().getReferences();
StringBuilder builder = new StringBuilder();
builder.append("Signature failed core validation\n");
boolean sv = signature.getSignatureValue().validate(context);
builder.append("Signature validation status: " + sv + "\n");
for (Reference ref : references) {
builder.append("references[");
builder.append(ref.getURI());
builder.append("] validity status: ");
builder.append(ref.validate(context));
builder.append("\n");
}
return builder.toString();
}
示例7: verifySignature
import javax.xml.crypto.dsig.dom.DOMValidateContext; //导入依赖的package包/类
/**
* Verification via the default JSR105 implementation triggers some
* canonicalization errors.
*
* @param odfUrl
* @param signatureNode
* @throws MarshalException
* @throws XMLSignatureException
*/
private boolean verifySignature(URL odfUrl, Node signatureNode) throws MarshalException, XMLSignatureException {
// work-around for Java 7
Element signedPropertiesElement = (Element) ((Element) signatureNode)
.getElementsByTagNameNS(XAdESXLSignatureFacet.XADES_NAMESPACE, "SignedProperties").item(0);
if (null != signedPropertiesElement) {
signedPropertiesElement.setIdAttribute("Id", true);
}
DOMValidateContext domValidateContext = new DOMValidateContext(new KeyInfoKeySelector(), signatureNode);
ODFURIDereferencer dereferencer = new ODFURIDereferencer(odfUrl);
domValidateContext.setURIDereferencer(dereferencer);
XMLSignatureFactory xmlSignatureFactory = XMLSignatureFactory.getInstance();
LOG.debug("java version: " + System.getProperty("java.version"));
/*
* Requires Java 6u10 because of a bug. See also:
* http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6696582
*/
XMLSignature xmlSignature = xmlSignatureFactory.unmarshalXMLSignature(domValidateContext);
boolean validity = xmlSignature.validate(domValidateContext);
return validity;
}
示例8: validate_error
import javax.xml.crypto.dsig.dom.DOMValidateContext; //导入依赖的package包/类
@Test(expected = DigitalSignatureValidationException.class)
public void validate_error() throws Exception {
// given
FileInputStream in = null;
Document document = null;
try {
in = new FileInputStream(FILE_OPENAM_RESPONSE);
document = XMLConverter.convertToDocument(in);
} finally {
if (in != null) {
in.close();
}
}
NodeList nl = document.getElementsByTagNameNS(XMLSignature.XMLNS,
"Signature");
doThrow(new XMLSignatureException("")).when(validator)
.workaroundOpenamBug(any(XMLSignature.class),
any(DOMValidateContext.class), anyBoolean());
// when
validator.validate(nl.item(0));
// then exception expected
}
示例9: validSignature
import javax.xml.crypto.dsig.dom.DOMValidateContext; //导入依赖的package包/类
/**
* Check the xmldsig signature of the XML document.
*
* @param document the document to test
* @param publicKey the public key corresponding to the key pair the document was signed with
* @return true if a correct signature is present, false otherwise
*/
public static boolean validSignature(Document document, Key publicKey) {
Node signatureNode = document.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature").item(0);
KeySelector keySelector = KeySelector.singletonKeySelector(publicKey);
try {
String providerName =
System.getProperty("jsr105Provider", "org.jcp.xml.dsig.internal.dom.XMLDSigRI");
XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM",
(Provider) Class.forName(providerName).newInstance());
DOMValidateContext valContext = new DOMValidateContext(keySelector, signatureNode);
XMLSignature signature = fac.unmarshalXMLSignature(valContext);
return signature.validate(valContext);
} catch (Exception e) {
logger.warn("Error validating an XML signature.", e);
return false;
}
}
示例10: isValid
import javax.xml.crypto.dsig.dom.DOMValidateContext; //导入依赖的package包/类
public boolean isValid() throws Exception {
NodeList nodes = xmlDoc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
if (nodes == null || nodes.getLength() == 0) {
throw new Exception("Can't find signature in document.");
}
if (setIdAttributeExists()) {
tagIdAttributes(xmlDoc);
}
X509Certificate cert = samlSettings.getCertificate();
DOMValidateContext ctx = new DOMValidateContext(cert.getPublicKey(), nodes.item(0));
XMLSignatureFactory sigF = XMLSignatureFactory.getInstance("DOM");
XMLSignature xmlSignature = sigF.unmarshalXMLSignature(ctx);
return xmlSignature.validate(ctx);
}
示例11: isValida
import javax.xml.crypto.dsig.dom.DOMValidateContext; //导入依赖的package包/类
public boolean isValida(final InputStream xmlStream) throws Exception {
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
final Document document = dbf.newDocumentBuilder().parse(xmlStream);
final NodeList nodeList = document.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
if (nodeList.getLength() == 0) {
throw new IllegalStateException("Nao foi encontrada a assinatura do XML.");
}
final String providerName = System.getProperty("jsr105Provider", "org.jcp.xml.dsig.internal.dom.XMLDSigRI");
final XMLSignatureFactory signatureFactory = XMLSignatureFactory.getInstance("DOM", (Provider) Class.forName(providerName).newInstance());
final DOMValidateContext validateContext = new DOMValidateContext(new X509KeySelector(), nodeList.item(0));
for (final String tag : AssinaturaDigital.ELEMENTOS_ASSINAVEIS) {
final NodeList elements = document.getElementsByTagName(tag);
if (elements.getLength() > 0) {
validateContext.setIdAttributeNS((Element) elements.item(0), null, "Id");
}
}
return signatureFactory.unmarshalXMLSignature(validateContext).validate(validateContext);
}
示例12: validate
import javax.xml.crypto.dsig.dom.DOMValidateContext; //导入依赖的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;
}
}
示例13: verifySignature
import javax.xml.crypto.dsig.dom.DOMValidateContext; //导入依赖的package包/类
public static boolean verifySignature(Document doc , X509Certificate cert) {
try{
if (doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature").getLength() == 0)
throw new Exception("Cannot find Signature element");
DOMValidateContext valContext = new DOMValidateContext(cert.getPublicKey(), doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature").item(0));
XMLSignature signature = XMLSignatureFactory.getInstance("DOM").unmarshalXMLSignature(valContext);
return signature.validate(valContext);
}catch(Exception e){e.printStackTrace();}
return false;
}
示例14: validate
import javax.xml.crypto.dsig.dom.DOMValidateContext; //导入依赖的package包/类
private boolean validate(final DOMValidateContext validationContext)
throws DigitalSignatureValidationException {
try {
// if (getLogger().isDebugLoggingEnabled()) {
// enableReferenceCaching(validationContext);
// }
XMLSignatureFactory factory = XMLSignatureFactory
.getInstance(XML_MECHANISM_TYPE);
XMLSignature signature = factory
.unmarshalXMLSignature(validationContext);
boolean validationResult = signature.validate(validationContext);
validationResult = workaroundOpenamBug(signature,
validationContext, validationResult);
// if (getLogger().isDebugLoggingEnabled()) {
// debugLogReferences(signature, validationContext);
// }
return validationResult;
} catch (XMLSignatureException | MarshalException exception) {
throw new DigitalSignatureValidationException(
"Error occurred during digital signature validation process",
DigitalSignatureValidationException.ReasonEnum.EXCEPTION_OCCURRED,
exception);
}
}
示例15: workaroundOpenamBug
import javax.xml.crypto.dsig.dom.DOMValidateContext; //导入依赖的package包/类
/**
* The overall signature validation consists of two steps, one is the
* validation of the signature itself and the other the validation of the
* references digest values. Because of a canonicalization bug in openam,
* which is not yet registered, the second verification cannot be done.
*
* @return true if the signature validation has not failed, even if the
* reference validation failed.
*/
boolean workaroundOpenamBug(XMLSignature signature,
DOMValidateContext validationContext, boolean validationResult)
throws XMLSignatureException {
if (!validationResult) {
if (signature.getSignatureValue().validate(validationContext)) {
return true;
}
}
return validationResult;
}