You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

620 rivejä
22 KiB

  1. import binascii
  2. import json
  3. import zlib
  4. from collections.abc import Mapping
  5. from struct import pack
  6. from . import jwk
  7. from .backends import get_random_bytes
  8. from .constants import ALGORITHMS, JWE_SIZE_LIMIT, ZIPS
  9. from .exceptions import JWEError, JWEParseError
  10. from .utils import base64url_decode, base64url_encode, ensure_binary
  11. def encrypt(plaintext, key, encryption=ALGORITHMS.A256GCM, algorithm=ALGORITHMS.DIR, zip=None, cty=None, kid=None):
  12. """Encrypts plaintext and returns a JWE compact serialization string.
  13. Args:
  14. plaintext (bytes): A bytes object to encrypt
  15. key (str or dict): The key(s) to use for encrypting the content. Can be
  16. individual JWK or JWK set.
  17. encryption (str, optional): The content encryption algorithm used to
  18. perform authenticated encryption on the plaintext to produce the
  19. ciphertext and the Authentication Tag. Defaults to A256GCM.
  20. algorithm (str, optional): The cryptographic algorithm used
  21. to encrypt or determine the value of the CEK. Defaults to dir.
  22. zip (str, optional): The compression algorithm) applied to the
  23. plaintext before encryption. Defaults to None.
  24. cty (str, optional): The media type for the secured content.
  25. See http://www.iana.org/assignments/media-types/media-types.xhtml
  26. kid (str, optional): Key ID for the provided key
  27. Returns:
  28. bytes: The string representation of the header, encrypted key,
  29. initialization vector, ciphertext, and authentication tag.
  30. Raises:
  31. JWEError: If there is an error signing the token.
  32. Examples:
  33. >>> from jose import jwe
  34. >>> jwe.encrypt('Hello, World!', 'asecret128bitkey', algorithm='dir', encryption='A128GCM')
  35. 'eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4R0NNIn0..McILMB3dYsNJSuhcDzQshA.OfX9H_mcUpHDeRM4IA.CcnTWqaqxNsjT4eCaUABSg'
  36. """
  37. plaintext = ensure_binary(plaintext) # Make sure it's bytes
  38. if algorithm not in ALGORITHMS.SUPPORTED:
  39. raise JWEError("Algorithm %s not supported." % algorithm)
  40. if encryption not in ALGORITHMS.SUPPORTED:
  41. raise JWEError("Algorithm %s not supported." % encryption)
  42. key = jwk.construct(key, algorithm)
  43. encoded_header = _encoded_header(algorithm, encryption, zip, cty, kid)
  44. plaintext = _compress(zip, plaintext)
  45. enc_cek, iv, cipher_text, auth_tag = _encrypt_and_auth(key, algorithm, encryption, zip, plaintext, encoded_header)
  46. jwe_string = _jwe_compact_serialize(encoded_header, enc_cek, iv, cipher_text, auth_tag)
  47. return jwe_string
  48. def decrypt(jwe_str, key):
  49. """Decrypts a JWE compact serialized string and returns the plaintext.
  50. Args:
  51. jwe_str (str): A JWE to be decrypt.
  52. key (str or dict): A key to attempt to decrypt the payload with. Can be
  53. individual JWK or JWK set.
  54. Returns:
  55. bytes: The plaintext bytes, assuming the authentication tag is valid.
  56. Raises:
  57. JWEError: If there is an exception verifying the token.
  58. Examples:
  59. >>> from jose import jwe
  60. >>> jwe.decrypt(jwe_string, 'asecret128bitkey')
  61. 'Hello, World!'
  62. """
  63. # Limit the token size - if the data is compressed then decompressing the
  64. # data could lead to large memory usage. This helps address This addresses
  65. # CVE-2024-33664. Also see _decompress()
  66. if len(jwe_str) > JWE_SIZE_LIMIT:
  67. raise JWEError(f"JWE string {len(jwe_str)} bytes exceeds {JWE_SIZE_LIMIT} bytes")
  68. header, encoded_header, encrypted_key, iv, cipher_text, auth_tag = _jwe_compact_deserialize(jwe_str)
  69. # Verify that the implementation understands and can process all
  70. # fields that it is required to support, whether required by this
  71. # specification, by the algorithms being used, or by the "crit"
  72. # Header Parameter value, and that the values of those parameters
  73. # are also understood and supported.
  74. try:
  75. # Determine the Key Management Mode employed by the algorithm
  76. # specified by the "alg" (algorithm) Header Parameter.
  77. alg = header["alg"]
  78. enc = header["enc"]
  79. if alg not in ALGORITHMS.SUPPORTED:
  80. raise JWEError("Algorithm %s not supported." % alg)
  81. if enc not in ALGORITHMS.SUPPORTED:
  82. raise JWEError("Algorithm %s not supported." % enc)
  83. except KeyError:
  84. raise JWEParseError("alg and enc headers are required!")
  85. # Verify that the JWE uses a key known to the recipient.
  86. key = jwk.construct(key, alg)
  87. # When Direct Key Agreement or Key Agreement with Key Wrapping are
  88. # employed, use the key agreement algorithm to compute the value
  89. # of the agreed upon key. When Direct Key Agreement is employed,
  90. # let the CEK be the agreed upon key. When Key Agreement with Key
  91. # Wrapping is employed, the agreed upon key will be used to
  92. # decrypt the JWE Encrypted Key.
  93. #
  94. # When Key Wrapping, Key Encryption, or Key Agreement with Key
  95. # Wrapping are employed, decrypt the JWE Encrypted Key to produce
  96. # the CEK. The CEK MUST have a length equal to that required for
  97. # the content encryption algorithm. Note that when there are
  98. # multiple recipients, each recipient will only be able to decrypt
  99. # JWE Encrypted Key values that were encrypted to a key in that
  100. # recipient's possession. It is therefore normal to only be able
  101. # to decrypt one of the per-recipient JWE Encrypted Key values to
  102. # obtain the CEK value. Also, see Section 11.5 for security
  103. # considerations on mitigating timing attacks.
  104. if alg == ALGORITHMS.DIR:
  105. # When Direct Key Agreement or Direct Encryption are employed,
  106. # verify that the JWE Encrypted Key value is an empty octet
  107. # sequence.
  108. # Record whether the CEK could be successfully determined for this
  109. # recipient or not.
  110. cek_valid = encrypted_key == b""
  111. # When Direct Encryption is employed, let the CEK be the shared
  112. # symmetric key.
  113. cek_bytes = _get_key_bytes_from_key(key)
  114. else:
  115. try:
  116. cek_bytes = key.unwrap_key(encrypted_key)
  117. # Record whether the CEK could be successfully determined for this
  118. # recipient or not.
  119. cek_valid = True
  120. except NotImplementedError:
  121. raise JWEError(f"alg {alg} is not implemented")
  122. except Exception:
  123. # Record whether the CEK could be successfully determined for this
  124. # recipient or not.
  125. cek_valid = False
  126. # To mitigate the attacks described in RFC 3218 [RFC3218], the
  127. # recipient MUST NOT distinguish between format, padding, and length
  128. # errors of encrypted keys. It is strongly recommended, in the event
  129. # of receiving an improperly formatted key, that the recipient
  130. # substitute a randomly generated CEK and proceed to the next step, to
  131. # mitigate timing attacks.
  132. cek_bytes = _get_random_cek_bytes_for_enc(enc)
  133. # Compute the Encoded Protected Header value BASE64URL(UTF8(JWE
  134. # Protected Header)). If the JWE Protected Header is not present
  135. # (which can only happen when using the JWE JSON Serialization and
  136. # no "protected" member is present), let this value be the empty
  137. # string.
  138. protected_header = encoded_header
  139. # Let the Additional Authenticated Data encryption parameter be
  140. # ASCII(Encoded Protected Header). However, if a JWE AAD value is
  141. # present (which can only be the case when using the JWE JSON
  142. # Serialization), instead let the Additional Authenticated Data
  143. # encryption parameter be ASCII(Encoded Protected Header || '.' ||
  144. # BASE64URL(JWE AAD)).
  145. aad = protected_header
  146. # Decrypt the JWE Ciphertext using the CEK, the JWE Initialization
  147. # Vector, the Additional Authenticated Data value, and the JWE
  148. # Authentication Tag (which is the Authentication Tag input to the
  149. # calculation) using the specified content encryption algorithm,
  150. # returning the decrypted plaintext and validating the JWE
  151. # Authentication Tag in the manner specified for the algorithm,
  152. # rejecting the input without emitting any decrypted output if the
  153. # JWE Authentication Tag is incorrect.
  154. try:
  155. plain_text = _decrypt_and_auth(cek_bytes, enc, cipher_text, iv, aad, auth_tag)
  156. except NotImplementedError:
  157. raise JWEError(f"enc {enc} is not implemented")
  158. except Exception as e:
  159. raise JWEError(e)
  160. # If a "zip" parameter was included, uncompress the decrypted
  161. # plaintext using the specified compression algorithm.
  162. if plain_text is not None:
  163. plain_text = _decompress(header.get("zip"), plain_text)
  164. return plain_text if cek_valid else None
  165. def get_unverified_header(jwe_str):
  166. """Returns the decoded headers without verification of any kind.
  167. Args:
  168. jwe_str (str): A compact serialized JWE to decode the headers from.
  169. Returns:
  170. dict: The dict representation of the JWE headers.
  171. Raises:
  172. JWEError: If there is an exception decoding the JWE.
  173. """
  174. header = _jwe_compact_deserialize(jwe_str)[0]
  175. return header
  176. def _decrypt_and_auth(cek_bytes, enc, cipher_text, iv, aad, auth_tag):
  177. """
  178. Decrypt and verify the data
  179. Args:
  180. cek_bytes (bytes): cek to derive encryption and possible auth key to
  181. verify the auth tag
  182. cipher_text (bytes): Encrypted data
  183. iv (bytes): Initialization vector (iv) used to encrypt data
  184. aad (bytes): Additional Authenticated Data used to verify the data
  185. auth_tag (bytes): Authentication ntag to verify the data
  186. Returns:
  187. (bytes): Decrypted data
  188. """
  189. # Decrypt the JWE Ciphertext using the CEK, the JWE Initialization
  190. # Vector, the Additional Authenticated Data value, and the JWE
  191. # Authentication Tag (which is the Authentication Tag input to the
  192. # calculation) using the specified content encryption algorithm,
  193. # returning the decrypted plaintext
  194. # and validating the JWE
  195. # Authentication Tag in the manner specified for the algorithm,
  196. if enc in ALGORITHMS.HMAC_AUTH_TAG:
  197. encryption_key, mac_key, key_len = _get_encryption_key_mac_key_and_key_length_from_cek(cek_bytes, enc)
  198. auth_tag_check = _auth_tag(cipher_text, iv, aad, mac_key, key_len)
  199. elif enc in ALGORITHMS.GCM:
  200. encryption_key = jwk.construct(cek_bytes, enc)
  201. auth_tag_check = auth_tag # GCM check auth on decrypt
  202. else:
  203. raise NotImplementedError(f"enc {enc} is not implemented!")
  204. plaintext = encryption_key.decrypt(cipher_text, iv, aad, auth_tag)
  205. if auth_tag != auth_tag_check:
  206. raise JWEError("Invalid JWE Auth Tag")
  207. return plaintext
  208. def _get_encryption_key_mac_key_and_key_length_from_cek(cek_bytes, enc):
  209. derived_key_len = len(cek_bytes) // 2
  210. mac_key_bytes = cek_bytes[0:derived_key_len]
  211. mac_key = _get_hmac_key(enc, mac_key_bytes)
  212. encryption_key_bytes = cek_bytes[-derived_key_len:]
  213. encryption_alg, _ = enc.split("-")
  214. encryption_key = jwk.construct(encryption_key_bytes, encryption_alg)
  215. return encryption_key, mac_key, derived_key_len
  216. def _jwe_compact_deserialize(jwe_bytes):
  217. """
  218. Deserialize and verify the header and segments are appropriate.
  219. Args:
  220. jwe_bytes (bytes): The compact serialized JWE
  221. Returns:
  222. (dict, bytes, bytes, bytes, bytes, bytes)
  223. """
  224. # Base64url decode the encoded representations of the JWE
  225. # Protected Header, the JWE Encrypted Key, the JWE Initialization
  226. # Vector, the JWE Ciphertext, the JWE Authentication Tag, and the
  227. # JWE AAD, following the restriction that no line breaks,
  228. # whitespace, or other additional characters have been used.
  229. jwe_bytes = ensure_binary(jwe_bytes)
  230. try:
  231. header_segment, encrypted_key_segment, iv_segment, cipher_text_segment, auth_tag_segment = jwe_bytes.split(
  232. b".", 4
  233. )
  234. header_data = base64url_decode(header_segment)
  235. except ValueError:
  236. raise JWEParseError("Not enough segments")
  237. except (TypeError, binascii.Error):
  238. raise JWEParseError("Invalid header")
  239. # Verify that the octet sequence resulting from decoding the
  240. # encoded JWE Protected Header is a UTF-8-encoded representation
  241. # of a completely valid JSON object conforming to RFC 7159
  242. # [RFC7159]; let the JWE Protected Header be this JSON object.
  243. #
  244. # If using the JWE Compact Serialization, let the JOSE Header be
  245. # the JWE Protected Header. Otherwise, when using the JWE JSON
  246. # Serialization, let the JOSE Header be the union of the members
  247. # of the JWE Protected Header, the JWE Shared Unprotected Header
  248. # and the corresponding JWE Per-Recipient Unprotected Header, all
  249. # of which must be completely valid JSON objects. During this
  250. # step, verify that the resulting JOSE Header does not contain
  251. # duplicate Header Parameter names. When using the JWE JSON
  252. # Serialization, this restriction includes that the same Header
  253. # Parameter name also MUST NOT occur in distinct JSON object
  254. # values that together comprise the JOSE Header.
  255. try:
  256. header = json.loads(header_data)
  257. except ValueError as e:
  258. raise JWEParseError(f"Invalid header string: {e}")
  259. if not isinstance(header, Mapping):
  260. raise JWEParseError("Invalid header string: must be a json object")
  261. try:
  262. encrypted_key = base64url_decode(encrypted_key_segment)
  263. except (TypeError, binascii.Error):
  264. raise JWEParseError("Invalid encrypted key")
  265. try:
  266. iv = base64url_decode(iv_segment)
  267. except (TypeError, binascii.Error):
  268. raise JWEParseError("Invalid IV")
  269. try:
  270. ciphertext = base64url_decode(cipher_text_segment)
  271. except (TypeError, binascii.Error):
  272. raise JWEParseError("Invalid cyphertext")
  273. try:
  274. auth_tag = base64url_decode(auth_tag_segment)
  275. except (TypeError, binascii.Error):
  276. raise JWEParseError("Invalid auth tag")
  277. return header, header_segment, encrypted_key, iv, ciphertext, auth_tag
  278. def _encoded_header(alg, enc, zip, cty, kid):
  279. """
  280. Generate an appropriate JOSE header based on the values provided
  281. Args:
  282. alg (str): Key wrap/negotiation algorithm
  283. enc (str): Encryption algorithm
  284. zip (str): Compression method
  285. cty (str): Content type of the encrypted data
  286. kid (str): ID for the key used for the operation
  287. Returns:
  288. bytes: JSON object of header based on input
  289. """
  290. header = {"alg": alg, "enc": enc}
  291. if zip:
  292. header["zip"] = zip
  293. if cty:
  294. header["cty"] = cty
  295. if kid:
  296. header["kid"] = kid
  297. json_header = json.dumps(
  298. header,
  299. separators=(",", ":"),
  300. sort_keys=True,
  301. ).encode("utf-8")
  302. return base64url_encode(json_header)
  303. def _big_endian(int_val):
  304. return pack("!Q", int_val)
  305. def _encrypt_and_auth(key, alg, enc, zip, plaintext, aad):
  306. """
  307. Generate a content encryption key (cek) and initialization
  308. vector (iv) based on enc and alg, compress the plaintext based on zip,
  309. encrypt the compressed plaintext using the cek and iv based on enc
  310. Args:
  311. key (Key): The key provided for encryption
  312. alg (str): The algorithm use for key wrap/negotiation
  313. enc (str): The encryption algorithm with which to encrypt the plaintext
  314. zip (str): The compression algorithm with which to compress the plaintext
  315. plaintext (bytes): The data to encrypt
  316. aad (str): Additional authentication data utilized for generating an
  317. auth tag
  318. Returns:
  319. (bytes, bytes, bytes, bytes): A tuple of the following data
  320. (key wrapped cek, iv, cipher text, auth tag)
  321. """
  322. try:
  323. cek_bytes, kw_cek = _get_cek(enc, alg, key)
  324. except NotImplementedError:
  325. raise JWEError(f"alg {alg} is not implemented")
  326. if enc in ALGORITHMS.HMAC_AUTH_TAG:
  327. encryption_key, mac_key, key_len = _get_encryption_key_mac_key_and_key_length_from_cek(cek_bytes, enc)
  328. iv, ciphertext, tag = encryption_key.encrypt(plaintext, aad)
  329. auth_tag = _auth_tag(ciphertext, iv, aad, mac_key, key_len)
  330. elif enc in ALGORITHMS.GCM:
  331. encryption_key = jwk.construct(cek_bytes, enc)
  332. iv, ciphertext, auth_tag = encryption_key.encrypt(plaintext, aad)
  333. else:
  334. raise NotImplementedError(f"enc {enc} is not implemented!")
  335. return kw_cek, iv, ciphertext, auth_tag
  336. def _get_hmac_key(enc, mac_key_bytes):
  337. """
  338. Get an HMACKey for the provided encryption algorithm and key bytes
  339. Args:
  340. enc (str): Encryption algorithm
  341. mac_key_bytes (bytes): vytes for the HMAC key
  342. Returns:
  343. (HMACKey): The key to perform HMAC actions
  344. """
  345. _, hash_alg = enc.split("-")
  346. mac_key = jwk.construct(mac_key_bytes, hash_alg)
  347. return mac_key
  348. def _compress(zip, plaintext):
  349. """
  350. Compress the plaintext based on the algorithm supplied
  351. Args:
  352. zip (str): Compression Algorithm
  353. plaintext (bytes): plaintext to compress
  354. Returns:
  355. (bytes): Compressed plaintext
  356. """
  357. if zip not in ZIPS.SUPPORTED:
  358. raise NotImplementedError(f"ZIP {zip} is not supported!")
  359. if zip is None:
  360. compressed = plaintext
  361. elif zip == ZIPS.DEF:
  362. compressed = zlib.compress(plaintext)
  363. else:
  364. raise NotImplementedError(f"ZIP {zip} is not implemented!")
  365. return compressed
  366. def _decompress(zip, compressed):
  367. """
  368. Decompress the plaintext based on the algorithm supplied
  369. Args:
  370. zip (str): Compression Algorithm
  371. plaintext (bytes): plaintext to decompress
  372. Returns:
  373. (bytes): Compressed plaintext
  374. """
  375. if zip not in ZIPS.SUPPORTED:
  376. raise NotImplementedError(f"ZIP {zip} is not supported!")
  377. if zip is None:
  378. decompressed = compressed
  379. elif zip == ZIPS.DEF:
  380. # If, during decompression, there is more data than expected, the
  381. # decompression halts and raise an error. This addresses CVE-2024-33664
  382. decompressor = zlib.decompressobj()
  383. decompressed = decompressor.decompress(compressed, max_length=JWE_SIZE_LIMIT)
  384. if decompressor.unconsumed_tail:
  385. raise JWEError(f"Decompressed JWE string exceeds {JWE_SIZE_LIMIT} bytes")
  386. else:
  387. raise NotImplementedError(f"ZIP {zip} is not implemented!")
  388. return decompressed
  389. def _get_cek(enc, alg, key):
  390. """
  391. Get the content encryption key
  392. Args:
  393. enc (str): Encryption algorithm
  394. alg (str): kwy wrap/negotiation algorithm
  395. key (Key): Key provided to encryption method
  396. Return:
  397. (bytes, bytes): Tuple of (cek bytes and wrapped cek)
  398. """
  399. if alg == ALGORITHMS.DIR:
  400. cek, wrapped_cek = _get_direct_key_wrap_cek(key)
  401. else:
  402. cek, wrapped_cek = _get_key_wrap_cek(enc, key)
  403. return cek, wrapped_cek
  404. def _get_direct_key_wrap_cek(key):
  405. """
  406. Get the cek and wrapped cek from the encryption key direct
  407. Args:
  408. key (Key): Key provided to encryption method
  409. Return:
  410. (Key, bytes): Tuple of (cek Key object and wrapped cek)
  411. """
  412. # Get the JWK data to determine how to derive the cek
  413. jwk_data = key.to_dict()
  414. if jwk_data["kty"] == "oct":
  415. # Get the last half of an octal key as the cek
  416. cek_bytes = _get_key_bytes_from_key(key)
  417. wrapped_cek = b""
  418. else:
  419. raise NotImplementedError("JWK type {} not supported!".format(jwk_data["kty"]))
  420. return cek_bytes, wrapped_cek
  421. def _get_key_bytes_from_key(key):
  422. """
  423. Get the raw key bytes from a Key object
  424. Args:
  425. key (Key): Key from which to extract the raw key bytes
  426. Returns:
  427. (bytes) key data
  428. """
  429. jwk_data = key.to_dict()
  430. encoded_key = jwk_data["k"]
  431. cek_bytes = base64url_decode(encoded_key)
  432. return cek_bytes
  433. def _get_key_wrap_cek(enc, key):
  434. """_get_rsa_key_wrap_cek
  435. Get the content encryption key for RSA key wrap
  436. Args:
  437. enc (str): Encryption algorithm
  438. key (Key): Key provided to encryption method
  439. Returns:
  440. (Key, bytes): Tuple of (cek Key object and wrapped cek)
  441. """
  442. cek_bytes = _get_random_cek_bytes_for_enc(enc)
  443. wrapped_cek = key.wrap_key(cek_bytes)
  444. return cek_bytes, wrapped_cek
  445. def _get_random_cek_bytes_for_enc(enc):
  446. """
  447. Get the random cek bytes based on the encryption algorithm
  448. Args:
  449. enc (str): Encryption algorithm
  450. Returns:
  451. (bytes) random bytes for cek key
  452. """
  453. if enc == ALGORITHMS.A128GCM:
  454. num_bits = 128
  455. elif enc == ALGORITHMS.A192GCM:
  456. num_bits = 192
  457. elif enc in (ALGORITHMS.A128CBC_HS256, ALGORITHMS.A256GCM):
  458. num_bits = 256
  459. elif enc == ALGORITHMS.A192CBC_HS384:
  460. num_bits = 384
  461. elif enc == ALGORITHMS.A256CBC_HS512:
  462. num_bits = 512
  463. else:
  464. raise NotImplementedError(f"{enc} not supported")
  465. cek_bytes = get_random_bytes(num_bits // 8)
  466. return cek_bytes
  467. def _auth_tag(ciphertext, iv, aad, mac_key, tag_length):
  468. """
  469. Get ann auth tag from the provided data
  470. Args:
  471. ciphertext (bytes): Encrypted value
  472. iv (bytes): Initialization vector
  473. aad (bytes): Additional Authenticated Data
  474. mac_key (bytes): Key to use in generating the MAC
  475. tag_length (int): How log the tag should be
  476. Returns:
  477. (bytes) Auth tag
  478. """
  479. al = _big_endian(len(aad) * 8)
  480. auth_tag_input = aad + iv + ciphertext + al
  481. signature = mac_key.sign(auth_tag_input)
  482. auth_tag = signature[0:tag_length]
  483. return auth_tag
  484. def _jwe_compact_serialize(encoded_header, encrypted_cek, iv, cipher_text, auth_tag):
  485. """
  486. Generate a compact serialized JWE
  487. Args:
  488. encoded_header (bytes): Base64 URL Encoded JWE header JSON
  489. encrypted_cek (bytes): Encrypted content encryption key (cek)
  490. iv (bytes): Initialization vector (IV)
  491. cipher_text (bytes): Cipher text
  492. auth_tag (bytes): JWE Auth Tag
  493. Returns:
  494. (str): JWE compact serialized string
  495. """
  496. cipher_text = ensure_binary(cipher_text)
  497. encoded_encrypted_cek = base64url_encode(encrypted_cek)
  498. encoded_iv = base64url_encode(iv)
  499. encoded_cipher_text = base64url_encode(cipher_text)
  500. encoded_auth_tag = base64url_encode(auth_tag)
  501. return (
  502. encoded_header
  503. + b"."
  504. + encoded_encrypted_cek
  505. + b"."
  506. + encoded_iv
  507. + b"."
  508. + encoded_cipher_text
  509. + b"."
  510. + encoded_auth_tag
  511. )