From f2154d9875c57b468a8de26eedc48793d98db708 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Wed, 4 Mar 2020 19:43:18 +0100 Subject: [PATCH] crypto: add property for private_key --- passbook/crypto/models.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/passbook/crypto/models.py b/passbook/crypto/models.py index 80bd7f29b..e3ec5c607 100644 --- a/passbook/crypto/models.py +++ b/passbook/crypto/models.py @@ -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"""