Skip to content

Commit

Permalink
FIX: decrypting voucher
Browse files Browse the repository at this point in the history
In some cases, decrypting of voucher are incomplete. The last bytes are
missing. So a conversion from string to dict with json fails.

In these situations, only the key and iv from the voucher are returned.
  • Loading branch information
mkb79 committed Oct 25, 2020
1 parent b21bdd0 commit 6259776
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/audible/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__description__ = "A(Sync) Interface for internal Audible API written in " \
"pure Python."
__url__ = "https://github.com/mkb79/audible"
__version__ = "0.4.2"
__version__ = "0.4.3"
__author__ = "mkb79"
__author_email__ = "[email protected]"
__license__ = "AGPL"
Expand Down
14 changes: 11 additions & 3 deletions src/audible/aescipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import os
import pathlib
import re
import struct
from hashlib import sha256
from typing import Dict, Tuple
Expand Down Expand Up @@ -241,8 +242,15 @@ def _decrypt_voucher(device_serial_number, customer_id, device_type, asin, vouch
iv = digest[16:]

# decrypt "voucher" using AES in CBC mode with no padding
plaintext = aes_cbc_decrypt(key, iv, voucher).rstrip("\x00")
return json.loads(plaintext)
b64d_voucher = base64.b64decode(voucher)
plaintext = aes_cbc_decrypt(key, iv, b64d_voucher).rstrip("\x00")

try:
return json.loads(plaintext)
except json.JSONDecodeError:
fmt = r"^{\"key\":\"(?P<key>.*?)\",\"iv\":\"(?P<iv>.*?)\","
match = re.match(fmt, plaintext)
return match.groupdict()


def decrypt_voucher_from_licenserequest(auth, license_response):
Expand Down Expand Up @@ -271,7 +279,7 @@ def decrypt_voucher_from_licenserequest(auth, license_response):

# book specific data
asin = license_response["content_license"]["asin"]
encrypted_voucher = base64.b64decode(license_response["content_license"]["license_response"])
encrypted_voucher = license_response["content_license"]["license_response"]

return _decrypt_voucher(
device_serial_number=device_serial_number,
Expand Down

0 comments on commit 6259776

Please sign in to comment.