"""Test utilities. .. warning:: This module is not part of the public API. """ import os import OpenSSL import pkg_resources from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import serialization from josepy import ComparableRSAKey, ComparableX509 from josepy.util import ComparableECKey def vector_path(*names): """Path to a test vector.""" return pkg_resources.resource_filename( __name__, os.path.join('testdata', *names)) def load_vector(*names): """Load contents of a test vector.""" # luckily, resource_string opens file in binary mode return pkg_resources.resource_string( __name__, os.path.join('testdata', *names)) def _guess_loader(filename, loader_pem, loader_der): _, ext = os.path.splitext(filename) if ext.lower() == '.pem': return loader_pem elif ext.lower() == '.der': return loader_der else: # pragma: no cover raise ValueError("Loader could not be recognized based on extension") def load_cert(*names): """Load certificate.""" loader = _guess_loader( names[-1], OpenSSL.crypto.FILETYPE_PEM, OpenSSL.crypto.FILETYPE_ASN1) return OpenSSL.crypto.load_certificate(loader, load_vector(*names)) def load_comparable_cert(*names): """Load ComparableX509 cert.""" return ComparableX509(load_cert(*names)) def load_csr(*names): """Load certificate request.""" loader = _guess_loader( names[-1], OpenSSL.crypto.FILETYPE_PEM, OpenSSL.crypto.FILETYPE_ASN1) return OpenSSL.crypto.load_certificate_request(loader, load_vector(*names)) def load_comparable_csr(*names): """Load ComparableX509 certificate request.""" return ComparableX509(load_csr(*names)) def load_rsa_private_key(*names): """Load RSA private key.""" loader = _guess_loader(names[-1], serialization.load_pem_private_key, serialization.load_der_private_key) return ComparableRSAKey(loader( load_vector(*names), password=None, backend=default_backend())) def load_ec_private_key(*names): """Load EC private key.""" loader = _guess_loader(names[-1], serialization.load_pem_private_key, serialization.load_der_private_key) return ComparableECKey(loader( load_vector(*names), password=None, backend=default_backend())) def load_pyopenssl_private_key(*names): """Load pyOpenSSL private key.""" loader = _guess_loader( names[-1], OpenSSL.crypto.FILETYPE_PEM, OpenSSL.crypto.FILETYPE_ASN1) return OpenSSL.crypto.load_privatekey(loader, load_vector(*names))
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
__pycache__ | Folder | 0755 |
|
|
testdata | Folder | 0755 |
|
|
__init__.py | File | 1.66 KB | 0644 |
|
b64.py | File | 1.4 KB | 0644 |
|
b64_test.py | File | 2.2 KB | 0644 |
|
errors.py | File | 762 B | 0644 |
|
errors_test.py | File | 463 B | 0644 |
|
interfaces.py | File | 7.51 KB | 0644 |
|
interfaces_test.py | File | 3.51 KB | 0644 |
|
json_util.py | File | 15.35 KB | 0644 |
|
json_util_test.py | File | 13.83 KB | 0644 |
|
jwa.py | File | 8.17 KB | 0644 |
|
jwa_test.py | File | 8.8 KB | 0644 |
|
jwk.py | File | 13.12 KB | 0644 |
|
jwk_test.py | File | 12.69 KB | 0644 |
|
jws.py | File | 13.84 KB | 0644 |
|
jws_test.py | File | 8.32 KB | 0644 |
|
magic_typing.py | File | 529 B | 0644 |
|
magic_typing_test.py | File | 1.05 KB | 0644 |
|
test_util.py | File | 2.57 KB | 0644 |
|
util.py | File | 8.37 KB | 0644 |
|
util_test.py | File | 8.15 KB | 0644 |
|