From 8e0c24db23705c13bdb379884337ac08f0ab596f Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Fri, 19 Jan 2024 17:57:38 +0530 Subject: [PATCH 1/2] use cppgc for managing X509Certificate --- .cargo/local-build.toml | 2 +- Cargo.lock | 6 -- ext/node/ops/crypto/x509.rs | 125 ++++++++++++------------------------ 3 files changed, 42 insertions(+), 91 deletions(-) diff --git a/.cargo/local-build.toml b/.cargo/local-build.toml index a2d68e4f277192..bc4a97999b54e6 100644 --- a/.cargo/local-build.toml +++ b/.cargo/local-build.toml @@ -12,4 +12,4 @@ deno_core = { path = "../deno_core/core" } deno_ops = { path = "../deno_core/ops" } serde_v8 = { path = "../deno_core/serde_v8" } -v8 = { path = "../rusty_v8" } +# v8 = { path = "../rusty_v8" } diff --git a/Cargo.lock b/Cargo.lock index 19a83602665c0c..622f77ed6f933e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1151,8 +1151,6 @@ dependencies = [ [[package]] name = "deno_core" version = "0.247.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff6e8ee2e08528c1da4f5a48659c67d12012e831c737f6266534d4f550ccc8c3" dependencies = [ "anyhow", "bit-set", @@ -1601,8 +1599,6 @@ dependencies = [ [[package]] name = "deno_ops" version = "0.123.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19f29c5fe78479c3fa437409dca009363feb77036c067d6f3682adff8d06f547" dependencies = [ "proc-macro-rules", "proc-macro2", @@ -5256,8 +5252,6 @@ dependencies = [ [[package]] name = "serde_v8" version = "0.156.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48ad52e56e21faee5d310a898ab77f83abdab737fde1e97fa6a8cd6615d139ef" dependencies = [ "bytes", "derive_more", diff --git a/ext/node/ops/crypto/x509.rs b/ext/node/ops/crypto/x509.rs index b5779ffa93b02a..440e7c291cca9e 100644 --- a/ext/node/ops/crypto/x509.rs +++ b/ext/node/ops/crypto/x509.rs @@ -3,10 +3,7 @@ use deno_core::error::bad_resource_id; use deno_core::error::AnyError; use deno_core::op2; -use deno_core::OpState; -use deno_core::Resource; - -use std::borrow::Cow; +use deno_core::v8; use x509_parser::der_parser::asn1_rs::Any; use x509_parser::der_parser::asn1_rs::Tag; @@ -48,17 +45,11 @@ impl std::ops::Deref for Certificate { } } -impl Resource for Certificate { - fn name(&self) -> Cow { - "x509Certificate".into() - } -} - -#[op2(fast)] -pub fn op_node_x509_parse( - state: &mut OpState, +#[op2] +pub fn op_node_x509_parse<'s>( + scope: &'s mut v8::HandleScope, #[buffer] buf: &[u8], -) -> Result { +) -> Result, AnyError> { let pem = match pem::parse_x509_pem(buf) { Ok((_, pem)) => Some(pem), Err(_) => None, @@ -76,32 +67,25 @@ pub fn op_node_x509_parse( cert: unsafe { std::mem::transmute(cert) }, pem, }; - let rid = state.resource_table.add(cert); - Ok(rid) + + let obj = deno_core::cppgc::make_cppgc_object(scope, cert); + Ok(obj) } #[op2(fast)] -pub fn op_node_x509_ca( - state: &mut OpState, - rid: u32, -) -> Result { - let cert = state - .resource_table - .get::(rid) - .map_err(|_| bad_resource_id())?; +pub fn op_node_x509_ca(rid: v8::Local) -> Result { + let cert = deno_core::cppgc::unwrap_cppgc_object::(rid) + .ok_or_else(bad_resource_id)?; Ok(cert.is_ca()) } #[op2(fast)] pub fn op_node_x509_check_email( - state: &mut OpState, - rid: u32, + rid: v8::Local, #[string] email: &str, ) -> Result { - let cert = state - .resource_table - .get::(rid) - .map_err(|_| bad_resource_id())?; + let cert = deno_core::cppgc::unwrap_cppgc_object::(rid) + .ok_or_else(bad_resource_id)?; let subject = cert.subject(); if subject @@ -137,65 +121,50 @@ pub fn op_node_x509_check_email( #[op2] #[string] pub fn op_node_x509_fingerprint( - state: &mut OpState, - rid: u32, + rid: v8::Local, ) -> Result, AnyError> { - let cert = state - .resource_table - .get::(rid) - .map_err(|_| bad_resource_id())?; + let cert = deno_core::cppgc::unwrap_cppgc_object::(rid) + .ok_or_else(bad_resource_id)?; Ok(cert.fingerprint::()) } #[op2] #[string] pub fn op_node_x509_fingerprint256( - state: &mut OpState, - rid: u32, + rid: v8::Local, ) -> Result, AnyError> { - let cert = state - .resource_table - .get::(rid) - .map_err(|_| bad_resource_id())?; + let cert = deno_core::cppgc::unwrap_cppgc_object::(rid) + .ok_or_else(bad_resource_id)?; Ok(cert.fingerprint::()) } #[op2] #[string] pub fn op_node_x509_fingerprint512( - state: &mut OpState, - rid: u32, + rid: v8::Local, ) -> Result, AnyError> { - let cert = state - .resource_table - .get::(rid) - .map_err(|_| bad_resource_id())?; + let cert = deno_core::cppgc::unwrap_cppgc_object::(rid) + .ok_or_else(bad_resource_id)?; Ok(cert.fingerprint::()) } #[op2] #[string] pub fn op_node_x509_get_issuer( - state: &mut OpState, - rid: u32, + rid: v8::Local, ) -> Result { - let cert = state - .resource_table - .get::(rid) - .map_err(|_| bad_resource_id())?; + let cert = deno_core::cppgc::unwrap_cppgc_object::(rid) + .ok_or_else(bad_resource_id)?; Ok(x509name_to_string(cert.issuer(), oid_registry())?) } #[op2] #[string] pub fn op_node_x509_get_subject( - state: &mut OpState, - rid: u32, + rid: v8::Local, ) -> Result { - let cert = state - .resource_table - .get::(rid) - .map_err(|_| bad_resource_id())?; + let cert = deno_core::cppgc::unwrap_cppgc_object::(rid) + .ok_or_else(bad_resource_id)?; Ok(x509name_to_string(cert.subject(), oid_registry())?) } @@ -262,39 +231,30 @@ fn x509name_to_string( #[op2] #[string] pub fn op_node_x509_get_valid_from( - state: &mut OpState, - rid: u32, + rid: v8::Local, ) -> Result { - let cert = state - .resource_table - .get::(rid) - .map_err(|_| bad_resource_id())?; + let cert = deno_core::cppgc::unwrap_cppgc_object::(rid) + .ok_or_else(bad_resource_id)?; Ok(cert.validity().not_before.to_string()) } #[op2] #[string] pub fn op_node_x509_get_valid_to( - state: &mut OpState, - rid: u32, + rid: v8::Local, ) -> Result { - let cert = state - .resource_table - .get::(rid) - .map_err(|_| bad_resource_id())?; + let cert = deno_core::cppgc::unwrap_cppgc_object::(rid) + .ok_or_else(bad_resource_id)?; Ok(cert.validity().not_after.to_string()) } #[op2] #[string] pub fn op_node_x509_get_serial_number( - state: &mut OpState, - rid: u32, + rid: v8::Local, ) -> Result { - let cert = state - .resource_table - .get::(rid) - .map_err(|_| bad_resource_id())?; + let cert = deno_core::cppgc::unwrap_cppgc_object::(rid) + .ok_or_else(bad_resource_id)?; let mut s = cert.serial.to_str_radix(16); s.make_ascii_uppercase(); Ok(s) @@ -302,13 +262,10 @@ pub fn op_node_x509_get_serial_number( #[op2(fast)] pub fn op_node_x509_key_usage( - state: &mut OpState, - rid: u32, + rid: v8::Local, ) -> Result { - let cert = state - .resource_table - .get::(rid) - .map_err(|_| bad_resource_id())?; + let cert = deno_core::cppgc::unwrap_cppgc_object::(rid) + .ok_or_else(bad_resource_id)?; let key_usage = cert .extensions() From e7f89c7deb06cfb144f1574a9b98be32afb13585 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Sat, 20 Jan 2024 08:46:19 +0530 Subject: [PATCH 2/2] update deno_core --- .cargo/local-build.toml | 2 +- Cargo.lock | 12 +++++++++--- Cargo.toml | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/.cargo/local-build.toml b/.cargo/local-build.toml index bc4a97999b54e6..a2d68e4f277192 100644 --- a/.cargo/local-build.toml +++ b/.cargo/local-build.toml @@ -12,4 +12,4 @@ deno_core = { path = "../deno_core/core" } deno_ops = { path = "../deno_core/ops" } serde_v8 = { path = "../deno_core/serde_v8" } -# v8 = { path = "../rusty_v8" } +v8 = { path = "../rusty_v8" } diff --git a/Cargo.lock b/Cargo.lock index 622f77ed6f933e..528d2427097eec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1150,7 +1150,9 @@ dependencies = [ [[package]] name = "deno_core" -version = "0.247.0" +version = "0.248.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "974d075b6647aa6cca14ea75c3833f8996b1c3e487adcde490977f21ac14e27e" dependencies = [ "anyhow", "bit-set", @@ -1598,7 +1600,9 @@ dependencies = [ [[package]] name = "deno_ops" -version = "0.123.0" +version = "0.124.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1bc46bc85a26530e196a5f675dab6e06fb86b9104dc6fdc31cfc9ae1832d911" dependencies = [ "proc-macro-rules", "proc-macro2", @@ -5251,7 +5255,9 @@ dependencies = [ [[package]] name = "serde_v8" -version = "0.156.0" +version = "0.157.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8853e607c700ade0acb853aafda7e4cc1f752f4ebb35b0e2ecbf42e784e6d0e0" dependencies = [ "bytes", "derive_more", diff --git a/Cargo.toml b/Cargo.toml index 2354702abe3c39..33a8904e138734 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,7 +41,7 @@ repository = "https://github.com/denoland/deno" [workspace.dependencies] deno_ast = { version = "1.0.1", features = ["transpiling"] } -deno_core = { version = "0.247.0" } +deno_core = { version = "0.248.0" } deno_runtime = { version = "0.140.0", path = "./runtime" } napi_sym = { version = "0.62.0", path = "./cli/napi/sym" }