Changeset - 1f9ad2819335
[Not reviewed]
0 5 0
Branko Majic (branko) - 2 months ago 2024-02-23 22:17:59
branko@majic.rs
GC-45: Upgrade to cryptographyt version 42.0:

- Passing in backend is no longer required/possible.
5 files changed with 10 insertions and 20 deletions:
0 comments (0 inline, 0 general)
gimmecert/crypto.py
Show inline comments
 
@@ -82,19 +82,17 @@ class KeyGenerator:
 
        if self._algorithm == "rsa":
 

	
 
            rsa_public_exponent = 65537
 

	
 
            private_key = cryptography.hazmat.primitives.asymmetric.rsa.generate_private_key(
 
                public_exponent=rsa_public_exponent,
 
                key_size=self._parameters,
 
                backend=cryptography.hazmat.backends.default_backend()
 
                key_size=self._parameters
 
            )
 
        else:
 
            private_key = cryptography.hazmat.primitives.asymmetric.ec.generate_private_key(
 
                curve=self._parameters,
 
                backend=cryptography.hazmat.backends.default_backend()
 
                curve=self._parameters
 
            )
 

	
 
        return private_key
 

	
 

	
 
def get_dn(name):
 
@@ -180,14 +178,13 @@ def issue_certificate(issuer_dn, subject_dn, signing_key, public_key, not_before
 

	
 
    for extension in extensions:
 
        builder = builder.add_extension(extension[0], critical=extension[1])
 

	
 
    certificate = builder.sign(
 
        private_key=signing_key,
 
        algorithm=cryptography.hazmat.primitives.hashes.SHA256(),
 
        backend=cryptography.hazmat.backends.default_backend()
 
        algorithm=cryptography.hazmat.primitives.hashes.SHA256()
 
    )
 

	
 
    return certificate
 

	
 

	
 
def generate_ca_hierarchy(base_name, depth, key_generator):
 
@@ -431,14 +428,13 @@ def generate_csr(name, private_key):
 

	
 
    builder = cryptography.x509.CertificateSigningRequestBuilder()
 
    builder = builder.subject_name(subject_dn)
 

	
 
    csr = builder.sign(
 
        private_key,
 
        cryptography.hazmat.primitives.hashes.SHA256(),
 
        cryptography.hazmat.backends.default_backend()
 
        cryptography.hazmat.primitives.hashes.SHA256()
 
    )
 

	
 
    return csr
 

	
 

	
 
def key_specification_from_public_key(public_key):
gimmecert/storage.py
Show inline comments
 
@@ -168,14 +168,13 @@ def read_private_key(private_key_path):
 
            cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey
 
    """
 

	
 
    with open(private_key_path, 'rb') as private_key_file:
 
        private_key = cryptography.hazmat.primitives.serialization.load_pem_private_key(
 
            private_key_file.read(),
 
            None,  # no password
 
            cryptography.hazmat.backends.default_backend()
 
            None  # no password
 
        )
 

	
 
    return private_key
 

	
 

	
 
def read_certificate(certificate_path):
 
@@ -189,14 +188,13 @@ def read_certificate(certificate_path):
 

	
 
    :returns: Certificate object read from the specified file.
 
    :rtype: cryptography.x509.Certificate
 
    """
 
    with open(certificate_path, 'rb') as certificate_file:
 
        certificate = cryptography.x509.load_pem_x509_certificate(
 
            certificate_file.read(),
 
            cryptography.hazmat.backends.default_backend()
 
            certificate_file.read()
 
        )
 

	
 
    return certificate
 

	
 

	
 
def write_csr(csr, path):
 
@@ -229,11 +227,10 @@ def read_csr(csr_path):
 
    :returns: CSR object read from the specified file.
 
    :rtype: cryptography.x509.CertificateSigningRequest
 
    """
 

	
 
    with open(csr_path, 'rb') as csr_file:
 
        csr = cryptography.x509.load_pem_x509_csr(
 
            csr_file.read(),
 
            cryptography.hazmat.backends.default_backend()
 
            csr_file.read()
 
        )
 

	
 
    return csr
gimmecert/utils.py
Show inline comments
 
@@ -153,11 +153,10 @@ def csr_from_pem(csr_pem):
 

	
 
    :returns: CSR object.
 
    :rtype: cryptography.x509.CertificateSigningRequest
 
    """
 

	
 
    csr = cryptography.x509.load_pem_x509_csr(
 
        bytes(csr_pem, encoding='utf8'),
 
        cryptography.hazmat.backends.default_backend()
 
        bytes(csr_pem, encoding='utf8')
 
    )
 

	
 
    return csr
setup.py
Show inline comments
 
@@ -24,13 +24,13 @@ from setuptools import setup, find_packages
 

	
 
README = open(os.path.join(os.path.dirname(__file__), 'README.rst')).read()
 

	
 
python_requirements = ">=3.8,<3.10"
 

	
 
install_requirements = [
 
    'cryptography>=3.2,<3.3',
 
    'cryptography>=42.0,<42.1',
 
    'python-dateutil>=2.8,<2.9',
 
]
 

	
 
doc_requirements = [
 
    'sphinx>=7.1,<7.2',
 
]
tests/test_utils.py
Show inline comments
 
@@ -20,13 +20,12 @@
 

	
 

	
 
import datetime
 
import io
 

	
 
import cryptography.x509
 
import cryptography.hazmat.backends
 

	
 
import gimmecert.crypto
 
import gimmecert.utils
 

	
 
import pytest
 

	
 
@@ -37,14 +36,13 @@ def test_certificate_to_pem_returns_valid_pem():
 
    not_before, not_after = gimmecert.crypto.get_validity_range()
 
    certificate = gimmecert.crypto.issue_certificate(dn, dn, private_key, private_key.public_key(), not_before, not_after)
 

	
 
    certificate_pem = gimmecert.utils.certificate_to_pem(certificate)
 

	
 
    assert isinstance(certificate_pem, str)
 
    certificate_from_pem = cryptography.x509.load_pem_x509_certificate(bytes(certificate_pem, encoding='UTF-8'),
 
                                                                       cryptography.hazmat.backends.default_backend())  # Should not throw
 
    certificate_from_pem = cryptography.x509.load_pem_x509_certificate(bytes(certificate_pem, encoding='UTF-8'))  # Should not throw
 
    assert certificate_from_pem.subject == certificate.subject
 
    assert certificate_from_pem.issuer == certificate.issuer
 

	
 

	
 
def test_dn_to_str_with_cn():
 
    dn = gimmecert.crypto.get_dn('My test 1')
0 comments (0 inline, 0 general)