Skip to content

Commit 927e0e1

Browse files
committed
src: drop ByteSource::Builder in favor of ncrypto::DataPointer
Signed-off-by: James M Snell <[email protected]>
1 parent 771713c commit 927e0e1

File tree

8 files changed

+62
-92
lines changed

8 files changed

+62
-92
lines changed

deps/ncrypto/ncrypto.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,12 @@ class DataPointer final {
384384

385385
inline bool operator==(std::nullptr_t) noexcept { return data_ == nullptr; }
386386
inline operator bool() const { return data_ != nullptr; }
387-
inline void* get() const noexcept { return data_; }
387+
388+
template <typename T = void>
389+
inline T* get() const noexcept {
390+
return static_cast<T*>(data_);
391+
}
392+
388393
inline size_t size() const noexcept { return len_; }
389394
void reset(void* data = nullptr, size_t len = 0);
390395
void reset(const Buffer<void>& buffer);

src/crypto/README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,6 @@ an `ArrayBuffer` (`v8::BackingStore`), or allocated data.
106106
* If allocated data is used, then it must have been allocated using OpenSSL's
107107
allocator. It will be freed automatically when the `ByteSource` is destroyed.
108108

109-
The `ByteSource::Builder` class can be used to allocate writable memory that can
110-
then be released as a `ByteSource`, making it read-only, or freed by destroying
111-
the `ByteSource::Builder` without releasing it as a `ByteSource`.
112-
113109
### `ArrayBufferOrViewContents`
114110

115111
The `ArrayBufferOrViewContents` class is a helper utility that abstracts

src/crypto/crypto_ec.cc

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -483,15 +483,16 @@ bool ECDHBitsTraits::DeriveBits(Environment* env,
483483
const auto pub = ECKeyPointer::GetPublicKey(public_key);
484484
int field_size = EC_GROUP_get_degree(group);
485485
len = (field_size + 7) / 8;
486-
ByteSource::Builder buf(len);
486+
auto buf = DataPointer::Alloc(len);
487487
CHECK_NOT_NULL(pub);
488488
CHECK_NOT_NULL(private_key);
489-
if (ECDH_compute_key(buf.data<char>(), len, pub, private_key, nullptr) <=
489+
if (ECDH_compute_key(
490+
static_cast<char*>(buf.get()), len, pub, private_key, nullptr) <=
490491
0) {
491492
return false;
492493
}
493494

494-
*out = std::move(buf).release();
495+
*out = ByteSource::Allocated(buf.release());
495496
}
496497
}
497498

@@ -605,14 +606,19 @@ WebCryptoKeyExportStatus EC_Raw_Export(const KeyObjectData& key_data,
605606
size_t len = EC_POINT_point2oct(group, point, form, nullptr, 0, nullptr);
606607
if (len == 0)
607608
return WebCryptoKeyExportStatus::FAILED;
608-
ByteSource::Builder data(len);
609-
size_t check_len = EC_POINT_point2oct(
610-
group, point, form, data.data<unsigned char>(), len, nullptr);
609+
auto data = DataPointer::Alloc(len);
610+
size_t check_len =
611+
EC_POINT_point2oct(group,
612+
point,
613+
form,
614+
static_cast<unsigned char*>(data.get()),
615+
len,
616+
nullptr);
611617
if (check_len == 0)
612618
return WebCryptoKeyExportStatus::FAILED;
613619

614620
CHECK_EQ(len, check_len);
615-
*out = std::move(data).release();
621+
*out = ByteSource::Allocated(data.release());
616622
}
617623

618624
return WebCryptoKeyExportStatus::OK;
@@ -660,15 +666,20 @@ WebCryptoKeyExportStatus ECKeyExportTraits::DoExport(
660666
const size_t need =
661667
EC_POINT_point2oct(group, point, form, nullptr, 0, nullptr);
662668
if (need == 0) return WebCryptoKeyExportStatus::FAILED;
663-
ByteSource::Builder data(need);
664-
const size_t have = EC_POINT_point2oct(
665-
group, point, form, data.data<unsigned char>(), need, nullptr);
669+
auto data = DataPointer::Alloc(need);
670+
const size_t have =
671+
EC_POINT_point2oct(group,
672+
point,
673+
form,
674+
static_cast<unsigned char*>(data.get()),
675+
need,
676+
nullptr);
666677
if (have == 0) return WebCryptoKeyExportStatus::FAILED;
667678
auto ec = ECKeyPointer::New(group);
668679
CHECK(ec);
669680
auto uncompressed = ECPointPointer::New(group);
670681
ncrypto::Buffer<const unsigned char> buffer{
671-
.data = data.data<unsigned char>(),
682+
.data = static_cast<unsigned char*>(data.get()),
672683
.len = data.size(),
673684
};
674685
CHECK(uncompressed.setFromBuffer(buffer, group));

src/crypto/crypto_keygen.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace node {
1414

15+
using ncrypto::DataPointer;
1516
using ncrypto::EVPKeyCtxPointer;
1617
using v8::FunctionCallbackInfo;
1718
using v8::Int32;
@@ -70,10 +71,11 @@ Maybe<void> SecretKeyGenTraits::AdditionalConfig(
7071

7172
KeyGenJobStatus SecretKeyGenTraits::DoKeyGen(Environment* env,
7273
SecretKeyGenConfig* params) {
73-
ByteSource::Builder bytes(params->length);
74-
if (!ncrypto::CSPRNG(bytes.data<unsigned char>(), params->length))
74+
auto bytes = DataPointer::Alloc(params->length);
75+
if (!ncrypto::CSPRNG(static_cast<unsigned char*>(bytes.get()),
76+
params->length))
7577
return KeyGenJobStatus::FAILED;
76-
params->out = std::move(bytes).release();
78+
params->out = ByteSource::Allocated(bytes.release());
7779
return KeyGenJobStatus::OK;
7880
}
7981

src/crypto/crypto_random.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace node {
1313

1414
using ncrypto::BignumPointer;
1515
using ncrypto::ClearErrorOnReturn;
16+
using ncrypto::DataPointer;
1617
using v8::ArrayBuffer;
1718
using v8::Boolean;
1819
using v8::FunctionCallbackInfo;
@@ -196,9 +197,9 @@ bool CheckPrimeTraits::DeriveBits(
196197
int ret = params.candidate.isPrime(params.checks, getPrimeCheckCallback(env));
197198
if (ret < 0) [[unlikely]]
198199
return false;
199-
ByteSource::Builder buf(1);
200-
buf.data<char>()[0] = ret;
201-
*out = std::move(buf).release();
200+
auto buf = DataPointer::Alloc(1);
201+
static_cast<char*>(buf.get())[0] = ret;
202+
*out = ByteSource::Allocated(buf.release());
202203
return true;
203204
}
204205

src/crypto/crypto_sig.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -700,12 +700,12 @@ bool SignTraits::DeriveBits(
700700
break;
701701
}
702702
case SignConfiguration::Mode::Verify: {
703-
ByteSource::Builder buf(1);
704-
buf.data<char>()[0] = 0;
703+
auto buf = DataPointer::Alloc(1);
704+
static_cast<char*>(buf.get())[0] = 0;
705705
if (context.verify(params.data, params.signature)) {
706-
buf.data<char>()[0] = 1;
706+
static_cast<char*>(buf.get())[0] = 1;
707707
}
708-
*out = std::move(buf).release();
708+
*out = ByteSource::Allocated(buf.release());
709709
}
710710
}
711711

src/crypto/crypto_util.cc

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace node {
2929
using ncrypto::BignumPointer;
3030
using ncrypto::BIOPointer;
3131
using ncrypto::CryptoErrorList;
32+
using ncrypto::DataPointer;
3233
using ncrypto::EnginePointer;
3334
using ncrypto::EVPKeyCtxPointer;
3435
using v8::ArrayBuffer;
@@ -363,9 +364,9 @@ MaybeLocal<Uint8Array> ByteSource::ToBuffer(Environment* env) {
363364
ByteSource ByteSource::FromBIO(const BIOPointer& bio) {
364365
CHECK(bio);
365366
BUF_MEM* bptr = bio;
366-
ByteSource::Builder out(bptr->length);
367-
memcpy(out.data<void>(), bptr->data, bptr->length);
368-
return std::move(out).release();
367+
auto out = DataPointer::Alloc(bptr->length);
368+
memcpy(out.get(), bptr->data, bptr->length);
369+
return ByteSource::Allocated(out.release());
369370
}
370371

371372
ByteSource ByteSource::FromEncodedString(Environment* env,
@@ -375,10 +376,10 @@ ByteSource ByteSource::FromEncodedString(Environment* env,
375376
ByteSource out;
376377

377378
if (StringBytes::Size(env->isolate(), key, enc).To(&length) && length > 0) {
378-
ByteSource::Builder buf(length);
379-
size_t actual =
380-
StringBytes::Write(env->isolate(), buf.data<char>(), length, key, enc);
381-
out = std::move(buf).release(actual);
379+
auto buf = DataPointer::Alloc(length);
380+
size_t actual = StringBytes::Write(
381+
env->isolate(), static_cast<char*>(buf.get()), length, key, enc);
382+
out = ByteSource::Allocated(buf.resize(actual).release());
382383
}
383384

384385
return out;
@@ -395,11 +396,12 @@ ByteSource ByteSource::FromString(Environment* env, Local<String> str,
395396
CHECK(str->IsString());
396397
size_t size = str->Utf8Length(env->isolate());
397398
size_t alloc_size = ntc ? size + 1 : size;
398-
ByteSource::Builder out(alloc_size);
399+
auto out = DataPointer::Alloc(alloc_size);
399400
int opts = String::NO_OPTIONS;
400401
if (!ntc) opts |= String::NO_NULL_TERMINATION;
401-
str->WriteUtf8(env->isolate(), out.data<char>(), alloc_size, nullptr, opts);
402-
return std::move(out).release();
402+
str->WriteUtf8(
403+
env->isolate(), static_cast<char*>(out.get()), alloc_size, nullptr, opts);
404+
return ByteSource::Allocated(out.release());
403405
}
404406

405407
ByteSource ByteSource::FromBuffer(Local<Value> buffer, bool ntc) {

src/crypto/crypto_util.h

Lines changed: 9 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -166,53 +166,6 @@ T* MallocOpenSSL(size_t count) {
166166
// contents are zeroed.
167167
class ByteSource final {
168168
public:
169-
class Builder {
170-
public:
171-
// Allocates memory using OpenSSL's memory allocator.
172-
explicit Builder(size_t size)
173-
: data_(MallocOpenSSL<char>(size)), size_(size) {}
174-
175-
Builder(Builder&& other) = delete;
176-
Builder& operator=(Builder&& other) = delete;
177-
Builder(const Builder&) = delete;
178-
Builder& operator=(const Builder&) = delete;
179-
180-
~Builder() { OPENSSL_clear_free(data_, size_); }
181-
182-
// Returns the underlying non-const pointer.
183-
template <typename T>
184-
T* data() {
185-
return reinterpret_cast<T*>(data_);
186-
}
187-
188-
// Returns the (allocated) size in bytes.
189-
size_t size() const { return size_; }
190-
191-
// Returns if (allocated) size is zero.
192-
bool empty() const { return size_ == 0; }
193-
194-
// Finalizes the Builder and returns a read-only view that is optionally
195-
// truncated.
196-
ByteSource release(std::optional<size_t> resize = std::nullopt) && {
197-
if (resize) {
198-
CHECK_LE(*resize, size_);
199-
if (*resize == 0) {
200-
OPENSSL_clear_free(data_, size_);
201-
data_ = nullptr;
202-
}
203-
size_ = *resize;
204-
}
205-
ByteSource out = ByteSource::Allocated(data_, size_);
206-
data_ = nullptr;
207-
size_ = 0;
208-
return out;
209-
}
210-
211-
private:
212-
void* data_;
213-
size_t size_;
214-
};
215-
216169
ByteSource() = default;
217170
ByteSource(ByteSource&& other) noexcept;
218171
~ByteSource();
@@ -651,18 +604,18 @@ class ArrayBufferOrViewContents final {
651604
}
652605

653606
inline ByteSource ToCopy() const {
654-
if (empty()) return ByteSource();
655-
ByteSource::Builder buf(size());
656-
memcpy(buf.data<void>(), data(), size());
657-
return std::move(buf).release();
607+
if (empty()) return {};
608+
auto buf = ncrypto::DataPointer::Alloc(size());
609+
memcpy(buf.get(), data(), size());
610+
return ByteSource::Allocated(buf.release());
658611
}
659612

660613
inline ByteSource ToNullTerminatedCopy() const {
661-
if (empty()) return ByteSource();
662-
ByteSource::Builder buf(size() + 1);
663-
memcpy(buf.data<void>(), data(), size());
664-
buf.data<char>()[size()] = 0;
665-
return std::move(buf).release(size());
614+
if (empty()) return {};
615+
auto buf = ncrypto::DataPointer::Alloc(size() + 1);
616+
memcpy(buf.get(), data(), size());
617+
static_cast<char*>(buf.get())[size()] = 0;
618+
return ByteSource::Allocated(buf.release());
666619
}
667620

668621
inline ncrypto::DataPointer ToDataPointer() const {

0 commit comments

Comments
 (0)