Skip to content

Add pyasn1 as dependency #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ This driver depends on:

* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
* `Adafruit CircuitPython Logger Module <https://github.com/adafruit/Adafruit_CircuitPython_Logger>`_
* `pyasn1 Library <https://github.com/etingof/pyasn1>`_ (some functionality)
* CPython's ``rsa`` Library (some functionality)

Please ensure all dependencies are available on the CircuitPython filesystem.
This is easily achieved by downloading
Expand Down
5 changes: 4 additions & 1 deletion adafruit_rsa/asn1.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"""

# pylint: disable=no-name-in-module, too-few-public-methods
from pyasn1.type import univ, namedtype, tag
try:
from pyasn1.type import univ, namedtype, tag
except ImportError as err:
raise ImportError("Usage of asn1.py requires pyasn1 library") from err

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RSA.git"
Expand Down
45 changes: 33 additions & 12 deletions adafruit_rsa/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,11 @@ def _load_pkcs1_der(cls, keyfile):

"""
# pylint: disable=import-outside-toplevel
from adafruit_rsa.tools.pyasn1.codec.der import decoder
from adafruit_rsa.asn1 import AsnPubKey
try:
from adafruit_rsa.tools.pyasn1.codec.der import decoder
from adafruit_rsa.asn1 import AsnPubKey
except ImportError as err:
raise ImportError("This functionality requires the pyasn1 library") from err

(priv, _) = decoder.decode(keyfile, asn1Spec=AsnPubKey())
return cls(n=int(priv["modulus"]), e=int(priv["publicExponent"]))
Expand All @@ -263,8 +266,17 @@ def _save_pkcs1_der(self):
:rtype: bytes
"""
# pylint: disable=import-outside-toplevel
from pyasn1.codec.der import encoder
from rsa.asn1 import AsnPubKey
try:
from pyasn1.codec.der import encoder
except ImportError as err:
raise ImportError("This functionality requires the library") from err
try:
from rsa.asn1 import AsnPubKey
except ImportError as err:
raise ImportError(
"This functionality requres the CPython rsa library, "
"not available in CircuitPython"
) from err

# Create the ASN object
asn_key = AsnPubKey()
Expand Down Expand Up @@ -328,9 +340,12 @@ def load_pkcs1_openssl_der(cls, keyfile):

"""
# pylint: disable=import-outside-toplevel
from adafruit_rsa.asn1 import OpenSSLPubKey
from pyasn1.codec.der import decoder
from pyasn1.type import univ
try:
from adafruit_rsa.asn1 import OpenSSLPubKey
from pyasn1.codec.der import decoder
from pyasn1.type import univ
except ImportError as err:
raise ImportError("This functionality requires the pyasn1 library") from err

(keyinfo, _) = decoder.decode(keyfile, asn1Spec=OpenSSLPubKey())

Expand Down Expand Up @@ -477,9 +492,12 @@ def _load_pkcs1_der(cls, keyfile):

"""

from adafruit_rsa.tools.pyasn1.codec.der import ( # pylint: disable=import-outside-toplevel
decoder,
)
try:
from adafruit_rsa.tools.pyasn1.codec.der import ( # pylint: disable=import-outside-toplevel
decoder,
)
except ImportError as err:
raise ImportError("This functionality requires the pyasn1 library") from err

(priv, _) = decoder.decode(keyfile)

Expand Down Expand Up @@ -521,8 +539,11 @@ def _save_pkcs1_der(self):
:rtype: bytes
"""
# pylint: disable=import-outside-toplevel
from pyasn1.type import univ, namedtype
from pyasn1.codec.der import encoder
try:
from pyasn1.type import univ, namedtype
from pyasn1.codec.der import encoder
except ImportError as err:
raise ImportError("This functionality requires the pyasn1 library") from err

class AsnPrivKey(univ.Sequence):
"""Creates PKCS#1 DER Formatted AsnPrivKey"""
Expand Down