Decrypts a JWE compact serialized string and returns the plaintext. Args: jwe_str (str): A JWE to be decrypt. key (str or dict): A key to attempt to decrypt the payload with. Can be individual JWK or JWK set. Returns: bytes: The plaintext bytes, assuming
(jwe_str, key)
| 58 | |
| 59 | |
| 60 | def decrypt(jwe_str, key): |
| 61 | """Decrypts a JWE compact serialized string and returns the plaintext. |
| 62 | |
| 63 | Args: |
| 64 | jwe_str (str): A JWE to be decrypt. |
| 65 | key (str or dict): A key to attempt to decrypt the payload with. Can be |
| 66 | individual JWK or JWK set. |
| 67 | |
| 68 | Returns: |
| 69 | bytes: The plaintext bytes, assuming the authentication tag is valid. |
| 70 | |
| 71 | Raises: |
| 72 | JWEError: If there is an exception verifying the token. |
| 73 | |
| 74 | Examples: |
| 75 | >>> from jose import jwe |
| 76 | >>> jwe.decrypt(jwe_string, 'asecret128bitkey') |
| 77 | 'Hello, World!' |
| 78 | """ |
| 79 | |
| 80 | # Limit the token size - if the data is compressed then decompressing the |
| 81 | # data could lead to large memory usage. This helps address This addresses |
| 82 | # CVE-2024-33664. Also see _decompress() |
| 83 | if len(jwe_str) > JWE_SIZE_LIMIT: |
| 84 | raise JWEError(f"JWE string {len(jwe_str)} bytes exceeds {JWE_SIZE_LIMIT} bytes") |
| 85 | |
| 86 | header, encoded_header, encrypted_key, iv, cipher_text, auth_tag = _jwe_compact_deserialize(jwe_str) |
| 87 | |
| 88 | # Verify that the implementation understands and can process all |
| 89 | # fields that it is required to support, whether required by this |
| 90 | # specification, by the algorithms being used, or by the "crit" |
| 91 | # Header Parameter value, and that the values of those parameters |
| 92 | # are also understood and supported. |
| 93 | |
| 94 | try: |
| 95 | # Determine the Key Management Mode employed by the algorithm |
| 96 | # specified by the "alg" (algorithm) Header Parameter. |
| 97 | alg = header["alg"] |
| 98 | enc = header["enc"] |
| 99 | if alg not in ALGORITHMS.SUPPORTED: |
| 100 | raise JWEError("Algorithm %s not supported." % alg) |
| 101 | if enc not in ALGORITHMS.SUPPORTED: |
| 102 | raise JWEError("Algorithm %s not supported." % enc) |
| 103 | |
| 104 | except KeyError: |
| 105 | raise JWEParseError("alg and enc headers are required!") |
| 106 | |
| 107 | # Verify that the JWE uses a key known to the recipient. |
| 108 | key = jwk.construct(key, alg) |
| 109 | |
| 110 | # When Direct Key Agreement or Key Agreement with Key Wrapping are |
| 111 | # employed, use the key agreement algorithm to compute the value |
| 112 | # of the agreed upon key. When Direct Key Agreement is employed, |
| 113 | # let the CEK be the agreed upon key. When Key Agreement with Key |
| 114 | # Wrapping is employed, the agreed upon key will be used to |
| 115 | # decrypt the JWE Encrypted Key. |
| 116 | # |
| 117 | # When Key Wrapping, Key Encryption, or Key Agreement with Key |
nothing calls this directly
no test coverage detected
searching dependent graphs…