test_e2e_keys.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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 synapse.api.constants import RoomEncryptionAlgorithms
  19. from synapse.api.errors import Codes, SynapseError
  20. from tests import unittest
  21. class E2eKeysHandlerTestCase(unittest.HomeserverTestCase):
  22. def make_homeserver(self, reactor, clock):
  23. return self.setup_test_homeserver(federation_client=mock.Mock())
  24. def prepare(self, reactor, clock, hs):
  25. self.handler = hs.get_e2e_keys_handler()
  26. self.store = self.hs.get_datastore()
  27. def test_query_local_devices_no_devices(self):
  28. """If the user has no devices, we expect an empty list."""
  29. local_user = "@boris:" + self.hs.hostname
  30. res = self.get_success(self.handler.query_local_devices({local_user: None}))
  31. self.assertDictEqual(res, {local_user: {}})
  32. def test_reupload_one_time_keys(self):
  33. """we should be able to re-upload the same keys"""
  34. local_user = "@boris:" + self.hs.hostname
  35. device_id = "xyz"
  36. keys = {
  37. "alg1:k1": "key1",
  38. "alg2:k2": {"key": "key2", "signatures": {"k1": "sig1"}},
  39. "alg2:k3": {"key": "key3"},
  40. }
  41. res = self.get_success(
  42. self.handler.upload_keys_for_user(
  43. local_user, device_id, {"one_time_keys": keys}
  44. )
  45. )
  46. self.assertDictEqual(res, {"one_time_key_counts": {"alg1": 1, "alg2": 2}})
  47. # we should be able to change the signature without a problem
  48. keys["alg2:k2"]["signatures"]["k1"] = "sig2"
  49. res = self.get_success(
  50. self.handler.upload_keys_for_user(
  51. local_user, device_id, {"one_time_keys": keys}
  52. )
  53. )
  54. self.assertDictEqual(res, {"one_time_key_counts": {"alg1": 1, "alg2": 2}})
  55. def test_change_one_time_keys(self):
  56. """attempts to change one-time-keys should be rejected"""
  57. local_user = "@boris:" + self.hs.hostname
  58. device_id = "xyz"
  59. keys = {
  60. "alg1:k1": "key1",
  61. "alg2:k2": {"key": "key2", "signatures": {"k1": "sig1"}},
  62. "alg2:k3": {"key": "key3"},
  63. }
  64. res = self.get_success(
  65. self.handler.upload_keys_for_user(
  66. local_user, device_id, {"one_time_keys": keys}
  67. )
  68. )
  69. self.assertDictEqual(res, {"one_time_key_counts": {"alg1": 1, "alg2": 2}})
  70. # Error when changing string key
  71. self.get_failure(
  72. self.handler.upload_keys_for_user(
  73. local_user, device_id, {"one_time_keys": {"alg1:k1": "key2"}}
  74. ),
  75. SynapseError,
  76. )
  77. # Error when replacing dict key with strin
  78. self.get_failure(
  79. self.handler.upload_keys_for_user(
  80. local_user, device_id, {"one_time_keys": {"alg2:k3": "key2"}}
  81. ),
  82. SynapseError,
  83. )
  84. # Error when replacing string key with dict
  85. self.get_failure(
  86. self.handler.upload_keys_for_user(
  87. local_user,
  88. device_id,
  89. {"one_time_keys": {"alg1:k1": {"key": "key"}}},
  90. ),
  91. SynapseError,
  92. )
  93. # Error when replacing dict key
  94. self.get_failure(
  95. self.handler.upload_keys_for_user(
  96. local_user,
  97. device_id,
  98. {
  99. "one_time_keys": {
  100. "alg2:k2": {"key": "key3", "signatures": {"k1": "sig1"}}
  101. }
  102. },
  103. ),
  104. SynapseError,
  105. )
  106. def test_claim_one_time_key(self):
  107. local_user = "@boris:" + self.hs.hostname
  108. device_id = "xyz"
  109. keys = {"alg1:k1": "key1"}
  110. res = self.get_success(
  111. self.handler.upload_keys_for_user(
  112. local_user, device_id, {"one_time_keys": keys}
  113. )
  114. )
  115. self.assertDictEqual(res, {"one_time_key_counts": {"alg1": 1}})
  116. res2 = self.get_success(
  117. self.handler.claim_one_time_keys(
  118. {"one_time_keys": {local_user: {device_id: "alg1"}}}, timeout=None
  119. )
  120. )
  121. self.assertEqual(
  122. res2,
  123. {
  124. "failures": {},
  125. "one_time_keys": {local_user: {device_id: {"alg1:k1": "key1"}}},
  126. },
  127. )
  128. def test_fallback_key(self):
  129. local_user = "@boris:" + self.hs.hostname
  130. device_id = "xyz"
  131. fallback_key = {"alg1:k1": "key1"}
  132. otk = {"alg1:k2": "key2"}
  133. # we shouldn't have any unused fallback keys yet
  134. res = self.get_success(
  135. self.store.get_e2e_unused_fallback_key_types(local_user, device_id)
  136. )
  137. self.assertEqual(res, [])
  138. self.get_success(
  139. self.handler.upload_keys_for_user(
  140. local_user,
  141. device_id,
  142. {"org.matrix.msc2732.fallback_keys": fallback_key},
  143. )
  144. )
  145. # we should now have an unused alg1 key
  146. res = self.get_success(
  147. self.store.get_e2e_unused_fallback_key_types(local_user, device_id)
  148. )
  149. self.assertEqual(res, ["alg1"])
  150. # claiming an OTK when no OTKs are available should return the fallback
  151. # key
  152. res = self.get_success(
  153. self.handler.claim_one_time_keys(
  154. {"one_time_keys": {local_user: {device_id: "alg1"}}}, timeout=None
  155. )
  156. )
  157. self.assertEqual(
  158. res,
  159. {"failures": {}, "one_time_keys": {local_user: {device_id: fallback_key}}},
  160. )
  161. # we shouldn't have any unused fallback keys again
  162. res = self.get_success(
  163. self.store.get_e2e_unused_fallback_key_types(local_user, device_id)
  164. )
  165. self.assertEqual(res, [])
  166. # claiming an OTK again should return the same fallback key
  167. res = self.get_success(
  168. self.handler.claim_one_time_keys(
  169. {"one_time_keys": {local_user: {device_id: "alg1"}}}, timeout=None
  170. )
  171. )
  172. self.assertEqual(
  173. res,
  174. {"failures": {}, "one_time_keys": {local_user: {device_id: fallback_key}}},
  175. )
  176. # if the user uploads a one-time key, the next claim should fetch the
  177. # one-time key, and then go back to the fallback
  178. self.get_success(
  179. self.handler.upload_keys_for_user(
  180. local_user, device_id, {"one_time_keys": otk}
  181. )
  182. )
  183. res = self.get_success(
  184. self.handler.claim_one_time_keys(
  185. {"one_time_keys": {local_user: {device_id: "alg1"}}}, timeout=None
  186. )
  187. )
  188. self.assertEqual(
  189. res,
  190. {"failures": {}, "one_time_keys": {local_user: {device_id: otk}}},
  191. )
  192. res = self.get_success(
  193. self.handler.claim_one_time_keys(
  194. {"one_time_keys": {local_user: {device_id: "alg1"}}}, timeout=None
  195. )
  196. )
  197. self.assertEqual(
  198. res,
  199. {"failures": {}, "one_time_keys": {local_user: {device_id: fallback_key}}},
  200. )
  201. def test_replace_master_key(self):
  202. """uploading a new signing key should make the old signing key unavailable"""
  203. local_user = "@boris:" + self.hs.hostname
  204. keys1 = {
  205. "master_key": {
  206. # private key: 2lonYOM6xYKdEsO+6KrC766xBcHnYnim1x/4LFGF8B0
  207. "user_id": local_user,
  208. "usage": ["master"],
  209. "keys": {
  210. "ed25519:nqOvzeuGWT/sRx3h7+MHoInYj3Uk2LD/unI9kDYcHwk": "nqOvzeuGWT/sRx3h7+MHoInYj3Uk2LD/unI9kDYcHwk"
  211. },
  212. }
  213. }
  214. self.get_success(self.handler.upload_signing_keys_for_user(local_user, keys1))
  215. keys2 = {
  216. "master_key": {
  217. # private key: 4TL4AjRYwDVwD3pqQzcor+ez/euOB1/q78aTJ+czDNs
  218. "user_id": local_user,
  219. "usage": ["master"],
  220. "keys": {
  221. "ed25519:Hq6gL+utB4ET+UvD5ci0kgAwsX6qP/zvf8v6OInU5iw": "Hq6gL+utB4ET+UvD5ci0kgAwsX6qP/zvf8v6OInU5iw"
  222. },
  223. }
  224. }
  225. self.get_success(self.handler.upload_signing_keys_for_user(local_user, keys2))
  226. devices = self.get_success(
  227. self.handler.query_devices(
  228. {"device_keys": {local_user: []}}, 0, local_user, "device123"
  229. )
  230. )
  231. self.assertDictEqual(devices["master_keys"], {local_user: keys2["master_key"]})
  232. def test_reupload_signatures(self):
  233. """re-uploading a signature should not fail"""
  234. local_user = "@boris:" + self.hs.hostname
  235. keys1 = {
  236. "master_key": {
  237. # private key: HvQBbU+hc2Zr+JP1sE0XwBe1pfZZEYtJNPJLZJtS+F8
  238. "user_id": local_user,
  239. "usage": ["master"],
  240. "keys": {
  241. "ed25519:EmkqvokUn8p+vQAGZitOk4PWjp7Ukp3txV2TbMPEiBQ": "EmkqvokUn8p+vQAGZitOk4PWjp7Ukp3txV2TbMPEiBQ"
  242. },
  243. },
  244. "self_signing_key": {
  245. # private key: 2lonYOM6xYKdEsO+6KrC766xBcHnYnim1x/4LFGF8B0
  246. "user_id": local_user,
  247. "usage": ["self_signing"],
  248. "keys": {
  249. "ed25519:nqOvzeuGWT/sRx3h7+MHoInYj3Uk2LD/unI9kDYcHwk": "nqOvzeuGWT/sRx3h7+MHoInYj3Uk2LD/unI9kDYcHwk"
  250. },
  251. },
  252. }
  253. master_signing_key = key.decode_signing_key_base64(
  254. "ed25519",
  255. "EmkqvokUn8p+vQAGZitOk4PWjp7Ukp3txV2TbMPEiBQ",
  256. "HvQBbU+hc2Zr+JP1sE0XwBe1pfZZEYtJNPJLZJtS+F8",
  257. )
  258. sign.sign_json(keys1["self_signing_key"], local_user, master_signing_key)
  259. signing_key = key.decode_signing_key_base64(
  260. "ed25519",
  261. "nqOvzeuGWT/sRx3h7+MHoInYj3Uk2LD/unI9kDYcHwk",
  262. "2lonYOM6xYKdEsO+6KrC766xBcHnYnim1x/4LFGF8B0",
  263. )
  264. self.get_success(self.handler.upload_signing_keys_for_user(local_user, keys1))
  265. # upload two device keys, which will be signed later by the self-signing key
  266. device_key_1 = {
  267. "user_id": local_user,
  268. "device_id": "abc",
  269. "algorithms": [
  270. "m.olm.curve25519-aes-sha2",
  271. RoomEncryptionAlgorithms.MEGOLM_V1_AES_SHA2,
  272. ],
  273. "keys": {
  274. "ed25519:abc": "base64+ed25519+key",
  275. "curve25519:abc": "base64+curve25519+key",
  276. },
  277. "signatures": {local_user: {"ed25519:abc": "base64+signature"}},
  278. }
  279. device_key_2 = {
  280. "user_id": local_user,
  281. "device_id": "def",
  282. "algorithms": [
  283. "m.olm.curve25519-aes-sha2",
  284. RoomEncryptionAlgorithms.MEGOLM_V1_AES_SHA2,
  285. ],
  286. "keys": {
  287. "ed25519:def": "base64+ed25519+key",
  288. "curve25519:def": "base64+curve25519+key",
  289. },
  290. "signatures": {local_user: {"ed25519:def": "base64+signature"}},
  291. }
  292. self.get_success(
  293. self.handler.upload_keys_for_user(
  294. local_user, "abc", {"device_keys": device_key_1}
  295. )
  296. )
  297. self.get_success(
  298. self.handler.upload_keys_for_user(
  299. local_user, "def", {"device_keys": device_key_2}
  300. )
  301. )
  302. # sign the first device key and upload it
  303. del device_key_1["signatures"]
  304. sign.sign_json(device_key_1, local_user, signing_key)
  305. self.get_success(
  306. self.handler.upload_signatures_for_device_keys(
  307. local_user, {local_user: {"abc": device_key_1}}
  308. )
  309. )
  310. # sign the second device key and upload both device keys. The server
  311. # should ignore the first device key since it already has a valid
  312. # signature for it
  313. del device_key_2["signatures"]
  314. sign.sign_json(device_key_2, local_user, signing_key)
  315. self.get_success(
  316. self.handler.upload_signatures_for_device_keys(
  317. local_user, {local_user: {"abc": device_key_1, "def": device_key_2}}
  318. )
  319. )
  320. device_key_1["signatures"][local_user]["ed25519:abc"] = "base64+signature"
  321. device_key_2["signatures"][local_user]["ed25519:def"] = "base64+signature"
  322. devices = self.get_success(
  323. self.handler.query_devices(
  324. {"device_keys": {local_user: []}}, 0, local_user, "device123"
  325. )
  326. )
  327. del devices["device_keys"][local_user]["abc"]["unsigned"]
  328. del devices["device_keys"][local_user]["def"]["unsigned"]
  329. self.assertDictEqual(devices["device_keys"][local_user]["abc"], device_key_1)
  330. self.assertDictEqual(devices["device_keys"][local_user]["def"], device_key_2)
  331. def test_self_signing_key_doesnt_show_up_as_device(self):
  332. """signing keys should be hidden when fetching a user's devices"""
  333. local_user = "@boris:" + self.hs.hostname
  334. keys1 = {
  335. "master_key": {
  336. # private key: 2lonYOM6xYKdEsO+6KrC766xBcHnYnim1x/4LFGF8B0
  337. "user_id": local_user,
  338. "usage": ["master"],
  339. "keys": {
  340. "ed25519:nqOvzeuGWT/sRx3h7+MHoInYj3Uk2LD/unI9kDYcHwk": "nqOvzeuGWT/sRx3h7+MHoInYj3Uk2LD/unI9kDYcHwk"
  341. },
  342. }
  343. }
  344. self.get_success(self.handler.upload_signing_keys_for_user(local_user, keys1))
  345. e = self.get_failure(
  346. self.hs.get_device_handler().check_device_registered(
  347. user_id=local_user,
  348. device_id="nqOvzeuGWT/sRx3h7+MHoInYj3Uk2LD/unI9kDYcHwk",
  349. initial_device_display_name="new display name",
  350. ),
  351. SynapseError,
  352. )
  353. res = e.value.code
  354. self.assertEqual(res, 400)
  355. res = self.get_success(self.handler.query_local_devices({local_user: None}))
  356. self.assertDictEqual(res, {local_user: {}})
  357. def test_upload_signatures(self):
  358. """should check signatures that are uploaded"""
  359. # set up a user with cross-signing keys and a device. This user will
  360. # try uploading signatures
  361. local_user = "@boris:" + self.hs.hostname
  362. device_id = "xyz"
  363. # private key: OMkooTr76ega06xNvXIGPbgvvxAOzmQncN8VObS7aBA
  364. device_pubkey = "NnHhnqiMFQkq969szYkooLaBAXW244ZOxgukCvm2ZeY"
  365. device_key = {
  366. "user_id": local_user,
  367. "device_id": device_id,
  368. "algorithms": [
  369. "m.olm.curve25519-aes-sha2",
  370. RoomEncryptionAlgorithms.MEGOLM_V1_AES_SHA2,
  371. ],
  372. "keys": {"curve25519:xyz": "curve25519+key", "ed25519:xyz": device_pubkey},
  373. "signatures": {local_user: {"ed25519:xyz": "something"}},
  374. }
  375. device_signing_key = key.decode_signing_key_base64(
  376. "ed25519", "xyz", "OMkooTr76ega06xNvXIGPbgvvxAOzmQncN8VObS7aBA"
  377. )
  378. self.get_success(
  379. self.handler.upload_keys_for_user(
  380. local_user, device_id, {"device_keys": device_key}
  381. )
  382. )
  383. # private key: 2lonYOM6xYKdEsO+6KrC766xBcHnYnim1x/4LFGF8B0
  384. master_pubkey = "nqOvzeuGWT/sRx3h7+MHoInYj3Uk2LD/unI9kDYcHwk"
  385. master_key = {
  386. "user_id": local_user,
  387. "usage": ["master"],
  388. "keys": {"ed25519:" + master_pubkey: master_pubkey},
  389. }
  390. master_signing_key = key.decode_signing_key_base64(
  391. "ed25519", master_pubkey, "2lonYOM6xYKdEsO+6KrC766xBcHnYnim1x/4LFGF8B0"
  392. )
  393. usersigning_pubkey = "Hq6gL+utB4ET+UvD5ci0kgAwsX6qP/zvf8v6OInU5iw"
  394. usersigning_key = {
  395. # private key: 4TL4AjRYwDVwD3pqQzcor+ez/euOB1/q78aTJ+czDNs
  396. "user_id": local_user,
  397. "usage": ["user_signing"],
  398. "keys": {"ed25519:" + usersigning_pubkey: usersigning_pubkey},
  399. }
  400. usersigning_signing_key = key.decode_signing_key_base64(
  401. "ed25519", usersigning_pubkey, "4TL4AjRYwDVwD3pqQzcor+ez/euOB1/q78aTJ+czDNs"
  402. )
  403. sign.sign_json(usersigning_key, local_user, master_signing_key)
  404. # private key: HvQBbU+hc2Zr+JP1sE0XwBe1pfZZEYtJNPJLZJtS+F8
  405. selfsigning_pubkey = "EmkqvokUn8p+vQAGZitOk4PWjp7Ukp3txV2TbMPEiBQ"
  406. selfsigning_key = {
  407. "user_id": local_user,
  408. "usage": ["self_signing"],
  409. "keys": {"ed25519:" + selfsigning_pubkey: selfsigning_pubkey},
  410. }
  411. selfsigning_signing_key = key.decode_signing_key_base64(
  412. "ed25519", selfsigning_pubkey, "HvQBbU+hc2Zr+JP1sE0XwBe1pfZZEYtJNPJLZJtS+F8"
  413. )
  414. sign.sign_json(selfsigning_key, local_user, master_signing_key)
  415. cross_signing_keys = {
  416. "master_key": master_key,
  417. "user_signing_key": usersigning_key,
  418. "self_signing_key": selfsigning_key,
  419. }
  420. self.get_success(
  421. self.handler.upload_signing_keys_for_user(local_user, cross_signing_keys)
  422. )
  423. # set up another user with a master key. This user will be signed by
  424. # the first user
  425. other_user = "@otherboris:" + self.hs.hostname
  426. other_master_pubkey = "fHZ3NPiKxoLQm5OoZbKa99SYxprOjNs4TwJUKP+twCM"
  427. other_master_key = {
  428. # private key: oyw2ZUx0O4GifbfFYM0nQvj9CL0b8B7cyN4FprtK8OI
  429. "user_id": other_user,
  430. "usage": ["master"],
  431. "keys": {"ed25519:" + other_master_pubkey: other_master_pubkey},
  432. }
  433. self.get_success(
  434. self.handler.upload_signing_keys_for_user(
  435. other_user, {"master_key": other_master_key}
  436. )
  437. )
  438. # test various signature failures (see below)
  439. ret = self.get_success(
  440. self.handler.upload_signatures_for_device_keys(
  441. local_user,
  442. {
  443. local_user: {
  444. # fails because the signature is invalid
  445. # should fail with INVALID_SIGNATURE
  446. device_id: {
  447. "user_id": local_user,
  448. "device_id": device_id,
  449. "algorithms": [
  450. "m.olm.curve25519-aes-sha2",
  451. RoomEncryptionAlgorithms.MEGOLM_V1_AES_SHA2,
  452. ],
  453. "keys": {
  454. "curve25519:xyz": "curve25519+key",
  455. # private key: OMkooTr76ega06xNvXIGPbgvvxAOzmQncN8VObS7aBA
  456. "ed25519:xyz": device_pubkey,
  457. },
  458. "signatures": {
  459. local_user: {
  460. "ed25519:" + selfsigning_pubkey: "something"
  461. }
  462. },
  463. },
  464. # fails because device is unknown
  465. # should fail with NOT_FOUND
  466. "unknown": {
  467. "user_id": local_user,
  468. "device_id": "unknown",
  469. "signatures": {
  470. local_user: {
  471. "ed25519:" + selfsigning_pubkey: "something"
  472. }
  473. },
  474. },
  475. # fails because the signature is invalid
  476. # should fail with INVALID_SIGNATURE
  477. master_pubkey: {
  478. "user_id": local_user,
  479. "usage": ["master"],
  480. "keys": {"ed25519:" + master_pubkey: master_pubkey},
  481. "signatures": {
  482. local_user: {"ed25519:" + device_pubkey: "something"}
  483. },
  484. },
  485. },
  486. other_user: {
  487. # fails because the device is not the user's master-signing key
  488. # should fail with NOT_FOUND
  489. "unknown": {
  490. "user_id": other_user,
  491. "device_id": "unknown",
  492. "signatures": {
  493. local_user: {
  494. "ed25519:" + usersigning_pubkey: "something"
  495. }
  496. },
  497. },
  498. other_master_pubkey: {
  499. # fails because the key doesn't match what the server has
  500. # should fail with UNKNOWN
  501. "user_id": other_user,
  502. "usage": ["master"],
  503. "keys": {
  504. "ed25519:" + other_master_pubkey: other_master_pubkey
  505. },
  506. "something": "random",
  507. "signatures": {
  508. local_user: {
  509. "ed25519:" + usersigning_pubkey: "something"
  510. }
  511. },
  512. },
  513. },
  514. },
  515. )
  516. )
  517. user_failures = ret["failures"][local_user]
  518. self.assertEqual(user_failures[device_id]["errcode"], Codes.INVALID_SIGNATURE)
  519. self.assertEqual(
  520. user_failures[master_pubkey]["errcode"], Codes.INVALID_SIGNATURE
  521. )
  522. self.assertEqual(user_failures["unknown"]["errcode"], Codes.NOT_FOUND)
  523. other_user_failures = ret["failures"][other_user]
  524. self.assertEqual(other_user_failures["unknown"]["errcode"], Codes.NOT_FOUND)
  525. self.assertEqual(
  526. other_user_failures[other_master_pubkey]["errcode"], Codes.UNKNOWN
  527. )
  528. # test successful signatures
  529. del device_key["signatures"]
  530. sign.sign_json(device_key, local_user, selfsigning_signing_key)
  531. sign.sign_json(master_key, local_user, device_signing_key)
  532. sign.sign_json(other_master_key, local_user, usersigning_signing_key)
  533. ret = self.get_success(
  534. self.handler.upload_signatures_for_device_keys(
  535. local_user,
  536. {
  537. local_user: {device_id: device_key, master_pubkey: master_key},
  538. other_user: {other_master_pubkey: other_master_key},
  539. },
  540. )
  541. )
  542. self.assertEqual(ret["failures"], {})
  543. # fetch the signed keys/devices and make sure that the signatures are there
  544. ret = self.get_success(
  545. self.handler.query_devices(
  546. {"device_keys": {local_user: [], other_user: []}},
  547. 0,
  548. local_user,
  549. "device123",
  550. )
  551. )
  552. self.assertEqual(
  553. ret["device_keys"][local_user]["xyz"]["signatures"][local_user][
  554. "ed25519:" + selfsigning_pubkey
  555. ],
  556. device_key["signatures"][local_user]["ed25519:" + selfsigning_pubkey],
  557. )
  558. self.assertEqual(
  559. ret["master_keys"][local_user]["signatures"][local_user][
  560. "ed25519:" + device_id
  561. ],
  562. master_key["signatures"][local_user]["ed25519:" + device_id],
  563. )
  564. self.assertEqual(
  565. ret["master_keys"][other_user]["signatures"][local_user][
  566. "ed25519:" + usersigning_pubkey
  567. ],
  568. other_master_key["signatures"][local_user]["ed25519:" + usersigning_pubkey],
  569. )