2121
2222#include " node.h"
2323#include " node_buffer.h"
24+ #include " node_errors.h"
2425#include " node_constants.h"
2526#include " node_crypto.h"
2627#include " node_crypto_bio.h"
4546#include < memory>
4647#include < vector>
4748
48- #define THROW_AND_RETURN_IF_NOT_BUFFER (val, prefix ) \
49- do { \
50- if (!Buffer::HasInstance (val)) { \
51- return env->ThrowTypeError (prefix " must be a buffer" ); \
52- } \
53- } while (0 )
54-
55- #define THROW_AND_RETURN_IF_NOT_STRING (val, prefix ) \
56- do { \
57- if (!val->IsString ()) { \
58- return env->ThrowTypeError (prefix " must be a string" ); \
59- } \
60- } while (0 )
61-
6249static const char PUBLIC_KEY_PFX[] = " -----BEGIN PUBLIC KEY-----" ;
6350static const int PUBLIC_KEY_PFX_LEN = sizeof (PUBLIC_KEY_PFX) - 1 ;
6451static const char PUBRSA_KEY_PFX[] = " -----BEGIN RSA PUBLIC KEY-----" ;
@@ -518,7 +505,7 @@ void SecureContext::SetKey(const FunctionCallbackInfo<Value>& args) {
518505 if (args[1 ]->IsUndefined () || args[1 ]->IsNull ())
519506 len = 1 ;
520507 else
521- THROW_AND_RETURN_IF_NOT_STRING (args[1 ], " Pass phrase" );
508+ THROW_AND_RETURN_IF_NOT_STRING (env, args[1 ], " Pass phrase" );
522509 }
523510
524511 BIO *bio = LoadBIO (env, args[0 ]);
@@ -916,7 +903,7 @@ void SecureContext::SetCiphers(const FunctionCallbackInfo<Value>& args) {
916903 return env->ThrowTypeError (" Ciphers argument is mandatory" );
917904 }
918905
919- THROW_AND_RETURN_IF_NOT_STRING (args[0 ], " Ciphers" );
906+ THROW_AND_RETURN_IF_NOT_STRING (env, args[0 ], " Ciphers" );
920907
921908 const node::Utf8Value ciphers (args.GetIsolate (), args[0 ]);
922909 SSL_CTX_set_cipher_list (sc->ctx_ , *ciphers);
@@ -931,7 +918,7 @@ void SecureContext::SetECDHCurve(const FunctionCallbackInfo<Value>& args) {
931918 if (args.Length () != 1 )
932919 return env->ThrowTypeError (" ECDH curve name argument is mandatory" );
933920
934- THROW_AND_RETURN_IF_NOT_STRING (args[0 ], " ECDH curve name" );
921+ THROW_AND_RETURN_IF_NOT_STRING (env, args[0 ], " ECDH curve name" );
935922
936923 node::Utf8Value curve (env->isolate (), args[0 ]);
937924
@@ -989,7 +976,8 @@ void SecureContext::SetOptions(const FunctionCallbackInfo<Value>& args) {
989976 ASSIGN_OR_RETURN_UNWRAP (&sc, args.Holder ());
990977
991978 if (args.Length () != 1 || !args[0 ]->IntegerValue ()) {
992- return sc->env ()->ThrowTypeError (" Options must be an integer value" );
979+ return THROW_ERR_INVALID_ARG_TYPE (
980+ sc->env (), " Options must be an integer value" );
993981 }
994982
995983 SSL_CTX_set_options (
@@ -1008,7 +996,7 @@ void SecureContext::SetSessionIdContext(
1008996 return env->ThrowTypeError (" Session ID context argument is mandatory" );
1009997 }
1010998
1011- THROW_AND_RETURN_IF_NOT_STRING (args[0 ], " Session ID context" );
999+ THROW_AND_RETURN_IF_NOT_STRING (env, args[0 ], " Session ID context" );
10121000
10131001 const node::Utf8Value sessionIdContext (args.GetIsolate (), args[0 ]);
10141002 const unsigned char * sid_ctx =
@@ -1043,8 +1031,8 @@ void SecureContext::SetSessionTimeout(const FunctionCallbackInfo<Value>& args) {
10431031 ASSIGN_OR_RETURN_UNWRAP (&sc, args.Holder ());
10441032
10451033 if (args.Length () != 1 || !args[0 ]->IsInt32 ()) {
1046- return sc-> env ()-> ThrowTypeError (
1047- " Session timeout must be a 32-bit integer" );
1034+ return THROW_ERR_INVALID_ARG_TYPE (
1035+ sc-> env (), " Session timeout must be a 32-bit integer" );
10481036 }
10491037
10501038 int32_t sessionTimeout = args[0 ]->Int32Value ();
@@ -1085,7 +1073,7 @@ void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) {
10851073 }
10861074
10871075 if (args.Length () >= 2 ) {
1088- THROW_AND_RETURN_IF_NOT_BUFFER (args[1 ], " Pass phrase" );
1076+ THROW_AND_RETURN_IF_NOT_BUFFER (env, args[1 ], " Pass phrase" );
10891077 size_t passlen = Buffer::Length (args[1 ]);
10901078 pass = new char [passlen + 1 ];
10911079 memcpy (pass, Buffer::Data (args[1 ]), passlen);
@@ -1212,7 +1200,7 @@ void SecureContext::SetTicketKeys(const FunctionCallbackInfo<Value>& args) {
12121200 return env->ThrowTypeError (" Ticket keys argument is mandatory" );
12131201 }
12141202
1215- THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ], " Ticket keys" );
1203+ THROW_AND_RETURN_IF_NOT_BUFFER (env, args[0 ], " Ticket keys" );
12161204
12171205 if (Buffer::Length (args[0 ]) != 48 ) {
12181206 return env->ThrowTypeError (" Ticket keys length must be 48 bytes" );
@@ -1964,7 +1952,7 @@ void SSLWrap<Base>::SetSession(const FunctionCallbackInfo<Value>& args) {
19641952 return env->ThrowError (" Session argument is mandatory" );
19651953 }
19661954
1967- THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ], " Session" );
1955+ THROW_AND_RETURN_IF_NOT_BUFFER (env, args[0 ], " Session" );
19681956 size_t slen = Buffer::Length (args[0 ]);
19691957 char * sbuf = new char [slen];
19701958 memcpy (sbuf, Buffer::Data (args[0 ]), slen);
@@ -2088,7 +2076,7 @@ void SSLWrap<Base>::SetOCSPResponse(
20882076 if (args.Length () < 1 )
20892077 return env->ThrowTypeError (" OCSP response argument is mandatory" );
20902078
2091- THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ], " OCSP response" );
2079+ THROW_AND_RETURN_IF_NOT_BUFFER (env, args[0 ], " OCSP response" );
20922080
20932081 w->ocsp_response_ .Reset (args.GetIsolate (), args[0 ].As <Object>());
20942082#endif // NODE__HAVE_TLSEXT_STATUS_CB
@@ -3938,11 +3926,11 @@ template <PublicKeyCipher::Operation operation,
39383926void PublicKeyCipher::Cipher (const FunctionCallbackInfo<Value>& args) {
39393927 Environment* env = Environment::GetCurrent (args);
39403928
3941- THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ], " Key" );
3929+ THROW_AND_RETURN_IF_NOT_BUFFER (env, args[0 ], " Key" );
39423930 char * kbuf = Buffer::Data (args[0 ]);
39433931 ssize_t klen = Buffer::Length (args[0 ]);
39443932
3945- THROW_AND_RETURN_IF_NOT_BUFFER (args[1 ], " Data" );
3933+ THROW_AND_RETURN_IF_NOT_BUFFER (env, args[1 ], " Data" );
39463934 char * buf = Buffer::Data (args[1 ]);
39473935 ssize_t len = Buffer::Length (args[1 ]);
39483936
@@ -4098,7 +4086,7 @@ void DiffieHellman::DiffieHellmanGroup(
40984086 return env->ThrowError (" Group name argument is mandatory" );
40994087 }
41004088
4101- THROW_AND_RETURN_IF_NOT_STRING (args[0 ], " Group name" );
4089+ THROW_AND_RETURN_IF_NOT_STRING (env, args[0 ], " Group name" );
41024090
41034091 bool initialized = false ;
41044092
@@ -4247,7 +4235,7 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
42474235 if (args.Length () == 0 ) {
42484236 return env->ThrowError (" Other party's public key argument is mandatory" );
42494237 } else {
4250- THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ], " Other party's public key" );
4238+ THROW_AND_RETURN_IF_NOT_BUFFER (env, args[0 ], " Other party's public key" );
42514239 key = BN_bin2bn (
42524240 reinterpret_cast <unsigned char *>(Buffer::Data (args[0 ])),
42534241 Buffer::Length (args[0 ]),
@@ -4320,7 +4308,7 @@ void DiffieHellman::SetKey(const v8::FunctionCallbackInfo<v8::Value>& args,
43204308
43214309 if (!Buffer::HasInstance (args[0 ])) {
43224310 snprintf (errmsg, sizeof (errmsg), " %s must be a buffer" , what);
4323- return env-> ThrowTypeError ( errmsg);
4311+ return THROW_ERR_INVALID_ARG_TYPE (env, errmsg);
43244312 }
43254313
43264314 BIGNUM* num =
@@ -4398,7 +4386,7 @@ void ECDH::New(const FunctionCallbackInfo<Value>& args) {
43984386 MarkPopErrorOnReturn mark_pop_error_on_return;
43994387
44004388 // TODO(indutny): Support raw curves?
4401- THROW_AND_RETURN_IF_NOT_STRING (args[0 ], " ECDH curve name" );
4389+ THROW_AND_RETURN_IF_NOT_STRING (env, args[0 ], " ECDH curve name" );
44024390 node::Utf8Value curve (env->isolate (), args[0 ]);
44034391
44044392 int nid = OBJ_sn2nid (*curve);
@@ -4455,7 +4443,7 @@ EC_POINT* ECDH::BufferToPoint(Environment* env,
44554443void ECDH::ComputeSecret (const FunctionCallbackInfo<Value>& args) {
44564444 Environment* env = Environment::GetCurrent (args);
44574445
4458- THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ], " Data" );
4446+ THROW_AND_RETURN_IF_NOT_BUFFER (env, args[0 ], " Data" );
44594447
44604448 ECDH* ecdh;
44614449 ASSIGN_OR_RETURN_UNWRAP (&ecdh, args.Holder ());
@@ -4558,7 +4546,7 @@ void ECDH::SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
45584546 ECDH* ecdh;
45594547 ASSIGN_OR_RETURN_UNWRAP (&ecdh, args.Holder ());
45604548
4561- THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ], " Private key" );
4549+ THROW_AND_RETURN_IF_NOT_BUFFER (env, args[0 ], " Private key" );
45624550
45634551 BIGNUM* priv = BN_bin2bn (
45644552 reinterpret_cast <unsigned char *>(Buffer::Data (args[0 ].As <Object>())),
@@ -4612,7 +4600,7 @@ void ECDH::SetPublicKey(const FunctionCallbackInfo<Value>& args) {
46124600 ECDH* ecdh;
46134601 ASSIGN_OR_RETURN_UNWRAP (&ecdh, args.Holder ());
46144602
4615- THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ], " Public key" );
4603+ THROW_AND_RETURN_IF_NOT_BUFFER (env, args[0 ], " Public key" );
46164604
46174605 MarkPopErrorOnReturn mark_pop_error_on_return;
46184606
0 commit comments