assassinlike commited on
Commit
8ab8940
·
verified ·
1 Parent(s): 3429363

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. data/miniconda3/lib/python3.13/site-packages/anaconda_cli_base-0.7.0.dist-info/licenses/LICENSE +27 -0
  2. data/miniconda3/lib/python3.13/site-packages/conda_anaconda_tos-0.2.2.dist-info/licenses/LICENSE +27 -0
  3. data/miniconda3/lib/python3.13/site-packages/conda_content_trust/__pycache__/__init__.cpython-313.pyc +0 -0
  4. data/miniconda3/lib/python3.13/site-packages/conda_content_trust/__pycache__/__main__.cpython-313.pyc +0 -0
  5. data/miniconda3/lib/python3.13/site-packages/conda_content_trust/__pycache__/__version__.cpython-313.pyc +0 -0
  6. data/miniconda3/lib/python3.13/site-packages/conda_content_trust/__pycache__/authentication.cpython-313.pyc +0 -0
  7. data/miniconda3/lib/python3.13/site-packages/conda_content_trust/__pycache__/cli.cpython-313.pyc +0 -0
  8. data/miniconda3/lib/python3.13/site-packages/conda_content_trust/__pycache__/common.cpython-313.pyc +0 -0
  9. data/miniconda3/lib/python3.13/site-packages/conda_content_trust/__pycache__/metadata_construction.cpython-313.pyc +0 -0
  10. data/miniconda3/lib/python3.13/site-packages/conda_content_trust/__pycache__/plugin.cpython-313.pyc +0 -0
  11. data/miniconda3/lib/python3.13/site-packages/conda_content_trust/__pycache__/root_signing.cpython-313.pyc +0 -0
  12. data/miniconda3/lib/python3.13/site-packages/conda_content_trust/__pycache__/signing.cpython-313.pyc +0 -0
  13. data/miniconda3/lib/python3.13/site-packages/cycler/__pycache__/__init__.cpython-313.pyc +0 -0
  14. data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/__init__.cpython-313.pyc +0 -0
  15. data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/duration.cpython-313.pyc +0 -0
  16. data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/isodates.cpython-313.pyc +0 -0
  17. data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/isodatetime.cpython-313.pyc +0 -0
  18. data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/isoduration.cpython-313.pyc +0 -0
  19. data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/isoerror.cpython-313.pyc +0 -0
  20. data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/isostrf.cpython-313.pyc +0 -0
  21. data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/isotime.cpython-313.pyc +0 -0
  22. data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/isotzinfo.cpython-313.pyc +0 -0
  23. data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/tzinfo.cpython-313.pyc +0 -0
  24. data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/version.cpython-313.pyc +0 -0
  25. data/miniconda3/lib/python3.13/site-packages/pyasn1-0.6.3.dist-info/licenses/LICENSE.rst +24 -0
  26. data/miniconda3/lib/python3.13/site-packages/pyasn1/__pycache__/__init__.cpython-313.pyc +0 -0
  27. data/miniconda3/lib/python3.13/site-packages/pyasn1/__pycache__/debug.cpython-313.pyc +0 -0
  28. data/miniconda3/lib/python3.13/site-packages/pyasn1/__pycache__/error.cpython-313.pyc +0 -0
  29. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/__init__.py +1 -0
  30. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/__pycache__/__init__.cpython-313.pyc +0 -0
  31. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/__pycache__/streaming.cpython-313.pyc +0 -0
  32. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/ber/__init__.py +1 -0
  33. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/ber/__pycache__/__init__.cpython-313.pyc +0 -0
  34. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/ber/__pycache__/decoder.cpython-313.pyc +0 -0
  35. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/ber/__pycache__/encoder.cpython-313.pyc +0 -0
  36. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/ber/__pycache__/eoo.cpython-313.pyc +0 -0
  37. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/ber/decoder.py +2225 -0
  38. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/ber/encoder.py +954 -0
  39. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/ber/eoo.py +28 -0
  40. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/cer/__init__.py +1 -0
  41. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/cer/__pycache__/__init__.cpython-313.pyc +0 -0
  42. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/cer/__pycache__/decoder.cpython-313.pyc +0 -0
  43. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/cer/__pycache__/encoder.cpython-313.pyc +0 -0
  44. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/cer/decoder.py +149 -0
  45. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/cer/encoder.py +331 -0
  46. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/der/__init__.py +1 -0
  47. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/der/__pycache__/__init__.cpython-313.pyc +0 -0
  48. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/der/__pycache__/decoder.cpython-313.pyc +0 -0
  49. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/der/__pycache__/encoder.cpython-313.pyc +0 -0
  50. data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/der/decoder.py +120 -0
data/miniconda3/lib/python3.13/site-packages/anaconda_cli_base-0.7.0.dist-info/licenses/LICENSE ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2023, Anaconda, Inc.
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ * Redistributions of source code must retain the above copyright
10
+ notice, this list of conditions and the following disclaimer.
11
+ * Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer in the
13
+ documentation and/or other materials provided with the distribution.
14
+ * Neither the name of the copyright holder nor the names of its
15
+ contributors may be used to endorse or promote products
16
+ derived from this software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
22
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/miniconda3/lib/python3.13/site-packages/conda_anaconda_tos-0.2.2.dist-info/licenses/LICENSE ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2024, Anaconda, Inc.
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ * Redistributions of source code must retain the above copyright
10
+ notice, this list of conditions and the following disclaimer.
11
+ * Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer in the
13
+ documentation and/or other materials provided with the distribution.
14
+ * Neither the name of the copyright holder nor the names of its
15
+ contributors may be used to endorse or promote products
16
+ derived from this software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
22
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/miniconda3/lib/python3.13/site-packages/conda_content_trust/__pycache__/__init__.cpython-313.pyc ADDED
Binary file (194 Bytes). View file
 
data/miniconda3/lib/python3.13/site-packages/conda_content_trust/__pycache__/__main__.cpython-313.pyc ADDED
Binary file (252 Bytes). View file
 
data/miniconda3/lib/python3.13/site-packages/conda_content_trust/__pycache__/__version__.cpython-313.pyc ADDED
Binary file (572 Bytes). View file
 
data/miniconda3/lib/python3.13/site-packages/conda_content_trust/__pycache__/authentication.cpython-313.pyc ADDED
Binary file (14.2 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/conda_content_trust/__pycache__/cli.cpython-313.pyc ADDED
Binary file (16.2 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/conda_content_trust/__pycache__/common.cpython-313.pyc ADDED
Binary file (32.3 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/conda_content_trust/__pycache__/metadata_construction.cpython-313.pyc ADDED
Binary file (5.48 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/conda_content_trust/__pycache__/plugin.cpython-313.pyc ADDED
Binary file (664 Bytes). View file
 
data/miniconda3/lib/python3.13/site-packages/conda_content_trust/__pycache__/root_signing.cpython-313.pyc ADDED
Binary file (11.3 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/conda_content_trust/__pycache__/signing.cpython-313.pyc ADDED
Binary file (6.12 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/cycler/__pycache__/__init__.cpython-313.pyc ADDED
Binary file (22.4 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/__init__.cpython-313.pyc ADDED
Binary file (2.18 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/duration.cpython-313.pyc ADDED
Binary file (12.4 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/isodates.cpython-313.pyc ADDED
Binary file (6.9 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/isodatetime.cpython-313.pyc ADDED
Binary file (1.84 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/isoduration.cpython-313.pyc ADDED
Binary file (5.36 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/isoerror.cpython-313.pyc ADDED
Binary file (566 Bytes). View file
 
data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/isostrf.cpython-313.pyc ADDED
Binary file (10.7 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/isotime.cpython-313.pyc ADDED
Binary file (5.13 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/isotzinfo.cpython-313.pyc ADDED
Binary file (3.53 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/tzinfo.cpython-313.pyc ADDED
Binary file (5.3 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/isodate/__pycache__/version.cpython-313.pyc ADDED
Binary file (582 Bytes). View file
 
data/miniconda3/lib/python3.13/site-packages/pyasn1-0.6.3.dist-info/licenses/LICENSE.rst ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Copyright (c) 2005-2020, Ilya Etingof <etingof@gmail.com>
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+
10
+ * Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24
+ POSSIBILITY OF SUCH DAMAGE.
data/miniconda3/lib/python3.13/site-packages/pyasn1/__pycache__/__init__.cpython-313.pyc ADDED
Binary file (196 Bytes). View file
 
data/miniconda3/lib/python3.13/site-packages/pyasn1/__pycache__/debug.cpython-313.pyc ADDED
Binary file (6.32 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/pyasn1/__pycache__/error.cpython-313.pyc ADDED
Binary file (4.6 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ # This file is necessary to make this directory a package.
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/__pycache__/__init__.cpython-313.pyc ADDED
Binary file (176 Bytes). View file
 
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/__pycache__/streaming.cpython-313.pyc ADDED
Binary file (8.31 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/ber/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ # This file is necessary to make this directory a package.
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/ber/__pycache__/__init__.cpython-313.pyc ADDED
Binary file (180 Bytes). View file
 
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/ber/__pycache__/decoder.cpython-313.pyc ADDED
Binary file (78 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/ber/__pycache__/encoder.cpython-313.pyc ADDED
Binary file (33.5 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/ber/__pycache__/eoo.cpython-313.pyc ADDED
Binary file (1.16 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/ber/decoder.py ADDED
@@ -0,0 +1,2225 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ #
7
+ import io
8
+ import os
9
+ import sys
10
+ import warnings
11
+
12
+ from pyasn1 import debug
13
+ from pyasn1 import error
14
+ from pyasn1.codec.ber import eoo
15
+ from pyasn1.codec.streaming import asSeekableStream
16
+ from pyasn1.codec.streaming import isEndOfStream
17
+ from pyasn1.codec.streaming import peekIntoStream
18
+ from pyasn1.codec.streaming import readFromStream
19
+ from pyasn1.compat import _MISSING
20
+ from pyasn1.error import PyAsn1Error
21
+ from pyasn1.type import base
22
+ from pyasn1.type import char
23
+ from pyasn1.type import tag
24
+ from pyasn1.type import tagmap
25
+ from pyasn1.type import univ
26
+ from pyasn1.type import useful
27
+
28
+ __all__ = ['StreamingDecoder', 'Decoder', 'decode']
29
+
30
+ LOG = debug.registerLoggee(__name__, flags=debug.DEBUG_DECODER)
31
+
32
+ noValue = base.noValue
33
+
34
+ SubstrateUnderrunError = error.SubstrateUnderrunError
35
+
36
+ # Maximum number of continuation octets (high-bit set) allowed per OID arc.
37
+ # 20 octets allows up to 140-bit integers, supporting UUID-based OIDs
38
+ MAX_OID_ARC_CONTINUATION_OCTETS = 20
39
+ MAX_NESTING_DEPTH = 100
40
+
41
+ # Maximum number of bytes in a BER length field (8 bytes = up to 2^64-1)
42
+ MAX_LENGTH_OCTETS = 8
43
+
44
+
45
+ class AbstractPayloadDecoder(object):
46
+ protoComponent = None
47
+
48
+ def valueDecoder(self, substrate, asn1Spec,
49
+ tagSet=None, length=None, state=None,
50
+ decodeFun=None, substrateFun=None,
51
+ **options):
52
+ """Decode value with fixed byte length.
53
+
54
+ The decoder is allowed to consume as many bytes as necessary.
55
+ """
56
+ raise error.PyAsn1Error('SingleItemDecoder not implemented for %s' % (tagSet,)) # TODO: Seems more like an NotImplementedError?
57
+
58
+ def indefLenValueDecoder(self, substrate, asn1Spec,
59
+ tagSet=None, length=None, state=None,
60
+ decodeFun=None, substrateFun=None,
61
+ **options):
62
+ """Decode value with undefined length.
63
+
64
+ The decoder is allowed to consume as many bytes as necessary.
65
+ """
66
+ raise error.PyAsn1Error('Indefinite length mode decoder not implemented for %s' % (tagSet,)) # TODO: Seems more like an NotImplementedError?
67
+
68
+ @staticmethod
69
+ def _passAsn1Object(asn1Object, options):
70
+ if 'asn1Object' not in options:
71
+ options['asn1Object'] = asn1Object
72
+
73
+ return options
74
+
75
+
76
+ class AbstractSimplePayloadDecoder(AbstractPayloadDecoder):
77
+ @staticmethod
78
+ def substrateCollector(asn1Object, substrate, length, options):
79
+ for chunk in readFromStream(substrate, length, options):
80
+ yield chunk
81
+
82
+ def _createComponent(self, asn1Spec, tagSet, value, **options):
83
+ if options.get('native'):
84
+ return value
85
+ elif asn1Spec is None:
86
+ return self.protoComponent.clone(value, tagSet=tagSet)
87
+ elif value is noValue:
88
+ return asn1Spec
89
+ else:
90
+ return asn1Spec.clone(value)
91
+
92
+
93
+ class RawPayloadDecoder(AbstractSimplePayloadDecoder):
94
+ protoComponent = univ.Any('')
95
+
96
+ def valueDecoder(self, substrate, asn1Spec,
97
+ tagSet=None, length=None, state=None,
98
+ decodeFun=None, substrateFun=None,
99
+ **options):
100
+ if substrateFun:
101
+ asn1Object = self._createComponent(asn1Spec, tagSet, '', **options)
102
+
103
+ for chunk in substrateFun(asn1Object, substrate, length, options):
104
+ yield chunk
105
+
106
+ return
107
+
108
+ for value in decodeFun(substrate, asn1Spec, tagSet, length, **options):
109
+ yield value
110
+
111
+ def indefLenValueDecoder(self, substrate, asn1Spec,
112
+ tagSet=None, length=None, state=None,
113
+ decodeFun=None, substrateFun=None,
114
+ **options):
115
+ if substrateFun:
116
+ asn1Object = self._createComponent(asn1Spec, tagSet, '', **options)
117
+
118
+ for chunk in substrateFun(asn1Object, substrate, length, options):
119
+ yield chunk
120
+
121
+ return
122
+
123
+ while True:
124
+ for value in decodeFun(
125
+ substrate, asn1Spec, tagSet, length,
126
+ allowEoo=True, **options):
127
+
128
+ if value is eoo.endOfOctets:
129
+ return
130
+
131
+ yield value
132
+
133
+
134
+ rawPayloadDecoder = RawPayloadDecoder()
135
+
136
+
137
+ class IntegerPayloadDecoder(AbstractSimplePayloadDecoder):
138
+ protoComponent = univ.Integer(0)
139
+
140
+ def valueDecoder(self, substrate, asn1Spec,
141
+ tagSet=None, length=None, state=None,
142
+ decodeFun=None, substrateFun=None,
143
+ **options):
144
+
145
+ if tagSet[0].tagFormat != tag.tagFormatSimple:
146
+ raise error.PyAsn1Error('Simple tag format expected')
147
+
148
+ for chunk in readFromStream(substrate, length, options):
149
+ if isinstance(chunk, SubstrateUnderrunError):
150
+ yield chunk
151
+
152
+ if chunk:
153
+ value = int.from_bytes(bytes(chunk), 'big', signed=True)
154
+
155
+ else:
156
+ value = 0
157
+
158
+ yield self._createComponent(asn1Spec, tagSet, value, **options)
159
+
160
+
161
+ class BooleanPayloadDecoder(IntegerPayloadDecoder):
162
+ protoComponent = univ.Boolean(0)
163
+
164
+ def _createComponent(self, asn1Spec, tagSet, value, **options):
165
+ return IntegerPayloadDecoder._createComponent(
166
+ self, asn1Spec, tagSet, value and 1 or 0, **options)
167
+
168
+
169
+ class BitStringPayloadDecoder(AbstractSimplePayloadDecoder):
170
+ protoComponent = univ.BitString(())
171
+ supportConstructedForm = True
172
+
173
+ def valueDecoder(self, substrate, asn1Spec,
174
+ tagSet=None, length=None, state=None,
175
+ decodeFun=None, substrateFun=None,
176
+ **options):
177
+
178
+ if substrateFun:
179
+ asn1Object = self._createComponent(asn1Spec, tagSet, noValue, **options)
180
+
181
+ for chunk in substrateFun(asn1Object, substrate, length, options):
182
+ yield chunk
183
+
184
+ return
185
+
186
+ if not length:
187
+ raise error.PyAsn1Error('Empty BIT STRING substrate')
188
+
189
+ for chunk in isEndOfStream(substrate):
190
+ if isinstance(chunk, SubstrateUnderrunError):
191
+ yield chunk
192
+
193
+ if chunk:
194
+ raise error.PyAsn1Error('Empty BIT STRING substrate')
195
+
196
+ if tagSet[0].tagFormat == tag.tagFormatSimple: # XXX what tag to check?
197
+
198
+ for trailingBits in readFromStream(substrate, 1, options):
199
+ if isinstance(trailingBits, SubstrateUnderrunError):
200
+ yield trailingBits
201
+
202
+ trailingBits = ord(trailingBits)
203
+ if trailingBits > 7:
204
+ raise error.PyAsn1Error(
205
+ 'Trailing bits overflow %s' % trailingBits
206
+ )
207
+
208
+ for chunk in readFromStream(substrate, length - 1, options):
209
+ if isinstance(chunk, SubstrateUnderrunError):
210
+ yield chunk
211
+
212
+ value = self.protoComponent.fromOctetString(
213
+ chunk, internalFormat=True, padding=trailingBits)
214
+
215
+ yield self._createComponent(asn1Spec, tagSet, value, **options)
216
+
217
+ return
218
+
219
+ if not self.supportConstructedForm:
220
+ raise error.PyAsn1Error('Constructed encoding form prohibited '
221
+ 'at %s' % self.__class__.__name__)
222
+
223
+ if LOG:
224
+ LOG('assembling constructed serialization')
225
+
226
+ # All inner fragments are of the same type, treat them as octet string
227
+ substrateFun = self.substrateCollector
228
+
229
+ bitString = self.protoComponent.fromOctetString(b'', internalFormat=True)
230
+
231
+ current_position = substrate.tell()
232
+
233
+ while substrate.tell() - current_position < length:
234
+ for component in decodeFun(
235
+ substrate, self.protoComponent, substrateFun=substrateFun,
236
+ **options):
237
+ if isinstance(component, SubstrateUnderrunError):
238
+ yield component
239
+
240
+ trailingBits = component[0]
241
+ if trailingBits > 7:
242
+ raise error.PyAsn1Error(
243
+ 'Trailing bits overflow %s' % trailingBits
244
+ )
245
+
246
+ bitString = self.protoComponent.fromOctetString(
247
+ component[1:], internalFormat=True,
248
+ prepend=bitString, padding=trailingBits
249
+ )
250
+
251
+ yield self._createComponent(asn1Spec, tagSet, bitString, **options)
252
+
253
+ def indefLenValueDecoder(self, substrate, asn1Spec,
254
+ tagSet=None, length=None, state=None,
255
+ decodeFun=None, substrateFun=None,
256
+ **options):
257
+
258
+ if substrateFun:
259
+ asn1Object = self._createComponent(asn1Spec, tagSet, noValue, **options)
260
+
261
+ for chunk in substrateFun(asn1Object, substrate, length, options):
262
+ yield chunk
263
+
264
+ return
265
+
266
+ # All inner fragments are of the same type, treat them as octet string
267
+ substrateFun = self.substrateCollector
268
+
269
+ bitString = self.protoComponent.fromOctetString(b'', internalFormat=True)
270
+
271
+ while True: # loop over fragments
272
+
273
+ for component in decodeFun(
274
+ substrate, self.protoComponent, substrateFun=substrateFun,
275
+ allowEoo=True, **options):
276
+
277
+ if component is eoo.endOfOctets:
278
+ break
279
+
280
+ if isinstance(component, SubstrateUnderrunError):
281
+ yield component
282
+
283
+ if component is eoo.endOfOctets:
284
+ break
285
+
286
+ trailingBits = component[0]
287
+ if trailingBits > 7:
288
+ raise error.PyAsn1Error(
289
+ 'Trailing bits overflow %s' % trailingBits
290
+ )
291
+
292
+ bitString = self.protoComponent.fromOctetString(
293
+ component[1:], internalFormat=True,
294
+ prepend=bitString, padding=trailingBits
295
+ )
296
+
297
+ yield self._createComponent(asn1Spec, tagSet, bitString, **options)
298
+
299
+
300
+ class OctetStringPayloadDecoder(AbstractSimplePayloadDecoder):
301
+ protoComponent = univ.OctetString('')
302
+ supportConstructedForm = True
303
+
304
+ def valueDecoder(self, substrate, asn1Spec,
305
+ tagSet=None, length=None, state=None,
306
+ decodeFun=None, substrateFun=None,
307
+ **options):
308
+ if substrateFun:
309
+ asn1Object = self._createComponent(asn1Spec, tagSet, noValue, **options)
310
+
311
+ for chunk in substrateFun(asn1Object, substrate, length, options):
312
+ yield chunk
313
+
314
+ return
315
+
316
+ if tagSet[0].tagFormat == tag.tagFormatSimple: # XXX what tag to check?
317
+ for chunk in readFromStream(substrate, length, options):
318
+ if isinstance(chunk, SubstrateUnderrunError):
319
+ yield chunk
320
+
321
+ yield self._createComponent(asn1Spec, tagSet, chunk, **options)
322
+
323
+ return
324
+
325
+ if not self.supportConstructedForm:
326
+ raise error.PyAsn1Error('Constructed encoding form prohibited at %s' % self.__class__.__name__)
327
+
328
+ if LOG:
329
+ LOG('assembling constructed serialization')
330
+
331
+ # All inner fragments are of the same type, treat them as octet string
332
+ substrateFun = self.substrateCollector
333
+
334
+ header = b''
335
+
336
+ original_position = substrate.tell()
337
+ # head = popSubstream(substrate, length)
338
+ while substrate.tell() - original_position < length:
339
+ for component in decodeFun(
340
+ substrate, self.protoComponent, substrateFun=substrateFun,
341
+ **options):
342
+ if isinstance(component, SubstrateUnderrunError):
343
+ yield component
344
+
345
+ header += component
346
+
347
+ yield self._createComponent(asn1Spec, tagSet, header, **options)
348
+
349
+ def indefLenValueDecoder(self, substrate, asn1Spec,
350
+ tagSet=None, length=None, state=None,
351
+ decodeFun=None, substrateFun=None,
352
+ **options):
353
+ if substrateFun and substrateFun is not self.substrateCollector:
354
+ asn1Object = self._createComponent(asn1Spec, tagSet, noValue, **options)
355
+
356
+ for chunk in substrateFun(asn1Object, substrate, length, options):
357
+ yield chunk
358
+
359
+ return
360
+
361
+ # All inner fragments are of the same type, treat them as octet string
362
+ substrateFun = self.substrateCollector
363
+
364
+ header = b''
365
+
366
+ while True: # loop over fragments
367
+
368
+ for component in decodeFun(
369
+ substrate, self.protoComponent, substrateFun=substrateFun,
370
+ allowEoo=True, **options):
371
+
372
+ if isinstance(component, SubstrateUnderrunError):
373
+ yield component
374
+
375
+ if component is eoo.endOfOctets:
376
+ break
377
+
378
+ if component is eoo.endOfOctets:
379
+ break
380
+
381
+ header += component
382
+
383
+ yield self._createComponent(asn1Spec, tagSet, header, **options)
384
+
385
+
386
+ class NullPayloadDecoder(AbstractSimplePayloadDecoder):
387
+ protoComponent = univ.Null('')
388
+
389
+ def valueDecoder(self, substrate, asn1Spec,
390
+ tagSet=None, length=None, state=None,
391
+ decodeFun=None, substrateFun=None,
392
+ **options):
393
+
394
+ if tagSet[0].tagFormat != tag.tagFormatSimple:
395
+ raise error.PyAsn1Error('Simple tag format expected')
396
+
397
+ for chunk in readFromStream(substrate, length, options):
398
+ if isinstance(chunk, SubstrateUnderrunError):
399
+ yield chunk
400
+
401
+ component = self._createComponent(asn1Spec, tagSet, '', **options)
402
+
403
+ if chunk:
404
+ raise error.PyAsn1Error('Unexpected %d-octet substrate for Null' % length)
405
+
406
+ yield component
407
+
408
+
409
+ class ObjectIdentifierPayloadDecoder(AbstractSimplePayloadDecoder):
410
+ protoComponent = univ.ObjectIdentifier(())
411
+
412
+ def valueDecoder(self, substrate, asn1Spec,
413
+ tagSet=None, length=None, state=None,
414
+ decodeFun=None, substrateFun=None,
415
+ **options):
416
+ if tagSet[0].tagFormat != tag.tagFormatSimple:
417
+ raise error.PyAsn1Error('Simple tag format expected')
418
+
419
+ for chunk in readFromStream(substrate, length, options):
420
+ if isinstance(chunk, SubstrateUnderrunError):
421
+ yield chunk
422
+
423
+ if not chunk:
424
+ raise error.PyAsn1Error('Empty substrate')
425
+
426
+ oid = ()
427
+ index = 0
428
+ substrateLen = len(chunk)
429
+ while index < substrateLen:
430
+ subId = chunk[index]
431
+ index += 1
432
+ if subId < 128:
433
+ oid += (subId,)
434
+ elif subId > 128:
435
+ # Construct subid from a number of octets
436
+ nextSubId = subId
437
+ subId = 0
438
+ continuationOctetCount = 0
439
+ while nextSubId >= 128:
440
+ continuationOctetCount += 1
441
+ if continuationOctetCount > MAX_OID_ARC_CONTINUATION_OCTETS:
442
+ raise error.PyAsn1Error(
443
+ 'OID arc exceeds maximum continuation octets limit (%d) '
444
+ 'at position %d' % (MAX_OID_ARC_CONTINUATION_OCTETS, index)
445
+ )
446
+ subId = (subId << 7) + (nextSubId & 0x7F)
447
+ if index >= substrateLen:
448
+ raise error.SubstrateUnderrunError(
449
+ 'Short substrate for sub-OID past %s' % (oid,)
450
+ )
451
+ nextSubId = chunk[index]
452
+ index += 1
453
+ oid += ((subId << 7) + nextSubId,)
454
+ elif subId == 128:
455
+ # ASN.1 spec forbids leading zeros (0x80) in OID
456
+ # encoding, tolerating it opens a vulnerability. See
457
+ # https://www.esat.kuleuven.be/cosic/publications/article-1432.pdf
458
+ # page 7
459
+ raise error.PyAsn1Error('Invalid octet 0x80 in OID encoding')
460
+
461
+ # Decode two leading arcs
462
+ if 0 <= oid[0] <= 39:
463
+ oid = (0,) + oid
464
+ elif 40 <= oid[0] <= 79:
465
+ oid = (1, oid[0] - 40) + oid[1:]
466
+ elif oid[0] >= 80:
467
+ oid = (2, oid[0] - 80) + oid[1:]
468
+ else:
469
+ raise error.PyAsn1Error('Malformed first OID octet: %s' % chunk[0])
470
+
471
+ yield self._createComponent(asn1Spec, tagSet, oid, **options)
472
+
473
+
474
+ class RelativeOIDPayloadDecoder(AbstractSimplePayloadDecoder):
475
+ protoComponent = univ.RelativeOID(())
476
+
477
+ def valueDecoder(self, substrate, asn1Spec,
478
+ tagSet=None, length=None, state=None,
479
+ decodeFun=None, substrateFun=None,
480
+ **options):
481
+ if tagSet[0].tagFormat != tag.tagFormatSimple:
482
+ raise error.PyAsn1Error('Simple tag format expected')
483
+
484
+ for chunk in readFromStream(substrate, length, options):
485
+ if isinstance(chunk, SubstrateUnderrunError):
486
+ yield chunk
487
+
488
+ if not chunk:
489
+ raise error.PyAsn1Error('Empty substrate')
490
+
491
+ reloid = ()
492
+ index = 0
493
+ substrateLen = len(chunk)
494
+ while index < substrateLen:
495
+ subId = chunk[index]
496
+ index += 1
497
+ if subId < 128:
498
+ reloid += (subId,)
499
+ elif subId > 128:
500
+ # Construct subid from a number of octets
501
+ nextSubId = subId
502
+ subId = 0
503
+ continuationOctetCount = 0
504
+ while nextSubId >= 128:
505
+ continuationOctetCount += 1
506
+ if continuationOctetCount > MAX_OID_ARC_CONTINUATION_OCTETS:
507
+ raise error.PyAsn1Error(
508
+ 'RELATIVE-OID arc exceeds maximum continuation octets limit (%d) '
509
+ 'at position %d' % (MAX_OID_ARC_CONTINUATION_OCTETS, index)
510
+ )
511
+ subId = (subId << 7) + (nextSubId & 0x7F)
512
+ if index >= substrateLen:
513
+ raise error.SubstrateUnderrunError(
514
+ 'Short substrate for sub-OID past %s' % (reloid,)
515
+ )
516
+ nextSubId = chunk[index]
517
+ index += 1
518
+ reloid += ((subId << 7) + nextSubId,)
519
+ elif subId == 128:
520
+ # ASN.1 spec forbids leading zeros (0x80) in OID
521
+ # encoding, tolerating it opens a vulnerability. See
522
+ # https://www.esat.kuleuven.be/cosic/publications/article-1432.pdf
523
+ # page 7
524
+ raise error.PyAsn1Error('Invalid octet 0x80 in RELATIVE-OID encoding')
525
+
526
+ yield self._createComponent(asn1Spec, tagSet, reloid, **options)
527
+
528
+
529
+ class RealPayloadDecoder(AbstractSimplePayloadDecoder):
530
+ protoComponent = univ.Real()
531
+
532
+ def valueDecoder(self, substrate, asn1Spec,
533
+ tagSet=None, length=None, state=None,
534
+ decodeFun=None, substrateFun=None,
535
+ **options):
536
+ if tagSet[0].tagFormat != tag.tagFormatSimple:
537
+ raise error.PyAsn1Error('Simple tag format expected')
538
+
539
+ for chunk in readFromStream(substrate, length, options):
540
+ if isinstance(chunk, SubstrateUnderrunError):
541
+ yield chunk
542
+
543
+ if not chunk:
544
+ yield self._createComponent(asn1Spec, tagSet, 0.0, **options)
545
+ return
546
+
547
+ fo = chunk[0]
548
+ chunk = chunk[1:]
549
+ if fo & 0x80: # binary encoding
550
+ if not chunk:
551
+ raise error.PyAsn1Error("Incomplete floating-point value")
552
+
553
+ if LOG:
554
+ LOG('decoding binary encoded REAL')
555
+
556
+ n = (fo & 0x03) + 1
557
+
558
+ if n == 4:
559
+ n = chunk[0]
560
+ chunk = chunk[1:]
561
+
562
+ eo, chunk = chunk[:n], chunk[n:]
563
+
564
+ if not eo or not chunk:
565
+ raise error.PyAsn1Error('Real exponent screwed')
566
+
567
+ e = eo[0] & 0x80 and -1 or 0
568
+
569
+ while eo: # exponent
570
+ e <<= 8
571
+ e |= eo[0]
572
+ eo = eo[1:]
573
+
574
+ b = fo >> 4 & 0x03 # base bits
575
+
576
+ if b > 2:
577
+ raise error.PyAsn1Error('Illegal Real base')
578
+
579
+ if b == 1: # encbase = 8
580
+ e *= 3
581
+
582
+ elif b == 2: # encbase = 16
583
+ e *= 4
584
+ p = 0
585
+
586
+ while chunk: # value
587
+ p <<= 8
588
+ p |= chunk[0]
589
+ chunk = chunk[1:]
590
+
591
+ if fo & 0x40: # sign bit
592
+ p = -p
593
+
594
+ sf = fo >> 2 & 0x03 # scale bits
595
+ p *= 2 ** sf
596
+ value = (p, 2, e)
597
+
598
+ elif fo & 0x40: # infinite value
599
+ if LOG:
600
+ LOG('decoding infinite REAL')
601
+
602
+ value = fo & 0x01 and '-inf' or 'inf'
603
+
604
+ elif fo & 0xc0 == 0: # character encoding
605
+ if not chunk:
606
+ raise error.PyAsn1Error("Incomplete floating-point value")
607
+
608
+ if LOG:
609
+ LOG('decoding character encoded REAL')
610
+
611
+ try:
612
+ if fo & 0x3 == 0x1: # NR1
613
+ value = (int(chunk), 10, 0)
614
+
615
+ elif fo & 0x3 == 0x2: # NR2
616
+ value = float(chunk)
617
+
618
+ elif fo & 0x3 == 0x3: # NR3
619
+ value = float(chunk)
620
+
621
+ else:
622
+ raise error.SubstrateUnderrunError(
623
+ 'Unknown NR (tag %s)' % fo
624
+ )
625
+
626
+ except ValueError:
627
+ raise error.SubstrateUnderrunError(
628
+ 'Bad character Real syntax'
629
+ )
630
+
631
+ else:
632
+ raise error.SubstrateUnderrunError(
633
+ 'Unknown encoding (tag %s)' % fo
634
+ )
635
+
636
+ yield self._createComponent(asn1Spec, tagSet, value, **options)
637
+
638
+
639
+ class AbstractConstructedPayloadDecoder(AbstractPayloadDecoder):
640
+ protoComponent = None
641
+
642
+
643
+ class ConstructedPayloadDecoderBase(AbstractConstructedPayloadDecoder):
644
+ protoRecordComponent = None
645
+ protoSequenceComponent = None
646
+
647
+ def _getComponentTagMap(self, asn1Object, idx):
648
+ raise NotImplementedError
649
+
650
+ def _getComponentPositionByType(self, asn1Object, tagSet, idx):
651
+ raise NotImplementedError
652
+
653
+ def _decodeComponentsSchemaless(
654
+ self, substrate, tagSet=None, decodeFun=None,
655
+ length=None, **options):
656
+
657
+ asn1Object = None
658
+
659
+ components = []
660
+ componentTypes = set()
661
+
662
+ original_position = substrate.tell()
663
+
664
+ while length == -1 or substrate.tell() < original_position + length:
665
+ for component in decodeFun(substrate, **options):
666
+ if isinstance(component, SubstrateUnderrunError):
667
+ yield component
668
+
669
+ if length == -1 and component is eoo.endOfOctets:
670
+ break
671
+
672
+ components.append(component)
673
+ componentTypes.add(component.tagSet)
674
+
675
+ # Now we have to guess is it SEQUENCE/SET or SEQUENCE OF/SET OF
676
+ # The heuristics is:
677
+ # * 1+ components of different types -> likely SEQUENCE/SET
678
+ # * otherwise -> likely SEQUENCE OF/SET OF
679
+ if len(componentTypes) > 1:
680
+ protoComponent = self.protoRecordComponent
681
+
682
+ else:
683
+ protoComponent = self.protoSequenceComponent
684
+
685
+ asn1Object = protoComponent.clone(
686
+ # construct tagSet from base tag from prototype ASN.1 object
687
+ # and additional tags recovered from the substrate
688
+ tagSet=tag.TagSet(protoComponent.tagSet.baseTag, *tagSet.superTags)
689
+ )
690
+
691
+ if LOG:
692
+ LOG('guessed %r container type (pass `asn1Spec` to guide the '
693
+ 'decoder)' % asn1Object)
694
+
695
+ for idx, component in enumerate(components):
696
+ asn1Object.setComponentByPosition(
697
+ idx, component,
698
+ verifyConstraints=False,
699
+ matchTags=False, matchConstraints=False
700
+ )
701
+
702
+ yield asn1Object
703
+
704
+ def valueDecoder(self, substrate, asn1Spec,
705
+ tagSet=None, length=None, state=None,
706
+ decodeFun=None, substrateFun=None,
707
+ **options):
708
+ if tagSet[0].tagFormat != tag.tagFormatConstructed:
709
+ raise error.PyAsn1Error('Constructed tag format expected')
710
+
711
+ original_position = substrate.tell()
712
+
713
+ if substrateFun:
714
+ if asn1Spec is not None:
715
+ asn1Object = asn1Spec.clone()
716
+
717
+ elif self.protoComponent is not None:
718
+ asn1Object = self.protoComponent.clone(tagSet=tagSet)
719
+
720
+ else:
721
+ asn1Object = self.protoRecordComponent, self.protoSequenceComponent
722
+
723
+ for chunk in substrateFun(asn1Object, substrate, length, options):
724
+ yield chunk
725
+
726
+ return
727
+
728
+ if asn1Spec is None:
729
+ for asn1Object in self._decodeComponentsSchemaless(
730
+ substrate, tagSet=tagSet, decodeFun=decodeFun,
731
+ length=length, **options):
732
+ if isinstance(asn1Object, SubstrateUnderrunError):
733
+ yield asn1Object
734
+
735
+ if substrate.tell() < original_position + length:
736
+ if LOG:
737
+ for trailing in readFromStream(substrate, context=options):
738
+ if isinstance(trailing, SubstrateUnderrunError):
739
+ yield trailing
740
+
741
+ LOG('Unused trailing %d octets encountered: %s' % (
742
+ len(trailing), debug.hexdump(trailing)))
743
+
744
+ yield asn1Object
745
+
746
+ return
747
+
748
+ asn1Object = asn1Spec.clone()
749
+ asn1Object.clear()
750
+
751
+ options = self._passAsn1Object(asn1Object, options)
752
+
753
+ if asn1Spec.typeId in (univ.Sequence.typeId, univ.Set.typeId):
754
+
755
+ namedTypes = asn1Spec.componentType
756
+
757
+ isSetType = asn1Spec.typeId == univ.Set.typeId
758
+ isDeterministic = not isSetType and not namedTypes.hasOptionalOrDefault
759
+
760
+ if LOG:
761
+ LOG('decoding %sdeterministic %s type %r chosen by type ID' % (
762
+ not isDeterministic and 'non-' or '', isSetType and 'SET' or '',
763
+ asn1Spec))
764
+
765
+ seenIndices = set()
766
+ idx = 0
767
+ while substrate.tell() - original_position < length:
768
+ if not namedTypes:
769
+ componentType = None
770
+
771
+ elif isSetType:
772
+ componentType = namedTypes.tagMapUnique
773
+
774
+ else:
775
+ try:
776
+ if isDeterministic:
777
+ componentType = namedTypes[idx].asn1Object
778
+
779
+ elif namedTypes[idx].isOptional or namedTypes[idx].isDefaulted:
780
+ componentType = namedTypes.getTagMapNearPosition(idx)
781
+
782
+ else:
783
+ componentType = namedTypes[idx].asn1Object
784
+
785
+ except IndexError:
786
+ raise error.PyAsn1Error(
787
+ 'Excessive components decoded at %r' % (asn1Spec,)
788
+ )
789
+
790
+ for component in decodeFun(substrate, componentType, **options):
791
+ if isinstance(component, SubstrateUnderrunError):
792
+ yield component
793
+
794
+ if not isDeterministic and namedTypes:
795
+ if isSetType:
796
+ idx = namedTypes.getPositionByType(component.effectiveTagSet)
797
+
798
+ elif namedTypes[idx].isOptional or namedTypes[idx].isDefaulted:
799
+ idx = namedTypes.getPositionNearType(component.effectiveTagSet, idx)
800
+
801
+ asn1Object.setComponentByPosition(
802
+ idx, component,
803
+ verifyConstraints=False,
804
+ matchTags=False, matchConstraints=False
805
+ )
806
+
807
+ seenIndices.add(idx)
808
+ idx += 1
809
+
810
+ if LOG:
811
+ LOG('seen component indices %s' % seenIndices)
812
+
813
+ if namedTypes:
814
+ if not namedTypes.requiredComponents.issubset(seenIndices):
815
+ raise error.PyAsn1Error(
816
+ 'ASN.1 object %s has uninitialized '
817
+ 'components' % asn1Object.__class__.__name__)
818
+
819
+ if namedTypes.hasOpenTypes:
820
+
821
+ openTypes = options.get('openTypes', {})
822
+
823
+ if LOG:
824
+ LOG('user-specified open types map:')
825
+
826
+ for k, v in openTypes.items():
827
+ LOG('%s -> %r' % (k, v))
828
+
829
+ if openTypes or options.get('decodeOpenTypes', False):
830
+
831
+ for idx, namedType in enumerate(namedTypes.namedTypes):
832
+ if not namedType.openType:
833
+ continue
834
+
835
+ if namedType.isOptional and not asn1Object.getComponentByPosition(idx).isValue:
836
+ continue
837
+
838
+ governingValue = asn1Object.getComponentByName(
839
+ namedType.openType.name
840
+ )
841
+
842
+ try:
843
+ openType = openTypes[governingValue]
844
+
845
+ except KeyError:
846
+
847
+ if LOG:
848
+ LOG('default open types map of component '
849
+ '"%s.%s" governed by component "%s.%s"'
850
+ ':' % (asn1Object.__class__.__name__,
851
+ namedType.name,
852
+ asn1Object.__class__.__name__,
853
+ namedType.openType.name))
854
+
855
+ for k, v in namedType.openType.items():
856
+ LOG('%s -> %r' % (k, v))
857
+
858
+ try:
859
+ openType = namedType.openType[governingValue]
860
+
861
+ except KeyError:
862
+ if LOG:
863
+ LOG('failed to resolve open type by governing '
864
+ 'value %r' % (governingValue,))
865
+ continue
866
+
867
+ if LOG:
868
+ LOG('resolved open type %r by governing '
869
+ 'value %r' % (openType, governingValue))
870
+
871
+ containerValue = asn1Object.getComponentByPosition(idx)
872
+
873
+ if containerValue.typeId in (
874
+ univ.SetOf.typeId, univ.SequenceOf.typeId):
875
+
876
+ for pos, containerElement in enumerate(
877
+ containerValue):
878
+
879
+ stream = asSeekableStream(containerValue[pos].asOctets())
880
+
881
+ for component in decodeFun(stream, asn1Spec=openType, **options):
882
+ if isinstance(component, SubstrateUnderrunError):
883
+ yield component
884
+
885
+ containerValue[pos] = component
886
+
887
+ else:
888
+ stream = asSeekableStream(asn1Object.getComponentByPosition(idx).asOctets())
889
+
890
+ for component in decodeFun(stream, asn1Spec=openType, **options):
891
+ if isinstance(component, SubstrateUnderrunError):
892
+ yield component
893
+
894
+ asn1Object.setComponentByPosition(idx, component)
895
+
896
+ else:
897
+ inconsistency = asn1Object.isInconsistent
898
+ if inconsistency:
899
+ raise error.PyAsn1Error(
900
+ f"ASN.1 object {asn1Object.__class__.__name__} is inconsistent")
901
+
902
+ else:
903
+ componentType = asn1Spec.componentType
904
+
905
+ if LOG:
906
+ LOG('decoding type %r chosen by given `asn1Spec`' % componentType)
907
+
908
+ idx = 0
909
+
910
+ while substrate.tell() - original_position < length:
911
+ for component in decodeFun(substrate, componentType, **options):
912
+ if isinstance(component, SubstrateUnderrunError):
913
+ yield component
914
+
915
+ asn1Object.setComponentByPosition(
916
+ idx, component,
917
+ verifyConstraints=False,
918
+ matchTags=False, matchConstraints=False
919
+ )
920
+
921
+ idx += 1
922
+
923
+ yield asn1Object
924
+
925
+ def indefLenValueDecoder(self, substrate, asn1Spec,
926
+ tagSet=None, length=None, state=None,
927
+ decodeFun=None, substrateFun=None,
928
+ **options):
929
+ if tagSet[0].tagFormat != tag.tagFormatConstructed:
930
+ raise error.PyAsn1Error('Constructed tag format expected')
931
+
932
+ if substrateFun is not None:
933
+ if asn1Spec is not None:
934
+ asn1Object = asn1Spec.clone()
935
+
936
+ elif self.protoComponent is not None:
937
+ asn1Object = self.protoComponent.clone(tagSet=tagSet)
938
+
939
+ else:
940
+ asn1Object = self.protoRecordComponent, self.protoSequenceComponent
941
+
942
+ for chunk in substrateFun(asn1Object, substrate, length, options):
943
+ yield chunk
944
+
945
+ return
946
+
947
+ if asn1Spec is None:
948
+ for asn1Object in self._decodeComponentsSchemaless(
949
+ substrate, tagSet=tagSet, decodeFun=decodeFun,
950
+ length=length, **dict(options, allowEoo=True)):
951
+ if isinstance(asn1Object, SubstrateUnderrunError):
952
+ yield asn1Object
953
+
954
+ yield asn1Object
955
+
956
+ return
957
+
958
+ asn1Object = asn1Spec.clone()
959
+ asn1Object.clear()
960
+
961
+ options = self._passAsn1Object(asn1Object, options)
962
+
963
+ if asn1Spec.typeId in (univ.Sequence.typeId, univ.Set.typeId):
964
+
965
+ namedTypes = asn1Object.componentType
966
+
967
+ isSetType = asn1Object.typeId == univ.Set.typeId
968
+ isDeterministic = not isSetType and not namedTypes.hasOptionalOrDefault
969
+
970
+ if LOG:
971
+ LOG('decoding %sdeterministic %s type %r chosen by type ID' % (
972
+ not isDeterministic and 'non-' or '', isSetType and 'SET' or '',
973
+ asn1Spec))
974
+
975
+ seenIndices = set()
976
+
977
+ idx = 0
978
+
979
+ while True: # loop over components
980
+ if len(namedTypes) <= idx:
981
+ asn1Spec = None
982
+
983
+ elif isSetType:
984
+ asn1Spec = namedTypes.tagMapUnique
985
+
986
+ else:
987
+ try:
988
+ if isDeterministic:
989
+ asn1Spec = namedTypes[idx].asn1Object
990
+
991
+ elif namedTypes[idx].isOptional or namedTypes[idx].isDefaulted:
992
+ asn1Spec = namedTypes.getTagMapNearPosition(idx)
993
+
994
+ else:
995
+ asn1Spec = namedTypes[idx].asn1Object
996
+
997
+ except IndexError:
998
+ raise error.PyAsn1Error(
999
+ 'Excessive components decoded at %r' % (asn1Object,)
1000
+ )
1001
+
1002
+ for component in decodeFun(substrate, asn1Spec, allowEoo=True, **options):
1003
+
1004
+ if isinstance(component, SubstrateUnderrunError):
1005
+ yield component
1006
+
1007
+ if component is eoo.endOfOctets:
1008
+ break
1009
+
1010
+ if component is eoo.endOfOctets:
1011
+ break
1012
+
1013
+ if not isDeterministic and namedTypes:
1014
+ if isSetType:
1015
+ idx = namedTypes.getPositionByType(component.effectiveTagSet)
1016
+
1017
+ elif namedTypes[idx].isOptional or namedTypes[idx].isDefaulted:
1018
+ idx = namedTypes.getPositionNearType(component.effectiveTagSet, idx)
1019
+
1020
+ asn1Object.setComponentByPosition(
1021
+ idx, component,
1022
+ verifyConstraints=False,
1023
+ matchTags=False, matchConstraints=False
1024
+ )
1025
+
1026
+ seenIndices.add(idx)
1027
+ idx += 1
1028
+
1029
+ if LOG:
1030
+ LOG('seen component indices %s' % seenIndices)
1031
+
1032
+ if namedTypes:
1033
+ if not namedTypes.requiredComponents.issubset(seenIndices):
1034
+ raise error.PyAsn1Error(
1035
+ 'ASN.1 object %s has uninitialized '
1036
+ 'components' % asn1Object.__class__.__name__)
1037
+
1038
+ if namedTypes.hasOpenTypes:
1039
+
1040
+ openTypes = options.get('openTypes', {})
1041
+
1042
+ if LOG:
1043
+ LOG('user-specified open types map:')
1044
+
1045
+ for k, v in openTypes.items():
1046
+ LOG('%s -> %r' % (k, v))
1047
+
1048
+ if openTypes or options.get('decodeOpenTypes', False):
1049
+
1050
+ for idx, namedType in enumerate(namedTypes.namedTypes):
1051
+ if not namedType.openType:
1052
+ continue
1053
+
1054
+ if namedType.isOptional and not asn1Object.getComponentByPosition(idx).isValue:
1055
+ continue
1056
+
1057
+ governingValue = asn1Object.getComponentByName(
1058
+ namedType.openType.name
1059
+ )
1060
+
1061
+ try:
1062
+ openType = openTypes[governingValue]
1063
+
1064
+ except KeyError:
1065
+
1066
+ if LOG:
1067
+ LOG('default open types map of component '
1068
+ '"%s.%s" governed by component "%s.%s"'
1069
+ ':' % (asn1Object.__class__.__name__,
1070
+ namedType.name,
1071
+ asn1Object.__class__.__name__,
1072
+ namedType.openType.name))
1073
+
1074
+ for k, v in namedType.openType.items():
1075
+ LOG('%s -> %r' % (k, v))
1076
+
1077
+ try:
1078
+ openType = namedType.openType[governingValue]
1079
+
1080
+ except KeyError:
1081
+ if LOG:
1082
+ LOG('failed to resolve open type by governing '
1083
+ 'value %r' % (governingValue,))
1084
+ continue
1085
+
1086
+ if LOG:
1087
+ LOG('resolved open type %r by governing '
1088
+ 'value %r' % (openType, governingValue))
1089
+
1090
+ containerValue = asn1Object.getComponentByPosition(idx)
1091
+
1092
+ if containerValue.typeId in (
1093
+ univ.SetOf.typeId, univ.SequenceOf.typeId):
1094
+
1095
+ for pos, containerElement in enumerate(
1096
+ containerValue):
1097
+
1098
+ stream = asSeekableStream(containerValue[pos].asOctets())
1099
+
1100
+ for component in decodeFun(stream, asn1Spec=openType,
1101
+ **dict(options, allowEoo=True)):
1102
+ if isinstance(component, SubstrateUnderrunError):
1103
+ yield component
1104
+
1105
+ if component is eoo.endOfOctets:
1106
+ break
1107
+
1108
+ containerValue[pos] = component
1109
+
1110
+ else:
1111
+ stream = asSeekableStream(asn1Object.getComponentByPosition(idx).asOctets())
1112
+ for component in decodeFun(stream, asn1Spec=openType,
1113
+ **dict(options, allowEoo=True)):
1114
+ if isinstance(component, SubstrateUnderrunError):
1115
+ yield component
1116
+
1117
+ if component is eoo.endOfOctets:
1118
+ break
1119
+
1120
+ asn1Object.setComponentByPosition(idx, component)
1121
+
1122
+ else:
1123
+ inconsistency = asn1Object.isInconsistent
1124
+ if inconsistency:
1125
+ raise error.PyAsn1Error(
1126
+ f"ASN.1 object {asn1Object.__class__.__name__} is inconsistent")
1127
+
1128
+ else:
1129
+ componentType = asn1Spec.componentType
1130
+
1131
+ if LOG:
1132
+ LOG('decoding type %r chosen by given `asn1Spec`' % componentType)
1133
+
1134
+ idx = 0
1135
+
1136
+ while True:
1137
+
1138
+ for component in decodeFun(
1139
+ substrate, componentType, allowEoo=True, **options):
1140
+
1141
+ if isinstance(component, SubstrateUnderrunError):
1142
+ yield component
1143
+
1144
+ if component is eoo.endOfOctets:
1145
+ break
1146
+
1147
+ if component is eoo.endOfOctets:
1148
+ break
1149
+
1150
+ asn1Object.setComponentByPosition(
1151
+ idx, component,
1152
+ verifyConstraints=False,
1153
+ matchTags=False, matchConstraints=False
1154
+ )
1155
+
1156
+ idx += 1
1157
+
1158
+ yield asn1Object
1159
+
1160
+
1161
+ class SequenceOrSequenceOfPayloadDecoder(ConstructedPayloadDecoderBase):
1162
+ protoRecordComponent = univ.Sequence()
1163
+ protoSequenceComponent = univ.SequenceOf()
1164
+
1165
+
1166
+ class SequencePayloadDecoder(SequenceOrSequenceOfPayloadDecoder):
1167
+ protoComponent = univ.Sequence()
1168
+
1169
+
1170
+ class SequenceOfPayloadDecoder(SequenceOrSequenceOfPayloadDecoder):
1171
+ protoComponent = univ.SequenceOf()
1172
+
1173
+
1174
+ class SetOrSetOfPayloadDecoder(ConstructedPayloadDecoderBase):
1175
+ protoRecordComponent = univ.Set()
1176
+ protoSequenceComponent = univ.SetOf()
1177
+
1178
+
1179
+ class SetPayloadDecoder(SetOrSetOfPayloadDecoder):
1180
+ protoComponent = univ.Set()
1181
+
1182
+
1183
+ class SetOfPayloadDecoder(SetOrSetOfPayloadDecoder):
1184
+ protoComponent = univ.SetOf()
1185
+
1186
+
1187
+ class ChoicePayloadDecoder(ConstructedPayloadDecoderBase):
1188
+ protoComponent = univ.Choice()
1189
+
1190
+ def valueDecoder(self, substrate, asn1Spec,
1191
+ tagSet=None, length=None, state=None,
1192
+ decodeFun=None, substrateFun=None,
1193
+ **options):
1194
+ if asn1Spec is None:
1195
+ asn1Object = self.protoComponent.clone(tagSet=tagSet)
1196
+
1197
+ else:
1198
+ asn1Object = asn1Spec.clone()
1199
+
1200
+ if substrateFun:
1201
+ for chunk in substrateFun(asn1Object, substrate, length, options):
1202
+ yield chunk
1203
+
1204
+ return
1205
+
1206
+ options = self._passAsn1Object(asn1Object, options)
1207
+
1208
+ if asn1Object.tagSet == tagSet:
1209
+ if LOG:
1210
+ LOG('decoding %s as explicitly tagged CHOICE' % (tagSet,))
1211
+
1212
+ for component in decodeFun(
1213
+ substrate, asn1Object.componentTagMap, **options):
1214
+ if isinstance(component, SubstrateUnderrunError):
1215
+ yield component
1216
+
1217
+ else:
1218
+ if LOG:
1219
+ LOG('decoding %s as untagged CHOICE' % (tagSet,))
1220
+
1221
+ for component in decodeFun(
1222
+ substrate, asn1Object.componentTagMap, tagSet, length,
1223
+ state, **options):
1224
+ if isinstance(component, SubstrateUnderrunError):
1225
+ yield component
1226
+
1227
+ effectiveTagSet = component.effectiveTagSet
1228
+
1229
+ if LOG:
1230
+ LOG('decoded component %s, effective tag set %s' % (component, effectiveTagSet))
1231
+
1232
+ asn1Object.setComponentByType(
1233
+ effectiveTagSet, component,
1234
+ verifyConstraints=False,
1235
+ matchTags=False, matchConstraints=False,
1236
+ innerFlag=False
1237
+ )
1238
+
1239
+ yield asn1Object
1240
+
1241
+ def indefLenValueDecoder(self, substrate, asn1Spec,
1242
+ tagSet=None, length=None, state=None,
1243
+ decodeFun=None, substrateFun=None,
1244
+ **options):
1245
+ if asn1Spec is None:
1246
+ asn1Object = self.protoComponent.clone(tagSet=tagSet)
1247
+
1248
+ else:
1249
+ asn1Object = asn1Spec.clone()
1250
+
1251
+ if substrateFun:
1252
+ for chunk in substrateFun(asn1Object, substrate, length, options):
1253
+ yield chunk
1254
+
1255
+ return
1256
+
1257
+ options = self._passAsn1Object(asn1Object, options)
1258
+
1259
+ isTagged = asn1Object.tagSet == tagSet
1260
+
1261
+ if LOG:
1262
+ LOG('decoding %s as %stagged CHOICE' % (
1263
+ tagSet, isTagged and 'explicitly ' or 'un'))
1264
+
1265
+ while True:
1266
+
1267
+ if isTagged:
1268
+ iterator = decodeFun(
1269
+ substrate, asn1Object.componentType.tagMapUnique,
1270
+ **dict(options, allowEoo=True))
1271
+
1272
+ else:
1273
+ iterator = decodeFun(
1274
+ substrate, asn1Object.componentType.tagMapUnique,
1275
+ tagSet, length, state, **dict(options, allowEoo=True))
1276
+
1277
+ for component in iterator:
1278
+
1279
+ if isinstance(component, SubstrateUnderrunError):
1280
+ yield component
1281
+
1282
+ if component is eoo.endOfOctets:
1283
+ break
1284
+
1285
+ effectiveTagSet = component.effectiveTagSet
1286
+
1287
+ if LOG:
1288
+ LOG('decoded component %s, effective tag set '
1289
+ '%s' % (component, effectiveTagSet))
1290
+
1291
+ asn1Object.setComponentByType(
1292
+ effectiveTagSet, component,
1293
+ verifyConstraints=False,
1294
+ matchTags=False, matchConstraints=False,
1295
+ innerFlag=False
1296
+ )
1297
+
1298
+ if not isTagged:
1299
+ break
1300
+
1301
+ if not isTagged or component is eoo.endOfOctets:
1302
+ break
1303
+
1304
+ yield asn1Object
1305
+
1306
+
1307
+ class AnyPayloadDecoder(AbstractSimplePayloadDecoder):
1308
+ protoComponent = univ.Any()
1309
+
1310
+ def valueDecoder(self, substrate, asn1Spec,
1311
+ tagSet=None, length=None, state=None,
1312
+ decodeFun=None, substrateFun=None,
1313
+ **options):
1314
+ if asn1Spec is None:
1315
+ isUntagged = True
1316
+
1317
+ elif asn1Spec.__class__ is tagmap.TagMap:
1318
+ isUntagged = tagSet not in asn1Spec.tagMap
1319
+
1320
+ else:
1321
+ isUntagged = tagSet != asn1Spec.tagSet
1322
+
1323
+ if isUntagged:
1324
+ fullPosition = substrate.markedPosition
1325
+ currentPosition = substrate.tell()
1326
+
1327
+ substrate.seek(fullPosition, os.SEEK_SET)
1328
+ length += currentPosition - fullPosition
1329
+
1330
+ if LOG:
1331
+ for chunk in peekIntoStream(substrate, length):
1332
+ if isinstance(chunk, SubstrateUnderrunError):
1333
+ yield chunk
1334
+ LOG('decoding as untagged ANY, substrate '
1335
+ '%s' % debug.hexdump(chunk))
1336
+
1337
+ if substrateFun:
1338
+ for chunk in substrateFun(
1339
+ self._createComponent(asn1Spec, tagSet, noValue, **options),
1340
+ substrate, length, options):
1341
+ yield chunk
1342
+
1343
+ return
1344
+
1345
+ for chunk in readFromStream(substrate, length, options):
1346
+ if isinstance(chunk, SubstrateUnderrunError):
1347
+ yield chunk
1348
+
1349
+ yield self._createComponent(asn1Spec, tagSet, chunk, **options)
1350
+
1351
+ def indefLenValueDecoder(self, substrate, asn1Spec,
1352
+ tagSet=None, length=None, state=None,
1353
+ decodeFun=None, substrateFun=None,
1354
+ **options):
1355
+ if asn1Spec is None:
1356
+ isTagged = False
1357
+
1358
+ elif asn1Spec.__class__ is tagmap.TagMap:
1359
+ isTagged = tagSet in asn1Spec.tagMap
1360
+
1361
+ else:
1362
+ isTagged = tagSet == asn1Spec.tagSet
1363
+
1364
+ if isTagged:
1365
+ # tagged Any type -- consume header substrate
1366
+ chunk = b''
1367
+
1368
+ if LOG:
1369
+ LOG('decoding as tagged ANY')
1370
+
1371
+ else:
1372
+ # TODO: Seems not to be tested
1373
+ fullPosition = substrate.markedPosition
1374
+ currentPosition = substrate.tell()
1375
+
1376
+ substrate.seek(fullPosition, os.SEEK_SET)
1377
+ for chunk in readFromStream(substrate, currentPosition - fullPosition, options):
1378
+ if isinstance(chunk, SubstrateUnderrunError):
1379
+ yield chunk
1380
+
1381
+ if LOG:
1382
+ LOG('decoding as untagged ANY, header substrate %s' % debug.hexdump(chunk))
1383
+
1384
+ # Any components do not inherit initial tag
1385
+ asn1Spec = self.protoComponent
1386
+
1387
+ if substrateFun and substrateFun is not self.substrateCollector:
1388
+ asn1Object = self._createComponent(
1389
+ asn1Spec, tagSet, noValue, **options)
1390
+
1391
+ for chunk in substrateFun(
1392
+ asn1Object, chunk + substrate, length + len(chunk), options):
1393
+ yield chunk
1394
+
1395
+ return
1396
+
1397
+ if LOG:
1398
+ LOG('assembling constructed serialization')
1399
+
1400
+ # All inner fragments are of the same type, treat them as octet string
1401
+ substrateFun = self.substrateCollector
1402
+
1403
+ while True: # loop over fragments
1404
+
1405
+ for component in decodeFun(
1406
+ substrate, asn1Spec, substrateFun=substrateFun,
1407
+ allowEoo=True, **options):
1408
+
1409
+ if isinstance(component, SubstrateUnderrunError):
1410
+ yield component
1411
+
1412
+ if component is eoo.endOfOctets:
1413
+ break
1414
+
1415
+ if component is eoo.endOfOctets:
1416
+ break
1417
+
1418
+ chunk += component
1419
+
1420
+ if substrateFun:
1421
+ yield chunk # TODO: Weird
1422
+
1423
+ else:
1424
+ yield self._createComponent(asn1Spec, tagSet, chunk, **options)
1425
+
1426
+
1427
+ # character string types
1428
+ class UTF8StringPayloadDecoder(OctetStringPayloadDecoder):
1429
+ protoComponent = char.UTF8String()
1430
+
1431
+
1432
+ class NumericStringPayloadDecoder(OctetStringPayloadDecoder):
1433
+ protoComponent = char.NumericString()
1434
+
1435
+
1436
+ class PrintableStringPayloadDecoder(OctetStringPayloadDecoder):
1437
+ protoComponent = char.PrintableString()
1438
+
1439
+
1440
+ class TeletexStringPayloadDecoder(OctetStringPayloadDecoder):
1441
+ protoComponent = char.TeletexString()
1442
+
1443
+
1444
+ class VideotexStringPayloadDecoder(OctetStringPayloadDecoder):
1445
+ protoComponent = char.VideotexString()
1446
+
1447
+
1448
+ class IA5StringPayloadDecoder(OctetStringPayloadDecoder):
1449
+ protoComponent = char.IA5String()
1450
+
1451
+
1452
+ class GraphicStringPayloadDecoder(OctetStringPayloadDecoder):
1453
+ protoComponent = char.GraphicString()
1454
+
1455
+
1456
+ class VisibleStringPayloadDecoder(OctetStringPayloadDecoder):
1457
+ protoComponent = char.VisibleString()
1458
+
1459
+
1460
+ class GeneralStringPayloadDecoder(OctetStringPayloadDecoder):
1461
+ protoComponent = char.GeneralString()
1462
+
1463
+
1464
+ class UniversalStringPayloadDecoder(OctetStringPayloadDecoder):
1465
+ protoComponent = char.UniversalString()
1466
+
1467
+
1468
+ class BMPStringPayloadDecoder(OctetStringPayloadDecoder):
1469
+ protoComponent = char.BMPString()
1470
+
1471
+
1472
+ # "useful" types
1473
+ class ObjectDescriptorPayloadDecoder(OctetStringPayloadDecoder):
1474
+ protoComponent = useful.ObjectDescriptor()
1475
+
1476
+
1477
+ class GeneralizedTimePayloadDecoder(OctetStringPayloadDecoder):
1478
+ protoComponent = useful.GeneralizedTime()
1479
+
1480
+
1481
+ class UTCTimePayloadDecoder(OctetStringPayloadDecoder):
1482
+ protoComponent = useful.UTCTime()
1483
+
1484
+
1485
+ TAG_MAP = {
1486
+ univ.Integer.tagSet: IntegerPayloadDecoder(),
1487
+ univ.Boolean.tagSet: BooleanPayloadDecoder(),
1488
+ univ.BitString.tagSet: BitStringPayloadDecoder(),
1489
+ univ.OctetString.tagSet: OctetStringPayloadDecoder(),
1490
+ univ.Null.tagSet: NullPayloadDecoder(),
1491
+ univ.ObjectIdentifier.tagSet: ObjectIdentifierPayloadDecoder(),
1492
+ univ.RelativeOID.tagSet: RelativeOIDPayloadDecoder(),
1493
+ univ.Enumerated.tagSet: IntegerPayloadDecoder(),
1494
+ univ.Real.tagSet: RealPayloadDecoder(),
1495
+ univ.Sequence.tagSet: SequenceOrSequenceOfPayloadDecoder(), # conflicts with SequenceOf
1496
+ univ.Set.tagSet: SetOrSetOfPayloadDecoder(), # conflicts with SetOf
1497
+ univ.Choice.tagSet: ChoicePayloadDecoder(), # conflicts with Any
1498
+ # character string types
1499
+ char.UTF8String.tagSet: UTF8StringPayloadDecoder(),
1500
+ char.NumericString.tagSet: NumericStringPayloadDecoder(),
1501
+ char.PrintableString.tagSet: PrintableStringPayloadDecoder(),
1502
+ char.TeletexString.tagSet: TeletexStringPayloadDecoder(),
1503
+ char.VideotexString.tagSet: VideotexStringPayloadDecoder(),
1504
+ char.IA5String.tagSet: IA5StringPayloadDecoder(),
1505
+ char.GraphicString.tagSet: GraphicStringPayloadDecoder(),
1506
+ char.VisibleString.tagSet: VisibleStringPayloadDecoder(),
1507
+ char.GeneralString.tagSet: GeneralStringPayloadDecoder(),
1508
+ char.UniversalString.tagSet: UniversalStringPayloadDecoder(),
1509
+ char.BMPString.tagSet: BMPStringPayloadDecoder(),
1510
+ # useful types
1511
+ useful.ObjectDescriptor.tagSet: ObjectDescriptorPayloadDecoder(),
1512
+ useful.GeneralizedTime.tagSet: GeneralizedTimePayloadDecoder(),
1513
+ useful.UTCTime.tagSet: UTCTimePayloadDecoder()
1514
+ }
1515
+
1516
+ # Type-to-codec map for ambiguous ASN.1 types
1517
+ TYPE_MAP = {
1518
+ univ.Set.typeId: SetPayloadDecoder(),
1519
+ univ.SetOf.typeId: SetOfPayloadDecoder(),
1520
+ univ.Sequence.typeId: SequencePayloadDecoder(),
1521
+ univ.SequenceOf.typeId: SequenceOfPayloadDecoder(),
1522
+ univ.Choice.typeId: ChoicePayloadDecoder(),
1523
+ univ.Any.typeId: AnyPayloadDecoder()
1524
+ }
1525
+
1526
+ # Put in non-ambiguous types for faster codec lookup
1527
+ for typeDecoder in TAG_MAP.values():
1528
+ if typeDecoder.protoComponent is not None:
1529
+ typeId = typeDecoder.protoComponent.__class__.typeId
1530
+ if typeId is not None and typeId not in TYPE_MAP:
1531
+ TYPE_MAP[typeId] = typeDecoder
1532
+
1533
+
1534
+ (stDecodeTag,
1535
+ stDecodeLength,
1536
+ stGetValueDecoder,
1537
+ stGetValueDecoderByAsn1Spec,
1538
+ stGetValueDecoderByTag,
1539
+ stTryAsExplicitTag,
1540
+ stDecodeValue,
1541
+ stDumpRawValue,
1542
+ stErrorCondition,
1543
+ stStop) = [x for x in range(10)]
1544
+
1545
+
1546
+ EOO_SENTINEL = bytes((0, 0))
1547
+
1548
+
1549
+ class SingleItemDecoder(object):
1550
+ defaultErrorState = stErrorCondition
1551
+ #defaultErrorState = stDumpRawValue
1552
+ defaultRawDecoder = AnyPayloadDecoder()
1553
+
1554
+ supportIndefLength = True
1555
+
1556
+ TAG_MAP = TAG_MAP
1557
+ TYPE_MAP = TYPE_MAP
1558
+
1559
+ def __init__(self, tagMap=_MISSING, typeMap=_MISSING, **ignored):
1560
+ self._tagMap = tagMap if tagMap is not _MISSING else self.TAG_MAP
1561
+ self._typeMap = typeMap if typeMap is not _MISSING else self.TYPE_MAP
1562
+
1563
+ # Tag & TagSet objects caches
1564
+ self._tagCache = {}
1565
+ self._tagSetCache = {}
1566
+
1567
+ def __call__(self, substrate, asn1Spec=None,
1568
+ tagSet=None, length=None, state=stDecodeTag,
1569
+ decodeFun=None, substrateFun=None,
1570
+ **options):
1571
+
1572
+ _nestingLevel = options.get('_nestingLevel', 0)
1573
+
1574
+ if _nestingLevel > MAX_NESTING_DEPTH:
1575
+ raise error.PyAsn1Error(
1576
+ 'ASN.1 structure nesting depth exceeds limit (%d)' % MAX_NESTING_DEPTH
1577
+ )
1578
+
1579
+ options['_nestingLevel'] = _nestingLevel + 1
1580
+
1581
+ allowEoo = options.pop('allowEoo', False)
1582
+
1583
+ if LOG:
1584
+ LOG('decoder called at scope %s with state %d, working with up '
1585
+ 'to %s octets of substrate: '
1586
+ '%s' % (debug.scope, state, length, substrate))
1587
+
1588
+ # Look for end-of-octets sentinel
1589
+ if allowEoo and self.supportIndefLength:
1590
+
1591
+ for eoo_candidate in readFromStream(substrate, 2, options):
1592
+ if isinstance(eoo_candidate, SubstrateUnderrunError):
1593
+ yield eoo_candidate
1594
+
1595
+ if eoo_candidate == EOO_SENTINEL:
1596
+ if LOG:
1597
+ LOG('end-of-octets sentinel found')
1598
+ yield eoo.endOfOctets
1599
+ return
1600
+
1601
+ else:
1602
+ substrate.seek(-2, os.SEEK_CUR)
1603
+
1604
+ tagMap = self._tagMap
1605
+ typeMap = self._typeMap
1606
+ tagCache = self._tagCache
1607
+ tagSetCache = self._tagSetCache
1608
+
1609
+ value = noValue
1610
+
1611
+ substrate.markedPosition = substrate.tell()
1612
+
1613
+ while state is not stStop:
1614
+
1615
+ if state is stDecodeTag:
1616
+ # Decode tag
1617
+ isShortTag = True
1618
+
1619
+ for firstByte in readFromStream(substrate, 1, options):
1620
+ if isinstance(firstByte, SubstrateUnderrunError):
1621
+ yield firstByte
1622
+
1623
+ firstOctet = ord(firstByte)
1624
+
1625
+ try:
1626
+ lastTag = tagCache[firstOctet]
1627
+
1628
+ except KeyError:
1629
+ integerTag = firstOctet
1630
+ tagClass = integerTag & 0xC0
1631
+ tagFormat = integerTag & 0x20
1632
+ tagId = integerTag & 0x1F
1633
+
1634
+ if tagId == 0x1F:
1635
+ isShortTag = False
1636
+ lengthOctetIdx = 0
1637
+ tagId = 0
1638
+
1639
+ while True:
1640
+ for integerByte in readFromStream(substrate, 1, options):
1641
+ if isinstance(integerByte, SubstrateUnderrunError):
1642
+ yield integerByte
1643
+
1644
+ if not integerByte:
1645
+ raise error.SubstrateUnderrunError(
1646
+ 'Short octet stream on long tag decoding'
1647
+ )
1648
+
1649
+ integerTag = ord(integerByte)
1650
+ lengthOctetIdx += 1
1651
+ tagId <<= 7
1652
+ tagId |= (integerTag & 0x7F)
1653
+
1654
+ if not integerTag & 0x80:
1655
+ break
1656
+
1657
+ lastTag = tag.Tag(
1658
+ tagClass=tagClass, tagFormat=tagFormat, tagId=tagId
1659
+ )
1660
+
1661
+ if isShortTag:
1662
+ # cache short tags
1663
+ tagCache[firstOctet] = lastTag
1664
+
1665
+ if tagSet is None:
1666
+ if isShortTag:
1667
+ try:
1668
+ tagSet = tagSetCache[firstOctet]
1669
+
1670
+ except KeyError:
1671
+ # base tag not recovered
1672
+ tagSet = tag.TagSet((), lastTag)
1673
+ tagSetCache[firstOctet] = tagSet
1674
+ else:
1675
+ tagSet = tag.TagSet((), lastTag)
1676
+
1677
+ else:
1678
+ tagSet = lastTag + tagSet
1679
+
1680
+ state = stDecodeLength
1681
+
1682
+ if LOG:
1683
+ LOG('tag decoded into %s, decoding length' % tagSet)
1684
+
1685
+ if state is stDecodeLength:
1686
+ # Decode length
1687
+ for firstOctet in readFromStream(substrate, 1, options):
1688
+ if isinstance(firstOctet, SubstrateUnderrunError):
1689
+ yield firstOctet
1690
+
1691
+ firstOctet = ord(firstOctet)
1692
+
1693
+ if firstOctet < 128:
1694
+ length = firstOctet
1695
+
1696
+ elif firstOctet > 128:
1697
+ size = firstOctet & 0x7F
1698
+
1699
+ if size > MAX_LENGTH_OCTETS:
1700
+ raise error.PyAsn1Error(
1701
+ 'BER length field size %d exceeds limit (%d)' % (
1702
+ size, MAX_LENGTH_OCTETS)
1703
+ )
1704
+
1705
+ # encoded in size bytes
1706
+ for encodedLength in readFromStream(substrate, size, options):
1707
+ if isinstance(encodedLength, SubstrateUnderrunError):
1708
+ yield encodedLength
1709
+ encodedLength = list(encodedLength)
1710
+ if len(encodedLength) != size:
1711
+ raise error.SubstrateUnderrunError(
1712
+ '%s<%s at %s' % (size, len(encodedLength), tagSet)
1713
+ )
1714
+
1715
+ length = 0
1716
+ for lengthOctet in encodedLength:
1717
+ length <<= 8
1718
+ length |= lengthOctet
1719
+ size += 1
1720
+
1721
+ else: # 128 means indefinite
1722
+ length = -1
1723
+
1724
+ if length == -1 and not self.supportIndefLength:
1725
+ raise error.PyAsn1Error('Indefinite length encoding not supported by this codec')
1726
+
1727
+ state = stGetValueDecoder
1728
+
1729
+ if LOG:
1730
+ LOG('value length decoded into %d' % length)
1731
+
1732
+ if state is stGetValueDecoder:
1733
+ if asn1Spec is None:
1734
+ state = stGetValueDecoderByTag
1735
+
1736
+ else:
1737
+ state = stGetValueDecoderByAsn1Spec
1738
+ #
1739
+ # There're two ways of creating subtypes in ASN.1 what influences
1740
+ # decoder operation. These methods are:
1741
+ # 1) Either base types used in or no IMPLICIT tagging has been
1742
+ # applied on subtyping.
1743
+ # 2) Subtype syntax drops base type information (by means of
1744
+ # IMPLICIT tagging.
1745
+ # The first case allows for complete tag recovery from substrate
1746
+ # while the second one requires original ASN.1 type spec for
1747
+ # decoding.
1748
+ #
1749
+ # In either case a set of tags (tagSet) is coming from substrate
1750
+ # in an incremental, tag-by-tag fashion (this is the case of
1751
+ # EXPLICIT tag which is most basic). Outermost tag comes first
1752
+ # from the wire.
1753
+ #
1754
+ if state is stGetValueDecoderByTag:
1755
+ try:
1756
+ concreteDecoder = tagMap[tagSet]
1757
+
1758
+ except KeyError:
1759
+ concreteDecoder = None
1760
+
1761
+ if concreteDecoder:
1762
+ state = stDecodeValue
1763
+
1764
+ else:
1765
+ try:
1766
+ concreteDecoder = tagMap[tagSet[:1]]
1767
+
1768
+ except KeyError:
1769
+ concreteDecoder = None
1770
+
1771
+ if concreteDecoder:
1772
+ state = stDecodeValue
1773
+ else:
1774
+ state = stTryAsExplicitTag
1775
+
1776
+ if LOG:
1777
+ 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'))
1778
+ debug.scope.push(concreteDecoder is None and '?' or concreteDecoder.protoComponent.__class__.__name__)
1779
+
1780
+ if state is stGetValueDecoderByAsn1Spec:
1781
+
1782
+ if asn1Spec.__class__ is tagmap.TagMap:
1783
+ try:
1784
+ chosenSpec = asn1Spec[tagSet]
1785
+
1786
+ except KeyError:
1787
+ chosenSpec = None
1788
+
1789
+ if LOG:
1790
+ LOG('candidate ASN.1 spec is a map of:')
1791
+
1792
+ for firstOctet, v in asn1Spec.presentTypes.items():
1793
+ LOG(' %s -> %s' % (firstOctet, v.__class__.__name__))
1794
+
1795
+ if asn1Spec.skipTypes:
1796
+ LOG('but neither of: ')
1797
+ for firstOctet, v in asn1Spec.skipTypes.items():
1798
+ LOG(' %s -> %s' % (firstOctet, v.__class__.__name__))
1799
+ LOG('new candidate ASN.1 spec is %s, chosen by %s' % (chosenSpec is None and '<none>' or chosenSpec.prettyPrintType(), tagSet))
1800
+
1801
+ elif tagSet == asn1Spec.tagSet or tagSet in asn1Spec.tagMap:
1802
+ chosenSpec = asn1Spec
1803
+ if LOG:
1804
+ LOG('candidate ASN.1 spec is %s' % asn1Spec.__class__.__name__)
1805
+
1806
+ else:
1807
+ chosenSpec = None
1808
+
1809
+ if chosenSpec is not None:
1810
+ try:
1811
+ # ambiguous type or just faster codec lookup
1812
+ concreteDecoder = typeMap[chosenSpec.typeId]
1813
+
1814
+ if LOG:
1815
+ LOG('value decoder chosen for an ambiguous type by type ID %s' % (chosenSpec.typeId,))
1816
+
1817
+ except KeyError:
1818
+ # use base type for codec lookup to recover untagged types
1819
+ baseTagSet = tag.TagSet(chosenSpec.tagSet.baseTag, chosenSpec.tagSet.baseTag)
1820
+ try:
1821
+ # base type or tagged subtype
1822
+ concreteDecoder = tagMap[baseTagSet]
1823
+
1824
+ if LOG:
1825
+ LOG('value decoder chosen by base %s' % (baseTagSet,))
1826
+
1827
+ except KeyError:
1828
+ concreteDecoder = None
1829
+
1830
+ if concreteDecoder:
1831
+ asn1Spec = chosenSpec
1832
+ state = stDecodeValue
1833
+
1834
+ else:
1835
+ state = stTryAsExplicitTag
1836
+
1837
+ else:
1838
+ concreteDecoder = None
1839
+ state = stTryAsExplicitTag
1840
+
1841
+ if LOG:
1842
+ 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'))
1843
+ debug.scope.push(chosenSpec is None and '?' or chosenSpec.__class__.__name__)
1844
+
1845
+ if state is stDecodeValue:
1846
+ if not options.get('recursiveFlag', True) and not substrateFun: # deprecate this
1847
+ def substrateFun(asn1Object, _substrate, _length, _options):
1848
+ """Legacy hack to keep the recursiveFlag=False option supported.
1849
+
1850
+ The decode(..., substrateFun=userCallback) option was introduced in 0.1.4 as a generalization
1851
+ of the old recursiveFlag=False option. Users should pass their callback instead of using
1852
+ recursiveFlag.
1853
+ """
1854
+ yield asn1Object
1855
+
1856
+ original_position = substrate.tell()
1857
+
1858
+ if length == -1: # indef length
1859
+ for value in concreteDecoder.indefLenValueDecoder(
1860
+ substrate, asn1Spec,
1861
+ tagSet, length, stGetValueDecoder,
1862
+ self, substrateFun, **options):
1863
+ if isinstance(value, SubstrateUnderrunError):
1864
+ yield value
1865
+
1866
+ else:
1867
+ for value in concreteDecoder.valueDecoder(
1868
+ substrate, asn1Spec,
1869
+ tagSet, length, stGetValueDecoder,
1870
+ self, substrateFun, **options):
1871
+ if isinstance(value, SubstrateUnderrunError):
1872
+ yield value
1873
+
1874
+ bytesRead = substrate.tell() - original_position
1875
+ if not substrateFun and bytesRead != length:
1876
+ raise PyAsn1Error(
1877
+ "Read %s bytes instead of expected %s." % (bytesRead, length))
1878
+ elif substrateFun and bytesRead > length:
1879
+ # custom substrateFun may be used for partial decoding, reading less is expected there
1880
+ raise PyAsn1Error(
1881
+ "Read %s bytes are more than expected %s." % (bytesRead, length))
1882
+
1883
+ if LOG:
1884
+ LOG('codec %s yields type %s, value:\n%s\n...' % (
1885
+ concreteDecoder.__class__.__name__, value.__class__.__name__,
1886
+ isinstance(value, base.Asn1Item) and value.prettyPrint() or value))
1887
+
1888
+ state = stStop
1889
+ break
1890
+
1891
+ if state is stTryAsExplicitTag:
1892
+ if (tagSet and
1893
+ tagSet[0].tagFormat == tag.tagFormatConstructed and
1894
+ tagSet[0].tagClass != tag.tagClassUniversal):
1895
+ # Assume explicit tagging
1896
+ concreteDecoder = rawPayloadDecoder
1897
+ state = stDecodeValue
1898
+
1899
+ else:
1900
+ concreteDecoder = None
1901
+ state = self.defaultErrorState
1902
+
1903
+ if LOG:
1904
+ LOG('codec %s chosen, decoding %s' % (concreteDecoder and concreteDecoder.__class__.__name__ or "<none>", state is stDecodeValue and 'value' or 'as failure'))
1905
+
1906
+ if state is stDumpRawValue:
1907
+ concreteDecoder = self.defaultRawDecoder
1908
+
1909
+ if LOG:
1910
+ LOG('codec %s chosen, decoding value' % concreteDecoder.__class__.__name__)
1911
+
1912
+ state = stDecodeValue
1913
+
1914
+ if state is stErrorCondition:
1915
+ raise error.PyAsn1Error(
1916
+ '%s not in asn1Spec: %r' % (tagSet, asn1Spec)
1917
+ )
1918
+
1919
+ if LOG:
1920
+ debug.scope.pop()
1921
+ LOG('decoder left scope %s, call completed' % debug.scope)
1922
+
1923
+ yield value
1924
+
1925
+
1926
+ class StreamingDecoder(object):
1927
+ """Create an iterator that turns BER/CER/DER byte stream into ASN.1 objects.
1928
+
1929
+ On each iteration, consume whatever BER/CER/DER serialization is
1930
+ available in the `substrate` stream-like object and turns it into
1931
+ one or more, possibly nested, ASN.1 objects.
1932
+
1933
+ Parameters
1934
+ ----------
1935
+ substrate: :py:class:`file`, :py:class:`io.BytesIO`
1936
+ BER/CER/DER serialization in form of a byte stream
1937
+
1938
+ Keyword Args
1939
+ ------------
1940
+ asn1Spec: :py:class:`~pyasn1.type.base.PyAsn1Item`
1941
+ A pyasn1 type object to act as a template guiding the decoder.
1942
+ Depending on the ASN.1 structure being decoded, `asn1Spec` may
1943
+ or may not be required. One of the reasons why `asn1Spec` may
1944
+ me required is that ASN.1 structure is encoded in the *IMPLICIT*
1945
+ tagging mode.
1946
+
1947
+ Yields
1948
+ ------
1949
+ : :py:class:`~pyasn1.type.base.PyAsn1Item`, :py:class:`~pyasn1.error.SubstrateUnderrunError`
1950
+ Decoded ASN.1 object (possibly, nested) or
1951
+ :py:class:`~pyasn1.error.SubstrateUnderrunError` object indicating
1952
+ insufficient BER/CER/DER serialization on input to fully recover ASN.1
1953
+ objects from it.
1954
+
1955
+ In the latter case the caller is advised to ensure some more data in
1956
+ the input stream, then call the iterator again. The decoder will resume
1957
+ the decoding process using the newly arrived data.
1958
+
1959
+ The `context` property of :py:class:`~pyasn1.error.SubstrateUnderrunError`
1960
+ object might hold a reference to the partially populated ASN.1 object
1961
+ being reconstructed.
1962
+
1963
+ Raises
1964
+ ------
1965
+ ~pyasn1.error.PyAsn1Error, ~pyasn1.error.EndOfStreamError
1966
+ `PyAsn1Error` on deserialization error, `EndOfStreamError` on
1967
+ premature stream closure.
1968
+
1969
+ Examples
1970
+ --------
1971
+ Decode BER serialisation without ASN.1 schema
1972
+
1973
+ .. code-block:: pycon
1974
+
1975
+ >>> stream = io.BytesIO(
1976
+ ... b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03')
1977
+ >>>
1978
+ >>> for asn1Object in StreamingDecoder(stream):
1979
+ ... print(asn1Object)
1980
+ >>>
1981
+ SequenceOf:
1982
+ 1 2 3
1983
+
1984
+ Decode BER serialisation with ASN.1 schema
1985
+
1986
+ .. code-block:: pycon
1987
+
1988
+ >>> stream = io.BytesIO(
1989
+ ... b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03')
1990
+ >>>
1991
+ >>> schema = SequenceOf(componentType=Integer())
1992
+ >>>
1993
+ >>> decoder = StreamingDecoder(stream, asn1Spec=schema)
1994
+ >>> for asn1Object in decoder:
1995
+ ... print(asn1Object)
1996
+ >>>
1997
+ SequenceOf:
1998
+ 1 2 3
1999
+ """
2000
+
2001
+ SINGLE_ITEM_DECODER = SingleItemDecoder
2002
+
2003
+ def __init__(self, substrate, asn1Spec=None, **options):
2004
+ self._singleItemDecoder = self.SINGLE_ITEM_DECODER(**options)
2005
+ self._substrate = asSeekableStream(substrate)
2006
+ self._asn1Spec = asn1Spec
2007
+ self._options = options
2008
+
2009
+ def __iter__(self):
2010
+ while True:
2011
+ for asn1Object in self._singleItemDecoder(
2012
+ self._substrate, self._asn1Spec, **self._options):
2013
+ yield asn1Object
2014
+
2015
+ for chunk in isEndOfStream(self._substrate):
2016
+ if isinstance(chunk, SubstrateUnderrunError):
2017
+ yield
2018
+
2019
+ break
2020
+
2021
+ if chunk:
2022
+ break
2023
+
2024
+
2025
+ class Decoder(object):
2026
+ """Create a BER decoder object.
2027
+
2028
+ Parse BER/CER/DER octet-stream into one, possibly nested, ASN.1 object.
2029
+ """
2030
+ STREAMING_DECODER = StreamingDecoder
2031
+
2032
+ @classmethod
2033
+ def __call__(cls, substrate, asn1Spec=None, **options):
2034
+ """Turns BER/CER/DER octet stream into an ASN.1 object.
2035
+
2036
+ Takes BER/CER/DER octet-stream in form of :py:class:`bytes`
2037
+ and decode it into an ASN.1 object
2038
+ (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative) which
2039
+ may be a scalar or an arbitrary nested structure.
2040
+
2041
+ Parameters
2042
+ ----------
2043
+ substrate: :py:class:`bytes`
2044
+ BER/CER/DER octet-stream to parse
2045
+
2046
+ Keyword Args
2047
+ ------------
2048
+ asn1Spec: :py:class:`~pyasn1.type.base.PyAsn1Item`
2049
+ A pyasn1 type object (:py:class:`~pyasn1.type.base.PyAsn1Item`
2050
+ derivative) to act as a template guiding the decoder.
2051
+ Depending on the ASN.1 structure being decoded, `asn1Spec` may or
2052
+ may not be required. Most common reason for it to require is that
2053
+ ASN.1 structure is encoded in *IMPLICIT* tagging mode.
2054
+
2055
+ substrateFun: :py:class:`Union[
2056
+ Callable[[pyasn1.type.base.PyAsn1Item, bytes, int],
2057
+ Tuple[pyasn1.type.base.PyAsn1Item, bytes]],
2058
+ Callable[[pyasn1.type.base.PyAsn1Item, io.BytesIO, int, dict],
2059
+ Generator[Union[pyasn1.type.base.PyAsn1Item,
2060
+ pyasn1.error.SubstrateUnderrunError],
2061
+ None, None]]
2062
+ ]`
2063
+ User callback meant to generalize special use cases like non-recursive or
2064
+ partial decoding. A 3-arg non-streaming variant is supported for backwards
2065
+ compatiblilty in addition to the newer 4-arg streaming variant.
2066
+ The callback will receive the uninitialized object recovered from substrate
2067
+ as 1st argument, the uninterpreted payload as 2nd argument, and the length
2068
+ of the uninterpreted payload as 3rd argument. The streaming variant will
2069
+ additionally receive the decode(..., **options) kwargs as 4th argument.
2070
+ The non-streaming variant shall return an object that will be propagated
2071
+ as decode() return value as 1st item, and the remainig payload for further
2072
+ decode passes as 2nd item.
2073
+ The streaming variant shall yield an object that will be propagated as
2074
+ decode() return value, and leave the remaining payload in the stream.
2075
+
2076
+ Returns
2077
+ -------
2078
+ : :py:class:`tuple`
2079
+ A tuple of :py:class:`~pyasn1.type.base.PyAsn1Item` object
2080
+ recovered from BER/CER/DER substrate and the unprocessed trailing
2081
+ portion of the `substrate` (may be empty)
2082
+
2083
+ Raises
2084
+ ------
2085
+ : :py:class:`~pyasn1.error.PyAsn1Error`
2086
+ :py:class:`~pyasn1.error.SubstrateUnderrunError` on insufficient
2087
+ input or :py:class:`~pyasn1.error.PyAsn1Error` on decoding error.
2088
+
2089
+ Examples
2090
+ --------
2091
+ Decode BER/CER/DER serialisation without ASN.1 schema
2092
+
2093
+ .. code-block:: pycon
2094
+
2095
+ >>> s, unprocessed = decode(b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03')
2096
+ >>> str(s)
2097
+ SequenceOf:
2098
+ 1 2 3
2099
+
2100
+ Decode BER/CER/DER serialisation with ASN.1 schema
2101
+
2102
+ .. code-block:: pycon
2103
+
2104
+ >>> seq = SequenceOf(componentType=Integer())
2105
+ >>> s, unprocessed = decode(
2106
+ b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03', asn1Spec=seq)
2107
+ >>> str(s)
2108
+ SequenceOf:
2109
+ 1 2 3
2110
+
2111
+ """
2112
+ substrate = asSeekableStream(substrate)
2113
+
2114
+ if "substrateFun" in options:
2115
+ origSubstrateFun = options["substrateFun"]
2116
+
2117
+ def substrateFunWrapper(asn1Object, substrate, length, options=None):
2118
+ """Support both 0.4 and 0.5 style APIs.
2119
+
2120
+ substrateFun API has changed in 0.5 for use with streaming decoders. To stay backwards compatible,
2121
+ we first try if we received a streaming user callback. If that fails,we assume we've received a
2122
+ non-streaming v0.4 user callback and convert it for streaming on the fly
2123
+ """
2124
+ try:
2125
+ substrate_gen = origSubstrateFun(asn1Object, substrate, length, options)
2126
+ except TypeError as _value:
2127
+ if _value.__traceback__.tb_next:
2128
+ # Traceback depth > 1 means TypeError from inside user provided function
2129
+ raise
2130
+ # invariant maintained at Decoder.__call__ entry
2131
+ assert isinstance(substrate, io.BytesIO) # nosec assert_used
2132
+ substrate_gen = Decoder._callSubstrateFunV4asV5(origSubstrateFun, asn1Object, substrate, length)
2133
+ for value in substrate_gen:
2134
+ yield value
2135
+
2136
+ options["substrateFun"] = substrateFunWrapper
2137
+
2138
+ streamingDecoder = cls.STREAMING_DECODER(
2139
+ substrate, asn1Spec, **options)
2140
+
2141
+ for asn1Object in streamingDecoder:
2142
+ if isinstance(asn1Object, SubstrateUnderrunError):
2143
+ raise error.SubstrateUnderrunError('Short substrate on input')
2144
+
2145
+ try:
2146
+ tail = next(readFromStream(substrate))
2147
+
2148
+ except error.EndOfStreamError:
2149
+ tail = b''
2150
+
2151
+ return asn1Object, tail
2152
+
2153
+ @staticmethod
2154
+ def _callSubstrateFunV4asV5(substrateFunV4, asn1Object, substrate, length):
2155
+ substrate_bytes = substrate.read()
2156
+ if length == -1:
2157
+ length = len(substrate_bytes)
2158
+ value, nextSubstrate = substrateFunV4(asn1Object, substrate_bytes, length)
2159
+ nbytes = substrate.write(nextSubstrate)
2160
+ substrate.truncate()
2161
+ substrate.seek(-nbytes, os.SEEK_CUR)
2162
+ yield value
2163
+
2164
+ #: Turns BER octet stream into an ASN.1 object.
2165
+ #:
2166
+ #: Takes BER octet-stream and decode it into an ASN.1 object
2167
+ #: (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative) which
2168
+ #: may be a scalar or an arbitrary nested structure.
2169
+ #:
2170
+ #: Parameters
2171
+ #: ----------
2172
+ #: substrate: :py:class:`bytes`
2173
+ #: BER octet-stream
2174
+ #:
2175
+ #: Keyword Args
2176
+ #: ------------
2177
+ #: asn1Spec: any pyasn1 type object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
2178
+ #: A pyasn1 type object to act as a template guiding the decoder. Depending on the ASN.1 structure
2179
+ #: being decoded, *asn1Spec* may or may not be required. Most common reason for
2180
+ #: it to require is that ASN.1 structure is encoded in *IMPLICIT* tagging mode.
2181
+ #:
2182
+ #: Returns
2183
+ #: -------
2184
+ #: : :py:class:`tuple`
2185
+ #: A tuple of pyasn1 object recovered from BER substrate (:py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
2186
+ #: and the unprocessed trailing portion of the *substrate* (may be empty)
2187
+ #:
2188
+ #: Raises
2189
+ #: ------
2190
+ #: ~pyasn1.error.PyAsn1Error, ~pyasn1.error.SubstrateUnderrunError
2191
+ #: On decoding errors
2192
+ #:
2193
+ #: Notes
2194
+ #: -----
2195
+ #: This function is deprecated. Please use :py:class:`Decoder` or
2196
+ #: :py:class:`StreamingDecoder` class instance.
2197
+ #:
2198
+ #: Examples
2199
+ #: --------
2200
+ #: Decode BER serialisation without ASN.1 schema
2201
+ #:
2202
+ #: .. code-block:: pycon
2203
+ #:
2204
+ #: >>> s, _ = decode(b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03')
2205
+ #: >>> str(s)
2206
+ #: SequenceOf:
2207
+ #: 1 2 3
2208
+ #:
2209
+ #: Decode BER serialisation with ASN.1 schema
2210
+ #:
2211
+ #: .. code-block:: pycon
2212
+ #:
2213
+ #: >>> seq = SequenceOf(componentType=Integer())
2214
+ #: >>> s, _ = decode(b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03', asn1Spec=seq)
2215
+ #: >>> str(s)
2216
+ #: SequenceOf:
2217
+ #: 1 2 3
2218
+ #:
2219
+ decode = Decoder()
2220
+
2221
+ def __getattr__(attr: str):
2222
+ if newAttr := {"tagMap": "TAG_MAP", "typeMap": "TYPE_MAP"}.get(attr):
2223
+ warnings.warn(f"{attr} is deprecated. Please use {newAttr} instead.", DeprecationWarning, stacklevel=2)
2224
+ return globals()[newAttr]
2225
+ raise AttributeError(attr)
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/ber/encoder.py ADDED
@@ -0,0 +1,954 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ #
7
+ import sys
8
+ import warnings
9
+
10
+ from pyasn1 import debug
11
+ from pyasn1 import error
12
+ from pyasn1.codec.ber import eoo
13
+ from pyasn1.compat import _MISSING
14
+ from pyasn1.compat.integer import to_bytes
15
+ from pyasn1.type import char
16
+ from pyasn1.type import tag
17
+ from pyasn1.type import univ
18
+ from pyasn1.type import useful
19
+
20
+ __all__ = ['Encoder', 'encode']
21
+
22
+ LOG = debug.registerLoggee(__name__, flags=debug.DEBUG_ENCODER)
23
+
24
+
25
+ class AbstractItemEncoder(object):
26
+ supportIndefLenMode = True
27
+
28
+ # An outcome of otherwise legit call `encodeFun(eoo.endOfOctets)`
29
+ eooIntegerSubstrate = (0, 0)
30
+ eooOctetsSubstrate = bytes(eooIntegerSubstrate)
31
+
32
+ # noinspection PyMethodMayBeStatic
33
+ def encodeTag(self, singleTag, isConstructed):
34
+ tagClass, tagFormat, tagId = singleTag
35
+ encodedTag = tagClass | tagFormat
36
+ if isConstructed:
37
+ encodedTag |= tag.tagFormatConstructed
38
+
39
+ if tagId < 31:
40
+ return encodedTag | tagId,
41
+
42
+ else:
43
+ substrate = tagId & 0x7f,
44
+
45
+ tagId >>= 7
46
+
47
+ while tagId:
48
+ substrate = (0x80 | (tagId & 0x7f),) + substrate
49
+ tagId >>= 7
50
+
51
+ return (encodedTag | 0x1F,) + substrate
52
+
53
+ def encodeLength(self, length, defMode):
54
+ if not defMode and self.supportIndefLenMode:
55
+ return (0x80,)
56
+
57
+ if length < 0x80:
58
+ return length,
59
+
60
+ else:
61
+ substrate = ()
62
+ while length:
63
+ substrate = (length & 0xff,) + substrate
64
+ length >>= 8
65
+
66
+ substrateLen = len(substrate)
67
+
68
+ if substrateLen > 126:
69
+ raise error.PyAsn1Error('Length octets overflow (%d)' % substrateLen)
70
+
71
+ return (0x80 | substrateLen,) + substrate
72
+
73
+ def encodeValue(self, value, asn1Spec, encodeFun, **options):
74
+ raise error.PyAsn1Error('Not implemented')
75
+
76
+ def encode(self, value, asn1Spec=None, encodeFun=None, **options):
77
+
78
+ if asn1Spec is None:
79
+ tagSet = value.tagSet
80
+ else:
81
+ tagSet = asn1Spec.tagSet
82
+
83
+ # untagged item?
84
+ if not tagSet:
85
+ substrate, isConstructed, isOctets = self.encodeValue(
86
+ value, asn1Spec, encodeFun, **options
87
+ )
88
+ return substrate
89
+
90
+ defMode = options.get('defMode', True)
91
+
92
+ substrate = b''
93
+
94
+ for idx, singleTag in enumerate(tagSet.superTags):
95
+
96
+ defModeOverride = defMode
97
+
98
+ # base tag?
99
+ if not idx:
100
+ try:
101
+ substrate, isConstructed, isOctets = self.encodeValue(
102
+ value, asn1Spec, encodeFun, **options
103
+ )
104
+
105
+ except error.PyAsn1Error as exc:
106
+ raise error.PyAsn1Error(
107
+ 'Error encoding %r: %s' % (value, exc))
108
+
109
+ if LOG:
110
+ LOG('encoded %svalue %s into %s' % (
111
+ isConstructed and 'constructed ' or '', value, substrate
112
+ ))
113
+
114
+ if not substrate and isConstructed and options.get('ifNotEmpty', False):
115
+ return substrate
116
+
117
+ if not isConstructed:
118
+ defModeOverride = True
119
+
120
+ if LOG:
121
+ LOG('overridden encoding mode into definitive for primitive type')
122
+
123
+ header = self.encodeTag(singleTag, isConstructed)
124
+
125
+ if LOG:
126
+ LOG('encoded %stag %s into %s' % (
127
+ isConstructed and 'constructed ' or '',
128
+ singleTag, debug.hexdump(bytes(header))))
129
+
130
+ header += self.encodeLength(len(substrate), defModeOverride)
131
+
132
+ if LOG:
133
+ LOG('encoded %s octets (tag + payload) into %s' % (
134
+ len(substrate), debug.hexdump(bytes(header))))
135
+
136
+ if isOctets:
137
+ substrate = bytes(header) + substrate
138
+
139
+ if not defModeOverride:
140
+ substrate += self.eooOctetsSubstrate
141
+
142
+ else:
143
+ substrate = header + substrate
144
+
145
+ if not defModeOverride:
146
+ substrate += self.eooIntegerSubstrate
147
+
148
+ if not isOctets:
149
+ substrate = bytes(substrate)
150
+
151
+ return substrate
152
+
153
+
154
+ class EndOfOctetsEncoder(AbstractItemEncoder):
155
+ def encodeValue(self, value, asn1Spec, encodeFun, **options):
156
+ return b'', False, True
157
+
158
+
159
+ class BooleanEncoder(AbstractItemEncoder):
160
+ supportIndefLenMode = False
161
+
162
+ def encodeValue(self, value, asn1Spec, encodeFun, **options):
163
+ return value and (1,) or (0,), False, False
164
+
165
+
166
+ class IntegerEncoder(AbstractItemEncoder):
167
+ supportIndefLenMode = False
168
+ supportCompactZero = False
169
+
170
+ def encodeValue(self, value, asn1Spec, encodeFun, **options):
171
+ if value == 0:
172
+ if LOG:
173
+ LOG('encoding %spayload for zero INTEGER' % (
174
+ self.supportCompactZero and 'no ' or ''
175
+ ))
176
+
177
+ # de-facto way to encode zero
178
+ if self.supportCompactZero:
179
+ return (), False, False
180
+ else:
181
+ return (0,), False, False
182
+
183
+ return to_bytes(int(value), signed=True), False, True
184
+
185
+
186
+ class BitStringEncoder(AbstractItemEncoder):
187
+ def encodeValue(self, value, asn1Spec, encodeFun, **options):
188
+ if asn1Spec is not None:
189
+ # TODO: try to avoid ASN.1 schema instantiation
190
+ value = asn1Spec.clone(value)
191
+
192
+ valueLength = len(value)
193
+ if valueLength % 8:
194
+ alignedValue = value << (8 - valueLength % 8)
195
+ else:
196
+ alignedValue = value
197
+
198
+ maxChunkSize = options.get('maxChunkSize', 0)
199
+ if not maxChunkSize or len(alignedValue) <= maxChunkSize * 8:
200
+ substrate = alignedValue.asOctets()
201
+ return bytes((len(substrate) * 8 - valueLength,)) + substrate, False, True
202
+
203
+ if LOG:
204
+ LOG('encoding into up to %s-octet chunks' % maxChunkSize)
205
+
206
+ baseTag = value.tagSet.baseTag
207
+
208
+ # strip off explicit tags
209
+ if baseTag:
210
+ tagSet = tag.TagSet(baseTag, baseTag)
211
+
212
+ else:
213
+ tagSet = tag.TagSet()
214
+
215
+ alignedValue = alignedValue.clone(tagSet=tagSet)
216
+
217
+ stop = 0
218
+ substrate = b''
219
+ while stop < valueLength:
220
+ start = stop
221
+ stop = min(start + maxChunkSize * 8, valueLength)
222
+ substrate += encodeFun(alignedValue[start:stop], asn1Spec, **options)
223
+
224
+ return substrate, True, True
225
+
226
+
227
+ class OctetStringEncoder(AbstractItemEncoder):
228
+
229
+ def encodeValue(self, value, asn1Spec, encodeFun, **options):
230
+
231
+ if asn1Spec is None:
232
+ substrate = value.asOctets()
233
+
234
+ elif not isinstance(value, bytes):
235
+ substrate = asn1Spec.clone(value).asOctets()
236
+
237
+ else:
238
+ substrate = value
239
+
240
+ maxChunkSize = options.get('maxChunkSize', 0)
241
+
242
+ if not maxChunkSize or len(substrate) <= maxChunkSize:
243
+ return substrate, False, True
244
+
245
+ if LOG:
246
+ LOG('encoding into up to %s-octet chunks' % maxChunkSize)
247
+
248
+ # strip off explicit tags for inner chunks
249
+
250
+ if asn1Spec is None:
251
+ baseTag = value.tagSet.baseTag
252
+
253
+ # strip off explicit tags
254
+ if baseTag:
255
+ tagSet = tag.TagSet(baseTag, baseTag)
256
+
257
+ else:
258
+ tagSet = tag.TagSet()
259
+
260
+ asn1Spec = value.clone(tagSet=tagSet)
261
+
262
+ elif not isinstance(value, bytes):
263
+ baseTag = asn1Spec.tagSet.baseTag
264
+
265
+ # strip off explicit tags
266
+ if baseTag:
267
+ tagSet = tag.TagSet(baseTag, baseTag)
268
+
269
+ else:
270
+ tagSet = tag.TagSet()
271
+
272
+ asn1Spec = asn1Spec.clone(tagSet=tagSet)
273
+
274
+ pos = 0
275
+ substrate = b''
276
+
277
+ while True:
278
+ chunk = value[pos:pos + maxChunkSize]
279
+ if not chunk:
280
+ break
281
+
282
+ substrate += encodeFun(chunk, asn1Spec, **options)
283
+ pos += maxChunkSize
284
+
285
+ return substrate, True, True
286
+
287
+
288
+ class NullEncoder(AbstractItemEncoder):
289
+ supportIndefLenMode = False
290
+
291
+ def encodeValue(self, value, asn1Spec, encodeFun, **options):
292
+ return b'', False, True
293
+
294
+
295
+ class ObjectIdentifierEncoder(AbstractItemEncoder):
296
+ supportIndefLenMode = False
297
+
298
+ def encodeValue(self, value, asn1Spec, encodeFun, **options):
299
+ if asn1Spec is not None:
300
+ value = asn1Spec.clone(value)
301
+
302
+ oid = value.asTuple()
303
+
304
+ # Build the first pair
305
+ try:
306
+ first = oid[0]
307
+ second = oid[1]
308
+
309
+ except IndexError:
310
+ raise error.PyAsn1Error('Short OID %s' % (value,))
311
+
312
+ if 0 <= second <= 39:
313
+ if first == 1:
314
+ oid = (second + 40,) + oid[2:]
315
+ elif first == 0:
316
+ oid = (second,) + oid[2:]
317
+ elif first == 2:
318
+ oid = (second + 80,) + oid[2:]
319
+ else:
320
+ raise error.PyAsn1Error('Impossible first/second arcs at %s' % (value,))
321
+
322
+ elif first == 2:
323
+ oid = (second + 80,) + oid[2:]
324
+
325
+ else:
326
+ raise error.PyAsn1Error('Impossible first/second arcs at %s' % (value,))
327
+
328
+ octets = ()
329
+
330
+ # Cycle through subIds
331
+ for subOid in oid:
332
+ if 0 <= subOid <= 127:
333
+ # Optimize for the common case
334
+ octets += (subOid,)
335
+
336
+ elif subOid > 127:
337
+ # Pack large Sub-Object IDs
338
+ res = (subOid & 0x7f,)
339
+ subOid >>= 7
340
+
341
+ while subOid:
342
+ res = (0x80 | (subOid & 0x7f),) + res
343
+ subOid >>= 7
344
+
345
+ # Add packed Sub-Object ID to resulted Object ID
346
+ octets += res
347
+
348
+ else:
349
+ raise error.PyAsn1Error('Negative OID arc %s at %s' % (subOid, value))
350
+
351
+ return octets, False, False
352
+
353
+
354
+ class RelativeOIDEncoder(AbstractItemEncoder):
355
+ supportIndefLenMode = False
356
+
357
+ def encodeValue(self, value, asn1Spec, encodeFun, **options):
358
+ if asn1Spec is not None:
359
+ value = asn1Spec.clone(value)
360
+
361
+ octets = ()
362
+
363
+ # Cycle through subIds
364
+ for subOid in value.asTuple():
365
+ if 0 <= subOid <= 127:
366
+ # Optimize for the common case
367
+ octets += (subOid,)
368
+
369
+ elif subOid > 127:
370
+ # Pack large Sub-Object IDs
371
+ res = (subOid & 0x7f,)
372
+ subOid >>= 7
373
+
374
+ while subOid:
375
+ res = (0x80 | (subOid & 0x7f),) + res
376
+ subOid >>= 7
377
+
378
+ # Add packed Sub-Object ID to resulted RELATIVE-OID
379
+ octets += res
380
+
381
+ else:
382
+ raise error.PyAsn1Error('Negative RELATIVE-OID arc %s at %s' % (subOid, value))
383
+
384
+ return octets, False, False
385
+
386
+
387
+ class RealEncoder(AbstractItemEncoder):
388
+ supportIndefLenMode = False
389
+ binEncBase = 2 # set to None to choose encoding base automatically
390
+
391
+ @staticmethod
392
+ def _dropFloatingPoint(m, encbase, e):
393
+ ms, es = 1, 1
394
+ if m < 0:
395
+ ms = -1 # mantissa sign
396
+
397
+ if e < 0:
398
+ es = -1 # exponent sign
399
+
400
+ m *= ms
401
+
402
+ if encbase == 8:
403
+ m *= 2 ** (abs(e) % 3 * es)
404
+ e = abs(e) // 3 * es
405
+
406
+ elif encbase == 16:
407
+ m *= 2 ** (abs(e) % 4 * es)
408
+ e = abs(e) // 4 * es
409
+
410
+ while True:
411
+ if int(m) != m:
412
+ m *= encbase
413
+ e -= 1
414
+ continue
415
+ break
416
+
417
+ return ms, int(m), encbase, e
418
+
419
+ def _chooseEncBase(self, value):
420
+ m, b, e = value
421
+ encBase = [2, 8, 16]
422
+ if value.binEncBase in encBase:
423
+ return self._dropFloatingPoint(m, value.binEncBase, e)
424
+
425
+ elif self.binEncBase in encBase:
426
+ return self._dropFloatingPoint(m, self.binEncBase, e)
427
+
428
+ # auto choosing base 2/8/16
429
+ mantissa = [m, m, m]
430
+ exponent = [e, e, e]
431
+ sign = 1
432
+ encbase = 2
433
+ e = float('inf')
434
+
435
+ for i in range(3):
436
+ (sign,
437
+ mantissa[i],
438
+ encBase[i],
439
+ exponent[i]) = self._dropFloatingPoint(mantissa[i], encBase[i], exponent[i])
440
+
441
+ if abs(exponent[i]) < abs(e) or (abs(exponent[i]) == abs(e) and mantissa[i] < m):
442
+ e = exponent[i]
443
+ m = int(mantissa[i])
444
+ encbase = encBase[i]
445
+
446
+ if LOG:
447
+ LOG('automatically chosen REAL encoding base %s, sign %s, mantissa %s, '
448
+ 'exponent %s' % (encbase, sign, m, e))
449
+
450
+ return sign, m, encbase, e
451
+
452
+ def encodeValue(self, value, asn1Spec, encodeFun, **options):
453
+ if asn1Spec is not None:
454
+ value = asn1Spec.clone(value)
455
+
456
+ if value.isPlusInf:
457
+ return (0x40,), False, False
458
+
459
+ if value.isMinusInf:
460
+ return (0x41,), False, False
461
+
462
+ m, b, e = value
463
+
464
+ if not m:
465
+ return b'', False, True
466
+
467
+ if b == 10:
468
+ if LOG:
469
+ LOG('encoding REAL into character form')
470
+
471
+ return b'\x03%dE%s%d' % (m, e == 0 and b'+' or b'', e), False, True
472
+
473
+ elif b == 2:
474
+ fo = 0x80 # binary encoding
475
+ ms, m, encbase, e = self._chooseEncBase(value)
476
+
477
+ if ms < 0: # mantissa sign
478
+ fo |= 0x40 # sign bit
479
+
480
+ # exponent & mantissa normalization
481
+ if encbase == 2:
482
+ while m & 0x1 == 0:
483
+ m >>= 1
484
+ e += 1
485
+
486
+ elif encbase == 8:
487
+ while m & 0x7 == 0:
488
+ m >>= 3
489
+ e += 1
490
+ fo |= 0x10
491
+
492
+ else: # encbase = 16
493
+ while m & 0xf == 0:
494
+ m >>= 4
495
+ e += 1
496
+ fo |= 0x20
497
+
498
+ sf = 0 # scale factor
499
+
500
+ while m & 0x1 == 0:
501
+ m >>= 1
502
+ sf += 1
503
+
504
+ if sf > 3:
505
+ raise error.PyAsn1Error('Scale factor overflow') # bug if raised
506
+
507
+ fo |= sf << 2
508
+ eo = b''
509
+ if e == 0 or e == -1:
510
+ eo = bytes((e & 0xff,))
511
+
512
+ else:
513
+ while e not in (0, -1):
514
+ eo = bytes((e & 0xff,)) + eo
515
+ e >>= 8
516
+
517
+ if e == 0 and eo and eo[0] & 0x80:
518
+ eo = bytes((0,)) + eo
519
+
520
+ if e == -1 and eo and not (eo[0] & 0x80):
521
+ eo = bytes((0xff,)) + eo
522
+
523
+ n = len(eo)
524
+ if n > 0xff:
525
+ raise error.PyAsn1Error('Real exponent overflow')
526
+
527
+ if n == 1:
528
+ pass
529
+
530
+ elif n == 2:
531
+ fo |= 1
532
+
533
+ elif n == 3:
534
+ fo |= 2
535
+
536
+ else:
537
+ fo |= 3
538
+ eo = bytes((n & 0xff,)) + eo
539
+
540
+ po = b''
541
+
542
+ while m:
543
+ po = bytes((m & 0xff,)) + po
544
+ m >>= 8
545
+
546
+ substrate = bytes((fo,)) + eo + po
547
+
548
+ return substrate, False, True
549
+
550
+ else:
551
+ raise error.PyAsn1Error('Prohibited Real base %s' % b)
552
+
553
+
554
+ class SequenceEncoder(AbstractItemEncoder):
555
+ omitEmptyOptionals = False
556
+
557
+ # TODO: handling three flavors of input is too much -- split over codecs
558
+
559
+ def encodeValue(self, value, asn1Spec, encodeFun, **options):
560
+
561
+ substrate = b''
562
+
563
+ omitEmptyOptionals = options.get(
564
+ 'omitEmptyOptionals', self.omitEmptyOptionals)
565
+
566
+ if LOG:
567
+ LOG('%sencoding empty OPTIONAL components' % (
568
+ omitEmptyOptionals and 'not ' or ''))
569
+
570
+ if asn1Spec is None:
571
+ # instance of ASN.1 schema
572
+ inconsistency = value.isInconsistent
573
+ if inconsistency:
574
+ raise error.PyAsn1Error(
575
+ f"ASN.1 object {value.__class__.__name__} is inconsistent")
576
+
577
+ namedTypes = value.componentType
578
+
579
+ for idx, component in enumerate(value.values()):
580
+ if namedTypes:
581
+ namedType = namedTypes[idx]
582
+
583
+ if namedType.isOptional and not component.isValue:
584
+ if LOG:
585
+ LOG('not encoding OPTIONAL component %r' % (namedType,))
586
+ continue
587
+
588
+ if namedType.isDefaulted and component == namedType.asn1Object:
589
+ if LOG:
590
+ LOG('not encoding DEFAULT component %r' % (namedType,))
591
+ continue
592
+
593
+ if omitEmptyOptionals:
594
+ options.update(ifNotEmpty=namedType.isOptional)
595
+
596
+ # wrap open type blob if needed
597
+ if namedTypes and namedType.openType:
598
+
599
+ wrapType = namedType.asn1Object
600
+
601
+ if wrapType.typeId in (
602
+ univ.SetOf.typeId, univ.SequenceOf.typeId):
603
+
604
+ substrate += encodeFun(
605
+ component, asn1Spec,
606
+ **dict(options, wrapType=wrapType.componentType))
607
+
608
+ else:
609
+ chunk = encodeFun(component, asn1Spec, **options)
610
+
611
+ if wrapType.isSameTypeWith(component):
612
+ substrate += chunk
613
+
614
+ else:
615
+ substrate += encodeFun(chunk, wrapType, **options)
616
+
617
+ if LOG:
618
+ LOG('wrapped with wrap type %r' % (wrapType,))
619
+
620
+ else:
621
+ substrate += encodeFun(component, asn1Spec, **options)
622
+
623
+ else:
624
+ # bare Python value + ASN.1 schema
625
+ for idx, namedType in enumerate(asn1Spec.componentType.namedTypes):
626
+
627
+ try:
628
+ component = value[namedType.name]
629
+
630
+ except KeyError:
631
+ raise error.PyAsn1Error('Component name "%s" not found in %r' % (
632
+ namedType.name, value))
633
+
634
+ if namedType.isOptional and namedType.name not in value:
635
+ if LOG:
636
+ LOG('not encoding OPTIONAL component %r' % (namedType,))
637
+ continue
638
+
639
+ if namedType.isDefaulted and component == namedType.asn1Object:
640
+ if LOG:
641
+ LOG('not encoding DEFAULT component %r' % (namedType,))
642
+ continue
643
+
644
+ if omitEmptyOptionals:
645
+ options.update(ifNotEmpty=namedType.isOptional)
646
+
647
+ componentSpec = namedType.asn1Object
648
+
649
+ # wrap open type blob if needed
650
+ if namedType.openType:
651
+
652
+ if componentSpec.typeId in (
653
+ univ.SetOf.typeId, univ.SequenceOf.typeId):
654
+
655
+ substrate += encodeFun(
656
+ component, componentSpec,
657
+ **dict(options, wrapType=componentSpec.componentType))
658
+
659
+ else:
660
+ chunk = encodeFun(component, componentSpec, **options)
661
+
662
+ if componentSpec.isSameTypeWith(component):
663
+ substrate += chunk
664
+
665
+ else:
666
+ substrate += encodeFun(chunk, componentSpec, **options)
667
+
668
+ if LOG:
669
+ LOG('wrapped with wrap type %r' % (componentSpec,))
670
+
671
+ else:
672
+ substrate += encodeFun(component, componentSpec, **options)
673
+
674
+ return substrate, True, True
675
+
676
+
677
+ class SequenceOfEncoder(AbstractItemEncoder):
678
+ def _encodeComponents(self, value, asn1Spec, encodeFun, **options):
679
+
680
+ if asn1Spec is None:
681
+ inconsistency = value.isInconsistent
682
+ if inconsistency:
683
+ raise error.PyAsn1Error(
684
+ f"ASN.1 object {value.__class__.__name__} is inconsistent")
685
+
686
+ else:
687
+ asn1Spec = asn1Spec.componentType
688
+
689
+ chunks = []
690
+
691
+ wrapType = options.pop('wrapType', None)
692
+
693
+ for idx, component in enumerate(value):
694
+ chunk = encodeFun(component, asn1Spec, **options)
695
+
696
+ if (wrapType is not None and
697
+ not wrapType.isSameTypeWith(component)):
698
+ # wrap encoded value with wrapper container (e.g. ANY)
699
+ chunk = encodeFun(chunk, wrapType, **options)
700
+
701
+ if LOG:
702
+ LOG('wrapped with wrap type %r' % (wrapType,))
703
+
704
+ chunks.append(chunk)
705
+
706
+ return chunks
707
+
708
+ def encodeValue(self, value, asn1Spec, encodeFun, **options):
709
+ chunks = self._encodeComponents(
710
+ value, asn1Spec, encodeFun, **options)
711
+
712
+ return b''.join(chunks), True, True
713
+
714
+
715
+ class ChoiceEncoder(AbstractItemEncoder):
716
+ def encodeValue(self, value, asn1Spec, encodeFun, **options):
717
+ if asn1Spec is None:
718
+ component = value.getComponent()
719
+ else:
720
+ names = [namedType.name for namedType in asn1Spec.componentType.namedTypes
721
+ if namedType.name in value]
722
+ if len(names) != 1:
723
+ raise error.PyAsn1Error('%s components for Choice at %r' % (len(names) and 'Multiple ' or 'None ', value))
724
+
725
+ name = names[0]
726
+
727
+ component = value[name]
728
+ asn1Spec = asn1Spec[name]
729
+
730
+ return encodeFun(component, asn1Spec, **options), True, True
731
+
732
+
733
+ class AnyEncoder(OctetStringEncoder):
734
+ def encodeValue(self, value, asn1Spec, encodeFun, **options):
735
+ if asn1Spec is None:
736
+ value = value.asOctets()
737
+ elif not isinstance(value, bytes):
738
+ value = asn1Spec.clone(value).asOctets()
739
+
740
+ return value, not options.get('defMode', True), True
741
+
742
+
743
+ TAG_MAP = {
744
+ eoo.endOfOctets.tagSet: EndOfOctetsEncoder(),
745
+ univ.Boolean.tagSet: BooleanEncoder(),
746
+ univ.Integer.tagSet: IntegerEncoder(),
747
+ univ.BitString.tagSet: BitStringEncoder(),
748
+ univ.OctetString.tagSet: OctetStringEncoder(),
749
+ univ.Null.tagSet: NullEncoder(),
750
+ univ.ObjectIdentifier.tagSet: ObjectIdentifierEncoder(),
751
+ univ.RelativeOID.tagSet: RelativeOIDEncoder(),
752
+ univ.Enumerated.tagSet: IntegerEncoder(),
753
+ univ.Real.tagSet: RealEncoder(),
754
+ # Sequence & Set have same tags as SequenceOf & SetOf
755
+ univ.SequenceOf.tagSet: SequenceOfEncoder(),
756
+ univ.SetOf.tagSet: SequenceOfEncoder(),
757
+ univ.Choice.tagSet: ChoiceEncoder(),
758
+ # character string types
759
+ char.UTF8String.tagSet: OctetStringEncoder(),
760
+ char.NumericString.tagSet: OctetStringEncoder(),
761
+ char.PrintableString.tagSet: OctetStringEncoder(),
762
+ char.TeletexString.tagSet: OctetStringEncoder(),
763
+ char.VideotexString.tagSet: OctetStringEncoder(),
764
+ char.IA5String.tagSet: OctetStringEncoder(),
765
+ char.GraphicString.tagSet: OctetStringEncoder(),
766
+ char.VisibleString.tagSet: OctetStringEncoder(),
767
+ char.GeneralString.tagSet: OctetStringEncoder(),
768
+ char.UniversalString.tagSet: OctetStringEncoder(),
769
+ char.BMPString.tagSet: OctetStringEncoder(),
770
+ # useful types
771
+ useful.ObjectDescriptor.tagSet: OctetStringEncoder(),
772
+ useful.GeneralizedTime.tagSet: OctetStringEncoder(),
773
+ useful.UTCTime.tagSet: OctetStringEncoder()
774
+ }
775
+
776
+ # Put in ambiguous & non-ambiguous types for faster codec lookup
777
+ TYPE_MAP = {
778
+ univ.Boolean.typeId: BooleanEncoder(),
779
+ univ.Integer.typeId: IntegerEncoder(),
780
+ univ.BitString.typeId: BitStringEncoder(),
781
+ univ.OctetString.typeId: OctetStringEncoder(),
782
+ univ.Null.typeId: NullEncoder(),
783
+ univ.ObjectIdentifier.typeId: ObjectIdentifierEncoder(),
784
+ univ.RelativeOID.typeId: RelativeOIDEncoder(),
785
+ univ.Enumerated.typeId: IntegerEncoder(),
786
+ univ.Real.typeId: RealEncoder(),
787
+ # Sequence & Set have same tags as SequenceOf & SetOf
788
+ univ.Set.typeId: SequenceEncoder(),
789
+ univ.SetOf.typeId: SequenceOfEncoder(),
790
+ univ.Sequence.typeId: SequenceEncoder(),
791
+ univ.SequenceOf.typeId: SequenceOfEncoder(),
792
+ univ.Choice.typeId: ChoiceEncoder(),
793
+ univ.Any.typeId: AnyEncoder(),
794
+ # character string types
795
+ char.UTF8String.typeId: OctetStringEncoder(),
796
+ char.NumericString.typeId: OctetStringEncoder(),
797
+ char.PrintableString.typeId: OctetStringEncoder(),
798
+ char.TeletexString.typeId: OctetStringEncoder(),
799
+ char.VideotexString.typeId: OctetStringEncoder(),
800
+ char.IA5String.typeId: OctetStringEncoder(),
801
+ char.GraphicString.typeId: OctetStringEncoder(),
802
+ char.VisibleString.typeId: OctetStringEncoder(),
803
+ char.GeneralString.typeId: OctetStringEncoder(),
804
+ char.UniversalString.typeId: OctetStringEncoder(),
805
+ char.BMPString.typeId: OctetStringEncoder(),
806
+ # useful types
807
+ useful.ObjectDescriptor.typeId: OctetStringEncoder(),
808
+ useful.GeneralizedTime.typeId: OctetStringEncoder(),
809
+ useful.UTCTime.typeId: OctetStringEncoder()
810
+ }
811
+
812
+
813
+ class SingleItemEncoder(object):
814
+ fixedDefLengthMode = None
815
+ fixedChunkSize = None
816
+
817
+ TAG_MAP = TAG_MAP
818
+ TYPE_MAP = TYPE_MAP
819
+
820
+ def __init__(self, tagMap=_MISSING, typeMap=_MISSING, **ignored):
821
+ self._tagMap = tagMap if tagMap is not _MISSING else self.TAG_MAP
822
+ self._typeMap = typeMap if typeMap is not _MISSING else self.TYPE_MAP
823
+
824
+ def __call__(self, value, asn1Spec=None, **options):
825
+ try:
826
+ if asn1Spec is None:
827
+ typeId = value.typeId
828
+ else:
829
+ typeId = asn1Spec.typeId
830
+
831
+ except AttributeError:
832
+ raise error.PyAsn1Error('Value %r is not ASN.1 type instance '
833
+ 'and "asn1Spec" not given' % (value,))
834
+
835
+ if LOG:
836
+ LOG('encoder called in %sdef mode, chunk size %s for type %s, '
837
+ 'value:\n%s' % (not options.get('defMode', True) and 'in' or '',
838
+ options.get('maxChunkSize', 0),
839
+ asn1Spec is None and value.prettyPrintType() or
840
+ asn1Spec.prettyPrintType(), value))
841
+
842
+ if self.fixedDefLengthMode is not None:
843
+ options.update(defMode=self.fixedDefLengthMode)
844
+
845
+ if self.fixedChunkSize is not None:
846
+ options.update(maxChunkSize=self.fixedChunkSize)
847
+
848
+ try:
849
+ concreteEncoder = self._typeMap[typeId]
850
+
851
+ if LOG:
852
+ LOG('using value codec %s chosen by type ID '
853
+ '%s' % (concreteEncoder.__class__.__name__, typeId))
854
+
855
+ except KeyError:
856
+ if asn1Spec is None:
857
+ tagSet = value.tagSet
858
+ else:
859
+ tagSet = asn1Spec.tagSet
860
+
861
+ # use base type for codec lookup to recover untagged types
862
+ baseTagSet = tag.TagSet(tagSet.baseTag, tagSet.baseTag)
863
+
864
+ try:
865
+ concreteEncoder = self._tagMap[baseTagSet]
866
+
867
+ except KeyError:
868
+ raise error.PyAsn1Error('No encoder for %r (%s)' % (value, tagSet))
869
+
870
+ if LOG:
871
+ LOG('using value codec %s chosen by tagSet '
872
+ '%s' % (concreteEncoder.__class__.__name__, tagSet))
873
+
874
+ substrate = concreteEncoder.encode(value, asn1Spec, self, **options)
875
+
876
+ if LOG:
877
+ LOG('codec %s built %s octets of substrate: %s\nencoder '
878
+ 'completed' % (concreteEncoder, len(substrate),
879
+ debug.hexdump(substrate)))
880
+
881
+ return substrate
882
+
883
+
884
+ class Encoder(object):
885
+ SINGLE_ITEM_ENCODER = SingleItemEncoder
886
+
887
+ def __init__(self, tagMap=_MISSING, typeMap=_MISSING, **options):
888
+ self._singleItemEncoder = self.SINGLE_ITEM_ENCODER(
889
+ tagMap=tagMap, typeMap=typeMap, **options
890
+ )
891
+
892
+ def __call__(self, pyObject, asn1Spec=None, **options):
893
+ return self._singleItemEncoder(
894
+ pyObject, asn1Spec=asn1Spec, **options)
895
+
896
+
897
+ #: Turns ASN.1 object into BER octet stream.
898
+ #:
899
+ #: Takes any ASN.1 object (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
900
+ #: walks all its components recursively and produces a BER octet stream.
901
+ #:
902
+ #: Parameters
903
+ #: ----------
904
+ #: value: either a Python or pyasn1 object (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
905
+ #: A Python or pyasn1 object to encode. If Python object is given, `asnSpec`
906
+ #: parameter is required to guide the encoding process.
907
+ #:
908
+ #: Keyword Args
909
+ #: ------------
910
+ #: asn1Spec:
911
+ #: Optional ASN.1 schema or value object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
912
+ #:
913
+ #: defMode: :py:class:`bool`
914
+ #: If :obj:`False`, produces indefinite length encoding
915
+ #:
916
+ #: maxChunkSize: :py:class:`int`
917
+ #: Maximum chunk size in chunked encoding mode (0 denotes unlimited chunk size)
918
+ #:
919
+ #: Returns
920
+ #: -------
921
+ #: : :py:class:`bytes`
922
+ #: Given ASN.1 object encoded into BER octetstream
923
+ #:
924
+ #: Raises
925
+ #: ------
926
+ #: ~pyasn1.error.PyAsn1Error
927
+ #: On encoding errors
928
+ #:
929
+ #: Examples
930
+ #: --------
931
+ #: Encode Python value into BER with ASN.1 schema
932
+ #:
933
+ #: .. code-block:: pycon
934
+ #:
935
+ #: >>> seq = SequenceOf(componentType=Integer())
936
+ #: >>> encode([1, 2, 3], asn1Spec=seq)
937
+ #: b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03'
938
+ #:
939
+ #: Encode ASN.1 value object into BER
940
+ #:
941
+ #: .. code-block:: pycon
942
+ #:
943
+ #: >>> seq = SequenceOf(componentType=Integer())
944
+ #: >>> seq.extend([1, 2, 3])
945
+ #: >>> encode(seq)
946
+ #: b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03'
947
+ #:
948
+ encode = Encoder()
949
+
950
+ def __getattr__(attr: str):
951
+ if newAttr := {"tagMap": "TAG_MAP", "typeMap": "TYPE_MAP"}.get(attr):
952
+ warnings.warn(f"{attr} is deprecated. Please use {newAttr} instead.", DeprecationWarning, stacklevel=2)
953
+ return globals()[newAttr]
954
+ raise AttributeError(attr)
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/ber/eoo.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ #
7
+ from pyasn1.type import base
8
+ from pyasn1.type import tag
9
+
10
+ __all__ = ['endOfOctets']
11
+
12
+
13
+ class EndOfOctets(base.SimpleAsn1Type):
14
+ defaultValue = 0
15
+ tagSet = tag.initTagSet(
16
+ tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x00)
17
+ )
18
+
19
+ _instance = None
20
+
21
+ def __new__(cls, *args, **kwargs):
22
+ if cls._instance is None:
23
+ cls._instance = object.__new__(cls, *args, **kwargs)
24
+
25
+ return cls._instance
26
+
27
+
28
+ endOfOctets = EndOfOctets()
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/cer/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ # This file is necessary to make this directory a package.
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/cer/__pycache__/__init__.cpython-313.pyc ADDED
Binary file (180 Bytes). View file
 
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/cer/__pycache__/decoder.cpython-313.pyc ADDED
Binary file (4.32 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/cer/__pycache__/encoder.cpython-313.pyc ADDED
Binary file (12 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/cer/decoder.py ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ #
7
+ import warnings
8
+
9
+ from pyasn1 import error
10
+ from pyasn1.codec.streaming import readFromStream
11
+ from pyasn1.codec.ber import decoder
12
+ from pyasn1.type import univ
13
+
14
+ __all__ = ['decode', 'StreamingDecoder']
15
+
16
+ SubstrateUnderrunError = error.SubstrateUnderrunError
17
+
18
+
19
+ class BooleanPayloadDecoder(decoder.AbstractSimplePayloadDecoder):
20
+ protoComponent = univ.Boolean(0)
21
+
22
+ def valueDecoder(self, substrate, asn1Spec,
23
+ tagSet=None, length=None, state=None,
24
+ decodeFun=None, substrateFun=None,
25
+ **options):
26
+
27
+ if length != 1:
28
+ raise error.PyAsn1Error('Not single-octet Boolean payload')
29
+
30
+ for chunk in readFromStream(substrate, length, options):
31
+ if isinstance(chunk, SubstrateUnderrunError):
32
+ yield chunk
33
+
34
+ byte = chunk[0]
35
+
36
+ # CER/DER specifies encoding of TRUE as 0xFF and FALSE as 0x0, while
37
+ # BER allows any non-zero value as TRUE; cf. sections 8.2.2. and 11.1
38
+ # in https://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
39
+ if byte == 0xff:
40
+ value = 1
41
+
42
+ elif byte == 0x00:
43
+ value = 0
44
+
45
+ else:
46
+ raise error.PyAsn1Error('Unexpected Boolean payload: %s' % byte)
47
+
48
+ yield self._createComponent(asn1Spec, tagSet, value, **options)
49
+
50
+
51
+ # TODO: prohibit non-canonical encoding
52
+ BitStringPayloadDecoder = decoder.BitStringPayloadDecoder
53
+ OctetStringPayloadDecoder = decoder.OctetStringPayloadDecoder
54
+ RealPayloadDecoder = decoder.RealPayloadDecoder
55
+
56
+ TAG_MAP = decoder.TAG_MAP.copy()
57
+ TAG_MAP.update(
58
+ {univ.Boolean.tagSet: BooleanPayloadDecoder(),
59
+ univ.BitString.tagSet: BitStringPayloadDecoder(),
60
+ univ.OctetString.tagSet: OctetStringPayloadDecoder(),
61
+ univ.Real.tagSet: RealPayloadDecoder()}
62
+ )
63
+
64
+ TYPE_MAP = decoder.TYPE_MAP.copy()
65
+
66
+ # Put in non-ambiguous types for faster codec lookup
67
+ for typeDecoder in TAG_MAP.values():
68
+ if typeDecoder.protoComponent is not None:
69
+ typeId = typeDecoder.protoComponent.__class__.typeId
70
+ if typeId is not None and typeId not in TYPE_MAP:
71
+ TYPE_MAP[typeId] = typeDecoder
72
+
73
+
74
+ class SingleItemDecoder(decoder.SingleItemDecoder):
75
+ __doc__ = decoder.SingleItemDecoder.__doc__
76
+
77
+ TAG_MAP = TAG_MAP
78
+ TYPE_MAP = TYPE_MAP
79
+
80
+
81
+ class StreamingDecoder(decoder.StreamingDecoder):
82
+ __doc__ = decoder.StreamingDecoder.__doc__
83
+
84
+ SINGLE_ITEM_DECODER = SingleItemDecoder
85
+
86
+
87
+ class Decoder(decoder.Decoder):
88
+ __doc__ = decoder.Decoder.__doc__
89
+
90
+ STREAMING_DECODER = StreamingDecoder
91
+
92
+
93
+ #: Turns CER octet stream into an ASN.1 object.
94
+ #:
95
+ #: Takes CER octet-stream and decode it into an ASN.1 object
96
+ #: (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative) which
97
+ #: may be a scalar or an arbitrary nested structure.
98
+ #:
99
+ #: Parameters
100
+ #: ----------
101
+ #: substrate: :py:class:`bytes`
102
+ #: CER octet-stream
103
+ #:
104
+ #: Keyword Args
105
+ #: ------------
106
+ #: asn1Spec: any pyasn1 type object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
107
+ #: A pyasn1 type object to act as a template guiding the decoder. Depending on the ASN.1 structure
108
+ #: being decoded, *asn1Spec* may or may not be required. Most common reason for
109
+ #: it to require is that ASN.1 structure is encoded in *IMPLICIT* tagging mode.
110
+ #:
111
+ #: Returns
112
+ #: -------
113
+ #: : :py:class:`tuple`
114
+ #: A tuple of pyasn1 object recovered from CER substrate (:py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
115
+ #: and the unprocessed trailing portion of the *substrate* (may be empty)
116
+ #:
117
+ #: Raises
118
+ #: ------
119
+ #: ~pyasn1.error.PyAsn1Error, ~pyasn1.error.SubstrateUnderrunError
120
+ #: On decoding errors
121
+ #:
122
+ #: Examples
123
+ #: --------
124
+ #: Decode CER serialisation without ASN.1 schema
125
+ #:
126
+ #: .. code-block:: pycon
127
+ #:
128
+ #: >>> s, _ = decode(b'0\x80\x02\x01\x01\x02\x01\x02\x02\x01\x03\x00\x00')
129
+ #: >>> str(s)
130
+ #: SequenceOf:
131
+ #: 1 2 3
132
+ #:
133
+ #: Decode CER serialisation with ASN.1 schema
134
+ #:
135
+ #: .. code-block:: pycon
136
+ #:
137
+ #: >>> seq = SequenceOf(componentType=Integer())
138
+ #: >>> s, _ = decode(b'0\x80\x02\x01\x01\x02\x01\x02\x02\x01\x03\x00\x00', asn1Spec=seq)
139
+ #: >>> str(s)
140
+ #: SequenceOf:
141
+ #: 1 2 3
142
+ #:
143
+ decode = Decoder()
144
+
145
+ def __getattr__(attr: str):
146
+ if newAttr := {"tagMap": "TAG_MAP", "typeMap": "TYPE_MAP"}.get(attr):
147
+ warnings.warn(f"{attr} is deprecated. Please use {newAttr} instead.", DeprecationWarning, stacklevel=2)
148
+ return globals()[newAttr]
149
+ raise AttributeError(attr)
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/cer/encoder.py ADDED
@@ -0,0 +1,331 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ #
7
+ import warnings
8
+
9
+ from pyasn1 import error
10
+ from pyasn1.codec.ber import encoder
11
+ from pyasn1.type import univ
12
+ from pyasn1.type import useful
13
+
14
+ __all__ = ['Encoder', 'encode']
15
+
16
+
17
+ class BooleanEncoder(encoder.IntegerEncoder):
18
+ def encodeValue(self, value, asn1Spec, encodeFun, **options):
19
+ if value == 0:
20
+ substrate = (0,)
21
+ else:
22
+ substrate = (255,)
23
+ return substrate, False, False
24
+
25
+
26
+ class RealEncoder(encoder.RealEncoder):
27
+ def _chooseEncBase(self, value):
28
+ m, b, e = value
29
+ return self._dropFloatingPoint(m, b, e)
30
+
31
+
32
+ # specialized GeneralStringEncoder here
33
+
34
+ class TimeEncoderMixIn(object):
35
+ Z_CHAR = ord('Z')
36
+ PLUS_CHAR = ord('+')
37
+ MINUS_CHAR = ord('-')
38
+ COMMA_CHAR = ord(',')
39
+ DOT_CHAR = ord('.')
40
+ ZERO_CHAR = ord('0')
41
+
42
+ MIN_LENGTH = 12
43
+ MAX_LENGTH = 19
44
+
45
+ def encodeValue(self, value, asn1Spec, encodeFun, **options):
46
+ # CER encoding constraints:
47
+ # - minutes are mandatory, seconds are optional
48
+ # - sub-seconds must NOT be zero / no meaningless zeros
49
+ # - no hanging fraction dot
50
+ # - time in UTC (Z)
51
+ # - only dot is allowed for fractions
52
+
53
+ if asn1Spec is not None:
54
+ value = asn1Spec.clone(value)
55
+
56
+ numbers = value.asNumbers()
57
+
58
+ if self.PLUS_CHAR in numbers or self.MINUS_CHAR in numbers:
59
+ raise error.PyAsn1Error('Must be UTC time: %r' % value)
60
+
61
+ if numbers[-1] != self.Z_CHAR:
62
+ raise error.PyAsn1Error('Missing "Z" time zone specifier: %r' % value)
63
+
64
+ if self.COMMA_CHAR in numbers:
65
+ raise error.PyAsn1Error('Comma in fractions disallowed: %r' % value)
66
+
67
+ if self.DOT_CHAR in numbers:
68
+
69
+ isModified = False
70
+
71
+ numbers = list(numbers)
72
+
73
+ searchIndex = min(numbers.index(self.DOT_CHAR) + 4, len(numbers) - 1)
74
+
75
+ while numbers[searchIndex] != self.DOT_CHAR:
76
+ if numbers[searchIndex] == self.ZERO_CHAR:
77
+ del numbers[searchIndex]
78
+ isModified = True
79
+
80
+ searchIndex -= 1
81
+
82
+ searchIndex += 1
83
+
84
+ if searchIndex < len(numbers):
85
+ if numbers[searchIndex] == self.Z_CHAR:
86
+ # drop hanging comma
87
+ del numbers[searchIndex - 1]
88
+ isModified = True
89
+
90
+ if isModified:
91
+ value = value.clone(numbers)
92
+
93
+ if not self.MIN_LENGTH < len(numbers) < self.MAX_LENGTH:
94
+ raise error.PyAsn1Error('Length constraint violated: %r' % value)
95
+
96
+ options.update(maxChunkSize=1000)
97
+
98
+ return encoder.OctetStringEncoder.encodeValue(
99
+ self, value, asn1Spec, encodeFun, **options
100
+ )
101
+
102
+
103
+ class GeneralizedTimeEncoder(TimeEncoderMixIn, encoder.OctetStringEncoder):
104
+ MIN_LENGTH = 12
105
+ MAX_LENGTH = 20
106
+
107
+
108
+ class UTCTimeEncoder(TimeEncoderMixIn, encoder.OctetStringEncoder):
109
+ MIN_LENGTH = 10
110
+ MAX_LENGTH = 14
111
+
112
+
113
+ class SetOfEncoder(encoder.SequenceOfEncoder):
114
+ def encodeValue(self, value, asn1Spec, encodeFun, **options):
115
+ chunks = self._encodeComponents(
116
+ value, asn1Spec, encodeFun, **options)
117
+
118
+ # sort by serialised and padded components
119
+ if len(chunks) > 1:
120
+ zero = b'\x00'
121
+ maxLen = max(map(len, chunks))
122
+ paddedChunks = [
123
+ (x.ljust(maxLen, zero), x) for x in chunks
124
+ ]
125
+ paddedChunks.sort(key=lambda x: x[0])
126
+
127
+ chunks = [x[1] for x in paddedChunks]
128
+
129
+ return b''.join(chunks), True, True
130
+
131
+
132
+ class SequenceOfEncoder(encoder.SequenceOfEncoder):
133
+ def encodeValue(self, value, asn1Spec, encodeFun, **options):
134
+
135
+ if options.get('ifNotEmpty', False) and not len(value):
136
+ return b'', True, True
137
+
138
+ chunks = self._encodeComponents(
139
+ value, asn1Spec, encodeFun, **options)
140
+
141
+ return b''.join(chunks), True, True
142
+
143
+
144
+ class SetEncoder(encoder.SequenceEncoder):
145
+ @staticmethod
146
+ def _componentSortKey(componentAndType):
147
+ """Sort SET components by tag
148
+
149
+ Sort regardless of the Choice value (static sort)
150
+ """
151
+ component, asn1Spec = componentAndType
152
+
153
+ if asn1Spec is None:
154
+ asn1Spec = component
155
+
156
+ if asn1Spec.typeId == univ.Choice.typeId and not asn1Spec.tagSet:
157
+ if asn1Spec.tagSet:
158
+ return asn1Spec.tagSet
159
+ else:
160
+ return asn1Spec.componentType.minTagSet
161
+ else:
162
+ return asn1Spec.tagSet
163
+
164
+ def encodeValue(self, value, asn1Spec, encodeFun, **options):
165
+
166
+ substrate = b''
167
+
168
+ comps = []
169
+ compsMap = {}
170
+
171
+ if asn1Spec is None:
172
+ # instance of ASN.1 schema
173
+ inconsistency = value.isInconsistent
174
+ if inconsistency:
175
+ raise error.PyAsn1Error(
176
+ f"ASN.1 object {value.__class__.__name__} is inconsistent")
177
+
178
+ namedTypes = value.componentType
179
+
180
+ for idx, component in enumerate(value.values()):
181
+ if namedTypes:
182
+ namedType = namedTypes[idx]
183
+
184
+ if namedType.isOptional and not component.isValue:
185
+ continue
186
+
187
+ if namedType.isDefaulted and component == namedType.asn1Object:
188
+ continue
189
+
190
+ compsMap[id(component)] = namedType
191
+
192
+ else:
193
+ compsMap[id(component)] = None
194
+
195
+ comps.append((component, asn1Spec))
196
+
197
+ else:
198
+ # bare Python value + ASN.1 schema
199
+ for idx, namedType in enumerate(asn1Spec.componentType.namedTypes):
200
+
201
+ try:
202
+ component = value[namedType.name]
203
+
204
+ except KeyError:
205
+ raise error.PyAsn1Error('Component name "%s" not found in %r' % (namedType.name, value))
206
+
207
+ if namedType.isOptional and namedType.name not in value:
208
+ continue
209
+
210
+ if namedType.isDefaulted and component == namedType.asn1Object:
211
+ continue
212
+
213
+ compsMap[id(component)] = namedType
214
+ comps.append((component, asn1Spec[idx]))
215
+
216
+ for comp, compType in sorted(comps, key=self._componentSortKey):
217
+ namedType = compsMap[id(comp)]
218
+
219
+ if namedType:
220
+ options.update(ifNotEmpty=namedType.isOptional)
221
+
222
+ chunk = encodeFun(comp, compType, **options)
223
+
224
+ # wrap open type blob if needed
225
+ if namedType and namedType.openType:
226
+ wrapType = namedType.asn1Object
227
+ if wrapType.tagSet and not wrapType.isSameTypeWith(comp):
228
+ chunk = encodeFun(chunk, wrapType, **options)
229
+
230
+ substrate += chunk
231
+
232
+ return substrate, True, True
233
+
234
+
235
+ class SequenceEncoder(encoder.SequenceEncoder):
236
+ omitEmptyOptionals = True
237
+
238
+
239
+ TAG_MAP = encoder.TAG_MAP.copy()
240
+
241
+ TAG_MAP.update({
242
+ univ.Boolean.tagSet: BooleanEncoder(),
243
+ univ.Real.tagSet: RealEncoder(),
244
+ useful.GeneralizedTime.tagSet: GeneralizedTimeEncoder(),
245
+ useful.UTCTime.tagSet: UTCTimeEncoder(),
246
+ # Sequence & Set have same tags as SequenceOf & SetOf
247
+ univ.SetOf.tagSet: SetOfEncoder(),
248
+ univ.Sequence.typeId: SequenceEncoder()
249
+ })
250
+
251
+ TYPE_MAP = encoder.TYPE_MAP.copy()
252
+
253
+ TYPE_MAP.update({
254
+ univ.Boolean.typeId: BooleanEncoder(),
255
+ univ.Real.typeId: RealEncoder(),
256
+ useful.GeneralizedTime.typeId: GeneralizedTimeEncoder(),
257
+ useful.UTCTime.typeId: UTCTimeEncoder(),
258
+ # Sequence & Set have same tags as SequenceOf & SetOf
259
+ univ.Set.typeId: SetEncoder(),
260
+ univ.SetOf.typeId: SetOfEncoder(),
261
+ univ.Sequence.typeId: SequenceEncoder(),
262
+ univ.SequenceOf.typeId: SequenceOfEncoder()
263
+ })
264
+
265
+
266
+ class SingleItemEncoder(encoder.SingleItemEncoder):
267
+ fixedDefLengthMode = False
268
+ fixedChunkSize = 1000
269
+
270
+ TAG_MAP = TAG_MAP
271
+ TYPE_MAP = TYPE_MAP
272
+
273
+
274
+ class Encoder(encoder.Encoder):
275
+ SINGLE_ITEM_ENCODER = SingleItemEncoder
276
+
277
+
278
+ #: Turns ASN.1 object into CER octet stream.
279
+ #:
280
+ #: Takes any ASN.1 object (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
281
+ #: walks all its components recursively and produces a CER octet stream.
282
+ #:
283
+ #: Parameters
284
+ #: ----------
285
+ #: value: either a Python or pyasn1 object (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
286
+ #: A Python or pyasn1 object to encode. If Python object is given, `asnSpec`
287
+ #: parameter is required to guide the encoding process.
288
+ #:
289
+ #: Keyword Args
290
+ #: ------------
291
+ #: asn1Spec:
292
+ #: Optional ASN.1 schema or value object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
293
+ #:
294
+ #: Returns
295
+ #: -------
296
+ #: : :py:class:`bytes`
297
+ #: Given ASN.1 object encoded into BER octet-stream
298
+ #:
299
+ #: Raises
300
+ #: ------
301
+ #: ~pyasn1.error.PyAsn1Error
302
+ #: On encoding errors
303
+ #:
304
+ #: Examples
305
+ #: --------
306
+ #: Encode Python value into CER with ASN.1 schema
307
+ #:
308
+ #: .. code-block:: pycon
309
+ #:
310
+ #: >>> seq = SequenceOf(componentType=Integer())
311
+ #: >>> encode([1, 2, 3], asn1Spec=seq)
312
+ #: b'0\x80\x02\x01\x01\x02\x01\x02\x02\x01\x03\x00\x00'
313
+ #:
314
+ #: Encode ASN.1 value object into CER
315
+ #:
316
+ #: .. code-block:: pycon
317
+ #:
318
+ #: >>> seq = SequenceOf(componentType=Integer())
319
+ #: >>> seq.extend([1, 2, 3])
320
+ #: >>> encode(seq)
321
+ #: b'0\x80\x02\x01\x01\x02\x01\x02\x02\x01\x03\x00\x00'
322
+ #:
323
+ encode = Encoder()
324
+
325
+ # EncoderFactory queries class instance and builds a map of tags -> encoders
326
+
327
+ def __getattr__(attr: str):
328
+ if newAttr := {"tagMap": "TAG_MAP", "typeMap": "TYPE_MAP"}.get(attr):
329
+ warnings.warn(f"{attr} is deprecated. Please use {newAttr} instead.", DeprecationWarning, stacklevel=2)
330
+ return globals()[newAttr]
331
+ raise AttributeError(attr)
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/der/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ # This file is necessary to make this directory a package.
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/der/__pycache__/__init__.cpython-313.pyc ADDED
Binary file (180 Bytes). View file
 
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/der/__pycache__/decoder.cpython-313.pyc ADDED
Binary file (3.26 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/der/__pycache__/encoder.cpython-313.pyc ADDED
Binary file (3.44 kB). View file
 
data/miniconda3/lib/python3.13/site-packages/pyasn1/codec/der/decoder.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ #
7
+ import warnings
8
+
9
+ from pyasn1.codec.cer import decoder
10
+ from pyasn1.type import univ
11
+
12
+ __all__ = ['decode', 'StreamingDecoder']
13
+
14
+
15
+ class BitStringPayloadDecoder(decoder.BitStringPayloadDecoder):
16
+ supportConstructedForm = False
17
+
18
+
19
+ class OctetStringPayloadDecoder(decoder.OctetStringPayloadDecoder):
20
+ supportConstructedForm = False
21
+
22
+
23
+ # TODO: prohibit non-canonical encoding
24
+ RealPayloadDecoder = decoder.RealPayloadDecoder
25
+
26
+ TAG_MAP = decoder.TAG_MAP.copy()
27
+ TAG_MAP.update(
28
+ {univ.BitString.tagSet: BitStringPayloadDecoder(),
29
+ univ.OctetString.tagSet: OctetStringPayloadDecoder(),
30
+ univ.Real.tagSet: RealPayloadDecoder()}
31
+ )
32
+
33
+ TYPE_MAP = decoder.TYPE_MAP.copy()
34
+
35
+ # Put in non-ambiguous types for faster codec lookup
36
+ for typeDecoder in TAG_MAP.values():
37
+ if typeDecoder.protoComponent is not None:
38
+ typeId = typeDecoder.protoComponent.__class__.typeId
39
+ if typeId is not None and typeId not in TYPE_MAP:
40
+ TYPE_MAP[typeId] = typeDecoder
41
+
42
+
43
+ class SingleItemDecoder(decoder.SingleItemDecoder):
44
+ __doc__ = decoder.SingleItemDecoder.__doc__
45
+
46
+ TAG_MAP = TAG_MAP
47
+ TYPE_MAP = TYPE_MAP
48
+
49
+ supportIndefLength = False
50
+
51
+
52
+ class StreamingDecoder(decoder.StreamingDecoder):
53
+ __doc__ = decoder.StreamingDecoder.__doc__
54
+
55
+ SINGLE_ITEM_DECODER = SingleItemDecoder
56
+
57
+
58
+ class Decoder(decoder.Decoder):
59
+ __doc__ = decoder.Decoder.__doc__
60
+
61
+ STREAMING_DECODER = StreamingDecoder
62
+
63
+
64
+ #: Turns DER octet stream into an ASN.1 object.
65
+ #:
66
+ #: Takes DER octet-stream and decode it into an ASN.1 object
67
+ #: (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative) which
68
+ #: may be a scalar or an arbitrary nested structure.
69
+ #:
70
+ #: Parameters
71
+ #: ----------
72
+ #: substrate: :py:class:`bytes`
73
+ #: DER octet-stream
74
+ #:
75
+ #: Keyword Args
76
+ #: ------------
77
+ #: asn1Spec: any pyasn1 type object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
78
+ #: A pyasn1 type object to act as a template guiding the decoder. Depending on the ASN.1 structure
79
+ #: being decoded, *asn1Spec* may or may not be required. Most common reason for
80
+ #: it to require is that ASN.1 structure is encoded in *IMPLICIT* tagging mode.
81
+ #:
82
+ #: Returns
83
+ #: -------
84
+ #: : :py:class:`tuple`
85
+ #: A tuple of pyasn1 object recovered from DER substrate (:py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
86
+ #: and the unprocessed trailing portion of the *substrate* (may be empty)
87
+ #:
88
+ #: Raises
89
+ #: ------
90
+ #: ~pyasn1.error.PyAsn1Error, ~pyasn1.error.SubstrateUnderrunError
91
+ #: On decoding errors
92
+ #:
93
+ #: Examples
94
+ #: --------
95
+ #: Decode DER serialisation without ASN.1 schema
96
+ #:
97
+ #: .. code-block:: pycon
98
+ #:
99
+ #: >>> s, _ = decode(b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03')
100
+ #: >>> str(s)
101
+ #: SequenceOf:
102
+ #: 1 2 3
103
+ #:
104
+ #: Decode DER serialisation with ASN.1 schema
105
+ #:
106
+ #: .. code-block:: pycon
107
+ #:
108
+ #: >>> seq = SequenceOf(componentType=Integer())
109
+ #: >>> s, _ = decode(b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03', asn1Spec=seq)
110
+ #: >>> str(s)
111
+ #: SequenceOf:
112
+ #: 1 2 3
113
+ #:
114
+ decode = Decoder()
115
+
116
+ def __getattr__(attr: str):
117
+ if newAttr := {"tagMap": "TAG_MAP", "typeMap": "TYPE_MAP"}.get(attr):
118
+ warnings.warn(f"{attr} is deprecated. Please use {newAttr} instead.", DeprecationWarning, stacklevel=2)
119
+ return globals()[newAttr]
120
+ raise AttributeError(attr)