Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src,crypto: remove AllocatedBuffers from crypto_spkac #40752

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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