Skip to content

Commit

Permalink
src,crypto: remove AllocatedBuffers from crypto_spkac
Browse files Browse the repository at this point in the history
Signed-off-by: Darshan Sen <darshan.sen@postman.com>

PR-URL: #40752
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
  • Loading branch information
RaisinTen authored and targos committed Nov 21, 2021
1 parent 727b34e commit 82b4226
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
35 changes: 11 additions & 24 deletions src/crypto/crypto_spkac.cc
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions src/crypto/crypto_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
}
Expand Down
2 changes: 2 additions & 0 deletions src/crypto/crypto_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 82b4226

Please sign in to comment.