Skip to content

Commit 9f5f3a9

Browse files
committed
src: move more crypto code to ncrypto
1 parent 66c8076 commit 9f5f3a9

9 files changed

Lines changed: 89 additions & 21 deletions

File tree

deps/ncrypto/ncrypto.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,4 +703,46 @@ bool SafeX509InfoAccessPrint(const BIOPointer& out, X509_EXTENSION* ext) {
703703
return ok;
704704
}
705705

706+
// ============================================================================
707+
// BIOPointer
708+
709+
BIOPointer::BIOPointer(BIO* bio) : bio_(bio) {}
710+
711+
BIOPointer::BIOPointer(BIOPointer&& other) noexcept : bio_(other.release()) {}
712+
713+
BIOPointer& BIOPointer::operator=(BIOPointer&& other) noexcept {
714+
if (this == &other) return *this;
715+
this->~BIOPointer();
716+
return *new (this) BIOPointer(std::move(other));
717+
}
718+
719+
BIOPointer::~BIOPointer() { reset(); }
720+
721+
void BIOPointer::reset(BIO* bio) { bio_.reset(bio); }
722+
723+
BIO* BIOPointer::release() { return bio_.release(); }
724+
725+
BIOPointer BIOPointer::NewMem() {
726+
return BIOPointer(BIO_new(BIO_s_mem()));
727+
}
728+
729+
BIOPointer BIOPointer::NewSecMem() {
730+
return BIOPointer(BIO_new(BIO_s_secmem()));
731+
}
732+
733+
BIOPointer BIOPointer::New(const BIO_METHOD* method) {
734+
return BIOPointer(BIO_new(method));
735+
}
736+
737+
BIOPointer BIOPointer::New(const void* data, size_t len) {
738+
return BIOPointer(BIO_new_mem_buf(data, len));
739+
}
740+
741+
BIOPointer BIOPointer::NewFile(std::string_view filename, std::string_view mode) {
742+
return BIOPointer(BIO_new_file(filename.data(), mode.data()));
743+
}
744+
745+
BIOPointer BIOPointer::NewFd(int fd, int close_flag) {
746+
return BIOPointer(BIO_new_fp(fd, close_flag));
747+
}
706748
} // namespace ncrypto

deps/ncrypto/ncrypto.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ template <typename T, void (*function)(T*)>
177177
using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer;
178178

179179
using BignumCtxPointer = DeleteFnPtr<BN_CTX, BN_CTX_free>;
180-
using BIOPointer = DeleteFnPtr<BIO, BIO_free_all>;
181180
using CipherCtxPointer = DeleteFnPtr<EVP_CIPHER_CTX, EVP_CIPHER_CTX_free>;
182181
using DHPointer = DeleteFnPtr<DH, DH_free>;
183182
using DSAPointer = DeleteFnPtr<DSA, DSA_free>;
@@ -244,6 +243,33 @@ class DataPointer final {
244243
size_t len_ = 0;
245244
};
246245

246+
class BIOPointer final {
247+
public:
248+
static BIOPointer NewMem();
249+
static BIOPointer NewSecMem();
250+
static BIOPointer New(const BIO_METHOD* method);
251+
static BIOPointer New(const void* data, size_t len);
252+
static BIOPointer NewFile(std::string_view filename, std::string_view mode);
253+
static BIOPointer NewFd(int fd, int flags);
254+
255+
BIOPointer() = default;
256+
explicit BIOPointer(BIO* bio);
257+
BIOPointer(BIOPointer&& other) noexcept;
258+
BIOPointer& operator=(BIOPointer&& other) noexcept;
259+
NCRYPTO_DISALLOW_COPY(BIOPointer)
260+
~BIOPointer();
261+
262+
inline bool operator==(std::nullptr_t) noexcept { return bio_ == nullptr; }
263+
inline operator bool() const { return bio_ != nullptr; }
264+
inline BIO* get() const noexcept { return bio_.get(); }
265+
266+
void reset(BIO* bio = nullptr);
267+
BIO* release();
268+
269+
private:
270+
DeleteFnPtr<BIO, BIO_free_all> bio_;
271+
};
272+
247273
class BignumPointer final {
248274
public:
249275
BignumPointer() = default;

src/crypto/crypto_bio.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace node {
3333
namespace crypto {
3434

3535
BIOPointer NodeBIO::New(Environment* env) {
36-
BIOPointer bio(BIO_new(GetMethod()));
36+
auto bio = BIOPointer::New(GetMethod());
3737
if (bio && env != nullptr)
3838
NodeBIO::FromBIO(bio.get())->env_ = env;
3939
return bio;

src/crypto/crypto_common.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ MaybeLocal<Object> X509ToObject(
967967
Local<Context> context = env->context();
968968
Local<Object> info = Object::New(env->isolate());
969969

970-
BIOPointer bio(BIO_new(BIO_s_mem()));
970+
auto bio = BIOPointer::NewMem();
971971
CHECK(bio);
972972

973973
// X509_check_ca() returns a range of values. Only 1 means "is a CA"

src/crypto/crypto_context.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ X509_STORE* GetOrCreateRootCertStore() {
6464
// Caller responsible for BIO_free_all-ing the returned object.
6565
BIOPointer LoadBIO(Environment* env, Local<Value> v) {
6666
if (v->IsString() || v->IsArrayBufferView()) {
67-
BIOPointer bio(BIO_new(BIO_s_secmem()));
68-
if (!bio) return nullptr;
67+
auto bio = BIOPointer::NewSecMem();
68+
if (!bio) return {};
6969
ByteSource bsrc = ByteSource::FromStringOrBuffer(env, v);
70-
if (bsrc.size() > INT_MAX) return nullptr;
70+
if (bsrc.size() > INT_MAX) return {};
7171
int written = BIO_write(bio.get(), bsrc.data<char>(), bsrc.size());
72-
if (written < 0) return nullptr;
73-
if (static_cast<size_t>(written) != bsrc.size()) return nullptr;
72+
if (written < 0) return {};
73+
if (static_cast<size_t>(written) != bsrc.size()) return {};
7474
return bio;
7575
}
7676
return nullptr;
@@ -202,7 +202,7 @@ unsigned long LoadCertsFromFile( // NOLINT(runtime/int)
202202
const char* file) {
203203
MarkPopErrorOnReturn mark_pop_error_on_return;
204204

205-
BIOPointer bio(BIO_new_file(file, "r"));
205+
auto bio = BIOPointer::NewFile(file, "r");
206206
if (!bio) return ERR_get_error();
207207

208208
while (X509* x509 = PEM_read_bio_X509(
@@ -1015,7 +1015,7 @@ void SecureContext::SetSessionIdContext(
10151015
BUF_MEM* mem;
10161016
Local<String> message;
10171017

1018-
BIOPointer bio(BIO_new(BIO_s_mem()));
1018+
auto bio = BIOPointer::NewMem();
10191019
if (!bio) {
10201020
message = FIXED_ONE_BYTE_STRING(env->isolate(),
10211021
"SSL_CTX_set_session_id_context error");

src/crypto/crypto_ec.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ WebCryptoKeyExportStatus ECKeyExportTraits::DoExport(
736736
CHECK_EQ(1, EC_KEY_set_public_key(ec.get(), uncompressed.get()));
737737
EVPKeyPointer pkey(EVP_PKEY_new());
738738
CHECK_EQ(1, EVP_PKEY_set1_EC_KEY(pkey.get(), ec.get()));
739-
BIOPointer bio(BIO_new(BIO_s_mem()));
739+
auto bio = BIOPointer::NewMem();
740740
CHECK(bio);
741741
if (!i2d_PUBKEY_bio(bio.get(), pkey.get()))
742742
return WebCryptoKeyExportStatus::FAILED;

src/crypto/crypto_keys.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ ParseKeyResult TryParsePublicKey(EVPKeyPointer* pkey,
104104
ParseKeyResult ParsePublicKeyPEM(EVPKeyPointer* pkey,
105105
const char* key_pem,
106106
int key_pem_len) {
107-
BIOPointer bp(BIO_new_mem_buf(const_cast<char*>(key_pem), key_pem_len));
107+
auto bp = BIOPointer::New(key_pem, key_pem_len);
108108
if (!bp)
109109
return ParseKeyResult::kParseKeyFailed;
110110

@@ -218,7 +218,7 @@ ParseKeyResult ParsePrivateKey(EVPKeyPointer* pkey,
218218
const ByteSource* passphrase = config.passphrase_.get();
219219

220220
if (config.format_ == kKeyFormatPEM) {
221-
BIOPointer bio(BIO_new_mem_buf(key, key_len));
221+
auto bio = BIOPointer::New(key, key_len);
222222
if (!bio)
223223
return ParseKeyResult::kParseKeyFailed;
224224

@@ -233,7 +233,7 @@ ParseKeyResult ParsePrivateKey(EVPKeyPointer* pkey,
233233
const unsigned char* p = reinterpret_cast<const unsigned char*>(key);
234234
pkey->reset(d2i_PrivateKey(EVP_PKEY_RSA, nullptr, &p, key_len));
235235
} else if (config.type_.ToChecked() == kKeyEncodingPKCS8) {
236-
BIOPointer bio(BIO_new_mem_buf(key, key_len));
236+
auto bio = BIOPointer::New(key, key_len);
237237
if (!bio)
238238
return ParseKeyResult::kParseKeyFailed;
239239

@@ -292,7 +292,7 @@ MaybeLocal<Value> BIOToStringOrBuffer(
292292
MaybeLocal<Value> WritePrivateKey(Environment* env,
293293
OSSL3_CONST EVP_PKEY* pkey,
294294
const PrivateKeyEncodingConfig& config) {
295-
BIOPointer bio(BIO_new(BIO_s_mem()));
295+
auto bio = BIOPointer::NewMem();
296296
CHECK(bio);
297297

298298
// If an empty string was passed as the passphrase, the ByteSource might
@@ -422,7 +422,7 @@ bool WritePublicKeyInner(OSSL3_CONST EVP_PKEY* pkey,
422422
MaybeLocal<Value> WritePublicKey(Environment* env,
423423
OSSL3_CONST EVP_PKEY* pkey,
424424
const PublicKeyEncodingConfig& config) {
425-
BIOPointer bio(BIO_new(BIO_s_mem()));
425+
auto bio = BIOPointer::NewMem();
426426
CHECK(bio);
427427

428428
if (!WritePublicKeyInner(pkey, bio, config)) {
@@ -1448,7 +1448,7 @@ WebCryptoKeyExportStatus PKEY_SPKI_Export(
14481448
CHECK_EQ(key_data->GetKeyType(), kKeyTypePublic);
14491449
ManagedEVPPKey m_pkey = key_data->GetAsymmetricKey();
14501450
Mutex::ScopedLock lock(*m_pkey.mutex());
1451-
BIOPointer bio(BIO_new(BIO_s_mem()));
1451+
auto bio = BIOPointer::NewMem();
14521452
CHECK(bio);
14531453
if (!i2d_PUBKEY_bio(bio.get(), m_pkey.get()))
14541454
return WebCryptoKeyExportStatus::FAILED;
@@ -1464,7 +1464,7 @@ WebCryptoKeyExportStatus PKEY_PKCS8_Export(
14641464
ManagedEVPPKey m_pkey = key_data->GetAsymmetricKey();
14651465
Mutex::ScopedLock lock(*m_pkey.mutex());
14661466

1467-
BIOPointer bio(BIO_new(BIO_s_mem()));
1467+
auto bio = BIOPointer::NewMem();
14681468
CHECK(bio);
14691469
PKCS8Pointer p8inf(EVP_PKEY2PKCS8(m_pkey.get()));
14701470
if (!i2d_PKCS8_PRIV_KEY_INFO_bio(bio.get(), p8inf.get()))

src/crypto/crypto_tls.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ void TLSWrap::EnableTrace(const FunctionCallbackInfo<Value>& args) {
12441244

12451245
#if HAVE_SSL_TRACE
12461246
if (wrap->ssl_) {
1247-
wrap->bio_trace_.reset(BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT));
1247+
wrap->bio_trace_ = BIOPointer::NewFd(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
12481248
SSL_set_msg_callback(wrap->ssl_.get(), [](int write_p, int version, int
12491249
content_type, const void* buf, size_t len, SSL* ssl, void* arg)
12501250
-> void {

src/crypto/crypto_x509.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ static void ReturnPropertyThroughBIO(const FunctionCallbackInfo<Value>& args) {
209209
Environment* env = Environment::GetCurrent(args);
210210
X509Certificate* cert;
211211
ASSIGN_OR_RETURN_UNWRAP(&cert, args.This());
212-
BIOPointer bio(BIO_new(BIO_s_mem()));
212+
auto bio = BIOPointer::NewMem();
213213
CHECK(bio);
214214
Local<Value> ret;
215215
if (Property(env, cert->get(), bio).ToLocal(&ret))
@@ -284,7 +284,7 @@ void X509Certificate::Pem(const FunctionCallbackInfo<Value>& args) {
284284
Environment* env = Environment::GetCurrent(args);
285285
X509Certificate* cert;
286286
ASSIGN_OR_RETURN_UNWRAP(&cert, args.This());
287-
BIOPointer bio(BIO_new(BIO_s_mem()));
287+
auto bio = BIOPointer::NewMem();
288288
CHECK(bio);
289289
if (PEM_write_bio_X509(bio.get(), cert->get()))
290290
args.GetReturnValue().Set(ToV8Value(env, bio));

0 commit comments

Comments
 (0)