Skip to content

Commit bc3d62c

Browse files
committed
Fixed issues
1 parent 157b8fa commit bc3d62c

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

satcfdi/models/certificate.py

+21-19
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from OpenSSL.crypto import X509
88
from cryptography.exceptions import InvalidSignature
99
from cryptography.hazmat.primitives import hashes
10+
from cryptography.hazmat.primitives import serialization
11+
from cryptography.hazmat.primitives.serialization import PublicFormat, Encoding
1012
from cryptography.hazmat.primitives.asymmetric import padding, rsa
1113

1214
from .curp import CURP
@@ -48,34 +50,32 @@ def __init__(self, certificate: X509):
4850
self.certificate = certificate
4951

5052
@classmethod
51-
def load_certificate(cls, certificate: bytes, type: int = crypto.FILETYPE_ASN1) -> 'Certificate':
52-
return cls(crypto.load_certificate(type, certificate))
53+
def load_certificate(cls, certificate: bytes, encoding: Encoding = Encoding.DER) -> 'Certificate':
54+
if encoding == Encoding.PEM:
55+
t = crypto.FILETYPE_PEM
56+
elif encoding == Encoding.DER:
57+
t = crypto.FILETYPE_ASN1
58+
else:
59+
raise CFDIError(f"Invalid encoding {encoding}")
60+
return cls(crypto.load_certificate(t, certificate))
5361

5462
def fingerprint(self, algorithm=hashes.SHA1()) -> bytes:
5563
return self.certificate.to_cryptography().fingerprint(algorithm=algorithm)
5664

57-
def certificate_bytes(self, type: int = crypto.FILETYPE_ASN1) -> bytes:
58-
return crypto.dump_certificate(type, self.certificate)
65+
def certificate_bytes(self, encoding: Encoding = Encoding.DER) -> bytes:
66+
return self.certificate.to_cryptography().public_bytes(
67+
encoding=encoding
68+
)
5969

60-
def certificate_base64(self, type: int = crypto.FILETYPE_ASN1) -> str:
70+
def certificate_base64(self) -> str:
6171
"""Returns the certificate in base64 encoding
62-
63-
Args:
64-
type (Literal["ASN1";, "PEM"], optional): The format of the certificate. Defaults to `ASN1`.
65-
- `ASN1`: Returns the certificate in ASN.1 format
66-
- `PEM`: Returns the certificate in PEM format
67-
68-
Raises:
69-
ValueError: If the format is not "ASN1" or "PEM"
70-
7172
Returns:
7273
str: The certificate in base64 encoding
7374
"""
74-
cert = self.certificate_bytes(type)
75+
cert = self.certificate_bytes()
7576
return base64.b64encode(cert).decode()
7677

7778
def issuer(self) -> str:
78-
# return self.certificate.to_cryptography().issuer.rfc4514_string()
7979
d = self.certificate.get_issuer().get_components()
8080
return ','.join(f'{k.decode()}={v.decode()}' for k, v in reversed(d))
8181

@@ -174,9 +174,11 @@ def certificate_number(self) -> str:
174174
def public_key(self) -> rsa.RSAPublicKey:
175175
return self.certificate.get_pubkey().to_cryptography_key()
176176

177-
# @property Fill fix later
178-
# def public_key(self) -> str:
179-
# return crypto.dump_publickey(crypto.FILETYPE_PEM, self.certificate.get_pubkey())
177+
def public_key_bytes(self, encoding: serialization.Encoding = serialization.Encoding.DER) -> bytes:
178+
return self.public_key().public_bytes(
179+
encoding=encoding,
180+
format=PublicFormat.SubjectPublicKeyInfo
181+
)
180182

181183
def _verify(self, data, signature, algorithm) -> bool:
182184
try:

0 commit comments

Comments
 (0)