Skip to content

Commit

Permalink
Utility is fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
karen-avetisyan-mc committed Feb 20, 2024
1 parent 4e0e00a commit df6c011
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
32 changes: 19 additions & 13 deletions client_encryption/encryption_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,25 @@ class FileType(IntEnum):
def load_encryption_certificate(certificate_path):
"""Load X509 encryption certificate data at the given file path."""

with open(certificate_path, "rb") as cert_content:
certificate = cert_content.read()

type = __get_crypto_file_type(certificate)

if type == FileType.FILETYPE_PEM:
cert = x509.load_pem_x509_certificate(certificate)
return cert, Encoding.PEM
if type == FileType.FILETYPE_ASN1:
cert = x509.load_der_x509_certificate(certificate)
return cert, Encoding.DER
if type == FileType.FILETYPE_INVALID:
raise CertificateError("Wrong encryption certificate format.")
try:
with open(certificate_path, "rb") as cert_content:
certificate = cert_content.read()
except IOError:
raise CertificateError ("Unable to load certificate.")

try:
type = __get_crypto_file_type(certificate)

if type == FileType.FILETYPE_PEM:
cert = x509.load_pem_x509_certificate(certificate)
return cert, Encoding.PEM
if type == FileType.FILETYPE_ASN1:
cert = x509.load_der_x509_certificate(certificate)
return cert, Encoding.DER
if type == FileType.FILETYPE_INVALID:
raise CertificateError("Wrong certificate format.")
except ValueError:
raise CertificateError ("Invalid certificate format.")

def write_encryption_certificate(certificate_path, certificate, cert_type):
with open(certificate_path, "wb") as f:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_encryption_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_load_encryption_certificate_der(self):
cert, type = to_test.load_encryption_certificate(cert_path)

self.assertIsNotNone(cert)
self.assertIsInstance(cert, X509, "Must be X509 certificate")
self.assertIsInstance(cert, x509.Certificate, "Must be X509 certificate")

def test_load_encryption_certificate_invalid(self):
cert_path = resource_path("keys/test_invalid_key.der")
Expand Down

0 comments on commit df6c011

Please sign in to comment.