Example usage for javax.xml.crypto.dsig XMLSignatureFactory newXMLObject

List of usage examples for javax.xml.crypto.dsig XMLSignatureFactory newXMLObject

Introduction

In this page you can find the example usage for javax.xml.crypto.dsig XMLSignatureFactory newXMLObject.

Prototype

public abstract XMLObject newXMLObject(List<? extends XMLStructure> content, String id, String mimeType,
        String encoding);

Source Link

Document

Creates an XMLObject from the specified parameters.

Usage

From source file:org.roda.common.certification.ODFSignatureUtils.java

private static List<XMLObject> getXMLObjectList(XMLSignatureFactory factory, Document docSignatures,
        String signatureId, String signaturePropertyId) {

    Element content = docSignatures.createElement("dc:date");
    content.setAttribute("xmlns:dc", "http://purl.org/dc/elements/1.1/");

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss,SS");
    content.setTextContent(sdf.format(new Date()));
    XMLStructure str = new DOMStructure(content);
    List<XMLStructure> contentList = new ArrayList<XMLStructure>();
    contentList.add(str);//from w ww.  j  av a 2 s .c  om

    SignatureProperty sp = factory.newSignatureProperty(contentList, "#" + signatureId, signaturePropertyId);
    List<SignatureProperty> spList = new ArrayList<SignatureProperty>();
    spList.add(sp);

    SignatureProperties sps = factory.newSignatureProperties(spList, null);
    List<SignatureProperties> spsList = new ArrayList<SignatureProperties>();
    spsList.add(sps);

    XMLObject object = factory.newXMLObject(spsList, null, null, null);
    List<XMLObject> objectList = new ArrayList<XMLObject>();
    objectList.add(object);

    return objectList;
}

From source file:org.roda.core.plugins.plugins.characterization.ODFSignatureUtils.java

private static List<XMLObject> getXMLObjectList(XMLSignatureFactory factory, Document docSignatures,
        String signatureId, String signaturePropertyId) {

    Element content = docSignatures.createElement("dc:date");
    content.setAttribute("xmlns:dc", "http://purl.org/dc/elements/1.1/");

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss,SS");
    content.setTextContent(sdf.format(new Date()));
    XMLStructure str = new DOMStructure(content);
    List<XMLStructure> contentList = new ArrayList<>();
    contentList.add(str);/*from   w  w  w  .j av  a 2 s.co  m*/

    SignatureProperty sp = factory.newSignatureProperty(contentList, "#" + signatureId, signaturePropertyId);
    List<SignatureProperty> spList = new ArrayList<>();
    spList.add(sp);

    SignatureProperties sps = factory.newSignatureProperties(spList, null);
    List<SignatureProperties> spsList = new ArrayList<>();
    spsList.add(sps);

    XMLObject object = factory.newXMLObject(spsList, null, null, null);
    List<XMLObject> objectList = new ArrayList<>();
    objectList.add(object);

    return objectList;
}

From source file:org.warlock.itk.distributionenvelope.Payload.java

/** 
 * Sign the payloadBody as-is. Note that this is going to be encrypted anyway
 * so we avoid any incompatibilities due to canonicalisation, and we don't
 * care if the payloadBody is text, compressed and so on. Re-writes payloadBody
 * with a serialised XML Digital Signature "Signature" element containing an
 * enveloping signature, or throws an exception to signal failure. 
 * /*www  .  j a v  a  2 s.  c o m*/
 * @param pk
 * @param cert
 * @throws Exception 
 */
private void signPayload(PrivateKey pk, X509Certificate cert) throws Exception {
    if ((pk == null) || (cert == null)) {
        throw new Exception("Null signing material");
    }
    cert.checkValidity();

    XMLSignatureFactory xsf = XMLSignatureFactory.getInstance("DOM");
    Reference ref = null;
    String objectRef = "uuid" + UUID.randomUUID().toString();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    Document doc = null;
    DOMStructure payloadContent = null;
    if (compressed || base64 || !mimeType.contains("xml")) {
        ref = xsf.newReference("#" + objectRef, xsf.newDigestMethod(DigestMethod.SHA1, null));
        doc = dbf.newDocumentBuilder().newDocument();
        payloadContent = new DOMStructure(doc.createTextNode(payloadBody));
    } else {
        Transform t = xsf.newTransform("http://www.w3.org/2001/10/xml-exc-c14n#",
                (TransformParameterSpec) null);
        ref = xsf.newReference("#" + objectRef, xsf.newDigestMethod(DigestMethod.SHA1, null),
                Collections.singletonList(t), null, null);
        doc = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(payloadBody)));
        payloadContent = new DOMStructure(doc.getDocumentElement());
    }
    XMLObject payloadObject = xsf.newXMLObject(Collections.singletonList(payloadContent), objectRef, null,
            null);
    SignedInfo si = xsf.newSignedInfo(
            xsf.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,
                    (C14NMethodParameterSpec) null),
            xsf.newSignatureMethod(SignatureMethod.RSA_SHA1, null), Collections.singletonList(ref));

    KeyInfoFactory kif = xsf.getKeyInfoFactory();
    ArrayList<Object> x509content = new ArrayList<Object>();
    x509content.add(cert);
    X509Data xd = kif.newX509Data(x509content);

    KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd));
    XMLSignature signature = xsf.newXMLSignature(si, ki, Collections.singletonList(payloadObject), null, null);
    DOMSignContext dsc = new DOMSignContext(pk, doc);
    signature.sign(dsc);
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    Transformer tx = TransformerFactory.newInstance().newTransformer();
    tx.transform(new DOMSource(doc), sr);
    if (sw.toString().indexOf("<?xml ") == 0) {
        payloadBody = sw.toString().substring(sw.toString().indexOf("?>") + "?>".length());
    } else {
        payloadBody = sw.toString();
    }
}