Skip to content

Commit 2563ace

Browse files
committed
feat(rust): Update the KidURI struct to match the formal spec
1 parent 83a293e commit 2563ace

File tree

11 files changed

+314
-131
lines changed

11 files changed

+314
-131
lines changed

.config/dictionaries/project.dic

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ coverallsapp
4949
cpus
5050
crontabs
5151
crontagged
52+
csprng
5253
cstring
5354
dalek
5455
dashmap
@@ -208,6 +209,8 @@ reqwest
208209
retriggering
209210
ristretto
210211
rlib
212+
rngs
213+
rsplit
211214
rulelist
212215
RULENAME
213216
runable

rust/signed_doc/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,21 @@ license.workspace = true
1111
workspace = true
1212

1313
[dependencies]
14+
cardano-blockchain-types = { version = "0.0.1", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "v0.0.11" }
1415
anyhow = "1.0.95"
1516
serde = { version = "1.0.217", features = ["derive"] }
1617
serde_json = "1.0.134"
1718
# TODO: Bump this to the latest version and fix the code
1819
jsonschema = "0.18.3"
1920
coset = "0.3.8"
2021
brotli = "7.0.0"
21-
ed25519-dalek = { version = "2.1.1", features = ["pem"] }
22+
ed25519-dalek = { version = "2.1.1", features = ["pem", "rand_core"] }
2223
uuid = { version = "1.11.0", features = ["v4", "v7", "serde"] }
2324
hex = "0.4.3"
25+
fluent-uri = "0.3.2"
26+
thiserror = "2.0.9"
27+
base64-url = "3.0.0"
2428

2529
[dev-dependencies]
2630
clap = { version = "4.5.23", features = ["derive", "env"] }
31+
rand = "0.8.5"

rust/signed_doc/examples/mk_signed_doc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use ed25519_dalek::{
1414
ed25519::signature::Signer,
1515
pkcs8::{DecodePrivateKey, DecodePublicKey},
1616
};
17-
use signed_doc::{DocumentRef, Kid, Metadata, UuidV7};
17+
use signed_doc::{DocumentRef, KidURI, Metadata, UuidV7};
1818

1919
fn main() {
2020
if let Err(err) = Cli::parse().exec() {
@@ -305,7 +305,7 @@ fn validate_cose(
305305
"COSE missing signature protected header `kid` field "
306306
);
307307

308-
let kid = Kid::try_from(key_id.as_ref())?;
308+
let kid = KidURI::try_from(key_id.as_ref())?;
309309
println!("Signature Key ID: {kid}");
310310
let data_to_sign = cose.tbs_data(&[], sign);
311311
let signature_bytes = sign.signature.as_slice().try_into().map_err(|_| {

rust/signed_doc/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mod metadata;
1212
mod signature;
1313

1414
pub use metadata::{DocumentRef, Metadata, UuidV7};
15-
pub use signature::Kid;
15+
pub use signature::KidURI;
1616

1717
/// Keep all the contents private.
1818
/// Better even to use a structure like this. Wrapping in an Arc means we don't have to
@@ -154,5 +154,4 @@ impl CatalystSignedDocument {
154154
pub fn doc_section(&self) -> Option<String> {
155155
self.inner.metadata.doc_section()
156156
}
157-
158157
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//! Errors returned by this type
2+
3+
use thiserror::Error;
4+
5+
use super::{key_rotation::KeyRotationError, role_index::RoleIndexError};
6+
7+
/// Errors that can occur when parsing a `KidURI`
8+
#[derive(Error, Debug)]
9+
pub enum KidURIError {
10+
/// Invalid KID URI
11+
#[error("Invalid URI")]
12+
InvalidURI(#[from] fluent_uri::error::ParseError),
13+
/// Invalid Scheme, not a KID URI
14+
#[error("Invalid Scheme, not a KID URI")]
15+
InvalidScheme,
16+
/// Network not defined in URI
17+
#[error("No defined Network")]
18+
NoDefinedNetwork,
19+
/// Path of URI is invalid
20+
#[error("Invalid Path")]
21+
InvalidPath,
22+
/// Role 0 Key in path is invalid
23+
#[error("Invalid Role 0 Key")]
24+
InvalidRole0Key,
25+
/// Role 0 Key in path is not encoded correctly
26+
#[error("Invalid Role 0 Key Encoding")]
27+
InvalidRole0KeyEncoding(#[from] base64_url::base64::DecodeError),
28+
/// Role Index is invalid
29+
#[error("Invalid Role")]
30+
InvalidRole,
31+
/// Role Index is not encoded correctly
32+
#[error("Invalid Role Index")]
33+
InvalidRoleIndex(#[from] RoleIndexError),
34+
/// Role Key Rotation is invalid
35+
#[error("Invalid Rotation")]
36+
InvalidRotation,
37+
/// Role Key Rotation is not encoded correctly
38+
#[error("Invalid Rotation Value")]
39+
InvalidRotationValue(#[from] KeyRotationError),
40+
/// Encryption key Identifier Fragment is not valid
41+
#[error("Invalid Encryption Key Fragment")]
42+
InvalidEncryptionKeyFragment,
43+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//! COSE Signature Protected Header `kid` Role0 Key Version.
2+
3+
use std::{
4+
fmt::{Display, Formatter},
5+
num::ParseIntError,
6+
str::FromStr,
7+
};
8+
9+
use thiserror::Error;
10+
11+
/// Errors from parsing the `KeyRotation`
12+
#[derive(Error, Debug)]
13+
#[allow(clippy::module_name_repetitions)]
14+
pub enum KeyRotationError {
15+
/// Key Rotation could not be parsed from a string
16+
#[error("Invalid Role Key Rotation")]
17+
InvalidRole(#[from] ParseIntError),
18+
}
19+
20+
/// Rotation count of the Role Key.
21+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
22+
pub struct KeyRotation(u16);
23+
24+
impl From<u16> for KeyRotation {
25+
fn from(value: u16) -> Self {
26+
Self(value)
27+
}
28+
}
29+
30+
impl FromStr for KeyRotation {
31+
type Err = KeyRotationError;
32+
33+
fn from_str(s: &str) -> Result<Self, Self::Err> {
34+
Ok(Self(s.parse::<u16>()?))
35+
}
36+
}
37+
38+
impl Display for KeyRotation {
39+
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
40+
write!(f, "{}", self.0)
41+
}
42+
}

rust/signed_doc/src/signature/kid/key_version.rs

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)