-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: split test-crypto-keygen.js #49221
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
const assert = require('assert'); | ||
const { | ||
generateKeyPair, | ||
} = require('crypto'); | ||
|
||
// Test async DSA key object generation. | ||
{ | ||
generateKeyPair('dsa', { | ||
modulusLength: common.hasOpenSSL3 ? 2048 : 512, | ||
divisorLength: 256 | ||
}, common.mustSucceed((publicKey, privateKey) => { | ||
assert.strictEqual(publicKey.type, 'public'); | ||
assert.strictEqual(publicKey.asymmetricKeyType, 'dsa'); | ||
assert.deepStrictEqual(publicKey.asymmetricKeyDetails, { | ||
modulusLength: common.hasOpenSSL3 ? 2048 : 512, | ||
divisorLength: 256 | ||
}); | ||
|
||
assert.strictEqual(privateKey.type, 'private'); | ||
assert.strictEqual(privateKey.asymmetricKeyType, 'dsa'); | ||
assert.deepStrictEqual(privateKey.asymmetricKeyDetails, { | ||
modulusLength: common.hasOpenSSL3 ? 2048 : 512, | ||
divisorLength: 256 | ||
}); | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
const assert = require('assert'); | ||
const { | ||
generateKeyPair, | ||
} = require('crypto'); | ||
const { | ||
assertApproximateSize, | ||
testSignVerify, | ||
spkiExp, | ||
} = require('../common/crypto'); | ||
|
||
// Test async DSA key generation. | ||
{ | ||
const privateKeyEncoding = { | ||
type: 'pkcs8', | ||
format: 'der' | ||
}; | ||
|
||
generateKeyPair('dsa', { | ||
modulusLength: common.hasOpenSSL3 ? 2048 : 512, | ||
divisorLength: 256, | ||
publicKeyEncoding: { | ||
type: 'spki', | ||
format: 'pem' | ||
}, | ||
privateKeyEncoding: { | ||
cipher: 'aes-128-cbc', | ||
passphrase: 'secret', | ||
...privateKeyEncoding | ||
} | ||
}, common.mustSucceed((publicKey, privateKeyDER) => { | ||
assert.strictEqual(typeof publicKey, 'string'); | ||
assert.match(publicKey, spkiExp); | ||
// The private key is DER-encoded. | ||
assert(Buffer.isBuffer(privateKeyDER)); | ||
|
||
assertApproximateSize(publicKey, common.hasOpenSSL3 ? 1194 : 440); | ||
assertApproximateSize(privateKeyDER, common.hasOpenSSL3 ? 721 : 336); | ||
|
||
// Since the private key is encrypted, signing shouldn't work anymore. | ||
assert.throws(() => { | ||
return testSignVerify(publicKey, { | ||
key: privateKeyDER, | ||
...privateKeyEncoding | ||
}); | ||
}, { | ||
name: 'TypeError', | ||
code: 'ERR_MISSING_PASSPHRASE', | ||
message: 'Passphrase required for encrypted key' | ||
}); | ||
|
||
// Signing should work with the correct password. | ||
testSignVerify(publicKey, { | ||
key: privateKeyDER, | ||
...privateKeyEncoding, | ||
passphrase: 'secret' | ||
}); | ||
})); | ||
} |
35 changes: 35 additions & 0 deletions
35
test/parallel/test-crypto-keygen-async-elliptic-curve-jwk-ec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
const assert = require('assert'); | ||
const { | ||
generateKeyPair, | ||
} = require('crypto'); | ||
|
||
// Test async elliptic curve key generation with 'jwk' encoding and named | ||
// curve. | ||
['P-384', 'P-256', 'P-521', 'secp256k1'].forEach((curve) => { | ||
generateKeyPair('ec', { | ||
namedCurve: curve, | ||
publicKeyEncoding: { | ||
format: 'jwk' | ||
}, | ||
privateKeyEncoding: { | ||
format: 'jwk' | ||
} | ||
}, common.mustSucceed((publicKey, privateKey) => { | ||
assert.strictEqual(typeof publicKey, 'object'); | ||
assert.strictEqual(typeof privateKey, 'object'); | ||
assert.strictEqual(publicKey.x, privateKey.x); | ||
assert.strictEqual(publicKey.y, privateKey.y); | ||
assert(!publicKey.d); | ||
assert(privateKey.d); | ||
assert.strictEqual(publicKey.kty, 'EC'); | ||
assert.strictEqual(publicKey.kty, privateKey.kty); | ||
assert.strictEqual(publicKey.crv, curve); | ||
assert.strictEqual(publicKey.crv, privateKey.crv); | ||
})); | ||
}); |
38 changes: 38 additions & 0 deletions
38
test/parallel/test-crypto-keygen-async-elliptic-curve-jwk-rsa.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
const assert = require('assert'); | ||
const { | ||
generateKeyPair, | ||
} = require('crypto'); | ||
|
||
// Test async elliptic curve key generation with 'jwk' encoding and RSA. | ||
{ | ||
generateKeyPair('rsa', { | ||
modulusLength: 1024, | ||
publicKeyEncoding: { | ||
format: 'jwk' | ||
}, | ||
privateKeyEncoding: { | ||
format: 'jwk' | ||
} | ||
}, common.mustSucceed((publicKey, privateKey) => { | ||
assert.strictEqual(typeof publicKey, 'object'); | ||
assert.strictEqual(typeof privateKey, 'object'); | ||
assert.strictEqual(publicKey.kty, 'RSA'); | ||
assert.strictEqual(publicKey.kty, privateKey.kty); | ||
assert.strictEqual(typeof publicKey.n, 'string'); | ||
assert.strictEqual(publicKey.n, privateKey.n); | ||
assert.strictEqual(typeof publicKey.e, 'string'); | ||
assert.strictEqual(publicKey.e, privateKey.e); | ||
assert.strictEqual(typeof privateKey.d, 'string'); | ||
assert.strictEqual(typeof privateKey.p, 'string'); | ||
assert.strictEqual(typeof privateKey.q, 'string'); | ||
assert.strictEqual(typeof privateKey.dp, 'string'); | ||
assert.strictEqual(typeof privateKey.dq, 'string'); | ||
assert.strictEqual(typeof privateKey.qi, 'string'); | ||
})); | ||
} |
40 changes: 40 additions & 0 deletions
40
test/parallel/test-crypto-keygen-async-elliptic-curve-jwk.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
const assert = require('assert'); | ||
const { | ||
generateKeyPair, | ||
} = require('crypto'); | ||
|
||
// Test async elliptic curve key generation with 'jwk' encoding. | ||
{ | ||
[ | ||
'ed25519', | ||
'ed448', | ||
'x25519', | ||
'x448', | ||
].forEach((type) => { | ||
generateKeyPair(type, { | ||
publicKeyEncoding: { | ||
format: 'jwk' | ||
}, | ||
privateKeyEncoding: { | ||
format: 'jwk' | ||
} | ||
}, common.mustSucceed((publicKey, privateKey) => { | ||
assert.strictEqual(typeof publicKey, 'object'); | ||
assert.strictEqual(typeof privateKey, 'object'); | ||
assert.strictEqual(publicKey.x, privateKey.x); | ||
assert(!publicKey.d); | ||
assert(privateKey.d); | ||
assert.strictEqual(publicKey.kty, 'OKP'); | ||
assert.strictEqual(publicKey.kty, privateKey.kty); | ||
const expectedCrv = `${type.charAt(0).toUpperCase()}${type.slice(1)}`; | ||
assert.strictEqual(publicKey.crv, expectedCrv); | ||
assert.strictEqual(publicKey.crv, privateKey.crv); | ||
})); | ||
}); | ||
} |
50 changes: 50 additions & 0 deletions
50
test/parallel/test-crypto-keygen-async-encrypted-private-key-der.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
const assert = require('assert'); | ||
const { | ||
generateKeyPair, | ||
} = require('crypto'); | ||
const { | ||
assertApproximateSize, | ||
testEncryptDecrypt, | ||
testSignVerify, | ||
} = require('../common/crypto'); | ||
|
||
// Test async RSA key generation with an encrypted private key, but encoded as DER. | ||
{ | ||
generateKeyPair('rsa', { | ||
publicExponent: 0x10001, | ||
modulusLength: 512, | ||
publicKeyEncoding: { | ||
type: 'pkcs1', | ||
format: 'der' | ||
}, | ||
privateKeyEncoding: { | ||
type: 'pkcs8', | ||
format: 'der' | ||
} | ||
}, common.mustSucceed((publicKeyDER, privateKeyDER) => { | ||
assert(Buffer.isBuffer(publicKeyDER)); | ||
assertApproximateSize(publicKeyDER, 74); | ||
|
||
assert(Buffer.isBuffer(privateKeyDER)); | ||
|
||
const publicKey = { | ||
key: publicKeyDER, | ||
type: 'pkcs1', | ||
format: 'der', | ||
}; | ||
const privateKey = { | ||
key: privateKeyDER, | ||
format: 'der', | ||
type: 'pkcs8', | ||
passphrase: 'secret' | ||
}; | ||
testEncryptDecrypt(publicKey, privateKey); | ||
testSignVerify(publicKey, privateKey); | ||
})); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These functions have very generic names but very specific implementations. For example,
testEncryptDecrypt
works with certain asymmetric key pairs only, whereas most encryption and decryption operations in cryptography rely on symmetric keys.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have a suggestions for the names? i think generally it's fine to have very generic names in the test commons - that's what we've been doing for many tests utils too. The point is just sharing code among tests. The were named that way in one long test test anyway, so keeping the name doesn't make much of a difference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, in a test file that exclusively used asymmetric encryption. But moving them into
common
, these functions become available to all tests.I don't want to make this more work for you than it has to be, so I am fine with leaving the names as they are.