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 exportKey(session: graphene.Session, format: KeyFormat, key: RsaCryptoKey): Promise {
switch (format.toLowerCase()) {
case "jwk":
if (key.type === "private") {
return this.exportJwkPrivateKey(key);
} else {
return this.exportJwkPublicKey(key);
}
case "pkcs8": {
const jwk = await this.exportJwkPrivateKey(key);
const privateKey = new PrivateKeyInfo();
privateKey.fromJSON(jwk);
return privateKey.toSchema(true).toBER(false);
}
case "spki": {
const jwk = await this.exportJwkPublicKey(key);
const publicKey = new PublicKeyInfo();
publicKey.fromJSON(jwk);
return publicKey.toSchema(true).toBER(false);
}
case "raw": {
// export subjectPublicKey BIT_STRING value
const jwk = await this.exportJwkPublicKey(key);
const publicKey = new PublicKeyInfo();
publicKey.fromJSON(jwk);
return publicKey.toSchema(true).valueBlock.value[1].valueBlock.valueHex;
}
protected static pkcs2jwk(raw: ArrayBuffer): JsonWebKey {
const asn1Pkcs8 = Asn1Js.fromBER(raw);
const pkcs8 = new pkijs.PrivateKeyInfo({ schema: asn1Pkcs8.result });
if (pkcs8.privateKeyAlgorithm.algorithmId !== "1.2.840.10045.2.1") {
throw new Error("PKCS8 is not EC private key");
}
const algId = pkcs8.privateKeyAlgorithm.algorithmParams.valueBlock.toString();
let crvName = algId;
switch (crvName) {
case "1.3.132.0.10": // K-256
crvName = "K-256";
break;
case "1.2.840.10045.3.1.7": // P-256
crvName = "P-256";
break;
case "1.3.132.0.34": // P-384
public static async exportKey(format: string, key: CryptoKey, session?: Session): Promise {
await super.exportKey.call(this, format, key, session);
switch (format.toLowerCase()) {
case "jwk":
if (key.type === "private") {
return this.exportJwkPrivateKey(key);
} else {
return this.exportJwkPublicKey(key);
}
case "pkcs8": {
const jwk = await this.exportJwkPrivateKey(key);
const privateKey = new PrivateKeyInfo();
privateKey.fromJSON(jwk);
return privateKey.toSchema(true).toBER(false);
}
case "spki": {
const jwk = await this.exportJwkPublicKey(key);
const publicKey = new PublicKeyInfo();
publicKey.fromJSON(jwk);
return publicKey.toSchema(true).toBER(false);
}
case "raw": {
// export subjectPublicKey BIT_STRING value
const jwk = await this.exportJwkPublicKey(key);
const publicKey = new PublicKeyInfo();
publicKey.fromJSON(jwk);
return publicKey.toSchema(true).valueBlock.value[1].valueBlock.valueHex;
}
function pkcs2jwk(raw: ArrayBuffer): JsonWebKey {
const asn1Pkcs8 = Asn1Js.fromBER(raw);
const pkcs8 = new PrivateKeyInfo({ schema: asn1Pkcs8.result });
if (pkcs8.privateKeyAlgorithm.algorithmId !== "1.2.840.10045.2.1") {
throw new Error("PKCS8 is not EC private key");
}
const algId = pkcs8.privateKeyAlgorithm.algorithmParams.valueBlock.toString();
let crvName = algId;
switch (crvName) {
case "1.3.132.0.10": // K-256
crvName = "K-256";
break;
case "1.2.840.10045.3.1.7": // P-256
crvName = "P-256";
break;
case "1.3.132.0.34": // P-384
break;
default:
}
} else {
throw new Error("Absent mandatory parameter \"crv\"");
}
["d"].forEach((name) => {
if (name in jwk) {
parsedKey.privateKey = new Asn1Js.OctetString({ valueHex: getCoordinate((jwk as any)[name], coordinateLength) });
} else {
throw new Error(`Absent mandatory parameter '${name}'`);
}
});
const pkcs8 = new PrivateKeyInfo();
pkcs8.privateKeyAlgorithm = new AlgorithmIdentifier({
algorithmId: "1.2.840.10045.2.1",
algorithmParams: new Asn1Js.ObjectIdentifier({ value: parsedKey.namedCurve }),
});
pkcs8.privateKey = new Asn1Js.OctetString({ valueHex: parsedKey.toSchema().toBER(false) });
return pkcs8.toSchema().toBER(false);
}
return this.importJwkPrivateKey(session!, jwk, algorithm as RsaHashedKeyGenParams, extractable, keyUsages);
} else {
return this.importJwkPublicKey(session!, jwk, algorithm as RsaHashedKeyGenParams, extractable, keyUsages);
}
case "spki": {
const arBuf = new Uint8Array(keyData as Uint8Array).buffer as ArrayBuffer;
const asn1 = asn1js.fromBER(arBuf);
const jwk = new PublicKeyInfo({ schema: asn1.result }).toJSON();
return this.importJwkPublicKey(session!, jwk, algorithm as RsaHashedKeyGenParams, extractable, keyUsages);
}
case "pkcs8": {
const arBuf = new Uint8Array(keyData as Uint8Array).buffer as ArrayBuffer;
const asn1 = asn1js.fromBER(arBuf);
const jwk = new PrivateKeyInfo({ schema: asn1.result }).toJSON();
return this.importJwkPrivateKey(session!, jwk, algorithm as RsaHashedKeyGenParams, extractable, keyUsages);
}
default:
throw new core.OperationError("format: Must be 'jwk', 'pkcs8' or 'spki'");
}
}
break;
default:
}
} else {
throw new Error("Absent mandatory parameter \"crv\"");
}
["d"].forEach((name) => {
if (name in jwk) {
parsedKey.privateKey = new Asn1Js.OctetString({ valueHex: this.getCoordinate((jwk as any)[name], coordinateLength) });
} else {
throw new Error(`Absent mandatory parameter '${name}'`);
}
});
const pkcs8 = new pkijs.PrivateKeyInfo();
pkcs8.privateKeyAlgorithm = new pkijs.AlgorithmIdentifier({
algorithmId: "1.2.840.10045.2.1",
algorithmParams: new Asn1Js.ObjectIdentifier({ value: parsedKey.namedCurve }),
});
pkcs8.privateKey = new Asn1Js.OctetString({ valueHex: parsedKey.toSchema().toBER(false) });
return pkcs8.toSchema().toBER(false);
}
return this.importJwkPrivateKey(session!, jwk, algorithm as RsaHashedKeyGenParams, extractable, keyUsages);
} else {
return this.importJwkPublicKey(session!, jwk, algorithm as RsaHashedKeyGenParams, extractable, keyUsages);
}
case "spki": {
const arBuf = new Uint8Array(keyData as Uint8Array).buffer as ArrayBuffer;
const asn1 = Asn1Js.fromBER(arBuf);
const jwk = new PublicKeyInfo({ schema: asn1.result }).toJSON();
return this.importJwkPublicKey(session!, jwk, algorithm as RsaHashedKeyGenParams, extractable, keyUsages);
}
case "pkcs8": {
const arBuf = new Uint8Array(keyData as Uint8Array).buffer as ArrayBuffer;
const asn1 = Asn1Js.fromBER(arBuf);
const jwk = new PrivateKeyInfo({ schema: asn1.result }).toJSON();
return this.importJwkPrivateKey(session!, jwk, algorithm as RsaHashedKeyGenParams, extractable, keyUsages);
}
default:
throw new Error(`Not supported format '${format}'`);
}
});
}