diff options
author | Darshan Sen <darshan.sen@postman.com> | 2021-11-13 11:14:49 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-13 11:14:49 +0300 |
commit | 3e5a5e8e1afcbf4c2d4a89f0359199d10da072a9 (patch) | |
tree | 8ad4262e5b2c2e31f066a8d7ba800ba7c0f7c10c /src/crypto | |
parent | 9cd30894f6e708475ec56cea63eb8c960df779a4 (diff) |
src,crypto: remove `AllocatedBuffer`s from `crypto_spkac`
Signed-off-by: Darshan Sen <darshan.sen@postman.com>
PR-URL: https://github.com/nodejs/node/pull/40752
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Diffstat (limited to 'src/crypto')
-rw-r--r-- | src/crypto/crypto_spkac.cc | 35 | ||||
-rw-r--r-- | src/crypto/crypto_util.cc | 5 | ||||
-rw-r--r-- | src/crypto/crypto_util.h | 2 |
3 files changed, 18 insertions, 24 deletions
diff --git a/src/crypto/crypto_spkac.cc b/src/crypto/crypto_spkac.cc index db58fe4a11b..ed093451325 100644 --- a/src/crypto/crypto_spkac.cc +++ b/src/crypto/crypto_spkac.cc @@ -1,7 +1,6 @@ #include "crypto/crypto_spkac.h" #include "crypto/crypto_common.h" #include "crypto/crypto_util.h" -#include "allocated_buffer-inl.h" #include "env-inl.h" #include "memory_tracker-inl.h" #include "node.h" @@ -41,48 +40,36 @@ void VerifySpkac(const FunctionCallbackInfo<Value>& args) { args.GetReturnValue().Set(VerifySpkac(input)); } -AllocatedBuffer ExportPublicKey(Environment* env, - const ArrayBufferOrViewContents<char>& input, - size_t* size) { +ByteSource ExportPublicKey(Environment* env, + const ArrayBufferOrViewContents<char>& input) { BIOPointer bio(BIO_new(BIO_s_mem())); - if (!bio) return AllocatedBuffer(); + if (!bio) return ByteSource(); NetscapeSPKIPointer spki( NETSCAPE_SPKI_b64_decode(input.data(), input.size())); - if (!spki) return AllocatedBuffer(); + if (!spki) return ByteSource(); EVPKeyPointer pkey(NETSCAPE_SPKI_get_pubkey(spki.get())); - if (!pkey) return AllocatedBuffer(); + if (!pkey) return ByteSource(); - if (PEM_write_bio_PUBKEY(bio.get(), pkey.get()) <= 0) - return AllocatedBuffer(); + if (PEM_write_bio_PUBKEY(bio.get(), pkey.get()) <= 0) return ByteSource(); - BUF_MEM* ptr; - BIO_get_mem_ptr(bio.get(), &ptr); - - *size = ptr->length; - AllocatedBuffer buf = AllocatedBuffer::AllocateManaged(env, *size); - memcpy(buf.data(), ptr->data, *size); - - return buf; + return ByteSource::FromBIO(bio); } void ExportPublicKey(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); ArrayBufferOrViewContents<char> input(args[0]); - if (input.size() == 0) - return args.GetReturnValue().SetEmptyString(); + if (input.size() == 0) return args.GetReturnValue().SetEmptyString(); if (UNLIKELY(!input.CheckSizeInt32())) return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large"); - size_t pkey_size; - AllocatedBuffer pkey = ExportPublicKey(env, input, &pkey_size); - if (pkey.data() == nullptr) - return args.GetReturnValue().SetEmptyString(); + ByteSource pkey = ExportPublicKey(env, input); + if (!pkey) return args.GetReturnValue().SetEmptyString(); - args.GetReturnValue().Set(pkey.ToBuffer().FromMaybe(Local<Value>())); + args.GetReturnValue().Set(pkey.ToBuffer(env).FromMaybe(Local<Value>())); } ByteSource ExportChallenge(const ArrayBufferOrViewContents<char>& input) { diff --git a/src/crypto/crypto_util.cc b/src/crypto/crypto_util.cc index d175566d603..e1a6c41c91d 100644 --- a/src/crypto/crypto_util.cc +++ b/src/crypto/crypto_util.cc @@ -350,6 +350,11 @@ Local<ArrayBuffer> ByteSource::ToArrayBuffer(Environment* env) { return ArrayBuffer::New(env->isolate(), std::move(store)); } +MaybeLocal<Uint8Array> ByteSource::ToBuffer(Environment* env) { + Local<ArrayBuffer> ab = ToArrayBuffer(env); + return Buffer::New(env, ab, 0, ab->ByteLength()); +} + const char* ByteSource::get() const { return data_; } diff --git a/src/crypto/crypto_util.h b/src/crypto/crypto_util.h index 463a19f516d..5060fc3f2fb 100644 --- a/src/crypto/crypto_util.h +++ b/src/crypto/crypto_util.h @@ -259,6 +259,8 @@ class ByteSource { v8::Local<v8::ArrayBuffer> ToArrayBuffer(Environment* env); + v8::MaybeLocal<v8::Uint8Array> ToBuffer(Environment* env); + void reset(); // Allows an Allocated ByteSource to be truncated. |