From b7ad06a92f62bac54bdf2f7512e93729317750d7 Mon Sep 17 00:00:00 2001 From: Josef Date: Tue, 18 Aug 2020 17:46:39 +0200 Subject: [PATCH] Null, Bool, Integer support (without *_ws methods). --- pycrate_asn1rt/asnobj.py | 54 ++++++++++- pycrate_asn1rt/asnobj_basic.py | 55 ++++++++++- pycrate_asn1rt/codecs.py | 104 +++++++++++++++++++++ test/test_asn1rt.py | 165 ++++++++++++++++++++++++++++++--- 4 files changed, 361 insertions(+), 17 deletions(-) diff --git a/pycrate_asn1rt/asnobj.py b/pycrate_asn1rt/asnobj.py index 2b27bb8..bf57d95 100644 --- a/pycrate_asn1rt/asnobj.py +++ b/pycrate_asn1rt/asnobj.py @@ -1724,7 +1724,7 @@ class ASN1Obj(Element): ### # conversion between internal value and ASN.1 JER encoding ### - + if _with_json: def _from_jval(self, val): @@ -1756,6 +1756,58 @@ class ASN1Obj(Element): to_json = to_jer from_json = from_jer + ### + # conversion between internal value and ASN.1 OER/COER encoding + ### + + def _from_oer(self, char): + raise (ASN1NotSuppErr(self.fullname())) + + def _to_oer(self): + raise (ASN1NotSuppErr(self.fullname())) + + def from_oer(self, buf): + # ASN1CodecOER.CANONICAL = False + + if isinstance(buf, bytes_types): + char = Charpy(buf) + else: + char = buf + + self._from_oer(char) + + if self._SAFE_BND: + self._safechk_bnd(self._val) + + def to_oer(self, val=None): + ASN1CodecOER.CANONICAL = False + if val is not None: + self.set_val(val) + if self._val is not None: + ret = pack_val(*self._to_oer())[0] + if ret: + return ret + else: + return b'\0' + else: + return None + + def from_coer(self, buf): + self.from_oer(buf) + + def to_coer(self, val=None): + ASN1CodecOER.CANONICAL = True + if val is not None: + self.set_val(val) + if self._val is not None: + ret = pack_val(*self._to_oer())[0] + if ret: + return ret + else: + return b'\0' + else: + return None + def _save_ber_params(): global __ber_enc_llong diff --git a/pycrate_asn1rt/asnobj_basic.py b/pycrate_asn1rt/asnobj_basic.py index 470c29a..0a9c7bc 100644 --- a/pycrate_asn1rt/asnobj_basic.py +++ b/pycrate_asn1rt/asnobj_basic.py @@ -136,6 +136,16 @@ Single value: int 0 def _to_jval(self): return None + ### + # conversion between internal value and ASN.1 OER/COER encoding + ### + + def _from_oer(self, char): + self._from_per(char) + + def _to_oer(self): + return self._to_per() + class BOOL(ASN1Obj): __doc__ = """ @@ -263,7 +273,18 @@ Single value: Python bool def _to_jval(self): return self._val - + + ### + # conversion between internal value and ASN.1 OER/COER encoding + ### + + def _from_oer(self, char): + self._val = (char.get_uint(8) > ASN1CodecOER.FALSE) + + def _to_oer(self): + # actually, COER only + val = ASN1CodecOER.TRUE if (self._val is True) else ASN1CodecOER.FALSE + return [(T_UINT, val, 8)] #------------------------------------------------------------------------------# # INTEGER and REAL @@ -544,6 +565,38 @@ Specific attribute: self._names_to_val() return self._val + ### + # conversion between internal value and ASN.1 OER/COER encoding + ### + + def _to_oer(self): + if self._const_val: + # Constraints defined + if self._const_val.ext is not None: + # Extensible OER-visible constraints are encoded as integer + # type with no bounds + return ASN1CodecOER.encode_intunconst(self._val) + + # Constrained + return ASN1CodecOER.encode_intconst(self._val, self._const_val) + else: + # Unconstrained + return ASN1CodecOER.encode_intunconst(self._val) + + def _from_oer(self, char): + if self._const_val: + if self._const_val.ext is not None: + self._val = ASN1CodecOER.decode_intunconst(char) + return + + # Constrained + self._val = ASN1CodecOER.decode_intconst(char, self._const_val) + return + else: + # Unconstrained + self._val = ASN1CodecOER.decode_intunconst(char) + return + class REAL(ASN1Obj): __doc__ = """ diff --git a/pycrate_asn1rt/codecs.py b/pycrate_asn1rt/codecs.py index 98855cf..186736d 100644 --- a/pycrate_asn1rt/codecs.py +++ b/pycrate_asn1rt/codecs.py @@ -1660,3 +1660,107 @@ class ASN1CodecGSER(ASN1Codec): # TODO: implement this pass + +class ASN1CodecOER(ASN1Codec): + + # canonicity is used to decide whether to encode default values or not in + # constructed object + CANONICAL = True + + TRUE = 0xFF + FALSE = 0x00 + + @classmethod + def encode_length_determinant(cls, length): + # Always canonical (implementing non-canonical doesn't make sense) + determinant = [] + if length > 127: + # long determinant + dl = uint_bytelen(length) + determinant.extend([(T_UINT, 1, 1), + (T_UINT, dl, 7), + (T_UINT, length, dl*8) + ]) + else: + # short determinant + determinant.append((T_UINT, length, 8)) + + return determinant + + @classmethod + def decode_length_determinant(cls, char): + long_form = char.get_uint(1) + length = char.get_uint(7) + if long_form: + length = char.get_uint(length*8) + return length + + @classmethod + def encode_intunconst(cls, val, signed=True): + if val < 0: signed = True + vl = int_bytelen(val) if signed else uint_bytelen(val) + GEN = cls.encode_length_determinant(vl) + vt = T_INT if signed else T_UINT + GEN.append((vt, val, vl * 8)) + return GEN + + @classmethod + def decode_intunconst(cls, char, signed=True): + vl = cls.decode_length_determinant(char) + if signed: + return char.get_int(vl*8) + else: + return char.get_uint(vl*8) + + @classmethod + def encode_intconst(cls, val, const_val): + if const_val.lb is not None: + if const_val.lb >= 0: + # 10.3 a ~ d Check on the upper bound + if const_val.ub is not None: + ubl = round_p2(uint_bytelen(const_val.ub)) + if ubl <= 8: + return [(T_UINT, val, ubl*8)] + # No other conditions are fulfilled + return cls.encode_intunconst(val, signed=False) + else: + # 10.4 a ~ d + if const_val.ub is not None: + dbl = round_p2(max(int_bytelen(const_val.lb), + int_bytelen(const_val.ub))) + if dbl <= 8: + return [(T_INT, val, dbl*8)] + # No upper bound etc. + return cls.encode_intunconst(val) + else: + # No lower bound -> encode with length determinant + return cls.encode_intunconst(val) + + @classmethod + def decode_intconst(cls, char, const_val): + if const_val.lb is not None: + if const_val.lb >= 0: + # 10.3 a ~ d Check on the upper bound + if const_val.ub is not None: + ubl = round_p2(uint_bytelen(const_val.ub)) + if ubl <= 8: + return char.get_uint(ubl*8) + # No other conditions are fulfilled + return cls.decode_intunconst(char, signed=False) + else: + # 10.4 a ~ d + if const_val.ub is not None: + dbl = round_p2(max(int_bytelen(const_val.lb), + int_bytelen(const_val.ub))) + if dbl <= 8: + return char.get_int(dbl*8) + # No upper bound etc. + return cls.decode_intunconst(char) + else: + # No lower bound -> encode with length determinant + return cls.decode_intunconst(char) + + + + + diff --git a/test/test_asn1rt.py b/test/test_asn1rt.py index 44f780c..cba7c72 100644 --- a/test/test_asn1rt.py +++ b/test/test_asn1rt.py @@ -77,6 +77,7 @@ def _test_rt_base(): assert( Boo01.to_ber() == Boo01.to_ber_ws() == b'\x01\x01\xff' ) assert( Boo01.to_cer() == Boo01.to_cer_ws() == b'\x01\x01\xff' ) assert( Boo01.to_der() == Boo01.to_der_ws() == b'\x01\x01\xff' ) + assert( Boo01.to_coer() == Boo01.to_oer() == b'\xff' ) # decoding Boo01.from_aper(b'\x80') assert( Boo01._val == True ) @@ -103,7 +104,12 @@ def _test_rt_base(): assert( Boo01.to_jer() == 'true' ) Boo01.from_jer( 'true' ) assert( Boo01._val == True ) - + + Boo01.from_coer(b'\xff') + assert( Boo01._val == True ) + Boo01.from_oer(b'\xff') + assert( Boo01._val == True ) + # Int01 ::= INTEGER Int01 = Mod['Int01'] # @@ -114,6 +120,8 @@ def _test_rt_base(): assert( Int01.to_ber() == Int01.to_ber_ws() == b'\x02\x01\x00' ) assert( Int01.to_cer() == Int01.to_cer_ws() == b'\x02\x01\x00' ) assert( Int01.to_der() == Int01.to_der_ws() == b'\x02\x01\x00' ) + assert( Int01.to_oer() == b'\x01\x00' ) + assert( Int01.to_coer() == b'\x01\x00' ) # decoding Int01.from_aper(b'\x01\x00') assert( Int01._val == 0 ) @@ -140,7 +148,11 @@ def _test_rt_base(): assert( Int01.to_jer() == '0' ) Int01.from_jer('0') assert( Int01._val == 0 ) - # + # OER/COER + Int01.from_oer(b'\x01\x00') + assert( Int01._val == 0 ) + Int01.from_coer(b'\x01\x00') + assert( Int01._val == 0 ) Int01.from_asn1('4096') # encoding assert( Int01.to_aper() == Int01.to_aper_ws() == b'\x02\x10\x00' ) @@ -148,6 +160,8 @@ def _test_rt_base(): assert( Int01.to_ber() == Int01.to_ber_ws() == b'\x02\x02\x10\x00' ) assert( Int01.to_cer() == Int01.to_cer_ws() == b'\x02\x02\x10\x00' ) assert( Int01.to_der() == Int01.to_der_ws() == b'\x02\x02\x10\x00' ) + assert( Int01.to_oer() == b'\x02\x10\x00' ) + assert( Int01.to_coer() == b'\x02\x10\x00' ) # decoding Int01.from_aper(b'\x02\x10\x00') assert( Int01._val == 4096 ) @@ -174,7 +188,12 @@ def _test_rt_base(): assert( Int01.to_jer() == '4096' ) Int01.from_jer('4096') assert( Int01._val == 4096 ) - + # OER/COER + Int01.from_oer(b'\x02\x10\x00') + assert( Int01._val == 4096 ) + Int01.from_coer(b'\x02\x10\x00') + assert( Int01._val == 4096 ) + # Int02 ::= INTEGER (MIN..65535) Int02 = Mod['Int02'] Int02.from_asn1('127') @@ -184,6 +203,8 @@ def _test_rt_base(): assert( Int02.to_ber() == Int02.to_ber_ws() == b'\x02\x01\x7f' ) assert( Int02.to_cer() == Int02.to_cer_ws() == b'\x02\x01\x7f' ) assert( Int02.to_der() == Int02.to_der_ws() == b'\x02\x01\x7f' ) + assert( Int02.to_oer() == b'\x01\x7f' ) + assert( Int02.to_coer() == b'\x01\x7f' ) # decoding Int02.from_aper(b'\x01\x7f') assert( Int02._val == 127 ) @@ -210,6 +231,11 @@ def _test_rt_base(): assert( Int02.to_jer() == '127' ) Int02.from_jer( '127' ) assert( Int02._val == 127 ) + # OER/COER + Int02.from_oer(b'\x01\x7f') + assert( Int02._val == 127 ) + Int02.from_coer(b'\x01\x7f') + assert( Int02._val == 127 ) # Int02.from_asn1('-128') # encoding @@ -218,6 +244,8 @@ def _test_rt_base(): assert( Int02.to_ber() == Int02.to_ber_ws() == b'\x02\x01\x80' ) assert( Int02.to_cer() == Int02.to_cer_ws() == b'\x02\x01\x80' ) assert( Int02.to_der() == Int02.to_der_ws() == b'\x02\x01\x80' ) + assert( Int02.to_oer() == b'\x01\x80' ) + assert( Int02.to_coer() == b'\x01\x80' ) # decoding Int02.from_aper(b'\x01\x80') assert( Int02._val == -128 ) @@ -244,6 +272,11 @@ def _test_rt_base(): assert( Int02.to_jer() == '-128' ) Int02.from_jer('-128') assert( Int02._val == -128 ) + # OER/COER + Int02.from_oer(b'\x01\x80') + assert( Int02._val == -128 ) + Int02.from_coer(b'\x01\x80') + assert( Int02._val == -128 ) # Int02.from_asn1('128') # encoding @@ -252,6 +285,8 @@ def _test_rt_base(): assert( Int02.to_ber() == Int02.to_ber_ws() == b'\x02\x02\x00\x80' ) assert( Int02.to_cer() == Int02.to_cer_ws() == b'\x02\x02\x00\x80' ) assert( Int02.to_der() == Int02.to_der_ws() == b'\x02\x02\x00\x80' ) + assert( Int02.to_oer() == b'\x02\x00\x80' ) + assert( Int02.to_coer() == b'\x02\x00\x80' ) # decoding Int02.from_aper(b'\x02\x00\x80') assert( Int02._val == 128 ) @@ -273,7 +308,11 @@ def _test_rt_base(): assert( Int02._val == 128 ) Int02.from_der_ws(b'\x02\x02\x00\x80') assert( Int02._val == 128 ) - + Int02.from_oer(b'\x02\x00\x80') + assert( Int02._val == 128 ) + Int02.from_coer(b'\x02\x00\x80') + assert( Int02._val == 128 ) + # Int03 ::= INTEGER (-1..MAX) Int03 = Mod['Int03'] Int03.from_asn1('4096') @@ -283,6 +322,8 @@ def _test_rt_base(): assert( Int03.to_ber() == Int03.to_ber_ws() == b'\x02\x02\x10\x00' ) assert( Int03.to_cer() == Int03.to_cer_ws() == b'\x02\x02\x10\x00' ) assert( Int03.to_der() == Int03.to_der_ws() == b'\x02\x02\x10\x00' ) + assert( Int03.to_oer() == b'\x02\x10\x00' ) + assert( Int03.to_coer() == b'\x02\x10\x00' ) # decoding Int03.from_aper(b'\x02\x10\x01') assert( Int03._val == 4096 ) @@ -308,8 +349,13 @@ def _test_rt_base(): if _with_json: assert( Int03.to_jer() == '4096' ) Int03.from_jer('4096') - assert( Int03._val == 4096 ) - + assert( Int03._val == 4096 ) + # OER/COER + Int03.from_oer(b'\x02\x10\x00') + assert( Int03._val == 4096 ) + Int03.from_coer(b'\x02\x10\x00') + assert( Int03._val == 4096 ) + # Int04 ::= INTEGER (1..MAX) Int04 = Mod['Int04'] # @@ -320,6 +366,8 @@ def _test_rt_base(): assert( Int04.to_ber() == Int04.to_ber_ws() == b'\x02\x01\x01' ) assert( Int04.to_cer() == Int04.to_cer_ws() == b'\x02\x01\x01' ) assert( Int04.to_der() == Int04.to_der_ws() == b'\x02\x01\x01' ) + assert( Int04.to_oer() == b'\x01\x01' ) + assert( Int04.to_coer() == b'\x01\x01' ) # decoding Int04.from_aper(b'\x01\x00') assert( Int04._val == 1) @@ -346,6 +394,11 @@ def _test_rt_base(): assert( Int04.to_jer() == '1' ) Int04.from_jer('1') assert( Int04._val == 1 ) + # OER/COER + Int04.from_oer(b'\x01\x01') + assert( Int04._val == 1) + Int04.from_coer(b'\x01\x01') + assert( Int04._val == 1 ) # Int04.from_asn1('127') # encoding @@ -354,6 +407,8 @@ def _test_rt_base(): assert( Int04.to_ber() == Int04.to_ber_ws() == b'\x02\x01\x7f' ) assert( Int04.to_cer() == Int04.to_cer_ws() == b'\x02\x01\x7f' ) assert( Int04.to_der() == Int04.to_der_ws() == b'\x02\x01\x7f' ) + assert( Int04.to_oer() == b'\x01\x7f' ) + assert( Int04.to_coer() == b'\x01\x7f' ) # decoding Int04.from_aper(b'\x01~') assert( Int04._val == 127 ) @@ -380,7 +435,12 @@ def _test_rt_base(): assert( Int04.to_jer() == '127' ) Int04.from_jer('127') assert( Int04._val == 127 ) - + # OER/COER + Int04.from_oer(b'\x01\x7f') + assert( Int04._val == 127) + Int04.from_coer(b'\x01\x7f') + assert( Int04._val == 127 ) + # Int05 ::= INTEGER (0..MAX) Int05 = Mod['Int05'] Int05.from_asn1('128') @@ -390,6 +450,8 @@ def _test_rt_base(): assert( Int05.to_ber() == Int05.to_ber_ws() == b'\x02\x02\x00\x80' ) assert( Int05.to_cer() == Int05.to_cer_ws() == b'\x02\x02\x00\x80' ) assert( Int05.to_der() == Int05.to_der_ws() == b'\x02\x02\x00\x80' ) + assert( Int05.to_oer() == b'\x01\x80' ) + assert( Int05.to_coer() == b'\x01\x80' ) # decoding Int05.from_aper(b'\x01\x80') assert( Int05._val == 128 ) @@ -416,7 +478,12 @@ def _test_rt_base(): assert( Int05.to_jer() == '128' ) Int05.from_jer('128') assert( Int05._val == 128 ) - + # OER/COER + Int05.from_oer(b'\x01\x80') + assert( Int05._val == 128 ) + Int05.from_coer(b'\x01\x80') + assert( Int05._val == 128 ) + # Int06 ::= INTEGER (3..6) Int06 = Mod['Int06'] Int06.from_asn1('3') @@ -426,6 +493,8 @@ def _test_rt_base(): assert( Int06.to_ber() == Int06.to_ber_ws() == b'\x02\x01\x03' ) assert( Int06.to_cer() == Int06.to_cer_ws() == b'\x02\x01\x03' ) assert( Int06.to_der() == Int06.to_der_ws() == b'\x02\x01\x03' ) + assert( Int06.to_oer() == b'\x03' ) + assert( Int06.to_coer() == b'\x03' ) # decoding Int06.from_aper(b'\x00') assert( Int06._val == 3 ) @@ -447,6 +516,10 @@ def _test_rt_base(): assert( Int06._val == 3 ) Int06.from_der_ws(b'\x02\x01\x03') assert( Int06._val == 3 ) + Int06.from_oer(b'\x03') + assert( Int06._val == 3 ) + Int06.from_coer(b'\x03') + assert( Int06._val == 3 ) # Int06.from_asn1('6') # encoding @@ -455,6 +528,8 @@ def _test_rt_base(): assert( Int06.to_ber() == Int06.to_ber_ws() == b'\x02\x01\x06' ) assert( Int06.to_cer() == Int06.to_cer_ws() == b'\x02\x01\x06' ) assert( Int06.to_der() == Int06.to_der_ws() == b'\x02\x01\x06' ) + assert( Int06.to_oer() == b'\x06' ) + assert( Int06.to_coer() == b'\x06' ) # decoding Int06.from_aper(b'\xc0') assert( Int06._val == 6 ) @@ -476,7 +551,11 @@ def _test_rt_base(): assert( Int06._val == 6 ) Int06.from_der_ws(b'\x02\x01\x06') assert( Int06._val == 6 ) - + Int06.from_oer(b'\x06') + assert( Int06._val == 6 ) + Int06.from_coer(b'\x06') + assert( Int06._val == 6 ) + # Int07 ::= INTEGER (4000..4254) Int07 = Mod['Int07'] Int07.from_asn1('4002') @@ -486,6 +565,8 @@ def _test_rt_base(): assert( Int07.to_ber() == Int07.to_ber_ws() == b'\x02\x02\x0f\xa2' ) assert( Int07.to_cer() == Int07.to_cer_ws() == b'\x02\x02\x0f\xa2' ) assert( Int07.to_der() == Int07.to_der_ws() == b'\x02\x02\x0f\xa2' ) + assert( Int07.to_oer() == b'\x0f\xa2' ) + assert( Int07.to_coer() == b'\x0f\xa2' ) # decoding Int07.from_aper(b'\x02') assert( Int07._val == 4002 ) @@ -507,6 +588,10 @@ def _test_rt_base(): assert( Int07._val == 4002 ) Int07.from_der_ws(b'\x02\x02\x0f\xa2') assert( Int07._val == 4002 ) + Int07.from_oer(b'\x0f\xa2') + assert( Int07._val == 4002 ) + Int07.from_coer(b'\x0f\xa2') + assert( Int07._val == 4002 ) # Int07.from_asn1('4129') # encoding @@ -515,6 +600,8 @@ def _test_rt_base(): assert( Int07.to_ber() == Int07.to_ber_ws() == b'\x02\x02\x10!' ) assert( Int07.to_cer() == Int07.to_cer_ws() == b'\x02\x02\x10!' ) assert( Int07.to_der() == Int07.to_der_ws() == b'\x02\x02\x10!' ) + assert( Int07.to_oer() == b'\x10!' ) + assert( Int07.to_coer() == b'\x10!' ) # decoding Int07.from_aper(b'\x81') assert( Int07._val == 4129 ) @@ -536,7 +623,11 @@ def _test_rt_base(): assert( Int07._val == 4129 ) Int07.from_der_ws(b'\x02\x02\x10!') assert( Int07._val == 4129 ) - + Int07.from_oer(b'\x10!') + assert( Int07._val == 4129 ) + Int07.from_coer(b'\x10!') + assert( Int07._val == 4129 ) + # Int08 ::= INTEGER (4000..4255) Int08 = Mod['Int08'] Int08.from_asn1('4255') @@ -546,6 +637,8 @@ def _test_rt_base(): assert( Int08.to_ber() == Int08.to_ber_ws() == b'\x02\x02\x10\x9f' ) assert( Int08.to_cer() == Int08.to_cer_ws() == b'\x02\x02\x10\x9f' ) assert( Int08.to_der() == Int08.to_der_ws() == b'\x02\x02\x10\x9f' ) + assert( Int08.to_oer() == b'\x10\x9f' ) + assert( Int08.to_coer() == b'\x10\x9f' ) # decoding Int08.from_aper(b'\xff') assert( Int08._val == 4255 ) @@ -567,7 +660,11 @@ def _test_rt_base(): assert( Int08._val == 4255 ) Int08.from_der_ws(b'\x02\x02\x10\x9f') assert( Int08._val == 4255 ) - + Int08.from_oer(b'\x10\x9f') + assert( Int08._val == 4255 ) + Int08.from_coer(b'\x10\x9f') + assert( Int08._val == 4255 ) + # Int09 ::= INTEGER (0..32000) Int09 = Mod['Int09'] Int09.from_asn1('31000') @@ -577,6 +674,8 @@ def _test_rt_base(): assert( Int09.to_ber() == Int09.to_ber_ws() == b'\x02\x02y\x18' ) assert( Int09.to_cer() == Int09.to_cer_ws() == b'\x02\x02y\x18' ) assert( Int09.to_der() == Int09.to_der_ws() == b'\x02\x02y\x18' ) + assert( Int09.to_oer() == b'y\x18' ) + assert( Int09.to_coer() == b'y\x18' ) # decoding Int09.from_aper(b'y\x18') assert( Int09._val == 31000 ) @@ -598,7 +697,11 @@ def _test_rt_base(): assert( Int09._val == 31000 ) Int09.from_der_ws(b'\x02\x02y\x18') assert( Int09._val == 31000 ) - + Int09.from_oer(b'y\x18') + assert( Int09._val == 31000 ) + Int09.from_coer(b'y\x18') + assert( Int09._val == 31000 ) + # Int10 ::= INTEGER (1..65538) Int10 = Mod['Int10'] Int10.from_asn1('1') @@ -608,6 +711,8 @@ def _test_rt_base(): assert( Int10.to_ber() == Int10.to_ber_ws() == b'\x02\x01\x01' ) assert( Int10.to_cer() == Int10.to_cer_ws() == b'\x02\x01\x01' ) assert( Int10.to_der() == Int10.to_der_ws() == b'\x02\x01\x01' ) + assert( Int10.to_oer() == b'\x00\x00\x00\x01' ) + assert( Int10.to_coer() == b'\x00\x00\x00\x01' ) # decoding Int10.from_aper(b'\x00\x00') assert( Int10._val == 1 ) @@ -629,6 +734,10 @@ def _test_rt_base(): assert( Int10._val == 1 ) Int10.from_der_ws(b'\x02\x01\x01') assert( Int10._val == 1 ) + Int10.from_oer(b'\x00\x00\x00\x01') + assert( Int10._val == 1 ) + Int10.from_coer(b'\x00\x00\x00\x01') + assert( Int10._val == 1 ) # Int10.from_asn1('257') # encoding @@ -637,6 +746,8 @@ def _test_rt_base(): assert( Int10.to_ber() == Int10.to_ber_ws() == b'\x02\x02\x01\x01' ) assert( Int10.to_cer() == Int10.to_cer_ws() == b'\x02\x02\x01\x01' ) assert( Int10.to_der() == Int10.to_der_ws() == b'\x02\x02\x01\x01' ) + assert( Int10.to_oer() == b'\x00\x00\x01\x01' ) + assert( Int10.to_coer() == b'\x00\x00\x01\x01' ) # decoding Int10.from_aper(b'@\x01\x00') assert( Int10._val == 257 ) @@ -658,6 +769,10 @@ def _test_rt_base(): assert( Int10._val == 257 ) Int10.from_der_ws(b'\x02\x02\x01\x01') assert( Int10._val == 257 ) + Int10.from_oer(b'\x00\x00\x01\x01') + assert( Int10._val == 257 ) + Int10.from_coer(b'\x00\x00\x01\x01') + assert( Int10._val == 257 ) # Int10.from_asn1('65538') # encoding @@ -666,6 +781,8 @@ def _test_rt_base(): assert( Int10.to_ber() == Int10.to_ber_ws() == b'\x02\x03\x01\x00\x02' ) assert( Int10.to_cer() == Int10.to_cer_ws() == b'\x02\x03\x01\x00\x02' ) assert( Int10.to_der() == Int10.to_der_ws() == b'\x02\x03\x01\x00\x02' ) + assert( Int10.to_oer() == b'\x00\x01\x00\x02' ) + assert( Int10.to_coer() == b'\x00\x01\x00\x02' ) # decoding Int10.from_aper(b'\x80\x01\x00\x01') assert( Int10._val == 65538 ) @@ -687,7 +804,11 @@ def _test_rt_base(): assert( Int10._val == 65538 ) Int10.from_der_ws(b'\x02\x03\x01\x00\x02') assert( Int10._val == 65538 ) - + Int10.from_oer(b'\x00\x01\x00\x02') + assert( Int10._val == 65538 ) + Int10.from_coer(b'\x00\x01\x00\x02') + assert( Int10._val == 65538 ) + # Int12 ::= INTEGER (-1..MAX, ...) Int12 = Mod['Int12'] Int12.from_asn1('-100') @@ -697,6 +818,8 @@ def _test_rt_base(): assert( Int12.to_ber() == Int12.to_ber_ws() == b'\x02\x01\x9c' ) assert( Int12.to_cer() == Int12.to_cer_ws() == b'\x02\x01\x9c' ) assert( Int12.to_der() == Int12.to_der_ws() == b'\x02\x01\x9c' ) + assert( Int12.to_oer() == b'\x01\x9c' ) + assert( Int12.to_coer() == b'\x01\x9c' ) # decoding Int12.from_aper(b'\x80\x01\x9c') assert( Int12._val == -100 ) @@ -723,7 +846,12 @@ def _test_rt_base(): assert( Int12.to_jer() == '-100' ) Int12.from_jer('-100') assert( Int12._val == -100 ) - + # OER/COER + Int12.from_oer(b'\x01\x9c') + assert( Int12._val == -100 ) + Int12.from_coer(b'\x01\x9c') + assert( Int12._val == -100 ) + # Int13 ::= INTEGER (3..6, ...) # encoding untested against commercial tools (often bound to 32 bits integers) Int13 = Mod['Int13'] @@ -734,6 +862,8 @@ def _test_rt_base(): assert( Int13.to_ber() == Int13.to_ber_ws() == b'\x02\x11\x03\xa0\xc9 u\xc0\xdb\xf3\xb8\xac\xbc_\x96\xce?\n\xd2' ) assert( Int13.to_cer() == Int13.to_cer_ws() == b'\x02\x11\x03\xa0\xc9 u\xc0\xdb\xf3\xb8\xac\xbc_\x96\xce?\n\xd2' ) assert( Int13.to_der() == Int13.to_der_ws() == b'\x02\x11\x03\xa0\xc9 u\xc0\xdb\xf3\xb8\xac\xbc_\x96\xce?\n\xd2' ) + assert( Int13.to_oer() == b'\x11\x03\xa0\xc9 u\xc0\xdb\xf3\xb8\xac\xbc_\x96\xce?\n\xd2' ) + assert( Int13.to_coer() == b'\x11\x03\xa0\xc9 u\xc0\xdb\xf3\xb8\xac\xbc_\x96\xce?\n\xd2' ) # decoding Int13.from_aper(b'\x80\x11\x03\xa0\xc9 u\xc0\xdb\xf3\xb8\xac\xbc_\x96\xce?\n\xd2') assert( Int13._val == 1234567890123456789012345678901234567890 ) @@ -760,7 +890,12 @@ def _test_rt_base(): assert( Int13.to_jer() == '1234567890123456789012345678901234567890' ) Int13.from_jer('1234567890123456789012345678901234567890') assert( Int13._val == 1234567890123456789012345678901234567890 ) - + # OER/COER + Int13.from_oer(b'\x11\x03\xa0\xc9 u\xc0\xdb\xf3\xb8\xac\xbc_\x96\xce?\n\xd2') + assert( Int13._val == 1234567890123456789012345678901234567890 ) + Int13.from_coer(b'\x11\x03\xa0\xc9 u\xc0\xdb\xf3\xb8\xac\xbc_\x96\xce?\n\xd2') + assert( Int13._val == 1234567890123456789012345678901234567890 ) + # Rea01 ::= REAL Rea01 = Mod['Rea01'] Rea01.from_asn1('8.56323e215')