1 import type { PrivateKeyReference, PublicKeyReference, SessionKey } from '@proton/crypto';
2 import { CryptoProxy } from '@proton/crypto';
3 import { binaryStringToArray, decodeBase64 } from '@proton/crypto/lib/utils';
5 import type { MIME_TYPES } from '../../constants';
6 import { hasBitBigInt } from '../../helpers/bitset';
7 import type { Attachment } from '../../interfaces/mail/Message';
8 import type { Packets } from '../../interfaces/mail/crypto';
9 import { MESSAGE_FLAGS } from '../constants';
11 export const encryptAttachment = async (
12 data: Uint8Array | string,
13 { name, type, size }: File = {} as File,
15 publicKeys: PublicKeyReference[],
16 privateKeys: PrivateKeyReference[] = []
17 ): Promise<Packets> => {
18 const dataType = data instanceof Uint8Array ? 'binaryData' : 'textData';
19 const sessionKey = await CryptoProxy.generateSessionKey({ recipientKeys: publicKeys });
21 // we encrypt using `sessionKey` directly instead of `encryptionKeys` so that returned message only includes
22 // symmetrically encrypted data
23 const { message: encryptedData, signature } = await CryptoProxy.encryptMessage({
25 detached: privateKeys.length > 0, // Only relevant if private keys are given
27 stripTrailingSpaces: dataType === 'textData',
29 signingKeys: privateKeys,
32 const encryptedSessionKey = await CryptoProxy.encryptSessionKey({
34 encryptionKeys: publicKeys[0],
40 MIMEType: type as MIME_TYPES,
45 keys: encryptedSessionKey,
50 export const getSessionKey = async (
51 attachment: Pick<Attachment, 'KeyPackets'>,
52 privateKeys: PrivateKeyReference[],
54 ): Promise<SessionKey> => {
55 // if (attachment.sessionKey) {
59 const keyPackets = binaryStringToArray(decodeBase64(attachment.KeyPackets) || '');
61 binaryMessage: keyPackets,
62 decryptionKeys: privateKeys,
64 allowForwardedMessages: hasBitBigInt(BigInt(messageFlags || 0), MESSAGE_FLAGS.FLAG_AUTO_FORWARDEE),
69 // options.passwords = [eoStore.getPassword()];
71 // options.privateKeys = keysModel.getPrivateKeys(message.AddressID);
74 const sessionKey = await CryptoProxy.decryptSessionKey(options);
76 if (sessionKey === undefined) {
77 throw new Error('Error while decrypting session keys');
83 export const getEOSessionKey = async (attachment: Attachment, password: string): Promise<SessionKey> => {
84 const keyPackets = binaryStringToArray(decodeBase64(attachment.KeyPackets) || '');
85 const options = { binaryMessage: keyPackets, passwords: [password] };
87 const sessionKey = await CryptoProxy.decryptSessionKey(options);
89 if (sessionKey === undefined) {
90 throw new Error('Error while decrypting session keys');