From a8aded6346b8016d8f9e7e5d2ad75319dd69dcbb Mon Sep 17 00:00:00 2001 From: Andre Caron Date: Tue, 19 May 2015 20:11:57 -0400 Subject: Adds certificate encoding to PEM and DER. --- src/cryptography/hazmat/backends/openssl/x509.py | 12 ++++++++++++ src/cryptography/x509.py | 4 ++++ 2 files changed, 16 insertions(+) (limited to 'src') diff --git a/src/cryptography/hazmat/backends/openssl/x509.py b/src/cryptography/hazmat/backends/openssl/x509.py index 72041366..c7d2d154 100644 --- a/src/cryptography/hazmat/backends/openssl/x509.py +++ b/src/cryptography/hazmat/backends/openssl/x509.py @@ -311,6 +311,18 @@ class _Certificate(object): return x509.Extensions(extensions) + def public_bytes(self, encoding): + if not isinstance(encoding, serialization.Encoding): + raise TypeError("encoding must be an item from the Encoding enum") + + bio = self._backend._create_mem_bio() + if encoding is serialization.Encoding.PEM: + res = self._backend._lib.PEM_write_bio_X509(bio, self._x509) + elif encoding is serialization.Encoding.DER: + res = self._backend._lib.i2d_X509_bio(bio, self._x509) + assert res == 1 + return self._backend._read_mem_bio(bio) + def _decode_certificate_policies(backend, ext): cp = backend._ffi.cast( diff --git a/src/cryptography/x509.py b/src/cryptography/x509.py index 9a3295ce..a786c705 100644 --- a/src/cryptography/x509.py +++ b/src/cryptography/x509.py @@ -1167,6 +1167,10 @@ class Certificate(object): Checks not equal. """ + @abc.abstractmethod + def public_bytes(self, encoding): + pass + @six.add_metaclass(abc.ABCMeta) class CertificateSigningRequest(object): -- cgit v1.2.3 From 18ef34b9139ccc2018aada63c2386bceae0da4c9 Mon Sep 17 00:00:00 2001 From: Andre Caron Date: Tue, 19 May 2015 21:24:31 -0400 Subject: Adds missing docstring. --- src/cryptography/x509.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src') diff --git a/src/cryptography/x509.py b/src/cryptography/x509.py index a786c705..1635a517 100644 --- a/src/cryptography/x509.py +++ b/src/cryptography/x509.py @@ -1169,6 +1169,9 @@ class Certificate(object): @abc.abstractmethod def public_bytes(self, encoding): + """ + Serializes the certificate to PEM or DER format. + """ pass -- cgit v1.2.3 From 9dd2ab94131c57af7aceac6785ee3607e02b1353 Mon Sep 17 00:00:00 2001 From: Andre Caron Date: Tue, 19 May 2015 21:28:04 -0400 Subject: Removes useless pass statement. --- src/cryptography/x509.py | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/cryptography/x509.py b/src/cryptography/x509.py index 1635a517..e6d19ae7 100644 --- a/src/cryptography/x509.py +++ b/src/cryptography/x509.py @@ -1172,7 +1172,6 @@ class Certificate(object): """ Serializes the certificate to PEM or DER format. """ - pass @six.add_metaclass(abc.ABCMeta) -- cgit v1.2.3