Skip to content

Commit b78fa4a

Browse files
committed
support ecc to rsa and vice versa
1 parent 85ece8f commit b78fa4a

4 files changed

Lines changed: 123 additions & 124 deletions

File tree

src/onlykey-fido2/onlykey/onlykey-pgp.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ module.exports = function(imports) {
7777
};
7878

7979
onlykey_pgp_api.getPublicKeyInfo = function(public_key, callback) {
80+
console.info(public_key);
8081
kbpgp2.KeyManager.import_from_armored_pgp({
8182
armored: public_key
8283
}, (error, keyObj) => {
@@ -88,6 +89,7 @@ module.exports = function(imports) {
8889
}
8990
});
9091
};
92+
9193
const OKDECRYPT = 240;
9294
const OKSIGN = 237;
9395
const OKPING = 243;
@@ -474,7 +476,7 @@ module.exports = function(imports) {
474476
}
475477

476478
async function startDecryption(signer, my_public, message, file, callback) {
477-
var sender_public_key, my_public_key;
479+
var sender_public_key, onlykeyKey;
478480

479481
_poll_type = 3;
480482
_poll_delay = 1;
@@ -500,16 +502,16 @@ module.exports = function(imports) {
500502
}
501503
else {
502504
if (my_public.slice(0, 10) != '-----BEGIN') { // Check if its a pasted public key
503-
my_public_key = await onlykeyApi.getKey(my_public);
505+
onlykeyKey = await onlykeyApi.getKey(my_public);
504506
}
505507
else {
506-
my_public_key = my_public;
508+
onlykeyKey = my_public;
507509
}
508510
}
509511

510512
if (message != null)
511-
decryptText(sender_public_key, my_public_key, message, callback);
512-
else decryptFile(sender_public_key, my_public_key, file, callback);
513+
decryptText(sender_public_key, onlykeyKey, message, callback);
514+
else decryptFile(sender_public_key, onlykeyKey, file, callback);
513515
};
514516

515517
function decryptText(key, onlykeyKey, encryptedMessage, callback) {
@@ -519,6 +521,7 @@ module.exports = function(imports) {
519521
switch (_$mode()) {
520522
case 'Decrypt and Verify':
521523
await keyStore.loadPublic(key);
524+
await keyStore.loadPublic(onlykeyKey);
522525
_api.emit("status", "Decrypting and verifying message ...");
523526
break;
524527
case 'Decrypt Only':
@@ -532,7 +535,7 @@ module.exports = function(imports) {
532535
kbpgp.unbox({
533536
keyfetch: keyStore.ring,
534537
armored: encryptedMessage,
535-
// strict: Decrypt_Only ? false : true
538+
strict: Decrypt_Only ? false : true
536539
}, (err, decryptedMessage) => {
537540
if (err) {
538541
_api.emit("error", err);
@@ -561,7 +564,6 @@ module.exports = function(imports) {
561564
console.log(keyid);
562565
console.log(userid);
563566
_api.emit("status", "Done :) Signed by " + userid + " (Key ID: " + keyid + "), Click here to copy message");
564-
_$status("finished");
565567
}
566568
}
567569
}
@@ -575,7 +577,7 @@ module.exports = function(imports) {
575577
});
576578
}
577579

578-
function decryptFile(key, my_public_key, ct, callback) {
580+
function decryptFile(key, onlykeyKey, ct, callback) {
579581
return new Promise(async(resolve) => {
580582
var txt = "";
581583
if ('files' in ct) {
@@ -629,6 +631,7 @@ module.exports = function(imports) {
629631
switch (_$mode()) {
630632
case 'Decrypt and Verify':
631633
await keyStore.loadPublic(key);
634+
await keyStore.loadPublic(onlykeyKey);
632635
_api.emit("status", "Decrypting and verifying...");
633636
break;
634637
case 'Decrypt Only':
@@ -638,7 +641,7 @@ module.exports = function(imports) {
638641
break;
639642
default:
640643
}
641-
await keyStore.loadPrivate(my_public_key);
644+
await keyStore.loadPrivate(onlykeyKey);
642645
kbpgp.unbox({
643646
keyfetch: keyStore.ring,
644647
raw: buffer,
@@ -672,7 +675,6 @@ module.exports = function(imports) {
672675
}
673676
}
674677
}
675-
_$status("finished");
676678
var finalfile = new Blob([ct[0].toBuffer()], { type: "text/plain;charset=utf-8" });
677679
saveAs(finalfile, filename);
678680
_api.emit("done");

src/plugins/decrypt/decrypt.js

Lines changed: 111 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var pagesList = {
1111
}
1212
};
1313

14+
1415
module.exports = {
1516
pagesList: pagesList,
1617
consumes: ["app"],
@@ -37,8 +38,9 @@ module.exports = {
3738
return arr1.join('');
3839
}
3940

40-
var page = {}; //encrypt and encrypt-file will take over this object;
41+
var terminal_applied = false;
4142

43+
var page = {};
4244
var $page = {
4345
view: require("./decrypt.page.html").default,
4446
init: function(app, $page, pathname) {
@@ -52,36 +54,48 @@ module.exports = {
5254
var $ = app.$;
5355
var onlykeyApi = app.onlykeyApi;
5456

55-
page.okpgp = onlykeyApi.pgp().api();
56-
57+
page.okpgp = app.onlykeyApi.pgp().api();
58+
5759
if (doInit) {
60+
5861
page.gun = app.newGun();
59-
60-
var params = app.pages.getAllUrlParams();
61-
if (params.type == 'dv') document.getElementById('decrypt_and_verify').checked = true;
62-
if (params.type == 'd') document.getElementById('decrypt_only').checked = true;
62+
63+
var params = onlykeyApi.api.getAllUrlParams();
64+
65+
page.initParams = params;
66+
6367
if (params.sender) document.getElementById('pgpkeyurl').value = params.sender;
64-
68+
// if (params.recipients) document.getElementById('pgpkeyurl').value = params.recipients;
69+
if (params.type == 'dv') {
70+
document.getElementById('decrypt_and_verify').checked = true;
71+
page.okpgp._$mode('Decrypt and Verify');
72+
}
73+
if (params.type == 'd') {
74+
document.getElementById('decrypt_only').checked = true;
75+
page.okpgp._$mode('Decrypt Only');
76+
document.getElementById('pgpkeyurl').style.display = "none";
77+
}
78+
6579
if (params.gm == 1) {
6680
var gm = params["#"].split("-");
6781
page.gun_message_key = gm[1];
6882
page.gun_message = gm_decode(gm[0]);
6983
}
70-
84+
7185
onlykeyApi.api.on("status", function(message) {
7286
page.button.textContent = message;
7387
});
74-
88+
7589
onlykeyApi.api.on("error", function(message) {
7690
console.log("okapi-error", message);
7791
page.button.textContent = message;
7892
page.button.classList.add('error');
7993
page.button.classList.remove('working');
94+
if (page.okpgp) page.okpgp.emit("completed");
8095
});
8196
}
82-
97+
8398
page.okpgp._$mode($("#action")[0].select_one.value);
84-
8599

86100
page.okpgp.on("status", function(message) {
87101
page.button.textContent = message;
@@ -109,6 +123,8 @@ module.exports = {
109123

110124
app.xterm.writeln("PGP Mode to " + page.okpgp._$mode());
111125

126+
if (page.initParams.recipients) document.getElementById('pgpkeyurl2').value = page.initParams.recipients;
127+
112128
$(".messageLink").html("");
113129
onlykeyApi.api.request_pgp_pubkey = function() {
114130
function error_1(err) {
@@ -152,17 +168,64 @@ module.exports = {
152168
}
153169
}
154170

171+
onlykeyApi.api.request_pgp_pubkey = function() {
172+
function error_1(err) {
173+
page.button.textContent = err;
174+
}
175+
176+
function error_2(err) {
177+
page.button.textContent = 'Your PublicKey is ' + err;
178+
}
179+
return new Promise(async function(resolve) {
180+
var pubkey = $("#pgpkeyurl2").val();
181+
if (pubkey == "" || !pubkey) {
182+
resolve({ value: false, on_error: error_1 });
183+
}
184+
else {
185+
pubkey = await onlykeyApi.api.getKey(pubkey);
186+
resolve({
187+
value: pubkey,
188+
on_error: error_2
189+
});
190+
}
191+
});
192+
};
155193

156194
document.getElementsByTagName('fieldset')[0].style.backgroundColor = app.randomColor({
157195
luminosity: 'bright',
158196
format: 'rgba'
159197
});
160198

161-
page.urlinputbox = document.getElementById('pgpkeyurl');
162199
page.urlinputbox2 = document.getElementById('pgpkeyurl2');
200+
page.urlinputbox = document.getElementById('pgpkeyurl');
163201
page.messagebox = document.getElementById('message');
164202
page.button = document.getElementById('onlykey_start');
165203

204+
var pageType = false;
205+
206+
if (page.okpgp._$mode_is('Decrypt Only')) {
207+
document.getElementById('pgpkeyurl2').style.display = "initial";
208+
document.getElementById('pgpkeyurl').style.display = "none";
209+
page.button.textContent = 'Decrypt';
210+
pageType = "d";
211+
}
212+
if (page.okpgp._$mode_is('Decrypt and Verify')) {
213+
document.getElementById('pgpkeyurl').style.display = "initial";
214+
document.getElementById('pgpkeyurl2').style.display = "initial";
215+
page.button.textContent = 'Decrypt and Verify';
216+
pageType = "dv";
217+
}
218+
219+
if (pageType)
220+
app.pages.state.replace({ pathname: pathname }, $("title").text(),
221+
"./" + pathname +
222+
"?type=" + pageType +
223+
(page.initParams.recipients ? "&recipients=" + page.initParams.recipients : ''));
224+
225+
$(".messageLink").html("");
226+
//$(window).scrollTo("h1", 1000);
227+
228+
166229
page.button.classList.remove('error');
167230
page.button.classList.remove('working');
168231

@@ -193,33 +256,21 @@ module.exports = {
193256
};
194257
dlGM();
195258

196-
var pageType = false;
197-
198-
switch (page.okpgp._$mode()) {
199-
case 'Decrypt and Verify':
200-
$("#pgpkeyurl").show();
201-
$("#pgpkeyurl2").show();
202-
$("#pgpkeyurl_tokenizer").show();
203-
$("#message").show();
204-
page.button.textContent = 'Decrypt and Verify';
205-
pageType = "dv";
206-
break;
207-
case 'Decrypt Only':
208-
$("#pgpkeyurl").show();
209-
$("#pgpkeyurl2").show();
210-
$("#pgpkeyurl_tokenizer").show();
211-
$("#message").show();
212-
pageType = "d";
213-
page.button.textContent = 'Decrypt';
214-
break;
215-
}
216-
217259

218-
if (pageType)
219-
app.pages.state.replace({ pathname: pathname }, $("title").text(), "./" + pathname + "?type=" + pageType);
260+
$("#pgpkeyurl2").change(function() {
261+
switch (page.okpgp._$mode()) {
262+
case 'Decrypt and Verify':
263+
pageType = "dv";
264+
break;
265+
case 'Decrypt Only':
266+
pageType = "d";
267+
break;
268+
}
220269

270+
if (pageType)
271+
app.pages.state.replace({ pathname: pathname }, $("title").text(), "./" + pathname + "?type=" + pageType + "&recipients=" + page.urlinputbox.value);
221272

222-
//$(window).scrollTo("h1", 1000);
273+
});
223274

224275
page.okpgp.on("completed", function(data) {
225276
$(".messageLink").html("");
@@ -239,17 +290,30 @@ module.exports = {
239290

240291
});
241292

242-
243293
if (!$("#action").data("changeSet")) {
294+
244295
$("#action").data("changeSet", true);
245296
$("#action")[0].select_one.forEach(el => el.addEventListener('change', (function() {
246297
page.setup(app, $page, pathname);
247298
}).bind(null, false)));
299+
248300
page.button.addEventListener('click', async function() {
249301

250302
var message = null;
251303
var file = null;
252304

305+
switch (page.okpgp._$mode()) {
306+
case 'Decrypt and Verify':
307+
pageType = "dv";
308+
break;
309+
case 'Decrypt Only':
310+
pageType = "s";
311+
break;
312+
}
313+
314+
if (pageType)
315+
app.pages.state.replace({ pathname: pathname }, $("title").text(), "./" + pathname + "?type=" + pageType + "&recipients=" + page.urlinputbox.value);
316+
253317
switch (page.okpgp._$status()) {
254318
case 'pending_pin':
255319
break;
@@ -290,6 +354,10 @@ module.exports = {
290354
else {
291355
message = page.messagebox.value;
292356
}
357+
358+
console.log(page.urlinputbox.value);
359+
console.log(page.urlinputbox.value);
360+
293361
page.okpgp.startDecryption(page.urlinputbox.value, page.urlinputbox2.value, message, file, function(err, data) {
294362
if (page.messagebox && data)
295363
page.messagebox.value = data;
@@ -298,12 +366,16 @@ module.exports = {
298366
});
299367
break;
300368
}
369+
301370
}
302371
}, false);
372+
303373
}
374+
304375
},
305376
dispose: function(app, pathname) {
306377
//init = false;
378+
// terminal_applied = false;
307379
// console.log("disposed", pathname);
308380
}
309381
};
@@ -319,6 +391,7 @@ module.exports = {
319391
setup: $page.setup
320392
};
321393

394+
322395
register(null, {
323396
plugin_decrypt: {
324397
pagesList: pagesList

src/plugins/encrypt/encrypt-file.page.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ <h4>
55
</h4>
66
<fieldset>
77
<textarea placeholder="Your Keybase username, Protonmail email, or paste your public key..." rows="1" id="pgpkeyurl2"></textarea>
8-
<!--<textarea placeholder="Recipient's Keybase username, Protonmail email, or paste their public key..." rows="1" id="pgpkeyurl"></textarea>-->
98
<input type="text" data-provide="tokenizer" placeholder="Recipient's Keybase username, Protonmail email, or paste their public key..." rows="1" id="pgpkeyurl" />
109
<textarea placeholder="Enter a name to use for your encrypted files (optional)..." rows="1" id="filename"></textarea>
1110
<form name="fileform">

0 commit comments

Comments
 (0)