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 derived = this.props.etesync.encryptionKey;
const keyPair = userInfo.getKeyPair(new EteSync.CryptoManager(derived, 'userInfo', userInfo.version));
let cryptoManager: EteSync.CryptoManager;
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 keyPair = userInfo.getKeyPair(userInfo.getCryptoManager(derived));
const cryptoManager = journal.getCryptoManager(derived, keyPair);
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)));

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

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

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

@ -27,15 +27,8 @@ export function createJournalEntryFromSyncEntry(
const derived = etesync.encryptionKey;
let cryptoManager: EteSync.CryptoManager;
if (journal.key) {
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 keyPair = userInfo.getKeyPair(userInfo.getCryptoManager(derived));
const cryptoManager = journal.getCryptoManager(derived, keyPair);
const entry = new EteSync.Entry();
entry.setSyncEntry(cryptoManager, syncEntry, prevUid);

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

Loading…
Cancel
Save