aboutsummaryrefslogtreecommitdiffstats
path: root/src/cryptography/hazmat/backends/openssl/backend.py
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2019-01-12 21:18:21 -0800
committerAlex Gaynor <alex.gaynor@gmail.com>2019-01-13 00:18:21 -0500
commitdbcbffa06c9930a687010ca816596ca3f5cc78e9 (patch)
tree27f88222ed222e45784f4c1e6ea0b8d6b9f9d07b /src/cryptography/hazmat/backends/openssl/backend.py
parent9b198104db8b53178212b5849919b6a61ca794ab (diff)
downloadcryptography-dbcbffa06c9930a687010ca816596ca3f5cc78e9.tar.gz
cryptography-dbcbffa06c9930a687010ca816596ca3f5cc78e9.tar.bz2
cryptography-dbcbffa06c9930a687010ca816596ca3f5cc78e9.zip
support x448 public/private serialization both raw and pkcs8 (#4653)
* support x448 public/private serialization both raw and pkcs8 * add tests for all other asym key types to prevent Raw * more tests * better tests * fix a test * funny story, I'm actually illiterate. * pep8 * require PrivateFormat.Raw or PublicFormat.Raw with Encoding.Raw * missing docs * parametrize * docs fixes * remove dupe line * assert something
Diffstat (limited to 'src/cryptography/hazmat/backends/openssl/backend.py')
-rw-r--r--src/cryptography/hazmat/backends/openssl/backend.py28
1 files changed, 27 insertions, 1 deletions
diff --git a/src/cryptography/hazmat/backends/openssl/backend.py b/src/cryptography/hazmat/backends/openssl/backend.py
index cfe146f2..ecebe7b8 100644
--- a/src/cryptography/hazmat/backends/openssl/backend.py
+++ b/src/cryptography/hazmat/backends/openssl/backend.py
@@ -508,6 +508,9 @@ class Backend(object):
self.openssl_assert(dh_cdata != self._ffi.NULL)
dh_cdata = self._ffi.gc(dh_cdata, self._lib.DH_free)
return _DHPrivateKey(self, dh_cdata, evp_pkey)
+ elif key_type == getattr(self._lib, "EVP_PKEY_X448", None):
+ # EVP_PKEY_X448 is not present in OpenSSL < 1.1.1
+ return _X448PrivateKey(self, evp_pkey)
else:
raise UnsupportedAlgorithm("Unsupported key type.")
@@ -539,6 +542,9 @@ class Backend(object):
self.openssl_assert(dh_cdata != self._ffi.NULL)
dh_cdata = self._ffi.gc(dh_cdata, self._lib.DH_free)
return _DHPublicKey(self, dh_cdata, evp_pkey)
+ elif key_type == getattr(self._lib, "EVP_PKEY_X448", None):
+ # EVP_PKEY_X448 is not present in OpenSSL < 1.1.1
+ return _X448PublicKey(self, evp_pkey)
else:
raise UnsupportedAlgorithm("Unsupported key type.")
@@ -1678,6 +1684,16 @@ class Backend(object):
"format must be an item from the PrivateFormat enum"
)
+ # Raw format and encoding are only valid for X25519, Ed25519, X448, and
+ # Ed448 keys. We capture those cases before this method is called so if
+ # we see those enum values here it means the caller has passed them to
+ # a key that doesn't support raw type
+ if format is serialization.PrivateFormat.Raw:
+ raise ValueError("raw format is invalid with this key or encoding")
+
+ if encoding is serialization.Encoding.Raw:
+ raise ValueError("raw encoding is invalid with this key or format")
+
if not isinstance(encryption_algorithm,
serialization.KeySerializationEncryption):
raise TypeError(
@@ -1737,7 +1753,7 @@ class Backend(object):
write_bio = self._lib.i2d_PKCS8PrivateKey_bio
key = evp_pkey
else:
- raise TypeError("encoding must be an item from the Encoding enum")
+ raise TypeError("encoding must be Encoding.PEM or Encoding.DER")
bio = self._create_mem_bio_gc()
res = write_bio(
@@ -1770,6 +1786,16 @@ class Backend(object):
if not isinstance(encoding, serialization.Encoding):
raise TypeError("encoding must be an item from the Encoding enum")
+ # Raw format and encoding are only valid for X25519, Ed25519, X448, and
+ # Ed448 keys. We capture those cases before this method is called so if
+ # we see those enum values here it means the caller has passed them to
+ # a key that doesn't support raw type
+ if format is serialization.PublicFormat.Raw:
+ raise ValueError("raw format is invalid with this key or encoding")
+
+ if encoding is serialization.Encoding.Raw:
+ raise ValueError("raw encoding is invalid with this key or format")
+
if (
format is serialization.PublicFormat.OpenSSH or
encoding is serialization.Encoding.OpenSSH