aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cryptography/hazmat/backends/openssl/backend.py26
-rw-r--r--src/cryptography/hazmat/backends/openssl/rsa.py13
-rw-r--r--src/cryptography/hazmat/primitives/asymmetric/rsa.py19
-rw-r--r--src/cryptography/hazmat/primitives/serialization.py5
4 files changed, 60 insertions, 3 deletions
diff --git a/src/cryptography/hazmat/backends/openssl/backend.py b/src/cryptography/hazmat/backends/openssl/backend.py
index 42dcc0fb..f33aba95 100644
--- a/src/cryptography/hazmat/backends/openssl/backend.py
+++ b/src/cryptography/hazmat/backends/openssl/backend.py
@@ -1184,6 +1184,32 @@ class Backend(object):
assert res == 1
return self._read_mem_bio(bio)
+ def _public_key_bytes(self, encoding, format, pkcs1_write_func, evp_pkey,
+ cdata):
+ if not isinstance(encoding, serialization.Encoding):
+ raise TypeError("encoding must be an item from the Encoding enum")
+
+ if not isinstance(format, serialization.PublicFormat):
+ raise TypeError(
+ "format must be an item from the PublicFormat enum"
+ )
+
+ # This is a temporary check until we land DER serialization.
+ if encoding is not serialization.Encoding.PEM:
+ raise ValueError("Only PEM encoding is supported by this backend")
+
+ if format is serialization.PublicFormat.SubjectPublicKeyInfo:
+ write_bio = self._lib.PEM_write_bio_PUBKEY
+ key = evp_pkey
+ elif format is serialization.PublicFormat.PKCS1:
+ write_bio = pkcs1_write_func
+ key = cdata
+
+ bio = self._create_mem_bio()
+ res = write_bio(bio, key)
+ assert res == 1
+ return self._read_mem_bio(bio)
+
class GetCipherByName(object):
def __init__(self, fmt):
diff --git a/src/cryptography/hazmat/backends/openssl/rsa.py b/src/cryptography/hazmat/backends/openssl/rsa.py
index 0470c3fd..25168c2f 100644
--- a/src/cryptography/hazmat/backends/openssl/rsa.py
+++ b/src/cryptography/hazmat/backends/openssl/rsa.py
@@ -19,7 +19,7 @@ from cryptography.hazmat.primitives.asymmetric.padding import (
)
from cryptography.hazmat.primitives.asymmetric.rsa import (
RSAPrivateKeyWithNumbers, RSAPrivateKeyWithSerialization,
- RSAPublicKeyWithNumbers
+ RSAPublicKeyWithSerialization
)
@@ -572,7 +572,7 @@ class _RSAPrivateKey(object):
)
-@utils.register_interface(RSAPublicKeyWithNumbers)
+@utils.register_interface(RSAPublicKeyWithSerialization)
class _RSAPublicKey(object):
def __init__(self, backend, rsa_cdata):
self._backend = backend
@@ -604,3 +604,12 @@ class _RSAPublicKey(object):
e=self._backend._bn_to_int(self._rsa_cdata.e),
n=self._backend._bn_to_int(self._rsa_cdata.n),
)
+
+ def public_bytes(self, encoding, format):
+ return self._backend._public_key_bytes(
+ encoding,
+ format,
+ self._backend._lib.PEM_write_bio_RSAPublicKey,
+ self._evp_pkey,
+ self._rsa_cdata
+ )
diff --git a/src/cryptography/hazmat/primitives/asymmetric/rsa.py b/src/cryptography/hazmat/primitives/asymmetric/rsa.py
index 4963d85c..8adc7459 100644
--- a/src/cryptography/hazmat/primitives/asymmetric/rsa.py
+++ b/src/cryptography/hazmat/primitives/asymmetric/rsa.py
@@ -89,13 +89,30 @@ class RSAPublicKey(object):
@six.add_metaclass(abc.ABCMeta)
-class RSAPublicKeyWithNumbers(RSAPublicKey):
+class RSAPublicKeyWithSerialization(RSAPublicKey):
@abc.abstractmethod
def public_numbers(self):
"""
Returns an RSAPublicNumbers
"""
+ @abc.abstractmethod
+ def public_bytes(self, encoding, format):
+ """
+ Returns the key serialized as bytes.
+ """
+
+
+RSAPublicKeyWithNumbers = utils.deprecated(
+ RSAPublicKeyWithSerialization,
+ __name__,
+ (
+ "The RSAPublicKeyWithNumbers interface has been renamed to "
+ "RSAPublicKeyWithSerialization"
+ ),
+ utils.DeprecatedIn08
+)
+
def generate_private_key(public_exponent, key_size, backend):
if not isinstance(backend, RSABackend):
diff --git a/src/cryptography/hazmat/primitives/serialization.py b/src/cryptography/hazmat/primitives/serialization.py
index 7e363198..8699fa91 100644
--- a/src/cryptography/hazmat/primitives/serialization.py
+++ b/src/cryptography/hazmat/primitives/serialization.py
@@ -179,6 +179,11 @@ class PrivateFormat(Enum):
TraditionalOpenSSL = "TraditionalOpenSSL"
+class PublicFormat(Enum):
+ SubjectPublicKeyInfo = "X.509 subjectPublicKeyInfo with PKCS#1"
+ PKCS1 = "Raw PKCS#1"
+
+
@six.add_metaclass(abc.ABCMeta)
class KeySerializationEncryption(object):
pass