use of javax.xml.transform.TransformerException in project OpenAM by OpenRock.
the class AMSignatureProvider method verifyXMLSignature.
/**
* Verify the signature of a DOM Document
* @param doc a DOM Document
* @param idAttrName Attribute name for the id attribute
* @param certAlias certAlias alias for Signer's certificate, this is used
to search signer's public certificate if it is not
presented in ds:KeyInfo
* @return true if the xml signature is verified, false otherwise
* @throws XMLSignatureException if problem occurs during verification
*/
public boolean verifyXMLSignature(Document doc, java.lang.String idAttrName, java.lang.String certAlias) throws XMLSignatureException {
try {
Element nscontext = org.apache.xml.security.utils.XMLUtils.createDSctx(doc, "ds", Constants.SignatureSpecNS);
Element sigElement = (Element) XPathAPI.selectSingleNode(doc, "//ds:Signature[1]", nscontext);
Element refElement;
try {
refElement = (Element) XPathAPI.selectSingleNode(sigElement, "//ds:Reference[1]", nscontext);
} catch (TransformerException te) {
throw new XMLSignatureException(te);
}
String refUri = refElement.getAttribute("URI");
String signedId = ((Element) sigElement.getParentNode()).getAttribute(idAttrName);
if (refUri == null || signedId == null || !refUri.substring(1).equals(signedId)) {
SAMLUtilsCommon.debug.error("Signature reference ID does not match with element ID");
throw new XMLSignatureException(SAMLUtilsCommon.bundle.getString("uriNoMatchWithId"));
}
XMLSignature signature = new XMLSignature(sigElement, "");
signature.addResourceResolver(new com.sun.identity.saml.xmlsig.OfflineResolver());
doc.getDocumentElement().setIdAttribute(idAttrName, true);
KeyInfo ki = signature.getKeyInfo();
PublicKey pk = this.getX509PublicKey(doc, ki);
if (pk != null) {
// verify using public key
if (signature.checkSignatureValue(pk)) {
return true;
} else {
return false;
}
} else {
if (certAlias == null || certAlias.length() == 0) {
return false;
}
if (SAMLUtilsCommon.debug.messageEnabled()) {
SAMLUtilsCommon.debug.message("Could not find a KeyInfo, " + "try to use certAlias");
}
X509Certificate newcert = keystore.getX509Certificate(certAlias);
if (newcert != null) {
if (signature.checkSignatureValue(newcert)) {
return true;
} else {
return false;
}
} else {
PublicKey key = keystore.getPublicKey(certAlias);
if (key != null) {
if (signature.checkSignatureValue(key)) {
return true;
} else {
return false;
}
} else {
SAMLUtilsCommon.debug.error("Could not find " + "public key based on certAlias to verify" + " signature");
return false;
}
}
}
} catch (Exception ex) {
SAMLUtilsCommon.debug.error("verifyXMLSignature Exception: ", ex);
throw new XMLSignatureException(ex.getMessage());
}
}
use of javax.xml.transform.TransformerException in project OpenAM by OpenRock.
the class FMSigProvider method verify.
public boolean verify(String xmlString, String idValue, Set<X509Certificate> verificationCerts) throws SAML2Exception {
String classMethod = "FMSigProvider.verify: ";
if (xmlString == null || xmlString.length() == 0 || idValue == null || idValue.length() == 0) {
SAML2SDKUtils.debug.error(classMethod + "Either input xmlString or idValue is null.");
throw new SAML2Exception(SAML2SDKUtils.bundle.getString("nullInput"));
}
Document doc = XMLUtils.toDOMDocument(xmlString, SAML2SDKUtils.debug);
if (doc == null) {
throw new SAML2Exception(SAML2SDKUtils.bundle.getString("errorObtainingElement"));
}
Element nscontext = org.apache.xml.security.utils.XMLUtils.createDSctx(doc, "ds", Constants.SignatureSpecNS);
Element sigElement = null;
try {
sigElement = (Element) org.apache.xpath.XPathAPI.selectSingleNode(doc, "//ds:Signature[1]", nscontext);
} catch (TransformerException te) {
throw new SAML2Exception(te);
}
Element refElement;
try {
refElement = (Element) XPathAPI.selectSingleNode(doc, "//ds:Reference[1]", nscontext);
} catch (TransformerException te) {
throw new SAML2Exception(te);
}
String refUri = refElement.getAttribute("URI");
String signedId = ((Element) sigElement.getParentNode()).getAttribute(SAML2Constants.ID);
if (refUri == null || signedId == null || !refUri.substring(1).equals(signedId)) {
SAML2SDKUtils.debug.error(classMethod + "Signature reference ID does " + "not match with element ID");
throw new SAML2Exception(SAML2SDKUtils.bundle.getString("uriNoMatchWithId"));
}
doc.getDocumentElement().setIdAttribute(SAML2Constants.ID, true);
XMLSignature signature = null;
try {
signature = new XMLSignature((Element) sigElement, "");
} catch (XMLSignatureException sige) {
throw new SAML2Exception(sige);
} catch (XMLSecurityException xse) {
throw new SAML2Exception(xse);
}
signature.addResourceResolver(new com.sun.identity.saml.xmlsig.OfflineResolver());
KeyInfo ki = signature.getKeyInfo();
X509Certificate certToUse = null;
if (ki != null && ki.containsX509Data()) {
try {
certToUse = ki.getX509Certificate();
} catch (KeyResolverException kre) {
SAML2SDKUtils.debug.error(classMethod + "Could not obtain a certificate " + "from inside the document.");
certToUse = null;
}
if (certToUse != null && checkCert) {
if (!verificationCerts.contains(certToUse)) {
SAML2SDKUtils.debug.error(classMethod + "The cert contained in the document is NOT trusted");
throw new SAML2Exception(SAML2SDKUtils.bundle.getString("invalidCertificate"));
}
if (SAML2SDKUtils.debug.messageEnabled()) {
SAML2SDKUtils.debug.message(classMethod + "The cert contained in the document is trusted");
}
}
}
if (certToUse != null) {
verificationCerts = Collections.singleton(certToUse);
}
if (!isValidSignature(signature, verificationCerts)) {
SAML2SDKUtils.debug.error(classMethod + "Signature verification failed.");
return false;
}
if (SAML2SDKUtils.debug.messageEnabled()) {
SAML2SDKUtils.debug.message(classMethod + "Signature verification successful.");
}
return true;
}
use of javax.xml.transform.TransformerException in project mustangproject by ZUGFeRD.
the class ZUGFeRDExporterFromA1Factory method makePDFA3compliant.
private void makePDFA3compliant(PDDocument doc) throws IOException {
String fullProducer = producer + " (via mustangproject.org " + Version.VERSION + ")";
PDDocumentCatalog cat = doc.getDocumentCatalog();
PDMetadata metadata = new PDMetadata(doc);
cat.setMetadata(metadata);
XMPMetadata xmp = XMPMetadata.createXMPMetadata();
PDFAIdentificationSchema pdfaid = new PDFAIdentificationSchema(xmp);
xmp.addSchema(pdfaid);
DublinCoreSchema dc = xmp.createAndAddDublinCoreSchema();
dc.addCreator(creator);
XMPBasicSchema xsb = xmp.createAndAddXMPBasicSchema();
xsb.setCreatorTool(creator);
xsb.setCreateDate(GregorianCalendar.getInstance());
// PDDocumentInformation pdi=doc.getDocumentInformation();
PDDocumentInformation pdi = new PDDocumentInformation();
pdi.setProducer(fullProducer);
pdi.setAuthor(creator);
doc.setDocumentInformation(pdi);
AdobePDFSchema pdf = xmp.createAndAddAdobePDFSchema();
pdf.setProducer(fullProducer);
/*
*
* To be on the safe side, we use level B without Markinfo because we
* can not guarantee that the user correctly tagged the templates for
* the PDF.
*/
try {
//$NON-NLS-1$ //$NON-NLS-1$
pdfaid.setConformance(conformanceLevel.getLetter());
} catch (BadFieldValueException ex) {
// however the enum enforces that the conformance level is valid.
throw new Error(ex);
}
pdfaid.setPart(3);
if (attachZugferdHeaders) {
addZugferdXMP(xmp);
/*
* this is the only line where we do something
* Zugferd-specific, i.e. add PDF metadata
* specifically for Zugferd, not generically for
* a embedded file
*/
}
try {
metadata.importXMPMetadata(serializeXmpMetadata(xmp));
} catch (TransformerException e) {
throw new ZUGFeRDExportException("Could not export XmpMetadata", e);
}
}
use of javax.xml.transform.TransformerException in project voltdb by VoltDB.
the class JDBCSQLXML method init.
/**
* Initializes this object's SQLXML value from the given Source
* object. <p>
*
* @param source the Source representing the SQLXML value
* @throws SQLException if the argument does not represent a
* valid SQLXML value
*/
protected void init(Source source) throws SQLException {
if (source == null) {
throw Util.nullArgument("source");
}
Transformer transformer = JDBCSQLXML.getIdentityTransformer();
StreamResult result = new StreamResult();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzos;
try {
gzos = new GZIPOutputStream(baos);
} catch (IOException ex) {
throw Exceptions.transformFailed(ex);
}
result.setOutputStream(gzos);
try {
transformer.transform(source, result);
} catch (TransformerException ex) {
throw Exceptions.transformFailed(ex);
}
try {
gzos.close();
} catch (IOException ex) {
throw Exceptions.transformFailed(ex);
}
byte[] data = baos.toByteArray();
setGZipData(data);
setReadable(true);
setWritable(false);
}
use of javax.xml.transform.TransformerException in project voltdb by VoltDB.
the class JDBCSQLXML method createDOMSource.
/**
* Retrieves a new DOMSource for reading the XML value designated by this
* SQLXML instance. <p>
*
* @param sourceClass The class of the source
* @throws java.sql.SQLException if there is an error processing the XML
* value or if the given <tt>sourceClass</tt> is not supported.
* @return a new DOMSource for reading the XML value designated by this
* SQLXML instance
*/
@SuppressWarnings("unchecked")
protected <T extends Source> T createDOMSource(Class<T> sourceClass) throws SQLException {
DOMSource source = null;
try {
source = (sourceClass == null) ? new DOMSource() : (DOMSource) sourceClass.newInstance();
} catch (SecurityException ex) {
throw Exceptions.sourceInstantiation(ex);
} catch (IllegalAccessException ex) {
throw Exceptions.sourceInstantiation(ex);
} catch (InstantiationException ex) {
throw Exceptions.sourceInstantiation(ex);
} catch (ClassCastException ex) {
throw Exceptions.sourceInstantiation(ex);
}
Transformer transformer = JDBCSQLXML.getIdentityTransformer();
InputStream inputStream = this.getBinaryStreamImpl();
StreamSource streamSource = new StreamSource();
DOMResult domResult = new DOMResult();
streamSource.setInputStream(inputStream);
try {
transformer.transform(streamSource, domResult);
} catch (TransformerException ex) {
throw Exceptions.transformFailed(ex);
}
source.setNode(domResult.getNode());
return (T) source;
}
Aggregations