Representation of a (TLS) certificate.
| 62 | |
| 63 | |
| 64 | class Cert(serializable.Serializable): |
| 65 | """Representation of a (TLS) certificate.""" |
| 66 | |
| 67 | _cert: x509.Certificate |
| 68 | |
| 69 | def __init__(self, cert: x509.Certificate): |
| 70 | assert isinstance(cert, x509.Certificate) |
| 71 | self._cert = cert |
| 72 | |
| 73 | def __eq__(self, other): |
| 74 | return self.fingerprint() == other.fingerprint() |
| 75 | |
| 76 | def __repr__(self): |
| 77 | altnames = [str(x.value) for x in self.altnames] |
| 78 | return f"<Cert(cn={self.cn!r}, altnames={altnames!r})>" |
| 79 | |
| 80 | def __hash__(self): |
| 81 | return self._cert.__hash__() |
| 82 | |
| 83 | @classmethod |
| 84 | def from_state(cls, state): |
| 85 | return cls.from_pem(state) |
| 86 | |
| 87 | def get_state(self): |
| 88 | return self.to_pem() |
| 89 | |
| 90 | def set_state(self, state): |
| 91 | self._cert = x509.load_pem_x509_certificate(state) |
| 92 | |
| 93 | @classmethod |
| 94 | def from_pem(cls, data: bytes) -> "Cert": |
| 95 | cert = x509.load_pem_x509_certificate(data) # type: ignore |
| 96 | return cls(cert) |
| 97 | |
| 98 | def to_pem(self) -> bytes: |
| 99 | return self._cert.public_bytes(serialization.Encoding.PEM) |
| 100 | |
| 101 | @classmethod |
| 102 | def from_pyopenssl(self, x509: OpenSSL.crypto.X509) -> "Cert": |
| 103 | return Cert(x509.to_cryptography()) |
| 104 | |
| 105 | @deprecated("Use `to_cryptography` instead.") |
| 106 | def to_pyopenssl(self) -> OpenSSL.crypto.X509: # pragma: no cover |
| 107 | return OpenSSL.crypto.X509.from_cryptography(self._cert) |
| 108 | |
| 109 | def to_cryptography(self) -> x509.Certificate: |
| 110 | return self._cert |
| 111 | |
| 112 | def public_key(self) -> CertificatePublicKeyTypes: |
| 113 | return self._cert.public_key() |
| 114 | |
| 115 | def fingerprint(self) -> bytes: |
| 116 | return self._cert.fingerprint(hashes.SHA256()) |
| 117 | |
| 118 | @property |
| 119 | def issuer(self) -> list[tuple[str, str]]: |
| 120 | return _name_to_keyval(self._cert.issuer) |
| 121 |
no outgoing calls
no test coverage detected
searching dependent graphs…