33#include "crypto/crypto_util.h"
44#include "env-inl.h"
55#include "memory_tracker-inl.h"
6+ #include "ncrypto.h"
67#include "node.h"
78#include "v8.h"
89
@@ -16,25 +17,6 @@ using v8::Value;
1617
1718namespace crypto {
1819namespace SPKAC {
19- bool VerifySpkac(const ArrayBufferOrViewContents<char>& input) {
20- size_t length = input.size();
21- #ifdef OPENSSL_IS_BORINGSSL
22- // OpenSSL uses EVP_DecodeBlock, which explicitly removes trailing characters,
23- // while BoringSSL uses EVP_DecodedLength and EVP_DecodeBase64, which do not.
24- // As such, we trim those characters here for compatibility.
25- length = std::string(input.data()).find_last_not_of(" \n\r\t") + 1;
26- #endif
27- NetscapeSPKIPointer spki(
28- NETSCAPE_SPKI_b64_decode(input.data(), length));
29- if (!spki)
30- return false;
31-
32- EVPKeyPointer pkey(X509_PUBKEY_get(spki->spkac->pubkey));
33- if (!pkey)
34- return false;
35-
36- return NETSCAPE_SPKI_verify(spki.get(), pkey.get()) > 0;
37- }
3820
3921void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
4022 Environment* env = Environment::GetCurrent(args);
@@ -44,31 +26,7 @@ void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
4426 if (UNLIKELY(!input.CheckSizeInt32()))
4527 return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large");
4628
47- args.GetReturnValue().Set(VerifySpkac(input));
48- }
49-
50- ByteSource ExportPublicKey(Environment* env,
51- const ArrayBufferOrViewContents<char>& input) {
52- BIOPointer bio(BIO_new(BIO_s_mem()));
53- if (!bio) return ByteSource();
54-
55- size_t length = input.size();
56- #ifdef OPENSSL_IS_BORINGSSL
57- // OpenSSL uses EVP_DecodeBlock, which explicitly removes trailing characters,
58- // while BoringSSL uses EVP_DecodedLength and EVP_DecodeBase64, which do not.
59- // As such, we trim those characters here for compatibility.
60- length = std::string(input.data()).find_last_not_of(" \n\r\t") + 1;
61- #endif
62- NetscapeSPKIPointer spki(
63- NETSCAPE_SPKI_b64_decode(input.data(), length));
64- if (!spki) return ByteSource();
65-
66- EVPKeyPointer pkey(NETSCAPE_SPKI_get_pubkey(spki.get()));
67- if (!pkey) return ByteSource();
68-
69- if (PEM_write_bio_PUBKEY(bio.get(), pkey.get()) <= 0) return ByteSource();
70-
71- return ByteSource::FromBIO(bio);
29+ args.GetReturnValue().Set(ncrypto::VerifySpkac(input.data(), input.size()));
7230}
7331
7432void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
@@ -80,30 +38,13 @@ void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
8038 if (UNLIKELY(!input.CheckSizeInt32()))
8139 return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large");
8240
83- ByteSource pkey = ExportPublicKey(env , input);
84- if (!pkey ) return args.GetReturnValue().SetEmptyString();
41+ BIOPointer bio = ncrypto:: ExportPublicKey(input.data() , input.size() );
42+ if (!bio ) return args.GetReturnValue().SetEmptyString();
8543
44+ auto pkey = ByteSource::FromBIO(bio);
8645 args.GetReturnValue().Set(pkey.ToBuffer(env).FromMaybe(Local<Value>()));
8746}
8847
89- ByteSource ExportChallenge(const ArrayBufferOrViewContents<char>& input) {
90- size_t length = input.size();
91- #ifdef OPENSSL_IS_BORINGSSL
92- // OpenSSL uses EVP_DecodeBlock, which explicitly removes trailing characters,
93- // while BoringSSL uses EVP_DecodedLength and EVP_DecodeBase64, which do not.
94- // As such, we trim those characters here for compatibility.
95- length = std::string(input.data()).find_last_not_of(" \n\r\t") + 1;
96- #endif
97- NetscapeSPKIPointer sp(
98- NETSCAPE_SPKI_b64_decode(input.data(), length));
99- if (!sp)
100- return ByteSource();
101-
102- unsigned char* buf = nullptr;
103- int buf_size = ASN1_STRING_to_UTF8(&buf, sp->spkac->challenge);
104- return (buf_size >= 0) ? ByteSource::Allocated(buf, buf_size) : ByteSource();
105- }
106-
10748void ExportChallenge(const FunctionCallbackInfo<Value>& args) {
10849 Environment* env = Environment::GetCurrent(args);
10950
@@ -113,7 +54,8 @@ void ExportChallenge(const FunctionCallbackInfo<Value>& args) {
11354 if (UNLIKELY(!input.CheckSizeInt32()))
11455 return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large");
11556
116- ByteSource cert = ExportChallenge(input);
57+ auto cert = ByteSource::Allocated(
58+ ncrypto::ExportChallenge(input.data(), input.size()));
11759 if (!cert)
11860 return args.GetReturnValue().SetEmptyString();
11961
0 commit comments