Search in sources :

Example 1 with CertificateCoreException

use of org.demoiselle.signer.core.exception.CertificateCoreException in project signer by demoiselle.

the class CAdESChecker method validateTimestamp.

/**
 *  validade a timestampo on signature
 * @param attributeTimeStamp
 * @param varSignature
 * @return
 */
private Timestamp validateTimestamp(Attribute attributeTimeStamp, byte[] varSignature) {
    try {
        TimeStampOperator timeStampOperator = new TimeStampOperator();
        byte[] varTimeStamp = attributeTimeStamp.getAttrValues().getObjectAt(0).toASN1Primitive().getEncoded();
        TimeStampToken timeStampToken = new TimeStampToken(new CMSSignedData(varTimeStamp));
        Timestamp timeStampSigner = new Timestamp(timeStampToken);
        timeStampOperator.validate(varSignature, varTimeStamp, null);
        return timeStampSigner;
    } catch (CertificateCoreException | IOException | TSPException | CMSException e) {
        throw new SignerException(e);
    }
}
Also used : TimeStampOperator(org.demoiselle.signer.timestamp.connector.TimeStampOperator) IOException(java.io.IOException) TSPException(org.bouncycastle.tsp.TSPException) TimeStampToken(org.bouncycastle.tsp.TimeStampToken) CMSSignedData(org.bouncycastle.cms.CMSSignedData) Timestamp(org.demoiselle.signer.timestamp.Timestamp) SignerException(org.demoiselle.signer.policy.impl.cades.SignerException) CertificateCoreException(org.demoiselle.signer.core.exception.CertificateCoreException) CMSException(org.bouncycastle.cms.CMSException)

Example 2 with CertificateCoreException

use of org.demoiselle.signer.core.exception.CertificateCoreException in project signer by demoiselle.

the class CertificateManager method load.

/**
 *  Load a java.security.cert.X509Certificate
 * @param object destiny of load operation
 */
public void load(Object object) {
    Field[] fields = object.getClass().getDeclaredFields();
    for (Field field : fields) {
        for (Annotation annotation : field.getAnnotations()) {
            if (annotation.annotationType().isAnnotationPresent(OIDExtension.class)) {
                OIDExtension oid = annotation.annotationType().getAnnotation(OIDExtension.class);
                Class<? extends IOIDExtensionLoader> loaderClass = oid.loader();
                try {
                    IOIDExtensionLoader loader = loaderClass.newInstance();
                    loader.load(object, field, x509);
                } catch (IllegalAccessException | InstantiationException e) {
                    throw new CertificateCoreException(coreMessagesBundle.getString("error.initialize.attribute", field.getName()), e);
                }
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) Annotation(java.lang.annotation.Annotation) CertificateCoreException(org.demoiselle.signer.core.exception.CertificateCoreException)

Example 3 with CertificateCoreException

use of org.demoiselle.signer.core.exception.CertificateCoreException in project signer by demoiselle.

the class CertificateManager method load.

/**
 *  New Instance for a class
 * @param <T> Type parameter for returned instance
 * @param clazz class to be instantiated
 * @return new instance of class
 */
public <T> T load(Class<T> clazz) {
    T object;
    try {
        object = clazz.newInstance();
    } catch (IllegalAccessException | InstantiationException e) {
        throw new CertificateCoreException(coreMessagesBundle.getString("error.new.instace", clazz.getName()), e);
    }
    load(object);
    return object;
}
Also used : CertificateCoreException(org.demoiselle.signer.core.exception.CertificateCoreException)

Example 4 with CertificateCoreException

use of org.demoiselle.signer.core.exception.CertificateCoreException in project signer by demoiselle.

the class ICPBrasilExtensionLoader method load.

@Override
public void load(Object object, Field field, X509Certificate x509) {
    if (field.isAnnotationPresent(ICPBrasilExtension.class)) {
        ICPBrasilExtension annotation = field.getAnnotation(ICPBrasilExtension.class);
        Object keyValue;
        try {
            BasicCertificate cert = new BasicCertificate(x509);
            switch(annotation.type()) {
                case CPF:
                    if (cert.hasCertificatePF()) {
                        keyValue = cert.getICPBRCertificatePF().getCPF();
                    } else {
                        keyValue = "";
                    }
                    break;
                case CNPJ:
                    if (cert.hasCertificatePJ()) {
                        keyValue = cert.getICPBRCertificatePJ().getCNPJ();
                    } else {
                        if (cert.hasCertificateEquipment()) {
                            keyValue = cert.getICPBRCertificateEquipment().getCNPJ();
                        } else {
                            keyValue = "";
                        }
                    }
                    break;
                case PIS_PASEP:
                    if (cert.hasCertificatePF()) {
                        keyValue = cert.getICPBRCertificatePF().getNis();
                    } else {
                        if (cert.hasCertificatePJ()) {
                            keyValue = cert.getICPBRCertificatePJ().getNis();
                        } else {
                            if (cert.hasCertificateEquipment()) {
                                keyValue = cert.getICPBRCertificateEquipment().getNis();
                            } else {
                                keyValue = "";
                            }
                        }
                    }
                    break;
                case NIS:
                    if (cert.hasCertificatePF()) {
                        keyValue = cert.getICPBRCertificatePF().getNis();
                    } else {
                        if (cert.hasCertificatePJ()) {
                            keyValue = cert.getICPBRCertificatePJ().getNis();
                        } else {
                            if (cert.hasCertificateEquipment()) {
                                keyValue = cert.getICPBRCertificateEquipment().getNis();
                            } else {
                                keyValue = "";
                            }
                        }
                    }
                    break;
                case CEI:
                    if (cert.hasCertificatePF()) {
                        keyValue = cert.getICPBRCertificatePF().getCEI();
                    } else {
                        if (cert.hasCertificatePJ()) {
                            keyValue = cert.getICPBRCertificatePJ().getCEI();
                        } else {
                            keyValue = "";
                        }
                    }
                    break;
                case CEI_PESSOA_FISICA:
                    if (cert.hasCertificatePF()) {
                        keyValue = cert.getICPBRCertificatePF().getCEI();
                    } else {
                        keyValue = "";
                    }
                    break;
                case CEI_PESSOA_JURIDICA:
                    if (cert.hasCertificatePJ()) {
                        keyValue = cert.getICPBRCertificatePJ().getCEI();
                    } else {
                        keyValue = "";
                    }
                    break;
                case NAME:
                    keyValue = cert.getNome();
                    break;
                case NAME_RESPONSIBLE_PESSOA_JURIDICA:
                    if (cert.hasCertificatePJ()) {
                        keyValue = cert.getICPBRCertificatePJ().getResponsibleName();
                    } else {
                        keyValue = "";
                    }
                    break;
                case CPF_RESPONSIBLE_PESSOA_JURIDICA:
                    if (cert.hasCertificatePJ()) {
                        keyValue = cert.getICPBRCertificatePJ().getResponsibleCPF();
                    } else {
                        keyValue = "";
                    }
                    break;
                case EMAIL:
                    keyValue = cert.getEmail();
                    break;
                case BIRTH_DATE:
                    if (cert.hasCertificatePF()) {
                        keyValue = cert.getICPBRCertificatePF().getBirthDate();
                    } else {
                        if (cert.hasCertificatePJ()) {
                            keyValue = cert.getICPBRCertificatePJ().getBirthDate();
                        } else {
                            if (cert.hasCertificateEquipment()) {
                                keyValue = cert.getICPBRCertificateEquipment().getBirthDate().toString();
                            } else {
                                keyValue = "";
                            }
                        }
                    }
                    break;
                case ID_NUMBER:
                    if (cert.hasCertificatePF()) {
                        keyValue = cert.getICPBRCertificatePF().getRg();
                    } else {
                        if (cert.hasCertificatePJ()) {
                            keyValue = cert.getICPBRCertificatePJ().getRg();
                        } else {
                            if (cert.hasCertificateEquipment()) {
                                keyValue = cert.getICPBRCertificateEquipment().getRg();
                            } else {
                                keyValue = "";
                            }
                        }
                    }
                    break;
                case IDENTITY_DISPATCHER:
                    if (cert.hasCertificatePF()) {
                        keyValue = cert.getICPBRCertificatePF().getIssuingAgencyRg();
                    } else {
                        if (cert.hasCertificatePJ()) {
                            keyValue = cert.getICPBRCertificatePJ().getIssuingAgencyRg();
                        } else {
                            if (cert.hasCertificateEquipment()) {
                                keyValue = cert.getICPBRCertificateEquipment().getIssuingAgencyRg();
                            } else {
                                keyValue = "";
                            }
                        }
                    }
                    break;
                case UF_IDENTITY_DISPATCHER:
                    if (cert.hasCertificatePF()) {
                        keyValue = cert.getICPBRCertificatePF().getUfIssuingAgencyRg();
                    } else {
                        if (cert.hasCertificatePJ()) {
                            keyValue = cert.getICPBRCertificatePJ().getUfIssuingAgencyRg();
                        } else {
                            if (cert.hasCertificateEquipment()) {
                                keyValue = cert.getICPBRCertificateEquipment().getUfIssuingAgencyRg();
                            } else {
                                keyValue = "";
                            }
                        }
                    }
                    break;
                case NUMBER_ELECTORAL_DOCUMENT:
                    if (cert.hasCertificatePF()) {
                        keyValue = cert.getICPBRCertificatePF().getElectoralDocument();
                    } else {
                        keyValue = "";
                    }
                    break;
                case ZONE_ELECTORAL_DOCUMENT:
                    if (cert.hasCertificatePF()) {
                        keyValue = cert.getICPBRCertificatePF().getZoneElectoralDocument();
                    } else {
                        keyValue = "";
                    }
                    break;
                case SECTION_ELECTORAL_DOCUMENT:
                    if (cert.hasCertificatePF()) {
                        keyValue = cert.getICPBRCertificatePF().getSectionElectoralDocument();
                    } else {
                        keyValue = "";
                    }
                    break;
                case CITY_ELECTORAL_DOCUMENT:
                    if (cert.hasCertificatePF()) {
                        keyValue = cert.getICPBRCertificatePF().getCityElectoralDocument();
                    } else {
                        keyValue = "";
                    }
                    break;
                case UF_ELECTORAL_DOCUMENT:
                    if (cert.hasCertificatePF()) {
                        keyValue = cert.getICPBRCertificatePF().getUFElectoralDocument();
                    } else {
                        keyValue = "";
                    }
                    break;
                case BUSINESS_NAME:
                    if (cert.hasCertificateEquipment()) {
                        keyValue = cert.getICPBRCertificateEquipment().getCorporateName();
                    } else {
                        keyValue = "";
                    }
                    break;
                case CERTIFICATE_TYPE:
                    if (cert.hasCertificatePF()) {
                        keyValue = "PF";
                    } else {
                        if (cert.hasCertificatePJ()) {
                            keyValue = "PJ";
                        } else {
                            if (cert.hasCertificateEquipment()) {
                                keyValue = "EA";
                            } else {
                                keyValue = "";
                            }
                        }
                    }
                    break;
                case CERTIFICATE_LEVEL:
                    keyValue = cert.getCertificateLevel();
                    break;
                default:
                    throw new CertificateCoreException(coreMessagesBundle.getString("error.field.not.implemented", annotation.type()));
            }
            try {
                field.setAccessible(true);
                field.set(object, keyValue);
            } catch (Exception e) {
                throw new CertificateCoreException(coreMessagesBundle.getString("error.load.value.field", field.getName()), e);
            }
        } catch (Exception e) {
            throw new CertificateCoreException(coreMessagesBundle.getString("error.get.value.field", field.getName()), e);
        }
    }
}
Also used : CertificateCoreException(org.demoiselle.signer.core.exception.CertificateCoreException) CertificateCoreException(org.demoiselle.signer.core.exception.CertificateCoreException)

Example 5 with CertificateCoreException

use of org.demoiselle.signer.core.exception.CertificateCoreException in project signer by demoiselle.

the class TimeStampGeneratorSelector method getPriority.

/**
 *  verify if have a @Priotity annotation
 * @param clazz
 * @return
 */
private static int getPriority(TimeStampGenerator clazz) {
    int result = Priority.MAX_PRIORITY;
    Priority priority = clazz.getClass().getAnnotation(Priority.class);
    if (priority != null) {
        result = priority.value();
    }
    if (priority == null) {
        new CertificateCoreException(coreMessagesBundle.getString("error.priority.null", clazz.getClass().getName()));
    }
    return result;
}
Also used : Priority(org.demoiselle.signer.core.Priority) CertificateCoreException(org.demoiselle.signer.core.exception.CertificateCoreException)

Aggregations

CertificateCoreException (org.demoiselle.signer.core.exception.CertificateCoreException)16 IOException (java.io.IOException)9 Timestamp (org.demoiselle.signer.timestamp.Timestamp)5 TSPException (org.bouncycastle.tsp.TSPException)4 TimeStampToken (org.bouncycastle.tsp.TimeStampToken)4 SignerException (org.demoiselle.signer.policy.impl.cades.SignerException)4 TimeStampOperator (org.demoiselle.signer.timestamp.connector.TimeStampOperator)4 CMSException (org.bouncycastle.cms.CMSException)3 CMSSignedData (org.bouncycastle.cms.CMSSignedData)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 BigInteger (java.math.BigInteger)2 KeyStoreException (java.security.KeyStoreException)2 CertificateException (java.security.cert.CertificateException)2 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)2 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)2 Digest (org.demoiselle.signer.cryptography.Digest)2 SignedOrUnsignedAttribute (org.demoiselle.signer.policy.impl.cades.pkcs7.attribute.SignedOrUnsignedAttribute)2 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1