summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/internal/crypto/keys.ts
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2024-04-29 19:16:38 +0530
committerGitHub <noreply@github.com>2024-04-29 19:16:38 +0530
commitb02ffec37c73be8a73b95b33b32efa693e84e01b (patch)
tree6bdcda1ee6e6e7d1b63d05320fe2236dfa86999b /ext/node/polyfills/internal/crypto/keys.ts
parent7d937045910968fbb2c050e803d79bc1c1e5984b (diff)
fix(ext/node): exporting rsa public keys (#23596)
Initial support for exporting rsa public KeyObject. Current assumption is that RSA keys are stored in pkcs1 der format in key storage. Ref https://github.com/denoland/deno/issues/23471 Ref https://github.com/denoland/deno/issues/18928 Ref https://github.com/denoland/deno/issues/21124
Diffstat (limited to 'ext/node/polyfills/internal/crypto/keys.ts')
-rw-r--r--ext/node/polyfills/internal/crypto/keys.ts34
1 files changed, 30 insertions, 4 deletions
diff --git a/ext/node/polyfills/internal/crypto/keys.ts b/ext/node/polyfills/internal/crypto/keys.ts
index 4ab8cac4f..8cb9ab690 100644
--- a/ext/node/polyfills/internal/crypto/keys.ts
+++ b/ext/node/polyfills/internal/crypto/keys.ts
@@ -7,6 +7,8 @@
import {
op_node_create_private_key,
op_node_create_public_key,
+ op_node_export_rsa_public_pem,
+ op_node_export_rsa_spki_der,
} from "ext:core/ops";
import {
@@ -360,7 +362,7 @@ class AsymmetricKeyObject extends KeyObject {
}
}
-class PrivateKeyObject extends AsymmetricKeyObject {
+export class PrivateKeyObject extends AsymmetricKeyObject {
constructor(handle: unknown, details: unknown) {
super("private", handle, details);
}
@@ -370,13 +372,35 @@ class PrivateKeyObject extends AsymmetricKeyObject {
}
}
-class PublicKeyObject extends AsymmetricKeyObject {
+export class PublicKeyObject extends AsymmetricKeyObject {
constructor(handle: unknown, details: unknown) {
super("public", handle, details);
}
- export(_options: unknown) {
- notImplemented("crypto.PublicKeyObject.prototype.export");
+ export(options: unknown) {
+ const key = KEY_STORE.get(this[kHandle]);
+ switch (this.asymmetricKeyType) {
+ case "rsa":
+ case "rsa-pss": {
+ switch (options.format) {
+ case "pem":
+ return op_node_export_rsa_public_pem(key);
+ case "der": {
+ if (options.type == "pkcs1") {
+ return key;
+ } else {
+ return op_node_export_rsa_spki_der(key);
+ }
+ }
+ default:
+ throw new TypeError(`exporting ${options.type} is not implemented`);
+ }
+ }
+ default:
+ throw new TypeError(
+ `exporting ${this.asymmetricKeyType} is not implemented`,
+ );
+ }
}
}
@@ -414,4 +438,6 @@ export default {
prepareSecretKey,
setOwnedKey,
SecretKeyObject,
+ PrivateKeyObject,
+ PublicKeyObject,
};