summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/internal/crypto
diff options
context:
space:
mode:
Diffstat (limited to 'ext/node/polyfills/internal/crypto')
-rw-r--r--ext/node/polyfills/internal/crypto/keygen.ts10
-rw-r--r--ext/node/polyfills/internal/crypto/keys.ts34
2 files changed, 36 insertions, 8 deletions
diff --git a/ext/node/polyfills/internal/crypto/keygen.ts b/ext/node/polyfills/internal/crypto/keygen.ts
index f3263aecf..dd5d5ad7e 100644
--- a/ext/node/polyfills/internal/crypto/keygen.ts
+++ b/ext/node/polyfills/internal/crypto/keygen.ts
@@ -7,6 +7,8 @@
import { KeyObject } from "ext:deno_node/internal/crypto/keys.ts";
import { kAesKeyLengths } from "ext:deno_node/internal/crypto/util.ts";
import {
+ PrivateKeyObject,
+ PublicKeyObject,
SecretKeyObject,
setOwnedKey,
} from "ext:deno_node/internal/crypto/keys.ts";
@@ -564,8 +566,8 @@ export function generateKeyPair(
) => void,
) {
createJob(kAsync, type, options).then(([privateKey, publicKey]) => {
- privateKey = new KeyObject("private", setOwnedKey(privateKey));
- publicKey = new KeyObject("public", setOwnedKey(publicKey));
+ privateKey = new PrivateKeyObject(setOwnedKey(privateKey), { type });
+ publicKey = new PublicKeyObject(setOwnedKey(publicKey), { type });
if (typeof options === "object" && options !== null) {
const { publicKeyEncoding, privateKeyEncoding } = options as any;
@@ -766,8 +768,8 @@ export function generateKeyPairSync(
| KeyPairSyncResult<string | Buffer, string | Buffer> {
let [privateKey, publicKey] = createJob(kSync, type, options);
- privateKey = new KeyObject("private", setOwnedKey(privateKey));
- publicKey = new KeyObject("public", setOwnedKey(publicKey));
+ privateKey = new PrivateKeyObject(setOwnedKey(privateKey), { type });
+ publicKey = new PublicKeyObject(setOwnedKey(publicKey), { type });
if (typeof options === "object" && options !== null) {
const { publicKeyEncoding, privateKeyEncoding } = options as any;
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,
};