|
| 1 | +// Built around ShellTear's POC at #2215#issuecomment-1292885678 on @adiwajshing/baileys |
| 2 | +// Copyright ~ purpshell |
| 3 | + |
| 4 | +import crypto from 'node:crypto'; |
| 5 | + |
| 6 | +const enc = new TextEncoder(); |
| 7 | +/** |
| 8 | + * Decrypt PollUpdate messages |
| 9 | + */ |
| 10 | +export class PollUpdateDecrypt { |
| 11 | + /** |
| 12 | + * Compare the SHA-256 hashes of the poll options from the update to find the original choices |
| 13 | + * @param options Options from the poll creation message |
| 14 | + * @param pollOptionHash hash from `this.decrypt()` |
| 15 | + * @returns the original option, can be empty when none are currently selected |
| 16 | + */ |
| 17 | + static async compare(options: string[], pollOptionHashes: string[]): Promise<string[]> { |
| 18 | + const selectedOptions = []; |
| 19 | + for (const option of options) { |
| 20 | + const hash = Buffer.from( |
| 21 | + await crypto.webcrypto.subtle.digest('SHA-256', new TextEncoder().encode(option)), |
| 22 | + ) |
| 23 | + .toString('hex') |
| 24 | + .toUpperCase(); |
| 25 | + for (const pollOptionHash of pollOptionHashes) { |
| 26 | + if (pollOptionHash === hash) { |
| 27 | + selectedOptions.push(option); |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + return selectedOptions; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * decrypt a poll message update |
| 36 | + * @param encPayload from the update |
| 37 | + * @param encIv from the update |
| 38 | + * @param encKey from the original poll |
| 39 | + * @param pollMsgSender sender jid of the pollCreation message |
| 40 | + * @param pollMsgId id of the pollCreation message |
| 41 | + * @param voteMsgSender sender of the pollUpdate message |
| 42 | + * @returns The option or empty array if something went wrong OR everything was unticked |
| 43 | + */ |
| 44 | + static async decrypt( |
| 45 | + encKey: Uint8Array, |
| 46 | + encPayload: Uint8Array, |
| 47 | + encIv: Uint8Array, |
| 48 | + pollMsgSender: string, |
| 49 | + pollMsgId: string, |
| 50 | + voteMsgSender: string, |
| 51 | + ): Promise<string[]> { |
| 52 | + const stanzaId = enc.encode(pollMsgId); |
| 53 | + const parentMsgOriginalSender = enc.encode(pollMsgSender); |
| 54 | + const modificationSender = enc.encode(voteMsgSender); |
| 55 | + const modificationType = enc.encode('Poll Vote'); |
| 56 | + const pad = new Uint8Array([1]); |
| 57 | + |
| 58 | + const signMe = new Uint8Array([ |
| 59 | + ...stanzaId, |
| 60 | + ...parentMsgOriginalSender, |
| 61 | + ...modificationSender, |
| 62 | + ...modificationType, |
| 63 | + pad, |
| 64 | + ] as any); |
| 65 | + |
| 66 | + const createSignKey = async (n: Uint8Array = new Uint8Array(32)) => { |
| 67 | + return await crypto.webcrypto.subtle.importKey( |
| 68 | + 'raw', |
| 69 | + n, |
| 70 | + { name: 'HMAC', hash: 'SHA-256' }, |
| 71 | + false, |
| 72 | + ['sign'], |
| 73 | + ); |
| 74 | + }; |
| 75 | + |
| 76 | + const sign = async ( |
| 77 | + n: crypto.webcrypto.BufferSource, |
| 78 | + key: crypto.webcrypto.CryptoKey, |
| 79 | + ) => { |
| 80 | + return await crypto.webcrypto.subtle.sign( |
| 81 | + { name: 'HMAC', hash: 'SHA-256' }, |
| 82 | + key, |
| 83 | + n, |
| 84 | + ); |
| 85 | + }; |
| 86 | + |
| 87 | + let key = await createSignKey(); |
| 88 | + |
| 89 | + const temp = await sign(encKey, key); |
| 90 | + |
| 91 | + key = await createSignKey(new Uint8Array(temp)); |
| 92 | + |
| 93 | + const decryptionKey = new Uint8Array(await sign(signMe, key)); |
| 94 | + |
| 95 | + const additionalData = enc.encode(`${pollMsgId}\u0000${voteMsgSender}`); |
| 96 | + |
| 97 | + const decryptedMessage = await this._decryptMessage( |
| 98 | + encPayload, |
| 99 | + encIv, |
| 100 | + additionalData, |
| 101 | + decryptionKey, |
| 102 | + ); |
| 103 | + |
| 104 | + const pollOptionHash = this._decodeMessage(decryptedMessage); |
| 105 | + |
| 106 | + // '0A20' in hex represents unicode " " and "\n" thus declaring the end of one option |
| 107 | + // we want multiple hashes to make it easier to iterate and understand for your use cases |
| 108 | + return pollOptionHash.split('0A20') || []; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Internal method to decrypt the message after gathering all information |
| 113 | + * @deprecated Use `this.decrypt()` instead, only use this if you know what you are doing |
| 114 | + * @param encPayload |
| 115 | + * @param encIv |
| 116 | + * @param additionalData |
| 117 | + * @param decryptionKey |
| 118 | + * @returns |
| 119 | + */ |
| 120 | + static async _decryptMessage( |
| 121 | + encPayload: Uint8Array, |
| 122 | + encIv: Uint8Array, |
| 123 | + additionalData: Uint8Array, |
| 124 | + decryptionKey: Uint8Array, |
| 125 | + ) { |
| 126 | + const tagSize_multiplier = 16; |
| 127 | + const encoded = encPayload; |
| 128 | + const key = await crypto.webcrypto.subtle.importKey( |
| 129 | + 'raw', |
| 130 | + decryptionKey, |
| 131 | + 'AES-GCM', |
| 132 | + false, |
| 133 | + ['encrypt', 'decrypt'], |
| 134 | + ); |
| 135 | + const decrypted = await crypto.webcrypto.subtle.decrypt( |
| 136 | + { |
| 137 | + name: 'AES-GCM', |
| 138 | + iv: encIv, |
| 139 | + additionalData: additionalData, |
| 140 | + tagLength: 8 * tagSize_multiplier, |
| 141 | + }, |
| 142 | + key, |
| 143 | + encoded, |
| 144 | + ); |
| 145 | + return new Uint8Array(decrypted).slice(2); // remove 2 bytes (OA20)(space+newline) |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Decode the message from `this._decryptMessage()` |
| 150 | + * @param decryptedMessage the message from `this._decrpytMessage()` |
| 151 | + * @returns |
| 152 | + */ |
| 153 | + static _decodeMessage(decryptedMessage: Uint8Array) { |
| 154 | + const n = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70]; |
| 155 | + const outarr: number[] = []; |
| 156 | + |
| 157 | + for (let i = 0; i < decryptedMessage.length; i++) { |
| 158 | + const val = decryptedMessage[i]; |
| 159 | + outarr.push(n[val >> 4], n[15 & val]); |
| 160 | + } |
| 161 | + |
| 162 | + return String.fromCharCode(...outarr); |
| 163 | + } |
| 164 | +} |
0 commit comments