Skip to content

Commit 38d1afc

Browse files
committed
crypto: add getCurves() to get supported ECs
PR-URL: #1914 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent ff39ecb commit 38d1afc

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

doc/api/crypto.markdown

+11-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Returns an array with the names of the supported ciphers.
4242
Example:
4343

4444
var ciphers = crypto.getCiphers();
45-
console.log(ciphers); // ['AES-128-CBC', 'AES-128-CBC-HMAC-SHA1', ...]
45+
console.log(ciphers); // ['aes-128-cbc', 'aes-128-ccm', ...]
4646

4747

4848
## crypto.getHashes()
@@ -55,6 +55,16 @@ Example:
5555
console.log(hashes); // ['sha', 'sha1', 'sha1WithRSAEncryption', ...]
5656

5757

58+
## crypto.getCurves()
59+
60+
Returns an array with the names of the supported elliptic curves.
61+
62+
Example:
63+
64+
var curves = crypto.getCurves();
65+
console.log(curves); // ['secp256k1', 'secp384r1', ...]
66+
67+
5868
## crypto.createCredentials(details)
5969

6070
Stability: 0 - Deprecated. Use [tls.createSecureContext][] instead.

lib/crypto.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ try {
1010
var randomBytes = binding.randomBytes;
1111
var getCiphers = binding.getCiphers;
1212
var getHashes = binding.getHashes;
13+
var getCurves = binding.getCurves;
1314
} catch (e) {
1415
throw new Error('node.js not compiled with openssl crypto support.');
1516
}
@@ -652,13 +653,17 @@ exports.randomBytes = exports.pseudoRandomBytes = randomBytes;
652653
exports.rng = exports.prng = randomBytes;
653654

654655
exports.getCiphers = function() {
655-
return filterDuplicates(getCiphers.call(null, arguments));
656+
return filterDuplicates(getCiphers());
656657
};
657658

658659

659660
exports.getHashes = function() {
660-
return filterDuplicates(getHashes.call(null, arguments));
661+
return filterDuplicates(getHashes());
662+
};
663+
661664

665+
exports.getCurves = function() {
666+
return filterDuplicates(getCurves());
662667
};
663668

664669

src/node_crypto.cc

+27
Original file line numberDiff line numberDiff line change
@@ -4878,6 +4878,32 @@ void GetHashes(const FunctionCallbackInfo<Value>& args) {
48784878
}
48794879

48804880

4881+
void GetCurves(const FunctionCallbackInfo<Value>& args) {
4882+
Environment* env = Environment::GetCurrent(args);
4883+
const size_t num_curves = EC_get_builtin_curves(nullptr, 0);
4884+
Local<Array> arr = Array::New(env->isolate(), num_curves);
4885+
EC_builtin_curve* curves;
4886+
size_t alloc_size;
4887+
4888+
if (num_curves) {
4889+
alloc_size = sizeof(*curves) * num_curves;
4890+
curves = static_cast<EC_builtin_curve*>(malloc(alloc_size));
4891+
4892+
CHECK_NE(curves, nullptr);
4893+
4894+
if (EC_get_builtin_curves(curves, num_curves)) {
4895+
for (size_t i = 0; i < num_curves; i++) {
4896+
arr->Set(i, OneByteString(env->isolate(), OBJ_nid2sn(curves[i].nid)));
4897+
}
4898+
}
4899+
4900+
free(curves);
4901+
}
4902+
4903+
args.GetReturnValue().Set(arr);
4904+
}
4905+
4906+
48814907
void Certificate::Initialize(Environment* env, Handle<Object> target) {
48824908
HandleScope scope(env->isolate());
48834909

@@ -5160,6 +5186,7 @@ void InitCrypto(Handle<Object> target,
51605186
env->SetMethod(target, "getSSLCiphers", GetSSLCiphers);
51615187
env->SetMethod(target, "getCiphers", GetCiphers);
51625188
env->SetMethod(target, "getHashes", GetHashes);
5189+
env->SetMethod(target, "getCurves", GetCurves);
51635190
env->SetMethod(target, "publicEncrypt",
51645191
PublicKeyCipher::Cipher<PublicKeyCipher::kPublic,
51655192
EVP_PKEY_encrypt_init,

test/parallel/test-crypto.js

+6
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ assert.notEqual(-1, crypto.getHashes().indexOf('RSA-SHA1'));
9696
assert.equal(-1, crypto.getHashes().indexOf('rsa-sha1'));
9797
assertSorted(crypto.getHashes());
9898

99+
// Assume that we have at least secp384r1.
100+
assert.notEqual(0, crypto.getCurves().length);
101+
assert.notEqual(-1, crypto.getCurves().indexOf('secp384r1'));
102+
assert.equal(-1, crypto.getCurves().indexOf('SECP384R1'));
103+
assertSorted(crypto.getCurves());
104+
99105
// Regression tests for #5725: hex input that's not a power of two should
100106
// throw, not assert in C++ land.
101107
assert.throws(function() {

0 commit comments

Comments
 (0)