Search in sources :

Example 1 with PolicyException

use of org.demoiselle.signer.policy.engine.exception.PolicyException in project signer by demoiselle.

the class PolicyValidator method validate.

public boolean validate() {
    try {
        boolean valid = true;
        Date dateNotBefore = this.sp.getSignPolicyInfo().getSignatureValidationPolicy().getSigningPeriod().getNotBefore().getDate();
        Date dateNotAfter = this.sp.getSignPolicyInfo().getSignatureValidationPolicy().getSigningPeriod().getNotAfter().getDate();
        Date actualDate = new GregorianCalendar().getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy - hh:mm:ss");
        if (actualDate.before(dateNotBefore) || actualDate.after(dateNotAfter)) {
            throw new PolicyException(policyMessagesBundle.getString("error.policy.valid.period", sdf.format(dateNotBefore), sdf.format(dateNotBefore)));
        }
        PolicyFactory factory = PolicyFactory.getInstance();
        LPA tempListOfPolicies = null;
        if (policyName.contains("CADES")) {
            tempListOfPolicies = factory.loadLPACAdES();
            listOfPolicies = tempListOfPolicies;
            Date nextUpdate = tempListOfPolicies.getNextUpdate().getDate();
            if (actualDate.after(nextUpdate)) {
                LOGGER.warn(policyMessagesBundle.getString("error.policy.not.updated", sdf.format(nextUpdate)));
                LOGGER.info(policyMessagesBundle.getString("info.lpa.load.local"));
                tempListOfPolicies = factory.loadLPACAdESLocal();
                if (tempListOfPolicies != null) {
                    nextUpdate = tempListOfPolicies.getNextUpdate().getDate();
                    if (actualDate.after(nextUpdate)) {
                        LOGGER.warn(policyMessagesBundle.getString("error.policy.local.not.updated", LPARepository.FULL_PATH_FOLDER_SIGNER.toString() + "LPA_CAdES.der", sdf.format(nextUpdate)));
                        tempListOfPolicies = factory.loadLPACAdESUrl();
                        if (tempListOfPolicies != null) {
                            nextUpdate = tempListOfPolicies.getNextUpdate().getDate();
                            if (actualDate.after(nextUpdate)) {
                                LOGGER.warn(policyMessagesBundle.getString("error.policy.not.updated", sdf.format(nextUpdate)));
                            } else {
                                listOfPolicies = tempListOfPolicies;
                            }
                        }
                    } else {
                        listOfPolicies = tempListOfPolicies;
                    }
                } else {
                    tempListOfPolicies = factory.loadLPACAdESUrl();
                    if (tempListOfPolicies != null) {
                        nextUpdate = tempListOfPolicies.getNextUpdate().getDate();
                        if (actualDate.after(nextUpdate)) {
                            LOGGER.warn(policyMessagesBundle.getString("error.policy.not.updated", sdf.format(nextUpdate)));
                        } else {
                            listOfPolicies = tempListOfPolicies;
                        }
                    } else {
                        LOGGER.warn(policyMessagesBundle.getString("error.lpa.not.found"));
                    }
                }
            }
            for (PolicyInfo policyInfo : listOfPolicies.getPolicyInfos()) {
                if (policyInfo.getPolicyOID().getValue().contentEquals(sp.getSignPolicyInfo().getSignPolicyIdentifier().getValue())) {
                    GeneralizedTime revocationDate = policyInfo.getRevocationDate();
                    if (revocationDate != null) {
                        throw new PolicyException(policyMessagesBundle.getString("error.policy.revocated", sdf.format(revocationDate.getDate())));
                    }
                }
            }
        } else {
            if (policyName.contains("PADES")) {
                tempListOfPolicies = factory.loadLPAPAdES();
                listOfPolicies = tempListOfPolicies;
                Date nextUpdate = tempListOfPolicies.getNextUpdate().getDate();
                if (actualDate.after(nextUpdate)) {
                    LOGGER.warn(policyMessagesBundle.getString("error.policy.not.updated", sdf.format(nextUpdate)));
                    LOGGER.info(policyMessagesBundle.getString("info.lpa.load.local"));
                    tempListOfPolicies = factory.loadLPAPAdESLocal();
                    if (tempListOfPolicies != null) {
                        nextUpdate = tempListOfPolicies.getNextUpdate().getDate();
                        if (actualDate.after(nextUpdate)) {
                            LOGGER.warn(policyMessagesBundle.getString("error.policy.local.not.updated", LPARepository.FULL_PATH_FOLDER_SIGNER.toString() + "LPA_PAdES.der", sdf.format(nextUpdate)));
                            tempListOfPolicies = factory.loadLPAPAdESUrl();
                            if (tempListOfPolicies != null) {
                                nextUpdate = tempListOfPolicies.getNextUpdate().getDate();
                                if (actualDate.after(nextUpdate)) {
                                    LOGGER.warn(policyMessagesBundle.getString("error.policy.not.updated", sdf.format(nextUpdate)));
                                } else {
                                    listOfPolicies = tempListOfPolicies;
                                }
                            }
                        } else {
                            listOfPolicies = tempListOfPolicies;
                        }
                    } else {
                        tempListOfPolicies = factory.loadLPAPAdESUrl();
                        if (tempListOfPolicies != null) {
                            nextUpdate = tempListOfPolicies.getNextUpdate().getDate();
                            if (actualDate.after(nextUpdate)) {
                                LOGGER.warn(policyMessagesBundle.getString("error.policy.not.updated", sdf.format(nextUpdate)));
                            } else {
                                listOfPolicies = tempListOfPolicies;
                            }
                        } else {
                            LOGGER.warn(policyMessagesBundle.getString("error.lpa.not.found"));
                        }
                    }
                }
                for (PolicyInfo policyInfo : listOfPolicies.getPolicyInfos()) {
                    if (policyInfo.getPolicyOID().getValue().contentEquals(sp.getSignPolicyInfo().getSignPolicyIdentifier().getValue())) {
                        GeneralizedTime revocationDate = policyInfo.getRevocationDate();
                        if (revocationDate != null) {
                            throw new PolicyException(policyMessagesBundle.getString("error.policy.revocated", sdf.format(revocationDate.getDate())));
                        }
                    }
                }
            } else {
                if (policyName.contains("XADES")) {
                    // TODO verificar como é procesado em XML
                    listOfPolicies = factory.loadLPAXAdES();
                } else {
                    throw new PolicyException(policyMessagesBundle.getString("error.policy.not.recognized", policyName));
                }
            }
        }
        return valid;
    } catch (Exception ex) {
        throw new PolicyException(ex.getMessage(), ex);
    }
}
Also used : PolicyFactory(org.demoiselle.signer.policy.engine.factory.PolicyFactory) PolicyException(org.demoiselle.signer.policy.engine.exception.PolicyException) GregorianCalendar(java.util.GregorianCalendar) GeneralizedTime(org.demoiselle.signer.policy.engine.asn1.GeneralizedTime) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) PolicyException(org.demoiselle.signer.policy.engine.exception.PolicyException)

Aggregations

SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 GregorianCalendar (java.util.GregorianCalendar)1 GeneralizedTime (org.demoiselle.signer.policy.engine.asn1.GeneralizedTime)1 PolicyException (org.demoiselle.signer.policy.engine.exception.PolicyException)1 PolicyFactory (org.demoiselle.signer.policy.engine.factory.PolicyFactory)1