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 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
|
2019-05-29 18:43:44 +00:00
|
|
|
const certPem = fixtures.readKey('rsa_cert.crt');
|
|
|
|
const keyPem = fixtures.readKey('rsa_private.pem');
|
|
|
|
const keySize = 2048;
|
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`');
|
|
|
|
}
|
|
|
|
|
2019-04-09 00:11:14 +00:00
|
|
|
// Test handling of exceptional conditions
|
|
|
|
{
|
|
|
|
const library = {
|
|
|
|
configurable: true,
|
|
|
|
set() {
|
|
|
|
throw new Error('bye, bye, library');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Object.defineProperty(Object.prototype, 'library', library);
|
|
|
|
|
|
|
|
assert.throws(() => {
|
|
|
|
crypto.createSign('sha1').sign(
|
|
|
|
`-----BEGIN RSA PRIVATE KEY-----
|
|
|
|
AAAAAAAAAAAA
|
|
|
|
-----END RSA PRIVATE KEY-----`);
|
2021-04-28 10:49:26 +00:00
|
|
|
}, { message: 'bye, bye, library' });
|
2019-04-09 00:11:14 +00:00
|
|
|
|
|
|
|
delete Object.prototype.library;
|
|
|
|
|
|
|
|
const errorStack = {
|
|
|
|
configurable: true,
|
|
|
|
set() {
|
|
|
|
throw new Error('bye, bye, error stack');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Object.defineProperty(Object.prototype, 'opensslErrorStack', errorStack);
|
|
|
|
|
|
|
|
assert.throws(() => {
|
|
|
|
crypto.createSign('SHA1')
|
|
|
|
.update('Test123')
|
|
|
|
.sign({
|
|
|
|
key: keyPem,
|
|
|
|
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING
|
|
|
|
});
|
2020-10-26 06:23:55 +00:00
|
|
|
}, { message: common.hasOpenSSL3 ?
|
|
|
|
'error:1C8000A5:Provider routines::illegal or unsupported padding mode' :
|
|
|
|
'bye, bye, error stack' });
|
2019-04-09 00:11:14 +00:00
|
|
|
|
|
|
|
delete Object.prototype.opensslErrorStack;
|
|
|
|
}
|
|
|
|
|
2019-12-25 17:02:16 +00:00
|
|
|
assert.throws(
|
2017-12-02 13:03:04 +00:00
|
|
|
() => crypto.createVerify('SHA256').verify({
|
|
|
|
key: certPem,
|
2019-03-26 11:16:30 +00:00
|
|
|
padding: null,
|
2017-12-02 13:03:04 +00:00
|
|
|
}, ''),
|
|
|
|
{
|
2020-08-08 16:01:59 +00:00
|
|
|
code: 'ERR_INVALID_ARG_VALUE',
|
2019-12-25 17:02:16 +00:00
|
|
|
name: 'TypeError',
|
2020-08-08 16:01:59 +00:00
|
|
|
message: "The property 'options.padding' is invalid. Received null",
|
2017-12-02 13:03:04 +00:00
|
|
|
});
|
|
|
|
|
2019-12-25 17:02:16 +00:00
|
|
|
assert.throws(
|
2017-12-02 13:03:04 +00:00
|
|
|
() => crypto.createVerify('SHA256').verify({
|
|
|
|
key: certPem,
|
2019-03-26 11:16:30 +00:00
|
|
|
saltLength: null,
|
2017-12-02 13:03:04 +00:00
|
|
|
}, ''),
|
|
|
|
{
|
2020-08-08 16:01:59 +00:00
|
|
|
code: 'ERR_INVALID_ARG_VALUE',
|
2019-12-25 17:02:16 +00:00
|
|
|
name: 'TypeError',
|
2020-08-08 16:01:59 +00:00
|
|
|
message: "The property 'options.saltLength' is invalid. Received null",
|
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
|
2019-05-29 18:43:44 +00:00
|
|
|
const max = keySize / 8 - hLen - 2;
|
2017-03-05 23:41:26 +00:00
|
|
|
|
|
|
|
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),
|
2021-03-26 15:51:08 +00:00
|
|
|
0, 16, 32, 64, 128,
|
2017-03-05 23:41:26 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
const verifySaltLengths = [
|
|
|
|
crypto.constants.RSA_PSS_SALTLEN_DIGEST,
|
|
|
|
getEffectiveSaltLength(crypto.constants.RSA_PSS_SALTLEN_DIGEST),
|
|
|
|
getEffectiveSaltLength(crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN),
|
2021-03-26 15:51:08 +00:00
|
|
|
0, 16, 32, 64, 128,
|
2017-03-05 23:41:26 +00:00
|
|
|
];
|
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
|
|
|
|
2019-03-12 13:17:10 +00:00
|
|
|
const data = Buffer.from('Test123');
|
|
|
|
|
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)
|
2019-03-12 13:17:10 +00:00
|
|
|
.update(data)
|
2017-03-05 23:41:26 +00:00
|
|
|
.sign({
|
|
|
|
key: keyPem,
|
|
|
|
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
|
|
saltLength: signSaltLength
|
|
|
|
});
|
2017-06-18 13:22:32 +00:00
|
|
|
}, errMessage);
|
2019-03-12 13:17:10 +00:00
|
|
|
assert.throws(() => {
|
|
|
|
crypto.sign(algo, data, {
|
|
|
|
key: keyPem,
|
|
|
|
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
|
|
saltLength: signSaltLength
|
|
|
|
});
|
|
|
|
}, errMessage);
|
2017-03-05 23:41:26 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise, a valid signature should be generated
|
|
|
|
const s4 = crypto.createSign(algo)
|
2019-03-12 13:17:10 +00:00
|
|
|
.update(data)
|
2017-03-05 23:41:26 +00:00
|
|
|
.sign({
|
|
|
|
key: keyPem,
|
|
|
|
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
|
|
saltLength: signSaltLength
|
|
|
|
});
|
2019-03-12 13:17:10 +00:00
|
|
|
const s4_2 = crypto.sign(algo, data, {
|
|
|
|
key: keyPem,
|
|
|
|
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
|
|
saltLength: signSaltLength
|
|
|
|
});
|
2017-03-05 23:41:26 +00:00
|
|
|
|
2019-03-12 13:17:10 +00:00
|
|
|
[s4, s4_2].forEach((sig) => {
|
|
|
|
let verified;
|
|
|
|
verifySaltLengths.forEach((verifySaltLength) => {
|
|
|
|
// Verification should succeed if and only if the salt length is
|
|
|
|
// correct
|
|
|
|
verified = crypto.createVerify(algo)
|
|
|
|
.update(data)
|
|
|
|
.verify({
|
|
|
|
key: certPem,
|
|
|
|
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
|
|
saltLength: verifySaltLength
|
|
|
|
}, sig);
|
|
|
|
assert.strictEqual(verified, crypto.verify(algo, data, {
|
|
|
|
key: certPem,
|
|
|
|
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
|
|
saltLength: verifySaltLength
|
|
|
|
}, sig));
|
|
|
|
const saltLengthCorrect = getEffectiveSaltLength(signSaltLength) ===
|
|
|
|
getEffectiveSaltLength(verifySaltLength);
|
|
|
|
assert.strictEqual(verified, saltLengthCorrect);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Verification using RSA_PSS_SALTLEN_AUTO should always work
|
2017-03-05 23:41:26 +00:00
|
|
|
verified = crypto.createVerify(algo)
|
2019-03-12 13:17:10 +00:00
|
|
|
.update(data)
|
2017-03-05 23:41:26 +00:00
|
|
|
.verify({
|
|
|
|
key: certPem,
|
|
|
|
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
2019-03-12 13:17:10 +00:00
|
|
|
saltLength: crypto.constants.RSA_PSS_SALTLEN_AUTO
|
|
|
|
}, sig);
|
|
|
|
assert.strictEqual(verified, true);
|
|
|
|
assert.strictEqual(verified, crypto.verify(algo, data, {
|
|
|
|
key: certPem,
|
|
|
|
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
|
|
saltLength: crypto.constants.RSA_PSS_SALTLEN_AUTO
|
|
|
|
}, sig));
|
2017-03-05 23:41:26 +00:00
|
|
|
|
2019-03-12 13:17:10 +00:00
|
|
|
// Verifying an incorrect message should never work
|
|
|
|
const wrongData = Buffer.from('Test1234');
|
|
|
|
verified = crypto.createVerify(algo)
|
|
|
|
.update(wrongData)
|
|
|
|
.verify({
|
|
|
|
key: certPem,
|
|
|
|
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
|
|
saltLength: crypto.constants.RSA_PSS_SALTLEN_AUTO
|
|
|
|
}, sig);
|
|
|
|
assert.strictEqual(verified, false);
|
|
|
|
assert.strictEqual(verified, crypto.verify(algo, wrongData, {
|
|
|
|
key: certPem,
|
|
|
|
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
|
|
saltLength: crypto.constants.RSA_PSS_SALTLEN_AUTO
|
|
|
|
}, sig));
|
|
|
|
});
|
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) => {
|
2019-12-25 17:02:16 +00:00
|
|
|
assert.throws(() => {
|
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
|
|
|
}, {
|
2020-08-08 16:01:59 +00:00
|
|
|
code: 'ERR_INVALID_ARG_VALUE',
|
2019-12-25 17:02:16 +00:00
|
|
|
name: 'TypeError'
|
2017-09-06 15:10:34 +00:00
|
|
|
});
|
2017-03-05 23:41:26 +00:00
|
|
|
|
2019-12-25 17:02:16 +00:00
|
|
|
assert.throws(() => {
|
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
|
|
|
}, {
|
2020-08-08 16:01:59 +00:00
|
|
|
code: 'ERR_INVALID_ARG_VALUE',
|
2019-12-25 17:02:16 +00:00
|
|
|
name: 'TypeError'
|
2017-09-06 15:10:34 +00:00
|
|
|
});
|
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
|
|
|
|
});
|
2020-10-26 06:23:55 +00:00
|
|
|
}, common.hasOpenSSL3 ? {
|
|
|
|
code: 'ERR_OSSL_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE',
|
|
|
|
message: /illegal or unsupported padding mode/,
|
|
|
|
} : {
|
2019-04-09 00:11:14 +00:00
|
|
|
code: 'ERR_OSSL_RSA_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE',
|
|
|
|
message: /illegal or unsupported padding mode/,
|
|
|
|
opensslErrorStack: [
|
|
|
|
'error:06089093:digital envelope routines:EVP_PKEY_CTX_ctrl:' +
|
|
|
|
'command not supported',
|
|
|
|
],
|
|
|
|
});
|
2017-03-05 23:41:26 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 04:05:45 +00:00
|
|
|
// Test throws exception when key options is null
|
|
|
|
{
|
2019-12-25 17:02:16 +00:00
|
|
|
assert.throws(() => {
|
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',
|
2019-12-25 17:02:16 +00:00
|
|
|
name: 'Error'
|
2017-09-06 15:10:34 +00:00
|
|
|
});
|
2017-02-10 04:05:45 +00:00
|
|
|
}
|
2017-03-05 23:41:26 +00:00
|
|
|
|
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 errObj = {
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-03-16 11:09:14 +00:00
|
|
|
name: 'TypeError',
|
2019-09-23 06:17:25 +00:00
|
|
|
message: 'The "algorithm" argument must be of type string.' +
|
|
|
|
`${common.invalidArgTypeHelper(input)}`
|
2018-03-19 12:33:46 +00:00
|
|
|
};
|
|
|
|
assert.throws(() => crypto.createSign(input), errObj);
|
|
|
|
assert.throws(() => crypto.createVerify(input), errObj);
|
|
|
|
|
2019-09-23 06:17:25 +00:00
|
|
|
errObj.message = 'The "data" argument must be of type string or an ' +
|
|
|
|
'instance of Buffer, TypedArray, or DataView.' +
|
|
|
|
common.invalidArgTypeHelper(input);
|
2018-03-19 12:33:46 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
[
|
2021-03-26 15:51:08 +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 errObj = {
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-03-16 11:09:14 +00:00
|
|
|
name: 'TypeError',
|
2018-03-19 12:33:46 +00:00
|
|
|
};
|
|
|
|
assert.throws(() => sign.sign(input), errObj);
|
|
|
|
assert.throws(() => verify.verify(input), errObj);
|
|
|
|
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'),
|
2020-08-25 17:05:51 +00:00
|
|
|
/Invalid digest/);
|
2019-03-12 13:17:10 +00:00
|
|
|
assert.throws(
|
|
|
|
() => crypto.sign('sha8', Buffer.alloc(1), keyPem),
|
2020-08-25 17:05:51 +00:00
|
|
|
/Invalid digest/);
|
2019-03-12 13:17:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[
|
2019-05-28 18:39:24 +00:00
|
|
|
{ private: fixtures.readKey('ed25519_private.pem', 'ascii'),
|
|
|
|
public: fixtures.readKey('ed25519_public.pem', 'ascii'),
|
2019-03-12 13:17:10 +00:00
|
|
|
algo: null,
|
|
|
|
sigLen: 64 },
|
2019-05-28 21:40:58 +00:00
|
|
|
{ private: fixtures.readKey('ed448_private.pem', 'ascii'),
|
|
|
|
public: fixtures.readKey('ed448_public.pem', 'ascii'),
|
2019-03-12 13:17:10 +00:00
|
|
|
algo: null,
|
|
|
|
sigLen: 114 },
|
|
|
|
{ private: fixtures.readKey('rsa_private_2048.pem', 'ascii'),
|
|
|
|
public: fixtures.readKey('rsa_public_2048.pem', 'ascii'),
|
|
|
|
algo: 'sha1',
|
2021-03-26 15:51:08 +00:00
|
|
|
sigLen: 256 },
|
2019-03-12 13:17:10 +00:00
|
|
|
].forEach((pair) => {
|
|
|
|
const algo = pair.algo;
|
|
|
|
|
|
|
|
{
|
|
|
|
const data = Buffer.from('Hello world');
|
|
|
|
const sig = crypto.sign(algo, data, pair.private);
|
|
|
|
assert.strictEqual(sig.length, pair.sigLen);
|
|
|
|
|
|
|
|
assert.strictEqual(crypto.verify(algo, data, pair.private, sig),
|
|
|
|
true);
|
|
|
|
assert.strictEqual(crypto.verify(algo, data, pair.public, sig),
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const data = Buffer.from('Hello world');
|
|
|
|
const privKeyObj = crypto.createPrivateKey(pair.private);
|
|
|
|
const pubKeyObj = crypto.createPublicKey(pair.public);
|
|
|
|
|
|
|
|
const sig = crypto.sign(algo, data, privKeyObj);
|
|
|
|
assert.strictEqual(sig.length, pair.sigLen);
|
|
|
|
|
|
|
|
assert.strictEqual(crypto.verify(algo, data, privKeyObj, sig), true);
|
|
|
|
assert.strictEqual(crypto.verify(algo, data, pubKeyObj, sig), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const data = Buffer.from('Hello world');
|
|
|
|
const otherData = Buffer.from('Goodbye world');
|
|
|
|
const otherSig = crypto.sign(algo, otherData, pair.private);
|
|
|
|
assert.strictEqual(crypto.verify(algo, data, pair.private, otherSig),
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
|
|
|
[
|
2021-03-26 15:51:08 +00:00
|
|
|
Uint8Array, Uint16Array, Uint32Array, Float32Array, Float64Array,
|
2019-03-12 13:17:10 +00:00
|
|
|
].forEach((clazz) => {
|
|
|
|
const data = new clazz();
|
|
|
|
const sig = crypto.sign(algo, data, pair.private);
|
|
|
|
assert.strictEqual(crypto.verify(algo, data, pair.private, sig),
|
|
|
|
true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
[1, {}, [], true, Infinity].forEach((input) => {
|
|
|
|
const data = Buffer.alloc(1);
|
|
|
|
const sig = Buffer.alloc(1);
|
|
|
|
const errObj = {
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
|
|
name: 'TypeError',
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.throws(() => crypto.sign(null, input, 'asdf'), errObj);
|
|
|
|
assert.throws(() => crypto.verify(null, input, 'asdf', sig), errObj);
|
|
|
|
|
|
|
|
assert.throws(() => crypto.sign(null, data, input), errObj);
|
|
|
|
assert.throws(() => crypto.verify(null, data, input, sig), errObj);
|
|
|
|
|
2019-09-23 06:17:25 +00:00
|
|
|
errObj.message = 'The "signature" argument must be an instance of ' +
|
crypto: fix wrong error message
When calling `crypto.sign()`, if the `key` parameter object is
missing the `key` property, the error message is wrong.
Before the fix:
TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be of
type string or an instance of Buffer, TypedArray, DataView, or
KeyObject. Received an instance of Object
Expected:
TypeError [ERR_INVALID_ARG_TYPE]: The "key.key property" argument
must be of type string or an instance of Buffer, TypedArray,
DataView, or KeyObject. Received undefined
This seems like a copy&paste bug. Somebody copied from the end of
the function, where this is correct, to here, where it's wrong.
PR-URL: https://github.com/nodejs/node/pull/33482
Fixes: https://github.com/nodejs/node/issues/33480
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Ujjwal Sharma <ryzokuken@disroot.org>
2020-05-20 12:13:29 +00:00
|
|
|
'Buffer, TypedArray, or DataView.' +
|
|
|
|
common.invalidArgTypeHelper(input);
|
2019-03-12 13:17:10 +00:00
|
|
|
assert.throws(() => crypto.verify(null, data, 'test', input), errObj);
|
|
|
|
});
|
|
|
|
|
|
|
|
{
|
|
|
|
const data = Buffer.from('Hello world');
|
2019-08-20 22:05:55 +00:00
|
|
|
const keys = [['ec-key.pem', 64], ['dsa_private_1025.pem', 40]];
|
|
|
|
|
|
|
|
for (const [file, length] of keys) {
|
|
|
|
const privKey = fixtures.readKey(file);
|
|
|
|
[
|
|
|
|
crypto.createSign('sha1').update(data).sign(privKey),
|
|
|
|
crypto.sign('sha1', data, privKey),
|
2021-03-26 15:51:08 +00:00
|
|
|
crypto.sign('sha1', data, { key: privKey, dsaEncoding: 'der' }),
|
2019-08-20 22:05:55 +00:00
|
|
|
].forEach((sig) => {
|
|
|
|
// Signature length variability due to DER encoding
|
|
|
|
assert(sig.length >= length + 4 && sig.length <= length + 8);
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
crypto.createVerify('sha1').update(data).verify(privKey, sig),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
assert.strictEqual(crypto.verify('sha1', data, privKey, sig), true);
|
|
|
|
});
|
2019-03-12 13:17:10 +00:00
|
|
|
|
2019-08-20 22:05:55 +00:00
|
|
|
// Test (EC)DSA signature conversion.
|
|
|
|
const opts = { key: privKey, dsaEncoding: 'ieee-p1363' };
|
|
|
|
let sig = crypto.sign('sha1', data, opts);
|
|
|
|
// Unlike DER signatures, IEEE P1363 signatures have a predictable length.
|
|
|
|
assert.strictEqual(sig.length, length);
|
|
|
|
assert.strictEqual(crypto.verify('sha1', data, opts, sig), true);
|
2020-02-20 00:16:14 +00:00
|
|
|
assert.strictEqual(crypto.createVerify('sha1')
|
|
|
|
.update(data)
|
|
|
|
.verify(opts, sig), true);
|
2019-08-20 22:05:55 +00:00
|
|
|
|
|
|
|
// Test invalid signature lengths.
|
|
|
|
for (const i of [-2, -1, 1, 2, 4, 8]) {
|
|
|
|
sig = crypto.randomBytes(length + i);
|
2021-03-21 10:04:26 +00:00
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = crypto.verify('sha1', data, opts, sig);
|
|
|
|
} catch (err) {
|
|
|
|
assert.match(err.message, /asn1 encoding/);
|
|
|
|
assert.strictEqual(err.library, 'asn1 encoding routines');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
assert.strictEqual(result, false);
|
2019-08-20 22:05:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test verifying externally signed messages.
|
|
|
|
const extSig = Buffer.from('494c18ab5c8a62a72aea5041966902bcfa229821af2bf65' +
|
|
|
|
'0b5b4870d1fe6aebeaed9460c62210693b5b0a300033823' +
|
|
|
|
'33d9529c8abd8c5948940af944828be16c', 'hex');
|
|
|
|
for (const ok of [true, false]) {
|
2019-03-12 13:17:10 +00:00
|
|
|
assert.strictEqual(
|
2019-08-20 22:05:55 +00:00
|
|
|
crypto.verify('sha256', data, {
|
|
|
|
key: fixtures.readKey('ec-key.pem'),
|
|
|
|
dsaEncoding: 'ieee-p1363'
|
|
|
|
}, extSig),
|
|
|
|
ok
|
2019-03-12 13:17:10 +00:00
|
|
|
);
|
2019-08-20 22:05:55 +00:00
|
|
|
|
2020-02-20 00:16:14 +00:00
|
|
|
assert.strictEqual(
|
|
|
|
crypto.createVerify('sha256').update(data).verify({
|
|
|
|
key: fixtures.readKey('ec-key.pem'),
|
|
|
|
dsaEncoding: 'ieee-p1363'
|
|
|
|
}, extSig),
|
|
|
|
ok
|
|
|
|
);
|
|
|
|
|
2019-08-20 22:05:55 +00:00
|
|
|
extSig[Math.floor(Math.random() * extSig.length)] ^= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Non-(EC)DSA keys should ignore the option.
|
|
|
|
const sig = crypto.sign('sha1', data, {
|
|
|
|
key: keyPem,
|
|
|
|
dsaEncoding: 'ieee-p1363'
|
2019-03-12 13:17:10 +00:00
|
|
|
});
|
2019-08-20 22:05:55 +00:00
|
|
|
assert.strictEqual(crypto.verify('sha1', data, certPem, sig), true);
|
|
|
|
assert.strictEqual(
|
|
|
|
crypto.verify('sha1', data, {
|
|
|
|
key: certPem,
|
|
|
|
dsaEncoding: 'ieee-p1363'
|
|
|
|
}, sig),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
assert.strictEqual(
|
|
|
|
crypto.verify('sha1', data, {
|
|
|
|
key: certPem,
|
|
|
|
dsaEncoding: 'der'
|
|
|
|
}, sig),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const dsaEncoding of ['foo', null, {}, 5, true, NaN]) {
|
2019-12-25 17:02:16 +00:00
|
|
|
assert.throws(() => {
|
2019-08-20 22:05:55 +00:00
|
|
|
crypto.sign('sha1', data, {
|
|
|
|
key: certPem,
|
|
|
|
dsaEncoding
|
|
|
|
});
|
|
|
|
}, {
|
2020-08-08 16:01:59 +00:00
|
|
|
code: 'ERR_INVALID_ARG_VALUE'
|
2019-08-20 22:05:55 +00:00
|
|
|
});
|
|
|
|
}
|
2019-03-12 13:17:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// RSA-PSS Sign test by verifying with 'openssl dgst -verify'
|
|
|
|
// Note: this particular test *must* be the last in this file as it will exit
|
|
|
|
// early if no openssl binary is found
|
|
|
|
{
|
|
|
|
if (!common.opensslCli)
|
|
|
|
common.skip('node compiled without OpenSSL CLI.');
|
|
|
|
|
|
|
|
const pubfile = fixtures.path('keys', 'rsa_public_2048.pem');
|
|
|
|
const privkey = fixtures.readKey('rsa_private_2048.pem');
|
|
|
|
|
|
|
|
const msg = 'Test123';
|
|
|
|
const s5 = crypto.createSign('SHA256')
|
|
|
|
.update(msg)
|
|
|
|
.sign({
|
|
|
|
key: privkey,
|
|
|
|
padding: crypto.constants.RSA_PKCS1_PSS_PADDING
|
|
|
|
});
|
|
|
|
|
|
|
|
const tmpdir = require('../common/tmpdir');
|
|
|
|
tmpdir.refresh();
|
|
|
|
|
2023-08-15 13:45:34 +00:00
|
|
|
const sigfile = tmpdir.resolve('s5.sig');
|
2019-03-12 13:17:10 +00:00
|
|
|
fs.writeFileSync(sigfile, s5);
|
2023-08-15 13:45:34 +00:00
|
|
|
const msgfile = tmpdir.resolve('s5.msg');
|
2019-03-12 13:17:10 +00:00
|
|
|
fs.writeFileSync(msgfile, msg);
|
|
|
|
|
2024-09-29 20:44:52 +00:00
|
|
|
exec(...common.escapePOSIXShell`"${
|
|
|
|
common.opensslCli}" dgst -sha256 -verify "${pubfile}" -signature "${
|
|
|
|
sigfile}" -sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:-2 "${msgfile
|
|
|
|
}"`, common.mustCall((err, stdout, stderr) => {
|
2019-03-12 13:17:10 +00:00
|
|
|
assert(stdout.includes('Verified OK'));
|
|
|
|
}));
|
2019-01-07 19:27:31 +00:00
|
|
|
}
|
2021-08-20 16:57:31 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// Test RSA-PSS.
|
|
|
|
{
|
|
|
|
// This key pair does not restrict the message digest algorithm or salt
|
|
|
|
// length.
|
|
|
|
const publicPem = fixtures.readKey('rsa_pss_public_2048.pem');
|
|
|
|
const privatePem = fixtures.readKey('rsa_pss_private_2048.pem');
|
|
|
|
|
|
|
|
const publicKey = crypto.createPublicKey(publicPem);
|
|
|
|
const privateKey = crypto.createPrivateKey(privatePem);
|
|
|
|
|
|
|
|
for (const key of [privatePem, privateKey]) {
|
|
|
|
// Any algorithm should work.
|
|
|
|
for (const algo of ['sha1', 'sha256']) {
|
|
|
|
// Any salt length should work.
|
|
|
|
for (const saltLength of [undefined, 8, 10, 12, 16, 18, 20]) {
|
|
|
|
const signature = crypto.sign(algo, 'foo', { key, saltLength });
|
|
|
|
|
|
|
|
for (const pkey of [key, publicKey, publicPem]) {
|
|
|
|
const okay = crypto.verify(
|
|
|
|
algo,
|
|
|
|
'foo',
|
|
|
|
{ key: pkey, saltLength },
|
|
|
|
signature
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.ok(okay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// This key pair enforces sha256 as the message digest and the MGF1
|
|
|
|
// message digest and a salt length of at least 16 bytes.
|
|
|
|
const publicPem =
|
|
|
|
fixtures.readKey('rsa_pss_public_2048_sha256_sha256_16.pem');
|
|
|
|
const privatePem =
|
|
|
|
fixtures.readKey('rsa_pss_private_2048_sha256_sha256_16.pem');
|
|
|
|
|
|
|
|
const publicKey = crypto.createPublicKey(publicPem);
|
|
|
|
const privateKey = crypto.createPrivateKey(privatePem);
|
|
|
|
|
|
|
|
for (const key of [privatePem, privateKey]) {
|
|
|
|
// Signing with anything other than sha256 should fail.
|
|
|
|
assert.throws(() => {
|
|
|
|
crypto.sign('sha1', 'foo', key);
|
|
|
|
}, /digest not allowed/);
|
|
|
|
|
|
|
|
// Signing with salt lengths less than 16 bytes should fail.
|
|
|
|
for (const saltLength of [8, 10, 12]) {
|
|
|
|
assert.throws(() => {
|
|
|
|
crypto.sign('sha256', 'foo', { key, saltLength });
|
|
|
|
}, /pss saltlen too small/);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signing with sha256 and appropriate salt lengths should work.
|
|
|
|
for (const saltLength of [undefined, 16, 18, 20]) {
|
|
|
|
const signature = crypto.sign('sha256', 'foo', { key, saltLength });
|
|
|
|
|
|
|
|
for (const pkey of [key, publicKey, publicPem]) {
|
|
|
|
const okay = crypto.verify(
|
|
|
|
'sha256',
|
|
|
|
'foo',
|
|
|
|
{ key: pkey, saltLength },
|
|
|
|
signature
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.ok(okay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// This key enforces sha512 as the message digest and sha256 as the MGF1
|
|
|
|
// message digest.
|
|
|
|
const publicPem =
|
|
|
|
fixtures.readKey('rsa_pss_public_2048_sha512_sha256_20.pem');
|
|
|
|
const privatePem =
|
|
|
|
fixtures.readKey('rsa_pss_private_2048_sha512_sha256_20.pem');
|
|
|
|
|
|
|
|
const publicKey = crypto.createPublicKey(publicPem);
|
|
|
|
const privateKey = crypto.createPrivateKey(privatePem);
|
|
|
|
|
|
|
|
// Node.js usually uses the same hash function for the message and for MGF1.
|
|
|
|
// However, when a different MGF1 message digest algorithm has been
|
|
|
|
// specified as part of the key, it should automatically switch to that.
|
|
|
|
// This behavior is required by sections 3.1 and 3.3 of RFC4055.
|
|
|
|
for (const key of [privatePem, privateKey]) {
|
|
|
|
// sha256 matches the MGF1 hash function and should be used internally,
|
|
|
|
// but it should not be permitted as the main message digest algorithm.
|
|
|
|
for (const algo of ['sha1', 'sha256']) {
|
|
|
|
assert.throws(() => {
|
|
|
|
crypto.sign(algo, 'foo', key);
|
|
|
|
}, /digest not allowed/);
|
|
|
|
}
|
|
|
|
|
|
|
|
// sha512 should produce a valid signature.
|
|
|
|
const signature = crypto.sign('sha512', 'foo', key);
|
|
|
|
|
|
|
|
for (const pkey of [key, publicKey, publicPem]) {
|
|
|
|
const okay = crypto.verify('sha512', 'foo', pkey, signature);
|
|
|
|
|
|
|
|
assert.ok(okay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-15 15:50:20 +00:00
|
|
|
|
|
|
|
// The sign function should not swallow OpenSSL errors.
|
|
|
|
// Regression test for https://github.com/nodejs/node/issues/40794.
|
|
|
|
{
|
|
|
|
assert.throws(() => {
|
|
|
|
const { privateKey } = crypto.generateKeyPairSync('rsa', {
|
|
|
|
modulusLength: 512
|
|
|
|
});
|
|
|
|
crypto.sign('sha512', 'message', privateKey);
|
|
|
|
}, {
|
|
|
|
code: 'ERR_OSSL_RSA_DIGEST_TOO_BIG_FOR_RSA_KEY',
|
|
|
|
message: /digest too big for rsa key/
|
|
|
|
});
|
|
|
|
}
|
2022-09-03 12:36:20 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// This should not cause a crash: https://github.com/nodejs/node/issues/44471
|
|
|
|
for (const key of ['', 'foo', null, undefined, true, Boolean]) {
|
|
|
|
assert.throws(() => {
|
|
|
|
crypto.verify('sha256', 'foo', { key, format: 'jwk' }, Buffer.alloc(0));
|
|
|
|
}, { code: 'ERR_INVALID_ARG_TYPE', message: /The "key\.key" property must be of type object/ });
|
|
|
|
assert.throws(() => {
|
|
|
|
crypto.createVerify('sha256').verify({ key, format: 'jwk' }, Buffer.alloc(0));
|
|
|
|
}, { code: 'ERR_INVALID_ARG_TYPE', message: /The "key\.key" property must be of type object/ });
|
|
|
|
assert.throws(() => {
|
|
|
|
crypto.sign('sha256', 'foo', { key, format: 'jwk' });
|
|
|
|
}, { code: 'ERR_INVALID_ARG_TYPE', message: /The "key\.key" property must be of type object/ });
|
|
|
|
assert.throws(() => {
|
|
|
|
crypto.createSign('sha256').sign({ key, format: 'jwk' });
|
|
|
|
}, { code: 'ERR_INVALID_ARG_TYPE', message: /The "key\.key" property must be of type object/ });
|
|
|
|
}
|
|
|
|
}
|
2024-04-08 06:18:43 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// Ed25519 and Ed448 must use the one-shot methods
|
|
|
|
const keys = [{ privateKey: fixtures.readKey('ed25519_private.pem', 'ascii'),
|
|
|
|
publicKey: fixtures.readKey('ed25519_public.pem', 'ascii') },
|
|
|
|
{ privateKey: fixtures.readKey('ed448_private.pem', 'ascii'),
|
|
|
|
publicKey: fixtures.readKey('ed448_public.pem', 'ascii') }];
|
|
|
|
|
|
|
|
for (const { publicKey, privateKey } of keys) {
|
|
|
|
assert.throws(() => {
|
|
|
|
crypto.createSign('SHA256').update('Test123').sign(privateKey);
|
|
|
|
}, { code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION', message: 'Unsupported crypto operation' });
|
|
|
|
assert.throws(() => {
|
|
|
|
crypto.createVerify('SHA256').update('Test123').verify(privateKey, 'sig');
|
|
|
|
}, { code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION', message: 'Unsupported crypto operation' });
|
|
|
|
assert.throws(() => {
|
|
|
|
crypto.createVerify('SHA256').update('Test123').verify(publicKey, 'sig');
|
|
|
|
}, { code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION', message: 'Unsupported crypto operation' });
|
|
|
|
}
|
|
|
|
}
|
2024-09-06 22:07:16 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// Dh, x25519 and x448 should not be used for signing/verifying
|
|
|
|
// https://github.com/nodejs/node/issues/53742
|
|
|
|
for (const algo of ['dh', 'x25519', 'x448']) {
|
|
|
|
const privateKey = fixtures.readKey(`${algo}_private.pem`, 'ascii');
|
|
|
|
const publicKey = fixtures.readKey(`${algo}_public.pem`, 'ascii');
|
|
|
|
assert.throws(() => {
|
|
|
|
crypto.createSign('SHA256').update('Test123').sign(privateKey);
|
|
|
|
}, { code: 'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE', message: /operation not supported for this keytype/ });
|
|
|
|
assert.throws(() => {
|
|
|
|
crypto.createVerify('SHA256').update('Test123').verify(privateKey, 'sig');
|
|
|
|
}, { code: 'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE', message: /operation not supported for this keytype/ });
|
|
|
|
assert.throws(() => {
|
|
|
|
crypto.createVerify('SHA256').update('Test123').verify(publicKey, 'sig');
|
|
|
|
}, { code: 'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE', message: /operation not supported for this keytype/ });
|
|
|
|
}
|
|
|
|
}
|