|
7 | 7 | from OpenSSL.crypto import X509
|
8 | 8 | from cryptography.exceptions import InvalidSignature
|
9 | 9 | from cryptography.hazmat.primitives import hashes
|
| 10 | +from cryptography.hazmat.primitives import serialization |
| 11 | +from cryptography.hazmat.primitives.serialization import PublicFormat, Encoding |
10 | 12 | from cryptography.hazmat.primitives.asymmetric import padding, rsa
|
11 | 13 |
|
12 | 14 | from .curp import CURP
|
@@ -48,34 +50,32 @@ def __init__(self, certificate: X509):
|
48 | 50 | self.certificate = certificate
|
49 | 51 |
|
50 | 52 | @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)) |
53 | 61 |
|
54 | 62 | def fingerprint(self, algorithm=hashes.SHA1()) -> bytes:
|
55 | 63 | return self.certificate.to_cryptography().fingerprint(algorithm=algorithm)
|
56 | 64 |
|
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 | + ) |
59 | 69 |
|
60 |
| - def certificate_base64(self, type: int = crypto.FILETYPE_ASN1) -> str: |
| 70 | + def certificate_base64(self) -> str: |
61 | 71 | """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 |
| -
|
71 | 72 | Returns:
|
72 | 73 | str: The certificate in base64 encoding
|
73 | 74 | """
|
74 |
| - cert = self.certificate_bytes(type) |
| 75 | + cert = self.certificate_bytes() |
75 | 76 | return base64.b64encode(cert).decode()
|
76 | 77 |
|
77 | 78 | def issuer(self) -> str:
|
78 |
| - # return self.certificate.to_cryptography().issuer.rfc4514_string() |
79 | 79 | d = self.certificate.get_issuer().get_components()
|
80 | 80 | return ','.join(f'{k.decode()}={v.decode()}' for k, v in reversed(d))
|
81 | 81 |
|
@@ -174,9 +174,11 @@ def certificate_number(self) -> str:
|
174 | 174 | def public_key(self) -> rsa.RSAPublicKey:
|
175 | 175 | return self.certificate.get_pubkey().to_cryptography_key()
|
176 | 176 |
|
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 | + ) |
180 | 182 |
|
181 | 183 | def _verify(self, data, signature, algorithm) -> bool:
|
182 | 184 | try:
|
|
0 commit comments