|
| 1 | +# Copyright (c) 2025 Tulir Asokan |
| 2 | +# |
| 3 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | +import pytest |
| 7 | + |
| 8 | +from ...types.event.type import EventType |
| 9 | +from .key import Key, KeyMetadata |
| 10 | +from .types import EncryptedAccountDataEventContent |
| 11 | + |
| 12 | +KEY1_CROSS_SIGNING_MASTER_KEY = """{ |
| 13 | + "encrypted": { |
| 14 | + "gEJqbfSEMnP5JXXcukpXEX1l0aI3MDs0": { |
| 15 | + "iv": "BpKP9nQJTE9jrsAssoxPqQ==", |
| 16 | + "ciphertext": "fNRiiiidezjerTgV+G6pUtmeF3izzj5re/mVvY0hO2kM6kYGrxLuIu2ej80=", |
| 17 | + "mac": "/gWGDGMyOLmbJp+aoSLh5JxCs0AdS6nAhjzpe+9G2Q0=" |
| 18 | + } |
| 19 | + } |
| 20 | +}""" |
| 21 | + |
| 22 | +KEY1_CROSS_SIGNING_MASTER_KEY_DECRYPTED = bytes( |
| 23 | + [ |
| 24 | + 0x68, |
| 25 | + 0xF9, |
| 26 | + 0x7F, |
| 27 | + 0xD1, |
| 28 | + 0x92, |
| 29 | + 0x2E, |
| 30 | + 0xEC, |
| 31 | + 0xF6, |
| 32 | + 0xB8, |
| 33 | + 0x2B, |
| 34 | + 0xB8, |
| 35 | + 0x90, |
| 36 | + 0xD2, |
| 37 | + 0x4D, |
| 38 | + 0x06, |
| 39 | + 0x52, |
| 40 | + 0x98, |
| 41 | + 0x4E, |
| 42 | + 0x7A, |
| 43 | + 0x1D, |
| 44 | + 0x70, |
| 45 | + 0x3B, |
| 46 | + 0x9E, |
| 47 | + 0x86, |
| 48 | + 0x7B, |
| 49 | + 0x7E, |
| 50 | + 0xBA, |
| 51 | + 0xF7, |
| 52 | + 0xFE, |
| 53 | + 0xB9, |
| 54 | + 0x5B, |
| 55 | + 0x6F, |
| 56 | + ] |
| 57 | +) |
| 58 | + |
| 59 | +KEY1_META = """{ |
| 60 | + "algorithm": "m.secret_storage.v1.aes-hmac-sha2", |
| 61 | + "passphrase": { |
| 62 | + "algorithm": "m.pbkdf2", |
| 63 | + "iterations": 500000, |
| 64 | + "salt": "y863BOoqOadgDp8S3FtHXikDJEalsQ7d" |
| 65 | + }, |
| 66 | + "iv": "xxkTK0L4UzxgAFkQ6XPwsw", |
| 67 | + "mac": "MEhooO0ZhFJNxUhvRMSxBnJfL20wkLgle3ocY0ee/eA" |
| 68 | +}""" |
| 69 | +KEY1_ID = "gEJqbfSEMnP5JXXcukpXEX1l0aI3MDs0" |
| 70 | +KEY1_RECOVERY_KEY = "EsTE s92N EtaX s2h6 VQYF 9Kao tHYL mkyL GKMh isZb KJ4E tvoC" |
| 71 | +KEY1_PASSPHRASE = "correct horse battery staple" |
| 72 | + |
| 73 | +KEY2_META = """{ |
| 74 | + "algorithm": "m.secret_storage.v1.aes-hmac-sha2", |
| 75 | + "iv": "O0BOvTqiIAYjC+RMcyHfWw==", |
| 76 | + "mac": "7k6OruQlWg0UmQjxGZ0ad4Q6DdwkgnoI7G6X3IjBYtI=" |
| 77 | +}""" |
| 78 | +KEY2_ID = "NVe5vK6lZS9gEMQLJw0yqkzmE5Mr7dLv" |
| 79 | +KEY2_RECOVERY_KEY = "EsUC xSxt XJgQ dz19 8WBZ rHdE GZo7 ybsn EFmG Y5HY MDAG GNWe" |
| 80 | + |
| 81 | +KEY2_META_BROKEN_IV = """{ |
| 82 | + "algorithm": "m.secret_storage.v1.aes-hmac-sha2", |
| 83 | + "iv": "O0BOvTqiIAYjC+RMcyHfWwMeowMeowMeow", |
| 84 | + "mac": "7k6OruQlWg0UmQjxGZ0ad4Q6DdwkgnoI7G6X3IjBYtI=" |
| 85 | +}""" |
| 86 | + |
| 87 | +KEY2_META_BROKEN_MAC = """{ |
| 88 | + "algorithm": "m.secret_storage.v1.aes-hmac-sha2", |
| 89 | + "iv": "O0BOvTqiIAYjC+RMcyHfWw==", |
| 90 | + "mac": "7k6OruQlWg0UmQjxGZ0ad4Q6DdwkgnoI7G6X3IjBYtIMeowMeowMeow" |
| 91 | +}""" |
| 92 | + |
| 93 | + |
| 94 | +def get_key_meta(meta: str) -> KeyMetadata: |
| 95 | + return KeyMetadata.parse_json(meta) |
| 96 | + |
| 97 | + |
| 98 | +def get_key1() -> Key: |
| 99 | + return get_key_meta(KEY1_META).verify_recovery_key(KEY1_ID, KEY1_RECOVERY_KEY) |
| 100 | + |
| 101 | + |
| 102 | +def get_key2() -> Key: |
| 103 | + return get_key_meta(KEY2_META).verify_recovery_key(KEY2_ID, KEY2_RECOVERY_KEY) |
| 104 | + |
| 105 | + |
| 106 | +def get_encrypted_master_key() -> EncryptedAccountDataEventContent: |
| 107 | + return EncryptedAccountDataEventContent.parse_json(KEY1_CROSS_SIGNING_MASTER_KEY) |
| 108 | + |
| 109 | + |
| 110 | +def test_decrypt_success() -> None: |
| 111 | + key = get_key1() |
| 112 | + emk = get_encrypted_master_key() |
| 113 | + assert ( |
| 114 | + emk.decrypt(EventType.CROSS_SIGNING_MASTER, key) == KEY1_CROSS_SIGNING_MASTER_KEY_DECRYPTED |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +def test_decrypt_fail_wrong_key() -> None: |
| 119 | + key = get_key2() |
| 120 | + emk = get_encrypted_master_key() |
| 121 | + with pytest.raises(ValueError): |
| 122 | + emk.decrypt(EventType.CROSS_SIGNING_MASTER, key) |
| 123 | + |
| 124 | + |
| 125 | +def test_decrypt_fail_fake_key() -> None: |
| 126 | + key = get_key2() |
| 127 | + key.id = KEY1_ID |
| 128 | + emk = get_encrypted_master_key() |
| 129 | + with pytest.raises(ValueError): |
| 130 | + emk.decrypt(EventType.CROSS_SIGNING_MASTER, key) |
| 131 | + |
| 132 | + |
| 133 | +def test_decrypt_fail_wrong_type() -> None: |
| 134 | + key = get_key1() |
| 135 | + emk = get_encrypted_master_key() |
| 136 | + with pytest.raises(ValueError): |
| 137 | + emk.decrypt(EventType.CROSS_SIGNING_SELF_SIGNING, key) |
| 138 | + |
| 139 | + |
| 140 | +def test_encrypt_roundtrip() -> None: |
| 141 | + key = get_key1() |
| 142 | + data = bytes([0xDE, 0xAD, 0xBE, 0xEF]) |
| 143 | + ciphertext = key.encrypt("net.maunium.data", data) |
| 144 | + plaintext = key.decrypt("net.maunium.data", ciphertext) |
| 145 | + assert plaintext == data |
| 146 | + |
| 147 | + |
| 148 | +def test_verify_recovery_key_correct() -> None: |
| 149 | + meta = get_key_meta(KEY1_META) |
| 150 | + key = meta.verify_recovery_key(KEY1_ID, KEY1_RECOVERY_KEY) |
| 151 | + assert key.recovery_key == KEY1_RECOVERY_KEY |
| 152 | + |
| 153 | + |
| 154 | +def test_verify_recovery_key_correct2() -> None: |
| 155 | + meta = get_key_meta(KEY2_META) |
| 156 | + key = meta.verify_recovery_key(KEY2_ID, KEY2_RECOVERY_KEY) |
| 157 | + assert key.recovery_key == KEY2_RECOVERY_KEY |
| 158 | + |
| 159 | + |
| 160 | +def test_verify_recovery_key_invalid() -> None: |
| 161 | + meta = get_key_meta(KEY1_META) |
| 162 | + with pytest.raises(ValueError): |
| 163 | + meta.verify_recovery_key(KEY1_ID, "foo") |
| 164 | + |
| 165 | + |
| 166 | +def test_verify_recovery_key_incorrect() -> None: |
| 167 | + meta = get_key_meta(KEY1_META) |
| 168 | + with pytest.raises(ValueError): |
| 169 | + meta.verify_recovery_key(KEY2_ID, KEY2_RECOVERY_KEY) |
| 170 | + |
| 171 | + |
| 172 | +def test_verify_recovery_key_broken_iv() -> None: |
| 173 | + meta = get_key_meta(KEY2_META_BROKEN_IV) |
| 174 | + with pytest.raises(ValueError): |
| 175 | + meta.verify_recovery_key(KEY2_ID, KEY2_RECOVERY_KEY) |
| 176 | + |
| 177 | + |
| 178 | +def test_verify_recovery_key_broken_mac() -> None: |
| 179 | + meta = get_key_meta(KEY2_META_BROKEN_MAC) |
| 180 | + with pytest.raises(ValueError): |
| 181 | + meta.verify_recovery_key(KEY2_ID, KEY2_RECOVERY_KEY) |
| 182 | + |
| 183 | + |
| 184 | +def test_verify_passphrase_correct() -> None: |
| 185 | + meta = get_key_meta(KEY1_META) |
| 186 | + key = meta.verify_passphrase(KEY1_ID, KEY1_PASSPHRASE) |
| 187 | + assert key.recovery_key == KEY1_RECOVERY_KEY |
| 188 | + |
| 189 | + |
| 190 | +def test_verify_passphrase_incorrect() -> None: |
| 191 | + meta = get_key_meta(KEY1_META) |
| 192 | + with pytest.raises(ValueError): |
| 193 | + meta.verify_passphrase(KEY1_ID, "incorrect horse battery staple") |
| 194 | + |
| 195 | + |
| 196 | +def test_verify_passphrase_notset() -> None: |
| 197 | + meta = get_key_meta(KEY2_META) |
| 198 | + with pytest.raises(ValueError): |
| 199 | + meta.verify_passphrase(KEY2_ID, "hmm") |
0 commit comments