Search in sources :

Example 1 with BerTag

use of org.openmuc.jasn1.ber.BerTag 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 BerTag

use of org.openmuc.jasn1.ber.BerTag 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 3 with BerTag

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

the class LoadCRLRequest 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(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 0)) {
            crl = new CertificateList();
            subCodeLength += crl.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(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 0)) {
        crl = new CertificateList();
        subCodeLength += crl.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) CertificateList(org.openmuc.jasn1.compiler.pkix1explicit88.CertificateList) IOException(java.io.IOException)

Example 4 with BerTag

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

the class BerClassWriter method writeSequenceOrSetOfDecodeFunction.

private void writeSequenceOrSetOfDecodeFunction(AsnElementType componentType, String classNameOfSequenceOfElement, boolean hasExplicitTag, boolean isSequence) throws IOException {
    Tag componentTag = getTag(componentType);
    write("public int decode(InputStream is, boolean withTag) throws IOException {");
    write("int codeLength = 0;");
    write("int subCodeLength = 0;");
    if (componentTag != null || supportIndefiniteLength) {
        write("BerTag berTag = new BerTag();");
    }
    write("if (withTag) {");
    write("codeLength += tag.decodeAndCheck(is);");
    write("}\n");
    write("BerLength length = new BerLength();");
    write("codeLength += length.decode(is);");
    write("int totalLength = length.val;\n");
    if (supportIndefiniteLength) {
        writeSequenceOfDecodeIndefiniteLenghtPart(componentType, classNameOfSequenceOfElement, hasExplicitTag);
    }
    if (hasExplicitTag) {
        write("int nextByte = is.read();");
        write("if (nextByte == -1) {");
        write("throw new EOFException(\"Unexpected end of input stream.\");");
        write("}");
        if (isSequence) {
            write("if (nextByte != (0x30)) {");
        } else {
            write("if (nextByte != (0x31)) {");
        }
        write("throw new IOException(\"Tag does not match!\");");
        write("}");
        write("length.decode(is);");
        write("totalLength = length.val;\n");
    }
    write("while (subCodeLength < totalLength) {");
    write(classNameOfSequenceOfElement + " element = new " + classNameOfSequenceOfElement + "();");
    String explicitEncoding = getExplicitDecodingParameter(componentType);
    if (componentTag != null) {
        if (componentTag.type == TagType.EXPLICIT) {
            write("subCodeLength += berTag.decode(is);");
            write("subCodeLength += length.decode(is);");
            explicitEncoding = ", true";
        } else {
            write("subCodeLength += berTag.decode(is);");
            if (isDirectAnyOrChoice(componentType)) {
                explicitEncoding = ", berTag";
            } else {
                explicitEncoding = ", false";
            }
        }
        write("subCodeLength += element.decode(is" + explicitEncoding + ");");
    } else {
        if (isDirectAnyOrChoice(componentType)) {
            write("subCodeLength += element.decode(is, null);");
        } else {
            write("subCodeLength += element.decode(is, true);");
        }
    }
    write("seqOf.add(element);");
    write("}");
    write("if (subCodeLength != totalLength) {");
    write("throw new IOException(\"Decoded SequenceOf or SetOf has wrong length. Expected \" + totalLength + \" but has \" + subCodeLength);\n");
    write("}");
    write("codeLength += subCodeLength;\n");
    write("return codeLength;");
    write("}\n");
}
Also used : BerTag(org.openmuc.jasn1.ber.BerTag) AsnTag(org.openmuc.jasn1.compiler.model.AsnTag) AsnBitString(org.openmuc.jasn1.compiler.model.AsnBitString) AsnCharacterString(org.openmuc.jasn1.compiler.model.AsnCharacterString) AsnOctetString(org.openmuc.jasn1.compiler.model.AsnOctetString)

Example 5 with BerTag

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

the class BerClassWriter method writeSequenceDecodeIndefiniteLenghtPart.

private void writeSequenceDecodeIndefiniteLenghtPart(List<AsnElementType> componentTypes) throws IOException {
    write("if (totalLength == -1) {");
    write("subCodeLength += berTag.decode(is);\n");
    String initChoiceDecodeLength = "int ";
    for (AsnElementType componentType : componentTypes) {
        Tag componentTag = getTag(componentType);
        write("if (berTag.tagNumber == 0 && berTag.tagClass == 0 && berTag.primitive == 0) {");
        write("int nextByte = is.read();");
        write("if (nextByte != 0) {");
        write("if (nextByte == -1) {");
        write("throw new EOFException(\"Unexpected end of input stream.\");");
        write("}");
        write("throw new IOException(\"Decoded sequence has wrong end of contents octets\");");
        write("}");
        write("codeLength += subCodeLength + 1;");
        write("return codeLength;");
        write("}");
        String explicitEncoding;
        if (isDirectAnyOrChoice(componentType)) {
            if (isExplicit(componentTag)) {
                write("if (berTag.equals(" + getBerTagParametersString(componentTag) + ")) {");
                write("subCodeLength += length.decode(is);");
                explicitEncoding = "null";
            } else {
                explicitEncoding = "berTag";
            }
            write(getName(componentType) + " = new " + getClassNameOfComponent(componentType) + "();");
            write(initChoiceDecodeLength + "choiceDecodeLength = " + getName(componentType) + ".decode(is, " + explicitEncoding + ");");
            if (!isExplicit(componentTag)) {
                initChoiceDecodeLength = "";
            }
            write("if (choiceDecodeLength != 0) {");
            write("subCodeLength += choiceDecodeLength;");
            write("subCodeLength += berTag.decode(is);");
            write("}");
            write("else {");
            write(getName(componentType) + " = null;");
            write("}\n");
            if (isExplicit(componentTag)) {
                write("}");
            }
        } else {
            explicitEncoding = ", false";
            if (componentTag != null) {
                write("if (berTag.equals(" + getBerTagParametersString(componentTag) + ")) {");
                if (isExplicit(componentTag)) {
                    write("codeLength += length.decode(is);");
                    explicitEncoding = ", true";
                }
            } else {
                write("if (berTag.equals(" + getClassNameOfComponent(componentType) + ".tag)) {");
            }
            write(getName(componentType) + " = new " + getClassNameOfComponent(componentType) + "();");
            write("subCodeLength += " + getName(componentType) + ".decode(is" + explicitEncoding + ");");
            write("subCodeLength += berTag.decode(is);");
            write("}");
        }
    }
    write("int nextByte = is.read();");
    write("if (berTag.tagNumber != 0 || berTag.tagClass != 0 || berTag.primitive != 0");
    write("|| nextByte != 0) {");
    write("if (nextByte == -1) {");
    write("throw new EOFException(\"Unexpected end of input stream.\");");
    write("}");
    write("throw new IOException(\"Decoded sequence has wrong end of contents octets\");");
    write("}");
    write("codeLength += subCodeLength + 1;");
    write("return codeLength;");
    write("}\n");
}
Also used : AsnBitString(org.openmuc.jasn1.compiler.model.AsnBitString) AsnCharacterString(org.openmuc.jasn1.compiler.model.AsnCharacterString) AsnOctetString(org.openmuc.jasn1.compiler.model.AsnOctetString) BerTag(org.openmuc.jasn1.ber.BerTag) AsnTag(org.openmuc.jasn1.compiler.model.AsnTag) AsnElementType(org.openmuc.jasn1.compiler.model.AsnElementType)

Aggregations

IOException (java.io.IOException)14 BerTag (org.openmuc.jasn1.ber.BerTag)12 AsnBitString (org.openmuc.jasn1.compiler.model.AsnBitString)11 AsnCharacterString (org.openmuc.jasn1.compiler.model.AsnCharacterString)11 AsnOctetString (org.openmuc.jasn1.compiler.model.AsnOctetString)11 EOFException (java.io.EOFException)10 AsnTag (org.openmuc.jasn1.compiler.model.AsnTag)9 AsnElementType (org.openmuc.jasn1.compiler.model.AsnElementType)8 Certificate (org.openmuc.jasn1.compiler.pkix1explicit88.Certificate)5 BerLength (org.openmuc.jasn1.ber.BerLength)2 AsnConstructedType (org.openmuc.jasn1.compiler.model.AsnConstructedType)2 AsnParameter (org.openmuc.jasn1.compiler.model.AsnParameter)2 RelativeDistinguishedName (org.openmuc.jasn1.compiler.pkix1explicit88.RelativeDistinguishedName)2 SubjectKeyIdentifier (org.openmuc.jasn1.compiler.pkix1implicit88.SubjectKeyIdentifier)2 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