Commit 3908b0ae authored by Cédric Moreau's avatar Cédric Moreau

Now records pubkey eligibility and interesting packets on pks/add

parent 8f92a65b
var openpgp = require('openpgp');
var base64 = require('./base64');
var md5 = require('./md5');
var PacketList = openpgp.packet.List;
module.exports = {
......@@ -104,7 +105,7 @@ function KeyHelper (packetList) {
var certifs = [];
if (primaryUser) {
(primaryUser.user.otherCertifications || []).forEach(function(oCert){
certifs.push(base64.encode(oCert.write()));
certifs.push(base64.encode(writePacket(oCert)));
// oCert.verify(key, { userid: primaryUser.user.userId, key: key }))) {
});
}
......@@ -139,4 +140,29 @@ function KeyHelper (packetList) {
}
return potentials;
};
this.getHashedSubkeyPackets = function (){
var subkeys = this.getBase64subkeys(); // Array of 2 packets lists (subkey + binding)
return this.getHashedPackets(subkeys);
};
this.getHashedCertifPackets = function (){
var certifs = this.getBase64primaryUserOtherCertifications(); // Array of 1 packet lists (signature)
return this.getHashedPackets(certifs);
};
this.getHashedPackets = function (encodedPacketListArray){
var hash = {};
encodedPacketListArray.forEach(function(encodedPacketList){
var md5ed = md5(encodedPacketList);
hash[md5ed] = encodedPacketList;
});
return hash;
};
function writePacket (packet) {
var list = new PacketList();
list.push(packet);
return list.write();
}
}
......@@ -9,6 +9,9 @@ var KeySchema = new Schema({
managed: { type: Boolean, default: false },
member: { type: Boolean, default: false },
kick: { type: Boolean, default: false },
eligible: { type: Boolean, default: false },
certifs: [String], // Array of md5 hashes of packets to integrate
subkeys: [String], // Array of md5 hashes of packets to integrate
created: { type: Date, default: Date.now },
updated: { type: Date, default: Date.now }
});
......@@ -18,6 +21,17 @@ KeySchema.pre('save', function (next) {
next();
});
KeySchema.statics.getTheOne = function (fingerprint, done) {
this.find({ fingerprint: fingerprint }, function (err, keys) {
if(keys.length < 1){
done('Key 0x' + fingerprint + ' not found.');
return;
}
var key = keys[0];
done(null, key);
});
};
KeySchema.statics.getToBeKicked = function(done){
var Key = this.model('Key');
Key.find({ kick: true }, done);
......
......@@ -238,15 +238,6 @@ PublicKeySchema.statics.persist = function (pubkey, done) {
var storedKey = jpgp().certificate(foundKeys[0].raw).key;
// Merges packets
storedKey.update(comingKey);
var kh = KHelper.fromPackets(storedKey.toPacketlist());
var potentials = kh.getPotentials();
potentials.forEach(function(encoded){
var md5ed = md5(encoded);
if (foundKeys[0].registered.indexOf(md5ed) == -1 && foundKeys[0].eligible.indexOf(md5ed) == -1) {
foundKeys[0].eligible.push(md5ed);
}
});
// Check for unknown packets
var mergedCert = jpgp().certificate(storedKey.armor());
var raw = unix2dos(storedKey.armor());
foundKeys[0].subkeys = mergedCert.subkeys;
......
......@@ -16,7 +16,8 @@ TrustedKeySchema.pre('save', function (next) {
});
TrustedKeySchema.statics.getTheOne = function (keyID, done) {
this.find({ keyID: keyID }, function (err, keys) {
var search = keyID.length == 40 ? keyID.substring(24) : keyID;
this.find({ keyID: search }, function (err, keys) {
if(keys.length < 1){
done('Trusted Key 0x' + keyID + ' not found.');
return;
......
var jpgp = require('../lib/jpgp');
var async = require('async');
var _ = require('underscore');
var merkle = require('merkle');
var vucoin = require('vucoin');
var logger = require('../lib/logger')('pubkey');
var jpgp = require('../lib/jpgp');
var async = require('async');
var _ = require('underscore');
var merkle = require('merkle');
var vucoin = require('vucoin');
var keyhelper = require('../lib/keyhelper');
var logger = require('../lib/logger')('pubkey');
module.exports.get = function (conn, conf, KeyService) {
return new PublicKeyService(conn, conf, KeyService);
......@@ -11,8 +12,9 @@ module.exports.get = function (conn, conf, KeyService) {
function PublicKeyService (conn, conf, KeyService) {
var PublicKey = conn.model('PublicKey');
var Key = conn.model('Key');
var PublicKey = conn.model('PublicKey');
var TrustedKey = conn.model('TrustedKey');
var Key = conn.model('Key');
var fifo = async.queue(function (task, callback) {
task(callback);
......@@ -55,6 +57,42 @@ function PublicKeyService (conn, conf, KeyService) {
next(err);
});
},
function (next) {
async.parallel({
pubkey: function(callback){
PublicKey.getTheOne(pubkey.fingerprint, callback);
},
trusted: function(callback){
TrustedKey.getTheOne(pubkey.fingerprint, function (err, trusted) {
if (err)
trusted = null;
callback(null, trusted);
});
},
key: function(callback){
Key.getTheOne(pubkey.fingerprint, callback);
},
}, next);
},
function (res, next){
var pubkey = res.pubkey;
var trusted = res.trusted;
var key = res.key;
var keyN = keyhelper.fromArmored(pubkey.raw);
var keyT = trusted == null ? null : keyhelper.fromEncodedPackets(trusted.packets);
// Compute new subkeys
var recordedSubKeys = _((keyT && keyT.getHashedSubkeyPackets()) || {}).keys();
var availableSubKeys = _(keyN.getHashedSubkeyPackets()).keys();
// Compute new certifications
var recordedCertifs = _((keyT && keyT.getHashedCertifPackets()) || {}).keys();
var availableCertifs = _(keyN.getHashedCertifPackets()).keys();
key.subkeys = _(availableSubKeys).without(availableSubKeys);
key.certifs = _(availableCertifs).without(availableCertifs);
key.eligible = keyN.hasValidUdid2();
key.save(function (err) {
next(err);
});
},
], next);
},
function (next) {
......
......@@ -30,6 +30,21 @@ var catPubkeyPackets = "" +
"hMUN3UJwGzk7HdO7wo0F3e5onOinit7RTpg/tAZX+r3VIj8TzZnl4QpCS15A\n" +
"9r9tAcdC0An1ji4sVQ==\n";
describe('A packetlist of one element', function(){
var packets = new openpgp.packet.List();
packets.read(base64.decode(catPubkeyPackets));
var oneElementList = packets.filterByTag(openpgp.enums.packet.publicKey);
it('should have one element', function(){
assert.equal(oneElementList.length, 1);
});
it('should NOT be the same as the element', function(){
assert.notEqual(base64.encode(oneElementList.write()), base64.encode(oneElementList[0].write()));
});
});
describe('Reading key packets:', function(){
var packets = new openpgp.packet.List();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment