crypto: add property for private_key

This commit is contained in:
Jens Langhammer 2020-03-04 19:43:18 +01:00
parent 80a50f9bdb
commit f2154d9875
1 changed files with 14 additions and 0 deletions

View File

@ -4,6 +4,8 @@ from typing import Optional
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey
from cryptography.hazmat.primitives.serialization import load_pem_private_key
from cryptography.x509 import Certificate, load_pem_x509_certificate
from django.db import models
from django.utils.translation import gettext_lazy as _
@ -26,6 +28,7 @@ class CertificateKeyPair(UUIDModel, CreatedUpdatedModel):
)
_cert: Optional[Certificate] = None
_key: Optional[RSAPrivateKey] = None
@property
def certificate(self) -> Certificate:
@ -36,6 +39,17 @@ class CertificateKeyPair(UUIDModel, CreatedUpdatedModel):
)
return self._cert
@property
def private_key(self) -> Optional[RSAPrivateKey]:
"""Get python cryptography PrivateKey instance"""
if not self._key:
self._key = load_pem_private_key(
str.encode("\n".join([x.strip() for x in self.key_data.split("\n")])),
password=None,
backend=default_backend(),
)
return self._key
@property
def fingerprint(self) -> str:
"""Get SHA256 Fingerprint of certificate_data"""