Search in sources :

Example 21 with ASN1Exception

use of com.github.zhenwei.core.asn1.ASN1Exception 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;
            }
            throw new Asn1Exception(// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            "Error en el elemento " + i + " (" + this.elementsTypes[i].getElementType().getName() + ") de la secuencia ASN.1", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            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)

Example 22 with ASN1Exception

use of com.github.zhenwei.core.asn1.ASN1Exception in project jmulticard by ctt-gob-es.

the class SequenceOf method decodeValue.

@Override
protected void decodeValue() throws Asn1Exception, TlvException {
    Tlv tlv = new Tlv(getRawDerValue());
    checkTag(tlv.getTag());
    int offset = 0;
    byte[] remainingBytes;
    DecoderObject tmpDo;
    final byte[] valueBytes = tlv.getValue();
    this.sequenceObjects = new Vector<>();
    while (offset < valueBytes.length) {
        remainingBytes = new byte[valueBytes.length - offset];
        System.arraycopy(valueBytes, offset, remainingBytes, 0, remainingBytes.length);
        tlv = new Tlv(remainingBytes);
        try {
            tmpDo = this.elementsType.getConstructor().newInstance();
        } catch (final Exception e) {
            throw new Asn1Exception(// $NON-NLS-1$ //$NON-NLS-2$
            "No se ha podido instanciar un " + this.elementsType.getName() + " en la secuencia", // $NON-NLS-1$ //$NON-NLS-2$
            e);
        }
        offset = offset + tlv.getBytes().length;
        tmpDo.checkTag(tlv.getTag());
        tmpDo.setDerValue(tlv.getBytes());
        this.sequenceObjects.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)

Example 23 with ASN1Exception

use of com.github.zhenwei.core.asn1.ASN1Exception in project jmulticard by ctt-gob-es.

the class ContextSpecific method decodeValue.

@Override
protected void decodeValue() throws Asn1Exception, TlvException {
    final DecoderObject tmpDo;
    try {
        tmpDo = this.elementType.getConstructor().newInstance();
    } catch (final IllegalAccessException | InstantiationException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
        throw new Asn1Exception(// $NON-NLS-1$ //$NON-NLS-2$
        "No se ha podido instanciar un " + this.elementType.getName() + " en el contexto especifico", // $NON-NLS-1$ //$NON-NLS-2$
        e);
    }
    final Tlv tlv = new Tlv(getRawDerValue());
    tmpDo.setDerValue(tlv.getValue());
    this.object = tmpDo;
}
Also used : DecoderObject(es.gob.jmulticard.asn1.DecoderObject) Asn1Exception(es.gob.jmulticard.asn1.Asn1Exception) InvocationTargetException(java.lang.reflect.InvocationTargetException) Tlv(es.gob.jmulticard.asn1.Tlv)

Example 24 with ASN1Exception

use of com.github.zhenwei.core.asn1.ASN1Exception 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 25 with ASN1Exception

use of com.github.zhenwei.core.asn1.ASN1Exception 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)

Aggregations

IOException (java.io.IOException)18 Asn1Exception (es.gob.jmulticard.asn1.Asn1Exception)16 ASN1Exception (com.unboundid.asn1.ASN1Exception)12 TlvException (es.gob.jmulticard.asn1.TlvException)12 Asn1Exception (sun.security.krb5.Asn1Exception)11 ASN1Element (com.unboundid.asn1.ASN1Element)7 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)7 NotNull (com.unboundid.util.NotNull)7 Iso7816FourCardException (es.gob.jmulticard.card.iso7816four.Iso7816FourCardException)7 CertificateException (java.security.cert.CertificateException)7 X509Certificate (java.security.cert.X509Certificate)7 Asn1Exception (org.kse.utilities.asn1.Asn1Exception)7 ASN1Exception (org.wildfly.security.asn1.ASN1Exception)6 ApduConnectionException (es.gob.jmulticard.apdu.connection.ApduConnectionException)5 Cdf (es.gob.jmulticard.asn1.der.pkcs15.Cdf)5 CryptoCardException (es.gob.jmulticard.card.CryptoCardException)5 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)4 DecoderObject (es.gob.jmulticard.asn1.DecoderObject)4 InvalidCardException (es.gob.jmulticard.card.InvalidCardException)4 DerValue (sun.security.util.DerValue)4