Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ext/crypto): support exporting RSA JWKs #13081

Merged
merged 7 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 127 additions & 34 deletions ext/crypto/00_crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,32 @@
const { btoa } = window.__bootstrap.base64;

const {
ArrayPrototypeFind,
ArrayPrototypeEvery,
ArrayPrototypeIncludes,
ArrayBuffer,
ArrayBufferIsView,
ArrayPrototypeEvery,
ArrayPrototypeFind,
ArrayPrototypeIncludes,
BigInt64Array,
Int16Array,
Int32Array,
Int8Array,
ObjectAssign,
StringFromCharCode,
StringPrototypeReplace,
StringPrototypeToLowerCase,
StringPrototypeToUpperCase,
StringPrototypeReplace,
StringFromCharCode,
Symbol,
SymbolFor,
SyntaxError,
WeakMap,
WeakMapPrototypeGet,
WeakMapPrototypeSet,
Int8Array,
Uint8Array,
TypedArrayPrototypeSlice,
Int16Array,
TypeError,
Uint16Array,
Int32Array,
Uint32Array,
Uint8Array,
Uint8ClampedArray,
TypeError,
WeakMap,
WeakMapPrototypeGet,
WeakMapPrototypeSet,
} = window.__bootstrap.primordials;

// P-521 is not yet supported.
Expand Down Expand Up @@ -2464,27 +2465,52 @@
let hash;

// 8.
switch (jwk.alg) {
case undefined:
hash = undefined;
break;
case "RS1":
hash = "SHA-1";
break;
case "RS256":
hash = "SHA-256";
break;
case "RS384":
hash = "SHA-384";
break;
case "RS512":
hash = "SHA-512";
break;
default:
throw new DOMException(
`'alg' property of JsonWebKey must be one of 'RS1', 'RS256', 'RS384', 'RS512'`,
"DataError",
);
if (normalizedAlgorithm.name === "RSA-OAEP") {
switch (jwk.alg) {
case undefined:
hash = undefined;
break;
case "RSA-OAEP":
hash = "SHA-1";
break;
case "RSA-OAEP-256":
hash = "SHA-256";
break;
case "RSA-OAEP-384":
hash = "SHA-384";
break;
case "RSA-OAEP-512":
hash = "SHA-512";
break;
default:
throw new DOMException(
`'alg' property of JsonWebKey must be one of 'RSA-OAEP', 'RSA-OAEP-256', 'RSA-OAEP-384', or 'RSA-OAEP-512'`,
"DataError",
);
}
} else {
lucacasonato marked this conversation as resolved.
Show resolved Hide resolved
switch (jwk.alg) {
case undefined:
hash = undefined;
break;
case "RS1":
hash = "SHA-1";
break;
case "RS256":
hash = "SHA-256";
break;
case "RS384":
hash = "SHA-384";
break;
case "RS512":
hash = "SHA-512";
break;
default:
throw new DOMException(
`'alg' property of JsonWebKey must be one of 'RS1', 'RS256', 'RS384', 'RS512'`,
"DataError",
);
}
}

// 9.
Expand Down Expand Up @@ -2822,6 +2848,73 @@
// 3.
return data.buffer;
}
case "jwk": {
// 1-2.
const jwk = {
kty: "RSA",
};

// 3.
const hash = key[_algorithm].hash.name;

// 4.
if (key[_algorithm].name === "RSA-OAEP") {
switch (hash) {
case "SHA-1":
jwk.alg = "RSA-OAEP";
break;
case "SHA-256":
jwk.alg = "RSA-OAEP-256";
break;
case "SHA-384":
jwk.alg = "RSA-OAEP-384";
break;
case "SHA-512":
jwk.alg = "RSA-OAEP-512";
break;
default:
throw new DOMException(
"Hash algorithm not supported",
"NotSupportedError",
);
}
} else {
switch (hash) {
case "SHA-1":
jwk.alg = "RS1";
break;
case "SHA-256":
jwk.alg = "RS256";
break;
case "SHA-384":
jwk.alg = "RS384";
break;
case "SHA-512":
jwk.alg = "RS512";
break;
default:
throw new DOMException(
"Hash algorithm not supported",
"NotSupportedError",
);
}
}

// 5-6.
const data = core.opSync("op_crypto_export_key", {
format: key[_type] === "private" ? "jwkprivate" : "jwkpublic",
algorithm: key[_algorithm].name,
}, innerKey);
ObjectAssign(jwk, data);

// 7.
jwk.key_ops = key.usages;

// 8.
jwk.ext = key[_extractable];

return jwk;
}
default:
throw new DOMException("Not implemented", "NotSupportedError");
}
Expand Down
61 changes: 59 additions & 2 deletions ext/crypto/export_key.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use deno_core::error::custom_error;
use deno_core::error::AnyError;
use deno_core::OpState;
use deno_core::ZeroCopyBuf;
use rsa::pkcs1::UIntBytes;
use serde::Deserialize;
use serde::Serialize;
use spki::der::asn1;
use spki::der::Decodable;
use spki::der::Encodable;

use crate::shared::*;
Expand All @@ -21,7 +24,8 @@ pub struct ExportKeyOptions {
pub enum ExportKeyFormat {
Pkcs8,
Spki,
Jwk,
JwkPublic,
JwkPrivate,
}

#[derive(Deserialize)]
Expand All @@ -40,6 +44,20 @@ pub enum ExportKeyAlgorithm {
pub enum ExportKeyResult {
Pkcs8(ZeroCopyBuf),
Spki(ZeroCopyBuf),
JwkPublicRsa {
n: String,
e: String,
},
JwkPrivateRsa {
n: String,
e: String,
d: String,
p: String,
q: String,
dp: String,
dq: String,
qi: String,
},
}

pub fn op_crypto_export_key(
Expand All @@ -54,6 +72,10 @@ pub fn op_crypto_export_key(
}
}

fn uint_to_b64(bytes: UIntBytes) -> String {
base64::encode_config(bytes.as_bytes(), base64::URL_SAFE_NO_PAD)
}

fn export_key_rsa(
format: ExportKeyFormat,
key_data: RawKeyData,
Expand Down Expand Up @@ -108,6 +130,41 @@ fn export_key_rsa(

Ok(ExportKeyResult::Pkcs8(pkcs8_der.into()))
}
_ => Err(unsupported_format()),
ExportKeyFormat::JwkPublic => {
let public_key = key_data.as_rsa_public_key()?;
let public_key = rsa::pkcs1::RsaPublicKey::from_der(&public_key)
.map_err(|_| {
custom_error(
"DOMExceptionOperationError",
"failed to decode public key",
)
})?;

Ok(ExportKeyResult::JwkPublicRsa {
n: uint_to_b64(public_key.modulus),
e: uint_to_b64(public_key.public_exponent),
})
}
ExportKeyFormat::JwkPrivate => {
let private_key = key_data.as_rsa_private_key()?;
let private_key = rsa::pkcs1::RsaPrivateKey::from_der(private_key)
.map_err(|_| {
custom_error(
"DOMExceptionOperationError",
"failed to decode private key",
)
})?;

Ok(ExportKeyResult::JwkPrivateRsa {
n: uint_to_b64(private_key.modulus),
e: uint_to_b64(private_key.public_exponent),
d: uint_to_b64(private_key.private_exponent),
p: uint_to_b64(private_key.prime1),
q: uint_to_b64(private_key.prime2),
dp: uint_to_b64(private_key.exponent1),
dq: uint_to_b64(private_key.exponent2),
qi: uint_to_b64(private_key.coefficient),
})
}
}
}
Loading