omemo.js 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  1. /*global mock */
  2. const { $iq, $pres, $msg, _, Strophe } = converse.env;
  3. const u = converse.env.utils;
  4. async function deviceListFetched (_converse, jid) {
  5. const selector = `iq[to="${jid}"] items[node="eu.siacs.conversations.axolotl.devicelist"]`;
  6. const stanza = await u.waitUntil(
  7. () => Array.from(_converse.connection.IQ_stanzas).filter(iq => iq.querySelector(selector)).pop()
  8. );
  9. await u.waitUntil(() => _converse.devicelists.get(jid));
  10. return stanza;
  11. }
  12. function ownDeviceHasBeenPublished (_converse) {
  13. return _.filter(
  14. Array.from(_converse.connection.IQ_stanzas),
  15. iq => iq.querySelector('iq[from="'+_converse.bare_jid+'"] publish[node="eu.siacs.conversations.axolotl.devicelist"]')
  16. ).pop();
  17. }
  18. function bundleHasBeenPublished (_converse) {
  19. const selector = 'publish[node="eu.siacs.conversations.axolotl.bundles:123456789"]';
  20. return Array.from(_converse.connection.IQ_stanzas).filter(iq => iq.querySelector(selector)).pop();
  21. }
  22. function bundleFetched (_converse, jid, device_id) {
  23. return _.filter(
  24. Array.from(_converse.connection.IQ_stanzas),
  25. iq => iq.querySelector(`iq[to="${jid}"] items[node="eu.siacs.conversations.axolotl.bundles:${device_id}"]`)
  26. ).pop();
  27. }
  28. async function initializedOMEMO (_converse) {
  29. await mock.waitUntilDiscoConfirmed(
  30. _converse, _converse.bare_jid,
  31. [{'category': 'pubsub', 'type': 'pep'}],
  32. ['http://jabber.org/protocol/pubsub#publish-options']
  33. );
  34. let iq_stanza = await u.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid));
  35. let stanza = $iq({
  36. 'from': _converse.bare_jid,
  37. 'id': iq_stanza.getAttribute('id'),
  38. 'to': _converse.bare_jid,
  39. 'type': 'result',
  40. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  41. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  42. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  43. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  44. .c('device', {'id': '482886413b977930064a5888b92134fe'});
  45. _converse.connection._dataRecv(mock.createRequest(stanza));
  46. iq_stanza = await u.waitUntil(() => ownDeviceHasBeenPublished(_converse))
  47. stanza = $iq({
  48. 'from': _converse.bare_jid,
  49. 'id': iq_stanza.getAttribute('id'),
  50. 'to': _converse.bare_jid,
  51. 'type': 'result'});
  52. _converse.connection._dataRecv(mock.createRequest(stanza));
  53. iq_stanza = await u.waitUntil(() => bundleHasBeenPublished(_converse))
  54. stanza = $iq({
  55. 'from': _converse.bare_jid,
  56. 'id': iq_stanza.getAttribute('id'),
  57. 'to': _converse.bare_jid,
  58. 'type': 'result'});
  59. _converse.connection._dataRecv(mock.createRequest(stanza));
  60. await _converse.api.waitUntil('OMEMOInitialized');
  61. }
  62. describe("The OMEMO module", function() {
  63. it("adds methods for encrypting and decrypting messages via AES GCM",
  64. mock.initConverse(
  65. ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  66. async function (done, _converse) {
  67. const message = 'This message will be encrypted'
  68. await mock.waitForRoster(_converse, 'current', 1);
  69. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  70. const view = await mock.openChatBoxFor(_converse, contact_jid);
  71. const payload = await view.model.encryptMessage(message);
  72. const result = await view.model.decryptMessage(payload);
  73. expect(result).toBe(message);
  74. done();
  75. }));
  76. it("enables encrypted messages to be sent and received",
  77. mock.initConverse(
  78. ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  79. async function (done, _converse) {
  80. let sent_stanza;
  81. await mock.waitForRoster(_converse, 'current', 1);
  82. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  83. await u.waitUntil(() => initializedOMEMO(_converse));
  84. await mock.openChatBoxFor(_converse, contact_jid);
  85. let iq_stanza = await u.waitUntil(() => deviceListFetched(_converse, contact_jid));
  86. let stanza = $iq({
  87. 'from': contact_jid,
  88. 'id': iq_stanza.getAttribute('id'),
  89. 'to': _converse.connection.jid,
  90. 'type': 'result',
  91. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  92. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  93. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  94. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  95. .c('device', {'id': '555'});
  96. _converse.connection._dataRecv(mock.createRequest(stanza));
  97. await u.waitUntil(() => _converse.omemo_store);
  98. const devicelist = _converse.devicelists.get({'jid': contact_jid});
  99. await u.waitUntil(() => devicelist.devices.length === 1);
  100. const view = _converse.chatboxviews.get(contact_jid);
  101. view.model.set('omemo_active', true);
  102. const textarea = view.el.querySelector('.chat-textarea');
  103. textarea.value = 'This message will be encrypted';
  104. view.onKeyDown({
  105. target: textarea,
  106. preventDefault: function preventDefault () {},
  107. keyCode: 13 // Enter
  108. });
  109. iq_stanza = await u.waitUntil(() => bundleFetched(_converse, contact_jid, '555'));
  110. stanza = $iq({
  111. 'from': contact_jid,
  112. 'id': iq_stanza.getAttribute('id'),
  113. 'to': _converse.bare_jid,
  114. 'type': 'result',
  115. }).c('pubsub', {
  116. 'xmlns': 'http://jabber.org/protocol/pubsub'
  117. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:555"})
  118. .c('item')
  119. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  120. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('1111')).up()
  121. .c('signedPreKeySignature').t(btoa('2222')).up()
  122. .c('identityKey').t(btoa('3333')).up()
  123. .c('prekeys')
  124. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1001')).up()
  125. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1002')).up()
  126. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1003'));
  127. _converse.connection._dataRecv(mock.createRequest(stanza));
  128. iq_stanza = await u.waitUntil(() => bundleFetched(_converse, _converse.bare_jid, '482886413b977930064a5888b92134fe'));
  129. stanza = $iq({
  130. 'from': _converse.bare_jid,
  131. 'id': iq_stanza.getAttribute('id'),
  132. 'to': _converse.bare_jid,
  133. 'type': 'result',
  134. }).c('pubsub', {
  135. 'xmlns': 'http://jabber.org/protocol/pubsub'
  136. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:482886413b977930064a5888b92134fe"})
  137. .c('item')
  138. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  139. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('100000')).up()
  140. .c('signedPreKeySignature').t(btoa('200000')).up()
  141. .c('identityKey').t(btoa('300000')).up()
  142. .c('prekeys')
  143. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1991')).up()
  144. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1992')).up()
  145. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1993'));
  146. spyOn(_converse.connection, 'send').and.callFake(stanza => { sent_stanza = stanza });
  147. _converse.connection._dataRecv(mock.createRequest(stanza));
  148. await u.waitUntil(() => sent_stanza);
  149. expect(sent_stanza.toLocaleString()).toBe(
  150. `<message from="romeo@montague.lit/orchard" id="${sent_stanza.nodeTree.getAttribute("id")}" `+
  151. `to="mercutio@montague.lit" `+
  152. `type="chat" xmlns="jabber:client">`+
  153. `<body>This is an OMEMO encrypted message which your client doesn’t seem to support. Find more information on https://conversations.im/omemo</body>`+
  154. `<request xmlns="urn:xmpp:receipts"/>`+
  155. `<encrypted xmlns="eu.siacs.conversations.axolotl">`+
  156. `<header sid="123456789">`+
  157. `<key rid="482886413b977930064a5888b92134fe">YzFwaDNSNzNYNw==</key>`+
  158. `<key rid="555">YzFwaDNSNzNYNw==</key>`+
  159. `<iv>${sent_stanza.nodeTree.querySelector("iv").textContent}</iv>`+
  160. `</header>`+
  161. `<payload>${sent_stanza.nodeTree.querySelector("payload").textContent}</payload>`+
  162. `</encrypted>`+
  163. `<store xmlns="urn:xmpp:hints"/>`+
  164. `</message>`);
  165. // Test reception of an encrypted message
  166. let obj = await view.model.encryptMessage('This is an encrypted message from the contact')
  167. // XXX: Normally the key will be encrypted via libsignal.
  168. // However, we're mocking libsignal in the tests, so we include
  169. // it as plaintext in the message.
  170. stanza = $msg({
  171. 'from': contact_jid,
  172. 'to': _converse.connection.jid,
  173. 'type': 'chat',
  174. 'id': _converse.connection.getUniqueId()
  175. }).c('body').t('This is a fallback message').up()
  176. .c('encrypted', {'xmlns': Strophe.NS.OMEMO})
  177. .c('header', {'sid': '555'})
  178. .c('key', {'rid': _converse.omemo_store.get('device_id')}).t(u.arrayBufferToBase64(obj.key_and_tag)).up()
  179. .c('iv').t(obj.iv)
  180. .up().up()
  181. .c('payload').t(obj.payload);
  182. _converse.connection._dataRecv(mock.createRequest(stanza));
  183. await new Promise(resolve => view.model.messages.once('rendered', resolve));
  184. expect(view.model.messages.length).toBe(2);
  185. expect(view.el.querySelectorAll('.chat-msg__body')[1].textContent.trim())
  186. .toBe('This is an encrypted message from the contact');
  187. // #1193 Check for a received message without <body> tag
  188. obj = await view.model.encryptMessage('Another received encrypted message without fallback')
  189. stanza = $msg({
  190. 'from': contact_jid,
  191. 'to': _converse.connection.jid,
  192. 'type': 'chat',
  193. 'id': _converse.connection.getUniqueId()
  194. }).c('encrypted', {'xmlns': Strophe.NS.OMEMO})
  195. .c('header', {'sid': '555'})
  196. .c('key', {'rid': _converse.omemo_store.get('device_id')}).t(u.arrayBufferToBase64(obj.key_and_tag)).up()
  197. .c('iv').t(obj.iv)
  198. .up().up()
  199. .c('payload').t(obj.payload);
  200. _converse.connection._dataRecv(mock.createRequest(stanza));
  201. await new Promise(resolve => view.model.messages.once('rendered', resolve));
  202. await u.waitUntil(() => view.model.messages.length > 1);
  203. expect(view.model.messages.length).toBe(3);
  204. expect(view.el.querySelectorAll('.chat-msg__body')[2].textContent.trim())
  205. .toBe('Another received encrypted message without fallback');
  206. done();
  207. }));
  208. it("enables encrypted groupchat messages to be sent and received",
  209. mock.initConverse(
  210. ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  211. async function (done, _converse) {
  212. // MEMO encryption works only in members only conferences
  213. // that are non-anonymous.
  214. const features = [
  215. 'http://jabber.org/protocol/muc',
  216. 'jabber:iq:register',
  217. 'muc_passwordprotected',
  218. 'muc_hidden',
  219. 'muc_temporary',
  220. 'muc_membersonly',
  221. 'muc_unmoderated',
  222. 'muc_nonanonymous'
  223. ];
  224. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo', features);
  225. const view = _converse.chatboxviews.get('lounge@montague.lit');
  226. await u.waitUntil(() => initializedOMEMO(_converse));
  227. const toolbar = view.el.querySelector('.chat-toolbar');
  228. let toggle = toolbar.querySelector('.toggle-omemo');
  229. toggle.click();
  230. expect(view.model.get('omemo_active')).toBe(true);
  231. // newguy enters the room
  232. const contact_jid = 'newguy@montague.lit';
  233. let stanza = $pres({
  234. 'to': 'romeo@montague.lit/orchard',
  235. 'from': 'lounge@montague.lit/newguy'
  236. })
  237. .c('x', {xmlns: Strophe.NS.MUC_USER})
  238. .c('item', {
  239. 'affiliation': 'none',
  240. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  241. 'role': 'participant'
  242. }).tree();
  243. _converse.connection._dataRecv(mock.createRequest(stanza));
  244. // Wait for Converse to fetch newguy's device list
  245. let iq_stanza = await u.waitUntil(() => deviceListFetched(_converse, contact_jid));
  246. expect(Strophe.serialize(iq_stanza)).toBe(
  247. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="${contact_jid}" type="get" xmlns="jabber:client">`+
  248. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  249. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  250. `</pubsub>`+
  251. `</iq>`);
  252. // The server returns his device list
  253. stanza = $iq({
  254. 'from': contact_jid,
  255. 'id': iq_stanza.getAttribute('id'),
  256. 'to': _converse.bare_jid,
  257. 'type': 'result',
  258. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  259. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  260. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  261. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  262. .c('device', {'id': '4e30f35051b7b8b42abe083742187228'}).up()
  263. _converse.connection._dataRecv(mock.createRequest(stanza));
  264. await u.waitUntil(() => _converse.omemo_store);
  265. expect(_converse.devicelists.length).toBe(2);
  266. await u.waitUntil(() => deviceListFetched(_converse, contact_jid));
  267. const devicelist = _converse.devicelists.get(contact_jid);
  268. expect(devicelist.devices.length).toBe(1);
  269. expect(devicelist.devices.at(0).get('id')).toBe('4e30f35051b7b8b42abe083742187228');
  270. toggle = toolbar.querySelector('.toggle-omemo');
  271. expect(view.model.get('omemo_active')).toBe(true);
  272. expect(u.hasClass('fa-unlock', toggle)).toBe(false);
  273. expect(u.hasClass('fa-lock', toggle)).toBe(true);
  274. const textarea = view.el.querySelector('.chat-textarea');
  275. textarea.value = 'This message will be encrypted';
  276. view.onKeyDown({
  277. target: textarea,
  278. preventDefault: function preventDefault () {},
  279. keyCode: 13 // Enter
  280. });
  281. iq_stanza = await u.waitUntil(() => bundleFetched(_converse, contact_jid, '4e30f35051b7b8b42abe083742187228'), 1000);
  282. console.log("Bundle fetched 4e30f35051b7b8b42abe083742187228");
  283. stanza = $iq({
  284. 'from': contact_jid,
  285. 'id': iq_stanza.getAttribute('id'),
  286. 'to': _converse.bare_jid,
  287. 'type': 'result',
  288. }).c('pubsub', {
  289. 'xmlns': 'http://jabber.org/protocol/pubsub'
  290. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:4e30f35051b7b8b42abe083742187228"})
  291. .c('item')
  292. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  293. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('1111')).up()
  294. .c('signedPreKeySignature').t(btoa('2222')).up()
  295. .c('identityKey').t(btoa('3333')).up()
  296. .c('prekeys')
  297. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1001')).up()
  298. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1002')).up()
  299. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1003'));
  300. _converse.connection._dataRecv(mock.createRequest(stanza));
  301. iq_stanza = await u.waitUntil(() => bundleFetched(_converse, _converse.bare_jid, '482886413b977930064a5888b92134fe'), 1000);
  302. console.log("Bundle fetched 482886413b977930064a5888b92134fe");
  303. stanza = $iq({
  304. 'from': _converse.bare_jid,
  305. 'id': iq_stanza.getAttribute('id'),
  306. 'to': _converse.bare_jid,
  307. 'type': 'result',
  308. }).c('pubsub', {
  309. 'xmlns': 'http://jabber.org/protocol/pubsub'
  310. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:482886413b977930064a5888b92134fe"})
  311. .c('item')
  312. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  313. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('100000')).up()
  314. .c('signedPreKeySignature').t(btoa('200000')).up()
  315. .c('identityKey').t(btoa('300000')).up()
  316. .c('prekeys')
  317. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1991')).up()
  318. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1992')).up()
  319. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1993'));
  320. spyOn(_converse.connection, 'send');
  321. _converse.connection._dataRecv(mock.createRequest(stanza));
  322. await u.waitUntil(() => _converse.connection.send.calls.count(), 1000);
  323. const sent_stanza = _converse.connection.send.calls.all()[0].args[0];
  324. expect(Strophe.serialize(sent_stanza)).toBe(
  325. `<message from="romeo@montague.lit/orchard" `+
  326. `id="${sent_stanza.nodeTree.getAttribute("id")}" `+
  327. `to="lounge@montague.lit" `+
  328. `type="groupchat" `+
  329. `xmlns="jabber:client">`+
  330. `<body>This is an OMEMO encrypted message which your client doesn’t seem to support. Find more information on https://conversations.im/omemo</body>`+
  331. `<encrypted xmlns="eu.siacs.conversations.axolotl">`+
  332. `<header sid="123456789">`+
  333. `<key rid="482886413b977930064a5888b92134fe">YzFwaDNSNzNYNw==</key>`+
  334. `<key rid="4e30f35051b7b8b42abe083742187228">YzFwaDNSNzNYNw==</key>`+
  335. `<iv>${sent_stanza.nodeTree.querySelector("iv").textContent}</iv>`+
  336. `</header>`+
  337. `<payload>${sent_stanza.nodeTree.querySelector("payload").textContent}</payload>`+
  338. `</encrypted>`+
  339. `<store xmlns="urn:xmpp:hints"/>`+
  340. `</message>`);
  341. done();
  342. }));
  343. it("will create a new device based on a received carbon message",
  344. mock.initConverse(
  345. ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  346. async function (done, _converse) {
  347. await mock.waitUntilDiscoConfirmed(_converse, _converse.bare_jid, [], [Strophe.NS.SID]);
  348. await mock.waitForRoster(_converse, 'current', 1);
  349. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  350. await u.waitUntil(() => initializedOMEMO(_converse));
  351. await mock.openChatBoxFor(_converse, contact_jid);
  352. let iq_stanza = await u.waitUntil(() => deviceListFetched(_converse, contact_jid));
  353. const stanza = $iq({
  354. 'from': contact_jid,
  355. 'id': iq_stanza.getAttribute('id'),
  356. 'to': _converse.connection.jid,
  357. 'type': 'result',
  358. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  359. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  360. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  361. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  362. .c('device', {'id': '555'});
  363. _converse.connection._dataRecv(mock.createRequest(stanza));
  364. await u.waitUntil(() => _converse.omemo_store);
  365. const devicelist = _converse.devicelists.get({'jid': contact_jid});
  366. await u.waitUntil(() => devicelist.devices.length === 1);
  367. const view = _converse.chatboxviews.get(contact_jid);
  368. view.model.set('omemo_active', true);
  369. // Test reception of an encrypted carbon message
  370. const obj = await view.model.encryptMessage('This is an encrypted carbon message from another device of mine')
  371. const carbon = u.toStanza(`
  372. <message xmlns="jabber:client" to="romeo@montague.lit/orchard" from="romeo@montague.lit" type="chat">
  373. <sent xmlns="urn:xmpp:carbons:2">
  374. <forwarded xmlns="urn:xmpp:forward:0">
  375. <message xmlns="jabber:client"
  376. from="romeo@montague.lit/gajim.HE02SW1L"
  377. xml:lang="en"
  378. to="${contact_jid}/gajim.0LATM5V2"
  379. type="chat" id="87141781-61d6-4eb3-9a31-429935a61b76">
  380. <archived xmlns="urn:xmpp:mam:tmp" by="romeo@montague.lit" id="1554033877043470"/>
  381. <stanza-id xmlns="urn:xmpp:sid:0" by="romeo@montague.lit" id="1554033877043470"/>
  382. <request xmlns="urn:xmpp:receipts"/>
  383. <active xmlns="http://jabber.org/protocol/chatstates"/>
  384. <origin-id xmlns="urn:xmpp:sid:0" id="87141781-61d6-4eb3-9a31-429935a61b76"/>
  385. <encrypted xmlns="eu.siacs.conversations.axolotl">
  386. <header sid="988349631">
  387. <key rid="${_converse.omemo_store.get('device_id')}"
  388. prekey="true">${u.arrayBufferToBase64(obj.key_and_tag)}</key>
  389. <iv>${obj.iv}</iv>
  390. </header>
  391. <payload>${obj.payload}</payload>
  392. </encrypted>
  393. <encryption xmlns="urn:xmpp:eme:0" namespace="eu.siacs.conversations.axolotl" name="OMEMO"/>
  394. <store xmlns="urn:xmpp:hints"/>
  395. </message>
  396. </forwarded>
  397. </sent>
  398. </message>
  399. `);
  400. _converse.connection._dataRecv(mock.createRequest(carbon));
  401. await new Promise(resolve => view.model.messages.once('rendered', resolve));
  402. expect(view.model.messages.length).toBe(1);
  403. expect(view.el.querySelector('.chat-msg__body').textContent.trim())
  404. .toBe('This is an encrypted carbon message from another device of mine');
  405. expect(devicelist.devices.length).toBe(2);
  406. expect(devicelist.devices.at(0).get('id')).toBe('555');
  407. expect(devicelist.devices.at(1).get('id')).toBe('988349631');
  408. expect(devicelist.devices.get('988349631').get('active')).toBe(true);
  409. const textarea = view.el.querySelector('.chat-textarea');
  410. textarea.value = 'This is an encrypted message from this device';
  411. view.onKeyDown({
  412. target: textarea,
  413. preventDefault: function preventDefault () {},
  414. keyCode: 13 // Enter
  415. });
  416. iq_stanza = await u.waitUntil(() => bundleFetched(_converse, _converse.bare_jid, '988349631'));
  417. expect(Strophe.serialize(iq_stanza)).toBe(
  418. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="${_converse.bare_jid}" type="get" xmlns="jabber:client">`+
  419. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  420. `<items node="eu.siacs.conversations.axolotl.bundles:988349631"/>`+
  421. `</pubsub>`+
  422. `</iq>`);
  423. done();
  424. }));
  425. it("gracefully handles auth errors when trying to send encrypted groupchat messages",
  426. mock.initConverse(
  427. ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  428. async function (done, _converse) {
  429. // MEMO encryption works only in members only conferences
  430. // that are non-anonymous.
  431. const features = [
  432. 'http://jabber.org/protocol/muc',
  433. 'jabber:iq:register',
  434. 'muc_passwordprotected',
  435. 'muc_hidden',
  436. 'muc_temporary',
  437. 'muc_membersonly',
  438. 'muc_unmoderated',
  439. 'muc_nonanonymous'
  440. ];
  441. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo', features);
  442. const view = _converse.chatboxviews.get('lounge@montague.lit');
  443. await u.waitUntil(() => initializedOMEMO(_converse));
  444. const contact_jid = 'newguy@montague.lit';
  445. let stanza = $pres({
  446. 'to': 'romeo@montague.lit/orchard',
  447. 'from': 'lounge@montague.lit/newguy'
  448. })
  449. .c('x', {xmlns: Strophe.NS.MUC_USER})
  450. .c('item', {
  451. 'affiliation': 'none',
  452. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  453. 'role': 'participant'
  454. }).tree();
  455. _converse.connection._dataRecv(mock.createRequest(stanza));
  456. const toolbar = view.el.querySelector('.chat-toolbar');
  457. const toggle = toolbar.querySelector('.toggle-omemo');
  458. toggle.click();
  459. expect(view.model.get('omemo_active')).toBe(true);
  460. expect(view.model.get('omemo_supported')).toBe(true);
  461. const textarea = view.el.querySelector('.chat-textarea');
  462. textarea.value = 'This message will be encrypted';
  463. view.onKeyDown({
  464. target: textarea,
  465. preventDefault: function preventDefault () {},
  466. keyCode: 13 // Enter
  467. });
  468. let iq_stanza = await u.waitUntil(() => deviceListFetched(_converse, contact_jid));
  469. expect(Strophe.serialize(iq_stanza)).toBe(
  470. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="${contact_jid}" type="get" xmlns="jabber:client">`+
  471. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  472. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  473. `</pubsub>`+
  474. `</iq>`);
  475. stanza = $iq({
  476. 'from': contact_jid,
  477. 'id': iq_stanza.getAttribute('id'),
  478. 'to': _converse.bare_jid,
  479. 'type': 'result',
  480. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  481. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  482. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  483. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  484. .c('device', {'id': '4e30f35051b7b8b42abe083742187228'}).up()
  485. _converse.connection._dataRecv(mock.createRequest(stanza));
  486. await u.waitUntil(() => _converse.omemo_store);
  487. expect(_converse.devicelists.length).toBe(2);
  488. const devicelist = _converse.devicelists.get(contact_jid);
  489. await u.waitUntil(() => deviceListFetched(_converse, contact_jid));
  490. expect(devicelist.devices.length).toBe(1);
  491. expect(devicelist.devices.at(0).get('id')).toBe('4e30f35051b7b8b42abe083742187228');
  492. iq_stanza = await u.waitUntil(() => bundleFetched(_converse, _converse.bare_jid, '482886413b977930064a5888b92134fe'));
  493. stanza = $iq({
  494. 'from': _converse.bare_jid,
  495. 'id': iq_stanza.getAttribute('id'),
  496. 'to': _converse.bare_jid,
  497. 'type': 'result',
  498. }).c('pubsub', {
  499. 'xmlns': 'http://jabber.org/protocol/pubsub'
  500. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:482886413b977930064a5888b92134fe"})
  501. .c('item')
  502. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  503. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('100000')).up()
  504. .c('signedPreKeySignature').t(btoa('200000')).up()
  505. .c('identityKey').t(btoa('300000')).up()
  506. .c('prekeys')
  507. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1991')).up()
  508. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1992')).up()
  509. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1993'));
  510. iq_stanza = await u.waitUntil(() => bundleFetched(_converse, contact_jid, '4e30f35051b7b8b42abe083742187228'));
  511. /* <iq xmlns="jabber:client" to="jc@opkode.com/converse.js-34183907" type="error" id="945c8ab3-b561-4d8a-92da-77c226bb1689:sendIQ" from="joris@konuro.net">
  512. * <pubsub xmlns="http://jabber.org/protocol/pubsub">
  513. * <items node="eu.siacs.conversations.axolotl.bundles:7580"/>
  514. * </pubsub>
  515. * <error code="401" type="auth">
  516. * <presence-subscription-required xmlns="http://jabber.org/protocol/pubsub#errors"/>
  517. * <not-authorized xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
  518. * </error>
  519. * </iq>
  520. */
  521. stanza = $iq({
  522. 'from': contact_jid,
  523. 'id': iq_stanza.getAttribute('id'),
  524. 'to': _converse.bare_jid,
  525. 'type': 'result',
  526. }).c('pubsub', {'xmlns': 'http://jabber.org/protocol/pubsub'})
  527. .c('items', {'node': "eu.siacs.conversations.axolotl.bundles:4e30f35051b7b8b42abe083742187228"}).up().up()
  528. .c('error', {'code': '401', 'type': 'auth'})
  529. .c('presence-subscription-required', {'xmlns':"http://jabber.org/protocol/pubsub#errors" }).up()
  530. .c('not-authorized', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  531. _converse.connection._dataRecv(mock.createRequest(stanza));
  532. await u.waitUntil(() => document.querySelectorAll('.alert-danger').length, 2000);
  533. const header = document.querySelector('.alert-danger .modal-title');
  534. expect(header.textContent).toBe("Error");
  535. expect(u.ancestor(header, '.modal-content').querySelector('.modal-body p').textContent.trim())
  536. .toBe("Sorry, we're unable to send an encrypted message because newguy@montague.lit requires you "+
  537. "to be subscribed to their presence in order to see their OMEMO information");
  538. expect(view.model.get('omemo_supported')).toBe(false);
  539. expect(view.el.querySelector('.chat-textarea').value).toBe('This message will be encrypted');
  540. done();
  541. }));
  542. it("can receive a PreKeySignalMessage",
  543. mock.initConverse(
  544. ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  545. async function (done, _converse) {
  546. _converse.NUM_PREKEYS = 5; // Restrict to 5, otherwise the resulting stanza is too large to easily test
  547. await mock.waitForRoster(_converse, 'current', 1);
  548. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  549. await u.waitUntil(() => initializedOMEMO(_converse));
  550. const obj = await _converse.ChatBox.prototype.encryptMessage('This is an encrypted message from the contact');
  551. // XXX: Normally the key will be encrypted via libsignal.
  552. // However, we're mocking libsignal in the tests, so we include
  553. // it as plaintext in the message.
  554. let stanza = $msg({
  555. 'from': contact_jid,
  556. 'to': _converse.connection.jid,
  557. 'type': 'chat',
  558. 'id': 'qwerty'
  559. }).c('body').t('This is a fallback message').up()
  560. .c('encrypted', {'xmlns': Strophe.NS.OMEMO})
  561. .c('header', {'sid': '555'})
  562. .c('key', {
  563. 'prekey': 'true',
  564. 'rid': _converse.omemo_store.get('device_id')
  565. }).t(u.arrayBufferToBase64(obj.key_and_tag)).up()
  566. .c('iv').t(obj.iv)
  567. .up().up()
  568. .c('payload').t(obj.payload);
  569. const generateMissingPreKeys = _converse.omemo_store.generateMissingPreKeys;
  570. spyOn(_converse.omemo_store, 'generateMissingPreKeys').and.callFake(() => {
  571. // Since it's difficult to override
  572. // decryptPreKeyWhisperMessage, where a prekey will be
  573. // removed from the store, we do it here, before the
  574. // missing prekeys are generated.
  575. _converse.omemo_store.removePreKey(1);
  576. return generateMissingPreKeys.apply(_converse.omemo_store, arguments);
  577. });
  578. _converse.connection._dataRecv(mock.createRequest(stanza));
  579. let iq_stanza = await u.waitUntil(() => _converse.chatboxviews.get(contact_jid));
  580. iq_stanza = await deviceListFetched(_converse, contact_jid);
  581. stanza = $iq({
  582. 'from': contact_jid,
  583. 'id': iq_stanza.getAttribute('id'),
  584. 'to': _converse.connection.jid,
  585. 'type': 'result',
  586. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  587. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  588. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  589. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  590. .c('device', {'id': '555'});
  591. // XXX: the bundle gets published twice, we want to make sure
  592. // that we wait for the 2nd, so we clear all the already sent
  593. // stanzas.
  594. _converse.connection.IQ_stanzas = [];
  595. _converse.connection._dataRecv(mock.createRequest(stanza));
  596. await u.waitUntil(() => _converse.omemo_store);
  597. iq_stanza = await u.waitUntil(() => bundleHasBeenPublished(_converse));
  598. expect(Strophe.serialize(iq_stanza)).toBe(
  599. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" type="set" xmlns="jabber:client">`+
  600. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  601. `<publish node="eu.siacs.conversations.axolotl.bundles:123456789">`+
  602. `<item>`+
  603. `<bundle xmlns="eu.siacs.conversations.axolotl">`+
  604. `<signedPreKeyPublic signedPreKeyId="0">${btoa("1234")}</signedPreKeyPublic>`+
  605. `<signedPreKeySignature>${btoa("11112222333344445555")}</signedPreKeySignature>`+
  606. `<identityKey>${btoa("1234")}</identityKey>`+
  607. `<prekeys>`+
  608. `<preKeyPublic preKeyId="0">${btoa("1234")}</preKeyPublic>`+
  609. `<preKeyPublic preKeyId="1">${btoa("1234")}</preKeyPublic>`+
  610. `<preKeyPublic preKeyId="2">${btoa("1234")}</preKeyPublic>`+
  611. `<preKeyPublic preKeyId="3">${btoa("1234")}</preKeyPublic>`+
  612. `<preKeyPublic preKeyId="4">${btoa("1234")}</preKeyPublic>`+
  613. `</prekeys>`+
  614. `</bundle>`+
  615. `</item>`+
  616. `</publish>`+
  617. `<publish-options>`+
  618. `<x type="submit" xmlns="jabber:x:data">`+
  619. `<field type="hidden" var="FORM_TYPE">`+
  620. `<value>http://jabber.org/protocol/pubsub#publish-options</value>`+
  621. `</field>`+
  622. `<field var="pubsub#access_model">`+
  623. `<value>open</value>`+
  624. `</field>`+
  625. `</x>`+
  626. `</publish-options>`+
  627. `</pubsub>`+
  628. `</iq>`)
  629. const own_device = _converse.devicelists.get(_converse.bare_jid).devices.get(_converse.omemo_store.get('device_id'));
  630. expect(own_device.get('bundle').prekeys.length).toBe(5);
  631. expect(_converse.omemo_store.generateMissingPreKeys).toHaveBeenCalled();
  632. done();
  633. }));
  634. it("updates device lists based on PEP messages",
  635. mock.initConverse(
  636. ['rosterGroupsFetched'], {'allow_non_roster_messaging': true},
  637. async function (done, _converse) {
  638. await mock.waitUntilDiscoConfirmed(
  639. _converse, _converse.bare_jid,
  640. [{'category': 'pubsub', 'type': 'pep'}],
  641. ['http://jabber.org/protocol/pubsub#publish-options']
  642. );
  643. await mock.waitForRoster(_converse, 'current', 1);
  644. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  645. // Wait until own devices are fetched
  646. let iq_stanza = await u.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid));
  647. expect(Strophe.serialize(iq_stanza)).toBe(
  648. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="romeo@montague.lit" type="get" xmlns="jabber:client">`+
  649. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  650. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  651. `</pubsub>`+
  652. `</iq>`);
  653. let stanza = $iq({
  654. 'from': _converse.bare_jid,
  655. 'id': iq_stanza.getAttribute('id'),
  656. 'to': _converse.bare_jid,
  657. 'type': 'result',
  658. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  659. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  660. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  661. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  662. .c('device', {'id': '555'});
  663. _converse.connection._dataRecv(mock.createRequest(stanza));
  664. await u.waitUntil(() => _converse.omemo_store);
  665. expect(_converse.chatboxes.length).toBe(1);
  666. expect(_converse.devicelists.length).toBe(1);
  667. const devicelist = _converse.devicelists.get(_converse.bare_jid);
  668. expect(devicelist.devices.length).toBe(2);
  669. expect(devicelist.devices.at(0).get('id')).toBe('555');
  670. expect(devicelist.devices.at(1).get('id')).toBe('123456789');
  671. iq_stanza = await u.waitUntil(() => ownDeviceHasBeenPublished(_converse));
  672. stanza = $iq({
  673. 'from': _converse.bare_jid,
  674. 'id': iq_stanza.getAttribute('id'),
  675. 'to': _converse.bare_jid,
  676. 'type': 'result'});
  677. _converse.connection._dataRecv(mock.createRequest(stanza));
  678. iq_stanza = await u.waitUntil(() => bundleHasBeenPublished(_converse));
  679. stanza = $iq({
  680. 'from': _converse.bare_jid,
  681. 'id': iq_stanza.getAttribute('id'),
  682. 'to': _converse.bare_jid,
  683. 'type': 'result'});
  684. _converse.connection._dataRecv(mock.createRequest(stanza));
  685. await _converse.api.waitUntil('OMEMOInitialized');
  686. stanza = $msg({
  687. 'from': contact_jid,
  688. 'to': _converse.bare_jid,
  689. 'type': 'headline',
  690. 'id': 'update_01',
  691. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  692. .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
  693. .c('item')
  694. .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
  695. .c('device', {'id': '1234'})
  696. .c('device', {'id': '4223'})
  697. _converse.connection._dataRecv(mock.createRequest(stanza));
  698. expect(_converse.devicelists.length).toBe(2);
  699. let devices = _converse.devicelists.get(contact_jid).devices;
  700. expect(devices.length).toBe(2);
  701. expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('1234,4223');
  702. stanza = $msg({
  703. 'from': contact_jid,
  704. 'to': _converse.bare_jid,
  705. 'type': 'headline',
  706. 'id': 'update_02',
  707. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  708. .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
  709. .c('item')
  710. .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
  711. .c('device', {'id': '4223'})
  712. .c('device', {'id': '4224'})
  713. _converse.connection._dataRecv(mock.createRequest(stanza));
  714. expect(_converse.devicelists.length).toBe(2);
  715. expect(devices.length).toBe(3);
  716. expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('1234,4223,4224');
  717. expect(devices.get('1234').get('active')).toBe(false);
  718. expect(devices.get('4223').get('active')).toBe(true);
  719. expect(devices.get('4224').get('active')).toBe(true);
  720. // Check that own devicelist gets updated
  721. stanza = $msg({
  722. 'from': _converse.bare_jid,
  723. 'to': _converse.bare_jid,
  724. 'type': 'headline',
  725. 'id': 'update_03',
  726. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  727. .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
  728. .c('item')
  729. .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
  730. .c('device', {'id': '123456789'})
  731. .c('device', {'id': '555'})
  732. .c('device', {'id': '777'})
  733. _converse.connection._dataRecv(mock.createRequest(stanza));
  734. expect(_converse.devicelists.length).toBe(2);
  735. devices = _converse.devicelists.get(_converse.bare_jid).devices;
  736. expect(devices.length).toBe(3);
  737. expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('123456789,555,777');
  738. expect(devices.get('123456789').get('active')).toBe(true);
  739. expect(devices.get('555').get('active')).toBe(true);
  740. expect(devices.get('777').get('active')).toBe(true);
  741. _converse.connection.IQ_stanzas = [];
  742. // Check that own device gets re-added
  743. stanza = $msg({
  744. 'from': _converse.bare_jid,
  745. 'to': _converse.bare_jid,
  746. 'type': 'headline',
  747. 'id': 'update_04',
  748. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  749. .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
  750. .c('item')
  751. .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
  752. .c('device', {'id': '444'})
  753. _converse.connection._dataRecv(mock.createRequest(stanza));
  754. iq_stanza = await u.waitUntil(() => ownDeviceHasBeenPublished(_converse));
  755. // Check that our own device is added again, but that removed
  756. // devices are not added.
  757. expect(Strophe.serialize(iq_stanza)).toBe(
  758. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute(`id`)}" type="set" xmlns="jabber:client">`+
  759. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  760. `<publish node="eu.siacs.conversations.axolotl.devicelist">`+
  761. `<item>`+
  762. `<list xmlns="eu.siacs.conversations.axolotl">`+
  763. `<device id="123456789"/>`+
  764. `<device id="444"/>`+
  765. `</list>`+
  766. `</item>`+
  767. `</publish>`+
  768. `<publish-options>`+
  769. `<x type="submit" xmlns="jabber:x:data">`+
  770. `<field type="hidden" var="FORM_TYPE">`+
  771. `<value>http://jabber.org/protocol/pubsub#publish-options</value>`+
  772. `</field>`+
  773. `<field var="pubsub#access_model">`+
  774. `<value>open</value>`+
  775. `</field>`+
  776. `</x>`+
  777. `</publish-options>`+
  778. `</pubsub>`+
  779. `</iq>`);
  780. expect(_converse.devicelists.length).toBe(2);
  781. devices = _converse.devicelists.get(_converse.bare_jid).devices;
  782. // The device id for this device (123456789) was also generated and added to the list,
  783. // which is why we have 2 devices now.
  784. expect(devices.length).toBe(4);
  785. expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('123456789,444,555,777');
  786. expect(devices.get('123456789').get('active')).toBe(true);
  787. expect(devices.get('444').get('active')).toBe(true);
  788. expect(devices.get('555').get('active')).toBe(false);
  789. expect(devices.get('777').get('active')).toBe(false);
  790. done();
  791. }));
  792. it("updates device bundles based on PEP messages",
  793. mock.initConverse(
  794. ['rosterGroupsFetched'], {},
  795. async function (done, _converse) {
  796. await mock.waitUntilDiscoConfirmed(
  797. _converse, _converse.bare_jid,
  798. [{'category': 'pubsub', 'type': 'pep'}],
  799. ['http://jabber.org/protocol/pubsub#publish-options']
  800. );
  801. await mock.waitForRoster(_converse, 'current');
  802. const contact_jid = mock.cur_names[3].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  803. let iq_stanza = await u.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid));
  804. expect(Strophe.serialize(iq_stanza)).toBe(
  805. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="romeo@montague.lit" type="get" xmlns="jabber:client">`+
  806. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  807. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  808. `</pubsub>`+
  809. `</iq>`);
  810. let stanza = $iq({
  811. 'from': contact_jid,
  812. 'id': iq_stanza.getAttribute('id'),
  813. 'to': _converse.bare_jid,
  814. 'type': 'result',
  815. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  816. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  817. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  818. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  819. .c('device', {'id': '555'});
  820. _converse.connection._dataRecv(mock.createRequest(stanza));
  821. await await u.waitUntil(() => _converse.omemo_store);
  822. expect(_converse.devicelists.length).toBe(1);
  823. let devicelist = _converse.devicelists.get(_converse.bare_jid);
  824. expect(devicelist.devices.length).toBe(2);
  825. expect(devicelist.devices.at(0).get('id')).toBe('555');
  826. expect(devicelist.devices.at(1).get('id')).toBe('123456789');
  827. iq_stanza = await u.waitUntil(() => ownDeviceHasBeenPublished(_converse));
  828. stanza = $iq({
  829. 'from': _converse.bare_jid,
  830. 'id': iq_stanza.getAttribute('id'),
  831. 'to': _converse.bare_jid,
  832. 'type': 'result'});
  833. _converse.connection._dataRecv(mock.createRequest(stanza));
  834. iq_stanza = await u.waitUntil(() => bundleHasBeenPublished(_converse));
  835. stanza = $iq({
  836. 'from': _converse.bare_jid,
  837. 'id': iq_stanza.getAttribute('id'),
  838. 'to': _converse.bare_jid,
  839. 'type': 'result'});
  840. _converse.connection._dataRecv(mock.createRequest(stanza));
  841. await _converse.api.waitUntil('OMEMOInitialized');
  842. stanza = $msg({
  843. 'from': contact_jid,
  844. 'to': _converse.bare_jid,
  845. 'type': 'headline',
  846. 'id': 'update_01',
  847. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  848. .c('items', {'node': 'eu.siacs.conversations.axolotl.bundles:555'})
  849. .c('item')
  850. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  851. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t('1111').up()
  852. .c('signedPreKeySignature').t('2222').up()
  853. .c('identityKey').t('3333').up()
  854. .c('prekeys')
  855. .c('preKeyPublic', {'preKeyId': '1001'}).up()
  856. .c('preKeyPublic', {'preKeyId': '1002'}).up()
  857. .c('preKeyPublic', {'preKeyId': '1003'});
  858. _converse.connection._dataRecv(mock.createRequest(stanza));
  859. expect(_converse.devicelists.length).toBe(2);
  860. devicelist = _converse.devicelists.get(contact_jid);
  861. expect(devicelist.devices.length).toBe(1);
  862. let device = devicelist.devices.at(0);
  863. expect(device.get('bundle').identity_key).toBe('3333');
  864. expect(device.get('bundle').signed_prekey.public_key).toBe('1111');
  865. expect(device.get('bundle').signed_prekey.id).toBe(4223);
  866. expect(device.get('bundle').signed_prekey.signature).toBe('2222');
  867. expect(device.get('bundle').prekeys.length).toBe(3);
  868. expect(device.get('bundle').prekeys[0].id).toBe(1001);
  869. expect(device.get('bundle').prekeys[1].id).toBe(1002);
  870. expect(device.get('bundle').prekeys[2].id).toBe(1003);
  871. stanza = $msg({
  872. 'from': contact_jid,
  873. 'to': _converse.bare_jid,
  874. 'type': 'headline',
  875. 'id': 'update_02',
  876. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  877. .c('items', {'node': 'eu.siacs.conversations.axolotl.bundles:555'})
  878. .c('item')
  879. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  880. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t('5555').up()
  881. .c('signedPreKeySignature').t('6666').up()
  882. .c('identityKey').t('7777').up()
  883. .c('prekeys')
  884. .c('preKeyPublic', {'preKeyId': '2001'}).up()
  885. .c('preKeyPublic', {'preKeyId': '2002'}).up()
  886. .c('preKeyPublic', {'preKeyId': '2003'});
  887. _converse.connection._dataRecv(mock.createRequest(stanza));
  888. expect(_converse.devicelists.length).toBe(2);
  889. devicelist = _converse.devicelists.get(contact_jid);
  890. expect(devicelist.devices.length).toBe(1);
  891. device = devicelist.devices.at(0);
  892. expect(device.get('bundle').identity_key).toBe('7777');
  893. expect(device.get('bundle').signed_prekey.public_key).toBe('5555');
  894. expect(device.get('bundle').signed_prekey.id).toBe(4223);
  895. expect(device.get('bundle').signed_prekey.signature).toBe('6666');
  896. expect(device.get('bundle').prekeys.length).toBe(3);
  897. expect(device.get('bundle').prekeys[0].id).toBe(2001);
  898. expect(device.get('bundle').prekeys[1].id).toBe(2002);
  899. expect(device.get('bundle').prekeys[2].id).toBe(2003);
  900. stanza = $msg({
  901. 'from': _converse.bare_jid,
  902. 'to': _converse.bare_jid,
  903. 'type': 'headline',
  904. 'id': 'update_03',
  905. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  906. .c('items', {'node': 'eu.siacs.conversations.axolotl.bundles:123456789'})
  907. .c('item')
  908. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  909. .c('signedPreKeyPublic', {'signedPreKeyId': '9999'}).t('8888').up()
  910. .c('signedPreKeySignature').t('3333').up()
  911. .c('identityKey').t('1111').up()
  912. .c('prekeys')
  913. .c('preKeyPublic', {'preKeyId': '3001'}).up()
  914. .c('preKeyPublic', {'preKeyId': '3002'}).up()
  915. .c('preKeyPublic', {'preKeyId': '3003'});
  916. _converse.connection._dataRecv(mock.createRequest(stanza));
  917. expect(_converse.devicelists.length).toBe(2);
  918. devicelist = _converse.devicelists.get(_converse.bare_jid);
  919. expect(devicelist.devices.length).toBe(2);
  920. expect(devicelist.devices.at(0).get('id')).toBe('555');
  921. expect(devicelist.devices.at(1).get('id')).toBe('123456789');
  922. device = devicelist.devices.at(1);
  923. expect(device.get('bundle').identity_key).toBe('1111');
  924. expect(device.get('bundle').signed_prekey.public_key).toBe('8888');
  925. expect(device.get('bundle').signed_prekey.id).toBe(9999);
  926. expect(device.get('bundle').signed_prekey.signature).toBe('3333');
  927. expect(device.get('bundle').prekeys.length).toBe(3);
  928. expect(device.get('bundle').prekeys[0].id).toBe(3001);
  929. expect(device.get('bundle').prekeys[1].id).toBe(3002);
  930. expect(device.get('bundle').prekeys[2].id).toBe(3003);
  931. done();
  932. }));
  933. it("publishes a bundle with which an encrypted session can be created",
  934. mock.initConverse(
  935. ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  936. async function (done, _converse) {
  937. await mock.waitUntilDiscoConfirmed(
  938. _converse, _converse.bare_jid,
  939. [{'category': 'pubsub', 'type': 'pep'}],
  940. ['http://jabber.org/protocol/pubsub#publish-options']
  941. );
  942. _converse.NUM_PREKEYS = 2; // Restrict to 2, otherwise the resulting stanza is too large to easily test
  943. await mock.waitForRoster(_converse, 'current', 1);
  944. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  945. let iq_stanza = await u.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid));
  946. let stanza = $iq({
  947. 'from': contact_jid,
  948. 'id': iq_stanza.getAttribute('id'),
  949. 'to': _converse.bare_jid,
  950. 'type': 'result',
  951. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  952. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  953. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  954. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  955. .c('device', {'id': '482886413b977930064a5888b92134fe'});
  956. _converse.connection._dataRecv(mock.createRequest(stanza));
  957. expect(_converse.devicelists.length).toBe(1);
  958. await mock.openChatBoxFor(_converse, contact_jid);
  959. iq_stanza = await ownDeviceHasBeenPublished(_converse);
  960. stanza = $iq({
  961. 'from': _converse.bare_jid,
  962. 'id': iq_stanza.getAttribute('id'),
  963. 'to': _converse.bare_jid,
  964. 'type': 'result'});
  965. _converse.connection._dataRecv(mock.createRequest(stanza));
  966. iq_stanza = await u.waitUntil(() => bundleHasBeenPublished(_converse));
  967. expect(Strophe.serialize(iq_stanza)).toBe(
  968. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" type="set" xmlns="jabber:client">`+
  969. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  970. `<publish node="eu.siacs.conversations.axolotl.bundles:123456789">`+
  971. `<item>`+
  972. `<bundle xmlns="eu.siacs.conversations.axolotl">`+
  973. `<signedPreKeyPublic signedPreKeyId="0">${btoa("1234")}</signedPreKeyPublic>`+
  974. `<signedPreKeySignature>${btoa("11112222333344445555")}</signedPreKeySignature>`+
  975. `<identityKey>${btoa("1234")}</identityKey>`+
  976. `<prekeys>`+
  977. `<preKeyPublic preKeyId="0">${btoa("1234")}</preKeyPublic>`+
  978. `<preKeyPublic preKeyId="1">${btoa("1234")}</preKeyPublic>`+
  979. `</prekeys>`+
  980. `</bundle>`+
  981. `</item>`+
  982. `</publish>`+
  983. `<publish-options>`+
  984. `<x type="submit" xmlns="jabber:x:data">`+
  985. `<field type="hidden" var="FORM_TYPE">`+
  986. `<value>http://jabber.org/protocol/pubsub#publish-options</value>`+
  987. `</field>`+
  988. `<field var="pubsub#access_model">`+
  989. `<value>open</value>`+
  990. `</field>`+
  991. `</x>`+
  992. `</publish-options>`+
  993. `</pubsub>`+
  994. `</iq>`)
  995. stanza = $iq({
  996. 'from': _converse.bare_jid,
  997. 'id': iq_stanza.getAttribute('id'),
  998. 'to': _converse.bare_jid,
  999. 'type': 'result'});
  1000. _converse.connection._dataRecv(mock.createRequest(stanza));
  1001. await _converse.api.waitUntil('OMEMOInitialized');
  1002. done();
  1003. }));
  1004. it("adds a toolbar button for starting an encrypted chat session",
  1005. mock.initConverse(
  1006. ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  1007. async function (done, _converse) {
  1008. await mock.waitUntilDiscoConfirmed(
  1009. _converse, _converse.bare_jid,
  1010. [{'category': 'pubsub', 'type': 'pep'}],
  1011. ['http://jabber.org/protocol/pubsub#publish-options']
  1012. );
  1013. await mock.waitForRoster(_converse, 'current', 1);
  1014. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  1015. let iq_stanza = await u.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid));
  1016. expect(Strophe.serialize(iq_stanza)).toBe(
  1017. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="romeo@montague.lit" type="get" xmlns="jabber:client">`+
  1018. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  1019. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  1020. `</pubsub>`+
  1021. `</iq>`);
  1022. let stanza = $iq({
  1023. 'from': _converse.bare_jid,
  1024. 'id': iq_stanza.getAttribute('id'),
  1025. 'to': _converse.bare_jid,
  1026. 'type': 'result',
  1027. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  1028. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  1029. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  1030. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  1031. .c('device', {'id': '482886413b977930064a5888b92134fe'});
  1032. _converse.connection._dataRecv(mock.createRequest(stanza));
  1033. await u.waitUntil(() => _converse.omemo_store);
  1034. expect(_converse.devicelists.length).toBe(1);
  1035. let devicelist = _converse.devicelists.get(_converse.bare_jid);
  1036. expect(devicelist.devices.length).toBe(2);
  1037. expect(devicelist.devices.at(0).get('id')).toBe('482886413b977930064a5888b92134fe');
  1038. expect(devicelist.devices.at(1).get('id')).toBe('123456789');
  1039. // Check that own device was published
  1040. iq_stanza = await u.waitUntil(() => ownDeviceHasBeenPublished(_converse));
  1041. expect(Strophe.serialize(iq_stanza)).toBe(
  1042. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute(`id`)}" type="set" xmlns="jabber:client">`+
  1043. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  1044. `<publish node="eu.siacs.conversations.axolotl.devicelist">`+
  1045. `<item>`+
  1046. `<list xmlns="eu.siacs.conversations.axolotl">`+
  1047. `<device id="482886413b977930064a5888b92134fe"/>`+
  1048. `<device id="123456789"/>`+
  1049. `</list>`+
  1050. `</item>`+
  1051. `</publish>`+
  1052. `<publish-options>`+
  1053. `<x type="submit" xmlns="jabber:x:data">`+
  1054. `<field type="hidden" var="FORM_TYPE">`+
  1055. `<value>http://jabber.org/protocol/pubsub#publish-options</value>`+
  1056. `</field>`+
  1057. `<field var="pubsub#access_model">`+
  1058. `<value>open</value>`+
  1059. `</field>`+
  1060. `</x>`+
  1061. `</publish-options>`+
  1062. `</pubsub>`+
  1063. `</iq>`);
  1064. stanza = $iq({
  1065. 'from': _converse.bare_jid,
  1066. 'id': iq_stanza.getAttribute('id'),
  1067. 'to': _converse.bare_jid,
  1068. 'type': 'result'});
  1069. _converse.connection._dataRecv(mock.createRequest(stanza));
  1070. const iq_el = await u.waitUntil(() => bundleHasBeenPublished(_converse));
  1071. expect(iq_el.getAttributeNames().sort().join()).toBe(["from", "type", "xmlns", "id"].sort().join());
  1072. expect(iq_el.querySelector('prekeys').childNodes.length).toBe(100);
  1073. const signed_prekeys = iq_el.querySelectorAll('signedPreKeyPublic');
  1074. expect(signed_prekeys.length).toBe(1);
  1075. const signed_prekey = signed_prekeys[0];
  1076. expect(signed_prekey.getAttribute('signedPreKeyId')).toBe('0')
  1077. expect(iq_el.querySelectorAll('signedPreKeySignature').length).toBe(1);
  1078. expect(iq_el.querySelectorAll('identityKey').length).toBe(1);
  1079. stanza = $iq({
  1080. 'from': _converse.bare_jid,
  1081. 'id': iq_el.getAttribute('id'),
  1082. 'to': _converse.bare_jid,
  1083. 'type': 'result'});
  1084. _converse.connection._dataRecv(mock.createRequest(stanza));
  1085. await _converse.api.waitUntil('OMEMOInitialized', 1000);
  1086. await mock.openChatBoxFor(_converse, contact_jid);
  1087. iq_stanza = await u.waitUntil(() => deviceListFetched(_converse, contact_jid));
  1088. expect(Strophe.serialize(iq_stanza)).toBe(
  1089. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="${contact_jid}" type="get" xmlns="jabber:client">`+
  1090. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  1091. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  1092. `</pubsub>`+
  1093. `</iq>`);
  1094. stanza = $iq({
  1095. 'from': contact_jid,
  1096. 'id': iq_stanza.getAttribute('id'),
  1097. 'to': _converse.bare_jid,
  1098. 'type': 'result',
  1099. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  1100. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  1101. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  1102. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  1103. .c('device', {'id': '368866411b877c30064a5f62b917cffe'}).up()
  1104. .c('device', {'id': '3300659945416e274474e469a1f0154c'}).up()
  1105. .c('device', {'id': '4e30f35051b7b8b42abe083742187228'}).up()
  1106. .c('device', {'id': 'ae890ac52d0df67ed7cfdf51b644e901'});
  1107. _converse.connection._dataRecv(mock.createRequest(stanza));
  1108. devicelist = _converse.devicelists.get(contact_jid);
  1109. await u.waitUntil(() => devicelist.devices.length);
  1110. expect(_converse.devicelists.length).toBe(2);
  1111. devicelist = _converse.devicelists.get(contact_jid);
  1112. expect(devicelist.devices.length).toBe(4);
  1113. expect(devicelist.devices.at(0).get('id')).toBe('368866411b877c30064a5f62b917cffe');
  1114. expect(devicelist.devices.at(1).get('id')).toBe('3300659945416e274474e469a1f0154c');
  1115. expect(devicelist.devices.at(2).get('id')).toBe('4e30f35051b7b8b42abe083742187228');
  1116. expect(devicelist.devices.at(3).get('id')).toBe('ae890ac52d0df67ed7cfdf51b644e901');
  1117. await u.waitUntil(() => _converse.chatboxviews.get(contact_jid).el.querySelector('.chat-toolbar'));
  1118. const view = _converse.chatboxviews.get(contact_jid);
  1119. const toolbar = view.el.querySelector('.chat-toolbar');
  1120. expect(view.model.get('omemo_active')).toBe(undefined);
  1121. let toggle = toolbar.querySelector('.toggle-omemo');
  1122. expect(toggle === null).toBe(false);
  1123. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1124. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1125. spyOn(view, 'toggleOMEMO').and.callThrough();
  1126. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  1127. toolbar.querySelector('.toggle-omemo').click();
  1128. expect(view.toggleOMEMO).toHaveBeenCalled();
  1129. expect(view.model.get('omemo_active')).toBe(true);
  1130. await u.waitUntil(() => u.hasClass('fa-lock', toolbar.querySelector('.toggle-omemo')));
  1131. toggle = toolbar.querySelector('.toggle-omemo');
  1132. expect(u.hasClass('fa-unlock', toggle)).toBe(false);
  1133. expect(u.hasClass('fa-lock', toggle)).toBe(true);
  1134. const textarea = view.el.querySelector('.chat-textarea');
  1135. textarea.value = 'This message will be sent encrypted';
  1136. view.onKeyDown({
  1137. target: textarea,
  1138. preventDefault: function preventDefault () {},
  1139. keyCode: 13
  1140. });
  1141. view.model.save({'omemo_supported': false});
  1142. toggle = toolbar.querySelector('.toggle-omemo');
  1143. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1144. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1145. expect(u.hasClass('disabled', toggle)).toBe(true);
  1146. view.model.save({'omemo_supported': true});
  1147. toggle = toolbar.querySelector('.toggle-omemo');
  1148. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1149. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1150. expect(u.hasClass('disabled', toggle)).toBe(false);
  1151. done();
  1152. }));
  1153. it("adds a toolbar button for starting an encrypted groupchat session",
  1154. mock.initConverse(
  1155. ['rosterGroupsFetched', 'chatBoxesFetched'], {'view_mode': 'fullscreen'},
  1156. async function (done, _converse) {
  1157. await mock.waitUntilDiscoConfirmed(
  1158. _converse, _converse.bare_jid,
  1159. [{'category': 'pubsub', 'type': 'pep'}],
  1160. ['http://jabber.org/protocol/pubsub#publish-options']
  1161. );
  1162. // MEMO encryption works only in members-only conferences that are non-anonymous.
  1163. const features = [
  1164. 'http://jabber.org/protocol/muc',
  1165. 'jabber:iq:register',
  1166. 'muc_passwordprotected',
  1167. 'muc_hidden',
  1168. 'muc_temporary',
  1169. 'muc_membersonly',
  1170. 'muc_unmoderated',
  1171. 'muc_nonanonymous'
  1172. ];
  1173. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo', features);
  1174. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1175. await u.waitUntil(() => initializedOMEMO(_converse));
  1176. const toolbar = view.el.querySelector('.chat-toolbar');
  1177. let toggle = toolbar.querySelector('.toggle-omemo');
  1178. expect(view.model.get('omemo_active')).toBe(undefined);
  1179. expect(toggle === null).toBe(false);
  1180. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1181. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1182. expect(u.hasClass('disabled', toggle)).toBe(false);
  1183. expect(view.model.get('omemo_supported')).toBe(true);
  1184. toggle.click();
  1185. toggle = toolbar.querySelector('.toggle-omemo');
  1186. expect(view.model.get('omemo_active')).toBe(true);
  1187. expect(u.hasClass('fa-unlock', toggle)).toBe(false);
  1188. expect(u.hasClass('fa-lock', toggle)).toBe(true);
  1189. expect(u.hasClass('disabled', toggle)).toBe(false);
  1190. expect(view.model.get('omemo_supported')).toBe(true);
  1191. let contact_jid = 'newguy@montague.lit';
  1192. let stanza = $pres({
  1193. to: 'romeo@montague.lit/orchard',
  1194. from: 'lounge@montague.lit/newguy'
  1195. })
  1196. .c('x', {xmlns: Strophe.NS.MUC_USER})
  1197. .c('item', {
  1198. 'affiliation': 'none',
  1199. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  1200. 'role': 'participant'
  1201. }).tree();
  1202. _converse.connection._dataRecv(mock.createRequest(stanza));
  1203. let iq_stanza = await u.waitUntil(() => deviceListFetched(_converse, contact_jid));
  1204. expect(Strophe.serialize(iq_stanza)).toBe(
  1205. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="${contact_jid}" type="get" xmlns="jabber:client">`+
  1206. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  1207. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  1208. `</pubsub>`+
  1209. `</iq>`);
  1210. stanza = $iq({
  1211. 'from': contact_jid,
  1212. 'id': iq_stanza.getAttribute('id'),
  1213. 'to': _converse.bare_jid,
  1214. 'type': 'result',
  1215. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  1216. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  1217. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  1218. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  1219. .c('device', {'id': '4e30f35051b7b8b42abe083742187228'}).up()
  1220. .c('device', {'id': 'ae890ac52d0df67ed7cfdf51b644e901'});
  1221. _converse.connection._dataRecv(mock.createRequest(stanza));
  1222. await u.waitUntil(() => _converse.omemo_store);
  1223. expect(_converse.devicelists.length).toBe(2);
  1224. await u.waitUntil(() => deviceListFetched(_converse, contact_jid));
  1225. const devicelist = _converse.devicelists.get(contact_jid);
  1226. expect(devicelist.devices.length).toBe(2);
  1227. expect(devicelist.devices.at(0).get('id')).toBe('4e30f35051b7b8b42abe083742187228');
  1228. expect(devicelist.devices.at(1).get('id')).toBe('ae890ac52d0df67ed7cfdf51b644e901');
  1229. expect(view.model.get('omemo_active')).toBe(true);
  1230. toggle = toolbar.querySelector('.toggle-omemo');
  1231. expect(toggle === null).toBe(false);
  1232. expect(u.hasClass('fa-unlock', toggle)).toBe(false);
  1233. expect(u.hasClass('fa-lock', toggle)).toBe(true);
  1234. expect(u.hasClass('disabled', toggle)).toBe(false);
  1235. expect(view.model.get('omemo_supported')).toBe(true);
  1236. // Test that the button gets disabled when the room becomes
  1237. // anonymous or semi-anonymous
  1238. view.model.features.save({'nonanonymous': false, 'semianonymous': true});
  1239. await u.waitUntil(() => !view.model.get('omemo_supported'));
  1240. toggle = toolbar.querySelector('.toggle-omemo');
  1241. expect(toggle === null).toBe(true);
  1242. expect(view.model.get('omemo_supported')).toBe(false);
  1243. view.model.features.save({'nonanonymous': true, 'semianonymous': false});
  1244. await u.waitUntil(() => view.model.get('omemo_supported'));
  1245. toggle = toolbar.querySelector('.toggle-omemo');
  1246. expect(toggle === null).toBe(false);
  1247. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1248. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1249. expect(u.hasClass('disabled', toggle)).toBe(false);
  1250. // Test that the button gets disabled when the room becomes open
  1251. view.model.features.save({'membersonly': false, 'open': true});
  1252. await u.waitUntil(() => !view.model.get('omemo_supported'));
  1253. toggle = toolbar.querySelector('.toggle-omemo');
  1254. expect(toggle === null).toBe(true);
  1255. view.model.features.save({'membersonly': true, 'open': false});
  1256. await u.waitUntil(() => view.model.get('omemo_supported'));
  1257. toggle = toolbar.querySelector('.toggle-omemo');
  1258. expect(toggle === null).toBe(false);
  1259. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1260. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1261. expect(u.hasClass('disabled', toggle)).toBe(false);
  1262. expect(view.model.get('omemo_supported')).toBe(true);
  1263. expect(view.model.get('omemo_active')).toBe(false);
  1264. toggle.click();
  1265. expect(view.model.get('omemo_active')).toBe(true);
  1266. // Someone enters the room who doesn't have OMEMO support, while we
  1267. // have OMEMO activated...
  1268. contact_jid = 'oldguy@montague.lit';
  1269. stanza = $pres({
  1270. to: 'romeo@montague.lit/orchard',
  1271. from: 'lounge@montague.lit/oldguy'
  1272. })
  1273. .c('x', {xmlns: Strophe.NS.MUC_USER})
  1274. .c('item', {
  1275. 'affiliation': 'none',
  1276. 'jid': `${contact_jid}/_converse.js-290929788`,
  1277. 'role': 'participant'
  1278. }).tree();
  1279. _converse.connection._dataRecv(mock.createRequest(stanza));
  1280. iq_stanza = await u.waitUntil(() => deviceListFetched(_converse, contact_jid));
  1281. expect(Strophe.serialize(iq_stanza)).toBe(
  1282. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="${contact_jid}" type="get" xmlns="jabber:client">`+
  1283. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  1284. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  1285. `</pubsub>`+
  1286. `</iq>`);
  1287. stanza = $iq({
  1288. 'from': contact_jid,
  1289. 'id': iq_stanza.getAttribute('id'),
  1290. 'to': _converse.bare_jid,
  1291. 'type': 'error'
  1292. }).c('error', {'type': 'cancel'})
  1293. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  1294. _converse.connection._dataRecv(mock.createRequest(stanza));
  1295. await u.waitUntil(() => !view.model.get('omemo_supported'));
  1296. expect(view.el.querySelector('.chat-error').textContent.trim()).toBe(
  1297. "oldguy doesn't appear to have a client that supports OMEMO. "+
  1298. "Encrypted chat will no longer be possible in this grouchat."
  1299. );
  1300. toggle = toolbar.querySelector('.toggle-omemo');
  1301. expect(toggle === null).toBe(false);
  1302. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1303. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1304. expect(u.hasClass('disabled', toggle)).toBe(true);
  1305. expect( _converse.chatboxviews.el.querySelector('.modal-body p')).toBe(null);
  1306. toggle.click();
  1307. const msg = _converse.chatboxviews.el.querySelector('.modal-body p');
  1308. expect(msg.textContent).toBe(
  1309. 'Cannot use end-to-end encryption in this groupchat, '+
  1310. 'either the groupchat has some anonymity or not all participants support OMEMO.');
  1311. done();
  1312. }));
  1313. it("shows OMEMO device fingerprints in the user details modal",
  1314. mock.initConverse(
  1315. ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  1316. async function (done, _converse) {
  1317. await mock.waitUntilDiscoConfirmed(
  1318. _converse, _converse.bare_jid,
  1319. [{'category': 'pubsub', 'type': 'pep'}],
  1320. ['http://jabber.org/protocol/pubsub#publish-options']
  1321. );
  1322. await mock.waitForRoster(_converse, 'current', 1);
  1323. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  1324. await mock.openChatBoxFor(_converse, contact_jid)
  1325. // We simply emit, to avoid doing all the setup work
  1326. _converse.api.trigger('OMEMOInitialized');
  1327. const view = _converse.chatboxviews.get(contact_jid);
  1328. const show_modal_button = view.el.querySelector('.show-user-details-modal');
  1329. show_modal_button.click();
  1330. const modal = view.user_details_modal;
  1331. await u.waitUntil(() => u.isVisible(modal.el), 1000);
  1332. let iq_stanza = await u.waitUntil(() => deviceListFetched(_converse, contact_jid));
  1333. expect(Strophe.serialize(iq_stanza)).toBe(
  1334. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="mercutio@montague.lit" type="get" xmlns="jabber:client">`+
  1335. `<pubsub xmlns="http://jabber.org/protocol/pubsub"><items node="eu.siacs.conversations.axolotl.devicelist"/></pubsub>`+
  1336. `</iq>`);
  1337. let stanza = $iq({
  1338. 'from': contact_jid,
  1339. 'id': iq_stanza.getAttribute('id'),
  1340. 'to': _converse.bare_jid,
  1341. 'type': 'result',
  1342. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  1343. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  1344. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  1345. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  1346. .c('device', {'id': '555'});
  1347. _converse.connection._dataRecv(mock.createRequest(stanza));
  1348. await u.waitUntil(() => u.isVisible(modal.el), 1000);
  1349. iq_stanza = await u.waitUntil(() => bundleFetched(_converse, contact_jid, '555'));
  1350. expect(Strophe.serialize(iq_stanza)).toBe(
  1351. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="mercutio@montague.lit" type="get" xmlns="jabber:client">`+
  1352. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  1353. `<items node="eu.siacs.conversations.axolotl.bundles:555"/>`+
  1354. `</pubsub>`+
  1355. `</iq>`);
  1356. stanza = $iq({
  1357. 'from': contact_jid,
  1358. 'id': iq_stanza.getAttribute('id'),
  1359. 'to': _converse.bare_jid,
  1360. 'type': 'result',
  1361. }).c('pubsub', {
  1362. 'xmlns': 'http://jabber.org/protocol/pubsub'
  1363. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:555"})
  1364. .c('item')
  1365. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  1366. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('1111')).up()
  1367. .c('signedPreKeySignature').t(btoa('2222')).up()
  1368. .c('identityKey').t('BQmHEOHjsYm3w5M8VqxAtqJmLCi7CaxxsdZz6G0YpuMI').up()
  1369. .c('prekeys')
  1370. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1001')).up()
  1371. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1002')).up()
  1372. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1003'));
  1373. _converse.connection._dataRecv(mock.createRequest(stanza));
  1374. await u.waitUntil(() => modal.el.querySelectorAll('.fingerprints .fingerprint').length);
  1375. expect(modal.el.querySelectorAll('.fingerprints .fingerprint').length).toBe(1);
  1376. const el = modal.el.querySelector('.fingerprints .fingerprint');
  1377. expect(el.textContent.trim()).toBe(
  1378. u.formatFingerprint(u.arrayBufferToHex(u.base64ToArrayBuffer('BQmHEOHjsYm3w5M8VqxAtqJmLCi7CaxxsdZz6G0YpuMI')))
  1379. );
  1380. expect(modal.el.querySelectorAll('input[type="radio"]').length).toBe(2);
  1381. const devicelist = _converse.devicelists.get(contact_jid);
  1382. expect(devicelist.devices.get('555').get('trusted')).toBe(0);
  1383. let trusted_radio = modal.el.querySelector('input[type="radio"][name="555"][value="1"]');
  1384. expect(trusted_radio.checked).toBe(true);
  1385. let untrusted_radio = modal.el.querySelector('input[type="radio"][name="555"][value="-1"]');
  1386. expect(untrusted_radio.checked).toBe(false);
  1387. // Test that the device can be set to untrusted
  1388. untrusted_radio.click();
  1389. trusted_radio = document.querySelector('input[type="radio"][name="555"][value="1"]');
  1390. expect(trusted_radio.hasAttribute('checked')).toBe(false);
  1391. expect(devicelist.devices.get('555').get('trusted')).toBe(-1);
  1392. untrusted_radio = document.querySelector('input[type="radio"][name="555"][value="-1"]');
  1393. expect(untrusted_radio.hasAttribute('checked')).toBe(true);
  1394. trusted_radio.click();
  1395. expect(devicelist.devices.get('555').get('trusted')).toBe(1);
  1396. done();
  1397. }));
  1398. });