omemo.js 81 KB

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