Finish merging key validation
This commit is contained in:
parent
b9afd26912
commit
f6ee6e959e
|
@ -198,7 +198,7 @@ class SecretKeyPacket extends PublicKeyPacket {
|
||||||
}
|
}
|
||||||
arr.push(new Uint8Array(optionalFieldsArr));
|
arr.push(new Uint8Array(optionalFieldsArr));
|
||||||
|
|
||||||
if (!this.s2k || this.s2k.type !== 'gnu-dummy') {
|
if (!this.isDummy()) {
|
||||||
if (!this.s2k_usage) {
|
if (!this.s2k_usage) {
|
||||||
const cleartextParams = write_cleartext_params(this.params, this.algorithm);
|
const cleartextParams = write_cleartext_params(this.params, this.algorithm);
|
||||||
this.keyMaterial = util.concatUint8Array([
|
this.keyMaterial = util.concatUint8Array([
|
||||||
|
@ -395,7 +395,7 @@ class SecretKeyPacket extends PublicKeyPacket {
|
||||||
* Clear private key parameters
|
* Clear private key parameters
|
||||||
*/
|
*/
|
||||||
clearPrivateParams() {
|
clearPrivateParams() {
|
||||||
if (this.s2k && this.s2k.type === 'gnu-dummy') {
|
if (this.isDummy()) {
|
||||||
this.isEncrypted = true;
|
this.isEncrypted = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,5 +10,5 @@ module.exports = () => describe('Crypto', function () {
|
||||||
require('./eax.js')();
|
require('./eax.js')();
|
||||||
require('./ocb.js')();
|
require('./ocb.js')();
|
||||||
require('./rsa.js')();
|
require('./rsa.js')();
|
||||||
require('./validate.js');
|
require('./validate.js')();
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
const openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('../../dist/openpgp');
|
const openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('../..');
|
||||||
const chai = require('chai');
|
const chai = require('chai');
|
||||||
const BN = require('bn.js');
|
const BN = require('bn.js');
|
||||||
|
|
||||||
|
@ -74,314 +74,316 @@ vqBGKJzmO5q3cECw
|
||||||
=X9kJ
|
=X9kJ
|
||||||
-----END PGP PRIVATE KEY BLOCK-----`;
|
-----END PGP PRIVATE KEY BLOCK-----`;
|
||||||
|
|
||||||
describe('EdDSA parameter validation', function() {
|
module.exports = () => {
|
||||||
let keyParams;
|
describe('EdDSA parameter validation', function() {
|
||||||
before(async () => {
|
|
||||||
keyParams = await openpgp.crypto.generateParams(openpgp.enums.publicKey.eddsa, null, 'ed25519');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('EdDSA params should be valid', async function() {
|
|
||||||
const { oid, Q, seed } = openpgp.crypto.publicKey.elliptic.eddsa.parseParams(keyParams);
|
|
||||||
const valid = await openpgp.crypto.publicKey.elliptic.eddsa.validateParams(oid, Q, seed);
|
|
||||||
expect(valid).to.be.true;
|
|
||||||
});
|
|
||||||
|
|
||||||
it('detect invalid edDSA Q', async function() {
|
|
||||||
const { oid, Q, seed } = openpgp.crypto.publicKey.elliptic.eddsa.parseParams(keyParams);
|
|
||||||
|
|
||||||
|
|
||||||
Q[0]++;
|
|
||||||
let valid = await openpgp.crypto.publicKey.elliptic.eddsa.validateParams(oid, Q, seed);
|
|
||||||
expect(valid).to.be.false;
|
|
||||||
|
|
||||||
const infQ = new Uint8Array(Q.length);
|
|
||||||
valid = await openpgp.crypto.publicKey.elliptic.eddsa.validateParams(oid, infQ, seed);
|
|
||||||
expect(valid).to.be.false;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('ECC curve validation', function() {
|
|
||||||
it('EdDSA params are not valid for ECDH', async function() {
|
|
||||||
const keyParams = await openpgp.crypto.generateParams(
|
|
||||||
openpgp.enums.publicKey.eddsa,
|
|
||||||
null,
|
|
||||||
'ed25519'
|
|
||||||
);
|
|
||||||
const { oid, Q, seed } = openpgp.crypto.publicKey.elliptic.eddsa.parseParams(keyParams);
|
|
||||||
const valid = await openpgp.crypto.publicKey.elliptic.ecdh.validateParams(oid, Q, seed);
|
|
||||||
expect(valid).to.be.false;
|
|
||||||
});
|
|
||||||
|
|
||||||
it('EdDSA params are not valid for EcDSA', async function() {
|
|
||||||
const keyParams = await openpgp.crypto.generateParams(
|
|
||||||
openpgp.enums.publicKey.eddsa,
|
|
||||||
null,
|
|
||||||
'ed25519'
|
|
||||||
);
|
|
||||||
const { oid, Q, seed } = openpgp.crypto.publicKey.elliptic.eddsa.parseParams(keyParams);
|
|
||||||
const valid = await openpgp.crypto.publicKey.elliptic.ecdsa.validateParams(oid, Q, seed);
|
|
||||||
expect(valid).to.be.false;
|
|
||||||
});
|
|
||||||
|
|
||||||
it('x25519 params are not valid for EcDSA', async function() {
|
|
||||||
const keyParams = await openpgp.crypto.generateParams(
|
|
||||||
openpgp.enums.publicKey.ecdsa,
|
|
||||||
null,
|
|
||||||
'curve25519'
|
|
||||||
);
|
|
||||||
const { oid, Q, d } = openpgp.crypto.publicKey.elliptic.ecdsa.parseParams(keyParams);
|
|
||||||
const valid = await openpgp.crypto.publicKey.elliptic.ecdsa.validateParams(oid, Q, d);
|
|
||||||
expect(valid).to.be.false;
|
|
||||||
});
|
|
||||||
|
|
||||||
it('EcDSA params are not valid for EdDSA', async function() {
|
|
||||||
const keyParams = await openpgp.crypto.generateParams(
|
|
||||||
openpgp.enums.publicKey.ecdsa, null, 'p256'
|
|
||||||
);
|
|
||||||
const { oid, Q, d } = openpgp.crypto.publicKey.elliptic.ecdsa.parseParams(keyParams);
|
|
||||||
const valid = await openpgp.crypto.publicKey.elliptic.eddsa.validateParams(oid, Q, d);
|
|
||||||
expect(valid).to.be.false;
|
|
||||||
});
|
|
||||||
|
|
||||||
it('x25519 params are not valid for EdDSA', async function() {
|
|
||||||
const keyParams = await openpgp.crypto.generateParams(
|
|
||||||
openpgp.enums.publicKey.ecdsa, null, 'curve25519'
|
|
||||||
);
|
|
||||||
const { oid, Q, d } = openpgp.crypto.publicKey.elliptic.ecdsa.parseParams(keyParams);
|
|
||||||
const valid = await openpgp.crypto.publicKey.elliptic.eddsa.validateParams(oid, Q, d);
|
|
||||||
expect(valid).to.be.false;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
const curves = ['curve25519', 'p256', 'p384', 'p521', 'secp256k1', 'brainpoolP256r1', 'brainpoolP384r1', 'brainpoolP512r1'];
|
|
||||||
curves.forEach(curve => {
|
|
||||||
describe(`ECC ${curve} parameter validation`, () => {
|
|
||||||
let keyParams;
|
let keyParams;
|
||||||
before(async () => {
|
before(async () => {
|
||||||
// we generate also ecdh params as ecdsa ones since we do not need the kdf params
|
keyParams = await openpgp.crypto.generateParams(openpgp.enums.publicKey.eddsa, null, 'ed25519');
|
||||||
keyParams = await openpgp.crypto.generateParams(
|
|
||||||
openpgp.enums.publicKey.ecdsa, null, curve
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (curve !== 'curve25519') {
|
it('EdDSA params should be valid', async function() {
|
||||||
it(`EcDSA ${curve} params should be valid`, async function() {
|
const { oid, Q, seed } = openpgp.crypto.publicKey.elliptic.eddsa.parseParams(keyParams);
|
||||||
const { oid, Q, d } = openpgp.crypto.publicKey.elliptic.ecdsa.parseParams(keyParams);
|
const valid = await openpgp.crypto.publicKey.elliptic.eddsa.validateParams(oid, Q, seed);
|
||||||
const valid = await openpgp.crypto.publicKey.elliptic.ecdsa.validateParams(oid, Q, d);
|
|
||||||
expect(valid).to.be.true;
|
|
||||||
});
|
|
||||||
|
|
||||||
it('detect invalid EcDSA Q', async function() {
|
|
||||||
const { oid, Q, d } = openpgp.crypto.publicKey.elliptic.ecdsa.parseParams(keyParams);
|
|
||||||
|
|
||||||
Q[16]++;
|
|
||||||
let valid = await openpgp.crypto.publicKey.elliptic.ecdsa.validateParams(oid, Q, d);
|
|
||||||
expect(valid).to.be.false;
|
|
||||||
|
|
||||||
const infQ = new Uint8Array(Q.length);
|
|
||||||
valid = await openpgp.crypto.publicKey.elliptic.ecdsa.validateParams(oid, infQ, d);
|
|
||||||
expect(valid).to.be.false;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
it(`ECDH ${curve} params should be valid`, async function() {
|
|
||||||
const { oid, Q, d } = openpgp.crypto.publicKey.elliptic.ecdsa.parseParams(keyParams);
|
|
||||||
const valid = await openpgp.crypto.publicKey.elliptic.ecdh.validateParams(oid, Q, d);
|
|
||||||
expect(valid).to.be.true;
|
expect(valid).to.be.true;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('detect invalid ECDH Q', async function() {
|
it('detect invalid edDSA Q', async function() {
|
||||||
const { oid, Q, d } = openpgp.crypto.publicKey.elliptic.ecdsa.parseParams(keyParams);
|
const { oid, Q, seed } = openpgp.crypto.publicKey.elliptic.eddsa.parseParams(keyParams);
|
||||||
|
|
||||||
Q[16]++;
|
|
||||||
let valid = await openpgp.crypto.publicKey.elliptic.ecdh.validateParams(oid, Q, d);
|
Q[0]++;
|
||||||
|
let valid = await openpgp.crypto.publicKey.elliptic.eddsa.validateParams(oid, Q, seed);
|
||||||
expect(valid).to.be.false;
|
expect(valid).to.be.false;
|
||||||
|
|
||||||
const infQ = new Uint8Array(Q.length);
|
const infQ = new Uint8Array(Q.length);
|
||||||
valid = await openpgp.crypto.publicKey.elliptic.ecdh.validateParams(oid, infQ, d);
|
valid = await openpgp.crypto.publicKey.elliptic.eddsa.validateParams(oid, infQ, seed);
|
||||||
expect(valid).to.be.false;
|
expect(valid).to.be.false;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
describe('RSA parameter validation', function() {
|
describe('ECC curve validation', function() {
|
||||||
let keyParams;
|
it('EdDSA params are not valid for ECDH', async function() {
|
||||||
before(async () => {
|
const keyParams = await openpgp.crypto.generateParams(
|
||||||
keyParams = await openpgp.crypto.generateParams(openpgp.enums.publicKey.rsa_sign, 2048);
|
openpgp.enums.publicKey.eddsa,
|
||||||
|
null,
|
||||||
|
'ed25519'
|
||||||
|
);
|
||||||
|
const { oid, Q, seed } = openpgp.crypto.publicKey.elliptic.eddsa.parseParams(keyParams);
|
||||||
|
const valid = await openpgp.crypto.publicKey.elliptic.ecdh.validateParams(oid, Q, seed);
|
||||||
|
expect(valid).to.be.false;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('EdDSA params are not valid for EcDSA', async function() {
|
||||||
|
const keyParams = await openpgp.crypto.generateParams(
|
||||||
|
openpgp.enums.publicKey.eddsa,
|
||||||
|
null,
|
||||||
|
'ed25519'
|
||||||
|
);
|
||||||
|
const { oid, Q, seed } = openpgp.crypto.publicKey.elliptic.eddsa.parseParams(keyParams);
|
||||||
|
const valid = await openpgp.crypto.publicKey.elliptic.ecdsa.validateParams(oid, Q, seed);
|
||||||
|
expect(valid).to.be.false;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('x25519 params are not valid for EcDSA', async function() {
|
||||||
|
const keyParams = await openpgp.crypto.generateParams(
|
||||||
|
openpgp.enums.publicKey.ecdsa,
|
||||||
|
null,
|
||||||
|
'curve25519'
|
||||||
|
);
|
||||||
|
const { oid, Q, d } = openpgp.crypto.publicKey.elliptic.ecdsa.parseParams(keyParams);
|
||||||
|
const valid = await openpgp.crypto.publicKey.elliptic.ecdsa.validateParams(oid, Q, d);
|
||||||
|
expect(valid).to.be.false;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('EcDSA params are not valid for EdDSA', async function() {
|
||||||
|
const keyParams = await openpgp.crypto.generateParams(
|
||||||
|
openpgp.enums.publicKey.ecdsa, null, 'p256'
|
||||||
|
);
|
||||||
|
const { oid, Q, d } = openpgp.crypto.publicKey.elliptic.ecdsa.parseParams(keyParams);
|
||||||
|
const valid = await openpgp.crypto.publicKey.elliptic.eddsa.validateParams(oid, Q, d);
|
||||||
|
expect(valid).to.be.false;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('x25519 params are not valid for EdDSA', async function() {
|
||||||
|
const keyParams = await openpgp.crypto.generateParams(
|
||||||
|
openpgp.enums.publicKey.ecdsa, null, 'curve25519'
|
||||||
|
);
|
||||||
|
const { oid, Q, d } = openpgp.crypto.publicKey.elliptic.ecdsa.parseParams(keyParams);
|
||||||
|
const valid = await openpgp.crypto.publicKey.elliptic.eddsa.validateParams(oid, Q, d);
|
||||||
|
expect(valid).to.be.false;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('generated RSA params are valid', async function() {
|
|
||||||
const n = keyParams[0].toUint8Array();
|
const curves = ['curve25519', 'p256', 'p384', 'p521', 'secp256k1', 'brainpoolP256r1', 'brainpoolP384r1', 'brainpoolP512r1'];
|
||||||
const e = keyParams[1].toUint8Array();
|
curves.forEach(curve => {
|
||||||
const d = keyParams[2].toUint8Array();
|
describe(`ECC ${curve} parameter validation`, () => {
|
||||||
const p = keyParams[3].toUint8Array();
|
let keyParams;
|
||||||
const q = keyParams[4].toUint8Array();
|
before(async () => {
|
||||||
const u = keyParams[5].toUint8Array();
|
// we generate also ecdh params as ecdsa ones since we do not need the kdf params
|
||||||
const valid = await openpgp.crypto.publicKey.rsa.validateParams(n, e, d, p, q, u);
|
keyParams = await openpgp.crypto.generateParams(
|
||||||
expect(valid).to.be.true;
|
openpgp.enums.publicKey.ecdsa, null, curve
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (curve !== 'curve25519') {
|
||||||
|
it(`EcDSA ${curve} params should be valid`, async function() {
|
||||||
|
const { oid, Q, d } = openpgp.crypto.publicKey.elliptic.ecdsa.parseParams(keyParams);
|
||||||
|
const valid = await openpgp.crypto.publicKey.elliptic.ecdsa.validateParams(oid, Q, d);
|
||||||
|
expect(valid).to.be.true;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('detect invalid EcDSA Q', async function() {
|
||||||
|
const { oid, Q, d } = openpgp.crypto.publicKey.elliptic.ecdsa.parseParams(keyParams);
|
||||||
|
|
||||||
|
Q[16]++;
|
||||||
|
let valid = await openpgp.crypto.publicKey.elliptic.ecdsa.validateParams(oid, Q, d);
|
||||||
|
expect(valid).to.be.false;
|
||||||
|
|
||||||
|
const infQ = new Uint8Array(Q.length);
|
||||||
|
valid = await openpgp.crypto.publicKey.elliptic.ecdsa.validateParams(oid, infQ, d);
|
||||||
|
expect(valid).to.be.false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
it(`ECDH ${curve} params should be valid`, async function() {
|
||||||
|
const { oid, Q, d } = openpgp.crypto.publicKey.elliptic.ecdsa.parseParams(keyParams);
|
||||||
|
const valid = await openpgp.crypto.publicKey.elliptic.ecdh.validateParams(oid, Q, d);
|
||||||
|
expect(valid).to.be.true;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('detect invalid ECDH Q', async function() {
|
||||||
|
const { oid, Q, d } = openpgp.crypto.publicKey.elliptic.ecdsa.parseParams(keyParams);
|
||||||
|
|
||||||
|
Q[16]++;
|
||||||
|
let valid = await openpgp.crypto.publicKey.elliptic.ecdh.validateParams(oid, Q, d);
|
||||||
|
expect(valid).to.be.false;
|
||||||
|
|
||||||
|
const infQ = new Uint8Array(Q.length);
|
||||||
|
valid = await openpgp.crypto.publicKey.elliptic.ecdh.validateParams(oid, infQ, d);
|
||||||
|
expect(valid).to.be.false;
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('detect invalid RSA n', async function() {
|
describe('RSA parameter validation', function() {
|
||||||
const n = keyParams[0].toUint8Array();
|
let keyParams;
|
||||||
const e = keyParams[1].toUint8Array();
|
before(async () => {
|
||||||
const d = keyParams[2].toUint8Array();
|
keyParams = await openpgp.crypto.generateParams(openpgp.enums.publicKey.rsaSign, 2048);
|
||||||
const p = keyParams[3].toUint8Array();
|
});
|
||||||
const q = keyParams[4].toUint8Array();
|
|
||||||
const u = keyParams[5].toUint8Array();
|
|
||||||
|
|
||||||
n[0]++;
|
it('generated RSA params are valid', async function() {
|
||||||
const valid = await openpgp.crypto.publicKey.rsa.validateParams(n, e, d, p, q, u);
|
const n = keyParams[0].toUint8Array();
|
||||||
expect(valid).to.be.false;
|
const e = keyParams[1].toUint8Array();
|
||||||
|
const d = keyParams[2].toUint8Array();
|
||||||
|
const p = keyParams[3].toUint8Array();
|
||||||
|
const q = keyParams[4].toUint8Array();
|
||||||
|
const u = keyParams[5].toUint8Array();
|
||||||
|
const valid = await openpgp.crypto.publicKey.rsa.validateParams(n, e, d, p, q, u);
|
||||||
|
expect(valid).to.be.true;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('detect invalid RSA n', async function() {
|
||||||
|
const n = keyParams[0].toUint8Array();
|
||||||
|
const e = keyParams[1].toUint8Array();
|
||||||
|
const d = keyParams[2].toUint8Array();
|
||||||
|
const p = keyParams[3].toUint8Array();
|
||||||
|
const q = keyParams[4].toUint8Array();
|
||||||
|
const u = keyParams[5].toUint8Array();
|
||||||
|
|
||||||
|
n[0]++;
|
||||||
|
const valid = await openpgp.crypto.publicKey.rsa.validateParams(n, e, d, p, q, u);
|
||||||
|
expect(valid).to.be.false;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('detect invalid RSA e', async function() {
|
||||||
|
const n = keyParams[0].toUint8Array();
|
||||||
|
const e = keyParams[1].toUint8Array();
|
||||||
|
const d = keyParams[2].toUint8Array();
|
||||||
|
const p = keyParams[3].toUint8Array();
|
||||||
|
const q = keyParams[4].toUint8Array();
|
||||||
|
const u = keyParams[5].toUint8Array();
|
||||||
|
|
||||||
|
e[0]++;
|
||||||
|
const valid = await openpgp.crypto.publicKey.rsa.validateParams(n, e, d, p, q, u);
|
||||||
|
expect(valid).to.be.false;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('detect invalid RSA e', async function() {
|
describe('DSA parameter validation', function() {
|
||||||
const n = keyParams[0].toUint8Array();
|
let dsaKey;
|
||||||
const e = keyParams[1].toUint8Array();
|
before(async () => {
|
||||||
const d = keyParams[2].toUint8Array();
|
dsaKey = await openpgp.key.readArmored(armoredDSAKey);
|
||||||
const p = keyParams[3].toUint8Array();
|
});
|
||||||
const q = keyParams[4].toUint8Array();
|
|
||||||
const u = keyParams[5].toUint8Array();
|
|
||||||
|
|
||||||
e[0]++;
|
it('DSA params should be valid', async function() {
|
||||||
const valid = await openpgp.crypto.publicKey.rsa.validateParams(n, e, d, p, q, u);
|
const params = dsaKey.keyPacket.params;
|
||||||
expect(valid).to.be.false;
|
const p = params[0].toUint8Array();
|
||||||
});
|
const q = params[1].toUint8Array();
|
||||||
});
|
const g = params[2].toUint8Array();
|
||||||
|
const y = params[3].toUint8Array();
|
||||||
|
const x = params[4].toUint8Array();
|
||||||
|
const valid = await openpgp.crypto.publicKey.dsa.validateParams(p, q, g, y, x);
|
||||||
|
expect(valid).to.be.true;
|
||||||
|
});
|
||||||
|
|
||||||
describe('DSA parameter validation', function() {
|
it('detect invalid DSA p', async function() {
|
||||||
let dsaKey;
|
const params = dsaKey.keyPacket.params;
|
||||||
before(async () => {
|
const p = params[0].toUint8Array();
|
||||||
dsaKey = (await openpgp.key.readArmored(armoredDSAKey)).keys[0];
|
const q = params[1].toUint8Array();
|
||||||
|
const g = params[2].toUint8Array();
|
||||||
|
const y = params[3].toUint8Array();
|
||||||
|
const x = params[4].toUint8Array();
|
||||||
|
|
||||||
|
p[0]++;
|
||||||
|
const valid = await openpgp.crypto.publicKey.dsa.validateParams(p, q, g, y, x);
|
||||||
|
|
||||||
|
expect(valid).to.be.false;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('detect invalid DSA y', async function() {
|
||||||
|
const params = dsaKey.keyPacket.params;
|
||||||
|
const p = params[0].toUint8Array();
|
||||||
|
const q = params[1].toUint8Array();
|
||||||
|
const g = params[2].toUint8Array();
|
||||||
|
const y = params[3].toUint8Array();
|
||||||
|
const x = params[4].toUint8Array();
|
||||||
|
|
||||||
|
y[0]++;
|
||||||
|
const valid = await openpgp.crypto.publicKey.dsa.validateParams(p, q, g, y, x);
|
||||||
|
|
||||||
|
expect(valid).to.be.false;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('detect invalid DSA g', async function() {
|
||||||
|
const params = dsaKey.keyPacket.params;
|
||||||
|
const p = params[0].toUint8Array();
|
||||||
|
const q = params[1].toUint8Array();
|
||||||
|
const g = params[2].toUint8Array();
|
||||||
|
const y = params[3].toUint8Array();
|
||||||
|
const x = params[4].toUint8Array();
|
||||||
|
|
||||||
|
g[0]++;
|
||||||
|
let valid = await openpgp.crypto.publicKey.dsa.validateParams(p, q, g, y, x);
|
||||||
|
expect(valid).to.be.false;
|
||||||
|
|
||||||
|
const gOne = new Uint8Array([1]);
|
||||||
|
valid = await openpgp.crypto.publicKey.dsa.validateParams(p, q, gOne, y, x);
|
||||||
|
expect(valid).to.be.false;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('DSA params should be valid', async function() {
|
describe('ElGamal parameter validation', function() {
|
||||||
const params = dsaKey.keyPacket.params;
|
let egKey;
|
||||||
const p = params[0].toUint8Array();
|
before(async () => {
|
||||||
const q = params[1].toUint8Array();
|
egKey = (await openpgp.key.readArmored(armoredElGamalKey)).subKeys[0];
|
||||||
const g = params[2].toUint8Array();
|
});
|
||||||
const y = params[3].toUint8Array();
|
|
||||||
const x = params[4].toUint8Array();
|
it('params should be valid', async function() {
|
||||||
const valid = await openpgp.crypto.publicKey.dsa.validateParams(p, q, g, y, x);
|
const params = egKey.keyPacket.params;
|
||||||
expect(valid).to.be.true;
|
const p = params[0].toUint8Array();
|
||||||
|
const g = params[1].toUint8Array();
|
||||||
|
const y = params[2].toUint8Array();
|
||||||
|
const x = params[3].toUint8Array();
|
||||||
|
|
||||||
|
const valid = await openpgp.crypto.publicKey.elgamal.validateParams(p, g, y, x);
|
||||||
|
expect(valid).to.be.true;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('detect invalid p', async function() {
|
||||||
|
const params = egKey.keyPacket.params;
|
||||||
|
const p = params[0].toUint8Array();
|
||||||
|
const g = params[1].toUint8Array();
|
||||||
|
const y = params[2].toUint8Array();
|
||||||
|
const x = params[3].toUint8Array();
|
||||||
|
p[0]++;
|
||||||
|
const valid = await openpgp.crypto.publicKey.elgamal.validateParams(p, g, y, x);
|
||||||
|
|
||||||
|
expect(valid).to.be.false;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('detect invalid y', async function() {
|
||||||
|
const params = egKey.keyPacket.params;
|
||||||
|
const p = params[0].toUint8Array();
|
||||||
|
const g = params[1].toUint8Array();
|
||||||
|
const y = params[2].toUint8Array();
|
||||||
|
const x = params[3].toUint8Array();
|
||||||
|
|
||||||
|
y[0]++;
|
||||||
|
const valid = await openpgp.crypto.publicKey.elgamal.validateParams(p, g, y, x);
|
||||||
|
|
||||||
|
expect(valid).to.be.false;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('detect invalid g', async function() {
|
||||||
|
const params = egKey.keyPacket.params;
|
||||||
|
const p = params[0].toUint8Array();
|
||||||
|
const g = params[1].toUint8Array();
|
||||||
|
const y = params[2].toUint8Array();
|
||||||
|
const x = params[3].toUint8Array();
|
||||||
|
|
||||||
|
g[0]++;
|
||||||
|
let valid = await openpgp.crypto.publicKey.elgamal.validateParams(p, g, y, x);
|
||||||
|
expect(valid).to.be.false;
|
||||||
|
|
||||||
|
const gOne = new Uint8Array([1]);
|
||||||
|
valid = await openpgp.crypto.publicKey.elgamal.validateParams(p, gOne, y, x);
|
||||||
|
expect(valid).to.be.false;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('detect g with small order', async function() {
|
||||||
|
const params = egKey.keyPacket.params;
|
||||||
|
const p = params[0].toUint8Array();
|
||||||
|
const g = params[1].toUint8Array();
|
||||||
|
const y = params[2].toUint8Array();
|
||||||
|
const x = params[3].toUint8Array();
|
||||||
|
|
||||||
|
const pBN = new BN(p);
|
||||||
|
const gModP = new BN(g).toRed(new BN.red(pBN));
|
||||||
|
// g**(p-1)/2 has order 2
|
||||||
|
const gOrd2 = gModP.redPow(pBN.subn(1).shrn(1));
|
||||||
|
const valid = await openpgp.crypto.publicKey.elgamal.validateParams(p, gOrd2.toArrayLike(Uint8Array, 'be'), y, x);
|
||||||
|
expect(valid).to.be.false;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
};
|
||||||
it('detect invalid DSA p', async function() {
|
|
||||||
const params = dsaKey.keyPacket.params;
|
|
||||||
const p = params[0].toUint8Array();
|
|
||||||
const q = params[1].toUint8Array();
|
|
||||||
const g = params[2].toUint8Array();
|
|
||||||
const y = params[3].toUint8Array();
|
|
||||||
const x = params[4].toUint8Array();
|
|
||||||
|
|
||||||
p[0]++;
|
|
||||||
const valid = await openpgp.crypto.publicKey.dsa.validateParams(p, q, g, y, x);
|
|
||||||
|
|
||||||
expect(valid).to.be.false;
|
|
||||||
});
|
|
||||||
|
|
||||||
it('detect invalid DSA y', async function() {
|
|
||||||
const params = dsaKey.keyPacket.params;
|
|
||||||
const p = params[0].toUint8Array();
|
|
||||||
const q = params[1].toUint8Array();
|
|
||||||
const g = params[2].toUint8Array();
|
|
||||||
const y = params[3].toUint8Array();
|
|
||||||
const x = params[4].toUint8Array();
|
|
||||||
|
|
||||||
y[0]++;
|
|
||||||
const valid = await openpgp.crypto.publicKey.dsa.validateParams(p, q, g, y, x);
|
|
||||||
|
|
||||||
expect(valid).to.be.false;
|
|
||||||
});
|
|
||||||
|
|
||||||
it('detect invalid DSA g', async function() {
|
|
||||||
const params = dsaKey.keyPacket.params;
|
|
||||||
const p = params[0].toUint8Array();
|
|
||||||
const q = params[1].toUint8Array();
|
|
||||||
const g = params[2].toUint8Array();
|
|
||||||
const y = params[3].toUint8Array();
|
|
||||||
const x = params[4].toUint8Array();
|
|
||||||
|
|
||||||
g[0]++;
|
|
||||||
let valid = await openpgp.crypto.publicKey.dsa.validateParams(p, q, g, y, x);
|
|
||||||
expect(valid).to.be.false;
|
|
||||||
|
|
||||||
const gOne = new Uint8Array([1]);
|
|
||||||
valid = await openpgp.crypto.publicKey.dsa.validateParams(p, q, gOne, y, x);
|
|
||||||
expect(valid).to.be.false;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('ElGamal parameter validation', function() {
|
|
||||||
let egKey;
|
|
||||||
before(async () => {
|
|
||||||
egKey = (await openpgp.key.readArmored(armoredElGamalKey)).keys[0].subKeys[0];
|
|
||||||
});
|
|
||||||
|
|
||||||
it('params should be valid', async function() {
|
|
||||||
const params = egKey.keyPacket.params;
|
|
||||||
const p = params[0].toUint8Array();
|
|
||||||
const g = params[1].toUint8Array();
|
|
||||||
const y = params[2].toUint8Array();
|
|
||||||
const x = params[3].toUint8Array();
|
|
||||||
|
|
||||||
const valid = await openpgp.crypto.publicKey.elgamal.validateParams(p, g, y, x);
|
|
||||||
expect(valid).to.be.true;
|
|
||||||
});
|
|
||||||
|
|
||||||
it('detect invalid p', async function() {
|
|
||||||
const params = egKey.keyPacket.params;
|
|
||||||
const p = params[0].toUint8Array();
|
|
||||||
const g = params[1].toUint8Array();
|
|
||||||
const y = params[2].toUint8Array();
|
|
||||||
const x = params[3].toUint8Array();
|
|
||||||
p[0]++;
|
|
||||||
const valid = await openpgp.crypto.publicKey.elgamal.validateParams(p, g, y, x);
|
|
||||||
|
|
||||||
expect(valid).to.be.false;
|
|
||||||
});
|
|
||||||
|
|
||||||
it('detect invalid y', async function() {
|
|
||||||
const params = egKey.keyPacket.params;
|
|
||||||
const p = params[0].toUint8Array();
|
|
||||||
const g = params[1].toUint8Array();
|
|
||||||
const y = params[2].toUint8Array();
|
|
||||||
const x = params[3].toUint8Array();
|
|
||||||
|
|
||||||
y[0]++;
|
|
||||||
const valid = await openpgp.crypto.publicKey.elgamal.validateParams(p, g, y, x);
|
|
||||||
|
|
||||||
expect(valid).to.be.false;
|
|
||||||
});
|
|
||||||
|
|
||||||
it('detect invalid g', async function() {
|
|
||||||
const params = egKey.keyPacket.params;
|
|
||||||
const p = params[0].toUint8Array();
|
|
||||||
const g = params[1].toUint8Array();
|
|
||||||
const y = params[2].toUint8Array();
|
|
||||||
const x = params[3].toUint8Array();
|
|
||||||
|
|
||||||
g[0]++;
|
|
||||||
let valid = await openpgp.crypto.publicKey.elgamal.validateParams(p, g, y, x);
|
|
||||||
expect(valid).to.be.false;
|
|
||||||
|
|
||||||
const gOne = new Uint8Array([1]);
|
|
||||||
valid = await openpgp.crypto.publicKey.elgamal.validateParams(p, gOne, y, x);
|
|
||||||
expect(valid).to.be.false;
|
|
||||||
});
|
|
||||||
|
|
||||||
it('detect g with small order', async function() {
|
|
||||||
const params = egKey.keyPacket.params;
|
|
||||||
const p = params[0].toUint8Array();
|
|
||||||
const g = params[1].toUint8Array();
|
|
||||||
const y = params[2].toUint8Array();
|
|
||||||
const x = params[3].toUint8Array();
|
|
||||||
|
|
||||||
const pBN = new BN(p);
|
|
||||||
const gModP = new BN(g).toRed(new BN.red(pBN));
|
|
||||||
// g**(p-1)/2 has order 2
|
|
||||||
const gOrd2 = gModP.redPow(pBN.subn(1).shrn(1));
|
|
||||||
const valid = await openpgp.crypto.publicKey.elgamal.validateParams(p, gOrd2.toArrayLike(Uint8Array, 'be'), y, x);
|
|
||||||
expect(valid).to.be.false;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
|
@ -2861,7 +2861,7 @@ module.exports = () => describe('Key', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('clearPrivateParams() - detect that private key parameters were zeroed out', async function() {
|
it('clearPrivateParams() - detect that private key parameters were zeroed out', async function() {
|
||||||
const { keys: [key] } = await openpgp.key.readArmored(priv_key_rsa);
|
const key = await openpgp.key.readArmored(priv_key_rsa);
|
||||||
await key.decrypt('hello world');
|
await key.decrypt('hello world');
|
||||||
const signingKeyPacket = key.subKeys[0].keyPacket;
|
const signingKeyPacket = key.subKeys[0].keyPacket;
|
||||||
const params = signingKeyPacket.params.slice();
|
const params = signingKeyPacket.params.slice();
|
||||||
|
|
|
@ -740,7 +740,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
|
||||||
privateKeys: privateKey
|
privateKeys: privateKey
|
||||||
};
|
};
|
||||||
const encrypted = await openpgp.encrypt(encOpt);
|
const encrypted = await openpgp.encrypt(encOpt);
|
||||||
decOpt.message = await openpgp.message.readArmored(encrypted.data);
|
decOpt.message = await openpgp.message.readArmored(encrypted);
|
||||||
await expect(openpgp.decrypt(decOpt)).to.be.rejectedWith('Error decrypting message: Private key is not decrypted.');
|
await expect(openpgp.decrypt(decOpt)).to.be.rejectedWith('Error decrypting message: Private key is not decrypted.');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -842,12 +842,14 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
|
||||||
return openpgp.encryptSessionKey({
|
return openpgp.encryptSessionKey({
|
||||||
data: sk,
|
data: sk,
|
||||||
algorithm: 'aes128',
|
algorithm: 'aes128',
|
||||||
publicKeys: publicKey.keys
|
publicKeys: publicKey,
|
||||||
|
armor: false
|
||||||
}).then(async function(encrypted) {
|
}).then(async function(encrypted) {
|
||||||
const invalidPrivateKey = (await openpgp.key.readArmored(priv_key)).keys[0];
|
const message = await openpgp.message.read(encrypted);
|
||||||
|
const invalidPrivateKey = await openpgp.key.readArmored(priv_key);
|
||||||
invalidPrivateKey.subKeys[0].bindingSignatures = [];
|
invalidPrivateKey.subKeys[0].bindingSignatures = [];
|
||||||
return openpgp.decryptSessionKeys({
|
return openpgp.decryptSessionKeys({
|
||||||
message: encrypted.message,
|
message,
|
||||||
privateKeys: invalidPrivateKey
|
privateKeys: invalidPrivateKey
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
throw new Error('Should not decrypt with invalid key');
|
throw new Error('Should not decrypt with invalid key');
|
||||||
|
|
Loading…
Reference in New Issue
Block a user