2024-03-15 14:20:33 +00:00
|
|
|
// Copyright 2023 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { pipeline } from 'stream/promises';
|
2024-04-15 20:54:21 +00:00
|
|
|
import { PassThrough } from 'stream';
|
|
|
|
import type { Readable, Writable } from 'stream';
|
2024-03-15 14:20:33 +00:00
|
|
|
import { createWriteStream } from 'fs';
|
2024-04-15 20:54:21 +00:00
|
|
|
import { createGzip, createGunzip } from 'zlib';
|
|
|
|
import { createCipheriv, createHmac, randomBytes } from 'crypto';
|
|
|
|
import { noop } from 'lodash';
|
2024-03-15 14:20:33 +00:00
|
|
|
|
|
|
|
import * as log from '../../logging/log';
|
|
|
|
import * as Bytes from '../../Bytes';
|
2024-04-22 14:11:36 +00:00
|
|
|
import { strictAssert } from '../../util/assert';
|
|
|
|
import { drop } from '../../util/drop';
|
2024-03-15 14:20:33 +00:00
|
|
|
import { DelimitedStream } from '../../util/DelimitedStream';
|
2024-04-15 20:54:21 +00:00
|
|
|
import { appendPaddingStream } from '../../util/logPadding';
|
|
|
|
import { prependStream } from '../../util/prependStream';
|
|
|
|
import { appendMacStream } from '../../util/appendMacStream';
|
2024-04-22 14:11:36 +00:00
|
|
|
import { HOUR } from '../../util/durations';
|
2024-04-15 20:54:21 +00:00
|
|
|
import { CipherType, HashType } from '../../types/Crypto';
|
2024-03-15 14:20:33 +00:00
|
|
|
import * as Errors from '../../types/errors';
|
2024-04-22 14:11:36 +00:00
|
|
|
import { constantTimeEqual } from '../../Crypto';
|
2024-04-15 20:54:21 +00:00
|
|
|
import { getIvAndDecipher, getMacAndUpdateHmac } from '../../AttachmentCrypto';
|
2024-03-15 14:20:33 +00:00
|
|
|
import { BackupExportStream } from './export';
|
|
|
|
import { BackupImportStream } from './import';
|
2024-04-22 14:11:36 +00:00
|
|
|
import { getKeyMaterial } from './crypto';
|
|
|
|
import { BackupCredentials } from './credentials';
|
|
|
|
import { BackupAPI } from './api';
|
2024-03-15 14:20:33 +00:00
|
|
|
|
2024-04-15 20:54:21 +00:00
|
|
|
const IV_LENGTH = 16;
|
|
|
|
|
2024-04-22 14:11:36 +00:00
|
|
|
const BACKUP_REFRESH_INTERVAL = 24 * HOUR;
|
2024-04-15 20:54:21 +00:00
|
|
|
|
2024-03-15 14:20:33 +00:00
|
|
|
export class BackupsService {
|
2024-04-22 14:11:36 +00:00
|
|
|
private isStarted = false;
|
2024-03-15 14:20:33 +00:00
|
|
|
private isRunning = false;
|
|
|
|
|
2024-04-22 14:11:36 +00:00
|
|
|
public readonly credentials = new BackupCredentials();
|
|
|
|
public readonly api = new BackupAPI(this.credentials);
|
|
|
|
|
|
|
|
public start(): void {
|
|
|
|
strictAssert(!this.isStarted, 'Already started');
|
|
|
|
this.isStarted = true;
|
|
|
|
|
|
|
|
setInterval(() => {
|
|
|
|
drop(this.runPeriodicRefresh());
|
|
|
|
}, BACKUP_REFRESH_INTERVAL);
|
|
|
|
|
|
|
|
drop(this.runPeriodicRefresh());
|
|
|
|
this.credentials.start();
|
|
|
|
}
|
|
|
|
|
2024-04-15 20:54:21 +00:00
|
|
|
public async exportBackup(sink: Writable): Promise<void> {
|
2024-04-22 14:11:36 +00:00
|
|
|
strictAssert(!this.isRunning, 'BackupService is already running');
|
2024-03-15 14:20:33 +00:00
|
|
|
|
|
|
|
log.info('exportBackup: starting...');
|
|
|
|
this.isRunning = true;
|
|
|
|
|
2024-04-15 20:54:21 +00:00
|
|
|
try {
|
|
|
|
const { aesKey, macKey } = getKeyMaterial();
|
2024-03-15 14:20:33 +00:00
|
|
|
|
2024-04-15 20:54:21 +00:00
|
|
|
const recordStream = new BackupExportStream();
|
|
|
|
recordStream.run();
|
2024-03-15 14:20:33 +00:00
|
|
|
|
2024-04-15 20:54:21 +00:00
|
|
|
const iv = randomBytes(IV_LENGTH);
|
2024-03-15 14:20:33 +00:00
|
|
|
|
2024-04-15 20:54:21 +00:00
|
|
|
await pipeline(
|
|
|
|
recordStream,
|
|
|
|
createGzip(),
|
|
|
|
appendPaddingStream(),
|
|
|
|
createCipheriv(CipherType.AES256CBC, aesKey, iv),
|
|
|
|
prependStream(iv),
|
|
|
|
appendMacStream(macKey),
|
|
|
|
sink
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
log.info('exportBackup: finished...');
|
|
|
|
this.isRunning = false;
|
|
|
|
}
|
2024-03-15 14:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test harness
|
|
|
|
public async exportBackupData(): Promise<Uint8Array> {
|
2024-04-15 20:54:21 +00:00
|
|
|
const sink = new PassThrough();
|
|
|
|
|
2024-03-15 14:20:33 +00:00
|
|
|
const chunks = new Array<Uint8Array>();
|
2024-04-15 20:54:21 +00:00
|
|
|
sink.on('data', chunk => chunks.push(chunk));
|
|
|
|
await this.exportBackup(sink);
|
2024-03-15 14:20:33 +00:00
|
|
|
|
|
|
|
return Bytes.concatenate(chunks);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test harness
|
|
|
|
public async exportToDisk(path: string): Promise<void> {
|
2024-04-15 20:54:21 +00:00
|
|
|
await this.exportBackup(createWriteStream(path));
|
2024-03-15 14:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test harness
|
|
|
|
public async exportWithDialog(): Promise<void> {
|
|
|
|
const data = await this.exportBackupData();
|
|
|
|
|
|
|
|
const { saveAttachmentToDisk } = window.Signal.Migrations;
|
|
|
|
|
|
|
|
await saveAttachmentToDisk({
|
|
|
|
name: 'backup.bin',
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-04-15 20:54:21 +00:00
|
|
|
public async importBackup(createBackupStream: () => Readable): Promise<void> {
|
2024-04-22 14:11:36 +00:00
|
|
|
strictAssert(!this.isRunning, 'BackupService is already running');
|
2024-03-15 14:20:33 +00:00
|
|
|
|
|
|
|
log.info('importBackup: starting...');
|
|
|
|
this.isRunning = true;
|
|
|
|
|
|
|
|
try {
|
2024-04-15 20:54:21 +00:00
|
|
|
const { aesKey, macKey } = getKeyMaterial();
|
|
|
|
|
|
|
|
// First pass - don't decrypt, only verify mac
|
|
|
|
let hmac = createHmac(HashType.size256, macKey);
|
|
|
|
let theirMac: Uint8Array | undefined;
|
|
|
|
|
|
|
|
const sink = new PassThrough();
|
|
|
|
// Discard the data in the first pass
|
|
|
|
sink.resume();
|
|
|
|
|
|
|
|
await pipeline(
|
|
|
|
createBackupStream(),
|
|
|
|
getMacAndUpdateHmac(hmac, theirMacValue => {
|
|
|
|
theirMac = theirMacValue;
|
|
|
|
}),
|
|
|
|
sink
|
|
|
|
);
|
|
|
|
|
2024-04-22 14:11:36 +00:00
|
|
|
strictAssert(theirMac != null, 'importBackup: Missing MAC');
|
|
|
|
strictAssert(
|
|
|
|
constantTimeEqual(hmac.digest(), theirMac),
|
|
|
|
'importBackup: Bad MAC'
|
|
|
|
);
|
2024-04-15 20:54:21 +00:00
|
|
|
|
|
|
|
// Second pass - decrypt (but still check the mac at the end)
|
|
|
|
hmac = createHmac(HashType.size256, macKey);
|
|
|
|
|
2024-03-15 14:20:33 +00:00
|
|
|
await pipeline(
|
2024-04-15 20:54:21 +00:00
|
|
|
createBackupStream(),
|
|
|
|
getMacAndUpdateHmac(hmac, noop),
|
|
|
|
getIvAndDecipher(aesKey),
|
|
|
|
createGunzip(),
|
2024-03-15 14:20:33 +00:00
|
|
|
new DelimitedStream(),
|
|
|
|
new BackupImportStream()
|
|
|
|
);
|
2024-04-15 20:54:21 +00:00
|
|
|
|
2024-04-22 14:11:36 +00:00
|
|
|
strictAssert(
|
|
|
|
constantTimeEqual(hmac.digest(), theirMac),
|
|
|
|
'importBackup: Bad MAC, second pass'
|
|
|
|
);
|
2024-04-15 20:54:21 +00:00
|
|
|
|
2024-03-15 14:20:33 +00:00
|
|
|
log.info('importBackup: finished...');
|
|
|
|
} catch (error) {
|
|
|
|
log.info(`importBackup: failed, error: ${Errors.toLogFormat(error)}`);
|
|
|
|
throw error;
|
|
|
|
} finally {
|
|
|
|
this.isRunning = false;
|
|
|
|
}
|
|
|
|
}
|
2024-04-22 14:11:36 +00:00
|
|
|
|
|
|
|
private async runPeriodicRefresh(): Promise<void> {
|
|
|
|
try {
|
|
|
|
await this.api.refresh();
|
|
|
|
log.info('Backup: refreshed');
|
|
|
|
} catch (error) {
|
|
|
|
log.error('Backup: periodic refresh failed', Errors.toLogFormat(error));
|
|
|
|
}
|
|
|
|
}
|
2024-03-15 14:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const backupsService = new BackupsService();
|