First draft of encrypt message function
This commit is contained in:
parent
8baaa7f511
commit
646e370df7
File diff suppressed because one or more lines are too long
54
src/key.js
54
src/key.js
|
@ -15,6 +15,10 @@
|
||||||
// License along with this library; if not, write to the Free Software
|
// License along with this library; if not, write to the Free Software
|
||||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
|
var packet = require('./packet');
|
||||||
|
var enums = require('./enums.js');
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class
|
* @class
|
||||||
* @classdesc Class that represents an OpenPGP key. Must contain a master key.
|
* @classdesc Class that represents an OpenPGP key. Must contain a master key.
|
||||||
|
@ -22,15 +26,16 @@
|
||||||
* user ids, user attributes.
|
* user ids, user attributes.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function openpgp_key() {
|
module.exports = function key() {
|
||||||
this.packets = new openpgp_packetlist();
|
|
||||||
|
|
||||||
/** Returns the master key (secret or public)
|
this.packets = new packet.list();
|
||||||
|
|
||||||
|
/** Returns the primary key (secret or public)
|
||||||
* @returns {openpgp_packet_secret_key|openpgp_packet_public_key|null} */
|
* @returns {openpgp_packet_secret_key|openpgp_packet_public_key|null} */
|
||||||
this.getKey = function() {
|
this.getKey = function() {
|
||||||
for (var i = 0; i < this.packets.length; i++)
|
for (var i = 0; i < this.packets.length; i++)
|
||||||
if (this.packets[i].tag == openpgp_packets.tags.public_key ||
|
if (this.packets[i].tag == enums.packet.public_key ||
|
||||||
this.packets[i].tag == openpgp_packets.tags.secret_key)
|
this.packets[i].tag == enums.packet.secret_key)
|
||||||
return this.packets[i];
|
return this.packets[i];
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
@ -43,8 +48,8 @@ function openpgp_key() {
|
||||||
var subkeys = [];
|
var subkeys = [];
|
||||||
|
|
||||||
for (var i = 0; i < this.packets.length; i++)
|
for (var i = 0; i < this.packets.length; i++)
|
||||||
if (this.packets[i].tag == openpgp_packet.tags.public_subkey ||
|
if (this.packets[i].tag == enums.packet.public_subkey ||
|
||||||
this.packets[i].tag == openpgp_packet.tags.secret_subkey)
|
this.packets[i].tag == enums.packet.secret_subkey)
|
||||||
subkeys.push(this.packets[i]);
|
subkeys.push(this.packets[i]);
|
||||||
|
|
||||||
return subkeys;
|
return subkeys;
|
||||||
|
@ -84,15 +89,44 @@ function openpgp_key() {
|
||||||
//TODO implement: https://tools.ietf.org/html/rfc4880#section-5.2.3.8
|
//TODO implement: https://tools.ietf.org/html/rfc4880#section-5.2.3.8
|
||||||
//separate private key preference from digest preferences
|
//separate private key preference from digest preferences
|
||||||
return openpgp.config.config.prefer_hash_algorithm;
|
return openpgp.config.config.prefer_hash_algorithm;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds an encryption key for this key
|
||||||
|
* @returns null if no encryption key has been found
|
||||||
|
*/
|
||||||
|
this.getEncryptionKey = function() {
|
||||||
|
// V4: by convention subkeys are prefered for encryption service
|
||||||
|
// V3: keys MUST NOT have subkeys
|
||||||
|
var isValidSignKey = function(key) {
|
||||||
|
return key.algorithm != enums.read(enums.publicKey, enums.publicKey.dsa)
|
||||||
|
&& key.algorithm != enums.read(enums.publicKey, enums.publicKey.rsa_sign)
|
||||||
|
//TODO verify key
|
||||||
|
//&& keys.verifyKey()
|
||||||
|
}
|
||||||
|
var subKeys = this.getSubkeys();
|
||||||
|
|
||||||
|
for (var j = 0; j < subKeys.length; j++) {
|
||||||
|
if (isValidSignKey(subKeys[j])) {
|
||||||
|
return subKeys[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// if no valid subkey for encryption, use primary key
|
||||||
|
var primaryKey = this.getKey();
|
||||||
|
if (isValidSignKey(primaryKey)) {
|
||||||
|
return primaryKey;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
this.decrypt = function(passphrase) {
|
this.decrypt = function(passphrase) {
|
||||||
var keys = this.getAllKeys();
|
var keys = this.getAllKeys();
|
||||||
|
|
||||||
for (var i in keys)
|
for (var i in keys)
|
||||||
if (keys[i].tag == openpgp_packet.tags.secret_subkey ||
|
if (keys[i].tag == enums.packet.secret_subkey ||
|
||||||
keys[i].tag == openpgp_packet.tags.secret_key)
|
keys[i].tag == enums.packet.secret_key)
|
||||||
|
|
||||||
keys[i].decrypt(passphrase);
|
keys[i].decrypt(passphrase);
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,9 @@ var armor = require('./encoding/armor.js');
|
||||||
var packet = require('./packet');
|
var packet = require('./packet');
|
||||||
var util = require('./util');
|
var util = require('./util');
|
||||||
var enums = require('./enums.js');
|
var enums = require('./enums.js');
|
||||||
|
var crypto = require('./crypto');
|
||||||
|
var key = require('./key.js');
|
||||||
|
var config = require('./config');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GPG4Browsers Core interface. A single instance is hold
|
* GPG4Browsers Core interface. A single instance is hold
|
||||||
|
@ -64,7 +67,45 @@ function _openpgp() {
|
||||||
return packetList;
|
return packetList;
|
||||||
}
|
}
|
||||||
|
|
||||||
function encryptMessage(publicKeyPacketlist, message) {
|
function encryptMessage(publicKeys, message) {
|
||||||
|
|
||||||
|
var packetList = new packet.list();
|
||||||
|
|
||||||
|
var literalDataPacket = new packet.literal();
|
||||||
|
literalDataPacket.set(message, 'utf8');
|
||||||
|
|
||||||
|
//TODO get preferred algo from signature
|
||||||
|
var sessionKey = crypto.generateSessionKey(enums.read(enums.symmetric, config.encryption_cipher));
|
||||||
|
|
||||||
|
publicKeys.forEach(function(publicKeyPacketlist) {
|
||||||
|
var pubKey = new key();
|
||||||
|
pubKey.packets = publicKeyPacketlist;
|
||||||
|
var encryptionKey = pubKey.getEncryptionKey();
|
||||||
|
if (encryptionKey) {
|
||||||
|
var pkESKeyPacket = new packet.public_key_encrypted_session_key();
|
||||||
|
pkESKeyPacket.publicKeyId = encryptionKey.getKeyId();
|
||||||
|
pkESKeyPacket.publicKeyAlgorithm = encryptionKey.algorithm;
|
||||||
|
pkESKeyPacket.sessionKey = sessionKey;
|
||||||
|
//TODO get preferred algo from signature
|
||||||
|
pkESKeyPacket.sessionKeyAlgorithm = enums.read(enums.symmetric, config.encryption_cipher);
|
||||||
|
pkESKeyPacket.encrypt(encryptionKey);
|
||||||
|
packetList.push(pkESKeyPacket);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
var symEncryptedPacket;
|
||||||
|
if (config.integrity_protect) {
|
||||||
|
symEncryptedPacket = new packet.sym_encrypted_integrity_protected();
|
||||||
|
} else {
|
||||||
|
symEncryptedPacket = new packet.symmetrically_encrypted();
|
||||||
|
}
|
||||||
|
symEncryptedPacket.packets = literalDataPacket;
|
||||||
|
//TODO get preferred algo from signature
|
||||||
|
symEncryptedPacket.encrypt(enums.read(enums.symmetric, config.encryption_cipher), sessionKey);
|
||||||
|
packetList.push(symEncryptedPacket);
|
||||||
|
|
||||||
|
var armored = armor.encode(3, packetList.write(), config);
|
||||||
|
return armored;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -318,7 +359,7 @@ function _openpgp() {
|
||||||
this.generateKeyPair = generateKeyPair;
|
this.generateKeyPair = generateKeyPair;
|
||||||
this.write_signed_message = write_signed_message;
|
this.write_signed_message = write_signed_message;
|
||||||
this.write_signed_and_encrypted_message = write_signed_and_encrypted_message;
|
this.write_signed_and_encrypted_message = write_signed_and_encrypted_message;
|
||||||
this.write_encrypted_message = write_encrypted_message;
|
this.encryptMessage = encryptMessage;
|
||||||
this.readArmoredPackets = readArmoredPackets;
|
this.readArmoredPackets = readArmoredPackets;
|
||||||
this.readDearmoredPackets = readDearmoredPackets;
|
this.readDearmoredPackets = readDearmoredPackets;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
var util = require('../util'),
|
var util = require('../util'),
|
||||||
type_mpi = require('../type/mpi.js'),
|
type_mpi = require('../type/mpi.js'),
|
||||||
|
type_keyid = require('../type/keyid.js'),
|
||||||
enums = require('../enums.js'),
|
enums = require('../enums.js'),
|
||||||
crypto = require('../crypto');
|
crypto = require('../crypto');
|
||||||
|
|
||||||
|
@ -122,7 +123,9 @@ module.exports = function packet_public_key() {
|
||||||
* @return {String} A 8 byte key id
|
* @return {String} A 8 byte key id
|
||||||
*/
|
*/
|
||||||
this.getKeyId = function() {
|
this.getKeyId = function() {
|
||||||
return this.getFingerprint().substr(12, 8);
|
var keyid = new type_keyid();
|
||||||
|
keyid.read(this.getFingerprint().substr(12, 8));
|
||||||
|
return keyid;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue
Block a user