From 3ddc88b5c2c310a127cc63463b068c6bbf4ce526 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Tue, 3 Oct 2017 06:40:56 -0700 Subject: [PATCH] crypto: migrate Certificate to internal/errors Move argument type checking to js, use internal/errors PR-URL: https://github.com/nodejs/node/pull/15756 Reviewed-By: Luigi Pinca Reviewed-By: Joyee Cheung Reviewed-By: Anna Henningsen --- doc/api/crypto.md | 3 ++- lib/internal/crypto/certificate.js | 29 +++++++++++++++----- src/node_crypto.cc | 16 ----------- test/parallel/test-crypto-certificate.js | 34 ++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 23 deletions(-) diff --git a/doc/api/crypto.md b/doc/api/crypto.md index 8bafbfe791b..fd2213f8b55 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -64,11 +64,12 @@ console.log(challenge.toString('utf8')); // Prints: the challenge as a UTF8 string ``` -### Certificate.exportPublicKey(spkac) +### Certificate.exportPublicKey(spkac[, encoding]) - `spkac` {string | Buffer | TypedArray | DataView} +- `encoding` {string} - Returns {Buffer} The public key component of the `spkac` data structure, which includes a public key and a challenge. diff --git a/lib/internal/crypto/certificate.js b/lib/internal/crypto/certificate.js index e37bedd2f92..b6b6ef8f5f6 100644 --- a/lib/internal/crypto/certificate.js +++ b/lib/internal/crypto/certificate.js @@ -6,20 +6,37 @@ const { certVerifySpkac } = process.binding('crypto'); +const errors = require('internal/errors'); +const { isArrayBufferView } = require('internal/util/types'); + const { toBuf } = require('internal/crypto/util'); -function verifySpkac(object) { - return certVerifySpkac(object); +function verifySpkac(spkac) { + if (!isArrayBufferView(spkac)) { + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'spkac', + ['Buffer', 'TypedArray', 'DataView']); + } + return certVerifySpkac(spkac); } -function exportPublicKey(object, encoding) { - return certExportPublicKey(toBuf(object, encoding)); +function exportPublicKey(spkac, encoding) { + spkac = toBuf(spkac, encoding); + if (!isArrayBufferView(spkac)) { + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'spkac', + ['string', 'Buffer', 'TypedArray', 'DataView']); + } + return certExportPublicKey(spkac); } -function exportChallenge(object, encoding) { - return certExportChallenge(toBuf(object, encoding)); +function exportChallenge(spkac, encoding) { + spkac = toBuf(spkac, encoding); + if (!isArrayBufferView(spkac)) { + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'spkac', + ['string', 'Buffer', 'TypedArray', 'DataView']); + } + return certExportChallenge(spkac); } // For backwards compatibility reasons, this cannot be converted into a diff --git a/src/node_crypto.cc b/src/node_crypto.cc index a1d601adf4b..fa4815a9890 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -5816,14 +5816,8 @@ bool VerifySpkac(const char* data, unsigned int len) { void VerifySpkac(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); bool i = false; - if (args.Length() < 1) - return env->ThrowTypeError("Data argument is mandatory"); - - THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Data"); - size_t length = Buffer::Length(args[0]); if (length == 0) return args.GetReturnValue().Set(i); @@ -5881,11 +5875,6 @@ char* ExportPublicKey(const char* data, int len, size_t* size) { void ExportPublicKey(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); - if (args.Length() < 1) - return env->ThrowTypeError("Public key argument is mandatory"); - - THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Public key"); - size_t length = Buffer::Length(args[0]); if (length == 0) return args.GetReturnValue().SetEmptyString(); @@ -5922,11 +5911,6 @@ const char* ExportChallenge(const char* data, int len) { void ExportChallenge(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); - if (args.Length() < 1) - return env->ThrowTypeError("Challenge argument is mandatory"); - - THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Challenge"); - size_t len = Buffer::Length(args[0]); if (len == 0) return args.GetReturnValue().SetEmptyString(); diff --git a/test/parallel/test-crypto-certificate.js b/test/parallel/test-crypto-certificate.js index b639185a55d..36742154efd 100644 --- a/test/parallel/test-crypto-certificate.js +++ b/test/parallel/test-crypto-certificate.js @@ -80,3 +80,37 @@ function stripLineEndings(obj) { // direct call Certificate() should return instance assert(Certificate() instanceof Certificate); + +[1, {}, [], Infinity, true, 'test', undefined, null].forEach((i) => { + common.expectsError( + () => Certificate.verifySpkac(i), + { + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: 'The "spkac" argument must be one of type Buffer, TypedArray, ' + + 'or DataView' + } + ); +}); + +[1, {}, [], Infinity, true, undefined, null].forEach((i) => { + common.expectsError( + () => Certificate.exportPublicKey(i), + { + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: 'The "spkac" argument must be one of type string, Buffer,' + + ' TypedArray, or DataView' + } + ); + + common.expectsError( + () => Certificate.exportChallenge(i), + { + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: 'The "spkac" argument must be one of type string, Buffer,' + + ' TypedArray, or DataView' + } + ); +});