Search in sources :

Example 11 with CryptoCardException

use of es.gob.jmulticard.card.CryptoCardException in project jmulticard by ctt-gob-es.

the class CeresSc method signOperation.

@Override
protected byte[] signOperation(final byte[] data, final String algorithm, final PrivateKeyReference privateKeyReference) throws CryptoCardException, PinException {
    openSecureChannelIfNotAlreadyOpened();
    ResponseApdu res;
    try {
        CommandApdu apdu = new MseSetComputationApduCommand((byte) 0x00, ((DniePrivateKeyReference) privateKeyReference).getKeyPath().getLastFilePath(), null);
        res = getConnection().transmit(apdu);
        if (!res.isOk()) {
            throw new DnieCardException(// $NON-NLS-1$
            "Error en el establecimiento de las clave de firma con respuesta: " + res.getStatusWord(), // $NON-NLS-1$
            res.getStatusWord());
        }
        final byte[] digestInfo;
        try {
            digestInfo = DigestInfo.encode(algorithm, data, this.cryptoHelper);
        } catch (final IOException e) {
            // $NON-NLS-1$
            throw new DnieCardException("Error en el calculo del hash para firmar: " + e, e);
        }
        apdu = new PsoSignHashApduCommand((byte) 0x00, digestInfo);
        res = getConnection().transmit(apdu);
        if (!res.isOk()) {
            throw new DnieCardException(// $NON-NLS-1$
            "Error durante la operacion de firma con respuesta: " + res.getStatusWord(), // $NON-NLS-1$
            res.getStatusWord());
        }
    } catch (final LostChannelException e) {
        try {
            getConnection().close();
            if (getConnection() instanceof Cwa14890Connection) {
                setConnection(((Cwa14890Connection) getConnection()).getSubConnection());
            }
        } catch (final Exception ex) {
            // $NON-NLS-1$
            throw new DnieCardException("No se pudo recuperar el canal seguro para firmar: " + ex, ex);
        }
        return signOperation(data, algorithm, privateKeyReference);
    } catch (final ApduConnectionException e) {
        // $NON-NLS-1$
        throw new DnieCardException("Error en la transmision de comandos a la tarjeta: " + e, e);
    }
    return res.getData();
}
Also used : LostChannelException(es.gob.jmulticard.apdu.connection.LostChannelException) MseSetComputationApduCommand(es.gob.jmulticard.apdu.iso7816four.MseSetComputationApduCommand) CommandApdu(es.gob.jmulticard.apdu.CommandApdu) PsoSignHashApduCommand(es.gob.jmulticard.apdu.iso7816eight.PsoSignHashApduCommand) Cwa14890Connection(es.gob.jmulticard.apdu.connection.cwa14890.Cwa14890Connection) ResponseApdu(es.gob.jmulticard.apdu.ResponseApdu) IOException(java.io.IOException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) CryptoCardException(es.gob.jmulticard.card.CryptoCardException) LostChannelException(es.gob.jmulticard.apdu.connection.LostChannelException) InvalidCardException(es.gob.jmulticard.card.InvalidCardException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) PinException(es.gob.jmulticard.card.PinException) TlvException(es.gob.jmulticard.asn1.TlvException) Iso7816FourCardException(es.gob.jmulticard.card.iso7816four.Iso7816FourCardException) Asn1Exception(es.gob.jmulticard.asn1.Asn1Exception) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException)

Example 12 with CryptoCardException

use of es.gob.jmulticard.card.CryptoCardException in project jmulticard by ctt-gob-es.

the class SmartCafePkcs15Applet method sign.

@Override
public byte[] sign(final byte[] data, final String algorithm, final PrivateKeyReference keyRef) throws CryptoCardException, PinException {
    if (data == null) {
        // $NON-NLS-1$
        throw new CryptoCardException("Los datos a firmar no pueden ser nulos");
    }
    if (keyRef == null) {
        // $NON-NLS-1$
        throw new IllegalArgumentException("La clave privada no puede ser nula");
    }
    if (!(keyRef instanceof SmartCafePrivateKeyReference)) {
        throw new IllegalArgumentException(// $NON-NLS-1$
        "La clave proporcionada debe ser de tipo " + SmartCafePrivateKeyReference.class.getName() + // $NON-NLS-1$
        ", pero se ha recibido de tipo " + keyRef.getClass().getName());
    }
    final SmartCafePrivateKeyReference scPrivateKey = (SmartCafePrivateKeyReference) keyRef;
    // Pedimos el PIN si no se ha pedido antes
    if (!this.authenticated) {
        try {
            verifyPin(getInternalPasswordCallback());
            this.authenticated = true;
        } catch (final ApduConnectionException e1) {
            // $NON-NLS-1$
            throw new CryptoCardException("Error en la verificacion de PIN: " + e1, e1);
        }
    }
    // Enviamos el MSE SET for Computation
    ResponseApdu res = null;
    try {
        res = sendArbitraryApdu(new MseSetComputationApduCommand(// CLA
        (byte) 0x01, new byte[] { (byte) scPrivateKey.getKeyOrdinal() }, // RSA
        new byte[] { (byte) 0x02 }));
    } catch (final ApduConnectionException e) {
        throw new CryptoCardException(// $NON-NLS-1$ //$NON-NLS-2$
        "Error estableciendo la clave y el algoritmo de firma (repuesta=" + res + "): " + e, // $NON-NLS-1$ //$NON-NLS-2$
        e);
    }
    if (res == null || !res.isOk()) {
        throw new CryptoCardException(// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
        "No se ha podido establecer la clave y el algoritmo de firma" + (res != null ? " (repuesta=" + res + ")" : ""));
    }
    // Creamos el DigestInfo
    final byte[] digestInfo;
    try {
        digestInfo = DigestInfo.encode(algorithm, data, this.cryptoHelper);
    } catch (final IOException e) {
        // $NON-NLS-1$
        throw new CryptoCardException("Error en el calculo de la huella para firmar: " + e, e);
    }
    // Y lo enviamos a firmar
    try {
        res = sendArbitraryApdu(new PsoSignHashApduCommand((byte) 0x01, digestInfo));
    } catch (final ApduConnectionException e) {
        throw new CryptoCardException(// $NON-NLS-1$ //$NON-NLS-2$
        "Error firmando (repuesta=" + res + "): " + e, // $NON-NLS-1$ //$NON-NLS-2$
        e);
    }
    if (res == null || !res.isOk()) {
        throw new CryptoCardException(// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
        "No se ha podido firmar el DigestInfo" + (res != null ? " (repuesta=" + res + ")" : ""));
    }
    return res.getData();
}
Also used : MseSetComputationApduCommand(es.gob.jmulticard.apdu.iso7816four.MseSetComputationApduCommand) PsoSignHashApduCommand(es.gob.jmulticard.apdu.iso7816eight.PsoSignHashApduCommand) ResponseApdu(es.gob.jmulticard.apdu.ResponseApdu) IOException(java.io.IOException) CryptoCardException(es.gob.jmulticard.card.CryptoCardException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException)

Aggregations

CryptoCardException (es.gob.jmulticard.card.CryptoCardException)12 ApduConnectionException (es.gob.jmulticard.apdu.connection.ApduConnectionException)11 IOException (java.io.IOException)9 Iso7816FourCardException (es.gob.jmulticard.card.iso7816four.Iso7816FourCardException)8 ResponseApdu (es.gob.jmulticard.apdu.ResponseApdu)7 CertificateException (java.security.cert.CertificateException)7 PinException (es.gob.jmulticard.card.PinException)6 Cwa14890Connection (es.gob.jmulticard.apdu.connection.cwa14890.Cwa14890Connection)5 BadPinException (es.gob.jmulticard.card.BadPinException)5 CommandApdu (es.gob.jmulticard.apdu.CommandApdu)4 AuthenticationModeLockedException (es.gob.jmulticard.card.AuthenticationModeLockedException)4 InvalidCardException (es.gob.jmulticard.card.InvalidCardException)4 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)4 ApduConnection (es.gob.jmulticard.apdu.connection.ApduConnection)3 LostChannelException (es.gob.jmulticard.apdu.connection.LostChannelException)3 PsoSignHashApduCommand (es.gob.jmulticard.apdu.iso7816eight.PsoSignHashApduCommand)3 MseSetComputationApduCommand (es.gob.jmulticard.apdu.iso7816four.MseSetComputationApduCommand)3 Asn1Exception (es.gob.jmulticard.asn1.Asn1Exception)3 TlvException (es.gob.jmulticard.asn1.TlvException)3 FileNotFoundException (es.gob.jmulticard.card.iso7816four.FileNotFoundException)3