Hi, i am trying to decypher web push notification in function node, with no luck

Recieving push notification in http in node. and than using this code to decypher with no luck for now...

const headers = msg.req.headers;
const keys = JSON.parse(msg.payload);

// Extracting headers
const salt = Buffer.from(headers['encryption'].split('=')[1], 'base64');
const clientPublicKey = Buffer.from(headers['crypto-key'].split('=')[1].split(';')[0], 'base64');

// ECDH key agreement
const ecdh = crypto.createECDH('prime256v1');
ecdh.setPrivateKey(Buffer.from(keys.privateKey, 'base64'), 'base64');
const sharedSecret = ecdh.computeSecret(clientPublicKey);

// HKDF
const info = Buffer.concat([Buffer.from('WebPush: info\x00', 'utf8'), clientPublicKey, ecdh.getPublicKey()]);
const rawDerivedKey = crypto.hkdfSync('sha256', sharedSecret, salt, info, 32);
const derivedKey = Buffer.from(rawDerivedKey);

// Decrypting
try {
    const encryptedData = msg.req.body;
    const nonce = encryptedData.slice(0, 12);
    const ciphertext = encryptedData.slice(12, encryptedData.length - 16);
    const authTag = encryptedData.slice(encryptedData.length - 16);

    const decipher = crypto.createDecipheriv('aes-256-gcm', derivedKey, nonce);
    decipher.setAuthTag(authTag);
    const decryptedData = Buffer.concat([decipher.update(ciphertext), decipher.final()]);
    return { payload: decryptedData.toString('utf8') };
} catch (error) {
    console.error(`Error decrypting message: ${error.message}`);
    return { error: error.message };
}

It would probably help to provide:

  • Sample input to the function
  • Expected output
  • Some indication of what you think isn't working, e.g. do you think you get the right delivered key?

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.