|
3 | 3 | from cryptography.hazmat.primitives import serialization |
4 | 4 | from cryptography.hazmat.primitives.asymmetric import ed448 |
5 | 5 | from cryptography.hazmat.primitives.asymmetric import ed25519 |
| 6 | +from cryptography.hazmat.primitives.asymmetric import x448 |
| 7 | +from cryptography.hazmat.primitives.asymmetric import x25519 |
6 | 8 |
|
7 | 9 | from cryptojwt.exception import KeyNotFound |
8 | 10 |
|
9 | 11 | from ..exception import DeSerializationNotPossible |
10 | 12 | from ..exception import JWKESTException |
11 | 13 | from ..exception import UnsupportedOKPCurve |
12 | | -from ..utils import as_unicode |
13 | 14 | from ..utils import b64d |
14 | 15 | from ..utils import b64e |
15 | 16 | from .asym import AsymmetricKey |
16 | 17 | from .x509 import import_private_key_from_pem_file |
17 | 18 | from .x509 import import_public_key_from_pem_data |
18 | 19 | from .x509 import import_public_key_from_pem_file |
19 | 20 |
|
20 | | -OKPPublicKey = Union[ed25519.Ed25519PublicKey, ed448.Ed448PublicKey] |
21 | | -OKPPrivateKey = Union[ed25519.Ed25519PrivateKey, ed448.Ed448PrivateKey] |
| 21 | +OKPPublicKey = Union[ |
| 22 | + ed25519.Ed25519PublicKey, ed448.Ed448PublicKey, x25519.X25519PublicKey, x448.X448PublicKey |
| 23 | +] |
| 24 | +OKPPrivateKey = Union[ |
| 25 | + ed25519.Ed25519PrivateKey, ed448.Ed448PrivateKey, x25519.X25519PrivateKey, x448.X448PrivateKey |
| 26 | +] |
22 | 27 |
|
23 | | -CRV2PUBLIC = {"Ed25519": ed25519.Ed25519PublicKey, "Ed448": ed448.Ed448PublicKey} |
| 28 | +CRV2PUBLIC = { |
| 29 | + "Ed25519": ed25519.Ed25519PublicKey, |
| 30 | + "Ed448": ed448.Ed448PublicKey, |
| 31 | + "X25519": x25519.X25519PublicKey, |
| 32 | + "X448": x448.X448PublicKey, |
| 33 | +} |
24 | 34 |
|
25 | | -CRV2PRIVATE = {"Ed25519": ed25519.Ed25519PrivateKey, "Ed448": ed448.Ed448PrivateKey} |
| 35 | +CRV2PRIVATE = { |
| 36 | + "Ed25519": ed25519.Ed25519PrivateKey, |
| 37 | + "Ed448": ed448.Ed448PrivateKey, |
| 38 | + "X25519": x25519.X25519PrivateKey, |
| 39 | + "X448": x448.X448PrivateKey, |
| 40 | +} |
| 41 | + |
| 42 | +CRV_SIGN = ["Ed25519", "Ed448"] |
| 43 | +CRV_ENCR = ["X25519", "X448"] |
26 | 44 |
|
27 | 45 |
|
28 | 46 | def is_private_key(key) -> bool: |
29 | | - if isinstance(key, (ed25519.Ed25519PrivateKey, ed448.Ed448PrivateKey)): |
| 47 | + if isinstance( |
| 48 | + key, |
| 49 | + ( |
| 50 | + ed25519.Ed25519PrivateKey, |
| 51 | + ed448.Ed448PrivateKey, |
| 52 | + x25519.X25519PrivateKey, |
| 53 | + x448.X448PrivateKey, |
| 54 | + ), |
| 55 | + ): |
30 | 56 | return True |
31 | | - elif isinstance(key, (ed25519.Ed25519PublicKey, ed448.Ed448PublicKey)): |
| 57 | + elif isinstance( |
| 58 | + key, |
| 59 | + ( |
| 60 | + ed25519.Ed25519PublicKey, |
| 61 | + ed448.Ed448PublicKey, |
| 62 | + ed448.Ed448PublicKey, |
| 63 | + x25519.X25519PublicKey, |
| 64 | + x448.X448PublicKey, |
| 65 | + ), |
| 66 | + ): |
32 | 67 | return False |
33 | 68 | raise TypeError |
34 | 69 |
|
@@ -119,51 +154,50 @@ def deserialize(self): |
119 | 154 | try: |
120 | 155 | self.pub_key = CRV2PUBLIC[self.crv].from_public_bytes(_x) |
121 | 156 | except KeyError: |
122 | | - raise UnsupportedOKPCurve("Unsupported OKP curve: {}".format(num["crv"])) |
| 157 | + raise UnsupportedOKPCurve("Unsupported OKP curve: {}".format(self.crv)) |
| 158 | + |
| 159 | + def _serialize_public(self, key): |
| 160 | + self.x = b64e( |
| 161 | + key.public_bytes( |
| 162 | + encoding=serialization.Encoding.Raw, format=serialization.PublicFormat.Raw |
| 163 | + ) |
| 164 | + ).decode("ascii") |
| 165 | + |
| 166 | + def _serialize_private(self, key): |
| 167 | + self._serialize_public(key.public_key()) |
| 168 | + self.d = b64e( |
| 169 | + key.private_bytes( |
| 170 | + encoding=serialization.Encoding.Raw, |
| 171 | + format=serialization.PrivateFormat.Raw, |
| 172 | + encryption_algorithm=serialization.NoEncryption(), |
| 173 | + ) |
| 174 | + ).decode("ascii") |
123 | 175 |
|
124 | 176 | def _serialize(self, key): |
125 | 177 | if isinstance(key, ed25519.Ed25519PublicKey): |
126 | | - self.x = b64e( |
127 | | - key.public_bytes( |
128 | | - encoding=serialization.Encoding.Raw, format=serialization.PublicFormat.Raw |
129 | | - ) |
130 | | - ).decode("ascii") |
| 178 | + self._serialize_public(key) |
131 | 179 | self.crv = "Ed25519" |
132 | 180 | elif isinstance(key, ed25519.Ed25519PrivateKey): |
133 | | - self.x = b64e( |
134 | | - key.public_key().public_bytes( |
135 | | - encoding=serialization.Encoding.Raw, format=serialization.PublicFormat.Raw |
136 | | - ) |
137 | | - ).decode("ascii") |
138 | | - self.d = b64e( |
139 | | - key.private_bytes( |
140 | | - encoding=serialization.Encoding.Raw, |
141 | | - format=serialization.PrivateFormat.Raw, |
142 | | - encryption_algorithm=serialization.NoEncryption(), |
143 | | - ) |
144 | | - ).decode("ascii") |
| 181 | + self._serialize_private(key) |
145 | 182 | self.crv = "Ed25519" |
| 183 | + elif isinstance(key, x25519.X25519PublicKey): |
| 184 | + self._serialize_public(key) |
| 185 | + self.crv = "X25519" |
| 186 | + elif isinstance(key, x25519.X25519PrivateKey): |
| 187 | + self._serialize_private(key) |
| 188 | + self.crv = "X25519" |
146 | 189 | elif isinstance(key, ed448.Ed448PublicKey): |
147 | | - self.x = b64e( |
148 | | - key.public_bytes( |
149 | | - encoding=serialization.Encoding.Raw, format=serialization.PublicFormat.Raw |
150 | | - ) |
151 | | - ).decode("ascii") |
| 190 | + self._serialize_public(key) |
152 | 191 | self.crv = "Ed448" |
153 | 192 | elif isinstance(key, ed448.Ed448PrivateKey): |
154 | | - self.x = b64e( |
155 | | - key.public_key().public_bytes( |
156 | | - encoding=serialization.Encoding.Raw, format=serialization.PublicFormat.Raw |
157 | | - ) |
158 | | - ).decode("ascii") |
159 | | - self.d = b64e( |
160 | | - key.private_bytes( |
161 | | - encoding=serialization.Encoding.Raw, |
162 | | - format=serialization.PrivateFormat.Raw, |
163 | | - encryption_algorithm=serialization.NoEncryption(), |
164 | | - ) |
165 | | - ).decode("ascii") |
| 193 | + self._serialize_private(key) |
166 | 194 | self.crv = "Ed448" |
| 195 | + elif isinstance(key, x448.X448PublicKey): |
| 196 | + self._serialize_public(key) |
| 197 | + self.crv = "X448" |
| 198 | + elif isinstance(key, x448.X448PrivateKey): |
| 199 | + self._serialize_private(key) |
| 200 | + self.crv = "X448" |
167 | 201 |
|
168 | 202 | def serialize(self, private=False): |
169 | 203 | """ |
|
0 commit comments