diff --git a/keyserver/src/user/login.js b/keyserver/src/user/login.js --- a/keyserver/src/user/login.js +++ b/keyserver/src/user/login.js @@ -3,6 +3,7 @@ import type { Account as OlmAccount } from '@commapp/olm'; import { getRustAPI } from 'rust-node-addon'; +import { getOneTimeKeyValuesFromBlob } from 'lib/shared/crypto-utils.js'; import { getCommConfig } from 'lib/utils/comm-config.js'; import { ServerError } from 'lib/utils/errors.js'; import { values } from 'lib/utils/objects.js'; @@ -14,10 +15,7 @@ } from './identity.js'; import { getMessageForException } from '../responders/utils.js'; import { fetchCallUpdateOlmAccount } from '../updaters/olm-account-updater.js'; -import { - getOneTimeKeyValues, - validateAccountPrekey, -} from '../utils/olm-utils.js'; +import { validateAccountPrekey } from '../utils/olm-utils.js'; type UserCredentials = { +username: string, +password: string }; @@ -25,7 +23,7 @@ +identityKeys: string, +prekey: string, +prekeySignature: string, - +oneTimeKey: $ReadOnlyArray, + +oneTimeKeys: $ReadOnlyArray, }; function retrieveAccountKeysSet(account: OlmAccount): AccountKeysSet { @@ -40,13 +38,14 @@ throw new ServerError('invalid_prekey'); } - if (getOneTimeKeyValues(account.one_time_keys()).length < 10) { + let oneTimeKeys = getOneTimeKeyValuesFromBlob(account.one_time_keys()); + + if (oneTimeKeys.length < 10) { account.generate_one_time_keys(10); + oneTimeKeys = getOneTimeKeyValuesFromBlob(account.one_time_keys()); } - const oneTimeKey = getOneTimeKeyValues(account.one_time_keys()); - - return { identityKeys, oneTimeKey, prekey, prekeySignature }; + return { identityKeys, oneTimeKeys, prekey, prekeySignature }; } // After register or login is successful @@ -83,13 +82,13 @@ identityKeys: notificationsIdentityKeys, prekey: notificationsPrekey, prekeySignature: notificationsPrekeySignature, - oneTimeKey: notificationsOneTimeKey, + oneTimeKeys: notificationsOneTimeKeys, } = await fetchCallUpdateOlmAccount('notifications', retrieveAccountKeysSet); const contentAccountCallback = async (account: OlmAccount) => { const { identityKeys: contentIdentityKeys, - oneTimeKey, + oneTimeKeys, prekey, prekeySignature, } = await retrieveAccountKeysSet(account); @@ -106,7 +105,7 @@ return { signedIdentityKeysBlob, - oneTimeKey, + oneTimeKeys, prekey, prekeySignature, }; @@ -118,7 +117,7 @@ signedIdentityKeysBlob, prekey: contentPrekey, prekeySignature: contentPrekeySignature, - oneTimeKey: contentOneTimeKey, + oneTimeKeys: contentOneTimeKeys, }, ] = await Promise.all([ rustAPIPromise, @@ -134,8 +133,8 @@ contentPrekeySignature, notificationsPrekey, notificationsPrekeySignature, - contentOneTimeKey, - notificationsOneTimeKey, + contentOneTimeKeys, + notificationsOneTimeKeys, ); await Promise.all([ fetchCallUpdateOlmAccount('content', markKeysAsPublished), @@ -153,8 +152,8 @@ contentPrekeySignature, notificationsPrekey, notificationsPrekeySignature, - contentOneTimeKey, - notificationsOneTimeKey, + contentOneTimeKeys, + notificationsOneTimeKeys, ); await Promise.all([ fetchCallUpdateOlmAccount('content', markKeysAsPublished), diff --git a/keyserver/src/utils/olm-utils.js b/keyserver/src/utils/olm-utils.js --- a/keyserver/src/utils/olm-utils.js +++ b/keyserver/src/utils/olm-utils.js @@ -9,12 +9,9 @@ import { getRustAPI } from 'rust-node-addon'; import uuid from 'uuid'; -import { - olmEncryptedMessageTypes, - type OLMOneTimeKeys, -} from 'lib/types/crypto-types.js'; +import { getOneTimeKeyValuesFromBlob } from 'lib/shared/crypto-utils.js'; +import { olmEncryptedMessageTypes } from 'lib/types/crypto-types.js'; import { ServerError } from 'lib/utils/errors.js'; -import { values } from 'lib/utils/objects.js'; import { fetchCallUpdateOlmAccount } from '../updaters/olm-account-updater.js'; import { fetchIdentityInfo } from '../user/identity.js'; @@ -108,12 +105,6 @@ } } -function getOneTimeKeyValues(keyBlob: string): $ReadOnlyArray { - const content: OLMOneTimeKeys = JSON.parse(keyBlob); - const keys: $ReadOnlyArray = values(content.curve25519); - return keys; -} - async function uploadNewOneTimeKeys(numberOfKeys: number) { const [rustAPI, identityInfo] = await Promise.all([ getRustAPI(), @@ -126,7 +117,7 @@ await fetchCallUpdateOlmAccount('content', (contentAccount: OlmAccount) => { contentAccount.generate_one_time_keys(numberOfKeys); - const contentOneTimeKeys = getOneTimeKeyValues( + const contentOneTimeKeys = getOneTimeKeyValuesFromBlob( contentAccount.one_time_keys(), ); const deviceID = JSON.parse(contentAccount.identity_keys()).curve25519; @@ -135,7 +126,7 @@ 'notifications', async (notifAccount: OlmAccount) => { notifAccount.generate_one_time_keys(numberOfKeys); - const notifOneTimeKeys = getOneTimeKeyValues( + const notifOneTimeKeys = getOneTimeKeyValuesFromBlob( notifAccount.one_time_keys(), ); await rustAPI.uploadOneTimeKeys( @@ -160,6 +151,5 @@ unpickleOlmAccount, unpickleOlmSession, validateAccountPrekey, - getOneTimeKeyValues, uploadNewOneTimeKeys, }; diff --git a/lib/shared/crypto-utils.js b/lib/shared/crypto-utils.js new file mode 100644 --- /dev/null +++ b/lib/shared/crypto-utils.js @@ -0,0 +1,17 @@ +//@flow + +import type { OLMOneTimeKeys } from '../types/crypto-types'; +import { values } from '../utils/objects.js'; + +function getOneTimeKeyValues( + oneTimeKeys: OLMOneTimeKeys, +): $ReadOnlyArray { + return values(oneTimeKeys.curve25519); +} + +function getOneTimeKeyValuesFromBlob(keyBlob: string): $ReadOnlyArray { + const oneTimeKeys: OLMOneTimeKeys = JSON.parse(keyBlob); + return getOneTimeKeyValues(oneTimeKeys); +} + +export { getOneTimeKeyValues, getOneTimeKeyValuesFromBlob };