Search in sources :

Example 6 with CertificateCoreException

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

the class CertificateLoaderImpl method loadFromToken.

/**
 * When a PIN(Personal Identification Number) was informed,
 * obtain the certificate from a Token or Smartcard, defined by ICP-BRASIL with the name A3.
 *
 * @param pinNumber personal id number
 * @return the certificate information in X509Certificate format
 */
@Override
public X509Certificate loadFromToken(String pinNumber) {
    if (this.keyStore == null) {
        KeyStoreLoader keyStoreLoader = KeyStoreLoaderFactory.factoryKeyStoreLoader();
        this.keyStore = keyStoreLoader.getKeyStore();
    }
    String alias;
    try {
        alias = this.keyStore.aliases().nextElement();
        return (X509Certificate) this.keyStore.getCertificateChain(alias)[0];
    } catch (KeyStoreException e) {
        throw new CertificateCoreException("", e);
    }
}
Also used : KeyStoreLoader(org.demoiselle.signer.core.keystore.loader.KeyStoreLoader) KeyStoreException(java.security.KeyStoreException) X509Certificate(java.security.cert.X509Certificate) CertificateCoreException(org.demoiselle.signer.core.exception.CertificateCoreException)

Example 7 with CertificateCoreException

use of org.demoiselle.signer.core.exception.CertificateCoreException 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 8 with CertificateCoreException

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

the class Utils method readContent.

/**
 * Loads the contents of a file from the disk
 *
 * @param parmFile Filename and path
 * @return The array of bytes in the file
 */
public static byte[] readContent(String parmFile) throws CertificateCoreException {
    try {
        File file = new File(parmFile);
        InputStream is = new FileInputStream(file);
        byte[] result = new byte[(int) file.length()];
        is.read(result);
        is.close();
        return result;
    } catch (FileNotFoundException ex) {
        throw new CertificateCoreException(ex.getMessage(), ex.getCause());
    } catch (IOException ex) {
        throw new CertificateCoreException(ex.getMessage(), ex.getCause());
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) CertificateCoreException(org.demoiselle.signer.core.exception.CertificateCoreException)

Example 9 with CertificateCoreException

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

the class Utils method writeContent.

/**
 * Writes a set of bytes to a file on disk
 *
 * @param content Content to be written to disk
 * @param parmFile Filename and path
 * @throws CertificateCoreException
 */
public static void writeContent(byte[] content, String parmFile) throws CertificateCoreException {
    try {
        File file = new File(parmFile);
        OutputStream os = new FileOutputStream(file);
        os.write(content);
        os.flush();
        os.close();
    } catch (IOException ex) {
        throw new CertificateCoreException(ex.getMessage(), ex.getCause());
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) CertificateCoreException(org.demoiselle.signer.core.exception.CertificateCoreException)

Example 10 with CertificateCoreException

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

the class TimeStampOperator method createRequest.

/**
 * Creates a time stamp request using a certificate of type PKCS12
 *
 * @param keystoreLocation key store location
 * @param pin personal identification number
 * @param alias alias
 * @param content content of the request
 * @return request as a byte[]
 * @throws CertificateCoreException exception
 */
public byte[] createRequest(String keystoreLocation, String pin, String alias, byte[] content, byte[] hash) throws CertificateCoreException {
    try {
        KeyStore ks = KeyStore.getInstance("PKCS12");
        ks.load(new FileInputStream(keystoreLocation), pin.toCharArray());
        PrivateKey pk = (PrivateKey) ks.getKey(alias, pin.toCharArray());
        Certificate[] certs = ks.getCertificateChain(alias);
        return this.createRequest(pk, certs, content, hash);
    } catch (NoSuchAlgorithmException | CertificateException | KeyStoreException | UnrecoverableKeyException | IOException ex) {
        throw new CertificateCoreException(ex.getMessage());
    }
}
Also used : PrivateKey(java.security.PrivateKey) UnrecoverableKeyException(java.security.UnrecoverableKeyException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) CertificateCoreException(org.demoiselle.signer.core.exception.CertificateCoreException) Certificate(java.security.cert.Certificate)

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