Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
}
default:
throw new Error(`Not supported format '${format}'`);
}
}
}
case "pkcs8": {
const jwk = await this.exportJwkPrivateKey(key);
return jwk2pkcs(jwk);
}
case "spki": {
const jwk = await this.exportJwkPublicKey(key);
return jwk2spki(jwk);
}
case "raw": {
// export subjectPublicKey BIT_STRING value
const jwk = await this.exportJwkPublicKey(key);
if ((key.algorithm as EcKeyGenParams).namedCurve === "X25519") {
return Base64Url.decode(jwk.x!).buffer as ArrayBuffer;
} else {
const publicKey = new PublicKeyInfo();
publicKey.fromJSON(jwk);
return publicKey.toSchema(true).valueBlock.value[1].valueBlock.valueHex;
}
}
default:
throw new Error(`Not supported format '${format}'`);
}
}
}
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'");
}
}
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;
}
default:
throw new core.OperationError("format: Must be 'jwk', 'pkcs8' or 'spki'");
}
}
function spki2jwk(raw: ArrayBuffer): JsonWebKey {
const asn1Spki = Asn1Js.fromBER(raw);
const spki = new PublicKeyInfo({ schema: asn1Spki.result });
if (spki.algorithm.algorithmId !== "1.2.840.10045.2.1") {
throw new Error("SPKI is not EC public key");
}
const algId = spki.algorithm.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 importKey(session: graphene.Session, format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: RsaHashedImportParams, extractable: boolean, keyUsages: KeyUsage[]): Promise {
switch (format.toLowerCase()) {
case "jwk":
const jwk: any = keyData;
if (jwk.d) {
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\"");
}
["x", "y"].forEach((name) => {
if (name in jwk) {
parsedKey[name] = getCoordinate((jwk as any)[name], coordinateLength);
} else {
throw new Error(`Absent mandatory parameter '${name}'`);
}
});
const spki = new PublicKeyInfo();
spki.algorithm = new AlgorithmIdentifier({
algorithmId: "1.2.840.10045.2.1",
algorithmParams: new Asn1Js.ObjectIdentifier({ value: parsedKey.namedCurve }),
});
spki.subjectPublicKey = new Asn1Js.BitString({ valueHex: parsedKey.toSchema().toBER(false) });
return spki.toSchema().toBER(false);
}
break;
default:
}
} else {
throw new Error("Absent mandatory parameter \"crv\"");
}
["x", "y"].forEach((name) => {
if (name in jwk) {
parsedKey[name] = this.getCoordinate((jwk as any)[name], coordinateLength);
} else {
throw new Error(`Absent mandatory parameter '${name}'`);
}
});
const spki = new pkijs.PublicKeyInfo();
spki.algorithm = new pkijs.AlgorithmIdentifier({
algorithmId: "1.2.840.10045.2.1",
algorithmParams: new Asn1Js.ObjectIdentifier({ value: parsedKey.namedCurve }),
});
spki.subjectPublicKey = new Asn1Js.BitString({ valueHex: parsedKey.toSchema().toBER(false) });
return spki.toSchema().toBER(false);
}
.then(() => {
switch (format.toLowerCase()) {
case "jwk":
const jwk: any = keyData;
if (jwk.d) {
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}'`);
}
});
}