Search in sources :

Example 1 with Iso7816FourCardException

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

the class Dnie method changePIN.

/**
 * Realiza la operación de cambio de PIN. Necesita tener un canal administrativo abierto.
 * @param oldPin PIN actual.
 * @param newPin PIN nuevo.
 * @return APDU de respuesta de la operación.
 * @throws CryptoCardException Cuando se produce un error en el cambio de PIN.
 * @throws PinException Si el PIN actual es incorrecto.
 * @throws AuthenticationModeLockedException Cuando el DNIe está bloqueado.
 */
public byte[] changePIN(final String oldPin, final String newPin) throws CryptoCardException, PinException, AuthenticationModeLockedException {
    openSecureChannelIfNotAlreadyOpened();
    try {
        // Seleccion de directorio maestro
        selectMasterFile();
        // Seleccion de fichero de PIN por Id
        final byte[] pinFile = new byte[] { (byte) 0x00, (byte) 0x00 };
        selectFileById(pinFile);
        // Envio de APDU de cambio de PIN
        final CommandApdu apdu = new ChangePINApduCommand(oldPin.getBytes(), newPin.getBytes());
        final ResponseApdu res = getConnection().transmit(apdu);
        if (!res.isOk()) {
            throw new DnieCardException(// $NON-NLS-1$
            "Error en el establecimiento de las variables de entorno para el cambio de PIN", // $NON-NLS-1$
            res.getStatusWord());
        }
        return res.getData();
    } catch (final LostChannelException e) {
        // $NON-NLS-1$
        LOGGER.warning("Se ha perdido el canal seguro para cambiar el PIN, se procede a recuperarlo: " + e);
        try {
            getConnection().close();
            if (getConnection() instanceof Cwa14890Connection) {
                setConnection(((Cwa14890Connection) getConnection()).getSubConnection());
            }
            // se terminara provocando un desbordamiento de pila.
            return changePIN(oldPin, newPin);
        } catch (final Exception ex) {
            // $NON-NLS-1$
            throw new DnieCardException("No se pudo recuperar el canal seguro para firmar: " + ex, ex);
        }
    } catch (final ApduConnectionException e) {
        // $NON-NLS-1$
        throw new DnieCardException("Error en la transmision de comandos a la tarjeta: " + e, e);
    } catch (final Iso7816FourCardException e) {
        // $NON-NLS-1$
        throw new DnieCardException("No se pudo seleccionar el fichero de PIN de la tarjeta: " + e, e);
    }
}
Also used : LostChannelException(es.gob.jmulticard.apdu.connection.LostChannelException) Iso7816FourCardException(es.gob.jmulticard.card.iso7816four.Iso7816FourCardException) CommandApdu(es.gob.jmulticard.apdu.CommandApdu) Cwa14890Connection(es.gob.jmulticard.apdu.connection.cwa14890.Cwa14890Connection) ResponseApdu(es.gob.jmulticard.apdu.ResponseApdu) ChangePINApduCommand(es.gob.jmulticard.apdu.dnie.ChangePINApduCommand) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) AuthenticationModeLockedException(es.gob.jmulticard.card.AuthenticationModeLockedException) BadPinException(es.gob.jmulticard.card.BadPinException) PinException(es.gob.jmulticard.card.PinException) Iso7816FourCardException(es.gob.jmulticard.card.iso7816four.Iso7816FourCardException) AccessControlException(java.security.AccessControlException) SecureChannelException(es.gob.jmulticard.apdu.connection.cwa14890.SecureChannelException) CryptoCardException(es.gob.jmulticard.card.CryptoCardException) LostChannelException(es.gob.jmulticard.apdu.connection.LostChannelException) CancelledOperationException(es.gob.jmulticard.CancelledOperationException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException)

Example 2 with Iso7816FourCardException

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

the class Ceres method verifyPin.

@Override
public void verifyPin(final PasswordCallback pinPc) throws ApduConnectionException, PinException {
    if (pinPc == null) {
        // $NON-NLS-1$
        throw new PinException("No se ha establecido un PasswordCallback");
    }
    final CommandApdu chv = new CeresVerifyApduCommand(CLA, pinPc);
    final ResponseApdu verifyResponse = sendArbitraryApdu(chv);
    if (!verifyResponse.isOk()) {
        if (verifyResponse.getStatusWord().getMsb() == ERROR_PIN_SW1 || verifyResponse.getStatusWord().getMsb() == ERROR_PIN_SW2) {
            if (AUTO_RETRY) {
                this.passwordCallback = null;
                verifyPin(getInternalPasswordCallback());
                return;
            }
            throw new BadPinException(verifyResponse.getStatusWord().getLsb() - (byte) 0xC0);
        } else if (new StatusWord((byte) 0x69, (byte) 0x83).equals(verifyResponse.getStatusWord())) {
            throw new AuthenticationModeLockedException();
        }
        throw new ApduConnectionException(new Iso7816FourCardException(// $NON-NLS-1$ //$NON-NLS-2$
        "Error en la verificacion de PIN (" + verifyResponse.getStatusWord() + ")", verifyResponse.getStatusWord()));
    }
}
Also used : AuthenticationModeLockedException(es.gob.jmulticard.card.AuthenticationModeLockedException) Iso7816FourCardException(es.gob.jmulticard.card.iso7816four.Iso7816FourCardException) CommandApdu(es.gob.jmulticard.apdu.CommandApdu) CeresVerifyApduCommand(es.gob.jmulticard.apdu.ceres.CeresVerifyApduCommand) BadPinException(es.gob.jmulticard.card.BadPinException) StatusWord(es.gob.jmulticard.apdu.StatusWord) ResponseApdu(es.gob.jmulticard.apdu.ResponseApdu) BadPinException(es.gob.jmulticard.card.BadPinException) PinException(es.gob.jmulticard.card.PinException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException)

Example 3 with Iso7816FourCardException

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

the class SmartCafePkcs15Applet method verifyPin.

@Override
public void verifyPin(final PasswordCallback psc) throws ApduConnectionException, PinException {
    if (psc == null) {
        throw new IllegalArgumentException(// $NON-NLS-1$
        "No se puede verificar el titular con un PasswordCallback nulo");
    }
    VerifyApduCommand verifyCommandApdu = new VerifyApduCommand(psc);
    final ResponseApdu verifyResponse = getConnection().transmit(verifyCommandApdu);
    verifyCommandApdu = null;
    if (!verifyResponse.isOk()) {
        if (verifyResponse.getStatusWord().getMsb() == ERROR_PIN_SW1) {
            throw new BadPinException(verifyResponse.getStatusWord().getLsb() - (byte) 0xC0);
        } else if (verifyResponse.getStatusWord().getMsb() == (byte) 0x69 && verifyResponse.getStatusWord().getLsb() == (byte) 0x83) {
            throw new AuthenticationModeLockedException();
        } else {
            throw new ApduConnectionException(new Iso7816FourCardException(// $NON-NLS-1$ //$NON-NLS-2$
            "Error en la verificacion de PIN (" + verifyResponse.getStatusWord() + ")", verifyResponse.getStatusWord()));
        }
    }
}
Also used : AuthenticationModeLockedException(es.gob.jmulticard.card.AuthenticationModeLockedException) Iso7816FourCardException(es.gob.jmulticard.card.iso7816four.Iso7816FourCardException) BadPinException(es.gob.jmulticard.card.BadPinException) ResponseApdu(es.gob.jmulticard.apdu.ResponseApdu) VerifyApduCommand(es.gob.jmulticard.apdu.gide.VerifyApduCommand) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException)

Example 4 with Iso7816FourCardException

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

the class SmartCafePkcs15Applet method preloadCertificates.

private void preloadCertificates() throws FileNotFoundException, Iso7816FourCardException, IOException, Asn1Exception, TlvException {
    selectMasterFile();
    // Seleccionamos el ODF, no nos devuelve FCI ni nada
    selectFileById(ODF_PATH);
    // Leemos el ODF
    final byte[] odfBytes = readBinaryComplete(162);
    final Odf odf = new Odf();
    odf.setDerValue(odfBytes);
    // Sacamos del ODF la ruta del CDF
    final Path cdfPath = odf.getCdfPath();
    // Leemos el CDF
    final Cdf cdf = new Cdf();
    try {
        selectMasterFile();
        final byte[] cdfBytes = selectFileByIdAndRead(cdfPath.getPathBytes());
        cdf.setDerValue(cdfBytes);
    } catch (final Exception e) {
        throw new ApduConnectionException(// $NON-NLS-1$
        "No se ha podido cargar el CDF de la tarjeta: " + e, // $NON-NLS-1$
        e);
    }
    final CertificateFactory cf;
    try {
        // $NON-NLS-1$
        cf = CertificateFactory.getInstance("X.509");
    } catch (final CertificateException e) {
        // $NON-NLS-1$
        throw new IOException("Error obteniendo la factoria de certificados X.509: " + e, e);
    }
    if (cdf.getCertificateCount() < 1) {
        // $NON-NLS-1$
        LOGGER.warning("La tarjeta no contiene ningun certificado");
    }
    for (int i = 0; i < cdf.getCertificateCount(); i++) {
        try {
            int fileLength = -1;
            Location certLocation = new Location(cdf.getCertificatePath(i));
            while (certLocation != null) {
                final byte[] id = certLocation.getFile();
                try {
                    fileLength = selectFileById(id);
                } catch (final FileNotFoundException e) {
                    System.out.println(// $NON-NLS-1$//$NON-NLS-2$
                    "El CDF indicaba un certificado en la ruta '" + certLocation + "', pero un elemento de esta no existe, se ignorara: " + e);
                }
                certLocation = certLocation.getChild();
            }
            final byte[] certBytes;
            if (fileLength > 0) {
                certBytes = readBinaryComplete(fileLength);
            } else {
                // A veces hay punteros que apuntan a localizaciones vacias
                LOGGER.warning(// $NON-NLS-1$ //$NON-NLS-2$
                "El certificado " + i + " del dispositivo esta vacio");
                continue;
            }
            CERTS_BY_ALIAS.put(cdf.getCertificateAlias(i), (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(certBytes)));
        } catch (final Exception e) {
            // Puede darse el caso de que el puntero apunte a algo que no es un certificado
            LOGGER.severe(// $NON-NLS-1$ //$NON-NLS-2$
            "Error en la lectura del certificado " + i + " del dispositivo: " + e);
            continue;
        }
    }
}
Also used : Path(es.gob.jmulticard.asn1.der.pkcs15.Path) Cdf(es.gob.jmulticard.asn1.der.pkcs15.Cdf) FileNotFoundException(es.gob.jmulticard.card.iso7816four.FileNotFoundException) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) CertificateFactory(java.security.cert.CertificateFactory) 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) BadPinException(es.gob.jmulticard.card.BadPinException) PinException(es.gob.jmulticard.card.PinException) Iso7816FourCardException(es.gob.jmulticard.card.iso7816four.Iso7816FourCardException) Asn1Exception(es.gob.jmulticard.asn1.Asn1Exception) CryptoCardException(es.gob.jmulticard.card.CryptoCardException) InvalidCardException(es.gob.jmulticard.card.InvalidCardException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) TlvException(es.gob.jmulticard.asn1.TlvException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) Odf(es.gob.jmulticard.asn1.der.pkcs15.Odf) ByteArrayInputStream(java.io.ByteArrayInputStream) Location(es.gob.jmulticard.card.Location)

Example 5 with Iso7816FourCardException

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

the class SmartCafePkcs15Applet method selectFileById.

/**
 * Selecciona un fichero (DF o EF).
 * @param id Identificador del fichero a seleccionar.
 * @return Tama&ntilde;o del fichero seleccionado.
 * @throws ApduConnectionException Si hay problemas en el env&iacute;o de la APDU.
 * @throws Iso7816FourCardException Si falla la selecci&oacute;n de fichero.
 */
@Override
public int selectFileById(final byte[] id) throws ApduConnectionException, Iso7816FourCardException {
    final CommandApdu selectCommand = new SelectFileByIdApduCommand(getCla(), id);
    final ResponseApdu res = getConnection().transmit(selectCommand);
    if (HexUtils.arrayEquals(res.getBytes(), new byte[] { (byte) 0x6a, (byte) 0x82 })) {
        throw new FileNotFoundException(id);
    }
    final SelectFileApduResponse response = new SelectFileApduResponse(res);
    if (response.isOk()) {
        return HexUtils.getUnsignedInt(new byte[] { response.getData()[4], response.getData()[5] }, // Offset
        0);
    }
    final StatusWord sw = response.getStatusWord();
    if (sw.equals(new StatusWord((byte) 0x6A, (byte) 0x82))) {
        throw new FileNotFoundException(id);
    }
    throw new Iso7816FourCardException(sw, selectCommand);
}
Also used : SelectFileByIdApduCommand(es.gob.jmulticard.apdu.iso7816four.SelectFileByIdApduCommand) SelectFileApduResponse(es.gob.jmulticard.apdu.iso7816four.SelectFileApduResponse) Iso7816FourCardException(es.gob.jmulticard.card.iso7816four.Iso7816FourCardException) CommandApdu(es.gob.jmulticard.apdu.CommandApdu) StatusWord(es.gob.jmulticard.apdu.StatusWord) FileNotFoundException(es.gob.jmulticard.card.iso7816four.FileNotFoundException) ResponseApdu(es.gob.jmulticard.apdu.ResponseApdu)

Aggregations

Iso7816FourCardException (es.gob.jmulticard.card.iso7816four.Iso7816FourCardException)11 ApduConnectionException (es.gob.jmulticard.apdu.connection.ApduConnectionException)9 BadPinException (es.gob.jmulticard.card.BadPinException)7 IOException (java.io.IOException)7 AuthenticationModeLockedException (es.gob.jmulticard.card.AuthenticationModeLockedException)6 CertificateException (java.security.cert.CertificateException)6 ResponseApdu (es.gob.jmulticard.apdu.ResponseApdu)5 CryptoCardException (es.gob.jmulticard.card.CryptoCardException)5 FileNotFoundException (es.gob.jmulticard.card.iso7816four.FileNotFoundException)5 CommandApdu (es.gob.jmulticard.apdu.CommandApdu)4 InvalidCardException (es.gob.jmulticard.card.InvalidCardException)4 PinException (es.gob.jmulticard.card.PinException)4 Asn1Exception (es.gob.jmulticard.asn1.Asn1Exception)3 TlvException (es.gob.jmulticard.asn1.TlvException)3 Cdf (es.gob.jmulticard.asn1.der.pkcs15.Cdf)3 Location (es.gob.jmulticard.card.Location)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 CertificateFactory (java.security.cert.CertificateFactory)3 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)3 StatusWord (es.gob.jmulticard.apdu.StatusWord)2