Skip to content

Commit f53441a

Browse files
committed
crypto: add crypto.getCiphers()
Returns a list of, unsurprisingly, the available ciphers.
1 parent 61978f5 commit f53441a

4 files changed

Lines changed: 48 additions & 0 deletions

File tree

doc/api/crypto.markdown

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ of a secure HTTPS net or http connection.
1010

1111
It also offers a set of wrappers for OpenSSL's hash, hmac, cipher, decipher, sign and verify methods.
1212

13+
14+
## crypto.getCiphers()
15+
16+
Returns an array with the names of the supported ciphers.
17+
18+
Example:
19+
20+
var ciphers = crypto.getCiphers();
21+
console.log(ciphers); // ['AES128-SHA', 'AES256-SHA', ...]
22+
23+
1324
## crypto.createCredentials(details)
1425

1526
Creates a credentials object, with the optional details being a dictionary with keys:

lib/crypto.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ try {
3434
var PBKDF2 = binding.PBKDF2;
3535
var randomBytes = binding.randomBytes;
3636
var pseudoRandomBytes = binding.pseudoRandomBytes;
37+
var getCiphers = binding.getCiphers;
3738
var crypto = true;
3839
} catch (e) {
3940

@@ -193,3 +194,5 @@ exports.pseudoRandomBytes = pseudoRandomBytes;
193194

194195
exports.rng = randomBytes;
195196
exports.prng = pseudoRandomBytes;
197+
198+
exports.getCiphers = getCiphers;

src/node_crypto.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4617,6 +4617,35 @@ Handle<Value> RandomBytes(const Arguments& args) {
46174617
}
46184618

46194619

4620+
Handle<Value> GetCiphers(const Arguments& args) {
4621+
HandleScope scope;
4622+
4623+
SSL_CTX* ctx = SSL_CTX_new(TLSv1_server_method());
4624+
if (ctx == NULL) {
4625+
return ThrowError("SSL_CTX_new() failed.");
4626+
}
4627+
4628+
SSL* ssl = SSL_new(ctx);
4629+
if (ssl == NULL) {
4630+
SSL_CTX_free(ctx);
4631+
return ThrowError("SSL_new() failed.");
4632+
}
4633+
4634+
Local<Array> arr = Array::New();
4635+
STACK_OF(SSL_CIPHER)* ciphers = SSL_get_ciphers(ssl);
4636+
4637+
for (int i = 0; i < sk_SSL_CIPHER_num(ciphers); ++i) {
4638+
SSL_CIPHER* cipher = sk_SSL_CIPHER_value(ciphers, i);
4639+
arr->Set(i, String::New(SSL_CIPHER_get_name(cipher)));
4640+
}
4641+
4642+
SSL_free(ssl);
4643+
SSL_CTX_free(ctx);
4644+
4645+
return scope.Close(arr);
4646+
}
4647+
4648+
46204649
void InitCrypto(Handle<Object> target) {
46214650
HandleScope scope;
46224651

@@ -4656,6 +4685,7 @@ void InitCrypto(Handle<Object> target) {
46564685
NODE_SET_METHOD(target, "PBKDF2", PBKDF2);
46574686
NODE_SET_METHOD(target, "randomBytes", RandomBytes<RAND_bytes>);
46584687
NODE_SET_METHOD(target, "pseudoRandomBytes", RandomBytes<RAND_pseudo_bytes>);
4688+
NODE_SET_METHOD(target, "getCiphers", GetCiphers);
46594689

46604690
subject_symbol = NODE_PSYMBOL("subject");
46614691
issuer_symbol = NODE_PSYMBOL("issuer");

test/simple/test-crypto.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,3 +683,7 @@ testPBKDF2('passwordPASSWORDpassword',
683683
testPBKDF2('pass\0word', 'sa\0lt', 4096, 16,
684684
'\x56\xfa\x6a\xa7\x55\x48\x09\x9d\xcc\x37\xd7\xf0\x34' +
685685
'\x25\xe0\xc3');
686+
687+
// Assume that we have at least AES256-SHA.
688+
assert.notEqual(0, crypto.getCiphers());
689+
assert.notEqual(-1, crypto.getCiphers().indexOf('AES256-SHA'));

0 commit comments

Comments
 (0)