• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <assert.h>
2 #include <node.h>
3 
4 #include <openssl/opensslv.h>
5 #if OPENSSL_VERSION_MAJOR >= 3
6 #include <openssl/provider.h>
7 #endif
8 
9 namespace {
10 
11 using v8::Array;
12 using v8::Context;
13 using v8::FunctionCallbackInfo;
14 using v8::Isolate;
15 using v8::Local;
16 using v8::Object;
17 using v8::String;
18 using v8::Value;
19 
20 #if OPENSSL_VERSION_MAJOR >= 3
collectProviders(OSSL_PROVIDER * provider,void * cbdata)21 int collectProviders(OSSL_PROVIDER* provider, void* cbdata) {
22   static_cast<std::vector<OSSL_PROVIDER*>*>(cbdata)->push_back(provider);
23   return 1;
24 }
25 #endif
26 
GetProviders(const FunctionCallbackInfo<Value> & args)27 inline void GetProviders(const FunctionCallbackInfo<Value>& args) {
28   Isolate* isolate = args.GetIsolate();
29   std::vector<Local<Value>> arr = {};
30 #if OPENSSL_VERSION_MAJOR >= 3
31   std::vector<OSSL_PROVIDER*> providers;
32   OSSL_PROVIDER_do_all(nullptr, &collectProviders, &providers);
33   for (auto provider : providers) {
34     arr.push_back(
35         String::NewFromUtf8(isolate, OSSL_PROVIDER_get0_name(provider))
36             .ToLocalChecked());
37   }
38 #endif
39   args.GetReturnValue().Set(Array::New(isolate, arr.data(), arr.size()));
40 }
41 
Initialize(Local<Object> exports,Local<Value> module,Local<Context> context)42 inline void Initialize(Local<Object> exports,
43                        Local<Value> module,
44                        Local<Context> context) {
45   NODE_SET_METHOD(exports, "getProviders", GetProviders);
46 }
47 
48 }  // anonymous namespace
49 
50 NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME, Initialize)
51