1 import type { PublicKeyReference } from '@proton/crypto';
2 import { CryptoProxy } from '@proton/crypto';
4 import { API_KEY_SOURCE, RECIPIENT_TYPES } from '../../lib/constants';
5 import { getContactPublicKeyModel, sortApiKeys, sortPinnedKeys } from '../../lib/keys/publicKeys';
6 import { ExpiredPublicKey, SignOnlyPublicKey, ValidPublicKey } from './keys.data';
8 describe('get contact public key model', () => {
9 const publicKeyConfig = {
18 it('should mark valid key as capable of encryption', async () => {
19 const publicKey = await CryptoProxy.importPublicKey({ armoredKey: ValidPublicKey });
20 const contactModel = await getContactPublicKeyModel({
23 pinnedKeys: [publicKey],
27 const fingerprint = publicKey.getFingerprint();
28 expect(contactModel.encryptionCapableFingerprints.has(fingerprint)).toBeTrue();
31 it('should mark expired key as incapable of encryption', async () => {
32 const publicKey = await CryptoProxy.importPublicKey({ armoredKey: ExpiredPublicKey });
33 const contactModel = await getContactPublicKeyModel({
36 pinnedKeys: [publicKey],
40 const fingerprint = publicKey.getFingerprint();
41 expect(contactModel.encryptionCapableFingerprints.has(fingerprint)).toBeFalse();
44 it('should mark sign-only as incapable of encryption', async () => {
45 const publicKey = await CryptoProxy.importPublicKey({ armoredKey: SignOnlyPublicKey });
46 const contactModel = await getContactPublicKeyModel({
49 pinnedKeys: [publicKey],
53 const fingerprint = publicKey.getFingerprint();
54 expect(contactModel.encryptionCapableFingerprints.has(fingerprint)).toBeFalse();
57 it('should determine encryption flag based on `encryptToPinned` when pinned keys are present', async () => {
58 const publicKey = await CryptoProxy.importPublicKey({ armoredKey: ValidPublicKey });
59 const contactModel = await getContactPublicKeyModel({
62 publicKeys: [{ publicKey, armoredKey: '', flags: 1, source: API_KEY_SOURCE.WKD }],
65 pinnedKeys: [publicKey],
66 encryptToPinned: false,
67 encryptToUntrusted: true,
71 expect(contactModel.encrypt).toBeFalse();
74 it('should determine encryption flag based on `encryptToUntrusted` when only API keys are present', async () => {
75 const publicKey = await CryptoProxy.importPublicKey({ armoredKey: ValidPublicKey });
76 const contactModel = await getContactPublicKeyModel({
79 publicKeys: [{ publicKey, armoredKey: '', flags: 1, source: API_KEY_SOURCE.WKD }],
83 encryptToPinned: false,
84 encryptToUntrusted: true,
88 expect(contactModel.encrypt).toBeTrue();
91 it('should set the encryption flag when pinned keys are present and `encryptToPinned` is missing', async () => {
92 const publicKey = await CryptoProxy.importPublicKey({ armoredKey: ValidPublicKey });
93 const contactModel = await getContactPublicKeyModel({
96 publicKeys: [{ publicKey, armoredKey: '', flags: 1, source: API_KEY_SOURCE.WKD }],
99 pinnedKeys: [publicKey],
100 // encryptToPinned: undefined,
101 encryptToUntrusted: false,
105 expect(contactModel.encrypt).toBeTrue();
108 it('should not determine encryption flag based on `encryptToUntrusted` if API keys are missing', async () => {
109 const contactModel = await getContactPublicKeyModel({
116 // encryptToPinned: undefined,
117 encryptToUntrusted: true,
121 expect(contactModel.encrypt).toBeUndefined();
124 it('should not determine encryption flag based on `encryptToUntrusted` for internal users', async () => {
125 const publicKey = await CryptoProxy.importPublicKey({ armoredKey: ValidPublicKey });
126 const contactModel = await getContactPublicKeyModel({
130 RecipientType: RECIPIENT_TYPES.TYPE_INTERNAL,
131 publicKeys: [{ publicKey, armoredKey: '', flags: 1, source: API_KEY_SOURCE.PROTON }],
134 pinnedKeys: [publicKey],
135 // encryptToPinned: undefined,
136 encryptToUntrusted: false,
140 expect(contactModel.encrypt).toBeTrue();
144 describe('sortApiKeys', () => {
145 const generateFakeKey = (fingerprint: string) =>
150 }) as PublicKeyReference;
151 it('sort keys as expected', () => {
152 const fingerprints = [
153 'trustedObsoleteNotCompromised',
154 'notTrustedObsoleteCompromised',
155 'trustedNotObsoleteNotCompromised',
156 'notTrustedNotObsoleteCompromised',
157 'trustedNotObsoleteCompromised',
158 'trustedObsoleteCompromised',
159 'notTrustedNotObsoleteNotCompromised',
160 'notTrustedObsoleteNotCompromised',
162 const sortedKeys = sortApiKeys({
163 keys: fingerprints.map(generateFakeKey),
164 obsoleteFingerprints: new Set([
165 'trustedObsoleteNotCompromised',
166 'trustedObsoleteCompromised',
167 'notTrustedObsoleteNotCompromised',
168 'notTrustedObsoleteCompromised',
170 compromisedFingerprints: new Set([
171 'trustedObsoleteCompromised',
172 'trustedNotObsoleteCompromised',
173 'notTrustedObsoleteCompromised',
174 'notTrustedNotObsoleteCompromised',
176 trustedFingerprints: new Set([
177 'trustedObsoleteCompromised',
178 'trustedNotObsoleteCompromised',
179 'trustedObsoleteNotCompromised',
180 'trustedNotObsoleteNotCompromised',
183 expect(sortedKeys.map((key) => key.getFingerprint())).toEqual([
184 'trustedNotObsoleteNotCompromised',
185 'trustedObsoleteNotCompromised',
186 'trustedNotObsoleteCompromised',
187 'trustedObsoleteCompromised',
188 'notTrustedNotObsoleteNotCompromised',
189 'notTrustedObsoleteNotCompromised',
190 'notTrustedNotObsoleteCompromised',
191 'notTrustedObsoleteCompromised',
196 describe('sortPinnedKeys', () => {
197 const generateFakeKey = (fingerprint: string) =>
202 }) as PublicKeyReference;
203 it('sort keys as expected', () => {
204 const fingerprints = [
205 'cannotEncryptObsoleteNotCompromised',
206 'canEncryptObsoleteCompromised',
207 'cannotEncryptNotObsoleteNotCompromised',
208 'canEncryptNotObsoleteCompromised',
209 'cannotEncryptNotObsoleteCompromised',
210 'cannotEncryptObsoleteCompromised',
211 'canEncryptNotObsoleteNotCompromised',
212 'canEncryptObsoleteNotCompromised',
214 const sortedKeys = sortPinnedKeys({
215 keys: fingerprints.map(generateFakeKey),
216 obsoleteFingerprints: new Set([
217 'canEncryptObsoleteNotCompromised',
218 'canEncryptObsoleteCompromised',
219 'cannotEncryptObsoleteNotCompromised',
220 'cannotEncryptObsoleteCompromised',
222 compromisedFingerprints: new Set([
223 'canEncryptObsoleteCompromised',
224 'canEncryptNotObsoleteCompromised',
225 'cannotEncryptObsoleteCompromised',
226 'cannotEncryptNotObsoleteCompromised',
228 encryptionCapableFingerprints: new Set([
229 'canEncryptObsoleteCompromised',
230 'canEncryptNotObsoleteCompromised',
231 'canEncryptObsoleteNotCompromised',
232 'canEncryptNotObsoleteNotCompromised',
235 expect(sortedKeys.map((key) => key.getFingerprint())).toEqual([
236 'canEncryptNotObsoleteNotCompromised',
237 'canEncryptObsoleteNotCompromised',
238 'canEncryptNotObsoleteCompromised',
239 'canEncryptObsoleteCompromised',
240 'cannotEncryptNotObsoleteNotCompromised',
241 'cannotEncryptObsoleteNotCompromised',
242 'cannotEncryptNotObsoleteCompromised',
243 'cannotEncryptObsoleteCompromised',