aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2015-03-05 21:01:16 -0600
committerPaul Kehrer <paul.l.kehrer@gmail.com>2015-03-08 15:50:17 -0500
commit419615b0cf02d3763b7da208d7118b39e5f25a3b (patch)
treef4e0eeacb33dfa03ad734c4aa2c8e6222edf59df /src
parent88e7ed6415ccf7fb2432b90876deefa8ab88cc98 (diff)
downloadcryptography-419615b0cf02d3763b7da208d7118b39e5f25a3b.tar.gz
cryptography-419615b0cf02d3763b7da208d7118b39e5f25a3b.tar.bz2
cryptography-419615b0cf02d3763b7da208d7118b39e5f25a3b.zip
serialize EC public keys
Diffstat (limited to 'src')
-rw-r--r--src/cryptography/hazmat/backends/openssl/ec.py23
-rw-r--r--src/cryptography/hazmat/primitives/asymmetric/ec.py19
2 files changed, 40 insertions, 2 deletions
diff --git a/src/cryptography/hazmat/backends/openssl/ec.py b/src/cryptography/hazmat/backends/openssl/ec.py
index 19d646e8..39b0a555 100644
--- a/src/cryptography/hazmat/backends/openssl/ec.py
+++ b/src/cryptography/hazmat/backends/openssl/ec.py
@@ -9,7 +9,7 @@ from cryptography.exceptions import (
InvalidSignature, UnsupportedAlgorithm, _Reasons
)
from cryptography.hazmat.backends.openssl.utils import _truncate_digest
-from cryptography.hazmat.primitives import hashes
+from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import (
AsymmetricSignatureContext, AsymmetricVerificationContext, ec
)
@@ -262,3 +262,24 @@ class _EllipticCurvePublicKey(object):
y=y,
curve=self._curve
)
+
+ def public_bytes(self, encoding, format):
+ if format is serialization.PublicFormat.PKCS1:
+ raise ValueError(
+ "EC public keys do not support PKCS1 serialization"
+ )
+
+ evp_pkey = self._backend._lib.EVP_PKEY_new()
+ assert evp_pkey != self._backend._ffi.NULL
+ evp_pkey = self._backend._ffi.gc(
+ evp_pkey, self._backend._lib.EVP_PKEY_free
+ )
+ res = self._backend._lib.EVP_PKEY_set1_EC_KEY(evp_pkey, self._ec_key)
+ assert res == 1
+ return self._backend._public_key_bytes(
+ encoding,
+ format,
+ None,
+ evp_pkey,
+ None
+ )
diff --git a/src/cryptography/hazmat/primitives/asymmetric/ec.py b/src/cryptography/hazmat/primitives/asymmetric/ec.py
index 52e14816..bf1705db 100644
--- a/src/cryptography/hazmat/primitives/asymmetric/ec.py
+++ b/src/cryptography/hazmat/primitives/asymmetric/ec.py
@@ -98,13 +98,30 @@ class EllipticCurvePublicKey(object):
@six.add_metaclass(abc.ABCMeta)
-class EllipticCurvePublicKeyWithNumbers(EllipticCurvePublicKey):
+class EllipticCurvePublicKeyWithSerialization(EllipticCurvePublicKey):
@abc.abstractmethod
def public_numbers(self):
"""
Returns an EllipticCurvePublicNumbers.
"""
+ @abc.abstractmethod
+ def public_bytes(self, encoding, format):
+ """
+ Returns the key serialized as bytes.
+ """
+
+
+EllipticCurvePublicKeyWithNumbers = utils.deprecated(
+ EllipticCurvePublicKeyWithSerialization,
+ __name__,
+ (
+ "The EllipticCurvePublicKeyWithNumbers interface has been renamed to "
+ "EllipticCurvePublicKeyWithSerialization"
+ ),
+ utils.DeprecatedIn08
+)
+
@utils.register_interface(EllipticCurve)
class SECT571R1(object):