Search in sources :

Example 1 with SignDataApduCommand

use of es.gob.jmulticard.apdu.ceres.SignDataApduCommand in project jmulticard by ctt-gob-es.

the class Ceres 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 CeresPrivateKeyReference)) {
        throw new IllegalArgumentException(// $NON-NLS-1$
        "La clave proporcionada debe ser de tipo CeresPrivateKeyReference, pero se ha recibido de tipo " + keyRef.getClass().getName());
    }
    final CeresPrivateKeyReference ceresPrivateKey = (CeresPrivateKeyReference) 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);
        }
    }
    final byte[] digestInfo;
    try {
        digestInfo = DigestInfo.encode(algorithm, data, this.cryptoHelper);
    } catch (final Exception e) {
        throw new CryptoCardException(// $NON-NLS-1$ //$NON-NLS-2$
        "Error creando el DigestInfo para la firma con el algoritmo " + algorithm + ": " + e, // $NON-NLS-1$ //$NON-NLS-2$
        e);
    }
    loadData(ceresPrivateKey.getKeyBitSize(), digestInfo);
    final ResponseApdu res;
    final CommandApdu cmd = new SignDataApduCommand(// Referencia
    ceresPrivateKey.getKeyReference(), // Tamano en bits de la clave
    ceresPrivateKey.getKeyBitSize());
    try {
        res = sendArbitraryApdu(cmd);
    } catch (final Exception e) {
        // $NON-NLS-1$
        throw new CryptoCardException("Error firmando los datos: " + e, e);
    }
    if (!res.isOk()) {
        throw new CryptoCardException(// $NON-NLS-1$
        "No se han podido firmar los datos. Respuesta: " + HexUtils.hexify(res.getBytes(), true));
    }
    return res.getData();
}
Also used : CommandApdu(es.gob.jmulticard.apdu.CommandApdu) ResponseApdu(es.gob.jmulticard.apdu.ResponseApdu) SignDataApduCommand(es.gob.jmulticard.apdu.ceres.SignDataApduCommand) CryptoCardException(es.gob.jmulticard.card.CryptoCardException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) AuthenticationModeLockedException(es.gob.jmulticard.card.AuthenticationModeLockedException) FileNotFoundException(es.gob.jmulticard.card.iso7816four.FileNotFoundException) CryptoCardException(es.gob.jmulticard.card.CryptoCardException) InvalidCardException(es.gob.jmulticard.card.InvalidCardException) BadPinException(es.gob.jmulticard.card.BadPinException) 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)

Aggregations

CommandApdu (es.gob.jmulticard.apdu.CommandApdu)1 ResponseApdu (es.gob.jmulticard.apdu.ResponseApdu)1 SignDataApduCommand (es.gob.jmulticard.apdu.ceres.SignDataApduCommand)1 ApduConnectionException (es.gob.jmulticard.apdu.connection.ApduConnectionException)1 Asn1Exception (es.gob.jmulticard.asn1.Asn1Exception)1 TlvException (es.gob.jmulticard.asn1.TlvException)1 AuthenticationModeLockedException (es.gob.jmulticard.card.AuthenticationModeLockedException)1 BadPinException (es.gob.jmulticard.card.BadPinException)1 CryptoCardException (es.gob.jmulticard.card.CryptoCardException)1 InvalidCardException (es.gob.jmulticard.card.InvalidCardException)1 PinException (es.gob.jmulticard.card.PinException)1 FileNotFoundException (es.gob.jmulticard.card.iso7816four.FileNotFoundException)1 Iso7816FourCardException (es.gob.jmulticard.card.iso7816four.Iso7816FourCardException)1 IOException (java.io.IOException)1 CertificateException (java.security.cert.CertificateException)1 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)1