Added more types.

Finalise MessageEncrypter.
This commit is contained in:
Mikunj 2020-06-01 12:53:51 +10:00
parent 36762dbbf2
commit b644e2a05f
7 changed files with 84 additions and 16 deletions

View file

@ -179,7 +179,7 @@ export function updateGuardNodes(nodes: Array<string>): Promise<void>;
// Storage Items
export function createOrUpdateItem(data: StorageItem): Promise<void>;
export function getItemById(id: string): Promise<StorageItem>;
export function getItemById(id: string): Promise<StorageItem | undefined>;
export function getAlItems(): Promise<Array<StorageItem>>;
export function bulkAddItems(array: Array<StorageItem>): Promise<void>;
export function removeItemById(id: string): Promise<void>;

View file

@ -0,0 +1,23 @@
import { SignalService } from '../../../ts/protobuf';
import {
BinaryString,
CipherTextObject,
} from '../../../libtextsecure/libsignal-protocol';
export declare class SecretSessionCipher {
constructor(storage: any);
encrypt(
destinationPubkey: string,
senderCertificate: SignalService.SenderCertificate,
innerEncryptedMessage: CipherTextObject
): Promise<ArrayBuffer>;
decrypt(
cipherText: ArrayBuffer,
me: { number: string; deviceId: number }
): Promise<{
isMe?: boolean;
sender: string;
content: ArrayBuffer;
type: SignalService.Envelope.Type;
}>;
}

9
js/modules/signal.d.ts vendored Normal file
View file

@ -0,0 +1,9 @@
import { SecretSessionCipher } from './metadata/SecretSessionCipher';
interface Metadata {
SecretSessionCipher: typeof SecretSessionCipher;
}
export interface SignalInterface {
Metadata: Metadata;
}

View file

@ -1,12 +1,10 @@
import { EncryptionType } from '../types/EncryptionType';
import { SignalService } from '../../protobuf';
import { libloki, libsignal, textsecure } from '../../window';
import {
CipherTextObject,
SignalProtocolAddress,
} from '../../../libtextsecure/libsignal-protocol';
import { libloki, libsignal, Signal, textsecure } from '../../window';
import { CipherTextObject } from '../../../libtextsecure/libsignal-protocol';
import { UserUtil } from '../../util';
function padPlainTextBuffer(messageBuffer: Uint8Array): Uint8Array {
export function padPlainTextBuffer(messageBuffer: Uint8Array): Uint8Array {
const plaintext = new Uint8Array(
getPaddedMessageLength(messageBuffer.byteLength + 1) - 1
);
@ -53,31 +51,49 @@ export async function encrypt(
throw new Error('Encryption is not yet supported');
}
let cipherText: CipherTextObject;
let innerCipherText: CipherTextObject;
if (encryptionType === EncryptionType.SessionReset) {
const cipher = new libloki.crypto.FallBackSessionCipher(address);
cipherText = await cipher.encrypt(plainText.buffer);
innerCipherText = await cipher.encrypt(plainText.buffer);
} else {
const cipher = new libsignal.SessionCipher(
textsecure.storage.protocol,
address
);
cipherText = await cipher.encrypt(plainText.buffer);
innerCipherText = await cipher.encrypt(plainText.buffer);
}
return encryptUsingSealedSender(address, cipherText);
return encryptUsingSealedSender(device, innerCipherText);
}
async function encryptUsingSealedSender(
address: SignalProtocolAddress,
cipherText: CipherTextObject
device: string,
innerCipherText: CipherTextObject
): Promise<{
envelopeType: SignalService.Envelope.Type;
cipherText: Base64String;
}> {
// TODO: Do stuff here
const ourNumber = await UserUtil.getCurrentDevicePubKey();
if (!ourNumber) {
throw new Error('Failed to fetch current device public key.');
}
const certificate = SignalService.SenderCertificate.create({
sender: ourNumber,
senderDevice: 1,
});
const cipher = new Signal.Metadata.SecretSessionCipher(
textsecure.storage.protocol
);
const cipherTextBuffer = await cipher.encrypt(
device,
certificate,
innerCipherText
);
return {
envelopeType: SignalService.Envelope.Type.UNIDENTIFIED_SENDER,
cipherText: 'implement me!',
cipherText: Buffer.from(cipherTextBuffer).toString('base64'),
};
}

View file

@ -4,6 +4,7 @@ import { isFileDangerous } from './isFileDangerous';
import { missingCaseError } from './missingCaseError';
import { migrateColor } from './migrateColor';
import { makeLookup } from './makeLookup';
import * as UserUtil from './user';
export {
arrayBufferToObjectURL,
@ -12,4 +13,5 @@ export {
makeLookup,
migrateColor,
missingCaseError,
UserUtil,
};

17
ts/util/user.ts Normal file
View file

@ -0,0 +1,17 @@
import { getItemById } from '../../js/modules/data';
import { KeyPair } from '../../libtextsecure/libsignal-protocol';
export async function getCurrentDevicePubKey(): Promise<string | undefined> {
const item = await getItemById('number_id');
if (!item || !item.value) {
return undefined;
}
return item.value.split('.')[0];
}
export async function getIdentityKeyPair(): Promise<KeyPair | undefined> {
const item = await getItemById('identityKey');
return item?.value;
}

View file

@ -1,5 +1,6 @@
import { LocalizerType } from './types/Util';
import { LibsignalProtocol } from '../libtextsecure/libsignal-protocol';
import { SignalInterface } from '../js/modules/signal';
interface WindowInterface extends Window {
seedNodeList: any;
@ -37,7 +38,7 @@ interface WindowInterface extends Window {
libloki: any;
displayNameRegex: any;
Signal: any;
Signal: SignalInterface;
Whisper: any;
ConversationController: any;