summaryrefslogtreecommitdiff
path: root/src/OpenSSL/crypto.py
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2017-06-25 16:28:24 -0400
committerPaul Kehrer <paul.l.kehrer@gmail.com>2017-06-25 10:28:24 -1000
commit9939ba1658868b68654501aac24c97db8d9335ae (patch)
tree46d036201c23060762b1044f89289f32e7e10c0d /src/OpenSSL/crypto.py
parentf8022d6a1d315ebcfe6cc25ae07dd8d3bd0cf1b4 (diff)
downloadpyopenssl-9939ba1658868b68654501aac24c97db8d9335ae.tar.gz
Added an API for converting X509 to/from cryptography (#640)
* Added an API for converting X509 to/from cryptography * changelog
Diffstat (limited to 'src/OpenSSL/crypto.py')
-rw-r--r--src/OpenSSL/crypto.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/OpenSSL/crypto.py b/src/OpenSSL/crypto.py
index ae05ede..cdbcc22 100644
--- a/src/OpenSSL/crypto.py
+++ b/src/OpenSSL/crypto.py
@@ -10,6 +10,7 @@ from six import (
text_type as _text_type,
PY3 as _PY3)
+from cryptography import x509
from cryptography.hazmat.primitives.asymmetric import dsa, rsa
from OpenSSL._util import (
@@ -996,6 +997,37 @@ class X509(object):
_openssl_assert(x509 != _ffi.NULL)
self._x509 = _ffi.gc(x509, _lib.X509_free)
+ def to_cryptography(self):
+ """
+ Export as a ``cryptography`` certificate.
+
+ :rtype: ``cryptography.x509.Certificate``
+
+ .. versionadded:: 17.1.0
+ """
+ from cryptography.hazmat.backends.openssl.x509 import _Certificate
+ backend = _get_backend()
+ return _Certificate(backend, self._x509)
+
+ @classmethod
+ def from_cryptography(cls, crypto_cert):
+ """
+ Construct based on a ``cryptography`` *crypto_cert*.
+
+ :param crypto_key: A ``cryptography`` X.509 certificate.
+ :type crypto_key: ``cryptography.x509.Certificate``
+
+ :rtype: PKey
+
+ .. versionadded:: 17.1.0
+ """
+ if not isinstance(crypto_cert, x509.Certificate):
+ raise TypeError("Must be a certificate")
+
+ cert = cls()
+ cert._x509 = crypto_cert._x509
+ return cert
+
def set_version(self, version):
"""
Set the version number of the certificate.