Skip to content

Commit 17f931c

Browse files
jasnelladuh95
authored andcommittedFeb 3, 2025
src: cleaning up more crypto internals for ncrypto
PR-URL: #56526 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 14c562c commit 17f931c

40 files changed

+336
-277
lines changed
 

‎deps/ncrypto/ncrypto.h

-3
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,7 @@ using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer;
197197

198198
using BignumCtxPointer = DeleteFnPtr<BN_CTX, BN_CTX_free>;
199199
using BignumGenCallbackPointer = DeleteFnPtr<BN_GENCB, BN_GENCB_free>;
200-
using DSAPointer = DeleteFnPtr<DSA, DSA_free>;
201-
using DSASigPointer = DeleteFnPtr<DSA_SIG, DSA_SIG_free>;
202200
using ECDSASigPointer = DeleteFnPtr<ECDSA_SIG, ECDSA_SIG_free>;
203-
using ECPointer = DeleteFnPtr<EC_KEY, EC_KEY_free>;
204201
using ECGroupPointer = DeleteFnPtr<EC_GROUP, EC_GROUP_free>;
205202
using ECKeyPointer = DeleteFnPtr<EC_KEY, EC_KEY_free>;
206203
using ECPointPointer = DeleteFnPtr<EC_POINT, EC_POINT_free>;

‎src/crypto/crypto_aes.cc

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
namespace node {
1818

19+
using ncrypto::BignumPointer;
20+
using ncrypto::Cipher;
21+
using ncrypto::CipherCtxPointer;
1922
using v8::FunctionCallbackInfo;
2023
using v8::Just;
2124
using v8::JustVoid;
@@ -60,7 +63,7 @@ WebCryptoCipherStatus AES_Cipher(Environment* env,
6063

6164
if (!ctx.setKeyLength(key_data.GetSymmetricKeySize()) ||
6265
!ctx.init(
63-
ncrypto::Cipher(),
66+
Cipher(),
6467
encrypt,
6568
reinterpret_cast<const unsigned char*>(key_data.GetSymmetricKey()),
6669
params.iv.data<unsigned char>())) {
@@ -464,7 +467,7 @@ Maybe<void> AESCipherTraits::AdditionalConfig(
464467
}
465468
#undef V
466469

467-
params->cipher = ncrypto::Cipher::FromNid(cipher_nid);
470+
params->cipher = Cipher::FromNid(cipher_nid);
468471
if (!params->cipher) {
469472
THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env);
470473
return Nothing<void>();

‎src/crypto/crypto_bio.cc

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#include <cstring>
3131

3232
namespace node {
33+
34+
using ncrypto::BIOPointer;
35+
3336
namespace crypto {
3437

3538
BIOPointer NodeBIO::New(Environment* env) {

‎src/crypto/crypto_bio.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ class NodeBIO : public MemoryRetainer {
4343
public:
4444
~NodeBIO() override;
4545

46-
static BIOPointer New(Environment* env = nullptr);
46+
static ncrypto::BIOPointer New(Environment* env = nullptr);
4747

4848
// NewFixed takes a copy of `len` bytes from `data` and returns a BIO that,
4949
// when read from, returns those bytes followed by EOF.
50-
static BIOPointer NewFixed(const char* data, size_t len,
51-
Environment* env = nullptr);
50+
static ncrypto::BIOPointer NewFixed(const char* data,
51+
size_t len,
52+
Environment* env = nullptr);
5253

5354
// Move read head to next buffer if needed
5455
void TryMoveReadHead();

‎src/crypto/crypto_cipher.cc

+14-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010

1111
namespace node {
1212

13+
using ncrypto::Cipher;
14+
using ncrypto::CipherCtxPointer;
15+
using ncrypto::EVPKeyCtxPointer;
16+
using ncrypto::EVPKeyPointer;
17+
using ncrypto::MarkPopErrorOnReturn;
18+
using ncrypto::SSLCtxPointer;
19+
using ncrypto::SSLPointer;
1320
using v8::Array;
1421
using v8::ArrayBuffer;
1522
using v8::BackingStore;
@@ -42,10 +49,10 @@ void GetCipherInfo(const FunctionCallbackInfo<Value>& args) {
4249
const auto cipher = ([&] {
4350
if (args[1]->IsString()) {
4451
Utf8Value name(env->isolate(), args[1]);
45-
return ncrypto::Cipher::FromName(*name);
52+
return Cipher::FromName(*name);
4653
} else {
4754
int nid = args[1].As<Int32>()->Value();
48-
return ncrypto::Cipher::FromNid(nid);
55+
return Cipher::FromNid(nid);
4956
}
5057
})();
5158

@@ -334,7 +341,7 @@ void CipherBase::CommonInit(const char* cipher_type,
334341
return THROW_ERR_CRYPTO_INVALID_KEYLEN(env());
335342
}
336343

337-
if (!ctx_.init(ncrypto::Cipher(), encrypt, key, iv)) {
344+
if (!ctx_.init(Cipher(), encrypt, key, iv)) {
338345
return ThrowCryptoError(env(), ERR_get_error(),
339346
"Failed to initialize cipher");
340347
}
@@ -345,7 +352,7 @@ void CipherBase::Init(const char* cipher_type,
345352
unsigned int auth_tag_len) {
346353
HandleScope scope(env()->isolate());
347354
MarkPopErrorOnReturn mark_pop_error_on_return;
348-
auto cipher = ncrypto::Cipher::FromName(cipher_type);
355+
auto cipher = Cipher::FromName(cipher_type);
349356
if (!cipher) {
350357
return THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env());
351358
}
@@ -415,7 +422,7 @@ void CipherBase::InitIv(const char* cipher_type,
415422
HandleScope scope(env()->isolate());
416423
MarkPopErrorOnReturn mark_pop_error_on_return;
417424

418-
auto cipher = ncrypto::Cipher::FromName(cipher_type);
425+
auto cipher = Cipher::FromName(cipher_type);
419426
if (!cipher) return THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env());
420427

421428
const int expected_iv_len = cipher.getIvLength();
@@ -628,8 +635,7 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
628635
} else {
629636
// At this point, the tag length is already known and must match the
630637
// length of the given authentication tag.
631-
CHECK(
632-
ncrypto::Cipher::FromCtx(cipher->ctx_).isSupportedAuthenticatedMode());
638+
CHECK(Cipher::FromCtx(cipher->ctx_).isSupportedAuthenticatedMode());
633639
CHECK_NE(cipher->auth_tag_len_, kNoAuthTagLength);
634640
is_valid = cipher->auth_tag_len_ == tag_len;
635641
}
@@ -855,7 +861,7 @@ bool CipherBase::Final(std::unique_ptr<BackingStore>* out) {
855861
}
856862

857863
if (kind_ == kDecipher &&
858-
ncrypto::Cipher::FromCtx(ctx_).isSupportedAuthenticatedMode()) {
864+
Cipher::FromCtx(ctx_).isSupportedAuthenticatedMode()) {
859865
MaybePassAuthTagToOpenSSL();
860866
}
861867

‎src/crypto/crypto_cipher.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class CipherBase : public BaseObject {
8585
CipherBase(Environment* env, v8::Local<v8::Object> wrap, CipherKind kind);
8686

8787
private:
88-
CipherCtxPointer ctx_;
88+
ncrypto::CipherCtxPointer ctx_;
8989
const CipherKind kind_;
9090
AuthTagState auth_tag_state_;
9191
unsigned int auth_tag_len_;
@@ -110,7 +110,7 @@ class PublicKeyCipher {
110110
EVP_PKEY_cipher_init_t EVP_PKEY_cipher_init,
111111
EVP_PKEY_cipher_t EVP_PKEY_cipher>
112112
static bool Cipher(Environment* env,
113-
const EVPKeyPointer& pkey,
113+
const ncrypto::EVPKeyPointer& pkey,
114114
int padding,
115115
const EVP_MD* digest,
116116
const ArrayBufferOrViewContents<unsigned char>& oaep_label,

‎src/crypto/crypto_common.cc

+11-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@
2727

2828
namespace node {
2929

30+
using ncrypto::ClearErrorOnReturn;
31+
using ncrypto::EVPKeyPointer;
32+
using ncrypto::SSLPointer;
33+
using ncrypto::SSLSessionPointer;
3034
using ncrypto::StackOfX509;
35+
using ncrypto::X509Pointer;
36+
using ncrypto::X509View;
3137
using v8::ArrayBuffer;
3238
using v8::BackingStore;
3339
using v8::Context;
@@ -135,7 +141,7 @@ MaybeLocal<Object> AddIssuerChainToObject(X509Pointer* cert,
135141
for (;;) {
136142
int i;
137143
for (i = 0; i < sk_X509_num(peer_certs.get()); i++) {
138-
ncrypto::X509View ca(sk_X509_value(peer_certs.get(), i));
144+
X509View ca(sk_X509_value(peer_certs.get(), i));
139145
if (!cert->view().isIssuedBy(ca)) continue;
140146

141147
Local<Value> ca_info;
@@ -243,7 +249,7 @@ MaybeLocal<Object> GetEphemeralKey(Environment* env, const SSLPointer& ssl) {
243249

244250
EscapableHandleScope scope(env->isolate());
245251
Local<Object> info = Object::New(env->isolate());
246-
crypto::EVPKeyPointer key = ssl.getPeerTempKey();
252+
EVPKeyPointer key = ssl.getPeerTempKey();
247253
if (!key) return scope.Escape(info);
248254

249255
Local<Context> context = env->context();
@@ -341,8 +347,8 @@ MaybeLocal<Value> GetPeerCert(
341347
if (cert) {
342348
return X509Certificate::toObject(env, cert.view());
343349
}
344-
return X509Certificate::toObject(
345-
env, ncrypto::X509View(sk_X509_value(ssl_certs, 0)));
350+
return X509Certificate::toObject(env,
351+
X509View(sk_X509_value(ssl_certs, 0)));
346352
}
347353

348354
StackOfX509 peer_certs = CloneSSLCerts(std::move(cert), ssl_certs);
@@ -351,7 +357,7 @@ MaybeLocal<Value> GetPeerCert(
351357

352358
// First and main certificate.
353359
Local<Value> result;
354-
ncrypto::X509View first_cert(sk_X509_value(peer_certs.get(), 0));
360+
X509View first_cert(sk_X509_value(peer_certs.get(), 0));
355361
CHECK(first_cert);
356362
if (!X509Certificate::toObject(env, first_cert).ToLocal(&result)) return {};
357363
CHECK(result->IsObject());

‎src/crypto/crypto_common.h

+18-18
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,35 @@
2222
namespace node {
2323
namespace crypto {
2424

25-
SSLSessionPointer GetTLSSession(const unsigned char* buf, size_t length);
25+
ncrypto::SSLSessionPointer GetTLSSession(const unsigned char* buf,
26+
size_t length);
2627

2728
long VerifyPeerCertificate( // NOLINT(runtime/int)
28-
const SSLPointer& ssl,
29+
const ncrypto::SSLPointer& ssl,
2930
long def = X509_V_ERR_UNSPECIFIED); // NOLINT(runtime/int)
3031

31-
bool UseSNIContext(const SSLPointer& ssl, BaseObjectPtr<SecureContext> context);
32+
bool UseSNIContext(const ncrypto::SSLPointer& ssl,
33+
BaseObjectPtr<SecureContext> context);
3234

3335
bool SetGroups(SecureContext* sc, const char* groups);
3436

3537
v8::MaybeLocal<v8::Value> GetValidationErrorReason(Environment* env, int err);
3638

3739
v8::MaybeLocal<v8::Value> GetValidationErrorCode(Environment* env, int err);
3840

39-
v8::MaybeLocal<v8::Value> GetCert(Environment* env, const SSLPointer& ssl);
41+
v8::MaybeLocal<v8::Value> GetCert(Environment* env,
42+
const ncrypto::SSLPointer& ssl);
4043

41-
v8::MaybeLocal<v8::Object> GetCipherInfo(
42-
Environment* env,
43-
const SSLPointer& ssl);
44+
v8::MaybeLocal<v8::Object> GetCipherInfo(Environment* env,
45+
const ncrypto::SSLPointer& ssl);
4446

45-
v8::MaybeLocal<v8::Object> GetEphemeralKey(
46-
Environment* env,
47-
const SSLPointer& ssl);
47+
v8::MaybeLocal<v8::Object> GetEphemeralKey(Environment* env,
48+
const ncrypto::SSLPointer& ssl);
4849

49-
v8::MaybeLocal<v8::Value> GetPeerCert(
50-
Environment* env,
51-
const SSLPointer& ssl,
52-
bool abbreviated = false,
53-
bool is_server = false);
50+
v8::MaybeLocal<v8::Value> GetPeerCert(Environment* env,
51+
const ncrypto::SSLPointer& ssl,
52+
bool abbreviated = false,
53+
bool is_server = false);
5454

5555
v8::MaybeLocal<v8::Object> ECPointToBuffer(
5656
Environment* env,
@@ -60,9 +60,9 @@ v8::MaybeLocal<v8::Object> ECPointToBuffer(
6060
const char** error);
6161

6262
v8::MaybeLocal<v8::Value> GetCurrentCipherName(Environment* env,
63-
const SSLPointer& ssl);
64-
v8::MaybeLocal<v8::Value> GetCurrentCipherVersion(Environment* env,
65-
const SSLPointer& ssl);
63+
const ncrypto::SSLPointer& ssl);
64+
v8::MaybeLocal<v8::Value> GetCurrentCipherVersion(
65+
Environment* env, const ncrypto::SSLPointer& ssl);
6666

6767
} // namespace crypto
6868
} // namespace node

‎src/crypto/crypto_context.cc

+16-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@
2121

2222
namespace node {
2323

24+
using ncrypto::BignumPointer;
25+
using ncrypto::BIOPointer;
26+
using ncrypto::ClearErrorOnReturn;
27+
using ncrypto::CryptoErrorList;
28+
using ncrypto::DHPointer;
29+
using ncrypto::EnginePointer;
30+
using ncrypto::EVPKeyPointer;
31+
using ncrypto::MarkPopErrorOnReturn;
32+
using ncrypto::SSLPointer;
2433
using ncrypto::StackOfX509;
34+
using ncrypto::X509Pointer;
2535
using v8::Array;
2636
using v8::ArrayBufferView;
2737
using v8::Boolean;
@@ -693,10 +703,10 @@ void SecureContext::SetEngineKey(const FunctionCallbackInfo<Value>& args) {
693703
"experimental permission model is enabled");
694704
}
695705

696-
ncrypto::CryptoErrorList errors;
706+
CryptoErrorList errors;
697707
Utf8Value engine_id(env->isolate(), args[1]);
698-
auto engine = ncrypto::EnginePointer::getEngineByName(
699-
engine_id.ToStringView(), &errors);
708+
auto engine =
709+
EnginePointer::getEngineByName(engine_id.ToStringView(), &errors);
700710
if (!engine) {
701711
Local<Value> exception;
702712
if (errors.empty()) {
@@ -1205,10 +1215,10 @@ void SecureContext::SetClientCertEngine(
12051215
"experimental permission model is enabled");
12061216
}
12071217

1208-
ncrypto::CryptoErrorList errors;
1218+
CryptoErrorList errors;
12091219
const Utf8Value engine_id(env->isolate(), args[0]);
1210-
auto engine = ncrypto::EnginePointer::getEngineByName(
1211-
engine_id.ToStringView(), &errors);
1220+
auto engine =
1221+
EnginePointer::getEngineByName(engine_id.ToStringView(), &errors);
12121222
if (!engine) {
12131223
Local<Value> exception;
12141224
if (errors.empty()) {

‎src/crypto/crypto_context.h

+15-15
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ X509_STORE* NewRootCertStore();
2323

2424
X509_STORE* GetOrCreateRootCertStore();
2525

26-
BIOPointer LoadBIO(Environment* env, v8::Local<v8::Value> v);
26+
ncrypto::BIOPointer LoadBIO(Environment* env, v8::Local<v8::Value> v);
2727

2828
class SecureContext final : public BaseObject {
2929
public:
@@ -41,27 +41,27 @@ class SecureContext final : public BaseObject {
4141
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
4242
static SecureContext* Create(Environment* env);
4343

44-
const SSLCtxPointer& ctx() const { return ctx_; }
44+
const ncrypto::SSLCtxPointer& ctx() const { return ctx_; }
4545

4646
// Non-const ctx() that allows for non-default initialization of
4747
// the SecureContext.
48-
SSLCtxPointer& ctx() { return ctx_; }
48+
ncrypto::SSLCtxPointer& ctx() { return ctx_; }
4949

50-
SSLPointer CreateSSL();
50+
ncrypto::SSLPointer CreateSSL();
5151

5252
void SetGetSessionCallback(GetSessionCb cb);
5353
void SetKeylogCallback(KeylogCb cb);
5454
void SetNewSessionCallback(NewSessionCb cb);
5555
void SetSelectSNIContextCallback(SelectSNIContextCb cb);
5656

57-
inline const X509Pointer& issuer() const { return issuer_; }
58-
inline const X509Pointer& cert() const { return cert_; }
57+
inline const ncrypto::X509Pointer& issuer() const { return issuer_; }
58+
inline const ncrypto::X509Pointer& cert() const { return cert_; }
5959

60-
v8::Maybe<void> AddCert(Environment* env, BIOPointer&& bio);
61-
v8::Maybe<void> SetCRL(Environment* env, const BIOPointer& bio);
60+
v8::Maybe<void> AddCert(Environment* env, ncrypto::BIOPointer&& bio);
61+
v8::Maybe<void> SetCRL(Environment* env, const ncrypto::BIOPointer& bio);
6262
v8::Maybe<void> UseKey(Environment* env, const KeyObjectData& key);
6363

64-
void SetCACert(const BIOPointer& bio);
64+
void SetCACert(const ncrypto::BIOPointer& bio);
6565
void SetRootCerts();
6666

6767
void SetX509StoreFlag(unsigned long flags); // NOLINT(runtime/int)
@@ -144,9 +144,9 @@ class SecureContext final : public BaseObject {
144144
void Reset();
145145

146146
private:
147-
SSLCtxPointer ctx_;
148-
X509Pointer cert_;
149-
X509Pointer issuer_;
147+
ncrypto::SSLCtxPointer ctx_;
148+
ncrypto::X509Pointer cert_;
149+
ncrypto::X509Pointer issuer_;
150150
// Non-owning cache for SSL_CTX_get_cert_store(ctx_.get())
151151
X509_STORE* own_cert_store_cache_ = nullptr;
152152
#ifndef OPENSSL_NO_ENGINE
@@ -160,9 +160,9 @@ class SecureContext final : public BaseObject {
160160
};
161161

162162
int SSL_CTX_use_certificate_chain(SSL_CTX* ctx,
163-
BIOPointer&& in,
164-
X509Pointer* cert,
165-
X509Pointer* issuer);
163+
ncrypto::BIOPointer&& in,
164+
ncrypto::X509Pointer* cert,
165+
ncrypto::X509Pointer* issuer);
166166

167167
} // namespace crypto
168168
} // namespace node

0 commit comments

Comments
 (0)
Please sign in to comment.