2021-05-25 22:40:04 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import {
|
2022-11-22 18:43:43 +00:00
|
|
|
ErrorCode,
|
|
|
|
LibSignalErrorBase,
|
2021-05-25 22:40:04 +00:00
|
|
|
PreKeyBundle,
|
|
|
|
processPreKeyBundle,
|
|
|
|
ProtocolAddress,
|
|
|
|
PublicKey,
|
2022-03-24 21:47:21 +00:00
|
|
|
} from '@signalapp/libsignal-client';
|
2021-05-25 22:40:04 +00:00
|
|
|
|
2022-02-25 23:39:24 +00:00
|
|
|
import {
|
2022-11-22 18:43:43 +00:00
|
|
|
OutgoingIdentityKeyError,
|
2022-02-25 23:39:24 +00:00
|
|
|
UnregisteredUserError,
|
|
|
|
HTTPError,
|
|
|
|
} from './Errors';
|
2021-05-25 22:40:04 +00:00
|
|
|
import { Sessions, IdentityKeys } from '../LibSignalStores';
|
2021-09-10 02:38:11 +00:00
|
|
|
import { Address } from '../types/Address';
|
|
|
|
import { QualifiedAddress } from '../types/QualifiedAddress';
|
|
|
|
import { UUID } from '../types/UUID';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ServerKeysType, WebAPIType } from './WebAPI';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2022-02-25 23:39:24 +00:00
|
|
|
import { isRecord } from '../util/isRecord';
|
2021-05-25 22:40:04 +00:00
|
|
|
|
|
|
|
export async function getKeysForIdentifier(
|
|
|
|
identifier: string,
|
|
|
|
server: WebAPIType,
|
|
|
|
devicesToUpdate?: Array<number>,
|
|
|
|
accessKey?: string
|
|
|
|
): Promise<{ accessKeyFailed?: boolean }> {
|
|
|
|
try {
|
|
|
|
const { keys, accessKeyFailed } = await getServerKeys(
|
|
|
|
identifier,
|
|
|
|
server,
|
|
|
|
accessKey
|
|
|
|
);
|
|
|
|
|
|
|
|
await handleServerKeys(identifier, keys, devicesToUpdate);
|
|
|
|
|
|
|
|
return {
|
|
|
|
accessKeyFailed,
|
|
|
|
};
|
|
|
|
} catch (error) {
|
2021-09-22 00:58:03 +00:00
|
|
|
if (error instanceof HTTPError && error.code === 404) {
|
2021-09-10 02:38:11 +00:00
|
|
|
const theirUuid = UUID.lookup(identifier);
|
|
|
|
|
|
|
|
if (theirUuid) {
|
|
|
|
await window.textsecure.storage.protocol.archiveAllSessions(theirUuid);
|
|
|
|
}
|
2021-11-20 15:58:38 +00:00
|
|
|
|
|
|
|
throw new UnregisteredUserError(identifier, error);
|
2021-05-25 22:40:04 +00:00
|
|
|
}
|
2021-11-20 15:58:38 +00:00
|
|
|
|
|
|
|
throw error;
|
2021-05-25 22:40:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getServerKeys(
|
|
|
|
identifier: string,
|
|
|
|
server: WebAPIType,
|
|
|
|
accessKey?: string
|
|
|
|
): Promise<{ accessKeyFailed?: boolean; keys: ServerKeysType }> {
|
|
|
|
try {
|
2022-02-25 23:39:24 +00:00
|
|
|
if (!accessKey) {
|
|
|
|
return {
|
|
|
|
keys: await server.getKeysForIdentifier(identifier),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-25 22:40:04 +00:00
|
|
|
return {
|
|
|
|
keys: await server.getKeysForIdentifierUnauth(identifier, undefined, {
|
|
|
|
accessKey,
|
|
|
|
}),
|
|
|
|
};
|
2022-02-25 23:39:24 +00:00
|
|
|
} catch (error: unknown) {
|
|
|
|
if (
|
|
|
|
accessKey &&
|
|
|
|
isRecord(error) &&
|
|
|
|
typeof error.code === 'number' &&
|
|
|
|
(error.code === 401 || error.code === 403)
|
|
|
|
) {
|
2021-05-25 22:40:04 +00:00
|
|
|
return {
|
|
|
|
accessKeyFailed: true,
|
|
|
|
keys: await server.getKeysForIdentifier(identifier),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleServerKeys(
|
|
|
|
identifier: string,
|
|
|
|
response: ServerKeysType,
|
|
|
|
devicesToUpdate?: Array<number>
|
|
|
|
): Promise<void> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const ourUuid = window.textsecure.storage.user.getCheckedUuid();
|
|
|
|
const sessionStore = new Sessions({ ourUuid });
|
|
|
|
const identityKeyStore = new IdentityKeys({ ourUuid });
|
2021-05-25 22:40:04 +00:00
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
response.devices.map(async device => {
|
|
|
|
const { deviceId, registrationId, preKey, signedPreKey } = device;
|
|
|
|
if (
|
|
|
|
devicesToUpdate !== undefined &&
|
|
|
|
!devicesToUpdate.includes(deviceId)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (device.registrationId === 0) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-05-25 22:40:04 +00:00
|
|
|
`handleServerKeys/${identifier}: Got device registrationId zero!`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!signedPreKey) {
|
|
|
|
throw new Error(
|
|
|
|
`getKeysForIdentifier/${identifier}: Missing signed prekey for deviceId ${deviceId}`
|
|
|
|
);
|
|
|
|
}
|
2021-09-10 02:38:11 +00:00
|
|
|
const theirUuid = UUID.checkedLookup(identifier);
|
|
|
|
const protocolAddress = ProtocolAddress.new(
|
|
|
|
theirUuid.toString(),
|
|
|
|
deviceId
|
|
|
|
);
|
2021-05-25 22:40:04 +00:00
|
|
|
const preKeyId = preKey?.keyId || null;
|
|
|
|
const preKeyObject = preKey
|
|
|
|
? PublicKey.deserialize(Buffer.from(preKey.publicKey))
|
|
|
|
: null;
|
|
|
|
const signedPreKeyObject = PublicKey.deserialize(
|
|
|
|
Buffer.from(signedPreKey.publicKey)
|
|
|
|
);
|
|
|
|
const identityKey = PublicKey.deserialize(
|
|
|
|
Buffer.from(response.identityKey)
|
|
|
|
);
|
|
|
|
|
|
|
|
const preKeyBundle = PreKeyBundle.new(
|
|
|
|
registrationId,
|
|
|
|
deviceId,
|
|
|
|
preKeyId,
|
|
|
|
preKeyObject,
|
|
|
|
signedPreKey.keyId,
|
|
|
|
signedPreKeyObject,
|
|
|
|
Buffer.from(signedPreKey.signature),
|
|
|
|
identityKey
|
|
|
|
);
|
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
const address = new QualifiedAddress(
|
|
|
|
ourUuid,
|
|
|
|
new Address(theirUuid, deviceId)
|
|
|
|
);
|
2022-11-22 18:43:43 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await window.textsecure.storage.protocol.enqueueSessionJob(
|
|
|
|
address,
|
|
|
|
() =>
|
|
|
|
processPreKeyBundle(
|
|
|
|
preKeyBundle,
|
|
|
|
protocolAddress,
|
|
|
|
sessionStore,
|
|
|
|
identityKeyStore
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
if (
|
|
|
|
error instanceof LibSignalErrorBase &&
|
|
|
|
error.code === ErrorCode.UntrustedIdentity
|
|
|
|
) {
|
2023-03-09 23:33:12 +00:00
|
|
|
throw new OutgoingIdentityKeyError(identifier, error);
|
2022-11-22 18:43:43 +00:00
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2021-05-25 22:40:04 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|