mirror of
https://github.com/denoland/deno.git
synced 2024-12-23 15:49:44 -05:00
fix(ext/node): support spki
format in createPublicKey (#22918)
This commit is contained in:
parent
cf3c6f9b08
commit
9c348a0acd
3 changed files with 43 additions and 11 deletions
|
@ -1480,14 +1480,11 @@ fn parse_public_key(
|
|||
}
|
||||
Ok(doc)
|
||||
}
|
||||
"der" => {
|
||||
match type_ {
|
||||
"pkcs1" => pkcs8::Document::from_pkcs1_der(key)
|
||||
.map_err(|_| type_error("Invalid PKCS1 public key")),
|
||||
// TODO(@iuioiua): spki type
|
||||
_ => Err(type_error(format!("Unsupported key type: {}", type_))),
|
||||
}
|
||||
}
|
||||
"der" => match type_ {
|
||||
"pkcs1" => pkcs8::Document::from_pkcs1_der(key)
|
||||
.map_err(|_| type_error("Invalid PKCS1 public key")),
|
||||
_ => Err(type_error(format!("Unsupported key type: {}", type_))),
|
||||
},
|
||||
_ => Err(type_error(format!("Unsupported key format: {}", format))),
|
||||
}
|
||||
}
|
||||
|
@ -1499,8 +1496,14 @@ pub fn op_node_create_public_key(
|
|||
#[string] format: &str,
|
||||
#[string] type_: &str,
|
||||
) -> Result<AsymmetricKeyDetails, AnyError> {
|
||||
let doc = parse_public_key(key, format, type_)?;
|
||||
let pk_info = spki::SubjectPublicKeyInfoRef::try_from(doc.as_bytes())?;
|
||||
let mut doc = None;
|
||||
|
||||
let pk_info = if type_ != "spki" {
|
||||
doc.replace(parse_public_key(key, format, type_)?);
|
||||
spki::SubjectPublicKeyInfoRef::try_from(doc.as_ref().unwrap().as_bytes())?
|
||||
} else {
|
||||
spki::SubjectPublicKeyInfoRef::try_from(key)?
|
||||
};
|
||||
|
||||
let alg = pk_info.algorithm.oid;
|
||||
|
||||
|
|
|
@ -66,7 +66,7 @@ export const getArrayBufferOrView = hideStackFrames(
|
|||
| Uint16Array
|
||||
| Uint32Array => {
|
||||
if (isAnyArrayBuffer(buffer)) {
|
||||
return buffer;
|
||||
return new Uint8Array(buffer);
|
||||
}
|
||||
if (typeof buffer === "string") {
|
||||
if (encoding === "buffer") {
|
||||
|
|
|
@ -284,3 +284,32 @@ Deno.test("createPublicKey() EC", function () {
|
|||
assertEquals(key.asymmetricKeyType, "ec");
|
||||
assertEquals(key.asymmetricKeyDetails?.namedCurve, "p256");
|
||||
});
|
||||
|
||||
Deno.test("createPublicKey SPKI for DH", async function () {
|
||||
const { publicKey, privateKey } = await crypto.subtle.generateKey(
|
||||
{
|
||||
name: "ECDH",
|
||||
namedCurve: "P-384",
|
||||
},
|
||||
true,
|
||||
["deriveKey", "deriveBits"],
|
||||
);
|
||||
|
||||
const exportedPublicKey = await crypto.subtle.exportKey("spki", publicKey);
|
||||
const exportedPrivateKey = await crypto.subtle.exportKey("pkcs8", privateKey);
|
||||
|
||||
const pubKey = createPublicKey({
|
||||
key: Buffer.from(exportedPublicKey),
|
||||
format: "der",
|
||||
type: "spki",
|
||||
});
|
||||
|
||||
const privKey = createPrivateKey({
|
||||
key: Buffer.from(exportedPrivateKey),
|
||||
format: "der",
|
||||
type: "pkcs8",
|
||||
});
|
||||
|
||||
assertEquals(pubKey.asymmetricKeyType, "ec");
|
||||
assertEquals(privKey.asymmetricKeyType, "ec");
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue