diff --git a/keyserver/src/session/cookies.js b/keyserver/src/session/cookies.js --- a/keyserver/src/session/cookies.js +++ b/keyserver/src/session/cookies.js @@ -5,6 +5,10 @@ import invariant from 'invariant'; import url from 'url'; +import { + hasMinCodeVersion, + FUTURE_CODE_VERSION, +} from 'lib/shared/version-utils.js'; import type { Shape } from 'lib/types/core.js'; import type { SignedIdentityKeysBlob } from 'lib/types/crypto-types.js'; import { isWebPlatform } from 'lib/types/device-types.js'; @@ -826,9 +830,12 @@ if ( !viewer.platformDetails || (viewer.platformDetails.platform !== 'ios' && - viewer.platformDetails.platform !== 'android') || - !viewer.platformDetails.codeVersion || - viewer.platformDetails.codeVersion <= 222 + viewer.platformDetails.platform !== 'android' && + viewer.platformDetails.platform !== 'web') || + !hasMinCodeVersion(viewer.platformDetails, { + native: 222, + web: FUTURE_CODE_VERSION, + }) ) { return false; } diff --git a/lib/shared/crypto-utils.js b/lib/shared/crypto-utils.js --- a/lib/shared/crypto-utils.js +++ b/lib/shared/crypto-utils.js @@ -6,7 +6,11 @@ getOlmSessionInitializationData, getOlmSessionInitializationDataActionTypes, } from '../actions/user-actions.js'; -import type { OLMIdentityKeys, OLMOneTimeKeys } from '../types/crypto-types'; +import type { + OLMIdentityKeys, + OLMOneTimeKeys, + OLMPrekey, +} from '../types/crypto-types'; import type { OlmSessionInitializationInfo } from '../types/request-types'; import { useServerCall, @@ -23,6 +27,10 @@ +callServerEndpointOptions?: ?CallServerEndpointOptions, }; +const initialEncryptedMessageContent = { + type: 'init', +}; + function useInitialNotificationsEncryptedMessage( platformSpecificSessionCreator: ( notificationsIdentityKeys: OLMIdentityKeys, @@ -75,13 +83,26 @@ return values(oneTimeKeys.curve25519); } +function getPrekeyValue(prekey: OLMPrekey): string { + const [prekeyValue] = values(prekey.curve25519); + return prekeyValue; +} + function getOneTimeKeyValuesFromBlob(keyBlob: string): $ReadOnlyArray { const oneTimeKeys: OLMOneTimeKeys = JSON.parse(keyBlob); return getOneTimeKeyValues(oneTimeKeys); } +function getPrekeyValueFromBlob(prekeyBlob: string): string { + const prekey: OLMPrekey = JSON.parse(prekeyBlob); + return getPrekeyValue(prekey); +} + export { getOneTimeKeyValues, + getPrekeyValue, getOneTimeKeyValuesFromBlob, + getPrekeyValueFromBlob, + initialEncryptedMessageContent, useInitialNotificationsEncryptedMessage, }; diff --git a/lib/types/crypto-types.js b/lib/types/crypto-types.js --- a/lib/types/crypto-types.js +++ b/lib/types/crypto-types.js @@ -2,6 +2,7 @@ import t, { type TInterface } from 'tcomb'; +import type { OlmSessionInitializationInfo } from '../types/request-types.js'; import { tShape } from '../utils/validation-utils.js'; export type OLMIdentityKeys = { @@ -25,6 +26,11 @@ +pickledAccount: string, }; +export type PickledOLMSession = { + +picklingKey: string, + +pickledSession: string, +}; + export type CryptoStore = { +primaryAccount: PickledOLMAccount, +primaryIdentityKeys: OLMIdentityKeys, @@ -36,6 +42,13 @@ +getInitializedCryptoStore: () => Promise, }; +export type NotificationsSessionCreatorContextType = { + +notificationsSessionCreator: ( + notificationsIdentityKeys: OLMIdentityKeys, + notificationsInitializationInfo: OlmSessionInitializationInfo, + ) => Promise, +}; + export type IdentityKeysBlob = { +primaryIdentityPublicKeys: OLMIdentityKeys, +notificationIdentityPublicKeys: OLMIdentityKeys, diff --git a/web/account/account-hooks.js b/web/account/account-hooks.js --- a/web/account/account-hooks.js +++ b/web/account/account-hooks.js @@ -2,17 +2,35 @@ import olm from '@commapp/olm'; import invariant from 'invariant'; +import localforage from 'localforage'; import * as React from 'react'; import { useDispatch } from 'react-redux'; import uuid from 'uuid'; +import { + initialEncryptedMessageContent, + getOneTimeKeyValuesFromBlob, + getPrekeyValueFromBlob, +} from 'lib/shared/crypto-utils.js'; import type { SignedIdentityKeysBlob, CryptoStore, IdentityKeysBlob, CryptoStoreContextType, + OLMIdentityKeys, + NotificationsSessionCreatorContextType, + PickledOLMSession, } from 'lib/types/crypto-types.js'; +import type { OlmSessionInitializationInfo } from 'lib/types/request-types.js'; +import { + generateCryptoKey, + encryptData, +} from '../crypto/aes-gcm-crypto-utils.js'; +import { + NOTIFICATIONS_OLM_SESSION_CONTENT, + NOTIFICATIONS_OLM_SESSION_ENCRYPTION_KEY, +} from '../database/utils/constants.js'; import { initOlm } from '../olm/olm-utils.js'; import { setCryptoStore } from '../redux/crypto-store-reducer.js'; import { useSelector } from '../redux/redux-utils.js'; @@ -20,6 +38,9 @@ const CryptoStoreContext: React.Context = React.createContext(null); +const WebNotificationsSessionCreatorContext: React.Context = + React.createContext(null); + type Props = { +children: React.Node, }; @@ -154,8 +175,133 @@ }, [getOrCreateCryptoStore]); } +function WebNotificationsSessionCreatorProvider(props: Props): React.Node { + const getOrCreateCryptoStore = useGetOrCreateCryptoStore(); + const currentCryptoStore = useSelector(state => state.cryptoStore); + + const createNewNotificationsSession = React.useCallback( + async ( + notificationsIdentityKeys: OLMIdentityKeys, + notificationsInitializationInfo: OlmSessionInitializationInfo, + ) => { + const [{ notificationAccount }, encryptionKey] = await Promise.all([ + getOrCreateCryptoStore(), + generateCryptoKey({ extractable: false }), + initOlm(), + ]); + + const account = new olm.Account(); + const { picklingKey, pickledAccount } = notificationAccount; + account.unpickle(picklingKey, pickledAccount); + + const notificationsPrekey = getPrekeyValueFromBlob( + notificationsInitializationInfo.prekey, + ); + const [notificationsOneTimeKey] = getOneTimeKeyValuesFromBlob( + notificationsInitializationInfo.oneTimeKey, + ); + + const session = new olm.Session(); + session.create_outbound( + account, + notificationsIdentityKeys.curve25519, + notificationsIdentityKeys.ed25519, + notificationsPrekey, + notificationsInitializationInfo.prekeySignature, + notificationsOneTimeKey, + ); + const { body: initialNotificationsEncryptedMessage } = session.encrypt( + JSON.stringify(initialEncryptedMessageContent), + ); + + const pickledOLMSession: PickledOLMSession = { + picklingKey, + pickledSession: session.pickle(picklingKey), + }; + const encryptedOlmSession = await encryptData( + new TextEncoder().encode(JSON.stringify(pickledOLMSession)), + encryptionKey, + ); + + await Promise.all([ + localforage.setItem( + NOTIFICATIONS_OLM_SESSION_CONTENT, + encryptedOlmSession, + ), + localforage.setItem( + NOTIFICATIONS_OLM_SESSION_ENCRYPTION_KEY, + encryptionKey, + ), + ]); + + return initialNotificationsEncryptedMessage; + }, + [getOrCreateCryptoStore], + ); + + const notificationsSessionPromise = React.useRef>(null); + const createNotificationsSession = React.useCallback( + async ( + notificationsIdentityKeys: OLMIdentityKeys, + notificationsInitializationInfo: OlmSessionInitializationInfo, + ) => { + if (notificationsSessionPromise.current) { + return notificationsSessionPromise.current; + } + + const newNotificationsSessionPromise = (async () => { + try { + return await createNewNotificationsSession( + notificationsIdentityKeys, + notificationsInitializationInfo, + ); + } catch (e) { + notificationsSessionPromise.current = undefined; + throw e; + } + })(); + + notificationsSessionPromise.current = newNotificationsSessionPromise; + return newNotificationsSessionPromise; + }, + [createNewNotificationsSession], + ); + + const isCryptoStoreSet = !!currentCryptoStore; + React.useEffect(() => { + if (!isCryptoStoreSet) { + notificationsSessionPromise.current = undefined; + } + }, [isCryptoStoreSet]); + + const contextValue = React.useMemo( + () => ({ + notificationsSessionCreator: createNotificationsSession, + }), + [createNotificationsSession], + ); + + return ( + + {props.children} + + ); +} + +function useWebNotificationsSessionCreator(): ( + notificationsIdentityKeys: OLMIdentityKeys, + notificationsInitializationInfo: OlmSessionInitializationInfo, +) => Promise { + const context = React.useContext(WebNotificationsSessionCreatorContext); + invariant(context, 'WebNotificationsSessionCreator not found.'); + + return context.notificationsSessionCreator; +} + export { useGetSignedIdentityKeysBlob, useGetOrCreateCryptoStore, + WebNotificationsSessionCreatorProvider, + useWebNotificationsSessionCreator, GetOrCreateCryptoStoreProvider, }; diff --git a/web/database/utils/constants.js b/web/database/utils/constants.js --- a/web/database/utils/constants.js +++ b/web/database/utils/constants.js @@ -14,6 +14,12 @@ export const COMM_SQLITE_DATABASE_PATH = 'comm.sqlite'; +export const NOTIFICATIONS_OLM_SESSION_CONTENT = + 'notificationsOlmSessionContent'; + +export const NOTIFICATIONS_OLM_SESSION_ENCRYPTION_KEY = + 'notificationsOlmSessionEncryptionKey'; + export const DB_SUPPORTED_OS: $ReadOnlyArray = [ 'Windows 10', 'Linux', diff --git a/web/root.js b/web/root.js --- a/web/root.js +++ b/web/root.js @@ -12,7 +12,10 @@ import IntegrityHandler from 'lib/components/integrity-handler.react.js'; import { reduxLoggerMiddleware } from 'lib/utils/action-logger.js'; -import { GetOrCreateCryptoStoreProvider } from './account/account-hooks.js'; +import { + GetOrCreateCryptoStoreProvider, + WebNotificationsSessionCreatorProvider, +} from './account/account-hooks.js'; import App from './app.react.js'; import { SQLiteDataHandler } from './database/sqlite-data-handler.js'; import { localforageConfig } from './database/utils/constants.js'; @@ -39,12 +42,14 @@ - - - - - - + + + + + + + + diff --git a/web/selectors/socket-selectors.js b/web/selectors/socket-selectors.js --- a/web/selectors/socket-selectors.js +++ b/web/selectors/socket-selectors.js @@ -47,6 +47,7 @@ type WebGetClientResponsesSelectorInputType = { +state: AppState, +getSignedIdentityKeysBlob: () => Promise, + +getInitialNotificationsEncryptedMessage: () => Promise, }; const webGetClientResponsesSelector: ( @@ -60,23 +61,26 @@ input.getSignedIdentityKeysBlob, (input: WebGetClientResponsesSelectorInputType) => input.state.navInfo.tab === 'calendar', + (input: WebGetClientResponsesSelectorInputType) => + input.getInitialNotificationsEncryptedMessage, ( getClientResponsesFunc: ( calendarActive: boolean, oneTimeKeyGenerator: ?OneTimeKeyGenerator, getSignedIdentityKeysBlob: () => Promise, - getInitialNotificationsEncryptedMessage: ?() => Promise, + getInitialNotificationsEncryptedMessage: () => Promise, serverRequests: $ReadOnlyArray, ) => Promise<$ReadOnlyArray>, getSignedIdentityKeysBlob: () => Promise, calendarActive: boolean, + getInitialNotificationsEncryptedMessage: () => Promise, ) => (serverRequests: $ReadOnlyArray) => getClientResponsesFunc( calendarActive, null, getSignedIdentityKeysBlob, - null, + getInitialNotificationsEncryptedMessage, serverRequests, ), ); diff --git a/web/socket.react.js b/web/socket.react.js --- a/web/socket.react.js +++ b/web/socket.react.js @@ -12,13 +12,17 @@ connectionSelector, lastCommunicatedPlatformDetailsSelector, } from 'lib/selectors/keyserver-selectors.js'; +import { useInitialNotificationsEncryptedMessage } from 'lib/shared/crypto-utils.js'; import Socket, { type BaseSocketProps } from 'lib/socket/socket.react.js'; import { useServerCall, useDispatchActionPromise, } from 'lib/utils/action-utils.js'; -import { useGetSignedIdentityKeysBlob } from './account/account-hooks.js'; +import { + useGetSignedIdentityKeysBlob, + useWebNotificationsSessionCreator, +} from './account/account-hooks.js'; import { useSelector } from './redux/redux-utils.js'; import { activeThreadSelector, @@ -51,8 +55,15 @@ const sessionIdentification = useSelector(sessionIdentificationSelector); const preRequestUserState = useSelector(preRequestUserStateSelector); const getSignedIdentityKeysBlob = useGetSignedIdentityKeysBlob(); + const webNotificationsSessionCreator = useWebNotificationsSessionCreator(); + const getInitialNotificationsEncryptedMessage = + useInitialNotificationsEncryptedMessage(webNotificationsSessionCreator); const getClientResponses = useSelector(state => - webGetClientResponsesSelector({ state, getSignedIdentityKeysBlob }), + webGetClientResponsesSelector({ + state, + getSignedIdentityKeysBlob, + getInitialNotificationsEncryptedMessage, + }), ); const sessionStateFunc = useSelector(webSessionStateFuncSelector); const currentCalendarQuery = useSelector(webCalendarQuery);