test: check curve algorithm is supported

parallel/test-crypto-dh.js assumes particular curve algorithms
(e.g. Oakley-EC2N-3) are supported, though this may not necessarily be
the case if Node.js was built with a system version of OpenSSL.

PR-URL: https://github.com/nodejs/node/pull/12265
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
Karl Cheng 2017-04-07 11:59:26 +10:00 committed by Anna Henningsen
parent b2a12ee782
commit 2555780aa6
No known key found for this signature in database
GPG Key ID: D8B9F5AEAE84E4CF

View File

@ -185,7 +185,17 @@ const bad_dh = crypto.createDiffieHellman(p, 'hex');
assert.strictEqual(bad_dh.verifyError, DH_NOT_SUITABLE_GENERATOR); assert.strictEqual(bad_dh.verifyError, DH_NOT_SUITABLE_GENERATOR);
const availableCurves = new Set(crypto.getCurves());
// Oakley curves do not clean up ERR stack, it was causing unexpected failure
// when accessing other OpenSSL APIs afterwards.
if (availableCurves.has('Oakley-EC2N-3')) {
crypto.createECDH('Oakley-EC2N-3');
crypto.createHash('sha256');
}
// Test ECDH // Test ECDH
if (availableCurves.has('prime256v1') && availableCurves.has('secp256k1')) {
const ecdh1 = crypto.createECDH('prime256v1'); const ecdh1 = crypto.createECDH('prime256v1');
const ecdh2 = crypto.createECDH('prime256v1'); const ecdh2 = crypto.createECDH('prime256v1');
key1 = ecdh1.generateKeys(); key1 = ecdh1.generateKeys();
@ -195,11 +205,6 @@ secret2 = ecdh2.computeSecret(key1, 'latin1', 'buffer');
assert.strictEqual(secret1, secret2.toString('base64')); assert.strictEqual(secret1, secret2.toString('base64'));
// Oakley curves do not clean up ERR stack, it was causing unexpected failure
// when accessing other OpenSSL APIs afterwards.
crypto.createECDH('Oakley-EC2N-3');
crypto.createHash('sha256');
// Point formats // Point formats
assert.strictEqual(ecdh1.getPublicKey('buffer', 'uncompressed')[0], 4); assert.strictEqual(ecdh1.getPublicKey('buffer', 'uncompressed')[0], 4);
let firstByte = ecdh1.getPublicKey('buffer', 'compressed')[0]; let firstByte = ecdh1.getPublicKey('buffer', 'compressed')[0];
@ -251,10 +256,12 @@ const cafebabePubPtUnComp =
'2e02c7f93d13dc2732b760ca377a5897b9dd41a1c1b29dc0442fdce6d0a04d1d'; '2e02c7f93d13dc2732b760ca377a5897b9dd41a1c1b29dc0442fdce6d0a04d1d';
ecdh5.setPrivateKey(cafebabeKey, 'hex'); ecdh5.setPrivateKey(cafebabeKey, 'hex');
assert.strictEqual(ecdh5.getPrivateKey('hex'), cafebabeKey); assert.strictEqual(ecdh5.getPrivateKey('hex'), cafebabeKey);
// Show that the public point (key) is generated while setting the private key. // Show that the public point (key) is generated while setting the
// private key.
assert.strictEqual(ecdh5.getPublicKey('hex'), cafebabePubPtUnComp); assert.strictEqual(ecdh5.getPublicKey('hex'), cafebabePubPtUnComp);
// Compressed and uncompressed public points/keys for other party's private key // Compressed and uncompressed public points/keys for other party's
// private key.
// 0xDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF // 0xDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF
const peerPubPtComp = const peerPubPtComp =
'02c6b754b20826eb925e052ee2c25285b162b51fdca732bcf67e39d647fb6830ae'; '02c6b754b20826eb925e052ee2c25285b162b51fdca732bcf67e39d647fb6830ae';
@ -277,12 +284,19 @@ assert.strictEqual(ecdh5.getPublicKey('hex'), cafebabePubPtUnComp);
// Verify setting and getting compressed and non-compressed serializations. // Verify setting and getting compressed and non-compressed serializations.
ecdh5.setPublicKey(cafebabePubPtComp, 'hex'); ecdh5.setPublicKey(cafebabePubPtComp, 'hex');
assert.strictEqual(ecdh5.getPublicKey('hex'), cafebabePubPtUnComp); assert.strictEqual(ecdh5.getPublicKey('hex'), cafebabePubPtUnComp);
assert.strictEqual(ecdh5.getPublicKey('hex', 'compressed'), cafebabePubPtComp); assert.strictEqual(
ecdh5.getPublicKey('hex', 'compressed'),
cafebabePubPtComp
);
ecdh5.setPublicKey(cafebabePubPtUnComp, 'hex'); ecdh5.setPublicKey(cafebabePubPtUnComp, 'hex');
assert.strictEqual(ecdh5.getPublicKey('hex'), cafebabePubPtUnComp); assert.strictEqual(ecdh5.getPublicKey('hex'), cafebabePubPtUnComp);
assert.strictEqual(ecdh5.getPublicKey('hex', 'compressed'), cafebabePubPtComp); assert.strictEqual(
ecdh5.getPublicKey('hex', 'compressed'),
cafebabePubPtComp
);
// Show why allowing the public key to be set on this type does not make sense. // Show why allowing the public key to be set on this type
// does not make sense.
ecdh5.setPublicKey(peerPubPtComp, 'hex'); ecdh5.setPublicKey(peerPubPtComp, 'hex');
assert.strictEqual(ecdh5.getPublicKey('hex'), peerPubPtUnComp); assert.strictEqual(ecdh5.getPublicKey('hex'), peerPubPtUnComp);
assert.throws(() => { assert.throws(() => {
@ -305,6 +319,7 @@ ecdh5.setPrivateKey(cafebabeKey, 'hex');
// Verify object state did not change. // Verify object state did not change.
assert.strictEqual(ecdh5.getPrivateKey('hex'), cafebabeKey); assert.strictEqual(ecdh5.getPrivateKey('hex'), cafebabeKey);
}); });
}
// invalid test: curve argument is undefined // invalid test: curve argument is undefined
assert.throws(() => { assert.throws(() => {