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,
|
2023-07-14 16:53:20 +00:00
|
|
|
KEMPublicKey,
|
2022-11-22 18:43:43 +00:00
|
|
|
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';
|
2023-08-10 16:43:33 +00:00
|
|
|
import type { ServiceIdString } from '../types/ServiceId';
|
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
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
export async function getKeysForServiceId(
|
|
|
|
serviceId: ServiceIdString,
|
2021-05-25 22:40:04 +00:00
|
|
|
server: WebAPIType,
|
|
|
|
devicesToUpdate?: Array<number>,
|
|
|
|
accessKey?: string
|
|
|
|
): Promise<{ accessKeyFailed?: boolean }> {
|
|
|
|
try {
|
|
|
|
const { keys, accessKeyFailed } = await getServerKeys(
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId,
|
2021-05-25 22:40:04 +00:00
|
|
|
server,
|
|
|
|
accessKey
|
|
|
|
);
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
await handleServerKeys(serviceId, keys, devicesToUpdate);
|
2021-05-25 22:40:04 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
accessKeyFailed,
|
|
|
|
};
|
|
|
|
} catch (error) {
|
2021-09-22 00:58:03 +00:00
|
|
|
if (error instanceof HTTPError && error.code === 404) {
|
2023-08-10 16:43:33 +00:00
|
|
|
await window.textsecure.storage.protocol.archiveAllSessions(serviceId);
|
2021-09-10 02:38:11 +00:00
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
throw new UnregisteredUserError(serviceId, 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(
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId: ServiceIdString,
|
2021-05-25 22:40:04 +00:00
|
|
|
server: WebAPIType,
|
|
|
|
accessKey?: string
|
|
|
|
): Promise<{ accessKeyFailed?: boolean; keys: ServerKeysType }> {
|
|
|
|
try {
|
2022-02-25 23:39:24 +00:00
|
|
|
if (!accessKey) {
|
|
|
|
return {
|
2023-08-10 16:43:33 +00:00
|
|
|
keys: await server.getKeysForServiceId(serviceId),
|
2022-02-25 23:39:24 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-25 22:40:04 +00:00
|
|
|
return {
|
2023-08-10 16:43:33 +00:00
|
|
|
keys: await server.getKeysForServiceIdUnauth(serviceId, undefined, {
|
2021-05-25 22:40:04 +00:00
|
|
|
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,
|
2023-08-10 16:43:33 +00:00
|
|
|
keys: await server.getKeysForServiceId(serviceId),
|
2021-05-25 22:40:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleServerKeys(
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId: ServiceIdString,
|
2021-05-25 22:40:04 +00:00
|
|
|
response: ServerKeysType,
|
|
|
|
devicesToUpdate?: Array<number>
|
|
|
|
): Promise<void> {
|
2023-08-10 16:43:33 +00:00
|
|
|
const ourAci = window.textsecure.storage.user.getCheckedAci();
|
|
|
|
const sessionStore = new Sessions({ ourServiceId: ourAci });
|
|
|
|
const identityKeyStore = new IdentityKeys({ ourServiceId: ourAci });
|
2021-05-25 22:40:04 +00:00
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
response.devices.map(async device => {
|
2023-07-14 16:53:20 +00:00
|
|
|
const { deviceId, registrationId, pqPreKey, preKey, signedPreKey } =
|
|
|
|
device;
|
2021-05-25 22:40:04 +00:00
|
|
|
if (
|
|
|
|
devicesToUpdate !== undefined &&
|
|
|
|
!devicesToUpdate.includes(deviceId)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (device.registrationId === 0) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2023-08-10 16:43:33 +00:00
|
|
|
`handleServerKeys/${serviceId}: Got device registrationId zero!`
|
2021-05-25 22:40:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!signedPreKey) {
|
|
|
|
throw new Error(
|
2023-08-10 16:43:33 +00:00
|
|
|
`getKeysForIdentifier/${serviceId}: Missing signed prekey for deviceId ${deviceId}`
|
2021-05-25 22:40:04 +00:00
|
|
|
);
|
|
|
|
}
|
2023-08-10 16:43:33 +00:00
|
|
|
const protocolAddress = ProtocolAddress.new(serviceId, 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)
|
|
|
|
);
|
|
|
|
|
2023-07-14 16:53:20 +00:00
|
|
|
const pqPreKeyId = pqPreKey?.keyId || null;
|
|
|
|
const pqPreKeyPublic = pqPreKey
|
|
|
|
? KEMPublicKey.deserialize(Buffer.from(pqPreKey.publicKey))
|
|
|
|
: null;
|
|
|
|
const pqPreKeySignature = pqPreKey
|
|
|
|
? Buffer.from(pqPreKey.signature)
|
|
|
|
: null;
|
|
|
|
|
2021-05-25 22:40:04 +00:00
|
|
|
const preKeyBundle = PreKeyBundle.new(
|
|
|
|
registrationId,
|
|
|
|
deviceId,
|
|
|
|
preKeyId,
|
|
|
|
preKeyObject,
|
|
|
|
signedPreKey.keyId,
|
|
|
|
signedPreKeyObject,
|
|
|
|
Buffer.from(signedPreKey.signature),
|
2023-07-14 16:53:20 +00:00
|
|
|
identityKey,
|
|
|
|
pqPreKeyId,
|
|
|
|
pqPreKeyPublic,
|
|
|
|
pqPreKeySignature
|
2021-05-25 22:40:04 +00:00
|
|
|
);
|
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
const address = new QualifiedAddress(
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci,
|
|
|
|
new Address(serviceId, deviceId)
|
2021-09-10 02:38:11 +00:00
|
|
|
);
|
2022-11-22 18:43:43 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await window.textsecure.storage.protocol.enqueueSessionJob(
|
|
|
|
address,
|
2023-08-10 16:43:33 +00:00
|
|
|
`handleServerKeys(${serviceId})`,
|
2022-11-22 18:43:43 +00:00
|
|
|
() =>
|
|
|
|
processPreKeyBundle(
|
|
|
|
preKeyBundle,
|
|
|
|
protocolAddress,
|
|
|
|
sessionStore,
|
|
|
|
identityKeyStore
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
if (
|
|
|
|
error instanceof LibSignalErrorBase &&
|
|
|
|
error.code === ErrorCode.UntrustedIdentity
|
|
|
|
) {
|
2023-08-10 16:43:33 +00:00
|
|
|
throw new OutgoingIdentityKeyError(serviceId, error);
|
2022-11-22 18:43:43 +00:00
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2021-05-25 22:40:04 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|