Signal-Desktop/ts/textsecure/WebAPI.ts

2782 lines
76 KiB
TypeScript
Raw Normal View History

// Copyright 2020-2021 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
/* eslint-disable no-param-reassign */
/* eslint-disable no-bitwise */
/* eslint-disable guard-for-in */
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-nested-ternary */
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { Response } from 'node-fetch';
import fetch from 'node-fetch';
import ProxyAgent from 'proxy-agent';
import { Agent } from 'https';
2020-09-04 01:25:19 +00:00
import pProps from 'p-props';
import type { Dictionary } from 'lodash';
import { compact, escapeRegExp, isNumber, mapValues, zipObject } from 'lodash';
2020-09-04 01:25:19 +00:00
import { createVerify } from 'crypto';
import { pki } from 'node-forge';
import is from '@sindresorhus/is';
import PQueue from 'p-queue';
import { v4 as getGuid } from 'uuid';
2021-05-25 22:40:04 +00:00
import { z } from 'zod';
2021-07-13 18:54:53 +00:00
import Long from 'long';
2021-09-28 23:38:55 +00:00
import { assert } from '../util/assert';
import * as durations from '../util/durations';
import { getUserAgent } from '../util/getUserAgent';
import { toWebSafeBase64 } from '../util/webSafeBase64';
import type { SocketStatus } from '../types/SocketStatus';
2021-09-24 00:49:05 +00:00
import { toLogFormat } from '../types/errors';
2021-07-09 19:36:10 +00:00
import { isPackIdValid, redactPackId } from '../types/Stickers';
2021-07-13 18:54:53 +00:00
import * as Bytes from '../Bytes';
2020-09-04 01:25:19 +00:00
import {
constantTimeEqual,
decryptAesGcm,
deriveSecrets,
2020-09-04 01:25:19 +00:00
encryptCdsDiscoveryRequest,
getRandomValue,
splitUuids,
} from '../Crypto';
import { calculateAgreement, generateKeyPair } from '../Curve';
2020-09-28 23:46:31 +00:00
import * as linkPreviewFetch from '../linkPreviews/linkPreviewFetch';
import type {
StorageServiceCallOptionsType,
StorageServiceCredentials,
} from '../textsecure.d';
import { SocketManager } from './SocketManager';
import type WebSocketResource from './WebsocketResources';
2021-06-22 14:46:42 +00:00
import { SignalService as Proto } from '../protobuf';
import { HTTPError } from './Errors';
import type MessageSender from './SendMessage';
import type { WebAPICredentials, IRequestHandler } from './Types.d';
import { handleStatusCode, translateError } from './Utils';
import * as log from '../logging/log';
// Note: this will break some code that expects to be able to use err.response when a
// web request fails, because it will force it to text. But it is very useful for
// debugging failed requests.
const DEBUG = false;
2020-09-04 01:25:19 +00:00
type SgxConstantsType = {
SGX_FLAGS_INITTED: Long;
SGX_FLAGS_DEBUG: Long;
SGX_FLAGS_MODE64BIT: Long;
SGX_FLAGS_PROVISION_KEY: Long;
SGX_FLAGS_EINITTOKEN_KEY: Long;
SGX_FLAGS_RESERVED: Long;
SGX_XFRM_LEGACY: Long;
SGX_XFRM_AVX: Long;
SGX_XFRM_RESERVED: Long;
};
let sgxConstantCache: SgxConstantsType | null = null;
function makeLong(value: string): Long {
2021-07-13 18:54:53 +00:00
return Long.fromString(value);
2020-09-04 01:25:19 +00:00
}
function getSgxConstants() {
if (sgxConstantCache) {
return sgxConstantCache;
}
sgxConstantCache = {
SGX_FLAGS_INITTED: makeLong('x0000000000000001L'),
SGX_FLAGS_DEBUG: makeLong('x0000000000000002L'),
SGX_FLAGS_MODE64BIT: makeLong('x0000000000000004L'),
SGX_FLAGS_PROVISION_KEY: makeLong('x0000000000000004L'),
SGX_FLAGS_EINITTOKEN_KEY: makeLong('x0000000000000004L'),
SGX_FLAGS_RESERVED: makeLong('xFFFFFFFFFFFFFFC8L'),
SGX_XFRM_LEGACY: makeLong('x0000000000000003L'),
SGX_XFRM_AVX: makeLong('x0000000000000006L'),
SGX_XFRM_RESERVED: makeLong('xFFFFFFFFFFFFFFF8L'),
};
return sgxConstantCache;
}
2020-08-20 22:15:50 +00:00
function _createRedactor(
...toReplace: ReadonlyArray<string | undefined>
): RedactUrl {
// NOTE: It would be nice to remove this cast, but TypeScript doesn't support
// it. However, there is [an issue][0] that discusses this in more detail.
// [0]: https://github.com/Microsoft/TypeScript/issues/16069
const stringsToReplace = toReplace.filter(Boolean) as Array<string>;
return href =>
stringsToReplace.reduce((result: string, stringToReplace: string) => {
const pattern = RegExp(escapeRegExp(stringToReplace), 'g');
const replacement = `[REDACTED]${stringToReplace.slice(-3)}`;
return result.replace(pattern, replacement);
}, href);
}
function _validateResponse(response: any, schema: any) {
try {
for (const i in schema) {
switch (schema[i]) {
case 'object':
case 'string':
case 'number':
if (typeof response[i] !== schema[i]) {
return false;
}
break;
default:
}
}
} catch (ex) {
return false;
}
return true;
}
const FIVE_MINUTES = 5 * durations.MINUTE;
type AgentCacheType = {
[name: string]: {
timestamp: number;
2021-10-06 16:25:22 +00:00
agent: ReturnType<typeof ProxyAgent> | Agent;
};
2018-11-07 19:20:43 +00:00
};
const agents: AgentCacheType = {};
2018-11-07 19:20:43 +00:00
function getContentType(response: Response) {
2019-01-16 03:03:56 +00:00
if (response.headers && response.headers.get) {
return response.headers.get('content-type');
}
return null;
}
type FetchHeaderListType = { [name: string]: string };
export type HeaderListType = { [name: string]: string | ReadonlyArray<string> };
type HTTPCodeType = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
2020-08-20 22:15:50 +00:00
type RedactUrl = (url: string) => string;
type PromiseAjaxOptionsType = {
socketManager?: SocketManager;
accessKey?: string;
2020-09-09 02:25:05 +00:00
basicAuth?: string;
certificateAuthority?: string;
contentType?: string;
2021-09-24 00:49:05 +00:00
data?: Uint8Array | string;
headers?: HeaderListType;
host?: string;
password?: string;
path?: string;
proxyUrl?: string;
2020-08-20 22:15:50 +00:00
redactUrl?: RedactUrl;
redirect?: 'error' | 'follow' | 'manual';
2021-09-24 00:49:05 +00:00
responseType?: 'json' | 'jsonwithdetails' | 'bytes' | 'byteswithdetails';
serverUrl?: string;
stack?: string;
timeout?: number;
type: HTTPCodeType;
unauthenticated?: boolean;
user?: string;
validateResponse?: any;
version: string;
};
2020-09-04 01:25:19 +00:00
type JSONWithDetailsType = {
2021-09-24 00:49:05 +00:00
data: unknown;
2020-09-09 02:25:05 +00:00
contentType: string | null;
response: Response;
};
2021-09-24 00:49:05 +00:00
type BytesWithDetailsType = {
data: Uint8Array;
2020-09-09 02:25:05 +00:00
contentType: string | null;
2020-09-04 01:25:19 +00:00
response: Response;
};
2021-05-25 22:40:04 +00:00
export const multiRecipient200ResponseSchema = z
.object({
uuids404: z.array(z.string()).optional(),
needsSync: z.boolean().optional(),
})
.passthrough();
export type MultiRecipient200ResponseType = z.infer<
typeof multiRecipient200ResponseSchema
>;
export const multiRecipient409ResponseSchema = z.array(
z
.object({
uuid: z.string(),
devices: z
.object({
missingDevices: z.array(z.number()).optional(),
extraDevices: z.array(z.number()).optional(),
})
.passthrough(),
})
.passthrough()
);
export type MultiRecipient409ResponseType = z.infer<
typeof multiRecipient409ResponseSchema
>;
export const multiRecipient410ResponseSchema = z.array(
z
.object({
uuid: z.string(),
devices: z
.object({
staleDevices: z.array(z.number()).optional(),
})
.passthrough(),
})
.passthrough()
);
export type MultiRecipient410ResponseType = z.infer<
typeof multiRecipient410ResponseSchema
>;
function isSuccess(status: number): boolean {
return status >= 0 && status < 400;
}
function getHostname(url: string): string {
const urlObject = new URL(url);
return urlObject.hostname;
}
async function _promiseAjax(
providedUrl: string | null,
options: PromiseAjaxOptionsType
2021-09-24 00:49:05 +00:00
): Promise<unknown> {
const { proxyUrl, socketManager } = options;
const url = providedUrl || `${options.host}/${options.path}`;
const logType = socketManager ? '(WS)' : '(REST)';
const redactedURL = options.redactUrl ? options.redactUrl(url) : url;
const unauthLabel = options.unauthenticated ? ' (unauth)' : '';
log.info(`${options.type} ${logType} ${redactedURL}${unauthLabel}`);
2021-06-09 22:28:54 +00:00
const timeout = typeof options.timeout === 'number' ? options.timeout : 10000;
const agentType = options.unauthenticated ? 'unauth' : 'auth';
const cacheKey = `${proxyUrl}-${agentType}`;
const { timestamp } = agents[cacheKey] || { timestamp: null };
if (!timestamp || timestamp + FIVE_MINUTES < Date.now()) {
if (timestamp) {
log.info(`Cycling agent for type ${cacheKey}`);
}
agents[cacheKey] = {
agent: proxyUrl
? new ProxyAgent(proxyUrl)
: new Agent({ keepAlive: true }),
timestamp: Date.now(),
};
2021-06-09 22:28:54 +00:00
}
const { agent } = agents[cacheKey];
const fetchOptions = {
method: options.type,
body: options.data,
headers: {
'User-Agent': getUserAgent(options.version),
'X-Signal-Agent': 'OWD',
...options.headers,
} as FetchHeaderListType,
redirect: options.redirect,
agent,
ca: options.certificateAuthority,
timeout,
};
2021-06-09 22:28:54 +00:00
2021-09-24 00:49:05 +00:00
if (fetchOptions.body instanceof Uint8Array) {
// node-fetch doesn't support Uint8Array, only node Buffer
const contentLength = fetchOptions.body.byteLength;
fetchOptions.body = Buffer.from(fetchOptions.body);
// node-fetch doesn't set content-length like S3 requires
fetchOptions.headers['Content-Length'] = contentLength.toString();
}
const { accessKey, basicAuth, unauthenticated } = options;
if (basicAuth) {
fetchOptions.headers.Authorization = `Basic ${basicAuth}`;
} else if (unauthenticated) {
if (!accessKey) {
throw new Error(
'_promiseAjax: mode is unauthenticated, but accessKey was not provided'
);
2019-01-16 03:03:56 +00:00
}
// Access key is already a Base64 string
fetchOptions.headers['Unidentified-Access-Key'] = accessKey;
} else if (options.user && options.password) {
2021-09-24 00:49:05 +00:00
const auth = Bytes.toBase64(
Bytes.fromString(`${options.user}:${options.password}`)
);
fetchOptions.headers.Authorization = `Basic ${auth}`;
}
if (options.contentType) {
fetchOptions.headers['Content-Type'] = options.contentType;
}
let response: Response;
2021-09-24 00:49:05 +00:00
let result: string | Uint8Array | unknown;
try {
response = socketManager
? await socketManager.fetch(url, fetchOptions)
: await fetch(url, fetchOptions);
if (
options.serverUrl &&
getHostname(options.serverUrl) === getHostname(url)
) {
await handleStatusCode(response.status);
2018-11-07 19:20:43 +00:00
if (!unauthenticated && response.status === 401) {
log.error('Got 401 from Signal Server. We might be unlinked.');
window.Whisper.events.trigger('mightBeUnlinked');
2018-11-07 19:20:43 +00:00
}
}
if (DEBUG && !isSuccess(response.status)) {
result = await response.text();
} else if (
(options.responseType === 'json' ||
options.responseType === 'jsonwithdetails') &&
/^application\/json(;.*)?$/.test(
response.headers.get('Content-Type') || ''
)
) {
result = await response.json();
} else if (
2021-09-24 00:49:05 +00:00
options.responseType === 'bytes' ||
options.responseType === 'byteswithdetails'
) {
2021-09-24 00:49:05 +00:00
result = await response.buffer();
} else {
result = await response.textConverted();
}
} catch (e) {
log.error(options.type, logType, redactedURL, 0, 'Error');
const stack = `${e.stack}\nInitial stack:\n${options.stack}`;
throw makeHTTPError('promiseAjax catch', 0, {}, e.toString(), stack);
}
if (!isSuccess(response.status)) {
log.error(options.type, logType, redactedURL, response.status, 'Error');
throw makeHTTPError(
'promiseAjax: error response',
response.status,
response.headers.raw(),
result,
options.stack
);
}
if (
options.responseType === 'json' ||
options.responseType === 'jsonwithdetails'
) {
if (options.validateResponse) {
if (!_validateResponse(result, options.validateResponse)) {
log.error(options.type, logType, redactedURL, response.status, 'Error');
throw makeHTTPError(
'promiseAjax: invalid response',
response.status,
response.headers.raw(),
result,
options.stack
);
}
}
}
2019-01-16 03:03:56 +00:00
log.info(options.type, logType, redactedURL, response.status, 'Success');
2021-09-24 00:49:05 +00:00
if (options.responseType === 'byteswithdetails') {
assert(result instanceof Uint8Array, 'Expected Uint8Array result');
const fullResult: BytesWithDetailsType = {
data: result,
contentType: getContentType(response),
response,
};
2020-09-09 02:25:05 +00:00
return fullResult;
}
if (options.responseType === 'jsonwithdetails') {
const fullResult: JSONWithDetailsType = {
data: result,
contentType: getContentType(response),
response,
};
2019-01-16 03:03:56 +00:00
return fullResult;
}
return result;
}
async function _retryAjax(
url: string | null,
options: PromiseAjaxOptionsType,
providedLimit?: number,
providedCount?: number
2021-09-24 00:49:05 +00:00
): Promise<unknown> {
const count = (providedCount || 0) + 1;
const limit = providedLimit || 3;
2021-09-24 00:49:05 +00:00
try {
return await _promiseAjax(url, options);
} catch (e) {
2021-09-22 00:58:03 +00:00
if (e instanceof HTTPError && e.code === -1 && count < limit) {
return new Promise(resolve => {
setTimeout(() => {
resolve(_retryAjax(url, options, limit, count));
}, 1000);
});
}
throw e;
2021-09-24 00:49:05 +00:00
}
}
2021-09-24 00:49:05 +00:00
function _outerAjax(
providedUrl: string | null,
options: PromiseAjaxOptionsType & { responseType: 'json' }
): Promise<unknown>;
function _outerAjax(
providedUrl: string | null,
options: PromiseAjaxOptionsType & { responseType: 'jsonwithdetails' }
): Promise<JSONWithDetailsType>;
function _outerAjax(
providedUrl: string | null,
options: PromiseAjaxOptionsType & { responseType?: 'bytes' }
): Promise<Uint8Array>;
function _outerAjax(
providedUrl: string | null,
options: PromiseAjaxOptionsType & { responseType: 'byteswithdetails' }
): Promise<BytesWithDetailsType>;
function _outerAjax(
providedUrl: string | null,
options: PromiseAjaxOptionsType
): Promise<unknown>;
async function _outerAjax(
url: string | null,
options: PromiseAjaxOptionsType
): Promise<unknown> {
options.stack = new Error().stack; // just in case, save stack here.
return _retryAjax(url, options);
}
function makeHTTPError(
message: string,
providedCode: number,
headers: HeaderListType,
2021-09-24 00:49:05 +00:00
response: unknown,
stack?: string
) {
return new HTTPError(message, {
code: providedCode,
headers,
response,
stack,
});
}
const URL_CALLS = {
accounts: 'v1/accounts',
attachmentId: 'v2/attachments/form/upload',
2020-09-09 02:25:05 +00:00
attestation: 'v1/attestation',
challenge: 'v1/challenge',
2020-09-09 02:25:05 +00:00
config: 'v1/config',
deliveryCert: 'v1/certificate/delivery',
devices: 'v1/devices',
2020-09-09 02:25:05 +00:00
directoryAuth: 'v1/directory/auth',
discovery: 'v1/discovery',
2020-11-20 17:30:45 +00:00
getGroupAvatarUpload: 'v1/groups/avatar/form',
2020-09-09 02:25:05 +00:00
getGroupCredentials: 'v1/certificate/group',
getIceServers: 'v1/accounts/turn',
getStickerPackUpload: 'v1/sticker/pack/form',
groupLog: 'v1/groups/logs',
groups: 'v1/groups',
groupsViaLink: 'v1/groups/join',
2020-11-13 19:57:55 +00:00
groupToken: 'v1/groups/token',
keys: 'v2/keys',
messages: 'v1/messages',
2021-05-25 22:40:04 +00:00
multiRecipient: 'v1/messages/multi_recipient',
profile: 'v1/profile',
registerCapabilities: 'v1/devices/capabilities',
reportMessage: 'v1/messages/report',
signed: 'v2/keys/signed',
storageManifest: 'v1/storage/manifest',
storageModify: 'v1/storage/',
storageRead: 'v1/storage/read',
storageToken: 'v1/storage/auth',
supportUnauthenticatedDelivery: 'v1/devices/unauthenticated_delivery',
2020-09-09 02:25:05 +00:00
updateDeviceName: 'v1/accounts/name',
username: 'v1/accounts/username',
whoami: 'v1/accounts/whoami',
};
const WEBSOCKET_CALLS = new Set<keyof typeof URL_CALLS>([
// MessageController
'messages',
'multiRecipient',
'reportMessage',
// ProfileController
'profile',
// AttachmentControllerV2
'attachmentId',
// RemoteConfigController
'config',
2021-08-04 00:37:17 +00:00
// Certificate
'deliveryCert',
'getGroupCredentials',
// Devices
'devices',
'registerCapabilities',
'supportUnauthenticatedDelivery',
// Directory
'directoryAuth',
// Storage
'storageToken',
]);
type InitializeOptionsType = {
url: string;
storageUrl: string;
2020-09-04 01:25:19 +00:00
directoryEnclaveId: string;
directoryTrustAnchor: string;
directoryUrl: string;
cdnUrlObject: {
readonly '0': string;
readonly [propName: string]: string;
};
certificateAuthority: string;
contentProxyUrl: string;
proxyUrl: string;
version: string;
};
2021-09-28 23:38:55 +00:00
export type MessageType = Readonly<{
type: number;
destinationDeviceId: number;
destinationRegistrationId: number;
content: string;
}>;
type AjaxOptionsType = {
accessKey?: string;
2020-09-09 02:25:05 +00:00
basicAuth?: string;
call: keyof typeof URL_CALLS;
contentType?: string;
2021-09-24 00:49:05 +00:00
data?: Uint8Array | Buffer | Uint8Array | string;
2021-05-25 22:40:04 +00:00
headers?: HeaderListType;
host?: string;
httpType: HTTPCodeType;
2021-09-24 00:49:05 +00:00
jsonData?: unknown;
password?: string;
2020-08-20 22:15:50 +00:00
redactUrl?: RedactUrl;
2021-09-24 00:49:05 +00:00
responseType?: 'json' | 'bytes' | 'byteswithdetails';
2021-09-22 00:58:03 +00:00
schema?: unknown;
timeout?: number;
unauthenticated?: boolean;
urlParameters?: string;
username?: string;
validateResponse?: any;
};
2021-08-19 00:13:32 +00:00
export type WebAPIConnectOptionsType = WebAPICredentials & {
useWebSocket?: boolean;
2021-08-19 00:13:32 +00:00
};
export type WebAPIConnectType = {
2021-08-19 00:13:32 +00:00
connect: (options: WebAPIConnectOptionsType) => WebAPIType;
};
2020-11-20 17:30:45 +00:00
export type CapabilitiesType = {
2021-07-20 20:18:35 +00:00
announcementGroup: boolean;
2020-11-20 17:30:45 +00:00
gv2: boolean;
'gv1-migration': boolean;
2021-05-25 22:40:04 +00:00
senderKey: boolean;
2021-09-09 20:53:58 +00:00
changeNumber: boolean;
2020-11-20 17:30:45 +00:00
};
export type CapabilitiesUploadType = {
announcementGroup: true;
'gv2-3': true;
'gv1-migration': true;
senderKey: true;
2021-09-09 20:53:58 +00:00
changeNumber: true;
2020-11-20 17:30:45 +00:00
};
2021-09-24 00:49:05 +00:00
type StickerPackManifestType = Uint8Array;
2020-09-09 02:25:05 +00:00
export type GroupCredentialType = {
credential: string;
redemptionTime: number;
};
export type GroupCredentialsType = {
groupPublicParamsHex: string;
authCredentialPresentationHex: string;
};
export type GroupLogResponseType = {
currentRevision?: number;
start?: number;
end?: number;
2021-06-22 14:46:42 +00:00
changes: Proto.GroupChanges;
2020-09-09 02:25:05 +00:00
};
2021-07-19 19:26:06 +00:00
export type ProfileRequestDataType = {
about: string | null;
aboutEmoji: string | null;
avatar: boolean;
commitment: string;
name: string;
paymentAddress: string | null;
version: string;
};
const uploadAvatarHeadersZod = z
.object({
acl: z.string(),
algorithm: z.string(),
credential: z.string(),
date: z.string(),
key: z.string(),
policy: z.string(),
signature: z.string(),
})
.passthrough();
export type UploadAvatarHeadersType = z.infer<typeof uploadAvatarHeadersZod>;
2021-09-22 00:58:03 +00:00
export type ProfileType = Readonly<{
identityKey?: string;
name?: string;
about?: string;
aboutEmoji?: string;
avatar?: string;
unidentifiedAccess?: string;
unrestrictedUnidentifiedAccess?: string;
username?: string;
uuid?: string;
credential?: string;
capabilities?: CapabilitiesType;
paymentAddress?: string;
2021-09-22 00:58:03 +00:00
}>;
2021-09-24 00:49:05 +00:00
export type GetIceServersResultType = Readonly<{
username: string;
password: string;
urls: ReadonlyArray<string>;
}>;
export type GetDevicesResultType = ReadonlyArray<
Readonly<{
id: number;
name: string;
lastSeen: number;
created: number;
}>
>;
export type GetSenderCertificateResultType = Readonly<{ certificate: string }>;
export type MakeProxiedRequestResultType =
| Uint8Array
| {
result: BytesWithDetailsType;
totalSize: number;
};
export type WhoamiResultType = Readonly<{
uuid?: string;
number?: string;
}>;
export type WebAPIType = {
confirmCode: (
number: string,
code: string,
newPassword: string,
registrationId: number,
deviceName?: string | null,
2021-09-24 00:49:05 +00:00
options?: { accessKey?: Uint8Array; uuid?: string }
) => Promise<{ uuid?: string; deviceId?: number }>;
2020-09-09 02:25:05 +00:00
createGroup: (
2021-06-22 14:46:42 +00:00
group: Proto.IGroup,
2020-09-09 02:25:05 +00:00
options: GroupCredentialsType
) => Promise<void>;
deleteUsername: () => Promise<void>;
2021-09-24 00:49:05 +00:00
getAttachment: (cdnKey: string, cdnNumber?: number) => Promise<Uint8Array>;
getAvatar: (path: string) => Promise<Uint8Array>;
getDevices: () => Promise<GetDevicesResultType>;
2021-06-22 14:46:42 +00:00
getGroup: (options: GroupCredentialsType) => Promise<Proto.Group>;
getGroupFromLink: (
inviteLinkPassword: string,
auth: GroupCredentialsType
2021-06-22 14:46:42 +00:00
) => Promise<Proto.GroupJoinInfo>;
2021-09-24 00:49:05 +00:00
getGroupAvatar: (key: string) => Promise<Uint8Array>;
2020-09-09 02:25:05 +00:00
getGroupCredentials: (
startDay: number,
endDay: number
) => Promise<Array<GroupCredentialType>>;
2020-11-13 19:57:55 +00:00
getGroupExternalCredential: (
options: GroupCredentialsType
2021-06-22 14:46:42 +00:00
) => Promise<Proto.GroupExternalCredential>;
2020-09-09 02:25:05 +00:00
getGroupLog: (
startVersion: number,
options: GroupCredentialsType
) => Promise<GroupLogResponseType>;
2021-09-24 00:49:05 +00:00
getIceServers: () => Promise<GetIceServersResultType>;
getKeysForIdentifier: (
identifier: string,
deviceId?: number
) => Promise<ServerKeysType>;
getKeysForIdentifierUnauth: (
identifier: string,
deviceId?: number,
options?: { accessKey?: string }
) => Promise<ServerKeysType>;
getMyKeys: () => Promise<number>;
getProfile: (
identifier: string,
options: {
profileKeyVersion?: string;
profileKeyCredentialRequest?: string;
}
2021-09-22 00:58:03 +00:00
) => Promise<ProfileType>;
getProfileUnauth: (
identifier: string,
options: {
accessKey: string;
profileKeyVersion?: string;
profileKeyCredentialRequest?: string;
}
2021-09-22 00:58:03 +00:00
) => Promise<ProfileType>;
getProvisioningResource: (
handler: IRequestHandler
) => Promise<WebSocketResource>;
2021-04-08 16:24:21 +00:00
getSenderCertificate: (
withUuid?: boolean
2021-09-24 00:49:05 +00:00
) => Promise<GetSenderCertificateResultType>;
getSticker: (packId: string, stickerId: number) => Promise<Uint8Array>;
getStickerPackManifest: (packId: string) => Promise<StickerPackManifestType>;
getStorageCredentials: MessageSender['getStorageCredentials'];
getStorageManifest: MessageSender['getStorageManifest'];
getStorageRecords: MessageSender['getStorageRecords'];
2020-09-04 01:25:19 +00:00
getUuidsForE164s: (
e164s: ReadonlyArray<string>
) => Promise<Dictionary<string | null>>;
2020-09-28 23:46:31 +00:00
fetchLinkPreviewMetadata: (
href: string,
abortSignal: AbortSignal
) => Promise<null | linkPreviewFetch.LinkPreviewMetadata>;
fetchLinkPreviewImage: (
href: string,
abortSignal: AbortSignal
) => Promise<null | linkPreviewFetch.LinkPreviewImage>;
makeProxiedRequest: (
targetUrl: string,
options?: ProxiedRequestOptionsType
2021-09-24 00:49:05 +00:00
) => Promise<MakeProxiedRequestResultType>;
2020-11-13 19:57:55 +00:00
makeSfuRequest: (
targetUrl: string,
type: HTTPCodeType,
headers: HeaderListType,
2021-09-24 00:49:05 +00:00
body: Uint8Array | undefined
) => Promise<BytesWithDetailsType>;
2020-09-09 02:25:05 +00:00
modifyGroup: (
2021-06-22 14:46:42 +00:00
changes: Proto.GroupChange.IActions,
options: GroupCredentialsType,
inviteLinkBase64?: string
2021-06-22 14:46:42 +00:00
) => Promise<Proto.IGroupChange>;
2020-09-09 00:56:23 +00:00
modifyStorageRecords: MessageSender['modifyStorageRecords'];
2021-09-24 00:49:05 +00:00
putAttachment: (encryptedBin: Uint8Array) => Promise<string>;
2021-07-19 19:26:06 +00:00
putProfile: (
jsonData: ProfileRequestDataType
) => Promise<UploadAvatarHeadersType | undefined>;
putStickers: (
2021-09-24 00:49:05 +00:00
encryptedManifest: Uint8Array,
encryptedStickers: Array<Uint8Array>,
onProgress?: () => void
) => Promise<string>;
putUsername: (newUsername: string) => Promise<void>;
registerCapabilities: (capabilities: CapabilitiesUploadType) => Promise<void>;
registerKeys: (genKeys: KeysType) => Promise<void>;
2021-09-22 00:58:03 +00:00
registerSupportForUnauthenticatedDelivery: () => Promise<void>;
reportMessage: (senderE164: string, serverGuid: string) => Promise<void>;
2021-09-22 00:58:03 +00:00
requestVerificationSMS: (number: string) => Promise<void>;
requestVerificationVoice: (number: string) => Promise<void>;
sendMessages: (
destination: string,
2021-09-28 23:38:55 +00:00
messageArray: ReadonlyArray<MessageType>,
timestamp: number,
online?: boolean
) => Promise<void>;
sendMessagesUnauth: (
destination: string,
2021-09-28 23:38:55 +00:00
messageArray: ReadonlyArray<MessageType>,
timestamp: number,
online?: boolean,
options?: { accessKey?: string }
) => Promise<void>;
2021-05-25 22:40:04 +00:00
sendWithSenderKey: (
2021-09-24 00:49:05 +00:00
payload: Uint8Array,
accessKeys: Uint8Array,
2021-05-25 22:40:04 +00:00
timestamp: number,
online?: boolean
) => Promise<MultiRecipient200ResponseType>;
setSignedPreKey: (signedPreKey: SignedPreKeyType) => Promise<void>;
updateDeviceName: (deviceName: string) => Promise<void>;
2021-07-19 19:26:06 +00:00
uploadAvatar: (
uploadAvatarRequestHeaders: UploadAvatarHeadersType,
2021-09-24 00:49:05 +00:00
avatarData: Uint8Array
2021-07-19 19:26:06 +00:00
) => Promise<string>;
2020-09-09 02:25:05 +00:00
uploadGroupAvatar: (
2021-06-22 14:46:42 +00:00
avatarData: Uint8Array,
2020-09-09 02:25:05 +00:00
options: GroupCredentialsType
) => Promise<string>;
2021-09-24 00:49:05 +00:00
whoami: () => Promise<WhoamiResultType>;
2021-09-22 00:58:03 +00:00
sendChallengeResponse: (challengeResponse: ChallengeType) => Promise<void>;
getConfig: () => Promise<
Array<{ name: string; enabled: boolean; value: string | null }>
>;
authenticate: (credentials: WebAPICredentials) => Promise<void>;
logout: () => Promise<void>;
getSocketStatus: () => SocketStatus;
registerRequestHandler: (handler: IRequestHandler) => void;
unregisterRequestHandler: (handler: IRequestHandler) => void;
checkSockets: () => void;
onOnline: () => Promise<void>;
onOffline: () => Promise<void>;
};
export type SignedPreKeyType = {
keyId: number;
2021-09-24 00:49:05 +00:00
publicKey: Uint8Array;
signature: Uint8Array;
};
export type KeysType = {
2021-09-24 00:49:05 +00:00
identityKey: Uint8Array;
signedPreKey: SignedPreKeyType;
preKeys: Array<{
keyId: number;
2021-09-24 00:49:05 +00:00
publicKey: Uint8Array;
}>;
};
export type ServerKeysType = {
devices: Array<{
deviceId: number;
registrationId: number;
signedPreKey: {
keyId: number;
2021-09-24 00:49:05 +00:00
publicKey: Uint8Array;
signature: Uint8Array;
};
preKey?: {
keyId: number;
2021-09-24 00:49:05 +00:00
publicKey: Uint8Array;
};
}>;
2021-09-24 00:49:05 +00:00
identityKey: Uint8Array;
};
export type ChallengeType = {
readonly type: 'recaptcha';
readonly token: string;
readonly captcha: string;
};
export type ProxiedRequestOptionsType = {
2021-09-24 00:49:05 +00:00
returnUint8Array?: boolean;
start?: number;
end?: number;
};
// We first set up the data that won't change during this session of the app
export function initialize({
2019-01-16 03:03:56 +00:00
url,
storageUrl,
2020-09-04 01:25:19 +00:00
directoryEnclaveId,
directoryTrustAnchor,
directoryUrl,
cdnUrlObject,
2019-01-16 03:03:56 +00:00
certificateAuthority,
contentProxyUrl,
proxyUrl,
version,
}: InitializeOptionsType): WebAPIConnectType {
if (!is.string(url)) {
throw new Error('WebAPI.initialize: Invalid server url');
}
if (!is.string(storageUrl)) {
throw new Error('WebAPI.initialize: Invalid storageUrl');
}
2020-09-04 01:25:19 +00:00
if (!is.string(directoryEnclaveId)) {
throw new Error('WebAPI.initialize: Invalid directory enclave id');
}
if (!is.string(directoryTrustAnchor)) {
throw new Error('WebAPI.initialize: Invalid directory enclave id');
}
if (!is.string(directoryUrl)) {
throw new Error('WebAPI.initialize: Invalid directory url');
}
if (!is.object(cdnUrlObject)) {
throw new Error('WebAPI.initialize: Invalid cdnUrlObject');
}
if (!is.string(cdnUrlObject['0'])) {
throw new Error('WebAPI.initialize: Missing CDN 0 configuration');
}
if (!is.string(cdnUrlObject['2'])) {
throw new Error('WebAPI.initialize: Missing CDN 2 configuration');
}
if (!is.string(certificateAuthority)) {
throw new Error('WebAPI.initialize: Invalid certificateAuthority');
}
2019-01-16 03:03:56 +00:00
if (!is.string(contentProxyUrl)) {
throw new Error('WebAPI.initialize: Invalid contentProxyUrl');
}
if (proxyUrl && !is.string(proxyUrl)) {
throw new Error('WebAPI.initialize: Invalid proxyUrl');
}
if (!is.string(version)) {
throw new Error('WebAPI.initialize: Invalid version');
}
// Thanks to function-hoisting, we can put this return statement before all of the
// below function definitions.
return {
connect,
};
// Then we connect to the server with user-specific information. This is the only API
// exposed to the browser context, ensuring that it can't connect to arbitrary
// locations.
function connect({
username: initialUsername,
password: initialPassword,
useWebSocket = true,
2021-08-19 00:13:32 +00:00
}: WebAPIConnectOptionsType) {
let username = initialUsername;
let password = initialPassword;
const PARSE_RANGE_HEADER = /\/(\d+)$/;
2020-09-09 02:25:05 +00:00
const PARSE_GROUP_LOG_RANGE_HEADER = /$versions (\d{1,10})-(\d{1,10})\/(d{1,10})/;
const socketManager = new SocketManager({
url,
certificateAuthority,
version,
proxyUrl,
});
2021-09-16 20:18:42 +00:00
socketManager.on('statusChange', () => {
window.Whisper.events.trigger('socketStatusChange');
});
socketManager.on('authError', () => {
window.Whisper.events.trigger('unlinkAndDisconnect');
});
if (useWebSocket) {
2021-08-19 00:13:32 +00:00
socketManager.authenticate({ username, password });
}
let fetchForLinkPreviews: linkPreviewFetch.FetchFn;
if (proxyUrl) {
const agent = new ProxyAgent(proxyUrl);
fetchForLinkPreviews = (href, init) => fetch(href, { ...init, agent });
} else {
fetchForLinkPreviews = fetch;
}
// Thanks, function hoisting!
return {
getSocketStatus,
checkSockets,
onOnline,
onOffline,
registerRequestHandler,
unregisterRequestHandler,
authenticate,
logout,
confirmCode,
2020-09-09 02:25:05 +00:00
createGroup,
deleteUsername,
fetchLinkPreviewImage,
fetchLinkPreviewMetadata,
getAttachment,
getAvatar,
2020-09-09 02:25:05 +00:00
getConfig,
getDevices,
2020-09-09 02:25:05 +00:00
getGroup,
getGroupAvatar,
getGroupCredentials,
2020-11-13 19:57:55 +00:00
getGroupExternalCredential,
getGroupFromLink,
2020-09-09 02:25:05 +00:00
getGroupLog,
2020-06-04 18:16:19 +00:00
getIceServers,
getKeysForIdentifier,
getKeysForIdentifierUnauth,
getMyKeys,
getProfile,
getProfileUnauth,
getProvisioningResource,
2019-01-16 03:03:56 +00:00
getSenderCertificate,
getSticker,
getStickerPackManifest,
getStorageCredentials,
getStorageManifest,
getStorageRecords,
2020-09-04 01:25:19 +00:00
getUuidsForE164s,
2019-01-16 03:03:56 +00:00
makeProxiedRequest,
2020-11-13 19:57:55 +00:00
makeSfuRequest,
2020-09-09 02:25:05 +00:00
modifyGroup,
2020-09-09 00:56:23 +00:00
modifyStorageRecords,
putAttachment,
2021-07-19 19:26:06 +00:00
putProfile,
2019-12-17 20:25:57 +00:00
putStickers,
putUsername,
2020-09-09 02:25:05 +00:00
registerCapabilities,
registerKeys,
2019-01-16 03:03:56 +00:00
registerSupportForUnauthenticatedDelivery,
reportMessage,
requestVerificationSMS,
requestVerificationVoice,
sendMessages,
sendMessagesUnauth,
2021-05-25 22:40:04 +00:00
sendWithSenderKey,
setSignedPreKey,
updateDeviceName,
2021-07-19 19:26:06 +00:00
uploadAvatar,
2020-09-09 02:25:05 +00:00
uploadGroupAvatar,
whoami,
sendChallengeResponse,
};
2021-09-24 00:49:05 +00:00
function _ajax(
param: AjaxOptionsType & { responseType?: 'bytes' }
): Promise<Uint8Array>;
function _ajax(
param: AjaxOptionsType & { responseType: 'byteswithdetails' }
): Promise<BytesWithDetailsType>;
function _ajax(
param: AjaxOptionsType & { responseType: 'json' }
): Promise<unknown>;
async function _ajax(param: AjaxOptionsType): Promise<unknown> {
if (!param.urlParameters) {
param.urlParameters = '';
}
const useWebSocketForEndpoint =
useWebSocket && WEBSOCKET_CALLS.has(param.call);
2021-09-24 00:49:05 +00:00
const outerParams = {
socketManager: useWebSocketForEndpoint ? socketManager : undefined,
2020-09-09 02:25:05 +00:00
basicAuth: param.basicAuth,
certificateAuthority,
contentType: param.contentType || 'application/json; charset=utf-8',
2021-09-24 00:49:05 +00:00
data:
param.data ||
2021-09-28 23:38:55 +00:00
(param.jsonData ? JSON.stringify(param.jsonData) : undefined),
2021-05-25 22:40:04 +00:00
headers: param.headers,
host: param.host || url,
password: param.password || password,
path: URL_CALLS[param.call] + param.urlParameters,
proxyUrl,
responseType: param.responseType,
timeout: param.timeout,
type: param.httpType,
user: param.username || username,
2020-08-20 22:15:50 +00:00
redactUrl: param.redactUrl,
serverUrl: url,
validateResponse: param.validateResponse,
version,
unauthenticated: param.unauthenticated,
accessKey: param.accessKey,
2021-09-24 00:49:05 +00:00
};
try {
return await _outerAjax(null, outerParams);
} catch (e) {
2021-09-22 00:58:03 +00:00
if (!(e instanceof HTTPError)) {
throw e;
}
const translatedError = translateError(e);
2021-06-09 22:28:54 +00:00
if (translatedError) {
throw translatedError;
}
2021-09-24 00:49:05 +00:00
throw e;
}
}
async function whoami() {
2021-09-24 00:49:05 +00:00
return (await _ajax({
call: 'whoami',
httpType: 'GET',
responseType: 'json',
2021-09-24 00:49:05 +00:00
})) as WhoamiResultType;
}
async function sendChallengeResponse(challengeResponse: ChallengeType) {
2021-09-24 00:49:05 +00:00
await _ajax({
call: 'challenge',
httpType: 'PUT',
jsonData: challengeResponse,
});
}
async function authenticate({
username: newUsername,
password: newPassword,
}: WebAPICredentials) {
username = newUsername;
password = newPassword;
if (useWebSocket) {
2021-08-19 00:13:32 +00:00
await socketManager.authenticate({ username, password });
}
}
async function logout() {
username = '';
password = '';
if (useWebSocket) {
await socketManager.logout();
}
}
function getSocketStatus(): SocketStatus {
return socketManager.getStatus();
}
function checkSockets(): void {
// Intentionally not awaiting
socketManager.check();
}
async function onOnline(): Promise<void> {
await socketManager.onOnline();
}
async function onOffline(): Promise<void> {
await socketManager.onOffline();
}
function registerRequestHandler(handler: IRequestHandler): void {
socketManager.registerRequestHandler(handler);
}
function unregisterRequestHandler(handler: IRequestHandler): void {
socketManager.unregisterRequestHandler(handler);
}
2020-05-27 21:37:06 +00:00
async function getConfig() {
type ResType = {
config: Array<{ name: string; enabled: boolean; value: string | null }>;
2020-05-27 21:37:06 +00:00
};
2021-09-24 00:49:05 +00:00
const res = (await _ajax({
2020-05-27 21:37:06 +00:00
call: 'config',
httpType: 'GET',
responseType: 'json',
2021-09-24 00:49:05 +00:00
})) as ResType;
2020-05-27 21:37:06 +00:00
return res.config.filter(
({ name }: { name: string }) =>
name.startsWith('desktop.') || name.startsWith('global.')
2020-05-27 21:37:06 +00:00
);
}
async function getSenderCertificate(omitE164?: boolean) {
2021-09-24 00:49:05 +00:00
return (await _ajax({
call: 'deliveryCert',
httpType: 'GET',
responseType: 'json',
validateResponse: { certificate: 'string' },
...(omitE164 ? { urlParameters: '?includeE164=false' } : {}),
2021-09-24 00:49:05 +00:00
})) as GetSenderCertificateResultType;
}
async function getStorageCredentials(): Promise<StorageServiceCredentials> {
2021-09-24 00:49:05 +00:00
return (await _ajax({
call: 'storageToken',
httpType: 'GET',
responseType: 'json',
schema: { username: 'string', password: 'string' },
2021-09-24 00:49:05 +00:00
})) as StorageServiceCredentials;
}
async function getStorageManifest(
options: StorageServiceCallOptionsType = {}
2021-09-24 00:49:05 +00:00
): Promise<Uint8Array> {
const { credentials, greaterThanVersion } = options;
return _ajax({
call: 'storageManifest',
contentType: 'application/x-protobuf',
host: storageUrl,
httpType: 'GET',
2021-09-24 00:49:05 +00:00
responseType: 'bytes',
urlParameters: greaterThanVersion
? `/version/${greaterThanVersion}`
: '',
...credentials,
});
}
async function getStorageRecords(
2021-09-24 00:49:05 +00:00
data: Uint8Array,
options: StorageServiceCallOptionsType = {}
2021-09-24 00:49:05 +00:00
): Promise<Uint8Array> {
const { credentials } = options;
return _ajax({
call: 'storageRead',
contentType: 'application/x-protobuf',
data,
host: storageUrl,
httpType: 'PUT',
2021-09-24 00:49:05 +00:00
responseType: 'bytes',
...credentials,
});
}
2020-09-09 00:56:23 +00:00
async function modifyStorageRecords(
2021-09-24 00:49:05 +00:00
data: Uint8Array,
2020-09-09 00:56:23 +00:00
options: StorageServiceCallOptionsType = {}
2021-09-24 00:49:05 +00:00
): Promise<Uint8Array> {
2020-09-09 00:56:23 +00:00
const { credentials } = options;
return _ajax({
call: 'storageModify',
contentType: 'application/x-protobuf',
data,
host: storageUrl,
httpType: 'PUT',
// If we run into a conflict, the current manifest is returned -
2021-09-24 00:49:05 +00:00
// it will will be an Uint8Array at the response key on the Error
responseType: 'bytes',
2020-09-09 00:56:23 +00:00
...credentials,
});
}
async function registerSupportForUnauthenticatedDelivery() {
2021-09-24 00:49:05 +00:00
await _ajax({
call: 'supportUnauthenticatedDelivery',
httpType: 'PUT',
responseType: 'json',
});
}
2020-11-20 17:30:45 +00:00
async function registerCapabilities(capabilities: CapabilitiesUploadType) {
2021-09-24 00:49:05 +00:00
await _ajax({
call: 'registerCapabilities',
httpType: 'PUT',
jsonData: capabilities,
});
}
function getProfileUrl(
identifier: string,
profileKeyVersion?: string,
profileKeyCredentialRequest?: string
) {
let profileUrl = `/${identifier}`;
if (profileKeyVersion) {
profileUrl += `/${profileKeyVersion}`;
}
if (profileKeyVersion && profileKeyCredentialRequest) {
profileUrl += `/${profileKeyCredentialRequest}`;
}
return profileUrl;
}
async function getProfile(
identifier: string,
options: {
profileKeyVersion?: string;
profileKeyCredentialRequest?: string;
}
) {
const { profileKeyVersion, profileKeyCredentialRequest } = options;
2021-09-24 00:49:05 +00:00
return (await _ajax({
call: 'profile',
httpType: 'GET',
urlParameters: getProfileUrl(
identifier,
profileKeyVersion,
profileKeyCredentialRequest
),
responseType: 'json',
2020-08-20 22:15:50 +00:00
redactUrl: _createRedactor(
identifier,
profileKeyVersion,
profileKeyCredentialRequest
),
2021-09-24 00:49:05 +00:00
})) as ProfileType;
}
2021-07-19 19:26:06 +00:00
async function putProfile(
jsonData: ProfileRequestDataType
): Promise<UploadAvatarHeadersType | undefined> {
const res = await _ajax({
call: 'profile',
httpType: 'PUT',
2021-09-24 00:49:05 +00:00
responseType: 'json',
2021-07-19 19:26:06 +00:00
jsonData,
});
if (!res) {
return;
}
2021-09-24 00:49:05 +00:00
return uploadAvatarHeadersZod.parse(res);
2021-07-19 19:26:06 +00:00
}
async function getProfileUnauth(
identifier: string,
options: {
accessKey: string;
profileKeyVersion?: string;
profileKeyCredentialRequest?: string;
}
) {
const {
accessKey,
profileKeyVersion,
profileKeyCredentialRequest,
} = options;
2021-09-24 00:49:05 +00:00
return (await _ajax({
call: 'profile',
httpType: 'GET',
urlParameters: getProfileUrl(
identifier,
profileKeyVersion,
profileKeyCredentialRequest
),
responseType: 'json',
unauthenticated: true,
accessKey,
2020-08-20 22:15:50 +00:00
redactUrl: _createRedactor(
identifier,
profileKeyVersion,
profileKeyCredentialRequest
),
2021-09-24 00:49:05 +00:00
})) as ProfileType;
}
async function getAvatar(path: string) {
// Using _outerAJAX, since it's not hardcoded to the Signal Server. Unlike our
// attachment CDN, it uses our self-signed certificate, so we pass it in.
2021-09-24 00:49:05 +00:00
return _outerAjax(`${cdnUrlObject['0']}/${path}`, {
certificateAuthority,
contentType: 'application/octet-stream',
proxyUrl,
2021-09-24 00:49:05 +00:00
responseType: 'bytes',
timeout: 0,
type: 'GET',
2020-08-20 22:15:50 +00:00
redactUrl: (href: string) => {
const pattern = RegExp(escapeRegExp(path), 'g');
return href.replace(pattern, `[REDACTED]${path.slice(-3)}`);
},
version,
2021-09-24 00:49:05 +00:00
});
}
async function deleteUsername() {
await _ajax({
call: 'username',
httpType: 'DELETE',
});
}
async function putUsername(newUsername: string) {
await _ajax({
call: 'username',
httpType: 'PUT',
urlParameters: `/${newUsername}`,
});
}
async function reportMessage(
senderE164: string,
serverGuid: string
): Promise<void> {
await _ajax({
call: 'reportMessage',
httpType: 'POST',
urlParameters: `/${senderE164}/${serverGuid}`,
2021-09-24 00:49:05 +00:00
responseType: 'bytes',
});
}
async function requestVerificationSMS(number: string) {
2021-09-24 00:49:05 +00:00
await _ajax({
call: 'accounts',
httpType: 'GET',
urlParameters: `/sms/code/${number}`,
});
}
async function requestVerificationVoice(number: string) {
2021-09-24 00:49:05 +00:00
await _ajax({
call: 'accounts',
httpType: 'GET',
urlParameters: `/voice/code/${number}`,
});
}
async function confirmCode(
number: string,
code: string,
newPassword: string,
registrationId: number,
deviceName?: string | null,
2021-09-24 00:49:05 +00:00
options: { accessKey?: Uint8Array; uuid?: string } = {}
) {
2020-11-20 17:30:45 +00:00
const capabilities: CapabilitiesUploadType = {
2021-07-20 20:18:35 +00:00
announcementGroup: true,
2020-11-20 17:30:45 +00:00
'gv2-3': true,
'gv1-migration': true,
senderKey: true,
2021-09-09 20:53:58 +00:00
changeNumber: true,
2020-11-20 17:30:45 +00:00
};
const { accessKey, uuid } = options;
2021-09-24 00:49:05 +00:00
const jsonData = {
2020-11-20 17:30:45 +00:00
capabilities,
fetchesMessages: true,
name: deviceName || undefined,
registrationId,
supportsSms: false,
unidentifiedAccessKey: accessKey
2021-09-24 00:49:05 +00:00
? Bytes.toBase64(accessKey)
: undefined,
unrestrictedUnidentifiedAccess: false,
};
const call = deviceName ? 'devices' : 'accounts';
const urlPrefix = deviceName ? '/' : '/code/';
// Reset old websocket credentials and disconnect.
// AccountManager is our only caller and it will trigger
// `registration_done` which will update credentials.
await logout();
// Update REST credentials, though. We need them for the call below
username = number;
password = newPassword;
2021-09-24 00:49:05 +00:00
const response = (await _ajax({
call,
httpType: 'PUT',
responseType: 'json',
urlParameters: urlPrefix + code,
jsonData,
2021-09-24 00:49:05 +00:00
})) as { uuid?: string; deviceId?: number };
// Set final REST credentials to let `registerKeys` succeed.
username = `${uuid || response.uuid || number}.${response.deviceId || 1}`;
password = newPassword;
return response;
}
async function updateDeviceName(deviceName: string) {
2021-09-24 00:49:05 +00:00
await _ajax({
call: 'updateDeviceName',
httpType: 'PUT',
jsonData: {
deviceName,
},
});
}
2020-06-04 18:16:19 +00:00
async function getIceServers() {
2021-09-24 00:49:05 +00:00
return (await _ajax({
2020-06-04 18:16:19 +00:00
call: 'getIceServers',
httpType: 'GET',
2021-09-22 00:58:03 +00:00
responseType: 'json',
2021-09-24 00:49:05 +00:00
})) as GetIceServersResultType;
2020-06-04 18:16:19 +00:00
}
async function getDevices() {
2021-09-24 00:49:05 +00:00
return (await _ajax({
call: 'devices',
httpType: 'GET',
2021-09-24 00:49:05 +00:00
responseType: 'json',
})) as GetDevicesResultType;
}
type JSONSignedPreKeyType = {
keyId: number;
publicKey: string;
signature: string;
};
type JSONKeysType = {
identityKey: string;
signedPreKey: JSONSignedPreKeyType;
preKeys: Array<{
keyId: number;
publicKey: string;
}>;
};
async function registerKeys(genKeys: KeysType) {
const preKeys = genKeys.preKeys.map(key => ({
keyId: key.keyId,
2021-09-24 00:49:05 +00:00
publicKey: Bytes.toBase64(key.publicKey),
}));
const keys: JSONKeysType = {
2021-09-24 00:49:05 +00:00
identityKey: Bytes.toBase64(genKeys.identityKey),
signedPreKey: {
keyId: genKeys.signedPreKey.keyId,
2021-09-24 00:49:05 +00:00
publicKey: Bytes.toBase64(genKeys.signedPreKey.publicKey),
signature: Bytes.toBase64(genKeys.signedPreKey.signature),
},
preKeys,
};
2021-09-24 00:49:05 +00:00
await _ajax({
call: 'keys',
httpType: 'PUT',
jsonData: keys,
});
}
async function setSignedPreKey(signedPreKey: SignedPreKeyType) {
2021-09-24 00:49:05 +00:00
await _ajax({
call: 'signed',
httpType: 'PUT',
jsonData: {
keyId: signedPreKey.keyId,
2021-09-24 00:49:05 +00:00
publicKey: Bytes.toBase64(signedPreKey.publicKey),
signature: Bytes.toBase64(signedPreKey.signature),
},
});
}
type ServerKeyCountType = {
count: number;
};
async function getMyKeys(): Promise<number> {
2021-09-24 00:49:05 +00:00
const result = (await _ajax({
call: 'keys',
httpType: 'GET',
responseType: 'json',
validateResponse: { count: 'number' },
2021-09-24 00:49:05 +00:00
})) as ServerKeyCountType;
return result.count;
}
type ServerKeyResponseType = {
devices: Array<{
deviceId: number;
registrationId: number;
signedPreKey: {
keyId: number;
publicKey: string;
signature: string;
};
preKey?: {
keyId: number;
publicKey: string;
};
}>;
identityKey: string;
};
function handleKeys(res: ServerKeyResponseType): ServerKeysType {
if (!Array.isArray(res.devices)) {
throw new Error('Invalid response');
}
const devices = res.devices.map(device => {
if (
!_validateResponse(device, { signedPreKey: 'object' }) ||
!_validateResponse(device.signedPreKey, {
publicKey: 'string',
signature: 'string',
})
) {
throw new Error('Invalid signedPreKey');
}
let preKey;
if (device.preKey) {
if (
!_validateResponse(device, { preKey: 'object' }) ||
!_validateResponse(device.preKey, { publicKey: 'string' })
) {
throw new Error('Invalid preKey');
}
preKey = {
keyId: device.preKey.keyId,
2021-09-24 00:49:05 +00:00
publicKey: Bytes.fromBase64(device.preKey.publicKey),
};
}
return {
deviceId: device.deviceId,
registrationId: device.registrationId,
preKey,
signedPreKey: {
keyId: device.signedPreKey.keyId,
2021-09-24 00:49:05 +00:00
publicKey: Bytes.fromBase64(device.signedPreKey.publicKey),
signature: Bytes.fromBase64(device.signedPreKey.signature),
},
};
});
return {
devices,
2021-09-24 00:49:05 +00:00
identityKey: Bytes.fromBase64(res.identityKey),
};
}
async function getKeysForIdentifier(identifier: string, deviceId?: number) {
2021-09-24 00:49:05 +00:00
const keys = (await _ajax({
call: 'keys',
httpType: 'GET',
urlParameters: `/${identifier}/${deviceId || '*'}`,
responseType: 'json',
validateResponse: { identityKey: 'string', devices: 'object' },
2021-09-24 00:49:05 +00:00
})) as ServerKeyResponseType;
return handleKeys(keys);
}
async function getKeysForIdentifierUnauth(
identifier: string,
deviceId?: number,
{ accessKey }: { accessKey?: string } = {}
) {
2021-09-24 00:49:05 +00:00
const keys = (await _ajax({
call: 'keys',
httpType: 'GET',
urlParameters: `/${identifier}/${deviceId || '*'}`,
responseType: 'json',
validateResponse: { identityKey: 'string', devices: 'object' },
unauthenticated: true,
accessKey,
2021-09-24 00:49:05 +00:00
})) as ServerKeyResponseType;
return handleKeys(keys);
}
async function sendMessagesUnauth(
destination: string,
2021-09-28 23:38:55 +00:00
messages: ReadonlyArray<MessageType>,
timestamp: number,
online?: boolean,
{ accessKey }: { accessKey?: string } = {}
) {
2021-09-24 00:49:05 +00:00
let jsonData;
2018-11-14 19:10:32 +00:00
if (online) {
2021-09-24 00:49:05 +00:00
jsonData = { messages, timestamp, online: true };
} else {
jsonData = { messages, timestamp };
2018-11-14 19:10:32 +00:00
}
2021-09-24 00:49:05 +00:00
await _ajax({
call: 'messages',
httpType: 'PUT',
urlParameters: `/${destination}`,
jsonData,
responseType: 'json',
unauthenticated: true,
accessKey,
});
}
async function sendMessages(
destination: string,
2021-09-28 23:38:55 +00:00
messages: ReadonlyArray<MessageType>,
timestamp: number,
online?: boolean
2018-11-14 19:10:32 +00:00
) {
2021-09-24 00:49:05 +00:00
let jsonData;
2018-11-14 19:10:32 +00:00
if (online) {
2021-09-24 00:49:05 +00:00
jsonData = { messages, timestamp, online: true };
} else {
jsonData = { messages, timestamp };
2018-11-14 19:10:32 +00:00
}
2021-09-24 00:49:05 +00:00
await _ajax({
call: 'messages',
httpType: 'PUT',
urlParameters: `/${destination}`,
jsonData,
responseType: 'json',
});
}
2021-05-25 22:40:04 +00:00
async function sendWithSenderKey(
2021-09-24 00:49:05 +00:00
data: Uint8Array,
accessKeys: Uint8Array,
2021-05-25 22:40:04 +00:00
timestamp: number,
online?: boolean
): Promise<MultiRecipient200ResponseType> {
2021-09-24 00:49:05 +00:00
const response = await _ajax({
2021-05-25 22:40:04 +00:00
call: 'multiRecipient',
httpType: 'PUT',
contentType: 'application/vnd.signal-messenger.mrm',
data,
urlParameters: `?ts=${timestamp}&online=${online ? 'true' : 'false'}`,
responseType: 'json',
unauthenticated: true,
2021-09-24 00:49:05 +00:00
accessKey: Bytes.toBase64(accessKeys),
2021-05-25 22:40:04 +00:00
});
2021-09-24 00:49:05 +00:00
const parseResult = multiRecipient200ResponseSchema.safeParse(response);
if (parseResult.success) {
return parseResult.data;
}
log.warn(
'WebAPI: invalid response from sendWithSenderKey',
toLogFormat(parseResult.error)
);
return response as MultiRecipient200ResponseType;
2021-05-25 22:40:04 +00:00
}
function redactStickerUrl(stickerUrl: string) {
return stickerUrl.replace(
/(\/stickers\/)([^/]+)(\/)/,
(_, begin: string, packId: string, end: string) =>
`${begin}${redactPackId(packId)}${end}`
);
}
async function getSticker(packId: string, stickerId: number) {
2020-08-31 16:29:22 +00:00
if (!isPackIdValid(packId)) {
throw new Error('getSticker: pack ID was invalid');
}
2021-09-24 00:49:05 +00:00
return _outerAjax(
`${cdnUrlObject['0']}/stickers/${packId}/full/${stickerId}`,
{
certificateAuthority,
proxyUrl,
2021-09-24 00:49:05 +00:00
responseType: 'bytes',
type: 'GET',
redactUrl: redactStickerUrl,
version,
}
2021-09-24 00:49:05 +00:00
);
}
async function getStickerPackManifest(packId: string) {
2020-08-31 16:29:22 +00:00
if (!isPackIdValid(packId)) {
throw new Error('getStickerPackManifest: pack ID was invalid');
}
2021-09-24 00:49:05 +00:00
return _outerAjax(
`${cdnUrlObject['0']}/stickers/${packId}/manifest.proto`,
{
certificateAuthority,
proxyUrl,
2021-09-24 00:49:05 +00:00
responseType: 'bytes',
type: 'GET',
redactUrl: redactStickerUrl,
version,
}
2021-09-24 00:49:05 +00:00
);
}
type ServerAttachmentType = {
key: string;
credential: string;
acl: string;
algorithm: string;
date: string;
policy: string;
signature: string;
};
2019-12-17 20:25:57 +00:00
function makePutParams(
{
key,
credential,
acl,
algorithm,
date,
policy,
signature,
}: ServerAttachmentType,
2021-09-24 00:49:05 +00:00
encryptedBin: Uint8Array
2019-12-17 20:25:57 +00:00
) {
// Note: when using the boundary string in the POST body, it needs to be prefixed by
// an extra --, and the final boundary string at the end gets a -- prefix and a --
// suffix.
const boundaryString = `----------------${getGuid().replace(/-/g, '')}`;
const CRLF = '\r\n';
const getSection = (name: string, value: string) =>
[
`--${boundaryString}`,
`Content-Disposition: form-data; name="${name}"${CRLF}`,
value,
].join(CRLF);
const start = [
getSection('key', key),
getSection('x-amz-credential', credential),
getSection('acl', acl),
getSection('x-amz-algorithm', algorithm),
getSection('x-amz-date', date),
getSection('policy', policy),
getSection('x-amz-signature', signature),
getSection('Content-Type', 'application/octet-stream'),
`--${boundaryString}`,
'Content-Disposition: form-data; name="file"',
`Content-Type: application/octet-stream${CRLF}${CRLF}`,
].join(CRLF);
const end = `${CRLF}--${boundaryString}--${CRLF}`;
const startBuffer = Buffer.from(start, 'utf8');
const attachmentBuffer = Buffer.from(encryptedBin);
const endBuffer = Buffer.from(end, 'utf8');
const contentLength =
startBuffer.length + attachmentBuffer.length + endBuffer.length;
const data = Buffer.concat(
[startBuffer, attachmentBuffer, endBuffer],
contentLength
);
2019-12-17 20:25:57 +00:00
return {
data,
contentType: `multipart/form-data; boundary=${boundaryString}`,
headers: {
'Content-Length': contentLength.toString(),
2019-12-17 20:25:57 +00:00
},
};
}
async function putStickers(
2021-09-24 00:49:05 +00:00
encryptedManifest: Uint8Array,
encryptedStickers: Array<Uint8Array>,
onProgress?: () => void
2019-12-17 20:25:57 +00:00
) {
// Get manifest and sticker upload parameters
2021-09-24 00:49:05 +00:00
const { packId, manifest, stickers } = (await _ajax({
2019-12-17 20:25:57 +00:00
call: 'getStickerPackUpload',
responseType: 'json',
httpType: 'GET',
2019-12-17 20:25:57 +00:00
urlParameters: `/${encryptedStickers.length}`,
2021-09-24 00:49:05 +00:00
})) as {
packId: string;
manifest: ServerAttachmentType;
stickers: ReadonlyArray<ServerAttachmentType>;
};
2019-12-17 20:25:57 +00:00
// Upload manifest
const manifestParams = makePutParams(manifest, encryptedManifest);
// This is going to the CDN, not the service, so we use _outerAjax
await _outerAjax(`${cdnUrlObject['0']}/`, {
2019-12-17 20:25:57 +00:00
...manifestParams,
certificateAuthority,
proxyUrl,
timeout: 0,
type: 'POST',
version,
2019-12-17 20:25:57 +00:00
});
// Upload stickers
const queue = new PQueue({ concurrency: 3, timeout: 1000 * 60 * 2 });
2019-12-17 20:25:57 +00:00
await Promise.all(
stickers.map(async (sticker: ServerAttachmentType, index: number) => {
const stickerParams = makePutParams(
sticker,
encryptedStickers[index]
);
await queue.add(async () =>
_outerAjax(`${cdnUrlObject['0']}/`, {
...stickerParams,
certificateAuthority,
proxyUrl,
timeout: 0,
type: 'POST',
version,
})
);
2019-12-17 20:25:57 +00:00
if (onProgress) {
onProgress();
}
})
);
// Done!
return packId;
}
async function getAttachment(cdnKey: string, cdnNumber?: number) {
const cdnUrl = isNumber(cdnNumber)
? cdnUrlObject[cdnNumber] || cdnUrlObject['0']
: cdnUrlObject['0'];
2019-12-17 20:25:57 +00:00
// This is going to the CDN, not the service, so we use _outerAjax
2021-09-24 00:49:05 +00:00
return _outerAjax(`${cdnUrl}/attachments/${cdnKey}`, {
2019-12-17 20:25:57 +00:00
certificateAuthority,
proxyUrl,
2021-09-24 00:49:05 +00:00
responseType: 'bytes',
2019-12-17 20:25:57 +00:00
timeout: 0,
type: 'GET',
2020-08-20 22:15:50 +00:00
redactUrl: _createRedactor(cdnKey),
version,
2021-09-24 00:49:05 +00:00
});
2019-12-17 20:25:57 +00:00
}
2021-09-24 00:49:05 +00:00
type PutAttachmentResponseType = ServerAttachmentType & {
attachmentIdString: string;
};
async function putAttachment(encryptedBin: Uint8Array) {
const response = (await _ajax({
2019-12-17 20:25:57 +00:00
call: 'attachmentId',
httpType: 'GET',
responseType: 'json',
2021-09-24 00:49:05 +00:00
})) as PutAttachmentResponseType;
2019-12-17 20:25:57 +00:00
const { attachmentIdString } = response;
const params = makePutParams(response, encryptedBin);
// This is going to the CDN, not the service, so we use _outerAjax
await _outerAjax(`${cdnUrlObject['0']}/attachments/`, {
2019-12-17 20:25:57 +00:00
...params,
certificateAuthority,
proxyUrl,
timeout: 0,
type: 'POST',
version,
});
return attachmentIdString;
}
function getHeaderPadding() {
const max = getRandomValue(1, 64);
let characters = '';
2019-01-16 03:03:56 +00:00
for (let i = 0; i < max; i += 1) {
characters += String.fromCharCode(getRandomValue(65, 122));
2019-01-16 03:03:56 +00:00
}
return characters;
2019-01-16 03:03:56 +00:00
}
2020-09-28 23:46:31 +00:00
async function fetchLinkPreviewMetadata(
href: string,
abortSignal: AbortSignal
) {
return linkPreviewFetch.fetchLinkPreviewMetadata(
fetchForLinkPreviews,
2020-09-28 23:46:31 +00:00
href,
abortSignal
);
}
async function fetchLinkPreviewImage(
href: string,
abortSignal: AbortSignal
) {
return linkPreviewFetch.fetchLinkPreviewImage(
fetchForLinkPreviews,
href,
abortSignal
);
2020-09-28 23:46:31 +00:00
}
async function makeProxiedRequest(
targetUrl: string,
options: ProxiedRequestOptionsType = {}
2021-09-24 00:49:05 +00:00
): Promise<MakeProxiedRequestResultType> {
const { returnUint8Array, start, end } = options;
const headers: HeaderListType = {
'X-SignalPadding': getHeaderPadding(),
};
2019-01-16 03:03:56 +00:00
if (is.number(start) && is.number(end)) {
headers.Range = `bytes=${start}-${end}`;
2019-01-16 03:03:56 +00:00
}
2021-09-24 00:49:05 +00:00
const result = await _outerAjax(targetUrl, {
responseType: returnUint8Array ? 'byteswithdetails' : undefined,
2019-01-16 03:03:56 +00:00
proxyUrl: contentProxyUrl,
type: 'GET',
redirect: 'follow',
redactUrl: () => '[REDACTED_URL]',
2019-01-16 03:03:56 +00:00
headers,
version,
2021-09-24 00:49:05 +00:00
});
2021-09-24 00:49:05 +00:00
if (!returnUint8Array) {
return result as Uint8Array;
}
2021-09-24 00:49:05 +00:00
const { response } = result as BytesWithDetailsType;
if (!response.headers || !response.headers.get) {
throw new Error('makeProxiedRequest: Problem retrieving header value');
}
const range = response.headers.get('content-range');
2020-09-09 02:25:05 +00:00
const match = PARSE_RANGE_HEADER.exec(range || '');
if (!match || !match[1]) {
throw new Error(
`makeProxiedRequest: Unable to parse total size from ${range}`
);
}
const totalSize = parseInt(match[1], 10);
return {
totalSize,
2021-09-24 00:49:05 +00:00
result: result as BytesWithDetailsType,
};
2019-01-16 03:03:56 +00:00
}
2020-11-13 19:57:55 +00:00
async function makeSfuRequest(
targetUrl: string,
type: HTTPCodeType,
headers: HeaderListType,
2021-09-24 00:49:05 +00:00
body: Uint8Array | undefined
): Promise<BytesWithDetailsType> {
2020-11-13 19:57:55 +00:00
return _outerAjax(targetUrl, {
certificateAuthority,
data: body,
headers,
proxyUrl,
2021-09-24 00:49:05 +00:00
responseType: 'byteswithdetails',
2020-11-13 19:57:55 +00:00
timeout: 0,
type,
version,
2021-09-24 00:49:05 +00:00
});
2020-11-13 19:57:55 +00:00
}
2020-09-09 02:25:05 +00:00
// Groups
function generateGroupAuth(
groupPublicParamsHex: string,
authCredentialPresentationHex: string
) {
2021-09-24 00:49:05 +00:00
return Bytes.toBase64(
Bytes.fromString(
`${groupPublicParamsHex}:${authCredentialPresentationHex}`
)
);
2020-09-09 02:25:05 +00:00
}
type CredentialResponseType = {
credentials: Array<GroupCredentialType>;
};
async function getGroupCredentials(
startDay: number,
endDay: number
): Promise<Array<GroupCredentialType>> {
2021-09-24 00:49:05 +00:00
const response = (await _ajax({
2020-09-09 02:25:05 +00:00
call: 'getGroupCredentials',
urlParameters: `/${startDay}/${endDay}`,
httpType: 'GET',
responseType: 'json',
2021-09-24 00:49:05 +00:00
})) as CredentialResponseType;
2020-09-09 02:25:05 +00:00
return response.credentials;
}
2020-11-13 19:57:55 +00:00
async function getGroupExternalCredential(
options: GroupCredentialsType
2021-06-22 14:46:42 +00:00
): Promise<Proto.GroupExternalCredential> {
2020-11-13 19:57:55 +00:00
const basicAuth = generateGroupAuth(
options.groupPublicParamsHex,
options.authCredentialPresentationHex
);
2021-09-24 00:49:05 +00:00
const response = await _ajax({
2020-11-13 19:57:55 +00:00
basicAuth,
call: 'groupToken',
httpType: 'GET',
contentType: 'application/x-protobuf',
2021-09-24 00:49:05 +00:00
responseType: 'bytes',
2020-11-13 19:57:55 +00:00
host: storageUrl,
});
2021-09-24 00:49:05 +00:00
return Proto.GroupExternalCredential.decode(response);
2020-11-13 19:57:55 +00:00
}
2021-07-09 19:36:10 +00:00
function verifyAttributes(attributes: Proto.IAvatarUploadAttributes) {
2020-09-09 02:25:05 +00:00
const {
key,
credential,
acl,
algorithm,
date,
policy,
signature,
} = attributes;
if (
!key ||
!credential ||
!acl ||
!algorithm ||
!date ||
!policy ||
!signature
) {
throw new Error(
'verifyAttributes: Missing value from AvatarUploadAttributes'
);
}
return {
key,
credential,
acl,
algorithm,
date,
policy,
signature,
};
}
2021-07-19 19:26:06 +00:00
async function uploadAvatar(
uploadAvatarRequestHeaders: UploadAvatarHeadersType,
2021-09-24 00:49:05 +00:00
avatarData: Uint8Array
2021-07-19 19:26:06 +00:00
): Promise<string> {
const verified = verifyAttributes(uploadAvatarRequestHeaders);
const { key } = verified;
const manifestParams = makePutParams(verified, avatarData);
await _outerAjax(`${cdnUrlObject['0']}/`, {
...manifestParams,
certificateAuthority,
proxyUrl,
timeout: 0,
type: 'POST',
version,
});
return key;
}
2020-09-09 02:25:05 +00:00
async function uploadGroupAvatar(
2021-06-22 14:46:42 +00:00
avatarData: Uint8Array,
2020-09-09 02:25:05 +00:00
options: GroupCredentialsType
): Promise<string> {
const basicAuth = generateGroupAuth(
options.groupPublicParamsHex,
options.authCredentialPresentationHex
);
2021-09-24 00:49:05 +00:00
const response = await _ajax({
2020-09-09 02:25:05 +00:00
basicAuth,
call: 'getGroupAvatarUpload',
httpType: 'GET',
2021-09-24 00:49:05 +00:00
responseType: 'bytes',
2020-09-09 02:25:05 +00:00
host: storageUrl,
});
2021-09-24 00:49:05 +00:00
const attributes = Proto.AvatarUploadAttributes.decode(response);
2020-09-09 02:25:05 +00:00
const verified = verifyAttributes(attributes);
const { key } = verified;
2021-09-24 00:49:05 +00:00
const manifestParams = makePutParams(verified, avatarData);
2020-09-09 02:25:05 +00:00
await _outerAjax(`${cdnUrlObject['0']}/`, {
...manifestParams,
certificateAuthority,
proxyUrl,
timeout: 0,
type: 'POST',
version,
});
return key;
}
2021-09-24 00:49:05 +00:00
async function getGroupAvatar(key: string): Promise<Uint8Array> {
2020-09-09 02:25:05 +00:00
return _outerAjax(`${cdnUrlObject['0']}/${key}`, {
certificateAuthority,
proxyUrl,
2021-09-24 00:49:05 +00:00
responseType: 'bytes',
2020-09-09 02:25:05 +00:00
timeout: 0,
type: 'GET',
version,
redactUrl: _createRedactor(key),
2021-09-24 00:49:05 +00:00
});
2020-09-09 02:25:05 +00:00
}
async function createGroup(
2021-06-22 14:46:42 +00:00
group: Proto.IGroup,
2020-09-09 02:25:05 +00:00
options: GroupCredentialsType
): Promise<void> {
const basicAuth = generateGroupAuth(
options.groupPublicParamsHex,
options.authCredentialPresentationHex
);
2021-06-22 14:46:42 +00:00
const data = Proto.Group.encode(group).finish();
2020-09-09 02:25:05 +00:00
await _ajax({
basicAuth,
call: 'groups',
2020-11-20 17:30:45 +00:00
contentType: 'application/x-protobuf',
2020-09-09 02:25:05 +00:00
data,
host: storageUrl,
2020-11-20 17:30:45 +00:00
httpType: 'PUT',
2020-09-09 02:25:05 +00:00
});
}
async function getGroup(
options: GroupCredentialsType
2021-06-22 14:46:42 +00:00
): Promise<Proto.Group> {
2020-09-09 02:25:05 +00:00
const basicAuth = generateGroupAuth(
options.groupPublicParamsHex,
options.authCredentialPresentationHex
);
2021-09-24 00:49:05 +00:00
const response = await _ajax({
2020-09-09 02:25:05 +00:00
basicAuth,
call: 'groups',
contentType: 'application/x-protobuf',
host: storageUrl,
2020-11-20 17:30:45 +00:00
httpType: 'GET',
2021-09-24 00:49:05 +00:00
responseType: 'bytes',
2020-09-09 02:25:05 +00:00
});
2021-09-24 00:49:05 +00:00
return Proto.Group.decode(response);
2020-09-09 02:25:05 +00:00
}
async function getGroupFromLink(
inviteLinkPassword: string,
auth: GroupCredentialsType
2021-06-22 14:46:42 +00:00
): Promise<Proto.GroupJoinInfo> {
const basicAuth = generateGroupAuth(
auth.groupPublicParamsHex,
auth.authCredentialPresentationHex
);
const safeInviteLinkPassword = toWebSafeBase64(inviteLinkPassword);
2021-09-24 00:49:05 +00:00
const response = await _ajax({
basicAuth,
call: 'groupsViaLink',
contentType: 'application/x-protobuf',
host: storageUrl,
httpType: 'GET',
2021-09-24 00:49:05 +00:00
responseType: 'bytes',
urlParameters: `/${safeInviteLinkPassword}`,
redactUrl: _createRedactor(safeInviteLinkPassword),
});
2021-09-24 00:49:05 +00:00
return Proto.GroupJoinInfo.decode(response);
}
2020-09-09 02:25:05 +00:00
async function modifyGroup(
2021-06-22 14:46:42 +00:00
changes: Proto.GroupChange.IActions,
options: GroupCredentialsType,
inviteLinkBase64?: string
2021-06-22 14:46:42 +00:00
): Promise<Proto.IGroupChange> {
2020-09-09 02:25:05 +00:00
const basicAuth = generateGroupAuth(
options.groupPublicParamsHex,
options.authCredentialPresentationHex
);
2021-06-22 14:46:42 +00:00
const data = Proto.GroupChange.Actions.encode(changes).finish();
const safeInviteLinkPassword = inviteLinkBase64
? toWebSafeBase64(inviteLinkBase64)
: undefined;
2020-09-09 02:25:05 +00:00
2021-09-24 00:49:05 +00:00
const response = await _ajax({
2020-09-09 02:25:05 +00:00
basicAuth,
call: 'groups',
contentType: 'application/x-protobuf',
2020-11-20 17:30:45 +00:00
data,
2020-09-09 02:25:05 +00:00
host: storageUrl,
2020-11-20 17:30:45 +00:00
httpType: 'PATCH',
2021-09-24 00:49:05 +00:00
responseType: 'bytes',
urlParameters: safeInviteLinkPassword
? `?inviteLinkPassword=${safeInviteLinkPassword}`
: undefined,
redactUrl: safeInviteLinkPassword
? _createRedactor(safeInviteLinkPassword)
: undefined,
2020-09-09 02:25:05 +00:00
});
2021-09-24 00:49:05 +00:00
return Proto.GroupChange.decode(response);
2020-09-09 02:25:05 +00:00
}
async function getGroupLog(
startVersion: number,
options: GroupCredentialsType
): Promise<GroupLogResponseType> {
const basicAuth = generateGroupAuth(
options.groupPublicParamsHex,
options.authCredentialPresentationHex
);
2021-09-24 00:49:05 +00:00
const withDetails = await _ajax({
2020-09-09 02:25:05 +00:00
basicAuth,
call: 'groupLog',
contentType: 'application/x-protobuf',
host: storageUrl,
2020-11-20 17:30:45 +00:00
httpType: 'GET',
2021-09-24 00:49:05 +00:00
responseType: 'byteswithdetails',
2020-11-20 17:30:45 +00:00
urlParameters: `/${startVersion}`,
2020-09-09 02:25:05 +00:00
});
const { data, response } = withDetails;
2021-09-24 00:49:05 +00:00
const changes = Proto.GroupChanges.decode(data);
2020-09-09 02:25:05 +00:00
if (response && response.status === 206) {
const range = response.headers.get('Content-Range');
const match = PARSE_GROUP_LOG_RANGE_HEADER.exec(range || '');
const start = match ? parseInt(match[0], 10) : undefined;
const end = match ? parseInt(match[1], 10) : undefined;
const currentRevision = match ? parseInt(match[2], 10) : undefined;
if (
match &&
is.number(start) &&
is.number(end) &&
is.number(currentRevision)
) {
return {
changes,
start,
end,
currentRevision,
};
}
}
return {
changes,
};
}
function getProvisioningResource(
handler: IRequestHandler
): Promise<WebSocketResource> {
return socketManager.getProvisioningResource(handler);
}
2020-09-04 01:25:19 +00:00
async function getDirectoryAuth(): Promise<{
username: string;
password: string;
}> {
2021-09-24 00:49:05 +00:00
return (await _ajax({
2020-09-04 01:25:19 +00:00
call: 'directoryAuth',
httpType: 'GET',
responseType: 'json',
2021-09-24 00:49:05 +00:00
})) as { username: string; password: string };
2020-09-04 01:25:19 +00:00
}
function validateAttestationQuote({
serverStaticPublic,
2021-09-24 00:49:05 +00:00
quote: quoteBytes,
2020-09-04 01:25:19 +00:00
}: {
2021-09-24 00:49:05 +00:00
serverStaticPublic: Uint8Array;
quote: Uint8Array;
2020-09-04 01:25:19 +00:00
}) {
const SGX_CONSTANTS = getSgxConstants();
2021-09-24 00:49:05 +00:00
const quote = Buffer.from(quoteBytes);
2021-07-13 18:54:53 +00:00
2021-07-15 23:17:22 +00:00
const quoteVersion = quote.readInt16LE(0) & 0xffff;
2020-09-04 01:25:19 +00:00
if (quoteVersion < 0 || quoteVersion > 2) {
throw new Error(`Unknown version ${quoteVersion}`);
}
2021-07-15 23:17:22 +00:00
const miscSelect = quote.slice(64, 64 + 4);
2020-09-04 01:25:19 +00:00
if (!miscSelect.every(byte => byte === 0)) {
throw new Error('Quote miscSelect invalid!');
}
2021-07-15 23:17:22 +00:00
const reserved1 = quote.slice(68, 68 + 28);
2020-09-04 01:25:19 +00:00
if (!reserved1.every(byte => byte === 0)) {
throw new Error('Quote reserved1 invalid!');
}
2021-07-13 18:54:53 +00:00
const flags = Long.fromBytesLE(
2021-07-15 23:17:22 +00:00
Array.from(quote.slice(96, 96 + 8).values())
2021-07-13 18:54:53 +00:00
);
2020-09-04 01:25:19 +00:00
if (
flags.and(SGX_CONSTANTS.SGX_FLAGS_RESERVED).notEquals(0) ||
flags.and(SGX_CONSTANTS.SGX_FLAGS_INITTED).equals(0) ||
flags.and(SGX_CONSTANTS.SGX_FLAGS_MODE64BIT).equals(0)
) {
throw new Error(`Quote flags invalid ${flags.toString()}`);
}
2021-07-13 18:54:53 +00:00
const xfrm = Long.fromBytesLE(
2021-07-15 23:17:22 +00:00
Array.from(quote.slice(104, 104 + 8).values())
2021-07-13 18:54:53 +00:00
);
2020-09-04 01:25:19 +00:00
if (xfrm.and(SGX_CONSTANTS.SGX_XFRM_RESERVED).notEquals(0)) {
throw new Error(`Quote xfrm invalid ${xfrm}`);
}
2021-07-15 23:17:22 +00:00
const mrenclave = quote.slice(112, 112 + 32);
2021-07-13 18:54:53 +00:00
const enclaveIdBytes = Bytes.fromHex(directoryEnclaveId);
if (mrenclave.compare(enclaveIdBytes) !== 0) {
2020-09-04 01:25:19 +00:00
throw new Error('Quote mrenclave invalid!');
}
2021-07-15 23:17:22 +00:00
const reserved2 = quote.slice(144, 144 + 32);
2020-09-04 01:25:19 +00:00
if (!reserved2.every(byte => byte === 0)) {
throw new Error('Quote reserved2 invalid!');
}
2021-07-15 23:17:22 +00:00
const reportData = quote.slice(368, 368 + 64);
2021-09-24 00:49:05 +00:00
const serverStaticPublicBytes = serverStaticPublic;
2020-09-04 01:25:19 +00:00
if (
!reportData.every((byte, index) => {
if (index >= 32) {
return byte === 0;
}
return byte === serverStaticPublicBytes[index];
})
) {
throw new Error('Quote report_data invalid!');
}
2021-07-15 23:17:22 +00:00
const reserved3 = quote.slice(208, 208 + 96);
2020-09-04 01:25:19 +00:00
if (!reserved3.every(byte => byte === 0)) {
throw new Error('Quote reserved3 invalid!');
}
2021-07-15 23:17:22 +00:00
const reserved4 = quote.slice(308, 308 + 60);
2020-09-04 01:25:19 +00:00
if (!reserved4.every(byte => byte === 0)) {
throw new Error('Quote reserved4 invalid!');
}
2021-07-13 18:54:53 +00:00
const signatureLength = quote.readInt32LE(432) >>> 0;
2020-09-04 01:25:19 +00:00
if (signatureLength !== quote.byteLength - 436) {
throw new Error(`Bad signatureLength ${signatureLength}`);
}
2021-07-15 23:17:22 +00:00
// const signature = quote.slice(436, 436 + signatureLength);
2020-09-04 01:25:19 +00:00
}
function validateAttestationSignatureBody(
signatureBody: {
timestamp: string;
version: number;
isvEnclaveQuoteBody: string;
isvEnclaveQuoteStatus: string;
},
encodedQuote: string
) {
// Parse timestamp as UTC
const { timestamp } = signatureBody;
const utcTimestamp = timestamp.endsWith('Z')
? timestamp
: `${timestamp}Z`;
const signatureTime = new Date(utcTimestamp).getTime();
const now = Date.now();
if (signatureBody.version !== 3) {
throw new Error('Attestation signature invalid version!');
}
if (!encodedQuote.startsWith(signatureBody.isvEnclaveQuoteBody)) {
throw new Error('Attestion signature mismatches quote!');
}
if (signatureBody.isvEnclaveQuoteStatus !== 'OK') {
throw new Error('Attestation signature status not "OK"!');
}
if (signatureTime < now - 24 * 60 * 60 * 1000) {
throw new Error('Attestation signature timestamp older than 24 hours!');
}
}
async function validateAttestationSignature(
2021-09-24 00:49:05 +00:00
signature: Uint8Array,
2020-09-04 01:25:19 +00:00
signatureBody: string,
certificates: string
) {
const CERT_PREFIX = '-----BEGIN CERTIFICATE-----';
const pem = compact(
certificates.split(CERT_PREFIX).map(match => {
if (!match) {
return null;
}
return `${CERT_PREFIX}${match}`;
})
);
if (pem.length < 2) {
throw new Error(
`validateAttestationSignature: Expect two or more entries; got ${pem.length}`
);
}
const verify = createVerify('RSA-SHA256');
2021-09-24 00:49:05 +00:00
verify.update(Buffer.from(Bytes.fromString(signatureBody)));
2020-09-04 01:25:19 +00:00
const isValid = verify.verify(pem[0], Buffer.from(signature));
if (!isValid) {
throw new Error('Validation of signature across signatureBody failed!');
}
const caStore = pki.createCaStore([directoryTrustAnchor]);
const chain = compact(pem.map(cert => pki.certificateFromPem(cert)));
const isChainValid = pki.verifyCertificateChain(caStore, chain);
if (!isChainValid) {
throw new Error('Validation of certificate chain failed!');
}
const leafCert = chain[0];
const fieldCN = leafCert.subject.getField('CN');
if (
!fieldCN ||
fieldCN.value !== 'Intel SGX Attestation Report Signing'
) {
throw new Error('Leaf cert CN field had unexpected value');
}
const fieldO = leafCert.subject.getField('O');
if (!fieldO || fieldO.value !== 'Intel Corporation') {
throw new Error('Leaf cert O field had unexpected value');
}
const fieldL = leafCert.subject.getField('L');
if (!fieldL || fieldL.value !== 'Santa Clara') {
throw new Error('Leaf cert L field had unexpected value');
}
const fieldST = leafCert.subject.getField('ST');
if (!fieldST || fieldST.value !== 'CA') {
throw new Error('Leaf cert ST field had unexpected value');
}
const fieldC = leafCert.subject.getField('C');
if (!fieldC || fieldC.value !== 'US') {
throw new Error('Leaf cert C field had unexpected value');
}
}
async function putRemoteAttestation(auth: {
username: string;
password: string;
}) {
const keyPair = generateKeyPair();
2020-09-04 01:25:19 +00:00
const { privKey, pubKey } = keyPair;
// Remove first "key type" byte from public key
const slicedPubKey = pubKey.slice(1);
2021-09-24 00:49:05 +00:00
const pubKeyBase64 = Bytes.toBase64(slicedPubKey);
2020-09-04 01:25:19 +00:00
// Do request
const data = JSON.stringify({ clientPublic: pubKeyBase64 });
const result: JSONWithDetailsType = (await _outerAjax(null, {
2020-09-04 01:25:19 +00:00
certificateAuthority,
type: 'PUT',
contentType: 'application/json; charset=utf-8',
host: directoryUrl,
path: `${URL_CALLS.attestation}/${directoryEnclaveId}`,
user: auth.username,
password: auth.password,
responseType: 'jsonwithdetails',
data,
2021-01-11 21:59:46 +00:00
timeout: 30000,
2020-09-04 01:25:19 +00:00
version,
})) as JSONWithDetailsType;
2020-09-04 01:25:19 +00:00
2021-09-24 00:49:05 +00:00
const { data: responseBody, response } = result as {
data: {
attestations: Record<
string,
{
ciphertext: string;
iv: string;
quote: string;
serverEphemeralPublic: string;
serverStaticPublic: string;
signature: string;
signatureBody: string;
tag: string;
certificates: string;
}
>;
};
response: Response;
};
2020-09-04 01:25:19 +00:00
const attestationsLength = Object.keys(responseBody.attestations).length;
if (attestationsLength > 3) {
throw new Error(
'Got more than three attestations from the Contact Discovery Service'
);
}
if (attestationsLength < 1) {
throw new Error(
'Got no attestations from the Contact Discovery Service'
);
}
const cookie = response.headers.get('set-cookie');
// Decode response
return {
cookie,
attestations: await pProps(
responseBody.attestations,
async attestation => {
2021-09-24 00:49:05 +00:00
const decoded = {
...attestation,
ciphertext: Bytes.fromBase64(attestation.ciphertext),
iv: Bytes.fromBase64(attestation.iv),
quote: Bytes.fromBase64(attestation.quote),
serverEphemeralPublic: Bytes.fromBase64(
attestation.serverEphemeralPublic
),
serverStaticPublic: Bytes.fromBase64(
attestation.serverStaticPublic
),
signature: Bytes.fromBase64(attestation.signature),
tag: Bytes.fromBase64(attestation.tag),
};
2020-09-04 01:25:19 +00:00
// Validate response
validateAttestationQuote(decoded);
validateAttestationSignatureBody(
JSON.parse(decoded.signatureBody),
attestation.quote
);
await validateAttestationSignature(
decoded.signature,
decoded.signatureBody,
decoded.certificates
);
// Derive key
const ephemeralToEphemeral = calculateAgreement(
2020-09-04 01:25:19 +00:00
decoded.serverEphemeralPublic,
privKey
);
const ephemeralToStatic = calculateAgreement(
2020-09-04 01:25:19 +00:00
decoded.serverStaticPublic,
privKey
);
2021-09-24 00:49:05 +00:00
const masterSecret = Bytes.concatenate([
2020-09-04 01:25:19 +00:00
ephemeralToEphemeral,
2021-09-24 00:49:05 +00:00
ephemeralToStatic,
]);
const publicKeys = Bytes.concatenate([
2020-09-04 01:25:19 +00:00
slicedPubKey,
decoded.serverEphemeralPublic,
2021-09-24 00:49:05 +00:00
decoded.serverStaticPublic,
]);
const [clientKey, serverKey] = deriveSecrets(
2020-09-04 01:25:19 +00:00
masterSecret,
publicKeys,
2021-09-24 00:49:05 +00:00
new Uint8Array(0)
2020-09-04 01:25:19 +00:00
);
// Decrypt ciphertext into requestId
2021-09-24 00:49:05 +00:00
const requestId = decryptAesGcm(
2020-09-04 01:25:19 +00:00
serverKey,
decoded.iv,
2021-09-24 00:49:05 +00:00
Bytes.concatenate([decoded.ciphertext, decoded.tag])
2020-09-04 01:25:19 +00:00
);
2021-09-24 00:49:05 +00:00
return {
clientKey,
serverKey,
requestId,
};
2020-09-04 01:25:19 +00:00
}
),
};
}
async function getUuidsForE164s(
e164s: ReadonlyArray<string>
): Promise<Dictionary<string | null>> {
const directoryAuth = await getDirectoryAuth();
const attestationResult = await putRemoteAttestation(directoryAuth);
// Encrypt data for discovery
const data = await encryptCdsDiscoveryRequest(
attestationResult.attestations,
e164s
);
const { cookie } = attestationResult;
// Send discovery request
2021-09-24 00:49:05 +00:00
const discoveryResponse = (await _outerAjax(null, {
2020-09-04 01:25:19 +00:00
certificateAuthority,
type: 'PUT',
headers: cookie
? {
cookie,
}
: undefined,
contentType: 'application/json; charset=utf-8',
host: directoryUrl,
path: `${URL_CALLS.discovery}/${directoryEnclaveId}`,
user: directoryAuth.username,
password: directoryAuth.password,
responseType: 'json',
2021-01-11 21:59:46 +00:00
timeout: 30000,
2020-09-04 01:25:19 +00:00
data: JSON.stringify(data),
version,
2021-09-24 00:49:05 +00:00
})) as {
requestId: string;
iv: string;
data: string;
mac: string;
};
2020-09-04 01:25:19 +00:00
// Decode discovery request response
2021-09-24 00:49:05 +00:00
const decodedDiscoveryResponse = (mapValues(discoveryResponse, value => {
return Bytes.fromBase64(value);
}) as unknown) as {
[K in keyof typeof discoveryResponse]: Uint8Array;
};
2020-09-04 01:25:19 +00:00
const returnedAttestation = Object.values(
attestationResult.attestations
).find(at =>
constantTimeEqual(at.requestId, decodedDiscoveryResponse.requestId)
);
if (!returnedAttestation) {
throw new Error('No known attestations returned from CDS');
}
// Decrypt discovery response
2021-09-24 00:49:05 +00:00
const decryptedDiscoveryData = decryptAesGcm(
2020-09-04 01:25:19 +00:00
returnedAttestation.serverKey,
decodedDiscoveryResponse.iv,
2021-09-24 00:49:05 +00:00
Bytes.concatenate([
2020-09-04 01:25:19 +00:00
decodedDiscoveryResponse.data,
2021-09-24 00:49:05 +00:00
decodedDiscoveryResponse.mac,
])
2020-09-04 01:25:19 +00:00
);
// Process and return result
const uuids = splitUuids(decryptedDiscoveryData);
if (uuids.length !== e164s.length) {
throw new Error(
'Returned set of UUIDs did not match returned set of e164s!'
);
}
return zipObject(e164s, uuids);
}
}
}