Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: migrate Certificate to internal/errors #15756

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only spkac doesn't have string in the error message?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't know. This was the way it was already. We can add string as an option in a separate follow on commit

}
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
15 changes: 0 additions & 15 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5819,11 +5819,6 @@ 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 @@ -5881,11 +5876,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 @@ -5922,11 +5912,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'
}
);
});