diff --git a/src/crypto/crypto_spkac.cc b/src/crypto/crypto_spkac.cc index db58fe4a11b4ad..ed0934513259d3 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& args) { args.GetReturnValue().Set(VerifySpkac(input)); } -AllocatedBuffer ExportPublicKey(Environment* env, - const ArrayBufferOrViewContents& input, - size_t* size) { +ByteSource ExportPublicKey(Environment* env, + const ArrayBufferOrViewContents& 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& args) { Environment* env = Environment::GetCurrent(args); ArrayBufferOrViewContents 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())); + args.GetReturnValue().Set(pkey.ToBuffer(env).FromMaybe(Local())); } ByteSource ExportChallenge(const ArrayBufferOrViewContents& input) { diff --git a/src/crypto/crypto_util.cc b/src/crypto/crypto_util.cc index d175566d603691..e1a6c41c91dccb 100644 --- a/src/crypto/crypto_util.cc +++ b/src/crypto/crypto_util.cc @@ -350,6 +350,11 @@ Local ByteSource::ToArrayBuffer(Environment* env) { return ArrayBuffer::New(env->isolate(), std::move(store)); } +MaybeLocal ByteSource::ToBuffer(Environment* env) { + Local 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 463a19f516d23f..5060fc3f2fbd67 100644 --- a/src/crypto/crypto_util.h +++ b/src/crypto/crypto_util.h @@ -259,6 +259,8 @@ class ByteSource { v8::Local ToArrayBuffer(Environment* env); + v8::MaybeLocal ToBuffer(Environment* env); + void reset(); // Allows an Allocated ByteSource to be truncated.