Working towards key generation, started cleanup of config, changing

armor api slightly to pass in whether or not to show version string, not
entirely happy with current solution.
This commit is contained in:
seancolyer
2013-10-14 22:52:10 -04:00
parent 42f26e543e
commit 52cf3eced8
9 changed files with 371 additions and 271 deletions

View File

@@ -15,6 +15,8 @@
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
var enums = require('../enums.js');
/**
*
* This object contains configuration values and implements
@@ -27,63 +29,32 @@
* @class
* @classdesc Implementation of the GPG4Browsers config object
*/
function openpgp_config() {
var config = function() {
/**
* The variable with the actual configuration
* @property {Integer} prefer_hash_algorithm
* @property {Integer} encryption_cipher
* @property {Integer} compression
* @property {Boolean} show_version
* @property {Boolean} show_comment
* @property {Boolean} integrity_protect
* @property {Integer} composition_behavior
* @property {String} keyserver
*/
this.config = null;
/**
* The default config object which is used if no
* configuration was in place
*/
this.default_config = {
prefer_hash_algorithm: 8,
encryption_cipher: 9,
compression: 1,
show_version: true,
show_comment: true,
integrity_protect: true,
composition_behavior: 0,
keyserver: "keyserver.linux.it" // "pgp.mit.edu:11371"
};
this.prefer_hash_algorithm = enums.hash.sha256;
this.encryption_cipher = enums.symmetric.aes256;
this.compression = enums.compression.zip;
this.show_version = true;
this.show_comment = true;
this.integrity_protect = true;
this.keyserver = "keyserver.linux.it"; // "pgp.mit.edu:11371"
this.versionstring = "OpenPGP.js VERSION";
this.commentstring = "http://openpgpjs.org";
/**
* Reads the config out of the HTML5 local storage
* and initializes the object config.
* if config is null the default config will be used
*/
function read() {
var cf = JSON.parse(window.localStorage.getItem("config"));
if (cf == null) {
this.config = this.default_config;
this.write();
} else
this.config = cf;
}
/**
* If enabled, debug messages will be printed
*/
this.debug = false;
/**
* Writes the config to HTML5 local storage
*/
function write() {
window.localStorage.setItem("config", JSON.stringify(this.config));
}
};
this.read = read;
this.write = write;
}
module.exports = new config();

View File

@@ -0,0 +1,28 @@
function config_localStorage() {
/**
* Reads the config out of the HTML5 local storage
* and initializes the object config.
* if config is null the default config will be used
*/
function read() {
var cf = JSON.parse(window.localStorage.getItem("config"));
if (cf === null) {
this.config = this.default_config;
this.write();
} else
this.config = cf;
}
/**
* Writes the config to HTML5 local storage
*/
function write() {
window.localStorage.setItem("config", JSON.stringify(this.config));
}
this.read = read;
this.write = write;
}
module.exports = config_localStorage;

5
src/config/package.json Normal file
View File

@@ -0,0 +1,5 @@
{
"name": "openpgp-config",
"version": "0.0.1",
"main": "./config.js"
}

View File

@@ -80,13 +80,13 @@ function get_type(text) {
* @version 2011-12-16
* @returns {String} The header information
*/
function armor_addheader() {
function armor_addheader(options) {
var result = "";
if (openpgp.config.config.show_version) {
result += "Version: " + openpgp.config.versionstring + '\r\n';
if (options.show_version) {
result += "Version: " + options.versionstring + '\r\n';
}
if (openpgp.config.config.show_comment) {
result += "Comment: " + openpgp.config.commentstring + '\r\n';
if (options.show_comment) {
result += "Comment: " + options.commentstring + '\r\n';
}
result += '\r\n';
return result;
@@ -262,19 +262,19 @@ function dearmor(text) {
* @param {Integer} parttotal
* @returns {String} Armored text
*/
function armor(messagetype, data, partindex, parttotal) {
function armor(messagetype, data, options, partindex, parttotal) {
var result = "";
switch (messagetype) {
case 0:
result += "-----BEGIN PGP MESSAGE, PART " + partindex + "/" + parttotal + "-----\r\n";
result += armor_addheader();
result += armor_addheader(options);
result += base64.encode(data);
result += "\r\n=" + getCheckSum(data) + "\r\n";
result += "-----END PGP MESSAGE, PART " + partindex + "/" + parttotal + "-----\r\n";
break;
case 1:
result += "-----BEGIN PGP MESSAGE, PART " + partindex + "-----\r\n";
result += armor_addheader();
result += armor_addheader(options);
result += base64.encode(data);
result += "\r\n=" + getCheckSum(data) + "\r\n";
result += "-----END PGP MESSAGE, PART " + partindex + "-----\r\n";
@@ -283,28 +283,28 @@ function armor(messagetype, data, partindex, parttotal) {
result += "\r\n-----BEGIN PGP SIGNED MESSAGE-----\r\nHash: " + data.hash + "\r\n\r\n";
result += data.text.replace(/\n-/g, "\n- -");
result += "\r\n-----BEGIN PGP SIGNATURE-----\r\n";
result += armor_addheader();
result += armor_addheader(options);
result += base64.encode(data.openpgp);
result += "\r\n=" + getCheckSum(data.openpgp) + "\r\n";
result += "-----END PGP SIGNATURE-----\r\n";
break;
case 3:
result += "-----BEGIN PGP MESSAGE-----\r\n";
result += armor_addheader();
result += armor_addheader(options);
result += base64.encode(data);
result += "\r\n=" + getCheckSum(data) + "\r\n";
result += "-----END PGP MESSAGE-----\r\n";
break;
case 4:
result += "-----BEGIN PGP PUBLIC KEY BLOCK-----\r\n";
result += armor_addheader();
result += armor_addheader(options);
result += base64.encode(data);
result += "\r\n=" + getCheckSum(data) + "\r\n";
result += "-----END PGP PUBLIC KEY BLOCK-----\r\n\r\n";
break;
case 5:
result += "-----BEGIN PGP PRIVATE KEY BLOCK-----\r\n";
result += armor_addheader();
result += armor_addheader(options);
result += base64.encode(data);
result += "\r\n=" + getCheckSum(data) + "\r\n";
result += "-----END PGP PRIVATE KEY BLOCK-----\r\n";

View File

@@ -8,6 +8,7 @@ module.exports.s2k = require('./type/s2k.js');
module.exports.keyid = require('./type/keyid.js');
module.exports.armor = require('./encoding/armor.js');
module.exports.enums = require('./enums.js');
module.exports.config = require('./config');
for (var i in crypto)
module.exports[i] = crypto[i];

View File

@@ -36,18 +36,6 @@ var enums = require('./enums.js');
function _openpgp() {
this.tostring = "";
/**
* initializes the library:
* - reading the keyring from local storage
* - reading the config from local storage
*/
function init() {
this.config = new openpgp_config();
this.config.read();
this.keyring = new openpgp_keyring();
this.keyring.init();
}
/**
* reads message packets out of an OpenPGP armored text and
* returns an array of message objects
@@ -112,15 +100,38 @@ function _openpgp() {
var userIdPacket = new packet.userid();
userIdPacket.read(userId);
var dataToSign = {};
dataToSign.userid = userIdPacket;
dataToSign.key = secretKeyPacket;
var signaturePacket = new packet.signature();
signaturePacket.signatureType = enums.signature.cert_casual;
signaturePacket.publicKeyAlgorithm = keyType;
//TODO we should load preferred hash from config, or as input to this function
signaturePacket.hashAlgorithm = enums.hash.sha256;
signaturePacket.sign(secretKeyPacket, dataToSign);
var secretSubkeyPacket = new packet.secret_subkey();
var overallSignaturePacket = new packet.signature();
secretSubkeyPacket.algorithm = enums.read(enums.publicKey, keyType);
secretSubkeyPacket.generate(numBits);
secretSubkeyPacket.encrypt(passphrase);
dataToSign = {};
dataToSign.key = secretKeyPacket;
dataToSign.bind = secretSubkeyPacket;
var subkeySignaturePacket = new packet.signature();
subkeySignaturePacket.signatureType = enums.signature.subkey_binding;
subkeySignaturePacket.publicKeyAlgorithm = keyType;
//TODO we should load preferred hash from config, or as input to this function
subkeySignaturePacket.hashAlgorithm = enums.hash.sha256;
subkeySignaturePacket.sign(secretSubkeyPacket, dataToSign);
packetlist.push(secretKeyPacket);
packetlist.push(userIdPacket);
packetlist.push(signaturePacket);
packetlist.push(secretSubkeyPacket);
packetlist.push(overallSignaturePacket);
packetlist.push(subkeySignaturePacket);
var armored = armor.encode(5, packetlist.write(), this.config);
}
/**
@@ -308,7 +319,6 @@ function _openpgp() {
this.write_encrypted_message = write_encrypted_message;
this.readArmoredPackets = readArmoredPackets;
this.readDearmoredPackets = readDearmoredPackets;
this.init = init;
}
module.exports = new _openpgp();