swarmauri_crypto_jwe 0.11.0.dev2__tar.gz → 0.11.0.dev4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {swarmauri_crypto_jwe-0.11.0.dev2 → swarmauri_crypto_jwe-0.11.0.dev4}/PKG-INFO +12 -1
- {swarmauri_crypto_jwe-0.11.0.dev2 → swarmauri_crypto_jwe-0.11.0.dev4}/README.md +11 -0
- {swarmauri_crypto_jwe-0.11.0.dev2 → swarmauri_crypto_jwe-0.11.0.dev4}/pyproject.toml +1 -1
- {swarmauri_crypto_jwe-0.11.0.dev2 → swarmauri_crypto_jwe-0.11.0.dev4}/swarmauri_crypto_jwe/JweCrypto.py +113 -29
- {swarmauri_crypto_jwe-0.11.0.dev2 → swarmauri_crypto_jwe-0.11.0.dev4}/LICENSE +0 -0
- {swarmauri_crypto_jwe-0.11.0.dev2 → swarmauri_crypto_jwe-0.11.0.dev4}/swarmauri_crypto_jwe/__init__.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: swarmauri_crypto_jwe
|
|
3
|
-
Version: 0.11.0.
|
|
3
|
+
Version: 0.11.0.dev4
|
|
4
4
|
Summary: RFC 7516/7518 compliant JWE crypto provider for Swarmauri
|
|
5
5
|
License-Expression: Apache-2.0
|
|
6
6
|
License-File: LICENSE
|
|
@@ -142,6 +142,17 @@ crypto = pm.load("swarmauri.cryptos", "JweCrypto")
|
|
|
142
142
|
- Decryption requires the matching private key via `dir_key`, `rsa_private_pem`/`rsa_private_password`, or `ecdh_private_key`.
|
|
143
143
|
- `expected_algs` and `expected_encs` constrain acceptable algorithms during decryption, and `aad` must match the authenticated data provided at encryption time.
|
|
144
144
|
|
|
145
|
+
### Opt-in X448
|
|
146
|
+
|
|
147
|
+
Use `JweCrypto(enable_x448=True)` with `alg=JWAAlg.ECDH_ES`,
|
|
148
|
+
`enc=JWAAlg.A256GCM`, and a public recipient JWK with `kty="OKP"`,
|
|
149
|
+
`crv="X448"`, and a base64url-encoded `x`. Pass an
|
|
150
|
+
`X448PrivateKey` as `ecdh_private_key` to decrypt.
|
|
151
|
+
|
|
152
|
+
New ECDH-ES tokens use the RFC 7518 Concat KDF. To read X25519 or
|
|
153
|
+
NIST-curve tokens issued with the previous Swarmauri KDF, pass
|
|
154
|
+
`legacy_ecdh_kdf=True` to `decrypt_compact` for those known tokens.
|
|
155
|
+
|
|
145
156
|
## Entry point
|
|
146
157
|
|
|
147
158
|
The provider is registered under the `swarmauri.cryptos` entry point as `JweCrypto`.
|
|
@@ -111,6 +111,17 @@ crypto = pm.load("swarmauri.cryptos", "JweCrypto")
|
|
|
111
111
|
- Decryption requires the matching private key via `dir_key`, `rsa_private_pem`/`rsa_private_password`, or `ecdh_private_key`.
|
|
112
112
|
- `expected_algs` and `expected_encs` constrain acceptable algorithms during decryption, and `aad` must match the authenticated data provided at encryption time.
|
|
113
113
|
|
|
114
|
+
### Opt-in X448
|
|
115
|
+
|
|
116
|
+
Use `JweCrypto(enable_x448=True)` with `alg=JWAAlg.ECDH_ES`,
|
|
117
|
+
`enc=JWAAlg.A256GCM`, and a public recipient JWK with `kty="OKP"`,
|
|
118
|
+
`crv="X448"`, and a base64url-encoded `x`. Pass an
|
|
119
|
+
`X448PrivateKey` as `ecdh_private_key` to decrypt.
|
|
120
|
+
|
|
121
|
+
New ECDH-ES tokens use the RFC 7518 Concat KDF. To read X25519 or
|
|
122
|
+
NIST-curve tokens issued with the previous Swarmauri KDF, pass
|
|
123
|
+
`legacy_ecdh_kdf=True` to `decrypt_compact` for those known tokens.
|
|
124
|
+
|
|
114
125
|
## Entry point
|
|
115
126
|
|
|
116
127
|
The provider is registered under the `swarmauri.cryptos` entry point as `JweCrypto`.
|
|
@@ -16,7 +16,13 @@ from typing import Any, Dict, Iterable, Mapping, Optional, Tuple, Union
|
|
|
16
16
|
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
|
17
17
|
from cryptography.hazmat.primitives import hashes, serialization
|
|
18
18
|
from cryptography.hazmat.primitives.kdf.concatkdf import ConcatKDFHash
|
|
19
|
-
from cryptography.hazmat.primitives.asymmetric import
|
|
19
|
+
from cryptography.hazmat.primitives.asymmetric import (
|
|
20
|
+
ec,
|
|
21
|
+
padding,
|
|
22
|
+
rsa,
|
|
23
|
+
x25519,
|
|
24
|
+
x448,
|
|
25
|
+
)
|
|
20
26
|
from cryptography.hazmat.primitives.serialization import (
|
|
21
27
|
load_pem_public_key,
|
|
22
28
|
load_pem_private_key,
|
|
@@ -102,12 +108,35 @@ def _ec_jwk_from_public_key(pk: ec.EllipticCurvePublicKey) -> Dict[str, Any]:
|
|
|
102
108
|
return {"kty": "EC", "crv": crv_map[crv], "x": _b64u(x), "y": _b64u(y)}
|
|
103
109
|
|
|
104
110
|
|
|
105
|
-
def
|
|
111
|
+
def _okp_jwk_from_public_key(
|
|
112
|
+
pk: x25519.X25519PublicKey | x448.X448PublicKey, crv: str
|
|
113
|
+
) -> Dict[str, Any]:
|
|
106
114
|
raw = pk.public_bytes(
|
|
107
115
|
encoding=serialization.Encoding.Raw,
|
|
108
116
|
format=serialization.PublicFormat.Raw,
|
|
109
117
|
)
|
|
110
|
-
return {"kty": "OKP", "crv":
|
|
118
|
+
return {"kty": "OKP", "crv": crv, "x": _b64u(raw)}
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _load_x448_public_jwk(jwk: Mapping[str, Any]) -> x448.X448PublicKey:
|
|
122
|
+
value = jwk.get("x")
|
|
123
|
+
if not isinstance(value, str) or "d" in jwk:
|
|
124
|
+
raise ValueError("X448 requires a public JWK with an 'x' value.")
|
|
125
|
+
raw = _decode_base64url(value)
|
|
126
|
+
if len(raw) != 56:
|
|
127
|
+
raise ValueError(
|
|
128
|
+
"X448 'x' must encode 56 bytes as unpadded base64url."
|
|
129
|
+
)
|
|
130
|
+
return x448.X448PublicKey.from_public_bytes(raw)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _decode_base64url(value: Any) -> bytes:
|
|
134
|
+
if not isinstance(value, str):
|
|
135
|
+
raise ValueError("Expected an unpadded base64url string.")
|
|
136
|
+
raw = _b64u_dec(value)
|
|
137
|
+
if _b64u(raw) != value:
|
|
138
|
+
raise ValueError("Expected an unpadded base64url string.")
|
|
139
|
+
return raw
|
|
111
140
|
|
|
112
141
|
|
|
113
142
|
def _load_rsa_public(key: Any) -> rsa.RSAPublicKey:
|
|
@@ -158,6 +187,8 @@ def _load_ecdh_recipient_public(jwk_or_pem: Any) -> Tuple[str, Any]:
|
|
|
158
187
|
if kty == "OKP" and jwk_or_pem.get("crv") == "X25519":
|
|
159
188
|
x = _b64u_dec(jwk_or_pem["x"])
|
|
160
189
|
return "X25519", x25519.X25519PublicKey.from_public_bytes(x)
|
|
190
|
+
if kty == "OKP" and jwk_or_pem.get("crv") == "X448":
|
|
191
|
+
return "X448", _load_x448_public_jwk(jwk_or_pem)
|
|
161
192
|
raise ValueError(
|
|
162
193
|
f"Unsupported JWK kty/crv for ECDH-ES: {kty}/{jwk_or_pem.get('crv')}" # noqa: E501
|
|
163
194
|
)
|
|
@@ -184,23 +215,33 @@ def _load_ecdh_recipient_public(jwk_or_pem: Any) -> Tuple[str, Any]:
|
|
|
184
215
|
def _concat_kdf(
|
|
185
216
|
z: bytes,
|
|
186
217
|
enc: JWAAlg,
|
|
187
|
-
hash_alg=hashes.SHA256(),
|
|
188
218
|
apu: Optional[bytes] = None,
|
|
189
219
|
apv: Optional[bytes] = None,
|
|
220
|
+
*,
|
|
221
|
+
legacy: bool = False,
|
|
190
222
|
) -> bytes:
|
|
191
|
-
|
|
192
|
-
alg_id =
|
|
223
|
+
key_length = _cek_len_for_enc(enc)
|
|
224
|
+
alg_id = _length_prefixed(enc.value.encode("ascii"))
|
|
225
|
+
party_u, party_v = apu or b"", apv or b""
|
|
226
|
+
if legacy:
|
|
227
|
+
other_info = alg_id + party_u + party_v
|
|
228
|
+
else:
|
|
229
|
+
other_info = (
|
|
230
|
+
alg_id
|
|
231
|
+
+ _length_prefixed(party_u)
|
|
232
|
+
+ _length_prefixed(party_v)
|
|
233
|
+
+ (key_length * 8).to_bytes(4, "big")
|
|
234
|
+
)
|
|
193
235
|
ckdf = ConcatKDFHash(
|
|
194
|
-
algorithm=
|
|
195
|
-
length=
|
|
196
|
-
otherinfo=
|
|
236
|
+
algorithm=hashes.SHA256(),
|
|
237
|
+
length=key_length,
|
|
238
|
+
otherinfo=other_info,
|
|
197
239
|
)
|
|
198
240
|
return ckdf.derive(z)
|
|
199
241
|
|
|
200
242
|
|
|
201
|
-
def
|
|
202
|
-
|
|
203
|
-
return len(enc_b).to_bytes(4, "big") + enc_b
|
|
243
|
+
def _length_prefixed(value: bytes) -> bytes:
|
|
244
|
+
return len(value).to_bytes(4, "big") + value
|
|
204
245
|
|
|
205
246
|
|
|
206
247
|
JWECompact = str
|
|
@@ -225,6 +266,26 @@ class JweCrypto:
|
|
|
225
266
|
asynchronous to integrate with async workflows.
|
|
226
267
|
"""
|
|
227
268
|
|
|
269
|
+
def __init__(self, *, enable_x448: bool = False) -> None:
|
|
270
|
+
self._enable_x448 = enable_x448
|
|
271
|
+
|
|
272
|
+
def _check_x448_profile(
|
|
273
|
+
self, header: Mapping[str, Any], aad: bytes | str | None
|
|
274
|
+
) -> tuple[bytes, bytes]:
|
|
275
|
+
if not self._enable_x448:
|
|
276
|
+
raise ValueError("X448 requires JweCrypto(enable_x448=True).")
|
|
277
|
+
if header.get("alg") != "ECDH-ES" or header.get("enc") != "A256GCM":
|
|
278
|
+
raise ValueError("X448 requires ECDH-ES/A256GCM.")
|
|
279
|
+
if aad or "zip" in header or "crit" in header:
|
|
280
|
+
raise ValueError(
|
|
281
|
+
"X448 compact JWE requires unextended serialization."
|
|
282
|
+
)
|
|
283
|
+
apu = _decode_base64url(header["apu"]) if "apu" in header else b""
|
|
284
|
+
apv = _decode_base64url(header["apv"]) if "apv" in header else b""
|
|
285
|
+
if "apu" in header and "apv" in header and apu == apv:
|
|
286
|
+
raise ValueError("apu and apv must be distinct.")
|
|
287
|
+
return apu, apv
|
|
288
|
+
|
|
228
289
|
async def encrypt_compact(
|
|
229
290
|
self,
|
|
230
291
|
*,
|
|
@@ -297,11 +358,20 @@ class JweCrypto:
|
|
|
297
358
|
encrypted_key_b64 = _b64u(ekey)
|
|
298
359
|
elif alg == JWAAlg.ECDH_ES:
|
|
299
360
|
crv, rpk = _load_ecdh_recipient_public(key.get("pub") or key)
|
|
300
|
-
if crv == "
|
|
361
|
+
if crv == "X448":
|
|
362
|
+
apu_b, apv_b = self._check_x448_profile(protected, aad)
|
|
363
|
+
if enc != JWAAlg.A256GCM:
|
|
364
|
+
raise ValueError("X448 requires ECDH-ES/A256GCM.")
|
|
365
|
+
ephemeral = x448.X448PrivateKey.generate()
|
|
366
|
+
z = ephemeral.exchange(rpk)
|
|
367
|
+
epk_header = _okp_jwk_from_public_key(
|
|
368
|
+
ephemeral.public_key(), crv
|
|
369
|
+
)
|
|
370
|
+
elif crv == "X25519":
|
|
301
371
|
esk = x25519.X25519PrivateKey.generate()
|
|
302
372
|
epk = esk.public_key()
|
|
303
373
|
z = esk.exchange(rpk) # type: ignore[arg-type]
|
|
304
|
-
epk_header =
|
|
374
|
+
epk_header = _okp_jwk_from_public_key(epk, crv)
|
|
305
375
|
else:
|
|
306
376
|
curve = {
|
|
307
377
|
"P-256": ec.SECP256R1(),
|
|
@@ -312,21 +382,14 @@ class JweCrypto:
|
|
|
312
382
|
epk = esk.public_key()
|
|
313
383
|
z = esk.exchange(ec.ECDH(), rpk) # type: ignore[arg-type]
|
|
314
384
|
epk_header = _ec_jwk_from_public_key(epk)
|
|
315
|
-
|
|
316
|
-
apv_b = None
|
|
317
|
-
if "apu" in (header_extra or {}):
|
|
385
|
+
if crv != "X448":
|
|
318
386
|
apu_b = (
|
|
319
|
-
_b64u_dec(
|
|
320
|
-
if isinstance(header_extra["apu"], str)
|
|
321
|
-
else header_extra["apu"]
|
|
387
|
+
_b64u_dec(protected["apu"]) if "apu" in protected else None
|
|
322
388
|
)
|
|
323
|
-
if "apv" in (header_extra or {}):
|
|
324
389
|
apv_b = (
|
|
325
|
-
_b64u_dec(
|
|
326
|
-
if isinstance(header_extra["apv"], str)
|
|
327
|
-
else header_extra["apv"]
|
|
390
|
+
_b64u_dec(protected["apv"]) if "apv" in protected else None
|
|
328
391
|
)
|
|
329
|
-
cek = _concat_kdf(z, enc,
|
|
392
|
+
cek = _concat_kdf(z, enc, apu_b, apv_b)
|
|
330
393
|
protected["epk"] = epk_header
|
|
331
394
|
else:
|
|
332
395
|
raise ValueError(f"Unsupported alg '{alg.value}'")
|
|
@@ -362,6 +425,7 @@ class JweCrypto:
|
|
|
362
425
|
expected_algs: Optional[Iterable[JWAAlg]] = None,
|
|
363
426
|
expected_encs: Optional[Iterable[JWAAlg]] = None,
|
|
364
427
|
aad: Optional[Union[bytes, str]] = None,
|
|
428
|
+
legacy_ecdh_kdf: bool = False,
|
|
365
429
|
) -> JweDecryptResult:
|
|
366
430
|
"""Decrypt a compact JWE string.
|
|
367
431
|
|
|
@@ -375,6 +439,7 @@ class JweCrypto:
|
|
|
375
439
|
expected_algs (Iterable[JWAAlg]): Allowed algorithm values.
|
|
376
440
|
expected_encs (Iterable[JWAAlg]): Allowed encryption values.
|
|
377
441
|
aad (Union[bytes, str]): Additional authenticated data.
|
|
442
|
+
legacy_ecdh_kdf (bool): Read pre-fix X25519 or NIST-curve JWE.
|
|
378
443
|
RETURNS (JweDecryptResult): Header and plaintext of the decrypted
|
|
379
444
|
token.
|
|
380
445
|
"""
|
|
@@ -434,6 +499,10 @@ class JweCrypto:
|
|
|
434
499
|
),
|
|
435
500
|
)
|
|
436
501
|
elif alg == JWAAlg.ECDH_ES:
|
|
502
|
+
if b64_ekey:
|
|
503
|
+
raise ValueError(
|
|
504
|
+
"ECDH-ES requires an empty Encrypted Key field."
|
|
505
|
+
)
|
|
437
506
|
if ecdh_private_key is None:
|
|
438
507
|
raise ValueError(
|
|
439
508
|
"ecdh_private_key is required for ECDH-ES decryption."
|
|
@@ -443,7 +512,21 @@ class JweCrypto:
|
|
|
443
512
|
raise ValueError(
|
|
444
513
|
"Missing/invalid 'epk' in JWE header for ECDH-ES."
|
|
445
514
|
)
|
|
446
|
-
|
|
515
|
+
is_x448 = (epk.get("kty"), epk.get("crv")) == ("OKP", "X448")
|
|
516
|
+
if is_x448:
|
|
517
|
+
apu_b, apv_b = self._check_x448_profile(header, aad)
|
|
518
|
+
if len(tag) != 16:
|
|
519
|
+
raise ValueError(
|
|
520
|
+
"A256GCM requires a 128-bit authentication tag."
|
|
521
|
+
)
|
|
522
|
+
if legacy_ecdh_kdf:
|
|
523
|
+
raise ValueError("X448 requires the RFC 7518 KDF.")
|
|
524
|
+
if not isinstance(ecdh_private_key, x448.X448PrivateKey):
|
|
525
|
+
raise TypeError(
|
|
526
|
+
"ECDH-ES with X448 requires an X448PrivateKey."
|
|
527
|
+
)
|
|
528
|
+
z = ecdh_private_key.exchange(_load_x448_public_jwk(epk))
|
|
529
|
+
elif epk.get("kty") == "OKP" and epk.get("crv") == "X25519":
|
|
447
530
|
if not isinstance(ecdh_private_key, x25519.X25519PrivateKey):
|
|
448
531
|
raise TypeError(
|
|
449
532
|
"ECDH-ES with X25519 requires an X25519PrivateKey."
|
|
@@ -474,9 +557,10 @@ class JweCrypto:
|
|
|
474
557
|
z = ecdh_private_key.exchange(ec.ECDH(), rpk)
|
|
475
558
|
else:
|
|
476
559
|
raise ValueError("Unsupported 'epk' kty/crv.")
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
560
|
+
if not is_x448:
|
|
561
|
+
apu_b = _b64u_dec(header["apu"]) if "apu" in header else None
|
|
562
|
+
apv_b = _b64u_dec(header["apv"]) if "apv" in header else None
|
|
563
|
+
cek = _concat_kdf(z, enc, apu_b, apv_b, legacy=legacy_ecdh_kdf)
|
|
480
564
|
else:
|
|
481
565
|
raise ValueError(f"Unsupported alg '{alg.value}'")
|
|
482
566
|
|
|
File without changes
|
|
File without changes
|