} else {
try {
xmlSignatureInput =
new XMLSignatureInput(((OctetStreamData)data).getOctetStream());
} catch (Exception ex) {
throw new TransformException(ex);
}
}
if (!xmlSignatureInput.isElement()) {
throw new TransformException(
"Wrong input format - only element input supported"
);
}
Element str = (Element)xmlSignatureInput.getSubNode();
//
// The element to transform MUST be a SecurityTokenReference
// element.
//
SecurityTokenReference secRef = new SecurityTokenReference(str);
Canonicalizer canon = Canonicalizer.getInstance(canonAlgo);
ByteArrayOutputStream bos = null;
byte[] buf = null;
//
// Third and fourth step are performed by dereferenceSTR()
//
WSDocInfo wsDocInfo = (WSDocInfo)xc.getProperty(TRANSFORM_WS_DOC_INFO);
if (wsDocInfo == null) {
throw new TransformException("no WSDocInfo found");
}
Document doc = str.getOwnerDocument();
Element dereferencedToken =
STRTransformUtil.dereferenceSTR(doc, secRef, wsDocInfo);
if (dereferencedToken != null) {
String type = dereferencedToken.getAttribute("ValueType");
if ((X509Security.X509_V3_TYPE.equals(type)
|| PKIPathSecurity.getType().equals(type))) {
//
// Add the WSSE/WSU namespaces to the element for C14n
//
WSSecurityUtil.setNamespace(
dereferencedToken, WSConstants.WSSE_NS, WSConstants.WSSE_PREFIX
);
WSSecurityUtil.setNamespace(
dereferencedToken, WSConstants.WSU_NS, WSConstants.WSU_PREFIX
);
}
}
//
// C14n with specified algorithm. According to WSS Specification.
//
buf = canon.canonicalizeSubtree(dereferencedToken, "#default");
if (doDebug) {
bos = new ByteArrayOutputStream(buf.length);
bos.write(buf, 0, buf.length);
log.debug("after c14n: " + bos.toString());
}
//
// Alert: Hacks ahead According to WSS spec an Apex node must
// contain a default namespace. If none is availabe in the first
// node of the c14n output (this is the apex element) then we do
// some editing to insert an empty default namespace
//
// TODO: Rework theses hacks after c14n was updated and can be
// instructed to insert empty default namespace if required
//
// If the problem with c14n method is solved then just do:
// return new XMLSignatureInput(buf);
// start of HACK
StringBuilder bf = new StringBuilder(new String(buf));
String bf1 = bf.toString();
//
// Find start and end of first element <....>, this is the Apex node
//
int gt = bf1.indexOf(">");
//
// Lookup the default namespace
//
int idx = bf1.indexOf("xmlns=");
//
// If none found or if it is outside of this (Apex) element look for
// first blank in, insert default namespace there (this is the
// correct place according to c14n specification)
//
if (idx < 0 || idx > gt) {
idx = bf1.indexOf(" ");
bf.insert(idx + 1, "xmlns=\"\" ");
bf1 = bf.toString();
}
if (doDebug) {
log.debug("last result: ");
log.debug(bf1);
}
XMLSignatureInput output = new XMLSignatureInput(bf1.getBytes());
if (os != null) {
output.updateOutputStream(os);
return null;
}
return new OctetStreamData(output.getOctetStream());
} catch (Exception ex) {
throw new TransformException(ex);
}
}