Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public static async importKey(format: string, keyData: JsonWebKey | ArrayBuffer, algorithm: core.DesImportParams, extractable: boolean, keyUsages: KeyUsage[]): Promise {
this.checkLib();
let raw: ArrayBuffer;
if (core.isJWK(keyData)) {
raw = Convert.FromBase64Url(keyData.k);
} else {
raw = core.BufferSourceConverter.toArrayBuffer(keyData);
}
// check key length
if ((algorithm.name === "DES-CBC" && raw.byteLength !== 8)
|| (algorithm.name === "DES-EDE3-CBC" && raw.byteLength !== 24)) {
throw new core.OperationError("keyData: Is wrong key length");
}
const key = new DesCryptoKey({ name: algorithm.name, length: raw.byteLength << 3 }, extractable, keyUsages, new Uint8Array(raw));
return key;
}
const res = new this();
const algName = publicKey.algorithm.name!.toUpperCase();
if (!(algName === "ECDH" || algName === "ECDSA")) {
throw new Error("Error: Unsupported asymmetric key algorithm.");
}
if (publicKey.type !== "public") {
throw new Error("Error: Expected key type to be public but it was not.");
}
res.key = publicKey;
// Serialize public key to JWK
const jwk = await getEngine().crypto.subtle.exportKey("jwk", publicKey);
if (!(jwk.x && jwk.y)) {
throw new Error("Wrong JWK data for EC public key. Parameters x and y are required.");
}
const x = Convert.FromBase64Url(jwk.x);
const y = Convert.FromBase64Url(jwk.y);
const xy = Convert.ToBinary(x) + Convert.ToBinary(y);
res.serialized = Convert.FromBinary(xy);
res.id = await res.thumbprint();
return res;
}
public async onImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: HmacImportParams, extractable: boolean, keyUsages: KeyUsage[]): Promise {
// get key value
let value: ArrayBuffer;
switch (format.toLowerCase()) {
case "jwk":
if (!("k" in keyData)) {
throw new core.OperationError("jwk.k: Cannot get required property");
}
keyData = Convert.FromBase64Url(keyData.k);
case "raw":
value = keyData as ArrayBuffer;
break;
default:
throw new core.OperationError("format: Must be 'jwk' or 'raw'");
}
// prepare key algorithm
const hmacAlg: HmacKeyGenParams = {
...algorithm,
name: this.name,
length: value.byteLength * 8 || this.getDefaultLength((algorithm.hash as Algorithm).name),
};
const template: graphene.ITemplate = this.createTemplate(hmacAlg, extractable, keyUsages);
template.value = Buffer.from(value);
// create session object
return this.exportJwkPublicKey(key);
}
}
case "pkcs8": {
const jwk = await this.exportJwkPrivateKey(key);
return this.jwk2pkcs(jwk);
}
case "spki": {
const jwk = await this.exportJwkPublicKey(key);
return this.jwk2spki(jwk);
}
case "raw": {
// export subjectPublicKey BIT_STRING value
const jwk = await this.exportJwkPublicKey(key);
if ((key.algorithm as EcKeyGenParams).namedCurve === "X25519") {
return Convert.FromBase64Url(jwk.x);
} else {
const publicKey = new pkijs.PublicKeyInfo();
publicKey.fromJSON(jwk);
return publicKey.toSchema(true).valueBlock.value[1].valueBlock.valueHex;
}
}
default:
throw new core.OperationError("format: Must be 'jwk', 'raw', pkcs8' or 'spki'");
}
}
public static async importKey(format: string, keyData: JsonWebKey | ArrayBuffer, algorithm: Algorithm, extractable: boolean, keyUsages: KeyUsage[]): Promise {
this.checkLib();
let raw: ArrayBuffer;
if (core.isJWK(keyData)) {
raw = Convert.FromBase64Url(keyData.k);
} else {
raw = core.BufferSourceConverter.toArrayBuffer(keyData);
}
// check key length
switch (raw.byteLength << 3) {
case 128:
case 192:
case 256:
break;
default:
throw new core.OperationError("keyData: Is wrong key length");
}
const key = new AesCryptoKey({ name: algorithm.name, length: raw.byteLength << 3 }, extractable, keyUsages, new Uint8Array(raw));
return key;
const algName = publicKey.algorithm.name!.toUpperCase();
if (!(algName === "ECDH" || algName === "ECDSA")) {
throw new Error("Error: Unsupported asymmetric key algorithm.");
}
if (publicKey.type !== "public") {
throw new Error("Error: Expected key type to be public but it was not.");
}
res.key = publicKey;
// Serialize public key to JWK
const jwk = await getEngine().crypto.subtle.exportKey("jwk", publicKey);
if (!(jwk.x && jwk.y)) {
throw new Error("Wrong JWK data for EC public key. Parameters x and y are required.");
}
const x = Convert.FromBase64Url(jwk.x);
const y = Convert.FromBase64Url(jwk.y);
const xy = Convert.ToBinary(x) + Convert.ToBinary(y);
res.serialized = Convert.FromBinary(xy);
res.id = await res.thumbprint();
return res;
}
public static async importKey(session: graphene.Session, format: string, keyData: JsonWebKey | ArrayBuffer, algorithm: any, extractable: boolean, keyUsages: KeyUsage[]): Promise {
// get key value
let value: ArrayBuffer;
switch (format.toLowerCase()) {
case "jwk":
if (!("k" in keyData)) {
throw new core.OperationError("jwk.k: Cannot get required property");
}
keyData = Convert.FromBase64Url(keyData.k);
case "raw":
value = keyData as ArrayBuffer;
switch (value.byteLength) {
case 16:
case 24:
case 32:
break;
default:
throw new core.OperationError("keyData: Is wrong key length");
}
break;
default:
throw new core.OperationError("format: Must be 'jwk' or 'raw'");
}
// prepare key algorithm
protected static getCoordinate(b64: string, coordinateLength: number) {
const buf = Convert.FromBase64Url(b64);
const offset = coordinateLength - buf.byteLength;
const res = new Uint8Array(coordinateLength);
res.set(new Uint8Array(buf), offset);
return res.buffer as ArrayBuffer;
}
export function b64UrlDecode(b64url: string): Buffer {
return Buffer.from(Convert.FromBase64Url(b64url));
}