1
0

test_e2e_keys.py 24 KB

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