Use the new getCryptoManager when editing journals/userInfos.

This also fixes an issue when editing and deleting journals after an encryption
password change.
master
Tom Hacohen 5 years ago
parent f5df8cb4e1
commit a252078d65

@ -155,15 +155,8 @@ class JournalMembers extends React.PureComponent<PropsTypeInner> {
const journal = syncJournal.journal; const journal = syncJournal.journal;
const derived = this.props.etesync.encryptionKey; const derived = this.props.etesync.encryptionKey;
const keyPair = userInfo.getKeyPair(new EteSync.CryptoManager(derived, 'userInfo', userInfo.version)); const keyPair = userInfo.getKeyPair(userInfo.getCryptoManager(derived));
let cryptoManager: EteSync.CryptoManager; const cryptoManager = journal.getCryptoManager(derived, keyPair);
if (journal.key) {
const asymmetricCryptoManager = new EteSync.AsymmetricCryptoManager(keyPair);
const derivedJournalKey = asymmetricCryptoManager.decryptBytes(journal.key);
cryptoManager = EteSync.CryptoManager.fromDerivedKey(derivedJournalKey, journal.version);
} else {
cryptoManager = new EteSync.CryptoManager(derived, journal.uid, journal.version);
}
const pubkeyBytes = sjcl.codec.bytes.fromBits(sjcl.codec.base64.toBits(publicKey)); const pubkeyBytes = sjcl.codec.bytes.fromBits(sjcl.codec.base64.toBits(publicKey));
const encryptedKey = sjcl.codec.base64.fromBits(sjcl.codec.bytes.toBits(cryptoManager.getEncryptedKey(keyPair, pubkeyBytes))); const encryptedKey = sjcl.codec.base64.fromBits(sjcl.codec.bytes.toBits(cryptoManager.getEncryptedKey(keyPair, pubkeyBytes)));

@ -11,8 +11,6 @@ import { List, ListItem } from '../widgets/List';
import AppBarOverride from '../widgets/AppBarOverride'; import AppBarOverride from '../widgets/AppBarOverride';
import Container from '../widgets/Container'; import Container from '../widgets/Container';
import * as EteSync from '../api/EteSync';
import { routeResolver } from '../App'; import { routeResolver } from '../App';
import { JournalsData, UserInfoData, CredentialsData } from '../store'; import { JournalsData, UserInfoData, CredentialsData } from '../store';
@ -32,21 +30,11 @@ class JournalsList extends React.PureComponent {
public render() { public render() {
const derived = this.props.etesync.encryptionKey; const derived = this.props.etesync.encryptionKey;
let asymmetricCryptoManager: EteSync.AsymmetricCryptoManager;
const journalMap = this.props.journals.reduce( const journalMap = this.props.journals.reduce(
(ret, journal) => { (ret, journal) => {
let cryptoManager: EteSync.CryptoManager;
if (journal.key) {
if (!asymmetricCryptoManager) {
const userInfo = this.props.userInfo; const userInfo = this.props.userInfo;
const keyPair = userInfo.getKeyPair(new EteSync.CryptoManager(derived, 'userInfo', userInfo.version)); const keyPair = userInfo.getKeyPair(userInfo.getCryptoManager(derived));
asymmetricCryptoManager = new EteSync.AsymmetricCryptoManager(keyPair); const cryptoManager = journal.getCryptoManager(derived, keyPair);
}
const derivedJournalKey = asymmetricCryptoManager.decryptBytes(journal.key);
cryptoManager = EteSync.CryptoManager.fromDerivedKey(derivedJournalKey, journal.version);
} else {
cryptoManager = new EteSync.CryptoManager(derived, journal.uid, journal.version);
}
const info = journal.getInfo(cryptoManager); const info = journal.getInfo(cryptoManager);
ret[info.type] = ret[info.type] || []; ret[info.type] = ret[info.type] || [];
ret[info.type].push( ret[info.type].push(

@ -118,8 +118,14 @@ class Journals extends React.PureComponent {
} }
public onItemSave(info: EteSync.CollectionInfo, originalInfo?: EteSync.CollectionInfo) { public onItemSave(info: EteSync.CollectionInfo, originalInfo?: EteSync.CollectionInfo) {
const journal = new EteSync.Journal(); const syncJournal = this.props.syncInfo.get(info.uid);
const cryptoManager = new EteSync.CryptoManager(this.props.etesync.encryptionKey, info.uid);
const derived = this.props.etesync.encryptionKey;
const userInfo = this.props.userInfo;
const existingJournal = (syncJournal) ? syncJournal.journal.serialize() : { uid: info.uid };
const journal = new EteSync.Journal(existingJournal);
const keyPair = userInfo.getKeyPair(userInfo.getCryptoManager(derived));
const cryptoManager = journal.getCryptoManager(derived, keyPair);
journal.setInfo(cryptoManager, info); journal.setInfo(cryptoManager, info);
if (originalInfo) { if (originalInfo) {
@ -134,8 +140,14 @@ class Journals extends React.PureComponent {
} }
public onItemDelete(info: EteSync.CollectionInfo) { public onItemDelete(info: EteSync.CollectionInfo) {
const journal = new EteSync.Journal(); const syncJournal = this.props.syncInfo.get(info.uid);
const cryptoManager = new EteSync.CryptoManager(this.props.etesync.encryptionKey, info.uid);
const derived = this.props.etesync.encryptionKey;
const userInfo = this.props.userInfo;
const existingJournal = (syncJournal) ? syncJournal.journal.serialize() : { uid: info.uid };
const journal = new EteSync.Journal(existingJournal);
const keyPair = userInfo.getKeyPair(userInfo.getCryptoManager(derived));
const cryptoManager = journal.getCryptoManager(derived, keyPair);
journal.setInfo(cryptoManager, info); journal.setInfo(cryptoManager, info);
store.dispatch<any>(deleteJournal(this.props.etesync, journal)).then(() => store.dispatch<any>(deleteJournal(this.props.etesync, journal)).then(() =>

@ -53,9 +53,8 @@ const syncInfoSelector = createSelector(
(props: PropsTypeInner) => props.userInfo.value!, (props: PropsTypeInner) => props.userInfo.value!,
(etesync, journals, entries, userInfo) => { (etesync, journals, entries, userInfo) => {
const derived = etesync.encryptionKey; const derived = etesync.encryptionKey;
let asymmetricCryptoManager: EteSync.AsymmetricCryptoManager; const userInfoCryptoManager = userInfo.getCryptoManager(etesync.encryptionKey);
try { try {
const userInfoCryptoManager = new EteSync.CryptoManager(etesync.encryptionKey, 'userInfo');
userInfo.verify(userInfoCryptoManager); userInfo.verify(userInfoCryptoManager);
} catch (error) { } catch (error) {
if (error instanceof EteSync.IntegrityError) { if (error instanceof EteSync.IntegrityError) {
@ -74,17 +73,8 @@ const syncInfoSelector = createSelector(
return ret; return ret;
} }
let cryptoManager: EteSync.CryptoManager; const keyPair = userInfo.getKeyPair(userInfoCryptoManager);
if (journal.key) { const cryptoManager = journal.getCryptoManager(derived, keyPair);
if (!asymmetricCryptoManager) {
const keyPair = userInfo.getKeyPair(new EteSync.CryptoManager(derived, 'userInfo', userInfo.version));
asymmetricCryptoManager = new EteSync.AsymmetricCryptoManager(keyPair);
}
const derivedJournalKey = asymmetricCryptoManager.decryptBytes(journal.key);
cryptoManager = EteSync.CryptoManager.fromDerivedKey(derivedJournalKey, journal.version);
} else {
cryptoManager = new EteSync.CryptoManager(derived, journal.uid, journal.version);
}
const collectionInfo = journal.getInfo(cryptoManager); const collectionInfo = journal.getInfo(cryptoManager);
@ -124,7 +114,7 @@ class SyncGate extends React.PureComponent<PropsTypeInner> {
collection.type = collectionType; collection.type = collectionType;
collection.displayName = 'Default'; collection.displayName = 'Default';
const journal = new EteSync.Journal(); const journal = new EteSync.Journal({ uid: collection.uid });
const cryptoManager = new EteSync.CryptoManager(this.props.etesync.encryptionKey, collection.uid); const cryptoManager = new EteSync.CryptoManager(this.props.etesync.encryptionKey, collection.uid);
journal.setInfo(cryptoManager, collection); journal.setInfo(cryptoManager, collection);
store.dispatch<any>(addJournal(this.props.etesync, journal)).then( store.dispatch<any>(addJournal(this.props.etesync, journal)).then(
@ -144,7 +134,7 @@ class SyncGate extends React.PureComponent<PropsTypeInner> {
} else { } else {
const userInfo = new EteSync.UserInfo(me, CURRENT_VERSION); const userInfo = new EteSync.UserInfo(me, CURRENT_VERSION);
const keyPair = EteSync.AsymmetricCryptoManager.generateKeyPair(); const keyPair = EteSync.AsymmetricCryptoManager.generateKeyPair();
const cryptoManager = new EteSync.CryptoManager(this.props.etesync.encryptionKey, 'userInfo'); const cryptoManager = userInfo.getCryptoManager(this.props.etesync.encryptionKey);
userInfo.setKeyPair(cryptoManager, keyPair); userInfo.setKeyPair(cryptoManager, keyPair);

@ -27,15 +27,8 @@ export function createJournalEntryFromSyncEntry(
const derived = etesync.encryptionKey; const derived = etesync.encryptionKey;
let cryptoManager: EteSync.CryptoManager; const keyPair = userInfo.getKeyPair(userInfo.getCryptoManager(derived));
if (journal.key) { const cryptoManager = journal.getCryptoManager(derived, keyPair);
const keyPair = userInfo.getKeyPair(new EteSync.CryptoManager(derived, 'userInfo', userInfo.version));
const asymmetricCryptoManager = new EteSync.AsymmetricCryptoManager(keyPair);
const derivedJournalKey = asymmetricCryptoManager.decryptBytes(journal.key);
cryptoManager = EteSync.CryptoManager.fromDerivedKey(derivedJournalKey, journal.version);
} else {
cryptoManager = new EteSync.CryptoManager(derived, journal.uid, journal.version);
}
const entry = new EteSync.Entry(); const entry = new EteSync.Entry();
entry.setSyncEntry(cryptoManager, syncEntry, prevUid); entry.setSyncEntry(cryptoManager, syncEntry, prevUid);

@ -56,7 +56,7 @@ const journalsDeserialize = (state: {}) => {
const newState = new Map<string, EteSync.Journal>(); const newState = new Map<string, EteSync.Journal>();
Object.keys(state).forEach((uid) => { Object.keys(state).forEach((uid) => {
const x = state[uid]; const x = state[uid];
const ret = new EteSync.Journal(uid, x.version); const ret = new EteSync.Journal({ uid }, x.version);
ret.deserialize(x); ret.deserialize(x);
newState.set(uid, ret); newState.set(uid, ret);
}); });

Loading…
Cancel
Save