-
-
Notifications
You must be signed in to change notification settings - Fork 583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passphrase Support for e2e backups #786
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
44d9927
Support passphrase-based e2e key backups
dbkr cb51799
Make backup restore work
dbkr 6047838
lint
dbkr eeea706
Add randomString factored out from client secret
dbkr abd2ac7
Rename backup API call in test
dbkr 092f421
docs
dbkr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,81 @@ | ||
/* | ||
Copyright 2018 New Vector Ltd | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { randomString } from '../randomstring'; | ||
|
||
const DEFAULT_ITERATIONS = 500000; | ||
|
||
export async function keyForExistingBackup(backupData, password) { | ||
if (!global.Olm) { | ||
throw new Error("Olm is not available"); | ||
} | ||
|
||
const authData = backupData.auth_data; | ||
|
||
if (!authData.private_key_salt || !authData.private_key_iterations) { | ||
throw new Error( | ||
"Salt and/or iterations not found: " + | ||
"this backup cannot be restored with a passphrase", | ||
); | ||
} | ||
|
||
return await deriveKey( | ||
password, backupData.auth_data.private_key_salt, | ||
backupData.auth_data.private_key_iterations, | ||
); | ||
} | ||
|
||
export async function keyForNewBackup(password) { | ||
if (!global.Olm) { | ||
throw new Error("Olm is not available"); | ||
} | ||
|
||
const salt = randomString(32); | ||
|
||
const key = await deriveKey(password, salt, DEFAULT_ITERATIONS); | ||
|
||
return { key, salt, iterations: DEFAULT_ITERATIONS }; | ||
} | ||
|
||
async function deriveKey(password, salt, iterations) { | ||
const subtleCrypto = global.crypto.subtle; | ||
const TextEncoder = global.TextEncoder; | ||
if (!subtleCrypto || !TextEncoder) { | ||
// TODO: Implement this for node | ||
throw new Error("Password-based backup is not avaiable on this platform"); | ||
} | ||
|
||
const key = await subtleCrypto.importKey( | ||
'raw', | ||
new TextEncoder().encode(password), | ||
{name: 'PBKDF2'}, | ||
false, | ||
['deriveBits'], | ||
); | ||
|
||
const keybits = await subtleCrypto.deriveBits( | ||
{ | ||
name: 'PBKDF2', | ||
salt: new TextEncoder().encode(salt), | ||
iterations: iterations, | ||
hash: 'SHA-512', | ||
}, | ||
key, | ||
global.Olm.PRIVATE_KEY_LENGTH * 8, | ||
); | ||
|
||
return new Uint8Array(keybits); | ||
} |
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,26 @@ | ||
/* | ||
Copyright 2018 New Vector Ltd | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
export function randomString(len) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fwiw there's libraries for this if we want to avoid maintaining this sort of thing. https://www.npmjs.com/package/random-string for example |
||
let ret = ""; | ||
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
|
||
for (let i = 0; i < len; ++i) { | ||
ret += chars.charAt(Math.floor(Math.random() * chars.length)); | ||
} | ||
|
||
return ret; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this now returns a promise, not an object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I was assuming the promise was implicit for async functions, but the convention seem to be to put it explicitly.