Skip to content

Commit 51b8655

Browse files
RaisinTendanielleadams
authored andcommitted
src,crypto: remove AllocatedBuffers from crypto_spkac
Signed-off-by: Darshan Sen <[email protected]> PR-URL: #40752 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent c5d0b3c commit 51b8655

File tree

3 files changed

+18
-24
lines changed

3 files changed

+18
-24
lines changed

src/crypto/crypto_spkac.cc

+11-24
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "crypto/crypto_spkac.h"
22
#include "crypto/crypto_common.h"
33
#include "crypto/crypto_util.h"
4-
#include "allocated_buffer-inl.h"
54
#include "env-inl.h"
65
#include "memory_tracker-inl.h"
76
#include "node.h"
@@ -41,48 +40,36 @@ void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
4140
args.GetReturnValue().Set(VerifySpkac(input));
4241
}
4342

44-
AllocatedBuffer ExportPublicKey(Environment* env,
45-
const ArrayBufferOrViewContents<char>& input,
46-
size_t* size) {
43+
ByteSource ExportPublicKey(Environment* env,
44+
const ArrayBufferOrViewContents<char>& input) {
4745
BIOPointer bio(BIO_new(BIO_s_mem()));
48-
if (!bio) return AllocatedBuffer();
46+
if (!bio) return ByteSource();
4947

5048
NetscapeSPKIPointer spki(
5149
NETSCAPE_SPKI_b64_decode(input.data(), input.size()));
52-
if (!spki) return AllocatedBuffer();
50+
if (!spki) return ByteSource();
5351

5452
EVPKeyPointer pkey(NETSCAPE_SPKI_get_pubkey(spki.get()));
55-
if (!pkey) return AllocatedBuffer();
53+
if (!pkey) return ByteSource();
5654

57-
if (PEM_write_bio_PUBKEY(bio.get(), pkey.get()) <= 0)
58-
return AllocatedBuffer();
55+
if (PEM_write_bio_PUBKEY(bio.get(), pkey.get()) <= 0) return ByteSource();
5956

60-
BUF_MEM* ptr;
61-
BIO_get_mem_ptr(bio.get(), &ptr);
62-
63-
*size = ptr->length;
64-
AllocatedBuffer buf = AllocatedBuffer::AllocateManaged(env, *size);
65-
memcpy(buf.data(), ptr->data, *size);
66-
67-
return buf;
57+
return ByteSource::FromBIO(bio);
6858
}
6959

7060
void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
7161
Environment* env = Environment::GetCurrent(args);
7262

7363
ArrayBufferOrViewContents<char> input(args[0]);
74-
if (input.size() == 0)
75-
return args.GetReturnValue().SetEmptyString();
64+
if (input.size() == 0) return args.GetReturnValue().SetEmptyString();
7665

7766
if (UNLIKELY(!input.CheckSizeInt32()))
7867
return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large");
7968

80-
size_t pkey_size;
81-
AllocatedBuffer pkey = ExportPublicKey(env, input, &pkey_size);
82-
if (pkey.data() == nullptr)
83-
return args.GetReturnValue().SetEmptyString();
69+
ByteSource pkey = ExportPublicKey(env, input);
70+
if (!pkey) return args.GetReturnValue().SetEmptyString();
8471

85-
args.GetReturnValue().Set(pkey.ToBuffer().FromMaybe(Local<Value>()));
72+
args.GetReturnValue().Set(pkey.ToBuffer(env).FromMaybe(Local<Value>()));
8673
}
8774

8875
ByteSource ExportChallenge(const ArrayBufferOrViewContents<char>& input) {

src/crypto/crypto_util.cc

+5
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ Local<ArrayBuffer> ByteSource::ToArrayBuffer(Environment* env) {
340340
return ArrayBuffer::New(env->isolate(), std::move(store));
341341
}
342342

343+
MaybeLocal<Uint8Array> ByteSource::ToBuffer(Environment* env) {
344+
Local<ArrayBuffer> ab = ToArrayBuffer(env);
345+
return Buffer::New(env, ab, 0, ab->ByteLength());
346+
}
347+
343348
const char* ByteSource::get() const {
344349
return data_;
345350
}

src/crypto/crypto_util.h

+2
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ class ByteSource {
259259

260260
v8::Local<v8::ArrayBuffer> ToArrayBuffer(Environment* env);
261261

262+
v8::MaybeLocal<v8::Uint8Array> ToBuffer(Environment* env);
263+
262264
void reset();
263265

264266
// Allows an Allocated ByteSource to be truncated.

0 commit comments

Comments
 (0)