This repository has been archived on 2023-04-04. You can view files and clone it, but cannot push or open issues or pull requests.
webcrypto/src/mechs/rsa/rsa_ssa.ts

50 lines
2.0 KiB
TypeScript
Raw Normal View History

2019-01-25 10:43:13 +00:00
import * as core from "webcrypto-core";
2020-03-13 15:20:02 +00:00
import { setCryptoKey, getCryptoKey } from "../storage";
2019-01-25 10:43:13 +00:00
import { RsaCrypto } from "./crypto";
import { RsaPrivateKey } from "./private_key";
import { RsaPublicKey } from "./public_key";
export class RsaSsaProvider extends core.RsaSsaProvider {
2022-03-02 18:35:31 +00:00
public async onGenerateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<globalThis.CryptoKeyPair> {
2020-03-13 15:20:02 +00:00
const keys = await RsaCrypto.generateKey(
2019-01-25 10:43:13 +00:00
{
...algorithm,
name: this.name,
},
extractable,
keyUsages);
2020-03-13 15:20:02 +00:00
return {
privateKey: setCryptoKey(keys.privateKey as RsaPrivateKey),
publicKey: setCryptoKey(keys.publicKey as RsaPublicKey),
};
2019-01-25 10:43:13 +00:00
}
public async onSign(algorithm: Algorithm, key: RsaPrivateKey, data: ArrayBuffer): Promise<ArrayBuffer> {
2020-03-13 15:20:02 +00:00
return RsaCrypto.sign(algorithm, getCryptoKey(key) as RsaPrivateKey, new Uint8Array(data));
2019-01-25 10:43:13 +00:00
}
public async onVerify(algorithm: Algorithm, key: RsaPublicKey, signature: ArrayBuffer, data: ArrayBuffer): Promise<boolean> {
2020-03-13 15:20:02 +00:00
return RsaCrypto.verify(algorithm, getCryptoKey(key) as RsaPublicKey, new Uint8Array(signature), new Uint8Array(data));
2019-01-25 10:43:13 +00:00
}
public async onExportKey(format: KeyFormat, key: CryptoKey): Promise<JsonWebKey | ArrayBuffer> {
2020-03-13 15:20:02 +00:00
return RsaCrypto.exportKey(format, getCryptoKey(key));
2019-01-25 10:43:13 +00:00
}
public async onImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: RsaHashedImportParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey> {
const key = await RsaCrypto.importKey(format, keyData, {...algorithm, name: this.name}, extractable, keyUsages);
2020-03-13 15:20:02 +00:00
return setCryptoKey(key);
2019-01-25 10:43:13 +00:00
}
2022-03-02 18:35:31 +00:00
public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) {
2019-01-25 10:43:13 +00:00
super.checkCryptoKey(key, keyUsage);
2020-03-13 15:20:02 +00:00
const internalKey = getCryptoKey(key);
if (!(internalKey instanceof RsaPrivateKey || internalKey instanceof RsaPublicKey)) {
2019-01-25 10:43:13 +00:00
throw new TypeError("key: Is not RSA CryptoKey");
}
}
}