-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug] Plugin chooses wrong decryption key #1862
Comments
Addition: Just had the same bug happen with an EPUB file, so the bug is not limited to just PDFs. |
Okay, due to the questionable legality of tools like this I do not want to make a PR, but here's what I think is going wrong: (Btw., does anyone know what the heck all that data before that NULL byte is?) However, if you take that encrypted book key, and decrypt it with a random, non-matching key for a different adobe account, it will lead to 128 random bytes instead due to the wrong key. If the 17th-to-last byte of these 128 random bytes happens to be a NULL byte, too - which probably has a chance of 1/256 - the plugin assumes it found the correct book key and then continues to decrypt the book with the wrong key. Of course, the more "wrong" user keys you have imported into the plugin settings, the higher is the chance of running into this issue. Without knowing what all the data before the book key is, there's not really a way to completely prevent that. However, I noticed that that data seems to always start with "00 02" for EPUBs and with "02" for PDFs. This means that by modifying the code to not only check for that NULL byte in the 17th-to-last place but also checking for the one (or two) bytes in the beginning, you decrease the chance for issues from 1/256 to 1/(256^2) = 1/65535 for PDF files, or down to 1/(256^3) = 1/16777216 for EPUB files, which should be way more reliable. EDIT: |
Those extra bytes are the result of RSAES-PKCS1-v1_5 encryption. The decrypted string should be start with 00 02, followed by at least eight randomly chosen non-zero bytes, followed by a 00 byte, and then the 16 byte key. A more rigorous verification could look at all of those bytes prior to the key. |
Yeah, the decrypted string starts with Verifying these With "more rigorous" you mean verifying that none of the junk bytes are zero? |
That is what I meant. Perhaps that check could be reinstated and only be relaxed if none of the possible results pass inspection. |
CheckList
The Title
and TheLog Title
are setted correctly.my environment
.the log
.[Bug] Plugin chooses wrong decryption key for PDF
I'm adding a PDF to Calibre. The modifications in #1689 are included so PDFs even work.
I have multiple different ADE keys imported into the plugin, with the "correct" one for this PDF being the last one in that list.
However, the plugin seems to think that the very first key it tries to use is correct. Probably, just by chance, decryption with that wrong key leads to a file that at least looks like it could be a valid PDF. Though, actually, it results in a PDF with all pages empty.
When I delete all the other keys from the plugin settings so only the correct key is left, the plugin correctly decrypts the book.
My Environment
Calibre: 5.28
Kindle: N/A
DeDRM: 7.2.1 plus PR #1689
Log
Log Title
(That "key1" that it tried to use is definitely, 100%, the wrong key for that book).
The text was updated successfully, but these errors were encountered: