Skip to content

Commit e45c952

Browse files
committed
Refactored to use a single library for JSON canonicalization.
Signed-off-by: Victor Embacher <[email protected]>
1 parent 6d51377 commit e45c952

File tree

5 files changed

+42
-38
lines changed

5 files changed

+42
-38
lines changed

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ ed25519-dalek = { version = "2.0.0-rc.2", features = ["pkcs8", "rand_core"] }
8383
elliptic-curve = { version = "0.13.5", features = ["arithmetic", "pem"] }
8484
lazy_static = "1.4.0"
8585
oci-distribution = { version = "0.10", default-features = false, optional = true }
86-
olpc-cjson = "0.1"
8786
openidconnect = { version = "3.0", default-features = false, features = [
8887
"reqwest",
8988
], optional = true }

src/cosign/bundle.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
use olpc_cjson::CanonicalFormatter;
16+
use json_syntax::Print;
1717
use serde::{Deserialize, Serialize};
1818
use std::cmp::PartialEq;
1919

@@ -78,17 +78,15 @@ impl Bundle {
7878
bundle: &Bundle,
7979
rekor_pub_key: &CosignVerificationKey,
8080
) -> Result<()> {
81-
let mut buf = Vec::new();
82-
let mut ser = serde_json::Serializer::with_formatter(&mut buf, CanonicalFormatter::new());
83-
bundle.payload.serialize(&mut ser).map_err(|e| {
84-
SigstoreError::UnexpectedError(format!(
85-
"Cannot create canonical JSON representation of bundle: {e:?}"
86-
))
81+
let mut body = json_syntax::to_value(&bundle.payload).map_err(|_| {
82+
SigstoreError::UnexpectedError("failed to serialize with json_syntax".to_string())
8783
})?;
84+
body.canonicalize();
85+
let encoded = body.compact_print().to_string();
8886

8987
rekor_pub_key.verify_signature(
9088
Signature::Base64Encoded(bundle.signed_entry_timestamp.as_bytes()),
91-
&buf,
89+
encoded.as_bytes(),
9290
)?;
9391
Ok(())
9492
}

src/registry/oci_caching_client.rs

+22-16
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::errors::{Result, SigstoreError};
1818

1919
use async_trait::async_trait;
2020
use cached::proc_macro::cached;
21-
use olpc_cjson::CanonicalFormatter;
21+
use json_syntax::Print;
2222
use serde::Serialize;
2323
use sha2::{Digest, Sha256};
2424
use tracing::{debug, error};
@@ -103,15 +103,18 @@ impl<'a> PullSettings<'a> {
103103
// Because of that the method will return the '0' value when something goes
104104
// wrong during the serialization operation. This is very unlikely to happen
105105
pub fn hash(&self) -> String {
106-
let mut buf = Vec::new();
107-
let mut ser = serde_json::Serializer::with_formatter(&mut buf, CanonicalFormatter::new());
108-
if let Err(e) = self.serialize(&mut ser) {
109-
error!(err=?e, settings=?self, "Cannot perform canonical serialization");
110-
return "0".to_string();
111-
}
106+
let mut body = match json_syntax::to_value(self) {
107+
Ok(body) => body,
108+
Err(_e) => {
109+
error!(err=?_e, settings=?self, "Cannot perform canonical serialization");
110+
return "0".to_string();
111+
}
112+
};
113+
body.canonicalize();
114+
let encoded = body.compact_print().to_string();
112115

113116
let mut hasher = Sha256::new();
114-
hasher.update(&buf);
117+
hasher.update(encoded.as_bytes());
115118
let result = hasher.finalize();
116119
result
117120
.iter()
@@ -194,15 +197,18 @@ impl PullManifestSettings {
194197
// Because of that the method will return the '0' value when something goes
195198
// wrong during the serialization operation. This is very unlikely to happen
196199
pub fn hash(&self) -> String {
197-
let mut buf = Vec::new();
198-
let mut ser = serde_json::Serializer::with_formatter(&mut buf, CanonicalFormatter::new());
199-
if let Err(e) = self.serialize(&mut ser) {
200-
error!(err=?e, settings=?self, "Cannot perform canonical serialization");
201-
return "0".to_string();
202-
}
200+
let mut body = match json_syntax::to_value(self) {
201+
Ok(body) => body,
202+
Err(_e) => {
203+
error!(err=?_e, settings=?self, "Cannot perform canonical serialization");
204+
return "0".to_string();
205+
}
206+
};
207+
body.canonicalize();
208+
let encoded = body.compact_print().to_string();
203209

204210
let mut hasher = Sha256::new();
205-
hasher.update(&buf);
211+
hasher.update(encoded.as_bytes());
206212
let result = hasher.finalize();
207213
result
208214
.iter()
@@ -243,7 +249,7 @@ async fn pull_manifest_cached(
243249
impl ClientCapabilitiesDeps for OciCachingClient {}
244250

245251
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
246-
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
252+
#[cfg_attr(target_arch = "wasm32", async_trait(? Send))]
247253
impl ClientCapabilities for OciCachingClient {
248254
async fn fetch_manifest_digest(
249255
&mut self,

src/rekor/models/checkpoint.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use base64::prelude::BASE64_STANDARD;
77
use base64::Engine;
88
use digest::Output;
99
use serde::{Deserialize, Deserializer, Serialize, Serializer};
10+
use std::fmt::Write;
1011
use std::fmt::{Display, Formatter};
1112
use std::str::FromStr;
1213

@@ -90,11 +91,10 @@ impl CheckpointNote {
9091
// Output is the part of the checkpoint that is signed.
9192
fn marshal(&self) -> String {
9293
let hash_b64 = BASE64_STANDARD.encode(self.hash);
93-
let other_content: String = self
94-
.other_content
95-
.iter()
96-
.map(|c| format!("{c}\n"))
97-
.collect();
94+
let other_content: String = self.other_content.iter().fold(String::new(), |mut acc, c| {
95+
writeln!(acc, "{c}").expect("failed to write to string");
96+
acc
97+
});
9898
format!(
9999
"{}\n{}\n{hash_b64}\n{other_content}",
100100
self.origin, self.size

src/rekor/models/log_entry.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::crypto::CosignVerificationKey;
2121
use crate::errors::SigstoreError::UnexpectedError;
2222
use crate::rekor::models::checkpoint::Checkpoint;
2323
use crate::rekor::models::InclusionProof as InclusionProof2;
24-
use olpc_cjson::CanonicalFormatter;
24+
use json_syntax::Print;
2525
use serde::{Deserialize, Serialize};
2626
use serde_json::{json, Error, Value};
2727
use std::collections::HashMap;
@@ -159,13 +159,14 @@ impl LogEntry {
159159
})
160160
.and_then(|proof| {
161161
// encode as canonical JSON
162-
let mut encoded_entry = Vec::new();
163-
let mut ser = serde_json::Serializer::with_formatter(
164-
&mut encoded_entry,
165-
CanonicalFormatter::new(),
166-
);
167-
self.body.serialize(&mut ser)?;
168-
proof.verify(&encoded_entry, rekor_key)
162+
let mut body = json_syntax::to_value(&self.body).map_err(|_| {
163+
SigstoreError::UnexpectedError(
164+
"failed to serialize with json_syntax".to_string(),
165+
)
166+
})?;
167+
body.canonicalize();
168+
let encoded_entry = body.compact_print().to_string();
169+
proof.verify(encoded_entry.as_bytes(), rekor_key)
169170
})
170171
}
171172
}

0 commit comments

Comments
 (0)