2015-05-19 11:00:06 +00:00
|
|
|
'use strict';
|
2016-12-30 23:38:06 +00:00
|
|
|
const common = require('../common');
|
2017-06-30 23:29:09 +00:00
|
|
|
if (!common.hasCrypto)
|
|
|
|
common.skip('missing crypto');
|
|
|
|
|
2016-12-30 23:38:06 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const fs = require('fs');
|
2017-03-05 23:41:26 +00:00
|
|
|
const path = require('path');
|
|
|
|
const exec = require('child_process').exec;
|
2016-12-30 23:38:06 +00:00
|
|
|
const crypto = require('crypto');
|
2017-07-17 22:33:46 +00:00
|
|
|
const fixtures = require('../common/fixtures');
|
2015-02-12 08:19:25 +00:00
|
|
|
|
|
|
|
// Test certificates
|
2017-07-17 22:33:46 +00:00
|
|
|
const certPem = fixtures.readSync('test_cert.pem', 'ascii');
|
|
|
|
const keyPem = fixtures.readSync('test_key.pem', 'ascii');
|
2017-03-05 23:41:26 +00:00
|
|
|
const modSize = 1024;
|
2015-02-12 08:19:25 +00:00
|
|
|
|
2017-12-02 13:03:04 +00:00
|
|
|
{
|
|
|
|
const Sign = crypto.Sign;
|
|
|
|
const instance = Sign('SHA256');
|
|
|
|
assert(instance instanceof Sign, 'Sign is expected to return a new ' +
|
|
|
|
'instance when called without `new`');
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const Verify = crypto.Verify;
|
|
|
|
const instance = Verify('SHA256');
|
|
|
|
assert(instance instanceof Verify, 'Verify is expected to return a new ' +
|
|
|
|
'instance when called without `new`');
|
|
|
|
}
|
|
|
|
|
|
|
|
common.expectsError(
|
|
|
|
() => crypto.createVerify('SHA256').verify({
|
|
|
|
key: certPem,
|
2019-03-26 11:16:30 +00:00
|
|
|
padding: null,
|
2017-12-02 13:03:04 +00:00
|
|
|
}, ''),
|
|
|
|
{
|
|
|
|
code: 'ERR_INVALID_OPT_VALUE',
|
2017-12-11 05:56:41 +00:00
|
|
|
type: TypeError,
|
2019-03-26 11:16:30 +00:00
|
|
|
message: 'The value "null" is invalid for option "padding"'
|
2017-12-02 13:03:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
common.expectsError(
|
|
|
|
() => crypto.createVerify('SHA256').verify({
|
|
|
|
key: certPem,
|
2019-03-26 11:16:30 +00:00
|
|
|
saltLength: null,
|
2017-12-02 13:03:04 +00:00
|
|
|
}, ''),
|
|
|
|
{
|
|
|
|
code: 'ERR_INVALID_OPT_VALUE',
|
2017-12-11 05:56:41 +00:00
|
|
|
type: TypeError,
|
2019-03-26 11:16:30 +00:00
|
|
|
message: 'The value "null" is invalid for option "saltLength"'
|
2017-12-02 13:03:04 +00:00
|
|
|
});
|
|
|
|
|
2015-02-12 08:19:25 +00:00
|
|
|
// Test signing and verifying
|
2016-01-30 23:43:38 +00:00
|
|
|
{
|
2017-09-09 22:41:56 +00:00
|
|
|
const s1 = crypto.createSign('SHA1')
|
2016-01-30 23:43:38 +00:00
|
|
|
.update('Test123')
|
|
|
|
.sign(keyPem, 'base64');
|
2017-09-09 22:41:56 +00:00
|
|
|
let s1stream = crypto.createSign('SHA1');
|
2016-01-30 23:43:38 +00:00
|
|
|
s1stream.end('Test123');
|
|
|
|
s1stream = s1stream.sign(keyPem, 'base64');
|
2017-10-06 18:55:28 +00:00
|
|
|
assert.strictEqual(s1, s1stream, `${s1} should equal ${s1stream}`);
|
2015-02-12 08:19:25 +00:00
|
|
|
|
2017-09-09 22:41:56 +00:00
|
|
|
const verified = crypto.createVerify('SHA1')
|
2016-01-30 23:43:38 +00:00
|
|
|
.update('Test')
|
|
|
|
.update('123')
|
|
|
|
.verify(certPem, s1, 'base64');
|
2018-03-10 21:27:10 +00:00
|
|
|
assert.strictEqual(verified, true);
|
2016-01-30 23:43:38 +00:00
|
|
|
}
|
2015-02-12 08:19:25 +00:00
|
|
|
|
2016-01-30 23:43:38 +00:00
|
|
|
{
|
2017-09-09 22:41:56 +00:00
|
|
|
const s2 = crypto.createSign('SHA256')
|
2016-01-30 23:43:38 +00:00
|
|
|
.update('Test123')
|
2016-06-02 16:55:36 +00:00
|
|
|
.sign(keyPem, 'latin1');
|
2017-09-09 22:41:56 +00:00
|
|
|
let s2stream = crypto.createSign('SHA256');
|
2016-01-30 23:43:38 +00:00
|
|
|
s2stream.end('Test123');
|
2016-06-02 16:55:36 +00:00
|
|
|
s2stream = s2stream.sign(keyPem, 'latin1');
|
2017-10-06 18:55:28 +00:00
|
|
|
assert.strictEqual(s2, s2stream, `${s2} should equal ${s2stream}`);
|
2015-02-12 08:19:25 +00:00
|
|
|
|
2017-09-09 22:41:56 +00:00
|
|
|
let verified = crypto.createVerify('SHA256')
|
2016-01-30 23:43:38 +00:00
|
|
|
.update('Test')
|
|
|
|
.update('123')
|
2016-06-02 16:55:36 +00:00
|
|
|
.verify(certPem, s2, 'latin1');
|
2018-03-10 21:27:10 +00:00
|
|
|
assert.strictEqual(verified, true);
|
2015-02-12 08:19:25 +00:00
|
|
|
|
2017-09-09 22:41:56 +00:00
|
|
|
const verStream = crypto.createVerify('SHA256');
|
2016-01-30 23:43:38 +00:00
|
|
|
verStream.write('Tes');
|
|
|
|
verStream.write('t12');
|
|
|
|
verStream.end('3');
|
2016-06-02 16:55:36 +00:00
|
|
|
verified = verStream.verify(certPem, s2, 'latin1');
|
2018-03-10 21:27:10 +00:00
|
|
|
assert.strictEqual(verified, true);
|
2016-01-30 23:43:38 +00:00
|
|
|
}
|
2015-02-12 08:19:25 +00:00
|
|
|
|
2016-01-30 23:43:38 +00:00
|
|
|
{
|
2017-09-09 22:41:56 +00:00
|
|
|
const s3 = crypto.createSign('SHA1')
|
2016-01-30 23:43:38 +00:00
|
|
|
.update('Test123')
|
|
|
|
.sign(keyPem, 'buffer');
|
2017-09-09 22:41:56 +00:00
|
|
|
let verified = crypto.createVerify('SHA1')
|
2016-01-30 23:43:38 +00:00
|
|
|
.update('Test')
|
|
|
|
.update('123')
|
|
|
|
.verify(certPem, s3);
|
2018-03-10 21:27:10 +00:00
|
|
|
assert.strictEqual(verified, true);
|
2015-02-12 08:19:25 +00:00
|
|
|
|
2017-09-09 22:41:56 +00:00
|
|
|
const verStream = crypto.createVerify('SHA1');
|
2016-01-30 23:43:38 +00:00
|
|
|
verStream.write('Tes');
|
|
|
|
verStream.write('t12');
|
|
|
|
verStream.end('3');
|
|
|
|
verified = verStream.verify(certPem, s3);
|
2018-03-10 21:27:10 +00:00
|
|
|
assert.strictEqual(verified, true);
|
2016-01-30 23:43:38 +00:00
|
|
|
}
|
2017-02-10 04:05:45 +00:00
|
|
|
|
2017-03-05 23:41:26 +00:00
|
|
|
// Special tests for RSA_PKCS1_PSS_PADDING
|
|
|
|
{
|
|
|
|
function testPSS(algo, hLen) {
|
|
|
|
// Maximum permissible salt length
|
|
|
|
const max = modSize / 8 - hLen - 2;
|
|
|
|
|
|
|
|
function getEffectiveSaltLength(saltLength) {
|
|
|
|
switch (saltLength) {
|
|
|
|
case crypto.constants.RSA_PSS_SALTLEN_DIGEST:
|
|
|
|
return hLen;
|
|
|
|
case crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN:
|
|
|
|
return max;
|
|
|
|
default:
|
|
|
|
return saltLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const signSaltLengths = [
|
|
|
|
crypto.constants.RSA_PSS_SALTLEN_DIGEST,
|
|
|
|
getEffectiveSaltLength(crypto.constants.RSA_PSS_SALTLEN_DIGEST),
|
|
|
|
crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN,
|
|
|
|
getEffectiveSaltLength(crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN),
|
|
|
|
0, 16, 32, 64, 128
|
|
|
|
];
|
|
|
|
|
|
|
|
const verifySaltLengths = [
|
|
|
|
crypto.constants.RSA_PSS_SALTLEN_DIGEST,
|
|
|
|
getEffectiveSaltLength(crypto.constants.RSA_PSS_SALTLEN_DIGEST),
|
|
|
|
getEffectiveSaltLength(crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN),
|
|
|
|
0, 16, 32, 64, 128
|
|
|
|
];
|
2017-06-18 13:22:32 +00:00
|
|
|
const errMessage = /^Error:.*data too large for key size$/;
|
2017-03-05 23:41:26 +00:00
|
|
|
|
|
|
|
signSaltLengths.forEach((signSaltLength) => {
|
|
|
|
if (signSaltLength > max) {
|
|
|
|
// If the salt length is too big, an Error should be thrown
|
|
|
|
assert.throws(() => {
|
|
|
|
crypto.createSign(algo)
|
|
|
|
.update('Test123')
|
|
|
|
.sign({
|
|
|
|
key: keyPem,
|
|
|
|
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
|
|
saltLength: signSaltLength
|
|
|
|
});
|
2017-06-18 13:22:32 +00:00
|
|
|
}, errMessage);
|
2017-03-05 23:41:26 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise, a valid signature should be generated
|
|
|
|
const s4 = crypto.createSign(algo)
|
|
|
|
.update('Test123')
|
|
|
|
.sign({
|
|
|
|
key: keyPem,
|
|
|
|
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
|
|
saltLength: signSaltLength
|
|
|
|
});
|
|
|
|
|
|
|
|
let verified;
|
|
|
|
verifySaltLengths.forEach((verifySaltLength) => {
|
|
|
|
// Verification should succeed if and only if the salt length is
|
|
|
|
// correct
|
|
|
|
verified = crypto.createVerify(algo)
|
|
|
|
.update('Test123')
|
|
|
|
.verify({
|
|
|
|
key: certPem,
|
|
|
|
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
|
|
saltLength: verifySaltLength
|
|
|
|
}, s4);
|
2017-04-13 20:26:00 +00:00
|
|
|
const saltLengthCorrect = getEffectiveSaltLength(signSaltLength) ===
|
2017-03-05 23:41:26 +00:00
|
|
|
getEffectiveSaltLength(verifySaltLength);
|
2018-03-10 21:27:10 +00:00
|
|
|
assert.strictEqual(verified, saltLengthCorrect);
|
2017-03-05 23:41:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Verification using RSA_PSS_SALTLEN_AUTO should always work
|
|
|
|
verified = crypto.createVerify(algo)
|
|
|
|
.update('Test123')
|
|
|
|
.verify({
|
|
|
|
key: certPem,
|
|
|
|
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
|
|
saltLength: crypto.constants.RSA_PSS_SALTLEN_AUTO
|
|
|
|
}, s4);
|
2018-03-10 21:27:10 +00:00
|
|
|
assert.strictEqual(verified, true);
|
2017-03-05 23:41:26 +00:00
|
|
|
|
|
|
|
// Verifying an incorrect message should never work
|
|
|
|
verified = crypto.createVerify(algo)
|
|
|
|
.update('Test1234')
|
|
|
|
.verify({
|
|
|
|
key: certPem,
|
|
|
|
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
|
|
saltLength: crypto.constants.RSA_PSS_SALTLEN_AUTO
|
|
|
|
}, s4);
|
2018-03-10 21:27:10 +00:00
|
|
|
assert.strictEqual(verified, false);
|
2017-03-05 23:41:26 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-09 22:41:56 +00:00
|
|
|
testPSS('SHA1', 20);
|
|
|
|
testPSS('SHA256', 32);
|
2017-03-05 23:41:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test vectors for RSA_PKCS1_PSS_PADDING provided by the RSA Laboratories:
|
|
|
|
// https://www.emc.com/emc-plus/rsa-labs/standards-initiatives/pkcs-rsa-cryptography-standard.htm
|
|
|
|
{
|
|
|
|
// We only test verification as we cannot specify explicit salts when signing
|
|
|
|
function testVerify(cert, vector) {
|
2017-09-09 22:41:56 +00:00
|
|
|
const verified = crypto.createVerify('SHA1')
|
2017-03-05 23:41:26 +00:00
|
|
|
.update(Buffer.from(vector.message, 'hex'))
|
|
|
|
.verify({
|
|
|
|
key: cert,
|
|
|
|
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
|
|
saltLength: vector.salt.length / 2
|
|
|
|
}, vector.signature, 'hex');
|
2018-03-10 21:27:10 +00:00
|
|
|
assert.strictEqual(verified, true);
|
2017-03-05 23:41:26 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 22:33:46 +00:00
|
|
|
const examples = JSON.parse(fixtures.readSync('pss-vectors.json', 'utf8'));
|
2017-03-05 23:41:26 +00:00
|
|
|
|
|
|
|
for (const key in examples) {
|
|
|
|
const example = examples[key];
|
|
|
|
const publicKey = example.publicKey.join('\n');
|
|
|
|
example.tests.forEach((test) => testVerify(publicKey, test));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test exceptions for invalid `padding` and `saltLength` values
|
|
|
|
{
|
2019-03-26 11:16:30 +00:00
|
|
|
[null, NaN, 'boom', {}, [], true, false]
|
2017-03-05 23:41:26 +00:00
|
|
|
.forEach((invalidValue) => {
|
2017-09-06 15:10:34 +00:00
|
|
|
common.expectsError(() => {
|
2017-09-09 22:41:56 +00:00
|
|
|
crypto.createSign('SHA256')
|
2017-03-05 23:41:26 +00:00
|
|
|
.update('Test123')
|
|
|
|
.sign({
|
|
|
|
key: keyPem,
|
|
|
|
padding: invalidValue
|
|
|
|
});
|
2017-09-06 15:10:34 +00:00
|
|
|
}, {
|
|
|
|
code: 'ERR_INVALID_OPT_VALUE',
|
|
|
|
type: TypeError
|
|
|
|
});
|
2017-03-05 23:41:26 +00:00
|
|
|
|
2017-09-06 15:10:34 +00:00
|
|
|
common.expectsError(() => {
|
2017-09-09 22:41:56 +00:00
|
|
|
crypto.createSign('SHA256')
|
2017-03-05 23:41:26 +00:00
|
|
|
.update('Test123')
|
|
|
|
.sign({
|
|
|
|
key: keyPem,
|
|
|
|
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
|
|
saltLength: invalidValue
|
|
|
|
});
|
2017-09-06 15:10:34 +00:00
|
|
|
}, {
|
|
|
|
code: 'ERR_INVALID_OPT_VALUE',
|
|
|
|
type: TypeError
|
|
|
|
});
|
2017-03-05 23:41:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.throws(() => {
|
2017-09-09 22:41:56 +00:00
|
|
|
crypto.createSign('SHA1')
|
2017-03-05 23:41:26 +00:00
|
|
|
.update('Test123')
|
|
|
|
.sign({
|
|
|
|
key: keyPem,
|
|
|
|
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING
|
|
|
|
});
|
|
|
|
}, /^Error:.*illegal or unsupported padding mode$/);
|
|
|
|
}
|
|
|
|
|
2017-02-10 04:05:45 +00:00
|
|
|
// Test throws exception when key options is null
|
|
|
|
{
|
2017-09-06 15:10:34 +00:00
|
|
|
common.expectsError(() => {
|
2017-09-09 22:41:56 +00:00
|
|
|
crypto.createSign('SHA1').update('Test123').sign(null, 'base64');
|
2017-09-06 15:10:34 +00:00
|
|
|
}, {
|
|
|
|
code: 'ERR_CRYPTO_SIGN_KEY_REQUIRED',
|
|
|
|
type: Error
|
|
|
|
});
|
2017-02-10 04:05:45 +00:00
|
|
|
}
|
2017-03-05 23:41:26 +00:00
|
|
|
|
|
|
|
// RSA-PSS Sign test by verifying with 'openssl dgst -verify'
|
|
|
|
{
|
2017-06-30 23:29:09 +00:00
|
|
|
if (!common.opensslCli)
|
2017-03-05 23:41:26 +00:00
|
|
|
common.skip('node compiled without OpenSSL CLI.');
|
|
|
|
|
2017-07-17 22:33:46 +00:00
|
|
|
const pubfile = fixtures.path('keys', 'rsa_public_2048.pem');
|
|
|
|
const privkey = fixtures.readKey('rsa_private_2048.pem');
|
2017-03-05 23:41:26 +00:00
|
|
|
|
|
|
|
const msg = 'Test123';
|
2017-09-09 22:41:56 +00:00
|
|
|
const s5 = crypto.createSign('SHA256')
|
2017-03-05 23:41:26 +00:00
|
|
|
.update(msg)
|
|
|
|
.sign({
|
|
|
|
key: privkey,
|
|
|
|
padding: crypto.constants.RSA_PKCS1_PSS_PADDING
|
|
|
|
});
|
|
|
|
|
2017-12-25 06:38:11 +00:00
|
|
|
const tmpdir = require('../common/tmpdir');
|
|
|
|
tmpdir.refresh();
|
2017-03-05 23:41:26 +00:00
|
|
|
|
2017-12-25 06:38:11 +00:00
|
|
|
const sigfile = path.join(tmpdir.path, 's5.sig');
|
2017-03-05 23:41:26 +00:00
|
|
|
fs.writeFileSync(sigfile, s5);
|
2017-12-25 06:38:11 +00:00
|
|
|
const msgfile = path.join(tmpdir.path, 's5.msg');
|
2017-03-05 23:41:26 +00:00
|
|
|
fs.writeFileSync(msgfile, msg);
|
|
|
|
|
2017-04-28 01:06:42 +00:00
|
|
|
const cmd =
|
|
|
|
`"${common.opensslCli}" dgst -sha256 -verify "${pubfile}" -signature "${
|
2017-07-25 17:37:08 +00:00
|
|
|
sigfile}" -sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:-2 "${
|
|
|
|
msgfile}"`;
|
2017-03-05 23:41:26 +00:00
|
|
|
|
|
|
|
exec(cmd, common.mustCall((err, stdout, stderr) => {
|
|
|
|
assert(stdout.includes('Verified OK'));
|
|
|
|
}));
|
|
|
|
}
|
2017-10-03 14:28:26 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
const sign = crypto.createSign('SHA1');
|
|
|
|
const verify = crypto.createVerify('SHA1');
|
|
|
|
|
2018-03-19 12:33:46 +00:00
|
|
|
[1, [], {}, undefined, null, true, Infinity].forEach((input) => {
|
|
|
|
const type = typeof input;
|
|
|
|
const errObj = {
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-03-16 11:09:14 +00:00
|
|
|
name: 'TypeError',
|
2018-03-19 12:33:46 +00:00
|
|
|
message: 'The "algorithm" argument must be of type string. ' +
|
|
|
|
`Received type ${type}`
|
|
|
|
};
|
|
|
|
assert.throws(() => crypto.createSign(input), errObj);
|
|
|
|
assert.throws(() => crypto.createVerify(input), errObj);
|
|
|
|
|
|
|
|
errObj.message = 'The "data" argument must be one of type string, ' +
|
|
|
|
`Buffer, TypedArray, or DataView. Received type ${type}`;
|
|
|
|
assert.throws(() => sign.update(input), errObj);
|
|
|
|
assert.throws(() => verify.update(input), errObj);
|
|
|
|
assert.throws(() => sign._write(input, 'utf8', () => {}), errObj);
|
|
|
|
assert.throws(() => verify._write(input, 'utf8', () => {}), errObj);
|
2017-10-03 14:28:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
[
|
|
|
|
Uint8Array, Uint16Array, Uint32Array, Float32Array, Float64Array
|
2018-03-19 12:33:46 +00:00
|
|
|
].forEach((clazz) => {
|
2017-10-03 14:28:26 +00:00
|
|
|
// These should all just work
|
2018-03-19 12:33:46 +00:00
|
|
|
sign.update(new clazz());
|
|
|
|
verify.update(new clazz());
|
2017-10-03 14:28:26 +00:00
|
|
|
});
|
|
|
|
|
2018-03-19 12:33:46 +00:00
|
|
|
[1, {}, [], Infinity].forEach((input) => {
|
|
|
|
const type = typeof input;
|
|
|
|
const errObj = {
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-03-16 11:09:14 +00:00
|
|
|
name: 'TypeError',
|
2018-03-19 12:33:46 +00:00
|
|
|
message: 'The "key" argument must be one of type string, Buffer, ' +
|
2018-09-20 17:53:44 +00:00
|
|
|
`TypedArray, DataView, or KeyObject. Received type ${type}`
|
2018-03-19 12:33:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
assert.throws(() => sign.sign(input), errObj);
|
|
|
|
assert.throws(() => verify.verify(input), errObj);
|
|
|
|
|
|
|
|
errObj.message = 'The "signature" argument must be one of type string, ' +
|
|
|
|
`Buffer, TypedArray, or DataView. Received type ${type}`;
|
|
|
|
assert.throws(() => verify.verify('test', input), errObj);
|
2017-10-03 14:28:26 +00:00
|
|
|
});
|
|
|
|
}
|
2019-01-07 19:27:31 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
assert.throws(
|
|
|
|
() => crypto.createSign('sha8'),
|
|
|
|
/Unknown message digest/);
|
|
|
|
}
|