本文整理匯總了Java中javax.xml.crypto.dsig.XMLSignature類的典型用法代碼示例。如果您正苦於以下問題:Java XMLSignature類的具體用法?Java XMLSignature怎麽用?Java XMLSignature使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
XMLSignature類屬於javax.xml.crypto.dsig包,在下文中一共展示了XMLSignature類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: validate
import javax.xml.crypto.dsig.XMLSignature; //導入依賴的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: newKeySelector_keyinfoEmpty
import javax.xml.crypto.dsig.XMLSignature; //導入依賴的package包/類
@Test
public void newKeySelector_keyinfoEmpty() throws Exception {
// given
String response = Strings
.textFileToString("javares/openamResponse.xml");
response = response.replaceAll(System.lineSeparator(), "").replaceAll(
"<ds:KeyInfo>.*</ds:KeyInfo>", "<ds:KeyInfo></ds:KeyInfo>");
Document document = XMLConverter.convertToDocument(response, true);
NodeList nl = document.getElementsByTagNameNS(XMLSignature.XMLNS,
"Signature");
// when
try {
factory.newKeySelector(nl.item(0));
fail();
} catch (DigitalSignatureValidationException e) {
assertTrue(e.getMessage().contains(
"Only RSA/DSA KeyValue and are X509Data supported"));
}
}
示例3: compare
import javax.xml.crypto.dsig.XMLSignature; //導入依賴的package包/類
public void compare ( final Document sourceDoc, final Document signedDoc ) throws Exception
{
final Document d1 = cloneDoc ( sourceDoc );
final Document d2 = cloneDoc ( signedDoc );
final NodeList nl = d2.getElementsByTagNameNS ( XMLSignature.XMLNS, "Signature" );
while ( nl.getLength () > 0 )
{
final Node item = nl.item ( 0 );
item.getParentNode ().removeChild ( item );
}
d1.normalizeDocument ();
d2.normalizeDocument ();
final Element root1 = d1.getDocumentElement ();
final Element root2 = d2.getDocumentElement ();
compareNode ( root1, root2 );
}
示例4: newKeySelector_keyinfoMissing
import javax.xml.crypto.dsig.XMLSignature; //導入依賴的package包/類
@Test
public void newKeySelector_keyinfoMissing() throws Exception {
// given
String response = Strings
.textFileToString("javares/openamResponse.xml");
response = response.replaceAll(System.lineSeparator(), "").replaceAll(
"<ds:KeyInfo>.*</ds:KeyInfo>", "");
Document document = XMLConverter.convertToDocument(response, true);
NodeList nl = document.getElementsByTagNameNS(XMLSignature.XMLNS,
"Signature");
try {
// when
factory.newKeySelector(nl.item(0));
fail();
} catch (DigitalSignatureValidationException e) {
// then
assertTrue(e.getMessage().contains(
"No KeyInfo element found in SAML assertion"));
}
}
示例5: newKeySelector_keyValue
import javax.xml.crypto.dsig.XMLSignature; //導入依賴的package包/類
@Test
public void newKeySelector_keyValue() throws Exception {
// given
String response = Strings
.textFileToString("javares/openamResponse.xml");
Document document = XMLConverter.convertToDocument(
replaceX509WithKeyValueData(response), true);
NodeList nl = document.getElementsByTagNameNS(XMLSignature.XMLNS,
"Signature");
// when
KeySelector keySelector = factory.newKeySelector(nl.item(0));
// then
assertTrue(keySelector instanceof KeyValueKeySelector);
}
示例6: newKeySelector_firstFound
import javax.xml.crypto.dsig.XMLSignature; //導入依賴的package包/類
@Test
public void newKeySelector_firstFound() throws Exception {
// given
String response = Strings
.textFileToString("javares/openamResponse.xml");
Document document = XMLConverter.convertToDocument(
addKeyValueAfterX509Data(response), true);
NodeList nl = document.getElementsByTagNameNS(XMLSignature.XMLNS,
"Signature");
// when
KeySelector keySelector = factory.newKeySelector(nl.item(0));
// then
assertTrue(keySelector instanceof X509KeySelector);
}
示例7: marshal
import javax.xml.crypto.dsig.XMLSignature; //導入依賴的package包/類
/**
* This method invokes the {@link #marshalParams marshalParams}
* method to marshal any algorithm-specific parameters.
*/
public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
throws MarshalException
{
Document ownerDoc = DOMUtils.getOwnerDocument(parent);
Element smElem = DOMUtils.createElement(ownerDoc, "SignatureMethod",
XMLSignature.XMLNS, dsPrefix);
DOMUtils.setAttribute(smElem, "Algorithm", getAlgorithm());
if (getParameterSpec() != null) {
marshalParams(smElem, dsPrefix);
}
parent.appendChild(smElem);
}
示例8: marshalPGPData
import javax.xml.crypto.dsig.XMLSignature; //導入依賴的package包/類
/**
* Marshals a {@link PGPData}
*
* @param xwriter
* @param pgpData
* @param dsPrefix
* @param context
* @throws MarshalException
*/
public static void marshalPGPData(XmlWriter xwriter, PGPData pgpData, String dsPrefix, XMLCryptoContext context)
throws MarshalException {
xwriter.writeStartElement(dsPrefix, "PGPData", XMLSignature.XMLNS);
// create and append PGPKeyID element
byte[] keyId = pgpData.getKeyId();
if (keyId != null) {
xwriter.writeTextElement(dsPrefix, "PGPKeyID", XMLSignature.XMLNS, Base64.encode(keyId));
}
// create and append PGPKeyPacket element
byte[] keyPacket = pgpData.getKeyPacket();
if (keyPacket != null) {
xwriter.writeTextElement(dsPrefix, "XMLSignature.XMLNS", XMLSignature.XMLNS, Base64.encode(keyPacket));
}
// create and append any elements
@SuppressWarnings("unchecked")
List<XMLStructure> externalElements = pgpData.getExternalElements();
for (XMLStructure externalItem : externalElements) {
xwriter.marshalStructure(externalItem, dsPrefix, context);
}
xwriter.writeEndElement(); // "PGPData"
}
示例9: marshal
import javax.xml.crypto.dsig.XMLSignature; //導入依賴的package包/類
@Override
public void marshal(XmlWriter xwriter, String dsPrefix, XMLCryptoContext context)
throws MarshalException
{
xwriter.writeStartElement(dsPrefix, "RetrievalMethod", XMLSignature.XMLNS);
// TODO - see whether it is important to capture the "here" attribute as part of the
// marshalling - do any of the tests fail?
// add URI and Type attributes
here = xwriter.writeAttribute("", "", "URI", uri);
xwriter.writeAttribute("", "", "Type", type);
// add Transforms elements
if (!transforms.isEmpty()) {
xwriter.writeStartElement(dsPrefix, "Transforms", XMLSignature.XMLNS);
for (Transform transform : transforms) {
((DOMTransform)transform).marshal(xwriter, dsPrefix, context);
}
xwriter.writeEndElement(); // "Transforms"
}
xwriter.writeEndElement(); // "RetrievalMethod"
}
示例10: dereferenceAsXMLStructure
import javax.xml.crypto.dsig.XMLSignature; //導入依賴的package包/類
public XMLStructure dereferenceAsXMLStructure(XMLCryptoContext context)
throws URIReferenceException
{
DocumentBuilder db = null;
boolean secVal = Utils.secureValidation(context);
try {
ApacheData data = (ApacheData)dereference(context);
db = XMLUtils.createDocumentBuilder(false, secVal);
Document doc = db.parse(new ByteArrayInputStream
(data.getXMLSignatureInput().getBytes()));
Element kiElem = doc.getDocumentElement();
if (kiElem.getLocalName().equals("X509Data")
&& XMLSignature.XMLNS.equals(kiElem.getNamespaceURI())) {
return new DOMX509Data(kiElem);
} else {
return null; // unsupported
}
} catch (Exception e) {
throw new URIReferenceException(e);
} finally {
if (db != null) {
XMLUtils.repoolDocumentBuilder(db);
}
}
}
示例11: marshalInternal
import javax.xml.crypto.dsig.XMLSignature; //導入依賴的package包/類
private static void marshalInternal(XmlWriter xwriter, KeyInfo ki,
String dsPrefix, XMLCryptoContext context, boolean declareNamespace) throws MarshalException {
xwriter.writeStartElement(dsPrefix, "KeyInfo", XMLSignature.XMLNS);
if (declareNamespace) {
xwriter.writeNamespace(dsPrefix, XMLSignature.XMLNS);
}
xwriter.writeIdAttribute("", "", "Id", ki.getId());
// create and append KeyInfoType elements
List<XMLStructure> keyInfoTypes = getContent(ki);
for (XMLStructure kiType : keyInfoTypes) {
xwriter.marshalStructure(kiType, dsPrefix, context);
}
xwriter.writeEndElement(); // "KeyInfo"
}
示例12: isSignatureGlobal
import javax.xml.crypto.dsig.XMLSignature; //導入依賴的package包/類
@SuppressWarnings("rawtypes")
private static boolean isSignatureGlobal(XMLSignature signature, String rootID) {
LOG.debug("Starting signature globality check...");
LOG.debug("Signature: {}", signature);
LOG.debug("Root ID: {}", rootID);
boolean isGlobal = false;
// We check each Reference. One must be the rootId or be ""
String refRootID = "#" + rootID;
for (Object o : signature.getSignedInfo().getReferences()) {
String uri = ((Reference) o).getURI();
if ("".equals(uri) || refRootID.equals(uri)) {
isGlobal = true;
break;
}
}
LOG.debug("Signature globality check result: {}", isGlobal);
return isGlobal;
}
示例13: explainValidationProblem
import javax.xml.crypto.dsig.XMLSignature; //導入依賴的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();
}
示例14: removeSignatureElements
import javax.xml.crypto.dsig.XMLSignature; //導入依賴的package包/類
/**
* Removes the Signature elements from the document.
*
* @param doc
* document
*/
protected void removeSignatureElements(Node node) {
Document doc = XmlSignatureHelper.getDocument(node);
NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
List<Node> nodesToBeRemoved = new ArrayList<Node>(nl.getLength());
for (int i = 0; i < nl.getLength(); i++) {
// you cannot remove the nodes within this loop, because nl list would change
nodesToBeRemoved.add(nl.item(i));
}
for (Node n : nodesToBeRemoved) {
Node parent = n.getParentNode();
if (parent != null) {
parent.removeChild(n);
}
}
}
示例15: replacePrefix
import javax.xml.crypto.dsig.XMLSignature; //導入依賴的package包/類
protected void replacePrefix(Element el, Input input) {
replacePrefixForNode(el, input);
NamedNodeMap nnm = el.getAttributes();
List<Attr> xmlnsToBeRemoved = new ArrayList<Attr>(2);
int length = nnm.getLength();
for (int i = 0; i < length; i++) {
Node attr = nnm.item(i);
replacePrefixForNode(attr, input);
if (attr.getNodeType() == Node.ATTRIBUTE_NODE) {
if ("xmlns".equals(attr.getLocalName()) || "xmlns".equals(attr.getPrefix())) {
if (XMLSignature.XMLNS.equals(attr.getTextContent()) || findNamespace(input.getMessage()).equals(attr.getTextContent())) {
xmlnsToBeRemoved.add((Attr) attr);
}
}
}
}
// remove xml namespace declaration for XML signature and XAdES namespace
for (Attr toBeRemoved : xmlnsToBeRemoved) {
el.removeAttributeNode(toBeRemoved);
}
}