Skip to content
This repository has been archived by the owner on Aug 31, 2018. It is now read-only.

Commit

Permalink
crypto: migrate Certificate to internal/errors
Browse files Browse the repository at this point in the history
Move argument type checking to js, use internal/errors

PR-URL: nodejs/node#15756
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
jasnell authored and addaleax committed Oct 18, 2017
1 parent 78963ee commit f4b171a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 23 deletions.
3 changes: 2 additions & 1 deletion doc/api/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ console.log(challenge.toString('utf8'));
// Prints: the challenge as a UTF8 string
```

### Certificate.exportPublicKey(spkac)
### Certificate.exportPublicKey(spkac[, encoding])
<!-- YAML
added: REPLACEME
-->
- `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.

Expand Down
29 changes: 23 additions & 6 deletions lib/internal/crypto/certificate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 0 additions & 16 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5832,14 +5832,8 @@ bool VerifySpkac(const char* data, unsigned int len) {


void VerifySpkac(const FunctionCallbackInfo<Value>& 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);
Expand Down Expand Up @@ -5897,11 +5891,6 @@ char* ExportPublicKey(const char* data, int len, size_t* size) {
void ExportPublicKey(const FunctionCallbackInfo<Value>& 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();
Expand Down Expand Up @@ -5938,11 +5927,6 @@ const char* ExportChallenge(const char* data, int len) {
void ExportChallenge(const FunctionCallbackInfo<Value>& 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();
Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-crypto-certificate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
);
});

0 comments on commit f4b171a

Please sign in to comment.