use of com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput in project jdk8u_jdk by JetBrains.
the class RetrievalMethodResolver method resolveInput.
/**
* Resolves the input from the given retrieval method
* @return
* @throws XMLSecurityException
*/
private static XMLSignatureInput resolveInput(RetrievalMethod rm, String baseURI, boolean secureValidation) throws XMLSecurityException {
Attr uri = rm.getURIAttr();
// Apply the transforms
Transforms transforms = rm.getTransforms();
ResourceResolver resRes = ResourceResolver.getInstance(uri, baseURI, secureValidation);
XMLSignatureInput resource = resRes.resolve(uri, baseURI, secureValidation);
if (transforms != null) {
if (log.isLoggable(java.util.logging.Level.FINE)) {
log.log(java.util.logging.Level.FINE, "We have Transforms");
}
resource = transforms.performTransforms(resource);
}
return resource;
}
use of com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput in project jdk8u_jdk by JetBrains.
the class RetrievalMethodResolver method engineLookupResolveX509Certificate.
/**
* Method engineResolveX509Certificate
* @inheritDoc
* @param element
* @param baseURI
* @param storage
*/
public X509Certificate engineLookupResolveX509Certificate(Element element, String baseURI, StorageResolver storage) {
if (!XMLUtils.elementIsInSignatureSpace(element, Constants._TAG_RETRIEVALMETHOD)) {
return null;
}
try {
RetrievalMethod rm = new RetrievalMethod(element, baseURI);
String type = rm.getType();
XMLSignatureInput resource = resolveInput(rm, baseURI, secureValidation);
if (RetrievalMethod.TYPE_RAWX509.equals(type)) {
return getRawCertificate(resource);
}
Element e = obtainReferenceElement(resource);
// which points to this element
if (XMLUtils.elementIsInSignatureSpace(e, Constants._TAG_RETRIEVALMETHOD)) {
if (secureValidation) {
String error = "Error: It is forbidden to have one RetrievalMethod " + "point to another with secure validation";
if (log.isLoggable(java.util.logging.Level.FINE)) {
log.log(java.util.logging.Level.FINE, error);
}
return null;
}
RetrievalMethod rm2 = new RetrievalMethod(e, baseURI);
XMLSignatureInput resource2 = resolveInput(rm2, baseURI, secureValidation);
Element e2 = obtainReferenceElement(resource2);
if (e2 == element) {
if (log.isLoggable(java.util.logging.Level.FINE)) {
log.log(java.util.logging.Level.FINE, "Error: Can't have RetrievalMethods pointing to each other");
}
return null;
}
}
return resolveCertificate(e, baseURI, storage);
} catch (XMLSecurityException ex) {
if (log.isLoggable(java.util.logging.Level.FINE)) {
log.log(java.util.logging.Level.FINE, "XMLSecurityException", ex);
}
} catch (CertificateException ex) {
if (log.isLoggable(java.util.logging.Level.FINE)) {
log.log(java.util.logging.Level.FINE, "CertificateException", ex);
}
} catch (IOException ex) {
if (log.isLoggable(java.util.logging.Level.FINE)) {
log.log(java.util.logging.Level.FINE, "IOException", ex);
}
} catch (ParserConfigurationException e) {
if (log.isLoggable(java.util.logging.Level.FINE)) {
log.log(java.util.logging.Level.FINE, "ParserConfigurationException", e);
}
} catch (SAXException e) {
if (log.isLoggable(java.util.logging.Level.FINE)) {
log.log(java.util.logging.Level.FINE, "SAXException", e);
}
}
return null;
}
use of com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput in project jdk8u_jdk by JetBrains.
the class KeyInfoReferenceResolver method resolveReferentKeyInfo.
/**
* Resolve the KeyInfoReference Element's URI attribute into a KeyInfo instance.
*
* @param element
* @param baseURI
* @param storage
* @return the KeyInfo which is referred to by this KeyInfoReference, or null if can not be resolved
* @throws XMLSecurityException
*/
private KeyInfo resolveReferentKeyInfo(Element element, String baseURI, StorageResolver storage) throws XMLSecurityException {
KeyInfoReference reference = new KeyInfoReference(element, baseURI);
Attr uriAttr = reference.getURIAttr();
XMLSignatureInput resource = resolveInput(uriAttr, baseURI, secureValidation);
Element referentElement = null;
try {
referentElement = obtainReferenceElement(resource);
} catch (Exception e) {
if (log.isLoggable(java.util.logging.Level.FINE)) {
log.log(java.util.logging.Level.FINE, "XMLSecurityException", e);
}
return null;
}
if (referentElement == null) {
log.log(java.util.logging.Level.FINE, "De-reference of KeyInfoReference URI returned null: " + uriAttr.getValue());
return null;
}
validateReference(referentElement);
KeyInfo referent = new KeyInfo(referentElement, baseURI);
referent.addStorageResolver(storage);
return referent;
}
use of com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput in project jdk8u_jdk by JetBrains.
the class TransformXSLT method enginePerformTransform.
protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream baos, Transform transformObject) throws IOException, TransformationException {
try {
Element transformElement = transformObject.getElement();
Element xsltElement = XMLUtils.selectNode(transformElement.getFirstChild(), XSLTSpecNS, "stylesheet", 0);
if (xsltElement == null) {
Object[] exArgs = { "xslt:stylesheet", "Transform" };
throw new TransformationException("xml.WrongContent", exArgs);
}
TransformerFactory tFactory = TransformerFactory.newInstance();
// Process XSLT stylesheets in a secure manner
tFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
/*
* This transform requires an octet stream as input. If the actual
* input is an XPath node-set, then the signature application should
* attempt to convert it to octets (apply Canonical XML]) as described
* in the Reference Processing Model (section 4.3.3.2).
*/
Source xmlSource = new StreamSource(new ByteArrayInputStream(input.getBytes()));
Source stylesheet;
/*
* This complicated transformation of the stylesheet itself is necessary
* because of the need to get the pure style sheet. If we simply say
* Source stylesheet = new DOMSource(this.xsltElement);
* whereby this.xsltElement is not the rootElement of the Document,
* this causes problems;
* so we convert the stylesheet to byte[] and use this as input stream
*/
{
ByteArrayOutputStream os = new ByteArrayOutputStream();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(xsltElement);
StreamResult result = new StreamResult(os);
transformer.transform(source, result);
stylesheet = new StreamSource(new ByteArrayInputStream(os.toByteArray()));
}
Transformer transformer = tFactory.newTransformer(stylesheet);
// implementations.
try {
transformer.setOutputProperty("{http://xml.apache.org/xalan}line-separator", "\n");
} catch (Exception e) {
log.log(java.util.logging.Level.WARNING, "Unable to set Xalan line-separator property: " + e.getMessage());
}
if (baos == null) {
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
StreamResult outputTarget = new StreamResult(baos1);
transformer.transform(xmlSource, outputTarget);
return new XMLSignatureInput(baos1.toByteArray());
}
StreamResult outputTarget = new StreamResult(baos);
transformer.transform(xmlSource, outputTarget);
XMLSignatureInput output = new XMLSignatureInput((byte[]) null);
output.setOutputStream(baos);
return output;
} catch (XMLSecurityException ex) {
Object[] exArgs = { ex.getMessage() };
throw new TransformationException("generic.EmptyMessage", exArgs, ex);
} catch (TransformerConfigurationException ex) {
Object[] exArgs = { ex.getMessage() };
throw new TransformationException("generic.EmptyMessage", exArgs, ex);
} catch (TransformerException ex) {
Object[] exArgs = { ex.getMessage() };
throw new TransformationException("generic.EmptyMessage", exArgs, ex);
}
}
use of com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput in project jdk8u_jdk by JetBrains.
the class ResolverDirectHTTP method engineResolveURI.
/**
* Method resolve
*
* @param uri
* @param baseURI
*
* @throws ResourceResolverException
* @return
* $todo$ calculate the correct URI from the attribute and the baseURI
*/
@Override
public XMLSignatureInput engineResolveURI(ResourceResolverContext context) throws ResourceResolverException {
try {
// calculate new URI
URI uriNew = getNewURI(context.uriToResolve, context.baseUri);
URL url = uriNew.toURL();
URLConnection urlConnection;
urlConnection = openConnection(url);
// check if Basic authentication is required
String auth = urlConnection.getHeaderField("WWW-Authenticate");
if (auth != null && auth.startsWith("Basic")) {
// do http basic authentication
String user = engineGetProperty(ResolverDirectHTTP.properties[ResolverDirectHTTP.HttpBasicUser]);
String pass = engineGetProperty(ResolverDirectHTTP.properties[ResolverDirectHTTP.HttpBasicPass]);
if ((user != null) && (pass != null)) {
urlConnection = openConnection(url);
String password = user + ":" + pass;
String encodedPassword = Base64.encode(password.getBytes("ISO-8859-1"));
// set authentication property in the http header
urlConnection.setRequestProperty("Authorization", "Basic " + encodedPassword);
}
}
String mimeType = urlConnection.getHeaderField("Content-Type");
InputStream inputStream = urlConnection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
int read = 0;
int summarized = 0;
while ((read = inputStream.read(buf)) >= 0) {
baos.write(buf, 0, read);
summarized += read;
}
if (log.isLoggable(java.util.logging.Level.FINE)) {
log.log(java.util.logging.Level.FINE, "Fetched " + summarized + " bytes from URI " + uriNew.toString());
}
XMLSignatureInput result = new XMLSignatureInput(baos.toByteArray());
result.setSourceURI(uriNew.toString());
result.setMIMEType(mimeType);
return result;
} catch (URISyntaxException ex) {
throw new ResourceResolverException("generic.EmptyMessage", ex, context.attr, context.baseUri);
} catch (MalformedURLException ex) {
throw new ResourceResolverException("generic.EmptyMessage", ex, context.attr, context.baseUri);
} catch (IOException ex) {
throw new ResourceResolverException("generic.EmptyMessage", ex, context.attr, context.baseUri);
} catch (IllegalArgumentException e) {
throw new ResourceResolverException("generic.EmptyMessage", e, context.attr, context.baseUri);
}
}
Aggregations