Bug: 128513485

Clone this repo:
  1. fb7b427 Disable unused Python 2 variant. am: 3dff7137c6 am: 60283c5ba5 am: 093e39b381 am: 32bfa6b76b am: bc6f4e48e6 by Krzysztof Kosiński · 1 year, 10 months ago aml_hef_341811030 aml_rkp_341810000 android14-d2-release android14-d2-s1-release android14-d2-s2-release android14-d2-s3-release android14-d2-s4-release android14-d2-s5-release android14-dev android14-mainline-healthfitness-release android14-qpr1-release android14-qpr1-s2-release android14-qpr2-release android14-qpr2-s1-release android14-qpr2-s2-release android14-qpr2-s3-release android14-qpr2-s4-release android14-qpr2-s5-release android14-qpr3-release android14-qpr3-s2-release main master aml_cfg_341510000 aml_hef_341114030 aml_hef_341311010 aml_hef_341415040 aml_hef_341512030 aml_hef_341613000 aml_hef_341717050 aml_hef_341811030 aml_rkp_341012000 aml_rkp_341015010 aml_rkp_341114000 aml_rkp_341311000 aml_rkp_341510000 aml_rkp_341810000 android-14.0.0_r16 android-14.0.0_r17 android-14.0.0_r18 android-14.0.0_r19 android-14.0.0_r20 android-14.0.0_r21 android-14.0.0_r22 android-14.0.0_r23 android-14.0.0_r24 android-14.0.0_r25 android-14.0.0_r26 android-14.0.0_r27 android-14.0.0_r29 android-14.0.0_r30 android-14.0.0_r31 android-14.0.0_r32 android-14.0.0_r33 android-14.0.0_r34 android-14.0.0_r35 android-14.0.0_r36 android-14.0.0_r37 android-14.0.0_r38 android-14.0.0_r39 android-14.0.0_r40 android-14.0.0_r41 android-14.0.0_r42 android-14.0.0_r43 android-14.0.0_r44 android-14.0.0_r45 android-14.0.0_r50 android-14.0.0_r51 android-14.0.0_r52 android-14.0.0_r53 android-14.0.0_r54
  2. bc6f4e4 Disable unused Python 2 variant. am: 3dff7137c6 am: 60283c5ba5 am: 093e39b381 am: 32bfa6b76b by Krzysztof Kosiński · 1 year, 10 months ago main-16k-with-phones android-u-beta-1-gpl
  3. 32bfa6b Disable unused Python 2 variant. am: 3dff7137c6 am: 60283c5ba5 am: 093e39b381 by Krzysztof Kosiński · 1 year, 10 months ago
  4. 093e39b Disable unused Python 2 variant. am: 3dff7137c6 am: 60283c5ba5 by Krzysztof Kosiński · 1 year, 10 months ago
  5. 60283c5 Disable unused Python 2 variant. am: 3dff7137c6 by Krzysztof Kosiński · 1 year, 10 months ago


A fast, pure Python library for parsing and serializing ASN.1 structures.

GitHub Actions CI Travis CI AppVeyor CircleCI PyPI


In addition to an ASN.1 BER/DER decoder and DER serializer, the project includes a bunch of ASN.1 structures for use with various common cryptography standards:

X.509asn1crypto.x509RFC 5280
CRLasn1crypto.crlRFC 5280
CSRasn1crypto.csrRFC 2986, RFC 2985
OCSPasn1crypto.ocspRFC 6960
PKCS#12asn1crypto.pkcs12RFC 7292
PKCS#8asn1crypto.keysRFC 5208
PKCS#1 v2.1 (RSA keys)asn1crypto.keysRFC 3447
DSA keysasn1crypto.keysRFC 3279
Elliptic curve keysasn1crypto.keysSECG SEC1 V2
PKCS#3 v1.4asn1crypto.algosPKCS#3 v1.4
PKCS#5 v2.1asn1crypto.algosPKCS#5 v2.1
CMS (and PKCS#7)asn1crypto.cmsRFC 5652, RFC 2315
TSPasn1crypto.tspRFC 3161
PDF signaturesasn1crypto.pdfPDF 1.7

Why Another Python ASN.1 Library?

Python has long had the pyasn1 and pyasn1_modules available for parsing and serializing ASN.1 structures. While the project does include a comprehensive set of tools for parsing and serializing, the performance of the library can be very poor, especially when dealing with bit fields and parsing large structures such as CRLs.

After spending extensive time using pyasn1, the following issues were identified:

  1. Poor performance
  2. Verbose, non-pythonic API
  3. Out-dated and incomplete definitions in pyasn1-modules
  4. No simple way to map data to native Python data structures
  5. No mechanism for overridden universal ASN.1 types

The pyasn1 API is largely method driven, and uses extensive configuration objects and lowerCamelCase names. There were no consistent options for converting types of native Python data structures. Since the project supports out-dated versions of Python, many newer language features are unavailable for use.

Time was spent trying to profile issues with the performance, however the architecture made it hard to pin down the primary source of the poor performance. Attempts were made to improve performance by utilizing unreleased patches and delaying parsing using the Any type. Even with such changes, the performance was still unacceptably slow.

Finally, a number of structures in the cryptographic space use universal data types such as BitString and OctetString, but interpret the data as other types. For instance, signatures are really byte strings, but are encoded as BitString. Elliptic curve keys use both BitString and OctetString to represent integers. Parsing these structures as the base universal types and then re-interpreting them wastes computation.

asn1crypto uses the following techniques to improve performance, especially when extracting one or two fields from large, complex structures:

  • Delayed parsing of byte string values
  • Persistence of original ASN.1 encoded data until a value is changed
  • Lazy loading of child fields
  • Utilization of high-level Python stdlib modules

While there is no extensive performance test suite, the CRLTests.test_parse_crl test case was used to parse a 21MB CRL file on a late 2013 rMBP. asn1crypto parsed the certificate serial numbers in just under 8 seconds. With pyasn1, using definitions from pyasn1-modules, the same parsing took over 4,100 seconds.

For smaller structures the performance difference can range from a few times faster to an order of magnitude or more.

Related Crypto Libraries

asn1crypto is part of the modularcrypto family of Python packages:

Current Release

1.4.0 - changelog


Python 2.6, 2.7, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8 or pypy. No third-party packages required.


pip install asn1crypto


asn1crypto is licensed under the terms of the MIT license. See the LICENSE file for the exact license text.


The documentation for asn1crypto is composed of tutorials on basic usage and links to the source for the various pre-defined type classes.



Continuous Integration

Various combinations of platforms and versions of Python are tested via:


Tests are written using unittest and require no third-party packages.

Depending on what type of source is available for the package, the following commands can be used to run the test suite.

Git Repository

When working within a Git working copy, or an archive of the Git repository, the full test suite is run via:

python run.py tests

To run only some tests, pass a regular expression as a parameter to tests.

python run.py tests ocsp

PyPi Source Distribution

When working within an extracted source distribution (aka .tar.gz) from PyPi, the full test suite is run via:

python setup.py test


When the package has been installed via pip (or another method), the package asn1crypto_tests may be installed and invoked to run the full test suite:

pip install asn1crypto_tests
python -m asn1crypto_tests


To install the package used for linting, execute:

pip install --user -r requires/lint

The following command will run the linter:

python run.py lint

Support for code coverage can be installed via:

pip install --user -r requires/coverage

Coverage is measured by running:

python run.py coverage

To change the version number of the package, run:

python run.py version {pep440_version}

To install the necessary packages for releasing a new version on PyPI, run:

pip install --user -r requires/release

Releases are created by:

  • Making a git tag in PEP 440 format

  • Running the command:

    python run.py release

Existing releases can be found at https://pypi.org/project/asn1crypto/.

CI Tasks

A task named deps exists to download and stage all necessary testing dependencies. On posix platforms, curl is used for downloads and on Windows PowerShell with Net.WebClient is used. This configuration sidesteps issues related to getting pip to work properly and messing with site-packages for the version of Python being used.

The ci task runs lint (if flake8 is available for the version of Python) and coverage (or tests if coverage is not available for the version of Python). If the current directory is a clean git working copy, the coverage data is submitted to codecov.io.

python run.py deps
python run.py ci