Skip to content

Commit

Permalink
untested conversion routines
Browse files Browse the repository at this point in the history
  • Loading branch information
reaperhulk committed Mar 11, 2016
1 parent c488dea commit ec66bcc
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/OpenSSL/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
text_type as _text_type,
PY3 as _PY3)

from cryptography.hazmat.backends.openssl.backend import backend
from cryptography.hazmat.backends.openssl.x509 import (
_Certificate, _CertificateSigningRequest
)

from OpenSSL._util import (
ffi as _ffi,
lib as _lib,
Expand Down Expand Up @@ -167,6 +172,19 @@ def __init__(self):
self._pkey = _ffi.gc(pkey, _lib.EVP_PKEY_free)
self._initialized = False

def to_cryptography_key(self):
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 cryptography_key_to_pkey(cls, crypto_key):
pkey = cls()
pkey._pkey = crypto_key._evp_pkey
pkey._initialized = True
return pkey

def generate_key(self, type, bits):
"""
Generate a key pair of the given type, with the given number of bits.
Expand Down Expand Up @@ -807,6 +825,15 @@ def __init__(self):
req = _lib.X509_REQ_new()
self._req = _ffi.gc(req, _lib.X509_REQ_free)

def to_cryptography_request(self):
return _CertificateSigningRequest(self._req)

@classmethod
def cryptography_cert_to_x509(cls, crypto_req):
req = cls()
req._req = crypto_req._x509_req
return req

def set_pubkey(self, pkey):
"""
Set the public key of the certificate signing request.
Expand Down Expand Up @@ -987,6 +1014,15 @@ def __init__(self):
x509 = _lib.X509_new()
self._x509 = _ffi.gc(x509, _lib.X509_free)

def to_cryptography_cert(self):
return _Certificate(self._x509)

@classmethod
def cryptography_cert_to_x509(cls, crypto_cert):
x509 = cls()
x509._x509 = crypto_x509._x509
return x509

def set_version(self, version):
"""
Set the version number of the certificate.
Expand Down

0 comments on commit ec66bcc

Please sign in to comment.