Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
t.equal(key4.getSKI().length, 64, 'Checking generated SKI hash string for 384 curve public key');
t.doesNotThrow(
() => {
key4.toBytes();
},
'Checking to dump a public ECDSAKey object to bytes'
);
t.equal(!key3.isPrivate() && !key4.isPrivate(), true, 'Checking if both keys are public');
t.equal(key3.getPublicKey().isPrivate(), false, 'Checking getPublicKey() logic');
t.equal(key4.getPublicKey().toBytes().length, 220, 'Checking toBytes() output');
// test CSR generation
const pair3 = KEYUTIL.generateKeypair('EC', 'secp256r1');
key3 = new ecdsaKey(pair3.prvKeyObj);
key4 = new ecdsaKey(pair3.pubKeyObj);
t.throws(
() => {
key4.generateCSR('CN=publickey');
},
/A CSR cannot be generated from a public key/,
'Checking that a CSR cannot be generated from a public key'
);
let csrPEM;
// malformed subjectDN
try {
csrPEM = key3.generateCSR('###############');
t.fail('Should not have generated a CSR with a malformed subject');
} catch (err) {
t.pass('Checking that CSR is not generated for a malformed subject');
);
// test private keys
const pair1 = KEYUTIL.generateKeypair('EC', 'secp256r1');
const key1 = new ecdsaKey(pair1.prvKeyObj);
t.equal(key1.getSKI().length, 64, 'Checking generated SKI hash string for 256 curve keys');
t.doesNotThrow(
() => {
key1.toBytes();
},
'Checking that a private key instance allows toBytes()'
);
const pair2 = KEYUTIL.generateKeypair('EC', 'secp384r1');
const key2 = new ecdsaKey(pair2.prvKeyObj);
t.equal(key2.getSKI().length, 64, 'Checking generated SKI hash string for 384 curve keys');
t.equal(key1.isSymmetric() || key2.isSymmetric(), false, 'Checking if key is symmetric');
t.equal(key1.isPrivate() && key2.isPrivate(), true, 'Checking if key is private');
t.equal(key1.getPublicKey().isPrivate(), false, 'Checking isPrivate() logic');
t.equal(key1.getPublicKey().toBytes().length, 182, 'Checking toBytes() output');
// test public keys
let key3 = new ecdsaKey(pair1.pubKeyObj);
t.equal(key3.getSKI().length, 64, 'Checking generated SKI hash string for 256 curve public key');
t.doesNotThrow(
() => {
key3.toBytes();
},
.then((dsID) => {
t.equal(dsID._certificate, TEST_CERT_PEM, 'Identity class function tests: deserialized certificate');
t.equal(dsID._publicKey.isPrivate(), false, 'Identity class function tests: deserialized public key');
t.equal(dsID._publicKey._key.pubKeyHex, '0452a75e1ee105da7ab3d389fda69d8a04f5cf65b305b49cec7cdbdeb91a585cf87bef5a96aa9683d96bbabfe60d8cc6f5db9d0bc8c58d56bb28887ed81c6005ac', 'Identity class function tests: deserialized public key ecparam check');
// manually construct a key based on the saved privKeyHex and pubKeyHex
const f = KEYUTIL.getKey(TEST_KEY_PRIVATE_PEM);
const testKey = new ecdsaKey(f);
pubKey = testKey.getPublicKey();
const signer = new Signer(cryptoUtils, testKey);
t.equal(signer.getPublicKey().isPrivate(), false, 'Test Signer class getPublicKey() method');
const signingID = new SigningIdentity(TEST_KEY_PRIVATE_CERT_PEM, pubKey, mspImpl.getId(), cryptoUtils, signer);
t.throws(
() => {
signingID.sign(TEST_MSG, {hashFunction: 'not_a_function'});
},
/The "hashFunction" field must be a function/,
'Test invalid hashFunction parameter for the sign() method'
);
const sig = signingID.sign(TEST_MSG);
// test private keys
var pair1 = KEYUTIL.generateKeypair('EC', 'secp256r1');
var key1 = new ecdsaKey(pair1.prvKeyObj);
t.equal(key1.getSKI().length, 64, 'Checking generated SKI hash string for 256 curve keys');
t.doesNotThrow(
function () {
key1.toBytes();
},
null,
'Checking that a private key instance allows toBytes()'
);
var pair2 = KEYUTIL.generateKeypair('EC', 'secp384r1');
var key2 = new ecdsaKey(pair2.prvKeyObj);
t.equal(key2.getSKI().length, 64, 'Checking generated SKI hash string for 384 curve keys');
t.equal(key1.isSymmetric() || key2.isSymmetric(), false, 'Checking if key is symmetric');
t.equal(key1.isPrivate() && key2.isPrivate(), true, 'Checking if key is private');
t.equal(key1.getPublicKey().isPrivate(), false, 'Checking isPrivate() logic');
t.equal(key1.getPublicKey().toBytes().length, 182, 'Checking toBytes() output');
// test public keys
var key3 = new ecdsaKey(pair1.pubKeyObj);
t.equal(key3.getSKI().length, 64, 'Checking generated SKI hash string for 256 curve public key');
t.doesNotThrow(
function() {
key3.toBytes();
},
t.doesNotThrow(
function() {
key4.toBytes();
},
null,
'Checking to dump a public ECDSAKey object to bytes'
);
t.equal(!key3.isPrivate() && !key4.isPrivate(), true, 'Checking if both keys are public');
t.equal(key3.getPublicKey().isPrivate(), false, 'Checking getPublicKey() logic');
t.equal(key4.getPublicKey().toBytes().length, 220, 'Checking toBytes() output');
//test CSR generation
var pair3 = KEYUTIL.generateKeypair('EC', 'secp256r1');
var key3 = new ecdsaKey(pair3.prvKeyObj);
var key4 = new ecdsaKey(pair3.pubKeyObj);
t.throws(
function () {
key4.generateCSR('CN=publickey');
},
/A CSR cannot be generated from a public key/,
'Checking that a CSR cannot be generated from a public key'
);
//malformed subjectDN
try {
var csrPEM = key3.generateCSR('###############');
t.fail('Should not have generated a CSR with a malformed subject');
}
catch (err) {
t.pass('Checking that CSR is not generated for a malformed subject');
new CKS();
},
/Must provide the path to the directory to hold files for the store/,
'Test invalid constructor calls: missing options parameter'
);
t.throws(
() => {
new CKS({something: 'useless'});
},
/Must provide the path to the directory to hold files for the store/,
'Test invalid constructor calls: missing "path" property in the "options" parameter'
);
var f1 = KEYUTIL.getKey(TEST_KEY_PRIVATE_PEM);
var testPrivKey = new ecdsaKey(f1);
var f2 = KEYUTIL.getKey(TEST_KEY_PRIVATE_CERT_PEM);
var testPubKey = new ecdsaKey(f2);
var store;
var _cks = new CKS({path: '/tmp/hfc-cks'})
.then((st) => {
store = st;
return store.putKey(testPrivKey);
}).then((keyPEM) => {
t.pass('Successfully saved private key in store');
t.equal(fs.existsSync(path.join('/tmp/hfc-cks', testPrivKey.getSKI() + '-priv')), true,
'Check that the private key has been saved with the proper -priv index');
return store.getKey(testPrivKey.getSKI());
}).then((recoveredKey) => {
() => {
new ecdsaKey('dummy private key');
},
/^Error: This key implementation only supports keys generated by jsrsasign.KEYUTIL. It must have a "type" property of value "EC"/,
function () {
var k = new ecdsaKey('dummy private key');
},
/^Error: This key implementation only supports keys generated by jsrsasign.KEYUTIL. It must have a "type" property of value "EC"/,
/Must provide the path to the directory to hold files for the store/,
'Test invalid constructor calls: missing options parameter'
);
t.throws(
() => {
new CKS({something: 'useless'});
},
/Must provide the path to the directory to hold files for the store/,
'Test invalid constructor calls: missing "path" property in the "options" parameter'
);
var f1 = KEYUTIL.getKey(TEST_KEY_PRIVATE_PEM);
var testPrivKey = new ecdsaKey(f1);
var f2 = KEYUTIL.getKey(TEST_KEY_PRIVATE_CERT_PEM);
var testPubKey = new ecdsaKey(f2);
var store;
var _cks = new CKS({path: '/tmp/hfc-cks'})
.then((st) => {
store = st;
return store.putKey(testPrivKey);
}).then((keyPEM) => {
t.pass('Successfully saved private key in store');
t.equal(fs.existsSync(path.join('/tmp/hfc-cks', testPrivKey.getSKI() + '-priv')), true,
'Check that the private key has been saved with the proper -priv index');
return store.getKey(testPrivKey.getSKI());
}).then((recoveredKey) => {
t.notEqual(recoveredKey, null, 'Successfully read private key from store using SKI');
t.equal(recoveredKey.isPrivate(), true, 'Test if the recovered key is a private key');
throw new Error('id not specified or not a string');
} else if (!publicCert || typeof publicCert !== 'string') {
throw new Error('publicCert not specified or not a string');
}
let client = await HLFConnectionManager.createClient(connectionOptions, true);
const mspID = client.getMspid();
let cryptoContent = {
signedCertPEM: publicCert
};
try {
if (HLFConnectionManager.useHSM(connectionOptions)) {
let publicKey = KEYUTIL.getKey(publicCert);
let ecdsakey = new ecdsaKey(publicKey);
cryptoContent.privateKeyObj = await client.getCryptoSuite().getKey(Buffer.from(ecdsakey.getSKI(), 'hex'));
} else {
if (!privateKey || typeof privateKey !== 'string') {
throw new Error('privateKey not specified or not a string');
}
cryptoContent.privateKeyPEM = privateKey;
}
await client.createUser(
{
username: id,
mspid: mspID,
cryptoContent: cryptoContent
});
LOG.exit(method);