Skip to content

Commit 270e43a

Browse files
author
Niklas Femerstrand
committed
Moves to removeKey()
Moves to verifyBasicSignatures() Moves to getKeyID() Moves to getPerson() Moves and rewrites getAlgorithmString() Moves to exportArmored() and getKeyID() Moves to getKeyUserids() Finishes isolating OpenPGP.js functions to .crypto.js for niklasfemerstrand#73
1 parent 2c326c8 commit 270e43a

File tree

2 files changed

+113
-36
lines changed

2 files changed

+113
-36
lines changed

js/rc_openpgpjs.crypto.js

+99
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,34 @@ function getFingerprint(i, private, niceformat) {
201201
return fingerprint;
202202
}
203203

204+
function getKeyID(i, private) {
205+
if(typeof(private) == "undefined") {
206+
private = false;
207+
}
208+
209+
if(private == false) {
210+
key_id = "0x" + util.hexstrdump(openpgp.keyring.publicKeys[i].obj.getKeyId()).toUpperCase().substring(8);
211+
} else {
212+
key_id = "0x" + util.hexstrdump(openpgp.keyring.privateKeys[i].obj.getKeyId()).toUpperCase().substring(8);
213+
}
214+
215+
return key_id;
216+
}
217+
218+
function getPerson(i, j, private) {
219+
if(typeof(private) == "undefined") {
220+
private = false;
221+
}
222+
223+
if(private == false) {
224+
person = openpgp.keyring.publicKeys[i].obj.userIds[j].text;
225+
} else {
226+
person = openpgp.keyring.privateKeys[i].obj.userIds[j].text;
227+
}
228+
229+
return person;
230+
}
231+
204232
function getPubkeyForAddress(address) {
205233
var pubkey = openpgp.keyring.getPublicKeyForAddress(address);
206234
return pubkey;
@@ -269,3 +297,74 @@ function parsePrivkey(key) {
269297
return false;
270298
}
271299
}
300+
301+
function removeKey(i, private) {
302+
if(typeof(private) == "undefined") {
303+
private = false;
304+
}
305+
306+
if(private) {
307+
return openpgp.keyring.removePrivateKey(i);
308+
}
309+
310+
return openpgp.keyring.removePublicKey(i);
311+
}
312+
313+
function verifyBasicSignatures(i) {
314+
return (openpgp.keyring.publicKeys[i].obj.verifyBasicSignatures() ? true : false);
315+
}
316+
317+
/**
318+
* Extract the algorithm string from a key and return the algorithm type.
319+
*
320+
* @param i {Integer} Key id in keyring
321+
* @return {String} Algorithm type
322+
*/
323+
324+
function getAlgorithmString(i, private) {
325+
if(typeof(private) == "undefined") {
326+
private = false;
327+
}
328+
329+
if(private) {
330+
key = openpgp.keyring.privateKeys[i].obj;
331+
} else {
332+
key = openpgp.keyring.publicKeys[i].obj;
333+
}
334+
335+
if(typeof(key.publicKeyPacket) !== "undefined") {
336+
var result = key.publicKeyPacket.MPIs[0].mpiByteLength * 8 + "/";
337+
var sw = key.publicKeyPacket.publicKeyAlgorithm;
338+
} else {
339+
// For some reason publicKeyAlgorithm doesn't work directly on the privatekeyPacket, heh
340+
var result = (key.privateKeyPacket.publicKey.MPIs[0].mpiByteLength * 8 + "/");
341+
var sw = key.privateKeyPacket.publicKey.publicKeyAlgorithm;
342+
}
343+
344+
result += typeToStr(sw);
345+
return result;
346+
}
347+
348+
function exportArmored(i, private) {
349+
if(typeof(private) == "undefined") {
350+
private = false;
351+
}
352+
353+
if(private) {
354+
return openpgp.keyring.privateKeys[i].armored;
355+
} else {
356+
return openpgp.keyring.publicKeys[i].armored;
357+
}
358+
}
359+
360+
function getKeyUserids(i, private) {
361+
if(typeof(private) == "undefined") {
362+
private = false;
363+
}
364+
365+
if(private) {
366+
return openpgp.keyring.privateKeys[i].obj.userIds;
367+
} else {
368+
return openpgp.keyring.publicKeys[i].obj.userIds;
369+
}
370+
}

js/rc_openpgpjs.js

+14-36
Original file line numberDiff line numberDiff line change
@@ -505,9 +505,9 @@ if(window.rcmail) {
505505
} else {
506506
// Selected set as $("#openpgpjs_selected_id").val(), then get that value from set_passphrase
507507
for (var i = 0; i < getPrivkeyCount(); i++) {
508-
for (var j = 0; j < openpgp.keyring.privateKeys[i].obj.userIds.length; j++) {
508+
for (var j = 0; j < getKeyUserids(i, true).length; j++) {
509509
fingerprint = getFingerprint(i, true, false);
510-
person = escapeHtml(openpgp.keyring.privateKeys[i].obj.userIds[j].text);
510+
person = escapeHtml(getPerson(i, j, true));
511511
$("#openpgpjs_key_select_list").append("<div class=\"clickme\" onclick=\"select_key(" + i + ");\">" + fingerprint + " " + person + "</div>");
512512
}
513513
}
@@ -526,13 +526,13 @@ if(window.rcmail) {
526526
// fill key manager public key table
527527
$("#openpgpjs_pubkeys tbody").empty();
528528
for (var i = 0; i < getPubkeyCount(); i++) {
529-
var key_id = "0x" + util.hexstrdump(openpgp.keyring.publicKeys[i].obj.getKeyId()).toUpperCase().substring(8);
529+
var key_id = getKeyID(i);
530530
var fingerprint = getFingerprint(i);
531-
var person = escapeHtml(openpgp.keyring.publicKeys[i].obj.userIds[0].text);
532-
var length_alg = getAlgorithmString(openpgp.keyring.publicKeys[i].obj);
533-
var status = (openpgp.keyring.publicKeys[i].obj.verifyBasicSignatures() ? rcmail.gettext("valid", "rc_openpgpjs") : rcmail.gettext("invalid", "rc_openpgpjs"));
534-
var del = "<a href='#' onclick='if(confirm(\"" + rcmail.gettext('delete_pub', 'rc_openpgpjs') + "\")) { openpgp.keyring.removePublicKey(" + i + "); updateKeyManager(); }'>" + rcmail.gettext('delete', 'rc_openpgpjs') + "</a>";
535-
var exp = "<a href=\"data:asc," + encodeURIComponent(openpgp.keyring.publicKeys[i].armored) + "\" download=\"pubkey_" + util.hexstrdump(openpgp.keyring.publicKeys[i].obj.getKeyId()).toUpperCase().substring(8) + ".asc\">Export</a> ";
531+
var person = escapeHtml(getPerson(i, 0));
532+
var length_alg = getAlgorithmString(i);
533+
var status = (verifyBasicSignatures(i) ? rcmail.gettext("valid", "rc_openpgpjs") : rcmail.gettext("invalid", "rc_openpgpjs"));
534+
var del = "<a href='#' onclick='if(confirm(\"" + rcmail.gettext('delete_pub', 'rc_openpgpjs') + "\")) { removeKey(" + i + "); updateKeyManager(); }'>" + rcmail.gettext('delete', 'rc_openpgpjs') + "</a>";
535+
var exp = "<a href=\"data:asc," + encodeURIComponent(exportArmored(i)) + "\" download=\"pubkey_" + getKeyID(i) + ".asc\">Export</a> ";
536536

537537
var result = "<tr>" +
538538
"<td>" + key_id + "</td>" +
@@ -548,13 +548,13 @@ if(window.rcmail) {
548548
// fill key manager private key table
549549
$("#openpgpjs_privkeys tbody").empty();
550550
for (var i = 0; i < getPrivkeyCount(); i++) {
551-
for (var j = 0; j < openpgp.keyring.privateKeys[i].obj.userIds.length; j++) {
552-
var key_id = "0x" + util.hexstrdump(openpgp.keyring.privateKeys[i].obj.getKeyId()).toUpperCase().substring(8);
551+
for (var j = 0; j < getKeyUserids(i, true).length; j++) {
552+
var key_id = getKeyID(i, true);
553553
var fingerprint = getFingerprint(i, true);
554-
var person = escapeHtml(openpgp.keyring.privateKeys[i].obj.userIds[j].text);
555-
var length_alg = getAlgorithmString(openpgp.keyring.privateKeys[i].obj);
556-
var del = "<a href='#' onclick='if(confirm(\"" + rcmail.gettext('delete_priv', 'rc_openpgpjs') + "\")) { openpgp.keyring.removePrivateKey(" + i + "); updateKeyManager(); }'>" + rcmail.gettext('delete', 'rc_openpgpjs') + "</a>";
557-
var exp = "<a href=\"data:asc," + encodeURIComponent(openpgp.keyring.privateKeys[i].armored) + "\" download=\"privkey_" + util.hexstrdump(openpgp.keyring.privateKeys[i].obj.getKeyId()).toUpperCase().substring(8) + ".asc\">Export</a> ";
554+
var person = escapeHtml(getPerson(i, j, true));
555+
var length_alg = getAlgorithmString(i, true);
556+
var del = "<a href='#' onclick='if(confirm(\"" + rcmail.gettext('delete_priv', 'rc_openpgpjs') + "\")) { removeKey(" + i + ", true); updateKeyManager(); }'>" + rcmail.gettext('delete', 'rc_openpgpjs') + "</a>";
557+
var exp = "<a href=\"data:asc," + encodeURIComponent(exportArmored(i, true)) + "\" download=\"privkey_" + getKeyID(i, true) + ".asc\">Export</a> ";
558558

559559
var result = "<tr>" +
560560
"<td>" + key_id + "</td>" +
@@ -576,28 +576,6 @@ if(window.rcmail) {
576576
}
577577
}
578578

579-
/**
580-
* Extract the algorithm string from a key and
581-
* return the algorithm type.
582-
*
583-
* @param key {String} Key
584-
* @return {String} Algorithm type
585-
*/
586-
function getAlgorithmString(key) {
587-
if(typeof(key.publicKeyPacket) !== "undefined") {
588-
var result = key.publicKeyPacket.MPIs[0].mpiByteLength * 8 + "/";
589-
var sw = key.publicKeyPacket.publicKeyAlgorithm;
590-
} else {
591-
// For some reason publicKeyAlgorithm doesn't work directly on the privatekeyPacket, heh
592-
var result = (key.privateKeyPacket.publicKey.MPIs[0].mpiByteLength * 8 + "/");
593-
var sw = key.privateKeyPacket.publicKey.publicKeyAlgorithm;
594-
}
595-
596-
result += typeToStr(sw);
597-
598-
return result;
599-
}
600-
601579
/**
602580
* Converts an algorithm id (1/2/3/16/17) to the
603581
* corresponding algorithm type

0 commit comments

Comments
 (0)