beanit / asn1bean

ASN1bean (formerly known as jASN1) is a Java ASN.1 BER and DER encoding/decoding library
https://www.beanit.com/asn1/
Apache License 2.0
110 stars 45 forks source link

Empty Octet String issue #4

Closed gleclezio closed 6 years ago

gleclezio commented 6 years ago

I'm using the jAsn1 library to convert ASN1 definition into java classes and then ASN1 values files (representation of the ASN1 definition with real values written in ASN1) to instantiate dynamically the java objects. During the parsing of the ASN1 values files with jAsn1, I found, if I'm not wrong, that the empty octet string (ie in ASN1: varName ''H ) was not correctly parsed in the ASNLexer class.

Example of definition:

MyElement ::= CHOICE {
    element ElementType
}

ElementType ::= SEQUENCE {
    eltOctetString OCTET STRING,
    eltOctetStringEmpty OCTET STRING
}

With corresponding Value used:

test MyElement ::= element : {
    eltOctetString 'ABBA'H,
    eltOctetStringEmpty ''H
}

Error reported by jAsn1:

line 1:20: unexpected token: element line 2:24: unexpected token: 'ABBA'H line 2:31: expecting ASSIGN_OP, found ',' line 3:29: unexpected token: ' line 4:1: expecting ASSIGN_OP, found '}'

To correct this, I modified the nextToken() method by adding in the default case of the switch a new else if :

else if ((LA(1) == '\'') && (LA(2) == '\'') && (LA(3) == 'H')) {
    mH_STRING(true);
    theRetToken = _returnToken;
}

and I modified the mH_STRING method by modifying the if (_cnt73 >= 1) into if (_cnt73 >= 0).

Would it be possible that you confirm me that these changes make sens for you ?

sfeuerhahn commented 6 years ago

actually the code you modified was automatically generated using antlr. So instead of modifying the java code directly it is better to change the antlr code located in jasn1/projects/jasn1-compiler/antlr/ans1.g and then generate the classes again.

Anyways, parsing of ASN.1 values is not well supported by this library since it was never really the goal of the project.

gleclezio commented 6 years ago

Why I didn't do that before... It was so easier. Thanks a lot

The asn1.g has been modified by replacing '+' by '*': B_STRING : SINGLE_QUOTE (BDIG)+ SINGLE_QUOTE 'B' ; into B_STRING : SINGLE_QUOTE (BDIG)* SINGLE_QUOTE 'B' ;

and

H_STRING : SINGLE_QUOTE (HDIG)+ SINGLE_QUOTE 'H' ; into H_STRING : SINGLE_QUOTE (HDIG)* SINGLE_QUOTE 'H' ;

gleclezio commented 6 years ago

Hello and thank you for your help, My company would like to use and commercialize my soft based on jAsn1. Do you allow us to do that ?

Regards, Gregory

2017-10-20 14:11 GMT+02:00 sfeuerhahn notifications@github.com:

actually the code you modified was automatically generated using antlr. So instead of modifying the java code directly it is better to change the antlr code located in jasn1/projects/jasn1-compiler/antlr/ans1.g and then generate the classes again.

Anyways, parsing of ASN.1 values is not well supported by this library since it was never really the goal of the project.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/openmuc/jasn1/issues/4#issuecomment-338189065, or mute the thread https://github.com/notifications/unsubscribe-auth/AfWuSHtVlQeRJfHKJshbQ14HpEoiCyCRks5suI4JgaJpZM4P_czO .