Example usage for javax.xml.crypto KeySelectorResult getKey

List of usage examples for javax.xml.crypto KeySelectorResult getKey

Introduction

In this page you can find the example usage for javax.xml.crypto KeySelectorResult getKey.

Prototype

Key getKey();

Source Link

Document

Returns the selected key.

Usage

From source file:org.apache.jcp.xml.dsig.internal.dom.DOMXMLSignature.java

public void sign(XMLSignContext signContext) throws MarshalException, XMLSignatureException {
    if (signContext == null) {
        throw new NullPointerException("signContext cannot be null");
    }//from   www .  ja va2 s  .co  m
    DOMSignContext context = (DOMSignContext) signContext;
    marshal(context.getParent(), context.getNextSibling(), DOMUtils.getSignaturePrefix(context), context);

    // generate references and signature value
    List<Reference> allReferences = new ArrayList<Reference>();

    // traverse the Signature and register all objects with IDs that
    // may contain References
    signatureIdMap = new HashMap<String, XMLStructure>();
    signatureIdMap.put(id, this);
    signatureIdMap.put(si.getId(), si);
    @SuppressWarnings("unchecked")
    List<Reference> refs = si.getReferences();
    for (Reference ref : refs) {
        signatureIdMap.put(ref.getId(), ref);
    }
    for (XMLObject obj : objects) {
        signatureIdMap.put(obj.getId(), obj);
        @SuppressWarnings("unchecked")
        List<XMLStructure> content = obj.getContent();
        for (XMLStructure xs : content) {
            if (xs instanceof Manifest) {
                Manifest man = (Manifest) xs;
                signatureIdMap.put(man.getId(), man);
                @SuppressWarnings("unchecked")
                List<Reference> manRefs = man.getReferences();
                for (Reference ref : manRefs) {
                    allReferences.add(ref);
                    signatureIdMap.put(ref.getId(), ref);
                }
            }
        }
    }
    // always add SignedInfo references after Manifest references so
    // that Manifest reference are digested first
    allReferences.addAll(refs);

    // generate/digest each reference
    for (Reference ref : allReferences) {
        digestReference((DOMReference) ref, signContext);
    }

    // do final sweep to digest any references that were skipped or missed
    for (Reference ref : allReferences) {
        if (((DOMReference) ref).isDigested()) {
            continue;
        }
        ((DOMReference) ref).digest(signContext);
    }

    Key signingKey = null;
    KeySelectorResult ksr = null;
    try {
        ksr = signContext.getKeySelector().select(ki, KeySelector.Purpose.SIGN, si.getSignatureMethod(),
                signContext);
        signingKey = ksr.getKey();
        if (signingKey == null) {
            throw new XMLSignatureException("the keySelector did not " + "find a signing key");
        }
    } catch (KeySelectorException kse) {
        throw new XMLSignatureException("cannot find signing key", kse);
    }

    // calculate signature value
    try {
        byte[] val = ((AbstractDOMSignatureMethod) si.getSignatureMethod()).sign(signingKey, si, signContext);
        ((DOMSignatureValue) sv).setValue(val);
    } catch (InvalidKeyException ike) {
        throw new XMLSignatureException(ike);
    }

    this.localSigElem = sigElem;
    this.ksr = ksr;
}

From source file:org.jcp.xml.dsig.internal.dom.DOMXMLSignature.java

public void sign(XMLSignContext signContext) throws MarshalException, XMLSignatureException {
    if (signContext == null) {
        throw new NullPointerException("signContext cannot be null");
    }//  w  ww  . j a va 2 s  .c om
    DOMSignContext context = (DOMSignContext) signContext;
    if (context != null) {
        marshal(context.getParent(), context.getNextSibling(), DOMUtils.getSignaturePrefix(context), context);
    }

    // generate references and signature value
    List<Reference> allReferences = new ArrayList<Reference>();

    // traverse the Signature and register all objects with IDs that
    // may contain References
    signatureIdMap = new HashMap<String, XMLStructure>();
    signatureIdMap.put(id, this);
    signatureIdMap.put(si.getId(), si);
    @SuppressWarnings("unchecked")
    List<Reference> refs = si.getReferences();
    for (Reference ref : refs) {
        signatureIdMap.put(ref.getId(), ref);
    }
    for (XMLObject obj : objects) {
        signatureIdMap.put(obj.getId(), obj);
        @SuppressWarnings("unchecked")
        List<XMLStructure> content = obj.getContent();
        for (XMLStructure xs : content) {
            if (xs instanceof Manifest) {
                Manifest man = (Manifest) xs;
                signatureIdMap.put(man.getId(), man);
                @SuppressWarnings("unchecked")
                List<Reference> manRefs = man.getReferences();
                for (Reference ref : manRefs) {
                    allReferences.add(ref);
                    signatureIdMap.put(ref.getId(), ref);
                }
            }
        }
    }
    // always add SignedInfo references after Manifest references so
    // that Manifest reference are digested first
    allReferences.addAll(refs);

    // generate/digest each reference
    for (Reference ref : allReferences) {
        digestReference((DOMReference) ref, signContext);
    }

    // do final sweep to digest any references that were skipped or missed
    for (Reference ref : allReferences) {
        if (((DOMReference) ref).isDigested()) {
            continue;
        }
        ((DOMReference) ref).digest(signContext);
    }

    Key signingKey = null;
    KeySelectorResult ksr = null;
    try {
        ksr = signContext.getKeySelector().select(ki, KeySelector.Purpose.SIGN, si.getSignatureMethod(),
                signContext);
        signingKey = ksr.getKey();
        if (signingKey == null) {
            throw new XMLSignatureException("the keySelector did not " + "find a signing key");
        }
    } catch (KeySelectorException kse) {
        throw new XMLSignatureException("cannot find signing key", kse);
    }

    // calculate signature value
    try {
        byte[] val = ((AbstractDOMSignatureMethod) si.getSignatureMethod()).sign(signingKey, si, signContext);
        ((DOMSignatureValue) sv).setValue(val);
    } catch (InvalidKeyException ike) {
        throw new XMLSignatureException(ike);
    }

    this.localSigElem = sigElem;
    this.ksr = ksr;
}