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

fix: Don't take ownership of data when using AsyncSigner; use a reference instead #471

Closed
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
4 changes: 2 additions & 2 deletions sdk/src/callback_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ use async_trait::async_trait;
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
// I'm not sure if this is useful since the callback is still synchronous.
impl AsyncSigner for CallbackSigner {
async fn sign(&self, data: Vec<u8>) -> Result<Vec<u8>> {
(self.callback)(self.context, &data)
async fn sign(&self, data: &[u8]) -> Result<Vec<u8>> {
(self.callback)(self.context, data)
}

fn alg(&self) -> SigningAlg {
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/cose_sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ pub(crate) fn cose_sign(signer: &dyn Signer, data: &[u8], box_size: usize) -> Re
if _sync {
sign1.signature = signer.sign(&tbs)?;
} else {
sign1.signature = signer.sign(tbs).await?;
sign1.signature = signer.sign(&tbs).await?;
}

sign1.payload = None; // clear the payload since it is known
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/openssl/temp_signer_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ impl AsyncSignerAdapter {
#[cfg(feature = "openssl_sign")]
#[async_trait::async_trait]
impl crate::AsyncSigner for AsyncSignerAdapter {
async fn sign(&self, data: Vec<u8>) -> crate::error::Result<Vec<u8>> {
async fn sign(&self, data: &[u8]) -> crate::error::Result<Vec<u8>> {
let signer = get_local_signer(self.alg);
signer.sign(&data)
signer.sign(data)
}

fn alg(&self) -> SigningAlg {
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ use async_trait::async_trait;
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait AsyncSigner: Sync {
/// Returns a new byte array which is a signature over the original.
async fn sign(&self, data: Vec<u8>) -> Result<Vec<u8>>;
async fn sign(&self, data: &[u8]) -> Result<Vec<u8>>;

/// Returns the algorithm of the Signer.
fn alg(&self) -> SigningAlg;
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ impl Store {
} else {
if signer.direct_cose_handling() {
// Let the signer do all the COSE processing and return the structured COSE data.
return signer.sign(claim_bytes.clone()).await; // do not verify remote signers (we never did)
return signer.sign(&claim_bytes).await; // do not verify remote signers (we never did)
} else {
cose_sign_async(signer, &claim_bytes, box_size).await
}
Expand Down
12 changes: 6 additions & 6 deletions sdk/src/utils/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ pub(crate) struct AsyncTestGoodSigner {}
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl crate::AsyncSigner for AsyncTestGoodSigner {
async fn sign(&self, _data: Vec<u8>) -> Result<Vec<u8>> {
async fn sign(&self, _data: &[u8]) -> Result<Vec<u8>> {
Ok(b"not a valid signature".to_vec())
}

Expand Down Expand Up @@ -476,7 +476,7 @@ impl crate::signer::AsyncSigner for WebCryptoSigner {
Ok(self.certs.clone())
}

async fn sign(&self, claim_bytes: Vec<u8>) -> crate::error::Result<Vec<u8>> {
async fn sign(&self, claim_bytes: &[u8]) -> crate::error::Result<Vec<u8>> {
use js_sys::{Array, Object, Reflect, Uint8Array};
use wasm_bindgen_futures::JsFuture;
use web_sys::CryptoKey;
Expand All @@ -485,7 +485,7 @@ impl crate::signer::AsyncSigner for WebCryptoSigner {
let context = WindowOrWorker::new().unwrap();
let crypto = context.subtle_crypto().unwrap();

let mut data = claim_bytes.clone();
let mut data = claim_bytes;
let promise = crypto
.digest_with_str_and_u8_array("SHA-256", &mut data)
.unwrap();
Expand Down Expand Up @@ -542,14 +542,14 @@ struct TempAsyncRemoteSigner {
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl crate::signer::AsyncSigner for TempAsyncRemoteSigner {
// this will not be called but requires an implementation
async fn sign(&self, claim_bytes: Vec<u8>) -> Result<Vec<u8>> {
async fn sign(&self, claim_bytes: &[u8]) -> Result<Vec<u8>> {
#[cfg(feature = "openssl_sign")]
{
let signer =
crate::openssl::temp_signer_async::AsyncSignerAdapter::new(SigningAlg::Ps256);

// this would happen on some remote server
crate::cose_sign::cose_sign_async(&signer, &claim_bytes, self.reserve_size()).await
crate::cose_sign::cose_sign_async(&signer, claim_bytes, self.reserve_size()).await
}
#[cfg(not(feature = "openssl_sign"))]
{
Expand All @@ -558,7 +558,7 @@ impl crate::signer::AsyncSigner for TempAsyncRemoteSigner {
let mut sign_bytes = std::io::Cursor::new(vec![0u8; self.reserve_size()]);

sign_bytes.rewind()?;
sign_bytes.write_all(&claim_bytes)?;
sign_bytes.write_all(claim_bytes)?;

// fake sig
Ok(sign_bytes.into_inner())
Expand Down
Loading