diff --git a/keyserver/src/push/encrypted-notif-utils-api.js b/keyserver/src/push/encrypted-notif-utils-api.js --- a/keyserver/src/push/encrypted-notif-utils-api.js +++ b/keyserver/src/push/encrypted-notif-utils-api.js @@ -52,7 +52,7 @@ uploadLargeNotifPayload: blobServiceUpload, getNotifByteSize: (serializedPayload: string) => Buffer.byteLength(serializedPayload), - getEncryptedNotifHash: (serializedNotification: string) => + getEncryptedNotifHash: async (serializedNotification: string) => getOlmUtility().sha256(serializedNotification), }; diff --git a/lib/push/crypto.js b/lib/push/crypto.js --- a/lib/push/crypto.js +++ b/lib/push/crypto.js @@ -171,9 +171,10 @@ dbPersistCondition, ); - const encryptedPayloadHash = encryptedNotifUtilsAPI.getEncryptedNotifHash( - serializedPayload.body, - ); + const encryptedPayloadHash = + await encryptedNotifUtilsAPI.getEncryptedNotifHash( + serializedPayload.body, + ); return { notification: { @@ -242,9 +243,10 @@ unencryptedSerializedPayload, ); - const encryptedPayloadHash = encryptedNotifUtilsAPI.getEncryptedNotifHash( - serializedPayload.body, - ); + const encryptedPayloadHash = + await encryptedNotifUtilsAPI.getEncryptedNotifHash( + serializedPayload.body, + ); return { notification: { diff --git a/lib/types/notif-types.js b/lib/types/notif-types.js --- a/lib/types/notif-types.js +++ b/lib/types/notif-types.js @@ -396,5 +396,5 @@ | { +blobUploadError: string }, >, +getNotifByteSize: (serializedNotification: string) => number, - +getEncryptedNotifHash: (serializedNotification: string) => string, + +getEncryptedNotifHash: (serializedNotification: string) => Promise, }; diff --git a/native/push/encrypted-notif-utils-api.js b/native/push/encrypted-notif-utils-api.js new file mode 100644 --- /dev/null +++ b/native/push/encrypted-notif-utils-api.js @@ -0,0 +1,42 @@ +// @flow + +import type { EncryptedNotifUtilsAPI } from 'lib/types/notif-types.js'; + +import { commUtilsModule } from '../native-modules.js'; + +const encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI = { + encryptSerializedNotifPayload: async ( + cryptoID: string, + unencryptedPayload: string, + encryptedPayloadSizeValidator?: ( + encryptedPayload: string, + type: '1' | '0', + ) => boolean, + ) => { + // The "mock" implementation below will be replaced with proper + // implementation after olm notif sessions initialization is + // implemented. for now it is actually beneficial to return + // original string as encrypted string since it allows for + // better testing as we can verify which data are encrypted + // and which aren't. + return { + encryptedData: { body: unencryptedPayload, type: 1 }, + sizeLimitViolated: encryptedPayloadSizeValidator + ? !encryptedPayloadSizeValidator(unencryptedPayload, '1') + : false, + }; + }, + uploadLargeNotifPayload: async () => ({ blobUploadError: 'not_implemented' }), + getNotifByteSize: (serializedNotification: string) => { + return commUtilsModule.encodeStringToUTF8ArrayBuffer(serializedNotification) + .byteLength; + }, + getEncryptedNotifHash: async (serializedNotification: string) => { + const notifAsArrayBuffer = commUtilsModule.encodeStringToUTF8ArrayBuffer( + serializedNotification, + ); + return commUtilsModule.sha256(notifAsArrayBuffer); + }, +}; + +export default encryptedNotifUtilsAPI; diff --git a/web/push-notif/encrypted-notif-utils-api.js b/web/push-notif/encrypted-notif-utils-api.js new file mode 100644 --- /dev/null +++ b/web/push-notif/encrypted-notif-utils-api.js @@ -0,0 +1,38 @@ +// @flow + +import type { EncryptedNotifUtilsAPI } from 'lib/types/notif-types.js'; + +const encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI = { + encryptSerializedNotifPayload: async ( + cryptoID: string, + unencryptedPayload: string, + encryptedPayloadSizeValidator?: ( + encryptedPayload: string, + type: '1' | '0', + ) => boolean, + ) => { + // The "mock" implementation below will be replaced with proper + // implementation after olm notif sessions initialization is + // implemented. for now it is actually beneficial to return + // original string as encrypted string since it allows for + // better testing as we can verify which data are encrypted + // and which aren't. + return { + encryptedData: { body: unencryptedPayload, type: 1 }, + sizeLimitViolated: encryptedPayloadSizeValidator + ? !encryptedPayloadSizeValidator(unencryptedPayload, '1') + : false, + }; + }, + uploadLargeNotifPayload: async () => ({ blobUploadError: 'not_implemented' }), + getNotifByteSize: (serializedNotification: string) => { + return new Blob([serializedNotification]).size; + }, + getEncryptedNotifHash: async (serializedNotification: string) => { + const notificationBytes = new TextEncoder().encode(serializedNotification); + const hashBytes = await crypto.subtle.digest('SHA-256', notificationBytes); + return btoa(String.fromCharCode(...new Uint8Array(hashBytes))); + }, +}; + +export default encryptedNotifUtilsAPI;