decoder.py 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  1. #
  2. # This file is part of pyasn1 software.
  3. #
  4. # Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com>
  5. # License: http://snmplabs.com/pyasn1/license.html
  6. #
  7. from pyasn1 import debug
  8. from pyasn1 import error
  9. from pyasn1.codec.ber import eoo
  10. from pyasn1.compat.integer import from_bytes
  11. from pyasn1.compat.octets import oct2int, octs2ints, ints2octs, null
  12. from pyasn1.type import base
  13. from pyasn1.type import char
  14. from pyasn1.type import tag
  15. from pyasn1.type import tagmap
  16. from pyasn1.type import univ
  17. from pyasn1.type import useful
  18. __all__ = ['decode']
  19. LOG = debug.registerLoggee(__name__, flags=debug.DEBUG_DECODER)
  20. noValue = base.noValue
  21. class AbstractDecoder(object):
  22. protoComponent = None
  23. def valueDecoder(self, substrate, asn1Spec,
  24. tagSet=None, length=None, state=None,
  25. decodeFun=None, substrateFun=None,
  26. **options):
  27. raise error.PyAsn1Error('Decoder not implemented for %s' % (tagSet,))
  28. def indefLenValueDecoder(self, substrate, asn1Spec,
  29. tagSet=None, length=None, state=None,
  30. decodeFun=None, substrateFun=None,
  31. **options):
  32. raise error.PyAsn1Error('Indefinite length mode decoder not implemented for %s' % (tagSet,))
  33. class AbstractSimpleDecoder(AbstractDecoder):
  34. @staticmethod
  35. def substrateCollector(asn1Object, substrate, length):
  36. return substrate[:length], substrate[length:]
  37. def _createComponent(self, asn1Spec, tagSet, value, **options):
  38. if options.get('native'):
  39. return value
  40. elif asn1Spec is None:
  41. return self.protoComponent.clone(value, tagSet=tagSet)
  42. elif value is noValue:
  43. return asn1Spec
  44. else:
  45. return asn1Spec.clone(value)
  46. class ExplicitTagDecoder(AbstractSimpleDecoder):
  47. protoComponent = univ.Any('')
  48. def valueDecoder(self, substrate, asn1Spec,
  49. tagSet=None, length=None, state=None,
  50. decodeFun=None, substrateFun=None,
  51. **options):
  52. if substrateFun:
  53. return substrateFun(
  54. self._createComponent(asn1Spec, tagSet, '', **options),
  55. substrate, length
  56. )
  57. head, tail = substrate[:length], substrate[length:]
  58. value, _ = decodeFun(head, asn1Spec, tagSet, length, **options)
  59. if LOG:
  60. LOG('explicit tag container carries %d octets of trailing payload '
  61. '(will be lost!): %s' % (len(_), debug.hexdump(_)))
  62. return value, tail
  63. def indefLenValueDecoder(self, substrate, asn1Spec,
  64. tagSet=None, length=None, state=None,
  65. decodeFun=None, substrateFun=None,
  66. **options):
  67. if substrateFun:
  68. return substrateFun(
  69. self._createComponent(asn1Spec, tagSet, '', **options),
  70. substrate, length
  71. )
  72. value, substrate = decodeFun(substrate, asn1Spec, tagSet, length, **options)
  73. eooMarker, substrate = decodeFun(substrate, allowEoo=True, **options)
  74. if eooMarker is eoo.endOfOctets:
  75. return value, substrate
  76. else:
  77. raise error.PyAsn1Error('Missing end-of-octets terminator')
  78. explicitTagDecoder = ExplicitTagDecoder()
  79. class IntegerDecoder(AbstractSimpleDecoder):
  80. protoComponent = univ.Integer(0)
  81. def valueDecoder(self, substrate, asn1Spec,
  82. tagSet=None, length=None, state=None,
  83. decodeFun=None, substrateFun=None,
  84. **options):
  85. if tagSet[0].tagFormat != tag.tagFormatSimple:
  86. raise error.PyAsn1Error('Simple tag format expected')
  87. head, tail = substrate[:length], substrate[length:]
  88. if not head:
  89. return self._createComponent(asn1Spec, tagSet, 0, **options), tail
  90. value = from_bytes(head, signed=True)
  91. return self._createComponent(asn1Spec, tagSet, value, **options), tail
  92. class BooleanDecoder(IntegerDecoder):
  93. protoComponent = univ.Boolean(0)
  94. def _createComponent(self, asn1Spec, tagSet, value, **options):
  95. return IntegerDecoder._createComponent(
  96. self, asn1Spec, tagSet, value and 1 or 0, **options)
  97. class BitStringDecoder(AbstractSimpleDecoder):
  98. protoComponent = univ.BitString(())
  99. supportConstructedForm = True
  100. def valueDecoder(self, substrate, asn1Spec,
  101. tagSet=None, length=None, state=None,
  102. decodeFun=None, substrateFun=None,
  103. **options):
  104. head, tail = substrate[:length], substrate[length:]
  105. if substrateFun:
  106. return substrateFun(self._createComponent(
  107. asn1Spec, tagSet, noValue, **options), substrate, length)
  108. if not head:
  109. raise error.PyAsn1Error('Empty BIT STRING substrate')
  110. if tagSet[0].tagFormat == tag.tagFormatSimple: # XXX what tag to check?
  111. trailingBits = oct2int(head[0])
  112. if trailingBits > 7:
  113. raise error.PyAsn1Error(
  114. 'Trailing bits overflow %s' % trailingBits
  115. )
  116. value = self.protoComponent.fromOctetString(
  117. head[1:], internalFormat=True, padding=trailingBits)
  118. return self._createComponent(asn1Spec, tagSet, value, **options), tail
  119. if not self.supportConstructedForm:
  120. raise error.PyAsn1Error('Constructed encoding form prohibited '
  121. 'at %s' % self.__class__.__name__)
  122. if LOG:
  123. LOG('assembling constructed serialization')
  124. # All inner fragments are of the same type, treat them as octet string
  125. substrateFun = self.substrateCollector
  126. bitString = self.protoComponent.fromOctetString(null, internalFormat=True)
  127. while head:
  128. component, head = decodeFun(head, self.protoComponent,
  129. substrateFun=substrateFun, **options)
  130. trailingBits = oct2int(component[0])
  131. if trailingBits > 7:
  132. raise error.PyAsn1Error(
  133. 'Trailing bits overflow %s' % trailingBits
  134. )
  135. bitString = self.protoComponent.fromOctetString(
  136. component[1:], internalFormat=True,
  137. prepend=bitString, padding=trailingBits
  138. )
  139. return self._createComponent(asn1Spec, tagSet, bitString, **options), tail
  140. def indefLenValueDecoder(self, substrate, asn1Spec,
  141. tagSet=None, length=None, state=None,
  142. decodeFun=None, substrateFun=None,
  143. **options):
  144. if substrateFun:
  145. return substrateFun(self._createComponent(asn1Spec, tagSet, noValue, **options), substrate, length)
  146. # All inner fragments are of the same type, treat them as octet string
  147. substrateFun = self.substrateCollector
  148. bitString = self.protoComponent.fromOctetString(null, internalFormat=True)
  149. while substrate:
  150. component, substrate = decodeFun(substrate, self.protoComponent,
  151. substrateFun=substrateFun,
  152. allowEoo=True, **options)
  153. if component is eoo.endOfOctets:
  154. break
  155. trailingBits = oct2int(component[0])
  156. if trailingBits > 7:
  157. raise error.PyAsn1Error(
  158. 'Trailing bits overflow %s' % trailingBits
  159. )
  160. bitString = self.protoComponent.fromOctetString(
  161. component[1:], internalFormat=True,
  162. prepend=bitString, padding=trailingBits
  163. )
  164. else:
  165. raise error.SubstrateUnderrunError('No EOO seen before substrate ends')
  166. return self._createComponent(asn1Spec, tagSet, bitString, **options), substrate
  167. class OctetStringDecoder(AbstractSimpleDecoder):
  168. protoComponent = univ.OctetString('')
  169. supportConstructedForm = True
  170. def valueDecoder(self, substrate, asn1Spec,
  171. tagSet=None, length=None, state=None,
  172. decodeFun=None, substrateFun=None,
  173. **options):
  174. head, tail = substrate[:length], substrate[length:]
  175. if substrateFun:
  176. return substrateFun(self._createComponent(asn1Spec, tagSet, noValue, **options),
  177. substrate, length)
  178. if tagSet[0].tagFormat == tag.tagFormatSimple: # XXX what tag to check?
  179. return self._createComponent(asn1Spec, tagSet, head, **options), tail
  180. if not self.supportConstructedForm:
  181. raise error.PyAsn1Error('Constructed encoding form prohibited at %s' % self.__class__.__name__)
  182. if LOG:
  183. LOG('assembling constructed serialization')
  184. # All inner fragments are of the same type, treat them as octet string
  185. substrateFun = self.substrateCollector
  186. header = null
  187. while head:
  188. component, head = decodeFun(head, self.protoComponent,
  189. substrateFun=substrateFun,
  190. **options)
  191. header += component
  192. return self._createComponent(asn1Spec, tagSet, header, **options), tail
  193. def indefLenValueDecoder(self, substrate, asn1Spec,
  194. tagSet=None, length=None, state=None,
  195. decodeFun=None, substrateFun=None,
  196. **options):
  197. if substrateFun and substrateFun is not self.substrateCollector:
  198. asn1Object = self._createComponent(asn1Spec, tagSet, noValue, **options)
  199. return substrateFun(asn1Object, substrate, length)
  200. # All inner fragments are of the same type, treat them as octet string
  201. substrateFun = self.substrateCollector
  202. header = null
  203. while substrate:
  204. component, substrate = decodeFun(substrate,
  205. self.protoComponent,
  206. substrateFun=substrateFun,
  207. allowEoo=True, **options)
  208. if component is eoo.endOfOctets:
  209. break
  210. header += component
  211. else:
  212. raise error.SubstrateUnderrunError(
  213. 'No EOO seen before substrate ends'
  214. )
  215. return self._createComponent(asn1Spec, tagSet, header, **options), substrate
  216. class NullDecoder(AbstractSimpleDecoder):
  217. protoComponent = univ.Null('')
  218. def valueDecoder(self, substrate, asn1Spec,
  219. tagSet=None, length=None, state=None,
  220. decodeFun=None, substrateFun=None,
  221. **options):
  222. if tagSet[0].tagFormat != tag.tagFormatSimple:
  223. raise error.PyAsn1Error('Simple tag format expected')
  224. head, tail = substrate[:length], substrate[length:]
  225. component = self._createComponent(asn1Spec, tagSet, '', **options)
  226. if head:
  227. raise error.PyAsn1Error('Unexpected %d-octet substrate for Null' % length)
  228. return component, tail
  229. class ObjectIdentifierDecoder(AbstractSimpleDecoder):
  230. protoComponent = univ.ObjectIdentifier(())
  231. def valueDecoder(self, substrate, asn1Spec,
  232. tagSet=None, length=None, state=None,
  233. decodeFun=None, substrateFun=None,
  234. **options):
  235. if tagSet[0].tagFormat != tag.tagFormatSimple:
  236. raise error.PyAsn1Error('Simple tag format expected')
  237. head, tail = substrate[:length], substrate[length:]
  238. if not head:
  239. raise error.PyAsn1Error('Empty substrate')
  240. head = octs2ints(head)
  241. oid = ()
  242. index = 0
  243. substrateLen = len(head)
  244. while index < substrateLen:
  245. subId = head[index]
  246. index += 1
  247. if subId < 128:
  248. oid += (subId,)
  249. elif subId > 128:
  250. # Construct subid from a number of octets
  251. nextSubId = subId
  252. subId = 0
  253. while nextSubId >= 128:
  254. subId = (subId << 7) + (nextSubId & 0x7F)
  255. if index >= substrateLen:
  256. raise error.SubstrateUnderrunError(
  257. 'Short substrate for sub-OID past %s' % (oid,)
  258. )
  259. nextSubId = head[index]
  260. index += 1
  261. oid += ((subId << 7) + nextSubId,)
  262. elif subId == 128:
  263. # ASN.1 spec forbids leading zeros (0x80) in OID
  264. # encoding, tolerating it opens a vulnerability. See
  265. # https://www.esat.kuleuven.be/cosic/publications/article-1432.pdf
  266. # page 7
  267. raise error.PyAsn1Error('Invalid octet 0x80 in OID encoding')
  268. # Decode two leading arcs
  269. if 0 <= oid[0] <= 39:
  270. oid = (0,) + oid
  271. elif 40 <= oid[0] <= 79:
  272. oid = (1, oid[0] - 40) + oid[1:]
  273. elif oid[0] >= 80:
  274. oid = (2, oid[0] - 80) + oid[1:]
  275. else:
  276. raise error.PyAsn1Error('Malformed first OID octet: %s' % head[0])
  277. return self._createComponent(asn1Spec, tagSet, oid, **options), tail
  278. class RealDecoder(AbstractSimpleDecoder):
  279. protoComponent = univ.Real()
  280. def valueDecoder(self, substrate, asn1Spec,
  281. tagSet=None, length=None, state=None,
  282. decodeFun=None, substrateFun=None,
  283. **options):
  284. if tagSet[0].tagFormat != tag.tagFormatSimple:
  285. raise error.PyAsn1Error('Simple tag format expected')
  286. head, tail = substrate[:length], substrate[length:]
  287. if not head:
  288. return self._createComponent(asn1Spec, tagSet, 0.0, **options), tail
  289. fo = oct2int(head[0])
  290. head = head[1:]
  291. if fo & 0x80: # binary encoding
  292. if not head:
  293. raise error.PyAsn1Error("Incomplete floating-point value")
  294. if LOG:
  295. LOG('decoding binary encoded REAL')
  296. n = (fo & 0x03) + 1
  297. if n == 4:
  298. n = oct2int(head[0])
  299. head = head[1:]
  300. eo, head = head[:n], head[n:]
  301. if not eo or not head:
  302. raise error.PyAsn1Error('Real exponent screwed')
  303. e = oct2int(eo[0]) & 0x80 and -1 or 0
  304. while eo: # exponent
  305. e <<= 8
  306. e |= oct2int(eo[0])
  307. eo = eo[1:]
  308. b = fo >> 4 & 0x03 # base bits
  309. if b > 2:
  310. raise error.PyAsn1Error('Illegal Real base')
  311. if b == 1: # encbase = 8
  312. e *= 3
  313. elif b == 2: # encbase = 16
  314. e *= 4
  315. p = 0
  316. while head: # value
  317. p <<= 8
  318. p |= oct2int(head[0])
  319. head = head[1:]
  320. if fo & 0x40: # sign bit
  321. p = -p
  322. sf = fo >> 2 & 0x03 # scale bits
  323. p *= 2 ** sf
  324. value = (p, 2, e)
  325. elif fo & 0x40: # infinite value
  326. if LOG:
  327. LOG('decoding infinite REAL')
  328. value = fo & 0x01 and '-inf' or 'inf'
  329. elif fo & 0xc0 == 0: # character encoding
  330. if not head:
  331. raise error.PyAsn1Error("Incomplete floating-point value")
  332. if LOG:
  333. LOG('decoding character encoded REAL')
  334. try:
  335. if fo & 0x3 == 0x1: # NR1
  336. value = (int(head), 10, 0)
  337. elif fo & 0x3 == 0x2: # NR2
  338. value = float(head)
  339. elif fo & 0x3 == 0x3: # NR3
  340. value = float(head)
  341. else:
  342. raise error.SubstrateUnderrunError(
  343. 'Unknown NR (tag %s)' % fo
  344. )
  345. except ValueError:
  346. raise error.SubstrateUnderrunError(
  347. 'Bad character Real syntax'
  348. )
  349. else:
  350. raise error.SubstrateUnderrunError(
  351. 'Unknown encoding (tag %s)' % fo
  352. )
  353. return self._createComponent(asn1Spec, tagSet, value, **options), tail
  354. class AbstractConstructedDecoder(AbstractDecoder):
  355. protoComponent = None
  356. class UniversalConstructedTypeDecoder(AbstractConstructedDecoder):
  357. protoRecordComponent = None
  358. protoSequenceComponent = None
  359. def _getComponentTagMap(self, asn1Object, idx):
  360. raise NotImplementedError()
  361. def _getComponentPositionByType(self, asn1Object, tagSet, idx):
  362. raise NotImplementedError()
  363. def _decodeComponents(self, substrate, tagSet=None, decodeFun=None, **options):
  364. components = []
  365. componentTypes = set()
  366. while substrate:
  367. component, substrate = decodeFun(substrate, **options)
  368. if component is eoo.endOfOctets:
  369. break
  370. components.append(component)
  371. componentTypes.add(component.tagSet)
  372. # Now we have to guess is it SEQUENCE/SET or SEQUENCE OF/SET OF
  373. # The heuristics is:
  374. # * 1+ components of different types -> likely SEQUENCE/SET
  375. # * otherwise -> likely SEQUENCE OF/SET OF
  376. if len(componentTypes) > 1:
  377. protoComponent = self.protoRecordComponent
  378. else:
  379. protoComponent = self.protoSequenceComponent
  380. asn1Object = protoComponent.clone(
  381. # construct tagSet from base tag from prototype ASN.1 object
  382. # and additional tags recovered from the substrate
  383. tagSet=tag.TagSet(protoComponent.tagSet.baseTag, *tagSet.superTags)
  384. )
  385. if LOG:
  386. LOG('guessed %r container type (pass `asn1Spec` to guide the '
  387. 'decoder)' % asn1Object)
  388. for idx, component in enumerate(components):
  389. asn1Object.setComponentByPosition(
  390. idx, component,
  391. verifyConstraints=False,
  392. matchTags=False, matchConstraints=False
  393. )
  394. return asn1Object, substrate
  395. def valueDecoder(self, substrate, asn1Spec,
  396. tagSet=None, length=None, state=None,
  397. decodeFun=None, substrateFun=None,
  398. **options):
  399. if tagSet[0].tagFormat != tag.tagFormatConstructed:
  400. raise error.PyAsn1Error('Constructed tag format expected')
  401. head, tail = substrate[:length], substrate[length:]
  402. if substrateFun is not None:
  403. if asn1Spec is not None:
  404. asn1Object = asn1Spec.clone()
  405. elif self.protoComponent is not None:
  406. asn1Object = self.protoComponent.clone(tagSet=tagSet)
  407. else:
  408. asn1Object = self.protoRecordComponent, self.protoSequenceComponent
  409. return substrateFun(asn1Object, substrate, length)
  410. if asn1Spec is None:
  411. asn1Object, trailing = self._decodeComponents(
  412. head, tagSet=tagSet, decodeFun=decodeFun, **options
  413. )
  414. if trailing:
  415. if LOG:
  416. LOG('Unused trailing %d octets encountered: %s' % (
  417. len(trailing), debug.hexdump(trailing)))
  418. return asn1Object, tail
  419. asn1Object = asn1Spec.clone()
  420. asn1Object.clear()
  421. if asn1Spec.typeId in (univ.Sequence.typeId, univ.Set.typeId):
  422. namedTypes = asn1Spec.componentType
  423. isSetType = asn1Spec.typeId == univ.Set.typeId
  424. isDeterministic = not isSetType and not namedTypes.hasOptionalOrDefault
  425. if LOG:
  426. LOG('decoding %sdeterministic %s type %r chosen by type ID' % (
  427. not isDeterministic and 'non-' or '', isSetType and 'SET' or '',
  428. asn1Spec))
  429. seenIndices = set()
  430. idx = 0
  431. while head:
  432. if not namedTypes:
  433. componentType = None
  434. elif isSetType:
  435. componentType = namedTypes.tagMapUnique
  436. else:
  437. try:
  438. if isDeterministic:
  439. componentType = namedTypes[idx].asn1Object
  440. elif namedTypes[idx].isOptional or namedTypes[idx].isDefaulted:
  441. componentType = namedTypes.getTagMapNearPosition(idx)
  442. else:
  443. componentType = namedTypes[idx].asn1Object
  444. except IndexError:
  445. raise error.PyAsn1Error(
  446. 'Excessive components decoded at %r' % (asn1Spec,)
  447. )
  448. component, head = decodeFun(head, componentType, **options)
  449. if not isDeterministic and namedTypes:
  450. if isSetType:
  451. idx = namedTypes.getPositionByType(component.effectiveTagSet)
  452. elif namedTypes[idx].isOptional or namedTypes[idx].isDefaulted:
  453. idx = namedTypes.getPositionNearType(component.effectiveTagSet, idx)
  454. asn1Object.setComponentByPosition(
  455. idx, component,
  456. verifyConstraints=False,
  457. matchTags=False, matchConstraints=False
  458. )
  459. seenIndices.add(idx)
  460. idx += 1
  461. if LOG:
  462. LOG('seen component indices %s' % seenIndices)
  463. if namedTypes:
  464. if not namedTypes.requiredComponents.issubset(seenIndices):
  465. raise error.PyAsn1Error(
  466. 'ASN.1 object %s has uninitialized '
  467. 'components' % asn1Object.__class__.__name__)
  468. if namedTypes.hasOpenTypes:
  469. openTypes = options.get('openTypes', {})
  470. if LOG:
  471. LOG('user-specified open types map:')
  472. for k, v in openTypes.items():
  473. LOG('%s -> %r' % (k, v))
  474. if openTypes or options.get('decodeOpenTypes', False):
  475. for idx, namedType in enumerate(namedTypes.namedTypes):
  476. if not namedType.openType:
  477. continue
  478. if namedType.isOptional and not asn1Object.getComponentByPosition(idx).isValue:
  479. continue
  480. governingValue = asn1Object.getComponentByName(
  481. namedType.openType.name
  482. )
  483. try:
  484. openType = openTypes[governingValue]
  485. except KeyError:
  486. if LOG:
  487. LOG('default open types map of component '
  488. '"%s.%s" governed by component "%s.%s"'
  489. ':' % (asn1Object.__class__.__name__,
  490. namedType.name,
  491. asn1Object.__class__.__name__,
  492. namedType.openType.name))
  493. for k, v in namedType.openType.items():
  494. LOG('%s -> %r' % (k, v))
  495. try:
  496. openType = namedType.openType[governingValue]
  497. except KeyError:
  498. if LOG:
  499. LOG('failed to resolve open type by governing '
  500. 'value %r' % (governingValue,))
  501. continue
  502. if LOG:
  503. LOG('resolved open type %r by governing '
  504. 'value %r' % (openType, governingValue))
  505. containerValue = asn1Object.getComponentByPosition(idx)
  506. if containerValue.typeId in (
  507. univ.SetOf.typeId, univ.SequenceOf.typeId):
  508. for pos, containerElement in enumerate(
  509. containerValue):
  510. component, rest = decodeFun(
  511. containerValue[pos].asOctets(),
  512. asn1Spec=openType, **options
  513. )
  514. containerValue[pos] = component
  515. else:
  516. component, rest = decodeFun(
  517. asn1Object.getComponentByPosition(idx).asOctets(),
  518. asn1Spec=openType, **options
  519. )
  520. asn1Object.setComponentByPosition(idx, component)
  521. else:
  522. inconsistency = asn1Object.isInconsistent
  523. if inconsistency:
  524. raise inconsistency
  525. else:
  526. asn1Object = asn1Spec.clone()
  527. asn1Object.clear()
  528. componentType = asn1Spec.componentType
  529. if LOG:
  530. LOG('decoding type %r chosen by given `asn1Spec`' % componentType)
  531. idx = 0
  532. while head:
  533. component, head = decodeFun(head, componentType, **options)
  534. asn1Object.setComponentByPosition(
  535. idx, component,
  536. verifyConstraints=False,
  537. matchTags=False, matchConstraints=False
  538. )
  539. idx += 1
  540. return asn1Object, tail
  541. def indefLenValueDecoder(self, substrate, asn1Spec,
  542. tagSet=None, length=None, state=None,
  543. decodeFun=None, substrateFun=None,
  544. **options):
  545. if tagSet[0].tagFormat != tag.tagFormatConstructed:
  546. raise error.PyAsn1Error('Constructed tag format expected')
  547. if substrateFun is not None:
  548. if asn1Spec is not None:
  549. asn1Object = asn1Spec.clone()
  550. elif self.protoComponent is not None:
  551. asn1Object = self.protoComponent.clone(tagSet=tagSet)
  552. else:
  553. asn1Object = self.protoRecordComponent, self.protoSequenceComponent
  554. return substrateFun(asn1Object, substrate, length)
  555. if asn1Spec is None:
  556. return self._decodeComponents(
  557. substrate, tagSet=tagSet, decodeFun=decodeFun,
  558. **dict(options, allowEoo=True)
  559. )
  560. asn1Object = asn1Spec.clone()
  561. asn1Object.clear()
  562. if asn1Spec.typeId in (univ.Sequence.typeId, univ.Set.typeId):
  563. namedTypes = asn1Object.componentType
  564. isSetType = asn1Object.typeId == univ.Set.typeId
  565. isDeterministic = not isSetType and not namedTypes.hasOptionalOrDefault
  566. if LOG:
  567. LOG('decoding %sdeterministic %s type %r chosen by type ID' % (
  568. not isDeterministic and 'non-' or '', isSetType and 'SET' or '',
  569. asn1Spec))
  570. seenIndices = set()
  571. idx = 0
  572. while substrate:
  573. if len(namedTypes) <= idx:
  574. asn1Spec = None
  575. elif isSetType:
  576. asn1Spec = namedTypes.tagMapUnique
  577. else:
  578. try:
  579. if isDeterministic:
  580. asn1Spec = namedTypes[idx].asn1Object
  581. elif namedTypes[idx].isOptional or namedTypes[idx].isDefaulted:
  582. asn1Spec = namedTypes.getTagMapNearPosition(idx)
  583. else:
  584. asn1Spec = namedTypes[idx].asn1Object
  585. except IndexError:
  586. raise error.PyAsn1Error(
  587. 'Excessive components decoded at %r' % (asn1Object,)
  588. )
  589. component, substrate = decodeFun(substrate, asn1Spec, allowEoo=True, **options)
  590. if component is eoo.endOfOctets:
  591. break
  592. if not isDeterministic and namedTypes:
  593. if isSetType:
  594. idx = namedTypes.getPositionByType(component.effectiveTagSet)
  595. elif namedTypes[idx].isOptional or namedTypes[idx].isDefaulted:
  596. idx = namedTypes.getPositionNearType(component.effectiveTagSet, idx)
  597. asn1Object.setComponentByPosition(
  598. idx, component,
  599. verifyConstraints=False,
  600. matchTags=False, matchConstraints=False
  601. )
  602. seenIndices.add(idx)
  603. idx += 1
  604. else:
  605. raise error.SubstrateUnderrunError(
  606. 'No EOO seen before substrate ends'
  607. )
  608. if LOG:
  609. LOG('seen component indices %s' % seenIndices)
  610. if namedTypes:
  611. if not namedTypes.requiredComponents.issubset(seenIndices):
  612. raise error.PyAsn1Error('ASN.1 object %s has uninitialized components' % asn1Object.__class__.__name__)
  613. if namedTypes.hasOpenTypes:
  614. openTypes = options.get('openTypes', {})
  615. if LOG:
  616. LOG('user-specified open types map:')
  617. for k, v in openTypes.items():
  618. LOG('%s -> %r' % (k, v))
  619. if openTypes or options.get('decodeOpenTypes', False):
  620. for idx, namedType in enumerate(namedTypes.namedTypes):
  621. if not namedType.openType:
  622. continue
  623. if namedType.isOptional and not asn1Object.getComponentByPosition(idx).isValue:
  624. continue
  625. governingValue = asn1Object.getComponentByName(
  626. namedType.openType.name
  627. )
  628. try:
  629. openType = openTypes[governingValue]
  630. except KeyError:
  631. if LOG:
  632. LOG('default open types map of component '
  633. '"%s.%s" governed by component "%s.%s"'
  634. ':' % (asn1Object.__class__.__name__,
  635. namedType.name,
  636. asn1Object.__class__.__name__,
  637. namedType.openType.name))
  638. for k, v in namedType.openType.items():
  639. LOG('%s -> %r' % (k, v))
  640. try:
  641. openType = namedType.openType[governingValue]
  642. except KeyError:
  643. if LOG:
  644. LOG('failed to resolve open type by governing '
  645. 'value %r' % (governingValue,))
  646. continue
  647. if LOG:
  648. LOG('resolved open type %r by governing '
  649. 'value %r' % (openType, governingValue))
  650. containerValue = asn1Object.getComponentByPosition(idx)
  651. if containerValue.typeId in (
  652. univ.SetOf.typeId, univ.SequenceOf.typeId):
  653. for pos, containerElement in enumerate(
  654. containerValue):
  655. component, rest = decodeFun(
  656. containerValue[pos].asOctets(),
  657. asn1Spec=openType, **dict(options, allowEoo=True)
  658. )
  659. containerValue[pos] = component
  660. else:
  661. component, rest = decodeFun(
  662. asn1Object.getComponentByPosition(idx).asOctets(),
  663. asn1Spec=openType, **dict(options, allowEoo=True)
  664. )
  665. if component is not eoo.endOfOctets:
  666. asn1Object.setComponentByPosition(idx, component)
  667. else:
  668. inconsistency = asn1Object.isInconsistent
  669. if inconsistency:
  670. raise inconsistency
  671. else:
  672. asn1Object = asn1Spec.clone()
  673. asn1Object.clear()
  674. componentType = asn1Spec.componentType
  675. if LOG:
  676. LOG('decoding type %r chosen by given `asn1Spec`' % componentType)
  677. idx = 0
  678. while substrate:
  679. component, substrate = decodeFun(substrate, componentType, allowEoo=True, **options)
  680. if component is eoo.endOfOctets:
  681. break
  682. asn1Object.setComponentByPosition(
  683. idx, component,
  684. verifyConstraints=False,
  685. matchTags=False, matchConstraints=False
  686. )
  687. idx += 1
  688. else:
  689. raise error.SubstrateUnderrunError(
  690. 'No EOO seen before substrate ends'
  691. )
  692. return asn1Object, substrate
  693. class SequenceOrSequenceOfDecoder(UniversalConstructedTypeDecoder):
  694. protoRecordComponent = univ.Sequence()
  695. protoSequenceComponent = univ.SequenceOf()
  696. class SequenceDecoder(SequenceOrSequenceOfDecoder):
  697. protoComponent = univ.Sequence()
  698. class SequenceOfDecoder(SequenceOrSequenceOfDecoder):
  699. protoComponent = univ.SequenceOf()
  700. class SetOrSetOfDecoder(UniversalConstructedTypeDecoder):
  701. protoRecordComponent = univ.Set()
  702. protoSequenceComponent = univ.SetOf()
  703. class SetDecoder(SetOrSetOfDecoder):
  704. protoComponent = univ.Set()
  705. class SetOfDecoder(SetOrSetOfDecoder):
  706. protoComponent = univ.SetOf()
  707. class ChoiceDecoder(AbstractConstructedDecoder):
  708. protoComponent = univ.Choice()
  709. def valueDecoder(self, substrate, asn1Spec,
  710. tagSet=None, length=None, state=None,
  711. decodeFun=None, substrateFun=None,
  712. **options):
  713. head, tail = substrate[:length], substrate[length:]
  714. if asn1Spec is None:
  715. asn1Object = self.protoComponent.clone(tagSet=tagSet)
  716. else:
  717. asn1Object = asn1Spec.clone()
  718. if substrateFun:
  719. return substrateFun(asn1Object, substrate, length)
  720. if asn1Object.tagSet == tagSet:
  721. if LOG:
  722. LOG('decoding %s as explicitly tagged CHOICE' % (tagSet,))
  723. component, head = decodeFun(
  724. head, asn1Object.componentTagMap, **options
  725. )
  726. else:
  727. if LOG:
  728. LOG('decoding %s as untagged CHOICE' % (tagSet,))
  729. component, head = decodeFun(
  730. head, asn1Object.componentTagMap,
  731. tagSet, length, state, **options
  732. )
  733. effectiveTagSet = component.effectiveTagSet
  734. if LOG:
  735. LOG('decoded component %s, effective tag set %s' % (component, effectiveTagSet))
  736. asn1Object.setComponentByType(
  737. effectiveTagSet, component,
  738. verifyConstraints=False,
  739. matchTags=False, matchConstraints=False,
  740. innerFlag=False
  741. )
  742. return asn1Object, tail
  743. def indefLenValueDecoder(self, substrate, asn1Spec,
  744. tagSet=None, length=None, state=None,
  745. decodeFun=None, substrateFun=None,
  746. **options):
  747. if asn1Spec is None:
  748. asn1Object = self.protoComponent.clone(tagSet=tagSet)
  749. else:
  750. asn1Object = asn1Spec.clone()
  751. if substrateFun:
  752. return substrateFun(asn1Object, substrate, length)
  753. if asn1Object.tagSet == tagSet:
  754. if LOG:
  755. LOG('decoding %s as explicitly tagged CHOICE' % (tagSet,))
  756. component, substrate = decodeFun(
  757. substrate, asn1Object.componentType.tagMapUnique, **options
  758. )
  759. # eat up EOO marker
  760. eooMarker, substrate = decodeFun(
  761. substrate, allowEoo=True, **options
  762. )
  763. if eooMarker is not eoo.endOfOctets:
  764. raise error.PyAsn1Error('No EOO seen before substrate ends')
  765. else:
  766. if LOG:
  767. LOG('decoding %s as untagged CHOICE' % (tagSet,))
  768. component, substrate = decodeFun(
  769. substrate, asn1Object.componentType.tagMapUnique,
  770. tagSet, length, state, **options
  771. )
  772. effectiveTagSet = component.effectiveTagSet
  773. if LOG:
  774. LOG('decoded component %s, effective tag set %s' % (component, effectiveTagSet))
  775. asn1Object.setComponentByType(
  776. effectiveTagSet, component,
  777. verifyConstraints=False,
  778. matchTags=False, matchConstraints=False,
  779. innerFlag=False
  780. )
  781. return asn1Object, substrate
  782. class AnyDecoder(AbstractSimpleDecoder):
  783. protoComponent = univ.Any()
  784. def valueDecoder(self, substrate, asn1Spec,
  785. tagSet=None, length=None, state=None,
  786. decodeFun=None, substrateFun=None,
  787. **options):
  788. if asn1Spec is None:
  789. isUntagged = True
  790. elif asn1Spec.__class__ is tagmap.TagMap:
  791. isUntagged = tagSet not in asn1Spec.tagMap
  792. else:
  793. isUntagged = tagSet != asn1Spec.tagSet
  794. if isUntagged:
  795. fullSubstrate = options['fullSubstrate']
  796. # untagged Any container, recover inner header substrate
  797. length += len(fullSubstrate) - len(substrate)
  798. substrate = fullSubstrate
  799. if LOG:
  800. LOG('decoding as untagged ANY, substrate %s' % debug.hexdump(substrate))
  801. if substrateFun:
  802. return substrateFun(self._createComponent(asn1Spec, tagSet, noValue, **options),
  803. substrate, length)
  804. head, tail = substrate[:length], substrate[length:]
  805. return self._createComponent(asn1Spec, tagSet, head, **options), tail
  806. def indefLenValueDecoder(self, substrate, asn1Spec,
  807. tagSet=None, length=None, state=None,
  808. decodeFun=None, substrateFun=None,
  809. **options):
  810. if asn1Spec is None:
  811. isTagged = False
  812. elif asn1Spec.__class__ is tagmap.TagMap:
  813. isTagged = tagSet in asn1Spec.tagMap
  814. else:
  815. isTagged = tagSet == asn1Spec.tagSet
  816. if isTagged:
  817. # tagged Any type -- consume header substrate
  818. header = null
  819. if LOG:
  820. LOG('decoding as tagged ANY')
  821. else:
  822. fullSubstrate = options['fullSubstrate']
  823. # untagged Any, recover header substrate
  824. header = fullSubstrate[:-len(substrate)]
  825. if LOG:
  826. LOG('decoding as untagged ANY, header substrate %s' % debug.hexdump(header))
  827. # Any components do not inherit initial tag
  828. asn1Spec = self.protoComponent
  829. if substrateFun and substrateFun is not self.substrateCollector:
  830. asn1Object = self._createComponent(asn1Spec, tagSet, noValue, **options)
  831. return substrateFun(asn1Object, header + substrate, length + len(header))
  832. if LOG:
  833. LOG('assembling constructed serialization')
  834. # All inner fragments are of the same type, treat them as octet string
  835. substrateFun = self.substrateCollector
  836. while substrate:
  837. component, substrate = decodeFun(substrate, asn1Spec,
  838. substrateFun=substrateFun,
  839. allowEoo=True, **options)
  840. if component is eoo.endOfOctets:
  841. break
  842. header += component
  843. else:
  844. raise error.SubstrateUnderrunError(
  845. 'No EOO seen before substrate ends'
  846. )
  847. if substrateFun:
  848. return header, substrate
  849. else:
  850. return self._createComponent(asn1Spec, tagSet, header, **options), substrate
  851. # character string types
  852. class UTF8StringDecoder(OctetStringDecoder):
  853. protoComponent = char.UTF8String()
  854. class NumericStringDecoder(OctetStringDecoder):
  855. protoComponent = char.NumericString()
  856. class PrintableStringDecoder(OctetStringDecoder):
  857. protoComponent = char.PrintableString()
  858. class TeletexStringDecoder(OctetStringDecoder):
  859. protoComponent = char.TeletexString()
  860. class VideotexStringDecoder(OctetStringDecoder):
  861. protoComponent = char.VideotexString()
  862. class IA5StringDecoder(OctetStringDecoder):
  863. protoComponent = char.IA5String()
  864. class GraphicStringDecoder(OctetStringDecoder):
  865. protoComponent = char.GraphicString()
  866. class VisibleStringDecoder(OctetStringDecoder):
  867. protoComponent = char.VisibleString()
  868. class GeneralStringDecoder(OctetStringDecoder):
  869. protoComponent = char.GeneralString()
  870. class UniversalStringDecoder(OctetStringDecoder):
  871. protoComponent = char.UniversalString()
  872. class BMPStringDecoder(OctetStringDecoder):
  873. protoComponent = char.BMPString()
  874. # "useful" types
  875. class ObjectDescriptorDecoder(OctetStringDecoder):
  876. protoComponent = useful.ObjectDescriptor()
  877. class GeneralizedTimeDecoder(OctetStringDecoder):
  878. protoComponent = useful.GeneralizedTime()
  879. class UTCTimeDecoder(OctetStringDecoder):
  880. protoComponent = useful.UTCTime()
  881. tagMap = {
  882. univ.Integer.tagSet: IntegerDecoder(),
  883. univ.Boolean.tagSet: BooleanDecoder(),
  884. univ.BitString.tagSet: BitStringDecoder(),
  885. univ.OctetString.tagSet: OctetStringDecoder(),
  886. univ.Null.tagSet: NullDecoder(),
  887. univ.ObjectIdentifier.tagSet: ObjectIdentifierDecoder(),
  888. univ.Enumerated.tagSet: IntegerDecoder(),
  889. univ.Real.tagSet: RealDecoder(),
  890. univ.Sequence.tagSet: SequenceOrSequenceOfDecoder(), # conflicts with SequenceOf
  891. univ.Set.tagSet: SetOrSetOfDecoder(), # conflicts with SetOf
  892. univ.Choice.tagSet: ChoiceDecoder(), # conflicts with Any
  893. # character string types
  894. char.UTF8String.tagSet: UTF8StringDecoder(),
  895. char.NumericString.tagSet: NumericStringDecoder(),
  896. char.PrintableString.tagSet: PrintableStringDecoder(),
  897. char.TeletexString.tagSet: TeletexStringDecoder(),
  898. char.VideotexString.tagSet: VideotexStringDecoder(),
  899. char.IA5String.tagSet: IA5StringDecoder(),
  900. char.GraphicString.tagSet: GraphicStringDecoder(),
  901. char.VisibleString.tagSet: VisibleStringDecoder(),
  902. char.GeneralString.tagSet: GeneralStringDecoder(),
  903. char.UniversalString.tagSet: UniversalStringDecoder(),
  904. char.BMPString.tagSet: BMPStringDecoder(),
  905. # useful types
  906. useful.ObjectDescriptor.tagSet: ObjectDescriptorDecoder(),
  907. useful.GeneralizedTime.tagSet: GeneralizedTimeDecoder(),
  908. useful.UTCTime.tagSet: UTCTimeDecoder()
  909. }
  910. # Type-to-codec map for ambiguous ASN.1 types
  911. typeMap = {
  912. univ.Set.typeId: SetDecoder(),
  913. univ.SetOf.typeId: SetOfDecoder(),
  914. univ.Sequence.typeId: SequenceDecoder(),
  915. univ.SequenceOf.typeId: SequenceOfDecoder(),
  916. univ.Choice.typeId: ChoiceDecoder(),
  917. univ.Any.typeId: AnyDecoder()
  918. }
  919. # Put in non-ambiguous types for faster codec lookup
  920. for typeDecoder in tagMap.values():
  921. if typeDecoder.protoComponent is not None:
  922. typeId = typeDecoder.protoComponent.__class__.typeId
  923. if typeId is not None and typeId not in typeMap:
  924. typeMap[typeId] = typeDecoder
  925. (stDecodeTag,
  926. stDecodeLength,
  927. stGetValueDecoder,
  928. stGetValueDecoderByAsn1Spec,
  929. stGetValueDecoderByTag,
  930. stTryAsExplicitTag,
  931. stDecodeValue,
  932. stDumpRawValue,
  933. stErrorCondition,
  934. stStop) = [x for x in range(10)]
  935. class Decoder(object):
  936. defaultErrorState = stErrorCondition
  937. #defaultErrorState = stDumpRawValue
  938. defaultRawDecoder = AnyDecoder()
  939. supportIndefLength = True
  940. # noinspection PyDefaultArgument
  941. def __init__(self, tagMap, typeMap={}):
  942. self.__tagMap = tagMap
  943. self.__typeMap = typeMap
  944. # Tag & TagSet objects caches
  945. self.__tagCache = {}
  946. self.__tagSetCache = {}
  947. self.__eooSentinel = ints2octs((0, 0))
  948. def __call__(self, substrate, asn1Spec=None,
  949. tagSet=None, length=None, state=stDecodeTag,
  950. decodeFun=None, substrateFun=None,
  951. **options):
  952. if LOG:
  953. LOG('decoder called at scope %s with state %d, working with up to %d octets of substrate: %s' % (debug.scope, state, len(substrate), debug.hexdump(substrate)))
  954. allowEoo = options.pop('allowEoo', False)
  955. # Look for end-of-octets sentinel
  956. if allowEoo and self.supportIndefLength:
  957. if substrate[:2] == self.__eooSentinel:
  958. if LOG:
  959. LOG('end-of-octets sentinel found')
  960. return eoo.endOfOctets, substrate[2:]
  961. value = noValue
  962. tagMap = self.__tagMap
  963. typeMap = self.__typeMap
  964. tagCache = self.__tagCache
  965. tagSetCache = self.__tagSetCache
  966. fullSubstrate = substrate
  967. while state is not stStop:
  968. if state is stDecodeTag:
  969. if not substrate:
  970. raise error.SubstrateUnderrunError(
  971. 'Short octet stream on tag decoding'
  972. )
  973. # Decode tag
  974. isShortTag = True
  975. firstOctet = substrate[0]
  976. substrate = substrate[1:]
  977. try:
  978. lastTag = tagCache[firstOctet]
  979. except KeyError:
  980. integerTag = oct2int(firstOctet)
  981. tagClass = integerTag & 0xC0
  982. tagFormat = integerTag & 0x20
  983. tagId = integerTag & 0x1F
  984. if tagId == 0x1F:
  985. isShortTag = False
  986. lengthOctetIdx = 0
  987. tagId = 0
  988. try:
  989. while True:
  990. integerTag = oct2int(substrate[lengthOctetIdx])
  991. lengthOctetIdx += 1
  992. tagId <<= 7
  993. tagId |= (integerTag & 0x7F)
  994. if not integerTag & 0x80:
  995. break
  996. substrate = substrate[lengthOctetIdx:]
  997. except IndexError:
  998. raise error.SubstrateUnderrunError(
  999. 'Short octet stream on long tag decoding'
  1000. )
  1001. lastTag = tag.Tag(
  1002. tagClass=tagClass, tagFormat=tagFormat, tagId=tagId
  1003. )
  1004. if isShortTag:
  1005. # cache short tags
  1006. tagCache[firstOctet] = lastTag
  1007. if tagSet is None:
  1008. if isShortTag:
  1009. try:
  1010. tagSet = tagSetCache[firstOctet]
  1011. except KeyError:
  1012. # base tag not recovered
  1013. tagSet = tag.TagSet((), lastTag)
  1014. tagSetCache[firstOctet] = tagSet
  1015. else:
  1016. tagSet = tag.TagSet((), lastTag)
  1017. else:
  1018. tagSet = lastTag + tagSet
  1019. state = stDecodeLength
  1020. if LOG:
  1021. LOG('tag decoded into %s, decoding length' % tagSet)
  1022. if state is stDecodeLength:
  1023. # Decode length
  1024. if not substrate:
  1025. raise error.SubstrateUnderrunError(
  1026. 'Short octet stream on length decoding'
  1027. )
  1028. firstOctet = oct2int(substrate[0])
  1029. if firstOctet < 128:
  1030. size = 1
  1031. length = firstOctet
  1032. elif firstOctet > 128:
  1033. size = firstOctet & 0x7F
  1034. # encoded in size bytes
  1035. encodedLength = octs2ints(substrate[1:size + 1])
  1036. # missing check on maximum size, which shouldn't be a
  1037. # problem, we can handle more than is possible
  1038. if len(encodedLength) != size:
  1039. raise error.SubstrateUnderrunError(
  1040. '%s<%s at %s' % (size, len(encodedLength), tagSet)
  1041. )
  1042. length = 0
  1043. for lengthOctet in encodedLength:
  1044. length <<= 8
  1045. length |= lengthOctet
  1046. size += 1
  1047. else:
  1048. size = 1
  1049. length = -1
  1050. substrate = substrate[size:]
  1051. if length == -1:
  1052. if not self.supportIndefLength:
  1053. raise error.PyAsn1Error('Indefinite length encoding not supported by this codec')
  1054. else:
  1055. if len(substrate) < length:
  1056. raise error.SubstrateUnderrunError('%d-octet short' % (length - len(substrate)))
  1057. state = stGetValueDecoder
  1058. if LOG:
  1059. LOG('value length decoded into %d, payload substrate is: %s' % (length, debug.hexdump(length == -1 and substrate or substrate[:length])))
  1060. if state is stGetValueDecoder:
  1061. if asn1Spec is None:
  1062. state = stGetValueDecoderByTag
  1063. else:
  1064. state = stGetValueDecoderByAsn1Spec
  1065. #
  1066. # There're two ways of creating subtypes in ASN.1 what influences
  1067. # decoder operation. These methods are:
  1068. # 1) Either base types used in or no IMPLICIT tagging has been
  1069. # applied on subtyping.
  1070. # 2) Subtype syntax drops base type information (by means of
  1071. # IMPLICIT tagging.
  1072. # The first case allows for complete tag recovery from substrate
  1073. # while the second one requires original ASN.1 type spec for
  1074. # decoding.
  1075. #
  1076. # In either case a set of tags (tagSet) is coming from substrate
  1077. # in an incremental, tag-by-tag fashion (this is the case of
  1078. # EXPLICIT tag which is most basic). Outermost tag comes first
  1079. # from the wire.
  1080. #
  1081. if state is stGetValueDecoderByTag:
  1082. try:
  1083. concreteDecoder = tagMap[tagSet]
  1084. except KeyError:
  1085. concreteDecoder = None
  1086. if concreteDecoder:
  1087. state = stDecodeValue
  1088. else:
  1089. try:
  1090. concreteDecoder = tagMap[tagSet[:1]]
  1091. except KeyError:
  1092. concreteDecoder = None
  1093. if concreteDecoder:
  1094. state = stDecodeValue
  1095. else:
  1096. state = stTryAsExplicitTag
  1097. if LOG:
  1098. LOG('codec %s chosen by a built-in type, decoding %s' % (concreteDecoder and concreteDecoder.__class__.__name__ or "<none>", state is stDecodeValue and 'value' or 'as explicit tag'))
  1099. debug.scope.push(concreteDecoder is None and '?' or concreteDecoder.protoComponent.__class__.__name__)
  1100. if state is stGetValueDecoderByAsn1Spec:
  1101. if asn1Spec.__class__ is tagmap.TagMap:
  1102. try:
  1103. chosenSpec = asn1Spec[tagSet]
  1104. except KeyError:
  1105. chosenSpec = None
  1106. if LOG:
  1107. LOG('candidate ASN.1 spec is a map of:')
  1108. for firstOctet, v in asn1Spec.presentTypes.items():
  1109. LOG(' %s -> %s' % (firstOctet, v.__class__.__name__))
  1110. if asn1Spec.skipTypes:
  1111. LOG('but neither of: ')
  1112. for firstOctet, v in asn1Spec.skipTypes.items():
  1113. LOG(' %s -> %s' % (firstOctet, v.__class__.__name__))
  1114. LOG('new candidate ASN.1 spec is %s, chosen by %s' % (chosenSpec is None and '<none>' or chosenSpec.prettyPrintType(), tagSet))
  1115. elif tagSet == asn1Spec.tagSet or tagSet in asn1Spec.tagMap:
  1116. chosenSpec = asn1Spec
  1117. if LOG:
  1118. LOG('candidate ASN.1 spec is %s' % asn1Spec.__class__.__name__)
  1119. else:
  1120. chosenSpec = None
  1121. if chosenSpec is not None:
  1122. try:
  1123. # ambiguous type or just faster codec lookup
  1124. concreteDecoder = typeMap[chosenSpec.typeId]
  1125. if LOG:
  1126. LOG('value decoder chosen for an ambiguous type by type ID %s' % (chosenSpec.typeId,))
  1127. except KeyError:
  1128. # use base type for codec lookup to recover untagged types
  1129. baseTagSet = tag.TagSet(chosenSpec.tagSet.baseTag, chosenSpec.tagSet.baseTag)
  1130. try:
  1131. # base type or tagged subtype
  1132. concreteDecoder = tagMap[baseTagSet]
  1133. if LOG:
  1134. LOG('value decoder chosen by base %s' % (baseTagSet,))
  1135. except KeyError:
  1136. concreteDecoder = None
  1137. if concreteDecoder:
  1138. asn1Spec = chosenSpec
  1139. state = stDecodeValue
  1140. else:
  1141. state = stTryAsExplicitTag
  1142. else:
  1143. concreteDecoder = None
  1144. state = stTryAsExplicitTag
  1145. if LOG:
  1146. LOG('codec %s chosen by ASN.1 spec, decoding %s' % (state is stDecodeValue and concreteDecoder.__class__.__name__ or "<none>", state is stDecodeValue and 'value' or 'as explicit tag'))
  1147. debug.scope.push(chosenSpec is None and '?' or chosenSpec.__class__.__name__)
  1148. if state is stDecodeValue:
  1149. if not options.get('recursiveFlag', True) and not substrateFun: # deprecate this
  1150. substrateFun = lambda a, b, c: (a, b[:c])
  1151. options.update(fullSubstrate=fullSubstrate)
  1152. if length == -1: # indef length
  1153. value, substrate = concreteDecoder.indefLenValueDecoder(
  1154. substrate, asn1Spec,
  1155. tagSet, length, stGetValueDecoder,
  1156. self, substrateFun,
  1157. **options
  1158. )
  1159. else:
  1160. value, substrate = concreteDecoder.valueDecoder(
  1161. substrate, asn1Spec,
  1162. tagSet, length, stGetValueDecoder,
  1163. self, substrateFun,
  1164. **options
  1165. )
  1166. if LOG:
  1167. LOG('codec %s yields type %s, value:\n%s\n...remaining substrate is: %s' % (concreteDecoder.__class__.__name__, value.__class__.__name__, isinstance(value, base.Asn1Item) and value.prettyPrint() or value, substrate and debug.hexdump(substrate) or '<none>'))
  1168. state = stStop
  1169. break
  1170. if state is stTryAsExplicitTag:
  1171. if (tagSet and
  1172. tagSet[0].tagFormat == tag.tagFormatConstructed and
  1173. tagSet[0].tagClass != tag.tagClassUniversal):
  1174. # Assume explicit tagging
  1175. concreteDecoder = explicitTagDecoder
  1176. state = stDecodeValue
  1177. else:
  1178. concreteDecoder = None
  1179. state = self.defaultErrorState
  1180. if LOG:
  1181. LOG('codec %s chosen, decoding %s' % (concreteDecoder and concreteDecoder.__class__.__name__ or "<none>", state is stDecodeValue and 'value' or 'as failure'))
  1182. if state is stDumpRawValue:
  1183. concreteDecoder = self.defaultRawDecoder
  1184. if LOG:
  1185. LOG('codec %s chosen, decoding value' % concreteDecoder.__class__.__name__)
  1186. state = stDecodeValue
  1187. if state is stErrorCondition:
  1188. raise error.PyAsn1Error(
  1189. '%s not in asn1Spec: %r' % (tagSet, asn1Spec)
  1190. )
  1191. if LOG:
  1192. debug.scope.pop()
  1193. LOG('decoder left scope %s, call completed' % debug.scope)
  1194. return value, substrate
  1195. #: Turns BER octet stream into an ASN.1 object.
  1196. #:
  1197. #: Takes BER octet-stream and decode it into an ASN.1 object
  1198. #: (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative) which
  1199. #: may be a scalar or an arbitrary nested structure.
  1200. #:
  1201. #: Parameters
  1202. #: ----------
  1203. #: substrate: :py:class:`bytes` (Python 3) or :py:class:`str` (Python 2)
  1204. #: BER octet-stream
  1205. #:
  1206. #: Keyword Args
  1207. #: ------------
  1208. #: asn1Spec: any pyasn1 type object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
  1209. #: A pyasn1 type object to act as a template guiding the decoder. Depending on the ASN.1 structure
  1210. #: being decoded, *asn1Spec* may or may not be required. Most common reason for
  1211. #: it to require is that ASN.1 structure is encoded in *IMPLICIT* tagging mode.
  1212. #:
  1213. #: Returns
  1214. #: -------
  1215. #: : :py:class:`tuple`
  1216. #: A tuple of pyasn1 object recovered from BER substrate (:py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
  1217. #: and the unprocessed trailing portion of the *substrate* (may be empty)
  1218. #:
  1219. #: Raises
  1220. #: ------
  1221. #: ~pyasn1.error.PyAsn1Error, ~pyasn1.error.SubstrateUnderrunError
  1222. #: On decoding errors
  1223. #:
  1224. #: Examples
  1225. #: --------
  1226. #: Decode BER serialisation without ASN.1 schema
  1227. #:
  1228. #: .. code-block:: pycon
  1229. #:
  1230. #: >>> s, _ = decode(b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03')
  1231. #: >>> str(s)
  1232. #: SequenceOf:
  1233. #: 1 2 3
  1234. #:
  1235. #: Decode BER serialisation with ASN.1 schema
  1236. #:
  1237. #: .. code-block:: pycon
  1238. #:
  1239. #: >>> seq = SequenceOf(componentType=Integer())
  1240. #: >>> s, _ = decode(b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03', asn1Spec=seq)
  1241. #: >>> str(s)
  1242. #: SequenceOf:
  1243. #: 1 2 3
  1244. #:
  1245. decode = Decoder(tagMap, typeMap)
  1246. # XXX
  1247. # non-recursive decoding; return position rather than substrate