Skip to content

Commit

Permalink
Add documentation and changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
hynek authored and reaperhulk committed Jul 1, 2016
1 parent c0afc89 commit 88d05f4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -25,6 +25,8 @@ Changes:

- Enable use of CRL (and more) in verify context.
`#483 <https://github.com/pyca/pyopenssl/pull/483>`_
- ``OpenSSL.crypto.PKey`` can now be constructed from ``cryptography`` objects and also exported as such.
`#439 <https://github.com/pyca/pyopenssl/pull/439>`_


----
Expand Down
20 changes: 20 additions & 0 deletions src/OpenSSL/crypto.py
Expand Up @@ -171,13 +171,33 @@ def __init__(self):
self._initialized = False

def to_cryptography_key(self):
"""
Export as a ``cryptography`` key.
:rtype: One of ``cryptography``'s `key interfaces`_.
.. _key interfaces: https://cryptography.io/en/latest/hazmat/\
primitives/asymmetric/rsa/#key-interfaces
.. versionadded:: 16.1.0
"""
if self._only_public:
return backend._evp_pkey_to_public_key(self._pkey)
else:
return backend._evp_pkey_to_private_key(self._pkey)

@classmethod
def from_cryptography_key(cls, crypto_key):
"""
Construct based on a ``cryptography`` *crypto_key*.
:param crypto_key: A ``cryptography`` key.
:type crypto_key: One of ``cryptography``'s `key interfaces`_.
:rtype: PKey
.. versionadded:: 16.1.0
"""
pkey = cls()
pkey._pkey = crypto_key._evp_pkey
if isinstance(crypto_key, (rsa.RSAPublicKey, dsa.DSAPublicKey)):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_crypto.py
Expand Up @@ -762,7 +762,7 @@ class TestPKey(object):

def test_convert_from_cryptography_private_key(self):
"""
Convert from a cryptography private key to a pyOpenSSL PKey.
PKey.from_cryptography_key creates a proper private PKey.
"""
key = serialization.load_pem_private_key(
intermediate_key_pem, None, backend
Expand All @@ -776,7 +776,7 @@ def test_convert_from_cryptography_private_key(self):

def test_convert_from_cryptography_public_key(self):
"""
Convert from a cryptography public key to a pyOpenSSL PKey.
PKey.from_cryptography_key creates a proper public PKey.
"""
key = serialization.load_pem_public_key(cleartextPublicKeyPEM, backend)
pkey = PKey.from_cryptography_key(key)
Expand All @@ -788,7 +788,7 @@ def test_convert_from_cryptography_public_key(self):

def test_convert_public_pkey_to_cryptography_key(self):
"""
Convert from a pyOpenSSL PKey to a cryptography public key.
PKey.to_cryptography_key creates a proper cryptography public key.
"""
pkey = load_publickey(FILETYPE_PEM, cleartextPublicKeyPEM)
key = pkey.to_cryptography_key()
Expand All @@ -798,7 +798,7 @@ def test_convert_public_pkey_to_cryptography_key(self):

def test_convert_private_pkey_to_cryptography_key(self):
"""
Convert from a pyOpenSSL PKey to a cryptography private key.
PKey.to_cryptography_key creates a proper cryptography private key.
"""
pkey = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM)
key = pkey.to_cryptography_key()
Expand Down

0 comments on commit 88d05f4

Please sign in to comment.