omemo.js 85 KB

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