omemo.js 79 KB

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