/src/node/src/crypto/crypto_dsa.cc
Line | Count | Source |
1 | | #include "crypto/crypto_dsa.h" |
2 | | #include "crypto/crypto_keys.h" |
3 | | #include "crypto/crypto_util.h" |
4 | | #include "async_wrap-inl.h" |
5 | | #include "env-inl.h" |
6 | | #include "memory_tracker-inl.h" |
7 | | #include "threadpoolwork-inl.h" |
8 | | #include "v8.h" |
9 | | |
10 | | #include <openssl/bn.h> |
11 | | #include <openssl/dsa.h> |
12 | | |
13 | | #include <cstdio> |
14 | | |
15 | | namespace node { |
16 | | |
17 | | using ncrypto::Dsa; |
18 | | using ncrypto::EVPKeyCtxPointer; |
19 | | using v8::FunctionCallbackInfo; |
20 | | using v8::Int32; |
21 | | using v8::JustVoid; |
22 | | using v8::Local; |
23 | | using v8::Maybe; |
24 | | using v8::Number; |
25 | | using v8::Object; |
26 | | using v8::Uint32; |
27 | | using v8::Value; |
28 | | |
29 | | namespace crypto { |
30 | 0 | EVPKeyCtxPointer DsaKeyGenTraits::Setup(DsaKeyPairGenConfig* params) { |
31 | 0 | auto param_ctx = EVPKeyCtxPointer::NewFromID(EVP_PKEY_DSA); |
32 | |
|
33 | 0 | if (!param_ctx || !param_ctx.initForParamgen() || |
34 | 0 | !param_ctx.setDsaParameters( |
35 | 0 | params->params.modulus_bits, |
36 | 0 | params->params.divisor_bits != -1 |
37 | 0 | ? std::optional<int>(params->params.divisor_bits) |
38 | 0 | : std::nullopt)) { |
39 | 0 | return {}; |
40 | 0 | } |
41 | | |
42 | 0 | auto key_params = param_ctx.paramgen(); |
43 | 0 | if (!key_params) return {}; |
44 | | |
45 | 0 | EVPKeyCtxPointer key_ctx = key_params.newCtx(); |
46 | 0 | if (!key_ctx.initForKeygen()) return {}; |
47 | 0 | return key_ctx; |
48 | 0 | } |
49 | | |
50 | | // Input arguments for DsaKeyPairGenJob |
51 | | // 1. CryptoJobMode |
52 | | // 2. Modulus Bits |
53 | | // 3. Divisor Bits |
54 | | // 4. Public Format |
55 | | // 5. Public Type |
56 | | // 6. Private Format |
57 | | // 7. Private Type |
58 | | // 8. Cipher |
59 | | // 9. Passphrase |
60 | | Maybe<void> DsaKeyGenTraits::AdditionalConfig( |
61 | | CryptoJobMode mode, |
62 | | const FunctionCallbackInfo<Value>& args, |
63 | | unsigned int* offset, |
64 | 0 | DsaKeyPairGenConfig* params) { |
65 | 0 | CHECK(args[*offset]->IsUint32()); // modulus bits |
66 | 0 | CHECK(args[*offset + 1]->IsInt32()); // divisor bits |
67 | | |
68 | 0 | params->params.modulus_bits = args[*offset].As<Uint32>()->Value(); |
69 | 0 | params->params.divisor_bits = args[*offset + 1].As<Int32>()->Value(); |
70 | 0 | CHECK_GE(params->params.divisor_bits, -1); |
71 | | |
72 | 0 | *offset += 2; |
73 | |
|
74 | 0 | return JustVoid(); |
75 | 0 | } |
76 | | |
77 | | bool GetDsaKeyDetail(Environment* env, |
78 | | const KeyObjectData& key, |
79 | 0 | Local<Object> target) { |
80 | 0 | if (!key) return false; |
81 | 0 | Dsa dsa = key.GetAsymmetricKey(); |
82 | 0 | if (!dsa) return false; |
83 | | |
84 | 0 | size_t modulus_length = dsa.getModulusLength(); |
85 | 0 | size_t divisor_length = dsa.getDivisorLength(); |
86 | |
|
87 | 0 | return target |
88 | 0 | ->Set(env->context(), |
89 | 0 | env->modulus_length_string(), |
90 | 0 | Number::New(env->isolate(), |
91 | 0 | static_cast<double>(modulus_length))) |
92 | 0 | .IsJust() && |
93 | 0 | target |
94 | 0 | ->Set(env->context(), |
95 | 0 | env->divisor_length_string(), |
96 | 0 | Number::New(env->isolate(), |
97 | 0 | static_cast<double>(divisor_length))) |
98 | 0 | .IsJust(); |
99 | 0 | } |
100 | | |
101 | | namespace DSAAlg { |
102 | 0 | void Initialize(Environment* env, Local<Object> target) { |
103 | 0 | DsaKeyPairGenJob::Initialize(env, target); |
104 | 0 | } |
105 | | |
106 | 0 | void RegisterExternalReferences(ExternalReferenceRegistry* registry) { |
107 | 0 | DsaKeyPairGenJob::RegisterExternalReferences(registry); |
108 | 0 | } |
109 | | } // namespace DSAAlg |
110 | | } // namespace crypto |
111 | | } // namespace node |