crypto: add api to get openssl security level · nodejs/node@5470cab · GitHub
Skip to content

Commit 5470cab

Browse files
mhdawsonaduh95
authored andcommitted
crypto: add api to get openssl security level
Distros may compile with a different openssl security level than the default. In addition there has been some discussion with respect to shipping with a different default security security level in different Node.js versions in order to main stabilty. Exposing the default openssl security level with let us have tests that work in these situations as well as allow applications to better cope with the avialable crypto algorithms. - add API to get openssl security level - modify one test to use security level instead of openssl version as an example Signed-off-by: Michael Dawson <midawson@redhat.com> PR-URL: #56601 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
1 parent 642959b commit 5470cab

4 files changed

Lines changed: 59 additions & 4 deletions

File tree

lib/internal/crypto/util.js

Lines changed: 2 additions & 0 deletions

src/crypto/crypto_util.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ using ncrypto::BIOPointer;
3131
using ncrypto::CryptoErrorList;
3232
using ncrypto::EnginePointer;
3333
using ncrypto::EVPKeyCtxPointer;
34+
using ncrypto::SSLCtxPointer;
35+
using ncrypto::SSLPointer;
3436
using v8::ArrayBuffer;
3537
using v8::BackingStore;
3638
using v8::BigInt;
@@ -201,6 +203,27 @@ void TestFipsCrypto(const v8::FunctionCallbackInfo<v8::Value>& args) {
201203
args.GetReturnValue().Set(ncrypto::testFipsEnabled() ? 1 : 0);
202204
}
203205

206+
void GetOpenSSLSecLevelCrypto(const FunctionCallbackInfo<Value>& args) {
207+
// for BoringSSL assume the same as the default
208+
int sec_level = OPENSSL_TLS_SECURITY_LEVEL;
209+
#ifndef OPENSSL_IS_BORINGSSL
210+
Environment* env = Environment::GetCurrent(args);
211+
212+
auto ctx = SSLCtxPointer::New();
213+
if (!ctx) {
214+
return ThrowCryptoError(env, ERR_get_error(), "SSL_CTX_new");
215+
}
216+
217+
auto ssl = SSLPointer::New(ctx);
218+
if (!ssl) {
219+
return ThrowCryptoError(env, ERR_get_error(), "SSL_new");
220+
}
221+
222+
sec_level = SSL_get_security_level(ssl);
223+
#endif // OPENSSL_IS_BORINGSSL
224+
args.GetReturnValue().Set(sec_level);
225+
}
226+
204227
void CryptoErrorStore::Capture() {
205228
errors_.clear();
206229
while (const uint32_t err = ERR_get_error()) {
@@ -703,6 +726,9 @@ void Initialize(Environment* env, Local<Object> target) {
703726

704727
SetMethod(context, target, "secureBuffer", SecureBuffer);
705728
SetMethod(context, target, "secureHeapUsed", SecureHeapUsed);
729+
730+
SetMethodNoSideEffect(
731+
context, target, "getOpenSSLSecLevelCrypto", GetOpenSSLSecLevelCrypto);
706732
}
707733
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
708734
#ifndef OPENSSL_NO_ENGINE
@@ -714,6 +740,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
714740
registry->Register(TestFipsCrypto);
715741
registry->Register(SecureBuffer);
716742
registry->Register(SecureHeapUsed);
743+
registry->Register(GetOpenSSLSecLevelCrypto);
717744
}
718745

719746
} // namespace Util
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
4+
const common = require('../common');
5+
if (!common.hasCrypto)
6+
common.skip('missing crypto');
7+
8+
const assert = require('assert');
9+
10+
// OpenSSL has a set of security levels which affect what algorithms
11+
// are available by default. Different OpenSSL veresions have different
12+
// default security levels and we use this value to adjust what a test
13+
// expects based on the security level. You can read more in
14+
// https://docs.openssl.org/1.1.1/man3/SSL_CTX_set_security_level/#default-callback-behaviour
15+
// This test simply validates that we can get some value for the secLevel
16+
// when needed by tests.
17+
const secLevel = require('internal/crypto/util').getOpenSSLSecLevel();
18+
assert.ok(secLevel >= 0 && secLevel <= 5);

test/parallel/test-tls-dhe.js

Lines changed: 12 additions & 4 deletions

0 commit comments

Comments
 (0)