Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
extractable,
usages: keyUsages,
});
if (format.toLowerCase() === "jwk") {
const namedCurve = this.getNamedCurve((algorithm as EcKeyImportParams).namedCurve);
console.log(namedCurve);
const ecKey = new elliptic.ec(namedCurve);
if ((keyData as JsonWebKey).d) {
// Private key
key.key = ecKey.keyFromPrivate(Base64Url.decode((keyData as JsonWebKey).d!));
key.type = "private";
} else {
// Public key
const bufferPubKey = concat(
new Uint8Array([4]),
Base64Url.decode((keyData as JsonWebKey).x!),
Base64Url.decode((keyData as JsonWebKey).y!),
);
const hexPubKey = buffer2hex(bufferPubKey);
key.key = ecKey.keyFromPublic(hexPubKey, "hex");
key.type = "public";
}
} else {
throw new LinerError(`Format '${format}' is not implemented`);
}
return key;
});
}
usages: keyUsages,
});
if (format.toLowerCase() === "jwk") {
const namedCurve = this.getNamedCurve((algorithm as EcKeyImportParams).namedCurve);
console.log(namedCurve);
const ecKey = new elliptic.ec(namedCurve);
if ((keyData as JsonWebKey).d) {
// Private key
key.key = ecKey.keyFromPrivate(Base64Url.decode((keyData as JsonWebKey).d!));
key.type = "private";
} else {
// Public key
const bufferPubKey = concat(
new Uint8Array([4]),
Base64Url.decode((keyData as JsonWebKey).x!),
Base64Url.decode((keyData as JsonWebKey).y!),
);
const hexPubKey = buffer2hex(bufferPubKey);
key.key = ecKey.keyFromPublic(hexPubKey, "hex");
key.type = "public";
}
} else {
throw new LinerError(`Format '${format}' is not implemented`);
}
return key;
});
}
.then(() => {
const key: EcCryptoKey = new CryptoKey({
algorithm,
extractable,
usages: keyUsages,
});
if (format.toLowerCase() === "jwk") {
const namedCurve = this.getNamedCurve((algorithm as EcKeyImportParams).namedCurve);
console.log(namedCurve);
const ecKey = new elliptic.ec(namedCurve);
if ((keyData as JsonWebKey).d) {
// Private key
key.key = ecKey.keyFromPrivate(Base64Url.decode((keyData as JsonWebKey).d!));
key.type = "private";
} else {
// Public key
const bufferPubKey = concat(
new Uint8Array([4]),
Base64Url.decode((keyData as JsonWebKey).x!),
Base64Url.decode((keyData as JsonWebKey).y!),
);
const hexPubKey = buffer2hex(bufferPubKey);
key.key = ecKey.keyFromPublic(hexPubKey, "hex");
key.type = "public";
}
} else {
throw new LinerError(`Format '${format}' is not implemented`);
}
nativeKey.exportJwk(type, (err, data) => {
try {
const jwk: JsonWebKey = { kty: "RSA" };
jwk.key_ops = key.usages;
// convert base64 -> base64url for all props
jwk.e = Base64Url.encode(data.e);
jwk.n = Base64Url.encode(data.n);
if (key.type === "private") {
jwk.d = Base64Url.encode(data.d);
jwk.p = Base64Url.encode(data.p);
jwk.q = Base64Url.encode(data.q);
jwk.dp = Base64Url.encode(data.dp);
jwk.dq = Base64Url.encode(data.dq);
jwk.qi = Base64Url.encode(data.qi);
}
resolve(jwk);
} catch (e) {
reject(e);
}
});
break;
nativeKey.exportJwk(type, (err, data) => {
try {
const jwk: JsonWebKey = { kty: "RSA" };
jwk.key_ops = key.usages;
// convert base64 -> base64url for all props
jwk.e = Base64Url.encode(data.e);
jwk.n = Base64Url.encode(data.n);
if (key.type === "private") {
jwk.d = Base64Url.encode(data.d);
jwk.p = Base64Url.encode(data.p);
jwk.q = Base64Url.encode(data.q);
jwk.dp = Base64Url.encode(data.dp);
jwk.dq = Base64Url.encode(data.dq);
jwk.qi = Base64Url.encode(data.qi);
}
resolve(jwk);
} catch (e) {
reject(e);
}
});
break;
nativeKey.exportJwk(type, (err, data) => {
try {
const jwk: JsonWebKey = { kty: "RSA" };
jwk.key_ops = key.usages;
// convert base64 -> base64url for all props
jwk.e = Base64Url.encode(data.e);
jwk.n = Base64Url.encode(data.n);
if (key.type === "private") {
jwk.d = Base64Url.encode(data.d);
jwk.p = Base64Url.encode(data.p);
jwk.q = Base64Url.encode(data.q);
jwk.dp = Base64Url.encode(data.dp);
jwk.dq = Base64Url.encode(data.dq);
jwk.qi = Base64Url.encode(data.qi);
}
resolve(jwk);
} catch (e) {
reject(e);
}
});
break;
// public
const jwk: JsonWebKey = {
crv: (key.algorithm as EcKeyGenParams).namedCurve,
ext: key.extractable,
x: Base64Url.encode(hex2buffer(hexX, true)),
y: Base64Url.encode(hex2buffer(hexY, true)),
key_ops: key.usages,
kty: "EC",
};
return jwk;
} else {
// private
const jwk: JsonWebKey = {
crv: (key.algorithm as EcKeyGenParams).namedCurve,
ext: key.extractable,
d: Base64Url.encode(hex2buffer(ecKey.getPrivate("hex"), true)),
x: Base64Url.encode(hex2buffer(hexX, true)),
y: Base64Url.encode(hex2buffer(hexY, true)),
key_ops: key.usages,
kty: "EC",
};
return jwk;
}
} else {
throw new LinerError(`Format '${format}' is not implemented`);
}
});
}
nativeKey.exportJwk(type, (err, data) => {
try {
const jwk: JsonWebKey = { kty: "RSA" };
jwk.key_ops = key.usages;
// convert base64 -> base64url for all props
jwk.e = Base64Url.encode(data.e);
jwk.n = Base64Url.encode(data.n);
if (key.type === "private") {
jwk.d = Base64Url.encode(data.d);
jwk.p = Base64Url.encode(data.p);
jwk.q = Base64Url.encode(data.q);
jwk.dp = Base64Url.encode(data.dp);
jwk.dq = Base64Url.encode(data.dq);
jwk.qi = Base64Url.encode(data.qi);
}
resolve(jwk);
} catch (e) {
reject(e);
}
});
break;
private static async cipher(algorithm: Algorithm, key: AesCryptoKey, data: ArrayBuffer, encrypt: boolean) {
this.checkLib();
const action = encrypt ? "encrypt" : "decrypt";
let res: Uint8Array;
if (isAlgorithm(algorithm, AesCrypto.AesCBC)) {
// AES-CBC
const iv = core.BufferSourceConverter.toArrayBuffer(algorithm.iv);
res = asmCrypto.AES_CBC[action](data, key.raw, undefined, iv);
} else if (isAlgorithm(algorithm, AesCrypto.AesGCM)) {
// AES-GCM
const iv = core.BufferSourceConverter.toArrayBuffer(algorithm.iv);
let additionalData;
if (algorithm.additionalData) {
additionalData = core.BufferSourceConverter.toArrayBuffer(algorithm.additionalData);
}
const tagLength = (algorithm.tagLength || 128) / 8;
res = asmCrypto.AES_GCM[action](data, key.raw, iv, additionalData, tagLength);
} else if (isAlgorithm(algorithm, AesCrypto.AesECB)) {
// // AES-ECB
res = asmCrypto.AES_ECB[action](data, key.raw, true);
} else {
throw new core.OperationError(`algorithm: Is not recognized`);
}
return res.buffer;
}
private fixFirefoxEcImportPkcs8(args: any[]) {
const preparedAlgorithm = this.prepareAlgorithm(args[2]) as EcKeyImportParams;
const algName = preparedAlgorithm.name.toUpperCase();
if (this.browserInfo.name === Browser.Firefox
&& args[0] === "pkcs8"
&& ~["ECDSA", "ECDH"].indexOf(algName)
&& ~["P-256", "P-384", "P-521"].indexOf(preparedAlgorithm.namedCurve)) {
if (!core.BufferSourceConverter.isBufferSource(args[1])) {
throw new TypeError("data: Is not ArrayBuffer or ArrayBufferView");
}
const preparedData = core.BufferSourceConverter.toArrayBuffer(args[1]);
// Convert PKCS8 to JWK
const keyInfo = AsnParser.parse(preparedData, asn.PrivateKeyInfo);
const privateKey = AsnParser.parse(keyInfo.privateKey, asn.EcPrivateKey);
const jwk: JsonWebKey = JsonSerializer.toJSON(privateKey);
jwk.ext = true;
jwk.key_ops = args[4];
jwk.crv = preparedAlgorithm.namedCurve;
jwk.kty = "EC";
args[0] = "jwk";
args[1] = jwk;
}