diff options
author | Alex Gaynor <alex.gaynor@gmail.com> | 2015-03-08 18:18:33 -0400 |
---|---|---|
committer | Alex Gaynor <alex.gaynor@gmail.com> | 2015-03-08 18:18:33 -0400 |
commit | 785cb422767cb7a99fa71d28e2e2e13b16f35c90 (patch) | |
tree | 04a8582915cffa714297a804c0b0792393607df2 /src | |
parent | 88e7ed6415ccf7fb2432b90876deefa8ab88cc98 (diff) | |
parent | c2ab3076c0d79e4d1b0027cdca7b99f14a5007c8 (diff) | |
download | cryptography-785cb422767cb7a99fa71d28e2e2e13b16f35c90.tar.gz cryptography-785cb422767cb7a99fa71d28e2e2e13b16f35c90.tar.bz2 cryptography-785cb422767cb7a99fa71d28e2e2e13b16f35c90.zip |
Merge pull request #1732 from reaperhulk/serialize-ec-public-key
serialize EC public keys
Diffstat (limited to 'src')
-rw-r--r-- | src/cryptography/hazmat/backends/openssl/ec.py | 23 | ||||
-rw-r--r-- | src/cryptography/hazmat/primitives/asymmetric/ec.py | 19 |
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): |