@contentauth/c2pa-node 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.lock +4901 -0
- package/Cargo.toml +38 -0
- package/dist/index.node +0 -0
- package/package.json +17 -5
- package/scripts/postinstall.js +212 -0
- package/src/asset.rs +114 -0
- package/src/error.rs +104 -0
- package/src/lib.rs +185 -0
- package/src/neon_builder.rs +562 -0
- package/src/neon_credential_holder.rs +149 -0
- package/src/neon_identity_assertion_builder.rs +211 -0
- package/src/neon_identity_assertion_signer.rs +191 -0
- package/src/neon_reader.rs +197 -0
- package/src/neon_signer.rs +509 -0
- package/src/neon_trustmark.rs +241 -0
- package/src/runtime.rs +47 -0
- package/src/settings.rs +487 -0
- package/src/utils.rs +30 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// Copyright 2025 Adobe. All rights reserved.
|
|
2
|
+
// This file is licensed to you under the Apache License,
|
|
3
|
+
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|
4
|
+
// or the MIT license (http://opensource.org/licenses/MIT),
|
|
5
|
+
// at your option.
|
|
6
|
+
|
|
7
|
+
// Unless required by applicable law or agreed to in writing,
|
|
8
|
+
// this software is distributed on an "AS IS" BASIS, WITHOUT
|
|
9
|
+
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
|
|
10
|
+
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
|
|
11
|
+
// specific language governing permissions and limitations under
|
|
12
|
+
// each license.
|
|
13
|
+
|
|
14
|
+
use async_trait::async_trait;
|
|
15
|
+
use c2pa::identity::builder::{AsyncCredentialHolder, IdentityBuilderError};
|
|
16
|
+
use c2pa::identity::SignerPayload;
|
|
17
|
+
use neon::prelude::*;
|
|
18
|
+
use neon::types::buffer::TypedArray;
|
|
19
|
+
use neon_serde4;
|
|
20
|
+
use std::sync::Arc;
|
|
21
|
+
use tokio::sync::oneshot;
|
|
22
|
+
|
|
23
|
+
/// NeonCallbackCredentialHolder allows JS to asynchronously sign a SignerPayload.
|
|
24
|
+
#[derive(Clone)]
|
|
25
|
+
pub struct NeonCallbackCredentialHolder {
|
|
26
|
+
channel: Channel,
|
|
27
|
+
// JS function: (payload: Buffer) => Promise<Buffer>
|
|
28
|
+
callback: Arc<Root<JsFunction>>,
|
|
29
|
+
reserve_size: usize,
|
|
30
|
+
sig_type: String,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
impl NeonCallbackCredentialHolder {
|
|
34
|
+
pub fn new(
|
|
35
|
+
channel: Channel,
|
|
36
|
+
callback: Arc<Root<JsFunction>>,
|
|
37
|
+
reserve_size: usize,
|
|
38
|
+
sig_type: String,
|
|
39
|
+
) -> Self {
|
|
40
|
+
Self {
|
|
41
|
+
channel,
|
|
42
|
+
callback,
|
|
43
|
+
reserve_size,
|
|
44
|
+
sig_type,
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
pub fn from_js(mut cx: FunctionContext) -> JsResult<JsBox<Self>> {
|
|
49
|
+
let reserve_size = cx.argument::<JsNumber>(0)?.value(&mut cx) as usize;
|
|
50
|
+
let sig_type = cx.argument::<JsString>(1)?.value(&mut cx);
|
|
51
|
+
let callback = cx.argument::<JsFunction>(2)?.root(&mut cx);
|
|
52
|
+
let channel = cx.channel();
|
|
53
|
+
Ok(cx.boxed(Self::new(
|
|
54
|
+
channel,
|
|
55
|
+
Arc::new(callback),
|
|
56
|
+
reserve_size,
|
|
57
|
+
sig_type,
|
|
58
|
+
)))
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/// # Safety
|
|
63
|
+
///
|
|
64
|
+
/// The implementations of `Send` and `Sync` are marked as `unsafe` because the compiler cannot automatically
|
|
65
|
+
/// verify that the type is safe to send or share between threads. This is because:
|
|
66
|
+
/// - The type contains `RwLock` fields (`signer` and `identity_assertions`)
|
|
67
|
+
/// - The type interacts with JavaScript through Neon bindings
|
|
68
|
+
/// - The type contains references to JavaScript objects and callbacks
|
|
69
|
+
///
|
|
70
|
+
/// However, the implementation is safe because:
|
|
71
|
+
/// - The `RwLock` fields provide thread-safe access to the contained data
|
|
72
|
+
/// - The JavaScript interactions are properly synchronized through the Neon runtime
|
|
73
|
+
/// - The type is used in a controlled environment where the JavaScript context is properly managed
|
|
74
|
+
/// - The type is used in a way that ensures proper synchronization of access to its resources
|
|
75
|
+
///
|
|
76
|
+
/// These traits are necessary because:
|
|
77
|
+
/// - `Send`: Allows the type to be transferred between threads
|
|
78
|
+
/// - `Sync`: Allows the type to be shared between threads
|
|
79
|
+
///
|
|
80
|
+
/// The type needs these capabilities because it:
|
|
81
|
+
/// - Implements `AsyncSigner` and `AsyncRawSigner` traits for asynchronous signing operations
|
|
82
|
+
/// - Is used in the `identity_sign_async` function which performs asynchronous operations
|
|
83
|
+
/// - Is part of a Node.js binding where async operations are common
|
|
84
|
+
unsafe impl Send for NeonCallbackCredentialHolder {}
|
|
85
|
+
unsafe impl Sync for NeonCallbackCredentialHolder {}
|
|
86
|
+
|
|
87
|
+
#[async_trait]
|
|
88
|
+
impl AsyncCredentialHolder for NeonCallbackCredentialHolder {
|
|
89
|
+
fn sig_type(&self) -> &'static str {
|
|
90
|
+
// This is safe because sig_type is stored as String and lives as long as self.
|
|
91
|
+
// We leak the string to get a &'static str.
|
|
92
|
+
Box::leak(self.sig_type.clone().into_boxed_str())
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
fn reserve_size(&self) -> usize {
|
|
96
|
+
self.reserve_size
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async fn sign(&self, signer_payload: &SignerPayload) -> Result<Vec<u8>, IdentityBuilderError> {
|
|
100
|
+
let (tx, rx) = oneshot::channel();
|
|
101
|
+
let callback = self.callback.clone();
|
|
102
|
+
let channel = self.channel.clone();
|
|
103
|
+
|
|
104
|
+
// Instead of CBOR, convert SignerPayload to a JS object
|
|
105
|
+
let payload = signer_payload.clone();
|
|
106
|
+
|
|
107
|
+
channel
|
|
108
|
+
.try_send(move |mut cx| {
|
|
109
|
+
// Convert Rust SignerPayload to JS object
|
|
110
|
+
let js_payload = match neon_serde4::to_value(&mut cx, &payload) {
|
|
111
|
+
Ok(val) => val,
|
|
112
|
+
Err(err) => return cx.throw_error(err.to_string()),
|
|
113
|
+
};
|
|
114
|
+
let js_fn = callback.to_inner(&mut cx);
|
|
115
|
+
let promise = js_fn
|
|
116
|
+
.call_with(&cx)
|
|
117
|
+
.arg(js_payload)
|
|
118
|
+
.apply::<JsPromise, _>(&mut cx)?
|
|
119
|
+
.to_future(&mut cx, |mut cx, result| match result {
|
|
120
|
+
Ok(value) => Ok(Ok(value
|
|
121
|
+
.downcast_or_throw::<JsBuffer, _>(&mut cx)?
|
|
122
|
+
.as_slice(&cx)
|
|
123
|
+
.to_vec())),
|
|
124
|
+
Err(err) => {
|
|
125
|
+
let js_string_result = err.to_string(&mut cx);
|
|
126
|
+
match js_string_result {
|
|
127
|
+
Ok(js_string) => {
|
|
128
|
+
let err_string: String = js_string.value(&mut cx);
|
|
129
|
+
Ok(Err(IdentityBuilderError::SignerError(err_string)))
|
|
130
|
+
}
|
|
131
|
+
Err(throw) => Err(throw),
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
})?;
|
|
135
|
+
let _ = tx.send(promise);
|
|
136
|
+
Ok(())
|
|
137
|
+
})
|
|
138
|
+
.map_err(|err| IdentityBuilderError::SignerError(err.to_string()))?;
|
|
139
|
+
|
|
140
|
+
let fut = rx
|
|
141
|
+
.await
|
|
142
|
+
.map_err(|err| IdentityBuilderError::SignerError(err.to_string()))?;
|
|
143
|
+
|
|
144
|
+
fut.await
|
|
145
|
+
.map_err(|err| IdentityBuilderError::SignerError(err.to_string()))?
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
impl Finalize for NeonCallbackCredentialHolder {}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// Copyright 2025 Adobe. All rights reserved.
|
|
2
|
+
// This file is licensed to you under the Apache License,
|
|
3
|
+
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|
4
|
+
// or the MIT license (http://opensource.org/licenses/MIT),
|
|
5
|
+
// at your option.
|
|
6
|
+
|
|
7
|
+
// Unless required by applicable law or agreed to in writing,
|
|
8
|
+
// this software is distributed on an "AS IS" BASIS, WITHOUT
|
|
9
|
+
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
|
|
10
|
+
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
|
|
11
|
+
// specific language governing permissions and limitations under
|
|
12
|
+
// each license.
|
|
13
|
+
|
|
14
|
+
use crate::neon_credential_holder::NeonCallbackCredentialHolder;
|
|
15
|
+
use c2pa::{
|
|
16
|
+
dynamic_assertion::{AsyncDynamicAssertion, DynamicAssertionContent},
|
|
17
|
+
identity::{builder::AsyncCredentialHolder, SignerPayload},
|
|
18
|
+
};
|
|
19
|
+
use neon::prelude::*;
|
|
20
|
+
use serde::{Deserialize, Serialize};
|
|
21
|
+
use serde_bytes::ByteBuf;
|
|
22
|
+
use std::ops::Deref;
|
|
23
|
+
use std::sync::RwLock;
|
|
24
|
+
|
|
25
|
+
/// A `NeonIdentityAssertionBuilder` gathers the necessary components
|
|
26
|
+
/// for an identity assertion using a Neon-based credential holder.
|
|
27
|
+
pub struct NeonIdentityAssertionBuilder {
|
|
28
|
+
credential_holder: RwLock<NeonCallbackCredentialHolder>,
|
|
29
|
+
referenced_assertions: RwLock<Vec<String>>,
|
|
30
|
+
roles: RwLock<Vec<String>>,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
#[derive(Deserialize, Serialize)]
|
|
34
|
+
pub struct IdentityAssertion {
|
|
35
|
+
pub(crate) signer_payload: SignerPayload,
|
|
36
|
+
|
|
37
|
+
#[serde(with = "serde_bytes")]
|
|
38
|
+
pub(crate) signature: Vec<u8>,
|
|
39
|
+
|
|
40
|
+
#[serde(with = "serde_bytes")]
|
|
41
|
+
pub(crate) pad1: Vec<u8>,
|
|
42
|
+
|
|
43
|
+
// Must use explicit ByteBuf here because #[serde(with = "serde_bytes")]
|
|
44
|
+
// does not work with Option<Vec<u8>>.
|
|
45
|
+
#[serde(skip_serializing_if = "Option::is_none")]
|
|
46
|
+
pub(crate) pad2: Option<ByteBuf>,
|
|
47
|
+
|
|
48
|
+
// Label for the assertion. Only assigned when reading from a manifest.
|
|
49
|
+
#[allow(dead_code)]
|
|
50
|
+
#[serde(skip)]
|
|
51
|
+
pub(crate) label: Option<String>,
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Note: unwrap is used on read() and write() results, as poisoning only occurs as a result of a
|
|
55
|
+
// panic on another thread.
|
|
56
|
+
impl Clone for NeonIdentityAssertionBuilder {
|
|
57
|
+
fn clone(&self) -> Self {
|
|
58
|
+
Self {
|
|
59
|
+
credential_holder: RwLock::new(self.credential_holder.read().unwrap().clone()),
|
|
60
|
+
referenced_assertions: RwLock::new(self.referenced_assertions.read().unwrap().clone()),
|
|
61
|
+
roles: RwLock::new(self.roles.read().unwrap().clone()),
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
impl NeonIdentityAssertionBuilder {
|
|
67
|
+
/// Create a `NeonIdentityAssertionBuilder` for the given JS credential holder.
|
|
68
|
+
pub fn for_credential_holder(mut cx: FunctionContext) -> JsResult<JsBox<Self>> {
|
|
69
|
+
let credential_holder_handle = cx.argument::<JsBox<NeonCallbackCredentialHolder>>(0)?;
|
|
70
|
+
let credential_holder_ref: &NeonCallbackCredentialHolder = credential_holder_handle.deref();
|
|
71
|
+
Ok(cx.boxed(Self {
|
|
72
|
+
credential_holder: RwLock::new(credential_holder_ref.clone()),
|
|
73
|
+
referenced_assertions: RwLock::new(vec![]),
|
|
74
|
+
roles: RwLock::new(vec![]),
|
|
75
|
+
}))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/// Add assertion labels to consider as referenced assertions.
|
|
79
|
+
pub fn add_referenced_assertions(mut cx: FunctionContext) -> JsResult<JsUndefined> {
|
|
80
|
+
let js_array = cx.argument::<JsArray>(0)?;
|
|
81
|
+
let this = cx.this::<JsBox<Self>>()?;
|
|
82
|
+
let mut referenced_assertions = this.referenced_assertions.write().unwrap();
|
|
83
|
+
let assertions: Vec<String> = js_array
|
|
84
|
+
.to_vec(&mut cx)?
|
|
85
|
+
.into_iter()
|
|
86
|
+
.map(|value| {
|
|
87
|
+
let js_string = value.downcast_or_throw::<JsString, _>(&mut cx)?;
|
|
88
|
+
Ok(js_string.value(&mut cx))
|
|
89
|
+
})
|
|
90
|
+
.collect::<Result<_, neon::result::Throw>>()?;
|
|
91
|
+
referenced_assertions.extend(assertions);
|
|
92
|
+
Ok(cx.undefined())
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/// Add roles to attach to the named actor for this identity assertion.
|
|
96
|
+
pub fn add_roles(mut cx: FunctionContext) -> JsResult<JsUndefined> {
|
|
97
|
+
let js_array = cx.argument::<JsArray>(0)?;
|
|
98
|
+
let this = cx.this::<JsBox<Self>>()?;
|
|
99
|
+
let mut roles = this.roles.write().unwrap();
|
|
100
|
+
let new_roles: Vec<String> = js_array
|
|
101
|
+
.to_vec(&mut cx)?
|
|
102
|
+
.into_iter()
|
|
103
|
+
.map(|value| {
|
|
104
|
+
let js_string = value.downcast_or_throw::<JsString, _>(&mut cx)?;
|
|
105
|
+
Ok(js_string.value(&mut cx))
|
|
106
|
+
})
|
|
107
|
+
.collect::<Result<_, neon::result::Throw>>()?;
|
|
108
|
+
roles.extend(new_roles);
|
|
109
|
+
Ok(cx.undefined())
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
impl Finalize for NeonIdentityAssertionBuilder {}
|
|
114
|
+
|
|
115
|
+
#[async_trait::async_trait]
|
|
116
|
+
impl AsyncDynamicAssertion for NeonIdentityAssertionBuilder {
|
|
117
|
+
fn label(&self) -> String {
|
|
118
|
+
"cawg.identity".to_string()
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
fn reserve_size(&self) -> c2pa::Result<usize> {
|
|
122
|
+
Ok(self.credential_holder.read().unwrap().reserve_size())
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async fn content(
|
|
126
|
+
&self,
|
|
127
|
+
_label: &str,
|
|
128
|
+
size: Option<usize>,
|
|
129
|
+
claim: &c2pa::dynamic_assertion::PartialClaim,
|
|
130
|
+
) -> c2pa::Result<c2pa::dynamic_assertion::DynamicAssertionContent> {
|
|
131
|
+
let referenced_assertions = claim
|
|
132
|
+
.assertions()
|
|
133
|
+
.filter(|a| {
|
|
134
|
+
// Always include hash data assertions
|
|
135
|
+
if a.url().contains("c2pa.assertions/c2pa.hash.") {
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
// Otherwise include if user-added label matches
|
|
139
|
+
let label = if let Some((_, label)) = a.url().rsplit_once('/') {
|
|
140
|
+
label.to_string()
|
|
141
|
+
} else {
|
|
142
|
+
a.url()
|
|
143
|
+
};
|
|
144
|
+
self.referenced_assertions.read().unwrap().contains(&label)
|
|
145
|
+
})
|
|
146
|
+
.cloned()
|
|
147
|
+
.collect();
|
|
148
|
+
|
|
149
|
+
let signer_payload = SignerPayload {
|
|
150
|
+
referenced_assertions,
|
|
151
|
+
sig_type: self
|
|
152
|
+
.credential_holder
|
|
153
|
+
.read()
|
|
154
|
+
.unwrap()
|
|
155
|
+
.sig_type()
|
|
156
|
+
.to_string(),
|
|
157
|
+
roles: self.roles.read().unwrap().clone(),
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
let credential_holder = self.credential_holder.read().unwrap().clone();
|
|
161
|
+
let signature = credential_holder
|
|
162
|
+
.sign(&signer_payload)
|
|
163
|
+
.await
|
|
164
|
+
.map_err(|e| c2pa::Error::OtherError(Box::new(e)))?;
|
|
165
|
+
|
|
166
|
+
finalize_identity_assertion(signer_payload, size, signature)
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
fn finalize_identity_assertion(
|
|
171
|
+
signer_payload: SignerPayload,
|
|
172
|
+
size: Option<usize>,
|
|
173
|
+
signature: Vec<u8>,
|
|
174
|
+
) -> c2pa::Result<DynamicAssertionContent> {
|
|
175
|
+
let mut ia = IdentityAssertion {
|
|
176
|
+
signer_payload,
|
|
177
|
+
signature,
|
|
178
|
+
pad1: vec![],
|
|
179
|
+
pad2: None,
|
|
180
|
+
label: None,
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
let mut assertion_cbor: Vec<u8> = vec![];
|
|
184
|
+
ciborium::into_writer(&ia, &mut assertion_cbor)
|
|
185
|
+
.map_err(|e| c2pa::Error::BadParam(e.to_string()))?;
|
|
186
|
+
// TO DO: Think through how errors map into crate::Error.
|
|
187
|
+
|
|
188
|
+
if let Some(assertion_size) = size {
|
|
189
|
+
if assertion_cbor.len() > assertion_size {
|
|
190
|
+
return Err(c2pa::Error::BadParam(format!("Serialized assertion is {len} bytes, which exceeds the planned size of {assertion_size} bytes", len = assertion_cbor.len())));
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
ia.pad1 = vec![0u8; assertion_size - assertion_cbor.len() - 15];
|
|
194
|
+
|
|
195
|
+
assertion_cbor.clear();
|
|
196
|
+
ciborium::into_writer(&ia, &mut assertion_cbor)
|
|
197
|
+
.map_err(|e| c2pa::Error::BadParam(e.to_string()))?;
|
|
198
|
+
|
|
199
|
+
ia.pad2 = Some(ByteBuf::from(vec![
|
|
200
|
+
0u8;
|
|
201
|
+
assertion_size - assertion_cbor.len() - 6
|
|
202
|
+
]));
|
|
203
|
+
|
|
204
|
+
assertion_cbor.clear();
|
|
205
|
+
ciborium::into_writer(&ia, &mut assertion_cbor)
|
|
206
|
+
.map_err(|e| c2pa::Error::BadParam(e.to_string()))?;
|
|
207
|
+
assert_eq!(assertion_size, assertion_cbor.len());
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
Ok(DynamicAssertionContent::Cbor(assertion_cbor))
|
|
211
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// Copyright 2025 Adobe. All rights reserved.
|
|
2
|
+
// This file is licensed to you under the Apache License,
|
|
3
|
+
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|
4
|
+
// or the MIT license (http://opensource.org/licenses/MIT),
|
|
5
|
+
// at your option.
|
|
6
|
+
|
|
7
|
+
// Unless required by applicable law or agreed to in writing,
|
|
8
|
+
// this software is distributed on an "AS IS" BASIS, WITHOUT
|
|
9
|
+
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
|
|
10
|
+
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
|
|
11
|
+
// specific language governing permissions and limitations under
|
|
12
|
+
// each license.
|
|
13
|
+
|
|
14
|
+
use crate::{
|
|
15
|
+
neon_identity_assertion_builder::NeonIdentityAssertionBuilder, neon_signer::NeonCallbackSigner,
|
|
16
|
+
};
|
|
17
|
+
use async_trait::async_trait;
|
|
18
|
+
use c2pa::{
|
|
19
|
+
crypto::{
|
|
20
|
+
raw_signature::{AsyncRawSigner, RawSignerError},
|
|
21
|
+
time_stamp::AsyncTimeStampProvider,
|
|
22
|
+
},
|
|
23
|
+
dynamic_assertion::AsyncDynamicAssertion,
|
|
24
|
+
AsyncSigner, SigningAlg,
|
|
25
|
+
};
|
|
26
|
+
use neon::prelude::FunctionContext;
|
|
27
|
+
use neon::prelude::*;
|
|
28
|
+
use std::ops::Deref;
|
|
29
|
+
use std::sync::RwLock;
|
|
30
|
+
|
|
31
|
+
pub struct NeonIdentityAssertionSigner {
|
|
32
|
+
signer: RwLock<NeonCallbackSigner>,
|
|
33
|
+
identity_assertions: RwLock<Vec<NeonIdentityAssertionBuilder>>,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
impl NeonIdentityAssertionSigner {
|
|
37
|
+
pub fn new(mut cx: FunctionContext) -> JsResult<JsBox<Self>> {
|
|
38
|
+
let signer_handle = cx.argument::<JsBox<NeonCallbackSigner>>(0)?;
|
|
39
|
+
let signer_ref: &NeonCallbackSigner = signer_handle.deref();
|
|
40
|
+
Ok(cx.boxed(Self {
|
|
41
|
+
signer: RwLock::new(signer_ref.clone()),
|
|
42
|
+
identity_assertions: RwLock::new(vec![]),
|
|
43
|
+
}))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
pub fn add_identity_assertion(mut cx: FunctionContext) -> JsResult<JsUndefined> {
|
|
47
|
+
let this = cx.this::<JsBox<Self>>()?;
|
|
48
|
+
let iab = cx.argument::<JsBox<NeonIdentityAssertionBuilder>>(0)?;
|
|
49
|
+
let iab_ref: &NeonIdentityAssertionBuilder = iab.deref();
|
|
50
|
+
let this_ref = this.deref();
|
|
51
|
+
this_ref
|
|
52
|
+
.identity_assertions
|
|
53
|
+
.write()
|
|
54
|
+
.unwrap()
|
|
55
|
+
.push(iab_ref.clone());
|
|
56
|
+
Ok(cx.undefined())
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
impl Clone for NeonIdentityAssertionSigner {
|
|
61
|
+
fn clone(&self) -> Self {
|
|
62
|
+
Self {
|
|
63
|
+
signer: RwLock::new(self.signer.read().unwrap().clone()),
|
|
64
|
+
identity_assertions: RwLock::new(self.identity_assertions.read().unwrap().clone()),
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/// # Safety
|
|
70
|
+
///
|
|
71
|
+
/// The implementations of `Send` and `Sync` are marked as `unsafe` because the compiler cannot automatically
|
|
72
|
+
/// verify that the type is safe to send or share between threads. This is because:
|
|
73
|
+
/// - The type contains `RwLock` fields (`signer` and `identity_assertions`)
|
|
74
|
+
/// - The type interacts with JavaScript through Neon bindings
|
|
75
|
+
/// - The type contains references to JavaScript objects and callbacks
|
|
76
|
+
///
|
|
77
|
+
/// However, the implementation is safe because:
|
|
78
|
+
/// - The `RwLock` fields provide thread-safe access to the contained data
|
|
79
|
+
/// - The JavaScript interactions are properly synchronized through the Neon runtime
|
|
80
|
+
/// - The type is used in a controlled environment where the JavaScript context is properly managed
|
|
81
|
+
/// - The type is used in a way that ensures proper synchronization of access to its resources
|
|
82
|
+
///
|
|
83
|
+
/// These traits are necessary because:
|
|
84
|
+
/// - `Send`: Allows the type to be transferred between threads
|
|
85
|
+
/// - `Sync`: Allows the type to be shared between threads
|
|
86
|
+
///
|
|
87
|
+
/// The type needs these capabilities because it:
|
|
88
|
+
/// - Implements `AsyncSigner` and `AsyncRawSigner` traits for asynchronous signing operations
|
|
89
|
+
/// - Is used in the `identity_sign_async` function which performs asynchronous operations
|
|
90
|
+
/// - Is part of a Node.js binding where async operations are common
|
|
91
|
+
unsafe impl Send for NeonIdentityAssertionSigner {}
|
|
92
|
+
unsafe impl Sync for NeonIdentityAssertionSigner {}
|
|
93
|
+
|
|
94
|
+
#[async_trait]
|
|
95
|
+
impl AsyncRawSigner for NeonIdentityAssertionSigner {
|
|
96
|
+
async fn sign(&self, data: Vec<u8>) -> Result<Vec<u8>, RawSignerError> {
|
|
97
|
+
let signer = self.signer.read().unwrap().clone();
|
|
98
|
+
AsyncRawSigner::sign(&signer, data).await
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
fn alg(&self) -> SigningAlg {
|
|
102
|
+
AsyncRawSigner::alg(&*self.signer.read().unwrap())
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
fn cert_chain(&self) -> Result<Vec<Vec<u8>>, RawSignerError> {
|
|
106
|
+
self.signer.read().unwrap().cert_chain()
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
fn reserve_size(&self) -> usize {
|
|
110
|
+
AsyncRawSigner::reserve_size(&*self.signer.read().unwrap())
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async fn ocsp_response(&self) -> Option<Vec<u8>> {
|
|
114
|
+
let signer = self.signer.read().unwrap().clone();
|
|
115
|
+
AsyncRawSigner::ocsp_response(&signer).await
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
impl AsyncTimeStampProvider for NeonIdentityAssertionSigner {
|
|
120
|
+
fn time_stamp_service_url(&self) -> Option<String> {
|
|
121
|
+
self.signer.read().unwrap().time_stamp_service_url()
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
fn time_stamp_request_headers(&self) -> Option<Vec<(String, String)>> {
|
|
125
|
+
self.signer.read().unwrap().time_stamp_request_headers()
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
#[async_trait]
|
|
130
|
+
impl AsyncSigner for NeonIdentityAssertionSigner {
|
|
131
|
+
async fn sign(&self, data: Vec<u8>) -> Result<Vec<u8>, c2pa::Error> {
|
|
132
|
+
let signer = self.signer.read().unwrap().clone();
|
|
133
|
+
c2pa::AsyncSigner::sign(&signer, data).await
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
fn alg(&self) -> SigningAlg {
|
|
137
|
+
c2pa::AsyncSigner::alg(&*self.signer.read().unwrap())
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
fn certs(&self) -> Result<Vec<Vec<u8>>, c2pa::Error> {
|
|
141
|
+
self.signer
|
|
142
|
+
.read()
|
|
143
|
+
.unwrap()
|
|
144
|
+
.cert_chain()
|
|
145
|
+
.map_err(|e| e.into())
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
fn reserve_size(&self) -> usize {
|
|
149
|
+
c2pa::AsyncSigner::reserve_size(&*self.signer.read().unwrap())
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async fn ocsp_val(&self) -> Option<Vec<u8>> {
|
|
153
|
+
let signer = self.signer.read().unwrap().clone();
|
|
154
|
+
c2pa::AsyncSigner::ocsp_val(&signer).await
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
fn time_authority_url(&self) -> Option<String> {
|
|
158
|
+
c2pa::AsyncSigner::time_authority_url(&*self.signer.read().unwrap())
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
fn timestamp_request_headers(&self) -> Option<Vec<(String, String)>> {
|
|
162
|
+
c2pa::AsyncSigner::timestamp_request_headers(&*self.signer.read().unwrap())
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
fn timestamp_request_body(&self, message: &[u8]) -> Result<Vec<u8>, c2pa::Error> {
|
|
166
|
+
c2pa::AsyncSigner::timestamp_request_body(&*self.signer.read().unwrap(), message)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async fn send_timestamp_request(&self, message: &[u8]) -> Option<Result<Vec<u8>, c2pa::Error>> {
|
|
170
|
+
let signer = self.signer.read().unwrap().clone();
|
|
171
|
+
c2pa::AsyncSigner::send_timestamp_request(&signer, message).await
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
fn async_raw_signer(&self) -> Option<Box<&dyn AsyncRawSigner>> {
|
|
175
|
+
Some(Box::new(self))
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
fn dynamic_assertions(&self) -> Vec<Box<dyn AsyncDynamicAssertion>> {
|
|
179
|
+
let mut identity_assertions = self.identity_assertions.write().unwrap();
|
|
180
|
+
let ia_clone = identity_assertions.split_off(0);
|
|
181
|
+
let mut dynamic_assertions: Vec<Box<dyn AsyncDynamicAssertion>> = vec![];
|
|
182
|
+
|
|
183
|
+
for ia in ia_clone.into_iter() {
|
|
184
|
+
dynamic_assertions.push(Box::new(ia));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
dynamic_assertions
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
impl Finalize for NeonIdentityAssertionSigner {}
|