Merge pull request #677 from openpgpjs/keygen_date
Date parameter for generateKey
This commit is contained in:
commit
c9d837cf8a
|
@ -1103,6 +1103,7 @@ export function readArmored(armoredText) {
|
||||||
* @param {Boolean} [options.unlocked=false] The secret part of the generated key is unlocked
|
* @param {Boolean} [options.unlocked=false] The secret part of the generated key is unlocked
|
||||||
* @param {Number} [options.keyExpirationTime=0]
|
* @param {Number} [options.keyExpirationTime=0]
|
||||||
* The number of seconds after the key creation time that the key expires
|
* The number of seconds after the key creation time that the key expires
|
||||||
|
* @param {Date} date Override the creation date of the key and the key signatures
|
||||||
* @returns {Promise<module:key.Key>}
|
* @returns {Promise<module:key.Key>}
|
||||||
* @async
|
* @async
|
||||||
* @static
|
* @static
|
||||||
|
@ -1154,7 +1155,7 @@ export function generate(options) {
|
||||||
});
|
});
|
||||||
|
|
||||||
function generateSecretKey() {
|
function generateSecretKey() {
|
||||||
secretKeyPacket = new packet.SecretKey();
|
secretKeyPacket = new packet.SecretKey(options.date);
|
||||||
secretKeyPacket.packets = null;
|
secretKeyPacket.packets = null;
|
||||||
secretKeyPacket.algorithm = enums.read(enums.publicKey, options.keyType);
|
secretKeyPacket.algorithm = enums.read(enums.publicKey, options.keyType);
|
||||||
options.curve = options.curve === enums.curve.curve25519 ? enums.curve.ed25519 : options.curve;
|
options.curve = options.curve === enums.curve.curve25519 ? enums.curve.ed25519 : options.curve;
|
||||||
|
@ -1162,7 +1163,7 @@ export function generate(options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateSecretSubkey() {
|
function generateSecretSubkey() {
|
||||||
secretSubkeyPacket = new packet.SecretSubkey();
|
secretSubkeyPacket = new packet.SecretSubkey(options.date);
|
||||||
secretKeyPacket.packets = null;
|
secretKeyPacket.packets = null;
|
||||||
secretSubkeyPacket.algorithm = enums.read(enums.publicKey, options.subkeyType);
|
secretSubkeyPacket.algorithm = enums.read(enums.publicKey, options.subkeyType);
|
||||||
options.curve = options.curve === enums.curve.ed25519 ? enums.curve.curve25519 : options.curve;
|
options.curve = options.curve === enums.curve.ed25519 ? enums.curve.curve25519 : options.curve;
|
||||||
|
@ -1245,7 +1246,7 @@ async function wrapKeyObject(secretKeyPacket, secretSubkeyPacket, options) {
|
||||||
const dataToSign = {};
|
const dataToSign = {};
|
||||||
dataToSign.userid = userIdPacket;
|
dataToSign.userid = userIdPacket;
|
||||||
dataToSign.key = secretKeyPacket;
|
dataToSign.key = secretKeyPacket;
|
||||||
const signaturePacket = new packet.Signature();
|
const signaturePacket = new packet.Signature(options.date);
|
||||||
signaturePacket.signatureType = enums.signature.cert_generic;
|
signaturePacket.signatureType = enums.signature.cert_generic;
|
||||||
signaturePacket.publicKeyAlgorithm = options.keyType;
|
signaturePacket.publicKeyAlgorithm = options.keyType;
|
||||||
signaturePacket.hashAlgorithm = await getPreferredHashAlgo(secretKeyPacket);
|
signaturePacket.hashAlgorithm = await getPreferredHashAlgo(secretKeyPacket);
|
||||||
|
@ -1290,7 +1291,7 @@ async function wrapKeyObject(secretKeyPacket, secretSubkeyPacket, options) {
|
||||||
const dataToSign = {};
|
const dataToSign = {};
|
||||||
dataToSign.key = secretKeyPacket;
|
dataToSign.key = secretKeyPacket;
|
||||||
dataToSign.bind = secretSubkeyPacket;
|
dataToSign.bind = secretSubkeyPacket;
|
||||||
const subkeySignaturePacket = new packet.Signature();
|
const subkeySignaturePacket = new packet.Signature(options.date);
|
||||||
subkeySignaturePacket.signatureType = enums.signature.subkey_binding;
|
subkeySignaturePacket.signatureType = enums.signature.subkey_binding;
|
||||||
subkeySignaturePacket.publicKeyAlgorithm = options.keyType;
|
subkeySignaturePacket.publicKeyAlgorithm = options.keyType;
|
||||||
subkeySignaturePacket.hashAlgorithm = await getPreferredHashAlgo(secretSubkeyPacket);
|
subkeySignaturePacket.hashAlgorithm = await getPreferredHashAlgo(secretSubkeyPacket);
|
||||||
|
|
|
@ -104,6 +104,7 @@ export function destroyWorker() {
|
||||||
* brainpoolP256r1, brainpoolP384r1, or brainpoolP512r1.
|
* brainpoolP256r1, brainpoolP384r1, or brainpoolP512r1.
|
||||||
* @param {Boolean} unlocked (optional) If the returned secret part of the generated key is unlocked
|
* @param {Boolean} unlocked (optional) If the returned secret part of the generated key is unlocked
|
||||||
* @param {Number} keyExpirationTime (optional) The number of seconds after the key creation time that the key expires
|
* @param {Number} keyExpirationTime (optional) The number of seconds after the key creation time that the key expires
|
||||||
|
* @param {Date} date (optional) override the creation date of the key and the key signatures
|
||||||
* @returns {Promise<Object>} The generated key object in the form:
|
* @returns {Promise<Object>} The generated key object in the form:
|
||||||
* { key:Key, privateKeyArmored:String, publicKeyArmored:String }
|
* { key:Key, privateKeyArmored:String, publicKeyArmored:String }
|
||||||
* @async
|
* @async
|
||||||
|
@ -111,11 +112,11 @@ export function destroyWorker() {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function generateKey({
|
export function generateKey({
|
||||||
userIds=[], passphrase, numBits=2048, unlocked=false, keyExpirationTime=0, curve=""
|
userIds=[], passphrase, numBits=2048, unlocked=false, keyExpirationTime=0, curve="", date=new Date()
|
||||||
} = {}) {
|
} = {}) {
|
||||||
userIds = formatUserIds(userIds);
|
userIds = formatUserIds(userIds);
|
||||||
const options = {
|
const options = {
|
||||||
userIds, passphrase, numBits, unlocked, keyExpirationTime, curve
|
userIds, passphrase, numBits, unlocked, keyExpirationTime, curve, date
|
||||||
};
|
};
|
||||||
|
|
||||||
if (util.getWebCryptoAll() && numBits < 2048) {
|
if (util.getWebCryptoAll() && numBits < 2048) {
|
||||||
|
|
|
@ -42,7 +42,7 @@ import util from '../util';
|
||||||
* @memberof module:packet
|
* @memberof module:packet
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
function PublicKey() {
|
function PublicKey(date=new Date()) {
|
||||||
/**
|
/**
|
||||||
* Packet type
|
* Packet type
|
||||||
* @type {module:enums.packet}
|
* @type {module:enums.packet}
|
||||||
|
@ -57,7 +57,7 @@ function PublicKey() {
|
||||||
* Key creation date.
|
* Key creation date.
|
||||||
* @type {Date}
|
* @type {Date}
|
||||||
*/
|
*/
|
||||||
this.created = util.normalizeDate();
|
this.created = util.normalizeDate(date);
|
||||||
/**
|
/**
|
||||||
* Algorithm specific params
|
* Algorithm specific params
|
||||||
* @type {Array<Object>}
|
* @type {Array<Object>}
|
||||||
|
|
|
@ -39,8 +39,8 @@ import util from '../util';
|
||||||
* @constructor
|
* @constructor
|
||||||
* @extends module:packet.PublicKey
|
* @extends module:packet.PublicKey
|
||||||
*/
|
*/
|
||||||
function SecretKey() {
|
function SecretKey(date=new Date()) {
|
||||||
publicKey.call(this);
|
publicKey.call(this, date);
|
||||||
/**
|
/**
|
||||||
* Packet type
|
* Packet type
|
||||||
* @type {module:enums.packet}
|
* @type {module:enums.packet}
|
||||||
|
|
|
@ -30,8 +30,8 @@ import enums from '../enums';
|
||||||
* @constructor
|
* @constructor
|
||||||
* @extends module:packet.SecretKey
|
* @extends module:packet.SecretKey
|
||||||
*/
|
*/
|
||||||
function SecretSubkey() {
|
function SecretSubkey(date=new Date()) {
|
||||||
secretKey.call(this);
|
secretKey.call(this, date);
|
||||||
this.tag = enums.packet.secretSubkey;
|
this.tag = enums.packet.secretSubkey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1076,6 +1076,23 @@ describe('Key', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Generate key - setting date to the past', function() {
|
||||||
|
const past = new Date(0);
|
||||||
|
const opt = {
|
||||||
|
userIds: { name: 'Test User', email: 'text@example.com' },
|
||||||
|
passphrase: 'secret',
|
||||||
|
unlocked: true,
|
||||||
|
date: past
|
||||||
|
};
|
||||||
|
|
||||||
|
return openpgp.generateKey(opt).then(function(newKey) {
|
||||||
|
expect(newKey.key).to.exist;
|
||||||
|
expect(+newKey.key.primaryKey.created).to.equal(+past);
|
||||||
|
expect(+newKey.key.subKeys[0].subKey.created).to.equal(+past);
|
||||||
|
expect(+newKey.key.subKeys[0].bindingSignatures[0].created).to.equal(+past);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
|
||||||
it('Generate key - multi userid', function() {
|
it('Generate key - multi userid', function() {
|
||||||
const userId1 = 'test <a@b.com>';
|
const userId1 = 'test <a@b.com>';
|
||||||
const userId2 = 'test <b@c.com>';
|
const userId2 = 'test <b@c.com>';
|
||||||
|
|
|
@ -483,10 +483,12 @@ describe('OpenPGP.js public api tests', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should have default params set', function() {
|
it('should have default params set', function() {
|
||||||
|
const now = new Date();
|
||||||
const opt = {
|
const opt = {
|
||||||
userIds: { name: 'Test User', email: 'text@example.com' },
|
userIds: { name: 'Test User', email: 'text@example.com' },
|
||||||
passphrase: 'secret',
|
passphrase: 'secret',
|
||||||
unlocked: true
|
unlocked: true,
|
||||||
|
date: now
|
||||||
};
|
};
|
||||||
return openpgp.generateKey(opt).then(function(newKey) {
|
return openpgp.generateKey(opt).then(function(newKey) {
|
||||||
expect(keyGenStub.withArgs({
|
expect(keyGenStub.withArgs({
|
||||||
|
@ -495,7 +497,8 @@ describe('OpenPGP.js public api tests', function() {
|
||||||
numBits: 2048,
|
numBits: 2048,
|
||||||
unlocked: true,
|
unlocked: true,
|
||||||
keyExpirationTime: 0,
|
keyExpirationTime: 0,
|
||||||
curve: ""
|
curve: "",
|
||||||
|
date: now
|
||||||
}).calledOnce).to.be.true;
|
}).calledOnce).to.be.true;
|
||||||
expect(newKey.key).to.exist;
|
expect(newKey.key).to.exist;
|
||||||
expect(newKey.privateKeyArmored).to.exist;
|
expect(newKey.privateKeyArmored).to.exist;
|
||||||
|
@ -504,14 +507,17 @@ describe('OpenPGP.js public api tests', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work for no params', function() {
|
it('should work for no params', function() {
|
||||||
return openpgp.generateKey().then(function(newKey) {
|
const now = new Date();
|
||||||
|
|
||||||
|
return openpgp.generateKey({date: now}).then(function(newKey) {
|
||||||
expect(keyGenStub.withArgs({
|
expect(keyGenStub.withArgs({
|
||||||
userIds: [],
|
userIds: [],
|
||||||
passphrase: undefined,
|
passphrase: undefined,
|
||||||
numBits: 2048,
|
numBits: 2048,
|
||||||
unlocked: false,
|
unlocked: false,
|
||||||
keyExpirationTime: 0,
|
keyExpirationTime: 0,
|
||||||
curve: ""
|
curve: "",
|
||||||
|
date: now
|
||||||
}).calledOnce).to.be.true;
|
}).calledOnce).to.be.true;
|
||||||
expect(newKey.key).to.exist;
|
expect(newKey.key).to.exist;
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user