Beispiel #1
0
 def testIndefModeChunkedSubst(self):
     assert decoder.decode(
         ints2octs((36, 128, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111,
                    120, 0, 0)),
         substrateFun=lambda a, b, c: (b, str2octs(''))
     ) == (ints2octs(
         (4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111, 120, 0, 0)), str2octs(''))
    def encode(self, encodeFun, value, defMode, maxChunkSize, ifNotEmpty=False):
        substrate, isConstructed, isOctets = self.encodeValue(
            encodeFun, value, defMode, maxChunkSize, ifNotEmpty=ifNotEmpty
        )

        if ifNotEmpty and not substrate:
            return substrate

        tagSet = value.tagSet

        # tagged value?
        if tagSet:
            if not isConstructed:  # primitive form implies definite mode
                defMode = True
            header = self.encodeTag(tagSet[-1], isConstructed)
            header += self.encodeLength(len(substrate), defMode)

            if isOctets:
                substrate = ints2octs(header) + substrate
            else:
                substrate = ints2octs(header + substrate)

            eoo =  self._encodeEndOfOctets(encodeFun, defMode)
            if eoo:
                substrate += eoo

        return substrate
Beispiel #3
0
 def testSpec(self):
     try:
         decoder.decode(ints2octs((2, 1, 12)), asn1Spec=univ.Null()) == (12, null)
     except PyAsn1Error:
         pass
     else:
         assert 0, "wrong asn1Spec worked out"
     assert decoder.decode(ints2octs((2, 1, 12)), asn1Spec=univ.Integer()) == (12, null)
Beispiel #4
0
    def _bitsFilter(value):
        # rfc1902.Bits does not really initialize from sequences
        # Clean bits values
        # .1.3.6.1.2.1.17.6.1.1.1.0 = BITS: 5B 00 00 00   [[...]1 3 4 6 7
        match = re.match(r'^([0-9a-fA-F]{2}(\s+[0-9a-fA-F]{2})*)\s+\[', value)
        if match:
            return ints2octs([int(y, 16) for y in match.group(1).split(' ')])

        return ints2octs([int(y, 16) for y in value.split(' ')])
Beispiel #5
0
 def testDefModeChunkedSubst(self):
     assert decoder.decode(
         ints2octs(
             (36, 23, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111, 120)
         ),
         substrateFun=lambda a, b, c: (b, c),
     ) == (
         ints2octs((4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111, 120)),
         23,
     )
Beispiel #6
0
 def testWithOptionalAndDefaultedIndefModeSubst(self):
     assert decoder.decode(
         ints2octs(
             (48, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)
         ),
         substrateFun=lambda a, b, c: (b, c),
     ) == (
         ints2octs((5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)),
         -1,
     )
Beispiel #7
0
    def encode(self, value, asn1Spec=None, encodeFun=None, **options):

        if asn1Spec is None:
            tagSet = value.tagSet
        else:
            tagSet = asn1Spec.tagSet

        # untagged item?
        if not tagSet:
            substrate, isConstructed, isOctets = self.encodeValue(
                value, asn1Spec, encodeFun, **options
            )
            return substrate

        defMode = options.get('defMode', True)

        for idx, singleTag in enumerate(tagSet.superTags):

            defModeOverride = defMode

            # base tag?
            if not idx:
                substrate, isConstructed, isOctets = self.encodeValue(
                    value, asn1Spec, encodeFun, **options
                )

                if not substrate and isConstructed and options.get('ifNotEmpty', False):
                    return substrate

                # primitive form implies definite mode
                if not isConstructed:
                    defModeOverride = True

            header = self.encodeTag(singleTag, isConstructed)
            header += self.encodeLength(len(substrate), defModeOverride)

            if isOctets:
                substrate = ints2octs(header) + substrate

                if not defModeOverride:
                    substrate += self.eooOctetsSubstrate

            else:
                substrate = header + substrate

                if not defModeOverride:
                    substrate += self.eooIntegerSubstrate

        if not isOctets:
            substrate = ints2octs(substrate)

        return substrate
 def testTaggedUndefLength(self):
     s = self.s.subtype(
         explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 4)
     )
     s.setComponentByPosition(2, univ.OctetString('abcdefgh'))
     assert encoder.encode(s, defMode=False, maxChunkSize=3) == ints2octs(
         (164, 128, 36, 128, 4, 3, 97, 98, 99, 4, 3, 100, 101, 102, 4, 2, 103, 104, 0, 0, 0, 0))
 def testBin4(self):
     # choose binEncBase automatically for all further Real (testBin[4-7])
     binEncBase, encoder.typeMap[univ.Real.typeId].binEncBase = encoder.typeMap[univ.Real.typeId].binEncBase, None
     assert encoder.encode(
         univ.Real((1, 2, 0))  # check exponenta = 0
     ) == ints2octs((9, 3, 128, 0, 1))
     encoder.typeMap[univ.Real.typeId].binEncBase = binEncBase
 def testBin3(self):
     # change binEncBase in the RealEncoder instance => for all further Real
     binEncBase, encoder.typeMap[univ.Real.typeId].binEncBase = encoder.typeMap[univ.Real.typeId].binEncBase, 16
     assert encoder.encode(
         univ.Real((0.00390625, 2, 0))  # check encbase = 16
     ) == ints2octs((9, 3, 160, 254, 1))
     encoder.typeMap[univ.Real.typeId].binEncBase = binEncBase
Beispiel #11
0
 def testNoEooData(self):
     try:
         decoder.decode(ints2octs((0x23, 0x80, 0x00, 0x01, 0x00)))
     except PyAsn1Error:
         pass
     else:
         assert 0, 'end-of-contents octets accepted with unexpected data'
Beispiel #12
0
 def testIndefModeChunked(self):
     assert decoder.decode(
         ints2octs(
             (
                 36,
                 128,
                 4,
                 4,
                 81,
                 117,
                 105,
                 99,
                 4,
                 4,
                 107,
                 32,
                 98,
                 114,
                 4,
                 4,
                 111,
                 119,
                 110,
                 32,
                 4,
                 3,
                 102,
                 111,
                 120,
                 0,
                 0,
             )
         )
     ) == (str2octs("Quick brown fox"), null)
Beispiel #13
0
 def testTagFormat(self):
     try:
         decoder.decode(ints2octs((33, 1, 1)))
     except PyAsn1Error:
         pass
     else:
         assert 0, 'wrong tagFormat worked out'
Beispiel #14
0
 def testNoLongFormEoo(self):
     try:
         decoder.decode(ints2octs((0x23, 0x80, 0x00, 0x81, 0x00)))
     except PyAsn1Error:
         pass
     else:
         assert 0, 'end-of-contents octets accepted with invalid long-form length'
Beispiel #15
0
 def testNoConstructedEoo(self):
     try:
         decoder.decode(ints2octs((0x23, 0x80, 0x20, 0x00)))
     except PyAsn1Error:
         pass
     else:
         assert 0, 'end-of-contents octets accepted with invalid constructed encoding'
Beispiel #16
0
 def testUnexpectedEoo(self):
     try:
         decoder.decode(ints2octs((0, 0)))
     except PyAsn1Error:
         pass
     else:
         assert 0, 'end-of-contents octets accepted at top level'
Beispiel #17
0
 def testDefiniteNoEoo(self):
     try:
         decoder.decode(ints2octs((0x23, 0x02, 0x00, 0x00)))
     except PyAsn1Error:
         pass
     else:
         assert 0, 'end-of-contents octets accepted inside definite-length encoding'
Beispiel #18
0
 def testReservedLength(self):
     try:
         decoder.decode(ints2octs((6, 255, 0)))
     except PyAsn1Error:
         pass
     else:
         assert 0, 'reserved length tolarated'
Beispiel #19
0
 def testShortEncoding(self):
     try:
         decoder.decode(ints2octs((9, 1, 131)))
     except PyAsn1Error:
         pass
     else:
         assert 0, 'accepted too-short real'
Beispiel #20
0
 def testZeroLength(self):
     try:
         decoder.decode(ints2octs((6, 0, 0)))
     except PyAsn1Error:
         pass
     else:
         assert 0, 'zero length tolarated'
Beispiel #21
0
 def testIndefiniteLength(self):
     try:
         decoder.decode(ints2octs((6, 128, 0)))
     except PyAsn1Error:
         pass
     else:
         assert 0, 'indefinite length tolarated'
Beispiel #22
0
 def testTypeChecking(self):
     try:
         decoder.decode(ints2octs((35, 4, 2, 2, 42, 42)))
     except PyAsn1Error:
         pass
     else:
         assert 0, 'accepted mis-encoded bit-string constructed out of an integer'
Beispiel #23
0
 def testBadSpec(self):
     try:
         decoder.decode(ints2octs((48, 2, 5, 0)), asn1Spec='not an Asn1Item')
     except PyAsn1Error:
         pass
     else:
         assert 0, 'Invalid asn1Spec accepted'
Beispiel #24
0
 def testWithOptionalIndefModeChunked(self):
     self.__initWithOptional()
     assert decoder.decode(
         ints2octs((48, 128, 5, 0, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0,
                    0, 0, 0)),
         asn1Spec=self.s
     ) == (self.s, null)
Beispiel #25
0
 def testLeading0x80Case4(self):
     try:
         decoder.decode(ints2octs((6, 2, 0x80, 0x7F)))
     except PyAsn1Error:
         pass
     else:
         assert 0, "Leading 0x80 tolarated"
Beispiel #26
0
 def __init__(self, tagMap, typeMap={}):
     self.__tagMap = tagMap
     self.__typeMap = typeMap
     # Tag & TagSet objects caches
     self.__tagCache = {}
     self.__tagSetCache = {}
     self.__eooSentinel = ints2octs((0, 0))
Beispiel #27
0
 def testLeading0x80Case1(self):
     try:
         decoder.decode(ints2octs((6, 5, 85, 4, 128, 129, 0)))
     except PyAsn1Error:
         pass
     else:
         assert 0, "Leading 0x80 tolarated"
Beispiel #28
0
 def testIndefMode(self):
     v, s = decoder.decode(
         ints2octs(
             (
                 101,
                 128,
                 36,
                 128,
                 4,
                 15,
                 81,
                 117,
                 105,
                 99,
                 107,
                 32,
                 98,
                 114,
                 111,
                 119,
                 110,
                 32,
                 102,
                 111,
                 120,
                 0,
                 0,
                 0,
                 0,
             )
         )
     )
     assert self.o.isSameTypeWith(v)
     assert not s
Beispiel #29
0
 def testLarge2(self):
     assert decoder.decode(
         ints2octs(
             (
                 0x06,
                 0x13,
                 0x88,
                 0x37,
                 0x83,
                 0xC6,
                 0xDF,
                 0xD4,
                 0xCC,
                 0xB3,
                 0xFF,
                 0xFF,
                 0xFE,
                 0xF0,
                 0xB8,
                 0xD6,
                 0xB8,
                 0xCB,
                 0xE2,
                 0xB6,
                 0x47,
             )
         )
     ) == ((2, 999, 18446744073709551535184467440737095), null)
Beispiel #30
0
 def testWithOptionalAndDefaultedDefModeChunked(self):
     self.__initWithOptionalAndDefaulted()
     assert decoder.decode(
         ints2octs(
             (48, 24, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 2, 1, 1)),
         asn1Spec=self.s
     ) == (self.s, null)
Beispiel #31
0
 def testBin5(self):  # case of 2 octs for exponenta and negative exponenta
     assert decoder.decode(  # (3, 2, -1020) encoded with base = 16
         ints2octs((9, 4, 161, 255, 1, 3))) == (univ.Real(
             (3, 2, -1020)), null)
Beispiel #32
0
 def testDecoder(self):
     assert decoder.decode(
         ints2octs((28, 12, 0, 0, 0, 97, 0, 0, 0, 98, 0, 0, 0,
                    99))) == (char.UniversalString(
                        version_info[0] == 3 and 'abc'
                        or unicode('abc')), null)
Beispiel #33
0
 def testMinusOne(self):
     assert decoder.decode(ints2octs((2, 1, 255))) == (-1, null)
Beispiel #34
0
 def testBin4(self):  # check exponenta = 0
     assert decoder.decode(  # (1, 2, 0) encoded with base = 2
         ints2octs((9, 3, 128, 0, 1))) == (univ.Real((1, 2, 0)), null)
Beispiel #35
0
 def testMinusInf(self):
     assert decoder.decode(ints2octs(
         (9, 1, 65))) == (univ.Real('-inf'), null)
Beispiel #36
0
 def testBin3(self):  # check base = 16
     assert decoder.decode(  # (0.00390625, 2, 0) encoded with base = 16
         ints2octs((9, 3, 160, 254, 1))) == (univ.Real((1, 2, -8)), null)
Beispiel #37
0
 def testTaggedImIndefMode(self):
     s = univ.Any('\004\003fox').subtype(
         implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4))
     assert decoder.decode(ints2octs((164, 128, 4, 3, 102, 111, 120, 0, 0)),
                           asn1Spec=s) == (s, null)
Beispiel #38
0
 def testByUntaggedIndefMode(self):
     assert decoder.decode(ints2octs((4, 3, 102, 111, 120)),
                           asn1Spec=self.s) == (univ.Any('\004\003fox'),
                                                null)
Beispiel #39
0
 def testPosLong(self):
     assert decoder.decode(
         ints2octs((2, 9, 0, 255, 255, 255, 255, 255, 255, 255,
                    255))) == (0xffffffffffffffff, null)
Beispiel #40
0
 def testExplicitTagUndefLength(self):
     s = self.s.subtype(explicitTag=tag.Tag(tag.tagClassContext,
                                            tag.tagFormatConstructed, 4))
     s.setComponentByPosition(0, univ.Null(null))
     assert decoder.decode(ints2octs((164, 128, 5, 0, 0, 0)),
                           asn1Spec=s) == (s, null)
Beispiel #41
0
 def testEmpty(self):
     assert decoder.decode(ints2octs((9, 0))) == (univ.Real(0.0), null)
Beispiel #42
0
 def testWithoutSpec(self):
     self.s.setComponentByPosition(0, univ.Null(null))
     assert decoder.decode(ints2octs((5, 0))) == (self.s, null)
     assert decoder.decode(ints2octs((5, 0))) == (univ.Null(null), null)
Beispiel #43
0
 def testWithOptionalAndDefaultedIndefModeChunked(self):
     self.__initWithOptionalAndDefaulted()
     assert decoder.decode(ints2octs(
         (48, 128, 5, 0, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32,
          98, 114, 4, 3, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)),
                           asn1Spec=self.s) == (self.s, null)
Beispiel #44
0
 def testUndefLength(self):
     self.s.setComponentByPosition(2, univ.OctetString('abcdefgh'))
     assert decoder.decode(ints2octs((36, 128, 4, 3, 97, 98, 99, 4, 3, 100,
                                      101, 102, 4, 2, 103, 104, 0, 0)),
                           asn1Spec=self.s) == (self.s, null)
Beispiel #45
0
 def testWithDefaultedIndefModeChunked(self):
     self.__initWithDefaulted()
     assert decoder.decode(ints2octs((48, 128, 5, 0, 2, 1, 1, 0, 0)),
                           asn1Spec=self.s) == (self.s, null)
Beispiel #46
0
 def testBySpec(self):
     self.s.setComponentByPosition(0, univ.Null(null))
     assert decoder.decode(ints2octs((5, 0)),
                           asn1Spec=self.s) == (self.s, null)
Beispiel #47
0
 def testWithOptionalDefModeChunked(self):
     self.__initWithOptional()
     assert decoder.decode(ints2octs(
         (48, 21, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98,
          114, 4, 3, 111, 119, 110)),
                           asn1Spec=self.s) == (self.s, null)
Beispiel #48
0
 def testZeroLong(self):
     assert decoder.decode(ints2octs((2, 1, 0))) == (0, null)
Beispiel #49
0
 def testWithOptionaIndefMode(self):
     self.__initWithOptional()
     assert decoder.decode(ints2octs(
         (48, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98,
          114, 111, 119, 110, 0, 0, 0, 0)),
                           asn1Spec=self.s) == (self.s, null)
Beispiel #50
0
 def testWithDefaultedDefMode(self):
     self.__initWithDefaulted()
     assert decoder.decode(ints2octs((48, 5, 5, 0, 2, 1, 1)),
                           asn1Spec=self.s) == (self.s, null)
Beispiel #51
0
 def testPosInt(self):
     assert decoder.decode(ints2octs((2, 1, 12))) == (12, null)
Beispiel #52
0
 def testNegInt(self):
     assert decoder.decode(ints2octs((2, 1, 244))) == (-12, null)
Beispiel #53
0
 def testWithOptionalAndDefaultedDefModeChunked(self):
     assert decoder.decode(
         ints2octs(
             (48, 24, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32,
              98, 114, 4, 3, 111, 119, 110, 2, 1, 1))) == (self.s, null)
Beispiel #54
0
 def testIndefModeChunked(self):
     self.__init()
     assert decoder.decode(ints2octs((48, 128, 5, 0, 0, 0)),
                           asn1Spec=self.s) == (self.s, null)
Beispiel #55
0
 def testDecoder(self):
     assert decoder.decode(ints2octs(
         (12, 3, 97, 98,
          99))) == (char.UTF8String(version_info[0] == 3 and 'abc'
                                    or unicode('abc')), null)
Beispiel #56
0
 def testCache(self):
     assert decoder.decode(ints2octs(
         (0x1f, 2, 1, 0))) == decoder.decode(ints2octs((0x1f, 2, 1, 0)))
Beispiel #57
0
 def testDecoder(self):
     assert decoder.decode(ints2octs(
         (30, 6, 0, 97, 0, 98, 0,
          99))) == (char.BMPString(version_info[0] == 3 and 'abc'
                                   or unicode('abc')), null)
Beispiel #58
0
 def testWithOptionalAndDefaultedIndefMode(self):
     assert decoder.decode(
         ints2octs(
             (48, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98,
              114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0))) == (self.s, null)
Beispiel #59
0
 def testByUntaggedSubst(self):
     assert decoder.decode(ints2octs((4, 3, 102, 111, 120)),
                           asn1Spec=self.s,
                           substrateFun=lambda a, b, c:
                           (b, c)) == (ints2octs((4, 3, 102, 111, 120)), 5)
Beispiel #60
0
 def testPlusInf(self):
     assert decoder.decode(ints2octs(
         (9, 1, 64))) == (univ.Real('inf'), null)