Search in sources :

Example 1 with BerOctetString

use of org.openmuc.jasn1.ber.types.BerOctetString 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)

Example 2 with BerOctetString

use of org.openmuc.jasn1.ber.types.BerOctetString in project jasn1 by openmuc.

the class GeneralName method decode.

public int decode(InputStream is, BerTag berTag) throws IOException {
    int codeLength = 0;
    BerTag passedTag = berTag;
    if (berTag == null) {
        berTag = new BerTag();
        codeLength += berTag.decode(is);
    }
    if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 0)) {
        otherName = new AnotherName();
        codeLength += otherName.decode(is, false);
        return codeLength;
    }
    if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 1)) {
        rfc822Name = new BerIA5String();
        codeLength += rfc822Name.decode(is, false);
        return codeLength;
    }
    if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 2)) {
        dNSName = new BerIA5String();
        codeLength += dNSName.decode(is, false);
        return codeLength;
    }
    if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 3)) {
        x400Address = new ORAddress();
        codeLength += x400Address.decode(is, false);
        return codeLength;
    }
    if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 4)) {
        codeLength += BerLength.skip(is);
        directoryName = new Name();
        codeLength += directoryName.decode(is, null);
        return codeLength;
    }
    if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 5)) {
        ediPartyName = new EDIPartyName();
        codeLength += ediPartyName.decode(is, false);
        return codeLength;
    }
    if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 6)) {
        uniformResourceIdentifier = new BerIA5String();
        codeLength += uniformResourceIdentifier.decode(is, false);
        return codeLength;
    }
    if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 7)) {
        iPAddress = new BerOctetString();
        codeLength += iPAddress.decode(is, false);
        return codeLength;
    }
    if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 8)) {
        registeredID = new BerObjectIdentifier();
        codeLength += registeredID.decode(is, false);
        return codeLength;
    }
    if (passedTag != null) {
        return 0;
    }
    throw new IOException("Error decoding CHOICE: Tag " + berTag + " matched to no item.");
}
Also used : IOException(java.io.IOException) ORAddress(org.openmuc.jasn1.compiler.pkix1explicit88.ORAddress) RelativeDistinguishedName(org.openmuc.jasn1.compiler.pkix1explicit88.RelativeDistinguishedName) Name(org.openmuc.jasn1.compiler.pkix1explicit88.Name)

Example 3 with BerOctetString

use of org.openmuc.jasn1.ber.types.BerOctetString in project jasn1 by openmuc.

the class BerOctetStringTest method explicitEncoding.

@Test
public void explicitEncoding() throws IOException {
    ReverseByteArrayOutputStream berStream = new ReverseByteArrayOutputStream(50);
    byte[] byteArray = new byte[] { 0x01, 0x02, 0x03 };
    BerOctetString asn1OctetString = new BerOctetString(byteArray);
    int length = asn1OctetString.encode(berStream, true);
    Assert.assertEquals(5, length);
    byte[] expectedBytes = new byte[] { 0x04, 0x03, 0x01, 0x02, 0x03 };
    Assert.assertArrayEquals(expectedBytes, berStream.getArray());
}
Also used : ReverseByteArrayOutputStream(org.openmuc.jasn1.ber.ReverseByteArrayOutputStream) Test(org.junit.Test)

Example 4 with BerOctetString

use of org.openmuc.jasn1.ber.types.BerOctetString in project jasn1 by openmuc.

the class MobileTest method header.

@Test
public void header() throws Exception {
    ProfileElement headerProfileElement = new ProfileElement();
    ServicesList servicesList = new ServicesList();
    ProfileHeader.EUICCMandatoryGFSTEList GFSTEList = new ProfileHeader.EUICCMandatoryGFSTEList();
    GFSTEList.seqOf = Arrays.asList(new BerObjectIdentifier(new int[] { 2, 23, 143, 1, 2, 1 }), new BerObjectIdentifier(new int[] { 2, 23, 143, 1, 2, 4 }));
    servicesList.usim = new BerNull();
    servicesList.milenage = new BerNull();
    servicesList.javacard = new BerNull();
    headerProfileElement.header = new ProfileHeader(new UInt8(2), new UInt8(0), new BerUTF8String("SIMalliance Sample Profile"), new BerOctetString(DatatypeConverter.parseHexBinary("89019990001234567893")), null, servicesList, GFSTEList, null);
    ReverseByteArrayOutputStream reverseByteArrayOutputStream = new ReverseByteArrayOutputStream(2048, true);
    headerProfileElement.encode(reverseByteArrayOutputStream);
    byte[] code = reverseByteArrayOutputStream.getArray();
    ProfileElement rereadProfileElement = new ProfileElement();
    rereadProfileElement.decode(new ByteArrayInputStream(code), null);
    ReverseByteArrayOutputStream reverseOutputStream2 = new ReverseByteArrayOutputStream(2048, true);
    rereadProfileElement.encode(reverseOutputStream2);
    byte[] code2 = reverseOutputStream2.getArray();
    Assert.assertArrayEquals(code, code2);
    String expected = "A0 48 80 01 02 81 01 00 82 1A 53494D616C6C69616E63652053616D706C652050726F66696C65 83 0A 89019990001234567893 A5 06 81 00 84 00 8B 00 A6 10 06 06 67810F010201 06 06 67810F010204".replaceAll("\\s", "");
    Assert.assertEquals(expected, DatatypeConverter.printHexBinary(code));
}
Also used : BerObjectIdentifier(org.openmuc.jasn1.ber.types.BerObjectIdentifier) BerUTF8String(org.openmuc.jasn1.ber.types.string.BerUTF8String) BerOctetString(org.openmuc.jasn1.ber.types.BerOctetString) BerUTF8String(org.openmuc.jasn1.ber.types.string.BerUTF8String) BerOctetString(org.openmuc.jasn1.ber.types.BerOctetString) ProfileElement(org.openmuc.jasn1.compiler.pedefinitions.ProfileElement) UInt8(org.openmuc.jasn1.compiler.pedefinitions.UInt8) ProfileHeader(org.openmuc.jasn1.compiler.pedefinitions.ProfileHeader) ReverseByteArrayOutputStream(org.openmuc.jasn1.ber.ReverseByteArrayOutputStream) ServicesList(org.openmuc.jasn1.compiler.pedefinitions.ServicesList) ByteArrayInputStream(java.io.ByteArrayInputStream) BerNull(org.openmuc.jasn1.ber.types.BerNull) Test(org.junit.Test)

Example 5 with BerOctetString

use of org.openmuc.jasn1.ber.types.BerOctetString in project jasn1 by openmuc.

the class BerEmbeddedPdv 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;
    codeLength += totalLength;
    subCodeLength += berTag.decode(is);
    if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 0)) {
        subCodeLength += length.decode(is);
        identification = new Identification();
        subCodeLength += identification.decode(is, null);
        subCodeLength += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match the mandatory sequence element tag.");
    }
    if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 1)) {
        dataValueDescriptor = new BerObjectDescriptor();
        subCodeLength += dataValueDescriptor.decode(is, false);
        subCodeLength += berTag.decode(is);
    }
    if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 2)) {
        dataValue = new BerOctetString();
        subCodeLength += dataValue.decode(is, false);
        if (subCodeLength == totalLength) {
            return codeLength;
        }
    }
    throw new IOException("Unexpected end of sequence, length tag: " + totalLength + ", actual sequence length: " + subCodeLength);
}
Also used : BerLength(org.openmuc.jasn1.ber.BerLength) BerObjectDescriptor(org.openmuc.jasn1.ber.types.string.BerObjectDescriptor) IOException(java.io.IOException) BerTag(org.openmuc.jasn1.ber.BerTag)

Aggregations

IOException (java.io.IOException)7 EOFException (java.io.EOFException)5 Certificate (org.openmuc.jasn1.compiler.pkix1explicit88.Certificate)5 Test (org.junit.Test)2 ReverseByteArrayOutputStream (org.openmuc.jasn1.ber.ReverseByteArrayOutputStream)2 SubjectKeyIdentifier (org.openmuc.jasn1.compiler.pkix1implicit88.SubjectKeyIdentifier)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 BerLength (org.openmuc.jasn1.ber.BerLength)1 BerTag (org.openmuc.jasn1.ber.BerTag)1 BerNull (org.openmuc.jasn1.ber.types.BerNull)1 BerObjectIdentifier (org.openmuc.jasn1.ber.types.BerObjectIdentifier)1 BerOctetString (org.openmuc.jasn1.ber.types.BerOctetString)1 BerObjectDescriptor (org.openmuc.jasn1.ber.types.string.BerObjectDescriptor)1 BerUTF8String (org.openmuc.jasn1.ber.types.string.BerUTF8String)1 ProfileElement (org.openmuc.jasn1.compiler.pedefinitions.ProfileElement)1 ProfileHeader (org.openmuc.jasn1.compiler.pedefinitions.ProfileHeader)1 ServicesList (org.openmuc.jasn1.compiler.pedefinitions.ServicesList)1 UInt8 (org.openmuc.jasn1.compiler.pedefinitions.UInt8)1 Name (org.openmuc.jasn1.compiler.pkix1explicit88.Name)1 ORAddress (org.openmuc.jasn1.compiler.pkix1explicit88.ORAddress)1