Skip to content

Commit b43b4d6

Browse files
committed
src: move crypto bio stuff to ncrypto
1 parent 9f5f3a9 commit b43b4d6

8 files changed

+67
-36
lines changed

deps/ncrypto/ncrypto.cc

+13-1
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,11 @@ void BIOPointer::reset(BIO* bio) { bio_.reset(bio); }
722722

723723
BIO* BIOPointer::release() { return bio_.release(); }
724724

725+
bool BIOPointer::resetBio() const {
726+
if (!bio_) return 0;
727+
return BIO_reset(bio_.get()) == 1;
728+
}
729+
725730
BIOPointer BIOPointer::NewMem() {
726731
return BIOPointer(BIO_new(BIO_s_mem()));
727732
}
@@ -742,7 +747,14 @@ BIOPointer BIOPointer::NewFile(std::string_view filename, std::string_view mode)
742747
return BIOPointer(BIO_new_file(filename.data(), mode.data()));
743748
}
744749

745-
BIOPointer BIOPointer::NewFd(int fd, int close_flag) {
750+
BIOPointer BIOPointer::NewFp(FILE* fd, int close_flag) {
746751
return BIOPointer(BIO_new_fp(fd, close_flag));
747752
}
753+
754+
int BIOPointer::Write(BIOPointer* bio, std::string_view message) {
755+
if (bio == nullptr || !*bio) return 0;
756+
return BIO_write(bio->get(), message.data(), message.size());
757+
}
758+
759+
748760
} // namespace ncrypto

deps/ncrypto/ncrypto.h

+24-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
#include <optional>
77
#include <string>
88
#include <string_view>
9-
#include "openssl/bn.h"
9+
#include <openssl/bio.h>
10+
#include <openssl/bn.h>
1011
#include <openssl/x509.h>
1112
#include <openssl/dh.h>
1213
#include <openssl/dsa.h>
@@ -250,9 +251,10 @@ class BIOPointer final {
250251
static BIOPointer New(const BIO_METHOD* method);
251252
static BIOPointer New(const void* data, size_t len);
252253
static BIOPointer NewFile(std::string_view filename, std::string_view mode);
253-
static BIOPointer NewFd(int fd, int flags);
254+
static BIOPointer NewFp(FILE* fd, int flags);
254255

255256
BIOPointer() = default;
257+
BIOPointer(std::nullptr_t) : bio_(nullptr) {}
256258
explicit BIOPointer(BIO* bio);
257259
BIOPointer(BIOPointer&& other) noexcept;
258260
BIOPointer& operator=(BIOPointer&& other) noexcept;
@@ -263,11 +265,30 @@ class BIOPointer final {
263265
inline operator bool() const { return bio_ != nullptr; }
264266
inline BIO* get() const noexcept { return bio_.get(); }
265267

268+
inline operator BUF_MEM*() const {
269+
BUF_MEM* mem = nullptr;
270+
if (!bio_) return mem;
271+
BIO_get_mem_ptr(bio_.get(), &mem);
272+
return mem;
273+
}
274+
275+
inline operator BIO*() const { return bio_.get(); }
276+
266277
void reset(BIO* bio = nullptr);
267278
BIO* release();
268279

280+
bool resetBio() const;
281+
282+
static int Write(BIOPointer* bio, std::string_view message);
283+
284+
template <typename...Args>
285+
static void Printf(BIOPointer* bio, const char* format, Args...args) {
286+
if (bio == nullptr || !*bio) return;
287+
BIO_printf(bio->get(), format, std::forward<Args...>(args...));
288+
}
289+
269290
private:
270-
DeleteFnPtr<BIO, BIO_free_all> bio_;
291+
mutable DeleteFnPtr<BIO, BIO_free_all> bio_;
271292
};
272293

273294
class BignumPointer final {

src/crypto/crypto_bio.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ BIOPointer NodeBIO::New(Environment* env) {
4343
BIOPointer NodeBIO::NewFixed(const char* data, size_t len, Environment* env) {
4444
BIOPointer bio = New(env);
4545

46-
if (!bio ||
47-
len > INT_MAX ||
48-
BIO_write(bio.get(), data, len) != static_cast<int>(len) ||
46+
if (!bio || len > INT_MAX ||
47+
BIOPointer::Write(&bio, std::string_view(data, len)) !=
48+
static_cast<int>(len) ||
4949
BIO_set_mem_eof_return(bio.get(), 0) != 1) {
5050
return BIOPointer();
5151
}

src/crypto/crypto_common.cc

+8-8
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,14 @@ MaybeLocal<Value> GetCert(Environment* env, const SSLPointer& ssl) {
272272
}
273273

274274
Local<Value> ToV8Value(Environment* env, const BIOPointer& bio) {
275-
BUF_MEM* mem;
276-
BIO_get_mem_ptr(bio.get(), &mem);
275+
BUF_MEM* mem = bio;
277276
MaybeLocal<String> ret =
278277
String::NewFromUtf8(
279278
env->isolate(),
280279
mem->data,
281280
NewStringType::kNormal,
282281
mem->length);
283-
CHECK_EQ(BIO_reset(bio.get()), 1);
282+
CHECK(bio.resetBio());
284283
return ret.FromMaybe(Local<Value>());
285284
}
286285

@@ -467,7 +466,8 @@ MaybeLocal<Value> GetExponentString(
467466
const BIOPointer& bio,
468467
const BIGNUM* e) {
469468
uint64_t exponent_word = static_cast<uint64_t>(BignumPointer::GetWord(e));
470-
BIO_printf(bio.get(), "0x%" PRIx64, exponent_word);
469+
BIOPointer::Printf(
470+
const_cast<BIOPointer*>(&bio), "0x%" PRIx64, exponent_word);
471471
return ToV8Value(env, bio);
472472
}
473473

@@ -588,7 +588,7 @@ v8::MaybeLocal<v8::Value> GetSubjectAltNameString(Environment* env,
588588
CHECK_NOT_NULL(ext);
589589

590590
if (!ncrypto::SafeX509SubjectAltNamePrint(bio, ext)) {
591-
CHECK_EQ(BIO_reset(bio.get()), 1);
591+
CHECK(bio.resetBio());
592592
return v8::Null(env->isolate());
593593
}
594594

@@ -606,7 +606,7 @@ v8::MaybeLocal<v8::Value> GetInfoAccessString(Environment* env,
606606
CHECK_NOT_NULL(ext);
607607

608608
if (!ncrypto::SafeX509InfoAccessPrint(bio, ext)) {
609-
CHECK_EQ(BIO_reset(bio.get()), 1);
609+
CHECK(bio.resetBio());
610610
return v8::Null(env->isolate());
611611
}
612612

@@ -622,7 +622,7 @@ MaybeLocal<Value> GetIssuerString(Environment* env,
622622
issuer_name,
623623
0,
624624
kX509NameFlagsMultiline) <= 0) {
625-
CHECK_EQ(BIO_reset(bio.get()), 1);
625+
CHECK(bio.resetBio());
626626
return Undefined(env->isolate());
627627
}
628628

@@ -637,7 +637,7 @@ MaybeLocal<Value> GetSubject(Environment* env,
637637
X509_get_subject_name(cert),
638638
0,
639639
kX509NameFlagsMultiline) <= 0) {
640-
CHECK_EQ(BIO_reset(bio.get()), 1);
640+
CHECK(bio.resetBio());
641641
return Undefined(env->isolate());
642642
}
643643

src/crypto/crypto_context.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,13 @@ BIOPointer LoadBIO(Environment* env, Local<Value> v) {
6868
if (!bio) return {};
6969
ByteSource bsrc = ByteSource::FromStringOrBuffer(env, v);
7070
if (bsrc.size() > INT_MAX) return {};
71-
int written = BIO_write(bio.get(), bsrc.data<char>(), bsrc.size());
71+
int written = BIOPointer::Write(
72+
&bio, std::string_view(bsrc.data<char>(), bsrc.size()));
7273
if (written < 0) return {};
7374
if (static_cast<size_t>(written) != bsrc.size()) return {};
7475
return bio;
7576
}
76-
return nullptr;
77+
return {};
7778
}
7879

7980
namespace {
@@ -1012,7 +1013,6 @@ void SecureContext::SetSessionIdContext(
10121013
if (SSL_CTX_set_session_id_context(sc->ctx_.get(), sid_ctx, sid_ctx_len) == 1)
10131014
return;
10141015

1015-
BUF_MEM* mem;
10161016
Local<String> message;
10171017

10181018
auto bio = BIOPointer::NewMem();
@@ -1021,7 +1021,7 @@ void SecureContext::SetSessionIdContext(
10211021
"SSL_CTX_set_session_id_context error");
10221022
} else {
10231023
ERR_print_errors(bio.get());
1024-
BIO_get_mem_ptr(bio.get(), &mem);
1024+
BUF_MEM* mem = bio;
10251025
message = OneByteString(env->isolate(), mem->data, mem->length);
10261026
}
10271027

src/crypto/crypto_keys.cc

+8-10
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ ParseKeyResult ParsePublicKeyPEM(EVPKeyPointer* pkey,
119119
return ret;
120120

121121
// Maybe it is PKCS#1.
122-
CHECK(BIO_reset(bp.get()));
122+
CHECK(bp.resetBio());
123123
ret = TryParsePublicKey(pkey, bp, "RSA PUBLIC KEY",
124124
[](const unsigned char** p, long l) { // NOLINT(runtime/int)
125125
return d2i_PublicKey(EVP_PKEY_RSA, nullptr, p, l);
@@ -128,7 +128,7 @@ ParseKeyResult ParsePublicKeyPEM(EVPKeyPointer* pkey,
128128
return ret;
129129

130130
// X.509 fallback.
131-
CHECK(BIO_reset(bp.get()));
131+
CHECK(bp.resetBio());
132132
return TryParsePublicKey(pkey, bp, "CERTIFICATE",
133133
[](const unsigned char** p, long l) { // NOLINT(runtime/int)
134134
X509Pointer x509(d2i_X509(nullptr, p, l));
@@ -270,12 +270,10 @@ ParseKeyResult ParsePrivateKey(EVPKeyPointer* pkey,
270270
return ParseKeyResult::kParseKeyFailed;
271271
}
272272

273-
MaybeLocal<Value> BIOToStringOrBuffer(
274-
Environment* env,
275-
BIO* bio,
276-
PKFormatType format) {
277-
BUF_MEM* bptr;
278-
BIO_get_mem_ptr(bio, &bptr);
273+
MaybeLocal<Value> BIOToStringOrBuffer(Environment* env,
274+
const BIOPointer& bio,
275+
PKFormatType format) {
276+
BUF_MEM* bptr = bio;
279277
if (format == kKeyFormatPEM) {
280278
// PEM is an ASCII format, so we will return it as a string.
281279
return String::NewFromUtf8(env->isolate(), bptr->data,
@@ -388,7 +386,7 @@ MaybeLocal<Value> WritePrivateKey(Environment* env,
388386
ThrowCryptoError(env, ERR_get_error(), "Failed to encode private key");
389387
return MaybeLocal<Value>();
390388
}
391-
return BIOToStringOrBuffer(env, bio.get(), config.format_);
389+
return BIOToStringOrBuffer(env, bio, config.format_);
392390
}
393391

394392
bool WritePublicKeyInner(OSSL3_CONST EVP_PKEY* pkey,
@@ -429,7 +427,7 @@ MaybeLocal<Value> WritePublicKey(Environment* env,
429427
ThrowCryptoError(env, ERR_get_error(), "Failed to encode public key");
430428
return MaybeLocal<Value>();
431429
}
432-
return BIOToStringOrBuffer(env, bio.get(), config.format_);
430+
return BIOToStringOrBuffer(env, bio, config.format_);
433431
}
434432

435433
Maybe<void> ExportJWKSecretKey(Environment* env,

src/crypto/crypto_tls.cc

+6-5
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
#include "crypto/crypto_tls.h"
23-
#include "crypto/crypto_context.h"
24-
#include "crypto/crypto_common.h"
25-
#include "crypto/crypto_util.h"
23+
#include <cstdio>
24+
#include "async_wrap-inl.h"
2625
#include "crypto/crypto_bio.h"
2726
#include "crypto/crypto_clienthello-inl.h"
28-
#include "async_wrap-inl.h"
27+
#include "crypto/crypto_common.h"
28+
#include "crypto/crypto_context.h"
29+
#include "crypto/crypto_util.h"
2930
#include "debug_utils-inl.h"
3031
#include "memory_tracker-inl.h"
3132
#include "node_buffer.h"
@@ -1244,7 +1245,7 @@ void TLSWrap::EnableTrace(const FunctionCallbackInfo<Value>& args) {
12441245

12451246
#if HAVE_SSL_TRACE
12461247
if (wrap->ssl_) {
1247-
wrap->bio_trace_ = BIOPointer::NewFd(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
1248+
wrap->bio_trace_ = BIOPointer::NewFp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
12481249
SSL_set_msg_callback(wrap->ssl_.get(), [](int write_p, int version, int
12491250
content_type, const void* buf, size_t len, SSL* ssl, void* arg)
12501251
-> void {

src/crypto/crypto_util.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,7 @@ MaybeLocal<Uint8Array> ByteSource::ToBuffer(Environment* env) {
355355

356356
ByteSource ByteSource::FromBIO(const BIOPointer& bio) {
357357
CHECK(bio);
358-
BUF_MEM* bptr;
359-
BIO_get_mem_ptr(bio.get(), &bptr);
358+
BUF_MEM* bptr = bio;
360359
ByteSource::Builder out(bptr->length);
361360
memcpy(out.data<void>(), bptr->data, bptr->length);
362361
return std::move(out).release();

0 commit comments

Comments
 (0)