Skip to content

Commit

Permalink
[python] Stop using deprecated cryptography.io's backend argument
Browse files Browse the repository at this point in the history
Signed-off-by: Michał Kowalczyk <[email protected]>
  • Loading branch information
mkow committed Nov 14, 2024
1 parent 0997103 commit 72b5631
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 18 deletions.
2 changes: 1 addition & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Depends:
python3,
python3 (>= 3.10) | python3-pkg-resources,
python3-click (>= 6.7),
python3-cryptography,
python3-cryptography (>= 3.1),
python3-jinja2,
python3-pyelftools,
python3-tomli (>= 1.1.0),
Expand Down
2 changes: 1 addition & 1 deletion gramine.spec
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ BuildRequires: python3-sphinx_rtd_theme
%endif

Requires: python3-click >= 6.7
Requires: python3-cryptography
Requires: python3-cryptography >= 3.1
Requires: python3-jinja2
Requires: python3-protobuf
Requires: python3-pyelftools
Expand Down
2 changes: 1 addition & 1 deletion packaging/alpine/APKBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ makedepends="
"
depends="
py3-click>=6.7
py3-cryptography
py3-cryptography>=3.1
py3-elftools
py3-jinja2
py3-tomli
Expand Down
11 changes: 2 additions & 9 deletions python/graminelibos/sgx_sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import click

from cryptography.hazmat import backends
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import padding, rsa

Expand All @@ -26,10 +25,6 @@

import _graminelibos_offsets as offs # pylint: disable=import-error,wrong-import-order

# TODO after deprecating 20.04: remove backend wrt
# https://cryptography.io/en/latest/faq/#what-happened-to-the-backend-argument
_cryptography_backend = backends.default_backend()

# Default / Architectural Options

ARCHITECTURE = 'amd64'
Expand Down Expand Up @@ -587,8 +582,7 @@ class InvalidKeyError(Exception):

def load_private_key_from_pem_file(file, passphrase=None):
with file:
private_key = serialization.load_pem_private_key(
file.read(), password=passphrase, backend=_cryptography_backend)
private_key = serialization.load_pem_private_key(file.read(), password=passphrase)

if not isinstance(private_key, rsa.RSAPrivateKey):
raise InvalidKeyError(
Expand Down Expand Up @@ -714,8 +708,7 @@ def generate_private_key():
"""
return rsa.generate_private_key(
public_exponent=SGX_RSA_PUBLIC_EXPONENT,
key_size=SGX_RSA_KEY_SIZE,
backend=_cryptography_backend)
key_size=SGX_RSA_KEY_SIZE)


def generate_private_key_pem():
Expand Down
9 changes: 3 additions & 6 deletions tests/test_sgx_sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@

@pytest.fixture
def tmp_rsa_key(tmpdir):
from graminelibos.sgx_sign import (SGX_RSA_KEY_SIZE, SGX_RSA_PUBLIC_EXPONENT,
_cryptography_backend)
from graminelibos.sgx_sign import SGX_RSA_KEY_SIZE, SGX_RSA_PUBLIC_EXPONENT
def gen_rsa_key(passphrase=None, key_size=SGX_RSA_KEY_SIZE):
# TODO: use `tmp_path` fixture after we drop support for distros (RHEL 8, CentOS Stream 8)
# that have old pytest version (< 3.9.0) installed
key_path = tmpdir.join('key.pem')
with open(key_path, 'wb') as pfile:
key = rsa.generate_private_key(public_exponent=SGX_RSA_PUBLIC_EXPONENT,
key_size=key_size, backend=_cryptography_backend)
key_size=key_size)

encryption_algorithm = serialization.NoEncryption()
if passphrase is not None:
Expand All @@ -35,9 +34,7 @@ def gen_rsa_key(passphrase=None, key_size=SGX_RSA_KEY_SIZE):

# pylint: disable=too-many-arguments
def verify_signature(data, exponent, modulus, signature, key_file, passphrase=None):
from graminelibos.sgx_sign import _cryptography_backend
private_key = serialization.load_pem_private_key(key_file.read(), password=passphrase,
backend=_cryptography_backend)
private_key = serialization.load_pem_private_key(key_file.read(), password=passphrase)

public_key = private_key.public_key()

Expand Down

0 comments on commit 72b5631

Please sign in to comment.