Search in sources :

Example 1 with DecoderObject

use of es.gob.jmulticard.asn1.DecoderObject 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)

Example 2 with DecoderObject

use of es.gob.jmulticard.asn1.DecoderObject 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$
            "No se ha podido instanciar un " + this.elementsType.getName() + " en la secuencia: " + // $NON-NLS-1$
            e, // $NON-NLS-1$
            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 3 with DecoderObject

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

the class ContextSpecific method decodeValue.

@Override
protected void decodeValue() throws Asn1Exception, TlvException {
    final Tlv tlv = new Tlv(this.getRawDerValue());
    final DecoderObject tmpDo;
    try {
        tmpDo = this.elementType.getConstructor().newInstance();
    } catch (final Exception e) {
        throw new Asn1Exception(// $NON-NLS-1$ //$NON-NLS-2$
        "No se ha podido instanciar un " + this.elementType.getName() + " en el contexto especifico: " + e, // $NON-NLS-1$ //$NON-NLS-2$
        e);
    }
    tmpDo.setDerValue(tlv.getValue());
    this.object = 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 4 with DecoderObject

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

the class Record method decodeValue.

@Override
protected void decodeValue() throws Asn1Exception, TlvException {
    if (getRawDerValue().length == 0) {
        // $NON-NLS-1$
        throw new Asn1Exception("El valor del objeto ASN.1 esta vacio");
    }
    int offset = 0;
    Tlv tlv;
    byte[] remainingBytes;
    DecoderObject tmpDo;
    for (int i = 0; i < this.elementsTypes.length; i++) {
        try {
            remainingBytes = new byte[getRawDerValue().length - offset];
            System.arraycopy(getRawDerValue(), offset, remainingBytes, 0, remainingBytes.length);
            tlv = new Tlv(remainingBytes);
            try {
                tmpDo = this.elementsTypes[i].getElementType().getConstructor().newInstance();
            } catch (final Exception e) {
                throw new Asn1Exception(// $NON-NLS-1$
                "No se ha podido instanciar un " + this.elementsTypes[i].getElementType().getName() + " en la posicion " + Integer.toString(i) + " del registro: " + // $NON-NLS-1$ //$NON-NLS-2$
                e, // $NON-NLS-1$ //$NON-NLS-2$
                e);
            }
            tmpDo.checkTag(tlv.getTag());
        } 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$
            throw new Asn1Exception("Error en el elemento " + i + " del registro ASN.1: " + e, e);
        }
        offset = offset + tlv.getBytes().length;
        tmpDo.setDerValue(tlv.getBytes());
        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 5 with DecoderObject

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

the class TestDerBoolean method testGetBytes.

/**
 * Test method for {@link es.gob.jmulticard.asn1.DecoderObject#getBytes()}.
 * @throws TlvException Si no se puede crear el TLV.
 * @throws Asn1Exception Si falla la creaci&oacute;n del tipo ASN1.
 */
public static final void testGetBytes() throws Asn1Exception, TlvException {
    final DerBoolean db = new DerBoolean();
    db.setDerValue(new byte[] { (byte) 0x01, (byte) 0x01, (byte) 0x00 });
    // $NON-NLS-1$
    Assert.assertEquals("010100", HexUtils.hexify(db.getBytes(), false));
}
Also used : DerBoolean(es.gob.jmulticard.asn1.der.DerBoolean)

Aggregations

Asn1Exception (es.gob.jmulticard.asn1.Asn1Exception)5 TlvException (es.gob.jmulticard.asn1.TlvException)5 DecoderObject (es.gob.jmulticard.asn1.DecoderObject)4 Tlv (es.gob.jmulticard.asn1.Tlv)4 DerBoolean (es.gob.jmulticard.asn1.der.DerBoolean)2