Search in sources :

Example 1 with TlvException

use of es.gob.jmulticard.asn1.TlvException 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 2 with TlvException

use of es.gob.jmulticard.asn1.TlvException in project jmulticard by ctt-gob-es.

the class TestTlvException method testCreationTlvException.

/**
 * Test method for {@link es.gob.jmulticard.asn1.TlvException#TlvException(java.lang.String)} and
 * {@link es.gob.jmulticard.asn1.TlvException#TlvException(String, Throwable)}.
 */
public static final void testCreationTlvException() {
    // $NON-NLS-1$
    Assert.assertNotNull(new TlvException(""));
    // $NON-NLS-1$ //$NON-NLS-2$
    Assert.assertNotNull(new TlvException("", new Exception("")));
}
Also used : TlvException(es.gob.jmulticard.asn1.TlvException) TlvException(es.gob.jmulticard.asn1.TlvException)

Example 3 with TlvException

use of es.gob.jmulticard.asn1.TlvException in project jmulticard by ctt-gob-es.

the class TestAsn1SimpleTypes method testUtf8StringCreationWithBadData.

/**
 * Prueba la creaci&oacute; de un tipo <code>UTF8String</code> con datos incorrectos.
 * @throws TlvException Si no se puede crear el TLV.
 */
@Test
@SuppressWarnings("static-method")
public void testUtf8StringCreationWithBadData() throws TlvException {
    final Utf8String u = new Utf8String();
    try {
        u.setDerValue(new byte[] { (byte) 0x00, (byte) 0x01, (byte) 0xff });
    } catch (final Asn1Exception e) {
        // $NON-NLS-1$
        System.out.println("Fallo esperado: " + e);
        return;
    }
    // $NON-NLS-1$
    Assert.fail("Tendria que haber saltado un Asn1Exception");
}
Also used : Utf8String(es.gob.jmulticard.asn1.der.Utf8String) Asn1Exception(es.gob.jmulticard.asn1.Asn1Exception) Test(org.junit.Test)

Example 4 with TlvException

use of es.gob.jmulticard.asn1.TlvException in project jmulticard by ctt-gob-es.

the class TestAsn1SimpleTypes method testBitStringCreationWithBadData.

/**
 * Prueba la creaci&oacute; de un tipo <code>BitString</code> con datos incorrectos.
 * @throws TlvException Si no se puede crear el TLV.
 */
@Test
@SuppressWarnings("static-method")
public void testBitStringCreationWithBadData() throws TlvException {
    final BitString u = new AccessFlags();
    try {
        u.setDerValue(new byte[] { (byte) 0x00, (byte) 0x01, (byte) 0xff });
    } catch (final Asn1Exception e) {
        // $NON-NLS-1$
        System.out.println("Fallo esperado: " + e);
        return;
    }
    // $NON-NLS-1$
    Assert.fail("Tendria que haber saltado un Asn1Exception");
}
Also used : BitString(es.gob.jmulticard.asn1.der.BitString) Asn1Exception(es.gob.jmulticard.asn1.Asn1Exception) AccessFlags(es.gob.jmulticard.asn1.der.pkcs15.AccessFlags) Test(org.junit.Test)

Example 5 with TlvException

use of es.gob.jmulticard.asn1.TlvException in project jmulticard by ctt-gob-es.

the class Sequence method decodeValue.

@Override
protected void decodeValue() throws Asn1Exception, TlvException {
    final Tlv mainTlv = new Tlv(getRawDerValue());
    checkTag(mainTlv.getTag());
    int offset = 0;
    Tlv tlv;
    byte[] remainingBytes;
    DecoderObject tmpDo;
    final byte[] rawValue = mainTlv.getValue();
    for (int i = 0; i < this.elementsTypes.length; i++) {
        remainingBytes = new byte[rawValue.length - offset];
        System.arraycopy(rawValue, offset, remainingBytes, 0, remainingBytes.length);
        try {
            tlv = new Tlv(remainingBytes);
            tmpDo = this.elementsTypes[i].getElementType().getConstructor().newInstance();
            tmpDo.checkTag(tlv.getTag());
            tmpDo.setDerValue(tlv.getBytes());
        } catch (final Exception e) {
            if (this.elementsTypes[i].isOptional()) {
                // Como no ha avanzado el offset, se reutilizara el tipo en el proximo elemento
                continue;
            }
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            throw new Asn1Exception("Error en el elemento " + i + " (" + this.elementsTypes[i].getElementType().getName() + ") de la secuencia ASN.1: " + e, e);
        }
        // El offset se avanza antes del continue de la opcionalidad
        offset = offset + tlv.getBytes().length;
        this.elements.add(tmpDo);
    }
}
Also used : DecoderObject(es.gob.jmulticard.asn1.DecoderObject) Asn1Exception(es.gob.jmulticard.asn1.Asn1Exception) TlvException(es.gob.jmulticard.asn1.TlvException) Asn1Exception(es.gob.jmulticard.asn1.Asn1Exception) Tlv(es.gob.jmulticard.asn1.Tlv)

Aggregations

TlvException (es.gob.jmulticard.asn1.TlvException)10 Asn1Exception (es.gob.jmulticard.asn1.Asn1Exception)9 Tlv (es.gob.jmulticard.asn1.Tlv)8 ApduConnectionException (es.gob.jmulticard.apdu.connection.ApduConnectionException)4 DecoderObject (es.gob.jmulticard.asn1.DecoderObject)4 IOException (java.io.IOException)4 Cdf (es.gob.jmulticard.asn1.der.pkcs15.Cdf)3 InvalidCardException (es.gob.jmulticard.card.InvalidCardException)3 Location (es.gob.jmulticard.card.Location)3 FileNotFoundException (es.gob.jmulticard.card.iso7816four.FileNotFoundException)3 Iso7816FourCardException (es.gob.jmulticard.card.iso7816four.Iso7816FourCardException)3 CertificateException (java.security.cert.CertificateException)3 X509Certificate (java.security.cert.X509Certificate)3 CommandApdu (es.gob.jmulticard.apdu.CommandApdu)2 Odf (es.gob.jmulticard.asn1.der.pkcs15.Odf)2 Path (es.gob.jmulticard.asn1.der.pkcs15.Path)2 Pkcs15Cdf (es.gob.jmulticard.asn1.der.pkcs15.Pkcs15Cdf)2 PrKdf (es.gob.jmulticard.asn1.der.pkcs15.PrKdf)2 AuthenticationModeLockedException (es.gob.jmulticard.card.AuthenticationModeLockedException)2 BadPinException (es.gob.jmulticard.card.BadPinException)2