Search in sources :

Example 1 with BerLength

use of org.openmuc.jasn1.ber.BerLength in project jasn1 by openmuc.

the class BerAny method decode.

public int decode(InputStream is, BerTag tag) throws IOException {
    int decodedLength = 0;
    int tagLength;
    if (tag == null) {
        tag = new BerTag();
        tagLength = tag.decode(is);
        decodedLength += tagLength;
    } else {
        tagLength = tag.encode(new ReverseByteArrayOutputStream(10));
    }
    BerLength lengthField = new BerLength();
    int lengthLength = lengthField.decode(is);
    decodedLength += lengthLength + lengthField.val;
    value = new byte[tagLength + lengthLength + lengthField.val];
    Util.readFully(is, value, tagLength + lengthLength, lengthField.val);
    ReverseByteArrayOutputStream os = new ReverseByteArrayOutputStream(value, tagLength + lengthLength - 1);
    BerLength.encodeLength(os, lengthField.val);
    tag.encode(os);
    return decodedLength;
}
Also used : BerLength(org.openmuc.jasn1.ber.BerLength) BerTag(org.openmuc.jasn1.ber.BerTag) ReverseByteArrayOutputStream(org.openmuc.jasn1.ber.ReverseByteArrayOutputStream)

Example 2 with BerLength

use of org.openmuc.jasn1.ber.BerLength in project jasn1 by openmuc.

the class BerBitString method decode.

public int decode(InputStream is, boolean withTag) throws IOException {
    // could be encoded in primitiv and constructed mode
    // only primitiv mode is implemented
    int codeLength = 0;
    if (withTag) {
        codeLength += tag.decodeAndCheck(is);
    }
    BerLength length = new BerLength();
    codeLength += length.decode(is);
    value = new byte[length.val - 1];
    int unusedBits = is.read();
    if (unusedBits == -1) {
        throw new EOFException("Unexpected end of input stream.");
    }
    if (unusedBits > 7) {
        throw new IOException("Number of unused bits in bit string expected to be less than 8 but is: " + unusedBits);
    }
    numBits = (value.length * 8) - unusedBits;
    if (value.length > 0) {
        Util.readFully(is, value);
    }
    codeLength += value.length + 1;
    return codeLength;
}
Also used : BerLength(org.openmuc.jasn1.ber.BerLength) EOFException(java.io.EOFException) IOException(java.io.IOException)

Example 3 with BerLength

use of org.openmuc.jasn1.ber.BerLength in project jasn1 by openmuc.

the class BerBoolean method decode.

public int decode(InputStream is, boolean withTag) throws IOException {
    int codeLength = 0;
    if (withTag) {
        codeLength += tag.decodeAndCheck(is);
    }
    BerLength length = new BerLength();
    codeLength += length.decode(is);
    if (length.val != 1) {
        throw new IOException("Decoded length of BerBoolean is not correct");
    }
    int nextByte = is.read();
    if (nextByte == -1) {
        throw new EOFException("Unexpected end of input stream.");
    }
    codeLength++;
    if (nextByte == 0) {
        value = false;
    } else {
        value = true;
    }
    return codeLength;
}
Also used : BerLength(org.openmuc.jasn1.ber.BerLength) EOFException(java.io.EOFException) IOException(java.io.IOException)

Example 4 with BerLength

use of org.openmuc.jasn1.ber.BerLength in project jasn1 by openmuc.

the class BerInteger method decode.

public int decode(InputStream is, boolean withTag) throws IOException {
    int codeLength = 0;
    if (withTag) {
        codeLength += tag.decodeAndCheck(is);
    }
    BerLength length = new BerLength();
    codeLength += length.decode(is);
    if (length.val < 1) {
        throw new IOException("Decoded length of BerInteger is not correct");
    }
    byte[] byteCode = new byte[length.val];
    Util.readFully(is, byteCode);
    codeLength += length.val;
    value = new BigInteger(byteCode);
    return codeLength;
}
Also used : BerLength(org.openmuc.jasn1.ber.BerLength) BigInteger(java.math.BigInteger) IOException(java.io.IOException)

Example 5 with BerLength

use of org.openmuc.jasn1.ber.BerLength in project jasn1 by openmuc.

the class AuthenticateResponseOk method decode.

public int decode(InputStream is, boolean withTag) throws IOException {
    int codeLength = 0;
    int subCodeLength = 0;
    BerTag berTag = new BerTag();
    if (withTag) {
        codeLength += tag.decodeAndCheck(is);
    }
    BerLength length = new BerLength();
    codeLength += length.decode(is);
    int totalLength = length.val;
    if (totalLength == -1) {
        subCodeLength += berTag.decode(is);
        if (berTag.tagNumber == 0 && berTag.tagClass == 0 && berTag.primitive == 0) {
            int nextByte = is.read();
            if (nextByte != 0) {
                if (nextByte == -1) {
                    throw new EOFException("Unexpected end of input stream.");
                }
                throw new IOException("Decoded sequence has wrong end of contents octets");
            }
            codeLength += subCodeLength + 1;
            return codeLength;
        }
        if (berTag.equals(EuiccSigned1.tag)) {
            euiccSigned1 = new EuiccSigned1();
            subCodeLength += euiccSigned1.decode(is, false);
            subCodeLength += berTag.decode(is);
        }
        if (berTag.tagNumber == 0 && berTag.tagClass == 0 && berTag.primitive == 0) {
            int nextByte = is.read();
            if (nextByte != 0) {
                if (nextByte == -1) {
                    throw new EOFException("Unexpected end of input stream.");
                }
                throw new IOException("Decoded sequence has wrong end of contents octets");
            }
            codeLength += subCodeLength + 1;
            return codeLength;
        }
        if (berTag.equals(BerTag.APPLICATION_CLASS, BerTag.PRIMITIVE, 55)) {
            euiccSignature1 = new BerOctetString();
            subCodeLength += euiccSignature1.decode(is, false);
            subCodeLength += berTag.decode(is);
        }
        if (berTag.tagNumber == 0 && berTag.tagClass == 0 && berTag.primitive == 0) {
            int nextByte = is.read();
            if (nextByte != 0) {
                if (nextByte == -1) {
                    throw new EOFException("Unexpected end of input stream.");
                }
                throw new IOException("Decoded sequence has wrong end of contents octets");
            }
            codeLength += subCodeLength + 1;
            return codeLength;
        }
        if (berTag.equals(Certificate.tag)) {
            euiccCertificate = new Certificate();
            subCodeLength += euiccCertificate.decode(is, false);
            subCodeLength += berTag.decode(is);
        }
        if (berTag.tagNumber == 0 && berTag.tagClass == 0 && berTag.primitive == 0) {
            int nextByte = is.read();
            if (nextByte != 0) {
                if (nextByte == -1) {
                    throw new EOFException("Unexpected end of input stream.");
                }
                throw new IOException("Decoded sequence has wrong end of contents octets");
            }
            codeLength += subCodeLength + 1;
            return codeLength;
        }
        if (berTag.equals(Certificate.tag)) {
            eumCertificate = new Certificate();
            subCodeLength += eumCertificate.decode(is, false);
            subCodeLength += berTag.decode(is);
        }
        int nextByte = is.read();
        if (berTag.tagNumber != 0 || berTag.tagClass != 0 || berTag.primitive != 0 || nextByte != 0) {
            if (nextByte == -1) {
                throw new EOFException("Unexpected end of input stream.");
            }
            throw new IOException("Decoded sequence has wrong end of contents octets");
        }
        codeLength += subCodeLength + 1;
        return codeLength;
    }
    codeLength += totalLength;
    subCodeLength += berTag.decode(is);
    if (berTag.equals(EuiccSigned1.tag)) {
        euiccSigned1 = new EuiccSigned1();
        subCodeLength += euiccSigned1.decode(is, false);
        subCodeLength += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match the mandatory sequence element tag.");
    }
    if (berTag.equals(BerTag.APPLICATION_CLASS, BerTag.PRIMITIVE, 55)) {
        euiccSignature1 = new BerOctetString();
        subCodeLength += euiccSignature1.decode(is, false);
        subCodeLength += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match the mandatory sequence element tag.");
    }
    if (berTag.equals(Certificate.tag)) {
        euiccCertificate = new Certificate();
        subCodeLength += euiccCertificate.decode(is, false);
        subCodeLength += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match the mandatory sequence element tag.");
    }
    if (berTag.equals(Certificate.tag)) {
        eumCertificate = new Certificate();
        subCodeLength += eumCertificate.decode(is, false);
        if (subCodeLength == totalLength) {
            return codeLength;
        }
    }
    throw new IOException("Unexpected end of sequence, length tag: " + totalLength + ", actual sequence length: " + subCodeLength);
}
Also used : EOFException(java.io.EOFException) IOException(java.io.IOException) Certificate(org.openmuc.jasn1.compiler.pkix1explicit88.Certificate)

Aggregations

IOException (java.io.IOException)18 EOFException (java.io.EOFException)13 BerLength (org.openmuc.jasn1.ber.BerLength)10 BerTag (org.openmuc.jasn1.ber.BerTag)6 AsnBitString (org.openmuc.jasn1.compiler.model.AsnBitString)5 AsnCharacterString (org.openmuc.jasn1.compiler.model.AsnCharacterString)5 AsnOctetString (org.openmuc.jasn1.compiler.model.AsnOctetString)5 Certificate (org.openmuc.jasn1.compiler.pkix1explicit88.Certificate)5 AsnTag (org.openmuc.jasn1.compiler.model.AsnTag)4 AsnElementType (org.openmuc.jasn1.compiler.model.AsnElementType)3 SubjectKeyIdentifier (org.openmuc.jasn1.compiler.pkix1implicit88.SubjectKeyIdentifier)2 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 ReverseByteArrayOutputStream (org.openmuc.jasn1.ber.ReverseByteArrayOutputStream)1 BerObjectDescriptor (org.openmuc.jasn1.ber.types.string.BerObjectDescriptor)1 EmployeeNumberZ (org.openmuc.jasn1.compiler.modules.module2.EmployeeNumberZ)1 Attribute (org.openmuc.jasn1.compiler.pkix1explicit88.Attribute)1 CertificateList (org.openmuc.jasn1.compiler.pkix1explicit88.CertificateList)1 CertificateSerialNumber (org.openmuc.jasn1.compiler.pkix1explicit88.CertificateSerialNumber)1 DirectoryString (org.openmuc.jasn1.compiler.pkix1explicit88.DirectoryString)1