|
1 | 1 | #include "crypto/crypto_dh.h" |
2 | 2 | #include "async_wrap-inl.h" |
3 | 3 | #include "base_object-inl.h" |
4 | | -#include "crypto/crypto_groups.h" |
5 | 4 | #include "crypto/crypto_keys.h" |
6 | 5 | #include "env-inl.h" |
7 | 6 | #include "memory_tracker-inl.h" |
@@ -138,6 +137,15 @@ void DiffieHellman::MemoryInfo(MemoryTracker* tracker) const { |
138 | 137 | tracker->TrackFieldWithSize("dh", dh_ ? kSizeOf_DH : 0); |
139 | 138 | } |
140 | 139 |
|
| 140 | +bool DiffieHellman::Init(BignumPointer&& bn_p, int g) { |
| 141 | + dh_.reset(DH_new()); |
| 142 | + CHECK_GE(g, 2); |
| 143 | + BignumPointer bn_g(BN_new()); |
| 144 | + return bn_g && BN_set_word(bn_g.get(), g) && |
| 145 | + DH_set0_pqg(dh_.get(), bn_p.release(), nullptr, bn_g.release()) && |
| 146 | + VerifyContext(); |
| 147 | +} |
| 148 | + |
141 | 149 | bool DiffieHellman::Init(const char* p, int p_len, int g) { |
142 | 150 | dh_.reset(DH_new()); |
143 | 151 | if (p_len <= 0) { |
@@ -192,11 +200,29 @@ bool DiffieHellman::Init(const char* p, int p_len, const char* g, int g_len) { |
192 | 200 | return VerifyContext(); |
193 | 201 | } |
194 | 202 |
|
195 | | -inline const modp_group* FindDiffieHellmanGroup(const char* name) { |
196 | | - for (const modp_group& group : modp_groups) { |
197 | | - if (StringEqualNoCase(name, group.name)) |
198 | | - return &group; |
199 | | - } |
| 203 | +constexpr int kStandardizedGenerator = 2; |
| 204 | + |
| 205 | +template <BIGNUM* (*p)(BIGNUM*)> |
| 206 | +BignumPointer InstantiateStandardizedGroup() { |
| 207 | + return BignumPointer(p(nullptr)); |
| 208 | +} |
| 209 | + |
| 210 | +typedef BignumPointer (*StandardizedGroupInstantiator)(); |
| 211 | + |
| 212 | +// Returns a function that can be used to create an instance of a standardized |
| 213 | +// Diffie-Hellman group. The generator is always kStandardizedGenerator. |
| 214 | +inline StandardizedGroupInstantiator FindDiffieHellmanGroup(const char* name) { |
| 215 | +#define V(n, p) \ |
| 216 | + if (StringEqualNoCase(name, n)) return InstantiateStandardizedGroup<p> |
| 217 | + V("modp1", BN_get_rfc2409_prime_768); |
| 218 | + V("modp2", BN_get_rfc2409_prime_1024); |
| 219 | + V("modp5", BN_get_rfc3526_prime_1536); |
| 220 | + V("modp14", BN_get_rfc3526_prime_2048); |
| 221 | + V("modp15", BN_get_rfc3526_prime_3072); |
| 222 | + V("modp16", BN_get_rfc3526_prime_4096); |
| 223 | + V("modp17", BN_get_rfc3526_prime_6144); |
| 224 | + V("modp18", BN_get_rfc3526_prime_8192); |
| 225 | +#undef V |
200 | 226 | return nullptr; |
201 | 227 | } |
202 | 228 |
|
@@ -211,13 +237,11 @@ void DiffieHellman::DiffieHellmanGroup( |
211 | 237 | bool initialized = false; |
212 | 238 |
|
213 | 239 | const node::Utf8Value group_name(env->isolate(), args[0]); |
214 | | - const modp_group* group = FindDiffieHellmanGroup(*group_name); |
| 240 | + auto group = FindDiffieHellmanGroup(*group_name); |
215 | 241 | if (group == nullptr) |
216 | 242 | return THROW_ERR_CRYPTO_UNKNOWN_DH_GROUP(env); |
217 | 243 |
|
218 | | - initialized = diffieHellman->Init(group->prime, |
219 | | - group->prime_size, |
220 | | - group->gen); |
| 244 | + initialized = diffieHellman->Init(group(), kStandardizedGenerator); |
221 | 245 | if (!initialized) |
222 | 246 | THROW_ERR_CRYPTO_INITIALIZATION_FAILED(env); |
223 | 247 | } |
@@ -480,16 +504,14 @@ Maybe<bool> DhKeyGenTraits::AdditionalConfig( |
480 | 504 |
|
481 | 505 | if (args[*offset]->IsString()) { |
482 | 506 | Utf8Value group_name(env->isolate(), args[*offset]); |
483 | | - const modp_group* group = FindDiffieHellmanGroup(*group_name); |
| 507 | + auto group = FindDiffieHellmanGroup(*group_name); |
484 | 508 | if (group == nullptr) { |
485 | 509 | THROW_ERR_CRYPTO_UNKNOWN_DH_GROUP(env); |
486 | 510 | return Nothing<bool>(); |
487 | 511 | } |
488 | 512 |
|
489 | | - params->params.prime = BignumPointer( |
490 | | - BN_bin2bn(reinterpret_cast<const unsigned char*>(group->prime), |
491 | | - group->prime_size, nullptr)); |
492 | | - params->params.generator = group->gen; |
| 513 | + params->params.prime = group(); |
| 514 | + params->params.generator = kStandardizedGenerator; |
493 | 515 | *offset += 1; |
494 | 516 | } else { |
495 | 517 | if (args[*offset]->IsInt32()) { |
|