Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pyasn1/codec/der/decoder.py: 97%

30 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-08 06:40 +0000

1# 

2# This file is part of pyasn1 software. 

3# 

4# Copyright (c) 2005-2020, Ilya Etingof <etingof@gmail.com> 

5# License: https://pyasn1.readthedocs.io/en/latest/license.html 

6# 

7from pyasn1.codec.cer import decoder 

8from pyasn1.type import univ 

9 

10__all__ = ['decode', 'StreamingDecoder'] 

11 

12 

13class BitStringPayloadDecoder(decoder.BitStringPayloadDecoder): 

14 supportConstructedForm = False 

15 

16 

17class OctetStringPayloadDecoder(decoder.OctetStringPayloadDecoder): 

18 supportConstructedForm = False 

19 

20 

21# TODO: prohibit non-canonical encoding 

22RealPayloadDecoder = decoder.RealPayloadDecoder 

23 

24TAG_MAP = decoder.TAG_MAP.copy() 

25TAG_MAP.update( 

26 {univ.BitString.tagSet: BitStringPayloadDecoder(), 

27 univ.OctetString.tagSet: OctetStringPayloadDecoder(), 

28 univ.Real.tagSet: RealPayloadDecoder()} 

29) 

30 

31TYPE_MAP = decoder.TYPE_MAP.copy() 

32 

33# deprecated aliases, https://github.com/pyasn1/pyasn1/issues/9 

34tagMap = TAG_MAP 

35typeMap = TYPE_MAP 

36 

37# Put in non-ambiguous types for faster codec lookup 

38for typeDecoder in TAG_MAP.values(): 

39 if typeDecoder.protoComponent is not None: 

40 typeId = typeDecoder.protoComponent.__class__.typeId 

41 if typeId is not None and typeId not in TYPE_MAP: 

42 TYPE_MAP[typeId] = typeDecoder 

43 

44 

45class SingleItemDecoder(decoder.SingleItemDecoder): 

46 __doc__ = decoder.SingleItemDecoder.__doc__ 

47 

48 TAG_MAP = TAG_MAP 

49 TYPE_MAP = TYPE_MAP 

50 

51 supportIndefLength = False 

52 

53 

54class StreamingDecoder(decoder.StreamingDecoder): 

55 __doc__ = decoder.StreamingDecoder.__doc__ 

56 

57 SINGLE_ITEM_DECODER = SingleItemDecoder 

58 

59 

60class Decoder(decoder.Decoder): 

61 __doc__ = decoder.Decoder.__doc__ 

62 

63 STREAMING_DECODER = StreamingDecoder 

64 

65 

66#: Turns DER octet stream into an ASN.1 object. 

67#: 

68#: Takes DER octet-stream and decode it into an ASN.1 object 

69#: (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative) which 

70#: may be a scalar or an arbitrary nested structure. 

71#: 

72#: Parameters 

73#: ---------- 

74#: substrate: :py:class:`bytes` (Python 3) or :py:class:`str` (Python 2) 

75#: DER octet-stream 

76#: 

77#: Keyword Args 

78#: ------------ 

79#: asn1Spec: any pyasn1 type object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative 

80#: A pyasn1 type object to act as a template guiding the decoder. Depending on the ASN.1 structure 

81#: being decoded, *asn1Spec* may or may not be required. Most common reason for 

82#: it to require is that ASN.1 structure is encoded in *IMPLICIT* tagging mode. 

83#: 

84#: Returns 

85#: ------- 

86#: : :py:class:`tuple` 

87#: A tuple of pyasn1 object recovered from DER substrate (:py:class:`~pyasn1.type.base.PyAsn1Item` derivative) 

88#: and the unprocessed trailing portion of the *substrate* (may be empty) 

89#: 

90#: Raises 

91#: ------ 

92#: ~pyasn1.error.PyAsn1Error, ~pyasn1.error.SubstrateUnderrunError 

93#: On decoding errors 

94#: 

95#: Examples 

96#: -------- 

97#: Decode DER serialisation without ASN.1 schema 

98#: 

99#: .. code-block:: pycon 

100#: 

101#: >>> s, _ = decode(b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03') 

102#: >>> str(s) 

103#: SequenceOf: 

104#: 1 2 3 

105#: 

106#: Decode DER serialisation with ASN.1 schema 

107#: 

108#: .. code-block:: pycon 

109#: 

110#: >>> seq = SequenceOf(componentType=Integer()) 

111#: >>> s, _ = decode(b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03', asn1Spec=seq) 

112#: >>> str(s) 

113#: SequenceOf: 

114#: 1 2 3 

115#: 

116decode = Decoder()