2017-09-06 17:10:34 +02:00
|
|
|
'use strict';
|
|
|
|
|
2018-03-04 22:16:24 +01:00
|
|
|
const {
|
|
|
|
ERR_CRYPTO_SIGN_KEY_REQUIRED,
|
|
|
|
ERR_INVALID_ARG_TYPE,
|
|
|
|
ERR_INVALID_OPT_VALUE
|
|
|
|
} = require('internal/errors').codes;
|
2017-09-06 17:10:34 +02:00
|
|
|
const {
|
|
|
|
Sign: _Sign,
|
|
|
|
Verify: _Verify
|
|
|
|
} = process.binding('crypto');
|
|
|
|
const {
|
|
|
|
RSA_PSS_SALTLEN_AUTO,
|
|
|
|
RSA_PKCS1_PADDING
|
|
|
|
} = process.binding('constants').crypto;
|
|
|
|
const {
|
|
|
|
getDefaultEncoding,
|
|
|
|
toBuf
|
|
|
|
} = require('internal/crypto/util');
|
2017-10-03 16:28:26 +02:00
|
|
|
const { isArrayBufferView } = require('internal/util/types');
|
2017-09-06 17:10:34 +02:00
|
|
|
const { Writable } = require('stream');
|
|
|
|
const { inherits } = require('util');
|
|
|
|
|
|
|
|
function Sign(algorithm, options) {
|
|
|
|
if (!(this instanceof Sign))
|
|
|
|
return new Sign(algorithm, options);
|
2017-10-03 16:28:26 +02:00
|
|
|
if (typeof algorithm !== 'string')
|
2018-03-19 13:33:46 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('algorithm', 'string', algorithm);
|
2017-09-06 17:10:34 +02:00
|
|
|
this._handle = new _Sign();
|
|
|
|
this._handle.init(algorithm);
|
|
|
|
|
|
|
|
Writable.call(this, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
inherits(Sign, Writable);
|
|
|
|
|
|
|
|
Sign.prototype._write = function _write(chunk, encoding, callback) {
|
2017-10-03 16:28:26 +02:00
|
|
|
this.update(chunk, encoding);
|
2017-09-06 17:10:34 +02:00
|
|
|
callback();
|
|
|
|
};
|
|
|
|
|
|
|
|
Sign.prototype.update = function update(data, encoding) {
|
|
|
|
encoding = encoding || getDefaultEncoding();
|
2017-10-03 16:28:26 +02:00
|
|
|
data = toBuf(data, encoding);
|
|
|
|
if (!isArrayBufferView(data)) {
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
'data',
|
2018-03-19 13:33:46 +01:00
|
|
|
['string', 'Buffer', 'TypedArray', 'DataView'],
|
|
|
|
data
|
2018-03-04 22:16:24 +01:00
|
|
|
);
|
2017-10-03 16:28:26 +02:00
|
|
|
}
|
|
|
|
this._handle.update(data);
|
2017-09-06 17:10:34 +02:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
Sign.prototype.sign = function sign(options, encoding) {
|
|
|
|
if (!options)
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_CRYPTO_SIGN_KEY_REQUIRED();
|
2017-09-06 17:10:34 +02:00
|
|
|
|
|
|
|
var key = options.key || options;
|
|
|
|
var passphrase = options.passphrase || null;
|
|
|
|
|
|
|
|
// Options specific to RSA
|
|
|
|
var rsaPadding = RSA_PKCS1_PADDING;
|
|
|
|
if (options.hasOwnProperty('padding')) {
|
|
|
|
if (options.padding === options.padding >> 0) {
|
|
|
|
rsaPadding = options.padding;
|
|
|
|
} else {
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_INVALID_OPT_VALUE('padding', options.padding);
|
2017-09-06 17:10:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var pssSaltLength = RSA_PSS_SALTLEN_AUTO;
|
|
|
|
if (options.hasOwnProperty('saltLength')) {
|
|
|
|
if (options.saltLength === options.saltLength >> 0) {
|
|
|
|
pssSaltLength = options.saltLength;
|
|
|
|
} else {
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_INVALID_OPT_VALUE('saltLength', options.saltLength);
|
2017-09-06 17:10:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 16:28:26 +02:00
|
|
|
key = toBuf(key);
|
|
|
|
if (!isArrayBufferView(key)) {
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
'key',
|
2018-03-19 13:33:46 +01:00
|
|
|
['string', 'Buffer', 'TypedArray', 'DataView'],
|
|
|
|
key
|
2018-03-04 22:16:24 +01:00
|
|
|
);
|
2017-10-03 16:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var ret = this._handle.sign(key, passphrase, rsaPadding, pssSaltLength);
|
2017-09-06 17:10:34 +02:00
|
|
|
|
|
|
|
encoding = encoding || getDefaultEncoding();
|
|
|
|
if (encoding && encoding !== 'buffer')
|
|
|
|
ret = ret.toString(encoding);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function Verify(algorithm, options) {
|
|
|
|
if (!(this instanceof Verify))
|
|
|
|
return new Verify(algorithm, options);
|
2017-10-03 16:28:26 +02:00
|
|
|
if (typeof algorithm !== 'string')
|
2018-03-19 13:33:46 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('algorithm', 'string', algorithm);
|
2017-09-06 17:10:34 +02:00
|
|
|
this._handle = new _Verify();
|
|
|
|
this._handle.init(algorithm);
|
|
|
|
|
|
|
|
Writable.call(this, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
inherits(Verify, Writable);
|
|
|
|
|
|
|
|
Verify.prototype._write = Sign.prototype._write;
|
|
|
|
Verify.prototype.update = Sign.prototype.update;
|
|
|
|
|
|
|
|
Verify.prototype.verify = function verify(options, signature, sigEncoding) {
|
|
|
|
var key = options.key || options;
|
|
|
|
sigEncoding = sigEncoding || getDefaultEncoding();
|
|
|
|
|
|
|
|
// Options specific to RSA
|
|
|
|
var rsaPadding = RSA_PKCS1_PADDING;
|
|
|
|
if (options.hasOwnProperty('padding')) {
|
|
|
|
if (options.padding === options.padding >> 0) {
|
|
|
|
rsaPadding = options.padding;
|
|
|
|
} else {
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_INVALID_OPT_VALUE('padding', options.padding);
|
2017-09-06 17:10:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var pssSaltLength = RSA_PSS_SALTLEN_AUTO;
|
|
|
|
if (options.hasOwnProperty('saltLength')) {
|
|
|
|
if (options.saltLength === options.saltLength >> 0) {
|
|
|
|
pssSaltLength = options.saltLength;
|
|
|
|
} else {
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_INVALID_OPT_VALUE('saltLength', options.saltLength);
|
2017-09-06 17:10:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 16:28:26 +02:00
|
|
|
key = toBuf(key);
|
|
|
|
if (!isArrayBufferView(key)) {
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
'key',
|
2018-03-19 13:33:46 +01:00
|
|
|
['string', 'Buffer', 'TypedArray', 'DataView'],
|
|
|
|
key
|
2018-03-04 22:16:24 +01:00
|
|
|
);
|
2017-10-03 16:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
signature = toBuf(signature, sigEncoding);
|
|
|
|
if (!isArrayBufferView(signature)) {
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
'signature',
|
2018-03-19 13:33:46 +01:00
|
|
|
['string', 'Buffer', 'TypedArray', 'DataView'],
|
|
|
|
signature
|
2018-03-04 22:16:24 +01:00
|
|
|
);
|
2017-10-03 16:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._handle.verify(key, signature, rsaPadding, pssSaltLength);
|
2017-09-06 17:10:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Sign,
|
|
|
|
Verify
|
|
|
|
};
|