-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Delete secrets DB when deleting current device (#16491)
- Loading branch information
Showing
5 changed files
with
191 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2024 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
* | ||
*/ | ||
|
||
import {Encoder, Decoder} from 'bazinga64'; | ||
|
||
import {SystemCrypto, wrapSystemCrypto} from './systemCryptoWrapper'; | ||
|
||
const systemCryptos = { | ||
v0: { | ||
encrypt: async (value: Uint8Array) => { | ||
return value; | ||
}, | ||
decrypt: async (value: Uint8Array) => { | ||
return value; | ||
}, | ||
version: undefined, | ||
} as SystemCrypto, | ||
|
||
v01: { | ||
encrypt: async (value: Uint8Array) => { | ||
return Encoder.toBase64(value).asBytes; | ||
}, | ||
decrypt: async (value: Uint8Array) => { | ||
return Decoder.fromBase64(Array.from(value.values())).asBytes; | ||
}, | ||
version: undefined, | ||
} as SystemCrypto, | ||
|
||
v1: { | ||
encrypt: async (value: string) => { | ||
const encoder = new TextEncoder(); | ||
return encoder.encode(value); | ||
}, | ||
decrypt: async (value: Uint8Array) => { | ||
const decoder = new TextDecoder(); | ||
return decoder.decode(value); | ||
}, | ||
version: 1, | ||
} as SystemCrypto, | ||
} as const; | ||
|
||
describe('systemCryptoWrapper', () => { | ||
it.each(Object.entries(systemCryptos))( | ||
'generates and store a secret key encrypted using system crypto (%s)', | ||
async (_name, systemCrypto) => { | ||
const {encrypt, decrypt} = wrapSystemCrypto(systemCrypto); | ||
|
||
const value = new Uint8Array([1, 2, 3, 4]); | ||
const encrypted = await encrypt(value); | ||
const decrypted = await decrypt(encrypted); | ||
|
||
expect(value).toEqual(decrypted); | ||
}, | ||
); | ||
|
||
it.each([['v01 > v1', systemCryptos.v01, systemCryptos.v1]])( | ||
'handles migration from old system crypto (%s)', | ||
async (_name, crypto1, crypto2) => { | ||
const wrap1 = wrapSystemCrypto(crypto1); | ||
const wrap2 = wrapSystemCrypto(crypto2); | ||
|
||
const value = new Uint8Array([1, 2, 3, 4]); | ||
const encrypted = await wrap1.encrypt(value); | ||
const decrypted = await wrap2.decrypt(encrypted); | ||
|
||
expect(value).toEqual(decrypted); | ||
}, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2024 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
* | ||
*/ | ||
|
||
import {Encoder, Decoder} from 'bazinga64'; | ||
|
||
type SystemCryptoV0 = { | ||
encrypt: (value: Uint8Array) => Promise<Uint8Array>; | ||
decrypt: (payload: Uint8Array) => Promise<Uint8Array>; | ||
version: undefined; | ||
}; | ||
type SystemCryptoV1 = { | ||
encrypt: (value: string) => Promise<Uint8Array>; | ||
decrypt: (payload: Uint8Array) => Promise<string>; | ||
version: 1; | ||
}; | ||
|
||
export type SystemCrypto = SystemCryptoV0 | SystemCryptoV1; | ||
|
||
export function wrapSystemCrypto(baseCrypto: SystemCrypto) { | ||
const isBase64 = /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$/; | ||
return { | ||
encrypt: (value: Uint8Array) => { | ||
if (baseCrypto.version === 1) { | ||
const strValue = Encoder.toBase64(value).asString; | ||
return (baseCrypto as SystemCryptoV1).encrypt(strValue); | ||
} | ||
// In previous versions of the systemCrypto (prior to February 2023), encrypt took a uint8Array | ||
return (baseCrypto as SystemCryptoV0).encrypt(value); | ||
}, | ||
|
||
decrypt: async (value: Uint8Array) => { | ||
if (typeof baseCrypto.version === 'undefined') { | ||
// In previous versions of the systemCrypto (prior to February 2023), the decrypt function returned a Uint8Array | ||
return (baseCrypto as SystemCryptoV0).decrypt(value); | ||
} | ||
const decrypted = await (baseCrypto as SystemCryptoV1).decrypt(value); | ||
if (isBase64.test(decrypted)) { | ||
return Decoder.fromBase64(decrypted).asBytes; | ||
} | ||
// Between June 2022 and October 2022, the systemCrypto returned a string encoded in UTF-8 | ||
const encoder = new TextEncoder(); | ||
|
||
return encoder.encode(decrypted); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters