|
12 | 12 |
|
13 | 13 | import logging |
14 | 14 | import plistlib |
| 15 | +import re |
15 | 16 | import subprocess |
16 | 17 | from pathlib import Path |
17 | 18 | from typing import IO |
|
26 | 27 | _DEFAULT_SEARCH_PATH = Path.home() / "Library" / "com.apple.icloud.searchpartyd" |
27 | 28 |
|
28 | 29 |
|
| 30 | +def _parse_beaconstore_key_from_string_output(output: str) -> bytes: |
| 31 | + if '"acct"<blob>="BeaconStoreKey"' not in output: |
| 32 | + raise ValueError |
| 33 | + m = re.search(r'"gena"<blob>=0x([0-9A-Fa-f]+)', output) |
| 34 | + if not m: |
| 35 | + raise ValueError |
| 36 | + return bytes.fromhex(m.group(1)) |
| 37 | + |
| 38 | + |
| 39 | +def _parse_beaconstore_key_from_hex_output(output: str) -> bytes: |
| 40 | + if not output: |
| 41 | + msg = "Empty output from security -w" |
| 42 | + raise ValueError(msg) |
| 43 | + return bytes.fromhex(output) |
| 44 | + |
| 45 | + |
29 | 46 | # consider switching to this library https://github.com/microsoft/keyper |
30 | 47 | # once they publish a version of it that includes my MR with the changes to make it compatible |
31 | 48 | # with keys that are non-utf-8 encoded (like the BeaconStore one) |
32 | 49 | # if I contribute this, properly escape the label argument here... |
33 | 50 | def _get_beaconstore_key() -> bytes: |
34 | | - """Get the decryption key for BeaconStore using the system password prompt window.""" |
35 | | - # This thing will pop up 2 Password Input windows... |
36 | | - key_in_hex = subprocess.getoutput("/usr/bin/security find-generic-password -l 'BeaconStore' -w") # noqa: S605 |
37 | | - return bytes.fromhex(key_in_hex) |
| 51 | + try: |
| 52 | + # This thing will pop up 2 Password Input windows... |
| 53 | + key_in_hex = subprocess.getoutput( # noqa: S605 |
| 54 | + "/usr/bin/security find-generic-password -l 'BeaconStore' -w" |
| 55 | + ) |
| 56 | + return _parse_beaconstore_key_from_hex_output(key_in_hex) |
| 57 | + except (ValueError, subprocess.SubprocessError): |
| 58 | + output = subprocess.getoutput("/usr/bin/security find-generic-password -l 'BeaconStore'") # noqa: S605 |
| 59 | + return _parse_beaconstore_key_from_string_output(output) |
38 | 60 |
|
39 | 61 |
|
40 | 62 | def _get_accessory_name( |
|
0 commit comments