Updated DB_Helper by adding firebase methods.
This commit is contained in:
parent
485cc3bbba
commit
c82121d036
1810 changed files with 537281 additions and 1 deletions
1
venv/Lib/site-packages/pyasn1/codec/__init__.py
Normal file
1
venv/Lib/site-packages/pyasn1/codec/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
# This file is necessary to make this directory a package.
|
Binary file not shown.
1
venv/Lib/site-packages/pyasn1/codec/ber/__init__.py
Normal file
1
venv/Lib/site-packages/pyasn1/codec/ber/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
# This file is necessary to make this directory a package.
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1682
venv/Lib/site-packages/pyasn1/codec/ber/decoder.py
Normal file
1682
venv/Lib/site-packages/pyasn1/codec/ber/decoder.py
Normal file
File diff suppressed because it is too large
Load diff
890
venv/Lib/site-packages/pyasn1/codec/ber/encoder.py
Normal file
890
venv/Lib/site-packages/pyasn1/codec/ber/encoder.py
Normal file
|
@ -0,0 +1,890 @@
|
|||
#
|
||||
# This file is part of pyasn1 software.
|
||||
#
|
||||
# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com>
|
||||
# License: http://snmplabs.com/pyasn1/license.html
|
||||
#
|
||||
import sys
|
||||
|
||||
from pyasn1 import debug
|
||||
from pyasn1 import error
|
||||
from pyasn1.codec.ber import eoo
|
||||
from pyasn1.compat.integer import to_bytes
|
||||
from pyasn1.compat.octets import (int2oct, oct2int, ints2octs, null,
|
||||
str2octs, isOctetsType)
|
||||
from pyasn1.type import char
|
||||
from pyasn1.type import tag
|
||||
from pyasn1.type import univ
|
||||
from pyasn1.type import useful
|
||||
|
||||
__all__ = ['encode']
|
||||
|
||||
LOG = debug.registerLoggee(__name__, flags=debug.DEBUG_ENCODER)
|
||||
|
||||
|
||||
class AbstractItemEncoder(object):
|
||||
supportIndefLenMode = True
|
||||
|
||||
# An outcome of otherwise legit call `encodeFun(eoo.endOfOctets)`
|
||||
eooIntegerSubstrate = (0, 0)
|
||||
eooOctetsSubstrate = ints2octs(eooIntegerSubstrate)
|
||||
|
||||
# noinspection PyMethodMayBeStatic
|
||||
def encodeTag(self, singleTag, isConstructed):
|
||||
tagClass, tagFormat, tagId = singleTag
|
||||
encodedTag = tagClass | tagFormat
|
||||
if isConstructed:
|
||||
encodedTag |= tag.tagFormatConstructed
|
||||
|
||||
if tagId < 31:
|
||||
return encodedTag | tagId,
|
||||
|
||||
else:
|
||||
substrate = tagId & 0x7f,
|
||||
|
||||
tagId >>= 7
|
||||
|
||||
while tagId:
|
||||
substrate = (0x80 | (tagId & 0x7f),) + substrate
|
||||
tagId >>= 7
|
||||
|
||||
return (encodedTag | 0x1F,) + substrate
|
||||
|
||||
def encodeLength(self, length, defMode):
|
||||
if not defMode and self.supportIndefLenMode:
|
||||
return (0x80,)
|
||||
|
||||
if length < 0x80:
|
||||
return length,
|
||||
|
||||
else:
|
||||
substrate = ()
|
||||
while length:
|
||||
substrate = (length & 0xff,) + substrate
|
||||
length >>= 8
|
||||
|
||||
substrateLen = len(substrate)
|
||||
|
||||
if substrateLen > 126:
|
||||
raise error.PyAsn1Error('Length octets overflow (%d)' % substrateLen)
|
||||
|
||||
return (0x80 | substrateLen,) + substrate
|
||||
|
||||
def encodeValue(self, value, asn1Spec, encodeFun, **options):
|
||||
raise error.PyAsn1Error('Not implemented')
|
||||
|
||||
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)
|
||||
|
||||
substrate = null
|
||||
|
||||
for idx, singleTag in enumerate(tagSet.superTags):
|
||||
|
||||
defModeOverride = defMode
|
||||
|
||||
# base tag?
|
||||
if not idx:
|
||||
try:
|
||||
substrate, isConstructed, isOctets = self.encodeValue(
|
||||
value, asn1Spec, encodeFun, **options
|
||||
)
|
||||
|
||||
except error.PyAsn1Error:
|
||||
exc = sys.exc_info()
|
||||
raise error.PyAsn1Error(
|
||||
'Error encoding %r: %s' % (value, exc[1]))
|
||||
|
||||
if LOG:
|
||||
LOG('encoded %svalue %s into %s' % (
|
||||
isConstructed and 'constructed ' or '', value, substrate
|
||||
))
|
||||
|
||||
if not substrate and isConstructed and options.get('ifNotEmpty', False):
|
||||
return substrate
|
||||
|
||||
if not isConstructed:
|
||||
defModeOverride = True
|
||||
|
||||
if LOG:
|
||||
LOG('overridden encoding mode into definitive for primitive type')
|
||||
|
||||
header = self.encodeTag(singleTag, isConstructed)
|
||||
|
||||
if LOG:
|
||||
LOG('encoded %stag %s into %s' % (
|
||||
isConstructed and 'constructed ' or '',
|
||||
singleTag, debug.hexdump(ints2octs(header))))
|
||||
|
||||
header += self.encodeLength(len(substrate), defModeOverride)
|
||||
|
||||
if LOG:
|
||||
LOG('encoded %s octets (tag + payload) into %s' % (
|
||||
len(substrate), debug.hexdump(ints2octs(header))))
|
||||
|
||||
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
|
||||
|
||||
|
||||
class EndOfOctetsEncoder(AbstractItemEncoder):
|
||||
def encodeValue(self, value, asn1Spec, encodeFun, **options):
|
||||
return null, False, True
|
||||
|
||||
|
||||
class BooleanEncoder(AbstractItemEncoder):
|
||||
supportIndefLenMode = False
|
||||
|
||||
def encodeValue(self, value, asn1Spec, encodeFun, **options):
|
||||
return value and (1,) or (0,), False, False
|
||||
|
||||
|
||||
class IntegerEncoder(AbstractItemEncoder):
|
||||
supportIndefLenMode = False
|
||||
supportCompactZero = False
|
||||
|
||||
def encodeValue(self, value, asn1Spec, encodeFun, **options):
|
||||
if value == 0:
|
||||
if LOG:
|
||||
LOG('encoding %spayload for zero INTEGER' % (
|
||||
self.supportCompactZero and 'no ' or ''
|
||||
))
|
||||
|
||||
# de-facto way to encode zero
|
||||
if self.supportCompactZero:
|
||||
return (), False, False
|
||||
else:
|
||||
return (0,), False, False
|
||||
|
||||
return to_bytes(int(value), signed=True), False, True
|
||||
|
||||
|
||||
class BitStringEncoder(AbstractItemEncoder):
|
||||
def encodeValue(self, value, asn1Spec, encodeFun, **options):
|
||||
if asn1Spec is not None:
|
||||
# TODO: try to avoid ASN.1 schema instantiation
|
||||
value = asn1Spec.clone(value)
|
||||
|
||||
valueLength = len(value)
|
||||
if valueLength % 8:
|
||||
alignedValue = value << (8 - valueLength % 8)
|
||||
else:
|
||||
alignedValue = value
|
||||
|
||||
maxChunkSize = options.get('maxChunkSize', 0)
|
||||
if not maxChunkSize or len(alignedValue) <= maxChunkSize * 8:
|
||||
substrate = alignedValue.asOctets()
|
||||
return int2oct(len(substrate) * 8 - valueLength) + substrate, False, True
|
||||
|
||||
if LOG:
|
||||
LOG('encoding into up to %s-octet chunks' % maxChunkSize)
|
||||
|
||||
baseTag = value.tagSet.baseTag
|
||||
|
||||
# strip off explicit tags
|
||||
if baseTag:
|
||||
tagSet = tag.TagSet(baseTag, baseTag)
|
||||
|
||||
else:
|
||||
tagSet = tag.TagSet()
|
||||
|
||||
alignedValue = alignedValue.clone(tagSet=tagSet)
|
||||
|
||||
stop = 0
|
||||
substrate = null
|
||||
while stop < valueLength:
|
||||
start = stop
|
||||
stop = min(start + maxChunkSize * 8, valueLength)
|
||||
substrate += encodeFun(alignedValue[start:stop], asn1Spec, **options)
|
||||
|
||||
return substrate, True, True
|
||||
|
||||
|
||||
class OctetStringEncoder(AbstractItemEncoder):
|
||||
|
||||
def encodeValue(self, value, asn1Spec, encodeFun, **options):
|
||||
|
||||
if asn1Spec is None:
|
||||
substrate = value.asOctets()
|
||||
|
||||
elif not isOctetsType(value):
|
||||
substrate = asn1Spec.clone(value).asOctets()
|
||||
|
||||
else:
|
||||
substrate = value
|
||||
|
||||
maxChunkSize = options.get('maxChunkSize', 0)
|
||||
|
||||
if not maxChunkSize or len(substrate) <= maxChunkSize:
|
||||
return substrate, False, True
|
||||
|
||||
if LOG:
|
||||
LOG('encoding into up to %s-octet chunks' % maxChunkSize)
|
||||
|
||||
# strip off explicit tags for inner chunks
|
||||
|
||||
if asn1Spec is None:
|
||||
baseTag = value.tagSet.baseTag
|
||||
|
||||
# strip off explicit tags
|
||||
if baseTag:
|
||||
tagSet = tag.TagSet(baseTag, baseTag)
|
||||
|
||||
else:
|
||||
tagSet = tag.TagSet()
|
||||
|
||||
asn1Spec = value.clone(tagSet=tagSet)
|
||||
|
||||
elif not isOctetsType(value):
|
||||
baseTag = asn1Spec.tagSet.baseTag
|
||||
|
||||
# strip off explicit tags
|
||||
if baseTag:
|
||||
tagSet = tag.TagSet(baseTag, baseTag)
|
||||
|
||||
else:
|
||||
tagSet = tag.TagSet()
|
||||
|
||||
asn1Spec = asn1Spec.clone(tagSet=tagSet)
|
||||
|
||||
pos = 0
|
||||
substrate = null
|
||||
|
||||
while True:
|
||||
chunk = value[pos:pos + maxChunkSize]
|
||||
if not chunk:
|
||||
break
|
||||
|
||||
substrate += encodeFun(chunk, asn1Spec, **options)
|
||||
pos += maxChunkSize
|
||||
|
||||
return substrate, True, True
|
||||
|
||||
|
||||
class NullEncoder(AbstractItemEncoder):
|
||||
supportIndefLenMode = False
|
||||
|
||||
def encodeValue(self, value, asn1Spec, encodeFun, **options):
|
||||
return null, False, True
|
||||
|
||||
|
||||
class ObjectIdentifierEncoder(AbstractItemEncoder):
|
||||
supportIndefLenMode = False
|
||||
|
||||
def encodeValue(self, value, asn1Spec, encodeFun, **options):
|
||||
if asn1Spec is not None:
|
||||
value = asn1Spec.clone(value)
|
||||
|
||||
oid = value.asTuple()
|
||||
|
||||
# Build the first pair
|
||||
try:
|
||||
first = oid[0]
|
||||
second = oid[1]
|
||||
|
||||
except IndexError:
|
||||
raise error.PyAsn1Error('Short OID %s' % (value,))
|
||||
|
||||
if 0 <= second <= 39:
|
||||
if first == 1:
|
||||
oid = (second + 40,) + oid[2:]
|
||||
elif first == 0:
|
||||
oid = (second,) + oid[2:]
|
||||
elif first == 2:
|
||||
oid = (second + 80,) + oid[2:]
|
||||
else:
|
||||
raise error.PyAsn1Error('Impossible first/second arcs at %s' % (value,))
|
||||
|
||||
elif first == 2:
|
||||
oid = (second + 80,) + oid[2:]
|
||||
|
||||
else:
|
||||
raise error.PyAsn1Error('Impossible first/second arcs at %s' % (value,))
|
||||
|
||||
octets = ()
|
||||
|
||||
# Cycle through subIds
|
||||
for subOid in oid:
|
||||
if 0 <= subOid <= 127:
|
||||
# Optimize for the common case
|
||||
octets += (subOid,)
|
||||
|
||||
elif subOid > 127:
|
||||
# Pack large Sub-Object IDs
|
||||
res = (subOid & 0x7f,)
|
||||
subOid >>= 7
|
||||
|
||||
while subOid:
|
||||
res = (0x80 | (subOid & 0x7f),) + res
|
||||
subOid >>= 7
|
||||
|
||||
# Add packed Sub-Object ID to resulted Object ID
|
||||
octets += res
|
||||
|
||||
else:
|
||||
raise error.PyAsn1Error('Negative OID arc %s at %s' % (subOid, value))
|
||||
|
||||
return octets, False, False
|
||||
|
||||
|
||||
class RealEncoder(AbstractItemEncoder):
|
||||
supportIndefLenMode = 0
|
||||
binEncBase = 2 # set to None to choose encoding base automatically
|
||||
|
||||
@staticmethod
|
||||
def _dropFloatingPoint(m, encbase, e):
|
||||
ms, es = 1, 1
|
||||
if m < 0:
|
||||
ms = -1 # mantissa sign
|
||||
|
||||
if e < 0:
|
||||
es = -1 # exponent sign
|
||||
|
||||
m *= ms
|
||||
|
||||
if encbase == 8:
|
||||
m *= 2 ** (abs(e) % 3 * es)
|
||||
e = abs(e) // 3 * es
|
||||
|
||||
elif encbase == 16:
|
||||
m *= 2 ** (abs(e) % 4 * es)
|
||||
e = abs(e) // 4 * es
|
||||
|
||||
while True:
|
||||
if int(m) != m:
|
||||
m *= encbase
|
||||
e -= 1
|
||||
continue
|
||||
break
|
||||
|
||||
return ms, int(m), encbase, e
|
||||
|
||||
def _chooseEncBase(self, value):
|
||||
m, b, e = value
|
||||
encBase = [2, 8, 16]
|
||||
if value.binEncBase in encBase:
|
||||
return self._dropFloatingPoint(m, value.binEncBase, e)
|
||||
|
||||
elif self.binEncBase in encBase:
|
||||
return self._dropFloatingPoint(m, self.binEncBase, e)
|
||||
|
||||
# auto choosing base 2/8/16
|
||||
mantissa = [m, m, m]
|
||||
exponent = [e, e, e]
|
||||
sign = 1
|
||||
encbase = 2
|
||||
e = float('inf')
|
||||
|
||||
for i in range(3):
|
||||
(sign,
|
||||
mantissa[i],
|
||||
encBase[i],
|
||||
exponent[i]) = self._dropFloatingPoint(mantissa[i], encBase[i], exponent[i])
|
||||
|
||||
if abs(exponent[i]) < abs(e) or (abs(exponent[i]) == abs(e) and mantissa[i] < m):
|
||||
e = exponent[i]
|
||||
m = int(mantissa[i])
|
||||
encbase = encBase[i]
|
||||
|
||||
if LOG:
|
||||
LOG('automatically chosen REAL encoding base %s, sign %s, mantissa %s, '
|
||||
'exponent %s' % (encbase, sign, m, e))
|
||||
|
||||
return sign, m, encbase, e
|
||||
|
||||
def encodeValue(self, value, asn1Spec, encodeFun, **options):
|
||||
if asn1Spec is not None:
|
||||
value = asn1Spec.clone(value)
|
||||
|
||||
if value.isPlusInf:
|
||||
return (0x40,), False, False
|
||||
|
||||
if value.isMinusInf:
|
||||
return (0x41,), False, False
|
||||
|
||||
m, b, e = value
|
||||
|
||||
if not m:
|
||||
return null, False, True
|
||||
|
||||
if b == 10:
|
||||
if LOG:
|
||||
LOG('encoding REAL into character form')
|
||||
|
||||
return str2octs('\x03%dE%s%d' % (m, e == 0 and '+' or '', e)), False, True
|
||||
|
||||
elif b == 2:
|
||||
fo = 0x80 # binary encoding
|
||||
ms, m, encbase, e = self._chooseEncBase(value)
|
||||
|
||||
if ms < 0: # mantissa sign
|
||||
fo |= 0x40 # sign bit
|
||||
|
||||
# exponent & mantissa normalization
|
||||
if encbase == 2:
|
||||
while m & 0x1 == 0:
|
||||
m >>= 1
|
||||
e += 1
|
||||
|
||||
elif encbase == 8:
|
||||
while m & 0x7 == 0:
|
||||
m >>= 3
|
||||
e += 1
|
||||
fo |= 0x10
|
||||
|
||||
else: # encbase = 16
|
||||
while m & 0xf == 0:
|
||||
m >>= 4
|
||||
e += 1
|
||||
fo |= 0x20
|
||||
|
||||
sf = 0 # scale factor
|
||||
|
||||
while m & 0x1 == 0:
|
||||
m >>= 1
|
||||
sf += 1
|
||||
|
||||
if sf > 3:
|
||||
raise error.PyAsn1Error('Scale factor overflow') # bug if raised
|
||||
|
||||
fo |= sf << 2
|
||||
eo = null
|
||||
if e == 0 or e == -1:
|
||||
eo = int2oct(e & 0xff)
|
||||
|
||||
else:
|
||||
while e not in (0, -1):
|
||||
eo = int2oct(e & 0xff) + eo
|
||||
e >>= 8
|
||||
|
||||
if e == 0 and eo and oct2int(eo[0]) & 0x80:
|
||||
eo = int2oct(0) + eo
|
||||
|
||||
if e == -1 and eo and not (oct2int(eo[0]) & 0x80):
|
||||
eo = int2oct(0xff) + eo
|
||||
|
||||
n = len(eo)
|
||||
if n > 0xff:
|
||||
raise error.PyAsn1Error('Real exponent overflow')
|
||||
|
||||
if n == 1:
|
||||
pass
|
||||
|
||||
elif n == 2:
|
||||
fo |= 1
|
||||
|
||||
elif n == 3:
|
||||
fo |= 2
|
||||
|
||||
else:
|
||||
fo |= 3
|
||||
eo = int2oct(n & 0xff) + eo
|
||||
|
||||
po = null
|
||||
|
||||
while m:
|
||||
po = int2oct(m & 0xff) + po
|
||||
m >>= 8
|
||||
|
||||
substrate = int2oct(fo) + eo + po
|
||||
|
||||
return substrate, False, True
|
||||
|
||||
else:
|
||||
raise error.PyAsn1Error('Prohibited Real base %s' % b)
|
||||
|
||||
|
||||
class SequenceEncoder(AbstractItemEncoder):
|
||||
omitEmptyOptionals = False
|
||||
|
||||
# TODO: handling three flavors of input is too much -- split over codecs
|
||||
|
||||
def encodeValue(self, value, asn1Spec, encodeFun, **options):
|
||||
|
||||
substrate = null
|
||||
|
||||
omitEmptyOptionals = options.get(
|
||||
'omitEmptyOptionals', self.omitEmptyOptionals)
|
||||
|
||||
if LOG:
|
||||
LOG('%sencoding empty OPTIONAL components' % (
|
||||
omitEmptyOptionals and 'not ' or ''))
|
||||
|
||||
if asn1Spec is None:
|
||||
# instance of ASN.1 schema
|
||||
inconsistency = value.isInconsistent
|
||||
if inconsistency:
|
||||
raise inconsistency
|
||||
|
||||
namedTypes = value.componentType
|
||||
|
||||
for idx, component in enumerate(value.values()):
|
||||
if namedTypes:
|
||||
namedType = namedTypes[idx]
|
||||
|
||||
if namedType.isOptional and not component.isValue:
|
||||
if LOG:
|
||||
LOG('not encoding OPTIONAL component %r' % (namedType,))
|
||||
continue
|
||||
|
||||
if namedType.isDefaulted and component == namedType.asn1Object:
|
||||
if LOG:
|
||||
LOG('not encoding DEFAULT component %r' % (namedType,))
|
||||
continue
|
||||
|
||||
if omitEmptyOptionals:
|
||||
options.update(ifNotEmpty=namedType.isOptional)
|
||||
|
||||
# wrap open type blob if needed
|
||||
if namedTypes and namedType.openType:
|
||||
|
||||
wrapType = namedType.asn1Object
|
||||
|
||||
if wrapType.typeId in (
|
||||
univ.SetOf.typeId, univ.SequenceOf.typeId):
|
||||
|
||||
substrate += encodeFun(
|
||||
component, asn1Spec,
|
||||
**dict(options, wrapType=wrapType.componentType))
|
||||
|
||||
else:
|
||||
chunk = encodeFun(component, asn1Spec, **options)
|
||||
|
||||
if wrapType.isSameTypeWith(component):
|
||||
substrate += chunk
|
||||
|
||||
else:
|
||||
substrate += encodeFun(chunk, wrapType, **options)
|
||||
|
||||
if LOG:
|
||||
LOG('wrapped with wrap type %r' % (wrapType,))
|
||||
|
||||
else:
|
||||
substrate += encodeFun(component, asn1Spec, **options)
|
||||
|
||||
else:
|
||||
# bare Python value + ASN.1 schema
|
||||
for idx, namedType in enumerate(asn1Spec.componentType.namedTypes):
|
||||
|
||||
try:
|
||||
component = value[namedType.name]
|
||||
|
||||
except KeyError:
|
||||
raise error.PyAsn1Error('Component name "%s" not found in %r' % (
|
||||
namedType.name, value))
|
||||
|
||||
if namedType.isOptional and namedType.name not in value:
|
||||
if LOG:
|
||||
LOG('not encoding OPTIONAL component %r' % (namedType,))
|
||||
continue
|
||||
|
||||
if namedType.isDefaulted and component == namedType.asn1Object:
|
||||
if LOG:
|
||||
LOG('not encoding DEFAULT component %r' % (namedType,))
|
||||
continue
|
||||
|
||||
if omitEmptyOptionals:
|
||||
options.update(ifNotEmpty=namedType.isOptional)
|
||||
|
||||
componentSpec = namedType.asn1Object
|
||||
|
||||
# wrap open type blob if needed
|
||||
if namedType.openType:
|
||||
|
||||
if componentSpec.typeId in (
|
||||
univ.SetOf.typeId, univ.SequenceOf.typeId):
|
||||
|
||||
substrate += encodeFun(
|
||||
component, componentSpec,
|
||||
**dict(options, wrapType=componentSpec.componentType))
|
||||
|
||||
else:
|
||||
chunk = encodeFun(component, componentSpec, **options)
|
||||
|
||||
if componentSpec.isSameTypeWith(component):
|
||||
substrate += chunk
|
||||
|
||||
else:
|
||||
substrate += encodeFun(chunk, componentSpec, **options)
|
||||
|
||||
if LOG:
|
||||
LOG('wrapped with wrap type %r' % (componentSpec,))
|
||||
|
||||
else:
|
||||
substrate += encodeFun(component, componentSpec, **options)
|
||||
|
||||
return substrate, True, True
|
||||
|
||||
|
||||
class SequenceOfEncoder(AbstractItemEncoder):
|
||||
def _encodeComponents(self, value, asn1Spec, encodeFun, **options):
|
||||
|
||||
if asn1Spec is None:
|
||||
inconsistency = value.isInconsistent
|
||||
if inconsistency:
|
||||
raise inconsistency
|
||||
|
||||
else:
|
||||
asn1Spec = asn1Spec.componentType
|
||||
|
||||
chunks = []
|
||||
|
||||
wrapType = options.pop('wrapType', None)
|
||||
|
||||
for idx, component in enumerate(value):
|
||||
chunk = encodeFun(component, asn1Spec, **options)
|
||||
|
||||
if (wrapType is not None and
|
||||
not wrapType.isSameTypeWith(component)):
|
||||
# wrap encoded value with wrapper container (e.g. ANY)
|
||||
chunk = encodeFun(chunk, wrapType, **options)
|
||||
|
||||
if LOG:
|
||||
LOG('wrapped with wrap type %r' % (wrapType,))
|
||||
|
||||
chunks.append(chunk)
|
||||
|
||||
return chunks
|
||||
|
||||
def encodeValue(self, value, asn1Spec, encodeFun, **options):
|
||||
chunks = self._encodeComponents(
|
||||
value, asn1Spec, encodeFun, **options)
|
||||
|
||||
return null.join(chunks), True, True
|
||||
|
||||
|
||||
class ChoiceEncoder(AbstractItemEncoder):
|
||||
def encodeValue(self, value, asn1Spec, encodeFun, **options):
|
||||
if asn1Spec is None:
|
||||
component = value.getComponent()
|
||||
else:
|
||||
names = [namedType.name for namedType in asn1Spec.componentType.namedTypes
|
||||
if namedType.name in value]
|
||||
if len(names) != 1:
|
||||
raise error.PyAsn1Error('%s components for Choice at %r' % (len(names) and 'Multiple ' or 'None ', value))
|
||||
|
||||
name = names[0]
|
||||
|
||||
component = value[name]
|
||||
asn1Spec = asn1Spec[name]
|
||||
|
||||
return encodeFun(component, asn1Spec, **options), True, True
|
||||
|
||||
|
||||
class AnyEncoder(OctetStringEncoder):
|
||||
def encodeValue(self, value, asn1Spec, encodeFun, **options):
|
||||
if asn1Spec is None:
|
||||
value = value.asOctets()
|
||||
elif not isOctetsType(value):
|
||||
value = asn1Spec.clone(value).asOctets()
|
||||
|
||||
return value, not options.get('defMode', True), True
|
||||
|
||||
|
||||
tagMap = {
|
||||
eoo.endOfOctets.tagSet: EndOfOctetsEncoder(),
|
||||
univ.Boolean.tagSet: BooleanEncoder(),
|
||||
univ.Integer.tagSet: IntegerEncoder(),
|
||||
univ.BitString.tagSet: BitStringEncoder(),
|
||||
univ.OctetString.tagSet: OctetStringEncoder(),
|
||||
univ.Null.tagSet: NullEncoder(),
|
||||
univ.ObjectIdentifier.tagSet: ObjectIdentifierEncoder(),
|
||||
univ.Enumerated.tagSet: IntegerEncoder(),
|
||||
univ.Real.tagSet: RealEncoder(),
|
||||
# Sequence & Set have same tags as SequenceOf & SetOf
|
||||
univ.SequenceOf.tagSet: SequenceOfEncoder(),
|
||||
univ.SetOf.tagSet: SequenceOfEncoder(),
|
||||
univ.Choice.tagSet: ChoiceEncoder(),
|
||||
# character string types
|
||||
char.UTF8String.tagSet: OctetStringEncoder(),
|
||||
char.NumericString.tagSet: OctetStringEncoder(),
|
||||
char.PrintableString.tagSet: OctetStringEncoder(),
|
||||
char.TeletexString.tagSet: OctetStringEncoder(),
|
||||
char.VideotexString.tagSet: OctetStringEncoder(),
|
||||
char.IA5String.tagSet: OctetStringEncoder(),
|
||||
char.GraphicString.tagSet: OctetStringEncoder(),
|
||||
char.VisibleString.tagSet: OctetStringEncoder(),
|
||||
char.GeneralString.tagSet: OctetStringEncoder(),
|
||||
char.UniversalString.tagSet: OctetStringEncoder(),
|
||||
char.BMPString.tagSet: OctetStringEncoder(),
|
||||
# useful types
|
||||
useful.ObjectDescriptor.tagSet: OctetStringEncoder(),
|
||||
useful.GeneralizedTime.tagSet: OctetStringEncoder(),
|
||||
useful.UTCTime.tagSet: OctetStringEncoder()
|
||||
}
|
||||
|
||||
# Put in ambiguous & non-ambiguous types for faster codec lookup
|
||||
typeMap = {
|
||||
univ.Boolean.typeId: BooleanEncoder(),
|
||||
univ.Integer.typeId: IntegerEncoder(),
|
||||
univ.BitString.typeId: BitStringEncoder(),
|
||||
univ.OctetString.typeId: OctetStringEncoder(),
|
||||
univ.Null.typeId: NullEncoder(),
|
||||
univ.ObjectIdentifier.typeId: ObjectIdentifierEncoder(),
|
||||
univ.Enumerated.typeId: IntegerEncoder(),
|
||||
univ.Real.typeId: RealEncoder(),
|
||||
# Sequence & Set have same tags as SequenceOf & SetOf
|
||||
univ.Set.typeId: SequenceEncoder(),
|
||||
univ.SetOf.typeId: SequenceOfEncoder(),
|
||||
univ.Sequence.typeId: SequenceEncoder(),
|
||||
univ.SequenceOf.typeId: SequenceOfEncoder(),
|
||||
univ.Choice.typeId: ChoiceEncoder(),
|
||||
univ.Any.typeId: AnyEncoder(),
|
||||
# character string types
|
||||
char.UTF8String.typeId: OctetStringEncoder(),
|
||||
char.NumericString.typeId: OctetStringEncoder(),
|
||||
char.PrintableString.typeId: OctetStringEncoder(),
|
||||
char.TeletexString.typeId: OctetStringEncoder(),
|
||||
char.VideotexString.typeId: OctetStringEncoder(),
|
||||
char.IA5String.typeId: OctetStringEncoder(),
|
||||
char.GraphicString.typeId: OctetStringEncoder(),
|
||||
char.VisibleString.typeId: OctetStringEncoder(),
|
||||
char.GeneralString.typeId: OctetStringEncoder(),
|
||||
char.UniversalString.typeId: OctetStringEncoder(),
|
||||
char.BMPString.typeId: OctetStringEncoder(),
|
||||
# useful types
|
||||
useful.ObjectDescriptor.typeId: OctetStringEncoder(),
|
||||
useful.GeneralizedTime.typeId: OctetStringEncoder(),
|
||||
useful.UTCTime.typeId: OctetStringEncoder()
|
||||
}
|
||||
|
||||
|
||||
class Encoder(object):
|
||||
fixedDefLengthMode = None
|
||||
fixedChunkSize = None
|
||||
|
||||
# noinspection PyDefaultArgument
|
||||
def __init__(self, tagMap, typeMap={}):
|
||||
self.__tagMap = tagMap
|
||||
self.__typeMap = typeMap
|
||||
|
||||
def __call__(self, value, asn1Spec=None, **options):
|
||||
try:
|
||||
if asn1Spec is None:
|
||||
typeId = value.typeId
|
||||
else:
|
||||
typeId = asn1Spec.typeId
|
||||
|
||||
except AttributeError:
|
||||
raise error.PyAsn1Error('Value %r is not ASN.1 type instance '
|
||||
'and "asn1Spec" not given' % (value,))
|
||||
|
||||
if LOG:
|
||||
LOG('encoder called in %sdef mode, chunk size %s for '
|
||||
'type %s, value:\n%s' % (not options.get('defMode', True) and 'in' or '', options.get('maxChunkSize', 0), asn1Spec is None and value.prettyPrintType() or asn1Spec.prettyPrintType(), value))
|
||||
|
||||
if self.fixedDefLengthMode is not None:
|
||||
options.update(defMode=self.fixedDefLengthMode)
|
||||
|
||||
if self.fixedChunkSize is not None:
|
||||
options.update(maxChunkSize=self.fixedChunkSize)
|
||||
|
||||
|
||||
try:
|
||||
concreteEncoder = self.__typeMap[typeId]
|
||||
|
||||
if LOG:
|
||||
LOG('using value codec %s chosen by type ID %s' % (concreteEncoder.__class__.__name__, typeId))
|
||||
|
||||
except KeyError:
|
||||
if asn1Spec is None:
|
||||
tagSet = value.tagSet
|
||||
else:
|
||||
tagSet = asn1Spec.tagSet
|
||||
|
||||
# use base type for codec lookup to recover untagged types
|
||||
baseTagSet = tag.TagSet(tagSet.baseTag, tagSet.baseTag)
|
||||
|
||||
try:
|
||||
concreteEncoder = self.__tagMap[baseTagSet]
|
||||
|
||||
except KeyError:
|
||||
raise error.PyAsn1Error('No encoder for %r (%s)' % (value, tagSet))
|
||||
|
||||
if LOG:
|
||||
LOG('using value codec %s chosen by tagSet %s' % (concreteEncoder.__class__.__name__, tagSet))
|
||||
|
||||
substrate = concreteEncoder.encode(value, asn1Spec, self, **options)
|
||||
|
||||
if LOG:
|
||||
LOG('codec %s built %s octets of substrate: %s\nencoder completed' % (concreteEncoder, len(substrate), debug.hexdump(substrate)))
|
||||
|
||||
return substrate
|
||||
|
||||
#: Turns ASN.1 object into BER octet stream.
|
||||
#:
|
||||
#: Takes any ASN.1 object (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
|
||||
#: walks all its components recursively and produces a BER octet stream.
|
||||
#:
|
||||
#: Parameters
|
||||
#: ----------
|
||||
#: value: either a Python or pyasn1 object (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
|
||||
#: A Python or pyasn1 object to encode. If Python object is given, `asnSpec`
|
||||
#: parameter is required to guide the encoding process.
|
||||
#:
|
||||
#: Keyword Args
|
||||
#: ------------
|
||||
#: asn1Spec:
|
||||
#: Optional ASN.1 schema or value object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
|
||||
#:
|
||||
#: defMode: :py:class:`bool`
|
||||
#: If :obj:`False`, produces indefinite length encoding
|
||||
#:
|
||||
#: maxChunkSize: :py:class:`int`
|
||||
#: Maximum chunk size in chunked encoding mode (0 denotes unlimited chunk size)
|
||||
#:
|
||||
#: Returns
|
||||
#: -------
|
||||
#: : :py:class:`bytes` (Python 3) or :py:class:`str` (Python 2)
|
||||
#: Given ASN.1 object encoded into BER octetstream
|
||||
#:
|
||||
#: Raises
|
||||
#: ------
|
||||
#: ~pyasn1.error.PyAsn1Error
|
||||
#: On encoding errors
|
||||
#:
|
||||
#: Examples
|
||||
#: --------
|
||||
#: Encode Python value into BER with ASN.1 schema
|
||||
#:
|
||||
#: .. code-block:: pycon
|
||||
#:
|
||||
#: >>> seq = SequenceOf(componentType=Integer())
|
||||
#: >>> encode([1, 2, 3], asn1Spec=seq)
|
||||
#: b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03'
|
||||
#:
|
||||
#: Encode ASN.1 value object into BER
|
||||
#:
|
||||
#: .. code-block:: pycon
|
||||
#:
|
||||
#: >>> seq = SequenceOf(componentType=Integer())
|
||||
#: >>> seq.extend([1, 2, 3])
|
||||
#: >>> encode(seq)
|
||||
#: b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03'
|
||||
#:
|
||||
encode = Encoder(tagMap, typeMap)
|
28
venv/Lib/site-packages/pyasn1/codec/ber/eoo.py
Normal file
28
venv/Lib/site-packages/pyasn1/codec/ber/eoo.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
#
|
||||
# This file is part of pyasn1 software.
|
||||
#
|
||||
# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com>
|
||||
# License: http://snmplabs.com/pyasn1/license.html
|
||||
#
|
||||
from pyasn1.type import base
|
||||
from pyasn1.type import tag
|
||||
|
||||
__all__ = ['endOfOctets']
|
||||
|
||||
|
||||
class EndOfOctets(base.SimpleAsn1Type):
|
||||
defaultValue = 0
|
||||
tagSet = tag.initTagSet(
|
||||
tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x00)
|
||||
)
|
||||
|
||||
_instance = None
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
if cls._instance is None:
|
||||
cls._instance = object.__new__(cls, *args, **kwargs)
|
||||
|
||||
return cls._instance
|
||||
|
||||
|
||||
endOfOctets = EndOfOctets()
|
1
venv/Lib/site-packages/pyasn1/codec/cer/__init__.py
Normal file
1
venv/Lib/site-packages/pyasn1/codec/cer/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
# This file is necessary to make this directory a package.
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
114
venv/Lib/site-packages/pyasn1/codec/cer/decoder.py
Normal file
114
venv/Lib/site-packages/pyasn1/codec/cer/decoder.py
Normal file
|
@ -0,0 +1,114 @@
|
|||
#
|
||||
# This file is part of pyasn1 software.
|
||||
#
|
||||
# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com>
|
||||
# License: http://snmplabs.com/pyasn1/license.html
|
||||
#
|
||||
from pyasn1 import error
|
||||
from pyasn1.codec.ber import decoder
|
||||
from pyasn1.compat.octets import oct2int
|
||||
from pyasn1.type import univ
|
||||
|
||||
__all__ = ['decode']
|
||||
|
||||
|
||||
class BooleanDecoder(decoder.AbstractSimpleDecoder):
|
||||
protoComponent = univ.Boolean(0)
|
||||
|
||||
def valueDecoder(self, substrate, asn1Spec,
|
||||
tagSet=None, length=None, state=None,
|
||||
decodeFun=None, substrateFun=None,
|
||||
**options):
|
||||
head, tail = substrate[:length], substrate[length:]
|
||||
if not head or length != 1:
|
||||
raise error.PyAsn1Error('Not single-octet Boolean payload')
|
||||
byte = oct2int(head[0])
|
||||
# CER/DER specifies encoding of TRUE as 0xFF and FALSE as 0x0, while
|
||||
# BER allows any non-zero value as TRUE; cf. sections 8.2.2. and 11.1
|
||||
# in https://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
|
||||
if byte == 0xff:
|
||||
value = 1
|
||||
elif byte == 0x00:
|
||||
value = 0
|
||||
else:
|
||||
raise error.PyAsn1Error('Unexpected Boolean payload: %s' % byte)
|
||||
return self._createComponent(asn1Spec, tagSet, value, **options), tail
|
||||
|
||||
# TODO: prohibit non-canonical encoding
|
||||
BitStringDecoder = decoder.BitStringDecoder
|
||||
OctetStringDecoder = decoder.OctetStringDecoder
|
||||
RealDecoder = decoder.RealDecoder
|
||||
|
||||
tagMap = decoder.tagMap.copy()
|
||||
tagMap.update(
|
||||
{univ.Boolean.tagSet: BooleanDecoder(),
|
||||
univ.BitString.tagSet: BitStringDecoder(),
|
||||
univ.OctetString.tagSet: OctetStringDecoder(),
|
||||
univ.Real.tagSet: RealDecoder()}
|
||||
)
|
||||
|
||||
typeMap = decoder.typeMap.copy()
|
||||
|
||||
# Put in non-ambiguous types for faster codec lookup
|
||||
for typeDecoder in tagMap.values():
|
||||
if typeDecoder.protoComponent is not None:
|
||||
typeId = typeDecoder.protoComponent.__class__.typeId
|
||||
if typeId is not None and typeId not in typeMap:
|
||||
typeMap[typeId] = typeDecoder
|
||||
|
||||
|
||||
class Decoder(decoder.Decoder):
|
||||
pass
|
||||
|
||||
|
||||
#: Turns CER octet stream into an ASN.1 object.
|
||||
#:
|
||||
#: Takes CER octet-stream and decode it into an ASN.1 object
|
||||
#: (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative) which
|
||||
#: may be a scalar or an arbitrary nested structure.
|
||||
#:
|
||||
#: Parameters
|
||||
#: ----------
|
||||
#: substrate: :py:class:`bytes` (Python 3) or :py:class:`str` (Python 2)
|
||||
#: CER octet-stream
|
||||
#:
|
||||
#: Keyword Args
|
||||
#: ------------
|
||||
#: asn1Spec: any pyasn1 type object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
|
||||
#: A pyasn1 type object to act as a template guiding the decoder. Depending on the ASN.1 structure
|
||||
#: being decoded, *asn1Spec* may or may not be required. Most common reason for
|
||||
#: it to require is that ASN.1 structure is encoded in *IMPLICIT* tagging mode.
|
||||
#:
|
||||
#: Returns
|
||||
#: -------
|
||||
#: : :py:class:`tuple`
|
||||
#: A tuple of pyasn1 object recovered from CER substrate (:py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
|
||||
#: and the unprocessed trailing portion of the *substrate* (may be empty)
|
||||
#:
|
||||
#: Raises
|
||||
#: ------
|
||||
#: ~pyasn1.error.PyAsn1Error, ~pyasn1.error.SubstrateUnderrunError
|
||||
#: On decoding errors
|
||||
#:
|
||||
#: Examples
|
||||
#: --------
|
||||
#: Decode CER serialisation without ASN.1 schema
|
||||
#:
|
||||
#: .. code-block:: pycon
|
||||
#:
|
||||
#: >>> s, _ = decode(b'0\x80\x02\x01\x01\x02\x01\x02\x02\x01\x03\x00\x00')
|
||||
#: >>> str(s)
|
||||
#: SequenceOf:
|
||||
#: 1 2 3
|
||||
#:
|
||||
#: Decode CER serialisation with ASN.1 schema
|
||||
#:
|
||||
#: .. code-block:: pycon
|
||||
#:
|
||||
#: >>> seq = SequenceOf(componentType=Integer())
|
||||
#: >>> s, _ = decode(b'0\x80\x02\x01\x01\x02\x01\x02\x02\x01\x03\x00\x00', asn1Spec=seq)
|
||||
#: >>> str(s)
|
||||
#: SequenceOf:
|
||||
#: 1 2 3
|
||||
#:
|
||||
decode = Decoder(tagMap, decoder.typeMap)
|
313
venv/Lib/site-packages/pyasn1/codec/cer/encoder.py
Normal file
313
venv/Lib/site-packages/pyasn1/codec/cer/encoder.py
Normal file
|
@ -0,0 +1,313 @@
|
|||
#
|
||||
# This file is part of pyasn1 software.
|
||||
#
|
||||
# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com>
|
||||
# License: http://snmplabs.com/pyasn1/license.html
|
||||
#
|
||||
from pyasn1 import error
|
||||
from pyasn1.codec.ber import encoder
|
||||
from pyasn1.compat.octets import str2octs, null
|
||||
from pyasn1.type import univ
|
||||
from pyasn1.type import useful
|
||||
|
||||
__all__ = ['encode']
|
||||
|
||||
|
||||
class BooleanEncoder(encoder.IntegerEncoder):
|
||||
def encodeValue(self, value, asn1Spec, encodeFun, **options):
|
||||
if value == 0:
|
||||
substrate = (0,)
|
||||
else:
|
||||
substrate = (255,)
|
||||
return substrate, False, False
|
||||
|
||||
|
||||
class RealEncoder(encoder.RealEncoder):
|
||||
def _chooseEncBase(self, value):
|
||||
m, b, e = value
|
||||
return self._dropFloatingPoint(m, b, e)
|
||||
|
||||
|
||||
# specialized GeneralStringEncoder here
|
||||
|
||||
class TimeEncoderMixIn(object):
|
||||
Z_CHAR = ord('Z')
|
||||
PLUS_CHAR = ord('+')
|
||||
MINUS_CHAR = ord('-')
|
||||
COMMA_CHAR = ord(',')
|
||||
DOT_CHAR = ord('.')
|
||||
ZERO_CHAR = ord('0')
|
||||
|
||||
MIN_LENGTH = 12
|
||||
MAX_LENGTH = 19
|
||||
|
||||
def encodeValue(self, value, asn1Spec, encodeFun, **options):
|
||||
# CER encoding constraints:
|
||||
# - minutes are mandatory, seconds are optional
|
||||
# - sub-seconds must NOT be zero / no meaningless zeros
|
||||
# - no hanging fraction dot
|
||||
# - time in UTC (Z)
|
||||
# - only dot is allowed for fractions
|
||||
|
||||
if asn1Spec is not None:
|
||||
value = asn1Spec.clone(value)
|
||||
|
||||
numbers = value.asNumbers()
|
||||
|
||||
if self.PLUS_CHAR in numbers or self.MINUS_CHAR in numbers:
|
||||
raise error.PyAsn1Error('Must be UTC time: %r' % value)
|
||||
|
||||
if numbers[-1] != self.Z_CHAR:
|
||||
raise error.PyAsn1Error('Missing "Z" time zone specifier: %r' % value)
|
||||
|
||||
if self.COMMA_CHAR in numbers:
|
||||
raise error.PyAsn1Error('Comma in fractions disallowed: %r' % value)
|
||||
|
||||
if self.DOT_CHAR in numbers:
|
||||
|
||||
isModified = False
|
||||
|
||||
numbers = list(numbers)
|
||||
|
||||
searchIndex = min(numbers.index(self.DOT_CHAR) + 4, len(numbers) - 1)
|
||||
|
||||
while numbers[searchIndex] != self.DOT_CHAR:
|
||||
if numbers[searchIndex] == self.ZERO_CHAR:
|
||||
del numbers[searchIndex]
|
||||
isModified = True
|
||||
|
||||
searchIndex -= 1
|
||||
|
||||
searchIndex += 1
|
||||
|
||||
if searchIndex < len(numbers):
|
||||
if numbers[searchIndex] == self.Z_CHAR:
|
||||
# drop hanging comma
|
||||
del numbers[searchIndex - 1]
|
||||
isModified = True
|
||||
|
||||
if isModified:
|
||||
value = value.clone(numbers)
|
||||
|
||||
if not self.MIN_LENGTH < len(numbers) < self.MAX_LENGTH:
|
||||
raise error.PyAsn1Error('Length constraint violated: %r' % value)
|
||||
|
||||
options.update(maxChunkSize=1000)
|
||||
|
||||
return encoder.OctetStringEncoder.encodeValue(
|
||||
self, value, asn1Spec, encodeFun, **options
|
||||
)
|
||||
|
||||
|
||||
class GeneralizedTimeEncoder(TimeEncoderMixIn, encoder.OctetStringEncoder):
|
||||
MIN_LENGTH = 12
|
||||
MAX_LENGTH = 20
|
||||
|
||||
|
||||
class UTCTimeEncoder(TimeEncoderMixIn, encoder.OctetStringEncoder):
|
||||
MIN_LENGTH = 10
|
||||
MAX_LENGTH = 14
|
||||
|
||||
|
||||
class SetOfEncoder(encoder.SequenceOfEncoder):
|
||||
def encodeValue(self, value, asn1Spec, encodeFun, **options):
|
||||
chunks = self._encodeComponents(
|
||||
value, asn1Spec, encodeFun, **options)
|
||||
|
||||
# sort by serialised and padded components
|
||||
if len(chunks) > 1:
|
||||
zero = str2octs('\x00')
|
||||
maxLen = max(map(len, chunks))
|
||||
paddedChunks = [
|
||||
(x.ljust(maxLen, zero), x) for x in chunks
|
||||
]
|
||||
paddedChunks.sort(key=lambda x: x[0])
|
||||
|
||||
chunks = [x[1] for x in paddedChunks]
|
||||
|
||||
return null.join(chunks), True, True
|
||||
|
||||
|
||||
class SequenceOfEncoder(encoder.SequenceOfEncoder):
|
||||
def encodeValue(self, value, asn1Spec, encodeFun, **options):
|
||||
|
||||
if options.get('ifNotEmpty', False) and not len(value):
|
||||
return null, True, True
|
||||
|
||||
chunks = self._encodeComponents(
|
||||
value, asn1Spec, encodeFun, **options)
|
||||
|
||||
return null.join(chunks), True, True
|
||||
|
||||
|
||||
class SetEncoder(encoder.SequenceEncoder):
|
||||
@staticmethod
|
||||
def _componentSortKey(componentAndType):
|
||||
"""Sort SET components by tag
|
||||
|
||||
Sort regardless of the Choice value (static sort)
|
||||
"""
|
||||
component, asn1Spec = componentAndType
|
||||
|
||||
if asn1Spec is None:
|
||||
asn1Spec = component
|
||||
|
||||
if asn1Spec.typeId == univ.Choice.typeId and not asn1Spec.tagSet:
|
||||
if asn1Spec.tagSet:
|
||||
return asn1Spec.tagSet
|
||||
else:
|
||||
return asn1Spec.componentType.minTagSet
|
||||
else:
|
||||
return asn1Spec.tagSet
|
||||
|
||||
def encodeValue(self, value, asn1Spec, encodeFun, **options):
|
||||
|
||||
substrate = null
|
||||
|
||||
comps = []
|
||||
compsMap = {}
|
||||
|
||||
if asn1Spec is None:
|
||||
# instance of ASN.1 schema
|
||||
inconsistency = value.isInconsistent
|
||||
if inconsistency:
|
||||
raise inconsistency
|
||||
|
||||
namedTypes = value.componentType
|
||||
|
||||
for idx, component in enumerate(value.values()):
|
||||
if namedTypes:
|
||||
namedType = namedTypes[idx]
|
||||
|
||||
if namedType.isOptional and not component.isValue:
|
||||
continue
|
||||
|
||||
if namedType.isDefaulted and component == namedType.asn1Object:
|
||||
continue
|
||||
|
||||
compsMap[id(component)] = namedType
|
||||
|
||||
else:
|
||||
compsMap[id(component)] = None
|
||||
|
||||
comps.append((component, asn1Spec))
|
||||
|
||||
else:
|
||||
# bare Python value + ASN.1 schema
|
||||
for idx, namedType in enumerate(asn1Spec.componentType.namedTypes):
|
||||
|
||||
try:
|
||||
component = value[namedType.name]
|
||||
|
||||
except KeyError:
|
||||
raise error.PyAsn1Error('Component name "%s" not found in %r' % (namedType.name, value))
|
||||
|
||||
if namedType.isOptional and namedType.name not in value:
|
||||
continue
|
||||
|
||||
if namedType.isDefaulted and component == namedType.asn1Object:
|
||||
continue
|
||||
|
||||
compsMap[id(component)] = namedType
|
||||
comps.append((component, asn1Spec[idx]))
|
||||
|
||||
for comp, compType in sorted(comps, key=self._componentSortKey):
|
||||
namedType = compsMap[id(comp)]
|
||||
|
||||
if namedType:
|
||||
options.update(ifNotEmpty=namedType.isOptional)
|
||||
|
||||
chunk = encodeFun(comp, compType, **options)
|
||||
|
||||
# wrap open type blob if needed
|
||||
if namedType and namedType.openType:
|
||||
wrapType = namedType.asn1Object
|
||||
if wrapType.tagSet and not wrapType.isSameTypeWith(comp):
|
||||
chunk = encodeFun(chunk, wrapType, **options)
|
||||
|
||||
substrate += chunk
|
||||
|
||||
return substrate, True, True
|
||||
|
||||
|
||||
class SequenceEncoder(encoder.SequenceEncoder):
|
||||
omitEmptyOptionals = True
|
||||
|
||||
|
||||
tagMap = encoder.tagMap.copy()
|
||||
tagMap.update({
|
||||
univ.Boolean.tagSet: BooleanEncoder(),
|
||||
univ.Real.tagSet: RealEncoder(),
|
||||
useful.GeneralizedTime.tagSet: GeneralizedTimeEncoder(),
|
||||
useful.UTCTime.tagSet: UTCTimeEncoder(),
|
||||
# Sequence & Set have same tags as SequenceOf & SetOf
|
||||
univ.SetOf.tagSet: SetOfEncoder(),
|
||||
univ.Sequence.typeId: SequenceEncoder()
|
||||
})
|
||||
|
||||
typeMap = encoder.typeMap.copy()
|
||||
typeMap.update({
|
||||
univ.Boolean.typeId: BooleanEncoder(),
|
||||
univ.Real.typeId: RealEncoder(),
|
||||
useful.GeneralizedTime.typeId: GeneralizedTimeEncoder(),
|
||||
useful.UTCTime.typeId: UTCTimeEncoder(),
|
||||
# Sequence & Set have same tags as SequenceOf & SetOf
|
||||
univ.Set.typeId: SetEncoder(),
|
||||
univ.SetOf.typeId: SetOfEncoder(),
|
||||
univ.Sequence.typeId: SequenceEncoder(),
|
||||
univ.SequenceOf.typeId: SequenceOfEncoder()
|
||||
})
|
||||
|
||||
|
||||
class Encoder(encoder.Encoder):
|
||||
fixedDefLengthMode = False
|
||||
fixedChunkSize = 1000
|
||||
|
||||
#: Turns ASN.1 object into CER octet stream.
|
||||
#:
|
||||
#: Takes any ASN.1 object (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
|
||||
#: walks all its components recursively and produces a CER octet stream.
|
||||
#:
|
||||
#: Parameters
|
||||
#: ----------
|
||||
#: value: either a Python or pyasn1 object (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
|
||||
#: A Python or pyasn1 object to encode. If Python object is given, `asnSpec`
|
||||
#: parameter is required to guide the encoding process.
|
||||
#:
|
||||
#: Keyword Args
|
||||
#: ------------
|
||||
#: asn1Spec:
|
||||
#: Optional ASN.1 schema or value object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
|
||||
#:
|
||||
#: Returns
|
||||
#: -------
|
||||
#: : :py:class:`bytes` (Python 3) or :py:class:`str` (Python 2)
|
||||
#: Given ASN.1 object encoded into BER octet-stream
|
||||
#:
|
||||
#: Raises
|
||||
#: ------
|
||||
#: ~pyasn1.error.PyAsn1Error
|
||||
#: On encoding errors
|
||||
#:
|
||||
#: Examples
|
||||
#: --------
|
||||
#: Encode Python value into CER with ASN.1 schema
|
||||
#:
|
||||
#: .. code-block:: pycon
|
||||
#:
|
||||
#: >>> seq = SequenceOf(componentType=Integer())
|
||||
#: >>> encode([1, 2, 3], asn1Spec=seq)
|
||||
#: b'0\x80\x02\x01\x01\x02\x01\x02\x02\x01\x03\x00\x00'
|
||||
#:
|
||||
#: Encode ASN.1 value object into CER
|
||||
#:
|
||||
#: .. code-block:: pycon
|
||||
#:
|
||||
#: >>> seq = SequenceOf(componentType=Integer())
|
||||
#: >>> seq.extend([1, 2, 3])
|
||||
#: >>> encode(seq)
|
||||
#: b'0\x80\x02\x01\x01\x02\x01\x02\x02\x01\x03\x00\x00'
|
||||
#:
|
||||
encode = Encoder(tagMap, typeMap)
|
||||
|
||||
# EncoderFactory queries class instance and builds a map of tags -> encoders
|
1
venv/Lib/site-packages/pyasn1/codec/der/__init__.py
Normal file
1
venv/Lib/site-packages/pyasn1/codec/der/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
# This file is necessary to make this directory a package.
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
94
venv/Lib/site-packages/pyasn1/codec/der/decoder.py
Normal file
94
venv/Lib/site-packages/pyasn1/codec/der/decoder.py
Normal file
|
@ -0,0 +1,94 @@
|
|||
#
|
||||
# This file is part of pyasn1 software.
|
||||
#
|
||||
# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com>
|
||||
# License: http://snmplabs.com/pyasn1/license.html
|
||||
#
|
||||
from pyasn1.codec.cer import decoder
|
||||
from pyasn1.type import univ
|
||||
|
||||
__all__ = ['decode']
|
||||
|
||||
|
||||
class BitStringDecoder(decoder.BitStringDecoder):
|
||||
supportConstructedForm = False
|
||||
|
||||
|
||||
class OctetStringDecoder(decoder.OctetStringDecoder):
|
||||
supportConstructedForm = False
|
||||
|
||||
# TODO: prohibit non-canonical encoding
|
||||
RealDecoder = decoder.RealDecoder
|
||||
|
||||
tagMap = decoder.tagMap.copy()
|
||||
tagMap.update(
|
||||
{univ.BitString.tagSet: BitStringDecoder(),
|
||||
univ.OctetString.tagSet: OctetStringDecoder(),
|
||||
univ.Real.tagSet: RealDecoder()}
|
||||
)
|
||||
|
||||
typeMap = decoder.typeMap.copy()
|
||||
|
||||
# Put in non-ambiguous types for faster codec lookup
|
||||
for typeDecoder in tagMap.values():
|
||||
if typeDecoder.protoComponent is not None:
|
||||
typeId = typeDecoder.protoComponent.__class__.typeId
|
||||
if typeId is not None and typeId not in typeMap:
|
||||
typeMap[typeId] = typeDecoder
|
||||
|
||||
|
||||
class Decoder(decoder.Decoder):
|
||||
supportIndefLength = False
|
||||
|
||||
|
||||
#: Turns DER octet stream into an ASN.1 object.
|
||||
#:
|
||||
#: Takes DER octet-stream and decode it into an ASN.1 object
|
||||
#: (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative) which
|
||||
#: may be a scalar or an arbitrary nested structure.
|
||||
#:
|
||||
#: Parameters
|
||||
#: ----------
|
||||
#: substrate: :py:class:`bytes` (Python 3) or :py:class:`str` (Python 2)
|
||||
#: DER octet-stream
|
||||
#:
|
||||
#: Keyword Args
|
||||
#: ------------
|
||||
#: asn1Spec: any pyasn1 type object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
|
||||
#: A pyasn1 type object to act as a template guiding the decoder. Depending on the ASN.1 structure
|
||||
#: being decoded, *asn1Spec* may or may not be required. Most common reason for
|
||||
#: it to require is that ASN.1 structure is encoded in *IMPLICIT* tagging mode.
|
||||
#:
|
||||
#: Returns
|
||||
#: -------
|
||||
#: : :py:class:`tuple`
|
||||
#: A tuple of pyasn1 object recovered from DER substrate (:py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
|
||||
#: and the unprocessed trailing portion of the *substrate* (may be empty)
|
||||
#:
|
||||
#: Raises
|
||||
#: ------
|
||||
#: ~pyasn1.error.PyAsn1Error, ~pyasn1.error.SubstrateUnderrunError
|
||||
#: On decoding errors
|
||||
#:
|
||||
#: Examples
|
||||
#: --------
|
||||
#: Decode DER serialisation without ASN.1 schema
|
||||
#:
|
||||
#: .. code-block:: pycon
|
||||
#:
|
||||
#: >>> s, _ = decode(b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03')
|
||||
#: >>> str(s)
|
||||
#: SequenceOf:
|
||||
#: 1 2 3
|
||||
#:
|
||||
#: Decode DER serialisation with ASN.1 schema
|
||||
#:
|
||||
#: .. code-block:: pycon
|
||||
#:
|
||||
#: >>> seq = SequenceOf(componentType=Integer())
|
||||
#: >>> s, _ = decode(b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03', asn1Spec=seq)
|
||||
#: >>> str(s)
|
||||
#: SequenceOf:
|
||||
#: 1 2 3
|
||||
#:
|
||||
decode = Decoder(tagMap, typeMap)
|
107
venv/Lib/site-packages/pyasn1/codec/der/encoder.py
Normal file
107
venv/Lib/site-packages/pyasn1/codec/der/encoder.py
Normal file
|
@ -0,0 +1,107 @@
|
|||
#
|
||||
# This file is part of pyasn1 software.
|
||||
#
|
||||
# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com>
|
||||
# License: http://snmplabs.com/pyasn1/license.html
|
||||
#
|
||||
from pyasn1 import error
|
||||
from pyasn1.codec.cer import encoder
|
||||
from pyasn1.type import univ
|
||||
|
||||
__all__ = ['encode']
|
||||
|
||||
|
||||
class SetEncoder(encoder.SetEncoder):
|
||||
@staticmethod
|
||||
def _componentSortKey(componentAndType):
|
||||
"""Sort SET components by tag
|
||||
|
||||
Sort depending on the actual Choice value (dynamic sort)
|
||||
"""
|
||||
component, asn1Spec = componentAndType
|
||||
|
||||
if asn1Spec is None:
|
||||
compType = component
|
||||
else:
|
||||
compType = asn1Spec
|
||||
|
||||
if compType.typeId == univ.Choice.typeId and not compType.tagSet:
|
||||
if asn1Spec is None:
|
||||
return component.getComponent().tagSet
|
||||
else:
|
||||
# TODO: move out of sorting key function
|
||||
names = [namedType.name for namedType in asn1Spec.componentType.namedTypes
|
||||
if namedType.name in component]
|
||||
if len(names) != 1:
|
||||
raise error.PyAsn1Error(
|
||||
'%s components for Choice at %r' % (len(names) and 'Multiple ' or 'None ', component))
|
||||
|
||||
# TODO: support nested CHOICE ordering
|
||||
return asn1Spec[names[0]].tagSet
|
||||
|
||||
else:
|
||||
return compType.tagSet
|
||||
|
||||
tagMap = encoder.tagMap.copy()
|
||||
tagMap.update({
|
||||
# Set & SetOf have same tags
|
||||
univ.Set.tagSet: SetEncoder()
|
||||
})
|
||||
|
||||
typeMap = encoder.typeMap.copy()
|
||||
typeMap.update({
|
||||
# Set & SetOf have same tags
|
||||
univ.Set.typeId: SetEncoder()
|
||||
})
|
||||
|
||||
|
||||
class Encoder(encoder.Encoder):
|
||||
fixedDefLengthMode = True
|
||||
fixedChunkSize = 0
|
||||
|
||||
#: Turns ASN.1 object into DER octet stream.
|
||||
#:
|
||||
#: Takes any ASN.1 object (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
|
||||
#: walks all its components recursively and produces a DER octet stream.
|
||||
#:
|
||||
#: Parameters
|
||||
#: ----------
|
||||
#: value: either a Python or pyasn1 object (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
|
||||
#: A Python or pyasn1 object to encode. If Python object is given, `asnSpec`
|
||||
#: parameter is required to guide the encoding process.
|
||||
#:
|
||||
#: Keyword Args
|
||||
#: ------------
|
||||
#: asn1Spec:
|
||||
#: Optional ASN.1 schema or value object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
|
||||
#:
|
||||
#: Returns
|
||||
#: -------
|
||||
#: : :py:class:`bytes` (Python 3) or :py:class:`str` (Python 2)
|
||||
#: Given ASN.1 object encoded into BER octet-stream
|
||||
#:
|
||||
#: Raises
|
||||
#: ------
|
||||
#: ~pyasn1.error.PyAsn1Error
|
||||
#: On encoding errors
|
||||
#:
|
||||
#: Examples
|
||||
#: --------
|
||||
#: Encode Python value into DER with ASN.1 schema
|
||||
#:
|
||||
#: .. code-block:: pycon
|
||||
#:
|
||||
#: >>> seq = SequenceOf(componentType=Integer())
|
||||
#: >>> encode([1, 2, 3], asn1Spec=seq)
|
||||
#: b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03'
|
||||
#:
|
||||
#: Encode ASN.1 value object into DER
|
||||
#:
|
||||
#: .. code-block:: pycon
|
||||
#:
|
||||
#: >>> seq = SequenceOf(componentType=Integer())
|
||||
#: >>> seq.extend([1, 2, 3])
|
||||
#: >>> encode(seq)
|
||||
#: b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03'
|
||||
#:
|
||||
encode = Encoder(tagMap, typeMap)
|
1
venv/Lib/site-packages/pyasn1/codec/native/__init__.py
Normal file
1
venv/Lib/site-packages/pyasn1/codec/native/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
# This file is necessary to make this directory a package.
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
213
venv/Lib/site-packages/pyasn1/codec/native/decoder.py
Normal file
213
venv/Lib/site-packages/pyasn1/codec/native/decoder.py
Normal file
|
@ -0,0 +1,213 @@
|
|||
#
|
||||
# This file is part of pyasn1 software.
|
||||
#
|
||||
# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com>
|
||||
# License: http://snmplabs.com/pyasn1/license.html
|
||||
#
|
||||
from pyasn1 import debug
|
||||
from pyasn1 import error
|
||||
from pyasn1.type import base
|
||||
from pyasn1.type import char
|
||||
from pyasn1.type import tag
|
||||
from pyasn1.type import univ
|
||||
from pyasn1.type import useful
|
||||
|
||||
__all__ = ['decode']
|
||||
|
||||
LOG = debug.registerLoggee(__name__, flags=debug.DEBUG_DECODER)
|
||||
|
||||
|
||||
class AbstractScalarDecoder(object):
|
||||
def __call__(self, pyObject, asn1Spec, decodeFun=None, **options):
|
||||
return asn1Spec.clone(pyObject)
|
||||
|
||||
|
||||
class BitStringDecoder(AbstractScalarDecoder):
|
||||
def __call__(self, pyObject, asn1Spec, decodeFun=None, **options):
|
||||
return asn1Spec.clone(univ.BitString.fromBinaryString(pyObject))
|
||||
|
||||
|
||||
class SequenceOrSetDecoder(object):
|
||||
def __call__(self, pyObject, asn1Spec, decodeFun=None, **options):
|
||||
asn1Value = asn1Spec.clone()
|
||||
|
||||
componentsTypes = asn1Spec.componentType
|
||||
|
||||
for field in asn1Value:
|
||||
if field in pyObject:
|
||||
asn1Value[field] = decodeFun(pyObject[field], componentsTypes[field].asn1Object, **options)
|
||||
|
||||
return asn1Value
|
||||
|
||||
|
||||
class SequenceOfOrSetOfDecoder(object):
|
||||
def __call__(self, pyObject, asn1Spec, decodeFun=None, **options):
|
||||
asn1Value = asn1Spec.clone()
|
||||
|
||||
for pyValue in pyObject:
|
||||
asn1Value.append(decodeFun(pyValue, asn1Spec.componentType), **options)
|
||||
|
||||
return asn1Value
|
||||
|
||||
|
||||
class ChoiceDecoder(object):
|
||||
def __call__(self, pyObject, asn1Spec, decodeFun=None, **options):
|
||||
asn1Value = asn1Spec.clone()
|
||||
|
||||
componentsTypes = asn1Spec.componentType
|
||||
|
||||
for field in pyObject:
|
||||
if field in componentsTypes:
|
||||
asn1Value[field] = decodeFun(pyObject[field], componentsTypes[field].asn1Object, **options)
|
||||
break
|
||||
|
||||
return asn1Value
|
||||
|
||||
|
||||
tagMap = {
|
||||
univ.Integer.tagSet: AbstractScalarDecoder(),
|
||||
univ.Boolean.tagSet: AbstractScalarDecoder(),
|
||||
univ.BitString.tagSet: BitStringDecoder(),
|
||||
univ.OctetString.tagSet: AbstractScalarDecoder(),
|
||||
univ.Null.tagSet: AbstractScalarDecoder(),
|
||||
univ.ObjectIdentifier.tagSet: AbstractScalarDecoder(),
|
||||
univ.Enumerated.tagSet: AbstractScalarDecoder(),
|
||||
univ.Real.tagSet: AbstractScalarDecoder(),
|
||||
univ.Sequence.tagSet: SequenceOrSetDecoder(), # conflicts with SequenceOf
|
||||
univ.Set.tagSet: SequenceOrSetDecoder(), # conflicts with SetOf
|
||||
univ.Choice.tagSet: ChoiceDecoder(), # conflicts with Any
|
||||
# character string types
|
||||
char.UTF8String.tagSet: AbstractScalarDecoder(),
|
||||
char.NumericString.tagSet: AbstractScalarDecoder(),
|
||||
char.PrintableString.tagSet: AbstractScalarDecoder(),
|
||||
char.TeletexString.tagSet: AbstractScalarDecoder(),
|
||||
char.VideotexString.tagSet: AbstractScalarDecoder(),
|
||||
char.IA5String.tagSet: AbstractScalarDecoder(),
|
||||
char.GraphicString.tagSet: AbstractScalarDecoder(),
|
||||
char.VisibleString.tagSet: AbstractScalarDecoder(),
|
||||
char.GeneralString.tagSet: AbstractScalarDecoder(),
|
||||
char.UniversalString.tagSet: AbstractScalarDecoder(),
|
||||
char.BMPString.tagSet: AbstractScalarDecoder(),
|
||||
# useful types
|
||||
useful.ObjectDescriptor.tagSet: AbstractScalarDecoder(),
|
||||
useful.GeneralizedTime.tagSet: AbstractScalarDecoder(),
|
||||
useful.UTCTime.tagSet: AbstractScalarDecoder()
|
||||
}
|
||||
|
||||
# Put in ambiguous & non-ambiguous types for faster codec lookup
|
||||
typeMap = {
|
||||
univ.Integer.typeId: AbstractScalarDecoder(),
|
||||
univ.Boolean.typeId: AbstractScalarDecoder(),
|
||||
univ.BitString.typeId: BitStringDecoder(),
|
||||
univ.OctetString.typeId: AbstractScalarDecoder(),
|
||||
univ.Null.typeId: AbstractScalarDecoder(),
|
||||
univ.ObjectIdentifier.typeId: AbstractScalarDecoder(),
|
||||
univ.Enumerated.typeId: AbstractScalarDecoder(),
|
||||
univ.Real.typeId: AbstractScalarDecoder(),
|
||||
# ambiguous base types
|
||||
univ.Set.typeId: SequenceOrSetDecoder(),
|
||||
univ.SetOf.typeId: SequenceOfOrSetOfDecoder(),
|
||||
univ.Sequence.typeId: SequenceOrSetDecoder(),
|
||||
univ.SequenceOf.typeId: SequenceOfOrSetOfDecoder(),
|
||||
univ.Choice.typeId: ChoiceDecoder(),
|
||||
univ.Any.typeId: AbstractScalarDecoder(),
|
||||
# character string types
|
||||
char.UTF8String.typeId: AbstractScalarDecoder(),
|
||||
char.NumericString.typeId: AbstractScalarDecoder(),
|
||||
char.PrintableString.typeId: AbstractScalarDecoder(),
|
||||
char.TeletexString.typeId: AbstractScalarDecoder(),
|
||||
char.VideotexString.typeId: AbstractScalarDecoder(),
|
||||
char.IA5String.typeId: AbstractScalarDecoder(),
|
||||
char.GraphicString.typeId: AbstractScalarDecoder(),
|
||||
char.VisibleString.typeId: AbstractScalarDecoder(),
|
||||
char.GeneralString.typeId: AbstractScalarDecoder(),
|
||||
char.UniversalString.typeId: AbstractScalarDecoder(),
|
||||
char.BMPString.typeId: AbstractScalarDecoder(),
|
||||
# useful types
|
||||
useful.ObjectDescriptor.typeId: AbstractScalarDecoder(),
|
||||
useful.GeneralizedTime.typeId: AbstractScalarDecoder(),
|
||||
useful.UTCTime.typeId: AbstractScalarDecoder()
|
||||
}
|
||||
|
||||
|
||||
class Decoder(object):
|
||||
|
||||
# noinspection PyDefaultArgument
|
||||
def __init__(self, tagMap, typeMap):
|
||||
self.__tagMap = tagMap
|
||||
self.__typeMap = typeMap
|
||||
|
||||
def __call__(self, pyObject, asn1Spec, **options):
|
||||
|
||||
if LOG:
|
||||
debug.scope.push(type(pyObject).__name__)
|
||||
LOG('decoder called at scope %s, working with type %s' % (debug.scope, type(pyObject).__name__))
|
||||
|
||||
if asn1Spec is None or not isinstance(asn1Spec, base.Asn1Item):
|
||||
raise error.PyAsn1Error('asn1Spec is not valid (should be an instance of an ASN.1 Item, not %s)' % asn1Spec.__class__.__name__)
|
||||
|
||||
try:
|
||||
valueDecoder = self.__typeMap[asn1Spec.typeId]
|
||||
|
||||
except KeyError:
|
||||
# use base type for codec lookup to recover untagged types
|
||||
baseTagSet = tag.TagSet(asn1Spec.tagSet.baseTag, asn1Spec.tagSet.baseTag)
|
||||
|
||||
try:
|
||||
valueDecoder = self.__tagMap[baseTagSet]
|
||||
except KeyError:
|
||||
raise error.PyAsn1Error('Unknown ASN.1 tag %s' % asn1Spec.tagSet)
|
||||
|
||||
if LOG:
|
||||
LOG('calling decoder %s on Python type %s <%s>' % (type(valueDecoder).__name__, type(pyObject).__name__, repr(pyObject)))
|
||||
|
||||
value = valueDecoder(pyObject, asn1Spec, self, **options)
|
||||
|
||||
if LOG:
|
||||
LOG('decoder %s produced ASN.1 type %s <%s>' % (type(valueDecoder).__name__, type(value).__name__, repr(value)))
|
||||
debug.scope.pop()
|
||||
|
||||
return value
|
||||
|
||||
|
||||
#: Turns Python objects of built-in types into ASN.1 objects.
|
||||
#:
|
||||
#: Takes Python objects of built-in types and turns them into a tree of
|
||||
#: ASN.1 objects (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative) which
|
||||
#: may be a scalar or an arbitrary nested structure.
|
||||
#:
|
||||
#: Parameters
|
||||
#: ----------
|
||||
#: pyObject: :py:class:`object`
|
||||
#: A scalar or nested Python objects
|
||||
#:
|
||||
#: Keyword Args
|
||||
#: ------------
|
||||
#: asn1Spec: any pyasn1 type object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
|
||||
#: A pyasn1 type object to act as a template guiding the decoder. It is required
|
||||
#: for successful interpretation of Python objects mapping into their ASN.1
|
||||
#: representations.
|
||||
#:
|
||||
#: Returns
|
||||
#: -------
|
||||
#: : :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
|
||||
#: A scalar or constructed pyasn1 object
|
||||
#:
|
||||
#: Raises
|
||||
#: ------
|
||||
#: ~pyasn1.error.PyAsn1Error
|
||||
#: On decoding errors
|
||||
#:
|
||||
#: Examples
|
||||
#: --------
|
||||
#: Decode native Python object into ASN.1 objects with ASN.1 schema
|
||||
#:
|
||||
#: .. code-block:: pycon
|
||||
#:
|
||||
#: >>> seq = SequenceOf(componentType=Integer())
|
||||
#: >>> s, _ = decode([1, 2, 3], asn1Spec=seq)
|
||||
#: >>> str(s)
|
||||
#: SequenceOf:
|
||||
#: 1 2 3
|
||||
#:
|
||||
decode = Decoder(tagMap, typeMap)
|
256
venv/Lib/site-packages/pyasn1/codec/native/encoder.py
Normal file
256
venv/Lib/site-packages/pyasn1/codec/native/encoder.py
Normal file
|
@ -0,0 +1,256 @@
|
|||
#
|
||||
# This file is part of pyasn1 software.
|
||||
#
|
||||
# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com>
|
||||
# License: http://snmplabs.com/pyasn1/license.html
|
||||
#
|
||||
try:
|
||||
from collections import OrderedDict
|
||||
|
||||
except ImportError:
|
||||
OrderedDict = dict
|
||||
|
||||
from pyasn1 import debug
|
||||
from pyasn1 import error
|
||||
from pyasn1.type import base
|
||||
from pyasn1.type import char
|
||||
from pyasn1.type import tag
|
||||
from pyasn1.type import univ
|
||||
from pyasn1.type import useful
|
||||
|
||||
__all__ = ['encode']
|
||||
|
||||
LOG = debug.registerLoggee(__name__, flags=debug.DEBUG_ENCODER)
|
||||
|
||||
|
||||
class AbstractItemEncoder(object):
|
||||
def encode(self, value, encodeFun, **options):
|
||||
raise error.PyAsn1Error('Not implemented')
|
||||
|
||||
|
||||
class BooleanEncoder(AbstractItemEncoder):
|
||||
def encode(self, value, encodeFun, **options):
|
||||
return bool(value)
|
||||
|
||||
|
||||
class IntegerEncoder(AbstractItemEncoder):
|
||||
def encode(self, value, encodeFun, **options):
|
||||
return int(value)
|
||||
|
||||
|
||||
class BitStringEncoder(AbstractItemEncoder):
|
||||
def encode(self, value, encodeFun, **options):
|
||||
return str(value)
|
||||
|
||||
|
||||
class OctetStringEncoder(AbstractItemEncoder):
|
||||
def encode(self, value, encodeFun, **options):
|
||||
return value.asOctets()
|
||||
|
||||
|
||||
class TextStringEncoder(AbstractItemEncoder):
|
||||
def encode(self, value, encodeFun, **options):
|
||||
return str(value)
|
||||
|
||||
|
||||
class NullEncoder(AbstractItemEncoder):
|
||||
def encode(self, value, encodeFun, **options):
|
||||
return None
|
||||
|
||||
|
||||
class ObjectIdentifierEncoder(AbstractItemEncoder):
|
||||
def encode(self, value, encodeFun, **options):
|
||||
return str(value)
|
||||
|
||||
|
||||
class RealEncoder(AbstractItemEncoder):
|
||||
def encode(self, value, encodeFun, **options):
|
||||
return float(value)
|
||||
|
||||
|
||||
class SetEncoder(AbstractItemEncoder):
|
||||
protoDict = dict
|
||||
|
||||
def encode(self, value, encodeFun, **options):
|
||||
inconsistency = value.isInconsistent
|
||||
if inconsistency:
|
||||
raise inconsistency
|
||||
|
||||
namedTypes = value.componentType
|
||||
substrate = self.protoDict()
|
||||
|
||||
for idx, (key, subValue) in enumerate(value.items()):
|
||||
if namedTypes and namedTypes[idx].isOptional and not value[idx].isValue:
|
||||
continue
|
||||
substrate[key] = encodeFun(subValue, **options)
|
||||
return substrate
|
||||
|
||||
|
||||
class SequenceEncoder(SetEncoder):
|
||||
protoDict = OrderedDict
|
||||
|
||||
|
||||
class SequenceOfEncoder(AbstractItemEncoder):
|
||||
def encode(self, value, encodeFun, **options):
|
||||
inconsistency = value.isInconsistent
|
||||
if inconsistency:
|
||||
raise inconsistency
|
||||
return [encodeFun(x, **options) for x in value]
|
||||
|
||||
|
||||
class ChoiceEncoder(SequenceEncoder):
|
||||
pass
|
||||
|
||||
|
||||
class AnyEncoder(AbstractItemEncoder):
|
||||
def encode(self, value, encodeFun, **options):
|
||||
return value.asOctets()
|
||||
|
||||
|
||||
tagMap = {
|
||||
univ.Boolean.tagSet: BooleanEncoder(),
|
||||
univ.Integer.tagSet: IntegerEncoder(),
|
||||
univ.BitString.tagSet: BitStringEncoder(),
|
||||
univ.OctetString.tagSet: OctetStringEncoder(),
|
||||
univ.Null.tagSet: NullEncoder(),
|
||||
univ.ObjectIdentifier.tagSet: ObjectIdentifierEncoder(),
|
||||
univ.Enumerated.tagSet: IntegerEncoder(),
|
||||
univ.Real.tagSet: RealEncoder(),
|
||||
# Sequence & Set have same tags as SequenceOf & SetOf
|
||||
univ.SequenceOf.tagSet: SequenceOfEncoder(),
|
||||
univ.SetOf.tagSet: SequenceOfEncoder(),
|
||||
univ.Choice.tagSet: ChoiceEncoder(),
|
||||
# character string types
|
||||
char.UTF8String.tagSet: TextStringEncoder(),
|
||||
char.NumericString.tagSet: TextStringEncoder(),
|
||||
char.PrintableString.tagSet: TextStringEncoder(),
|
||||
char.TeletexString.tagSet: TextStringEncoder(),
|
||||
char.VideotexString.tagSet: TextStringEncoder(),
|
||||
char.IA5String.tagSet: TextStringEncoder(),
|
||||
char.GraphicString.tagSet: TextStringEncoder(),
|
||||
char.VisibleString.tagSet: TextStringEncoder(),
|
||||
char.GeneralString.tagSet: TextStringEncoder(),
|
||||
char.UniversalString.tagSet: TextStringEncoder(),
|
||||
char.BMPString.tagSet: TextStringEncoder(),
|
||||
# useful types
|
||||
useful.ObjectDescriptor.tagSet: OctetStringEncoder(),
|
||||
useful.GeneralizedTime.tagSet: OctetStringEncoder(),
|
||||
useful.UTCTime.tagSet: OctetStringEncoder()
|
||||
}
|
||||
|
||||
|
||||
# Put in ambiguous & non-ambiguous types for faster codec lookup
|
||||
typeMap = {
|
||||
univ.Boolean.typeId: BooleanEncoder(),
|
||||
univ.Integer.typeId: IntegerEncoder(),
|
||||
univ.BitString.typeId: BitStringEncoder(),
|
||||
univ.OctetString.typeId: OctetStringEncoder(),
|
||||
univ.Null.typeId: NullEncoder(),
|
||||
univ.ObjectIdentifier.typeId: ObjectIdentifierEncoder(),
|
||||
univ.Enumerated.typeId: IntegerEncoder(),
|
||||
univ.Real.typeId: RealEncoder(),
|
||||
# Sequence & Set have same tags as SequenceOf & SetOf
|
||||
univ.Set.typeId: SetEncoder(),
|
||||
univ.SetOf.typeId: SequenceOfEncoder(),
|
||||
univ.Sequence.typeId: SequenceEncoder(),
|
||||
univ.SequenceOf.typeId: SequenceOfEncoder(),
|
||||
univ.Choice.typeId: ChoiceEncoder(),
|
||||
univ.Any.typeId: AnyEncoder(),
|
||||
# character string types
|
||||
char.UTF8String.typeId: OctetStringEncoder(),
|
||||
char.NumericString.typeId: OctetStringEncoder(),
|
||||
char.PrintableString.typeId: OctetStringEncoder(),
|
||||
char.TeletexString.typeId: OctetStringEncoder(),
|
||||
char.VideotexString.typeId: OctetStringEncoder(),
|
||||
char.IA5String.typeId: OctetStringEncoder(),
|
||||
char.GraphicString.typeId: OctetStringEncoder(),
|
||||
char.VisibleString.typeId: OctetStringEncoder(),
|
||||
char.GeneralString.typeId: OctetStringEncoder(),
|
||||
char.UniversalString.typeId: OctetStringEncoder(),
|
||||
char.BMPString.typeId: OctetStringEncoder(),
|
||||
# useful types
|
||||
useful.ObjectDescriptor.typeId: OctetStringEncoder(),
|
||||
useful.GeneralizedTime.typeId: OctetStringEncoder(),
|
||||
useful.UTCTime.typeId: OctetStringEncoder()
|
||||
}
|
||||
|
||||
|
||||
class Encoder(object):
|
||||
|
||||
# noinspection PyDefaultArgument
|
||||
def __init__(self, tagMap, typeMap={}):
|
||||
self.__tagMap = tagMap
|
||||
self.__typeMap = typeMap
|
||||
|
||||
def __call__(self, value, **options):
|
||||
if not isinstance(value, base.Asn1Item):
|
||||
raise error.PyAsn1Error('value is not valid (should be an instance of an ASN.1 Item)')
|
||||
|
||||
if LOG:
|
||||
debug.scope.push(type(value).__name__)
|
||||
LOG('encoder called for type %s <%s>' % (type(value).__name__, value.prettyPrint()))
|
||||
|
||||
tagSet = value.tagSet
|
||||
|
||||
try:
|
||||
concreteEncoder = self.__typeMap[value.typeId]
|
||||
|
||||
except KeyError:
|
||||
# use base type for codec lookup to recover untagged types
|
||||
baseTagSet = tag.TagSet(value.tagSet.baseTag, value.tagSet.baseTag)
|
||||
|
||||
try:
|
||||
concreteEncoder = self.__tagMap[baseTagSet]
|
||||
|
||||
except KeyError:
|
||||
raise error.PyAsn1Error('No encoder for %s' % (value,))
|
||||
|
||||
if LOG:
|
||||
LOG('using value codec %s chosen by %s' % (concreteEncoder.__class__.__name__, tagSet))
|
||||
|
||||
pyObject = concreteEncoder.encode(value, self, **options)
|
||||
|
||||
if LOG:
|
||||
LOG('encoder %s produced: %s' % (type(concreteEncoder).__name__, repr(pyObject)))
|
||||
debug.scope.pop()
|
||||
|
||||
return pyObject
|
||||
|
||||
|
||||
#: Turns ASN.1 object into a Python built-in type object(s).
|
||||
#:
|
||||
#: Takes any ASN.1 object (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
|
||||
#: walks all its components recursively and produces a Python built-in type or a tree
|
||||
#: of those.
|
||||
#:
|
||||
#: One exception is that instead of :py:class:`dict`, the :py:class:`OrderedDict`
|
||||
#: can be produced (whenever available) to preserve ordering of the components
|
||||
#: in ASN.1 SEQUENCE.
|
||||
#:
|
||||
#: Parameters
|
||||
#: ----------
|
||||
# asn1Value: any pyasn1 object (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
|
||||
#: pyasn1 object to encode (or a tree of them)
|
||||
#:
|
||||
#: Returns
|
||||
#: -------
|
||||
#: : :py:class:`object`
|
||||
#: Python built-in type instance (or a tree of them)
|
||||
#:
|
||||
#: Raises
|
||||
#: ------
|
||||
#: ~pyasn1.error.PyAsn1Error
|
||||
#: On encoding errors
|
||||
#:
|
||||
#: Examples
|
||||
#: --------
|
||||
#: Encode ASN.1 value object into native Python types
|
||||
#:
|
||||
#: .. code-block:: pycon
|
||||
#:
|
||||
#: >>> seq = SequenceOf(componentType=Integer())
|
||||
#: >>> seq.extend([1, 2, 3])
|
||||
#: >>> encode(seq)
|
||||
#: [1, 2, 3]
|
||||
#:
|
||||
encode = Encoder(tagMap, typeMap)
|
Loading…
Add table
Add a link
Reference in a new issue