44import com .google .gson .JsonObject ;
55import com .google .gson .JsonParser ;
66import org .cryptomator .jose .DecryptionAlg ;
7+ import org .cryptomator .jose .IntegratedDecryptionAlg ;
78import org .cryptomator .jose .JoseDecryptException ;
89import org .cryptomator .jose .enc .DecryptCiphertextException ;
910import org .cryptomator .jose .util .Ascii ;
@@ -75,15 +76,26 @@ private DecryptedJWE decrypt(Optional<String> kid, DecryptionAlg... algs) throws
7576 } catch (IllegalArgumentException e ) {
7677 throw new JoseDecryptException ("Malformed JWE: protected header and aad must be US-ASCII" , e );
7778 }
78- var decodedIv = base64url .decode (iv );
79- var decodedCiphertext = base64url .decode (ciphertext );
80- var decodedTag = base64url .decode (tag );
79+ final byte [] decodedIv , decodedCiphertext , decodedTag ;
80+ try {
81+ decodedIv = base64url .decode (iv );
82+ decodedCiphertext = base64url .decode (ciphertext );
83+ decodedTag = base64url .decode (tag );
84+ } catch (IllegalArgumentException e ) {
85+ throw new JoseDecryptException ("Malformed JWE: iv, ciphertext and tag must be base64url" , e ); // shared by all recipients, so not worth retrying
86+ }
8187
8288 var candidates = kid .isPresent () ? recipientsWithKid (kid .get (), sharedHeader ) : recipients .asList ();
8389 var failure = new JoseDecryptException (kid .isPresent () ? "Recipient '" + kid .get () + "' could not be decrypted." : "No matching recipient found for decryption." );
8490 for (var recipient : candidates ) {
8591 var recipientJson = recipient .getAsJsonObject ();
86- var encryptedKey = base64url .decode (recipientJson .get ("encrypted_key" ).getAsString ());
92+ final byte [] encryptedKey ;
93+ try {
94+ encryptedKey = base64url .decode (recipientJson .get ("encrypted_key" ).getAsString ());
95+ } catch (IllegalArgumentException e ) {
96+ failure .addSuppressed (new JoseDecryptException ("Malformed encrypted_key: not base64url" , e ));
97+ continue ;
98+ }
8799 var perRecipientUnprotectedHeader = recipientJson .has ("header" )
88100 ? recipientJson .get ("header" ).getAsJsonObject ()
89101 : new JsonObject ();
@@ -99,13 +111,20 @@ private DecryptedJWE decrypt(Optional<String> kid, DecryptionAlg... algs) throws
99111 if (!algValue .equals (alg .name ())) {
100112 continue ;
101113 }
114+ if (alg instanceof IntegratedDecryptionAlg && !parsedProtectedHeader .has ("alg" )) {
115+ // draft-ietf-jose-hpke-encrypt §5: for Integrated Encryption the protected header MUST contain alg (it is part of the HPKE aad)
116+ failure .addSuppressed (new JoseDecryptException ("alg must be in the protected header for Integrated Encryption" ));
117+ continue ;
118+ }
102119 try {
103120 var payload = alg .decrypt (combinedHeader , parts );
104121 return new DecryptedJWE (new String (payload , StandardCharsets .UTF_8 ), parsedProtectedHeader , JsonHelper .union (unprotectedHeader , perRecipientUnprotectedHeader ), aad );
105122 } catch (DecryptCiphertextException e ) {
106123 throw e ; // the key matched but the content is corrupt; other recipients share the same ciphertext and would fail identically
107124 } catch (JoseDecryptException e ) {
108125 failure .addSuppressed (e ); // this recipient/alg does not apply (wrong key, mode mismatch, malformed header); try the next candidate
126+ } catch (IllegalArgumentException | IllegalStateException e ) {
127+ failure .addSuppressed (new JoseDecryptException ("Malformed recipient: " + e .getMessage (), e )); // defense in depth: an alg tripping over a malformed header must not abort the loop
109128 }
110129 }
111130 }
0 commit comments