Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import * as graphene from "graphene-pk11";
let Module = graphene.Module;
let lib = "/usr/local/lib/softhsm/libsofthsm2.so";
let mod = Module.load(lib, "SoftHSM");
mod.initialize();
let slot = mod.getSlots(0);
if (slot.flags & graphene.SlotFlag.TOKEN_PRESENT) {
let session = slot.open();
session.login("12345");
// generate EC key
let keys = session.generateKeyPair(graphene.KeyGenMechanism.ECDSA, {
keyType: graphene.KeyType.ECDSA,
token: false,
derive: true,
paramsECDSA: graphene.NamedCurve.getByName("secp192r1").value
}, {
keyType: graphene.KeyType.ECDSA,
token: false,
derive: true
});
// derive algorithm
let alg = {
name: "ECDH1_DERIVE",
params: new graphene.EcdhParams(
graphene.EcKdf.SHA1,
null,
return new Promise((resolve, reject) => {
algorithm.length = algorithm.length
? algorithm.length
: this.getDefaultLength((algorithm.hash as Algorithm).name) >> 3;
const template = this.createTemplate(algorithm, extractable, keyUsages);
template.valueLen = algorithm.length << 3;
// PKCS11 generation
this.crypto.session.generateKey(graphene.KeyGenMechanism.GENERIC_SECRET, template, (err, aesKey) => {
try {
if (err) {
reject(new core.CryptoError(`HMAC: Cannot generate new key\n${err.message}`));
} else {
resolve(new HmacCryptoKey(aesKey, { ...algorithm, name: this.name } as HmacKeyAlgorithm));
}
} catch (e) {
reject(e);
}
});
});
}
return new Promise((resolve, reject) => {
const template = this.createTemplate(session!, algorithm, extractable, keyUsages);
// EC params
template.publicKey.paramsEC = this.getNamedCurve(algorithm.namedCurve).value;
// PKCS11 generation
session.generateKeyPair(graphene.KeyGenMechanism.EC, template.publicKey, template.privateKey, (err, keys) => {
try {
if (err) {
reject(err);
} else {
const wcKeyPair = {
privateKey: new EcCryptoKey(keys.privateKey, algorithm),
publicKey: new EcCryptoKey(keys.publicKey, algorithm),
};
resolve(wcKeyPair as any);
}
} catch (e) {
reject(e);
}
});
});
}
Aes.generateKey = function (session, alg, extractable, keyUsages, callback) {
try {
var _alg_1 = alg;
this.checkAlgorithmIdentifier(alg);
this.checkKeyGenAlgorithm(_alg_1);
var template = create_template(session, _alg_1, extractable, keyUsages);
template.valueLen = alg.length / 8;
session.generateKey(graphene_pk11_1.KeyGenMechanism.AES, template, function (err, key) {
try {
if (err)
callback(err, null);
else {
var wcKey = new key_1.P11CryptoKey(key, _alg_1);
callback(null, wcKey);
}
}
catch (e) {
callback(e, null);
}
});
}
catch (e) {
callback(e, null);
}
Rsa.generateKey = function (session, alg, extractable, keyUsages, callback) {
try {
var _alg_1 = alg;
this.checkAlgorithmIdentifier(alg);
this.checkAlgorithmHashedParams(alg);
this.checkKeyGenAlgorithm(_alg_1);
var template = create_template(session, _alg_1, extractable, keyUsages);
template.publicKey.publicExponent = new Buffer(_alg_1.publicExponent),
template.publicKey.modulusBits = _alg_1.modulusLength;
session.generateKeyPair(graphene_pk11_1.KeyGenMechanism.RSA, template.publicKey, template.privateKey, function (err, keys) {
try {
if (err)
callback(err, null);
else {
var wcKeyPair = {
privateKey: new key_1.P11CryptoKey(keys.privateKey, _alg_1),
publicKey: new key_1.P11CryptoKey(keys.publicKey, _alg_1)
};
callback(null, wcKeyPair);
}
}
catch (e) {
callback(e, null);
}
});
}
Ec.generateKey = function (session, alg, extractable, keyUsages, callback) {
try {
var _alg_1 = alg;
this.checkAlgorithmIdentifier(alg);
this.checkKeyGenAlgorithm(_alg_1);
var template = create_template(session, _alg_1, extractable, keyUsages);
template.publicKey.paramsEC = this.getNamedCurve(_alg_1.namedCurve).value;
session.generateKeyPair(graphene_pk11_1.KeyGenMechanism.EC, template.publicKey, template.privateKey, function (err, keys) {
try {
if (err)
callback(err, null);
else {
var wcKeyPair = {
privateKey: new key_1.P11CryptoKey(keys.privateKey, _alg_1),
publicKey: new key_1.P11CryptoKey(keys.publicKey, _alg_1)
};
callback(null, wcKeyPair);
}
}
catch (e) {
callback(e, null);
}
});
}