Skip to content

Commit 3ddc88b

Browse files
committed
crypto: migrate Certificate to internal/errors
Move argument type checking to js, use internal/errors PR-URL: #15756 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent abbdcaa commit 3ddc88b

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
@@ -5816,14 +5816,8 @@ bool VerifySpkac(const char* data, unsigned int len) {
58165816

58175817

58185818
void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
5819-
Environment* env = Environment::GetCurrent(args);
58205819
bool i = false;
58215820

5822-
if (args.Length() < 1)
5823-
return env->ThrowTypeError("Data argument is mandatory");
5824-
5825-
THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Data");
5826-
58275821
size_t length = Buffer::Length(args[0]);
58285822
if (length == 0)
58295823
return args.GetReturnValue().Set(i);
@@ -5881,11 +5875,6 @@ char* ExportPublicKey(const char* data, int len, size_t* size) {
58815875
void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
58825876
Environment* env = Environment::GetCurrent(args);
58835877

5884-
if (args.Length() < 1)
5885-
return env->ThrowTypeError("Public key argument is mandatory");
5886-
5887-
THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Public key");
5888-
58895878
size_t length = Buffer::Length(args[0]);
58905879
if (length == 0)
58915880
return args.GetReturnValue().SetEmptyString();
@@ -5922,11 +5911,6 @@ const char* ExportChallenge(const char* data, int len) {
59225911
void ExportChallenge(const FunctionCallbackInfo<Value>& args) {
59235912
Environment* env = Environment::GetCurrent(args);
59245913

5925-
if (args.Length() < 1)
5926-
return env->ThrowTypeError("Challenge argument is mandatory");
5927-
5928-
THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Challenge");
5929-
59305914
size_t len = Buffer::Length(args[0]);
59315915
if (len == 0)
59325916
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)