-
Notifications
You must be signed in to change notification settings - Fork 97
Description
Hello,
I regularly get the following error message when using DavMail.
davmail.exchange.auth.O365Token - refresh token failed javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
I made sure several times that my key does not change contrary to what the error message suggests.
It seems to me that the code for encryption and decryption of the refresh token tires to derive a salt for the cryptography via the hostname.
However, the getCanonicalHostName()
function seems to prioritize the reverse lookup of the IP address over the already-known hostname (from getLocalHost()
).
As an example, when I execute the following code, I get one.one.one.one
as the hostname instead of the expected myorg
.
System.out.println(InetAddress.getByAddress("myorg", new byte[]{1, 1, 1, 1}).getCanonicalHostName());
I suspect, that my decryption issues happen, when I change connect to a different network where my IP address (for some unknown reason) has a canonical hostname registered that then gets used instead of my local hostname.
I checked that changing the salt results in the same error message as using the wrong password.
Maybe this was also the underlying issue of #234 . (The old issue maybe just resolved because the network setting changed.)
I think, the best solution would be to generate a random salt value and store it along side the token (maybe just in the beginning of the Base64 encoded ciphertext).
But, I guess, just always using the fallback davmailgateway!&
or using getHostName()
(this does not do a reverse lookup but just takes the local hostname; but was abandoned in 4ddaea0) might also be viable options.
I do not know how to change this without breaking existing setups but maybe one could introduce another prefix to the stored refreshToken (similar to the existing {AES}
) to discriminate different methods of generating the salt.
I am using the newest DavMail version (6.3.0) from Flathub if that makes any difference.