javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document doc = db.newDocument();
//BaseURI provides the base for the path to referenced content from the signature
String BaseURI = signatureFile.toURI().toURL().toString();
XMLSignature sig = new XMLSignature(doc, BaseURI,
XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256,
Transforms.TRANSFORM_C14N11_OMIT_COMMENTS);
sig.setId(role);
doc.appendChild(sig.getElement());
{
Transforms transforms = new Transforms(doc);
transforms.addTransform(Transforms.TRANSFORM_C14N11_OMIT_COMMENTS);
addDocuments(sig, documentsList);
customizeToRole(role);
//Adding signature properties
SignatureProperty profile = new SignatureProperty(doc, target,
SignerConstants.SIGNATURE_PROPERTY_ID_PROFILE);
Element profileElement = doc
.createElement(SignerConstants.SIGNATURE_PROPERTY_ID_PROFILE_TAG_NAME);
profileElement.setAttribute("URI", SignerConstants.PROFILE_URI);
profile.appendChild(profileElement);
SignatureProperty role = new SignatureProperty(doc, target,
SignerConstants.SIGNATURE_PROPERTY_ID_ROLE);
Element roleElement = doc
.createElement(SignerConstants.SIGNATURE_PROPERTY_ID_ROLE_TAG_NAME);
roleElement.setAttribute("URI", roleURI);
role.appendChild(roleElement);
SignatureProperty identifier = new SignatureProperty(doc, target,
SignerConstants.SIGNATURE_PROPERTY_ID_IDENTIFIER);
Element identifierElement = doc
.createElement(SignerConstants.SIGNATURE_PROPERTY_ID_IDENTIFIER_TAG_NAME);
identifierElement.setTextContent(name);
identifier.appendChild(identifierElement);
SignatureProperties props = new SignatureProperties(doc);
props.setXPathNamespaceContext(SignerConstants.XMLNS_DSP,
SignerConstants.SIGNATURE_PROPERTIES_NAMESPACE);
props.addSignatureProperty(profile);
props.addSignatureProperty(role);
props.addSignatureProperty(identifier);
ObjectContainer object = new ObjectContainer(doc);
object.setId("prop");
object.appendChild(doc.createTextNode("\n"));
object.appendChild(props.getElement());
object.appendChild(doc.createTextNode("\n"));
//Refer the properties section of the signature which needs
//to be signed too, that include role information.
sig.appendObject(object);
sig.addDocument("#prop", transforms,
SignerConstants.DIGEST_METHOD_ALGORITHM);
ResourceResolver offlineResolver = null;
sig.addResourceResolver(offlineResolver);
{
X509Certificate cert = (X509Certificate) ks
.getCertificate(certificateAlias);
if(cert.equals(null)){
throw new NullPointerException();
}
sig.addKeyInfo(cert);
sig.addKeyInfo(cert.getPublicKey());
System.out.println("Start signing");
sig.sign(privateKey);
System.out.println("Finished signing");
}
FileOutputStream f = new FileOutputStream(signatureFile);