From 476c5df3f12967b397a3e02e303db774cfa95915 Mon Sep 17 00:00:00 2001 From: Andre Caron Date: Mon, 18 May 2015 10:23:28 -0400 Subject: Adds support for writing CSRs. --- src/cryptography/hazmat/backends/openssl/x509.py | 19 ++++++++++++++++++- src/cryptography/x509.py | 6 ++++++ 2 files changed, 24 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/cryptography/hazmat/backends/openssl/x509.py b/src/cryptography/hazmat/backends/openssl/x509.py index 6db6fc9c..67d0d51a 100644 --- a/src/cryptography/hazmat/backends/openssl/x509.py +++ b/src/cryptography/hazmat/backends/openssl/x509.py @@ -25,7 +25,7 @@ from six.moves import urllib_parse from cryptography import utils, x509 from cryptography.exceptions import UnsupportedAlgorithm -from cryptography.hazmat.primitives import hashes +from cryptography.hazmat.primitives import hashes, serialization def _obj2txt(backend, obj): @@ -689,3 +689,20 @@ class _CertificateSigningRequest(object): extensions.append(x509.Extension(oid, critical, value)) 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") + + # TODO: make text prelude optional. + bio = self._backend._create_mem_bio() + if encoding is serialization.Encoding.PEM: + res = self._backend._lib.X509_REQ_print(bio, self._x509_req) + assert res == 1 + res = self._backend._lib.PEM_write_bio_X509_REQ( + bio, self._x509_req + ) + elif encoding is serialization.Encoding.DER: + res = self._backend._lib.i2d_X509_REQ_bio(bio, self._x509_req) + assert res == 1 + return self._backend._read_mem_bio(bio) diff --git a/src/cryptography/x509.py b/src/cryptography/x509.py index ccb9f6de..c449b7ed 100644 --- a/src/cryptography/x509.py +++ b/src/cryptography/x509.py @@ -1190,3 +1190,9 @@ class CertificateSigningRequest(object): """ Returns the extensions in the signing request. """ + + @abc.abstractmethod + def public_bytes(self, encoding): + """ + Encodes the request to PEM or DER format. + """ -- cgit v1.2.3 From f1c3315a621eb895a9b6d52901cd8946413f53f6 Mon Sep 17 00:00:00 2001 From: Andre Caron Date: Mon, 18 May 2015 12:25:13 -0400 Subject: Removes PEM text prelude for CSRs. --- src/cryptography/hazmat/backends/openssl/x509.py | 3 --- 1 file changed, 3 deletions(-) (limited to 'src') diff --git a/src/cryptography/hazmat/backends/openssl/x509.py b/src/cryptography/hazmat/backends/openssl/x509.py index 67d0d51a..72041366 100644 --- a/src/cryptography/hazmat/backends/openssl/x509.py +++ b/src/cryptography/hazmat/backends/openssl/x509.py @@ -694,11 +694,8 @@ class _CertificateSigningRequest(object): if not isinstance(encoding, serialization.Encoding): raise TypeError("encoding must be an item from the Encoding enum") - # TODO: make text prelude optional. bio = self._backend._create_mem_bio() if encoding is serialization.Encoding.PEM: - res = self._backend._lib.X509_REQ_print(bio, self._x509_req) - assert res == 1 res = self._backend._lib.PEM_write_bio_X509_REQ( bio, self._x509_req ) -- cgit v1.2.3