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

Commit f4b171a

Browse files
jasnelladdaleax
authored andcommitted
crypto: migrate Certificate to internal/errors
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>
1 parent 78963ee commit f4b171a

File tree

4 files changed

+59
-23
lines changed

4 files changed

+59
-23
lines changed

doc/api/crypto.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,12 @@ console.log(challenge.toString('utf8'));
6464
// Prints: the challenge as a UTF8 string
6565
```
6666

67-
### Certificate.exportPublicKey(spkac)
67+
### Certificate.exportPublicKey(spkac[, encoding])
6868
<!-- YAML
6969
added: REPLACEME
7070
-->
7171
- `spkac` {string | Buffer | TypedArray | DataView}
72+
- `encoding` {string}
7273
- Returns {Buffer} The public key component of the `spkac` data structure,
7374
which includes a public key and a challenge.
7475

lib/internal/crypto/certificate.js

+23-6
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,37 @@ const {
66
certVerifySpkac
77
} = process.binding('crypto');
88

9+
const errors = require('internal/errors');
10+
const { isArrayBufferView } = require('internal/util/types');
11+
912
const {
1013
toBuf
1114
} = require('internal/crypto/util');
1215

13-
function verifySpkac(object) {
14-
return certVerifySpkac(object);
16+
function verifySpkac(spkac) {
17+
if (!isArrayBufferView(spkac)) {
18+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'spkac',
19+
['Buffer', 'TypedArray', 'DataView']);
20+
}
21+
return certVerifySpkac(spkac);
1522
}
1623

17-
function exportPublicKey(object, encoding) {
18-
return certExportPublicKey(toBuf(object, encoding));
24+
function exportPublicKey(spkac, encoding) {
25+
spkac = toBuf(spkac, encoding);
26+
if (!isArrayBufferView(spkac)) {
27+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'spkac',
28+
['string', 'Buffer', 'TypedArray', 'DataView']);
29+
}
30+
return certExportPublicKey(spkac);
1931
}
2032

21-
function exportChallenge(object, encoding) {
22-
return certExportChallenge(toBuf(object, encoding));
33+
function exportChallenge(spkac, encoding) {
34+
spkac = toBuf(spkac, encoding);
35+
if (!isArrayBufferView(spkac)) {
36+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'spkac',
37+
['string', 'Buffer', 'TypedArray', 'DataView']);
38+
}
39+
return certExportChallenge(spkac);
2340
}
2441

2542
// For backwards compatibility reasons, this cannot be converted into a

src/node_crypto.cc

-16
Original file line numberDiff line numberDiff line change
@@ -5832,14 +5832,8 @@ bool VerifySpkac(const char* data, unsigned int len) {
58325832

58335833

58345834
void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
5835-
Environment* env = Environment::GetCurrent(args);
58365835
bool i = false;
58375836

5838-
if (args.Length() < 1)
5839-
return env->ThrowTypeError("Data argument is mandatory");
5840-
5841-
THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Data");
5842-
58435837
size_t length = Buffer::Length(args[0]);
58445838
if (length == 0)
58455839
return args.GetReturnValue().Set(i);
@@ -5897,11 +5891,6 @@ char* ExportPublicKey(const char* data, int len, size_t* size) {
58975891
void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
58985892
Environment* env = Environment::GetCurrent(args);
58995893

5900-
if (args.Length() < 1)
5901-
return env->ThrowTypeError("Public key argument is mandatory");
5902-
5903-
THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Public key");
5904-
59055894
size_t length = Buffer::Length(args[0]);
59065895
if (length == 0)
59075896
return args.GetReturnValue().SetEmptyString();
@@ -5938,11 +5927,6 @@ const char* ExportChallenge(const char* data, int len) {
59385927
void ExportChallenge(const FunctionCallbackInfo<Value>& args) {
59395928
Environment* env = Environment::GetCurrent(args);
59405929

5941-
if (args.Length() < 1)
5942-
return env->ThrowTypeError("Challenge argument is mandatory");
5943-
5944-
THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Challenge");
5945-
59465930
size_t len = Buffer::Length(args[0]);
59475931
if (len == 0)
59485932
return args.GetReturnValue().SetEmptyString();

test/parallel/test-crypto-certificate.js

+34
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,37 @@ function stripLineEndings(obj) {
8080

8181
// direct call Certificate() should return instance
8282
assert(Certificate() instanceof Certificate);
83+
84+
[1, {}, [], Infinity, true, 'test', undefined, null].forEach((i) => {
85+
common.expectsError(
86+
() => Certificate.verifySpkac(i),
87+
{
88+
code: 'ERR_INVALID_ARG_TYPE',
89+
type: TypeError,
90+
message: 'The "spkac" argument must be one of type Buffer, TypedArray, ' +
91+
'or DataView'
92+
}
93+
);
94+
});
95+
96+
[1, {}, [], Infinity, true, undefined, null].forEach((i) => {
97+
common.expectsError(
98+
() => Certificate.exportPublicKey(i),
99+
{
100+
code: 'ERR_INVALID_ARG_TYPE',
101+
type: TypeError,
102+
message: 'The "spkac" argument must be one of type string, Buffer,' +
103+
' TypedArray, or DataView'
104+
}
105+
);
106+
107+
common.expectsError(
108+
() => Certificate.exportChallenge(i),
109+
{
110+
code: 'ERR_INVALID_ARG_TYPE',
111+
type: TypeError,
112+
message: 'The "spkac" argument must be one of type string, Buffer,' +
113+
' TypedArray, or DataView'
114+
}
115+
);
116+
});

0 commit comments

Comments
 (0)