forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_dsa.cc
149 lines (129 loc) · 4.11 KB
/
crypto_dsa.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "crypto/crypto_dsa.h"
#include "crypto/crypto_keys.h"
#include "crypto/crypto_util.h"
#include "async_wrap-inl.h"
#include "env-inl.h"
#include "memory_tracker-inl.h"
#include "threadpoolwork-inl.h"
#include "v8.h"
#include <openssl/bn.h>
#include <openssl/dsa.h>
#include <cstdio>
namespace node {
using ncrypto::Dsa;
using ncrypto::EVPKeyCtxPointer;
using v8::FunctionCallbackInfo;
using v8::Int32;
using v8::JustVoid;
using v8::Local;
using v8::Maybe;
using v8::Number;
using v8::Object;
using v8::Uint32;
using v8::Value;
namespace crypto {
EVPKeyCtxPointer DsaKeyGenTraits::Setup(DsaKeyPairGenConfig* params) {
#ifdef OPENSSL_IS_BORINGSSL
// Operation is unsupported in BoringSSL
return {};
#else
auto param_ctx = EVPKeyCtxPointer::NewFromID(EVP_PKEY_DSA);
if (!param_ctx.initForParamgen() ||
!param_ctx.setDsaParameters(
params->params.modulus_bits,
params->params.divisor_bits != -1
? std::optional<int>(params->params.divisor_bits)
: std::nullopt)) {
return {};
}
auto key_params = param_ctx.paramgen();
if (!key_params) return {};
EVPKeyCtxPointer key_ctx = key_params.newCtx();
if (!key_ctx.initForKeygen()) return {};
return key_ctx;
#endif
}
// Input arguments for DsaKeyPairGenJob
// 1. CryptoJobMode
// 2. Modulus Bits
// 3. Divisor Bits
// 4. Public Format
// 5. Public Type
// 6. Private Format
// 7. Private Type
// 8. Cipher
// 9. Passphrase
Maybe<void> DsaKeyGenTraits::AdditionalConfig(
CryptoJobMode mode,
const FunctionCallbackInfo<Value>& args,
unsigned int* offset,
DsaKeyPairGenConfig* params) {
CHECK(args[*offset]->IsUint32()); // modulus bits
CHECK(args[*offset + 1]->IsInt32()); // divisor bits
params->params.modulus_bits = args[*offset].As<Uint32>()->Value();
params->params.divisor_bits = args[*offset + 1].As<Int32>()->Value();
CHECK_GE(params->params.divisor_bits, -1);
*offset += 2;
return JustVoid();
}
Maybe<void> DSAKeyExportTraits::AdditionalConfig(
const FunctionCallbackInfo<Value>& args,
unsigned int offset,
DSAKeyExportConfig* params) {
return JustVoid();
}
WebCryptoKeyExportStatus DSAKeyExportTraits::DoExport(
const KeyObjectData& key_data,
WebCryptoKeyFormat format,
const DSAKeyExportConfig& params,
ByteSource* out) {
CHECK_NE(key_data.GetKeyType(), kKeyTypeSecret);
switch (format) {
case kWebCryptoKeyFormatRaw:
// Not supported for RSA keys of either type
return WebCryptoKeyExportStatus::FAILED;
case kWebCryptoKeyFormatPKCS8:
if (key_data.GetKeyType() != kKeyTypePrivate)
return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
return PKEY_PKCS8_Export(key_data, out);
case kWebCryptoKeyFormatSPKI:
if (key_data.GetKeyType() != kKeyTypePublic)
return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
return PKEY_SPKI_Export(key_data, out);
default:
UNREACHABLE();
}
}
bool GetDsaKeyDetail(Environment* env,
const KeyObjectData& key,
Local<Object> target) {
if (!key) return false;
Dsa dsa = key.GetAsymmetricKey();
if (!dsa) return false;
size_t modulus_length = dsa.getModulusLength();
size_t divisor_length = dsa.getDivisorLength();
return target
->Set(env->context(),
env->modulus_length_string(),
Number::New(env->isolate(),
static_cast<double>(modulus_length)))
.IsJust() &&
target
->Set(env->context(),
env->divisor_length_string(),
Number::New(env->isolate(),
static_cast<double>(divisor_length)))
.IsJust();
}
namespace DSAAlg {
void Initialize(Environment* env, Local<Object> target) {
DsaKeyPairGenJob::Initialize(env, target);
DSAKeyExportJob::Initialize(env, target);
}
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
DsaKeyPairGenJob::RegisterExternalReferences(registry);
DSAKeyExportJob::RegisterExternalReferences(registry);
}
} // namespace DSAAlg
} // namespace crypto
} // namespace node