Example usage for javax.xml.crypto.dsig XMLSignatureException printStackTrace

List of usage examples for javax.xml.crypto.dsig XMLSignatureException printStackTrace

Introduction

In this page you can find the example usage for javax.xml.crypto.dsig XMLSignatureException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this XMLSignatureException , its backtrace and the cause's backtrace to the standard error stream.

Usage

From source file:module.signature.util.XAdESValidator.java

private static void validateSigner(Document document, Set<User> usersPermitted, Set<User> usersExcluded,
        boolean allUsersPermittedShouldBeThere) throws SignatureDataException {

    if (!allUsersPermittedShouldBeThere || ((usersExcluded != null) && !usersExcluded.isEmpty())) {
        //TODO implement it when needed
        throw new DomainException("method.not.yet.implemented");
    }//from   w ww  .  j  a  va 2 s. co  m
    final String ID_NR_PREFIX = "OID.2.5.4.5=BI";
    ArrayList<String> usersPermittedIdNumbers = new ArrayList<String>();
    for (User user : usersPermitted) {
        usersPermittedIdNumbers.add(user.getPerson().getRemotePerson().getDocumentIdNumber());
    }
    //let's extract each signature
    // XMLDSIG
    NodeList nlSignature = document.getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature");
    //DEBUG 
    System.out.println("Got " + nlSignature.getLength() + " signatures");
    if (nlSignature.getLength() < 1) {
        throw new SignatureException("could.not.find.a.signature.in.incoming.data", true, null);
    }

    HashSet<String> usersFoundIdNumbers = new HashSet<String>();
    for (int i = 0; i < nlSignature.getLength(); i++) {
        //for each signature, let's extract the ID number of who did it
        Element signature = (Element) nlSignature.item(i);
        try {
            XMLSignature xmlSig = new XMLSignature(signature, null);
            KeyInfo ki = xmlSig.getKeyInfo();
            String certificateIDNr = ki.getX509Certificate().getSubjectX500Principal().getName("RFC1779");
            certificateIDNr = certificateIDNr
                    .substring(certificateIDNr.indexOf(ID_NR_PREFIX) + ID_NR_PREFIX.length());
            //let's take out the virgul and the last character, which is a control one
            certificateIDNr = certificateIDNr.substring(0, certificateIDNr.indexOf(',') - 1);
            usersFoundIdNumbers.add(certificateIDNr);
        } catch (XMLSignatureException e) {
            e.printStackTrace();
            throw new SignatureDataException("signature.error.XMLSignatureExceptionError", e);
        } catch (XMLSecurityException e) {
            throw new SignatureDataException("signature.error.XMLSecurityException", e);
        }
    }

    //now let's validate the extracted info
    if (allUsersPermittedShouldBeThere && usersFoundIdNumbers.containsAll(usersPermittedIdNumbers)) {
        return;
        //TODO TODO URGENT uncomment the next two lines (just made possible to be able to test it!!)
    } else {
        throw new SignatureDataException("wrong.document.signer");
    }

    //TODO the rest of the use cases aren't implemented ATM

}