| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 7124b46 commit b8bc652
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,12 +13,15 @@ const { | |||
| 13 | 13 | getDefaultEncoding, | |
| 14 | 14 | toBuf | |
| 15 | 15 | } = require('internal/crypto/util'); | |
| 16 | + const { isArrayBufferView } = require('internal/util/types'); | ||
| 16 | 17 | const { Writable } = require('stream'); | |
| 17 | 18 | const { inherits } = require('util'); | |
| 18 | 19 | ||
| 19 | 20 | function Sign(algorithm, options) { | |
| 20 | 21 | if (!(this instanceof Sign)) | |
| 21 | 22 | return new Sign(algorithm, options); | |
| 23 | + if (typeof algorithm !== 'string') | ||
| 24 | + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'algorithm', 'string'); | ||
| 22 | 25 | this._handle = new _Sign(); | |
| 23 | 26 | this._handle.init(algorithm); | |
| 24 | 27 | ||
@@ -28,13 +31,18 @@ function Sign(algorithm, options) { | |||
| 28 | 31 | inherits(Sign, Writable); | |
| 29 | 32 | ||
| 30 | 33 | Sign.prototype._write = function _write(chunk, encoding, callback) { | |
| 31 | - this._handle.update(chunk, encoding); | ||
| 34 | + this.update(chunk, encoding); | ||
| 32 | 35 | callback(); | |
| 33 | 36 | }; | |
| 34 | 37 | ||
| 35 | 38 | Sign.prototype.update = function update(data, encoding) { | |
| 36 | 39 | encoding = encoding || getDefaultEncoding(); | |
| 37 | - this._handle.update(data, encoding); | ||
| 40 | + data = toBuf(data, encoding); | ||
| 41 | + if (!isArrayBufferView(data)) { | ||
| 42 | + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'data', | ||
| 43 | + ['string', 'Buffer', 'TypedArray', 'DataView']); | ||
| 44 | + } | ||
| 45 | + this._handle.update(data); | ||
| 38 | 46 | return this; | |
| 39 | 47 | }; | |
| 40 | 48 | ||
@@ -68,8 +76,13 @@ Sign.prototype.sign = function sign(options, encoding) { | |||
| 68 | 76 | } | |
| 69 | 77 | } | |
| 70 | 78 | ||
| 71 | - var ret = this._handle.sign(toBuf(key), passphrase, rsaPadding, | ||
| 72 | - pssSaltLength); | ||
| 79 | + key = toBuf(key); | ||
| 80 | + if (!isArrayBufferView(key)) { | ||
| 81 | + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'key', | ||
| 82 | + ['string', 'Buffer', 'TypedArray', 'DataView']); | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + var ret = this._handle.sign(key, passphrase, rsaPadding, pssSaltLength); | ||
| 73 | 86 | ||
| 74 | 87 | encoding = encoding || getDefaultEncoding(); | |
| 75 | 88 | if (encoding && encoding !== 'buffer') | |
@@ -82,7 +95,8 @@ Sign.prototype.sign = function sign(options, encoding) { | |||
| 82 | 95 | function Verify(algorithm, options) { | |
| 83 | 96 | if (!(this instanceof Verify)) | |
| 84 | 97 | return new Verify(algorithm, options); | |
| 85 | - | ||
| 98 | + if (typeof algorithm !== 'string') | ||
| 99 | + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'algorithm', 'string'); | ||
| 86 | 100 | this._handle = new _Verify(); | |
| 87 | 101 | this._handle.init(algorithm); | |
| 88 | 102 | ||
@@ -121,8 +135,19 @@ Verify.prototype.verify = function verify(options, signature, sigEncoding) { | |||
| 121 | 135 | } | |
| 122 | 136 | } | |
| 123 | 137 | ||
| 124 | - return this._handle.verify(toBuf(key), toBuf(signature, sigEncoding), | ||
| 125 | - rsaPadding, pssSaltLength); | ||
| 138 | + key = toBuf(key); | ||
| 139 | + if (!isArrayBufferView(key)) { | ||
| 140 | + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'key', | ||
| 141 | + ['string', 'Buffer', 'TypedArray', 'DataView']); | ||
| 142 | + } | ||
| 143 | + | ||
| 144 | + signature = toBuf(signature, sigEncoding); | ||
| 145 | + if (!isArrayBufferView(signature)) { | ||
| 146 | + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'signature', | ||
| 147 | + ['string', 'Buffer', 'TypedArray', 'DataView']); | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + return this._handle.verify(key, signature, rsaPadding, pssSaltLength); | ||
| 126 | 151 | }; | |
| 127 | 152 | ||
| 128 | 153 | module.exports = { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4036,13 +4036,6 @@ SignBase::Error Sign::SignInit(const char* sign_type) { | |||
| 4036 | 4036 | void Sign::SignInit(const FunctionCallbackInfo<Value>& args) { | |
| 4037 | 4037 | Sign* sign; | |
| 4038 | 4038 | ASSIGN_OR_RETURN_UNWRAP(&sign, args.Holder()); | |
| 4039 | - Environment* env = sign->env(); | ||
| 4040 | - | ||
| 4041 | - if (args.Length() == 0) { | ||
| 4042 | - return env->ThrowError("Sign type argument is mandatory"); | ||
| 4043 | - } | ||
| 4044 | - | ||
| 4045 | - THROW_AND_RETURN_IF_NOT_STRING(args[0], "Sign type"); | ||
| 4046 | 4039 | ||
| 4047 | 4040 | const node::Utf8Value sign_type(args.GetIsolate(), args[0]); | |
| 4048 | 4041 | sign->CheckThrow(sign->SignInit(*sign_type)); | |
@@ -4059,25 +4052,13 @@ SignBase::Error Sign::SignUpdate(const char* data, int len) { | |||
| 4059 | 4052 | ||
| 4060 | 4053 | ||
| 4061 | 4054 | void Sign::SignUpdate(const FunctionCallbackInfo<Value>& args) { | |
| 4062 | - Environment* env = Environment::GetCurrent(args); | ||
| 4063 | - | ||
| 4064 | 4055 | Sign* sign; | |
| 4065 | 4056 | ASSIGN_OR_RETURN_UNWRAP(&sign, args.Holder()); | |
| 4066 | 4057 | ||
| 4067 | - THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0], "Data"); | ||
| 4068 | - | ||
| 4069 | - // Only copy the data if we have to, because it's a string | ||
| 4070 | 4058 | Error err; | |
| 4071 | - if (args[0]->IsString()) { | ||
| 4072 | - StringBytes::InlineDecoder decoder; | ||
| 4073 | - if (!decoder.Decode(env, args[0].As<String>(), args[1], UTF8)) | ||
| 4074 | - return; | ||
| 4075 | - err = sign->SignUpdate(decoder.out(), decoder.size()); | ||
| 4076 | - } else { | ||
| 4077 | - char* buf = Buffer::Data(args[0]); | ||
| 4078 | - size_t buflen = Buffer::Length(args[0]); | ||
| 4079 | - err = sign->SignUpdate(buf, buflen); | ||
| 4080 | - } | ||
| 4059 | + char* buf = Buffer::Data(args[0]); | ||
| 4060 | + size_t buflen = Buffer::Length(args[0]); | ||
| 4061 | + err = sign->SignUpdate(buf, buflen); | ||
| 4081 | 4062 | ||
| 4082 | 4063 | sign->CheckThrow(err); | |
| 4083 | 4064 | } | |
@@ -4195,7 +4176,6 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) { | |||
| 4195 | 4176 | ||
| 4196 | 4177 | node::Utf8Value passphrase(env->isolate(), args[1]); | |
| 4197 | 4178 | ||
| 4198 | - THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Data"); | ||
| 4199 | 4179 | size_t buf_len = Buffer::Length(args[0]); | |
| 4200 | 4180 | char* buf = Buffer::Data(args[0]); | |
| 4201 | 4181 | ||
@@ -4269,13 +4249,6 @@ SignBase::Error Verify::VerifyInit(const char* verify_type) { | |||
| 4269 | 4249 | void Verify::VerifyInit(const FunctionCallbackInfo<Value>& args) { | |
| 4270 | 4250 | Verify* verify; | |
| 4271 | 4251 | ASSIGN_OR_RETURN_UNWRAP(&verify, args.Holder()); | |
| 4272 | - Environment* env = verify->env(); | ||
| 4273 | - | ||
| 4274 | - if (args.Length() == 0) { | ||
| 4275 | - return env->ThrowError("Verify type argument is mandatory"); | ||
| 4276 | - } | ||
| 4277 | - | ||
| 4278 | - THROW_AND_RETURN_IF_NOT_STRING(args[0], "Verify type"); | ||
| 4279 | 4252 | ||
| 4280 | 4253 | const node::Utf8Value verify_type(args.GetIsolate(), args[0]); | |
| 4281 | 4254 | verify->CheckThrow(verify->VerifyInit(*verify_type)); | |
@@ -4294,25 +4267,13 @@ SignBase::Error Verify::VerifyUpdate(const char* data, int len) { | |||
| 4294 | 4267 | ||
| 4295 | 4268 | ||
| 4296 | 4269 | void Verify::VerifyUpdate(const FunctionCallbackInfo<Value>& args) { | |
| 4297 | - Environment* env = Environment::GetCurrent(args); | ||
| 4298 | - | ||
| 4299 | 4270 | Verify* verify; | |
| 4300 | 4271 | ASSIGN_OR_RETURN_UNWRAP(&verify, args.Holder()); | |
| 4301 | 4272 | ||
| 4302 | - THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0], "Data"); | ||
| 4303 | - | ||
| 4304 | - // Only copy the data if we have to, because it's a string | ||
| 4305 | 4273 | Error err; | |
| 4306 | - if (args[0]->IsString()) { | ||
| 4307 | - StringBytes::InlineDecoder decoder; | ||
| 4308 | - if (!decoder.Decode(env, args[0].As<String>(), args[1], UTF8)) | ||
| 4309 | - return; | ||
| 4310 | - err = verify->VerifyUpdate(decoder.out(), decoder.size()); | ||
| 4311 | - } else { | ||
| 4312 | - char* buf = Buffer::Data(args[0]); | ||
| 4313 | - size_t buflen = Buffer::Length(args[0]); | ||
| 4314 | - err = verify->VerifyUpdate(buf, buflen); | ||
| 4315 | - } | ||
| 4274 | + char* buf = Buffer::Data(args[0]); | ||
| 4275 | + size_t buflen = Buffer::Length(args[0]); | ||
| 4276 | + err = verify->VerifyUpdate(buf, buflen); | ||
| 4316 | 4277 | ||
| 4317 | 4278 | verify->CheckThrow(err); | |
| 4318 | 4279 | } | |
@@ -4421,12 +4382,9 @@ void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) { | |||
| 4421 | 4382 | Verify* verify; | |
| 4422 | 4383 | ASSIGN_OR_RETURN_UNWRAP(&verify, args.Holder()); | |
| 4423 | 4384 | ||
| 4424 | - THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Key"); | ||
| 4425 | 4385 | char* kbuf = Buffer::Data(args[0]); | |
| 4426 | 4386 | ssize_t klen = Buffer::Length(args[0]); | |
| 4427 | 4387 | ||
| 4428 | - THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[1], "Hash"); | ||
| 4429 | - | ||
| 4430 | 4388 | char* hbuf = Buffer::Data(args[1]); | |
| 4431 | 4389 | ssize_t hlen = Buffer::Length(args[1]); | |
| 4432 | 4390 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -277,3 +277,106 @@ const modSize = 1024; | |||
| 277 | 277 | assert(stdout.includes('Verified OK')); | |
| 278 | 278 | })); | |
| 279 | 279 | } | |
| 280 | + | ||
| 281 | + [1, [], {}, undefined, null, true, Infinity].forEach((i) => { | ||
| 282 | + common.expectsError( | ||
| 283 | + () => crypto.createSign(), | ||
| 284 | + { | ||
| 285 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 286 | + type: TypeError, | ||
| 287 | + message: 'The "algorithm" argument must be of type string' | ||
| 288 | + } | ||
| 289 | + ); | ||
| 290 | + common.expectsError( | ||
| 291 | + () => crypto.createVerify(), | ||
| 292 | + { | ||
| 293 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 294 | + type: TypeError, | ||
| 295 | + message: 'The "algorithm" argument must be of type string' | ||
| 296 | + } | ||
| 297 | + ); | ||
| 298 | + }); | ||
| 299 | + | ||
| 300 | + { | ||
| 301 | + const sign = crypto.createSign('SHA1'); | ||
| 302 | + const verify = crypto.createVerify('SHA1'); | ||
| 303 | + | ||
| 304 | + [1, [], {}, undefined, null, true, Infinity].forEach((i) => { | ||
| 305 | + common.expectsError( | ||
| 306 | + () => sign.update(i), | ||
| 307 | + { | ||
| 308 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 309 | + type: TypeError, | ||
| 310 | + message: 'The "data" argument must be one of type string, Buffer, ' + | ||
| 311 | + 'TypedArray, or DataView' | ||
| 312 | + } | ||
| 313 | + ); | ||
| 314 | + common.expectsError( | ||
| 315 | + () => verify.update(i), | ||
| 316 | + { | ||
| 317 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 318 | + type: TypeError, | ||
| 319 | + message: 'The "data" argument must be one of type string, Buffer, ' + | ||
| 320 | + 'TypedArray, or DataView' | ||
| 321 | + } | ||
| 322 | + ); | ||
| 323 | + common.expectsError( | ||
| 324 | + () => sign._write(i, 'utf8', () => {}), | ||
| 325 | + { | ||
| 326 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 327 | + type: TypeError, | ||
| 328 | + message: 'The "data" argument must be one of type string, Buffer, ' + | ||
| 329 | + 'TypedArray, or DataView' | ||
| 330 | + } | ||
| 331 | + ); | ||
| 332 | + common.expectsError( | ||
| 333 | + () => verify._write(i, 'utf8', () => {}), | ||
| 334 | + { | ||
| 335 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 336 | + type: TypeError, | ||
| 337 | + message: 'The "data" argument must be one of type string, Buffer, ' + | ||
| 338 | + 'TypedArray, or DataView' | ||
| 339 | + } | ||
| 340 | + ); | ||
| 341 | + }); | ||
| 342 | + | ||
| 343 | + [ | ||
| 344 | + Uint8Array, Uint16Array, Uint32Array, Float32Array, Float64Array | ||
| 345 | + ].forEach((i) => { | ||
| 346 | + // These should all just work | ||
| 347 | + sign.update(new i()); | ||
| 348 | + verify.update(new i()); | ||
| 349 | + }); | ||
| 350 | + | ||
| 351 | + [1, {}, [], Infinity].forEach((i) => { | ||
| 352 | + common.expectsError( | ||
| 353 | + () => sign.sign(i), | ||
| 354 | + { | ||
| 355 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 356 | + type: TypeError, | ||
| 357 | + message: 'The "key" argument must be one of type string, Buffer, ' + | ||
| 358 | + 'TypedArray, or DataView' | ||
| 359 | + } | ||
| 360 | + ); | ||
| 361 | + | ||
| 362 | + common.expectsError( | ||
| 363 | + () => verify.verify(i), | ||
| 364 | + { | ||
| 365 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 366 | + type: TypeError, | ||
| 367 | + message: 'The "key" argument must be one of type string, Buffer, ' + | ||
| 368 | + 'TypedArray, or DataView' | ||
| 369 | + } | ||
| 370 | + ); | ||
| 371 | + | ||
| 372 | + common.expectsError( | ||
| 373 | + () => verify.verify('test', i), | ||
| 374 | + { | ||
| 375 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 376 | + type: TypeError, | ||
| 377 | + message: 'The "signature" argument must be one of type string, ' + | ||
| 378 | + 'Buffer, TypedArray, or DataView' | ||
| 379 | + } | ||
| 380 | + ); | ||
| 381 | + }); | ||
| 382 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -201,28 +201,6 @@ assert.throws(function() { | |||
| 201 | 201 | } | |
| 202 | 202 | }); | |
| 203 | 203 | ||
| 204 | - assert.throws(function() { | ||
| 205 | - crypto.createSign('SHA1').update('0', 'hex'); | ||
| 206 | - }, (err) => { | ||
| 207 | - // Throws TypeError, so there is no opensslErrorStack property. | ||
| 208 | - if ((err instanceof Error) && | ||
| 209 | - /^TypeError: Bad input string$/.test(err) && | ||
| 210 | - err.opensslErrorStack === undefined) { | ||
| 211 | - return true; | ||
| 212 | - } | ||
| 213 | - }); | ||
| 214 | - | ||
| 215 | - assert.throws(function() { | ||
| 216 | - crypto.createVerify('SHA1').update('0', 'hex'); | ||
| 217 | - }, (err) => { | ||
| 218 | - // Throws TypeError, so there is no opensslErrorStack property. | ||
| 219 | - if ((err instanceof Error) && | ||
| 220 | - /^TypeError: Bad input string$/.test(err) && | ||
| 221 | - err.opensslErrorStack === undefined) { | ||
| 222 | - return true; | ||
| 223 | - } | ||
| 224 | - }); | ||
| 225 | - | ||
| 226 | 204 | assert.throws(function() { | |
| 227 | 205 | const priv = [ | |
| 228 | 206 | '-----BEGIN RSA PRIVATE KEY-----', | |
| Back | FazBrowse Home | New Git URL |
0 commit comments