test_e2e_keys.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. # Copyright 2016 OpenMarket Ltd
  2. # Copyright 2019 New Vector Ltd
  3. # Copyright 2019 The Matrix.org Foundation C.I.C.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from unittest import mock
  17. from signedjson import key as key, sign as sign
  18. from twisted.internet import defer
  19. from synapse.api.constants import RoomEncryptionAlgorithms
  20. from synapse.api.errors import Codes, SynapseError
  21. from tests import unittest
  22. class E2eKeysHandlerTestCase(unittest.HomeserverTestCase):
  23. def make_homeserver(self, reactor, clock):
  24. return self.setup_test_homeserver(federation_client=mock.Mock())
  25. def prepare(self, reactor, clock, hs):
  26. self.handler = hs.get_e2e_keys_handler()
  27. self.store = self.hs.get_datastore()
  28. def test_query_local_devices_no_devices(self):
  29. """If the user has no devices, we expect an empty list."""
  30. local_user = "@boris:" + self.hs.hostname
  31. res = self.get_success(self.handler.query_local_devices({local_user: None}))
  32. self.assertDictEqual(res, {local_user: {}})
  33. def test_reupload_one_time_keys(self):
  34. """we should be able to re-upload the same keys"""
  35. local_user = "@boris:" + self.hs.hostname
  36. device_id = "xyz"
  37. keys = {
  38. "alg1:k1": "key1",
  39. "alg2:k2": {"key": "key2", "signatures": {"k1": "sig1"}},
  40. "alg2:k3": {"key": "key3"},
  41. }
  42. # Note that "signed_curve25519" is always returned in key count responses. This is necessary until
  43. # https://github.com/matrix-org/matrix-doc/issues/3298 is fixed.
  44. res = self.get_success(
  45. self.handler.upload_keys_for_user(
  46. local_user, device_id, {"one_time_keys": keys}
  47. )
  48. )
  49. self.assertDictEqual(
  50. res, {"one_time_key_counts": {"alg1": 1, "alg2": 2, "signed_curve25519": 0}}
  51. )
  52. # we should be able to change the signature without a problem
  53. keys["alg2:k2"]["signatures"]["k1"] = "sig2"
  54. res = self.get_success(
  55. self.handler.upload_keys_for_user(
  56. local_user, device_id, {"one_time_keys": keys}
  57. )
  58. )
  59. self.assertDictEqual(
  60. res, {"one_time_key_counts": {"alg1": 1, "alg2": 2, "signed_curve25519": 0}}
  61. )
  62. def test_change_one_time_keys(self):
  63. """attempts to change one-time-keys should be rejected"""
  64. local_user = "@boris:" + self.hs.hostname
  65. device_id = "xyz"
  66. keys = {
  67. "alg1:k1": "key1",
  68. "alg2:k2": {"key": "key2", "signatures": {"k1": "sig1"}},
  69. "alg2:k3": {"key": "key3"},
  70. }
  71. res = self.get_success(
  72. self.handler.upload_keys_for_user(
  73. local_user, device_id, {"one_time_keys": keys}
  74. )
  75. )
  76. self.assertDictEqual(
  77. res, {"one_time_key_counts": {"alg1": 1, "alg2": 2, "signed_curve25519": 0}}
  78. )
  79. # Error when changing string key
  80. self.get_failure(
  81. self.handler.upload_keys_for_user(
  82. local_user, device_id, {"one_time_keys": {"alg1:k1": "key2"}}
  83. ),
  84. SynapseError,
  85. )
  86. # Error when replacing dict key with string
  87. self.get_failure(
  88. self.handler.upload_keys_for_user(
  89. local_user, device_id, {"one_time_keys": {"alg2:k3": "key2"}}
  90. ),
  91. SynapseError,
  92. )
  93. # Error when replacing string key with dict
  94. self.get_failure(
  95. self.handler.upload_keys_for_user(
  96. local_user,
  97. device_id,
  98. {"one_time_keys": {"alg1:k1": {"key": "key"}}},
  99. ),
  100. SynapseError,
  101. )
  102. # Error when replacing dict key
  103. self.get_failure(
  104. self.handler.upload_keys_for_user(
  105. local_user,
  106. device_id,
  107. {
  108. "one_time_keys": {
  109. "alg2:k2": {"key": "key3", "signatures": {"k1": "sig1"}}
  110. }
  111. },
  112. ),
  113. SynapseError,
  114. )
  115. def test_claim_one_time_key(self):
  116. local_user = "@boris:" + self.hs.hostname
  117. device_id = "xyz"
  118. keys = {"alg1:k1": "key1"}
  119. res = self.get_success(
  120. self.handler.upload_keys_for_user(
  121. local_user, device_id, {"one_time_keys": keys}
  122. )
  123. )
  124. self.assertDictEqual(
  125. res, {"one_time_key_counts": {"alg1": 1, "signed_curve25519": 0}}
  126. )
  127. res2 = self.get_success(
  128. self.handler.claim_one_time_keys(
  129. {"one_time_keys": {local_user: {device_id: "alg1"}}}, timeout=None
  130. )
  131. )
  132. self.assertEqual(
  133. res2,
  134. {
  135. "failures": {},
  136. "one_time_keys": {local_user: {device_id: {"alg1:k1": "key1"}}},
  137. },
  138. )
  139. def test_fallback_key(self):
  140. local_user = "@boris:" + self.hs.hostname
  141. device_id = "xyz"
  142. fallback_key = {"alg1:k1": "key1"}
  143. fallback_key2 = {"alg1:k2": "key2"}
  144. otk = {"alg1:k2": "key2"}
  145. # we shouldn't have any unused fallback keys yet
  146. res = self.get_success(
  147. self.store.get_e2e_unused_fallback_key_types(local_user, device_id)
  148. )
  149. self.assertEqual(res, [])
  150. self.get_success(
  151. self.handler.upload_keys_for_user(
  152. local_user,
  153. device_id,
  154. {"org.matrix.msc2732.fallback_keys": fallback_key},
  155. )
  156. )
  157. # we should now have an unused alg1 key
  158. res = self.get_success(
  159. self.store.get_e2e_unused_fallback_key_types(local_user, device_id)
  160. )
  161. self.assertEqual(res, ["alg1"])
  162. # claiming an OTK when no OTKs are available should return the fallback
  163. # key
  164. res = self.get_success(
  165. self.handler.claim_one_time_keys(
  166. {"one_time_keys": {local_user: {device_id: "alg1"}}}, timeout=None
  167. )
  168. )
  169. self.assertEqual(
  170. res,
  171. {"failures": {}, "one_time_keys": {local_user: {device_id: fallback_key}}},
  172. )
  173. # we shouldn't have any unused fallback keys again
  174. res = self.get_success(
  175. self.store.get_e2e_unused_fallback_key_types(local_user, device_id)
  176. )
  177. self.assertEqual(res, [])
  178. # claiming an OTK again should return the same fallback key
  179. res = self.get_success(
  180. self.handler.claim_one_time_keys(
  181. {"one_time_keys": {local_user: {device_id: "alg1"}}}, timeout=None
  182. )
  183. )
  184. self.assertEqual(
  185. res,
  186. {"failures": {}, "one_time_keys": {local_user: {device_id: fallback_key}}},
  187. )
  188. # re-uploading the same fallback key should still result in no unused fallback
  189. # keys
  190. self.get_success(
  191. self.handler.upload_keys_for_user(
  192. local_user,
  193. device_id,
  194. {"org.matrix.msc2732.fallback_keys": fallback_key},
  195. )
  196. )
  197. res = self.get_success(
  198. self.store.get_e2e_unused_fallback_key_types(local_user, device_id)
  199. )
  200. self.assertEqual(res, [])
  201. # uploading a new fallback key should result in an unused fallback key
  202. self.get_success(
  203. self.handler.upload_keys_for_user(
  204. local_user,
  205. device_id,
  206. {"org.matrix.msc2732.fallback_keys": fallback_key2},
  207. )
  208. )
  209. res = self.get_success(
  210. self.store.get_e2e_unused_fallback_key_types(local_user, device_id)
  211. )
  212. self.assertEqual(res, ["alg1"])
  213. # if the user uploads a one-time key, the next claim should fetch the
  214. # one-time key, and then go back to the fallback
  215. self.get_success(
  216. self.handler.upload_keys_for_user(
  217. local_user, device_id, {"one_time_keys": otk}
  218. )
  219. )
  220. res = self.get_success(
  221. self.handler.claim_one_time_keys(
  222. {"one_time_keys": {local_user: {device_id: "alg1"}}}, timeout=None
  223. )
  224. )
  225. self.assertEqual(
  226. res,
  227. {"failures": {}, "one_time_keys": {local_user: {device_id: otk}}},
  228. )
  229. res = self.get_success(
  230. self.handler.claim_one_time_keys(
  231. {"one_time_keys": {local_user: {device_id: "alg1"}}}, timeout=None
  232. )
  233. )
  234. self.assertEqual(
  235. res,
  236. {"failures": {}, "one_time_keys": {local_user: {device_id: fallback_key2}}},
  237. )
  238. def test_replace_master_key(self):
  239. """uploading a new signing key should make the old signing key unavailable"""
  240. local_user = "@boris:" + self.hs.hostname
  241. keys1 = {
  242. "master_key": {
  243. # private key: 2lonYOM6xYKdEsO+6KrC766xBcHnYnim1x/4LFGF8B0
  244. "user_id": local_user,
  245. "usage": ["master"],
  246. "keys": {
  247. "ed25519:nqOvzeuGWT/sRx3h7+MHoInYj3Uk2LD/unI9kDYcHwk": "nqOvzeuGWT/sRx3h7+MHoInYj3Uk2LD/unI9kDYcHwk"
  248. },
  249. }
  250. }
  251. self.get_success(self.handler.upload_signing_keys_for_user(local_user, keys1))
  252. keys2 = {
  253. "master_key": {
  254. # private key: 4TL4AjRYwDVwD3pqQzcor+ez/euOB1/q78aTJ+czDNs
  255. "user_id": local_user,
  256. "usage": ["master"],
  257. "keys": {
  258. "ed25519:Hq6gL+utB4ET+UvD5ci0kgAwsX6qP/zvf8v6OInU5iw": "Hq6gL+utB4ET+UvD5ci0kgAwsX6qP/zvf8v6OInU5iw"
  259. },
  260. }
  261. }
  262. self.get_success(self.handler.upload_signing_keys_for_user(local_user, keys2))
  263. devices = self.get_success(
  264. self.handler.query_devices(
  265. {"device_keys": {local_user: []}}, 0, local_user, "device123"
  266. )
  267. )
  268. self.assertDictEqual(devices["master_keys"], {local_user: keys2["master_key"]})
  269. def test_reupload_signatures(self):
  270. """re-uploading a signature should not fail"""
  271. local_user = "@boris:" + self.hs.hostname
  272. keys1 = {
  273. "master_key": {
  274. # private key: HvQBbU+hc2Zr+JP1sE0XwBe1pfZZEYtJNPJLZJtS+F8
  275. "user_id": local_user,
  276. "usage": ["master"],
  277. "keys": {
  278. "ed25519:EmkqvokUn8p+vQAGZitOk4PWjp7Ukp3txV2TbMPEiBQ": "EmkqvokUn8p+vQAGZitOk4PWjp7Ukp3txV2TbMPEiBQ"
  279. },
  280. },
  281. "self_signing_key": {
  282. # private key: 2lonYOM6xYKdEsO+6KrC766xBcHnYnim1x/4LFGF8B0
  283. "user_id": local_user,
  284. "usage": ["self_signing"],
  285. "keys": {
  286. "ed25519:nqOvzeuGWT/sRx3h7+MHoInYj3Uk2LD/unI9kDYcHwk": "nqOvzeuGWT/sRx3h7+MHoInYj3Uk2LD/unI9kDYcHwk"
  287. },
  288. },
  289. }
  290. master_signing_key = key.decode_signing_key_base64(
  291. "ed25519",
  292. "EmkqvokUn8p+vQAGZitOk4PWjp7Ukp3txV2TbMPEiBQ",
  293. "HvQBbU+hc2Zr+JP1sE0XwBe1pfZZEYtJNPJLZJtS+F8",
  294. )
  295. sign.sign_json(keys1["self_signing_key"], local_user, master_signing_key)
  296. signing_key = key.decode_signing_key_base64(
  297. "ed25519",
  298. "nqOvzeuGWT/sRx3h7+MHoInYj3Uk2LD/unI9kDYcHwk",
  299. "2lonYOM6xYKdEsO+6KrC766xBcHnYnim1x/4LFGF8B0",
  300. )
  301. self.get_success(self.handler.upload_signing_keys_for_user(local_user, keys1))
  302. # upload two device keys, which will be signed later by the self-signing key
  303. device_key_1 = {
  304. "user_id": local_user,
  305. "device_id": "abc",
  306. "algorithms": [
  307. "m.olm.curve25519-aes-sha2",
  308. RoomEncryptionAlgorithms.MEGOLM_V1_AES_SHA2,
  309. ],
  310. "keys": {
  311. "ed25519:abc": "base64+ed25519+key",
  312. "curve25519:abc": "base64+curve25519+key",
  313. },
  314. "signatures": {local_user: {"ed25519:abc": "base64+signature"}},
  315. }
  316. device_key_2 = {
  317. "user_id": local_user,
  318. "device_id": "def",
  319. "algorithms": [
  320. "m.olm.curve25519-aes-sha2",
  321. RoomEncryptionAlgorithms.MEGOLM_V1_AES_SHA2,
  322. ],
  323. "keys": {
  324. "ed25519:def": "base64+ed25519+key",
  325. "curve25519:def": "base64+curve25519+key",
  326. },
  327. "signatures": {local_user: {"ed25519:def": "base64+signature"}},
  328. }
  329. self.get_success(
  330. self.handler.upload_keys_for_user(
  331. local_user, "abc", {"device_keys": device_key_1}
  332. )
  333. )
  334. self.get_success(
  335. self.handler.upload_keys_for_user(
  336. local_user, "def", {"device_keys": device_key_2}
  337. )
  338. )
  339. # sign the first device key and upload it
  340. del device_key_1["signatures"]
  341. sign.sign_json(device_key_1, local_user, signing_key)
  342. self.get_success(
  343. self.handler.upload_signatures_for_device_keys(
  344. local_user, {local_user: {"abc": device_key_1}}
  345. )
  346. )
  347. # sign the second device key and upload both device keys. The server
  348. # should ignore the first device key since it already has a valid
  349. # signature for it
  350. del device_key_2["signatures"]
  351. sign.sign_json(device_key_2, local_user, signing_key)
  352. self.get_success(
  353. self.handler.upload_signatures_for_device_keys(
  354. local_user, {local_user: {"abc": device_key_1, "def": device_key_2}}
  355. )
  356. )
  357. device_key_1["signatures"][local_user]["ed25519:abc"] = "base64+signature"
  358. device_key_2["signatures"][local_user]["ed25519:def"] = "base64+signature"
  359. devices = self.get_success(
  360. self.handler.query_devices(
  361. {"device_keys": {local_user: []}}, 0, local_user, "device123"
  362. )
  363. )
  364. del devices["device_keys"][local_user]["abc"]["unsigned"]
  365. del devices["device_keys"][local_user]["def"]["unsigned"]
  366. self.assertDictEqual(devices["device_keys"][local_user]["abc"], device_key_1)
  367. self.assertDictEqual(devices["device_keys"][local_user]["def"], device_key_2)
  368. def test_self_signing_key_doesnt_show_up_as_device(self):
  369. """signing keys should be hidden when fetching a user's devices"""
  370. local_user = "@boris:" + self.hs.hostname
  371. keys1 = {
  372. "master_key": {
  373. # private key: 2lonYOM6xYKdEsO+6KrC766xBcHnYnim1x/4LFGF8B0
  374. "user_id": local_user,
  375. "usage": ["master"],
  376. "keys": {
  377. "ed25519:nqOvzeuGWT/sRx3h7+MHoInYj3Uk2LD/unI9kDYcHwk": "nqOvzeuGWT/sRx3h7+MHoInYj3Uk2LD/unI9kDYcHwk"
  378. },
  379. }
  380. }
  381. self.get_success(self.handler.upload_signing_keys_for_user(local_user, keys1))
  382. e = self.get_failure(
  383. self.hs.get_device_handler().check_device_registered(
  384. user_id=local_user,
  385. device_id="nqOvzeuGWT/sRx3h7+MHoInYj3Uk2LD/unI9kDYcHwk",
  386. initial_device_display_name="new display name",
  387. ),
  388. SynapseError,
  389. )
  390. res = e.value.code
  391. self.assertEqual(res, 400)
  392. res = self.get_success(self.handler.query_local_devices({local_user: None}))
  393. self.assertDictEqual(res, {local_user: {}})
  394. def test_upload_signatures(self):
  395. """should check signatures that are uploaded"""
  396. # set up a user with cross-signing keys and a device. This user will
  397. # try uploading signatures
  398. local_user = "@boris:" + self.hs.hostname
  399. device_id = "xyz"
  400. # private key: OMkooTr76ega06xNvXIGPbgvvxAOzmQncN8VObS7aBA
  401. device_pubkey = "NnHhnqiMFQkq969szYkooLaBAXW244ZOxgukCvm2ZeY"
  402. device_key = {
  403. "user_id": local_user,
  404. "device_id": device_id,
  405. "algorithms": [
  406. "m.olm.curve25519-aes-sha2",
  407. RoomEncryptionAlgorithms.MEGOLM_V1_AES_SHA2,
  408. ],
  409. "keys": {"curve25519:xyz": "curve25519+key", "ed25519:xyz": device_pubkey},
  410. "signatures": {local_user: {"ed25519:xyz": "something"}},
  411. }
  412. device_signing_key = key.decode_signing_key_base64(
  413. "ed25519", "xyz", "OMkooTr76ega06xNvXIGPbgvvxAOzmQncN8VObS7aBA"
  414. )
  415. self.get_success(
  416. self.handler.upload_keys_for_user(
  417. local_user, device_id, {"device_keys": device_key}
  418. )
  419. )
  420. # private key: 2lonYOM6xYKdEsO+6KrC766xBcHnYnim1x/4LFGF8B0
  421. master_pubkey = "nqOvzeuGWT/sRx3h7+MHoInYj3Uk2LD/unI9kDYcHwk"
  422. master_key = {
  423. "user_id": local_user,
  424. "usage": ["master"],
  425. "keys": {"ed25519:" + master_pubkey: master_pubkey},
  426. }
  427. master_signing_key = key.decode_signing_key_base64(
  428. "ed25519", master_pubkey, "2lonYOM6xYKdEsO+6KrC766xBcHnYnim1x/4LFGF8B0"
  429. )
  430. usersigning_pubkey = "Hq6gL+utB4ET+UvD5ci0kgAwsX6qP/zvf8v6OInU5iw"
  431. usersigning_key = {
  432. # private key: 4TL4AjRYwDVwD3pqQzcor+ez/euOB1/q78aTJ+czDNs
  433. "user_id": local_user,
  434. "usage": ["user_signing"],
  435. "keys": {"ed25519:" + usersigning_pubkey: usersigning_pubkey},
  436. }
  437. usersigning_signing_key = key.decode_signing_key_base64(
  438. "ed25519", usersigning_pubkey, "4TL4AjRYwDVwD3pqQzcor+ez/euOB1/q78aTJ+czDNs"
  439. )
  440. sign.sign_json(usersigning_key, local_user, master_signing_key)
  441. # private key: HvQBbU+hc2Zr+JP1sE0XwBe1pfZZEYtJNPJLZJtS+F8
  442. selfsigning_pubkey = "EmkqvokUn8p+vQAGZitOk4PWjp7Ukp3txV2TbMPEiBQ"
  443. selfsigning_key = {
  444. "user_id": local_user,
  445. "usage": ["self_signing"],
  446. "keys": {"ed25519:" + selfsigning_pubkey: selfsigning_pubkey},
  447. }
  448. selfsigning_signing_key = key.decode_signing_key_base64(
  449. "ed25519", selfsigning_pubkey, "HvQBbU+hc2Zr+JP1sE0XwBe1pfZZEYtJNPJLZJtS+F8"
  450. )
  451. sign.sign_json(selfsigning_key, local_user, master_signing_key)
  452. cross_signing_keys = {
  453. "master_key": master_key,
  454. "user_signing_key": usersigning_key,
  455. "self_signing_key": selfsigning_key,
  456. }
  457. self.get_success(
  458. self.handler.upload_signing_keys_for_user(local_user, cross_signing_keys)
  459. )
  460. # set up another user with a master key. This user will be signed by
  461. # the first user
  462. other_user = "@otherboris:" + self.hs.hostname
  463. other_master_pubkey = "fHZ3NPiKxoLQm5OoZbKa99SYxprOjNs4TwJUKP+twCM"
  464. other_master_key = {
  465. # private key: oyw2ZUx0O4GifbfFYM0nQvj9CL0b8B7cyN4FprtK8OI
  466. "user_id": other_user,
  467. "usage": ["master"],
  468. "keys": {"ed25519:" + other_master_pubkey: other_master_pubkey},
  469. }
  470. self.get_success(
  471. self.handler.upload_signing_keys_for_user(
  472. other_user, {"master_key": other_master_key}
  473. )
  474. )
  475. # test various signature failures (see below)
  476. ret = self.get_success(
  477. self.handler.upload_signatures_for_device_keys(
  478. local_user,
  479. {
  480. local_user: {
  481. # fails because the signature is invalid
  482. # should fail with INVALID_SIGNATURE
  483. device_id: {
  484. "user_id": local_user,
  485. "device_id": device_id,
  486. "algorithms": [
  487. "m.olm.curve25519-aes-sha2",
  488. RoomEncryptionAlgorithms.MEGOLM_V1_AES_SHA2,
  489. ],
  490. "keys": {
  491. "curve25519:xyz": "curve25519+key",
  492. # private key: OMkooTr76ega06xNvXIGPbgvvxAOzmQncN8VObS7aBA
  493. "ed25519:xyz": device_pubkey,
  494. },
  495. "signatures": {
  496. local_user: {
  497. "ed25519:" + selfsigning_pubkey: "something"
  498. }
  499. },
  500. },
  501. # fails because device is unknown
  502. # should fail with NOT_FOUND
  503. "unknown": {
  504. "user_id": local_user,
  505. "device_id": "unknown",
  506. "signatures": {
  507. local_user: {
  508. "ed25519:" + selfsigning_pubkey: "something"
  509. }
  510. },
  511. },
  512. # fails because the signature is invalid
  513. # should fail with INVALID_SIGNATURE
  514. master_pubkey: {
  515. "user_id": local_user,
  516. "usage": ["master"],
  517. "keys": {"ed25519:" + master_pubkey: master_pubkey},
  518. "signatures": {
  519. local_user: {"ed25519:" + device_pubkey: "something"}
  520. },
  521. },
  522. },
  523. other_user: {
  524. # fails because the device is not the user's master-signing key
  525. # should fail with NOT_FOUND
  526. "unknown": {
  527. "user_id": other_user,
  528. "device_id": "unknown",
  529. "signatures": {
  530. local_user: {
  531. "ed25519:" + usersigning_pubkey: "something"
  532. }
  533. },
  534. },
  535. other_master_pubkey: {
  536. # fails because the key doesn't match what the server has
  537. # should fail with UNKNOWN
  538. "user_id": other_user,
  539. "usage": ["master"],
  540. "keys": {
  541. "ed25519:" + other_master_pubkey: other_master_pubkey
  542. },
  543. "something": "random",
  544. "signatures": {
  545. local_user: {
  546. "ed25519:" + usersigning_pubkey: "something"
  547. }
  548. },
  549. },
  550. },
  551. },
  552. )
  553. )
  554. user_failures = ret["failures"][local_user]
  555. self.assertEqual(user_failures[device_id]["errcode"], Codes.INVALID_SIGNATURE)
  556. self.assertEqual(
  557. user_failures[master_pubkey]["errcode"], Codes.INVALID_SIGNATURE
  558. )
  559. self.assertEqual(user_failures["unknown"]["errcode"], Codes.NOT_FOUND)
  560. other_user_failures = ret["failures"][other_user]
  561. self.assertEqual(other_user_failures["unknown"]["errcode"], Codes.NOT_FOUND)
  562. self.assertEqual(
  563. other_user_failures[other_master_pubkey]["errcode"], Codes.UNKNOWN
  564. )
  565. # test successful signatures
  566. del device_key["signatures"]
  567. sign.sign_json(device_key, local_user, selfsigning_signing_key)
  568. sign.sign_json(master_key, local_user, device_signing_key)
  569. sign.sign_json(other_master_key, local_user, usersigning_signing_key)
  570. ret = self.get_success(
  571. self.handler.upload_signatures_for_device_keys(
  572. local_user,
  573. {
  574. local_user: {device_id: device_key, master_pubkey: master_key},
  575. other_user: {other_master_pubkey: other_master_key},
  576. },
  577. )
  578. )
  579. self.assertEqual(ret["failures"], {})
  580. # fetch the signed keys/devices and make sure that the signatures are there
  581. ret = self.get_success(
  582. self.handler.query_devices(
  583. {"device_keys": {local_user: [], other_user: []}},
  584. 0,
  585. local_user,
  586. "device123",
  587. )
  588. )
  589. self.assertEqual(
  590. ret["device_keys"][local_user]["xyz"]["signatures"][local_user][
  591. "ed25519:" + selfsigning_pubkey
  592. ],
  593. device_key["signatures"][local_user]["ed25519:" + selfsigning_pubkey],
  594. )
  595. self.assertEqual(
  596. ret["master_keys"][local_user]["signatures"][local_user][
  597. "ed25519:" + device_id
  598. ],
  599. master_key["signatures"][local_user]["ed25519:" + device_id],
  600. )
  601. self.assertEqual(
  602. ret["master_keys"][other_user]["signatures"][local_user][
  603. "ed25519:" + usersigning_pubkey
  604. ],
  605. other_master_key["signatures"][local_user]["ed25519:" + usersigning_pubkey],
  606. )
  607. def test_query_devices_remote_no_sync(self):
  608. """Tests that querying keys for a remote user that we don't share a room
  609. with returns the cross signing keys correctly.
  610. """
  611. remote_user_id = "@test:other"
  612. local_user_id = "@test:test"
  613. remote_master_key = "85T7JXPFBAySB/jwby4S3lBPTqY3+Zg53nYuGmu1ggY"
  614. remote_self_signing_key = "QeIiFEjluPBtI7WQdG365QKZcFs9kqmHir6RBD0//nQ"
  615. self.hs.get_federation_client().query_client_keys = mock.Mock(
  616. return_value=defer.succeed(
  617. {
  618. "device_keys": {remote_user_id: {}},
  619. "master_keys": {
  620. remote_user_id: {
  621. "user_id": remote_user_id,
  622. "usage": ["master"],
  623. "keys": {"ed25519:" + remote_master_key: remote_master_key},
  624. },
  625. },
  626. "self_signing_keys": {
  627. remote_user_id: {
  628. "user_id": remote_user_id,
  629. "usage": ["self_signing"],
  630. "keys": {
  631. "ed25519:"
  632. + remote_self_signing_key: remote_self_signing_key
  633. },
  634. }
  635. },
  636. }
  637. )
  638. )
  639. e2e_handler = self.hs.get_e2e_keys_handler()
  640. query_result = self.get_success(
  641. e2e_handler.query_devices(
  642. {
  643. "device_keys": {remote_user_id: []},
  644. },
  645. timeout=10,
  646. from_user_id=local_user_id,
  647. from_device_id="some_device_id",
  648. )
  649. )
  650. self.assertEqual(query_result["failures"], {})
  651. self.assertEqual(
  652. query_result["master_keys"],
  653. {
  654. remote_user_id: {
  655. "user_id": remote_user_id,
  656. "usage": ["master"],
  657. "keys": {"ed25519:" + remote_master_key: remote_master_key},
  658. },
  659. },
  660. )
  661. self.assertEqual(
  662. query_result["self_signing_keys"],
  663. {
  664. remote_user_id: {
  665. "user_id": remote_user_id,
  666. "usage": ["self_signing"],
  667. "keys": {
  668. "ed25519:" + remote_self_signing_key: remote_self_signing_key
  669. },
  670. }
  671. },
  672. )
  673. def test_query_devices_remote_sync(self):
  674. """Tests that querying keys for a remote user that we share a room with,
  675. but haven't yet fetched the keys for, returns the cross signing keys
  676. correctly.
  677. """
  678. remote_user_id = "@test:other"
  679. local_user_id = "@test:test"
  680. self.store.get_rooms_for_user = mock.Mock(
  681. return_value=defer.succeed({"some_room_id"})
  682. )
  683. remote_master_key = "85T7JXPFBAySB/jwby4S3lBPTqY3+Zg53nYuGmu1ggY"
  684. remote_self_signing_key = "QeIiFEjluPBtI7WQdG365QKZcFs9kqmHir6RBD0//nQ"
  685. self.hs.get_federation_client().query_user_devices = mock.Mock(
  686. return_value=defer.succeed(
  687. {
  688. "user_id": remote_user_id,
  689. "stream_id": 1,
  690. "devices": [],
  691. "master_key": {
  692. "user_id": remote_user_id,
  693. "usage": ["master"],
  694. "keys": {"ed25519:" + remote_master_key: remote_master_key},
  695. },
  696. "self_signing_key": {
  697. "user_id": remote_user_id,
  698. "usage": ["self_signing"],
  699. "keys": {
  700. "ed25519:"
  701. + remote_self_signing_key: remote_self_signing_key
  702. },
  703. },
  704. }
  705. )
  706. )
  707. e2e_handler = self.hs.get_e2e_keys_handler()
  708. query_result = self.get_success(
  709. e2e_handler.query_devices(
  710. {
  711. "device_keys": {remote_user_id: []},
  712. },
  713. timeout=10,
  714. from_user_id=local_user_id,
  715. from_device_id="some_device_id",
  716. )
  717. )
  718. self.assertEqual(query_result["failures"], {})
  719. self.assertEqual(
  720. query_result["master_keys"],
  721. {
  722. remote_user_id: {
  723. "user_id": remote_user_id,
  724. "usage": ["master"],
  725. "keys": {"ed25519:" + remote_master_key: remote_master_key},
  726. }
  727. },
  728. )
  729. self.assertEqual(
  730. query_result["self_signing_keys"],
  731. {
  732. remote_user_id: {
  733. "user_id": remote_user_id,
  734. "usage": ["self_signing"],
  735. "keys": {
  736. "ed25519:" + remote_self_signing_key: remote_self_signing_key
  737. },
  738. }
  739. },
  740. )