Example usage for javax.xml.crypto XMLStructure getClass

List of usage examples for javax.xml.crypto XMLStructure getClass

Introduction

In this page you can find the example usage for javax.xml.crypto XMLStructure getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:be.fedict.eid.applet.service.signer.ooxml.RelationshipTransformService.java

@Override
public void init(XMLStructure parent, XMLCryptoContext context) throws InvalidAlgorithmParameterException {
    LOG.debug("init(parent,context)");
    LOG.debug("parent java type: " + parent.getClass().getName());
    DOMStructure domParent = (DOMStructure) parent;
    Node parentNode = domParent.getNode();
    try {//from  w w w. j  a  va2  s  . c o m
        LOG.debug("parent: " + toString(parentNode));
    } catch (TransformerException e) {
        throw new InvalidAlgorithmParameterException();
    }

    Element nsElement = parentNode.getOwnerDocument().createElement("ns");
    nsElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:ds", Constants.SignatureSpecNS);
    nsElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:mdssi",
            "http://schemas.openxmlformats.org/package/2006/digital-signature");

    /*
     * RelationshipReference
     */
    NodeList nodeList;
    try {
        nodeList = XPathAPI.selectNodeList(parentNode, "mdssi:RelationshipReference/@SourceId", nsElement);
    } catch (TransformerException e) {
        LOG.error("transformer exception: " + e.getMessage(), e);
        throw new InvalidAlgorithmParameterException();
    }
    for (int nodeIdx = 0; nodeIdx < nodeList.getLength(); nodeIdx++) {
        Node node = nodeList.item(nodeIdx);
        String sourceId = node.getTextContent();
        LOG.debug("sourceId: " + sourceId);
        this.sourceIds.add(sourceId);
    }

    /*
     * RelationshipsGroupReference
     */
    try {
        nodeList = XPathAPI.selectNodeList(parentNode, "mdssi:RelationshipsGroupReference/@SourceType",
                nsElement);
    } catch (TransformerException e) {
        LOG.error("transformer exception: " + e.getMessage(), e);
        throw new InvalidAlgorithmParameterException();
    }
    for (int nodeIdx = 0; nodeIdx < nodeList.getLength(); nodeIdx++) {
        Node node = nodeList.item(nodeIdx);
        String sourceType = node.getTextContent();
        LOG.debug("sourceType: " + sourceType);
        this.sourceTypes.add(sourceType);
    }
}

From source file:be.fedict.eid.applet.service.signer.AbstractXmlSignatureService.java

@SuppressWarnings("unchecked")
private byte[] getXmlSignatureDigestValue(DigestAlgo digestAlgo, List<DigestInfo> digestInfos,
        List<X509Certificate> signingCertificateChain)
        throws ParserConfigurationException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
        MarshalException, javax.xml.crypto.dsig.XMLSignatureException, TransformerFactoryConfigurationError,
        TransformerException, IOException, SAXException {
    /*/*from   ww  w. ja v  a  2s. c o m*/
     * DOM Document construction.
     */
    Document document = getEnvelopingDocument();
    if (null == document) {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        document = documentBuilder.newDocument();
    }

    /*
     * Signature context construction.
     */
    Key key = new Key() {
        private static final long serialVersionUID = 1L;

        public String getAlgorithm() {
            return null;
        }

        public byte[] getEncoded() {
            return null;
        }

        public String getFormat() {
            return null;
        }
    };
    XMLSignContext xmlSignContext = new DOMSignContext(key, document);
    URIDereferencer uriDereferencer = getURIDereferencer();
    if (null != uriDereferencer) {
        xmlSignContext.setURIDereferencer(uriDereferencer);
    }

    if (null != this.signatureNamespacePrefix) {
        /*
         * OOo doesn't like ds namespaces so per default prefixing is off.
         */
        xmlSignContext.putNamespacePrefix(javax.xml.crypto.dsig.XMLSignature.XMLNS,
                this.signatureNamespacePrefix);
    }

    XMLSignatureFactory signatureFactory = XMLSignatureFactory.getInstance("DOM",
            new org.apache.jcp.xml.dsig.internal.dom.XMLDSigRI());

    /*
     * Add ds:References that come from signing client local files.
     */
    List<Reference> references = new LinkedList<Reference>();
    addDigestInfosAsReferences(digestInfos, signatureFactory, references);

    /*
     * Invoke the signature facets.
     */
    String localSignatureId;
    if (null == this.signatureId) {
        localSignatureId = "xmldsig-" + UUID.randomUUID().toString();
    } else {
        localSignatureId = this.signatureId;
    }
    List<XMLObject> objects = new LinkedList<XMLObject>();
    for (SignatureFacet signatureFacet : this.signatureFacets) {
        LOG.debug("invoking signature facet: " + signatureFacet.getClass().getSimpleName());
        signatureFacet.preSign(signatureFactory, document, localSignatureId, signingCertificateChain,
                references, objects);
    }

    /*
     * ds:SignedInfo
     */
    SignatureMethod signatureMethod = signatureFactory.newSignatureMethod(getSignatureMethod(digestAlgo), null);
    CanonicalizationMethod canonicalizationMethod = signatureFactory
            .newCanonicalizationMethod(getCanonicalizationMethod(), (C14NMethodParameterSpec) null);
    SignedInfo signedInfo = signatureFactory.newSignedInfo(canonicalizationMethod, signatureMethod, references);

    /*
     * JSR105 ds:Signature creation
     */
    String signatureValueId = localSignatureId + "-signature-value";
    javax.xml.crypto.dsig.XMLSignature xmlSignature = signatureFactory.newXMLSignature(signedInfo, null,
            objects, localSignatureId, signatureValueId);

    /*
     * ds:Signature Marshalling.
     */
    DOMXMLSignature domXmlSignature = (DOMXMLSignature) xmlSignature;
    Node documentNode = document.getDocumentElement();
    if (null == documentNode) {
        /*
         * In case of an empty DOM document.
         */
        documentNode = document;
    }
    domXmlSignature.marshal(documentNode, this.signatureNamespacePrefix, (DOMCryptoContext) xmlSignContext);

    /*
     * Completion of undigested ds:References in the ds:Manifests.
     */
    for (XMLObject object : objects) {
        LOG.debug("object java type: " + object.getClass().getName());
        List<XMLStructure> objectContentList = object.getContent();
        for (XMLStructure objectContent : objectContentList) {
            LOG.debug("object content java type: " + objectContent.getClass().getName());
            if (false == objectContent instanceof Manifest) {
                continue;
            }
            Manifest manifest = (Manifest) objectContent;
            List<Reference> manifestReferences = manifest.getReferences();
            for (Reference manifestReference : manifestReferences) {
                if (null != manifestReference.getDigestValue()) {
                    continue;
                }
                DOMReference manifestDOMReference = (DOMReference) manifestReference;
                manifestDOMReference.digest(xmlSignContext);
            }
        }
    }

    /*
     * Completion of undigested ds:References.
     */
    List<Reference> signedInfoReferences = signedInfo.getReferences();
    for (Reference signedInfoReference : signedInfoReferences) {
        DOMReference domReference = (DOMReference) signedInfoReference;
        if (null != domReference.getDigestValue()) {
            // ds:Reference with external digest value
            continue;
        }
        domReference.digest(xmlSignContext);
    }

    /*
     * Store the intermediate XML signature document.
     */
    TemporaryDataStorage temporaryDataStorage = getTemporaryDataStorage();
    OutputStream tempDocumentOutputStream = temporaryDataStorage.getTempOutputStream();
    writeDocument(document, tempDocumentOutputStream);
    temporaryDataStorage.setAttribute(SIGNATURE_ID_ATTRIBUTE, localSignatureId);

    /*
     * Calculation of XML signature digest value.
     */
    DOMSignedInfo domSignedInfo = (DOMSignedInfo) signedInfo;
    ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
    domSignedInfo.canonicalize(xmlSignContext, dataStream);
    byte[] octets = dataStream.toByteArray();

    /*
     * TODO: we could be using DigestOutputStream here to optimize memory
     * usage.
     */

    MessageDigest jcaMessageDigest = MessageDigest.getInstance(digestAlgo.getAlgoId());
    byte[] digestValue = jcaMessageDigest.digest(octets);
    return digestValue;
}