Search in sources :

Example 11 with SignerException

use of org.demoiselle.signer.policy.impl.cades.SignerException in project signer by demoiselle.

the class CAdESSigner method getAttached.

/**
 * Extracts the signed content from the digital signature structure, if it
 * is a signature with attached content.
 *
 * @param signed
 *            Signature and signed content.
 * @param validateOnExtract
 *            TRUE (to execute validation) or FALSE (not execute validation)
 *
 * @return content for attached signature
 */
@Override
public byte[] getAttached(byte[] signed, boolean validateOnExtract) {
    byte[] result = null;
    if (validateOnExtract) {
        this.check(null, signed);
    }
    CMSSignedData signedData = null;
    try {
        signedData = new CMSSignedData(signed);
    } catch (CMSException exception) {
        throw new SignerException(cadesMessagesBundle.getString("error.invalid.bytes.pkcs7"), exception);
    }
    try {
        CMSProcessable contentProcessable = signedData.getSignedContent();
        if (contentProcessable != null) {
            result = (byte[]) contentProcessable.getContent();
        }
    } catch (Exception exception) {
        throw new SignerException(cadesMessagesBundle.getString("error.get.content.pkcs7"), exception);
    }
    return result;
}
Also used : CMSSignedData(org.bouncycastle.cms.CMSSignedData) SignerException(org.demoiselle.signer.policy.impl.cades.SignerException) CMSProcessable(org.bouncycastle.cms.CMSProcessable) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) CertificateCoreException(org.demoiselle.signer.core.exception.CertificateCoreException) CertificateValidatorException(org.demoiselle.signer.core.exception.CertificateValidatorException) ParseException(java.text.ParseException) TSPException(org.bouncycastle.tsp.TSPException) CertificateEncodingException(java.security.cert.CertificateEncodingException) CMSException(org.bouncycastle.cms.CMSException) CertificateValidatorCRLException(org.demoiselle.signer.core.exception.CertificateValidatorCRLException) CMSSignerDigestMismatchException(org.bouncycastle.cms.CMSSignerDigestMismatchException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SignerException(org.demoiselle.signer.policy.impl.cades.SignerException) CMSException(org.bouncycastle.cms.CMSException)

Example 12 with SignerException

use of org.demoiselle.signer.policy.impl.cades.SignerException in project signer by demoiselle.

the class CAdESSigner method validateTimestamp.

/**
 *  validade a timestampo on signature
 * @param attributeTimeStamp
 * @param varSignature
 * @return
 */
@Deprecated
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 13 with SignerException

use of org.demoiselle.signer.policy.impl.cades.SignerException in project signer by demoiselle.

the class GenericFactory method factoryFromClassName.

/**
 * Instantiate an object from the name of your class
 * @param className class name of new instance
 * @return new instance
 */
@SuppressWarnings("all")
public F factoryFromClassName(String className) {
    F result = null;
    Class clazz = null;
    try {
        clazz = Class.forName(className);
    } catch (Throwable error) {
        throw new SignerException(cadesMessagesBundle.getString("error.class.not.exist", className), error);
    }
    if (clazz != null) {
        try {
            result = (F) clazz.newInstance();
        } catch (Throwable error) {
            throw new SignerException(cadesMessagesBundle.getString("error.class.incompatible", clazz.getCanonicalName()), error);
        }
    }
    return result;
}
Also used : SignerException(org.demoiselle.signer.policy.impl.cades.SignerException)

Example 14 with SignerException

use of org.demoiselle.signer.policy.impl.cades.SignerException in project signer by demoiselle.

the class PKCS1SignerImpl method doSign.

/**
 * Performs the signature using the Java API.
 * It uses the algorithm value on property: algorithm.
 * If this property is not set, the {@link SignerAlgorithmEnum.DEFAULT} enumeration algorithm
 * will be used.
 * For this method it is necessary to inform the content and the private key.
 *
 * @param content Content to be signed.
 */
private byte[] doSign(byte[] content) {
    if (content == null) {
        throw new SignerException(cadesMessagesBundle.getString("error.value.null"));
    }
    if (this.privateKey == null) {
        throw new SignerException(cadesMessagesBundle.getString("error.private.key.null"));
    }
    if (this.algorithm == null) {
        this.algorithm = SignerAlgorithmEnum.DEFAULT.getAlgorithm();
    }
    Signature sign = null;
    byte[] result = null;
    try {
        if (this.provider != null) {
            sign = Signature.getInstance(this.algorithm, this.provider);
        } else {
            sign = Signature.getInstance(this.algorithm);
        }
        sign.initSign(this.privateKey);
        sign.update(content);
        result = sign.sign();
    } catch (NoSuchAlgorithmException exception) {
        throw new SignerException(cadesMessagesBundle.getString("error.load.algorithm", algorithm), exception);
    } catch (InvalidKeyException exception) {
        throw new SignerException(cadesMessagesBundle.getString("error.private.key.invalid"), exception);
    } catch (SignatureException exception) {
        throw new SignerException(cadesMessagesBundle.getString("error.sign.exception"), exception);
    }
    return result;
}
Also used : Signature(java.security.Signature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) SignerException(org.demoiselle.signer.policy.impl.cades.SignerException)

Example 15 with SignerException

use of org.demoiselle.signer.policy.impl.cades.SignerException in project signer by demoiselle.

the class PKCS1SignerImpl method check.

/**
 * Performs checking for signed content using the Java API.
 *  You must enter the original content and signature for verification.
 *  It uses the value algorithm of property: algorithm. If this property is not set,
 *  the @link SignerAlgorithmEnum.DEFAULT enumeration algorithm will be used.
 *  For this method it is necessary to inform the original content, signed content and the public key.
 *
 * @param content Original content to be compared to signed content.
 * @param signed Signed content to be verified.
 */
@Override
public boolean check(byte[] content, byte[] signed) {
    if (content == null) {
        throw new SignerException(cadesMessagesBundle.getString("error.value.null"));
    }
    if (signed == null) {
        throw new SignerException(cadesMessagesBundle.getString("error.content.signed.null"));
    }
    if (this.publicKey == null) {
        throw new SignerException(cadesMessagesBundle.getString("error.public.key.null"));
    }
    if (this.algorithm == null) {
        this.algorithm = SignerAlgorithmEnum.DEFAULT.getAlgorithm();
    }
    Signature signature = null;
    boolean result = false;
    try {
        if (this.provider != null) {
            signature = Signature.getInstance(this.algorithm, this.provider);
        } else {
            signature = Signature.getInstance(this.algorithm);
        }
        signature.initVerify(this.publicKey);
        signature.update(content);
        result = signature.verify(signed);
    } catch (NoSuchAlgorithmException exception) {
        throw new SignerException(cadesMessagesBundle.getString("error.load.algorithm", this.algorithm), exception);
    } catch (InvalidKeyException exception) {
        throw new SignerException(cadesMessagesBundle.getString("error.public.key.invalid"), exception);
    } catch (SignatureException exception) {
        throw new SignerException(cadesMessagesBundle.getString("error.check.exception"), exception);
    }
    return result;
}
Also used : Signature(java.security.Signature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) SignerException(org.demoiselle.signer.policy.impl.cades.SignerException)

Aggregations

SignerException (org.demoiselle.signer.policy.impl.cades.SignerException)24 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)11 Attribute (org.bouncycastle.asn1.cms.Attribute)11 X509Certificate (java.security.cert.X509Certificate)10 IOException (java.io.IOException)9 CertificateEncodingException (java.security.cert.CertificateEncodingException)9 DERSet (org.bouncycastle.asn1.DERSet)8 CMSSignedData (org.bouncycastle.cms.CMSSignedData)8 CertificateCoreException (org.demoiselle.signer.core.exception.CertificateCoreException)8 CMSException (org.bouncycastle.cms.CMSException)7 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)7 DERSequence (org.bouncycastle.asn1.DERSequence)6 TSPException (org.bouncycastle.tsp.TSPException)6 Timestamp (org.demoiselle.signer.timestamp.Timestamp)6 ArrayList (java.util.ArrayList)5 SignedOrUnsignedAttribute (org.demoiselle.signer.policy.impl.cades.pkcs7.attribute.SignedOrUnsignedAttribute)5 UnsignedAttribute (org.demoiselle.signer.policy.impl.cades.pkcs7.attribute.UnsignedAttribute)5 ParseException (java.text.ParseException)4 CMSSignerDigestMismatchException (org.bouncycastle.cms.CMSSignerDigestMismatchException)4 SignerInformation (org.bouncycastle.cms.SignerInformation)4