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
@@ -3937,11 +3925,11 @@ template <PublicKeyCipher::Operation operation,
39373925void PublicKeyCipher::Cipher (const FunctionCallbackInfo<Value>& args) {
39383926 Environment* env = Environment::GetCurrent (args);
39393927
3940- THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ], " Key" );
3928+ THROW_AND_RETURN_IF_NOT_BUFFER (env, args[0 ], " Key" );
39413929 char * kbuf = Buffer::Data (args[0 ]);
39423930 ssize_t klen = Buffer::Length (args[0 ]);
39433931
3944- THROW_AND_RETURN_IF_NOT_BUFFER (args[1 ], " Data" );
3932+ THROW_AND_RETURN_IF_NOT_BUFFER (env, args[1 ], " Data" );
39453933 char * buf = Buffer::Data (args[1 ]);
39463934 ssize_t len = Buffer::Length (args[1 ]);
39473935
@@ -4097,7 +4085,7 @@ void DiffieHellman::DiffieHellmanGroup(
40974085 return env->ThrowError (" Group name argument is mandatory" );
40984086 }
40994087
4100- THROW_AND_RETURN_IF_NOT_STRING (args[0 ], " Group name" );
4088+ THROW_AND_RETURN_IF_NOT_STRING (env, args[0 ], " Group name" );
41014089
41024090 bool initialized = false ;
41034091
@@ -4246,7 +4234,7 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
42464234 if (args.Length () == 0 ) {
42474235 return env->ThrowError (" Other party's public key argument is mandatory" );
42484236 } else {
4249- THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ], " Other party's public key" );
4237+ THROW_AND_RETURN_IF_NOT_BUFFER (env, args[0 ], " Other party's public key" );
42504238 key = BN_bin2bn (
42514239 reinterpret_cast <unsigned char *>(Buffer::Data (args[0 ])),
42524240 Buffer::Length (args[0 ]),
@@ -4319,7 +4307,7 @@ void DiffieHellman::SetKey(const v8::FunctionCallbackInfo<v8::Value>& args,
43194307
43204308 if (!Buffer::HasInstance (args[0 ])) {
43214309 snprintf (errmsg, sizeof (errmsg), " %s must be a buffer" , what);
4322- return env-> ThrowTypeError ( errmsg);
4310+ return THROW_ERR_INVALID_ARG_TYPE (env, errmsg);
43234311 }
43244312
43254313 BIGNUM* num =
@@ -4397,7 +4385,7 @@ void ECDH::New(const FunctionCallbackInfo<Value>& args) {
43974385 MarkPopErrorOnReturn mark_pop_error_on_return;
43984386
43994387 // TODO(indutny): Support raw curves?
4400- THROW_AND_RETURN_IF_NOT_STRING (args[0 ], " ECDH curve name" );
4388+ THROW_AND_RETURN_IF_NOT_STRING (env, args[0 ], " ECDH curve name" );
44014389 node::Utf8Value curve (env->isolate (), args[0 ]);
44024390
44034391 int nid = OBJ_sn2nid (*curve);
@@ -4454,7 +4442,7 @@ EC_POINT* ECDH::BufferToPoint(Environment* env,
44544442void ECDH::ComputeSecret (const FunctionCallbackInfo<Value>& args) {
44554443 Environment* env = Environment::GetCurrent (args);
44564444
4457- THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ], " Data" );
4445+ THROW_AND_RETURN_IF_NOT_BUFFER (env, args[0 ], " Data" );
44584446
44594447 ECDH* ecdh;
44604448 ASSIGN_OR_RETURN_UNWRAP (&ecdh, args.Holder ());
@@ -4557,7 +4545,7 @@ void ECDH::SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
45574545 ECDH* ecdh;
45584546 ASSIGN_OR_RETURN_UNWRAP (&ecdh, args.Holder ());
45594547
4560- THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ], " Private key" );
4548+ THROW_AND_RETURN_IF_NOT_BUFFER (env, args[0 ], " Private key" );
45614549
45624550 BIGNUM* priv = BN_bin2bn (
45634551 reinterpret_cast <unsigned char *>(Buffer::Data (args[0 ].As <Object>())),
@@ -4611,7 +4599,7 @@ void ECDH::SetPublicKey(const FunctionCallbackInfo<Value>& args) {
46114599 ECDH* ecdh;
46124600 ASSIGN_OR_RETURN_UNWRAP (&ecdh, args.Holder ());
46134601
4614- THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ], " Public key" );
4602+ THROW_AND_RETURN_IF_NOT_BUFFER (env, args[0 ], " Public key" );
46154603
46164604 MarkPopErrorOnReturn mark_pop_error_on_return;
46174605
0 commit comments