Skip to content
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

Fix/backup key mode no callback #181

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ <h2>
</td>
<td class="init-only device-specific ok-classic ok-go">
<button id="SetBackup" type="button">
Set Backup Passphrase or Key
Set Backup Passphrase
</button>
</td>
<td class="init-only device-specific ok-classic">
Expand Down Expand Up @@ -188,14 +188,14 @@ <h3>Set a Backup Key</h3>
<p id="step9-text"></p>
<p>
Your OpenPGP key will be used for secure backup and restore of
your OnlyKey, make sure to store it in a secure location.
your OnlyKey. Make sure to store it in a secure location.
</p>
<p>
Need a key? Follow our guide
Need a key? Follow
<a
href="https://docs.crp.to/importpgp.html#generating-keys"
class="external"
>here</a
>our guide</a
>
for generating an OpenPGP key.
</p>
Expand Down
54 changes: 24 additions & 30 deletions app/scripts/onlyKey/OnlyKeyComm.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ OnlyKey.prototype.setRSABackupKey = async function (key, passcode, cb) {
throw Error(error + "\n\n" + parseError);
}

await onlyKeyConfigWizard.initKeySelect(privKey, function (err) {
return onlyKeyConfigWizard.initKeySelect(privKey, function (err) {
ui.rsaForm.setError(err || "");
if (typeof cb === "function") cb(err);
});
Expand Down Expand Up @@ -851,7 +851,7 @@ OnlyKey.prototype.submitRestore = function (fileSelector, cbArg) {
}
};

OnlyKey.prototype.setPrivateKey = async function (slot, type, key, callback) {
OnlyKey.prototype.setPrivateKey = function (slot, type, key, callback) {
var msg, contentType;
if (Array.isArray(key) || key.constructor === Uint8Array) {
// RSA private key is an array of DEC bytes
Expand Down Expand Up @@ -958,11 +958,12 @@ OnlyKey.prototype.setmodkeyMode = function (modkeyMode) {
});
};

OnlyKey.prototype.setbackupKeyMode = function (backupKeyMode) {
OnlyKey.prototype.setbackupKeyMode = function (backupKeyMode, callback) {
backupKeyMode = parseInt(backupKeyMode, 10);
this.setSlot("XX", "BACKUPKEYMODE", backupKeyMode, async () => {
return await this.listenforvalue("set Backup Key Mode");
});
const cb = callback || async function () {
await this.listenforvalue("set Backup Key Mode");
}.bind(this);
return this.setSlot("XX", "BACKUPKEYMODE", backupKeyMode, cb);
};

OnlyKey.prototype.setTypeSpeed = function (typeSpeed) {
Expand Down Expand Up @@ -1865,9 +1866,7 @@ async function submitRsaForm(e) {
return ui.rsaForm.setError("Passcode cannot be empty.");
}

var privKey,
keyObj = {},
retKey;
let privKey;

try {
var privKeys = await openpgp.key.readArmored(key);
Expand Down Expand Up @@ -1899,7 +1898,7 @@ async function submitRsaForm(e) {
};
}

await onlyKeyConfigWizard.initKeySelect(allKeys, function (err) {
return onlyKeyConfigWizard.initKeySelect(allKeys, function (err) {
ui.rsaForm.setError(err || "");
});
}
Expand Down Expand Up @@ -1931,8 +1930,7 @@ OnlyKey.prototype.confirmRsaKeySelect = function (keyObj, slot, cb) {

var retKey = [...keyObj.p, ...keyObj.q];
}
var slot =
slot !== null ? slot : parseInt(ui.rsaForm.rsaSlot.value || "", 10);
slot = (slot !== null) ? slot : parseInt(ui.rsaForm.rsaSlot.value || "", 10);

// set all type modifiers
var typeModifier = 0;
Expand All @@ -1959,31 +1957,27 @@ OnlyKey.prototype.confirmRsaKeySelect = function (keyObj, slot, cb) {
console.info("Slot 2 set as signature key" + type);
}
}

let keyHandlerFn = submitRsaKey;

if (typeof keyObj.s !== "undefined") {
//ECC
if (slot < 101) slot += 100;
myOnlyKey.setPrivateKey(slot, type, retKey, (err) => {
// TODO: check for success, then reset
if (typeof cb === "function") cb(err);
ui.rsaForm.reset();
if (backupsigFlag >= 0) {
backupsigFlag = -1;
//reset backup form
}
this.listen(handleMessage);
});
} else {
submitRsaKey(slot, type, retKey, (err) => {
// TODO: check for success, then reset
if (typeof cb === "function") cb(err);
keyHandlerFn = myOnlyKey.setPrivateKey;
}

return keyHandlerFn(slot, type, retKey, (err) => {
if (err) {
return typeof cb === "function" && cb(err);
} else {
//reset backup form
ui.rsaForm.reset();
if (backupsigFlag >= 0) {
backupsigFlag = -1;
//reset backup form
}
this.listen(handleMessage);
});
}
return this.listen(cb || handleMessage);
}
});
};

function submitRsaKey(slot, type, key, callback) {
Expand Down
51 changes: 30 additions & 21 deletions app/scripts/onlyKey/OnlyKeyWizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ if (chrome.passwordsPrivate) {
}

Wizard.prototype.init = function (myOnlyKey) {
console.info('Wizard.init() called');
// reset all forms
document.querySelectorAll('form').forEach(form => form.reset());

Expand Down Expand Up @@ -243,13 +244,11 @@ if (chrome.passwordsPrivate) {
if (this.direction === NEXT) {
if (!this.checkInitialized() && this.advancedSetup) {
const backupKeyMode = this.initForm.backupKeyMode;
this.onlyKey.setbackupKeyMode(backupKeyMode.value, this.submitBackupKey.bind(this, cb));
} else {
// not going to next step due to [Previous] click
this.submitBackupKey(cb);
return this.onlyKey.setbackupKeyMode(backupKeyMode.value, this.submitBackupKey.bind(this, cb));
}
return this.submitBackupKey(cb);
} else {
cb();
return cb();
}
}
},
Expand All @@ -262,8 +261,8 @@ if (chrome.passwordsPrivate) {
this.onlyKey.flushMessage();
},
exitFn: (cb) => {
const backupKeyMode = this.initForm.backupKeyMode;
this.onlyKey.setbackupKeyMode(backupKeyMode.value, this.submitBackupRSAKey.bind(this, cb));
const backupKeyMode = this.initForm.backupKeyModePGP;
return this.onlyKey.setbackupKeyMode(backupKeyMode.value, this.submitBackupRSAKey.bind(this, cb));
}
},
Step10: { //Restore from backup
Expand Down Expand Up @@ -332,7 +331,7 @@ if (chrome.passwordsPrivate) {
document.getElementById('step8-2-text').innerHTML = `
<label>
<input type='radio' checked name='backupKeyMode' value=0 />
<u>Permit future backup key changes(Default)</u>
<u>Permit future backup key changes (Default)</u>
</label>
<br />
<label>
Expand All @@ -341,6 +340,7 @@ if (chrome.passwordsPrivate) {
</label>
<br />
<td>
<br />
<button id='SetPGPKey' type='button'>
<b>Use PGP Key instead of passphrase</b>
</button>
Expand Down Expand Up @@ -605,8 +605,8 @@ if (chrome.passwordsPrivate) {
};


Wizard.prototype.initKeySelect = async function (rawKey, cb) {
console.info(rawKey);
Wizard.prototype.initKeySelect = function (rawKey, cb) {
console.info({ rawKey });

//Check if PGP or SSH
if (rawKey.type == 'ed25519') {
Expand Down Expand Up @@ -709,16 +709,18 @@ if (chrome.passwordsPrivate) {
// then there is only one key, set it to slot this.rsaSlot_selection.value
const signingKeySlot = this.rsaSlot_selection.value === '99' ? 2 : this.rsaSlot_selection.value;

this.onlyKey.confirmRsaKeySelect(signingKey, signingKeySlot, err => {
return this.onlyKey.confirmRsaKeySelect(signingKey, signingKeySlot, (err, res) => {
if (err) return cb(err);
const decryptionKey = this.onlyKey.tempRsaKeys[1];
if (decryptionKey) {
this.onlyKey.confirmRsaKeySelect(decryptionKey, 1, err => {
this.onlyKey.confirmRsaKeySelect(decryptionKey, 1, (err, res) => {
if (err) return cb(err);
this.onlyKey.tempRsaKeys = null;
return cb(null, res);
});
this.onlyKey.tempRsaKeys = null;
this.reset();
} else {
this.onlyKey.tempRsaKeys = null;
this.reset();
return cb(null, res);
}
});
} else {
Expand All @@ -734,7 +736,6 @@ if (chrome.passwordsPrivate) {

this.dialog.open(this.selectPrivateKeyDialog, true);
}

return cb();
};

Expand Down Expand Up @@ -806,13 +807,21 @@ if (chrome.passwordsPrivate) {
}

if (!passcode) {
this.initConfigErrors.innerHTML = 'Passcode cannot be empty.';
this.initConfigErrors.innerHTML = 'Passphrase cannot be empty.';
return false;
}
backuprsaKey.value = '';
backuprsaPasscode.value = '';

this.onlyKey.setRSABackupKey(key, passcode, cb);
this.onlyKey.setRSABackupKey(key, passcode, (err, res) => {
if (err) {
return cb(err);
}

// reset form fields
backuprsaKey.value = '';
backuprsaPasscode.value = '';

return cb(null, res);
});
};

Wizard.prototype.setSlot = function () {
Expand Down Expand Up @@ -1415,7 +1424,7 @@ function makeRadioButton(name, value, text) {

function toggleAdvancedUI(e) {
e && e.preventDefault && e.preventDefault();
this.advancedSetup = e.target.checked;
this.setAdvancedSetup(e.target.checked);
this.initSteps();
}

Expand Down