3
3
#include " crypto/crypto_util.h"
4
4
#include " env-inl.h"
5
5
#include " memory_tracker-inl.h"
6
+ #include " ncrypto.h"
6
7
#include " node.h"
7
8
#include " v8.h"
8
9
@@ -16,25 +17,6 @@ using v8::Value;
16
17
17
18
namespace crypto {
18
19
namespace 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
- }
38
20
39
21
void VerifySpkac (const FunctionCallbackInfo<Value>& args) {
40
22
Environment* env = Environment::GetCurrent (args);
@@ -44,31 +26,7 @@ void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
44
26
if (UNLIKELY (!input.CheckSizeInt32 ()))
45
27
return THROW_ERR_OUT_OF_RANGE (env, " spkac is too large" );
46
28
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 ()));
72
30
}
73
31
74
32
void ExportPublicKey (const FunctionCallbackInfo<Value>& args) {
@@ -80,30 +38,13 @@ void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
80
38
if (UNLIKELY (!input.CheckSizeInt32 ()))
81
39
return THROW_ERR_OUT_OF_RANGE (env, " spkac is too large" );
82
40
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 ();
85
43
44
+ auto pkey = ByteSource::FromBIO (bio);
86
45
args.GetReturnValue ().Set (pkey.ToBuffer (env).FromMaybe (Local<Value>()));
87
46
}
88
47
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
-
107
48
void ExportChallenge (const FunctionCallbackInfo<Value>& args) {
108
49
Environment* env = Environment::GetCurrent (args);
109
50
@@ -113,7 +54,8 @@ void ExportChallenge(const FunctionCallbackInfo<Value>& args) {
113
54
if (UNLIKELY (!input.CheckSizeInt32 ()))
114
55
return THROW_ERR_OUT_OF_RANGE (env, " spkac is too large" );
115
56
116
- ByteSource cert = ExportChallenge (input);
57
+ auto cert = ByteSource::Allocated (
58
+ ncrypto::ExportChallenge (input.data (), input.size ()));
117
59
if (!cert)
118
60
return args.GetReturnValue ().SetEmptyString ();
119
61
0 commit comments